diff options
author | Baruch Siach <baruch@tkos.co.il> | 2011-09-07 17:52:37 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2011-09-07 17:55:40 +0200 |
commit | e8f36330d9bb27f9f7e66aa6f01ff92c07d86f62 (patch) | |
tree | 863018163a166cc690902d8027a3f04f9f812dd3 /libbb/inet_cksum.c | |
parent | 8c84f7545cf08925edb23d94d9f6519b338267c6 (diff) |
networking: consolidate the IP checksum code. -129 bytes.
Signed-off-by: Baruch Siach <baruch@tkos.co.il>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb/inet_cksum.c')
-rw-r--r-- | libbb/inet_cksum.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/libbb/inet_cksum.c b/libbb/inet_cksum.c new file mode 100644 index 000000000..31bf8c4d9 --- /dev/null +++ b/libbb/inet_cksum.c @@ -0,0 +1,32 @@ +/* + * Checksum routine for Internet Protocol family headers (C Version) + * + * Licensed under GPLv2, see file LICENSE in this source tree. + */ + +#include "libbb.h" + +uint16_t FAST_FUNC inet_cksum(uint16_t *addr, int nleft) +{ + /* + * Our algorithm is simple, using a 32 bit accumulator, + * we add sequential 16 bit words to it, and at the end, fold + * back all the carry bits from the top 16 bits into the lower + * 16 bits. + */ + unsigned sum = 0; + while (nleft > 1) { + sum += *addr++; + nleft -= 2; + } + + /* Mop up an odd byte, if necessary */ + if (nleft) + sum += *(uint8_t*)addr; + + /* Add back carry outs from top 16 bits to low 16 bits */ + sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ + sum += (sum >> 16); /* add carry */ + + return (uint16_t)~sum; +} |