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 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * This version of ping is adapted from the ping in netkit-base 0.10,
25 * Copyright (c) 1989 The Regents of the University of California.
26 * All rights reserved.
28 * This code is derived from software contributed to Berkeley by
31 * Original copyright notice is retained at the end of this file.
33 * This version is an adaptation of ping.c from busybox.
34 * The code was modified by Bart Visscher <magick@linux-fan.com>
37 #include <sys/param.h>
38 #include <sys/socket.h>
41 #include <sys/times.h>
42 #include <sys/signal.h>
44 #include <netinet/in.h>
45 #include <netinet/ip6.h>
46 #include <netinet/icmp6.h>
47 #include <arpa/inet.h>
56 #include <stddef.h> /* offsetof */
64 MAX_DUP_CHK = (8 * 128),
66 PINGINTERVAL = 1 /* second */
69 #define O_QUIET (1 << 0)
70 #define O_VERBOSE (1 << 1)
72 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
73 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
74 #define SET(bit) (A(bit) |= B(bit))
75 #define CLR(bit) (A(bit) &= (~B(bit)))
76 #define TST(bit) (A(bit) & B(bit))
78 static void ping(const char *host);
81 #ifndef CONFIG_FEATURE_FANCY_PING6
82 static struct hostent *h;
86 printf("No response from %s\n", h->h_name);
90 static void ping(const char *host)
92 struct sockaddr_in6 pingaddr;
93 struct icmp6_hdr *pkt;
96 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
98 pingsock = create_icmp6_socket();
100 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
102 pingaddr.sin6_family = AF_INET6;
103 h = xgethostbyname2(host, AF_INET6);
104 memcpy(&pingaddr.sin6_addr, h->h_addr, sizeof(pingaddr.sin6_addr));
106 pkt = (struct icmp6_hdr *) packet;
107 memset(pkt, 0, sizeof(packet));
108 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
110 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
111 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt,
114 c = sendto(pingsock, packet, sizeof(packet), 0,
115 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
117 if (c < 0 || c != sizeof(packet))
118 bb_perror_msg_and_die("sendto");
120 signal(SIGALRM, noresp);
121 alarm(5); /* give the host 5000ms to respond */
122 /* listen for replies */
124 struct sockaddr_in6 from;
125 size_t fromlen = sizeof(from);
127 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
128 (struct sockaddr *) &from, &fromlen)) < 0) {
131 bb_perror_msg("recvfrom");
134 if (c >= 8) { /* icmp6_hdr */
135 pkt = (struct icmp6_hdr *) packet;
136 if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
140 printf("%s is alive!\n", h->h_name);
144 int ping6_main(int argc, char **argv)
154 #else /* ! CONFIG_FEATURE_FANCY_PING6 */
155 /* full(er) version */
156 static struct sockaddr_in6 pingaddr;
157 static int pingsock = -1;
158 static int datalen; /* intentionally uninitialized to work around gcc bug */
161 static long ntransmitted, nreceived, nrepeats, pingcount;
162 static int myid, options;
163 static unsigned long tmin = ULONG_MAX, tmax, tsum;
164 static char rcvd_tbl[MAX_DUP_CHK / 8];
166 # ifdef CONFIG_FEATURE_FANCY_PING
169 struct hostent *hostent;
171 static void sendping(int);
172 static void pingstats(int);
173 static void unpack(char *, int, struct sockaddr_in6 *, int);
175 /**************************************************************************/
177 static void pingstats(int junk)
181 signal(SIGINT, SIG_IGN);
183 printf("\n--- %s ping statistics ---\n", hostent->h_name);
184 printf("%ld packets transmitted, ", ntransmitted);
185 printf("%ld packets received, ", nreceived);
187 printf("%ld duplicates, ", nrepeats);
189 printf("%ld%% packet loss\n",
190 (ntransmitted - nreceived) * 100 / ntransmitted);
192 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
193 tmin / 10, tmin % 10,
194 (tsum / (nreceived + nrepeats)) / 10,
195 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
197 status = EXIT_SUCCESS;
199 status = EXIT_FAILURE;
203 static void sendping(int junk)
205 struct icmp6_hdr *pkt;
207 char packet[datalen + 8];
209 pkt = (struct icmp6_hdr *) packet;
211 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
213 pkt->icmp6_cksum = 0;
214 pkt->icmp6_seq = ntransmitted++;
215 pkt->icmp6_id = myid;
216 CLR(pkt->icmp6_seq % MAX_DUP_CHK);
218 gettimeofday((struct timeval *) &pkt->icmp6_data8[4], NULL);
220 i = sendto(pingsock, packet, sizeof(packet), 0,
221 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
224 bb_perror_msg_and_die("sendto");
225 else if ((size_t)i != sizeof(packet))
226 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
227 (int)sizeof(packet));
229 signal(SIGALRM, sendping);
230 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
232 } else { /* done, wait for the last ping to come back */
233 /* todo, don't necessarily need to wait so long... */
234 signal(SIGALRM, pingstats);
239 static char *icmp6_type_name (int id)
242 case ICMP6_DST_UNREACH: return "Destination Unreachable";
243 case ICMP6_PACKET_TOO_BIG: return "Packet too big";
244 case ICMP6_TIME_EXCEEDED: return "Time Exceeded";
245 case ICMP6_PARAM_PROB: return "Parameter Problem";
246 case ICMP6_ECHO_REPLY: return "Echo Reply";
247 case ICMP6_ECHO_REQUEST: return "Echo Request";
248 case ICMP6_MEMBERSHIP_QUERY: return "Membership Query";
249 case ICMP6_MEMBERSHIP_REPORT: return "Membership Report";
250 case ICMP6_MEMBERSHIP_REDUCTION: return "Membership Reduction";
251 default: return "unknown ICMP type";
255 static void unpack(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit)
257 struct icmp6_hdr *icmppkt;
258 struct timeval tv, *tp;
260 unsigned long triptime;
261 char buf[INET6_ADDRSTRLEN];
263 gettimeofday(&tv, NULL);
265 /* discard if too short */
266 if (sz < (datalen + sizeof(struct icmp6_hdr)))
269 icmppkt = (struct icmp6_hdr *) packet;
271 if (icmppkt->icmp6_id != myid)
272 return; /* not our ping */
274 if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
276 tp = (struct timeval *) &icmppkt->icmp6_data8[4];
278 if ((tv.tv_usec -= tp->tv_usec) < 0) {
280 tv.tv_usec += 1000000;
282 tv.tv_sec -= tp->tv_sec;
284 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
291 if (TST(icmppkt->icmp6_seq % MAX_DUP_CHK)) {
296 SET(icmppkt->icmp6_seq % MAX_DUP_CHK);
300 if (options & O_QUIET)
303 printf("%d bytes from %s: icmp6_seq=%u", sz,
304 inet_ntop(AF_INET6, (struct in_addr6 *) &pingaddr.sin6_addr,
307 printf(" ttl=%d time=%lu.%lu ms", hoplimit,
308 triptime / 10, triptime % 10);
313 if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST)
314 bb_error_msg("Warning: Got ICMP %d (%s)",
315 icmppkt->icmp6_type, icmp6_type_name (icmppkt->icmp6_type));
318 static void ping(const char *host)
320 char packet[datalen + MAXIPLEN + MAXICMPLEN];
321 char buf[INET6_ADDRSTRLEN];
324 struct sockaddr_in6 from;
326 char control_buf[CMSG_SPACE(36)];
328 pingsock = create_icmp6_socket();
330 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
332 pingaddr.sin6_family = AF_INET6;
333 hostent = xgethostbyname2(host, AF_INET6);
334 if (hostent->h_addrtype != AF_INET6)
335 bb_error_msg_and_die("unknown address type; only AF_INET6 is currently supported.");
337 memcpy(&pingaddr.sin6_addr, hostent->h_addr, sizeof(pingaddr.sin6_addr));
341 struct icmp6_filter filt;
342 if (!(options & O_VERBOSE)) {
343 ICMP6_FILTER_SETBLOCKALL(&filt);
345 if ((options & F_FQDN) || (options & F_FQDNOLD) ||
346 (options & F_NODEADDR) || (options & F_SUPTYPES))
347 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt);
350 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
352 ICMP6_FILTER_SETPASSALL(&filt);
354 if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
356 bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
358 #endif /*ICMP6_FILTER*/
360 /* enable broadcast pings */
362 setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt,
365 /* set recv buf for broadcast pings */
367 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
370 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
371 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt,
375 setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, (char *) &sockopt,
379 if ((pingaddr.sin6_scope_id = if_nametoindex(ifname)) == 0)
380 bb_error_msg_and_die("%s: invalid interface name", ifname);
383 printf("PING %s (%s): %d data bytes\n",
385 inet_ntop(AF_INET6, (struct in_addr6 *) &pingaddr.sin6_addr,
389 signal(SIGINT, pingstats);
391 /* start the ping's going ... */
394 /* listen for replies */
396 msg.msg_namelen=sizeof(from);
399 msg.msg_control=control_buf;
401 iov.iov_len=sizeof(packet);
404 struct cmsghdr *cmsgptr = NULL;
406 msg.msg_controllen=sizeof(control_buf);
408 if ((c = recvmsg(pingsock, &msg, 0)) < 0) {
411 bb_perror_msg("recvfrom");
414 for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL;
415 cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
416 if (cmsgptr->cmsg_level == SOL_IPV6 &&
417 cmsgptr->cmsg_type == IPV6_HOPLIMIT ) {
418 hoplimit=*(int*)CMSG_DATA(cmsgptr);
421 unpack(packet, c, &from, hoplimit);
422 if (pingcount > 0 && nreceived >= pingcount)
428 int ping6_main(int argc, char **argv)
432 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
437 /* Parse any options */
438 while (argc >= 1 && **argv == '-') {
444 options |= O_VERBOSE;
447 options &= ~O_VERBOSE;
454 pingcount = atoi(*argv);
460 datalen = atoi(*argv);
477 myid = getpid() & 0xFFFF;
481 #endif /* ! CONFIG_FEATURE_FANCY_PING6 */
484 * Copyright (c) 1989 The Regents of the University of California.
485 * All rights reserved.
487 * This code is derived from software contributed to Berkeley by
490 * Redistribution and use in source and binary forms, with or without
491 * modification, are permitted provided that the following conditions
493 * 1. Redistributions of source code must retain the above copyright
494 * notice, this list of conditions and the following disclaimer.
495 * 2. Redistributions in binary form must reproduce the above copyright
496 * notice, this list of conditions and the following disclaimer in the
497 * documentation and/or other materials provided with the distribution.
499 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
500 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
502 * 4. Neither the name of the University nor the names of its contributors
503 * may be used to endorse or promote products derived from this software
504 * without specific prior written permission.
506 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
507 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
508 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
509 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
510 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
511 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
512 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
513 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
514 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
515 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF