xref: /freebsd/sbin/ping/main.c (revision 48cb3fee25862a6adc10fe0b2c2dc15c7fd7fd9e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2019 Jan Sucan <jansucan@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 
35 #include <arpa/inet.h>
36 #include <netdb.h>
37 #include <netinet/in.h>
38 
39 #include <err.h>
40 #include <stdbool.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include "main.h"
47 #ifdef INET
48 #include "ping.h"
49 #endif
50 #ifdef INET6
51 #include "ping6.h"
52 #endif
53 
54 #if defined(INET) && defined(INET6)
55 #define	OPTSTR PING6OPTS PING4OPTS
56 #elif defined(INET)
57 #define	OPTSTR PING4OPTS
58 #elif defined(INET6)
59 #define	OPTSTR PING6OPTS
60 #endif
61 
62 int
63 main(int argc, char *argv[])
64 {
65 #if defined(INET) && defined(INET6)
66 	struct in_addr a;
67 	struct in6_addr a6;
68 #endif
69 #if defined(INET) || defined(INET6)
70 	struct addrinfo hints;
71 #endif
72 	int ch;
73 #ifdef INET
74 	bool ipv4 = false;
75 #endif
76 #ifdef INET6
77 	bool ipv6 = false;
78 
79 	if (strcmp(getprogname(), "ping6") == 0)
80 		ipv6 = true;
81 #endif
82 
83 	while ((ch = getopt(argc, argv, ":" OPTSTR)) != -1) {
84 		switch(ch) {
85 #ifdef INET
86 		case '4':
87 			ipv4 = true;
88 			break;
89 #endif
90 #ifdef INET6
91 		case '6':
92 			ipv6 = true;
93 			break;
94 #endif
95 		default:
96 			break;
97 		}
98 	}
99 
100 	if (optind >= argc)
101 		usage();
102 
103 	optreset = 1;
104 	optind = 1;
105 #if defined(INET) && defined(INET6)
106 	if (ipv4 && ipv6)
107 		errx(1, "-4 and -6 cannot be used simultaneously");
108 #endif
109 
110 #if defined(INET) && defined(INET6)
111 	if (inet_pton(AF_INET, argv[argc - 1], &a) == 1) {
112 		if (ipv6)
113 			errx(1, "IPv6 requested but IPv4 target address "
114 			    "provided");
115 		hints.ai_family = AF_INET;
116 	}
117 	else if (inet_pton(AF_INET6, argv[argc - 1], &a6) == 1) {
118 		if (ipv4)
119 			errx(1, "IPv4 requested but IPv6 target address "
120 			    "provided");
121 		hints.ai_family = AF_INET6;
122 	} else if (ipv6)
123 		hints.ai_family = AF_INET6;
124 	else if (ipv4)
125 		hints.ai_family = AF_INET;
126 	else {
127 		if (!feature_present("inet6"))
128 			hints.ai_family = AF_INET;
129 		else if (!feature_present("inet"))
130 			hints.ai_family = AF_INET6;
131 		else {
132 			struct addrinfo *res;
133 
134 			memset(&hints, 0, sizeof(hints));
135 			hints.ai_socktype = SOCK_RAW;
136 			hints.ai_family = AF_UNSPEC;
137 			getaddrinfo(argv[argc - 1], NULL, &hints, &res);
138 			if (res != NULL) {
139 				hints.ai_family = res[0].ai_family;
140 				freeaddrinfo(res);
141 			}
142 		}
143 	}
144 #elif defined(INET)
145 	hints.ai_family = AF_INET;
146 #elif defined(INET6)
147 	hints.ai_family = AF_INET6;
148 #endif
149 
150 #ifdef INET
151 	if (hints.ai_family == AF_INET)
152 		return ping(argc, argv);
153 #endif /* INET */
154 #ifdef INET6
155 	if (hints.ai_family == AF_INET6)
156 		return ping6(argc, argv);
157 #endif /* INET6 */
158 	errx(1, "Unknown host");
159 }
160 
161 void
162 usage(void)
163 {
164 	(void)fprintf(stderr,
165 	    "usage:\n"
166 #ifdef INET
167 	    "\tping [-4AaDdfHnoQqRrv] [-C pcp] [-c count] "
168 	    "[-G sweepmaxsize]\n"
169 	    "	    [-g sweepminsize] [-h sweepincrsize] [-i wait] "
170 	    "[-l preload]\n"
171 	    "	    [-M mask | time] [-m ttl] "
172 #ifdef IPSEC
173 	    "[-P policy] "
174 #endif
175 	    "[-p pattern] [-S src_addr] \n"
176 	    "	    [-s packetsize] [-t timeout] [-W waittime] [-z tos] "
177 	    "IPv4-host\n"
178 	    "\tping [-4AaDdfHLnoQqRrv] [-C pcp] [-c count] [-I iface] "
179 	    "[-i wait]\n"
180 	    "	    [-l preload] [-M mask | time] [-m ttl] "
181 #ifdef IPSEC
182 	    "[-P policy] "
183 #endif
184 	    "[-p pattern]\n"
185 	    "	    [-S src_addr] [-s packetsize] [-T ttl] [-t timeout] [-W waittime]\n"
186 	    "            [-z tos] IPv4-mcast-group\n"
187 #endif /* INET */
188 #ifdef INET6
189             "\tping [-6AaDd"
190 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
191             "E"
192 #endif
193             "fHnNoOq"
194 #ifdef IPV6_USE_MIN_MTU
195             "u"
196 #endif
197             "vyY"
198 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
199             "Z"
200 #endif
201 	    "] "
202             "[-b bufsiz] [-c count] [-e gateway]\n"
203             "            [-I interface] [-i wait] [-k addrtype] [-l preload] "
204             "[-m hoplimit]\n"
205             "            [-p pattern]"
206 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
207             " [-P policy]"
208 #endif
209             " [-S sourceaddr] [-s packetsize] [-t timeout]\n"
210 	    "	    [-W waittime] [-z tclass] [IPv6-hops ...] IPv6-host\n"
211 #endif	/* INET6 */
212 	    );
213 
214 	exit(1);
215 }
216