This code properly implements the IP checksum function on a 32-bit machine: unsigned short cksum(unsigned short *buf, int count){unsigned long sum = 0;while (count--) {sum += *buf++;if (sum & 0xFFFF0000) {sum &= 0xFFFF;sum++;}}return ~(sum & 0xFFFF);}The if statement performs "wraparound" if the sum over?ows 16 bits. That is, it takes the 1 that over?owed into the 17th bit and adds it to the lower 16 bits of the sum.
Here's how to compute and check the checksum. The sender should perform these actions:
1. Completely ?ll in the IP header, with zero in the checksum ?eld.
2. Compute the checksum and store it in the checksum ?eld.
The receiver should perform these actions:
1. Copy the checksum ?eld's value into a variable.
2. Zero the IP header's checksum ?eld.
3. Compute the checksum over the IP header.
4. Compare the computed checksum value with the value in the variable.