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