碼為uip_arpc中的void uip_arp_arpin(void)函數(shù)_第1頁
碼為uip_arpc中的void uip_arp_arpin(void)函數(shù)_第2頁
碼為uip_arpc中的void uip_arp_arpin(void)函數(shù)_第3頁
碼為uip_arpc中的void uip_arp_arpin(void)函數(shù)_第4頁
碼為uip_arpc中的void uip_arp_arpin(void)函數(shù)_第5頁
免費預覽已結(jié)束,剩余1頁可下載查看

下載本文檔

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

文檔簡介

1、.ARP應答部分代碼為uip_arp.c中的void uip_arp_arpin(void)函數(shù).這個函數(shù)是在設備接收到ARP包時,由驅(qū)動程序調(diào)用的.如果收到是ARP包是一個對本地主機上次發(fā)送的ARP請求的應答,那么就從包中取得自己想要的主機的MAC地址,加入自己的ARP緩存表中.如果收到是一個ARP請求,那就把自己的MAC地址打包成一個ARP應答,發(fā)送給請求的主機.看代碼uip_arp.c的254行:1. /*-*/2. /*3. * ARP processing for incoming ARP packets.4. *對傳入的ARP包的處理.5. * This function shou

2、ld be called by the device driver when an ARP6. * packet has been received. 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

3、ARP packet is an ARP10. * request for our IP address, an ARP reply packet is created and put11. * into the uip_buf buffer.12. *此函數(shù)在收到ARP包時由設備驅(qū)動調(diào)用,函數(shù)行為會因包類型而有不同.如果收到的是一個對前先發(fā)送的請求的應答13. *則根據(jù)應答的值填充緩存.如果傳入的包是對我們的IP的請求,則創(chuàng)建一個ARP應答,并放入uip_buf中.14. * When the function returns, the value of the global variabl

4、e uip_len15. * indicates whether the device driver should send out a 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的值指明了設備驅(qū)動要不要發(fā)送包.若uip_len為

5、0,則不需發(fā)送,若uip_len不是0,20. * 則其值是uip_buf中包含的要傳出的包的大小.21. * This function 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ù)預期中的uip_buf中有一個帶以太網(wǎng)頭的ARP包.其長度存為uip_len中.24. */25. /*-*/26. void27. uip_arp_arpi

6、n(void)28. 29.30. if(uip_len < sizeof(struct arp_hdr) 31. uip_len = 0;32. return;33. 34. uip_len = 0;35.36. switch(BUF->opcode) 37. case HTONS(ARP_REQUEST):38. /* ARP request. If it asked for our address, we send out a39. reply. 如果是一個ARP請求,則發(fā)送應答.*/40. if(uip_ipaddr_cmp(BUF->dipaddr, uip_hos

7、taddr) 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.首先,我們將發(fā)送請求的主機注冊到ARP緩存表中,因為我們很可能要跟它要有更多的交流 */44. uip_arp_update(BUF->sipaddr, &BUF->shwaddr);45.46. /* The reply opcode is

8、 2. 應答的操作碼為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_ethaddr.addr, 6);52. memcpy(BUF->ethhdr.dest.addr, BUF->dhwaddr.addr, 6);53.54. BUF->dipaddr

9、0 = 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;63. case HTONS(ARP_REPLY):64. /* ARP reply. We insert or updat

10、e the ARP table if it was meant65. for us. 如果收到的是一個ARP應答,而且也是我們所要的應答的話,就插件并更新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. 復制代碼這里有一件事是很有意思的,就是說如果某個主機請求得到我們的MAC的地址,我們先把它的MAC地址加入到自己的表中.就好比社交網(wǎng)絡中

11、,別人請求加我們?yōu)楹糜?如果我們接收的話,也自動加對方為好友一樣.既然對方找上我們了,肯定是要做進一步的交流,互加MAC地址也很自然的.有了上一篇文章,這里似乎不必做過多的解釋了.但還是說一下吧,看看我們怎么做應答的.如果收到了一個請求,我們要做應答: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.addr, uip_ethaddr.addr, 6);6.

12、 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 = H

13、TONS(UIP_ETHTYPE_ARP);15. uip_len = sizeof(struct arp_hdr);復制代碼由于請求和應答包很多地方是相同的,如上文中的綠色部分.我們只需將收到的請求稍加修改就可以發(fā)送回去了.首先,要改一下MAC地址,四個地方.然后要將目標主機IP設為設為請求包的源主機IP,目的主機IP設為我們的IP.就可以了.另外說下對ARP緩存表的設置:1. 這個函數(shù)是集插入,更新一體的,有兩個參數(shù),IP地址,MAC地址.2. static void3. uip_arp_update(u16_t *ipaddr, struct uip_eth_addr *ethaddr)

14、4. 5. register 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 < UIP_ARPTAB_SIZE; +i) 10.11. tabptr = &a

15、mp;arp_tablei;12. /* Only check those entries that are actually in use. 所謂更新就是傳入?yún)?shù)IP在表中己經(jīng)存在了,這時不需要新建表項,而是要修改己存表項.只查使用中的表項,如果有些表項IP是0,那么就不是使用中的*/13. if(tabptr->ipaddr0 != 0 &&14. tabptr->ipaddr1 != 0) 15.16. /* Check if the source IP address of the incoming packet matches17. the IP addr

16、ess 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->addr, 6);23. tabptr->time = arptime;24.25.

17、 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 < UIP_ARPTAB_SIZE; +i) 35. tabptr = &arp_tablei;36. if(tabptr-&g

18、t;ipaddr0 = 0 &&37. tabptr->ipaddr1 = 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 < UIP_ARPTAB_SIZE; +i) 48. tabptr

19、= &arp_tablei;49. if(arptime - tabptr->time > tmpage) 50. tmpage = arptime - tabptr->time;51. c = i;52. 53. 54. i = c;55. tabptr = &arp_tablei;56. 57.   /* Now, i is the ARP table entry which we will fill with the new58.      information. 現(xiàn)在i就是我們最終得到的表項

20、,我們把新的信息插入到這里.*/59.   memcpy(tabptr->ipaddr, ipaddr, 4);60.   memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);61.   tabptr->time = arptime;復制代碼OK,uip_arp.c中還有兩個函數(shù),樓下繼續(xù).1.看代碼:1. /*-*/2. /*3. * Periodic ARP processing function.4. *ARP周期性處理函數(shù).5. * This function

21、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模塊中施行周期性處理,它應該每隔一段時間就調(diào)用一次.推薦為10秒.9. */10. /*-*/11. void12. uip_arp_timer(void)13. 14.   struct arp_entry *tabptr;15.   16.   +arptime;17.   for(i = 0; i &l

溫馨提示

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

評論

0/150

提交評論