2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (C) 2019 Jan Sucan <jansucan@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
30 #include <sys/socket.h>
32 #include <arpa/inet.h>
34 #include <netinet/in.h>
54 #if defined(INET) && defined(INET6)
55 #define OPTSTR PING6OPTS PING4OPTS
57 #define OPTSTR PING4OPTS
59 #define OPTSTR PING6OPTS
61 #error At least one of INET and INET6 is required
70 long nreceived; /* # of packets we got back */
71 long nrepeats; /* number of duplicates */
72 long ntransmitted; /* sequence # for outbound packets = #sent */
73 long nrcvtimeout = 0; /* # of packets we got back after waittime */
75 /* nonzero if we've been told to finish up */
76 volatile sig_atomic_t seenint;
77 volatile sig_atomic_t seeninfo;
80 int timing; /* flag to do timing */
81 double tmin = 999999999.0; /* minimum round trip time */
82 double tmax = 0.0; /* maximum round trip time */
83 double tsum = 0.0; /* sum of all times, for doing average */
84 double tsumsq = 0.0; /* sum of all times squared, for std. dev. */
87 main(int argc, char *argv[])
95 #if defined(INET) && defined(INET6)
96 struct addrinfo hints, *res, *ai;
103 if (strcmp(getprogname(), "ping6") == 0)
104 return ping6(argc, argv);
107 while ((opt = getopt(argc, argv, ":" OPTSTR)) != -1) {
119 * If -S is given with a numeric parameter,
120 * force use of the corresponding version.
123 if (inet_pton(AF_INET, optarg, &a) == 1)
127 if (inet_pton(AF_INET6, optarg, &a6) == 1)
137 * For IPv4, only one positional argument, the target, is allowed.
138 * For IPv6, multiple positional argument are allowed; the last
139 * one is the target, and preceding ones are intermediate hops.
140 * This nuance is lost here, but the only case where it matters is
146 #if defined(INET) && defined(INET6)
147 target = argv[argc - 1];
148 memset(&hints, 0, sizeof(hints));
149 hints.ai_socktype = SOCK_RAW;
150 if (feature_present("inet") && !feature_present("inet6"))
151 hints.ai_family = AF_INET;
152 else if (feature_present("inet6") && !feature_present("inet"))
153 hints.ai_family = AF_INET6;
155 hints.ai_family = AF_UNSPEC;
156 error = getaddrinfo(target, NULL, &hints, &res);
158 errx(EX_NOHOST, "cannot resolve %s: %s",
159 target, gai_strerror(error));
160 for (ai = res; ai != NULL; ai = ai->ai_next) {
161 if (ai->ai_family == AF_INET) {
165 if (ai->ai_family == AF_INET6) {
171 errx(EX_NOHOST, "cannot resolve %s", target);
177 return ping(argc, argv);
183 return ping6(argc, argv);
189 * Set the global bit that causes the main loop to quit.
198 * When doing reverse DNS lookups, the seenint flag might not
199 * be noticed for a while. Just exit if we get a second SIGINT.
201 if (!(options & F_HOSTNAME) && seenint != 0)
202 _exit(nreceived ? 0 : 2);
213 * Print out summary statistics to the given output stream.
216 pr_summary(FILE * restrict stream)
218 fprintf(stream, "\n--- %s ping statistics ---\n", hostname);
219 fprintf(stream, "%ld packets transmitted, ", ntransmitted);
220 fprintf(stream, "%ld packets received, ", nreceived);
222 fprintf(stream, "+%ld duplicates, ", nrepeats);
224 if (nreceived > ntransmitted)
225 fprintf(stream, "-- somebody's duplicating packets!");
227 fprintf(stream, "%.1f%% packet loss",
228 ((((double)ntransmitted - nreceived) * 100.0) /
232 fprintf(stream, ", %ld packets out of wait time", nrcvtimeout);
234 if (nreceived && timing) {
235 /* Only display average to microseconds */
236 double num = nreceived + nrepeats;
237 double avg = tsum / num;
238 double stddev = sqrt(fmax(0, tsumsq / num - avg * avg));
240 "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n",
241 tmin, avg, tmax, stddev);
249 (void)fprintf(stderr,
252 "\tping [-4AaDdfHnoQqRrv] [-C pcp] [-c count] "
253 "[-G sweepmaxsize]\n"
254 "\t [-g sweepminsize] [-h sweepincrsize] [-i wait] "
256 "\t [-M mask | time] [-m ttl] "
260 "[-p pattern] [-S src_addr] \n"
261 "\t [-s packetsize] [-t timeout] [-W waittime] [-z tos] "
263 "\tping [-4AaDdfHLnoQqRrv] [-C pcp] [-c count] [-I iface] "
265 "\t [-l preload] [-M mask | time] [-m ttl] "
270 "\t [-S src_addr] [-s packetsize] [-T ttl] [-t timeout] [-W waittime]\n"
271 "\t [-z tos] IPv4-mcast-group\n"
275 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
279 #ifdef IPV6_USE_MIN_MTU
283 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
287 "[-b bufsiz] [-C pcp] [-c count] [-e gateway]\n"
288 "\t [-I interface] [-i wait] [-k addrtype] [-l preload] "
291 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
294 " [-S sourceaddr] [-s packetsize] [-t timeout]\n"
295 "\t [-W waittime] [-z tclass] [IPv6-hops ...] IPv6-host\n"