home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / WATTCP / WNWATTCP.ZIP / SRC / OLDBOOTP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-17  |  3.3 KB  |  118 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.1 : May  9, 1991 : E. Engelke - made part of the library
  11.  *   0.0 : May  3, 1991 : E. Engelke - original program as an application
  12.  *
  13.  */
  14.  
  15. #include <copyright.h>
  16. #include <stdio.h>
  17. #include <wattcp.h>
  18. #include <bootp.h>
  19.  
  20. /* global variables */
  21. longword _bootphost = 0L;
  22. word _bootptimeout = 30;
  23.  
  24.  
  25. extern longword set_timeout();
  26.  
  27. /*
  28.  * _dobootpc - Checks global variables _bootptimeout, _bootphost
  29.  *             if no host specified, the broadcast address
  30.  *             returns 0 on success and sets ip address
  31.  */
  32. int _dobootp()
  33. {
  34.     udp_Socket bsock;
  35.     longword sendtimeout, bootptimeout;
  36.     word magictimeout;
  37.     word len, templen;
  38.     struct bootp sendbootp;     /* outgoing data */
  39.     struct bootp _bootp;        /* incoming data */
  40.     int found;
  41.     int status;
  42.     longword xid;
  43.  
  44.     if ( _pktdevclass == PD_SLIP ) return( -1 );
  45.  
  46.     found = 0;
  47.  
  48.     /* We must get Waterloo TCP to use IP address 0 for sending */
  49.     xid = my_ip_addr;   /* a unique value coming from the ethernet card */
  50.     my_ip_addr = 0;
  51.  
  52.     if (!udp_Open( &bsock, IPPORT_BOOTPC, _bootphost, IPPORT_BOOTPS, NULL )) {
  53.         outs("\n\rUnable to resolve bootp server\n\r");
  54.         return( -1 );
  55.     }
  56.  
  57.     bootptimeout = set_timeout( _bootptimeout );
  58.     magictimeout = (xid & 7) + 7;  /* between 7 and 14 seconds */
  59.  
  60.     memset( &sendbootp, 0, sizeof( struct bootp ));
  61.     sendbootp.bp_op = BOOTREQUEST;
  62.     sendbootp.bp_htype = _pktdevclass;
  63.  
  64.     if (_pktdevclass == PD_ETHER) sendbootp.bp_hlen = 6;
  65.  
  66.     sendbootp.bp_xid = xid;
  67.     sendbootp.bp_secs = intel16( 1 );
  68.  
  69.     movmem( _eth_addr, &sendbootp.bp_chaddr, sizeof(eth_address));
  70.  
  71.  
  72.     while ( 1 ) {
  73.         sock_FastWrite( &bsock, &sendbootp, sizeof( struct bootp ));
  74.         sendbootp.bp_secs += magictimeout;      /* for next time */
  75.         sendtimeout = set_timeout( magictimeout += (xid > 5) & 7 );
  76.  
  77.         while ( !chk_timeout( sendtimeout )) {
  78.  
  79.             if (chk_timeout( bootptimeout))
  80.                 goto give_up;
  81.  
  82.             sock_tick( &bsock, &status );
  83.             if (len = sock_DataReady( &bsock)) {
  84.  
  85.                 /* got a response, lets consider it */
  86.                 templen = sock_FastRead( &bsock, &_bootp, sizeof( struct bootp ));
  87.                 if ( len < sizeof( struct bootp )) {
  88.                     /* too small, not a bootp packet */
  89.                     memset( _bootp, 0, sizeof( struct bootp ));
  90.                     continue;
  91.                 }
  92.  
  93.                 /* we must see if this is for us */
  94.                 if (_bootp.bp_xid != sendbootp.bp_xid) {
  95.                     outs("Got someone else's xid ");
  96.                     continue;
  97.                 }
  98.  
  99.                 /* we must have found it */
  100.                 my_ip_addr = intel( _bootp.bp_yiaddr );
  101.         _bootphost = intel( _bootp.bp_siaddr );
  102.                 if (!sin_gate) sin_gate = intel( _bootp.bp_giaddr );
  103.                 goto give_up;
  104.             }
  105.         }
  106.     }
  107. give_up:
  108.  
  109.     sock_Close( &bsock );
  110.  
  111.     return (my_ip_addr == 0 );  /* return 0 on success */
  112.  
  113. sock_err:
  114.     /* major network error if UDP fails */
  115.     sock_Close( &bsock );
  116.     return( -1 );
  117. }
  118.