⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arp.lst

📁 tcpip.rar 是一个51控制8019的程序,我已用于商用.还很稳定,有兴趣的可以看一下,对TCP IP UDP ICMP ARP RARP HTTP均可以实现.
💻 LST
📖 第 1 页 / 共 2 页
字号:
 159          // Return pointer to the hardware address or NULL if not found
 160          // See "TCP/IP Illustrated, Volume 1" Sect 4.5
 161          //------------------------------------------------------------------------
 162          UCHAR xdata * arp_resolve(ULONG dest_ipaddr)
 163          {
 164   1         UCHAR i;
 165   1            
 166   1         // If destination IP is not on my subnet then we really want eth addr
 167   1              // of gateway, not destination IP
 168   1              if ((dest_ipaddr ^ my_ipaddr) & my_subnet)
 169   1              {
 170   2                      if (gateway_ipaddr == 0)
 171   2                      {
 172   3                              if (debug) printf("ARP: Error, gateway addr not set\n");
 173   3                              return (NULL);  
 174   3                      }
 175   2                      else dest_ipaddr = gateway_ipaddr;
 176   2              }
 177   1              
 178   1                 
 179   1         // See if IP addr of interest is in ARP cache
C51 COMPILER V7.06   ARP                                                                   05/21/2004 23:09:31 PAGE 4   

 180   1         for (i=0; i < CACHESIZE; i++)
 181   1         {
 182   2            if (arp_cache[i].ipaddr == dest_ipaddr)
 183   2               return (&arp_cache[i].hwaddr[0]);
 184   2         }
 185   1      
 186   1              if (debug) printf("ARP: IP addr not found in cache\n");
 187   1              if (debug) printf("ARP: Sending an ARP broadcast\n");
 188   1              // Not in cache so broadcast ARP request
 189   1              arp_send(NULL, dest_ipaddr, ARP_REQUEST);
 190   1                 
 191   1         // Set a flag to indicate that an IP datagram is waiting
 192   1         // to be sent
 193   1         waiting_for_arp = TRUE;
 194   1                                            
 195   1         // Null means that we have sent an ARP request
 196   1         return (NULL); 
 197   1      }
 198          
 199          
 200          
 201          
 202          
 203          //------------------------------------------------------------------------
 204          // This handles incoming ARP messages
 205          // See "TCP/IP Illustrated, Volume 1" Sect 4.4
 206          // Todo:  Resolve problem of trying to add to a full cache
 207          //------------------------------------------------------------------------
 208          void arp_rcve(UCHAR xdata * inbuf)
 209          {
 210   1         UCHAR idata i, cached, oldest;
 211   1         UINT idata minimum;
 212   1         ARP_HEADER xdata * arp;
 213   1            
 214   1         arp = (ARP_HEADER xdata *)(inbuf + 14);
 215   1         cached = FALSE;
 216   1         
 217   1         // Print message
 218   1         if (debug)
 219   1         {
 220   2            if (arp->message_type == ARP_REQUEST)
 221   2               printf("ARP: Request rcvd\n");
 222   2            else printf("ARP: Response rcvd\n");
 223   2         }
 224   1               
 225   1         // Validate incoming frame
 226   1         if ((arp->hardware_type != DIX_ETHERNET) ||
 227   1             (arp->protocol_type != IP_PACKET)) return;
 228   1      
 229   1         // Search ARP cache for senders IP address
 230   1         // If found, update entry and restart timer
 231   1         for (i=0; i < CACHESIZE; i++)
 232   1         {
 233   2            if (arp_cache[i].ipaddr == arp->source_ipaddr)
 234   2            {
 235   3               memcpy(&arp_cache[i].hwaddr[0], &arp->source_hwaddr[0], 6);
 236   3               arp_cache[i].timer = CACHETIME;                
 237   3               cached = TRUE;
 238   3               if (debug) printf("ARP: Cache entry updated\n");
 239   3                        
 240   3               break;  
 241   3            }
C51 COMPILER V7.06   ARP                                                                   05/21/2004 23:09:31 PAGE 5   

 242   2         }
 243   1         
 244   1         if (arp->dest_ipaddr != my_ipaddr) return;
 245   1         
 246   1         // At this point we know the the frame is addressed to me
 247   1         // If not already in cache then add entry and start timer
 248   1         if (cached == FALSE)
 249   1         {
 250   2            // Find first blank space and add entry
 251   2                      // Blank entries are indicated by ip addr = 0
 252   2            for (i=0; i < CACHESIZE; i++)
 253   2            {
 254   3               if (arp_cache[i].ipaddr == 0) 
 255   3               {
 256   4                  arp_cache[i].ipaddr = arp->source_ipaddr;
 257   4                  memcpy(&arp_cache[i].hwaddr[0], &arp->source_hwaddr[0], 6);   
 258   4                  arp_cache[i].timer = CACHETIME;
 259   4                      if (debug) printf("ARP: New cache entry added\n");
 260   4                      break;
 261   4               }
 262   3            }
 263   2      
 264   2                      // If no blank entries in arp cache     then sort cache
 265   2                      // to find oldest entry and replace it
 266   2                      if (i == CACHESIZE)
 267   2                      {
 268   3                              // Oldest entry is the one with lowest timer value                      
 269   3                              minimum = 0xFFFF;
 270   3                              for (i=0; i < CACHESIZE; i++)
 271   3              {
 272   4                                      if (arp_cache[i].timer < minimum) 
 273   4                                      {
 274   5                                              minimum = arp_cache[i].timer;
 275   5                                              oldest = i;
 276   5                                      }
 277   4                              }
 278   3              
 279   3                              // "oldest" is now index of oldest entry, so replace it
 280   3                              arp_cache[oldest].ipaddr = arp->source_ipaddr;
 281   3               memcpy(&arp_cache[oldest].hwaddr[0], &arp->source_hwaddr[0], 6);   
 282   3               arp_cache[oldest].timer = CACHETIME;
 283   3              if (debug) printf("ARP: Cache full, so replaced oldest\n");
 284   3              }
 285   2              }
 286   1      
 287   1         
 288   1         // If we are waiting for an arp response and the arp response
 289   1              // that just came in is addressed to me and is from the host
 290   1              // we are waiting for, then send        the message-in-waiting
 291   1         if (arp->message_type == ARP_RESPONSE)
 292   1         {
 293   2              if ((waiting_for_arp) && (wait.ipaddr == arp->source_ipaddr))
 294   2              {
 295   3                      waiting_for_arp = FALSE;
 296   3                              ip_send(wait.buf, wait.ipaddr, wait.proto_id, wait.len);
 297   3                      }
 298   2              }
 299   1              else if (arp->message_type == ARP_REQUEST)
 300   1         {
 301   2              // Send ARP response 
 302   2              if (debug) printf("ARP: Sending response\n");
 303   2              arp_send(arp->source_hwaddr, arp->source_ipaddr, ARP_RESPONSE);
C51 COMPILER V7.06   ARP                                                                   05/21/2004 23:09:31 PAGE 6   

 304   2              }
 305   1      }
 306          
 307          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   1822    ----
   CONSTANT SIZE    =    372    ----
   XDATA SIZE       =    111      20
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----    ----
   IDATA SIZE       =      1       5
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -