home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / WATTCP / UNZIPPED / WNWATTCP / SRC / PCARP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-06  |  8.9 KB  |  312 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.  
  14. #define MAX_ARP_DATA 40
  15. #define MAX_ARP_ALIVE  300 /* five minutes */
  16. #define MAX_ARP_GRACE  100 /* additional grace upon expiration */
  17.  
  18. extern word wathndlcbrk;
  19. extern word watcbroke;
  20.  
  21.  
  22. typedef struct {
  23.     longword        ip;
  24.     eth_address        hardware;
  25.     byte        flags;
  26.     byte        bits;        /* bits in network */
  27.     longword        expiry;
  28. } arp_tables;
  29.  
  30. typedef struct {
  31.     longword        gate_ip;
  32.     longword        subnet;
  33.     longword        mask;
  34. } gate_tables;
  35.  
  36. #define ARP_FLAG_NEED    0
  37. #define ARP_FLAG_FOUND  1
  38. #define ARP_FLAG_FIXED  255    /* cannot be removed */
  39.  
  40.  
  41. /*
  42.  * arp resolution cache - we zero fill it to save an initialization routine
  43.  */
  44. static arp_tables arp_data[ MAX_ARP_DATA ] =
  45.  { {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0},
  46.    {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0},
  47.    {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0},
  48.    {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0},
  49.    {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0},
  50.    {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0},
  51.    {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0},
  52.    {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0},
  53.    {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0},
  54.    {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}, {0,{0,0,0},0,0,0}};
  55.  
  56. gate_tables _arp_gate_data[ MAX_GATE_DATA ];
  57. word _arp_last_gateway;
  58.  
  59. /*
  60.  * _arp_add_gateway - if data is NULL, don't use string
  61.  */
  62. void _arp_add_gateway( char *data , longword ip )
  63. {
  64.     int i;
  65.     char *subnetp, *maskp;
  66.     longword subnet, mask;
  67.  
  68.     subnet = mask = 0;
  69.     if ( data ) {
  70.     maskp = NULL;
  71.     if ( subnetp = strchr( data, ',' ) ) {
  72.         *subnetp++ = 0;
  73.         if ( maskp = strchr( subnetp, ',' )) {
  74.         *maskp++ = 0;
  75.         mask = aton( maskp );
  76.         subnet = aton( subnetp );
  77.         } else {
  78.         subnet = aton( subnetp );
  79.         switch ( subnet >> 30 ) {
  80.             case 0 :
  81.             case 1 : mask = 0xff000000L; break;
  82.             case 2 : mask = 0xfffffe00L; break;    /* minimal class b */
  83.             case 3 : mask = 0xffffff00L; break;
  84.         }
  85.         }
  86.     }
  87.     }
  88.     ip = aton( data );
  89.  
  90.     if ( _arp_last_gateway < MAX_GATE_DATA ) {
  91.         for ( i = 0 ; i < _arp_last_gateway ; ++i ) {
  92.             if ( _arp_gate_data[i].mask < mask ) {
  93.                 movmem( &_arp_gate_data[i], &_arp_gate_data[i+1],
  94.                     (_arp_last_gateway - i) * sizeof( gate_tables ));
  95.                 break;
  96.             }
  97.         }
  98.         _arp_gate_data[i].gate_ip = ip;
  99.         _arp_gate_data[i].subnet = subnet;
  100.         _arp_gate_data[i].mask = mask;
  101.         ++_arp_last_gateway;    /* used up another one */
  102.     }
  103. }
  104.  
  105. static void _arp_request( longword ip )
  106. {
  107.     arp_Header *op;
  108.  
  109.     op = (arp_Header *)_eth_formatpacket(&_eth_brdcast[0], 0x608);
  110.     op->hwType = arp_TypeEther;
  111.     op->protType = 0x008;        /* IP */
  112.     op->hwProtAddrLen = sizeof(eth_address) + (sizeof(longword)<<8);
  113.     op->opcode = ARP_REQUEST;
  114.     op->srcIPAddr = intel( my_ip_addr );
  115.     movmem(_eth_addr, op->srcEthAddr, sizeof(eth_address));
  116.     op->dstIPAddr = intel( ip );
  117.  
  118.     /* ...and send the packet */
  119.     _eth_send( sizeof(arp_Header) );
  120. }
  121.  
  122. static word arp_index = 0;        /* rotates round-robin */
  123.  
  124.  
  125. static arp_tables *_arp_search( longword ip, int create )
  126. {
  127.     int i;
  128.     arp_tables *arp_ptr;
  129.  
  130.     for ( i = 0; i < MAX_ARP_DATA; ++i ) {
  131.     if ( ip == arp_data[i].ip )
  132.         return( &arp_data[i] );
  133.     }
  134.  
  135.     /* didn't find any */
  136.     if ( create ) {
  137.     /* pick an old or empty one */
  138.     for ( i = 0; i < MAX_ARP_DATA ; ++i ) {
  139.         arp_ptr = &arp_data[i];
  140.         if ( ! arp_ptr->ip || chk_timeout(arp_ptr->expiry+MAX_ARP_GRACE))
  141.         return( arp_ptr );
  142.     }
  143.  
  144.     /* pick one at pseudo-random */
  145.     return( &arp_data[ arp_index = ( arp_index + 1 ) % MAX_ARP_DATA ] );
  146.     }
  147.     return( NULL );
  148. }
  149.  
  150. void _arp_register( longword use, longword instead_of )
  151. {
  152.     word i;
  153.     arp_tables *arp_ptr;
  154.  
  155.     if ( arp_ptr = _arp_search( instead_of, 0 )) {
  156.     /* now insert the address of the new guy */
  157.     arp_ptr->flags = ARP_FLAG_NEED;
  158.     _arp_resolve( use, &(arp_ptr->hardware));
  159.     arp_ptr->expiry = set_timeout( MAX_ARP_ALIVE );
  160.     return;
  161.     }
  162.  
  163.     arp_ptr = _arp_search( use , 1 );    /* create a new one */
  164.     arp_ptr->flags = ARP_FLAG_NEED;
  165.     arp_ptr->ip = instead_of;
  166.     _arp_resolve( use, &(arp_ptr->hardware));
  167.     arp_ptr->expiry = set_timeout( MAX_ARP_ALIVE );
  168. }
  169.  
  170. _arp_tick( longword ip )
  171. {
  172.     arp_tables *arp_ptr;
  173.  
  174.     if ( arp_ptr = _arp_search( ip , 0))
  175.     arp_ptr->expiry = set_timeout( MAX_ARP_ALIVE );
  176. }
  177. /*
  178.  * _arp_handler - handle incomming ARP packets
  179.  */
  180. _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)) {
  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.      in->dstIPAddr == intel( my_ip_addr )/* for my addr. */
  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 ( 0 );
  217. }
  218.  
  219.  
  220. /*
  221.  * _arp_resolve - resolve IP address to hardware address
  222.  */
  223. _arp_resolve( longword ina, eth_address *ethap)
  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 ( ina == my_ip_addr ) {
  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)) {
  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.     /* not of this network */
  268.     for ( i = 0; i < _arp_last_gateway ; ++i ) {
  269.             /* is the gateway on our subnet */
  270.             if ( ((_arp_gate_data[i].gate_ip ^ my_ip_addr ) & sin_mask ) == 0) {
  271.                 /* compare the various subnet bits */
  272.                 if ( (_arp_gate_data[i].mask & ina ) == _arp_gate_data[i].subnet ) {
  273.                     if ( _arp_resolve( _arp_gate_data[i].gate_ip , ethap ))
  274.                         return( 1 );
  275.                 }
  276.         }
  277.     }
  278.         return( 0 );
  279.     }
  280.  
  281.     /* return if no host, or no gateway */
  282.     if (! ina )
  283.     return( 0 );
  284.  
  285.     /* is on our subnet, we must resolve */
  286.     timeout = set_timeout( 5 );        /* five seconds is long for ARP */
  287.     oldhndlcbrk = wathndlcbrk;
  288.     wathndlcbrk = 1;
  289.     watcbroke = 0;
  290.     while ( !chk_timeout( timeout )) {
  291.     /* do the request */
  292.     _arp_request( arp_ptr->ip = ina );
  293.     resend = set_timeout( 1 ) - 14L;    /* 250 ms */
  294.     while (!chk_timeout( resend )) {
  295.             if (watcbroke) goto fail;
  296.         tcp_tick( NULL );
  297.         if ( arp_ptr->flags) {
  298.         if (ethap)
  299.             movmem( arp_ptr->hardware, ethap, sizeof(eth_address));
  300.         arp_ptr->expiry = set_timeout( MAX_ARP_ALIVE );
  301.                 watcbroke = 0;
  302.                 wathndlcbrk = oldhndlcbrk;
  303.         return ( 1 );
  304.         }
  305.     }
  306.     }
  307. fail:
  308.     watcbroke = 0;
  309.     wathndlcbrk = oldhndlcbrk;
  310.     return ( 0 );
  311. }
  312.