unsigned short /* this function generates header checksums */
csum (unsigned short *buf, int nwords)
{
unsigned long sum;
for (sum = 0; nwords > 0; nwords--) // add words(16bits) together
{
sum += *buf++;
}
sum = (sum >> 16) + (sum & 0xffff); //add carry over
sum += (sum >> 16); //MY question: what exactly does this step do??? add possible left-over
//byte? But hasn't it already been added in the loop (if
//any)?
return ((unsigned short) ~sum);
}
- 我nwords承担16位的字的数量问题,而不是8位字节(如果有奇数个字节,nword四舍五入到下一个大),是正确的吗?说ip_hdr总共有27个字节,那么nword将是14而不是13,对不对?
- 总和=(总和>> 16)+(总和& 0xffff)是加进位16位补全
- sum + =(sum >> 16);这一步的目的是什么?添加剩余字节?但是,循环中已经添加了剩余的字节?
谢谢!关于IP校验码
可以格式化你的代码一点,好吗? – WhirlWind 2010-04-05 19:19:24
有人需要清理它。 :( – Rev316 2010-04-05 19:19:37
'ip_hdr'不能是27个字节,它们必须是4个字节长的倍数,并且长度必须在20到60个字节之间 - '(4 * ip_hdr-> ip_ihl)',因为'ip_ihl'是一个四位的字段 – Alnitak 2014-11-06 08:46:22