版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、uIP的ARP協(xié)議代碼分析之一 ARP請(qǐng)求(是根據(jù)IP地址獲取物理地址的一個(gè)TCP/IP協(xié)議 同時(shí)將IP地址和硬件地址存入本機(jī)ARP緩存中,下次請(qǐng)求時(shí)直接查詢ARP緩存。)對(duì)于一個(gè)設(shè)備用的ARP協(xié)議而言,重要的東西包括三方面:1. 一個(gè)本地的IP與MAC地址的緩存表.以有對(duì)應(yīng)的更新和查詢操作. 2. 一個(gè)發(fā)送ARP請(qǐng)求的函數(shù). 3. 一個(gè)處理ARP回復(fù)的函數(shù).下面我們來看uIP中是如何實(shí)現(xiàn)的(代碼見uip_arp.c:首先,定義一個(gè)緩存表的數(shù)據(jù)結(jié)構(gòu),99行起:struct arp_entry u16_t ipaddr2;struct uip_eth_addr ethaddr;u8_t time
2、;只有三個(gè)項(xiàng),很簡單第一項(xiàng)是ip地址,16*2=4*8位的,保存四個(gè)八位組.第二項(xiàng)是MAC地址.第三項(xiàng)是緩存更新時(shí)間.下來是ARP請(qǐng)求發(fā)送函數(shù):uip_arp.c L325/*-*/* Prepend Ethernet header to an outbound IP packet and see if we need* to send out an ARP request.*為傳出的IP包添加以太網(wǎng)頭并看是否需要發(fā)送ARP請(qǐng)求.* This function should be called before sending out an IP packet. The* function che
3、cks the destination IP address of the IP packet to see* what Ethernet MAC address that should be used as a destination MAC* address on the Ethernet.*此函數(shù)應(yīng)該在發(fā)送IP包時(shí)調(diào)用,它會(huì)檢查IP包的目的IP地址,看看以太網(wǎng)應(yīng)該使用什么目的MAC地址.* If the destination IP address is in the local network (determined* by logical ANDing of netmask and
4、our IP address), the function* checks the ARP cache to see if an entry for the destination IP* address is found. If so, an Ethernet header is prepended and the* function returns. If no ARP cache entry is found for the* destination IP address, the packet in the uip_buf is replaced by* an ARP request
5、packet for the IP address. The IP packet is dropped* and it is assumed that they higher level protocols (e.g., TCP)* eventually will retransmit the dropped packet.*如果目的IP地址是在局域網(wǎng)中(由IP地址與子網(wǎng)掩碼的與邏輯決定),函數(shù)就會(huì)從ARP緩存表中查找有*無對(duì)應(yīng)項(xiàng).若有,就取對(duì)應(yīng)的MAC地址,加上以太網(wǎng)頭,并返回,否則uip_buf中的數(shù)據(jù)包會(huì)被替換成一個(gè)*目的IP在址的ARP請(qǐng)求.原來的IP包會(huì)被簡單的仍掉,此函數(shù)假設(shè)高層協(xié)
6、議(如TCP)會(huì)最終重傳扔掉的包.* If the destination IP address is not on the local network, the IP* address of the default router is used instead.*如果目標(biāo)IP地址并非一個(gè)局域網(wǎng)IP,則會(huì)使用默認(rèn)路由的IP地址.* When the function returns, a packet is present in the uip_buf* buffer, and the length of the packet is in the global variable* uip_le
7、n.函數(shù)返回時(shí),uip_buf中已經(jīng)有了一個(gè)包,其長度由uip_len指定.*/*-*/voiduip_arp_out(void)struct arp_entry *tabptr;/* Find the destination IP address in the ARP table and constructthe Ethernet header. If the destination IP addres isnt on thelocal network, we use the default routers IP address instead./在ARP表中找到目的IP地址,構(gòu)成以太網(wǎng)頭.
8、如果目的IP地址不在局域網(wǎng)中,則使用默認(rèn)/路由的IP.If not ARP table entry is found, we overwrite the original IPpacket with an ARP request for the IP address. */如果ARP表中找不到,則將原來的IP包替換成一個(gè)ARP請(qǐng)求./* First check if destination is a local broadcast. 首先檢查目標(biāo)是不是廣播*/if(uip_ipaddr_cmp(IPBUF-destipaddr, broadcast_ipaddr) memcpy(IPBUF-e
9、thhdr.dest.addr, broadcast_ethaddr.addr, 6); else /* Check if the destination address is on the local network. 檢查目標(biāo)地址是否在局域網(wǎng)內(nèi) */if(!uip_ipaddr_maskcmp(IPBUF-destipaddr, uip_hostaddr, uip_netmask) /* Destination address was not on the local network, so we need touse the default routers IP address inst
10、ead of the destinationaddress when determining the MAC address. 目的地址不在局域網(wǎng)內(nèi),所以保用默認(rèn)路由器的地址來確在MAC地址*/uip_ipaddr_copy(ipaddr, uip_draddr); else /* Else, we use the destination IP address. 否則,使用目標(biāo)IP地址*/uip_ipaddr_copy(ipaddr, IPBUF-destipaddr);for(i = 0; i ipaddr) break;if(i = UIP_ARPTAB_SIZE) /* The dest
11、ination address was not in our ARP table, so weoverwrite the IP packet with an ARP request. 如果遍歷到頭沒找到,將原IP包替換為ARP請(qǐng)求并返回*/memset(BUF-ethhdr.dest.addr, 0xff, 6);memset(BUF-dhwaddr.addr, 0x00, 6);memcpy(BUF-ethhdr.src.addr, uip_ethaddr.addr, 6);memcpy(BUF-shwaddr.addr, uip_ethaddr.addr, 6);uip_ipaddr_co
12、py(BUF-dipaddr, ipaddr);uip_ipaddr_copy(BUF-sipaddr, uip_hostaddr);BUF-opcode = HTONS(ARP_REQUEST); /* ARP request. */BUF-hwtype = HTONS(ARP_HWTYPE_ETH);BUF-protocol = HTONS(UIP_ETHTYPE_IP);BUF-hwlen = 6;BUF-protolen = 4;BUF-ethhdr.type = HTONS(UIP_ETHTYPE_ARP);uip_appdata = &uip_bufUIP_TCPIP_HLEN +
13、 UIP_LLH_LEN;uip_len = sizeof(struct arp_hdr);return;/* Build an ethernet header. 如果是在局域網(wǎng)中,且在ARP緩存中找到了(如果沒找到進(jìn)行不到這一步,在上面就返回了),則構(gòu)建以太網(wǎng)頭*/memcpy(IPBUF-ethhdr.dest.addr, tabptr-ethaddr.addr, 6);memcpy(IPBUF-ethhdr.src.addr, uip_ethaddr.addr, 6);IPBUF-ethhdr.type = HTONS(UIP_ETHTYPE_IP);uip_len += sizeof(
14、struct uip_eth_hdr);以上內(nèi)容自325行起.下面再總結(jié)一下其基本順序:用IPBUF-ethhdr.dest.addr來存儲(chǔ)目的IP地址,它有可能是局域網(wǎng)內(nèi)一主機(jī)IP,也可能是路由器IP(如果是發(fā)往外網(wǎng),即原來的目的IP不在局域網(wǎng)內(nèi)),還有可能是廣播專用的IP.先看是不是在廣播:如果是廣播,將IPBUF-ethhdr.dest.addr設(shè)為廣播IP.再看是不是在局域網(wǎng)內(nèi):如果不是,則將IPBUF-ethhdr.dest.addr設(shè)為路由器IP.如果在局域網(wǎng)內(nèi),查看是否已經(jīng)存在于ARP緩存表中:如果不在,將要發(fā)送的換成ARP請(qǐng)求,返回.如果已存在,則查找使用查找到的MAC地址為I
15、P包添加以太網(wǎng)頭.這里還要解釋一些細(xì)節(jié)問題,主要是:1. 如何在IP包上添加以太網(wǎng)頭 2. 如果將IP包替換成ARP請(qǐng)求,ARP請(qǐng)求的格式是什么. 3. 廣播地址這些問題將在二樓來說.將IP包替換成ARP請(qǐng)求部分代碼(實(shí)際上IP包是放在uip_buf里的,這里只是將uip_buf填充上ARP請(qǐng)求即可),于uip_arp.c的388行:/* The destination address was not in our ARP table, so we overwrite the IP packet with an ARP request. */ memset(BUF-ethhdr.dest.ad
16、dr, 0xff, 6); memset(BUF-dhwaddr.addr, 0x00, 6); memcpy(BUF-ethhdr.src.addr, uip_ethaddr.addr, 6); memcpy(BUF-shwaddr.addr, uip_ethaddr.addr, 6); uip_ipaddr_copy(BUF-dipaddr, ipaddr); uip_ipaddr_copy(BUF-sipaddr, uip_hostaddr); BUF-opcode = HTONS(ARP_REQUEST); /* ARP request. */ BUF-hwtype = HTONS(A
17、RP_HWTYPE_ETH); BUF-protocol = HTONS(UIP_ETHTYPE_IP); BUF-hwlen = 6; BUF-protolen = 4; BUF-ethhdr.type = HTONS(UIP_ETHTYPE_ARP); uip_appdata = &uip_bufUIP_TCPIP_HLEN + UIP_LLH_LEN; uip_len = sizeof(struct arp_hdr); return;首先解釋這里的BUF(于uip_arp.c的116行):#define BUF (struct arp_hdr *)&uip_buf0)可見這里的BUF就是
18、uip_buf,只不過這里將它取做一個(gè)struct arp_hdr的結(jié)構(gòu)體:struct arp_hdr struct uip_eth_hdr ethhdr; u16_t hwtype; /硬件類型 u16_t protocol; /協(xié)議類型 u8_t hwlen; u8_t protolen; u16_t opcode; /操作碼 struct uip_eth_addr shwaddr; /源以太網(wǎng)地址 u16_t sipaddr2; /源IP地址 struct uip_eth_addr dhwaddr; /目的以太網(wǎng)地址 u16_t dipaddr2; /目的IP地址;struct uip
19、_eth_hdr struct uip_eth_addr dest;struct uip_eth_addr src;u16_t type;這是arp_hdr的第一個(gè)成員ethhdr的類型定義,對(duì)應(yīng)圖片中的前三項(xiàng):6+6+2,目的以太網(wǎng)地址,源以太網(wǎng)地址,2字節(jié)數(shù)據(jù)類型(ARP請(qǐng)求和應(yīng)答為0x0806).struct arp_hdr的第二個(gè)成員u16_t hwtype,對(duì)應(yīng)圖片中第三項(xiàng),2字節(jié)硬件類型(值為1表示以太網(wǎng)).struct arp_hdr的第三個(gè)成員u16_t protocol,對(duì)應(yīng)圖片中第四項(xiàng),2字節(jié)要映射的協(xié)議地址類型(ip地址為0x0800).struct arp_hdr的第四
20、個(gè)成員u8_t hwlen,對(duì)應(yīng)圖片中第五項(xiàng),1字節(jié)硬件地址長度(對(duì)MAC地址來說為6).struct arp_hdr的第五個(gè)成員u8_t protolen,對(duì)應(yīng)圖片中第六項(xiàng),1字節(jié)協(xié)議地址長度(對(duì)IP地址來說為4).struct arp_hdr的第六個(gè)成員u16_t opcode,對(duì)應(yīng)圖片中第七項(xiàng),2字節(jié)操作碼(1 ARP請(qǐng)求,2 ARP應(yīng)答,3 RARP請(qǐng)求,4 RARP應(yīng)答,必須字段).struct arp_hdr的第七個(gè)成員struct uip_eth_addr shwaddr,對(duì)應(yīng)圖片中第八項(xiàng),6字節(jié)源以太網(wǎng)地址.struct arp_hdr的第八個(gè)成員u16_t sipaddr2;
21、,對(duì)應(yīng)圖片中第九項(xiàng),2字節(jié)源IP地址.struct arp_hdr的第九個(gè)成員struct uip_eth_addr dhwaddr,對(duì)應(yīng)圖片中第十項(xiàng),6字節(jié)目標(biāo)以太網(wǎng)地址.struct arp_hdr的第十個(gè)成員u16_t dipaddr2;,對(duì)應(yīng)圖片中第十一項(xiàng),2字節(jié)目標(biāo)IP地址.上面綠色的表示已經(jīng)詳解的,紅字的表示要進(jìn)一步說明的.這就是一個(gè)ARP幀的結(jié)構(gòu),可以看到,里面的源和目的以太網(wǎng)地址都是重復(fù)的.我們?cè)倏春瘮?shù)中的代碼:memset(BUF-ethhdr.dest.addr, 0xff, 6);memset(BUF-dhwaddr.addr, 0x00, 6);memcpy(BUF-e
22、thhdr.src.addr, uip_ethaddr.addr, 6);memcpy(BUF-shwaddr.addr, uip_ethaddr.addr, 6);這里四個(gè)memset,重復(fù)的源和目的以太網(wǎng)地址一起設(shè)置了,四個(gè)memset對(duì)應(yīng)圖片中的1,10,2,8項(xiàng).但是:對(duì)1和10兩項(xiàng),都是源以太網(wǎng)地址,但置的值是不同的,分別為0xff*6和0x00*6.為什么會(huì)這樣呢?因?yàn)樗麄兊挠锰幉灰粯?見:【相關(guān)資料】ARP分組格式(幀格式,報(bào)文格式)6+6 以太網(wǎng)的源地址和目的地址,目的地址為全1的為廣播地址注意這里說,目的地址為全為1的廣播地址.什么意思?當(dāng)你發(fā)送一個(gè)ARP請(qǐng)求是,你是想知道一
23、個(gè)IP對(duì)應(yīng)的以太網(wǎng)地址(即MAC地址),但是你現(xiàn)在不知道目的主機(jī)的以太網(wǎng)地址,你問誰啊?不知道問誰,這種情況下,只能是廣播一下了,0xff*6就是廣播地址.從圖片中可以看到,ARP包是分成兩部分的,前6+6+2叫做以太網(wǎng)首部,它的意義就是分組是從誰(源地址)發(fā)給誰(目的地址)的什么類型(幀類型,請(qǐng)求和應(yīng)答為0x0806),第二部分則是內(nèi)容.來看這個(gè)例子:請(qǐng)求幀如下(為了清晰在每行的前面加了字節(jié)計(jì)數(shù),每行16個(gè)字節(jié)):以太網(wǎng)首部(14字節(jié))0000: ff ff ff ff ff ff 00 05 5d 61 58 a8 08 06ARP幀(28字節(jié))0000: 00 010010: 08 00
24、 06 04 00 01 00 05 5d 61 58 a8 c0 a8 00 370020: 00 00 00 00 00 00 c0 a8 00 02填充位(18字節(jié))0020: 00 77 31 d2 50 100030: fd 78 41 d3 00 00 00 00 00 00 00 00以太網(wǎng)首部: 目的主機(jī)采用廣播地址,源主機(jī)的MAC地址是00:05:5d:61:58:a8,上層協(xié)議類型0x0806表示ARP。ARP幀: 硬件類型0x0001表示以太網(wǎng), 協(xié)議類型0x0800表示IP協(xié)議, 硬件地址(MAC地址)長度為6, 協(xié)議地址(IP地址)長度為4, op為0x0001表示請(qǐng)
25、求目的主機(jī)的MAC地址, 源主機(jī)MAC地址為00:05:5d:61:58:a8,源主機(jī)IP地址為c0 a8 00 37(192.168.0.55), 目的主機(jī)MAC地址全0待填寫,目的主機(jī)IP地址為c0 a8 00 02(192.168.0.2)。由于以太網(wǎng)規(guī)定最小數(shù)據(jù)長度為46字節(jié),ARP幀長度只有28字節(jié),因此有18字節(jié)填充位,填充位的內(nèi)容沒有定義,與具體實(shí)現(xiàn)相關(guān)。 uIP的ARP協(xié)議代碼分析之二 ARP應(yīng)答 如果讀懂了上面的文章,這里的理解就相對(duì)容易了.ARP應(yīng)答部分代碼為uip_arp.c中的void uip_arp_arpin(void)函數(shù).這個(gè)函數(shù)是在設(shè)備接收到ARP包時(shí),由驅(qū)動(dòng)
26、程序調(diào)用的.如果收到是ARP包是一個(gè)對(duì)本地主機(jī)上次發(fā)送的ARP請(qǐng)求的應(yīng)答,那么就從包中取得自己想要的主機(jī)的MAC地址,加入自己的ARP緩存表中.如果收到是一個(gè)ARP請(qǐng)求,那就把自己的MAC地址打包成一個(gè)ARP應(yīng)答,發(fā)送給請(qǐng)求的主機(jī).看代碼uip_arp.c的254行:1. /*-*/2. /*3. * ARP processing for incoming ARP packets.4. *對(duì)傳入的ARP包的處理.5. * This function should be called by the device driver when an ARP6. * packet has been rec
27、eived. The function will act differently7. * depending on the ARP packet type: if it is a reply for a request8. * that we previously sent out, the ARP cache will be filled in with9. * the values from the ARP reply. If the incoming ARP packet is an ARP10. * request for our IP address, an ARP reply pa
28、cket is created and put11. * into the uip_buf buffer.12. *此函數(shù)在收到ARP包時(shí)由設(shè)備驅(qū)動(dòng)調(diào)用,函數(shù)行為會(huì)因包類型而有不同.如果收到的是一個(gè)對(duì)前先發(fā)送的請(qǐng)求的應(yīng)答13. *則根據(jù)應(yīng)答的值填充緩存.如果傳入的包是對(duì)我們的IP的請(qǐng)求,則創(chuàng)建一個(gè)ARP應(yīng)答,并放入uip_buf中.14. * When the function returns, the value of the global variable uip_len15. * indicates whether the device driver should send out a
29、packet or16. * not. If uip_len is zero, no packet should be sent. If uip_len is17. * non-zero, it contains the length of the outbound packet that is18. * present in the uip_buf buffer.19. *函數(shù)返回時(shí),全局變量uip_len的值指明了設(shè)備驅(qū)動(dòng)要不要發(fā)送包.若uip_len為0,則不需發(fā)送,若uip_len不是0,20. * 則其值是uip_buf中包含的要傳出的包的大小.21. * This function
30、 expects an ARP packet with a prepended Ethernet22. * header in the uip_buf buffer, and the length of the packet in the23. * global variable uip_len.此函數(shù)預(yù)期中的uip_buf中有一個(gè)帶以太網(wǎng)頭的ARP包.其長度存為uip_len中.24. */25. /*-*/26. void27. uip_arp_arpin(void)28. 29.30. if(uip_len opcode) 37. case HTONS(ARP_REQUEST):38.
31、/* ARP request. If it asked for our address, we send out a39. reply. 如果是一個(gè)ARP請(qǐng)求,則發(fā)送應(yīng)答.*/40. if(uip_ipaddr_cmp(BUF-dipaddr, uip_hostaddr) 41. /* First, we register the one who made the request in our ARP42. table, since it is likely that we will do more communication43. with this host in the future.首
32、先,我們將發(fā)送請(qǐng)求的主機(jī)注冊(cè)到ARP緩存表中,因?yàn)槲覀兒芸赡芤懈嗟慕涣?*/44. uip_arp_update(BUF-sipaddr, &BUF-shwaddr);45.46. /* The reply opcode is 2. 應(yīng)答的操作碼為2*/47. BUF-opcode = HTONS(2);48.49. memcpy(BUF-dhwaddr.addr, BUF-shwaddr.addr, 6);50. memcpy(BUF-shwaddr.addr, uip_ethaddr.addr, 6);51. memcpy(BUF-ethhdr.src.addr, uip_eth
33、addr.addr, 6);52. memcpy(BUF-ethhdr.dest.addr, BUF-dhwaddr.addr, 6);53.54. BUF-dipaddr0 = BUF-sipaddr0;55. BUF-dipaddr1 = BUF-sipaddr1;56. BUF-sipaddr0 = uip_hostaddr0;57. BUF-sipaddr1 = uip_hostaddr1;58.59. BUF-ethhdr.type = HTONS(UIP_ETHTYPE_ARP);60. uip_len = sizeof(struct arp_hdr);61. 62. break;
34、63. case HTONS(ARP_REPLY):64. /* ARP reply. We insert or update the ARP table if it was meant65. for us. 如果收到的是一個(gè)ARP應(yīng)答,而且也是我們所要的應(yīng)答的話,就插件并更新ARP緩存表*/66. if(uip_ipaddr_cmp(BUF-dipaddr, uip_hostaddr) 67. uip_arp_update(BUF-sipaddr, &BUF-shwaddr);68. 69. break;70. 71.72. return;73. 復(fù)制代碼這里有一件事是很有意思的,就是說如果
35、某個(gè)主機(jī)請(qǐng)求得到我們的MAC的地址,我們先把它的MAC地址加入到自己的表中.就好比社交網(wǎng)絡(luò)中,別人請(qǐng)求加我們?yōu)楹糜?如果我們接收的話,也自動(dòng)加對(duì)方為好友一樣.既然對(duì)方找上我們了,肯定是要做進(jìn)一步的交流,互加MAC地址也很自然的.有了上一篇文章,這里似乎不必做過多的解釋了.但還是說一下吧,看看我們?cè)趺醋鰬?yīng)答的.如果收到了一個(gè)請(qǐng)求,我們要做應(yīng)答:1. /* The reply opcode is 2. */2. BUF-opcode = HTONS(2);3.4. memcpy(BUF-dhwaddr.addr, BUF-shwaddr.addr, 6);5. memcpy(BUF-shwaddr
36、.addr, uip_ethaddr.addr, 6);6. memcpy(BUF-ethhdr.src.addr, uip_ethaddr.addr, 6);7. memcpy(BUF-ethhdr.dest.addr, BUF-dhwaddr.addr, 6);8.9. BUF-dipaddr0 = BUF-sipaddr0;10. BUF-dipaddr1 = BUF-sipaddr1;11. BUF-sipaddr0 = uip_hostaddr0;12. BUF-sipaddr1 = uip_hostaddr1;13.14. BUF-ethhdr.type = HTONS(UIP_E
37、THTYPE_ARP);15. uip_len = sizeof(struct arp_hdr);復(fù)制代碼由于請(qǐng)求和應(yīng)答包很多地方是相同的,如上文中的綠色部分.我們只需將收到的請(qǐng)求稍加修改就可以發(fā)送回去了.首先,要改一下MAC地址,四個(gè)地方.然后要將目標(biāo)主機(jī)IP設(shè)為設(shè)為請(qǐng)求包的源主機(jī)IP,目的主機(jī)IP設(shè)為我們的IP.就可以了.另外說下對(duì)ARP緩存表的設(shè)置:1. 這個(gè)函數(shù)是集插入,更新一體的,有兩個(gè)參數(shù),IP地址,MAC地址.2. static void3. uip_arp_update(u16_t *ipaddr, struct uip_eth_addr *ethaddr)4. 5. regi
38、ster struct arp_entry *tabptr;6. /* Walk through the ARP mapping table and try to find an entry to7. update. If none is found, the IP - MAC address mapping is8. inserted in the ARP table. 遍歷ARP映射表,看看有沒有需要更新的表項(xiàng),如果沒有,就將新的表項(xiàng)插入.*/9. for(i = 0; i ipaddr0 != 0 &14. tabptr-ipaddr1 != 0) 15.16. /* Check if
39、the source IP address of the incoming packet matches17. the IP address in this ARP table entry. 看看傳入的IP有沒有匹配項(xiàng).*/18. if(ipaddr0 = tabptr-ipaddr0 &19. ipaddr1 = tabptr-ipaddr1) 20.21. /* An old entry found, update this and return. 如果有己存的匹配項(xiàng),修改該項(xiàng)的MAC地址和更新時(shí)間.*/22. memcpy(tabptr-ethaddr.addr, ethaddr-add
40、r, 6);23. tabptr-time = arptime;24.25. return;26. 27. 28. 29.30. /* If we get here, no existing ARP table entry was found, so we31. create one. 如果運(yùn)行到這里,說明沒有己存的表項(xiàng),則創(chuàng)建一個(gè).*/32.33. /* First, we try to find an unused entry in the ARP table. 先看看有沒有空表項(xiàng)可用.*/34. for(i = 0; i ipaddr0 = 0 &37. tabptr-ipaddr1 =
41、 0) 38. break;39. 40. 41.42. /* If no unused entry is found, we try to find the oldest entry and43. throw it away. 如果沒空表項(xiàng),就找到一個(gè)最舊的表項(xiàng),扔掉它,換成我們的.*/44. if(i = UIP_ARPTAB_SIZE) 45. tmpage = 0;46. c = 0;47. for(i = 0; i time tmpage) 50. tmpage = arptime - tabptr-time;51. c = i;52. 53. 54. i = c;55. tabpt
42、r = &arp_tablei;56. 57. /* Now, i is the ARP table entry which we will fill with the new58. information. 現(xiàn)在i就是我們最終得到的表項(xiàng),我們把新的信息插入到這里.*/59. memcpy(tabptr-ipaddr, ipaddr, 4);60. memcpy(tabptr-ethaddr.addr, ethaddr-addr, 6);61. tabptr-time = arptime;復(fù)制代碼OK,uip_arp.c中還有兩個(gè)函數(shù),樓下繼續(xù).最后兩個(gè)函數(shù):1.看代碼:1. /*-*/2.
43、/*3. * Periodic ARP processing function.4. *ARP周期性處理函數(shù).5. * This function performs periodic timer processing in the ARP module6. * and should be called at regular intervals. The recommended interval7. * is 10 seconds between the calls.8. *此函數(shù)在ARP模塊中施行周期性處理,它應(yīng)該每隔一段時(shí)間就調(diào)用一次.推薦為10秒.9. */10. /*-*/11. voi
44、d12. uip_arp_timer(void)13. 14. struct arp_entry *tabptr;15. 16. +arptime;17. for(i = 0; i ipaddr0 | tabptr-ipaddr1) != 0 &20. arptime - tabptr-time = UIP_ARP_MAXAGE) 21. memset(tabptr-ipaddr, 0, 4);22. 23. 24. 復(fù)制代碼1. #define UIP_ARP_MAXAGE 120/即20分鐘.復(fù)制代碼從這里可以看出,ARP表項(xiàng)中的更新時(shí)間是一個(gè)數(shù),每調(diào)用一個(gè)上面這個(gè)函數(shù),這個(gè)數(shù)就加1.這個(gè)
45、周期性處理函數(shù)的作用就是每隔一段時(shí)間把超過20分鐘都沒有更新過的表項(xiàng)扔掉.2.看代碼:1. /*-*/2. /*3. * Initialize the ARP module.初始化ARP模塊.4. *5. */6. /*-*/7. void8. uip_arp_init(void)9. 10. for(i = 0; i 0) 3. uip_input();4. if(uip_len 0) 5. devicedriver_send();6. 7. 注意:如果你寫的uip設(shè)備驅(qū)動(dòng)需要使用ARP協(xié)議(Address Resolution Protocal),比如說在以太網(wǎng)內(nèi)使用uip的時(shí)候,你就得在
46、調(diào)用此函數(shù)之前先調(diào)用uip的ARP代碼:1. #define BUF (struct uip_eth_hdr *)&uip_buf0)2. uip_len = ethernet_devicedrver_poll();3. if(uip_len 0) 4. if(BUF-type = HTONS(UIP_ETHTYPE_IP) 5. uip_arp_ipin();6. uip_input();7. if(uip_len 0) 8. uip_arp_out();9. ethernet_devicedriver_send();10. 11. else if(BUF-type = HTONS(UIP_
47、ETHTYPE_ARP) 12. uip_arp_arpin();13. if(uip_len 0) 14. ethernet_devicedriver_send();15. 16. 使用例程:example-mainloop-with-arp.c, and example-mainloop-without-arp.c. 此接口定義于uip.h的第257行。2. #define uip_periodic(conn)周期性處理一連接,需借助其連接號(hào)。此函數(shù)對(duì)uip的tcp連接進(jìn)行一些必要的周期性處理(定時(shí)器,輪詢等),它應(yīng)該在周期性u(píng)ip定時(shí)器消息到來時(shí)被調(diào)用。它應(yīng)該對(duì)每一個(gè)連接都得到調(diào)用,不管連接是打開的還是關(guān)閉的。此函數(shù)返回時(shí),可能會(huì)有要傳送出去的數(shù)據(jù)包等待得到服務(wù),包內(nèi)容會(huì)被放到uip包緩沖區(qū)中。如果真的有數(shù)據(jù)包需要送出,則uip_len的值會(huì)被置為一個(gè)大于零的數(shù),此時(shí)設(shè)備驅(qū)動(dòng)應(yīng)該將數(shù)據(jù)包發(fā)送出去。(這一段與(1)中相應(yīng)段落的意思應(yīng)該是一致的。)通常調(diào)用此函數(shù)時(shí),會(huì)使用一個(gè)如下的for循環(huán):1. for(i = 0; i 0) 4. devicedriver_send();5. 6. 注意:如果你所寫的設(shè)備驅(qū)動(dòng)需要使用ARP協(xié)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年12月福建廈門市鷺江創(chuàng)新實(shí)驗(yàn)室管理序列崗位招聘8人筆試考試備考試題及答案解析
- 2025海南省水利水務(wù)發(fā)展集團(tuán)有限公司招聘5人考試筆試備考題庫及答案解析
- 2025廣西河池市天峨縣水果生產(chǎn)服務(wù)中心招聘農(nóng)民技術(shù)員1人筆試考試參考題庫及答案解析
- 2026中國農(nóng)業(yè)科學(xué)院第一批統(tǒng)一招聘(農(nóng)田灌溉研究所11人)考試筆試備考題庫及答案解析
- 中國雄安集團(tuán)有限公司2026校園招聘50人筆試考試參考試題及答案解析
- 2025年陜西德健眾普生物科技有限公司招聘(14人)筆試考試備考試題及答案解析
- 2025甘肅中醫(yī)藥大學(xué)招聘博士研究生5人(第二期)筆試考試參考試題及答案解析
- 糖醫(yī)幫基層糖尿病防治指南試題及答案
- 玻璃制品裝飾工創(chuàng)新實(shí)踐能力考核試卷含答案
- 2025年貴州省遵義市評(píng)審專家考試題庫及答案
- 物業(yè)反恐防暴培訓(xùn)
- 【完整版】2026國考《行測(cè)》真題(行政執(zhí)法)
- 2025年床上四件套市場(chǎng)調(diào)研:純棉印花需求與圖案美觀度分析
- 2025年度物流行業(yè)市場(chǎng)調(diào)研:產(chǎn)業(yè)規(guī)模、政策支持及數(shù)字化趨勢(shì)報(bào)告
- 2025年及未來5年市場(chǎng)數(shù)據(jù)中國拖拉機(jī)制造市場(chǎng)競(jìng)爭(zhēng)態(tài)勢(shì)及投資戰(zhàn)略規(guī)劃研究報(bào)告
- 廣東省廣州市越秀區(qū)2024-2025學(xué)年八年級(jí)上學(xué)期期末考試英語試題
- 地震波速反演方法-洞察及研究
- 百年未有之大變局課件
- 2025年時(shí)事政治考試100題及答案
- 應(yīng)急救援電源
- 電力行業(yè)電力工程設(shè)計(jì)師崗位招聘考試試卷及答案
評(píng)論
0/150
提交評(píng)論