xref: /freebsd/sbin/ping/main.c (revision 50696a6e8cbfdbf4a0d00f2f85f1951aa0d8e23d)
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 #include "ping.h"
48 #ifdef INET6
49 #include "ping6.h"
50 #endif
51 
52 #ifdef INET6
53 #define	OPTSTR ":46"
54 #else
55 #define OPTSTR ":4"
56 #endif
57 
58 int
59 main(int argc, char *argv[])
60 {
61 	struct in_addr a;
62 	struct addrinfo hints;
63 	int ch;
64 	bool ipv4;
65 #ifdef INET6
66 	struct in6_addr a6;
67 	bool ipv6;
68 
69 	ipv6 = false;
70 #endif
71 	ipv4 = false;
72 
73 	while ((ch = getopt(argc, argv, OPTSTR)) != -1) {
74 		switch(ch) {
75 		case '4':
76 			ipv4 = true;
77 			break;
78 #ifdef INET6
79 		case '6':
80 			ipv6 = true;
81 			break;
82 #endif
83 		default:
84 			break;
85 		}
86 	}
87 
88 	if (optind >= argc)
89 		usage();
90 
91 	optreset = 1;
92 	optind = 1;
93 #ifdef INET6
94 	if (ipv4 && ipv6)
95 		errx(1, "-4 and -6 cannot be used simultaneously");
96 #endif
97 
98 	if (inet_pton(AF_INET, argv[argc - 1], &a) == 1) {
99 #ifdef INET6
100 		if (ipv6)
101 			errx(1, "IPv6 requested but IPv4 target address "
102 			    "provided");
103 #endif
104 		hints.ai_family = AF_INET;
105 	}
106 #ifdef INET6
107 	else if (inet_pton(AF_INET6, argv[argc - 1], &a6) == 1) {
108 		if (ipv4)
109 			errx(1, "IPv4 requested but IPv6 target address "
110 			    "provided");
111 		hints.ai_family = AF_INET6;
112 	} else if (ipv6)
113 		hints.ai_family = AF_INET6;
114 #endif
115 	else if (ipv4)
116 		hints.ai_family = AF_INET;
117 	else {
118 		struct addrinfo *res;
119 
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);
124 		if (res != NULL) {
125 			hints.ai_family = res[0].ai_family;
126 			freeaddrinfo(res);
127 		}
128 	}
129 
130 	if (hints.ai_family == AF_INET)
131 		return ping(argc, argv);
132 #ifdef INET6
133 	else if (hints.ai_family == AF_INET6)
134 		return ping6(argc, argv);
135 #endif
136 	else
137 		errx(1, "Unknown host");
138 }
139 
140 void
141 usage(void)
142 {
143 	(void)fprintf(stderr,
144 	    "usage: ping [-4AaDdfHnoQqRrv] [-C pcp] [-c count] "
145 	    "[-G sweepmaxsize]\n"
146 	    "	    [-g sweepminsize] [-h sweepincrsize] [-i wait] "
147 	    "[-l preload]\n"
148 	    "	    [-M mask | time] [-m ttl]"
149 #ifdef IPSEC
150 	    "[-P policy] "
151 #endif
152 	    "[-p pattern] [-S src_addr] \n"
153 	    "	    [-s packetsize] [-t timeout] [-W waittime] [-z tos] "
154 	    "IPv4-host\n"
155 	    "       ping [-4AaDdfHLnoQqRrv] [-C pcp] [-c count] [-I iface] "
156 	    "[-i wait]\n"
157 	    "	    [-l preload] [-M mask | time] [-m ttl] "
158 #ifdef IPSEC
159 	    "[-P policy] "
160 #endif
161 	    "[-p pattern]\n"
162 	    "	    [-S src_addr] [-s packetsize] [-T ttl] [-t timeout] [-W waittime]\n"
163 	    "            [-z tos] IPv4-mcast-group\n"
164 #ifdef INET6
165             "       ping [-6aADd"
166 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
167             "E"
168 #endif
169             "fHnNoOq"
170 #ifdef IPV6_USE_MIN_MTU
171             "u"
172 #endif
173             "vyY"
174 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC)
175             "Z"
176 #endif
177 	    "] "
178             "[-b bufsiz] [-c count] [-e gateway]\n"
179             "            [-I interface] [-i wait] [-k addrtype] [-l preload] "
180             "[-m hoplimit]\n"
181             "            [-p pattern]"
182 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
183             " [-P policy]"
184 #endif
185             " [-S sourceaddr] [-s packetsize] [-t timeout]\n"
186 	    "	    [-W waittime] [-z tclass] [IPv6-hops ...] IPv6-host\n"
187 #endif	/* INET6 */
188 	    );
189 
190 	exit(1);
191 }
192