diff options
Diffstat (limited to 'Semestr 4/sieci/pracownia1/icmp_checksum.c')
-rw-r--r-- | Semestr 4/sieci/pracownia1/icmp_checksum.c | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/Semestr 4/sieci/pracownia1/icmp_checksum.c b/Semestr 4/sieci/pracownia1/icmp_checksum.c new file mode 100644 index 0000000..c9dc96b --- /dev/null +++ b/Semestr 4/sieci/pracownia1/icmp_checksum.c @@ -0,0 +1,10 @@ +u_int16_t compute_icmp_checksum (const void *buff, int length) +{ + u_int32_t sum; + const u_int16_t* ptr = buff; + assert (length % 2 == 0); + for (sum = 0; length > 0; length -= 2) + sum += *ptr++; + sum = (sum >> 16) + (sum & 0xffff); + return (u_int16_t)(~(sum + (sum >> 16))); +} |