summaryrefslogtreecommitdiff
path: root/networking/ping6.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-01-24 23:53:22 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-01-24 23:53:22 +0000
commitb9a279ba9466d8733261aff1bab0d3ec1031c34c (patch)
tree6a995795abf0322f540585a6c4d28847fbebb773 /networking/ping6.c
parent677cb5eea52f2424bef1fd0fcc6ed3c8c8b5f1d5 (diff)
downloadbusybox-w32-b9a279ba9466d8733261aff1bab0d3ec1031c34c.tar.gz
busybox-w32-b9a279ba9466d8733261aff1bab0d3ec1031c34c.tar.bz2
busybox-w32-b9a279ba9466d8733261aff1bab0d3ec1031c34c.zip
Unify ping and ping6. ping has -4 and -6 which force
name resolution into IP or IPv6 only, otherwise we take address family returned by host2sockaddr() in lsa->sa.sa_family. IOW: "ping ::1" with do IPv6 ping, "ping 127.0.0.1" will do IPv4 ping. ping6 is aliased to "ping -6".
Diffstat (limited to 'networking/ping6.c')
-rw-r--r--networking/ping6.c471
1 files changed, 0 insertions, 471 deletions
diff --git a/networking/ping6.c b/networking/ping6.c
deleted file mode 100644
index b67fab5b3..000000000
--- a/networking/ping6.c
+++ /dev/null
@@ -1,471 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini ping implementation for busybox
4 *
5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 *
9 * This version of ping is adapted from the ping in netkit-base 0.10,
10 * which is:
11 *
12 * Copyright (c) 1989 The Regents of the University of California.
13 * All rights reserved.
14 *
15 * This code is derived from software contributed to Berkeley by
16 * Mike Muuss.
17 *
18 * Original copyright notice is retained at the end of this file.
19 *
20 * This version is an adaptation of ping.c from busybox.
21 * The code was modified by Bart Visscher <magick@linux-fan.com>
22 */
23
24#include <netinet/icmp6.h>
25#include <net/if.h>
26#include "busybox.h"
27
28/* I see RENUMBERED constants in bits/in.h - !!?
29 * What a fuck is going on with libc? Is it a glibc joke? */
30#ifdef IPV6_2292HOPLIMIT
31#undef IPV6_HOPLIMIT
32#define IPV6_HOPLIMIT IPV6_2292HOPLIMIT
33#endif
34
35enum {
36 DEFDATALEN = 56,
37 MAXIPLEN = 60,
38 MAXICMPLEN = 76,
39 MAXPACKET = 65468,
40 MAX_DUP_CHK = (8 * 128),
41 MAXWAIT = 10,
42 PINGINTERVAL = 1 /* second */
43};
44
45static void ping(const char *host);
46
47#ifndef CONFIG_FEATURE_FANCY_PING6
48
49/* simple version */
50
51static const char *hostname;
52
53static void noresp(int ign)
54{
55 printf("No response from %s\n", hostname);
56 exit(EXIT_FAILURE);
57}
58
59static void ping(const char *host)
60{
61 len_and_sockaddr *lsa;
62 struct sockaddr_in6 pingaddr;
63 struct icmp6_hdr *pkt;
64 int pingsock, c;
65 int sockopt;
66 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
67
68 hostname = host;
69
70 pingsock = create_icmp6_socket();
71
72 lsa = host_and_af2sockaddr(host, 0, AF_INET6);
73 pingaddr = lsa->sin6;
74
75 pkt = (struct icmp6_hdr *) packet;
76 memset(pkt, 0, sizeof(packet));
77 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
78
79 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
80 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
81
82 c = sendto(pingsock, packet, DEFDATALEN + sizeof (struct icmp6_hdr), 0,
83 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
84
85 if (c < 0) {
86 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
87 bb_perror_msg_and_die("sendto");
88 }
89
90 signal(SIGALRM, noresp);
91 alarm(5); /* give the host 5000ms to respond */
92 /* listen for replies */
93 while (1) {
94 struct sockaddr_in6 from;
95 socklen_t fromlen = sizeof(from);
96
97 c = recvfrom(pingsock, packet, sizeof(packet), 0,
98 (struct sockaddr *) &from, &fromlen);
99 if (c < 0) {
100 if (errno != EINTR)
101 bb_perror_msg("recvfrom");
102 continue;
103 }
104 if (c >= 8) { /* icmp6_hdr */
105 pkt = (struct icmp6_hdr *) packet;
106 if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
107 break;
108 }
109 }
110 if (ENABLE_FEATURE_CLEAN_UP)
111 close(pingsock);
112 printf("%s is alive!\n", hostname);
113}
114
115int ping6_main(int argc, char **argv)
116{
117 argc--;
118 argv++;
119 if (argc < 1)
120 bb_show_usage();
121 ping(*argv);
122 return EXIT_SUCCESS;
123}
124
125#else /* ! CONFIG_FEATURE_FANCY_PING6 */
126
127/* full(er) version */
128
129#define OPT_STRING "qvc:s:I:"
130enum {
131 OPT_QUIET = 1 << 0,
132 OPT_VERBOSE = 1 << 1,
133};
134
135static struct sockaddr_in6 pingaddr;
136static int pingsock = -1;
137static unsigned datalen; /* intentionally uninitialized to work around gcc bug */
138static int if_index;
139
140static unsigned long ntransmitted, nreceived, nrepeats, pingcount;
141static int myid;
142static unsigned long tmin = ULONG_MAX, tmax, tsum;
143static char rcvd_tbl[MAX_DUP_CHK / 8];
144
145static char *hostname;
146
147static void sendping(int);
148static void pingstats(int);
149static void unpack(char *, int, struct sockaddr_in6 *, int);
150
151#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
152#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
153#define SET(bit) (A(bit) |= B(bit))
154#define CLR(bit) (A(bit) &= (~B(bit)))
155#define TST(bit) (A(bit) & B(bit))
156
157/**************************************************************************/
158
159static void pingstats(int junk)
160{
161 int status;
162
163 signal(SIGINT, SIG_IGN);
164
165 printf("\n--- %s ping statistics ---\n", hostname);
166 printf("%lu packets transmitted, ", ntransmitted);
167 printf("%lu packets received, ", nreceived);
168 if (nrepeats)
169 printf("%lu duplicates, ", nrepeats);
170 if (ntransmitted)
171 printf("%lu%% packet loss\n",
172 (ntransmitted - nreceived) * 100 / ntransmitted);
173 if (nreceived)
174 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
175 tmin / 10, tmin % 10,
176 (tsum / (nreceived + nrepeats)) / 10,
177 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
178 if (nreceived != 0)
179 status = EXIT_SUCCESS;
180 else
181 status = EXIT_FAILURE;
182 exit(status);
183}
184
185static void sendping(int junk)
186{
187 struct icmp6_hdr *pkt;
188 int i;
189 char packet[datalen + sizeof (struct icmp6_hdr)];
190
191 pkt = (struct icmp6_hdr *) packet;
192
193 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
194 pkt->icmp6_code = 0;
195 pkt->icmp6_cksum = 0;
196 pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
197 pkt->icmp6_id = myid;
198 CLR((uint16_t)ntransmitted % MAX_DUP_CHK);
199 ntransmitted++;
200
201 gettimeofday((struct timeval *) &pkt->icmp6_data8[4], NULL);
202
203 i = sendto(pingsock, packet, sizeof(packet), 0,
204 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
205
206 if (i < 0)
207 bb_perror_msg_and_die("sendto");
208 if ((size_t)i != sizeof(packet))
209 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
210 (int)sizeof(packet));
211
212 signal(SIGALRM, sendping);
213 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
214 alarm(PINGINTERVAL);
215 } else { /* done, wait for the last ping to come back */
216 /* todo, don't necessarily need to wait so long... */
217 signal(SIGALRM, pingstats);
218 alarm(MAXWAIT);
219 }
220}
221
222/* RFC3542 changed some definitions from RFC2292 for no good reason, whee!
223 * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */
224#ifndef MLD_LISTENER_QUERY
225# define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY
226#endif
227#ifndef MLD_LISTENER_REPORT
228# define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT
229#endif
230#ifndef MLD_LISTENER_REDUCTION
231# define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION
232#endif
233static char *icmp6_type_name(int id)
234{
235 switch (id) {
236 case ICMP6_DST_UNREACH: return "Destination Unreachable";
237 case ICMP6_PACKET_TOO_BIG: return "Packet too big";
238 case ICMP6_TIME_EXCEEDED: return "Time Exceeded";
239 case ICMP6_PARAM_PROB: return "Parameter Problem";
240 case ICMP6_ECHO_REPLY: return "Echo Reply";
241 case ICMP6_ECHO_REQUEST: return "Echo Request";
242 case MLD_LISTENER_QUERY: return "Listener Query";
243 case MLD_LISTENER_REPORT: return "Listener Report";
244 case MLD_LISTENER_REDUCTION: return "Listener Reduction";
245 default: return "unknown ICMP type";
246 }
247}
248
249static void unpack(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit)
250{
251 struct icmp6_hdr *icmppkt;
252 struct timeval tv, *tp;
253 int dupflag;
254 unsigned long triptime;
255 char buf[INET6_ADDRSTRLEN];
256
257 gettimeofday(&tv, NULL);
258
259 /* discard if too short */
260 if (sz < (datalen + sizeof(struct icmp6_hdr)))
261 return;
262
263 icmppkt = (struct icmp6_hdr *) packet;
264 if (icmppkt->icmp6_id != myid)
265 return; /* not our ping */
266
267 if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
268 uint16_t recv_seq = ntohs(icmppkt->icmp6_seq);
269 ++nreceived;
270 tp = (struct timeval *) &icmppkt->icmp6_data8[4];
271
272 if ((tv.tv_usec -= tp->tv_usec) < 0) {
273 --tv.tv_sec;
274 tv.tv_usec += 1000000;
275 }
276 tv.tv_sec -= tp->tv_sec;
277
278 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
279 tsum += triptime;
280 if (triptime < tmin)
281 tmin = triptime;
282 if (triptime > tmax)
283 tmax = triptime;
284
285 if (TST(recv_seq % MAX_DUP_CHK)) {
286 ++nrepeats;
287 --nreceived;
288 dupflag = 1;
289 } else {
290 SET(recv_seq % MAX_DUP_CHK);
291 dupflag = 0;
292 }
293
294 if (option_mask32 & OPT_QUIET)
295 return;
296
297 printf("%d bytes from %s: icmp6_seq=%u", sz,
298 inet_ntop(AF_INET6, &pingaddr.sin6_addr,
299 buf, sizeof(buf)),
300 recv_seq);
301 printf(" ttl=%d time=%lu.%lu ms", hoplimit,
302 triptime / 10, triptime % 10);
303 if (dupflag)
304 printf(" (DUP!)");
305 puts("");
306 } else {
307 if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST)
308 bb_error_msg("warning: got ICMP %d (%s)",
309 icmppkt->icmp6_type,
310 icmp6_type_name(icmppkt->icmp6_type));
311 }
312 fflush(stdout);
313}
314
315extern int BUG_bad_offsetof_icmp6_cksum(void);
316static void ping(const char *host)
317{
318 len_and_sockaddr *lsa;
319 char packet[datalen + MAXIPLEN + MAXICMPLEN];
320 char buf[INET6_ADDRSTRLEN];
321 int sockopt;
322 struct msghdr msg;
323 struct sockaddr_in6 from;
324 struct iovec iov;
325 char control_buf[CMSG_SPACE(36)];
326
327 pingsock = create_icmp6_socket();
328
329 lsa = host_and_af2sockaddr(host, 0, AF_INET6);
330 hostname = xmalloc_sockaddr2host_noport(&lsa->sa, lsa->len);
331 pingaddr = lsa->sin6;
332
333#ifdef ICMP6_FILTER
334 {
335 struct icmp6_filter filt;
336 if (!(option_mask32 & OPT_VERBOSE)) {
337 ICMP6_FILTER_SETBLOCKALL(&filt);
338 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
339 } else {
340 ICMP6_FILTER_SETPASSALL(&filt);
341 }
342 if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
343 sizeof(filt)) < 0)
344 bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
345 }
346#endif /*ICMP6_FILTER*/
347
348 /* enable broadcast pings */
349 setsockopt_broadcast(pingsock);
350
351 /* set recv buf for broadcast pings */
352 sockopt = 48 * 1024; /* explain why 48k? */
353 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
354
355 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
356 if (offsetof(struct icmp6_hdr, icmp6_cksum) != 2)
357 BUG_bad_offsetof_icmp6_cksum();
358 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
359
360 /* request ttl info to be returned in ancillary data */
361 setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, &const_int_1, sizeof(const_int_1));
362
363 if (if_index)
364 pingaddr.sin6_scope_id = if_index;
365
366 printf("PING %s (%s): %d data bytes\n",
367 hostname,
368 inet_ntop(AF_INET6, &pingaddr.sin6_addr,
369 buf, sizeof(buf)),
370 datalen);
371
372 signal(SIGINT, pingstats);
373
374 /* start the ping's going ... */
375 sendping(0);
376
377 /* listen for replies */
378 msg.msg_name = &from;
379 msg.msg_namelen = sizeof(from);
380 msg.msg_iov = &iov;
381 msg.msg_iovlen = 1;
382 msg.msg_control = control_buf;
383 iov.iov_base = packet;
384 iov.iov_len = sizeof(packet);
385 while (1) {
386 int c;
387 struct cmsghdr *mp;
388 int hoplimit = -1;
389 msg.msg_controllen = sizeof(control_buf);
390
391 c = recvmsg(pingsock, &msg, 0);
392 if (c < 0) {
393 if (errno != EINTR)
394 bb_perror_msg("recvfrom");
395 continue;
396 }
397 for (mp = CMSG_FIRSTHDR(&msg); mp; mp = CMSG_NXTHDR(&msg, mp)) {
398 if (mp->cmsg_level == SOL_IPV6
399 && mp->cmsg_type == IPV6_HOPLIMIT
400 /* don't check len - we trust the kernel: */
401 /* && mp->cmsg_len >= CMSG_LEN(sizeof(int)) */
402 ) {
403 hoplimit = *(int*)CMSG_DATA(mp);
404 }
405 }
406 unpack(packet, c, &from, hoplimit);
407 if (pingcount > 0 && nreceived >= pingcount)
408 break;
409 }
410 pingstats(0);
411}
412
413int ping6_main(int argc, char **argv)
414{
415 char *opt_c, *opt_s, *opt_I;
416
417 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
418
419 /* exactly one argument needed, -v and -q don't mix */
420 opt_complementary = "=1:q--v:v--q";
421 getopt32(argc, argv, OPT_STRING, &opt_c, &opt_s, &opt_I);
422 if (option_mask32 & 4) pingcount = xatoul(opt_c); // -c
423 if (option_mask32 & 8) datalen = xatou16(opt_s); // -s
424 if (option_mask32 & 0x10) { // -I
425 if_index = if_nametoindex(opt_I);
426 if (!if_index)
427 bb_error_msg_and_die(
428 "%s: invalid interface name", opt_I);
429 }
430
431 myid = (int16_t) getpid();
432 ping(argv[optind]);
433 return EXIT_SUCCESS;
434}
435#endif /* ! CONFIG_FEATURE_FANCY_PING6 */
436
437/*
438 * Copyright (c) 1989 The Regents of the University of California.
439 * All rights reserved.
440 *
441 * This code is derived from software contributed to Berkeley by
442 * Mike Muuss.
443 *
444 * Redistribution and use in source and binary forms, with or without
445 * modification, are permitted provided that the following conditions
446 * are met:
447 * 1. Redistributions of source code must retain the above copyright
448 * notice, this list of conditions and the following disclaimer.
449 * 2. Redistributions in binary form must reproduce the above copyright
450 * notice, this list of conditions and the following disclaimer in the
451 * documentation and/or other materials provided with the distribution.
452 *
453 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
454 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
455 *
456 * 4. Neither the name of the University nor the names of its contributors
457 * may be used to endorse or promote products derived from this software
458 * without specific prior written permission.
459 *
460 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
461 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
462 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
463 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
464 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
465 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
466 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
467 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
468 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
469 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
470 * SUCH DAMAGE.
471 */