{"id":99,"date":"2014-06-20T10:58:58","date_gmt":"2014-06-20T10:58:58","guid":{"rendered":""},"modified":"2014-06-20T10:58:58","modified_gmt":"2014-06-20T10:58:58","slug":"","status":"publish","type":"post","link":"http:\/\/weizn.net\/?p=99","title":{"rendered":"TCP\u6821\u9a8c\u548c\u8ba1\u7b97\u4ee3\u7801"},"content":{"rendered":"<p>\n\ttypedef struct DLC_Header<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; unsigned char DesMAC[6];&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u4ee5\u592a\u7f51\u76ee\u7684\u5730\u5740<br \/>\n&nbsp;&nbsp;&nbsp; unsigned char SrcMAC[6];&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u4ee5\u592a\u7f51\u6e90\u5730\u5740<br \/>\n&nbsp;&nbsp;&nbsp; unsigned short EtherType;&nbsp;&nbsp;&nbsp; \/\/\u4e0a\u5c42\u534f\u8bae\u7c7b\u578b<br \/>\n} DLCHEADER;<\/p>\n<p>typedef struct ipheader<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; unsigned char ip_hl:4;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/*header length(\u62a5\u5934\u957f\u5ea6\uff09*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned char ip_v:4;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/*version(\u7248\u672c)*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned char ip_tos;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/*type os service\u670d\u52a1\u7c7b\u578b*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned short int ip_len;&nbsp;&nbsp;&nbsp;&nbsp; \/*total length (\u603b\u957f\u5ea6)*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned short int ip_id;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/*identification (\u6807\u8bc6\u7b26)*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned short int ip_off;&nbsp;&nbsp;&nbsp;&nbsp; \/*fragment offset field(\u6bb5\u79fb\u4f4d\u57df)*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned char ip_ttl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/*time to live (\u751f\u5b58\u65f6\u95f4)*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned char ip_p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/*protocol(\u4e0a\u5c42\u534f\u8bae\u7c7b\u578b)*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned short int ip_sum;&nbsp;&nbsp;&nbsp;&nbsp; \/*checksum(\u6821\u9a8c\u548c)*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned char ip_src[4];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/*source address(\u6e90\u5730\u5740)*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned char ip_dst[4];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/*destination address(\u76ee\u7684\u5730\u5740)*\/<br \/>\n} IP;<\/p>\n<p>typedef struct tcpheader<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; unsigned short int sport;&nbsp;&nbsp;&nbsp; \/*source port (\u6e90\u7aef\u53e3\u53f7)*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned short int dport;&nbsp;&nbsp;&nbsp; \/*destination port(\u76ee\u7684\u7aef\u53e3\u53f7)*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned int th_seq;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/*sequence number(\u5305\u7684\u5e8f\u5217\u53f7)*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned int th_ack;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/*acknowledgement number(\u786e\u8ba4\u5e94\u7b54\u53f7)*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned char th_x:4;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/*unused(\u672a\u4f7f\u7528)*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned char th_off:4;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/*data offset(\u6570\u636e\u504f\u79fb\u91cf)*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned char Flags;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/*\u6807\u5fd7\u5168*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned short int th_win;&nbsp;&nbsp; \/*windows(\u7a97\u53e3)*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned short int th_sum;&nbsp;&nbsp; \/*checksum(\u6821\u9a8c\u548c)*\/<br \/>\n&nbsp;&nbsp;&nbsp; unsigned short int th_urp;&nbsp;&nbsp; \/*urgent pointer(\u7d27\u6025\u6307\u9488)*\/<br \/>\n} TCP;\n<\/p>\n<p>\n\t\n<\/p>\n<p>\n\ttypedef struct pseudoTcpHeader\n<\/p>\n<p>{<br \/>\n&nbsp;&nbsp;&nbsp; unsigned long int ip_src;<br \/>\n&nbsp;&nbsp;&nbsp; unsigned long int ip_dst;<br \/>\n&nbsp;&nbsp;&nbsp; unsigned char zero;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/always zero<br \/>\n&nbsp;&nbsp;&nbsp; unsigned char protocol;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ = 0x06<br \/>\n&nbsp;&nbsp;&nbsp; unsigned short tcp_len;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/TCP\u5934+\u6570\u636e&nbsp;&nbsp; \uff08\u4e0d\u52a0\u4f2a\u5934\u957f\u5ea6\uff09<\/p>\n<p>\n\t} TCP_Psd_Header;\n<\/p>\n<p>\n\t\n<\/p>\n<p>\n\tunsigned short int checksum(unsigned short int *buffer,int size)<br \/>\n{<br \/>\n&nbsp; &nbsp; unsigned long cksum=0;<br \/>\n&nbsp; &nbsp; while(size&gt;1)<br \/>\n&nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; cksum+=*buffer++;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; size-=sizeof(unsigned short int);<br \/>\n&nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; if(size)<br \/>\n&nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; cksum+=*(UCHAR *)buffer;<br \/>\n&nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; \/\/\u5c0632\u4f4d\u6570\u8f6c\u6362\u621016<br \/>\n&nbsp; &nbsp; while (cksum&gt;&gt;16)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; cksum=(cksum&gt;&gt;16)+(cksum &amp; 0xffff);<br \/>\n&nbsp; &nbsp; return (unsigned short int)(~cksum);<br \/>\n}\n<\/p>\n<p>\n\t\n<\/p>\n<p>void PacketCheckSum(unsigned char Packet[])<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; TCP_Psd_Header *TPH=NULL;<br \/>\n&nbsp;&nbsp;&nbsp; char *buffer=NULL;<br \/>\n&nbsp;&nbsp;&nbsp; unsigned int size=0;<\/p>\n<p>&nbsp;&nbsp;&nbsp; DLCHEADER *DLCHeader=NULL;<br \/>\n&nbsp;&nbsp;&nbsp; IP *IPHeader=NULL;<br \/>\n&nbsp;&nbsp;&nbsp; TCP *TCPHeader=NULL;<\/p>\n<p>&nbsp;&nbsp;&nbsp; DLCHeader=(DLCHEADER *)Packet;<br \/>\n&nbsp;&nbsp;&nbsp; IPHeader=(IP *)(Packet+sizeof(DLCHEADER));<br \/>\n&nbsp;&nbsp;&nbsp; TCPHeader=(TCP *)(Packet+sizeof(DLCHEADER)+sizeof(IP));<\/p>\n<p>&nbsp;&nbsp;&nbsp; TCPHeader-&gt;th_sum=0;<br \/>\n&nbsp;&nbsp;&nbsp; size=ntohs(IPHeader-&gt;ip_len);<br \/>\n&nbsp;&nbsp;&nbsp; buffer=(char *)malloc(size+sizeof(TCP_Psd_Header));<br \/>\n&nbsp;&nbsp;&nbsp; if(buffer==NULL) return;<br \/>\n&nbsp;&nbsp;&nbsp; memset(buffer,NULL,size+sizeof(TCP_Psd_Header));<br \/>\n&nbsp;&nbsp;&nbsp; TPH=(TCP_Psd_Header *)buffer;<br \/>\n&nbsp;&nbsp;&nbsp; TPH-&gt;ip_dst=*(unsigned long int *)IPHeader-&gt;ip_dst;<br \/>\n&nbsp;&nbsp;&nbsp; TPH-&gt;ip_src=*(unsigned long int *)IPHeader-&gt;ip_src;<br \/>\n&nbsp;&nbsp;&nbsp; TPH-&gt;protocol=0x06;<br \/>\n&nbsp;&nbsp;&nbsp; TPH-&gt;tcp_len=htons(size-sizeof(IP));<\/p>\n<p>&nbsp;&nbsp;&nbsp; memcpy(buffer+sizeof(TCP_Psd_Header),TCPHeader,size-sizeof(IP));<\/p>\n<p>&nbsp;&nbsp;&nbsp; TCPHeader-&gt;th_sum=checksum(buffer,size-sizeof(IP)+sizeof(TCP_Psd_Header));<\/p>\n<p>&nbsp;&nbsp;&nbsp; free(buffer);<\/p>\n<p>&nbsp;&nbsp;&nbsp; return;<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>typedef struct pseudoTcpHeader<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; unsigned long int ip_src;<br \/>\n&nbsp;&nbsp;&nbsp; unsigned long int ip_dst;<br \/>\n&nbsp;&nbsp;&nbsp; unsigned char zero;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[324],"tags":[],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>TCP\u6821\u9a8c\u548c\u8ba1\u7b97\u4ee3\u7801 - Wayne&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/weizn.net\/?p=99\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"TCP\u6821\u9a8c\u548c\u8ba1\u7b97\u4ee3\u7801 - Wayne&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"typedef struct pseudoTcpHeader { &nbsp;&nbsp;&nbsp; unsigned long int ip_src; &nbsp;&nbsp;&nbsp; unsigned long int ip_dst; &nbsp;&nbsp;&nbsp; unsigned char zero;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/weizn.net\/?p=99\" \/>\n<meta property=\"og:site_name\" content=\"Wayne&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2014-06-20T10:58:58+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"zinan\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/weizn.net\/#website\",\"url\":\"http:\/\/weizn.net\/\",\"name\":\"Wayne&#039;s Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/weizn.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/weizn.net\/?p=99#webpage\",\"url\":\"http:\/\/weizn.net\/?p=99\",\"name\":\"TCP\\u6821\\u9a8c\\u548c\\u8ba1\\u7b97\\u4ee3\\u7801 - Wayne&#039;s Blog\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/#website\"},\"datePublished\":\"2014-06-20T10:58:58+00:00\",\"dateModified\":\"2014-06-20T10:58:58+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/weizn.net\/?p=99#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/weizn.net\/?p=99\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/weizn.net\/?p=99#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\\u9996\\u9875\",\"item\":\"http:\/\/weizn.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"TCP\\u6821\\u9a8c\\u548c\\u8ba1\\u7b97\\u4ee3\\u7801\"}]},{\"@type\":\"Article\",\"@id\":\"http:\/\/weizn.net\/?p=99#article\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/?p=99#webpage\"},\"author\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"headline\":\"TCP\\u6821\\u9a8c\\u548c\\u8ba1\\u7b97\\u4ee3\\u7801\",\"datePublished\":\"2014-06-20T10:58:58+00:00\",\"dateModified\":\"2014-06-20T10:58:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/weizn.net\/?p=99#webpage\"},\"wordCount\":704,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"articleSection\":[\"C\/C++\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/weizn.net\/?p=99#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\",\"name\":\"zinan\",\"logo\":{\"@id\":\"http:\/\/weizn.net\/#personlogo\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"TCP\u6821\u9a8c\u548c\u8ba1\u7b97\u4ee3\u7801 - Wayne&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/weizn.net\/?p=99","og_locale":"zh_CN","og_type":"article","og_title":"TCP\u6821\u9a8c\u548c\u8ba1\u7b97\u4ee3\u7801 - Wayne&#039;s Blog","og_description":"typedef struct pseudoTcpHeader { &nbsp;&nbsp;&nbsp; unsigned long int ip_src; &nbsp;&nbsp;&nbsp; unsigned long int ip_dst; &nbsp;&nbsp;&nbsp; unsigned char zero;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...","og_url":"http:\/\/weizn.net\/?p=99","og_site_name":"Wayne&#039;s Blog","article_published_time":"2014-06-20T10:58:58+00:00","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"zinan","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"http:\/\/weizn.net\/#website","url":"http:\/\/weizn.net\/","name":"Wayne&#039;s Blog","description":"","publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/weizn.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"WebPage","@id":"http:\/\/weizn.net\/?p=99#webpage","url":"http:\/\/weizn.net\/?p=99","name":"TCP\u6821\u9a8c\u548c\u8ba1\u7b97\u4ee3\u7801 - Wayne&#039;s Blog","isPartOf":{"@id":"http:\/\/weizn.net\/#website"},"datePublished":"2014-06-20T10:58:58+00:00","dateModified":"2014-06-20T10:58:58+00:00","breadcrumb":{"@id":"http:\/\/weizn.net\/?p=99#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/weizn.net\/?p=99"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/weizn.net\/?p=99#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/weizn.net\/"},{"@type":"ListItem","position":2,"name":"TCP\u6821\u9a8c\u548c\u8ba1\u7b97\u4ee3\u7801"}]},{"@type":"Article","@id":"http:\/\/weizn.net\/?p=99#article","isPartOf":{"@id":"http:\/\/weizn.net\/?p=99#webpage"},"author":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"headline":"TCP\u6821\u9a8c\u548c\u8ba1\u7b97\u4ee3\u7801","datePublished":"2014-06-20T10:58:58+00:00","dateModified":"2014-06-20T10:58:58+00:00","mainEntityOfPage":{"@id":"http:\/\/weizn.net\/?p=99#webpage"},"wordCount":704,"commentCount":0,"publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"articleSection":["C\/C++"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/weizn.net\/?p=99#respond"]}]},{"@type":["Person","Organization"],"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264","name":"zinan","logo":{"@id":"http:\/\/weizn.net\/#personlogo"}}]}},"_links":{"self":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=99"}],"version-history":[{"count":0,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions"}],"wp:attachment":[{"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}