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,对吗?
- 行 sum = (sum >> 16) + (sum & 0xffff) 是添加进位以使 16 位补码
- 总和 += (总和 >> 16); 这一步的目的是什么?添加剩余字节?但是循环中已经添加了剩余字节?
谢谢!