home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / wattcp.zip / PCARP.C < prev    next >
C/C++ Source or Header  |  1992-02-03  |  9KB  |  311 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. extern word multihomes;
  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.         ip = aton( data );
  88.     }
  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.          ((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. }
  217.  
  218.  
  219. /*
  220.  * _arp_resolve - resolve IP address to hardware address
  221.  */
  222. _arp_resolve( longword ina, eth_address *ethap)
  223. {
  224.     static arp_tables *arp_ptr;
  225.     int i, oldhndlcbrk;
  226.     longword timeout, resend;
  227.     int packettype;
  228.  
  229.     if ( _pktdevclass == PD_SLIP ) {
  230.     /* we are running slip or somthing which does not use addresses */
  231.     return( 1 );
  232.     }
  233.  
  234.     if ( (longword)(ina - my_ip_addr) < multihomes) {
  235.     if (ethap)
  236.         movmem( _eth_addr, ethap, sizeof( eth_address ));
  237.     return( 1 );
  238.     }
  239.  
  240.     /* attempt to solve with ARP cache */
  241.     /* fake while loop */
  242.     while ( arp_ptr = _arp_search( ina, 0)) {
  243.     if ( arp_ptr->flags != ARP_FLAG_NEED ) {
  244.         /* has been resolved */
  245. #ifdef NEW_EXPIRY
  246.         if ( chk_timeout( arp_ptr->timeout ) {
  247.         if ( ! chk_timeout( arp_ptr->timeout + MAX_ARP_GRACE ) {
  248.             /* we wish to refresh it asynchronously */
  249.             _arp_request( ina );
  250.         else
  251.             break;    /* must do full fledged arp */
  252. #endif NEW_EXPIRY
  253.         if (ethap)
  254.         movmem( arp_ptr->hardware, ethap, sizeof(eth_address));
  255.         return( 1 );
  256.     }
  257.         break;
  258.     }
  259.  
  260.     /* make a new one if necessary */
  261.     if (! arp_ptr )
  262.     arp_ptr = _arp_search( ina, 1 );
  263.  
  264.     /* we must look elsewhere - but is it on our subnet??? */
  265.     if (( ina ^ my_ip_addr ) & sin_mask ) {
  266.     /* not of this network */
  267.     for ( i = 0; i < _arp_last_gateway ; ++i ) {
  268.             /* is the gateway on our subnet */
  269.             if ( ((_arp_gate_data[i].gate_ip ^ my_ip_addr ) & sin_mask ) == 0) {
  270.                 /* compare the various subnet bits */
  271.                 if ( (_arp_gate_data[i].mask & ina ) == _arp_gate_data[i].subnet ) {
  272.                     if ( _arp_resolve( _arp_gate_data[i].gate_ip , ethap ))
  273.                         return( 1 );
  274.                 }
  275.         }
  276.     }
  277.         return( 0 );
  278.     }
  279.  
  280.     /* return if no host, or no gateway */
  281.     if (! ina )
  282.     return( 0 );
  283.  
  284.     /* is on our subnet, we must resolve */
  285.     timeout = set_timeout( 5 );        /* five seconds is long for ARP */
  286.     oldhndlcbrk = wathndlcbrk;
  287.     wathndlcbrk = 1;
  288.     watcbroke = 0;
  289.     while ( !chk_timeout( timeout )) {
  290.     /* do the request */
  291.     _arp_request( arp_ptr->ip = ina );
  292.     resend = set_timeout( 1 ) - 14L;    /* 250 ms */
  293.     while (!chk_timeout( resend )) {
  294.             if (watcbroke) goto fail;
  295.         tcp_tick( NULL );
  296.         if ( arp_ptr->flags) {
  297.         if (ethap)
  298.             movmem( arp_ptr->hardware, ethap, sizeof(eth_address));
  299.         arp_ptr->expiry = set_timeout( MAX_ARP_ALIVE );
  300.                 watcbroke = 0;
  301.                 wathndlcbrk = oldhndlcbrk;
  302.         return ( 1 );
  303.         }
  304.     }
  305.     }
  306. fail:
  307.     watcbroke = 0;
  308.     wathndlcbrk = oldhndlcbrk;
  309.     return ( 0 );
  310. }
  311.