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>
59 main(int argc, char *argv[])
62 struct addrinfo hints;
73 while ((ch = getopt(argc, argv, OPTSTR)) != -1) {
95 errx(1, "-4 and -6 cannot be used simultaneously");
98 if (inet_pton(AF_INET, argv[argc - 1], &a) == 1) {
101 errx(1, "IPv6 requested but IPv4 target address "
104 hints.ai_family = AF_INET;
107 else if (inet_pton(AF_INET6, argv[argc - 1], &a6) == 1) {
109 errx(1, "IPv4 requested but IPv6 target address "
111 hints.ai_family = AF_INET6;
113 hints.ai_family = AF_INET6;
116 hints.ai_family = AF_INET;
118 struct addrinfo *res;
120 memset(&hints, 0, sizeof(hints));
121 hints.ai_socktype = SOCK_RAW;
122 hints.ai_family = AF_UNSPEC;
123 getaddrinfo(argv[argc - 1], NULL, &hints, &res);
125 hints.ai_family = res[0].ai_family;
130 if (hints.ai_family == AF_INET)
131 return ping(argc, argv);
133 else if (hints.ai_family == AF_INET6)
134 return ping6(argc, argv);
137 errx(1, "Unknown host");
143 (void)fprintf(stderr,
144 "usage: ping [-4AaDdfHnoQqRrv] [-C pcp] [-c count] "
145 "[-G sweepmaxsize]\n"
146 " [-g sweepminsize] [-h sweepincrsize] [-i wait] "
148 " [-M mask | time] [-m ttl]"
152 "[-p pattern] [-S src_addr] \n"
153 " [-s packetsize] [-t timeout] [-W waittime] [-z tos] "
155 " ping [-4AaDdfHLnoQqRrv] [-C pcp] [-c count] [-I iface] "
157 " [-l preload] [-M mask | time] [-m ttl] "
162 " [-S src_addr] [-s packetsize] [-T ttl] [-t timeout] [-W waittime]\n"
163 " [-z tos] IPv4-mcast-group\n"
166 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
170 #ifdef IPV6_USE_MIN_MTU
174 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
178 "[-b bufsiz] [-c count] [-e gateway]\n"
179 " [-I interface] [-i wait] [-k addrtype] [-l preload] "
182 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
185 " [-S sourceaddr] [-s packetsize] [-t timeout]\n"
186 " [-W waittime] [-z tclass] [IPv6-hops ...] IPv6-host\n"