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