diff options
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/Kbuild.src | 6 | ||||
-rw-r--r-- | libbb/inet_cksum.c | 32 |
2 files changed, 38 insertions, 0 deletions
diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src index 875d024..335b341 100644 --- a/libbb/Kbuild.src +++ b/libbb/Kbuild.src @@ -166,6 +166,12 @@ lib-$(CONFIG_IOSTAT) += get_cpu_count.o lib-$(CONFIG_MPSTAT) += get_cpu_count.o lib-$(CONFIG_POWERTOP) += get_cpu_count.o +lib-$(CONFIG_PING) += inet_cksum.o +lib-$(CONFIG_TRACEROUTE) += inet_cksum.o +lib-$(CONFIG_TRACEROUTE6) += inet_cksum.o +lib-$(CONFIG_UDHCPC) += inet_cksum.o +lib-$(CONFIG_UDHCPD) += inet_cksum.o + # We shouldn't build xregcomp.c if we don't need it - this ensures we don't # require regex.h to be in the include dir even if we don't need it thereby # allowing us to build busybox even if uclibc regex support is disabled. diff --git a/libbb/inet_cksum.c b/libbb/inet_cksum.c new file mode 100644 index 0000000..31bf8c4 --- /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; +} |