blob: 5cb02f51a962073daca0fd9390325dd277392f7a [file] [log] [blame]
Eric Andersen51b8bd62002-07-03 11:46:38 +00001/* vi: set sw=4 ts=4: */
2/*
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003 * $Id: ping6.c,v 1.6 2004/03/15 08:28:48 andersen Exp $
Eric Andersen51b8bd62002-07-03 11:46:38 +00004 * Mini ping implementation for busybox
5 *
6 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
7 *
"Robert P. J. Day"2819f752006-07-11 11:32:31 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen51b8bd62002-07-03 11:46:38 +00009 *
10 * This version of ping is adapted from the ping in netkit-base 0.10,
11 * which is:
12 *
13 * Copyright (c) 1989 The Regents of the University of California.
14 * All rights reserved.
15 *
16 * This code is derived from software contributed to Berkeley by
17 * Mike Muuss.
Eric Andersenc7bda1c2004-03-15 08:29:22 +000018 *
Eric Andersen51b8bd62002-07-03 11:46:38 +000019 * Original copyright notice is retained at the end of this file.
20 *
21 * This version is an adaptation of ping.c from busybox.
22 * The code was modified by Bart Visscher <magick@linux-fan.com>
23 */
24
Eric Andersen51b8bd62002-07-03 11:46:38 +000025#include <netinet/icmp6.h>
Eric Andersen51b8bd62002-07-03 11:46:38 +000026#include <net/if.h>
Eric Andersen51b8bd62002-07-03 11:46:38 +000027#include "busybox.h"
28
Denis Vlasenko44c2eb22007-01-08 23:55:33 +000029/* I see RENUMBERED constants in bits/in.h - !!?
30 * What a fuck is going on with libc? Is it a glibc joke? */
31#ifdef IPV6_2292HOPLIMIT
32#undef IPV6_HOPLIMIT
33#define IPV6_HOPLIMIT IPV6_2292HOPLIMIT
34#endif
35
Rob Landleybc68cd12006-03-10 19:22:06 +000036enum {
37 DEFDATALEN = 56,
38 MAXIPLEN = 60,
39 MAXICMPLEN = 76,
40 MAXPACKET = 65468,
41 MAX_DUP_CHK = (8 * 128),
42 MAXWAIT = 10,
43 PINGINTERVAL = 1 /* second */
44};
Eric Andersen51b8bd62002-07-03 11:46:38 +000045
Eric Andersen51b8bd62002-07-03 11:46:38 +000046static void ping(const char *host);
47
Eric Andersen51b8bd62002-07-03 11:46:38 +000048#ifndef CONFIG_FEATURE_FANCY_PING6
Denis Vlasenko4a5cf162006-11-20 00:48:22 +000049
50/* simple version */
51
Eric Andersen787ff552003-05-22 07:10:22 +000052static struct hostent *h;
53
Denis Vlasenkocb6874c2006-09-02 16:13:36 +000054static void noresp(int ign)
Eric Andersen4e486a52003-01-12 06:08:33 +000055{
56 printf("No response from %s\n", h->h_name);
57 exit(EXIT_FAILURE);
58}
Eric Andersen51b8bd62002-07-03 11:46:38 +000059
60static void ping(const char *host)
61{
Eric Andersen51b8bd62002-07-03 11:46:38 +000062 struct sockaddr_in6 pingaddr;
63 struct icmp6_hdr *pkt;
64 int pingsock, c;
65 int sockopt;
66 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
67
Eric Andersen51b8bd62002-07-03 11:46:38 +000068 pingsock = create_icmp6_socket();
69
70 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
71
72 pingaddr.sin6_family = AF_INET6;
73 h = xgethostbyname2(host, AF_INET6);
74 memcpy(&pingaddr.sin6_addr, h->h_addr, sizeof(pingaddr.sin6_addr));
75
76 pkt = (struct icmp6_hdr *) packet;
77 memset(pkt, 0, sizeof(packet));
78 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
79
80 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
81 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt,
82 sizeof(sockopt));
83
Rob Landley07a637d2006-04-01 17:28:11 +000084 c = sendto(pingsock, packet, DEFDATALEN + sizeof (struct icmp6_hdr), 0,
Eric Andersen51b8bd62002-07-03 11:46:38 +000085 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
86
Denis Vlasenko44c2eb22007-01-08 23:55:33 +000087 if (c < 0) {
Denis Vlasenkocb6874c2006-09-02 16:13:36 +000088 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
Manuel Novoa III cad53642003-03-19 09:13:01 +000089 bb_perror_msg_and_die("sendto");
Denis Vlasenkocb6874c2006-09-02 16:13:36 +000090 }
Eric Andersen51b8bd62002-07-03 11:46:38 +000091
92 signal(SIGALRM, noresp);
93 alarm(5); /* give the host 5000ms to respond */
94 /* listen for replies */
95 while (1) {
96 struct sockaddr_in6 from;
Denis Vlasenko806116b2006-12-31 12:14:16 +000097 socklen_t fromlen = sizeof(from);
Eric Andersen51b8bd62002-07-03 11:46:38 +000098
Denis Vlasenko806116b2006-12-31 12:14:16 +000099 c = recvfrom(pingsock, packet, sizeof(packet), 0,
100 (struct sockaddr *) &from, &fromlen);
101 if (c < 0) {
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000102 if (errno != EINTR)
103 bb_perror_msg("recvfrom");
Eric Andersen51b8bd62002-07-03 11:46:38 +0000104 continue;
105 }
106 if (c >= 8) { /* icmp6_hdr */
107 pkt = (struct icmp6_hdr *) packet;
108 if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
109 break;
110 }
111 }
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000112 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000113 printf("%s is alive!\n", h->h_name);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000114}
115
Rob Landleydfba7412006-03-06 20:47:33 +0000116int ping6_main(int argc, char **argv)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000117{
118 argc--;
119 argv++;
120 if (argc < 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 bb_show_usage();
Eric Andersen51b8bd62002-07-03 11:46:38 +0000122 ping(*argv);
123 return EXIT_SUCCESS;
124}
125
126#else /* ! CONFIG_FEATURE_FANCY_PING6 */
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000127
Eric Andersen51b8bd62002-07-03 11:46:38 +0000128/* full(er) version */
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000129
130#define OPT_STRING "qvc:s:I:"
131enum {
132 OPT_QUIET = 1 << 0,
133 OPT_VERBOSE = 1 << 1,
134};
135
Eric Andersen51b8bd62002-07-03 11:46:38 +0000136static struct sockaddr_in6 pingaddr;
137static int pingsock = -1;
Denis Vlasenko13858992006-10-08 12:49:22 +0000138static unsigned datalen; /* intentionally uninitialized to work around gcc bug */
Denis Vlasenkodb7f2e52006-09-02 16:16:23 +0000139static int if_index;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000140
Denis Vlasenko13858992006-10-08 12:49:22 +0000141static unsigned long ntransmitted, nreceived, nrepeats, pingcount;
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000142static int myid;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000143static unsigned long tmin = ULONG_MAX, tmax, tsum;
144static char rcvd_tbl[MAX_DUP_CHK / 8];
145
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000146static struct hostent *hostent;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000147
148static void sendping(int);
149static void pingstats(int);
150static void unpack(char *, int, struct sockaddr_in6 *, int);
151
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000152#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
153#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
154#define SET(bit) (A(bit) |= B(bit))
155#define CLR(bit) (A(bit) &= (~B(bit)))
156#define TST(bit) (A(bit) & B(bit))
157
Eric Andersen51b8bd62002-07-03 11:46:38 +0000158/**************************************************************************/
159
160static void pingstats(int junk)
161{
162 int status;
163
164 signal(SIGINT, SIG_IGN);
165
166 printf("\n--- %s ping statistics ---\n", hostent->h_name);
Denis Vlasenko13858992006-10-08 12:49:22 +0000167 printf("%lu packets transmitted, ", ntransmitted);
168 printf("%lu packets received, ", nreceived);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000169 if (nrepeats)
Denis Vlasenko13858992006-10-08 12:49:22 +0000170 printf("%lu duplicates, ", nrepeats);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000171 if (ntransmitted)
Denis Vlasenko13858992006-10-08 12:49:22 +0000172 printf("%lu%% packet loss\n",
Eric Andersen51b8bd62002-07-03 11:46:38 +0000173 (ntransmitted - nreceived) * 100 / ntransmitted);
174 if (nreceived)
175 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
176 tmin / 10, tmin % 10,
177 (tsum / (nreceived + nrepeats)) / 10,
178 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
179 if (nreceived != 0)
180 status = EXIT_SUCCESS;
181 else
182 status = EXIT_FAILURE;
183 exit(status);
184}
185
186static void sendping(int junk)
187{
188 struct icmp6_hdr *pkt;
189 int i;
Rob Landley07a637d2006-04-01 17:28:11 +0000190 char packet[datalen + sizeof (struct icmp6_hdr)];
Eric Andersen51b8bd62002-07-03 11:46:38 +0000191
192 pkt = (struct icmp6_hdr *) packet;
193
194 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
195 pkt->icmp6_code = 0;
196 pkt->icmp6_cksum = 0;
Denis Vlasenko919c10d2007-01-03 22:14:18 +0000197 pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
Eric Andersen51b8bd62002-07-03 11:46:38 +0000198 pkt->icmp6_id = myid;
199 CLR(pkt->icmp6_seq % MAX_DUP_CHK);
Denis Vlasenko919c10d2007-01-03 22:14:18 +0000200 ntransmitted++;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000201
202 gettimeofday((struct timeval *) &pkt->icmp6_data8[4], NULL);
203
204 i = sendto(pingsock, packet, sizeof(packet), 0,
205 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
206
207 if (i < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000208 bb_perror_msg_and_die("sendto");
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000209 if ((size_t)i != sizeof(packet))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000210 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
Eric Andersen51b8bd62002-07-03 11:46:38 +0000211 (int)sizeof(packet));
212
213 signal(SIGALRM, sendping);
214 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
215 alarm(PINGINTERVAL);
216 } else { /* done, wait for the last ping to come back */
217 /* todo, don't necessarily need to wait so long... */
218 signal(SIGALRM, pingstats);
219 alarm(MAXWAIT);
220 }
221}
222
Mike Frysinger2f135fc2006-03-13 23:48:18 +0000223/* RFC3542 changed some definitions from RFC2292 for no good reason, whee !
224 * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */
225#ifndef MLD_LISTENER_QUERY
226# define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY
Mike Frysinger9e094552006-03-10 23:41:29 +0000227#endif
Mike Frysinger2f135fc2006-03-13 23:48:18 +0000228#ifndef MLD_LISTENER_REPORT
229# define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT
Mike Frysinger9e094552006-03-10 23:41:29 +0000230#endif
Mike Frysinger2f135fc2006-03-13 23:48:18 +0000231#ifndef MLD_LISTENER_REDUCTION
232# define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION
Mike Frysinger9e094552006-03-10 23:41:29 +0000233#endif
Denis Vlasenkoe0b7f712006-09-02 16:57:59 +0000234static char *icmp6_type_name(int id)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000235{
236 switch (id) {
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000237 case ICMP6_DST_UNREACH: return "Destination Unreachable";
238 case ICMP6_PACKET_TOO_BIG: return "Packet too big";
239 case ICMP6_TIME_EXCEEDED: return "Time Exceeded";
240 case ICMP6_PARAM_PROB: return "Parameter Problem";
241 case ICMP6_ECHO_REPLY: return "Echo Reply";
242 case ICMP6_ECHO_REQUEST: return "Echo Request";
243 case MLD_LISTENER_QUERY: return "Listener Query";
244 case MLD_LISTENER_REPORT: return "Listener Report";
245 case MLD_LISTENER_REDUCTION: return "Listener Reduction";
246 default: return "unknown ICMP type";
Eric Andersen51b8bd62002-07-03 11:46:38 +0000247 }
248}
249
250static void unpack(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit)
251{
252 struct icmp6_hdr *icmppkt;
253 struct timeval tv, *tp;
254 int dupflag;
255 unsigned long triptime;
256 char buf[INET6_ADDRSTRLEN];
257
258 gettimeofday(&tv, NULL);
259
260 /* discard if too short */
261 if (sz < (datalen + sizeof(struct icmp6_hdr)))
262 return;
263
264 icmppkt = (struct icmp6_hdr *) packet;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000265 if (icmppkt->icmp6_id != myid)
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000266 return; /* not our ping */
Eric Andersen51b8bd62002-07-03 11:46:38 +0000267
268 if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000269 ++nreceived;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000270 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(icmppkt->icmp6_seq % MAX_DUP_CHK)) {
286 ++nrepeats;
287 --nreceived;
288 dupflag = 1;
289 } else {
290 SET(icmppkt->icmp6_seq % MAX_DUP_CHK);
291 dupflag = 0;
292 }
293
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000294 if (option_mask32 & OPT_QUIET)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000295 return;
296
297 printf("%d bytes from %s: icmp6_seq=%u", sz,
Rob Landley7a260f02006-06-19 03:20:03 +0000298 inet_ntop(AF_INET6, &pingaddr.sin6_addr,
Eric Andersen51b8bd62002-07-03 11:46:38 +0000299 buf, sizeof(buf)),
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000300 ntohs(icmppkt->icmp6_seq));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000301 printf(" ttl=%d time=%lu.%lu ms", hoplimit,
Eric Andersen51b8bd62002-07-03 11:46:38 +0000302 triptime / 10, triptime % 10);
303 if (dupflag)
304 printf(" (DUP!)");
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000305 puts("");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000306 } else
Eric Andersen51b8bd62002-07-03 11:46:38 +0000307 if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST)
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000308 bb_error_msg("warning: got ICMP %d (%s)",
Denis Vlasenkoe0b7f712006-09-02 16:57:59 +0000309 icmppkt->icmp6_type, icmp6_type_name(icmppkt->icmp6_type));
Eric Andersen51b8bd62002-07-03 11:46:38 +0000310}
311
312static void ping(const char *host)
313{
314 char packet[datalen + MAXIPLEN + MAXICMPLEN];
315 char buf[INET6_ADDRSTRLEN];
316 int sockopt;
317 struct msghdr msg;
318 struct sockaddr_in6 from;
319 struct iovec iov;
320 char control_buf[CMSG_SPACE(36)];
321
322 pingsock = create_icmp6_socket();
323
324 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
325
326 pingaddr.sin6_family = AF_INET6;
327 hostent = xgethostbyname2(host, AF_INET6);
328 if (hostent->h_addrtype != AF_INET6)
Denis Vlasenkoebe578a2006-10-26 17:17:59 +0000329 bb_error_msg_and_die("unknown address type; only AF_INET6 is currently supported");
Eric Andersen51b8bd62002-07-03 11:46:38 +0000330
331 memcpy(&pingaddr.sin6_addr, hostent->h_addr, sizeof(pingaddr.sin6_addr));
332
333#ifdef ICMP6_FILTER
334 {
335 struct icmp6_filter filt;
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000336 if (!(option_mask32 & OPT_VERBOSE)) {
Eric Andersen51b8bd62002-07-03 11:46:38 +0000337 ICMP6_FILTER_SETBLOCKALL(&filt);
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000338 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000339 } else {
340 ICMP6_FILTER_SETPASSALL(&filt);
341 }
342 if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
343 sizeof(filt)) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000344 bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
Eric Andersen51b8bd62002-07-03 11:46:38 +0000345 }
346#endif /*ICMP6_FILTER*/
347
348 /* enable broadcast pings */
Denis Vlasenko48237b02006-11-22 23:22:06 +0000349 setsockopt_broadcast(pingsock);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000350
351 /* set recv buf for broadcast pings */
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000352 sockopt = 48 * 1024; /* explain why 48k? */
Eric Andersen51b8bd62002-07-03 11:46:38 +0000353 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
354 sizeof(sockopt));
355
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000356 sockopt = 2; /* iputils-ss020927 does this */
Eric Andersen51b8bd62002-07-03 11:46:38 +0000357 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
358 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt,
359 sizeof(sockopt));
360
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000361 /* request ttl info to be returned in ancillary data */
Eric Andersen51b8bd62002-07-03 11:46:38 +0000362 sockopt = 1;
363 setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, (char *) &sockopt,
364 sizeof(sockopt));
365
Denis Vlasenkodb7f2e52006-09-02 16:16:23 +0000366 if (if_index)
367 pingaddr.sin6_scope_id = if_index;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000368
369 printf("PING %s (%s): %d data bytes\n",
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000370 hostent->h_name,
371 inet_ntop(AF_INET6, &pingaddr.sin6_addr,
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000372 buf, sizeof(buf)),
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000373 datalen);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000374
375 signal(SIGINT, pingstats);
376
377 /* start the ping's going ... */
378 sendping(0);
379
380 /* listen for replies */
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000381 msg.msg_name = &from;
382 msg.msg_namelen = sizeof(from);
383 msg.msg_iov = &iov;
384 msg.msg_iovlen = 1;
385 msg.msg_control = control_buf;
386 iov.iov_base = packet;
387 iov.iov_len = sizeof(packet);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000388 while (1) {
389 int c;
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000390 struct cmsghdr *mp;
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000391 int hoplimit = -1;
392 msg.msg_controllen = sizeof(control_buf);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000393
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000394 c = recvmsg(pingsock, &msg, 0);
395 if (c < 0) {
396 if (errno != EINTR)
397 bb_perror_msg("recvfrom");
Eric Andersen51b8bd62002-07-03 11:46:38 +0000398 continue;
399 }
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000400 for (mp = CMSG_FIRSTHDR(&msg); mp; mp = CMSG_NXTHDR(&msg, mp)) {
401 if (mp->cmsg_level == SOL_IPV6
402 && mp->cmsg_type == IPV6_HOPLIMIT
403 /* don't check len - we trust the kernel: */
404 /* && mp->cmsg_len >= CMSG_LEN(sizeof(int)) */
405 ) {
406 hoplimit = *(int*)CMSG_DATA(mp);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000407 }
408 }
409 unpack(packet, c, &from, hoplimit);
410 if (pingcount > 0 && nreceived >= pingcount)
411 break;
412 }
413 pingstats(0);
414}
415
Rob Landleydfba7412006-03-06 20:47:33 +0000416int ping6_main(int argc, char **argv)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000417{
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000418 char *opt_c, *opt_s, *opt_I;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000419
420 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
421
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000422 /* exactly one argument needed, -v and -q don't mix */
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000423 opt_complementary = "=1:q--v:v--q";
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000424 getopt32(argc, argv, OPT_STRING, &opt_c, &opt_s, &opt_I);
425 if (option_mask32 & 4) pingcount = xatoul(opt_c); // -c
426 if (option_mask32 & 8) datalen = xatou16(opt_s); // -s
427 if (option_mask32 & 0x10) { // -I
428 if_index = if_nametoindex(opt_I);
429 if (!if_index)
430 bb_error_msg_and_die(
431 "%s: invalid interface name", opt_I);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000432 }
Eric Andersen51b8bd62002-07-03 11:46:38 +0000433
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000434 myid = (int16_t)getpid();
435 ping(argv[optind]);
Eric Andersen51b8bd62002-07-03 11:46:38 +0000436 return EXIT_SUCCESS;
437}
438#endif /* ! CONFIG_FEATURE_FANCY_PING6 */
439
440/*
441 * Copyright (c) 1989 The Regents of the University of California.
442 * All rights reserved.
443 *
444 * This code is derived from software contributed to Berkeley by
445 * Mike Muuss.
446 *
447 * Redistribution and use in source and binary forms, with or without
448 * modification, are permitted provided that the following conditions
449 * are met:
450 * 1. Redistributions of source code must retain the above copyright
451 * notice, this list of conditions and the following disclaimer.
452 * 2. Redistributions in binary form must reproduce the above copyright
453 * notice, this list of conditions and the following disclaimer in the
454 * documentation and/or other materials provided with the distribution.
455 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000456 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
457 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
Eric Andersen51b8bd62002-07-03 11:46:38 +0000458 *
459 * 4. Neither the name of the University nor the names of its contributors
460 * may be used to endorse or promote products derived from this software
461 * without specific prior written permission.
462 *
463 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
464 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
465 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
466 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
467 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
468 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
469 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
471 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
472 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
473 * SUCH DAMAGE.
474 */