{"id":10,"date":"2016-03-26T20:31:00","date_gmt":"2016-03-26T12:31:00","guid":{"rendered":"http:\/\/www.91tfboys.com\/?p=10"},"modified":"2017-01-03T21:33:26","modified_gmt":"2017-01-03T13:33:26","slug":"%e8%a7%a3%e6%9e%90ip","status":"publish","type":"post","link":"https:\/\/www.91tfboys.com\/?p=10","title":{"rendered":"\u89e3\u6790IP"},"content":{"rendered":"<pre>\/************************************************************\r\n * IP\u5b9e\u9a8c\t\t\t\t\t\t\t\t\t\t\t\t\t*\r\n * 2016\u5e743\u670826\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\/\/ IP\u683c\u5f0f\r\nstruct ip_header\r\n{\r\n\tu8_t\tver_HeadLen;\t\t\/\/ Version (4 bits) + Internet header length (4 bits)\r\n\tu8_t\tserType;\t\t\t\/\/ Type of service \r\n\tu16_t\tlenSum;\t\t\t\t\/\/ Total length \r\n\tu16_t\tidentification;\t\t\/\/ Identification \u6807\u8bc6\r\n\tu16_t\tflags_Offset;\t\t\/\/ Flags (3 bits) + Fragment offset (13 bits) \u6807\u5fd7+\u5206\u7247\u504f\u79fb\r\n\tu8_t\t_TTL;\t\t\t\t\/\/ Time to live\r\n\tu8_t\tproto;\t\t\t\t\/\/ Protocol\r\n\tu16_t\tchecksum;\t\t\t\/\/ Header checksum\r\n\tu8_t\ts_ipAddr[4];\t\t\/\/ \u6e90IP\t4B\r\n\tu8_t\td_ipAddr[4];\t\t\/\/ \u76ee\u7684IP\t4B\r\n};\r\nstruct ip_packet {\r\n\tstruct ether_header eth;\t\t\/\/ Ethernet\u5934\u90e8\r\n\tstruct ip_header ip;\t\t\t\t\/\/ ip\u62a5\u6587\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\t\/\/\tu8_t *send_buff;   \/\/ \u5f85\u53d1\u9001\u7684\u6570\u636e\u5e27\r\n\t\/\/\tint packet_len;\r\n\t\r\n\t\/\/ \u521b\u5efa\u4ee5\u592a\u7f51\u5e27\u7684\u9996\u90e8\u7ed3\u6784\r\n\t\/\/\tstruct ip_packet ip_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\t\r\n\t\r\n\t\/\/ \u91ca\u653e\u8bbe\u5907\u5217\u8868\r\n    pcap_freealldevs(alldevs);\r\n    pcap_loop(adhandle, 0, packet_handler, NULL);\r\n    pcap_close(adhandle);\r\n    return 0;\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\n\tu_short ethernet_type;\t\t\t\/\/ \u4ee5\u592a\u7f51\u7c7b\u578b\r\n\tstruct ip_packet *sip_pkt;\r\n\t\/\/ struct ether_header *ethernet_protocol;\t\/\/ \u4ee5\u592a\u7f51\u9996\u90e8\u534f\u8bae\r\n\tu_char *mac_addr;\r\n\tu8_t version;\r\n\tu8_t headLen;\r\n\tu16_t f_o;\r\n\tu8_t DF, MF;\r\n\tu16_t offset;\r\n\tu8_t proto;\r\n\r\n\tu_char *ip_addr;\r\n\tstatic int packet_number = 1;\t\/\/ \u7edf\u8ba1\u6355\u83b7\u62a5\u6587\u4e2a\u6570\r\n\r\n\tsip_pkt = (struct ip_packet*)pkt_content;\t\/\/ \u83b7\u5f97\u5e27\u9996\u90e8\u4fe1\u606f\r\n\tethernet_type = ntohs(sip_pkt-&gt;eth.ether_type);\t\/\/ ethernet_protocol-&gt;ether_type);\r\n\r\n\tif (ethernet_type == 0x0800)\r\n\t{\r\n\t\tprintf(\"*********************************************\\n\");\r\n\t\tprintf(\"\u7b2c%d\u4e2a\u6570\u636e\u5305\\n%s\\n\", packet_number, ctime((const time_t*)&amp;header-&gt;ts.tv_sec));\t\/\/ \u6355\u83b7\u65f6\u95f4\r\n\t\r\n\t\t\/\/ MAC\u9996\u90e8\r\n\t\tprintf(\"\\tMAC\u9996\u90e8\\n\");\r\n\t\t\/\/ printf(\"\u6e90MAC\\t\\t\");\r\n\t\tmac_addr = sip_pkt-&gt;eth.ether_shost;\r\n\t\tprintf(\"\u6e90MAC\\t\\t%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\t\/\/ printf(\"\u76ee\u7684MAC\\t\");\r\n\t\tmac_addr = sip_pkt-&gt;eth.ether_dhost;\r\n\t\tprintf(\"\u76ee\u7684MAC\\t\\t%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(\"\u534f\u8bae\u7c7b\u578b\\tIP\u534f\u8bae\\n\");\r\n\r\n\t\t\/\/ IP\u9996\u90e8\r\n\t\tprintf(\"\\n\\tIP\u9996\u90e8\\n\");\r\n\t\tversion = sip_pkt-&gt;ip.ver_HeadLen &gt;&gt; 4;\r\n\t\tprintf(\"\u7248\u672c\\t\\t%d\\n\", version);\r\n\t\t\/\/ \u5934\u90e8\u957f\u5ea6\r\n\t\theadLen = (sip_pkt-&gt;ip.ver_HeadLen &amp; 0x0f) *4;\r\n\t\tprintf(\"\u5934\u90e8\u957f\u5ea6\\t%d\\n\", headLen);\r\n\t\t\/\/ \u603b\u957f\u5ea6\r\n\t\tprintf(\"\u603b\u957f\u5ea6\\t\\t%d\\n\", ntohs(sip_pkt-&gt;ip.lenSum));\r\n\t\t\/\/ \u6807\u8bc6\u7b26\r\n\t\tprintf(\"\u6807\u8bc6\\t\\t%d\\n\", ntohs(sip_pkt-&gt;ip.identification));\r\n\t\tf_o = ntohs(sip_pkt-&gt;ip.flags_Offset);\r\n\t\t\/\/ \u6807\u5fd7\u4f4d\r\n\t\tDF = (f_o &gt;&gt; 14) &amp; 0x03;\r\n\t\tMF = (f_o &gt;&gt; 13) &amp; 0x01;\r\n\t\tprintf(\"\u6807\u5fd7\u4f4d\\t\\tDF = %d, MF = %d\\n\", DF, MF);\r\n\t\t\/\/ IP\u7247\u504f\u79fb\r\n\t\toffset = f_o &amp; 0x1fff;\r\n\t\tprintf(\"\u7247\u504f\u79fb\\t\\t%d\\n\", offset);\r\n\t\t\/\/ TTL\r\n\t\tprintf(\"TTL\\t\\t%d\\n\", sip_pkt-&gt;ip._TTL);\r\n\t\t\/\/ IP\u534f\u8bae\u7c7b\u578b\r\n\t\tproto = sip_pkt-&gt;ip.proto;\r\n\t\tprintf(\"\u534f\u8bae\u7c7b\u578b\\t%d,\", proto);\r\n\t\tswitch (proto)\r\n\t\t{\r\n\t\tcase 1:\r\n\t\t\tprintf(\"ICMP\\n\");\r\n\t\t\tbreak;\r\n\t\tcase 2:\r\n\t\t\tprintf(\"IGMP\\n\");\r\n\t\t\tbreak;\r\n\t\tcase 4:\r\n\t\t\tprintf(\"IP\\n\");\r\n\t\t\tbreak;\r\n\t\tcase 6:\r\n\t\t\tprintf(\"TCP\\n\");\r\n\t\t\tbreak;\r\n\t\tcase 8:\r\n\t\t\tprintf(\"EGP\\n\");\r\n\t\t\tbreak;\r\n\t\tcase 9:\r\n\t\t\tprintf(\"IGP\\n\");\r\n\t\t\tbreak;\r\n\t\tcase 17:\r\n\t\t\tprintf(\"UDP\\n\");\r\n\t\t\tbreak;\r\n\t\tcase 41:\r\n\t\t\tprintf(\"IPv6\\n\");\r\n\t\t\tbreak;\r\n\t\tcase 50:\r\n\t\t\tprintf(\"ESP\\n\");\r\n\t\t\tbreak;\r\n\t\tcase 89:\r\n\t\t\tprintf(\"OSPF\\n\");\r\n\t\t\tbreak;\r\n\t\tdefault:\r\n\t\t\tprintf(\"\u5176\u4ed6\u7c7b\u578b\\n\");\r\n\t\t\tbreak;\r\n\t\t}\r\n\t\t\/\/ \u9996\u90e8\u6821\u9a8c\u548c\r\n\t\tprintf(\"\u9996\u90e8\u6821\u9a8c\u548c\\t0x%0x\\n\", ntohs(sip_pkt-&gt;ip.checksum));\r\n\t\t\/\/ printf(\"\u6e90IP\\t\\t\");\r\n\t\tip_addr = sip_pkt-&gt;ip.s_ipAddr;\r\n\t\tprintf(\"\u6e90IP\\t\\t%i.%i.%i.%i\\n\", *ip_addr, *(ip_addr + 1), *(ip_addr + 2), *(ip_addr + 3));\r\n\t\t\/\/ printf(\"\u76ee\u7684IP\\t\\t\");\r\n\t\tip_addr = sip_pkt-&gt;ip.d_ipAddr;\r\n\t\tprintf(\"\u76ee\u7684IP\\t\\t%i.%i.%i.%i\\n\", *ip_addr, *(ip_addr + 1), *(ip_addr + 2), *(ip_addr + 3));\r\n\r\n\t\tpacket_number++;\r\n\t\tsystem(\"pause\");\r\n\t}\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/****************************************************** [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":249,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-10","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\/10","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=10"}],"version-history":[{"count":1,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":11,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/posts\/10\/revisions\/11"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/media\/249"}],"wp:attachment":[{"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}