Reputation: 34914
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?
Upvotes: 0
Views: 1185
Reputation: 19395
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
Upvotes: 1