日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

tcp校验和

發布時間:2023/12/31 综合教程 28 生活家
生活随笔 收集整理的這篇文章主要介紹了 tcp校验和 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

偽首部(pseudo header),通常指TCP偽首部和UDP偽首部
TCP的校驗和是必需的,而UDP的校驗和是可選的
TCP校驗是需要校驗包頭和數據的

//共12字節
typedef struct
{
    unsigned long saddr; //源IP地址
    unsigned long daddr; //目的IP地址
    unsigned char mbz; // mbz = must be zero, 用于填充對齊
    unsigned char protocal; //8位協議號
    unsigned short tcpl; //TCP包長度
}psdheader_t;

偽首部是一個虛擬的數據結構,僅僅是為計算校驗和
接收方計算檢驗和錯誤,IP就丟棄收到的數據報

RFC 793的TCP校驗和定義
The checksum field is the 16 bit one’s complement of the one’s complement sum of all 16-bit words in the header and text.
If a segment contains an odd number of header and text octets to be checksummed, the last octet is padded on the right
with zeros to form a 16-bit word for checksum purposes. The pad is not transmitted as part of the segment. While computing
the checksum, the checksum field itself is replaced with zeros

如果總長度為奇數個字節,則在最后增添一位都為0的字節
首先,把TCP報頭中的校驗和字段置為0
其次,用反碼相加法累加所有的16位字
最后,對計算結果取反

詳細計算方法見ip校驗和:
http://blog.csdn.net/zhangxuechao_/article/details/50677220

舉例

本地IP: 0xc0 0xa8 0x9f 0x01
對方IP: 0xc0 0xa8 0x9f 0x82
TCP字段: 0x04 0xc6 0x87 0x01 0x4b 0xd7 0x89 0x9f 0x4e 0x3b  0x90 0xae 0x50 0x18 0xff 0xff 0xeb 0x69 0x00 0x00

(0xc0a8 + 0x9f01 + 0xc0a8 + 0x9f82 + 0x0006 + 0x0017) + 0x04c6 + 0x8701 + 0x4bd7 + 0x899f + 0x4e3b + 0x90ae + 0x5018 
+ 0xffff + 0x0000 + 0x0000 + 0x6162 + 0x6300 = 0x7148f
0x0007 + 0x148f = 0x1496
~0x1496 = 0xeb69

注:tcpl指的是tcp包頭和數據的總長度(網絡字節序)

總結

以上是生活随笔為你收集整理的tcp校验和的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。