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