home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / wattcp / src / pcarp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  8.9 KB  |  317 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"capalloc.h"
  11. #include"capstdio.h"
  12. #include <copyright.h>
  13. #include <wattcp.h>
  14. #include <string.h>
  15. #include <mem.h>
  16.  
  17. #define MAX_ARP_DATA 20
  18. #define MAX_ARP_ALIVE  300 /* five minutes */
  19. #define MAX_ARP_GRACE  100 /* additional grace upon expiration */
  20.  
  21. extern word wathndlcbrk;
  22. extern word watcbroke;
  23. extern word multihomes;
  24.  
  25. typedef struct {
  26.     longword        ip;
  27.     eth_address        hardware;
  28.     byte        flags;
  29.     byte        bits;        /* bits in network */
  30.     longword        expiry;
  31. } arp_tables;
  32.  
  33. typedef struct {
  34.     longword        gate_ip;
  35.     longword        subnet;
  36.     longword        mask;
  37. } gate_tables;
  38.  
  39. #define ARP_FLAG_NEED    0
  40. #define ARP_FLAG_FOUND  1
  41. #define ARP_FLAG_FIXED  255    /* cannot be removed */
  42.  
  43.  
  44. /*
  45.  * arp resolution cache - we zero fill it to save an initialization routine
  46.  */
  47. static arp_tables arp_data[ MAX_ARP_DATA ] =
  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.  
  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[0], 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.  
  123. static arp_tables *_arp_search( longword ip, int create )
  124. {
  125.     int i;
  126.     arp_tables *arp_ptr;
  127.  
  128.     for ( i = 0; i < MAX_ARP_DATA; ++i ) {
  129.     if ( ip == arp_data[i].ip )
  130.         return( &arp_data[i] );
  131.     }
  132.  
  133.     /* didn't find any */
  134.     if ( create ) {
  135.     /* pick an old or empty one */
  136.     for ( i = 0; i < MAX_ARP_DATA ; ++i ) {
  137.         arp_ptr = &arp_data[i];
  138.         if ( ! arp_ptr->ip || chk_timeout(arp_ptr->expiry+MAX_ARP_GRACE))
  139.         return( arp_ptr );
  140.     }
  141.  
  142.     /* pick one at pseudo-random */
  143.     return( &arp_data[ arp_index = ( arp_index + 1 ) % MAX_ARP_DATA ] );
  144.     }
  145.     return( NULL );
  146. }
  147.  
  148. void _arp_register( longword use, longword instead_of )
  149. {
  150.     word i;
  151.     arp_tables *arp_ptr;
  152.  
  153.     if ( (arp_ptr = _arp_search( instead_of, 0 )) != NULL) {
  154.     /* now insert the address of the new guy */
  155.     arp_ptr->flags = ARP_FLAG_NEED;
  156.         _arp_resolve( use, &(arp_ptr->hardware), 0);
  157.     arp_ptr->expiry = set_timeout( MAX_ARP_ALIVE );
  158.     return;
  159.     }
  160.  
  161.     arp_ptr = _arp_search( use , 1 );    /* create a new one */
  162.     arp_ptr->flags = ARP_FLAG_NEED;
  163.     arp_ptr->ip = instead_of;
  164.     _arp_resolve( use, &(arp_ptr->hardware), 0);
  165.     arp_ptr->expiry = set_timeout( MAX_ARP_ALIVE );
  166. }
  167.  
  168. void _arp_tick( longword ip )
  169. {
  170.     arp_tables *arp_ptr;
  171.  
  172.     if ( (arp_ptr = _arp_search( ip , 0)) != NULL )
  173.     arp_ptr->expiry = set_timeout( MAX_ARP_ALIVE );
  174. }
  175. /*
  176.  * _arp_handler - handle incomming ARP packets
  177.  */
  178. _arp_handler( arp_Header *in)
  179. {
  180.     arp_Header *op;
  181.     longword his_ip;
  182.     arp_tables *arp_ptr;
  183.  
  184.     if ( in->hwType != arp_TypeEther ||      /* have ethernet hardware, */
  185.     in->protType != 8 )              /* and internet software, */
  186.     return( 0 );
  187.  
  188.     /* continuously accept data - but only for people we talk to */
  189.     his_ip = intel( in->srcIPAddr );
  190.  
  191.     if ( (arp_ptr = _arp_search( his_ip, 0)) != NULL ) {
  192.     arp_ptr->expiry = set_timeout( MAX_ARP_ALIVE );
  193.     movmem( in->srcEthAddr, arp_ptr->hardware, sizeof( eth_address ));
  194.     arp_ptr->flags = ARP_FLAG_FOUND;
  195.     }
  196.  
  197.     /* does someone else want our Ethernet address ? */
  198.     if ( in->opcode == ARP_REQUEST &&        /* and be a resolution req. */
  199.      ((longword)(intel(in->dstIPAddr) - my_ip_addr ) <= multihomes )
  200.        )  {
  201.     op = (arp_Header *)_eth_formatpacket(in->srcEthAddr, 0x0608);
  202.     op->hwType = arp_TypeEther;
  203.     op->protType = 0x008;            /* intel for ip */
  204.     op->hwProtAddrLen = sizeof(eth_address) + (sizeof(longword) << 8 );
  205.     op->opcode = ARP_REPLY;
  206.  
  207.     op->dstIPAddr = in->srcIPAddr;
  208.     op->srcIPAddr = in->dstIPAddr;
  209.     movmem(_eth_addr, op->srcEthAddr, sizeof(eth_address));
  210.     movmem(in->srcEthAddr, op->dstEthAddr, sizeof(eth_address));
  211.     _eth_send(sizeof(arp_Header));
  212.     return ( 1 );
  213.     }
  214.     return( 1 );
  215. }
  216.  
  217.  
  218. /*
  219.  * _arp_resolve - resolve IP address to hardware address
  220.  */
  221. _arp_resolve( longword ina, eth_address *ethap, int nowait)
  222. {
  223.     static arp_tables *arp_ptr;
  224.     int i, oldhndlcbrk;
  225.     longword timeout, resend;
  226.     int packettype;
  227.  
  228.     if ( _pktdevclass == PD_SLIP ) {
  229.     /* we are running slip or somthing which does not use addresses */
  230.     return( 1 );
  231.     }
  232.  
  233.     if ( (longword)(ina - my_ip_addr) < multihomes) {
  234.     if (ethap)
  235.         movmem( _eth_addr, ethap, sizeof( eth_address ));
  236.     return( 1 );
  237.     }
  238.  
  239.     /* attempt to solve with ARP cache */
  240.     /* fake while loop */
  241.     while ( (arp_ptr = _arp_search( ina, 0)) != NULL ) {
  242.     if ( arp_ptr->flags != ARP_FLAG_NEED ) {
  243.         /* has been resolved */
  244. #ifdef NEW_EXPIRY
  245.         if ( chk_timeout( arp_ptr->timeout ) {
  246.         if ( ! chk_timeout( arp_ptr->timeout + MAX_ARP_GRACE ) {
  247.             /* we wish to refresh it asynchronously */
  248.             _arp_request( ina );
  249.         else
  250.             break;    /* must do full fledged arp */
  251. #endif NEW_EXPIRY
  252.         if (ethap)
  253.         movmem( arp_ptr->hardware, ethap, sizeof(eth_address));
  254.         return( 1 );
  255.     }
  256.         break;
  257.     }
  258.  
  259.     /* make a new one if necessary */
  260.     if (! arp_ptr )
  261.     arp_ptr = _arp_search( ina, 1 );
  262.  
  263.     /* we must look elsewhere - but is it on our subnet??? */
  264.     if (( ina ^ my_ip_addr ) & sin_mask ) {
  265.  
  266.     /* not of this network */
  267.     for ( i = 0; i < _arp_last_gateway ; ++i ) {
  268.  
  269.             //  is the gateway on our subnet
  270.             //  or if mask is ff...ff, we assume any gateway must succeed
  271.             //  because we are on 'no' network
  272.  
  273.             if ((((_arp_gate_data[i].gate_ip ^ my_ip_addr ) & sin_mask ) == 0)
  274.              || ( sin_mask == 0xffffffffL )) {
  275.                 /* compare the various subnet bits */
  276.                 if ( (_arp_gate_data[i].mask & ina ) == _arp_gate_data[i].subnet ) {
  277.                     if ( _arp_resolve( _arp_gate_data[i].gate_ip , ethap, nowait ))
  278.                         return( 1 );
  279.                 }
  280.         }
  281.     }
  282.         return( 0 );
  283.     }
  284.  
  285.     /* return if no host, or no gateway */
  286.     if (! ina )
  287.     return( 0 );
  288.  
  289.     /* is on our subnet, we must resolve */
  290.     timeout = set_timeout( 5 );        /* five seconds is long for ARP */
  291.     oldhndlcbrk = wathndlcbrk;
  292.     wathndlcbrk = 1;
  293.     watcbroke = 0;
  294.     while ( !chk_timeout( timeout )) {
  295.     /* do the request */
  296.     _arp_request( arp_ptr->ip = ina );
  297.     resend = set_timeout( 1 ) - 14L;    /* 250 ms */
  298.     while (!chk_timeout( resend )) {
  299.             if (watcbroke) goto fail;
  300.         tcp_tick( NULL );
  301.         if ( arp_ptr->flags) {
  302.         if (ethap)
  303.             movmem( arp_ptr->hardware, ethap, sizeof(eth_address));
  304.         arp_ptr->expiry = set_timeout( MAX_ARP_ALIVE );
  305.                 watcbroke = 0;
  306.                 wathndlcbrk = oldhndlcbrk;
  307.         return ( 1 );
  308.         }
  309.     }
  310.         if ( nowait ) goto fail;
  311.     }
  312. fail:
  313.     watcbroke = 0;
  314.     wathndlcbrk = oldhndlcbrk;
  315.     return ( 0 );
  316. }
  317.