ARP应答部分代码为 uip_arp.c 中的 void uip_arp_arpin(void)函数.
这个函数是在设备接收到 ARP包时,由驱动程序调用的.
如果收到是 ARP 包是一个对本地主机上次发送的 ARP请求的应答,那么就从包中取得自己想
要的主机的 MAC 地址,加入自己的 ARP缓存表中.
如果收到是一个 ARP 请求,那就把自己的 MAC地址打包成一个 ARP应答,发送给请求的主机.
看代码 uip_arp.c 的 254行:
1. /*-----------------------------------------------------------------
------------------*/
5. * This function should be called by the device driver when an ARP
6. * packet has been received. The function will act differently
7. * depending on the ARP packet type: if it is a reply for a request
8. * that we previously sent out, the ARP cache will be filled in with
9. * the values from the ARP reply. If the incoming ARP packet is an ARP
10. * request for our IP address, an ARP reply packet is created and put
11. * into the uip_buf[] buffer.
12. *此函数在收到 ARP 包时由设备驱动调用,函数行为会因包类型而有不同.如果
收到的是一个对前先发送的请求的应答
13. *则根据应答的值填充缓存.如果传入的包是对我们的 IP 的请求,则创建一个
ARP 应答,并放入 uip_buf[]中.
14. * When the function returns, the value of the global variable uip_len
15. * indicates whether the device driver should send out a packet or
16. * not. If uip_len is zero, no packet should be sent. If uip_len is
17. * non-zero, it contains the length of the outbound packet that is
18. * present in the uip_buf[] buffer.
19. *函数返回时,全局变量 uip_len 的值指明了设备驱动要不要发送包.若
uip_len 为 0,则不需发送,若 uip_len不是 0,
20. * 则其值是 uip_buf[]中包含的要传出的包的大小.
21. * This function expects an ARP packet with a prepended Ethernet
22. * header in the uip_buf[] buffer, and the length of the packet in the
23. * global variable uip_len.此函数预期中的 uip_buf 中有一个带以太网头的
ARP 包.其长度存为 uip_len 中.
25./*-----------------------------------------------------------------
------------------*/
30. if(uip_len < sizeof(struct arp_hdr)) {