From b58e7bc7cb8ce4fc6f824761ac8ef3920e7dfcc1 Mon Sep 17 00:00:00 2001 From: Miguel Date: Tue, 25 Sep 2018 20:46:52 +0200 Subject: working on icmp ping reply --- net/inet.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 net/inet.c (limited to 'net/inet.c') diff --git a/net/inet.c b/net/inet.c new file mode 100644 index 0000000..f7cc2a6 --- /dev/null +++ b/net/inet.c @@ -0,0 +1,37 @@ +#include "inet.h" + +uint16_t ntohs(uint16_t val) +{ + return val<<8|val>>8; +} +uint16_t htons(uint16_t val) +{ + return val<<8|val>>8; +} + +uint16_t checksum(void *addr, int count) +{ + /* Compute Internet Checksum for "count" bytes + * beginning at location "addr". + * Taken from https://tools.ietf.org/html/rfc1071 + */ + + register uint32_t sum = 0; + uint16_t * ptr = addr; + + while( count > 1 ) { + /* This is the inner loop */ + sum += ntohs(* ptr++); + count -= 2; + } + + /* Add left-over byte, if any */ + if( count > 0 ) + sum += * (uint8_t *) ptr; + + /* Fold 32-bit sum to 16 bits */ + while (sum>>16) + sum = (sum & 0xffff) + (sum >> 16); + + return ~sum; +} -- cgit v1.2.3