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

📄 uip_arp.lst

📁 基于STC单片机与MICROCHIP ENC28J60的TCPIP以太网控制器
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V8.08   UIP_ARP                                                               08/22/2008 14:32:52 PAGE 1   


C51 COMPILER V8.08, COMPILATION OF MODULE UIP_ARP
OBJECT MODULE PLACED IN uip_arp.OBJ
COMPILER INVOKED BY: D:\Keil\C51\BIN\C51.EXE uip_arp.c BROWSE DEBUG OBJECTEXTEND

line level    source

   1          /**
   2           * \addtogroup uip
   3           * @{
   4           */
   5          
   6          /**
   7           * \defgroup uiparp uIP 
   8           * @{
   9           * 
  10           * The Address Resolution Protocol ARP is used for mapping between IP
  11           * addresses and link level addresses such as the Ethernet MAC
  12           * addresses. ARP uses broadcast queries to ask for the link level
  13           * address of a known IP address and the host which is configured with
  14           * the IP address for which the query was meant, will respond with its
  15           * link level address.
  16           *
  17           * \note This ARP implementation only supports Ethernet.
  18           仅支持以太网*/
  19           
  20          /**
  21           * \file
  22           * Implementation of the ARP Address Resolution Protocol.
  23           * \author Adam Dunkels <adam@dunkels.com>
  24           *
  25           */
  26          
  27          /*
  28           * This file is part of the uIP TCP/IP stack.
  29           *
  30           * $Id: uip_arp.c,v 1.7.2.3 2003/10/06 22:42:30 adam Exp $
  31           *
  32           */
  33          
  34          #include "uip_arp.h"
  35          #include "reg51.h"
  36          #include <string.h>
  37          
  38          sbit P10 = P1^0;
  39          sbit P11 = P1^1;                                        //add1
  40          sbit P12 = P1^2;                                         //add1
  41          
  42          struct arp_hdr {
  43            struct uip_eth_hdr ethhdr;            //头    14字节          
  44            u16_t hwtype;
  45            u16_t protocol;
  46            u8_t hwlen;
  47            u8_t protolen;
  48            u16_t opcode;
  49            struct uip_eth_addr shwaddr;          //地址
  50            u16_t sipaddr[2];
  51            struct uip_eth_addr dhwaddr;
  52            u16_t dipaddr[2];                     //42字节 
  53          };
  54                          
  55          struct ethip_hdr {
C51 COMPILER V8.08   UIP_ARP                                                               08/22/2008 14:32:52 PAGE 2   

  56            struct uip_eth_hdr ethhdr;            //14字节
  57            /* IP header. */
  58            u8_t vhl,
  59              tos,          
  60              len[2],       
  61              ipid[2],        
  62              ipoffset[2],  
  63              ttl,          
  64              proto;     
  65            u16_t ipchksum;
  66            u16_t srcipaddr[2], 
  67              destipaddr[2];              //20字节
  68          };                                      
  69          
  70          #define ARP_REQUEST 1
  71          #define ARP_REPLY   2
  72          
  73          #define ARP_HWTYPE_ETH 1                        //硬件类型1代表MAC地址
  74          
  75          struct arp_entry {
  76            u16_t ipaddr[2];
  77            struct uip_eth_addr ethaddr;
  78            u8_t time;
  79          };
  80          
  81          struct uip_eth_addr xdata uip_ethaddr = {{UIP_ETHADDR0,
  82                                              UIP_ETHADDR1,
  83                                              UIP_ETHADDR2,
  84                                              UIP_ETHADDR3,
  85                                              UIP_ETHADDR4,
  86                                              UIP_ETHADDR5}};
  87          
  88          static struct arp_entry xdata arp_table[UIP_ARPTAB_SIZE];                       //UIP_ARPTAB_SIZE=2
  89          static u16_t xdata ipaddr[2];
  90          static u8_t xdata i, c;
  91          
  92          static u8_t xdata arptime;
  93          static u8_t xdata tmpage;
  94          
  95          #define BUF   ((struct arp_hdr *)&uip_buf[0])
  96          #define IPBUF ((struct ethip_hdr *)&uip_buf[0])
  97          /*-----------------------------------------------------------------------------------*/
  98          /**
  99           * Initialize the ARP module.
 100           *
 101           */
 102          /*-----------------------------------------------------------------------------------*/
 103          void
 104          uip_arp_init(void)
 105          {
 106   1        for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {                        //UIP_ARPTAB_SIZE=5
 107   2          memset(arp_table[i].ipaddr, 0, 4);                  //清0
 108   2        }
 109   1      }
 110          /*-----------------------------------------------------------------------------------*/
 111          /**
 112           * Periodic ARP processing function.
 113           *周期性ARP处理函数
 114           * This function performs periodic timer processing in the ARP module
 115           * and should be called at regular intervals. The recommended interval
 116           * is 10 seconds between the calls.
 117           *建议10s钟进行一次查询         arptime+1时间为10s
C51 COMPILER V8.08   UIP_ARP                                                               08/22/2008 14:32:52 PAGE 3   

 118           */
 119          /*-----------------------------------------------------------------------------------*/
 120          void
 121          uip_arp_timer(void)
 122          {
 123   1        struct arp_entry *tabptr;
 124   1        
 125   1        ++arptime;                                                            //静态变量
 126   1        for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {                                        //清0   
 127   2          tabptr = &arp_table[i];
 128   2          if((tabptr->ipaddr[0] | tabptr->ipaddr[1]) != 0 &&
 129   2             arptime - tabptr->time >= UIP_ARP_MAXAGE) {                              //UIP_ARP_MAXAGE=120,代表20分钟
 130   3            memset(tabptr->ipaddr, 0, 4);                     //该字符串函数的意义, 可能是清零
 131   3          }
 132   2        }
 133   1      
 134   1      }
 135          /*-----------------------------------------------------------------------------------*/
 136          static void
 137          uip_arp_update(u16_t *ipaddr, struct uip_eth_addr *ethaddr)
 138          {
 139   1        register struct arp_entry *tabptr;
 140   1        /* Walk through the ARP mapping table and try to find an entry 入口to
 141   1           update. If none is found, the IP -> MAC address mapping is
 142   1           inserted in the ARP table. */
 143   1        for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
 144   2                                      
 145   2          tabptr = &arp_table[i];
 146   2          /* Only check those entries that are actually in use. */
 147   2          if(tabptr->ipaddr[0] != 0 &&
 148   2             tabptr->ipaddr[1] != 0) {
 149   3      
 150   3            /* Check if the source IP address of the incoming packet matches
 151   3               the IP address in this ARP table entry. */
 152   3            if(ipaddr[0] == tabptr->ipaddr[0] &&
 153   3               ipaddr[1] == tabptr->ipaddr[1]) {
 154   4               
 155   4              /* An old entry found, update this and return. */
 156   4              memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);         //赋值
 157   4              tabptr->time = arptime;
 158   4      
 159   4              return;
 160   4            }
 161   3          }
 162   2        }
 163   1      
 164   1        /* If we get here, no existing ARP table entry was found, so we
 165   1           create one. */
 166   1      
 167   1        /* First, we try to find an unused entry in the ARP table. */
 168   1        for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
 169   2          tabptr = &arp_table[i];
 170   2          if(tabptr->ipaddr[0] == 0 &&
 171   2             tabptr->ipaddr[1] == 0) {
 172   3            break;
 173   3          }
 174   2        }
 175   1      
 176   1        /* If no unused entry is found, we try to find the oldest entry and
 177   1           throw it away. */
 178   1        if(i == UIP_ARPTAB_SIZE) {
 179   2          tmpage = 0;         
C51 COMPILER V8.08   UIP_ARP                                                               08/22/2008 14:32:52 PAGE 4   

 180   2          c = 0;
 181   2          for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
 182   3            tabptr = &arp_table[i];
 183   3            if(arptime - tabptr->time > tmpage) {
 184   4              tmpage = arptime - tabptr->time;
 185   4              c = i;
 186   4            }
 187   3          }
 188   2          i = c;
 189   2        }
 190   1      
 191   1        /* Now, i is the ARP table entry which we will fill with the new
 192   1           information.现在i是ARP表格的入口 */
 193   1        memcpy(tabptr->ipaddr, ipaddr, 4);
 194   1        memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
 195   1        tabptr->time = arptime;
 196   1      }
 197          /*-----------------------------------------------------------------------------------*/
 198          /**
 199           * ARP processing for incoming IP packets
 200           *      ARP处理进入的IP包
 201           * This function should be called by the device driver when an IP
 202           * packet has been received. The function will check if the address is
 203           * in the ARP cache, and if so the ARP cache entry will be
 204           * refreshed. If no ARP cache entry was found, a new one is created.
 205           *当接收到一个IP包的时候,这个函数奖被设备驱动调用,这个函数将核实该地址是不是ARP缓寸中的IP地址
 206          如果是的话,ARP 缓存将被更新,如果不是的话,将创立一个新的。

⌨️ 快捷键说明

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