我在x64机器上。以下是我计算ICMP校验和的方法:
unsigned short in_checksum(unsigned short *ptr, int n_bytes)
{
register long sum;
u_short odd_byte;
register u_short ret_checksum;
while (n_bytes > 1)
{
sum += *ptr++;
n_bytes -= 2;
}
if (n_bytes == 1)
{
odd_byte = 0;
*((u_char *) & odd_byte) = * (u_char *) ptr;
sum += odd_byte;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
ret_checksum = ~sum;
return ret_checksum;
}
当我通过wireshark嗅探发送的数据包时,我总是说每个icmp数据包的校验和不正确。怎么了?
答案 0 :(得分:1)
您忘了初始化
register long sum;
到0.将选项-W
传递给gcc就会告诉你。
...: In function 'in_checksum': ...: warning: 'sum' may be used uninitialized in this function