2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <arpa/inet.h>
37 #include <netinet/in.h>
54 #if defined(INET) && defined(INET6)
65 main(int argc, char *argv[])
67 #if defined(INET) && defined(INET6)
71 #if defined(INET) || defined(INET6)
72 struct addrinfo hints;
81 if (strcmp(getprogname(), "ping6") == 0)
85 while ((ch = getopt(argc, argv, OPTSTR)) != -1) {
107 #if defined(INET) && defined(INET6)
109 errx(1, "-4 and -6 cannot be used simultaneously");
112 #if defined(INET) && defined(INET6)
113 if (inet_pton(AF_INET, argv[argc - 1], &a) == 1) {
115 errx(1, "IPv6 requested but IPv4 target address "
117 hints.ai_family = AF_INET;
119 else if (inet_pton(AF_INET6, argv[argc - 1], &a6) == 1) {
121 errx(1, "IPv4 requested but IPv6 target address "
123 hints.ai_family = AF_INET6;
125 hints.ai_family = AF_INET6;
127 hints.ai_family = AF_INET;
129 struct addrinfo *res;
131 memset(&hints, 0, sizeof(hints));
132 hints.ai_socktype = SOCK_RAW;
133 hints.ai_family = AF_UNSPEC;
134 getaddrinfo(argv[argc - 1], NULL, &hints, &res);
136 hints.ai_family = res[0].ai_family;
141 hints.ai_family = AF_INET;
143 hints.ai_family = AF_INET6;
147 if (hints.ai_family == AF_INET)
148 return ping(argc, argv);
151 if (hints.ai_family == AF_INET6)
152 return ping6(argc, argv);
154 errx(1, "Unknown host");
160 (void)fprintf(stderr,
163 "\tping [-4AaDdfHnoQqRrv] [-C pcp] [-c count] "
164 "[-G sweepmaxsize]\n"
165 " [-g sweepminsize] [-h sweepincrsize] [-i wait] "
167 " [-M mask | time] [-m ttl]"
171 "[-p pattern] [-S src_addr] \n"
172 " [-s packetsize] [-t timeout] [-W waittime] [-z tos] "
174 "\tping [-4AaDdfHLnoQqRrv] [-C pcp] [-c count] [-I iface] "
176 " [-l preload] [-M mask | time] [-m ttl] "
181 " [-S src_addr] [-s packetsize] [-T ttl] [-t timeout] [-W waittime]\n"
182 " [-z tos] IPv4-mcast-group\n"
186 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
190 #ifdef IPV6_USE_MIN_MTU
194 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
198 "[-b bufsiz] [-c count] [-e gateway]\n"
199 " [-I interface] [-i wait] [-k addrtype] [-l preload] "
202 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
205 " [-S sourceaddr] [-s packetsize] [-t timeout]\n"
206 " [-W waittime] [-z tclass] [IPv6-hops ...] IPv6-host\n"