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 if (!feature_present("inet6")) 130 hints.ai_family = AF_INET; 131 else if (!feature_present("inet")) 132 hints.ai_family = AF_INET6; 133 else { 134 struct addrinfo *res; 135 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); 140 if (res != NULL) { 141 hints.ai_family = res[0].ai_family; 142 freeaddrinfo(res); 143 } 144 } 145 } 146 #elif defined(INET) 147 hints.ai_family = AF_INET; 148 #elif defined(INET6) 149 hints.ai_family = AF_INET6; 150 #endif 151 152 #ifdef INET 153 if (hints.ai_family == AF_INET) 154 return ping(argc, argv); 155 #endif /* INET */ 156 #ifdef INET6 157 if (hints.ai_family == AF_INET6) 158 return ping6(argc, argv); 159 #endif /* INET6 */ 160 errx(1, "Unknown host"); 161 } 162 163 void 164 usage(void) 165 { 166 (void)fprintf(stderr, 167 "usage:\n" 168 #ifdef INET 169 "\tping [-4AaDdfHnoQqRrv] [-C pcp] [-c count] " 170 "[-G sweepmaxsize]\n" 171 " [-g sweepminsize] [-h sweepincrsize] [-i wait] " 172 "[-l preload]\n" 173 " [-M mask | time] [-m ttl]" 174 #ifdef IPSEC 175 "[-P policy] " 176 #endif 177 "[-p pattern] [-S src_addr] \n" 178 " [-s packetsize] [-t timeout] [-W waittime] [-z tos] " 179 "IPv4-host\n" 180 "\tping [-4AaDdfHLnoQqRrv] [-C pcp] [-c count] [-I iface] " 181 "[-i wait]\n" 182 " [-l preload] [-M mask | time] [-m ttl] " 183 #ifdef IPSEC 184 "[-P policy] " 185 #endif 186 "[-p pattern]\n" 187 " [-S src_addr] [-s packetsize] [-T ttl] [-t timeout] [-W waittime]\n" 188 " [-z tos] IPv4-mcast-group\n" 189 #endif /* INET */ 190 #ifdef INET6 191 "\tping [-6aADd" 192 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC) 193 "E" 194 #endif 195 "fHnNoOq" 196 #ifdef IPV6_USE_MIN_MTU 197 "u" 198 #endif 199 "vyY" 200 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC) 201 "Z" 202 #endif 203 "] " 204 "[-b bufsiz] [-c count] [-e gateway]\n" 205 " [-I interface] [-i wait] [-k addrtype] [-l preload] " 206 "[-m hoplimit]\n" 207 " [-p pattern]" 208 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 209 " [-P policy]" 210 #endif 211 " [-S sourceaddr] [-s packetsize] [-t timeout]\n" 212 " [-W waittime] [-z tclass] [IPv6-hops ...] IPv6-host\n" 213 #endif /* INET6 */ 214 ); 215 216 exit(1); 217 } 218