?? uip_arp.lst
字號:
C51 COMPILER V7.06 UIP_ARP 04/05/2006 12:13:01 PAGE 1
C51 COMPILER V7.06, COMPILATION OF MODULE UIP_ARP
OBJECT MODULE PLACED IN .\DEBUG\uip_arp.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE uip\uip_arp.c LARGE OPTIMIZE(6,SPEED) BROWSE DEBUG OBJECTEXTEND PRINT(.\DEB
-UG\uip_arp.lst) OBJECT(.\DEBUG\uip_arp.obj)
stmt level source
1
2 #include "uip_arp.h"
3
4 struct arp_hdr {
5 struct uip_eth_hdr ethhdr;
6 u16_t hwtype;
7 u16_t protocol;
8 u8_t hwlen;
9 u8_t protolen;
10 u16_t opcode;
11 struct uip_eth_addr shwaddr;
12 u16_t sipaddr[2];
13 struct uip_eth_addr dhwaddr;
14 u16_t dipaddr[2];
15 };
16
17 struct ethip_hdr {
18 struct uip_eth_hdr ethhdr;
19 /* IP header. */
20 u8_t vhl,
21 tos,
22 len[2],
23 ipid[2],
24 ipoffset[2],
25 ttl,
26 proto;
27 u16_t ipchksum;
28 u16_t srcipaddr[2],
29 destipaddr[2];
30 };
31
32 #define ARP_REQUEST 1
33 #define ARP_REPLY 2
34
35 #define ARP_HWTYPE_ETH 1
36
37 struct arp_entry {
38 u16_t ipaddr[2];
39 struct uip_eth_addr ethaddr;
40 u8_t time;
41 };
42
43 static const struct uip_eth_addr ethaddr = {{UIP_ETHADDR0,
44 UIP_ETHADDR1,
45 UIP_ETHADDR2,
46 UIP_ETHADDR3,
47 UIP_ETHADDR4,
48 UIP_ETHADDR5}};
49
50 static struct arp_entry arp_table[UIP_ARPTAB_SIZE];
51 static u16_t ipaddr[2];
52 static u8_t i, c;
53
54 static u8_t time;
C51 COMPILER V7.06 UIP_ARP 04/05/2006 12:13:01 PAGE 2
55 static u8_t tmpage;
56
57 #define BUF ((struct arp_hdr *)&uip_buf[0])
58 #define IPBUF ((struct ethip_hdr *)&uip_buf[0])
59 /*-----------------------------------------------------------------------------------*/
60 void
61 uip_arp_init(void)
62 {
63 1 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
64 2 arp_table[i].ipaddr[0] =
65 2 arp_table[i].ipaddr[1] = 0;
66 2 }
67 1 }
68 /*-----------------------------------------------------------------------------------*/
69 void
70 uip_arp_timer(void)
71 {
72 1 ++time;
73 1 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
74 2 if((arp_table[i].ipaddr[0] | arp_table[i].ipaddr[1]) != 0 &&
75 2 time - arp_table[i].time >= UIP_ARP_MAXAGE) {
76 3 arp_table[i].ipaddr[0] =
77 3 arp_table[i].ipaddr[1] = 0;
78 3 }
79 2 }
80 1
81 1 }
82 /*-----------------------------------------------------------------------------------*/
83 static void
84 uip_arp_update(u16_t *ipaddr, struct uip_eth_addr *ethaddr)
85 {
86 1 /* Walk through the ARP mapping table and try to find an entry to
87 1 update. If none is found, the IP -> MAC address mapping is
88 1 inserted in the ARP table. */
89 1 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
90 2
91 2 /* Only check those entries that are actually in use. */
92 2 if(arp_table[i].ipaddr[0] != 0 &&
93 2 arp_table[i].ipaddr[1] != 0) {
94 3
95 3 /* Check if the source IP address of the incoming packet matches
96 3 the IP address in this ARP table entry. */
97 3 if(ipaddr[0] == arp_table[i].ipaddr[0] &&
98 3 ipaddr[1] == arp_table[i].ipaddr[1]) {
99 4
100 4 /* An old entry found, update this and return. */
101 4 for(c = 0; c < 6; ++c) {
102 5 arp_table[i].ethaddr.addr[c] = ethaddr->addr[c];
103 5 }
104 4 arp_table[i].time = time;
105 4 return;
106 4 }
107 3 }
108 2 }
109 1
110 1 /* If we get here, no existing ARP table entry was found, so we
111 1 create one. */
112 1
113 1 /* First, we try to find an unused entry in the ARP table. */
114 1 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
115 2 if(arp_table[i].ipaddr[0] == 0 &&
116 2 arp_table[i].ipaddr[1] == 0)
C51 COMPILER V7.06 UIP_ARP 04/05/2006 12:13:01 PAGE 3
117 2 break;
118 2 }
119 1
120 1 /* If no unused entry is found, we try to find the oldest entry and
121 1 throw it away. */
122 1 if(i == UIP_ARPTAB_SIZE) {
123 2 tmpage = 0;
124 2 c = 0;
125 2 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
126 3 if(time - arp_table[i].time > tmpage) {
127 4 tmpage = time - arp_table[i].time;
128 4 c = i;
129 4 }
130 3 }
131 2 i = c;
132 2 }
133 1
134 1 /* Now, i is the ARP table entry which we will fill with the new
135 1 information. */
136 1 arp_table[i].ipaddr[0] = ipaddr[0];
137 1 arp_table[i].ipaddr[1] = ipaddr[1];
138 1 for(c = 0; c < 6; ++c) {
139 2 arp_table[i].ethaddr.addr[c] = ethaddr->addr[c];
140 2 }
141 1 arp_table[i].time = time;
142 1 }
143 /*-----------------------------------------------------------------------------------*/
144 void
145 uip_arp_ipin(void)
146 {
147 1
148 1 /* Only insert/update an entry if the source IP address of the
149 1 incoming IP packet comes from a host on the local network. */
150 1 if((IPBUF->srcipaddr[0] & htons((UIP_NETMASK0 << 8) | UIP_NETMASK1)) !=
151 1 (htons((UIP_IPADDR0 << 8) | UIP_IPADDR1)
152 1 & htons((UIP_NETMASK0 << 8) | UIP_NETMASK1)))
153 1 return;
154 1 if((IPBUF->srcipaddr[1] & htons((UIP_NETMASK2 << 8) | UIP_NETMASK3)) !=
155 1 (htons((UIP_IPADDR2 << 8) | UIP_IPADDR3)
156 1 & htons((UIP_NETMASK2 << 8) | UIP_NETMASK3)))
157 1 return;
158 1
159 1 uip_arp_update(IPBUF->srcipaddr, &(IPBUF->ethhdr.src));
160 1
161 1 // uip length fix for ethernet padding and ethernet header
162 1 uip_len = (IPBUF->len[1]) + ((IPBUF->len[0])<<8) ;
163 1
164 1 return;
165 1 }
166 /*-----------------------------------------------------------------------------------*/
167 void
168 uip_arp_arpin(void)
169 {
170 1
171 1 if(uip_len < sizeof(struct arp_hdr)) {
172 2 uip_len = 0;
173 2 return;
174 2 }
175 1
176 1 uip_len = 0;
177 1
178 1 switch(BUF->opcode) {
C51 COMPILER V7.06 UIP_ARP 04/05/2006 12:13:01 PAGE 4
179 2 case htons(ARP_REQUEST):
180 2 /* ARP request. If it asked for our address, we send out a
181 2 reply. */
182 2 if(BUF->dipaddr[0] == htons((UIP_IPADDR0 << 8) | UIP_IPADDR1) &&
183 2 BUF->dipaddr[1] == htons((UIP_IPADDR2 << 8) | UIP_IPADDR3)) {
184 3 /* The reply opcode is 2. */
185 3 BUF->opcode = htons(2);
186 3 BUF->dhwaddr = BUF->shwaddr;
187 3
188 3 for(c = 0; c < 6; ++c) {
189 4 BUF->shwaddr.addr[c] =
190 4 BUF->ethhdr.src.addr[c] = ethaddr.addr[c];
191 4 BUF->ethhdr.dest.addr[c] = BUF->dhwaddr.addr[c];
192 4 }
193 3
194 3 BUF->dipaddr[0] = BUF->sipaddr[0];
195 3 BUF->dipaddr[1] = BUF->sipaddr[1];
196 3 BUF->sipaddr[0] = htons((UIP_IPADDR0 << 8) | UIP_IPADDR1);
197 3 BUF->sipaddr[1] = htons((UIP_IPADDR2 << 8) | UIP_IPADDR3);
198 3
199 3 BUF->ethhdr.type = htons(UIP_ETHTYPE_ARP);
200 3 uip_len = sizeof(struct arp_hdr);
201 3 }
202 2 break;
203 2 case htons(ARP_REPLY):
204 2 /* ARP reply. We insert or update the ARP table if it was meant
205 2 for us. */
206 2 if(BUF->dipaddr[0] == htons((UIP_IPADDR0 << 8) | UIP_IPADDR1) &&
207 2 BUF->dipaddr[1] == htons((UIP_IPADDR2 << 8) | UIP_IPADDR3)) {
208 3
209 3 uip_arp_update(BUF->sipaddr, &BUF->shwaddr);
210 3 }
211 2 break;
212 2 }
213 1
214 1 return;
215 1 }
216 /*-----------------------------------------------------------------------------------*/
217 void
218 uip_arp_out(void)
219 {
220 1 /* Find the destination IP address in the ARP table and construct
221 1 the Ethernet header. If the destination IP addres isn't on the
222 1 local network, we use the default router's IP address instead.
223 1
224 1 If not ARP table entry is found, we overwrite the original IP
225 1 packet with an ARP request for the IP address. */
226 1
227 1 /* Check if the destination address is on the local network. */
228 1 if((IPBUF->destipaddr[0] & htons((UIP_NETMASK0 << 8) | UIP_NETMASK1)) !=
229 1 (htons((UIP_IPADDR0 << 8) | UIP_IPADDR1)
230 1 & htons((UIP_NETMASK0 << 8) | UIP_NETMASK1)) ||
231 1 (IPBUF->destipaddr[1] & htons((UIP_NETMASK2 << 8) | UIP_NETMASK3)) !=
232 1 (htons((UIP_IPADDR2 << 8) | UIP_IPADDR3)
233 1 & htons((UIP_NETMASK2 << 8) | UIP_NETMASK3))) {
234 2 /* Destination address was not on the local network, so we need to
235 2 use the default router's IP address instead of the destination
236 2 address when determining the MAC address. */
237 2 ipaddr[0] = htons((UIP_DRIPADDR0 << 8) | UIP_DRIPADDR1);
238 2 ipaddr[1] = htons((UIP_DRIPADDR2 << 8) | UIP_DRIPADDR3);
239 2 } else {
240 2 /* Else, we use the destination IP address. */
C51 COMPILER V7.06 UIP_ARP 04/05/2006 12:13:01 PAGE 5
241 2 ipaddr[0] = IPBUF->destipaddr[0];
242 2 ipaddr[1] = IPBUF->destipaddr[1];
243 2 }
244 1
245 1 for(i = 0; i < UIP_ARPTAB_SIZE; ++i) {
246 2 if(ipaddr[0] == arp_table[i].ipaddr[0] &&
247 2 ipaddr[1] == arp_table[i].ipaddr[1])
248 2 break;
249 2 }
250 1
251 1 if(i == UIP_ARPTAB_SIZE) {
252 2 /* The destination address was not in our ARP table, so we
253 2 overwrite the IP packet with an ARP request. */
254 2
255 2 for(c = 0; c < 6; ++c) {
256 3 BUF->ethhdr.dest.addr[c] = 0xff; /* Broadcast ARP request. */
257 3 BUF->ethhdr.src.addr[c] =
258 3 BUF->shwaddr.addr[c] = ethaddr.addr[c];
259 3 BUF->dhwaddr.addr[c] = 0;
260 3 }
261 2
262 2 BUF->dipaddr[0] = ipaddr[0];
263 2 BUF->dipaddr[1] = ipaddr[1];
264 2 BUF->sipaddr[0] = htons((UIP_IPADDR0 << 8) | UIP_IPADDR1);
265 2 BUF->sipaddr[1] = htons((UIP_IPADDR2 << 8) | UIP_IPADDR3);
266 2 BUF->opcode = htons(ARP_REQUEST); /* ARP request. */
267 2 BUF->hwtype = htons(ARP_HWTYPE_ETH);
268 2 BUF->protocol = htons(UIP_ETHTYPE_IP);
269 2 BUF->hwlen = 6;
270 2 BUF->protolen = 4;
271 2 BUF->ethhdr.type = htons(UIP_ETHTYPE_ARP);
272 2
273 2 uip_appdata = &uip_buf[40 + UIP_LLH_LEN];
274 2
275 2 uip_len = sizeof(struct arp_hdr);
276 2 return;
277 2 }
278 1
279 1 /* Build an ethernet header. */
280 1 for(c = 0; c < 6; ++c) {
281 2 IPBUF->ethhdr.dest.addr[c] = arp_table[i].ethaddr.addr[c];
282 2 IPBUF->ethhdr.src.addr[c] = ethaddr.addr[c];
283 2 }
284 1 IPBUF->ethhdr.type = htons(UIP_ETHTYPE_IP);
285 1
286 1 uip_len += sizeof(struct uip_eth_hdr);
287 1 }
288 /*-----------------------------------------------------------------------------------*/
289
290
291
292
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1618 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = 25 6
PDATA SIZE = ---- ----
DATA SIZE = ---- ----
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
C51 COMPILER V7.06 UIP_ARP 04/05/2006 12:13:01 PAGE 6
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -