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