uip源碼分析[分享借鑒]_第1頁
uip源碼分析[分享借鑒]_第2頁
uip源碼分析[分享借鑒]_第3頁
已閱讀5頁,還剩48頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、uIP的ARP協(xié)議代碼分析之一 ARP請求(是根據(jù)IP地址獲取物理地址的一個TCP/IP協(xié)議 同時將IP地址和硬件地址存入本機(jī)ARP緩存中,下次請求時直接查詢ARP緩存。)對于一個設(shè)備用的ARP協(xié)議而言,重要的東西包括三方面:1. 一個本地的IP與MAC地址的緩存表.以有對應(yīng)的更新和查詢操作. 2. 一個發(fā)送ARP請求的函數(shù). 3. 一個處理ARP回復(fù)的函數(shù).下面我們來看uIP中是如何實現(xiàn)的(代碼見uip_arp.c:首先,定義一個緩存表的數(shù)據(jù)結(jié)構(gòu),99行起:struct arp_entry u16_t ipaddr2;struct uip_eth_addr ethaddr;u8_t time

2、;只有三個項,很簡單第一項是ip地址,16*2=4*8位的,保存四個八位組.第二項是MAC地址.第三項是緩存更新時間.下來是ARP請求發(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請求.* 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包時調(diào)用,它會檢查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ù)就會從ARP緩存表中查找有*無對應(yīng)項.若有,就取對應(yīng)的MAC地址,加上以太網(wǎng)頭,并返回,否則uip_buf中的數(shù)據(jù)包會被替換成一個*目的IP在址的ARP請求.原來的IP包會被簡單的仍掉,此函數(shù)假設(shè)高層協(xié)

6、議(如TCP)會最終重傳扔掉的包.* If the destination IP address is not on the local network, the IP* address of the default router is used instead.*如果目標(biāo)IP地址并非一個局域網(wǎng)IP,則會使用默認(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ù)返回時,uip_buf中已經(jīng)有了一個包,其長度由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包替換成一個ARP請求./* 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請求并返回*/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來存儲目的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請求,返回.如果已存在,則查找使用查找到的MAC地址為I

15、P包添加以太網(wǎng)頭.這里還要解釋一些細(xì)節(jié)問題,主要是:1. 如何在IP包上添加以太網(wǎng)頭 2. 如果將IP包替換成ARP請求,ARP請求的格式是什么. 3. 廣播地址這些問題將在二樓來說.將IP包替換成ARP請求部分代碼(實際上IP包是放在uip_buf里的,這里只是將uip_buf填充上ARP請求即可),于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,只不過這里將它取做一個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的第一個成員ethhdr的類型定義,對應(yīng)圖片中的前三項:6+6+2,目的以太網(wǎng)地址,源以太網(wǎng)地址,2字節(jié)數(shù)據(jù)類型(ARP請求和應(yīng)答為0x0806).struct arp_hdr的第二個成員u16_t hwtype,對應(yīng)圖片中第三項,2字節(jié)硬件類型(值為1表示以太網(wǎng)).struct arp_hdr的第三個成員u16_t protocol,對應(yīng)圖片中第四項,2字節(jié)要映射的協(xié)議地址類型(ip地址為0x0800).struct arp_hdr的第四

20、個成員u8_t hwlen,對應(yīng)圖片中第五項,1字節(jié)硬件地址長度(對MAC地址來說為6).struct arp_hdr的第五個成員u8_t protolen,對應(yīng)圖片中第六項,1字節(jié)協(xié)議地址長度(對IP地址來說為4).struct arp_hdr的第六個成員u16_t opcode,對應(yīng)圖片中第七項,2字節(jié)操作碼(1 ARP請求,2 ARP應(yīng)答,3 RARP請求,4 RARP應(yīng)答,必須字段).struct arp_hdr的第七個成員struct uip_eth_addr shwaddr,對應(yīng)圖片中第八項,6字節(jié)源以太網(wǎng)地址.struct arp_hdr的第八個成員u16_t sipaddr2;

21、,對應(yīng)圖片中第九項,2字節(jié)源IP地址.struct arp_hdr的第九個成員struct uip_eth_addr dhwaddr,對應(yīng)圖片中第十項,6字節(jié)目標(biāo)以太網(wǎng)地址.struct arp_hdr的第十個成員u16_t dipaddr2;,對應(yīng)圖片中第十一項,2字節(jié)目標(biāo)IP地址.上面綠色的表示已經(jīng)詳解的,紅字的表示要進(jìn)一步說明的.這就是一個ARP幀的結(jié)構(gòu),可以看到,里面的源和目的以太網(wǎng)地址都是重復(fù)的.我們再看函數(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);這里四個memset,重復(fù)的源和目的以太網(wǎng)地址一起設(shè)置了,四個memset對應(yīng)圖片中的1,10,2,8項.但是:對1和10兩項,都是源以太網(wǎng)地址,但置的值是不同的,分別為0xff*6和0x00*6.為什么會這樣呢?因為他們的用處不一樣,見:【相關(guān)資料】ARP分組格式(幀格式,報文格式)6+6 以太網(wǎng)的源地址和目的地址,目的地址為全1的為廣播地址注意這里說,目的地址為全為1的廣播地址.什么意思?當(dāng)你發(fā)送一個ARP請求是,你是想知道一

23、個IP對應(yīng)的以太網(wǎng)地址(即MAC地址),但是你現(xiàn)在不知道目的主機(jī)的以太網(wǎng)地址,你問誰啊?不知道問誰,這種情況下,只能是廣播一下了,0xff*6就是廣播地址.從圖片中可以看到,ARP包是分成兩部分的,前6+6+2叫做以太網(wǎng)首部,它的意義就是分組是從誰(源地址)發(fā)給誰(目的地址)的什么類型(幀類型,請求和應(yīng)答為0x0806),第二部分則是內(nèi)容.來看這個例子:請求幀如下(為了清晰在每行的前面加了字節(jié)計數(shù),每行16個字節(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表示請

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)容沒有定義,與具體實現(xiàn)相關(guān)。 uIP的ARP協(xié)議代碼分析之二 ARP應(yīng)答 如果讀懂了上面的文章,這里的理解就相對容易了.ARP應(yīng)答部分代碼為uip_arp.c中的void uip_arp_arpin(void)函數(shù).這個函數(shù)是在設(shè)備接收到ARP包時,由驅(qū)動

26、程序調(diào)用的.如果收到是ARP包是一個對本地主機(jī)上次發(fā)送的ARP請求的應(yīng)答,那么就從包中取得自己想要的主機(jī)的MAC地址,加入自己的ARP緩存表中.如果收到是一個ARP請求,那就把自己的MAC地址打包成一個ARP應(yīng)答,發(fā)送給請求的主機(jī).看代碼uip_arp.c的254行:1. /*-*/2. /*3. * ARP processing for incoming ARP packets.4. *對傳入的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è)備驅(qū)動調(diào)用,函數(shù)行為會因包類型而有不同.如果收到的是一個對前先發(fā)送的請求的應(yīng)答13. *則根據(jù)應(yīng)答的值填充緩存.如果傳入的包是對我們的IP的請求,則創(chuàng)建一個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ù)返回時,全局變量uip_len的值指明了設(shè)備驅(qū)動要不要發(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中有一個帶以太網(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. 如果是一個ARP請求,則發(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ā)送請求的主機(jī)注冊到ARP緩存表中,因為我們很可能要跟它要有更多的交流 */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. 如果收到的是一個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、某個主機(jī)請求得到我們的MAC的地址,我們先把它的MAC地址加入到自己的表中.就好比社交網(wǎng)絡(luò)中,別人請求加我們?yōu)楹糜?如果我們接收的話,也自動加對方為好友一樣.既然對方找上我們了,肯定是要做進(jìn)一步的交流,互加MAC地址也很自然的.有了上一篇文章,這里似乎不必做過多的解釋了.但還是說一下吧,看看我們怎么做應(yī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ù)制代碼由于請求和應(yīng)答包很多地方是相同的,如上文中的綠色部分.我們只需將收到的請求稍加修改就可以發(fā)送回去了.首先,要改一下MAC地址,四個地方.然后要將目標(biāo)主機(jī)IP設(shè)為設(shè)為請求包的源主機(jī)IP,目的主機(jī)IP設(shè)為我們的IP.就可以了.另外說下對ARP緩存表的設(shè)置:1. 這個函數(shù)是集插入,更新一體的,有兩個參數(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映射表,看看有沒有需要更新的表項,如果沒有,就將新的表項插入.*/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有沒有匹配項.*/18. if(ipaddr0 = tabptr-ipaddr0 &19. ipaddr1 = tabptr-ipaddr1) 20.21. /* An old entry found, update this and return. 如果有己存的匹配項,修改該項的MAC地址和更新時間.*/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. 如果運行到這里,說明沒有己存的表項,則創(chuàng)建一個.*/32.33. /* First, we try to find an unused entry in the ARP table. 先看看有沒有空表項可用.*/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. 如果沒空表項,就找到一個最舊的表項,扔掉它,換成我們的.*/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就是我們最終得到的表項,我們把新的信息插入到這里.*/59. memcpy(tabptr-ipaddr, ipaddr, 4);60. memcpy(tabptr-ethaddr.addr, ethaddr-addr, 6);61. tabptr-time = arptime;復(fù)制代碼OK,uip_arp.c中還有兩個函數(shù),樓下繼續(xù).最后兩個函數(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)該每隔一段時間就調(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表項中的更新時間是一個數(shù),每調(diào)用一個上面這個函數(shù),這個數(shù)就加1.這個

45、周期性處理函數(shù)的作用就是每隔一段時間把超過20分鐘都沒有更新過的表項扔掉.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ū)動需要使用ARP協(xié)議(Address Resolution Protocal),比如說在以太網(wǎng)內(nèi)使用uip的時候,你就得在

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)周期性處理一連接,需借助其連接號。此函數(shù)對uip的tcp連接進(jìn)行一些必要的周期性處理(定時器,輪詢等),它應(yīng)該在周期性uip定時器消息到來時被調(diào)用。它應(yīng)該對每一個連接都得到調(diào)用,不管連接是打開的還是關(guān)閉的。此函數(shù)返回時,可能會有要傳送出去的數(shù)據(jù)包等待得到服務(wù),包內(nèi)容會被放到uip包緩沖區(qū)中。如果真的有數(shù)據(jù)包需要送出,則uip_len的值會被置為一個大于零的數(shù),此時設(shè)備驅(qū)動應(yīng)該將數(shù)據(jù)包發(fā)送出去。(這一段與(1)中相應(yīng)段落的意思應(yīng)該是一致的。)通常調(diào)用此函數(shù)時,會使用一個如下的for循環(huán):1. for(i = 0; i 0) 4. devicedriver_send();5. 6. 注意:如果你所寫的設(shè)備驅(qū)動需要使用ARP協(xié)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論