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 /networking/ping.c | |
parent | 8c84f7545cf08925edb23d94d9f6519b338267c6 (diff) | |
download | busybox-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 'networking/ping.c')
-rw-r--r-- | networking/ping.c | 31 |
1 files changed, 3 insertions, 28 deletions
diff --git a/networking/ping.c b/networking/ping.c index efd4f210b..a1fd9dfb1 100644 --- a/networking/ping.c +++ b/networking/ping.c | |||
@@ -149,31 +149,6 @@ enum { | |||
149 | PINGINTERVAL = 1, /* 1 second */ | 149 | PINGINTERVAL = 1, /* 1 second */ |
150 | }; | 150 | }; |
151 | 151 | ||
152 | /* Common routines */ | ||
153 | |||
154 | static int in_cksum(unsigned short *buf, int sz) | ||
155 | { | ||
156 | int nleft = sz; | ||
157 | int sum = 0; | ||
158 | unsigned short *w = buf; | ||
159 | unsigned short ans = 0; | ||
160 | |||
161 | while (nleft > 1) { | ||
162 | sum += *w++; | ||
163 | nleft -= 2; | ||
164 | } | ||
165 | |||
166 | if (nleft == 1) { | ||
167 | *(unsigned char *) (&ans) = *(unsigned char *) w; | ||
168 | sum += ans; | ||
169 | } | ||
170 | |||
171 | sum = (sum >> 16) + (sum & 0xFFFF); | ||
172 | sum += (sum >> 16); | ||
173 | ans = ~sum; | ||
174 | return ans; | ||
175 | } | ||
176 | |||
177 | #if !ENABLE_FEATURE_FANCY_PING | 152 | #if !ENABLE_FEATURE_FANCY_PING |
178 | 153 | ||
179 | /* Simple version */ | 154 | /* Simple version */ |
@@ -201,7 +176,7 @@ static void ping4(len_and_sockaddr *lsa) | |||
201 | pkt = (struct icmp *) G.packet; | 176 | pkt = (struct icmp *) G.packet; |
202 | memset(pkt, 0, sizeof(G.packet)); | 177 | memset(pkt, 0, sizeof(G.packet)); |
203 | pkt->icmp_type = ICMP_ECHO; | 178 | pkt->icmp_type = ICMP_ECHO; |
204 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(G.packet)); | 179 | pkt->icmp_cksum = inet_cksum((uint16_t *) pkt, sizeof(G.packet)); |
205 | 180 | ||
206 | xsendto(pingsock, G.packet, DEFDATALEN + ICMP_MINLEN, &lsa->u.sa, lsa->len); | 181 | xsendto(pingsock, G.packet, DEFDATALEN + ICMP_MINLEN, &lsa->u.sa, lsa->len); |
207 | 182 | ||
@@ -493,7 +468,7 @@ static void sendping4(int junk UNUSED_PARAM) | |||
493 | /* No hton: we'll read it back on the same machine */ | 468 | /* No hton: we'll read it back on the same machine */ |
494 | *(uint32_t*)&pkt->icmp_dun = monotonic_us(); | 469 | *(uint32_t*)&pkt->icmp_dun = monotonic_us(); |
495 | 470 | ||
496 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, datalen + ICMP_MINLEN); | 471 | pkt->icmp_cksum = inet_cksum((uint16_t *) pkt, datalen + ICMP_MINLEN); |
497 | 472 | ||
498 | sendping_tail(sendping4, ICMP_MINLEN); | 473 | sendping_tail(sendping4, ICMP_MINLEN); |
499 | } | 474 | } |
@@ -512,7 +487,7 @@ static void sendping6(int junk UNUSED_PARAM) | |||
512 | /*if (datalen >= 4)*/ | 487 | /*if (datalen >= 4)*/ |
513 | *(uint32_t*)(&pkt->icmp6_data8[4]) = monotonic_us(); | 488 | *(uint32_t*)(&pkt->icmp6_data8[4]) = monotonic_us(); |
514 | 489 | ||
515 | //TODO? pkt->icmp_cksum = in_cksum(...); | 490 | //TODO? pkt->icmp_cksum = inet_cksum(...); |
516 | 491 | ||
517 | sendping_tail(sendping6, sizeof(struct icmp6_hdr)); | 492 | sendping_tail(sendping6, sizeof(struct icmp6_hdr)); |
518 | } | 493 | } |