I'm on x64 machine. Here is how I'm calculation the checksum for 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;
}
When I sniff the sent packets by wireshark, I always says the checksum is incorrect for each icmp packet. What's up with this?
You forgot to initialize
register long sum;
to 0. Passing option -W
to gcc would have told you.
...: In function 'in_checksum': ...: warning: 'sum' may be used uninitialized in this function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.