Home | History | Annotate | Line # | Download | only in ping
ping.c revision 1.115
      1 /*	$NetBSD: ping.c,v 1.115 2017/01/11 12:13:52 joerg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Mike Muuss.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  *			P I N G . C
     37  *
     38  * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
     39  * measure round-trip-delays and packet loss across network paths.
     40  *
     41  * Author -
     42  *	Mike Muuss
     43  *	U. S. Army Ballistic Research Laboratory
     44  *	December, 1983
     45  * Modified at Uc Berkeley
     46  * Record Route and verbose headers - Phil Dykstra, BRL, March 1988.
     47  * Multicast options (ttl, if, loop) - Steve Deering, Stanford, August 1988.
     48  * ttl, duplicate detection - Cliff Frost, UCB, April 1989
     49  * Pad pattern - Cliff Frost (from Tom Ferrin, UCSF), April 1989
     50  *
     51  * Status -
     52  *	Public Domain.  Distribution Unlimited.
     53  *
     54  * Bugs -
     55  *	More statistics could always be gathered.
     56  *	This program has to run SUID to ROOT to access the ICMP socket.
     57  */
     58 
     59 #include <sys/cdefs.h>
     60 #ifndef lint
     61 __RCSID("$NetBSD: ping.c,v 1.115 2017/01/11 12:13:52 joerg Exp $");
     62 #endif
     63 
     64 #include <stdio.h>
     65 #include <stddef.h>
     66 #include <errno.h>
     67 #include <signal.h>
     68 #include <sys/time.h>
     69 #include <sys/types.h>
     70 #include <sys/param.h>
     71 #include <sys/socket.h>
     72 #include <sys/file.h>
     73 #include <termios.h>
     74 #include <stdlib.h>
     75 #include <unistd.h>
     76 #include <poll.h>
     77 #include <limits.h>
     78 #include <math.h>
     79 #include <string.h>
     80 #include <err.h>
     81 
     82 #include <netinet/in_systm.h>
     83 #include <netinet/in.h>
     84 #include <netinet/ip.h>
     85 #include <netinet/ip_icmp.h>
     86 #include <netinet/ip_var.h>
     87 #include <arpa/inet.h>
     88 #include <ctype.h>
     89 #include <netdb.h>
     90 
     91 #ifdef IPSEC
     92 #include <netipsec/ipsec.h>
     93 #endif /*IPSEC*/
     94 
     95 #include "prog_ops.h"
     96 
     97 #define FLOOD_INTVL	0.01		/* default flood output interval */
     98 #define	MAXPACKET	(IP_MAXPACKET-60-8)	/* max packet size */
     99 
    100 #define F_VERBOSE	0x0001
    101 #define F_QUIET		0x0002		/* minimize all output */
    102 #define F_SEMI_QUIET	0x0004		/* ignore our ICMP errors */
    103 #define F_FLOOD		0x0008		/* flood-ping */
    104 #define	F_RECORD_ROUTE	0x0010		/* record route */
    105 #define F_SOURCE_ROUTE	0x0020		/* loose source route */
    106 #define F_PING_FILLED	0x0040		/* is buffer filled with user data? */
    107 #define F_PING_RANDOM	0x0080		/* use random data */
    108 #define	F_NUMERIC	0x0100		/* do not do gethostbyaddr() calls */
    109 #define F_TIMING	0x0200		/* room for a timestamp */
    110 #define F_DF		0x0400		/* set IP DF bit */
    111 #define F_SOURCE_ADDR	0x0800		/* set source IP address/interface */
    112 #define F_ONCE		0x1000		/* exit(0) after receiving 1 reply */
    113 #define F_MCAST		0x2000		/* multicast target */
    114 #define F_MCAST_NOLOOP	0x4000		/* no multicast loopback */
    115 #define F_AUDIBLE	0x8000		/* audible output */
    116 #define F_TIMING64	0x10000		/* 64 bit time, nanoseconds */
    117 #ifdef IPSEC
    118 #ifdef IPSEC_POLICY_IPSEC
    119 #define F_POLICY	0x20000
    120 #else
    121 #define	F_AUTHHDR	0x20000
    122 #define	F_ENCRYPT	0x40000
    123 #endif /*IPSEC_POLICY_IPSEC*/
    124 #endif /*IPSEC*/
    125 
    126 
    127 /* MAX_DUP_CHK is the number of bits in received table, the
    128  *	maximum number of received sequence numbers we can track to check
    129  *	for duplicates.
    130  */
    131 #define MAX_DUP_CHK     (8 * 2048)
    132 static u_char	rcvd_tbl[MAX_DUP_CHK/8];
    133 static int     nrepeats = 0;
    134 #define A(seq)	rcvd_tbl[(seq/8)%sizeof(rcvd_tbl)]  /* byte in array */
    135 #define B(seq)	(1 << (seq & 0x07))	/* bit in byte */
    136 #define SET(seq) (A(seq) |= B(seq))
    137 #define CLR(seq) (A(seq) &= (~B(seq)))
    138 #define TST(seq) (A(seq) & B(seq))
    139 
    140 struct tv32 {
    141 	int32_t tv32_sec;
    142 	int32_t tv32_usec;
    143 };
    144 
    145 
    146 static u_char	*packet;
    147 static int	packlen;
    148 static int	pingflags = 0, options;
    149 static int	pongflags = 0;
    150 static char	*fill_pat;
    151 
    152 static int s;					/* Socket file descriptor */
    153 static int sloop;				/* Socket file descriptor/loopback */
    154 
    155 #define PHDR_LEN sizeof(struct tv32)		/* size of timestamp header */
    156 #define PHDR64_LEN sizeof(struct timespec)	/* size of timestamp header */
    157 static struct sockaddr_in whereto, send_addr;	/* Who to ping */
    158 static struct sockaddr_in src_addr;		/* from where */
    159 static struct sockaddr_in loc_addr;		/* 127.1 */
    160 static int datalen;				/* How much data */
    161 static int phdrlen;
    162 
    163 static sigset_t blockmask, enablemask;		/* signal masks */
    164 
    165 #ifndef __NetBSD__
    166 static char *progname;
    167 #define	getprogname()		(progname)
    168 #define	setprogname(name)	((void)(progname = (name)))
    169 #endif
    170 
    171 static char hostname[MAXHOSTNAMELEN];
    172 
    173 static struct {
    174 	struct ip	o_ip;
    175 	char		o_opt[MAX_IPOPTLEN];
    176 	union {
    177 		u_char	    u_buf[MAXPACKET+offsetof(struct icmp, icmp_data)];
    178 		struct icmp u_icmp;
    179 	} o_u;
    180 } out_pack;
    181 #define	opack_icmp	out_pack.o_u.u_icmp
    182 static struct ip *opack_ip;
    183 
    184 static uint8_t optspace[MAX_IPOPTLEN];		/* record route space */
    185 static int optlen;
    186 
    187 static int npackets;				/* total packets to send */
    188 static int preload;				/* number of packets to "preload" */
    189 static int ntransmitted;			/* output sequence # = #sent */
    190 static int ident;				/* our ID, in network byte order */
    191 
    192 static int nreceived;				/* # of packets we got back */
    193 
    194 static double interval;			/* interval between packets */
    195 static struct timespec interval_tv;
    196 static double tmin = 999999999.0;
    197 static double tmax = 0.0;
    198 static double tsum = 0.0;			/* sum of all times */
    199 static double tsumsq = 0.0;
    200 static double maxwait = 0.0;
    201 
    202 static int bufspace = IP_MAXPACKET;
    203 
    204 static struct timespec now, clear_cache, last_tx, next_tx, first_tx;
    205 static struct timespec last_rx, first_rx;
    206 static int lastrcvd = 1;			/* last ping sent has been received */
    207 
    208 static struct timespec jiggle_time;
    209 static int jiggle_cnt, total_jiggled, jiggle_direction = -1;
    210 
    211 __dead static void doit(void);
    212 static void prefinish(int);
    213 static void prtsig(int);
    214 __dead static void finish(int);
    215 static void blocksignals(void);
    216 static void enablesignals(void);
    217 static void summary(int);
    218 static void pinger(void);
    219 static void fill(void);
    220 static void rnd_fill(void);
    221 static double diffsec(struct timespec *, struct timespec *);
    222 #if 0
    223 static void timespecadd(struct timespec *, struct timespec *);
    224 #endif
    225 static void sec_to_timespec(const double, struct timespec *);
    226 static double timespec_to_sec(const struct timespec *);
    227 static void pr_pack(u_char *, int, struct sockaddr_in *);
    228 static u_int16_t in_cksum(u_int16_t *, u_int);
    229 static void pr_saddr(u_char *);
    230 static char *pr_addr(struct in_addr *);
    231 static void pr_iph(struct icmp *, int);
    232 static void pr_retip(struct icmp *, int);
    233 static int pr_icmph(struct icmp *, struct sockaddr_in *, int);
    234 static void jiggle(int), jiggle_flush(int);
    235 static void gethost(const char *, const char *,
    236 		    struct sockaddr_in *, char *, int);
    237 __dead static void usage(void);
    238 
    239 int
    240 main(int argc, char *argv[])
    241 {
    242 	int c, i, on = 1, hostind = 0;
    243 	long l;
    244 	int len = -1, compat = 0;
    245 	u_char ttl = 0;
    246 	u_long tos = 0;
    247 	char *p;
    248 #ifdef IPSEC
    249 #ifdef IPSEC_POLICY_IPSEC
    250 	char *policy_in = NULL;
    251 	char *policy_out = NULL;
    252 #endif
    253 #endif
    254 #ifdef SIGINFO
    255 	struct sigaction sa;
    256 #endif
    257 
    258 	if (prog_init && prog_init() == -1)
    259 		err(EXIT_FAILURE, "init failed");
    260 
    261 	if ((s = prog_socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
    262 		err(EXIT_FAILURE, "Cannot create socket");
    263 	if ((sloop = prog_socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
    264 		err(EXIT_FAILURE, "Cannot create socket");
    265 
    266 	/*
    267 	 * sloop is never read on.  This prevents packets from
    268 	 * queueing in its recv buffer.
    269 	 */
    270 	if (prog_shutdown(sloop, SHUT_RD) == -1)
    271 		warn("Cannot shutdown for read");
    272 
    273 	if (prog_setuid(prog_getuid()) == -1)
    274 		err(EXIT_FAILURE, "setuid");
    275 
    276 	setprogname(argv[0]);
    277 
    278 #ifndef IPSEC
    279 #define IPSECOPT
    280 #else
    281 #ifdef IPSEC_POLICY_IPSEC
    282 #define IPSECOPT	"E:"
    283 #else
    284 #define IPSECOPT	"AE"
    285 #endif /*IPSEC_POLICY_IPSEC*/
    286 #endif
    287 	while ((c = getopt(argc, argv,
    288 			   "ac:CdDfg:h:i:I:l:Lnop:PqQrRs:t:T:vw:" IPSECOPT)) != -1) {
    289 #undef IPSECOPT
    290 		switch (c) {
    291 		case 'a':
    292 			pingflags |= F_AUDIBLE;
    293 			break;
    294 		case 'C':
    295 			compat = 1;
    296 			break;
    297 		case 'c':
    298 			l = strtol(optarg, &p, 0);
    299 			if (*p != '\0' || l <= 0)
    300 				errx(EXIT_FAILURE,
    301 				    "Bad/invalid number of packets: %s",
    302 				    optarg);
    303 #if INT_MAX < LONG_MAX
    304 			if (l > INT_MAX)
    305 				errx(EXIT_FAILURE,
    306 				    "Too many packets to count: %ld", l);
    307 #endif
    308 			npackets = l;
    309 			break;
    310 		case 'D':
    311 			pingflags |= F_DF;
    312 			break;
    313 		case 'd':
    314 			options |= SO_DEBUG;
    315 			break;
    316 		case 'f':
    317 			pingflags |= F_FLOOD;
    318 			break;
    319 		case 'h':
    320 			hostind = optind-1;
    321 			break;
    322 		case 'i':		/* wait between sending packets */
    323 			interval = strtod(optarg, &p);
    324 			if (*p != '\0' || interval <= 0)
    325 				errx(EXIT_FAILURE, "Bad/invalid interval: %s",
    326 				    optarg);
    327 			/*
    328 			 * In order to avoid overflowing the microseconds
    329 			 * argument of poll() the interval must be less than
    330 			 * INT_MAX/1000. Limit it to one second less than
    331 			 * that to be safe.
    332 			 */
    333 			if (interval >= INT_MAX/1000.0 - 1.0)
    334 				errx(EXIT_FAILURE,
    335 				    "Timing interval %g too large", interval);
    336 			break;
    337 		case 'l':
    338 			l = strtol(optarg, &p, 0);
    339 			if (*p != '\0' || l < 0)
    340 				errx(EXIT_FAILURE, "Bad/invalid preload value: "
    341 				    "%s", optarg);
    342 #if INT_MAX < LONG_MAX
    343 			if (l > INT_MAX)
    344 				errx(EXIT_FAILURE,
    345 				    "Too many preload packets: %ld", l);
    346 #endif
    347 			preload = l;
    348 			break;
    349 		case 'n':
    350 			pingflags |= F_NUMERIC;
    351 			break;
    352 		case 'o':
    353 			pingflags |= F_ONCE;
    354 			break;
    355 		case 'p':		/* fill buffer with user pattern */
    356 			if (pingflags & F_PING_RANDOM)
    357 				errx(EXIT_FAILURE,
    358 				    "Only one of -P and -p allowed");
    359 			pingflags |= F_PING_FILLED;
    360 			fill_pat = optarg;
    361 			break;
    362 		case 'P':
    363 			if (pingflags & F_PING_FILLED)
    364 				errx(EXIT_FAILURE,
    365 				    "Only one of -P and -p allowed");
    366 			pingflags |= F_PING_RANDOM;
    367 			break;
    368 		case 'q':
    369 			pingflags |= F_QUIET;
    370 			break;
    371 		case 'Q':
    372 			pingflags |= F_SEMI_QUIET;
    373 			break;
    374 		case 'r':
    375 			options |= SO_DONTROUTE;
    376 			break;
    377 		case 's':		/* size of packet to send */
    378 			l = strtol(optarg, &p, 0);
    379 			if (*p != '\0' || l < 0)
    380 				errx(EXIT_FAILURE,
    381 				    "Bad/invalid packet size: %s", optarg);
    382 			if (l > MAXPACKET)
    383 				errx(EXIT_FAILURE, "packet size is too large");
    384 			len = (int)l;
    385 			break;
    386 		case 'v':
    387 			pingflags |= F_VERBOSE;
    388 			break;
    389 		case 'R':
    390 			pingflags |= F_RECORD_ROUTE;
    391 			break;
    392 		case 'L':
    393 			pingflags |= F_MCAST_NOLOOP;
    394 			break;
    395 		case 't':
    396 			tos = strtoul(optarg, &p, 0);
    397 			if (*p != '\0' ||  tos > 0xFF)
    398 				errx(EXIT_FAILURE, "bad tos value: %s", optarg);
    399 			break;
    400 		case 'T':
    401 			l = strtol(optarg, &p, 0);
    402 			if (*p != '\0' || l > 255 || l <= 0)
    403 				errx(EXIT_FAILURE, "ttl out of range: %s",
    404 				    optarg);
    405 			ttl = (u_char)l;    /* cannot check >255 otherwise */
    406 			break;
    407 		case 'I':
    408 			pingflags |= F_SOURCE_ADDR;
    409 			gethost("-I", optarg, &src_addr, 0, 0);
    410 			break;
    411 		case 'g':
    412 			pingflags |= F_SOURCE_ROUTE;
    413 			gethost("-g", optarg, &send_addr, 0, 0);
    414 			break;
    415 		case 'w':
    416 			maxwait = strtod(optarg, &p);
    417 			if (*p != '\0' || maxwait <= 0)
    418 				errx(EXIT_FAILURE, "Bad/invalid maxwait time: "
    419 				    "%s", optarg);
    420 			break;
    421 #ifdef IPSEC
    422 #ifdef IPSEC_POLICY_IPSEC
    423 		case 'E':
    424 			pingflags |= F_POLICY;
    425 			if (!strncmp("in", optarg, 2)) {
    426 				policy_in = strdup(optarg);
    427 				if (!policy_in)
    428 					err(EXIT_FAILURE, "strdup");
    429 			} else if (!strncmp("out", optarg, 3)) {
    430 				policy_out = strdup(optarg);
    431 				if (!policy_out)
    432 					err(EXIT_FAILURE, "strdup");
    433 			} else
    434 				errx(EXIT_FAILURE, "invalid security policy: "
    435 				    "%s", optarg);
    436 			break;
    437 #else
    438 		case 'A':
    439 			pingflags |= F_AUTHHDR;
    440 			break;
    441 		case 'E':
    442 			pingflags |= F_ENCRYPT;
    443 			break;
    444 #endif /*IPSEC_POLICY_IPSEC*/
    445 #endif /*IPSEC*/
    446 		default:
    447 			usage();
    448 			break;
    449 		}
    450 	}
    451 
    452 	if (interval == 0)
    453 		interval = (pingflags & F_FLOOD) ? FLOOD_INTVL : 1.0;
    454 #ifndef sgi
    455 	if (pingflags & F_FLOOD && prog_getuid())
    456 		errx(EXIT_FAILURE, "Must be superuser to use -f");
    457 	if (interval < 1.0 && prog_getuid())
    458 		errx(EXIT_FAILURE, "Must be superuser to use < 1 sec "
    459 		    "ping interval");
    460 	if (preload > 0 && prog_getuid())
    461 		errx(EXIT_FAILURE, "Must be superuser to use -l");
    462 #endif
    463 	sec_to_timespec(interval, &interval_tv);
    464 	if (interval_tv.tv_sec == 0 && interval_tv.tv_nsec == 0) {
    465 		errx(EXIT_FAILURE, "Packet interval must be at least 1 ns");
    466 	}
    467 
    468 	if ((pingflags & (F_AUDIBLE|F_FLOOD)) == (F_AUDIBLE|F_FLOOD))
    469 		warnx("Sorry, no audible output for flood pings");
    470 
    471 	if (npackets != 0) {
    472 		npackets += preload;
    473 	} else {
    474 		npackets = INT_MAX;
    475 	}
    476 
    477 	if (hostind == 0) {
    478 		if (optind != argc-1)
    479 			usage();
    480 		else
    481 			hostind = optind;
    482 	}
    483 	else if (hostind >= argc - 1)
    484 		usage();
    485 
    486 	gethost("", argv[hostind], &whereto, hostname, sizeof(hostname));
    487 	if (IN_MULTICAST(ntohl(whereto.sin_addr.s_addr)))
    488 		pingflags |= F_MCAST;
    489 	if (!(pingflags & F_SOURCE_ROUTE))
    490 		(void) memcpy(&send_addr, &whereto, sizeof(send_addr));
    491 
    492 	loc_addr.sin_family = AF_INET;
    493 	loc_addr.sin_len = sizeof(struct sockaddr_in);
    494 	loc_addr.sin_addr.s_addr = htonl((127 << 24) + 1);
    495 
    496 	if (len != -1)
    497 		datalen = len;
    498 	else
    499 		datalen = 64 - PHDR_LEN;
    500 	if (!compat && datalen >= (int)PHDR64_LEN) { /* can we time them? */
    501 		pingflags |= F_TIMING64;
    502 		phdrlen = PHDR64_LEN;
    503 	} else if (datalen >= (int)PHDR_LEN) {	/* can we time them? */
    504 		pingflags |= F_TIMING;
    505 		phdrlen = PHDR_LEN;
    506 	} else
    507 		phdrlen = 0;
    508 
    509 	packlen = datalen + 60 + 76;	/* MAXIP + MAXICMP */
    510 	if ((packet = malloc(packlen)) == NULL)
    511 		err(EXIT_FAILURE, "Can't allocate %d bytes", packlen);
    512 
    513 	if (pingflags & F_PING_FILLED) {
    514 		fill();
    515 	} else if (pingflags & F_PING_RANDOM) {
    516 		rnd_fill();
    517 	} else {
    518 		for (i = phdrlen; i < datalen; i++)
    519 			opack_icmp.icmp_data[i] = i;
    520 	}
    521 
    522 	ident = arc4random() & 0xFFFF;
    523 
    524 	if (options & SO_DEBUG) {
    525 		if (prog_setsockopt(s, SOL_SOCKET, SO_DEBUG,
    526 			       (char *)&on, sizeof(on)) == -1)
    527 			warn("Can't turn on socket debugging");
    528 	}
    529 	if (options & SO_DONTROUTE) {
    530 		if (prog_setsockopt(s, SOL_SOCKET, SO_DONTROUTE,
    531 			       (char *)&on, sizeof(on)) == -1)
    532 			warn("SO_DONTROUTE");
    533 	}
    534 
    535 	if (options & SO_DEBUG) {
    536 		if (prog_setsockopt(sloop, SOL_SOCKET, SO_DEBUG,
    537 			       (char *)&on, sizeof(on)) == -1)
    538 			warn("Can't turn on socket debugging");
    539 	}
    540 	if (options & SO_DONTROUTE) {
    541 		if (prog_setsockopt(sloop, SOL_SOCKET, SO_DONTROUTE,
    542 			       (char *)&on, sizeof(on)) == -1)
    543 			warn("SO_DONTROUTE");
    544 	}
    545 
    546 	if (pingflags & F_SOURCE_ROUTE) {
    547 		optspace[IPOPT_OPTVAL] = IPOPT_LSRR;
    548 		optspace[IPOPT_OLEN] = optlen = 7;
    549 		optspace[IPOPT_OFFSET] = IPOPT_MINOFF;
    550 		(void)memcpy(&optspace[IPOPT_MINOFF-1], &whereto.sin_addr,
    551 			     sizeof(whereto.sin_addr));
    552 		optspace[optlen++] = IPOPT_NOP;
    553 	}
    554 	if (pingflags & F_RECORD_ROUTE) {
    555 		optspace[optlen+IPOPT_OPTVAL] = IPOPT_RR;
    556 		optspace[optlen+IPOPT_OLEN] = (MAX_IPOPTLEN -1-optlen);
    557 		optspace[optlen+IPOPT_OFFSET] = IPOPT_MINOFF;
    558 		optlen = MAX_IPOPTLEN;
    559 	}
    560 	/* this leaves opack_ip 0(mod 4) aligned */
    561 	opack_ip = (struct ip *)((char *)&out_pack.o_ip
    562 				 + sizeof(out_pack.o_opt)
    563 				 - optlen);
    564 	(void) memcpy(opack_ip + 1, optspace, optlen);
    565 
    566 	if (prog_setsockopt(s, IPPROTO_IP, IP_HDRINCL,
    567 	    (char *) &on, sizeof(on)) < 0)
    568 		err(EXIT_FAILURE, "Can't set special IP header");
    569 
    570 	opack_ip->ip_v = IPVERSION;
    571 	opack_ip->ip_hl = (sizeof(struct ip)+optlen) >> 2;
    572 	opack_ip->ip_tos = tos;
    573 	opack_ip->ip_off = (pingflags & F_DF) ? IP_DF : 0;
    574 	opack_ip->ip_ttl = ttl ? ttl : MAXTTL;
    575 	opack_ip->ip_p = IPPROTO_ICMP;
    576 	opack_ip->ip_src = src_addr.sin_addr;
    577 	opack_ip->ip_dst = send_addr.sin_addr;
    578 
    579 	if (pingflags & F_MCAST) {
    580 		if (pingflags & F_MCAST_NOLOOP) {
    581 			u_char loop = 0;
    582 			if (prog_setsockopt(s, IPPROTO_IP,
    583 			    IP_MULTICAST_LOOP,
    584 			    (char *) &loop, 1) < 0)
    585 				err(EXIT_FAILURE, "Can't disable multicast loopback");
    586 		}
    587 
    588 		if (ttl != 0
    589 		    && prog_setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
    590 		    (char *) &ttl, 1) < 0)
    591 			err(EXIT_FAILURE, "Can't set multicast time-to-live");
    592 
    593 		if ((pingflags & F_SOURCE_ADDR)
    594 		    && prog_setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
    595 				  (char *) &src_addr.sin_addr,
    596 				  sizeof(src_addr.sin_addr)) < 0)
    597 			err(EXIT_FAILURE, "Can't set multicast source interface");
    598 
    599 	} else if (pingflags & F_SOURCE_ADDR) {
    600 		if (prog_setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
    601 			       (char *) &src_addr.sin_addr,
    602 			       sizeof(src_addr.sin_addr)) < 0)
    603 			err(EXIT_FAILURE, "Can't set source interface/address");
    604 	}
    605 #ifdef IPSEC
    606 #ifdef IPSEC_POLICY_IPSEC
    607     {
    608 	char *buf;
    609 	if (pingflags & F_POLICY) {
    610 		if (policy_in != NULL) {
    611 			buf = ipsec_set_policy(policy_in, strlen(policy_in));
    612 			if (buf == NULL)
    613 				errx(EXIT_FAILURE, "%s", ipsec_strerror());
    614 			if (prog_setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY,
    615 					buf, ipsec_get_policylen(buf)) < 0) {
    616 				err(EXIT_FAILURE, "ipsec policy cannot be "
    617 				    "configured");
    618 			}
    619 			free(buf);
    620 		}
    621 		if (policy_out != NULL) {
    622 			buf = ipsec_set_policy(policy_out, strlen(policy_out));
    623 			if (buf == NULL)
    624 				errx(EXIT_FAILURE, "%s", ipsec_strerror());
    625 			if (prog_setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY,
    626 					buf, ipsec_get_policylen(buf)) < 0) {
    627 				err(EXIT_FAILURE, "ipsec policy cannot be "
    628 				    "configured");
    629 			}
    630 			free(buf);
    631 		}
    632 	}
    633 	buf = ipsec_set_policy("out bypass", strlen("out bypass"));
    634 	if (buf == NULL)
    635 		errx(EXIT_FAILURE, "%s", ipsec_strerror());
    636 	if (prog_setsockopt(sloop, IPPROTO_IP, IP_IPSEC_POLICY,
    637 			buf, ipsec_get_policylen(buf)) < 0) {
    638 #if 0
    639 		warnx("ipsec is not configured");
    640 #else
    641 		/* ignore it, should be okay */
    642 #endif
    643 	}
    644 	free(buf);
    645     }
    646 #else
    647     {
    648 	int optval;
    649 	if (pingflags & F_AUTHHDR) {
    650 		optval = IPSEC_LEVEL_REQUIRE;
    651 #ifdef IP_AUTH_TRANS_LEVEL
    652 		(void)prog_setsockopt(s, IPPROTO_IP, IP_AUTH_TRANS_LEVEL,
    653 			(char *)&optval, sizeof(optval));
    654 #else
    655 		(void)prog_setsockopt(s, IPPROTO_IP, IP_AUTH_LEVEL,
    656 			(char *)&optval, sizeof(optval));
    657 #endif
    658 	}
    659 	if (pingflags & F_ENCRYPT) {
    660 		optval = IPSEC_LEVEL_REQUIRE;
    661 		(void)prog_setsockopt(s, IPPROTO_IP, IP_ESP_TRANS_LEVEL,
    662 			(char *)&optval, sizeof(optval));
    663 	}
    664 	optval = IPSEC_LEVEL_BYPASS;
    665 #ifdef IP_AUTH_TRANS_LEVEL
    666 	(void)prog_setsockopt(sloop, IPPROTO_IP, IP_AUTH_TRANS_LEVEL,
    667 		(char *)&optval, sizeof(optval));
    668 #else
    669 	(void)prog_setsockopt(sloop, IPPROTO_IP, IP_AUTH_LEVEL,
    670 		(char *)&optval, sizeof(optval));
    671 #endif
    672 	(void)prog_setsockopt(sloop, IPPROTO_IP, IP_ESP_TRANS_LEVEL,
    673 		(char *)&optval, sizeof(optval));
    674     }
    675 #endif /*IPSEC_POLICY_IPSEC*/
    676 #endif /*IPSEC*/
    677 
    678 	(void)printf("PING %s (%s): %d data bytes\n", hostname,
    679 		     inet_ntoa(whereto.sin_addr), datalen);
    680 
    681 	/* When pinging the broadcast address, you can get a lot
    682 	 * of answers.  Doing something so evil is useful if you
    683 	 * are trying to stress the ethernet, or just want to
    684 	 * fill the arp cache to get some stuff for /etc/ethers.
    685 	 */
    686 	while (0 > prog_setsockopt(s, SOL_SOCKET, SO_RCVBUF,
    687 			      (char*)&bufspace, sizeof(bufspace))) {
    688 		if ((bufspace -= 4096) <= 0)
    689 			err(EXIT_FAILURE, "Cannot set the receive buffer size");
    690 	}
    691 
    692 	/* make it possible to send giant probes, but do not worry now
    693 	 * if it fails, since we probably won't send giant probes.
    694 	 */
    695 	(void)prog_setsockopt(s, SOL_SOCKET, SO_SNDBUF,
    696 			 (char*)&bufspace, sizeof(bufspace));
    697 
    698 	(void)signal(SIGINT, prefinish);
    699 
    700 	/*
    701 	 * Set up two signal masks:
    702 	 *    - blockmask blocks the signals we catch
    703 	 *    - enablemask does not
    704 	 */
    705 
    706 	sigemptyset(&enablemask);
    707 	sigemptyset(&blockmask);
    708 	sigaddset(&blockmask, SIGINT);
    709 #ifdef SIGINFO
    710 	sigaddset(&blockmask, SIGINFO);
    711 #else
    712 	sigaddset(&blockmask, SIGQUIT);
    713 #endif
    714 
    715 #ifdef SIGINFO
    716 	sa.sa_handler = prtsig;
    717 	sa.sa_flags = SA_NOKERNINFO;
    718 	sigemptyset(&sa.sa_mask);
    719 	(void)sigaction(SIGINFO, &sa, NULL);
    720 #else
    721 	(void)signal(SIGQUIT, prtsig);
    722 #endif
    723 	(void)signal(SIGCONT, prtsig);
    724 
    725 	blocksignals();
    726 
    727 	/* fire off them quickies */
    728 	for (i = 0; i < preload; i++) {
    729 		clock_gettime(CLOCK_MONOTONIC, &now);
    730 		pinger();
    731 	}
    732 
    733 	doit();
    734 	return 0;
    735 }
    736 
    737 static void
    738 doit(void)
    739 {
    740 	int cc;
    741 	struct sockaddr_in from;
    742 	socklen_t fromlen;
    743 	double sec, last, d_last;
    744 	struct pollfd fdmaskp[1];
    745 
    746 	(void)clock_gettime(CLOCK_MONOTONIC, &clear_cache);
    747 	if (maxwait != 0) {
    748 		last = timespec_to_sec(&clear_cache) + maxwait;
    749 		d_last = 0;
    750 	} else {
    751 		last = 0;
    752 		d_last = 365*24*60*60;
    753 	}
    754 
    755 	do {
    756 		clock_gettime(CLOCK_MONOTONIC, &now);
    757 
    758 		if (last != 0)
    759 			d_last = last - timespec_to_sec(&now);
    760 
    761 		if (ntransmitted < npackets && d_last > 0) {
    762 			/* send if within 100 usec or late for next packet */
    763 			sec = diffsec(&next_tx, &now);
    764 			if (sec <= 0.0001 ||
    765 			    (lastrcvd && (pingflags & F_FLOOD))) {
    766 				pinger();
    767 				sec = diffsec(&next_tx, &now);
    768 			}
    769 			if (sec < 0.0)
    770 				sec = 0.0;
    771 			if (d_last < sec)
    772 				sec = d_last;
    773 
    774 		} else {
    775 			/* For the last response, wait twice as long as the
    776 			 * worst case seen, or 10 times as long as the
    777 			 * maximum interpacket interval, whichever is longer.
    778 			 */
    779 			sec = MAX(2 * tmax, 10 * interval) -
    780 			    diffsec(&now, &last_tx);
    781 			if (d_last < sec)
    782 				sec = d_last;
    783 			if (sec <= 0)
    784 				break;
    785 		}
    786 
    787 
    788 		fdmaskp[0].fd = s;
    789 		fdmaskp[0].events = POLLIN;
    790 
    791 		enablesignals();
    792 		cc = prog_poll(fdmaskp, 1, (int)(sec * 1000));
    793 		blocksignals();
    794 
    795 		if (cc <= 0) {
    796 			if (cc < 0) {
    797 				if (errno == EINTR)
    798 					continue;
    799 				jiggle_flush(1);
    800 				err(EXIT_FAILURE, "poll");
    801 			}
    802 			continue;
    803 		}
    804 
    805 		fromlen  = sizeof(from);
    806 		cc = prog_recvfrom(s, (char *) packet, packlen,
    807 			      0, (struct sockaddr *)&from,
    808 			      &fromlen);
    809 		if (cc < 0) {
    810 			if (errno != EINTR) {
    811 				jiggle_flush(1);
    812 				warn("recvfrom");
    813 				(void)fflush(stderr);
    814 			}
    815 			continue;
    816 		}
    817 		clock_gettime(CLOCK_MONOTONIC, &now);
    818 		pr_pack(packet, cc, &from);
    819 
    820 	} while (nreceived < npackets
    821 		 && (nreceived == 0 || !(pingflags & F_ONCE)));
    822 
    823 	finish(0);
    824 }
    825 
    826 
    827 static void
    828 jiggle_flush(int nl)			/* new line if there are dots */
    829 {
    830 	int serrno = errno;
    831 
    832 	if (jiggle_cnt > 0) {
    833 		total_jiggled += jiggle_cnt;
    834 		jiggle_direction = 1;
    835 		do {
    836 			(void)putchar('.');
    837 		} while (--jiggle_cnt > 0);
    838 
    839 	} else if (jiggle_cnt < 0) {
    840 		total_jiggled -= jiggle_cnt;
    841 		jiggle_direction = -1;
    842 		do {
    843 			(void)putchar('\b');
    844 		} while (++jiggle_cnt < 0);
    845 	}
    846 
    847 	if (nl) {
    848 		if (total_jiggled != 0)
    849 			(void)putchar('\n');
    850 		total_jiggled = 0;
    851 		jiggle_direction = -1;
    852 	}
    853 
    854 	(void)fflush(stdout);
    855 	(void)fflush(stderr);
    856 	jiggle_time = now;
    857 	errno = serrno;
    858 }
    859 
    860 
    861 /* jiggle the cursor for flood-ping
    862  */
    863 static void
    864 jiggle(int delta)
    865 {
    866 	double dt;
    867 
    868 	if (pingflags & F_QUIET)
    869 		return;
    870 
    871 	/* do not back up into messages */
    872 	if (total_jiggled+jiggle_cnt+delta < 0)
    873 		return;
    874 
    875 	jiggle_cnt += delta;
    876 
    877 	/* flush the FLOOD dots when things are quiet
    878 	 * or occassionally to make the cursor jiggle.
    879 	 */
    880 	dt = diffsec(&last_tx, &jiggle_time);
    881 	if (dt > 0.2 || (dt >= 0.15 && delta*jiggle_direction < 0))
    882 		jiggle_flush(0);
    883 }
    884 
    885 
    886 /*
    887  * Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
    888  * will be added on by the kernel.  The ID field is our UNIX process ID,
    889  * and the sequence number is an ascending integer.  The first phdrlen bytes
    890  * of the data portion are used to hold a UNIX "timeval" struct in VAX
    891  * byte-order, to compute the round-trip time, or a UNIX "timespec" in native
    892  * format.
    893  */
    894 static void
    895 pinger(void)
    896 {
    897 	struct tv32 tv32;
    898 	int i, cc, sw;
    899 	double waittime;
    900 	long numskip;
    901 
    902 	opack_icmp.icmp_code = 0;
    903 	opack_icmp.icmp_seq = htons((u_int16_t)(ntransmitted));
    904 
    905 	/* clear the cached route in the kernel after an ICMP
    906 	 * response such as a Redirect is seen to stop causing
    907 	 * more such packets.  Also clear the cached route
    908 	 * periodically in case of routing changes that make
    909 	 * black holes come and go.
    910 	 */
    911 	if (clear_cache.tv_sec != now.tv_sec) {
    912 		opack_icmp.icmp_type = ICMP_ECHOREPLY;
    913 		opack_icmp.icmp_id = ~ident;
    914 		opack_icmp.icmp_cksum = 0;
    915 		opack_icmp.icmp_cksum = in_cksum((u_int16_t *)&opack_icmp,
    916 		    phdrlen);
    917 		sw = 0;
    918 		if (prog_setsockopt(sloop, IPPROTO_IP, IP_HDRINCL,
    919 			       (char *)&sw, sizeof(sw)) < 0)
    920 			err(EXIT_FAILURE, "Can't turn off special IP header");
    921 		if (prog_sendto(sloop, (char *) &opack_icmp,
    922 			   ICMP_MINLEN, MSG_DONTROUTE,
    923 			   (struct sockaddr *)&loc_addr,
    924 			   sizeof(struct sockaddr_in)) < 0) {
    925 			/*
    926 			 * XXX: we only report this as a warning in verbose
    927 			 * mode because people get confused when they see
    928 			 * this error when they are running in single user
    929 			 * mode and they have not configured lo0
    930 			 */
    931 			if (pingflags & F_VERBOSE)
    932 				warn("failed to clear cached route");
    933 		}
    934 		sw = 1;
    935 		if (prog_setsockopt(sloop, IPPROTO_IP, IP_HDRINCL,
    936 			       (char *)&sw, sizeof(sw)) < 0)
    937 			err(EXIT_FAILURE, "Can't set special IP header");
    938 
    939 		(void)clock_gettime(CLOCK_MONOTONIC, &clear_cache);
    940 	}
    941 
    942 	opack_icmp.icmp_type = ICMP_ECHO;
    943 	opack_icmp.icmp_id = ident;
    944 
    945 	if (pingflags & F_TIMING) {
    946 		tv32.tv32_sec = (uint32_t)htonl(now.tv_sec);
    947 		tv32.tv32_usec = htonl(now.tv_nsec / 1000);
    948 		(void) memcpy(&opack_icmp.icmp_data[0], &tv32, sizeof(tv32));
    949 	} else if (pingflags & F_TIMING64)
    950 		(void) memcpy(&opack_icmp.icmp_data[0], &now, sizeof(now));
    951 
    952 	cc = MAX(datalen, ICMP_MINLEN) + PHDR_LEN;
    953 	opack_icmp.icmp_cksum = 0;
    954 	opack_icmp.icmp_cksum = in_cksum((u_int16_t *)&opack_icmp, cc);
    955 
    956 	cc += opack_ip->ip_hl<<2;
    957 	opack_ip->ip_len = cc;
    958 	i = prog_sendto(s, (char *) opack_ip, cc, 0,
    959 		   (struct sockaddr *)&send_addr, sizeof(struct sockaddr_in));
    960 	if (i != cc) {
    961 		jiggle_flush(1);
    962 		if (i < 0)
    963 			warn("sendto");
    964 		else
    965 			warnx("wrote %s %d chars, ret=%d", hostname, cc, i);
    966 		(void)fflush(stderr);
    967 	}
    968 	lastrcvd = 0;
    969 
    970 	CLR(ntransmitted);
    971 	ntransmitted++;
    972 
    973 	last_tx = now;
    974 	if (next_tx.tv_sec == 0) {
    975 		first_tx = now;
    976 		next_tx = now;
    977 	}
    978 
    979 	/* Transmit regularly, at always the same microsecond in the
    980 	 * second when going at one packet per second.
    981 	 * If we are at most 100 ms behind, send extras to get caught up.
    982 	 * Otherwise, skip packets we were too slow to send.
    983 	 */
    984 	waittime = diffsec(&next_tx, &now);
    985 	if (waittime < -1.0) {
    986 		/* very behind - forget about being precise */
    987 		next_tx.tv_sec += (int)(-waittime);
    988 	} else if (waittime < -0.1) {
    989 		/* behind - skip a few */
    990 		if (interval_tv.tv_sec == 0) {
    991 			numskip = (long)(-waittime / interval_tv.tv_nsec);
    992 			next_tx.tv_nsec += numskip * interval_tv.tv_nsec;
    993 			/*
    994 			 * We can add at most one second's worth, but allow
    995 			 * for tv_nsec reaching 2 billion just in case FP
    996 			 * issues strike.
    997 			 */
    998 			while (next_tx.tv_nsec >= 1000000000) {
    999 				next_tx.tv_sec++;
   1000 				next_tx.tv_nsec -= 1000000000;
   1001 			}
   1002 		} else {
   1003 			do {
   1004 				timespecadd(&next_tx, &interval_tv, &next_tx);
   1005 			} while (diffsec(&next_tx, &now) < -0.1);
   1006 		}
   1007 
   1008 	} else if (waittime <= interval) {
   1009 		timespecadd(&next_tx, &interval_tv, &next_tx);
   1010 	}
   1011 
   1012 	if (pingflags & F_FLOOD)
   1013 		jiggle(1);
   1014 
   1015 	/* While the packet is going out, ready buffer for the next
   1016 	 * packet. Use a fast but not very good random number generator.
   1017 	 */
   1018 	if (pingflags & F_PING_RANDOM)
   1019 		rnd_fill();
   1020 }
   1021 
   1022 
   1023 static void
   1024 pr_pack_sub(int cc,
   1025 	    char *addr,
   1026 	    int seqno,
   1027 	    int dupflag,
   1028 	    int ttl,
   1029 	    double triptime)
   1030 {
   1031 	jiggle_flush(1);
   1032 
   1033 	if (pingflags & F_FLOOD)
   1034 		return;
   1035 
   1036 	(void)printf("%d bytes from %s: icmp_seq=%u", cc, addr, seqno);
   1037 	if (dupflag)
   1038 		(void)printf(" DUP!");
   1039 	(void)printf(" ttl=%d", ttl);
   1040 	if (pingflags & (F_TIMING|F_TIMING64)) {
   1041 		const unsigned int prec = (pingflags & F_TIMING64) != 0 ? 6 : 3;
   1042 
   1043 		(void)printf(" time=%.*f ms", prec, triptime*1000.0);
   1044 	}
   1045 
   1046 	/*
   1047 	 * Send beep to stderr, since that's more likely than stdout
   1048 	 * to go to a terminal..
   1049 	 */
   1050 	if (pingflags & F_AUDIBLE && !dupflag)
   1051 		(void)fprintf(stderr,"\a");
   1052 }
   1053 
   1054 
   1055 /*
   1056  * Print out the packet, if it came from us.  This logic is necessary
   1057  * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
   1058  * which arrive ('tis only fair).  This permits multiple copies of this
   1059  * program to be run without having intermingled output (or statistics!).
   1060  */
   1061 static void
   1062 pr_pack(u_char *buf,
   1063 	int tot_len,
   1064 	struct sockaddr_in *from)
   1065 {
   1066 	struct ip *ip;
   1067 	struct icmp *icp;
   1068 	int i, j, net_len;
   1069 	u_char *cp;
   1070 	static int old_rrlen;
   1071 	static char old_rr[MAX_IPOPTLEN];
   1072 	int hlen, dupflag = 0, dumped;
   1073 	double triptime = 0.0;
   1074 #define PR_PACK_SUB() {if (!dumped) {			\
   1075 	dumped = 1;					\
   1076 	pr_pack_sub(net_len, inet_ntoa(from->sin_addr),	\
   1077 		    ntohs((u_int16_t)icp->icmp_seq),	\
   1078 		    dupflag, ip->ip_ttl, triptime);}}
   1079 
   1080 	/* Check the IP header */
   1081 	ip = (struct ip *) buf;
   1082 	hlen = ip->ip_hl << 2;
   1083 	if (tot_len < hlen + ICMP_MINLEN) {
   1084 		if (pingflags & F_VERBOSE) {
   1085 			jiggle_flush(1);
   1086 			(void)printf("packet too short (%d bytes) from %s\n",
   1087 				     tot_len, inet_ntoa(from->sin_addr));
   1088 		}
   1089 		return;
   1090 	}
   1091 
   1092 	/* Now the ICMP part */
   1093 	dumped = 0;
   1094 	net_len = tot_len - hlen;
   1095 	icp = (struct icmp *)(buf + hlen);
   1096 	if (icp->icmp_type == ICMP_ECHOREPLY
   1097 	    && icp->icmp_id == ident) {
   1098 
   1099 		if (icp->icmp_seq == htons((u_int16_t)(ntransmitted-1)))
   1100 			lastrcvd = 1;
   1101 		last_rx = now;
   1102 		if (first_rx.tv_sec == 0)
   1103 			first_rx = last_rx;
   1104 		nreceived++;
   1105 		if (pingflags & (F_TIMING|F_TIMING64)) {
   1106 			struct timespec tv;
   1107 
   1108 			if (pingflags & F_TIMING) {
   1109 				struct tv32 tv32;
   1110 
   1111 				(void)memcpy(&tv32, icp->icmp_data, sizeof(tv32));
   1112 				tv.tv_sec = (uint32_t)ntohl(tv32.tv32_sec);
   1113 				tv.tv_nsec = ntohl(tv32.tv32_usec) * 1000;
   1114 			} else if (pingflags & F_TIMING64)
   1115 				(void)memcpy(&tv, icp->icmp_data, sizeof(tv));
   1116 			else
   1117 				memset(&tv, 0, sizeof(tv));	/* XXX: gcc */
   1118 
   1119 			triptime = diffsec(&last_rx, &tv);
   1120 			tsum += triptime;
   1121 			tsumsq += triptime * triptime;
   1122 			if (triptime < tmin)
   1123 				tmin = triptime;
   1124 			if (triptime > tmax)
   1125 				tmax = triptime;
   1126 		}
   1127 
   1128 		if (TST(ntohs((u_int16_t)icp->icmp_seq))) {
   1129 			nrepeats++, nreceived--;
   1130 			dupflag=1;
   1131 		} else {
   1132 			SET(ntohs((u_int16_t)icp->icmp_seq));
   1133 		}
   1134 
   1135 		if (tot_len != opack_ip->ip_len) {
   1136 			PR_PACK_SUB();
   1137 			switch (opack_ip->ip_len - tot_len) {
   1138 			case MAX_IPOPTLEN:
   1139 				if ((pongflags & F_RECORD_ROUTE) != 0)
   1140 					break;
   1141 				if ((pingflags & F_RECORD_ROUTE) == 0)
   1142 					goto out;
   1143 				pongflags |= F_RECORD_ROUTE;
   1144 				(void)printf("\nremote host does not "
   1145 				    "support record route");
   1146 				break;
   1147 			case 8:
   1148 				if ((pongflags & F_SOURCE_ROUTE) != 0)
   1149 					break;
   1150 				if ((pingflags & F_SOURCE_ROUTE) == 0)
   1151 					goto out;
   1152 				pongflags |= F_SOURCE_ROUTE;
   1153 				(void)printf("\nremote host does not "
   1154 				    "support source route");
   1155 				break;
   1156 			default:
   1157 			out:
   1158 				(void)printf("\nwrong total length %d "
   1159 				    "instead of %d", tot_len, opack_ip->ip_len);
   1160 				break;
   1161 			}
   1162 		}
   1163 
   1164 		if (!dupflag) {
   1165 			static u_int16_t last_seqno = 0xffff;
   1166 			u_int16_t seqno = ntohs((u_int16_t)icp->icmp_seq);
   1167 			u_int16_t gap = seqno - (last_seqno + 1);
   1168 			if (gap > 0 && gap < 0x8000 &&
   1169 			    (pingflags & F_VERBOSE)) {
   1170 				(void)printf("[*** sequence gap of %u "
   1171 				    "packets from %u ... %u ***]\n", gap,
   1172 				    (u_int16_t) (last_seqno + 1),
   1173 				    (u_int16_t) (seqno - 1));
   1174 				if (pingflags & F_QUIET)
   1175 					summary(0);
   1176 			}
   1177 
   1178 			if (gap < 0x8000)
   1179 				last_seqno = seqno;
   1180 		}
   1181 
   1182 		if (pingflags & F_QUIET)
   1183 			return;
   1184 
   1185 		if (!(pingflags & F_FLOOD))
   1186 			PR_PACK_SUB();
   1187 
   1188 		/* check the data */
   1189 		if ((size_t)(tot_len - hlen) >
   1190 		    offsetof(struct icmp, icmp_data) + datalen
   1191 		    && !(pingflags & F_PING_RANDOM)
   1192 		    && memcmp(icp->icmp_data + phdrlen,
   1193 			    opack_icmp.icmp_data + phdrlen,
   1194 			    datalen - phdrlen)) {
   1195 			for (i = phdrlen; i < datalen; i++) {
   1196 				if (icp->icmp_data[i] !=
   1197 				    opack_icmp.icmp_data[i])
   1198 					break;
   1199 			}
   1200 			PR_PACK_SUB();
   1201 			(void)printf("\nwrong data byte #%d should have been"
   1202 				     " %#x but was %#x", i - phdrlen,
   1203 				     (u_char)opack_icmp.icmp_data[i],
   1204 				     (u_char)icp->icmp_data[i]);
   1205 			for (i = phdrlen; i < datalen; i++) {
   1206 				if ((i % 16) == 0)
   1207 					(void)printf("\n\t");
   1208 				(void)printf("%2x ",(u_char)icp->icmp_data[i]);
   1209 			}
   1210 		}
   1211 
   1212 	} else {
   1213 		if (!pr_icmph(icp, from, net_len))
   1214 			return;
   1215 		dumped = 2;
   1216 	}
   1217 
   1218 	/* Display any IP options */
   1219 	cp = buf + sizeof(struct ip);
   1220 	while (hlen > (int)sizeof(struct ip)) {
   1221 		switch (*cp) {
   1222 		case IPOPT_EOL:
   1223 			hlen = 0;
   1224 			break;
   1225 		case IPOPT_LSRR:
   1226 			hlen -= 2;
   1227 			j = *++cp;
   1228 			++cp;
   1229 			j -= IPOPT_MINOFF;
   1230 			if (j <= 0)
   1231 				continue;
   1232 			if (dumped <= 1) {
   1233 				j = ((j+3)/4)*4;
   1234 				hlen -= j;
   1235 				cp += j;
   1236 				break;
   1237 			}
   1238 			PR_PACK_SUB();
   1239 			(void)printf("\nLSRR: ");
   1240 			for (;;) {
   1241 				pr_saddr(cp);
   1242 				cp += 4;
   1243 				hlen -= 4;
   1244 				j -= 4;
   1245 				if (j <= 0)
   1246 					break;
   1247 				(void)putchar('\n');
   1248 			}
   1249 			break;
   1250 		case IPOPT_RR:
   1251 			j = *++cp;	/* get length */
   1252 			i = *++cp;	/* and pointer */
   1253 			hlen -= 2;
   1254 			if (i > j)
   1255 				i = j;
   1256 			i -= IPOPT_MINOFF;
   1257 			if (i <= 0)
   1258 				continue;
   1259 			if (dumped <= 1) {
   1260 				if (i == old_rrlen
   1261 				    && !memcmp(cp, old_rr, i)) {
   1262 					if (dumped)
   1263 					    (void)printf("\t(same route)");
   1264 					j = ((i+3)/4)*4;
   1265 					hlen -= j;
   1266 					cp += j;
   1267 					break;
   1268 				}
   1269 				old_rrlen = i;
   1270 				(void) memcpy(old_rr, cp, i);
   1271 			}
   1272 			if (!dumped) {
   1273 				jiggle_flush(1);
   1274 				(void)printf("RR: ");
   1275 				dumped = 1;
   1276 			} else {
   1277 				(void)printf("\nRR: ");
   1278 			}
   1279 			for (;;) {
   1280 				pr_saddr(cp);
   1281 				cp += 4;
   1282 				hlen -= 4;
   1283 				i -= 4;
   1284 				if (i <= 0)
   1285 					break;
   1286 				(void)putchar('\n');
   1287 			}
   1288 			break;
   1289 		case IPOPT_NOP:
   1290 			if (dumped <= 1)
   1291 				break;
   1292 			PR_PACK_SUB();
   1293 			(void)printf("\nNOP");
   1294 			break;
   1295 #ifdef sgi
   1296 		case IPOPT_SECURITY:	/* RFC 1108 RIPSO BSO */
   1297 		case IPOPT_ESO:		/* RFC 1108 RIPSO ESO */
   1298 		case IPOPT_CIPSO:	/* Commercial IPSO */
   1299 			if ((sysconf(_SC_IP_SECOPTS)) > 0) {
   1300 				i = (unsigned)cp[1];
   1301 				hlen -= i - 1;
   1302 				PR_PACK_SUB();
   1303 				(void)printf("\nSEC:");
   1304 				while (i--) {
   1305 					(void)printf(" %02x", *cp++);
   1306 				}
   1307 				cp--;
   1308 				break;
   1309 			}
   1310 #endif
   1311 		default:
   1312 			PR_PACK_SUB();
   1313 			(void)printf("\nunknown option 0x%x", *cp);
   1314 			break;
   1315 		}
   1316 		hlen--;
   1317 		cp++;
   1318 	}
   1319 
   1320 	if (dumped) {
   1321 		(void)putchar('\n');
   1322 		(void)fflush(stdout);
   1323 	} else {
   1324 		jiggle(-1);
   1325 	}
   1326 }
   1327 
   1328 
   1329 /* Compute the IP checksum
   1330  *	This assumes the packet is less than 32K long.
   1331  */
   1332 static u_int16_t
   1333 in_cksum(u_int16_t *p, u_int len)
   1334 {
   1335 	u_int32_t sum = 0;
   1336 	int nwords = len >> 1;
   1337 
   1338 	while (nwords-- != 0)
   1339 		sum += *p++;
   1340 
   1341 	if (len & 1) {
   1342 		union {
   1343 			u_int16_t w;
   1344 			u_int8_t c[2];
   1345 		} u;
   1346 		u.c[0] = *(u_char *)p;
   1347 		u.c[1] = 0;
   1348 		sum += u.w;
   1349 	}
   1350 
   1351 	/* end-around-carry */
   1352 	sum = (sum >> 16) + (sum & 0xffff);
   1353 	sum += (sum >> 16);
   1354 	return (~sum);
   1355 }
   1356 
   1357 
   1358 /*
   1359  * compute the difference of two timespecs in seconds
   1360  */
   1361 static double
   1362 diffsec(struct timespec *timenow,
   1363 	struct timespec *then)
   1364 {
   1365 	if (timenow->tv_sec == 0)
   1366 		return -1;
   1367 	return (timenow->tv_sec - then->tv_sec)
   1368 	    * 1.0 + (timenow->tv_nsec - then->tv_nsec) / 1000000000.0;
   1369 }
   1370 
   1371 
   1372 #if 0
   1373 static void
   1374 timespecadd(struct timespec *t1,
   1375 	   struct timespec *t2)
   1376 {
   1377 
   1378 	t1->tv_sec += t2->tv_sec;
   1379 	if ((t1->tv_nsec += t2->tv_nsec) >= 1000000000) {
   1380 		t1->tv_sec++;
   1381 		t1->tv_nsec -= 1000000000;
   1382 	}
   1383 }
   1384 #endif
   1385 
   1386 
   1387 static void
   1388 sec_to_timespec(const double sec, struct timespec *tp)
   1389 {
   1390 	tp->tv_sec = sec;
   1391 	tp->tv_nsec = (sec - tp->tv_sec) * 1000000000.0;
   1392 }
   1393 
   1394 
   1395 static double
   1396 timespec_to_sec(const struct timespec *tp)
   1397 {
   1398 	return tp->tv_sec + tp->tv_nsec / 1000000000.0;
   1399 }
   1400 
   1401 
   1402 /*
   1403  * Print statistics.
   1404  * Heavily buffered STDIO is used here, so that all the statistics
   1405  * will be written with 1 sys-write call.  This is nice when more
   1406  * than one copy of the program is running on a terminal;  it prevents
   1407  * the statistics output from becomming intermingled.
   1408  */
   1409 static void
   1410 summary(int header)
   1411 {
   1412 	jiggle_flush(1);
   1413 
   1414 	if (header)
   1415 		(void)printf("\n----%s PING Statistics----\n", hostname);
   1416 	(void)printf("%d packets transmitted, ", ntransmitted);
   1417 	(void)printf("%d packets received, ", nreceived);
   1418 	if (nrepeats)
   1419 		(void)printf("+%d duplicates, ", nrepeats);
   1420 	if (ntransmitted) {
   1421 		if (nreceived > ntransmitted)
   1422 			(void)printf("-- somebody's duplicating packets!");
   1423 		else
   1424 			(void)printf("%.1f%% packet loss",
   1425 				     (((ntransmitted-nreceived)*100.0) /
   1426 					    ntransmitted));
   1427 	}
   1428 	(void)printf("\n");
   1429 	if (nreceived && (pingflags & (F_TIMING|F_TIMING64))) {
   1430 		double n = nreceived + nrepeats;
   1431 		double avg = (tsum / n);
   1432 		double variance = 0.0;
   1433 		const unsigned int prec = (pingflags & F_TIMING64) != 0 ? 6 : 3;
   1434 		if (n>1)
   1435 			variance = (tsumsq - n*avg*avg) /(n-1);
   1436 
   1437 		(void)printf("round-trip min/avg/max/stddev = "
   1438 			"%.*f/%.*f/%.*f/%.*f ms\n",
   1439 			prec, tmin * 1000.0,
   1440 			prec, avg * 1000.0,
   1441 			prec, tmax * 1000.0,
   1442 			prec, sqrt(variance) * 1000.0);
   1443 		if (pingflags & F_FLOOD) {
   1444 			double r = diffsec(&last_rx, &first_rx);
   1445 			double t = diffsec(&last_tx, &first_tx);
   1446 			if (r == 0)
   1447 				r = 0.0001;
   1448 			if (t == 0)
   1449 				t = 0.0001;
   1450 			(void)printf("  %.1f packets/sec sent, "
   1451 				     " %.1f packets/sec received\n",
   1452 				     ntransmitted/t, nreceived/r);
   1453 		}
   1454 	}
   1455 }
   1456 
   1457 
   1458 /*
   1459  * Print statistics when SIGINFO is received.
   1460  */
   1461 /* ARGSUSED */
   1462 static void
   1463 prtsig(int dummy)
   1464 {
   1465 
   1466 	summary(0);
   1467 #ifndef SIGINFO
   1468 	(void)signal(SIGQUIT, prtsig);
   1469 #endif
   1470 }
   1471 
   1472 
   1473 /*
   1474  * On the first SIGINT, allow any outstanding packets to dribble in
   1475  */
   1476 static void
   1477 prefinish(int sig)
   1478 {
   1479 	if (lastrcvd			/* quit now if caught up */
   1480 	    || nreceived == 0)		/* or if remote is dead */
   1481 		finish(0);
   1482 
   1483 	(void)signal(sig, finish);	/* do this only the 1st time */
   1484 
   1485 	if (npackets > ntransmitted)	/* let the normal limit work */
   1486 		npackets = ntransmitted;
   1487 }
   1488 
   1489 /*
   1490  * Print statistics and give up.
   1491  */
   1492 /* ARGSUSED */
   1493 static void
   1494 finish(int dummy)
   1495 {
   1496 #ifdef SIGINFO
   1497 	(void)signal(SIGINFO, SIG_DFL);
   1498 #else
   1499 	(void)signal(SIGQUIT, SIG_DFL);
   1500 #endif
   1501 
   1502 	summary(1);
   1503 	exit(nreceived > 0 ? 0 : 2);
   1504 }
   1505 
   1506 static void
   1507 blocksignals(void)
   1508 {
   1509 	if (sigprocmask(SIG_SETMASK, &blockmask, NULL) == -1) {
   1510 		err(EXIT_FAILURE, "blocksignals: sigprocmask");
   1511 	}
   1512 }
   1513 
   1514 static void
   1515 enablesignals(void)
   1516 {
   1517 	if (sigprocmask(SIG_SETMASK, &enablemask, NULL) == -1) {
   1518 		err(EXIT_FAILURE, "enablesignals: sigprocmask");
   1519 	}
   1520 }
   1521 
   1522 
   1523 static int				/* 0=do not print it */
   1524 ck_pr_icmph(struct icmp *icp,
   1525 	    struct sockaddr_in *from,
   1526 	    int cc,
   1527 	    int override)		/* 1=override VERBOSE if interesting */
   1528 {
   1529 	int	hlen;
   1530 	struct ip ipb, *ip = &ipb;
   1531 	struct icmp icp2b, *icp2 = &icp2b;
   1532 	int res;
   1533 
   1534 	if (pingflags & F_VERBOSE) {
   1535 		res = 1;
   1536 		jiggle_flush(1);
   1537 	} else {
   1538 		res = 0;
   1539 	}
   1540 
   1541 	(void) memcpy(ip, icp->icmp_data, sizeof(*ip));
   1542 	hlen = ip->ip_hl << 2;
   1543 	if (ip->ip_p == IPPROTO_ICMP
   1544 	    && hlen + 6 <= cc) {
   1545 		(void) memcpy(icp2, &icp->icmp_data[hlen], sizeof(*icp2));
   1546 		if (icp2->icmp_id == ident) {
   1547 			/* remember to clear route cached in kernel
   1548 			 * if this non-Echo-Reply ICMP message was for one
   1549 			 * of our packets.
   1550 			 */
   1551 			clear_cache.tv_sec = 0;
   1552 
   1553 			if (!res && override
   1554 			    && (pingflags & (F_QUIET|F_SEMI_QUIET)) == 0) {
   1555 				jiggle_flush(1);
   1556 				(void)printf("%d bytes from %s: ",
   1557 					     cc, pr_addr(&from->sin_addr));
   1558 				res = 1;
   1559 			}
   1560 		}
   1561 	}
   1562 
   1563 	return res;
   1564 }
   1565 
   1566 
   1567 /*
   1568  *  Print a descriptive string about an ICMP header other than an echo reply.
   1569  */
   1570 static int				/* 0=printed nothing */
   1571 pr_icmph(struct icmp *icp,
   1572 	 struct sockaddr_in *from,
   1573 	 int cc)
   1574 {
   1575 	switch (icp->icmp_type ) {
   1576 	case ICMP_UNREACH:
   1577 		if (!ck_pr_icmph(icp, from, cc, 1))
   1578 			return 0;
   1579 		switch (icp->icmp_code) {
   1580 		case ICMP_UNREACH_NET:
   1581 			(void)printf("Destination Net Unreachable");
   1582 			break;
   1583 		case ICMP_UNREACH_HOST:
   1584 			(void)printf("Destination Host Unreachable");
   1585 			break;
   1586 		case ICMP_UNREACH_PROTOCOL:
   1587 			(void)printf("Destination Protocol Unreachable");
   1588 			break;
   1589 		case ICMP_UNREACH_PORT:
   1590 			(void)printf("Destination Port Unreachable");
   1591 			break;
   1592 		case ICMP_UNREACH_NEEDFRAG:
   1593 			(void)printf("frag needed and DF set.  Next MTU=%d",
   1594 			       ntohs(icp->icmp_nextmtu));
   1595 			break;
   1596 		case ICMP_UNREACH_SRCFAIL:
   1597 			(void)printf("Source Route Failed");
   1598 			break;
   1599 		case ICMP_UNREACH_NET_UNKNOWN:
   1600 			(void)printf("Unreachable unknown net");
   1601 			break;
   1602 		case ICMP_UNREACH_HOST_UNKNOWN:
   1603 			(void)printf("Unreachable unknown host");
   1604 			break;
   1605 		case ICMP_UNREACH_ISOLATED:
   1606 			(void)printf("Unreachable host isolated");
   1607 			break;
   1608 		case ICMP_UNREACH_NET_PROHIB:
   1609 			(void)printf("Net prohibited access");
   1610 			break;
   1611 		case ICMP_UNREACH_HOST_PROHIB:
   1612 			(void)printf("Host prohibited access");
   1613 			break;
   1614 		case ICMP_UNREACH_TOSNET:
   1615 			(void)printf("Bad TOS for net");
   1616 			break;
   1617 		case ICMP_UNREACH_TOSHOST:
   1618 			(void)printf("Bad TOS for host");
   1619 			break;
   1620 		case 13:
   1621 			(void)printf("Communication prohibited");
   1622 			break;
   1623 		case 14:
   1624 			(void)printf("Host precedence violation");
   1625 			break;
   1626 		case 15:
   1627 			(void)printf("Precedence cutoff");
   1628 			break;
   1629 		default:
   1630 			(void)printf("Bad Destination Unreachable Code: %d",
   1631 				     icp->icmp_code);
   1632 			break;
   1633 		}
   1634 		/* Print returned IP header information */
   1635 		pr_retip(icp, cc);
   1636 		break;
   1637 
   1638 	case ICMP_SOURCEQUENCH:
   1639 		if (!ck_pr_icmph(icp, from, cc, 1))
   1640 			return 0;
   1641 		(void)printf("Source Quench");
   1642 		pr_retip(icp, cc);
   1643 		break;
   1644 
   1645 	case ICMP_REDIRECT:
   1646 		if (!ck_pr_icmph(icp, from, cc, 1))
   1647 			return 0;
   1648 		switch (icp->icmp_code) {
   1649 		case ICMP_REDIRECT_NET:
   1650 			(void)printf("Redirect Network");
   1651 			break;
   1652 		case ICMP_REDIRECT_HOST:
   1653 			(void)printf("Redirect Host");
   1654 			break;
   1655 		case ICMP_REDIRECT_TOSNET:
   1656 			(void)printf("Redirect Type of Service and Network");
   1657 			break;
   1658 		case ICMP_REDIRECT_TOSHOST:
   1659 			(void)printf("Redirect Type of Service and Host");
   1660 			break;
   1661 		default:
   1662 			(void)printf("Redirect--Bad Code: %d", icp->icmp_code);
   1663 			break;
   1664 		}
   1665 		(void)printf(" New router addr: %s",
   1666 			     pr_addr(&icp->icmp_hun.ih_gwaddr));
   1667 		pr_retip(icp, cc);
   1668 		break;
   1669 
   1670 	case ICMP_ECHO:
   1671 		if (!ck_pr_icmph(icp, from, cc, 0))
   1672 			return 0;
   1673 		(void)printf("Echo Request: ID=%d seq=%d",
   1674 			     ntohs(icp->icmp_id), ntohs(icp->icmp_seq));
   1675 		break;
   1676 
   1677 	case ICMP_ECHOREPLY:
   1678 		/* displaying other's pings is too noisey */
   1679 #if 0
   1680 		if (!ck_pr_icmph(icp, from, cc, 0))
   1681 			return 0;
   1682 		(void)printf("Echo Reply: ID=%d seq=%d",
   1683 			     ntohs(icp->icmp_id), ntohs(icp->icmp_seq));
   1684 		break;
   1685 #else
   1686 		return 0;
   1687 #endif
   1688 
   1689 	case ICMP_ROUTERADVERT:
   1690 		if (!ck_pr_icmph(icp, from, cc, 0))
   1691 			return 0;
   1692 		(void)printf("Router Discovery Advert");
   1693 		break;
   1694 
   1695 	case ICMP_ROUTERSOLICIT:
   1696 		if (!ck_pr_icmph(icp, from, cc, 0))
   1697 			return 0;
   1698 		(void)printf("Router Discovery Solicit");
   1699 		break;
   1700 
   1701 	case ICMP_TIMXCEED:
   1702 		if (!ck_pr_icmph(icp, from, cc, 1))
   1703 			return 0;
   1704 		switch (icp->icmp_code ) {
   1705 		case ICMP_TIMXCEED_INTRANS:
   1706 			(void)printf("Time To Live exceeded");
   1707 			break;
   1708 		case ICMP_TIMXCEED_REASS:
   1709 			(void)printf("Frag reassembly time exceeded");
   1710 			break;
   1711 		default:
   1712 			(void)printf("Time exceeded, Bad Code: %d",
   1713 				     icp->icmp_code);
   1714 			break;
   1715 		}
   1716 		pr_retip(icp, cc);
   1717 		break;
   1718 
   1719 	case ICMP_PARAMPROB:
   1720 		if (!ck_pr_icmph(icp, from, cc, 1))
   1721 			return 0;
   1722 		(void)printf("Parameter problem: pointer = 0x%02x",
   1723 			     icp->icmp_hun.ih_pptr);
   1724 		pr_retip(icp, cc);
   1725 		break;
   1726 
   1727 	case ICMP_TSTAMP:
   1728 		if (!ck_pr_icmph(icp, from, cc, 0))
   1729 			return 0;
   1730 		(void)printf("Timestamp");
   1731 		break;
   1732 
   1733 	case ICMP_TSTAMPREPLY:
   1734 		if (!ck_pr_icmph(icp, from, cc, 0))
   1735 			return 0;
   1736 		(void)printf("Timestamp Reply");
   1737 		break;
   1738 
   1739 	case ICMP_IREQ:
   1740 		if (!ck_pr_icmph(icp, from, cc, 0))
   1741 			return 0;
   1742 		(void)printf("Information Request");
   1743 		break;
   1744 
   1745 	case ICMP_IREQREPLY:
   1746 		if (!ck_pr_icmph(icp, from, cc, 0))
   1747 			return 0;
   1748 		(void)printf("Information Reply");
   1749 		break;
   1750 
   1751 	case ICMP_MASKREQ:
   1752 		if (!ck_pr_icmph(icp, from, cc, 0))
   1753 			return 0;
   1754 		(void)printf("Address Mask Request");
   1755 		break;
   1756 
   1757 	case ICMP_MASKREPLY:
   1758 		if (!ck_pr_icmph(icp, from, cc, 0))
   1759 			return 0;
   1760 		(void)printf("Address Mask Reply");
   1761 		break;
   1762 
   1763 	default:
   1764 		if (!ck_pr_icmph(icp, from, cc, 0))
   1765 			return 0;
   1766 		(void)printf("Bad ICMP type: %d", icp->icmp_type);
   1767 		if (pingflags & F_VERBOSE)
   1768 			pr_iph(icp, cc);
   1769 	}
   1770 
   1771 	return 1;
   1772 }
   1773 
   1774 
   1775 /*
   1776  *  Print an IP header with options.
   1777  */
   1778 static void
   1779 pr_iph(struct icmp *icp,
   1780        int cc)
   1781 {
   1782 	int	hlen;
   1783 	u_char	*cp;
   1784 	struct ip __aligned(4) ipb;
   1785 	struct ip *ip = &ipb;
   1786 
   1787 	(void) memcpy(ip, icp->icmp_data, sizeof(*ip));
   1788 
   1789 	hlen = ip->ip_hl << 2;
   1790 	cp = (u_char *) &icp->icmp_data[20];	/* point to options */
   1791 
   1792 	(void)printf("\n Vr HL TOS  Len   ID Flg  off TTL Pro  cks      Src	     Dst\n");
   1793 	(void)printf("  %1x  %1x  %02x %04x %04x",
   1794 		     ip->ip_v, ip->ip_hl, ip->ip_tos, ip->ip_len, ip->ip_id);
   1795 	(void)printf("   %1x %04x",
   1796 		     ((ip->ip_off)&0xe000)>>13, (ip->ip_off)&0x1fff);
   1797 	(void)printf("  %02x  %02x %04x",
   1798 		     ip->ip_ttl, ip->ip_p, ip->ip_sum);
   1799 	(void)printf(" %15s ",
   1800 		     inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr));
   1801 	(void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr));
   1802 	/* dump any option bytes */
   1803 	while (hlen-- > 20 && cp < (u_char*)icp+cc) {
   1804 		(void)printf("%02x", *cp++);
   1805 	}
   1806 }
   1807 
   1808 /*
   1809  * Print an ASCII host address starting from a string of bytes.
   1810  */
   1811 static void
   1812 pr_saddr(u_char *cp)
   1813 {
   1814 	n_long l;
   1815 	struct in_addr addr;
   1816 
   1817 	l = (u_char)*++cp;
   1818 	l = (l<<8) + (u_char)*++cp;
   1819 	l = (l<<8) + (u_char)*++cp;
   1820 	l = (l<<8) + (u_char)*++cp;
   1821 	addr.s_addr = htonl(l);
   1822 	(void)printf("\t%s", (l == 0) ? "0.0.0.0" : pr_addr(&addr));
   1823 }
   1824 
   1825 
   1826 /*
   1827  *  Return an ASCII host address
   1828  *  as a dotted quad and optionally with a hostname
   1829  */
   1830 static char *
   1831 pr_addr(struct in_addr *addr)		/* in network order */
   1832 {
   1833 	struct	hostent	*hp;
   1834 	static	char buf[MAXHOSTNAMELEN+4+16+1];
   1835 
   1836 	if ((pingflags & F_NUMERIC)
   1837 	    || !(hp = gethostbyaddr((char *)addr, sizeof(*addr), AF_INET))) {
   1838 		(void)snprintf(buf, sizeof(buf), "%s", inet_ntoa(*addr));
   1839 	} else {
   1840 		(void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name,
   1841 		    inet_ntoa(*addr));
   1842 	}
   1843 
   1844 	return buf;
   1845 }
   1846 
   1847 /*
   1848  *  Dump some info on a returned (via ICMP) IP packet.
   1849  */
   1850 static void
   1851 pr_retip(struct icmp *icp,
   1852 	 int cc)
   1853 {
   1854 	int	hlen;
   1855 	u_char	*cp;
   1856 	struct ip ipb, *ip = &ipb;
   1857 
   1858 	(void) memcpy(ip, icp->icmp_data, sizeof(*ip));
   1859 
   1860 	if (pingflags & F_VERBOSE)
   1861 		pr_iph(icp, cc);
   1862 
   1863 	hlen = ip->ip_hl << 2;
   1864 	cp = (u_char *) &icp->icmp_data[hlen];
   1865 
   1866 	if (ip->ip_p == IPPROTO_TCP) {
   1867 		if (pingflags & F_VERBOSE)
   1868 			(void)printf("\n  TCP: from port %u, to port %u",
   1869 				     (*cp*256+*(cp+1)), (*(cp+2)*256+*(cp+3)));
   1870 	} else if (ip->ip_p == IPPROTO_UDP) {
   1871 		if (pingflags & F_VERBOSE)
   1872 			(void)printf("\n  UDP: from port %u, to port %u",
   1873 				     (*cp*256+*(cp+1)), (*(cp+2)*256+*(cp+3)));
   1874 	} else if (ip->ip_p == IPPROTO_ICMP) {
   1875 		struct icmp icp2;
   1876 		(void) memcpy(&icp2, cp, sizeof(icp2));
   1877 		if (icp2.icmp_type == ICMP_ECHO) {
   1878 			if (pingflags & F_VERBOSE)
   1879 				(void)printf("\n  ID=%u icmp_seq=%u",
   1880 					     ntohs((u_int16_t)icp2.icmp_id),
   1881 					     ntohs((u_int16_t)icp2.icmp_seq));
   1882 			else
   1883 				(void)printf(" for icmp_seq=%u",
   1884 					     ntohs((u_int16_t)icp2.icmp_seq));
   1885 		}
   1886 	}
   1887 }
   1888 
   1889 static void
   1890 fill(void)
   1891 {
   1892 	int i, j, k;
   1893 	char *cp;
   1894 	int pat[16];
   1895 
   1896 	for (cp = fill_pat; *cp != '\0'; cp++) {
   1897 		if (!isxdigit((unsigned char)*cp))
   1898 			break;
   1899 	}
   1900 	if (cp == fill_pat || *cp != '\0' || (cp-fill_pat) > 16*2) {
   1901 		(void)fflush(stdout);
   1902 		errx(EXIT_FAILURE, "\"-p %s\": patterns must be specified with"
   1903 		     " 1-32 hex digits\n",
   1904 		     fill_pat);
   1905 	}
   1906 
   1907 	i = sscanf(fill_pat,
   1908 		   "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
   1909 		    &pat[0], &pat[1], &pat[2], &pat[3],
   1910 		    &pat[4], &pat[5], &pat[6], &pat[7],
   1911 		    &pat[8], &pat[9], &pat[10], &pat[11],
   1912 		    &pat[12], &pat[13], &pat[14], &pat[15]);
   1913 
   1914 	for (k = phdrlen, j = 0; k < datalen; k++) {
   1915 		opack_icmp.icmp_data[k] = pat[j];
   1916 		if (++j >= i)
   1917 			j = 0;
   1918 	}
   1919 
   1920 	if (!(pingflags & F_QUIET)) {
   1921 		(void)printf("PATTERN: 0x");
   1922 		for (j=0; j<i; j++)
   1923 			(void)printf("%02x",
   1924 				     (u_char)opack_icmp.icmp_data[phdrlen + j]);
   1925 		(void)printf("\n");
   1926 	}
   1927 
   1928 }
   1929 
   1930 
   1931 static void
   1932 rnd_fill(void)
   1933 {
   1934 	static u_int32_t rnd;
   1935 	int i;
   1936 
   1937 	for (i = phdrlen; i < datalen; i++) {
   1938 		rnd = (3141592621U * rnd + 663896637U);
   1939 		opack_icmp.icmp_data[i] = rnd>>24;
   1940 	}
   1941 }
   1942 
   1943 
   1944 static void
   1945 gethost(const char *arg,
   1946 	const char *name,
   1947 	struct sockaddr_in *sa,
   1948 	char *realname,
   1949 	int realname_len)
   1950 {
   1951 	struct hostent *hp;
   1952 
   1953 	(void)memset(sa, 0, sizeof(*sa));
   1954 	sa->sin_family = AF_INET;
   1955 	sa->sin_len = sizeof(struct sockaddr_in);
   1956 
   1957 	/* If it is an IP address, try to convert it to a name to
   1958 	 * have something nice to display.
   1959 	 */
   1960 	if (inet_aton(name, &sa->sin_addr) != 0) {
   1961 		if (realname) {
   1962 			if (pingflags & F_NUMERIC)
   1963 				hp = 0;
   1964 			else
   1965 				hp = gethostbyaddr((char *)&sa->sin_addr,
   1966 				    sizeof(sa->sin_addr), AF_INET);
   1967 			(void)strlcpy(realname, hp ? hp->h_name : name,
   1968 			    realname_len);
   1969 		}
   1970 		return;
   1971 	}
   1972 
   1973 	hp = gethostbyname(name);
   1974 	if (!hp)
   1975 		errx(EXIT_FAILURE, "Cannot resolve \"%s\" (%s)",
   1976 		    name, hstrerror(h_errno));
   1977 
   1978 	if (hp->h_addrtype != AF_INET)
   1979 		errx(EXIT_FAILURE, "%s only supported with IP", arg);
   1980 
   1981 	(void)memcpy(&sa->sin_addr, hp->h_addr, sizeof(sa->sin_addr));
   1982 
   1983 	if (realname)
   1984 		(void)strlcpy(realname, hp->h_name, realname_len);
   1985 }
   1986 
   1987 
   1988 static void
   1989 usage(void)
   1990 {
   1991 #ifdef IPSEC
   1992 #ifdef IPSEC_POLICY_IPSEC
   1993 #define IPSECOPT	"\n     [-E policy] "
   1994 #else
   1995 #define IPSECOPT	"\n     [-AE] "
   1996 #endif /*IPSEC_POLICY_IPSEC*/
   1997 #else
   1998 #define IPSECOPT	""
   1999 #endif /*IPSEC*/
   2000 
   2001 	(void)fprintf(stderr, "usage: \n"
   2002 	    "%s [-aCDdfLnoPQqRrv] [-c count] [-g gateway] [-h host]"
   2003 	    " [-I addr] [-i interval]\n"
   2004 	    "     [-l preload] [-p pattern] [-s size] [-T ttl] [-t tos]"
   2005 	    " [-w maxwait] " IPSECOPT "host\n",
   2006 	    getprogname());
   2007 	exit(1);
   2008 }
   2009