home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / WATTCP / MSWATTCP.ZIP / SRC / PCARP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-06  |  9.7 KB  |  357 lines

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