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