TCP,UDP校验和的计算
不需要分析了,有代码的直接给代码吧
书上的代码冒似不管用。。。郁闷
伪头部的协议、长度部分到底填的是什么啊
我下面的代码测试结果是错的,哪错啦。。。
。。。
unsigned short tcpcksum(PIPHeader pip, unsigned len)
/*
* Argument:
* pip - the pointer of the IP packet
* len - the len of tcp header
*/
{
PTCPHeader ptcp = (PTCPHeader)(pip + 1);
unsigned short *sptr;
unsigned long tcksum = 0;
unsigned i;
// The pseudo header
sptr = (unsigned short *)&pip->ipSource;
for (i = 0; i < 4; ++i)
{
tcksum += *sptr++;
}
tcksum += IPPROTO_TCP;
tcksum += htons(ntohs(pip->ipLength) - sizeof(IPHeader));
// The TCP header
sptr = (unsigned short *)ptcp;
if (len % 2)
{
// pad
((char *)ptcp)[len] = 0;
// For the following division
len += 1;
}
// Convert to length in shorts
len >>= 1;
for (i = 0; i < len; ++i)
{
tcksum += *sptr++;
}
tcksum = (tcksum >> 16) + (tcksum & 0xffff);
tcksum += (tcksum >> 16);
return (unsigned short)(~tcksum);
}