📄 uip_arp.lst
字号:
C51 COMPILER V8.02 UIP_ARP 10/13/2008 17:11:19 PAGE 1
C51 COMPILER V8.02, COMPILATION OF MODULE UIP_ARP
OBJECT MODULE PLACED IN .\out\uip_arp.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE uip_arp.c LARGE OPTIMIZE(SIZE) BROWSE DEBUG OBJECTEXTEND PRINT(.\out\uip_ar
-p.lst) OBJECT(.\out\uip_arp.obj)
line level source
1
2
3 #include "uip_arp.h"
4
5 #include <string.h>
6
7 struct arp_hdr {
8 struct uip_eth_hdr ethhdr;
9 u16_t hwtype;
10 u16_t protocol;
11 u8_t hwlen;
12 u8_t protolen;
13 u16_t opcode;
14 struct uip_eth_addr shwaddr;
15 u16_t sipaddr[2];
16 struct uip_eth_addr dhwaddr;
17 u16_t dipaddr[2];
18 };
19
20 struct ethip_hdr {
21 struct uip_eth_hdr ethhdr;
22 /* IP header. */
23 u8_t vhl,
24 tos,
25 len[2],
26 ipid[2],
27 ipoffset[2],
28 ttl,
29 proto;
30 u16_t ipchksum;
31 u16_t srcipaddr[2],
32 destipaddr[2];
33 };
34
35 #define ARP_REQUEST 1
36 #define ARP_REPLY 2
37
38 #define ARP_HWTYPE_ETH 1
39
40 struct arp_entry {
41 u16_t ipaddr[2];
42 struct uip_eth_addr ethaddr;
43 u8_t time;
44 };
45
46 struct uip_eth_addr uip_ethaddr = {{UIP_ETHADDR0,
47 UIP_ETHADDR1,
48 UIP_ETHADDR2,
49 UIP_ETHADDR3,
50 UIP_ETHADDR4,
51 UIP_ETHADDR5}};
52
53 static struct arp_entry arp_table[UIP_ARPTAB_SIZE];
54 static u16_t ipaddr[2];
C51 COMPILER V8.02 UIP_ARP 10/13/2008 17:11:19 PAGE 2
55 static u8_t i, c;
56
57 static u8_t arptime;
58 static u8_t tmpage;
59
60 #define BUF ((struct arp_hdr *)&uip_buf[0])
61 #define IPBUF ((struct ethip_hdr *)&uip_buf[0])
62 /*-----------------------------------------------------------------------------------*/
63 /**
64 * Initialize the ARP module.
65 *
66 */
67 /*-----------------------------------------------------------------------------------*/
68 void
69 uip_arp_init(void)
70 {
71 1 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
72 2 memset(arp_table[i].ipaddr, 0, 4);
73 2 }
74 1 }
75 /*-----------------------------------------------------------------------------------*/
76 /**
77 * Periodic ARP processing function.
78 *
79 * This function performs periodic timer processing in the ARP module
80 * and should be called at regular intervals. The recommended interval
81 * is 10 seconds between the calls.
82 *
83 */
84 /*-----------------------------------------------------------------------------------*/
85 void
86 uip_arp_timer(void)
87 {
88 1 struct arp_entry *tabptr;
89 1
90 1 ++arptime;
91 1 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
92 2 tabptr = &arp_table[i];
93 2 if((tabptr->ipaddr[0] | tabptr->ipaddr[1]) != 0 &&
94 2 arptime - tabptr->time >= UIP_ARP_MAXAGE) {
95 3 memset(tabptr->ipaddr, 0, 4);
96 3 }
97 2 }
98 1
99 1 }
100 /*-----------------------------------------------------------------------------------*/
101 static void
102 uip_arp_update(u16_t *ipaddr, struct uip_eth_addr *ethaddr)
103 {
104 1 register struct arp_entry *tabptr;
105 1 /* Walk through the ARP mapping table and try to find an entry to
106 1 update. If none is found, the IP -> MAC address mapping is
107 1 inserted in the ARP table. */
108 1 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
109 2
110 2 tabptr = &arp_table[i];
111 2 /* Only check those entries that are actually in use. */
112 2 if(tabptr->ipaddr[0] != 0 &&
113 2 tabptr->ipaddr[1] != 0) {
114 3
115 3 /* Check if the source IP address of the incoming packet matches
116 3 the IP address in this ARP table entry. */
C51 COMPILER V8.02 UIP_ARP 10/13/2008 17:11:19 PAGE 3
117 3 if(ipaddr[0] == tabptr->ipaddr[0] &&
118 3 ipaddr[1] == tabptr->ipaddr[1]) {
119 4
120 4 /* An old entry found, update this and return. */
121 4 memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
122 4 tabptr->time = arptime;
123 4
124 4 return;
125 4 }
126 3 }
127 2 }
128 1
129 1 /* If we get here, no existing ARP table entry was found, so we
130 1 create one. */
131 1
132 1 /* First, we try to find an unused entry in the ARP table. */
133 1 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
134 2 tabptr = &arp_table[i];
135 2 if(tabptr->ipaddr[0] == 0 &&
136 2 tabptr->ipaddr[1] == 0) {
137 3 break;
138 3 }
139 2 }
140 1
141 1 /* If no unused entry is found, we try to find the oldest entry and
142 1 throw it away. */
143 1 if(i == UIP_ARPTAB_SIZE) {
144 2 tmpage = 0;
145 2 c = 0;
146 2 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
147 3 tabptr = &arp_table[i];
148 3 if(arptime - tabptr->time > tmpage) {
149 4 tmpage = arptime - tabptr->time;
150 4 c = i;
151 4 }
152 3 }
153 2 i = c;
154 2 }
155 1
156 1 /* Now, i is the ARP table entry which we will fill with the new
157 1 information. */
158 1 memcpy(tabptr->ipaddr, ipaddr, 4);
159 1 memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
160 1 tabptr->time = arptime;
161 1 }
162 /*-----------------------------------------------------------------------------------*/
163 /**
164 * ARP processing for incoming IP packets
165 *
166 * This function should be called by the device driver when an IP
167 * packet has been received. The function will check if the address is
168 * in the ARP cache, and if so the ARP cache entry will be
169 * refreshed. If no ARP cache entry was found, a new one is created.
170 *
171 * This function expects an IP packet with a prepended Ethernet header
172 * in the uip_buf[] buffer, and the length of the packet in the global
173 * variable uip_len.
174 */
175 /*-----------------------------------------------------------------------------------*/
176 void
177 uip_arp_ipin(void)
178 {
C51 COMPILER V8.02 UIP_ARP 10/13/2008 17:11:19 PAGE 4
179 1 uip_len -= sizeof(struct uip_eth_hdr);
180 1
181 1 /* Only insert/update an entry if the source IP address of the
182 1 incoming IP packet comes from a host on the local network. */
183 1 if((IPBUF->srcipaddr[0] & uip_arp_netmask[0]) !=
184 1 (uip_hostaddr[0] & uip_arp_netmask[0])) {
185 2 return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -