📄 uip_arp.lst
字号:
207 * This function expects an IP packet with a prepended Ethernet header
208 * in the uip_buf[] buffer, and the length of the packet in the global
209 * variable uip_len.
210 */ // 驱动函数应该解决的问题
211 /*-----------------------------------------------------------------------------------*/
212 void
213 uip_arp_ipin(void)
214 {
215 1 uip_len -= sizeof(struct uip_eth_hdr); //14个字节
216 1
217 1 /* Only insert/update an entry if the source IP address of the
218 1 incoming IP packet comes from a host on the local network. */
219 1 if((IPBUF->srcipaddr[0] & uip_arp_netmask[0]) != //需要接收的ARP/IP包
220 1 (uip_hostaddr[0] & uip_arp_netmask[0])) {
221 2 return;
222 2 }
223 1 if((IPBUF->srcipaddr[1] & uip_arp_netmask[1]) !=
224 1 (uip_hostaddr[1] & uip_arp_netmask[1])) {
225 2 return;
226 2 }
227 1 uip_arp_update(IPBUF->srcipaddr, &(IPBUF->ethhdr.src));
228 1
229 1 P12=~P12; //add1
230 1
231 1 return;
232 1 }
233 /*-----------------------------------------------------------------------------------*/
234 /**
235 * ARP processing for incoming ARP packets.
236 *收到的ARP包
237 * This function should be called by the device driver when an ARP
238 * packet has been received. The function will act differently
239 * depending on the ARP packet type: if it is a reply for a request
240 * that we previously sent out, the ARP cache will be filled in with
241 * the values from the ARP reply. If the incoming ARP packet is an ARP
C51 COMPILER V8.08 UIP_ARP 08/22/2008 14:32:52 PAGE 5
242 * request for our IP address, an ARP reply packet is created and put
243 * into the uip_buf[] buffer.
244 *接收到ARP包的时候调用这个函数,对几种不的ARP包进行不同的处理,
245 如果是我发的请求的一个回复,我们将更新我们的ARP表格,如果是要求我们自己的IP地址
246 一个ARP回复包将创立,然后发送出去。
247 * When the function returns, the value of the global variable uip_len
248 * indicates whether the device driver should send out a packet or
249 * not. If uip_len is zero, no packet should be sent. If uip_len is
250 * non-zero, it contains the length of the outbound packet that is
251 * present in the uip_buf[] buffer.
252 *当这个函数返回时候,全局变量uiplen隐含了是否要发送一个包。
253 * This function expects an ARP packet with a prepended Ethernet
254 * header in the uip_buf[] buffer, and the length of the packet in the
255 * global variable uip_len.
256 */ // 驱动函数应该解决的问题
257 /*-----------------------------------------------------------------------------------*/
258 void
259 uip_arp_arpin(void)
260 {
261 1
262 1 if(uip_len < sizeof(struct arp_hdr)) {
263 2 uip_len = 0;
264 2 return;
265 2 }
266 1
267 1 uip_len = 0;
268 1
269 1 switch(BUF->opcode) {
270 2 case HTONS(ARP_REQUEST):
271 2 /* ARP request. If it asked for our address, we send out a
272 2 reply. */
273 2 if(BUF->dipaddr[0] == uip_hostaddr[0] && //IP确定是要求主机发送请求的
274 2 BUF->dipaddr[1] == uip_hostaddr[1]) {
275 3 /* The reply opcode is 2. */
276 3 BUF->opcode = HTONS(2);
277 3
278 3 memcpy(BUF->dhwaddr.addr, BUF->shwaddr.addr, 6);
279 3 memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6); //主要在于发送这个地址给对方
280 3 memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6); //主要在于发送这个地址给对方
281 3 memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6);
282 3
283 3 BUF->dipaddr[0] = BUF->sipaddr[0];
284 3 BUF->dipaddr[1] = BUF->sipaddr[1];
285 3 BUF->sipaddr[0] = uip_hostaddr[0];
286 3 BUF->sipaddr[1] = uip_hostaddr[1];
287 3
288 3 BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP);
289 3 uip_len = sizeof(struct arp_hdr); //设置发送包的长度为结构体的长度
290 3 }
291 2 break;
292 2 case HTONS(ARP_REPLY):
293 2 /* ARP reply. We insert or update the ARP table if it was meant
294 2 for us. */
295 2 if(BUF->dipaddr[0] == uip_hostaddr[0] && //确定是我要接收的数据包
296 2 BUF->dipaddr[1] == uip_hostaddr[1]) {
297 3
298 3 uip_arp_update(BUF->sipaddr, &BUF->shwaddr); //更新到ARP列表中
299 3
300 3 }
301 2 break;
302 2 }
303 1
C51 COMPILER V8.08 UIP_ARP 08/22/2008 14:32:52 PAGE 6
304 1 return;
305 1 }
306 /*-----------------------------------------------------------------------------------*/
307 /**
308 * Prepend Ethernet header to an outbound IP packet and see if we need
309 * to send out an ARP request.
310 *我准备发送一个数据包出去,但确定我要发送一个ARP请求包
311 * This function should be called before sending out an IP packet. The
312 * function checks the destination IP address of the IP packet to see
313 * what Ethernet MAC address that should be used as a destination MAC
314 * address on the Ethernet.
315 *要发送的目的地址的MAC地址不知道,而发送一个ARP请求的包
316 * If the destination IP address is in the local network (determined
317 * by logical ANDing of netmask and our IP address), the function
318 * checks the ARP cache to see if an entry for the destination IP
319 * address is found. If so, an Ethernet header is prepended and the
320 * function returns. If no ARP cache entry is found for the
321 * destination IP address, the packet in the uip_buf[] is replaced by
322 * an ARP request packet for the IP address. The IP packet is dropped
323 * and it is assumed that they higher level protocols (e.g., TCP)
324 * eventually will retransmit the dropped packet.
325 *如果ARP缓存中有目的的IP和子网地址,则不要在发送请求ARP包,如果没有,则暂时要
326 发送的IP包将被ARP包所覆盖,要TCP重新传送这个包。
327 * If the destination IP address is not on the local network, the IP
328 * address of the default router is used instead.
329 *如果目的的IP地址没有在本地网络中,则默认路由器的地址将取代它。
330 * When the function returns, a packet is present in the uip_buf[]
331 * buffer, and the length of the packet is in the global variable
332 * uip_len.
333 */
334 /*-----------------------------------------------------------------------------------*/
335 void
336 uip_arp_out(void)
337 {
338 1 struct arp_entry *tabptr;
339 1 /* Find the destination IP address in the ARP table and construct
340 1 the Ethernet header. If the destination IP addres isn't on the
341 1 local network, we use the default router's IP address instead.
342 1
343 1 If not ARP table entry is found, we overwrite the original IP
344 1 packet with an ARP request for the IP address. */
345 1
346 1 /* Check if the destination address is on the local network.这个是判断即将要发送的数据包 */
347 1 if((IPBUF->destipaddr[0] & uip_arp_netmask[0]) !=
348 1 (uip_hostaddr[0] & uip_arp_netmask[0]) ||
349 1 (IPBUF->destipaddr[1] & uip_arp_netmask[1]) !=
350 1 (uip_hostaddr[1] & uip_arp_netmask[1])) {
351 2 /* Destination address was not on the local network, so we need to
352 2 use the default router's IP address instead of the destination
353 2 address when determining the MAC address. */
354 2 ipaddr[0] = uip_arp_draddr[0];
355 2 ipaddr[1] = uip_arp_draddr[1]; //UIP.C的开始设置
356 2 } else {
357 2 /* Else, we use the destination IP address. */
358 2 ipaddr[0] = IPBUF->destipaddr[0];
359 2 ipaddr[1] = IPBUF->destipaddr[1];
360 2 }
361 1
362 1 for(i = 0; i < UIP_ARPTAB_SIZE; i++) { //改++i ->\i++
363 2 tabptr = &arp_table[i];
364 2 if(ipaddr[0] == tabptr->ipaddr[0] &&
365 2 ipaddr[1] == tabptr->ipaddr[1])
C51 COMPILER V8.08 UIP_ARP 08/22/2008 14:32:52 PAGE 7
366 2 break;
367 2 }
368 1
369 1 if(i == UIP_ARPTAB_SIZE-1) { //运行完没发现所要IP地址 改
370 2 /* The destination address was not in our ARP table, so we
371 2 overwrite the IP packet with an ARP request. */
372 2 //发送ARP包
373 2 memset(BUF->ethhdr.dest.addr, 0xff, 6); // 广播包发送
374 2 memset(BUF->dhwaddr.addr, 0x00, 6); //随意
375 2 memcpy(BUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
376 2 memcpy(BUF->shwaddr.addr, uip_ethaddr.addr, 6);
377 2
378 2 BUF->dipaddr[0] = ipaddr[0];
379 2 BUF->dipaddr[1] = ipaddr[1];
380 2 BUF->sipaddr[0] = uip_hostaddr[0];
381 2 BUF->sipaddr[1] = uip_hostaddr[1];
382 2 BUF->opcode = HTONS(ARP_REQUEST); /* ARP request. */
383 2 BUF->hwtype = HTONS(ARP_HWTYPE_ETH); //HW 硬件
384 2 BUF->protocol = HTONS(UIP_ETHTYPE_IP);
385 2 BUF->hwlen = 6;
386 2 BUF->protolen = 4;
387 2 BUF->ethhdr.type = HTONS(UIP_ETHTYPE_ARP);
388 2
389 2 uip_appdata = &uip_buf[40 + UIP_LLH_LEN]; //要改
390 2
391 2 uip_len = sizeof(struct arp_hdr);
392 2 return;
393 2 }
394 1 /* Build an ethernet header. 在缓存中IP找到了,作为IP包发送出去*/
395 1 memcpy(IPBUF->ethhdr.dest.addr, tabptr->ethaddr.addr, 6);
396 1 memcpy(IPBUF->ethhdr.src.addr, uip_ethaddr.addr, 6);
397 1
398 1 IPBUF->ethhdr.type = HTONS(UIP_ETHTYPE_IP);
399 1
400 1 uip_len += sizeof(struct uip_eth_hdr); //意思是我开始写的以太网头后面的数据
401 1 }
402 /*-----------------------------------------------------------------------------------*/
403
404 /** @} */
405 /** @} */
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1460 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = 69 ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 12
IDATA SIZE = ---- ----
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 + -