networking · advanced · ~15 min

Internet checksum (16-bit ones-complement)

Implement the checksum used by IP/UDP/TCP.

Challenge

Implement unsigned short inet_checksum(const unsigned char *data, int n) computing the standard 16-bit ones-complement Internet checksum over n bytes (big-endian 16-bit words, end-around carry, then bitwise NOT).

Starter code

unsigned short inet_checksum(const unsigned char *data, int n) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.