{"id":761,"date":"2011-06-06T18:15:10","date_gmt":"2011-06-06T10:15:10","guid":{"rendered":"http:\/\/www.kumouse.com\/?p=761"},"modified":"2011-06-06T18:15:10","modified_gmt":"2011-06-06T10:15:10","slug":"arp-%e5%8a%9f%e5%87%bb%e4%b8%8e%e6%8a%93%e5%8c%85%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.kumouse.com\/?p=761","title":{"rendered":"ARP \u529f\u51fb\u4e0e\u6293\u5305\u4ee3\u7801"},"content":{"rendered":"<p>\u4e0a\u8bfe\u7684\u5730\u65b9arp\u6a2a\u884c\uff0c\u603b\u662f\u4e0a\u4e0d\u53bb\u7f51\u3002<br \/>\n\t\u7814\u7a76\u4e86\u4e00\u4e0barp\u529f\u51fb\u7684\u4ee3\u7801\u3002\u603b\u7528\u66f4\u9ad8\u7684\u9891\u7387\u6765\u89e3\u51b3\u529f\u51fb\u7f51\u5173\u7684\u95ee\u9898\uff0c\u5931\u8d25\u544a\u7ec8\u3002<\/p>\n<p>\u6293\u5305<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n\t#include &lt;stdlib.h&gt;<br \/>\n\t#include &lt;sys\/socket.h&gt;<br \/>\n\t#include &lt;sys\/types.h&gt;<br \/>\n\t#include &lt;string.h&gt;<br \/>\n\t#include &lt;linux\/if_packet.h&gt;<br \/>\n\t#include &lt;linux\/if_ether.h&gt;<br \/>\n\t#include &lt;arpa\/inet.h&gt;<br \/>\n\t#include &lt;net\/if.h&gt;<br \/>\n\t#include &lt;errno.h&gt;<br \/>\n\t#include &lt;sys\/ioctl.h&gt;<br \/>\n\t#include &lt;unistd.h&gt;<br \/>\n\t#include &lt;sys\/socket.h&gt;<br \/>\n\t#include &lt;arpa\/inet.h&gt;<\/p>\n<p>void print_arp(unsigned char *a,int len)<br \/>\n\t{<br \/>\n\t&nbsp;int i;<br \/>\n\t&nbsp;char ccc=&#39;1&#39;;<br \/>\n\t&nbsp;for(i=0;i&lt;len;i++){<br \/>\n\t&nbsp;&nbsp;if(i==6 || i==12 || i==14 || i==16 || i==18 || i==19 || i==20 || i==22 || i==28 || i==32 || i==38 || i==42)<br \/>\n\t&nbsp;&nbsp;&nbsp;putchar(&#39;|&#39;);<br \/>\n\t&nbsp;&nbsp;if((i&gt;=28 &amp;&amp; i&lt;=31) || (i&gt;=38 &amp;&amp; i&lt;=41))<br \/>\n\t&nbsp;&nbsp;&nbsp;printf(&quot;%d.&quot;,a[i]);<br \/>\n\t&nbsp;&nbsp;else<br \/>\n\t&nbsp;&nbsp;&nbsp;printf(&quot;%02x&quot;,a[i]);<br \/>\n\t&nbsp;&nbsp;\/\/fflush(stdout);<br \/>\n\t&nbsp;}<br \/>\n\t&nbsp;putchar(&#39;n&#39;);<br \/>\n\t}<br \/>\n\tvoid print_eth(unsigned char *a,int len)<br \/>\n\t{<br \/>\n\t&nbsp;int i;<br \/>\n\t&nbsp;for(i=0;i&lt;len;i++){<br \/>\n\t&nbsp;&nbsp;printf(&quot;%02x&quot;,a[i]);<br \/>\n\t&nbsp;}<br \/>\n\t&nbsp;putchar(&#39;n&#39;);<br \/>\n\t}<\/p>\n<p>int set_promisc(char *interface, int fd) {<br \/>\n\t&nbsp;struct ifreq ifr;<br \/>\n\t&nbsp;strcpy(ifr.ifr_name, interface);<br \/>\n\t&nbsp;if(ioctl(fd, SIOCGIFFLAGS, &amp;ifr) == -1) {<br \/>\n\t&nbsp;&nbsp;perror(&quot;iotcl()&quot;);<br \/>\n\t&nbsp;&nbsp;return -1;<br \/>\n\t&nbsp;}<br \/>\n\t&nbsp;ifr.ifr_flags |= IFF_PROMISC;<br \/>\n\t&nbsp;if(ioctl(fd, SIOCSIFFLAGS, &amp;ifr) == -1) {<br \/>\n\t&nbsp;&nbsp;perror(&quot;iotcl()&quot;);<br \/>\n\t&nbsp;&nbsp;return -1;<br \/>\n\t&nbsp;}<br \/>\n\t&nbsp;return 0;<br \/>\n\t}<\/p>\n<p>int main(int argc, char **argv) {<br \/>\n\t&nbsp;int sock, n;<br \/>\n\t&nbsp;unsigned char buffer[2048];<br \/>\n\t&nbsp;unsigned char *iphead, *ethhead;<br \/>\n\t&nbsp;struct sockaddr_ll sll;<\/p>\n<p>&nbsp;\/\/&nbsp;if(argc != 3){<br \/>\n\t&nbsp;\/\/&nbsp;&nbsp;printf(&quot;need interface name and protocol as argumentsn&quot;);<br \/>\n\t&nbsp;\/\/&nbsp;&nbsp;return -1;<br \/>\n\t&nbsp;\/\/&nbsp;}<\/p>\n<p>&nbsp;if ( (sock=socket(PF_PACKET, SOCK_RAW,<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;htons(ETH_P_ARP)))&lt;0) {<br \/>\n\t&nbsp;&nbsp;perror(&quot;socket&quot;);<br \/>\n\t&nbsp;&nbsp;exit(1);<br \/>\n\t&nbsp;}<br \/>\n\t&nbsp;sll.sll_family = PF_PACKET;<br \/>\n\t&nbsp;\/\/&nbsp;&nbsp;&nbsp; sll.sll_ifindex = Get_IfaceIndex(sock,argv[1]); \/\/\u901a\u8fc7\u6b64\u5904\u4f20\u5165\u7f51\u7edc\u8bbe\u5907\u63a5\u53e3<br \/>\n\t&nbsp;struct ifreq ifstruct;<br \/>\n\t&nbsp;strcpy(ifstruct.ifr_name, &quot;eth0&quot;);<br \/>\n\t&nbsp;\/\/sll.sll_protocol = htons(atoi(argv[2]));<br \/>\n\t&nbsp;sll.sll_protocol=htons(ETH_P_ARP);<\/p>\n<p>&nbsp;if(bind(sock,(struct sockaddr *)(&amp;sll),sizeof(sll))==-1)<br \/>\n\t&nbsp;{<br \/>\n\t&nbsp;&nbsp;printf(&quot;bind error:%s !n&quot;,strerror(errno));<br \/>\n\t&nbsp;&nbsp;return -1;<br \/>\n\t&nbsp;}<\/p>\n<p>&nbsp;\/\/int set_promisc(char *interface, int fd) {<br \/>\n\t&nbsp;if(set_promisc(&quot;eth0&quot;,sock) == -1)<br \/>\n\t&nbsp;{<br \/>\n\t&nbsp;&nbsp;printf(&quot;BLUE set promisc failed !n&quot;);<br \/>\n\t&nbsp;&nbsp;return -1;<br \/>\n\t&nbsp;}<\/p>\n<p>&nbsp;while (1) {<br \/>\n\t&nbsp;&nbsp;printf(&quot;&#8212;&#8211;recive start&#8212;&#8211;n&quot;);<br \/>\n\t&nbsp;&nbsp;n = recvfrom(sock,buffer,2048,0,NULL,NULL);<br \/>\n\t&nbsp;&nbsp;printf(&quot;%d bytes readn&quot;,n);<br \/>\n\t&nbsp;&nbsp;printf(&quot;index:%dn&quot;,sll.sll_ifindex );<br \/>\n\t&nbsp;&nbsp;\/* Check to see if the packet contains at least<br \/>\n\t&nbsp;&nbsp; * complete Ethernet (14), IP (20) and TCP\/UDP<br \/>\n\t&nbsp;&nbsp; * (8) headers.<br \/>\n\t&nbsp;&nbsp; *\/<br \/>\n\t&nbsp;&nbsp;if (n&lt;42) {<br \/>\n\t&nbsp;&nbsp;&nbsp;perror(&quot;recvfrom():&quot;);<br \/>\n\t&nbsp;&nbsp;&nbsp;printf(&quot;Incomplete packet (errno is %d)n&quot;,<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errno);<br \/>\n\t&nbsp;&nbsp;&nbsp;close(sock);<br \/>\n\t&nbsp;&nbsp;&nbsp;exit(0);<br \/>\n\t&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;ethhead = buffer;<br \/>\n\t&nbsp;&nbsp;printf(&quot;Destination MAC address: &quot;<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&quot;%02x:%02x:%02x:%02x:%02x:%02xn&quot;,<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;ethhead[0],ethhead[1],ethhead[2],<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;ethhead[3],ethhead[4],ethhead[5]);<br \/>\n\t&nbsp;&nbsp;printf(&quot;Source MAC address: &quot;<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&quot;%02x:%02x:%02x:%02x:%02x:%02xn&quot;,<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;ethhead[6],ethhead[7],ethhead[8],<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;ethhead[9],ethhead[10],ethhead[11]);<br \/>\n\t&nbsp;&nbsp;printf(&quot;protocal:&quot;<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&quot;0x%02x%02xn&quot;,ethhead[12],ethhead[13]);<\/p>\n<p>&nbsp;&nbsp;iphead = buffer+14; \/* Skip Ethernet header *\/<br \/>\n\t&nbsp;&nbsp;if (*iphead==0x45) { \/* Double check for IPv4<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * and no options present *\/<br \/>\n\t&nbsp;&nbsp;&nbsp;printf(&quot;Source host %d.%d.%d.%dn&quot;,<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iphead[12],iphead[13],<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iphead[14],iphead[15]);<br \/>\n\t&nbsp;&nbsp;&nbsp;printf(&quot;Dest host %d.%d.%d.%dn&quot;,<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iphead[16],iphead[17],<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iphead[18],iphead[19]);<br \/>\n\t&nbsp;&nbsp;&nbsp;printf(&quot;Source,Dest ports %d,%dn&quot;,<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(iphead[20]&lt;&lt;8)+iphead[21],<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(iphead[22]&lt;&lt;8)+iphead[23]);<br \/>\n\t&nbsp;&nbsp;&nbsp;printf(&quot;Layer-4 protocol %dn&quot;,iphead[9]);<br \/>\n\t&nbsp;&nbsp;}<br \/>\n\t&nbsp;&nbsp;\/\/print_eth(1,buffer,512);<br \/>\n\t&nbsp;&nbsp;print_arp(ethhead,48);<br \/>\n\t&nbsp;&nbsp;\/\/&nbsp;&nbsp;print_eth(ethhead,n);<br \/>\n\t&nbsp;}<\/p>\n<p>}<br \/>\n\t&nbsp;<\/p>\n<p>\u529f\u51fb<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n\t#include &lt;stdlib.h&gt;<br \/>\n\t#include &lt;sys\/socket.h&gt;<br \/>\n\t#include &lt;sys\/types.h&gt;<br \/>\n\t#include &lt;string.h&gt;<br \/>\n\t#include &lt;linux\/if_packet.h&gt;<br \/>\n\t#include &lt;linux\/if_ether.h&gt;<br \/>\n\t#include &lt;arpa\/inet.h&gt;<br \/>\n\t#include &lt;net\/if.h&gt;<br \/>\n\t#include &lt;errno.h&gt;<br \/>\n\t#include &lt;sys\/ioctl.h&gt;<br \/>\n\t#include &lt;unistd.h&gt;<br \/>\n\t#include &lt;sys\/socket.h&gt;<br \/>\n\t#include &lt;arpa\/inet.h&gt;<\/p>\n<p>void print_eth(unsigned char *a,int len)<br \/>\n\t{<br \/>\n\t&nbsp;int i;<br \/>\n\t&nbsp;for(i=0;i&lt;len;i++){<br \/>\n\t&nbsp;&nbsp;printf(&quot;%02x&quot;,a[i]);<br \/>\n\t&nbsp;}<br \/>\n\t&nbsp;putchar(&#39;n&#39;);<br \/>\n\t}<br \/>\n\tint set_promisc(char *interface, int fd) {<br \/>\n\t&nbsp;struct ifreq ifr;<br \/>\n\t&nbsp;strcpy(ifr.ifr_name, interface);<br \/>\n\t&nbsp;if(ioctl(fd, SIOCGIFFLAGS, &amp;ifr) == -1) {<br \/>\n\t&nbsp;&nbsp;perror(&quot;iotcl()&quot;);<br \/>\n\t&nbsp;&nbsp;return -1;<br \/>\n\t&nbsp;}<br \/>\n\t&nbsp;ifr.ifr_flags |= IFF_PROMISC;<br \/>\n\t&nbsp;if(ioctl(fd, SIOCSIFFLAGS, &amp;ifr) == -1) {<br \/>\n\t&nbsp;&nbsp;perror(&quot;iotcl()&quot;);<br \/>\n\t&nbsp;&nbsp;return -1;<br \/>\n\t&nbsp;}<br \/>\n\t&nbsp;return 0;<br \/>\n\t}<\/p>\n<p>int main(int argc, char **argv){<br \/>\n\t&nbsp;int sock;<br \/>\n\t&nbsp;char SendBuffer[64];<br \/>\n\t&nbsp;char intfname[16];<br \/>\n\t&nbsp;struct sockaddr_ll dest;<br \/>\n\t&nbsp;struct sockaddr_ll sll;<\/p>\n<p>&nbsp;memset(&amp;dest,0,sizeof(dest));<br \/>\n\t&nbsp;memset(&amp;sll,0,sizeof(sll));<br \/>\n\t&nbsp;strcpy(intfname,argv[1]);<br \/>\n\t&nbsp;dest.sll_family=AF_PACKET;<br \/>\n\t&nbsp;dest.sll_protocol=htons(ETH_P_ALL);<br \/>\n\t&nbsp;if ( (sock=socket(PF_PACKET, SOCK_RAW,<br \/>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;htons(ETH_P_ALL)))&lt;0) {<br \/>\n\t&nbsp;&nbsp;perror(&quot;socket&quot;);<br \/>\n\t&nbsp;&nbsp;exit(1);<br \/>\n\t&nbsp;}<\/p>\n<p>&nbsp;sll.sll_family = AF_PACKET;<br \/>\n\t&nbsp;\/\/&nbsp;sll.sll_ifindex = Get_IfaceIndex(sock,intfname);<br \/>\n\t&nbsp;struct ifreq ifstruct;<br \/>\n\t&nbsp;strcpy(ifstruct.ifr_name, &quot;eth0&quot;); <br \/>\n\t&nbsp;sll.sll_protocol = htons(ETH_P_ALL);<br \/>\n\t&nbsp;dest.sll_ifindex =sll.sll_ifindex;<br \/>\n\t&nbsp;dest.sll_halen = 6;<br \/>\n\t&nbsp;memcpy((char*)dest.sll_addr,SendBuffer,6);<\/p>\n<p>&nbsp;if(bind(sock,(struct sockaddr *)(&amp;sll),sizeof(sll))==-1)<br \/>\n\t&nbsp;{<br \/>\n\t&nbsp;&nbsp;printf(&quot;bind error!!n&quot;);<br \/>\n\t&nbsp;&nbsp;return 0;<br \/>\n\t&nbsp;}<\/p>\n<p>&nbsp;if(set_promisc(&quot;eth0&quot;,sock) == -1)<br \/>\n\t&nbsp;{<br \/>\n\t&nbsp;&nbsp;printf(&quot;BLUE set promisc failed !n&quot;);<br \/>\n\t&nbsp;&nbsp;return 0;<br \/>\n\t&nbsp;}<\/p>\n<p>&nbsp;printf(&quot;nnnn&#8212;-send start&#8212;&#8212;n&quot;);<br \/>\n\t&nbsp;print_eth(SendBuffer,64);<br \/>\n\t&nbsp;sendto(sock,&amp;SendBuffer,64,0,(struct sockaddr *)(&amp;dest),sizeof(dest));<br \/>\n\t&nbsp;\/\/printf(&quot;send to %x:%x:%x:%x:%x:%xn&quot;,dest.sll_addr[0],dest.sll_addr[1],dest.sll_addr[2],dest.sll_addr[3],dest.sll_addr[4],dest.sll_addr[5]);<br \/>\n\t&nbsp;printf(&quot;&#8212;send success&#8212;-n&quot;);<\/p>\n<p>&nbsp;return 0;<br \/>\n\t}<br \/>\n\t&nbsp;<\/p>\n<p>\u539f\u6587\u4ef6\u4e0b\u8f7d:<\/p>\n<p><a href=\"http:\/\/www.kumouse.com\/wp-content\/uploads\/2011\/06\/tt21.c\">tt2<\/a>&nbsp;<a href=\"http:\/\/www.kumouse.com\/wp-content\/uploads\/2011\/06\/tt1.c\">tt1<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u8bfe\u7684\u5730\u65b9arp\u6a2a\u884c\uff0c\u603b\u662f\u4e0a\u4e0d\u53bb\u7f51\u3002 \u7814\u7a76\u4e86\u4e00\u4e0barp\u529f\u51fb\u7684\u4ee3\u7801\u3002\u603b\u7528\u66f4\u9ad8\u7684\u9891\u7387\u6765\u89e3\u51b3\u529f\u51fb\u7f51\u5173\u7684\u95ee\u9898\uff0c\u5931\u8d25\u544a\u7ec8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-761","post","type-post","status-publish","format-standard","hentry","category-linux-c"],"_links":{"self":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts\/761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=761"}],"version-history":[{"count":0,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts\/761\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}