home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / WATTCP / NEWWATCP.ZIP / SRC / PCBOOTP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-15  |  6.2 KB  |  188 lines

  1. /*
  2.  *
  3.  *   BOOTP - Boot Protocol (RFC 854)
  4.  *
  5.  *   These extensions get called if _bootphost is set to an IP address or
  6.  *   to 0xffffffff.
  7.  *
  8.  *   Version
  9.  *
  10.  *   0.3 : Feb  1, 1992 : J. Dent - patched up various things
  11.  *   0.2 : May 22, 1991 : E.J. Sutcliffe - added RFC_1048 vendor fields
  12.  *   0.1 : May  9, 1991 : E. Engelke - made part of the library
  13.  *   0.0 : May  3, 1991 : E. Engelke - original program as an application
  14.  *
  15.  */
  16.  
  17. #include <copyright.h>
  18. #include <stdio.h>
  19. #include <wattcp.h>
  20. #include <mem.h>
  21. #include <bootp.h>
  22. #include <conio.h>
  23.  
  24. /* global variables */
  25. longword _bootphost = 0xffffffffL;
  26. word _bootptimeout = 30;
  27. word _bootpon = 0;
  28.  
  29. extern longword set_timeout();
  30.  
  31. #define VM_RFC1048 0x63825363L        /* I think this is correct */
  32.  
  33. /*
  34.  * _dobootpc - Checks global variables _bootptimeout, _bootphost
  35.  *             if no host specified, the broadcast address
  36.  *             returns 0 on success and sets ip address
  37.  */
  38. int _dobootp()
  39. {
  40.     udp_Socket bsock;
  41.     longword sendtimeout, bootptimeout;
  42.     word magictimeout;
  43.     word len, templen;
  44.     struct bootp sendbootp;     /* outgoing data */
  45.     struct bootp _bootp;        /* incoming data */
  46.     int status;
  47.     longword xid;
  48.     unsigned char *p,*t;
  49.  
  50. //    if ( _pktdevclass == PD_SLIP ) return( -1 );
  51.  
  52.  
  53.     /* We must get Waterloo TCP to use IP address 0 for sending */
  54.     xid = my_ip_addr;   /* a unique value coming from the ethernet card */
  55.     my_ip_addr = 0;
  56.  
  57.     if (!udp_open( &bsock, IPPORT_BOOTPC, _bootphost, IPPORT_BOOTPS, NULL )) {
  58.         outs("\n\rUnable to resolve bootp server\n\r");
  59.         return( -1 );
  60.     }
  61.  
  62.     bootptimeout = set_timeout( _bootptimeout );
  63.     magictimeout = (xid & 7) + 7;  /* between 7 and 14 seconds */
  64.  
  65.     memset( &sendbootp, 0, sizeof( struct bootp ));
  66.     sendbootp.bp_op = BOOTREQUEST;
  67.     sendbootp.bp_htype = _pktdevclass;
  68.     /* Copy into position the Magic Number used by Bootp */
  69.     /* avoid static storage and pushf/call assembler instructions */
  70.     *(longword *)(&sendbootp.bp_vend) = intel(VM_RFC1048);
  71.  
  72.     if (_pktdevclass == PD_ETHER) sendbootp.bp_hlen = 6;
  73.  
  74.     sendbootp.bp_xid = xid;
  75.     sendbootp.bp_secs = intel16( 1 );
  76.  
  77.     movmem( _eth_addr, &sendbootp.bp_chaddr, sizeof(eth_address));
  78.  
  79.     while ( 1 ) {
  80.     sock_fastwrite( (sock_type*)&bsock, (byte *)&sendbootp, sizeof( struct bootp ));
  81.         sendbootp.bp_secs = intel16( intel16( sendbootp.bp_secs ) + magictimeout );      /* for next time */
  82.         sendtimeout = set_timeout( magictimeout += (xid >> 5) & 7 );
  83.  
  84.         while ( !chk_timeout( sendtimeout )) {
  85.  
  86.             if (chk_timeout( bootptimeout))
  87.                 goto give_up;
  88.             kbhit();
  89.         sock_tick( (sock_type*)&bsock, &status );
  90.         if ((len = sock_dataready( (sock_type*)&bsock)) != 0 ) {
  91.  
  92.                 /* got a response, lets consider it */
  93.         templen = sock_fastread( (sock_type*)&bsock, (byte *)&_bootp, sizeof( struct bootp ));
  94.                 if ( templen < sizeof( struct bootp )) {
  95.                     /* too small, not a bootp packet */
  96.             memset( &_bootp, 0, sizeof( struct bootp ));
  97.                     continue;
  98.                 }
  99.  
  100.                 /* we must see if this is for us */
  101.         if (_bootp.bp_xid != sendbootp.bp_xid) {
  102.             memset( &_bootp, 0, sizeof( struct bootp ));
  103.                     continue;
  104.                 }
  105.  
  106.                 /* we must have found it */
  107.         my_ip_addr = intel( _bootp.bp_yiaddr );
  108.  
  109.  
  110.         if ( intel( *(longword*)(&_bootp.bp_vend)) == VM_RFC1048 ) {
  111.             /*RFC1048 complient BOOTP vendor field */
  112.             /* Based heavily on NCSA Telnet BOOTP */
  113.  
  114.             p = &_bootp.bp_vend[4]; /* Point just after vendor field */
  115.  
  116.                     while ((*p!=255) && (p <= &_bootp.bp_vend[63])) {
  117.             switch(*p) {
  118.                           case 0: /* Nop Pad character */
  119.                                  p++;
  120.                                  break;
  121.                           case 1: /* Subnet Mask */
  122.                  sin_mask = intel( *(longword *)( &p[2] ));
  123.                  /* and fall through */
  124.               case 2: /* Time offset */
  125.                  p += *(p+1) + 2;
  126.                  break;
  127.               case 3: /* gateways */
  128.                   /* only add first */
  129.                   _arp_add_gateway( NULL,
  130.                      intel( *(longword*)(&p[2])));
  131.                                   p +=*(p+1)+2;
  132.                                   break;
  133.                   /* and fall through */
  134.               case 4: /*time servers */
  135.                   /* fall through */
  136.                           case 5: /* IEN=116 name server */
  137.                                  p +=*(p+1)+2;
  138.                                  break;
  139.               case 6: /* Domain Name Servers (BIND) */
  140.                 for ( len = 0; len < *(p+1) ; len += 4 )
  141.                     _add_server( &_last_nameserver,
  142.                     MAX_NAMESERVERS, def_nameservers,
  143.                         intel( *(longword*)(&p[2+len])));
  144.                 /* and fall through */
  145.               case 7: /* log server */
  146.                  p += *(p+1)+2;
  147.                  break;
  148.               case 8: /* cookie server */
  149.                  for ( len = 0; len < *(p+1) ; len += 4 )
  150.                      _add_server( &_last_cookie, MAX_COOKIES,
  151.                     _cookie, intel( *(longword*)(&p[2+len])));
  152.                                   /* and fall through */
  153.                                   p +=*(p+1)+2;
  154.                                   break;
  155.                           case 9: /* lpr server */
  156.                           case 10: /* impress server */
  157.                           case 11: /* rlp server */
  158.                                    p +=*(p+1)+2;
  159.                                    break;
  160.               case 12: /* Client Hostname */
  161.                   movmem( &p[2] , _hostname, MAX_STRING );
  162.                   _hostname[ MAX_STRING - 1 ] = 0;
  163.                   p += *(p+1)+2;
  164.                                   break;
  165.                           case 255:
  166.                                    break;
  167.                           default:
  168.                                    p +=*(p+1)+2;
  169.                                    break;
  170.                         } /* end of switch */
  171.                      } /* end of while */
  172.                 }/* end of RFC_1048 if */
  173.                 goto give_up;
  174.             }
  175.         }
  176.     }
  177. give_up:
  178.  
  179.     sock_close( (sock_type *)&bsock );
  180.  
  181.     return (my_ip_addr == 0 );  /* return 0 on success */
  182.  
  183. sock_err:
  184.     /* major network error if UDP fails */
  185.     sock_close( (sock_type *)&bsock );
  186.     return( -1 );
  187. }
  188.