{"id":12,"date":"2016-03-27T00:04:51","date_gmt":"2016-03-26T16:04:51","guid":{"rendered":"http:\/\/www.91tfboys.com\/?p=12"},"modified":"2017-01-03T21:28:45","modified_gmt":"2017-01-03T13:28:45","slug":"%e5%8f%91%e9%80%81%e5%88%86%e6%9e%90arp","status":"publish","type":"post","link":"https:\/\/www.91tfboys.com\/?p=12","title":{"rendered":"\u53d1\u9001\u5206\u6790ARP"},"content":{"rendered":"<pre>\u6ce8\uff1a\u6ce8\u91ca\u6709\u4e9b\u5730\u65b9\u6709\u8bef\uff0c\u4f46\u6574\u4f53\u4e0d\u5f71\u54cd\r\n\/************************************************************\r\n * ARP\u5b9e\u9a8c\t\t\t\t\t\t\t\t\t\t\t\t\t*\r\n * 2016\u5e743\u670820\u65e5\t\t\t\t\t\t\t\t\t\t\t*\r\n * 130342132\t\t\t\t\t\t\t\t\t\t\t\t*\r\n ************************************************************\/\r\n<!--more-->\r\n#include \"pcap.h\"\r\n\r\ntypedef unsigned char u8_t;  \/*\u65e0\u7b26\u53f78bit\u957f\u5ea6\u7c7b\u578b*\/\r\ntypedef unsigned short u16_t; \/*\u65e0\u7b26\u53f716 bit\u957f\u5ea6\u7c7b\u578b*\/\r\ntypedef unsigned long u32_t; \/*\u65e0\u7b26\u53f732 bit\u957f\u5ea6\u7c7b\u578b*\/\r\n\r\n\/\/ \u4ee5\u592a\u7f51\u5934\u90e8\r\nstruct ether_header\r\n{\r\n\tu8_t ether_dhost[6]; \/*\u76ee\u7684\u4ee5\u592a\u7f51\u5730\u57406B*\/\r\n\tu8_t ether_shost[6]; \/*\u6e90\u4ee5\u592a\u7f51\u5730\u57406B*\/\r\n\tu16_t ether_type; \/*\u4ee5\u592a\u7f51\u7c7b\u578b\u5b57\u6bb52B*\/\r\n};\r\n\r\n\/\/ ARP\u62a5\u6587\u7ed3\u6784\r\nstruct mac_addr {\r\n\tu8_t addr[6];\r\n};\r\nstruct arp_head {\r\n\tu16_t hardType;\t\t\t\t\/\/ \u786c\u4ef6\u7c7b\u578b\uff080x0001\uff09\r\n\tu16_t protoType;\t\t\t\/\/ \u534f\u8bae\u7c7b\u578b(IP\u534f\u8bae0x0800)\r\n\tu8_t hardLen;\t\t\t\t\/\/ \u786c\u4ef6\u5730\u5740\u957f\u5ea60x06\r\n\tu8_t protoLen;\t\t\t\t\/\/ IP\u5730\u5740\u957f\u5ea60x04\r\n\tu16_t operCode;\t\t\t\t\/\/ arp\u62a5\u6587\u7c7b\u578b\uff081\u8bf7\u6c422\u5e94\u7b54\uff090x0001\r\n\tu8_t s_hardAddr[6];\t\t\t\/\/ \u6e90MAC\uff086B\uff09\r\n\tu8_t s_ipAddr[4];\t\t\t\/\/ \u6e90IP\t4B\r\n\tu8_t d_hardAddr[6];\t\t\t\/\/ \u76ee\u7684MAC\t6B\r\n\tu8_t d_ipAddr[4];\t\t\t\/\/ \u76ee\u7684IP\t4B\r\n};\r\nstruct arp_packet {\r\n\tstruct ether_header eth;\t\t\/\/ Ethernet\u5934\u90e8\r\n\tstruct arp_head arp;\t\t\t\/\/ arp\u6570\u636e\u5e27\u9996\u90e8\r\n};\r\n\r\nvoid packet_handler(u_char *user_data, const struct pcap_pkthdr *header, const u_char *pkt_content);\r\n\r\nint main()\r\n{\r\n\tpcap_if_t *alldevs;\r\n\tpcap_if_t *d;\r\n\tint inum;\r\n\tint i=0;\r\n\tpcap_t *adhandle;\r\n\tchar errbuf[PCAP_ERRBUF_SIZE];\r\n\t\r\n\tu8_t *send_buff;   \/*\u5f85\u53d1\u9001\u7684\u6570\u636e\u5e27*\/\r\n\tint packet_len;\r\n\t\r\n\t\/*\u521b\u5efa\u4ee5\u592a\u7f51\u5e27\u7684\u9996\u90e8\u7ed3\u6784*\/\r\n\tstruct arp_packet arp_pkt;\t\/\/ \u5305\r\n\t\r\n    \/* \u83b7\u53d6\u672c\u673a\u8bbe\u5907\u5217\u8868 *\/\r\n    if (pcap_findalldevs(&amp;alldevs, errbuf) == -1)\r\n    {\r\n        fprintf(stderr,\"Error in pcap_findalldevs: %s\\n\", errbuf);\r\n        exit(1);\r\n    }\r\n    \r\n    \/* \u6253\u5370\u5217\u8868 *\/\r\n    for(d=alldevs; d; d=d-&gt;next)\r\n    {\r\n        printf(\"%d. %s\", ++i, d-&gt;name);\r\n        if (d-&gt;description)\r\n            printf(\" (%s)\\n\", d-&gt;description);\r\n        else\r\n            printf(\" (No description available)\\n\");\r\n    }\r\n    \r\n    if(i==0)\r\n    {\r\n        printf(\"\\nNo interfaces found! Make sure WinPcap is installed.\\n\");\r\n        return -1;\r\n    }\r\n    \r\n    printf(\"Enter the interface number (1-%d):\",i);\r\n    scanf(\"%d\", &amp;inum);\r\n    \r\n    if(inum &lt; 1 || inum &gt; i)\r\n    {\r\n        printf(\"\\nInterface number out of range.\\n\");\r\n        \/* \u91ca\u653e\u8bbe\u5907\u5217\u8868 *\/\r\n        pcap_freealldevs(alldevs);\r\n        return -1;\r\n    }\r\n    \r\n    \/* \u8df3\u8f6c\u5230\u9009\u4e2d\u7684\u9002\u914d\u5668 *\/\r\n    for(d=alldevs, i=0; i&lt; inum-1 ;d=d-&gt;next, i++);\r\n    \r\n    \/* \u6253\u5f00\u8bbe\u5907 *\/\r\n    if ( (adhandle= pcap_open_live(d-&gt;name,          \/\/ \u8bbe\u5907\u540d\r\n\t\t65536,            \/\/ 65535\u4fdd\u8bc1\u80fd\u6355\u83b7\u5230\u4e0d\u540c\u6570\u636e\u94fe\u8def\u5c42\u4e0a\u7684\u6bcf\u4e2a\u6570\u636e\u5305\u7684\u5168\u90e8\u5185\u5bb9\r\n\t\t1,    \/\/ \u6df7\u6742\u6a21\u5f0f\r\n\t\t1000,             \/\/ \u8bfb\u53d6\u8d85\u65f6\u65f6\u95f4\r\n\t\t\/\/    NULL,             \/\/ \u8fdc\u7a0b\u673a\u5668\u9a8c\u8bc1\r\n\t\terrbuf            \/\/ \u9519\u8bef\u7f13\u51b2\u6c60\r\n\t\t) ) == NULL)\r\n    {\r\n        fprintf(stderr,\"\\nUnable to open the adapter. %s is not supported by WinPcap\\n\", d-&gt;name);\r\n        \/* \u91ca\u653e\u8bbe\u5907\u5217\u8868 *\/\r\n        pcap_freealldevs(alldevs);\r\n        return -1;\r\n    }\r\n    \r\n\t\/*\u6784\u9020\u4ee5\u592a\u7f51\u5e27\u9996\u90e8*\/\r\n\tarp_pkt.eth.ether_dhost[0] = 0xFF;\r\n\tarp_pkt.eth.ether_dhost[1] = 0xFF;\r\n\tarp_pkt.eth.ether_dhost[2] = 0xFF;\r\n\tarp_pkt.eth.ether_dhost[3] = 0xFF;\r\n\tarp_pkt.eth.ether_dhost[4] = 0xFF;\r\n\tarp_pkt.eth.ether_dhost[5] = 0xFF;\r\n\t\r\n    \/*\u6e90MAC\u5730\u5740\uff0c\u672c\u673aA4-DB-30-AB-A3-DE*\/\r\n\tarp_pkt.eth.ether_shost[0] = 0xA4;\r\n\tarp_pkt.eth.ether_shost[1] = 0xDB;\r\n\tarp_pkt.eth.ether_shost[2] = 0x30;\r\n\tarp_pkt.eth.ether_shost[3] = 0xAB;\r\n\tarp_pkt.eth.ether_shost[4] = 0xA3;\r\n\tarp_pkt.eth.ether_shost[5] = 0xDE;\r\n\r\n\tarp_pkt.eth.ether_type = htons(0x0806);\r\n\t\r\n\t\/\/ \u6784\u9020\u6211\u7684ARP\r\n\tfor (i=0; i &lt; 6; i++)\r\n\t{\r\n\t\tarp_pkt.arp.s_hardAddr[i] = arp_pkt.eth.ether_shost[i];\t\/\/ \u672c\u673a\uff08\u6e90\uff09MAC\r\n\t\/\/\tarp_pkt.arp.s_hardAddr.addr[i] = arp_pkt.eth.ether_shost[i];\t\/\/ \u672c\u673a\uff08\u6e90\uff09MAC\r\n\t\tarp_pkt.arp.d_hardAddr[i] = 0x00;\t\/\/ \u76ee\u7684MAC\u51680\r\n\t}\r\n\tarp_pkt.arp.hardType = htons(0x0001);\r\n\tarp_pkt.arp.protoType = htons(0x0800);\r\n\tarp_pkt.arp.hardLen = 0x06;\r\n\tarp_pkt.arp.protoLen = 0x04;\r\n\tarp_pkt.arp.operCode = htons(0x0001);\r\n\tarp_pkt.arp.d_ipAddr[0] = 10;\r\n\tarp_pkt.arp.d_ipAddr[1] = 5;\r\n\tarp_pkt.arp.d_ipAddr[2] = 71;\r\n\tarp_pkt.arp.d_ipAddr[3] = 104;\t\/\/ \u6e90IP\r\n\tarp_pkt.arp.s_ipAddr[0] = 10;\r\n\tarp_pkt.arp.s_ipAddr[1] = 5;\r\n\tarp_pkt.arp.s_ipAddr[2] = 71;\r\n\tarp_pkt.arp.s_ipAddr[3] = 102;\t\/\/ \u76ee\u7684IP\r\n\r\n\t\/*\/\u7533\u8bf7\u53d1\u9001\u7f13\u51b2\u533a\uff0c\u7528\u4e8e\u5b58\u50a8\u5f85\u53d1\u9001\u7684\u6570\u636e*\/\r\n\tpacket_len = sizeof(struct arp_packet);\r\n\t\r\n\tsend_buff = (u8_t *)malloc(packet_len);\r\n\t\r\n\t\/\/\u5c06\u4ee5\u592a\u7f51\u5e27\u9996\u90e8\u7ed3\u6784\u548c\u8d1f\u8f7d\u62f7\u8d1d\u5230\u53d1\u9001\u7f13\u51b2\u533a\r\n\tmemcpy(send_buff, &amp;arp_pkt, sizeof(struct arp_packet));\r\n\t\r\n\t\/\/\u53d1\u9001\u6570\u636e\u5e27\r\n\tfor(i=0;i&lt;10;i++) \r\n\t{\r\n\t\tif(pcap_sendpacket(adhandle,send_buff,packet_len)!=0)\r\n\t\t{\r\n\t\t\tfprintf(stderr,\"\\n Error sending the packet:%s\\n\",pcap_geterr(adhandle)); \r\n\t\t\treturn -5; \r\n\t\t} \r\n\t} \r\n\t\/* \u91ca\u653e\u8bbe\u5907\u5217\u8868 *\/ \r\n\tpcap_freealldevs(alldevs); \r\n\tpcap_loop(adhandle, 0, packet_handler, NULL); \r\n\tpcap_close(adhandle); \r\n\treturn 0; \r\n} \r\nvoid packet_handler(u_char *user_data, const struct pcap_pkthdr *header, const u_char *pkt_content) \r\n{ \r\n\tu_short ethernet_type; \/\/ \u4ee5\u592a\u7f51\u7c7b\u578b \r\n\tstruct arp_packet *sarp_pkt; \r\n\t\/\/ struct ether_header *ethernet_protocol; \/\/ \u4ee5\u592a\u7f51\u9996\u90e8\u534f\u8bae \r\n\tu_char *mac_addr; \r\n\tu_char *ip_addr; \r\n\tstatic int packet_number = 1; \/\/ \u7edf\u8ba1\u6355\u83b7\u62a5\u6587\u4e2a\u6570 \r\n\tsarp_pkt = (struct arp_packet*)pkt_content; \/\/ \u83b7\u5f97\u5e27\u9996\u90e8\u4fe1\u606f \r\n\tethernet_type = ntohs(sarp_pkt-&gt;eth.ether_type);\t\r\n\t\/\/ ethernet_protocol-&gt;ether_type);\r\n\t\/\/ printf(\"%04x\", ethernet_type);\r\n\tif (ethernet_type == 0x0806)\r\n\t{\r\n\t\tprintf(\"*******************************************\\n\");\r\n\t\tprintf(\"\u6355\u83b7\u7b2c%d\u4e2aARP\u5305\\n\", packet_number);\r\n\t\tprintf(\"\u6355\u83b7\u65f6\u95f4\\t%s\\n\", ctime((const time_t*)&amp;header-&gt;ts.tv_sec));\r\n\t\tprintf(\"\u6570\u636e\u5305\u7684\u957f\u5ea6\\t%d\\n\", header-&gt;len);\r\n\t\t\r\n\t\tprintf(\"\u6e90\u5730\u5740\\t\\t\");\r\n\t\tmac_addr = sarp_pkt-&gt;eth.ether_shost;\r\n\t\tprintf(\"%02x:%02x:%02x:%02x:%02x:%02x\\n\", *mac_addr, *(mac_addr + 1),\r\n\t\t\t*(mac_addr + 2), *(mac_addr + 3), *(mac_addr + 4), *(mac_addr + 5));\r\n\t\tprintf(\"\u6e90IP\\t\\t\");\r\n\t\tip_addr = sarp_pkt-&gt;arp.s_ipAddr;\r\n\t\tprintf(\"%i.%i.%i.%i\\n\", *ip_addr, *(ip_addr + 1), *(ip_addr + 2), *(ip_addr + 3));\r\n\t\tprintf(\"\u76ee\u7684\u5730\u5740\\t\");\r\n\t\tmac_addr = sarp_pkt-&gt;eth.ether_dhost;\r\n\t\tprintf(\"%02x:%02x:%02x:%02x:%02x:%02x\\n\", *mac_addr, *(mac_addr + 1),\r\n\t\t\t*(mac_addr + 2), *(mac_addr + 3), *(mac_addr + 4), *(mac_addr + 5));\r\n\t\tprintf(\"\u76ee\u7684IP\\t\\t\");\r\n\t\tip_addr = sarp_pkt-&gt;arp.d_ipAddr;\r\n\t\tprintf(\"%i.%i.%i.%i\\n\", *ip_addr, *(ip_addr + 1), *(ip_addr + 2), *(ip_addr + 3));\r\n\t\tprintf(\"************************************\\n\");\r\n\t\tpacket_number++;\r\n\t\tsystem(\"pause\");\r\n\t}\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u6ce8\u91ca\u6709\u4e9b\u5730\u65b9\u6709\u8bef\uff0c\u4f46\u6574\u4f53\u4e0d\u5f71\u54cd \/************************************ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":247,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-12","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding"],"_links":{"self":[{"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12"}],"version-history":[{"count":2,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/posts\/12\/revisions"}],"predecessor-version":[{"id":36,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/posts\/12\/revisions\/36"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/media\/247"}],"wp:attachment":[{"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}