aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorBaruch Siach <baruch@tkos.co.il>2011-09-07 17:52:37 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2011-09-07 17:55:40 +0200
commite8f36330d9bb27f9f7e66aa6f01ff92c07d86f62 (patch)
tree863018163a166cc690902d8027a3f04f9f812dd3 /libbb
parent8c84f7545cf08925edb23d94d9f6519b338267c6 (diff)
downloadbusybox-w32-e8f36330d9bb27f9f7e66aa6f01ff92c07d86f62.tar.gz
busybox-w32-e8f36330d9bb27f9f7e66aa6f01ff92c07d86f62.tar.bz2
busybox-w32-e8f36330d9bb27f9f7e66aa6f01ff92c07d86f62.zip
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')
-rw-r--r--libbb/Kbuild.src6
-rw-r--r--libbb/inet_cksum.c32
2 files changed, 38 insertions, 0 deletions
diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src
index 875d02456..335b34128 100644
--- a/libbb/Kbuild.src
+++ b/libbb/Kbuild.src
@@ -166,6 +166,12 @@ lib-$(CONFIG_IOSTAT) += get_cpu_count.o
166lib-$(CONFIG_MPSTAT) += get_cpu_count.o 166lib-$(CONFIG_MPSTAT) += get_cpu_count.o
167lib-$(CONFIG_POWERTOP) += get_cpu_count.o 167lib-$(CONFIG_POWERTOP) += get_cpu_count.o
168 168
169lib-$(CONFIG_PING) += inet_cksum.o
170lib-$(CONFIG_TRACEROUTE) += inet_cksum.o
171lib-$(CONFIG_TRACEROUTE6) += inet_cksum.o
172lib-$(CONFIG_UDHCPC) += inet_cksum.o
173lib-$(CONFIG_UDHCPD) += inet_cksum.o
174
169# We shouldn't build xregcomp.c if we don't need it - this ensures we don't 175# We shouldn't build xregcomp.c if we don't need it - this ensures we don't
170# require regex.h to be in the include dir even if we don't need it thereby 176# require regex.h to be in the include dir even if we don't need it thereby
171# allowing us to build busybox even if uclibc regex support is disabled. 177# 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 000000000..31bf8c4d9
--- /dev/null
+++ b/libbb/inet_cksum.c
@@ -0,0 +1,32 @@
1/*
2 * Checksum routine for Internet Protocol family headers (C Version)
3 *
4 * Licensed under GPLv2, see file LICENSE in this source tree.
5 */
6
7#include "libbb.h"
8
9uint16_t FAST_FUNC inet_cksum(uint16_t *addr, int nleft)
10{
11 /*
12 * Our algorithm is simple, using a 32 bit accumulator,
13 * we add sequential 16 bit words to it, and at the end, fold
14 * back all the carry bits from the top 16 bits into the lower
15 * 16 bits.
16 */
17 unsigned sum = 0;
18 while (nleft > 1) {
19 sum += *addr++;
20 nleft -= 2;
21 }
22
23 /* Mop up an odd byte, if necessary */
24 if (nleft)
25 sum += *(uint8_t*)addr;
26
27 /* Add back carry outs from top 16 bits to low 16 bits */
28 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
29 sum += (sum >> 16); /* add carry */
30
31 return (uint16_t)~sum;
32}