tcp校验和
偽首部(pseudo header),通常指TCP偽首部和UDP偽首部
TCP的校驗(yàn)和是必需的,而UDP的校驗(yàn)和是可選的
TCP校驗(yàn)是需要校驗(yàn)包頭和數(shù)據(jù)的
//共12字節(jié)
typedef struct
{
unsigned long saddr; //源IP地址
unsigned long daddr; //目的IP地址
unsigned char mbz; // mbz = must be zero, 用于填充對齊
unsigned char protocal; //8位協(xié)議號
unsigned short tcpl; //TCP包長度
}psdheader_t;
偽首部是一個(gè)虛擬的數(shù)據(jù)結(jié)構(gòu),僅僅是為計(jì)算校驗(yàn)和
接收方計(jì)算檢驗(yàn)和錯(cuò)誤,IP就丟棄收到的數(shù)據(jù)報(bào)
RFC 793的TCP校驗(yàn)和定義
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
如果總長度為奇數(shù)個(gè)字節(jié),則在最后增添一位都為0的字節(jié)
首先,把TCP報(bào)頭中的校驗(yàn)和字段置為0
其次,用反碼相加法累加所有的16位字
最后,對計(jì)算結(jié)果取反
詳細(xì)計(jì)算方法見ip校驗(yàn)和:
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包頭和數(shù)據(jù)的總長度(網(wǎng)絡(luò)字節(jié)序)
總結(jié)
- 上一篇: 剑与家园英雄培养顺序是什么
- 下一篇: Arduino杀手在此!!NodeMCU