Originally Posted by
Yonut
Why would the program only work once a proper ping is issued? Why would the second ping correct the checksum of the original ping? I'm thinking this might have something to do with bind.
Perhaps your program was waiting for a response and stole the response from the other ping program (if that's possible).
Here's an implementation based on the RFC example C code that might work:
Code:
#include <stdio.h>
#include <inttypes.h>
// Compute Internet Checksum for "count" bytes
// beginning at location "addr".
uint16_t checksum(void* addr, int count)
{
uint16_t* buf = addr;
uint32_t sum = 0;
for ( ; count > 1; count -= 2)
sum += *(uint16_t*)buf++;
// Add left-over byte, if any
if (count > 0)
sum += *(unsigned char*)addr;
// Fold 32-bit sum to 16 bits
while (sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
return (uint16_t)~sum;
}
int main() {
// ICMP_ECHO == 8
char dat[] = "/x08/x00/x00/x00/x12/x34/x00/x00";
uint16_t cs = checksum(dat, 8);
printf("%04" PRIX16 "\n", cs); // yields A740
return 0;
}