1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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/types.h> 30 #include <sys/socket.h> 31 32 #include <arpa/inet.h> 33 #include <netdb.h> 34 #include <netinet/in.h> 35 36 #include <err.h> 37 #include <math.h> 38 #include <signal.h> 39 #include <stdbool.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <sysexits.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 #else 61 #error At least one of INET and INET6 is required 62 #endif 63 64 #ifdef __HAIKU__ 65 #define optreset optind 66 #define feature_present(x) true 67 #ifndef restrict 68 #define restrict 69 #endif 70 #endif 71 72 /* various options */ 73 u_int options; 74 75 char *hostname; 76 77 /* counters */ 78 long nreceived; /* # of packets we got back */ 79 long nrepeats; /* number of duplicates */ 80 long ntransmitted; /* sequence # for outbound packets = #sent */ 81 long nrcvtimeout = 0; /* # of packets we got back after waittime */ 82 83 /* nonzero if we've been told to finish up */ 84 volatile sig_atomic_t seenint; 85 volatile sig_atomic_t seeninfo; 86 87 /* timing */ 88 int timing; /* flag to do timing */ 89 double tmin = 999999999.0; /* minimum round trip time */ 90 double tmax = 0.0; /* maximum round trip time */ 91 double tsum = 0.0; /* sum of all times, for doing average */ 92 double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 93 94 int 95 main(int argc, char *argv[]) 96 { 97 #if defined(INET) 98 struct in_addr a; 99 #endif 100 #if defined(INET6) 101 struct in6_addr a6; 102 #endif 103 #if defined(INET) && defined(INET6) 104 struct addrinfo hints, *res, *ai; 105 const char *target; 106 int error; 107 #endif 108 int opt; 109 110 #ifdef INET6 111 if (strcmp(getprogname(), "ping6") == 0) 112 return ping6(argc, argv); 113 #endif 114 115 while ((opt = getopt(argc, argv, ":" OPTSTR)) != -1) { 116 switch (opt) { 117 #ifdef INET 118 case '4': 119 goto ping4; 120 #endif 121 #ifdef INET6 122 case '6': 123 goto ping6; 124 #endif 125 case 'S': 126 /* 127 * If -S is given with a numeric parameter, 128 * force use of the corresponding version. 129 */ 130 #ifdef INET 131 if (inet_pton(AF_INET, optarg, &a) == 1) 132 goto ping4; 133 #endif 134 #ifdef INET6 135 if (inet_pton(AF_INET6, optarg, &a6) == 1) 136 goto ping6; 137 #endif 138 break; 139 default: 140 break; 141 } 142 } 143 144 /* 145 * For IPv4, only one positional argument, the target, is allowed. 146 * For IPv6, multiple positional argument are allowed; the last 147 * one is the target, and preceding ones are intermediate hops. 148 * This nuance is lost here, but the only case where it matters is 149 * an error. 150 */ 151 if (optind >= argc) 152 usage(); 153 154 #if defined(INET) && defined(INET6) 155 target = argv[argc - 1]; 156 memset(&hints, 0, sizeof(hints)); 157 hints.ai_socktype = SOCK_RAW; 158 if (feature_present("inet") && !feature_present("inet6")) 159 hints.ai_family = AF_INET; 160 if (feature_present("inet6") && !feature_present("inet")) 161 hints.ai_family = AF_INET6; 162 else 163 hints.ai_family = AF_UNSPEC; 164 error = getaddrinfo(target, NULL, &hints, &res); 165 if (res == NULL) 166 errx(EX_NOHOST, "cannot resolve %s: %s", 167 target, gai_strerror(error)); 168 for (ai = res; ai != NULL; ai = ai->ai_next) { 169 if (ai->ai_family == AF_INET) { 170 freeaddrinfo(res); 171 goto ping4; 172 } 173 if (ai->ai_family == AF_INET6) { 174 freeaddrinfo(res); 175 goto ping6; 176 } 177 } 178 freeaddrinfo(res); 179 errx(EX_NOHOST, "cannot resolve %s", target); 180 #endif 181 #ifdef INET 182 ping4: 183 optreset = 1; 184 optind = 1; 185 return ping(argc, argv); 186 #endif 187 #ifdef INET6 188 ping6: 189 optreset = 1; 190 optind = 1; 191 return ping6(argc, argv); 192 #endif 193 } 194 195 /* 196 * onsignal -- 197 * Set the global bit that causes the main loop to quit. 198 */ 199 void 200 onsignal(int sig) 201 { 202 switch (sig) { 203 case SIGALRM: 204 case SIGINT: 205 /* 206 * When doing reverse DNS lookups, the seenint flag might not 207 * be noticed for a while. Just exit if we get a second SIGINT. 208 */ 209 if (!(options & F_HOSTNAME) && seenint != 0) 210 _exit(nreceived ? 0 : 2); 211 seenint++; 212 break; 213 #ifndef __HAIKU__ 214 case SIGINFO: 215 seeninfo++; 216 break; 217 #endif 218 } 219 } 220 221 /* 222 * pr_summary -- 223 * Print out summary statistics to the given output stream. 224 */ 225 void 226 pr_summary(FILE * restrict stream) 227 { 228 fprintf(stream, "\n--- %s ping statistics ---\n", hostname); 229 fprintf(stream, "%ld packets transmitted, ", ntransmitted); 230 fprintf(stream, "%ld packets received, ", nreceived); 231 if (nrepeats) 232 fprintf(stream, "+%ld duplicates, ", nrepeats); 233 if (ntransmitted) { 234 if (nreceived > ntransmitted) 235 fprintf(stream, "-- somebody's duplicating packets!"); 236 else 237 fprintf(stream, "%.1f%% packet loss", 238 ((((double)ntransmitted - nreceived) * 100.0) / 239 ntransmitted)); 240 } 241 if (nrcvtimeout) 242 fprintf(stream, ", %ld packets out of wait time", nrcvtimeout); 243 fputc('\n', stream); 244 if (nreceived && timing) { 245 /* Only display average to microseconds */ 246 double num = nreceived + nrepeats; 247 double avg = tsum / num; 248 double stddev = sqrt(fmax(0, tsumsq / num - avg * avg)); 249 fprintf(stream, 250 "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", 251 tmin, avg, tmax, stddev); 252 } 253 fflush(stream); 254 } 255 256 void 257 usage(void) 258 { 259 (void)fprintf(stderr, 260 "usage:\n" 261 #ifdef INET 262 "\tping [-4AaDdfHnoQqRrv] [-C pcp] [-c count] " 263 "[-G sweepmaxsize]\n" 264 "\t [-g sweepminsize] [-h sweepincrsize] [-i wait] " 265 "[-l preload]\n" 266 "\t [-M mask | time] [-m ttl] " 267 #ifdef IPSEC 268 "[-P policy] " 269 #endif 270 "[-p pattern] [-S src_addr] \n" 271 "\t [-s packetsize] [-t timeout] [-W waittime] [-z tos] " 272 "IPv4-host\n" 273 "\tping [-4AaDdfHLnoQqRrv] [-C pcp] [-c count] [-I iface] " 274 "[-i wait]\n" 275 "\t [-l preload] [-M mask | time] [-m ttl] " 276 #ifdef IPSEC 277 "[-P policy] " 278 #endif 279 "[-p pattern]\n" 280 "\t [-S src_addr] [-s packetsize] [-T ttl] [-t timeout] [-W waittime]\n" 281 "\t [-z tos] IPv4-mcast-group\n" 282 #endif /* INET */ 283 #ifdef INET6 284 "\tping [-6AaDd" 285 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC) 286 "E" 287 #endif 288 "fHnNoOq" 289 #ifdef IPV6_USE_MIN_MTU 290 "u" 291 #endif 292 "vyY" 293 #if defined(IPSEC) && !defined(IPSEC_POLICY_IPSEC) 294 "Z" 295 #endif 296 "] " 297 "[-b bufsiz] [-C pcp] [-c count] [-e gateway]\n" 298 "\t [-I interface] [-i wait] [-k addrtype] [-l preload] " 299 "[-m hoplimit]\n" 300 "\t [-p pattern]" 301 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 302 " [-P policy]" 303 #endif 304 " [-S sourceaddr] [-s packetsize] [-t timeout]\n" 305 "\t [-W waittime] [-z tclass] [IPv6-hops ...] IPv6-host\n" 306 #endif /* INET6 */ 307 ); 308 309 exit(1); 310 } 311