1 /* vi: set sw=4 ts=4: */
3 * $Id: ping6.c,v 1.6 2004/03/15 08:28:48 andersen Exp $
4 * Mini ping implementation for busybox
6 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 * This version of ping is adapted from the ping in netkit-base 0.10,
13 * Copyright (c) 1989 The Regents of the University of California.
14 * All rights reserved.
16 * This code is derived from software contributed to Berkeley by
19 * Original copyright notice is retained at the end of this file.
21 * This version is an adaptation of ping.c from busybox.
22 * The code was modified by Bart Visscher <magick@linux-fan.com>
25 #include <sys/param.h>
26 #include <sys/socket.h>
28 #include <sys/times.h>
31 #include <netinet/in.h>
32 #include <netinet/ip6.h>
33 #include <netinet/icmp6.h>
34 #include <arpa/inet.h>
43 #include <stddef.h> /* offsetof */
51 MAX_DUP_CHK = (8 * 128),
53 PINGINTERVAL = 1 /* second */
56 static void ping(const char *host);
58 #ifndef CONFIG_FEATURE_FANCY_PING6
62 static struct hostent *h;
64 static void noresp(int ign)
66 printf("No response from %s\n", h->h_name);
70 static void ping(const char *host)
72 struct sockaddr_in6 pingaddr;
73 struct icmp6_hdr *pkt;
76 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
78 pingsock = create_icmp6_socket();
80 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
82 pingaddr.sin6_family = AF_INET6;
83 h = xgethostbyname2(host, AF_INET6);
84 memcpy(&pingaddr.sin6_addr, h->h_addr, sizeof(pingaddr.sin6_addr));
86 pkt = (struct icmp6_hdr *) packet;
87 memset(pkt, 0, sizeof(packet));
88 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
90 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
91 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt,
94 c = sendto(pingsock, packet, DEFDATALEN + sizeof (struct icmp6_hdr), 0,
95 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
97 if (c < 0 || c != sizeof(packet)) {
98 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
99 bb_perror_msg_and_die("sendto");
102 signal(SIGALRM, noresp);
103 alarm(5); /* give the host 5000ms to respond */
104 /* listen for replies */
106 struct sockaddr_in6 from;
107 socklen_t fromlen = sizeof(from);
109 c = recvfrom(pingsock, packet, sizeof(packet), 0,
110 (struct sockaddr *) &from, &fromlen);
114 bb_perror_msg("recvfrom");
117 if (c >= 8) { /* icmp6_hdr */
118 pkt = (struct icmp6_hdr *) packet;
119 if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
123 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
124 printf("%s is alive!\n", h->h_name);
128 int ping6_main(int argc, char **argv)
138 #else /* ! CONFIG_FEATURE_FANCY_PING6 */
140 /* full(er) version */
142 #define OPT_STRING "qvc:s:I:"
145 OPT_VERBOSE = 1 << 1,
148 static struct sockaddr_in6 pingaddr;
149 static int pingsock = -1;
150 static unsigned datalen; /* intentionally uninitialized to work around gcc bug */
153 static unsigned long ntransmitted, nreceived, nrepeats, pingcount;
155 static unsigned long tmin = ULONG_MAX, tmax, tsum;
156 static char rcvd_tbl[MAX_DUP_CHK / 8];
158 static struct hostent *hostent;
160 static void sendping(int);
161 static void pingstats(int);
162 static void unpack(char *, int, struct sockaddr_in6 *, int);
164 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
165 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
166 #define SET(bit) (A(bit) |= B(bit))
167 #define CLR(bit) (A(bit) &= (~B(bit)))
168 #define TST(bit) (A(bit) & B(bit))
170 /**************************************************************************/
172 static void pingstats(int junk)
176 signal(SIGINT, SIG_IGN);
178 printf("\n--- %s ping statistics ---\n", hostent->h_name);
179 printf("%lu packets transmitted, ", ntransmitted);
180 printf("%lu packets received, ", nreceived);
182 printf("%lu duplicates, ", nrepeats);
184 printf("%lu%% packet loss\n",
185 (ntransmitted - nreceived) * 100 / ntransmitted);
187 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
188 tmin / 10, tmin % 10,
189 (tsum / (nreceived + nrepeats)) / 10,
190 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
192 status = EXIT_SUCCESS;
194 status = EXIT_FAILURE;
198 static void sendping(int junk)
200 struct icmp6_hdr *pkt;
202 char packet[datalen + sizeof (struct icmp6_hdr)];
204 pkt = (struct icmp6_hdr *) packet;
206 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
208 pkt->icmp6_cksum = 0;
209 pkt->icmp6_seq = htons(ntransmitted++);
210 pkt->icmp6_id = myid;
211 CLR(pkt->icmp6_seq % MAX_DUP_CHK);
213 gettimeofday((struct timeval *) &pkt->icmp6_data8[4], NULL);
215 i = sendto(pingsock, packet, sizeof(packet), 0,
216 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
219 bb_perror_msg_and_die("sendto");
220 else if ((size_t)i != sizeof(packet))
221 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
222 (int)sizeof(packet));
224 signal(SIGALRM, sendping);
225 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
227 } else { /* done, wait for the last ping to come back */
228 /* todo, don't necessarily need to wait so long... */
229 signal(SIGALRM, pingstats);
234 /* RFC3542 changed some definitions from RFC2292 for no good reason, whee !
235 * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */
236 #ifndef MLD_LISTENER_QUERY
237 # define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY
239 #ifndef MLD_LISTENER_REPORT
240 # define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT
242 #ifndef MLD_LISTENER_REDUCTION
243 # define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION
245 static char *icmp6_type_name(int id)
248 case ICMP6_DST_UNREACH: return "Destination Unreachable";
249 case ICMP6_PACKET_TOO_BIG: return "Packet too big";
250 case ICMP6_TIME_EXCEEDED: return "Time Exceeded";
251 case ICMP6_PARAM_PROB: return "Parameter Problem";
252 case ICMP6_ECHO_REPLY: return "Echo Reply";
253 case ICMP6_ECHO_REQUEST: return "Echo Request";
254 case MLD_LISTENER_QUERY: return "Listener Query";
255 case MLD_LISTENER_REPORT: return "Listener Report";
256 case MLD_LISTENER_REDUCTION: return "Listener Reduction";
257 default: return "unknown ICMP type";
261 static void unpack(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit)
263 struct icmp6_hdr *icmppkt;
264 struct timeval tv, *tp;
266 unsigned long triptime;
267 char buf[INET6_ADDRSTRLEN];
269 gettimeofday(&tv, NULL);
271 /* discard if too short */
272 if (sz < (datalen + sizeof(struct icmp6_hdr)))
275 icmppkt = (struct icmp6_hdr *) packet;
276 if (icmppkt->icmp6_id != myid)
277 return; /* not our ping */
279 if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
281 tp = (struct timeval *) &icmppkt->icmp6_data8[4];
283 if ((tv.tv_usec -= tp->tv_usec) < 0) {
285 tv.tv_usec += 1000000;
287 tv.tv_sec -= tp->tv_sec;
289 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
296 if (TST(icmppkt->icmp6_seq % MAX_DUP_CHK)) {
301 SET(icmppkt->icmp6_seq % MAX_DUP_CHK);
305 if (option_mask32 & OPT_QUIET)
308 printf("%d bytes from %s: icmp6_seq=%u", sz,
309 inet_ntop(AF_INET6, &pingaddr.sin6_addr,
312 printf(" ttl=%d time=%lu.%lu ms", hoplimit,
313 triptime / 10, triptime % 10);
318 if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST)
319 bb_error_msg("warning: got ICMP %d (%s)",
320 icmppkt->icmp6_type, icmp6_type_name(icmppkt->icmp6_type));
323 static void ping(const char *host)
325 char packet[datalen + MAXIPLEN + MAXICMPLEN];
326 char buf[INET6_ADDRSTRLEN];
329 struct sockaddr_in6 from;
331 char control_buf[CMSG_SPACE(36)];
333 pingsock = create_icmp6_socket();
335 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
337 pingaddr.sin6_family = AF_INET6;
338 hostent = xgethostbyname2(host, AF_INET6);
339 if (hostent->h_addrtype != AF_INET6)
340 bb_error_msg_and_die("unknown address type; only AF_INET6 is currently supported");
342 memcpy(&pingaddr.sin6_addr, hostent->h_addr, sizeof(pingaddr.sin6_addr));
346 struct icmp6_filter filt;
347 if (!(option_mask32 & OPT_VERBOSE)) {
348 ICMP6_FILTER_SETBLOCKALL(&filt);
349 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
351 ICMP6_FILTER_SETPASSALL(&filt);
353 if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
355 bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
357 #endif /*ICMP6_FILTER*/
359 /* enable broadcast pings */
360 setsockopt_broadcast(pingsock);
362 /* set recv buf for broadcast pings */
364 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
367 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
368 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt,
372 setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, (char *) &sockopt,
376 pingaddr.sin6_scope_id = if_index;
378 printf("PING %s (%s): %d data bytes\n",
380 inet_ntop(AF_INET6, &pingaddr.sin6_addr,
384 signal(SIGINT, pingstats);
386 /* start the ping's going ... */
389 /* listen for replies */
390 msg.msg_name = &from;
391 msg.msg_namelen = sizeof(from);
394 msg.msg_control = control_buf;
395 iov.iov_base = packet;
396 iov.iov_len = sizeof(packet);
399 struct cmsghdr *cmsgptr = NULL;
401 msg.msg_controllen = sizeof(control_buf);
403 if ((c = recvmsg(pingsock, &msg, 0)) < 0) {
406 bb_perror_msg("recvfrom");
409 for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL;
410 cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
411 if (cmsgptr->cmsg_level == SOL_IPV6 &&
412 cmsgptr->cmsg_type == IPV6_HOPLIMIT ) {
413 hoplimit = *(int*)CMSG_DATA(cmsgptr);
416 unpack(packet, c, &from, hoplimit);
417 if (pingcount > 0 && nreceived >= pingcount)
423 int ping6_main(int argc, char **argv)
425 char *opt_c, *opt_s, *opt_I;
427 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
429 /* exactly one argument needed, -v and -q don't mix */
430 opt_complementary = "=1:q--v:v--q";
431 getopt32(argc, argv, OPT_STRING, &opt_c, &opt_s, &opt_I);
432 if (option_mask32 & 4) pingcount = xatoul(opt_c); // -c
433 if (option_mask32 & 8) datalen = xatou16(opt_s); // -s
434 if (option_mask32 & 0x10) { // -I
435 if_index = if_nametoindex(opt_I);
437 bb_error_msg_and_die(
438 "%s: invalid interface name", opt_I);
441 myid = (int16_t)getpid();
445 #endif /* ! CONFIG_FEATURE_FANCY_PING6 */
448 * Copyright (c) 1989 The Regents of the University of California.
449 * All rights reserved.
451 * This code is derived from software contributed to Berkeley by
454 * Redistribution and use in source and binary forms, with or without
455 * modification, are permitted provided that the following conditions
457 * 1. Redistributions of source code must retain the above copyright
458 * notice, this list of conditions and the following disclaimer.
459 * 2. Redistributions in binary form must reproduce the above copyright
460 * notice, this list of conditions and the following disclaimer in the
461 * documentation and/or other materials provided with the distribution.
463 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
464 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
466 * 4. Neither the name of the University nor the names of its contributors
467 * may be used to endorse or promote products derived from this software
468 * without specific prior written permission.
470 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
471 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
472 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
473 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
474 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
475 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
476 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
477 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
478 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
479 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF