diff options
Diffstat (limited to 'net/inet.c')
| -rw-r--r-- | net/inet.c | 37 |
1 files changed, 37 insertions, 0 deletions
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; +} |
