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包頭和數據的總長度(網絡字節序)
總結
- 上一篇: 剑与家园英雄培养顺序是什么
- 下一篇: Arduino杀手在此!!NodeMCU