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 if (!feature_present("inet6"))
130 hints.ai_family = AF_INET;
131 else if (!feature_present("inet"))
132 hints.ai_family = AF_INET6;
134 struct addrinfo *res;
136 memset(&hints, 0, sizeof(hints));
137 hints.ai_socktype = SOCK_RAW;
138 hints.ai_family = AF_UNSPEC;
139 getaddrinfo(argv[argc - 1], NULL, &hints, &res);
141 hints.ai_family = res[0].ai_family;
147 hints.ai_family = AF_INET;
149 hints.ai_family = AF_INET6;
153 if (hints.ai_family == AF_INET)
154 return ping(argc, argv);
157 if (hints.ai_family == AF_INET6)
158 return ping6(argc, argv);
160 errx(1, "Unknown host");
166 (void)fprintf(stderr,
169 "\tping [-4AaDdfHnoQqRrv] [-C pcp] [-c count] "
170 "[-G sweepmaxsize]\n"
171 " [-g sweepminsize] [-h sweepincrsize] [-i wait] "
173 " [-M mask | time] [-m ttl]"
177 "[-p pattern] [-S src_addr] \n"
178 " [-s packetsize] [-t timeout] [-W waittime] [-z tos] "
180 "\tping [-4AaDdfHLnoQqRrv] [-C pcp] [-c count] [-I iface] "
182 " [-l preload] [-M mask | time] [-m ttl] "
187 " [-S src_addr] [-s packetsize] [-T ttl] [-t timeout] [-W waittime]\n"
188 " [-z tos] IPv4-mcast-group\n"
192 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
196 #ifdef IPV6_USE_MIN_MTU
200 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
204 "[-b bufsiz] [-c count] [-e gateway]\n"
205 " [-I interface] [-i wait] [-k addrtype] [-l preload] "
208 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
211 " [-S sourceaddr] [-s packetsize] [-t timeout]\n"
212 " [-W waittime] [-z tclass] [IPv6-hops ...] IPv6-host\n"