home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / CEREBRUM / WAT9609.ZIP / SRC / PCARP.C < prev    next >
Text File  |  1994-12-01  |  10KB  |  319 lines

  1. /*
  2.  * Address Resolution Protocol
  3.  *
  4.  *  Externals:
  5.  *  _arp_handler( pb ) - returns 1 on handled correctly, 0 on problems
  6.  *  _arp_resolve - rets 1 on success, 0 on fail
  7.  *               - does not return hardware address if passed NULL for buffer
  8.  *
  9.  */
  10. #include <copyright.h>
  11. #include <wattcp.h>
  12. #include <string.h>
  13. #include <mem.h>
  14.  
  15. #define MAX_ARP_DATA 20
  16. #define MAX_ARP_ALIVE  300 /* five minutes */
  17. #define MAX_ARP_GRACE  100 /* additional grace upon expiration */
  18.  
  19. extern word wathndlcbrk;
  20. extern word watcbroke;
  21. extern word multihomes;
  22.  
  23. typedef struct {
  24.     longword        ip;
  25.     eth_address        hardware;
  26.     byte        flags;
  27.     byte        bits;        /* bits in network */
  28.     longword        expiry;
  29. } arp_tables;
  30.  
  31. typedef struct {
  32.     longword        gate_ip;
  33.     longword        subnet;
  34.     longword        mask;
  35. } gate_tables;
  36.  
  37. #define ARP_FLAG_NEED    0
  38. #define ARP_FLAG_FOUND  1
  39. #define ARP_FLAG_FIXED  255    /* cannot be removed */
  40.  
  41.  
  42. /*
  43.  * arp resolution cache - we zero fill it to save an initialization routine
  44.  */
  45. static arp_tables arp_data[ MAX_ARP_DATA ] =
  46.  { {0uL,{{0,0,0,0,0,0}},0,0,0uL}, {0uL,{{0,0,0,0,0,0}},0,0,0uL}, {0uL,{{0,0,0,0,0,0}},0,0,0uL},
  47.    {0uL,{{0,0,0,0,0,0}},0,0,0uL}, {0uL,{{0,0,0,0,0,0}},0,0,0uL}, {0uL,{{0,0,0,0,0,0}},0,0,0uL},
  48.    {0uL,{{0,0,0,0,0,0}},0,0,0uL}, {0uL,{{0,0,0,0,0,0}},0,0,0uL}, {0uL,{{0,0,0,0,0,0}},0,0,0uL},
  49.    {0uL,{{0,0,0,0,0,0}},0,0,0uL}, {0uL,{{0,0,0,0,0,0}},0,0,0uL}, {0uL,{{0,0,0,0,0,0}},0,0,0uL},
  50.    {0uL,{{0,0,0,0,0,0}},0,0,0uL}, {0uL,{{0,0,0,0,0,0}},0,0,0uL}, {0uL,{{0,0,0,0,0,0}},0,0,0uL},
  51.    {0uL,{{0,0,0,0,0,0}},0,0,0uL}, {0uL,{{0,0,0,0,0,0}},0,0,0uL}, {0uL,{{0,0,0,0,0,0}},0,0,0uL},
  52.    {0uL,{{0,0,0,0,0,0}},0,0,0uL}, {0uL,{{0,0,0,0,0,0}},0,0,0uL}};
  53.  
  54. gate_tables _arp_gate_data[ MAX_GATE_DATA ];
  55. word _arp_last_gateway;
  56.  
  57. /*
  58.  * _arp_add_gateway - if data is NULL, don't use string
  59.  */
  60. void _arp_add_gateway( char *data, longword ip )
  61. {
  62.     int i;
  63.     char *subnetp, *maskp;
  64.     longword subnet, mask;
  65.  
  66.     subnet = mask = 0;
  67.     if ( data ) {
  68.     maskp = NULL;
  69.     if ( (subnetp = strchr( data, ',' )) != NULL ) {
  70.         *subnetp++ = 0;
  71.         if ( (maskp = strchr( subnetp, ',' )) != NULL ) {
  72.         *maskp++ = 0;
  73.         mask = aton( maskp );
  74.         subnet = aton( subnetp );
  75.         } else {
  76.         subnet = aton( subnetp );
  77.         switch ( subnet >> 30 ) {
  78.             case 0 :
  79.             case 1 : mask = 0xff000000L; break;
  80.             case 2 : mask = 0xfffffe00L; break;    /* minimal class b */
  81.             case 3 : mask = 0xffffff00L; break;
  82.         }
  83.         }
  84.     }
  85.         ip = aton( data );
  86.     }
  87.  
  88.     if ( _arp_last_gateway < MAX_GATE_DATA ) {
  89.         for ( i = 0 ; i < _arp_last_gateway ; ++i ) {
  90.             if ( _arp_gate_data[i].mask < mask ) {
  91.                 movmem( &_arp_gate_data[i], &_arp_gate_data[i+1],
  92.                     (_arp_last_gateway - i) * sizeof( gate_tables ));
  93.                 break;
  94.             }
  95.         }
  96.         _arp_gate_data[i].gate_ip = ip;
  97.         _arp_gate_data[i].subnet = subnet;
  98.         _arp_gate_data[i].mask = mask;
  99.         ++_arp_last_gateway;    /* used up another one */
  100.     }
  101. }
  102.  
  103. static void _arp_request( longword ip )
  104. {
  105.     arp_Header *op;
  106.  
  107.     op = (arp_Header *)_eth_formatpacket(&_eth_brdcast, 0x608);
  108.     op->hwType = arp_TypeEther;
  109.     op->protType = 0x008;        /* IP */
  110.     op->hwProtAddrLen = sizeof(eth_address) + (sizeof(longword)<<8);
  111.     op->opcode = ARP_REQUEST;
  112.     op->srcIPAddr = intel( my_ip_addr );
  113.     movmem(&_eth_addr, &op->srcEthAddr, sizeof(eth_address));
  114.     op->dstIPAddr = intel( ip );
  115.  
  116.     /* ...and send the packet */
  117.     _eth_send( sizeof(arp_Header) );
  118. }
  119.  
  120. static word arp_index = 0;        /* rotates round-robin */
  121.  
  122. static arp_tables *_arp_search( longword ip, int create )
  123. {
  124.     int i;
  125.     arp_tables *arp_ptr;
  126.  
  127.     for ( i = 0; i < MAX_ARP_DATA; ++i ) {
  128.     if ( ip == arp_data[i].ip )
  129.         return( &arp_data[i] );
  130.     }
  131.  
  132.     /* didn't find any */
  133.     if ( create ) {
  134.     /* pick an old or empty one */
  135.     for ( i = 0; i < MAX_ARP_DATA ; ++i ) {
  136.         arp_ptr = &arp_data[i];
  137.         if ( ! arp_ptr->ip || chk_timeout(arp_ptr->expiry+MAX_ARP_GRACE))
  138.         return( arp_ptr );
  139.     }
  140.  
  141.     /* pick one at pseudo-random */
  142.     return( &arp_data[ arp_index = ( arp_index + 1 ) % MAX_ARP_DATA ] );
  143.     }
  144.     return( NULL );
  145. }
  146.  
  147. void _arp_register( longword use, longword instead_of )
  148. {
  149. /*    word i; */
  150.     arp_tables *arp_ptr;
  151.  
  152.     if ( (arp_ptr = _arp_search( instead_of, 0 )) != NULL) {
  153.     /* now insert the address of the new guy */
  154.     arp_ptr->flags = ARP_FLAG_NEED;
  155.         _arp_resolve( use, &arp_ptr->hardware, 0);
  156.     arp_ptr->expiry = set_timeout( MAX_ARP_ALIVE );
  157.     return;
  158.     }
  159.  
  160.     arp_ptr = _arp_search( use , 1 );    /* create a new one */
  161.     arp_ptr->flags = ARP_FLAG_NEED;
  162. /* but now is this right?  if 'use' was already in the arp cache,
  163.      we're now nuking it... at worst an efficiency problem */
  164.     arp_ptr->ip = instead_of;  /* use; */               /* 94.11.30 */
  165.     _arp_resolve( use, &arp_ptr->hardware, 0);
  166.     arp_ptr->expiry = set_timeout( MAX_ARP_ALIVE );
  167. }
  168.  
  169. void _arp_tick( longword ip )
  170. {
  171.     arp_tables *arp_ptr;
  172.  
  173.     if ( (arp_ptr = _arp_search( ip , 0)) != NULL )
  174.     arp_ptr->expiry = set_timeout( MAX_ARP_ALIVE );
  175. }
  176.  
  177. /*
  178.  * _arp_handler - handle incomming ARP packets
  179.  */
  180. int _arp_handler( arp_Header *in)
  181. {
  182.     arp_Header *op;
  183.     longword his_ip;
  184.     arp_tables *arp_ptr;
  185.  
  186.     if ( in->hwType != arp_TypeEther ||      /* have ethernet hardware, */
  187.     in->protType != 8 )              /* and internet software, */
  188.     return( 0 );
  189.  
  190.     /* continuously accept data - but only for people we talk to */
  191.     his_ip = intel( in->srcIPAddr );
  192.  
  193.     if ( (arp_ptr = _arp_search( his_ip, 0)) != NULL ) {
  194.     arp_ptr->expiry = set_timeout( MAX_ARP_ALIVE );
  195.     movmem( &in->srcEthAddr, &arp_ptr->hardware, sizeof( eth_address ));
  196.     arp_ptr->flags = ARP_FLAG_FOUND;
  197.     }
  198.  
  199.     /* does someone else want our Ethernet address ? */
  200.     if ( in->opcode == ARP_REQUEST &&        /* and be a resolution req. */
  201.      ((longword)(intel(in->dstIPAddr) - my_ip_addr ) <= multihomes )
  202.        )  {
  203.     op = (arp_Header *)_eth_formatpacket(&in->srcEthAddr, 0x0608);
  204.     op->hwType = arp_TypeEther;
  205.     op->protType = 0x008;            /* intel for ip */
  206.     op->hwProtAddrLen = sizeof(eth_address) + (sizeof(longword) << 8 );
  207.     op->opcode = ARP_REPLY;
  208.  
  209.     op->dstIPAddr = in->srcIPAddr;
  210.     op->srcIPAddr = in->dstIPAddr;
  211.     movmem(&_eth_addr, &op->srcEthAddr, sizeof(eth_address));
  212.     movmem(&in->srcEthAddr, &op->dstEthAddr, sizeof(eth_address));
  213.     _eth_send(sizeof(arp_Header));
  214.     return ( 1 );
  215.     }
  216.     return( 1 );
  217. }
  218.  
  219.  
  220. /*
  221.  * _arp_resolve - resolve IP address to hardware address
  222.  */
  223. int _arp_resolve( longword ina, eth_address *ethap, int nowait )
  224. {
  225.     static arp_tables *arp_ptr;
  226.     int i, oldhndlcbrk;
  227.     longword timeout, resend;
  228. /*    int packettype; */
  229.  
  230.     if ( _pktdevclass == PD_SLIP ) {
  231.     /* we are running slip or somthing which does not use addresses */
  232.     return( 1 );
  233.     }
  234.  
  235.     if ( (longword)(ina - my_ip_addr) < multihomes) {
  236.     if (ethap)
  237.         movmem( &_eth_addr, ethap, sizeof( eth_address ));
  238.     return( 1 );
  239.     }
  240.  
  241.     /* attempt to solve with ARP cache */
  242.     /* fake while loop */
  243.     while ( (arp_ptr = _arp_search( ina, 0)) != NULL ) {
  244.     if ( arp_ptr->flags != ARP_FLAG_NEED ) {
  245.         /* has been resolved */
  246. #ifdef NEW_EXPIRY
  247.         if ( chk_timeout( arp_ptr->timeout ) {
  248.         if ( ! chk_timeout( arp_ptr->timeout + MAX_ARP_GRACE ) {
  249.             /* we wish to refresh it asynchronously */
  250.             _arp_request( ina );
  251.         else
  252.             break;    /* must do full fledged arp */
  253. #endif NEW_EXPIRY
  254.         if (ethap)
  255.         movmem( &arp_ptr->hardware, ethap, sizeof(eth_address));
  256.         return( 1 );
  257.     }
  258.         break;
  259.     }
  260.  
  261.     /* make a new one if necessary */
  262.     if (! arp_ptr )
  263.     arp_ptr = _arp_search( ina, 1 );
  264.  
  265.     /* we must look elsewhere - but is it on our subnet??? */
  266.     if (( ina ^ my_ip_addr ) & sin_mask ) {
  267.  
  268.     /* not of this network */
  269.     for ( i = 0; i < _arp_last_gateway ; ++i ) {
  270.  
  271.             //  is the gateway on our subnet
  272.             //  or if mask is ff...ff, we assume any gateway must succeed
  273.             //  because we are on 'no' network
  274.  
  275.             if ((((_arp_gate_data[i].gate_ip ^ my_ip_addr ) & sin_mask ) == 0)
  276.              || ( sin_mask == 0xffffffffL )) {
  277.                 /* compare the various subnet bits */
  278.                 if ( (_arp_gate_data[i].mask & ina ) == _arp_gate_data[i].subnet ) {
  279.                     if ( _arp_resolve( _arp_gate_data[i].gate_ip , ethap, nowait ))
  280.                         return( 1 );
  281.                 }
  282.         }
  283.     }
  284.         return( 0 );
  285.     }
  286.  
  287.     /* return if no host, or no gateway */
  288.     if (! ina )
  289.     return( 0 );
  290.  
  291.     /* is on our subnet, we must resolve */
  292.     timeout = set_timeout( 5 );        /* five seconds is long for ARP */
  293.     oldhndlcbrk = wathndlcbrk;
  294.     wathndlcbrk = 1;
  295.     watcbroke = 0;
  296.     while ( !chk_timeout( timeout )) {
  297.     /* do the request */
  298.     _arp_request( arp_ptr->ip = ina );
  299.     resend = set_timeout( 1 ) - 14L;    /* 250 ms */
  300.     while (!chk_timeout( resend )) {
  301.             if (watcbroke) goto fail;
  302.         tcp_tick( NULL );
  303.         if ( arp_ptr->flags) {
  304.         if (ethap)
  305.             movmem( &arp_ptr->hardware, ethap, sizeof(eth_address));
  306.         arp_ptr->expiry = set_timeout( MAX_ARP_ALIVE );
  307.                 watcbroke = 0;
  308.                 wathndlcbrk = oldhndlcbrk;
  309.         return ( 1 );
  310.         }
  311.     }
  312.         if ( nowait ) goto fail;
  313.     }
  314. fail:
  315.     watcbroke = 0;
  316.     wathndlcbrk = oldhndlcbrk;
  317.     return ( 0 );
  318. }
  319.