计算ICMP头的校验和:
程序员文章站
2022-06-30 18:20:10
...
计算ICMP头的校验和:
USHORT checksum(USHORT* buff, int nSize)
{
unsigned long cksum=0;
//将数据以字为单位加到cksum
while (nSize > 1)
{
cksum += *buff++;
nSize -= sizeof(USHORT);
}
//如果为奇数, 将最后一个字扩展到双字,再累加
if (nSize)
{
cksum += *(UCHAR*)buff;
}
//将cksum的高16位与低16位相加,取反后得到校验和
cksum = (cksum>>16) + (cksum&&0xffff);
cksum += (cksum>>16);
return (USHORT)(~cksum);
}
上一篇: TCP/IP网络编程学习笔记(9)