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包头和数据的总长度(网络字节序)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!