blob: 3aba4906e2aee8eb3ce1d59c7ebcbea689ffe3a3 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen485b9551999-12-07 23:14:59 +00002/*
Eric Andersen485b9551999-12-07 23:14:59 +00003 * Mini ping implementation for busybox
4 *
5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
6 *
Rob Landley42eddba2005-12-15 08:04:17 +00007 * Adapted from the ping in netkit-base 0.10:
Eric Andersen485b9551999-12-07 23:14:59 +00008 * Copyright (c) 1989 The Regents of the University of California.
Denis Vlasenko35d4da02007-01-22 14:04:27 +00009 * All rights reserved.
10 *
11 * This code is derived from software contributed to Berkeley by
12 * Mike Muuss.
Eric Andersen485b9551999-12-07 23:14:59 +000013 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020014 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen485b9551999-12-07 23:14:59 +000015 */
Denis Vlasenkob9a279b2007-01-24 23:53:22 +000016/* from ping6.c:
17 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
18 *
19 * This version of ping is adapted from the ping in netkit-base 0.10,
20 * which is:
21 *
22 * Original copyright notice is retained at the end of this file.
23 *
24 * This version is an adaptation of ping.c from busybox.
25 * The code was modified by Bart Visscher <magick@linux-fan.com>
26 */
Eric Andersen485b9551999-12-07 23:14:59 +000027
Denis Vlasenkob9a279b2007-01-24 23:53:22 +000028#include <net/if.h>
Eric Andersen485b9551999-12-07 23:14:59 +000029#include <netinet/ip_icmp.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000030#include "libbb.h"
Denis Vlasenkoe9356022007-01-29 18:03:54 +000031
Denis Vlasenkob9a279b2007-01-24 23:53:22 +000032#if ENABLE_PING6
Denys Vlasenkoad7d94b2009-11-20 18:12:12 +010033# include <netinet/icmp6.h>
Denis Vlasenkob9a279b2007-01-24 23:53:22 +000034/* I see RENUMBERED constants in bits/in.h - !!?
35 * What a fuck is going on with libc? Is it a glibc joke? */
Denys Vlasenkoad7d94b2009-11-20 18:12:12 +010036# ifdef IPV6_2292HOPLIMIT
37# undef IPV6_HOPLIMIT
38# define IPV6_HOPLIMIT IPV6_2292HOPLIMIT
39# endif
Denis Vlasenkob9a279b2007-01-24 23:53:22 +000040#endif
Eric Andersen485b9551999-12-07 23:14:59 +000041
Rob Landleybc68cd12006-03-10 19:22:06 +000042enum {
43 DEFDATALEN = 56,
44 MAXIPLEN = 60,
45 MAXICMPLEN = 76,
Rob Landleybc68cd12006-03-10 19:22:06 +000046 MAX_DUP_CHK = (8 * 128),
47 MAXWAIT = 10,
Denis Vlasenkob9a279b2007-01-24 23:53:22 +000048 PINGINTERVAL = 1, /* 1 second */
Rob Landleybc68cd12006-03-10 19:22:06 +000049};
Eric Andersen485b9551999-12-07 23:14:59 +000050
Denys Vlasenko9341fd22010-03-03 01:10:29 +010051/* Common routines */
Denis Vlasenko4a5cf162006-11-20 00:48:22 +000052
Eric Andersen485b9551999-12-07 23:14:59 +000053static int in_cksum(unsigned short *buf, int sz)
54{
Erik Andersene49d5ec2000-02-08 19:58:47 +000055 int nleft = sz;
56 int sum = 0;
57 unsigned short *w = buf;
58 unsigned short ans = 0;
Eric Andersen485b9551999-12-07 23:14:59 +000059
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 while (nleft > 1) {
61 sum += *w++;
62 nleft -= 2;
63 }
Eric Andersen485b9551999-12-07 23:14:59 +000064
Erik Andersene49d5ec2000-02-08 19:58:47 +000065 if (nleft == 1) {
66 *(unsigned char *) (&ans) = *(unsigned char *) w;
67 sum += ans;
68 }
Eric Andersen485b9551999-12-07 23:14:59 +000069
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 sum = (sum >> 16) + (sum & 0xFFFF);
71 sum += (sum >> 16);
72 ans = ~sum;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000073 return ans;
Erik Andersene49d5ec2000-02-08 19:58:47 +000074}
Eric Andersen485b9551999-12-07 23:14:59 +000075
Denis Vlasenkob9a279b2007-01-24 23:53:22 +000076#if !ENABLE_FEATURE_FANCY_PING
Denis Vlasenko4a5cf162006-11-20 00:48:22 +000077
Denys Vlasenko9341fd22010-03-03 01:10:29 +010078/* Simple version */
Denis Vlasenko4a5cf162006-11-20 00:48:22 +000079
Denys Vlasenko9341fd22010-03-03 01:10:29 +010080struct globals {
81 char *hostname;
82 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
83} FIX_ALIASING;
84#define G (*(struct globals*)&bb_common_bufsiz1)
85#define INIT_G() do { } while (0)
Denis Vlasenkocb6874c2006-09-02 16:13:36 +000086
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000087static void noresp(int ign UNUSED_PARAM)
Eric Andersenb5474c42002-03-20 11:59:28 +000088{
Denys Vlasenko9341fd22010-03-03 01:10:29 +010089 printf("No response from %s\n", G.hostname);
Eric Andersen4e486a52003-01-12 06:08:33 +000090 exit(EXIT_FAILURE);
Eric Andersenb5474c42002-03-20 11:59:28 +000091}
Eric Andersen19db07b1999-12-11 08:41:28 +000092
Denis Vlasenkoe9356022007-01-29 18:03:54 +000093static void ping4(len_and_sockaddr *lsa)
Eric Andersen19db07b1999-12-11 08:41:28 +000094{
Erik Andersene49d5ec2000-02-08 19:58:47 +000095 struct icmp *pkt;
96 int pingsock, c;
Eric Andersen19db07b1999-12-11 08:41:28 +000097
Matt Kraai06ef1652001-07-13 20:56:27 +000098 pingsock = create_icmp_socket();
Erik Andersene49d5ec2000-02-08 19:58:47 +000099
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100100 pkt = (struct icmp *) G.packet;
101 memset(pkt, 0, sizeof(G.packet));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 pkt->icmp_type = ICMP_ECHO;
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100103 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(G.packet));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000104
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100105 xsendto(pingsock, G.packet, DEFDATALEN + ICMP_MINLEN, &lsa->u.sa, lsa->len);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106
Erik Andersene49d5ec2000-02-08 19:58:47 +0000107 /* listen for replies */
108 while (1) {
109 struct sockaddr_in from;
Mike Frysinger03e827a2005-07-26 23:00:59 +0000110 socklen_t fromlen = sizeof(from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100112 c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0,
Denis Vlasenko806116b2006-12-31 12:14:16 +0000113 (struct sockaddr *) &from, &fromlen);
114 if (c < 0) {
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000115 if (errno != EINTR)
116 bb_perror_msg("recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000117 continue;
118 }
119 if (c >= 76) { /* ip + icmp */
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100120 struct iphdr *iphdr = (struct iphdr *) G.packet;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100122 pkt = (struct icmp *) (G.packet + (iphdr->ihl << 2)); /* skip ip hdr */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123 if (pkt->icmp_type == ICMP_ECHOREPLY)
124 break;
125 }
126 }
Denis Vlasenko9adc6ce2007-01-22 22:45:27 +0000127 if (ENABLE_FEATURE_CLEAN_UP)
128 close(pingsock);
Eric Andersen19db07b1999-12-11 08:41:28 +0000129}
130
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000131#if ENABLE_PING6
132static void ping6(len_and_sockaddr *lsa)
133{
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000134 struct icmp6_hdr *pkt;
135 int pingsock, c;
136 int sockopt;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000137
138 pingsock = create_icmp6_socket();
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000139
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100140 pkt = (struct icmp6_hdr *) G.packet;
141 memset(pkt, 0, sizeof(G.packet));
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000142 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
143
144 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
145 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
146
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100147 xsendto(pingsock, G.packet, DEFDATALEN + sizeof(struct icmp6_hdr), &lsa->u.sa, lsa->len);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000148
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000149 /* listen for replies */
150 while (1) {
151 struct sockaddr_in6 from;
152 socklen_t fromlen = sizeof(from);
153
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100154 c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0,
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000155 (struct sockaddr *) &from, &fromlen);
156 if (c < 0) {
157 if (errno != EINTR)
158 bb_perror_msg("recvfrom");
159 continue;
160 }
Bernhard Reutner-Fischerdfd38a42010-01-23 12:52:40 +0100161 if (c >= ICMP_MINLEN) { /* icmp6_hdr */
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100162 pkt = (struct icmp6_hdr *) G.packet;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000163 if (pkt->icmp6_type == ICMP6_ECHO_REPLY)
164 break;
165 }
166 }
167 if (ENABLE_FEATURE_CLEAN_UP)
168 close(pingsock);
169}
170#endif
171
Denys Vlasenkoad7d94b2009-11-20 18:12:12 +0100172#if !ENABLE_PING6
173# define common_ping_main(af, argv) common_ping_main(argv)
174#endif
175static int common_ping_main(sa_family_t af, char **argv)
Eric Andersen19db07b1999-12-11 08:41:28 +0000176{
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000177 len_and_sockaddr *lsa;
Denis Vlasenko3483e852007-07-02 15:47:52 +0000178
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100179 INIT_G();
180
Denys Vlasenkoad7d94b2009-11-20 18:12:12 +0100181#if ENABLE_PING6
Denis Vlasenko3483e852007-07-02 15:47:52 +0000182 while ((++argv)[0] && argv[0][0] == '-') {
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000183 if (argv[0][1] == '4') {
184 af = AF_INET;
185 continue;
186 }
187 if (argv[0][1] == '6') {
188 af = AF_INET6;
189 continue;
190 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000191 bb_show_usage();
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000192 }
193#else
194 argv++;
195#endif
196
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100197 G.hostname = *argv;
198 if (!G.hostname)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000199 bb_show_usage();
200
201#if ENABLE_PING6
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100202 lsa = xhost_and_af2sockaddr(G.hostname, 0, af);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000203#else
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100204 lsa = xhost_and_af2sockaddr(G.hostname, 0, AF_INET);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000205#endif
206 /* Set timer _after_ DNS resolution */
207 signal(SIGALRM, noresp);
208 alarm(5); /* give the host 5000ms to respond */
209
210#if ENABLE_PING6
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000211 if (lsa->u.sa.sa_family == AF_INET6)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000212 ping6(lsa);
213 else
214#endif
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000215 ping4(lsa);
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100216 printf("%s is alive!\n", G.hostname);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000217 return EXIT_SUCCESS;
Eric Andersen19db07b1999-12-11 08:41:28 +0000218}
219
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000220
221#else /* FEATURE_FANCY_PING */
222
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000223
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100224/* Full(er) version */
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000225
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000226#define OPT_STRING ("qvc:s:w:W:I:4" IF_PING6("6"))
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000227enum {
228 OPT_QUIET = 1 << 0,
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000229 OPT_VERBOSE = 1 << 1,
230 OPT_c = 1 << 2,
231 OPT_s = 1 << 3,
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000232 OPT_w = 1 << 4,
233 OPT_W = 1 << 5,
234 OPT_I = 1 << 6,
235 OPT_IPV4 = 1 << 7,
236 OPT_IPV6 = (1 << 8) * ENABLE_PING6,
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000237};
238
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000239
Denis Vlasenko821cc252007-06-04 10:33:48 +0000240struct globals {
241 int pingsock;
Denis Vlasenko1a7afb42007-10-19 21:39:25 +0000242 int if_index;
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000243 char *str_I;
Denis Vlasenko821cc252007-06-04 10:33:48 +0000244 len_and_sockaddr *source_lsa;
245 unsigned datalen;
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000246 unsigned pingcount; /* must be int-sized */
247 unsigned long ntransmitted, nreceived, nrepeats;
Denis Vlasenko821cc252007-06-04 10:33:48 +0000248 uint16_t myid;
Denis Vlasenko76791452007-06-18 08:55:57 +0000249 unsigned tmin, tmax; /* in us */
250 unsigned long long tsum; /* in us, sum of all times */
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000251 unsigned deadline;
252 unsigned timeout;
253 unsigned total_secs;
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100254 unsigned sizeof_rcv_packet;
255 char *rcv_packet; /* [datalen + MAXIPLEN + MAXICMPLEN] */
256 void *snd_packet; /* [datalen + ipv4/ipv6_const] */
Denis Vlasenko821cc252007-06-04 10:33:48 +0000257 const char *hostname;
258 const char *dotted;
259 union {
260 struct sockaddr sa;
261 struct sockaddr_in sin;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000262#if ENABLE_PING6
Denis Vlasenko821cc252007-06-04 10:33:48 +0000263 struct sockaddr_in6 sin6;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000264#endif
Denis Vlasenko821cc252007-06-04 10:33:48 +0000265 } pingaddr;
266 char rcvd_tbl[MAX_DUP_CHK / 8];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100267} FIX_ALIASING;
Denis Vlasenko821cc252007-06-04 10:33:48 +0000268#define G (*(struct globals*)&bb_common_bufsiz1)
269#define pingsock (G.pingsock )
Denis Vlasenko821cc252007-06-04 10:33:48 +0000270#define if_index (G.if_index )
Denis Vlasenko1a7afb42007-10-19 21:39:25 +0000271#define source_lsa (G.source_lsa )
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000272#define str_I (G.str_I )
Denis Vlasenko1a7afb42007-10-19 21:39:25 +0000273#define datalen (G.datalen )
Denis Vlasenko821cc252007-06-04 10:33:48 +0000274#define ntransmitted (G.ntransmitted)
275#define nreceived (G.nreceived )
276#define nrepeats (G.nrepeats )
277#define pingcount (G.pingcount )
278#define myid (G.myid )
279#define tmin (G.tmin )
280#define tmax (G.tmax )
281#define tsum (G.tsum )
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000282#define deadline (G.deadline )
283#define timeout (G.timeout )
284#define total_secs (G.total_secs )
Denis Vlasenko821cc252007-06-04 10:33:48 +0000285#define hostname (G.hostname )
286#define dotted (G.dotted )
287#define pingaddr (G.pingaddr )
288#define rcvd_tbl (G.rcvd_tbl )
289void BUG_ping_globals_too_big(void);
290#define INIT_G() do { \
Denis Vlasenko6b404432008-01-07 16:13:14 +0000291 if (sizeof(G) > COMMON_BUFSIZE) \
292 BUG_ping_globals_too_big(); \
Denis Vlasenko821cc252007-06-04 10:33:48 +0000293 pingsock = -1; \
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000294 datalen = DEFDATALEN; \
295 timeout = MAXWAIT; \
Denis Vlasenko821cc252007-06-04 10:33:48 +0000296 tmin = UINT_MAX; \
297} while (0)
Eric Andersen19db07b1999-12-11 08:41:28 +0000298
Eric Andersen19db07b1999-12-11 08:41:28 +0000299
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200300#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
301#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
302#define SET(bit) (A(bit) |= B(bit))
303#define CLR(bit) (A(bit) &= (~B(bit)))
304#define TST(bit) (A(bit) & B(bit))
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000305
Eric Andersen19db07b1999-12-11 08:41:28 +0000306/**************************************************************************/
307
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000308static void print_stats_and_exit(int junk) NORETURN;
309static void print_stats_and_exit(int junk UNUSED_PARAM)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000310{
311 signal(SIGINT, SIG_IGN);
312
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000313 printf("\n--- %s ping statistics ---\n", hostname);
Denis Vlasenko13858992006-10-08 12:49:22 +0000314 printf("%lu packets transmitted, ", ntransmitted);
315 printf("%lu packets received, ", nreceived);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000316 if (nrepeats)
Denis Vlasenko13858992006-10-08 12:49:22 +0000317 printf("%lu duplicates, ", nrepeats);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000318 if (ntransmitted)
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000319 ntransmitted = (ntransmitted - nreceived) * 100 / ntransmitted;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000320 printf("%lu%% packet loss\n", ntransmitted);
Denis Vlasenko76791452007-06-18 08:55:57 +0000321 if (tmin != UINT_MAX) {
322 unsigned tavg = tsum / (nreceived + nrepeats);
323 printf("round-trip min/avg/max = %u.%03u/%u.%03u/%u.%03u ms\n",
324 tmin / 1000, tmin % 1000,
325 tavg / 1000, tavg % 1000,
326 tmax / 1000, tmax % 1000);
327 }
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000328 /* if condition is true, exit with 1 -- 'failure' */
329 exit(nreceived == 0 || (deadline && nreceived < pingcount));
Eric Andersen485b9551999-12-07 23:14:59 +0000330}
331
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000332static void sendping_tail(void (*sp)(int), const void *pkt, int size_pkt)
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000333{
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000334 int sz;
335
336 CLR((uint16_t)ntransmitted % MAX_DUP_CHK);
337 ntransmitted++;
338
339 /* sizeof(pingaddr) can be larger than real sa size, but I think
340 * it doesn't matter */
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000341 sz = xsendto(pingsock, pkt, size_pkt, &pingaddr.sa, sizeof(pingaddr));
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000342 if (sz != size_pkt)
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000343 bb_error_msg_and_die(bb_msg_write_error);
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000344
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000345 if (pingcount == 0 || deadline || ntransmitted < pingcount) {
346 /* Didn't send all pings yet - schedule next in 1s */
347 signal(SIGALRM, sp);
348 if (deadline) {
349 total_secs += PINGINTERVAL;
350 if (total_secs >= deadline)
351 signal(SIGALRM, print_stats_and_exit);
352 }
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000353 alarm(PINGINTERVAL);
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000354 } else { /* -c NN, and all NN are sent (and no deadline) */
355 /* Wait for the last ping to come back.
356 * -W timeout: wait for a response in seconds.
357 * Affects only timeout in absense of any responses,
358 * otherwise ping waits for two RTTs. */
359 unsigned expire = timeout;
360
361 if (nreceived) {
362 /* approx. 2*tmax, in seconds (2 RTT) */
363 expire = tmax / (512*1024);
364 if (expire == 0)
365 expire = 1;
366 }
367 signal(SIGALRM, print_stats_and_exit);
368 alarm(expire);
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000369 }
370}
371
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000372static void sendping4(int junk UNUSED_PARAM)
Eric Andersen485b9551999-12-07 23:14:59 +0000373{
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100374 struct icmp *pkt = G.snd_packet;
Eric Andersen485b9551999-12-07 23:14:59 +0000375
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100376 //memset(pkt, 0, datalen + ICMP_MINLEN + 4); - G.snd_packet was xzalloced
Erik Andersene49d5ec2000-02-08 19:58:47 +0000377 pkt->icmp_type = ICMP_ECHO;
Denis Vlasenkob34266b2008-04-29 12:31:53 +0000378 /*pkt->icmp_code = 0;*/
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100379 pkt->icmp_cksum = 0; /* cksum is calculated with this field set to 0 */
Denis Vlasenko919c10d2007-01-03 22:14:18 +0000380 pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000381 pkt->icmp_id = myid;
Denis Vlasenko7b72fc12007-06-16 13:37:59 +0000382
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100383 /* If datalen < 4, we store timestamp _past_ the packet,
384 * but it's ok - we allocated 4 extra bytes in xzalloc() just in case.
385 */
Denis Vlasenko76791452007-06-18 08:55:57 +0000386 /*if (datalen >= 4)*/
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100387 /* No hton: we'll read it back on the same machine */
Denis Vlasenko76791452007-06-18 08:55:57 +0000388 *(uint32_t*)&pkt->icmp_dun = monotonic_us();
Denis Vlasenko7b72fc12007-06-16 13:37:59 +0000389
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000390 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, datalen + ICMP_MINLEN);
Eric Andersen485b9551999-12-07 23:14:59 +0000391
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000392 sendping_tail(sendping4, pkt, datalen + ICMP_MINLEN);
Eric Andersen485b9551999-12-07 23:14:59 +0000393}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000394#if ENABLE_PING6
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000395static void sendping6(int junk UNUSED_PARAM)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000396{
Denis Vlasenko76791452007-06-18 08:55:57 +0000397 struct icmp6_hdr *pkt = alloca(datalen + sizeof(struct icmp6_hdr) + 4);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000398
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100399 //memset(pkt, 0, datalen + sizeof(struct icmp6_hdr) + 4);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000400 pkt->icmp6_type = ICMP6_ECHO_REQUEST;
Denis Vlasenkob34266b2008-04-29 12:31:53 +0000401 /*pkt->icmp6_code = 0;*/
402 /*pkt->icmp6_cksum = 0;*/
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000403 pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
404 pkt->icmp6_id = myid;
Denis Vlasenko7b72fc12007-06-16 13:37:59 +0000405
Denis Vlasenko76791452007-06-18 08:55:57 +0000406 /*if (datalen >= 4)*/
407 *(uint32_t*)(&pkt->icmp6_data8[4]) = monotonic_us();
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000408
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100409 //TODO? pkt->icmp_cksum = in_cksum(...);
410
Denis Vlasenkoc8e99932007-02-09 18:14:42 +0000411 sendping_tail(sendping6, pkt, datalen + sizeof(struct icmp6_hdr));
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000412}
413#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000414
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000415static const char *icmp_type_name(int id)
Erik Andersen227a59b2000-04-25 23:24:55 +0000416{
417 switch (id) {
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000418 case ICMP_ECHOREPLY: return "Echo Reply";
419 case ICMP_DEST_UNREACH: return "Destination Unreachable";
420 case ICMP_SOURCE_QUENCH: return "Source Quench";
421 case ICMP_REDIRECT: return "Redirect (change route)";
422 case ICMP_ECHO: return "Echo Request";
423 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
424 case ICMP_PARAMETERPROB: return "Parameter Problem";
425 case ICMP_TIMESTAMP: return "Timestamp Request";
426 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
427 case ICMP_INFO_REQUEST: return "Information Request";
428 case ICMP_INFO_REPLY: return "Information Reply";
429 case ICMP_ADDRESS: return "Address Mask Request";
430 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
431 default: return "unknown ICMP type";
Erik Andersen227a59b2000-04-25 23:24:55 +0000432 }
433}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000434#if ENABLE_PING6
435/* RFC3542 changed some definitions from RFC2292 for no good reason, whee!
436 * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */
437#ifndef MLD_LISTENER_QUERY
438# define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY
439#endif
440#ifndef MLD_LISTENER_REPORT
441# define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT
442#endif
443#ifndef MLD_LISTENER_REDUCTION
444# define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION
445#endif
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000446static const char *icmp6_type_name(int id)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000447{
448 switch (id) {
449 case ICMP6_DST_UNREACH: return "Destination Unreachable";
450 case ICMP6_PACKET_TOO_BIG: return "Packet too big";
451 case ICMP6_TIME_EXCEEDED: return "Time Exceeded";
452 case ICMP6_PARAM_PROB: return "Parameter Problem";
453 case ICMP6_ECHO_REPLY: return "Echo Reply";
454 case ICMP6_ECHO_REQUEST: return "Echo Request";
455 case MLD_LISTENER_QUERY: return "Listener Query";
456 case MLD_LISTENER_REPORT: return "Listener Report";
457 case MLD_LISTENER_REDUCTION: return "Listener Reduction";
458 default: return "unknown ICMP type";
459 }
460}
461#endif
Erik Andersen227a59b2000-04-25 23:24:55 +0000462
Denis Vlasenko76791452007-06-18 08:55:57 +0000463static void unpack_tail(int sz, uint32_t *tp,
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000464 const char *from_str,
465 uint16_t recv_seq, int ttl)
466{
467 const char *dupmsg = " (DUP!)";
468 unsigned triptime = triptime; /* for gcc */
469
470 ++nreceived;
471
472 if (tp) {
Denis Vlasenko76791452007-06-18 08:55:57 +0000473 /* (int32_t) cast is for hypothetical 64-bit unsigned */
474 /* (doesn't hurt 32-bit real-world anyway) */
475 triptime = (int32_t) ((uint32_t)monotonic_us() - *tp);
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000476 tsum += triptime;
477 if (triptime < tmin)
478 tmin = triptime;
479 if (triptime > tmax)
480 tmax = triptime;
481 }
482
483 if (TST(recv_seq % MAX_DUP_CHK)) {
484 ++nrepeats;
485 --nreceived;
486 } else {
487 SET(recv_seq % MAX_DUP_CHK);
488 dupmsg += 7;
489 }
490
491 if (option_mask32 & OPT_QUIET)
492 return;
493
494 printf("%d bytes from %s: seq=%u ttl=%d", sz,
495 from_str, recv_seq, ttl);
496 if (tp)
Denis Vlasenko76791452007-06-18 08:55:57 +0000497 printf(" time=%u.%03u ms", triptime / 1000, triptime % 1000);
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000498 puts(dupmsg);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100499 fflush_all();
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000500}
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000501static void unpack4(char *buf, int sz, struct sockaddr_in *from)
Eric Andersen485b9551999-12-07 23:14:59 +0000502{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000503 struct icmp *icmppkt;
504 struct iphdr *iphdr;
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000505 int hlen;
Eric Andersen485b9551999-12-07 23:14:59 +0000506
Erik Andersene49d5ec2000-02-08 19:58:47 +0000507 /* discard if too short */
Pavel Roskin0024abc2000-06-07 20:38:15 +0000508 if (sz < (datalen + ICMP_MINLEN))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000509 return;
Eric Andersen485b9551999-12-07 23:14:59 +0000510
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000511 /* check IP header */
512 iphdr = (struct iphdr *) buf;
513 hlen = iphdr->ihl << 2;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000514 sz -= hlen;
515 icmppkt = (struct icmp *) (buf + hlen);
Erik Andersen227a59b2000-04-25 23:24:55 +0000516 if (icmppkt->icmp_id != myid)
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000517 return; /* not our ping */
Erik Andersen227a59b2000-04-25 23:24:55 +0000518
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000520 uint16_t recv_seq = ntohs(icmppkt->icmp_seq);
Denis Vlasenko76791452007-06-18 08:55:57 +0000521 uint32_t *tp = NULL;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000522
Denis Vlasenko76791452007-06-18 08:55:57 +0000523 if (sz >= ICMP_MINLEN + sizeof(uint32_t))
524 tp = (uint32_t *) icmppkt->icmp_data;
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000525 unpack_tail(sz, tp,
526 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
527 recv_seq, iphdr->ttl);
528 } else if (icmppkt->icmp_type != ICMP_ECHO) {
529 bb_error_msg("warning: got ICMP %d (%s)",
530 icmppkt->icmp_type,
531 icmp_type_name(icmppkt->icmp_type));
Denis Vlasenko35d4da02007-01-22 14:04:27 +0000532 }
Eric Andersen485b9551999-12-07 23:14:59 +0000533}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000534#if ENABLE_PING6
Denis Vlasenko68404f12008-03-17 09:00:54 +0000535static void unpack6(char *packet, int sz, /*struct sockaddr_in6 *from,*/ int hoplimit)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000536{
537 struct icmp6_hdr *icmppkt;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000538 char buf[INET6_ADDRSTRLEN];
Eric Andersen485b9551999-12-07 23:14:59 +0000539
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000540 /* discard if too short */
541 if (sz < (datalen + sizeof(struct icmp6_hdr)))
542 return;
543
544 icmppkt = (struct icmp6_hdr *) packet;
545 if (icmppkt->icmp6_id != myid)
546 return; /* not our ping */
547
548 if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) {
549 uint16_t recv_seq = ntohs(icmppkt->icmp6_seq);
Denis Vlasenko76791452007-06-18 08:55:57 +0000550 uint32_t *tp = NULL;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000551
Denis Vlasenko76791452007-06-18 08:55:57 +0000552 if (sz >= sizeof(struct icmp6_hdr) + sizeof(uint32_t))
553 tp = (uint32_t *) &icmppkt->icmp6_data8[4];
Denis Vlasenko19c238b2007-03-03 00:36:35 +0000554 unpack_tail(sz, tp,
555 inet_ntop(AF_INET6, &pingaddr.sin6.sin6_addr,
556 buf, sizeof(buf)),
557 recv_seq, hoplimit);
558 } else if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST) {
559 bb_error_msg("warning: got ICMP %d (%s)",
560 icmppkt->icmp6_type,
561 icmp6_type_name(icmppkt->icmp6_type));
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000562 }
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000563}
564#endif
565
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000566static void ping4(len_and_sockaddr *lsa)
Eric Andersen485b9551999-12-07 23:14:59 +0000567{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000568 int sockopt;
569
Matt Kraai06ef1652001-07-13 20:56:27 +0000570 pingsock = create_icmp_socket();
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000571 pingaddr.sin = lsa->u.sin;
Denis Vlasenko5ad10482007-06-19 22:54:21 +0000572 if (source_lsa) {
573 if (setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_IF,
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000574 &source_lsa->u.sa, source_lsa->len))
Denis Vlasenko5ad10482007-06-19 22:54:21 +0000575 bb_error_msg_and_die("can't set multicast source interface");
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000576 xbind(pingsock, &source_lsa->u.sa, source_lsa->len);
Denis Vlasenko5ad10482007-06-19 22:54:21 +0000577 }
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000578 if (str_I)
Denis Vlasenkoe5373852008-12-10 11:12:16 +0000579 setsockopt_bindtodevice(pingsock, str_I);
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000580
Erik Andersene49d5ec2000-02-08 19:58:47 +0000581 /* enable broadcast pings */
Denis Vlasenko48237b02006-11-22 23:22:06 +0000582 setsockopt_broadcast(pingsock);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000583
Denis Vlasenko57707152008-08-24 00:02:18 +0000584 /* set recv buf (needed if we can get lots of responses: flood ping,
585 * broadcast ping etc) */
586 sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */
Denis Vlasenko703e2022007-01-22 14:12:08 +0000587 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000588
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000589 signal(SIGINT, print_stats_and_exit);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000590
591 /* start the ping's going ... */
Denis Vlasenkoe9356022007-01-29 18:03:54 +0000592 sendping4(0);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000593
594 /* listen for replies */
595 while (1) {
596 struct sockaddr_in from;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000597 socklen_t fromlen = (socklen_t) sizeof(from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000598 int c;
599
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100600 c = recvfrom(pingsock, G.rcv_packet, G.sizeof_rcv_packet, 0,
Denis Vlasenko44c2eb22007-01-08 23:55:33 +0000601 (struct sockaddr *) &from, &fromlen);
602 if (c < 0) {
603 if (errno != EINTR)
604 bb_perror_msg("recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000605 continue;
606 }
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100607 unpack4(G.rcv_packet, c, &from);
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000608 if (pingcount && nreceived >= pingcount)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000609 break;
610 }
Eric Andersen485b9551999-12-07 23:14:59 +0000611}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000612#if ENABLE_PING6
613extern int BUG_bad_offsetof_icmp6_cksum(void);
614static void ping6(len_and_sockaddr *lsa)
615{
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000616 int sockopt;
617 struct msghdr msg;
618 struct sockaddr_in6 from;
619 struct iovec iov;
620 char control_buf[CMSG_SPACE(36)];
621
622 pingsock = create_icmp6_socket();
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000623 pingaddr.sin6 = lsa->u.sin6;
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000624 /* untested whether "-I addr" really works for IPv6: */
625 if (source_lsa)
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000626 xbind(pingsock, &source_lsa->u.sa, source_lsa->len);
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000627 if (str_I)
Denis Vlasenkoe5373852008-12-10 11:12:16 +0000628 setsockopt_bindtodevice(pingsock, str_I);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000629
630#ifdef ICMP6_FILTER
631 {
632 struct icmp6_filter filt;
633 if (!(option_mask32 & OPT_VERBOSE)) {
634 ICMP6_FILTER_SETBLOCKALL(&filt);
635 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt);
636 } else {
637 ICMP6_FILTER_SETPASSALL(&filt);
638 }
639 if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
640 sizeof(filt)) < 0)
641 bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
642 }
643#endif /*ICMP6_FILTER*/
644
645 /* enable broadcast pings */
646 setsockopt_broadcast(pingsock);
647
Denis Vlasenko57707152008-08-24 00:02:18 +0000648 /* set recv buf (needed if we can get lots of responses: flood ping,
649 * broadcast ping etc) */
650 sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000651 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt));
652
653 sockopt = offsetof(struct icmp6_hdr, icmp6_cksum);
654 if (offsetof(struct icmp6_hdr, icmp6_cksum) != 2)
655 BUG_bad_offsetof_icmp6_cksum();
656 setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt));
657
658 /* request ttl info to be returned in ancillary data */
659 setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, &const_int_1, sizeof(const_int_1));
660
661 if (if_index)
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000662 pingaddr.sin6.sin6_scope_id = if_index;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000663
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000664 signal(SIGINT, print_stats_and_exit);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000665
666 /* start the ping's going ... */
667 sendping6(0);
668
669 /* listen for replies */
670 msg.msg_name = &from;
671 msg.msg_namelen = sizeof(from);
672 msg.msg_iov = &iov;
673 msg.msg_iovlen = 1;
674 msg.msg_control = control_buf;
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100675 iov.iov_base = G.rcv_packet;
676 iov.iov_len = G.sizeof_rcv_packet;
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000677 while (1) {
678 int c;
679 struct cmsghdr *mp;
680 int hoplimit = -1;
681 msg.msg_controllen = sizeof(control_buf);
682
683 c = recvmsg(pingsock, &msg, 0);
684 if (c < 0) {
685 if (errno != EINTR)
686 bb_perror_msg("recvfrom");
687 continue;
688 }
689 for (mp = CMSG_FIRSTHDR(&msg); mp; mp = CMSG_NXTHDR(&msg, mp)) {
690 if (mp->cmsg_level == SOL_IPV6
691 && mp->cmsg_type == IPV6_HOPLIMIT
692 /* don't check len - we trust the kernel: */
693 /* && mp->cmsg_len >= CMSG_LEN(sizeof(int)) */
694 ) {
Denys Vlasenko57be1ee2009-11-26 15:26:31 +0100695 /*hoplimit = *(int*)CMSG_DATA(mp); - unaligned access */
696 move_from_unaligned_int(hoplimit, CMSG_DATA(mp));
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000697 }
698 }
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100699 unpack6(G.rcv_packet, c, /*&from,*/ hoplimit);
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000700 if (pingcount && nreceived >= pingcount)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000701 break;
702 }
703}
704#endif
Eric Andersen485b9551999-12-07 23:14:59 +0000705
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000706static void ping(len_and_sockaddr *lsa)
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000707{
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000708 printf("PING %s (%s)", hostname, dotted);
709 if (source_lsa) {
710 printf(" from %s",
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000711 xmalloc_sockaddr2dotted_noport(&source_lsa->u.sa));
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000712 }
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000713 printf(": %d data bytes\n", datalen);
714
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100715 G.sizeof_rcv_packet = datalen + MAXIPLEN + MAXICMPLEN;
716 G.rcv_packet = xzalloc(G.sizeof_rcv_packet);
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000717#if ENABLE_PING6
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100718 if (lsa->u.sa.sa_family == AF_INET6) {
719 /* +4 reserves a place for timestamp, which may end up sitting
720 * _after_ packet. Saves one if() - see sendping4/6() */
721 G.snd_packet = xzalloc(datalen + sizeof(struct icmp6_hdr) + 4);
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000722 ping6(lsa);
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100723 } else
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000724#endif
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100725 {
726 G.snd_packet = xzalloc(datalen + ICMP_MINLEN + 4);
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000727 ping4(lsa);
Denys Vlasenko9341fd22010-03-03 01:10:29 +0100728 }
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000729}
730
Denys Vlasenkoad7d94b2009-11-20 18:12:12 +0100731static int common_ping_main(int opt, char **argv)
Eric Andersen485b9551999-12-07 23:14:59 +0000732{
Denis Vlasenkoaeb4bdd2007-01-25 00:00:02 +0000733 len_and_sockaddr *lsa;
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000734 char *str_s;
Eric Andersen485b9551999-12-07 23:14:59 +0000735
Denis Vlasenko821cc252007-06-04 10:33:48 +0000736 INIT_G();
737
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000738 /* exactly one argument needed; -v and -q don't mix; -c NUM, -w NUM, -W NUM */
739 opt_complementary = "=1:q--v:v--q:c+:w+:W+";
Denys Vlasenko85164fa2009-11-21 15:44:56 +0100740 opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &deadline, &timeout, &str_I);
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000741 if (opt & OPT_s)
742 datalen = xatou16(str_s); // -s
743 if (opt & OPT_I) { // -I
744 if_index = if_nametoindex(str_I);
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000745 if (!if_index) {
746 /* TODO: I'm not sure it takes IPv6 unless in [XX:XX..] format */
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000747 source_lsa = xdotted2sockaddr(str_I, 0);
748 str_I = NULL; /* don't try to bind to device later */
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000749 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000750 }
Denis Vlasenko1c9ad622007-05-27 00:53:41 +0000751 myid = (uint16_t) getpid();
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000752 hostname = argv[optind];
753#if ENABLE_PING6
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000754 {
755 sa_family_t af = AF_UNSPEC;
756 if (opt & OPT_IPV4)
757 af = AF_INET;
758 if (opt & OPT_IPV6)
759 af = AF_INET6;
760 lsa = xhost_and_af2sockaddr(hostname, 0, af);
761 }
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000762#else
Denis Vlasenko42823d52007-02-04 02:39:08 +0000763 lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET);
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000764#endif
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000765
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000766 if (source_lsa && source_lsa->u.sa.sa_family != lsa->u.sa.sa_family)
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000767 /* leaking it here... */
768 source_lsa = NULL;
769
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000770 dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa);
Denis Vlasenko9ca26d32007-02-09 17:32:16 +0000771 ping(lsa);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000772 print_stats_and_exit(EXIT_SUCCESS);
Denis Vlasenko90c31b32008-04-07 00:46:29 +0000773 /*return EXIT_SUCCESS;*/
Eric Andersen485b9551999-12-07 23:14:59 +0000774}
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000775#endif /* FEATURE_FANCY_PING */
776
777
Denys Vlasenkoad7d94b2009-11-20 18:12:12 +0100778int ping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
779int ping_main(int argc UNUSED_PARAM, char **argv)
780{
781#if !ENABLE_FEATURE_FANCY_PING
782 return common_ping_main(AF_UNSPEC, argv);
783#else
784 return common_ping_main(0, argv);
785#endif
786}
787
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000788#if ENABLE_PING6
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000789int ping6_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko59f502b2008-10-27 11:54:45 +0000790int ping6_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000791{
Denys Vlasenkoad7d94b2009-11-20 18:12:12 +0100792# if !ENABLE_FEATURE_FANCY_PING
793 return common_ping_main(AF_INET6, argv);
794# else
795 return common_ping_main(OPT_IPV6, argv);
796# endif
Denis Vlasenkob9a279b2007-01-24 23:53:22 +0000797}
798#endif
799
800/* from ping6.c:
801 * Copyright (c) 1989 The Regents of the University of California.
802 * All rights reserved.
803 *
804 * This code is derived from software contributed to Berkeley by
805 * Mike Muuss.
806 *
807 * Redistribution and use in source and binary forms, with or without
808 * modification, are permitted provided that the following conditions
809 * are met:
810 * 1. Redistributions of source code must retain the above copyright
811 * notice, this list of conditions and the following disclaimer.
812 * 2. Redistributions in binary form must reproduce the above copyright
813 * notice, this list of conditions and the following disclaimer in the
814 * documentation and/or other materials provided with the distribution.
815 *
816 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
817 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
818 *
819 * 4. Neither the name of the University nor the names of its contributors
820 * may be used to endorse or promote products derived from this software
821 * without specific prior written permission.
822 *
823 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
824 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
825 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
826 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
827 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
828 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
829 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
830 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
831 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
832 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
833 * SUCH DAMAGE.
834 */