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 #define O_QUIET (1 << 0)
57 #define O_VERBOSE (1 << 1)
59 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
60 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
61 #define SET(bit) (A(bit) |= B(bit))
62 #define CLR(bit) (A(bit) &= (~B(bit)))
63 #define TST(bit) (A(bit) & B(bit))
65 static void ping(const char *host);
68 #ifndef CONFIG_FEATURE_FANCY_PING6
69 static struct hostent *h;
73 printf("No response from %s\n", h->h_name);
77 static void ping(const char *host)
79 struct sockaddr_in6 pingaddr;
80 struct icmp6_hdr *pkt;
83 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
85 pingsock = create_icmp6_socket();
87 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
89 pingaddr.sin6_family = AF_INET6;
90 h = xgethostbyname2(host, AF_INET6);
91 memcpy(&pingaddr.sin6_addr, h->h_addr, sizeof(pingaddr.sin6_addr));
93 pkt = (struct icmp6_hdr *) packet;
94 memset(pkt, 0, sizeof(packet));
95 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
97 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
98 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt,
101 c = sendto(pingsock, packet, DEFDATALEN + sizeof (struct icmp6_hdr), 0,
102 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
104 if (c < 0 || c != sizeof(packet))
105 bb_perror_msg_and_die("sendto");
107 signal(SIGALRM, noresp);
108 alarm(5); /* give the host 5000ms to respond */
109 /* listen for replies */
111 struct sockaddr_in6 from;
112 size_t fromlen = sizeof(from);
114 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
115 (struct sockaddr *) &from, &fromlen)) < 0) {
118 bb_perror_msg("recvfrom");
121 if (c >= 8) { /* icmp6_hdr */
122 pkt = (struct icmp6_hdr *) packet;
123 if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
127 printf("%s is alive!\n", h->h_name);
131 int ping6_main(int argc, char **argv)
141 #else /* ! CONFIG_FEATURE_FANCY_PING6 */
142 /* full(er) version */
143 static struct sockaddr_in6 pingaddr;
144 static int pingsock = -1;
145 static int datalen; /* intentionally uninitialized to work around gcc bug */
148 static long ntransmitted, nreceived, nrepeats, pingcount;
149 static int myid, options;
150 static unsigned long tmin = ULONG_MAX, tmax, tsum;
151 static char rcvd_tbl[MAX_DUP_CHK / 8];
153 # ifdef CONFIG_FEATURE_FANCY_PING
156 struct hostent *hostent;
158 static void sendping(int);
159 static void pingstats(int);
160 static void unpack(char *, int, struct sockaddr_in6 *, int);
162 /**************************************************************************/
164 static void pingstats(int junk)
168 signal(SIGINT, SIG_IGN);
170 printf("\n--- %s ping statistics ---\n", hostent->h_name);
171 printf("%ld packets transmitted, ", ntransmitted);
172 printf("%ld packets received, ", nreceived);
174 printf("%ld duplicates, ", nrepeats);
176 printf("%ld%% packet loss\n",
177 (ntransmitted - nreceived) * 100 / ntransmitted);
179 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
180 tmin / 10, tmin % 10,
181 (tsum / (nreceived + nrepeats)) / 10,
182 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
184 status = EXIT_SUCCESS;
186 status = EXIT_FAILURE;
190 static void sendping(int junk)
192 struct icmp6_hdr *pkt;
194 char packet[datalen + sizeof (struct icmp6_hdr)];
196 pkt = (struct icmp6_hdr *) packet;
198 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
200 pkt->icmp6_cksum = 0;
201 pkt->icmp6_seq = ntransmitted++;
202 pkt->icmp6_id = myid;
203 CLR(pkt->icmp6_seq % MAX_DUP_CHK);
205 gettimeofday((struct timeval *) &pkt->icmp6_data8[4], NULL);
207 i = sendto(pingsock, packet, sizeof(packet), 0,
208 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
211 bb_perror_msg_and_die("sendto");
212 else if ((size_t)i != sizeof(packet))
213 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
214 (int)sizeof(packet));
216 signal(SIGALRM, sendping);
217 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
219 } else { /* done, wait for the last ping to come back */
220 /* todo, don't necessarily need to wait so long... */
221 signal(SIGALRM, pingstats);
226 /* RFC3542 changed some definitions from RFC2292 for no good reason, whee !
227 * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */
228 #ifndef MLD_LISTENER_QUERY
229 # define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY
231 #ifndef MLD_LISTENER_REPORT
232 # define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT
234 #ifndef MLD_LISTENER_REDUCTION
235 # define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION
237 static char *icmp6_type_name (int id)
240 case ICMP6_DST_UNREACH: return "Destination Unreachable";
241 case ICMP6_PACKET_TOO_BIG: return "Packet too big";
242 case ICMP6_TIME_EXCEEDED: return "Time Exceeded";
243 case ICMP6_PARAM_PROB: return "Parameter Problem";
244 case ICMP6_ECHO_REPLY: return "Echo Reply";
245 case ICMP6_ECHO_REQUEST: return "Echo Request";
246 case MLD_LISTENER_QUERY: return "Listener Query";
247 case MLD_LISTENER_REPORT: return "Listener Report";
248 case MLD_LISTENER_REDUCTION: return "Listener Reduction";
249 default: return "unknown ICMP type";
253 static void unpack(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit)
255 struct icmp6_hdr *icmppkt;
256 struct timeval tv, *tp;
258 unsigned long triptime;
259 char buf[INET6_ADDRSTRLEN];
261 gettimeofday(&tv, NULL);
263 /* discard if too short */
264 if (sz < (datalen + sizeof(struct icmp6_hdr)))
267 icmppkt = (struct icmp6_hdr *) packet;
269 if (icmppkt->icmp6_id != myid)
270 return; /* not our ping */
272 if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
274 tp = (struct timeval *) &icmppkt->icmp6_data8[4];
276 if ((tv.tv_usec -= tp->tv_usec) < 0) {
278 tv.tv_usec += 1000000;
280 tv.tv_sec -= tp->tv_sec;
282 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
289 if (TST(icmppkt->icmp6_seq % MAX_DUP_CHK)) {
294 SET(icmppkt->icmp6_seq % MAX_DUP_CHK);
298 if (options & O_QUIET)
301 printf("%d bytes from %s: icmp6_seq=%u", sz,
302 inet_ntop(AF_INET6, &pingaddr.sin6_addr,
305 printf(" ttl=%d time=%lu.%lu ms", hoplimit,
306 triptime / 10, triptime % 10);
311 if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST)
312 bb_error_msg("Warning: Got ICMP %d (%s)",
313 icmppkt->icmp6_type, icmp6_type_name (icmppkt->icmp6_type));
316 static void ping(const char *host)
318 char packet[datalen + MAXIPLEN + MAXICMPLEN];
319 char buf[INET6_ADDRSTRLEN];
322 struct sockaddr_in6 from;
324 char control_buf[CMSG_SPACE(36)];
326 pingsock = create_icmp6_socket();
328 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
330 pingaddr.sin6_family = AF_INET6;
331 hostent = xgethostbyname2(host, AF_INET6);
332 if (hostent->h_addrtype != AF_INET6)
333 bb_error_msg_and_die("unknown address type; only AF_INET6 is currently supported.");
335 memcpy(&pingaddr.sin6_addr, hostent->h_addr, sizeof(pingaddr.sin6_addr));
339 struct icmp6_filter filt;
340 if (!(options & O_VERBOSE)) {
341 ICMP6_FILTER_SETBLOCKALL(&filt);
342 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
344 ICMP6_FILTER_SETPASSALL(&filt);
346 if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
348 bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
350 #endif /*ICMP6_FILTER*/
352 /* enable broadcast pings */
354 setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt,
357 /* set recv buf for broadcast pings */
359 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
362 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
363 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt,
367 setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, (char *) &sockopt,
371 if ((pingaddr.sin6_scope_id = if_nametoindex(ifname)) == 0)
372 bb_error_msg_and_die("%s: invalid interface name", ifname);
375 printf("PING %s (%s): %d data bytes\n",
377 inet_ntop(AF_INET6, &pingaddr.sin6_addr,
381 signal(SIGINT, pingstats);
383 /* start the ping's going ... */
386 /* listen for replies */
388 msg.msg_namelen=sizeof(from);
391 msg.msg_control=control_buf;
393 iov.iov_len=sizeof(packet);
396 struct cmsghdr *cmsgptr = NULL;
398 msg.msg_controllen=sizeof(control_buf);
400 if ((c = recvmsg(pingsock, &msg, 0)) < 0) {
403 bb_perror_msg("recvfrom");
406 for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL;
407 cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
408 if (cmsgptr->cmsg_level == SOL_IPV6 &&
409 cmsgptr->cmsg_type == IPV6_HOPLIMIT ) {
410 hoplimit=*(int*)CMSG_DATA(cmsgptr);
413 unpack(packet, c, &from, hoplimit);
414 if (pingcount > 0 && nreceived >= pingcount)
420 int ping6_main(int argc, char **argv)
424 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
429 /* Parse any options */
430 while (argc >= 1 && **argv == '-') {
436 options |= O_VERBOSE;
439 options &= ~O_VERBOSE;
446 pingcount = atoi(*argv);
452 datalen = atoi(*argv);
469 myid = getpid() & 0xFFFF;
473 #endif /* ! CONFIG_FEATURE_FANCY_PING6 */
476 * Copyright (c) 1989 The Regents of the University of California.
477 * All rights reserved.
479 * This code is derived from software contributed to Berkeley by
482 * Redistribution and use in source and binary forms, with or without
483 * modification, are permitted provided that the following conditions
485 * 1. Redistributions of source code must retain the above copyright
486 * notice, this list of conditions and the following disclaimer.
487 * 2. Redistributions in binary form must reproduce the above copyright
488 * notice, this list of conditions and the following disclaimer in the
489 * documentation and/or other materials provided with the distribution.
491 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
492 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
494 * 4. Neither the name of the University nor the names of its contributors
495 * may be used to endorse or promote products derived from this software
496 * without specific prior written permission.
498 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
499 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
500 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
501 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
502 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
503 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
504 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
505 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
506 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
507 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF