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

📄 arp.lst

📁 单片机TCP/IP源代码
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V7.09   ARP                                                                   07/07/2004 14:57:18 PAGE 1   


C51 COMPILER V7.09, COMPILATION OF MODULE ARP
OBJECT MODULE PLACED IN ARP.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE ARP.C OPTIMIZE(9,SPEED) BROWSE DEBUG OBJECTEXTEND

line level    source

   1          //-----------------------------------------------------------------------------
   2          // Net ARP.C
   3          //
   4          // This module handles ARP messages and ARP resolution and manages
   5          // the ARP cache. Refer to RFC 826 and RFC 1122
   6          //-----------------------------------------------------------------------------
   7          #include <string.h>
   8          #include <stdlib.h>
   9          #include <stdio.h>
  10          #include "C8051f.h"
  11          #include "net.h"
  12          #include "eth.h"
  13          #include "serial.h"
  14          #include "ip.h"
  15          #include "arp.h"
  16          
  17          
  18          
  19          extern WAIT xdata wait;
  20          extern UCHAR code my_hwaddr[];
  21          extern UCHAR code broadcast_hwaddr[]; 
  22          extern ULONG code my_ipaddr;
  23          extern ULONG code my_subnet;
  24          extern ULONG code gateway_ipaddr;
  25          extern UCHAR idata debug;
  26          ARP_CACHE xdata arp_cache[CACHESIZE];
  27          UCHAR waiting_for_arp;
  28          
  29          
  30          
  31          void init_arp(void)
  32          {
  33   1              memset(arp_cache, 0, sizeof(arp_cache)); 
  34   1              memset(&wait, 0, sizeof(wait));
  35   1              waiting_for_arp = FALSE;
  36   1      }
  37          
  38          
  39          
  40          
  41          //------------------------------------------------------------------------
  42          //      This is called every 60 seconds to age the ARP cache
  43          // If an entry times out then it is deleted from the cache
  44          // See "TCP/IP Illustrated, Volume 1" Sect 4.3
  45          //------------------------------------------------------------------------
  46          void age_arp_cache(void)
  47          {
  48   1              UCHAR i;
  49   1              
  50   1         for (i=0; i < CACHESIZE; i++)
  51   1         {
  52   2            if ((arp_cache[i].ipaddr != 0) && (arp_cache[i].timer))
  53   2            {
  54   3               arp_cache[i].timer--;
  55   3                              if (arp_cache[i].timer == 0)
C51 COMPILER V7.09   ARP                                                                   07/07/2004 14:57:18 PAGE 2   

  56   3               {
  57   4                                      // Timed out so clear out cache entry
  58   4                                      // Do not need to zero hwaddr
  59   4                                      arp_cache[i].ipaddr = 0;
  60   4                  if (debug) serial_send("ARP: Aged out a cache entry\r");
  61   4                              }
  62   3                      }
  63   2         }
  64   1      }
  65          
  66          
  67          
  68          
  69          //------------------------------------------------------------------------
  70          // This allocates memory for the entire outgoing message,
  71          // including eth and ip headers, then builds an outgoing
  72          // ARP response message
  73          // See "TCP/IP Illustrated, Volume 1" Sect 4.4
  74          //------------------------------------------------------------------------
  75          void arp_send(UCHAR * hwaddr, ULONG ipaddr, UCHAR msg_type)
  76          {
  77   1              UCHAR xdata * outbuf;
  78   1              ARP_HEADER xdata * arp;
  79   1               
  80   1         
  81   1         // Allocate memory for entire outgoing message including
  82   1         // eth header. Always 42 bytes
  83   1         outbuf = (UCHAR xdata *)malloc(42);
  84   1         if (outbuf == NULL)
  85   1         {
  86   2            if (debug) serial_send("ARP: Oops, out of memory\r");
  87   2            return;
  88   2         }      
  89   1           
  90   1         // Allow 14 bytes for the ethernet header
  91   1         arp = (ARP_HEADER xdata *)(outbuf + 14);
  92   1              
  93   1              arp->hardware_type = DIX_ETHERNET; 
  94   1         arp->protocol_type = IP_PACKET;
  95   1         arp->hwaddr_len = 6;
  96   1              arp->ipaddr_len = 4;               
  97   1         arp->message_type = (UINT)msg_type;
  98   1         
  99   1         // My hardware address and IP addresses 
 100   1         memcpy(arp->source_hwaddr, my_hwaddr, 6);
 101   1         arp->source_ipaddr = my_ipaddr;
 102   1      
 103   1         // Destination hwaddr and dest IP addr
 104   1         if (msg_type == ARP_REQUEST) memset(arp->dest_hwaddr, 0, 6);
 105   1         else memcpy(arp->dest_hwaddr, hwaddr, 6);
 106   1         
 107   1         arp->dest_ipaddr = ipaddr;
 108   1            
 109   1         // If request then the message is a brodcast, if a response then
 110   1         // send to specified hwaddr
 111   1         // ARP payload size is always 28 bytes
 112   1              if (msg_type == ARP_REQUEST) eth_send(outbuf, broadcast_hwaddr, ARP_PACKET, 28);
 113   1         else eth_send(outbuf, hwaddr, ARP_PACKET, 28);
 114   1      }
 115          
 116          
 117          
C51 COMPILER V7.09   ARP                                                                   07/07/2004 14:57:18 PAGE 3   

 118          //------------------------------------------------------------------------
 119          // This re-sends an ARP request if there was no response to
 120          // the first one.        It is called every 0.5 seconds.  If there
 121          // is no response after 2 re-tries, the datagram that IP was 
 122          // trying to send is deleted
 123          //-----------------------------------------------------------------------
 124          void arp_retransmit(void)
 125          {
 126   1              static UCHAR idata retries = 0; 
 127   1              
 128   1              if ((waiting_for_arp) && (wait.timer))
 129   1              {
 130   2                      wait.timer--;
 131   2                      if (wait.timer == 0)
 132   2                      {
 133   3                              retries++;
 134   3                              if (retries <= 2)
 135   3                              {
 136   4                                      if (debug) serial_send("ARP: Re-sending ARP broadcast\r");
 137   4                                      arp_send(NULL, wait.ipaddr, ARP_REQUEST);
 138   4                                      wait.timer = ARP_TIMEOUT;
 139   4                              }
 140   3                              else
 141   3                              {       
 142   4                                      if (debug) serial_send("ARP: Gave up waiting for response\r");
 143   4                                      wait.timer = 0;
 144   4                                      waiting_for_arp = 0;
 145   4                                      free(wait.buf);
 146   4                              }
 147   3                      }
 148   2              }
 149   1      }
 150          
 151          
 152          
 153          
 154          //------------------------------------------------------------------------
 155          // Find the ethernet hardware address for the given ip address
 156          // If destination IP is on my subnet then we want the eth
 157          // address      of destination, otherwise we want eth addr of gateway. 
 158          // Look in ARP cache first.  If not found there, send ARP request.

⌨️ 快捷键说明

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