Home | History | Annotate | Line # | Download | only in ping
ping.c revision 1.77.4.1
      1 /*	$NetBSD: ping.c,v 1.77.4.1 2007/08/26 19:55:21 bouyer 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.77.4.1 2007/08/26 19:55:21 bouyer Exp $");
     62 #endif
     63 
     64 #include <stdio.h>
     65 #include <stddef.h>
     66 #include <errno.h>
     67 #include <sys/time.h>
     68 #include <sys/types.h>
     69 #include <sys/signal.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 #include <err.h>
     93 #endif
     94 
     95 #include <netinet/in_systm.h>
     96 #include <netinet/in.h>
     97 #include <netinet/ip.h>
     98 #include <netinet/ip_icmp.h>
     99 #include <netinet/ip_var.h>
    100 #include <arpa/inet.h>
    101 #include <ctype.h>
    102 #include <netdb.h>
    103 
    104 #ifdef IPSEC
    105 #include <netinet6/ipsec.h>
    106 #endif /*IPSEC*/
    107 
    108 #define FLOOD_INTVL	0.01		/* default flood output interval */
    109 #define	MAXPACKET	(IP_MAXPACKET-60-8)	/* max packet size */
    110 
    111 #define F_VERBOSE	0x0001
    112 #define F_QUIET		0x0002		/* minimize all output */
    113 #define F_SEMI_QUIET	0x0004		/* ignore our ICMP errors */
    114 #define F_FLOOD		0x0008		/* flood-ping */
    115 #define	F_RECORD_ROUTE	0x0010		/* record route */
    116 #define F_SOURCE_ROUTE	0x0020		/* loose source route */
    117 #define F_PING_FILLED	0x0040		/* is buffer filled with user data? */
    118 #define F_PING_RANDOM	0x0080		/* use random data */
    119 #define	F_NUMERIC	0x0100		/* do not do gethostbyaddr() calls */
    120 #define F_TIMING	0x0200		/* room for a timestamp */
    121 #define F_DF		0x0400		/* set IP DF bit */
    122 #define F_SOURCE_ADDR	0x0800		/* set source IP address/interface */
    123 #define F_ONCE		0x1000		/* exit(0) after receiving 1 reply */
    124 #define F_MCAST		0x2000		/* multicast target */
    125 #define F_MCAST_NOLOOP	0x4000		/* no multicast loopback */
    126 #define F_AUDIBLE	0x8000		/* audible output */
    127 #ifdef IPSEC
    128 #ifdef IPSEC_POLICY_IPSEC
    129 #define F_POLICY	0x10000
    130 #else
    131 #define	F_AUTHHDR	0x10000
    132 #define	F_ENCRYPT	0x20000
    133 #endif /*IPSEC_POLICY_IPSEC*/
    134 #endif /*IPSEC*/
    135 
    136 
    137 /* MAX_DUP_CHK is the number of bits in received table, the
    138  *	maximum number of received sequence numbers we can track to check
    139  *	for duplicates.
    140  */
    141 #define MAX_DUP_CHK     (8 * 2048)
    142 u_char	rcvd_tbl[MAX_DUP_CHK/8];
    143 int     nrepeats = 0;
    144 #define A(seq)	rcvd_tbl[(seq/8)%sizeof(rcvd_tbl)]  /* byte in array */
    145 #define B(seq)	(1 << (seq & 0x07))	/* bit in byte */
    146 #define SET(seq) (A(seq) |= B(seq))
    147 #define CLR(seq) (A(seq) &= (~B(seq)))
    148 #define TST(seq) (A(seq) & B(seq))
    149 
    150 struct tv32 {
    151 	int32_t tv32_sec;
    152 	int32_t tv32_usec;
    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 tv32)	/* 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 	if ((s = cap_socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
    266 		err(1, "Cannot create socket");
    267 	if ((sloop = cap_socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
    268 		err(1, "Cannot create socket");
    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 #if defined(SIGINFO) && defined(NOKERNINFO)
    645 	if (tcgetattr (0, &ts) != -1) {
    646 		reset_kerninfo = !(ts.c_lflag & NOKERNINFO);
    647 		ts.c_lflag |= NOKERNINFO;
    648 		tcsetattr (STDIN_FILENO, TCSANOW, &ts);
    649 	}
    650 #endif
    651 
    652 #ifdef SIGINFO
    653 	(void)signal(SIGINFO, prtsig);
    654 #else
    655 	(void)signal(SIGQUIT, prtsig);
    656 #endif
    657 	(void)signal(SIGCONT, prtsig);
    658 
    659 	/* fire off them quickies */
    660 	for (i = 0; i < preload; i++) {
    661 		(void)gettimeofday(&now, 0);
    662 		pinger();
    663 	}
    664 
    665 	doit();
    666 	return 0;
    667 }
    668 
    669 
    670 static void
    671 doit(void)
    672 {
    673 	int cc;
    674 	struct sockaddr_in from;
    675 	int fromlen;
    676 	double sec, last, d_last;
    677 	struct pollfd fdmaskp[1];
    678 
    679 	(void)gettimeofday(&clear_cache,0);
    680 	if (maxwait != 0) {
    681 		last = timeval_to_sec(&clear_cache) + maxwait;
    682 		d_last = 0;
    683 	} else {
    684 		last = 0;
    685 		d_last = 365*24*60*60;
    686 	}
    687 
    688 	do {
    689 		(void)gettimeofday(&now,0);
    690 
    691 		if (last != 0)
    692 			d_last = last - timeval_to_sec(&now);
    693 
    694 		if (ntransmitted < npackets && d_last > 0) {
    695 			/* send if within 100 usec or late for next packet */
    696 			sec = diffsec(&next_tx,&now);
    697 			if (sec <= 0.0001 ||
    698 			    (lastrcvd && (pingflags & F_FLOOD))) {
    699 				pinger();
    700 				sec = diffsec(&next_tx,&now);
    701 			}
    702 			if (sec < 0.0)
    703 				sec = 0.0;
    704 			if (d_last < sec)
    705 				sec = d_last;
    706 
    707 		} else {
    708 			/* For the last response, wait twice as long as the
    709 			 * worst case seen, or 10 times as long as the
    710 			 * maximum interpacket interval, whichever is longer.
    711 			 */
    712 			sec = MAX(2 * tmax, 10 * interval) -
    713 			    diffsec(&now, &last_tx);
    714 			if (d_last < sec)
    715 				sec = d_last;
    716 			if (sec <= 0)
    717 				break;
    718 		}
    719 
    720 
    721 		fdmaskp[0].fd = s;
    722 		fdmaskp[0].events = POLLIN;
    723 		cc = poll(fdmaskp, 1, (int)(sec * 1000));
    724 		if (cc <= 0) {
    725 			if (cc < 0) {
    726 				if (errno == EINTR)
    727 					continue;
    728 				jiggle_flush(1);
    729 				err(1, "poll");
    730 			}
    731 			continue;
    732 		}
    733 
    734 		fromlen  = sizeof(from);
    735 		cc = recvfrom(s, (char *) packet, packlen,
    736 			      0, (struct sockaddr *)&from,
    737 			      &fromlen);
    738 		if (cc < 0) {
    739 			if (errno != EINTR) {
    740 				jiggle_flush(1);
    741 				warn("recvfrom");
    742 				(void)fflush(stderr);
    743 			}
    744 			continue;
    745 		}
    746 		(void)gettimeofday(&now, 0);
    747 		pr_pack(packet, cc, &from);
    748 
    749 	} while (nreceived < npackets
    750 		 && (nreceived == 0 || !(pingflags & F_ONCE)));
    751 
    752 	finish(0);
    753 }
    754 
    755 
    756 static void
    757 jiggle_flush(int nl)			/* new line if there are dots */
    758 {
    759 	int serrno = errno;
    760 
    761 	if (jiggle_cnt > 0) {
    762 		total_jiggled += jiggle_cnt;
    763 		jiggle_direction = 1;
    764 		do {
    765 			(void)putchar('.');
    766 		} while (--jiggle_cnt > 0);
    767 
    768 	} else if (jiggle_cnt < 0) {
    769 		total_jiggled -= jiggle_cnt;
    770 		jiggle_direction = -1;
    771 		do {
    772 			(void)putchar('\b');
    773 		} while (++jiggle_cnt < 0);
    774 	}
    775 
    776 	if (nl) {
    777 		if (total_jiggled != 0)
    778 			(void)putchar('\n');
    779 		total_jiggled = 0;
    780 		jiggle_direction = -1;
    781 	}
    782 
    783 	(void)fflush(stdout);
    784 	(void)fflush(stderr);
    785 	jiggle_time = now;
    786 	errno = serrno;
    787 }
    788 
    789 
    790 /* jiggle the cursor for flood-ping
    791  */
    792 static void
    793 jiggle(int delta)
    794 {
    795 	double dt;
    796 
    797 	if (pingflags & F_QUIET)
    798 		return;
    799 
    800 	/* do not back up into messages */
    801 	if (total_jiggled+jiggle_cnt+delta < 0)
    802 		return;
    803 
    804 	jiggle_cnt += delta;
    805 
    806 	/* flush the FLOOD dots when things are quiet
    807 	 * or occassionally to make the cursor jiggle.
    808 	 */
    809 	dt = diffsec(&last_tx, &jiggle_time);
    810 	if (dt > 0.2 || (dt >= 0.15 && delta*jiggle_direction < 0))
    811 		jiggle_flush(0);
    812 }
    813 
    814 
    815 /*
    816  * Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
    817  * will be added on by the kernel.  The ID field is our UNIX process ID,
    818  * and the sequence number is an ascending integer.  The first PHDR_LEN bytes
    819  * of the data portion are used to hold a UNIX "timeval" struct in VAX
    820  * byte-order, to compute the round-trip time.
    821  */
    822 static void
    823 pinger(void)
    824 {
    825 	struct tv32 tv32;
    826 	int i, cc, sw;
    827 
    828 	opack_icmp.icmp_code = 0;
    829 	opack_icmp.icmp_seq = htons((u_int16_t)(ntransmitted));
    830 
    831 	/* clear the cached route in the kernel after an ICMP
    832 	 * response such as a Redirect is seen to stop causing
    833 	 * more such packets.  Also clear the cached route
    834 	 * periodically in case of routing changes that make
    835 	 * black holes come and go.
    836 	 */
    837 	if (clear_cache.tv_sec != now.tv_sec) {
    838 		opack_icmp.icmp_type = ICMP_ECHOREPLY;
    839 		opack_icmp.icmp_id = ~ident;
    840 		opack_icmp.icmp_cksum = 0;
    841 		opack_icmp.icmp_cksum = in_cksum((u_int16_t *)&opack_icmp,
    842 		    PHDR_LEN);
    843 		sw = 0;
    844 		if (setsockopt(sloop,IPPROTO_IP,IP_HDRINCL,
    845 			       (char *)&sw,sizeof(sw)) < 0)
    846 			err(1, "Can't turn off special IP header");
    847 		if (sendto(sloop, (char *) &opack_icmp, PHDR_LEN, MSG_DONTROUTE,
    848 			   (struct sockaddr *)&loc_addr,
    849 			   sizeof(struct sockaddr_in)) < 0) {
    850 			/*
    851 			 * XXX: we only report this as a warning in verbose
    852 			 * mode because people get confused when they see
    853 			 * this error when they are running in single user
    854 			 * mode and they have not configured lo0
    855 			 */
    856 			if (pingflags & F_VERBOSE)
    857 				warn("failed to clear cached route");
    858 		}
    859 		sw = 1;
    860 		if (setsockopt(sloop,IPPROTO_IP,IP_HDRINCL,
    861 			       (char *)&sw, sizeof(sw)) < 0)
    862 			err(1, "Can't set special IP header");
    863 
    864 		(void)gettimeofday(&clear_cache,0);
    865 	}
    866 
    867 	opack_icmp.icmp_type = ICMP_ECHO;
    868 	opack_icmp.icmp_id = ident;
    869 	tv32.tv32_sec = htonl(now.tv_sec);
    870 	tv32.tv32_usec = htonl(now.tv_usec);
    871 	if (pingflags & F_TIMING)
    872 		(void) memcpy(&opack_icmp.icmp_data[0], &tv32, sizeof(tv32));
    873 	cc = datalen + PHDR_LEN;
    874 	opack_icmp.icmp_cksum = 0;
    875 	opack_icmp.icmp_cksum = in_cksum((u_int16_t *)&opack_icmp, cc);
    876 
    877 	cc += opack_ip->ip_hl<<2;
    878 	opack_ip->ip_len = cc;
    879 	i = sendto(s, (char *) opack_ip, cc, 0,
    880 		   (struct sockaddr *)&send_addr, sizeof(struct sockaddr_in));
    881 	if (i != cc) {
    882 		jiggle_flush(1);
    883 		if (i < 0)
    884 			warn("sendto");
    885 		else
    886 			warnx("wrote %s %d chars, ret=%d", hostname, cc, i);
    887 		(void)fflush(stderr);
    888 	}
    889 	lastrcvd = 0;
    890 
    891 	CLR(ntransmitted);
    892 	ntransmitted++;
    893 
    894 	last_tx = now;
    895 	if (next_tx.tv_sec == 0) {
    896 		first_tx = now;
    897 		next_tx = now;
    898 	}
    899 
    900 	/* Transmit regularly, at always the same microsecond in the
    901 	 * second when going at one packet per second.
    902 	 * If we are at most 100 ms behind, send extras to get caught up.
    903 	 * Otherwise, skip packets we were too slow to send.
    904 	 */
    905 	if (diffsec(&next_tx, &now) <= interval) {
    906 		do {
    907 			timevaladd(&next_tx, &interval_tv);
    908 		} while (diffsec(&next_tx, &now) < -0.1);
    909 	}
    910 
    911 	if (pingflags & F_FLOOD)
    912 		jiggle(1);
    913 
    914 	/* While the packet is going out, ready buffer for the next
    915 	 * packet. Use a fast but not very good random number generator.
    916 	 */
    917 	if (pingflags & F_PING_RANDOM)
    918 		rnd_fill();
    919 }
    920 
    921 
    922 static void
    923 pr_pack_sub(int cc,
    924 	    char *addr,
    925 	    int seqno,
    926 	    int dupflag,
    927 	    int ttl,
    928 	    double triptime)
    929 {
    930 	jiggle_flush(1);
    931 
    932 	if (pingflags & F_FLOOD)
    933 		return;
    934 
    935 	(void)printf("%d bytes from %s: icmp_seq=%u", cc, addr, seqno);
    936 	if (dupflag)
    937 		(void)printf(" DUP!");
    938 	(void)printf(" ttl=%d", ttl);
    939 	if (pingflags & F_TIMING)
    940 		(void)printf(" time=%.3f ms", triptime*1000.0);
    941 
    942 	/*
    943 	 * Send beep to stderr, since that's more likely than stdout
    944 	 * to go to a terminal..
    945 	 */
    946 	if (pingflags & F_AUDIBLE && !dupflag)
    947 		(void)fprintf(stderr,"\a");
    948 }
    949 
    950 
    951 /*
    952  * Print out the packet, if it came from us.  This logic is necessary
    953  * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
    954  * which arrive ('tis only fair).  This permits multiple copies of this
    955  * program to be run without having intermingled output (or statistics!).
    956  */
    957 static void
    958 pr_pack(u_char *buf,
    959 	int tot_len,
    960 	struct sockaddr_in *from)
    961 {
    962 	struct ip *ip;
    963 	struct icmp *icp;
    964 	int i, j, net_len;
    965 	u_char *cp;
    966 	static int old_rrlen;
    967 	static char old_rr[MAX_IPOPTLEN];
    968 	int hlen, dupflag = 0, dumped;
    969 	double triptime = 0.0;
    970 #define PR_PACK_SUB() {if (!dumped) {			\
    971 	dumped = 1;					\
    972 	pr_pack_sub(net_len, inet_ntoa(from->sin_addr),	\
    973 		    ntohs((u_int16_t)icp->icmp_seq),	\
    974 		    dupflag, ip->ip_ttl, triptime);}}
    975 
    976 	/* Check the IP header */
    977 	ip = (struct ip *) buf;
    978 	hlen = ip->ip_hl << 2;
    979 	if (tot_len < hlen + ICMP_MINLEN) {
    980 		if (pingflags & F_VERBOSE) {
    981 			jiggle_flush(1);
    982 			(void)printf("packet too short (%d bytes) from %s\n",
    983 				     tot_len, inet_ntoa(from->sin_addr));
    984 		}
    985 		return;
    986 	}
    987 
    988 	/* Now the ICMP part */
    989 	dumped = 0;
    990 	net_len = tot_len - hlen;
    991 	icp = (struct icmp *)(buf + hlen);
    992 	if (icp->icmp_type == ICMP_ECHOREPLY
    993 	    && icp->icmp_id == ident) {
    994 		if (icp->icmp_seq == htons((u_int16_t)(ntransmitted-1)))
    995 			lastrcvd = 1;
    996 		last_rx = now;
    997 		if (first_rx.tv_sec == 0)
    998 			first_rx = last_rx;
    999 		nreceived++;
   1000 		if (pingflags & F_TIMING) {
   1001 			struct timeval tv;
   1002 			struct tv32 tv32;
   1003 
   1004 			(void) memcpy(&tv32, icp->icmp_data, sizeof(tv32));
   1005 			tv.tv_sec = ntohl(tv32.tv32_sec);
   1006 			tv.tv_usec = ntohl(tv32.tv32_usec);
   1007 			triptime = diffsec(&last_rx, &tv);
   1008 			tsum += triptime;
   1009 			tsumsq += triptime * triptime;
   1010 			if (triptime < tmin)
   1011 				tmin = triptime;
   1012 			if (triptime > tmax)
   1013 				tmax = triptime;
   1014 		}
   1015 
   1016 		if (TST(ntohs((u_int16_t)icp->icmp_seq))) {
   1017 			nrepeats++, nreceived--;
   1018 			dupflag=1;
   1019 		} else {
   1020 			SET(ntohs((u_int16_t)icp->icmp_seq));
   1021 		}
   1022 
   1023 		if (tot_len != opack_ip->ip_len) {
   1024 			PR_PACK_SUB();
   1025 			(void)printf("\nwrong total length %d instead of %d",
   1026 				     tot_len, opack_ip->ip_len);
   1027 		}
   1028 
   1029 		if (!dupflag) {
   1030 			static u_int16_t last_seqno = 0xffff;
   1031 			u_int16_t seqno = ntohs((u_int16_t)icp->icmp_seq);
   1032 			u_int16_t gap = seqno - (last_seqno + 1);
   1033 			if (gap > 0 && gap < 0x8000 &&
   1034 			    (pingflags & F_VERBOSE)) {
   1035 				(void)printf("[*** sequence gap of %u "
   1036 				    "packets from %u ... %u ***]\n", gap,
   1037 				    (u_int16_t) (last_seqno + 1),
   1038 				    (u_int16_t) (seqno - 1));
   1039 				if (pingflags & F_QUIET)
   1040 					summary(0);
   1041 			}
   1042 
   1043 			if (gap < 0x8000)
   1044 				last_seqno = seqno;
   1045 		}
   1046 
   1047 		if (pingflags & F_QUIET)
   1048 			return;
   1049 
   1050 		if (!(pingflags & F_FLOOD))
   1051 			PR_PACK_SUB();
   1052 
   1053 		/* check the data */
   1054 		if (datalen > PHDR_LEN
   1055 		    && !(pingflags & F_PING_RANDOM)
   1056 		    && memcmp(&icp->icmp_data[PHDR_LEN],
   1057 			    &opack_icmp.icmp_data[PHDR_LEN],
   1058 			    datalen-PHDR_LEN)) {
   1059 			for (i=PHDR_LEN; i<datalen; i++) {
   1060 				if (icp->icmp_data[i] !=
   1061 				    opack_icmp.icmp_data[i])
   1062 					break;
   1063 			}
   1064 			PR_PACK_SUB();
   1065 			(void)printf("\nwrong data byte #%d should have been"
   1066 				     " %#x but was %#x", i,
   1067 				     (u_char)opack_icmp.icmp_data[i],
   1068 				     (u_char)icp->icmp_data[i]);
   1069 			for (i=PHDR_LEN; i<datalen; i++) {
   1070 				if ((i%16) == PHDR_LEN)
   1071 					(void)printf("\n\t");
   1072 				(void)printf("%2x ",(u_char)icp->icmp_data[i]);
   1073 			}
   1074 		}
   1075 
   1076 	} else {
   1077 		if (!pr_icmph(icp, from, net_len))
   1078 			return;
   1079 		dumped = 2;
   1080 	}
   1081 
   1082 	/* Display any IP options */
   1083 	cp = buf + sizeof(struct ip);
   1084 	while (hlen > (int)sizeof(struct ip)) {
   1085 		switch (*cp) {
   1086 		case IPOPT_EOL:
   1087 			hlen = 0;
   1088 			break;
   1089 		case IPOPT_LSRR:
   1090 			hlen -= 2;
   1091 			j = *++cp;
   1092 			++cp;
   1093 			j -= IPOPT_MINOFF;
   1094 			if (j <= 0)
   1095 				continue;
   1096 			if (dumped <= 1) {
   1097 				j = ((j+3)/4)*4;
   1098 				hlen -= j;
   1099 				cp += j;
   1100 				break;
   1101 			}
   1102 			PR_PACK_SUB();
   1103 			(void)printf("\nLSRR: ");
   1104 			for (;;) {
   1105 				pr_saddr(cp);
   1106 				cp += 4;
   1107 				hlen -= 4;
   1108 				j -= 4;
   1109 				if (j <= 0)
   1110 					break;
   1111 				(void)putchar('\n');
   1112 			}
   1113 			break;
   1114 		case IPOPT_RR:
   1115 			j = *++cp;	/* get length */
   1116 			i = *++cp;	/* and pointer */
   1117 			hlen -= 2;
   1118 			if (i > j)
   1119 				i = j;
   1120 			i -= IPOPT_MINOFF;
   1121 			if (i <= 0)
   1122 				continue;
   1123 			if (dumped <= 1) {
   1124 				if (i == old_rrlen
   1125 				    && !memcmp(cp, old_rr, i)) {
   1126 					if (dumped)
   1127 					    (void)printf("\t(same route)");
   1128 					j = ((i+3)/4)*4;
   1129 					hlen -= j;
   1130 					cp += j;
   1131 					break;
   1132 				}
   1133 				old_rrlen = i;
   1134 				(void) memcpy(old_rr, cp, i);
   1135 			}
   1136 			if (!dumped) {
   1137 				jiggle_flush(1);
   1138 				(void)printf("RR: ");
   1139 				dumped = 1;
   1140 			} else {
   1141 				(void)printf("\nRR: ");
   1142 			}
   1143 			for (;;) {
   1144 				pr_saddr(cp);
   1145 				cp += 4;
   1146 				hlen -= 4;
   1147 				i -= 4;
   1148 				if (i <= 0)
   1149 					break;
   1150 				(void)putchar('\n');
   1151 			}
   1152 			break;
   1153 		case IPOPT_NOP:
   1154 			if (dumped <= 1)
   1155 				break;
   1156 			PR_PACK_SUB();
   1157 			(void)printf("\nNOP");
   1158 			break;
   1159 #ifdef sgi
   1160 		case IPOPT_SECURITY:	/* RFC 1108 RIPSO BSO */
   1161 		case IPOPT_ESO:		/* RFC 1108 RIPSO ESO */
   1162 		case IPOPT_CIPSO:	/* Commercial IPSO */
   1163 			if ((sysconf(_SC_IP_SECOPTS)) > 0) {
   1164 				i = (unsigned)cp[1];
   1165 				hlen -= i - 1;
   1166 				PR_PACK_SUB();
   1167 				(void)printf("\nSEC:");
   1168 				while (i--) {
   1169 					(void)printf(" %02x", *cp++);
   1170 				}
   1171 				cp--;
   1172 				break;
   1173 			}
   1174 #endif
   1175 		default:
   1176 			PR_PACK_SUB();
   1177 			(void)printf("\nunknown option 0x%x", *cp);
   1178 			break;
   1179 		}
   1180 		hlen--;
   1181 		cp++;
   1182 	}
   1183 
   1184 	if (dumped) {
   1185 		(void)putchar('\n');
   1186 		(void)fflush(stdout);
   1187 	} else {
   1188 		jiggle(-1);
   1189 	}
   1190 }
   1191 
   1192 
   1193 /* Compute the IP checksum
   1194  *	This assumes the packet is less than 32K long.
   1195  */
   1196 static u_int16_t
   1197 in_cksum(u_int16_t *p, u_int len)
   1198 {
   1199 	u_int32_t sum = 0;
   1200 	int nwords = len >> 1;
   1201 
   1202 	while (nwords-- != 0)
   1203 		sum += *p++;
   1204 
   1205 	if (len & 1) {
   1206 		union {
   1207 			u_int16_t w;
   1208 			u_int8_t c[2];
   1209 		} u;
   1210 		u.c[0] = *(u_char *)p;
   1211 		u.c[1] = 0;
   1212 		sum += u.w;
   1213 	}
   1214 
   1215 	/* end-around-carry */
   1216 	sum = (sum >> 16) + (sum & 0xffff);
   1217 	sum += (sum >> 16);
   1218 	return (~sum);
   1219 }
   1220 
   1221 
   1222 /*
   1223  * compute the difference of two timevals in seconds
   1224  */
   1225 static double
   1226 diffsec(struct timeval *timenow,
   1227 	struct timeval *then)
   1228 {
   1229 	return ((timenow->tv_sec - then->tv_sec)*1.0
   1230 		+ (timenow->tv_usec - then->tv_usec)/1000000.0);
   1231 }
   1232 
   1233 
   1234 static void
   1235 timevaladd(struct timeval *t1,
   1236 	   struct timeval *t2)
   1237 {
   1238 
   1239 	t1->tv_sec += t2->tv_sec;
   1240 	if ((t1->tv_usec += t2->tv_usec) >= 1000000) {
   1241 		t1->tv_sec++;
   1242 		t1->tv_usec -= 1000000;
   1243 	}
   1244 }
   1245 
   1246 
   1247 static void
   1248 sec_to_timeval(const double sec, struct timeval *tp)
   1249 {
   1250 	tp->tv_sec = sec;
   1251 	tp->tv_usec = (sec - tp->tv_sec) * 1000000.0;
   1252 }
   1253 
   1254 
   1255 static double
   1256 timeval_to_sec(const struct timeval *tp)
   1257 {
   1258 	return tp->tv_sec + tp->tv_usec / 1000000.0;
   1259 }
   1260 
   1261 
   1262 /*
   1263  * Print statistics.
   1264  * Heavily buffered STDIO is used here, so that all the statistics
   1265  * will be written with 1 sys-write call.  This is nice when more
   1266  * than one copy of the program is running on a terminal;  it prevents
   1267  * the statistics output from becomming intermingled.
   1268  */
   1269 static void
   1270 summary(int header)
   1271 {
   1272 	jiggle_flush(1);
   1273 
   1274 	if (header)
   1275 		(void)printf("\n----%s PING Statistics----\n", hostname);
   1276 	(void)printf("%d packets transmitted, ", ntransmitted);
   1277 	(void)printf("%d packets received, ", nreceived);
   1278 	if (nrepeats)
   1279 		(void)printf("+%d duplicates, ", nrepeats);
   1280 	if (ntransmitted) {
   1281 		if (nreceived > ntransmitted)
   1282 			(void)printf("-- somebody's duplicating packets!");
   1283 		else
   1284 			(void)printf("%.1f%% packet loss",
   1285 				     (((ntransmitted-nreceived)*100.0) /
   1286 					    ntransmitted));
   1287 	}
   1288 	(void)printf("\n");
   1289 	if (nreceived && (pingflags & F_TIMING)) {
   1290 		double n = nreceived + nrepeats;
   1291 		double avg = (tsum / n);
   1292 		double variance = 0.0;
   1293 		if (n>1)
   1294 			variance = (tsumsq - n*avg*avg) /(n-1);
   1295 
   1296 		printf("round-trip min/avg/max/stddev = "
   1297 			"%.3f/%.3f/%.3f/%.3f ms\n",
   1298 			tmin * 1000.0, avg * 1000.0,
   1299 			tmax * 1000.0, sqrt(variance) * 1000.0);
   1300 		if (pingflags & F_FLOOD) {
   1301 			double r = diffsec(&last_rx, &first_rx);
   1302 			double t = diffsec(&last_tx, &first_tx);
   1303 			if (r == 0)
   1304 				r = 0.0001;
   1305 			if (t == 0)
   1306 				t = 0.0001;
   1307 			(void)printf("  %.1f packets/sec sent, "
   1308 				     " %.1f packets/sec received\n",
   1309 				     ntransmitted/t, nreceived/r);
   1310 		}
   1311 	}
   1312 }
   1313 
   1314 
   1315 /*
   1316  * Print statistics when SIGINFO is received.
   1317  */
   1318 /* ARGSUSED */
   1319 static void
   1320 prtsig(int dummy)
   1321 {
   1322 	summary(0);
   1323 #ifdef SIGINFO
   1324 	(void)signal(SIGINFO, prtsig);
   1325 #else
   1326 	(void)signal(SIGQUIT, prtsig);
   1327 #endif
   1328 }
   1329 
   1330 
   1331 /*
   1332  * On the first SIGINT, allow any outstanding packets to dribble in
   1333  */
   1334 static void
   1335 prefinish(int dummy)
   1336 {
   1337 	if (lastrcvd			/* quit now if caught up */
   1338 	    || nreceived == 0)		/* or if remote is dead */
   1339 		finish(0);
   1340 
   1341 	(void)signal(dummy, finish);	/* do this only the 1st time */
   1342 
   1343 	if (npackets > ntransmitted)	/* let the normal limit work */
   1344 		npackets = ntransmitted;
   1345 }
   1346 
   1347 
   1348 /*
   1349  * Print statistics and give up.
   1350  */
   1351 /* ARGSUSED */
   1352 static void
   1353 finish(int dummy)
   1354 {
   1355 #if defined(SIGINFO) && defined(NOKERNINFO)
   1356 	struct termios ts;
   1357 
   1358 	if (reset_kerninfo && tcgetattr (0, &ts) != -1) {
   1359 		ts.c_lflag &= ~NOKERNINFO;
   1360 		tcsetattr (STDIN_FILENO, TCSANOW, &ts);
   1361 	}
   1362 	(void)signal(SIGINFO, SIG_IGN);
   1363 #else
   1364 	(void)signal(SIGQUIT, SIG_DFL);
   1365 #endif
   1366 
   1367 	summary(1);
   1368 	exit(nreceived > 0 ? 0 : 2);
   1369 }
   1370 
   1371 
   1372 static int				/* 0=do not print it */
   1373 ck_pr_icmph(struct icmp *icp,
   1374 	    struct sockaddr_in *from,
   1375 	    int cc,
   1376 	    int override)		/* 1=override VERBOSE if interesting */
   1377 {
   1378 	int	hlen;
   1379 	struct ip ipb, *ip = &ipb;
   1380 	struct icmp icp2b, *icp2 = &icp2b;
   1381 	int res;
   1382 
   1383 	if (pingflags & F_VERBOSE) {
   1384 		res = 1;
   1385 		jiggle_flush(1);
   1386 	} else {
   1387 		res = 0;
   1388 	}
   1389 
   1390 	(void) memcpy(ip, icp->icmp_data, sizeof(*ip));
   1391 	hlen = ip->ip_hl << 2;
   1392 	if (ip->ip_p == IPPROTO_ICMP
   1393 	    && hlen + 6 <= cc) {
   1394 		(void) memcpy(icp2, &icp->icmp_data[hlen], sizeof(*icp2));
   1395 		if (icp2->icmp_id == ident) {
   1396 			/* remember to clear route cached in kernel
   1397 			 * if this non-Echo-Reply ICMP message was for one
   1398 			 * of our packets.
   1399 			 */
   1400 			clear_cache.tv_sec = 0;
   1401 
   1402 			if (!res && override
   1403 			    && (pingflags & (F_QUIET|F_SEMI_QUIET)) == 0) {
   1404 				jiggle_flush(1);
   1405 				(void)printf("%d bytes from %s: ",
   1406 					     cc, pr_addr(&from->sin_addr));
   1407 				res = 1;
   1408 			}
   1409 		}
   1410 	}
   1411 
   1412 	return res;
   1413 }
   1414 
   1415 
   1416 /*
   1417  *  Print a descriptive string about an ICMP header other than an echo reply.
   1418  */
   1419 static int				/* 0=printed nothing */
   1420 pr_icmph(struct icmp *icp,
   1421 	 struct sockaddr_in *from,
   1422 	 int cc)
   1423 {
   1424 	switch (icp->icmp_type ) {
   1425 	case ICMP_UNREACH:
   1426 		if (!ck_pr_icmph(icp, from, cc, 1))
   1427 			return 0;
   1428 		switch (icp->icmp_code) {
   1429 		case ICMP_UNREACH_NET:
   1430 			(void)printf("Destination Net Unreachable");
   1431 			break;
   1432 		case ICMP_UNREACH_HOST:
   1433 			(void)printf("Destination Host Unreachable");
   1434 			break;
   1435 		case ICMP_UNREACH_PROTOCOL:
   1436 			(void)printf("Destination Protocol Unreachable");
   1437 			break;
   1438 		case ICMP_UNREACH_PORT:
   1439 			(void)printf("Destination Port Unreachable");
   1440 			break;
   1441 		case ICMP_UNREACH_NEEDFRAG:
   1442 			(void)printf("frag needed and DF set.  Next MTU=%d",
   1443 			       ntohs(icp->icmp_nextmtu));
   1444 			break;
   1445 		case ICMP_UNREACH_SRCFAIL:
   1446 			(void)printf("Source Route Failed");
   1447 			break;
   1448 		case ICMP_UNREACH_NET_UNKNOWN:
   1449 			(void)printf("Unreachable unknown net");
   1450 			break;
   1451 		case ICMP_UNREACH_HOST_UNKNOWN:
   1452 			(void)printf("Unreachable unknown host");
   1453 			break;
   1454 		case ICMP_UNREACH_ISOLATED:
   1455 			(void)printf("Unreachable host isolated");
   1456 			break;
   1457 		case ICMP_UNREACH_NET_PROHIB:
   1458 			(void)printf("Net prohibited access");
   1459 			break;
   1460 		case ICMP_UNREACH_HOST_PROHIB:
   1461 			(void)printf("Host prohibited access");
   1462 			break;
   1463 		case ICMP_UNREACH_TOSNET:
   1464 			(void)printf("Bad TOS for net");
   1465 			break;
   1466 		case ICMP_UNREACH_TOSHOST:
   1467 			(void)printf("Bad TOS for host");
   1468 			break;
   1469 		case 13:
   1470 			(void)printf("Communication prohibited");
   1471 			break;
   1472 		case 14:
   1473 			(void)printf("Host precedence violation");
   1474 			break;
   1475 		case 15:
   1476 			(void)printf("Precedence cutoff");
   1477 			break;
   1478 		default:
   1479 			(void)printf("Bad Destination Unreachable Code: %d",
   1480 				     icp->icmp_code);
   1481 			break;
   1482 		}
   1483 		/* Print returned IP header information */
   1484 		pr_retip(icp, cc);
   1485 		break;
   1486 
   1487 	case ICMP_SOURCEQUENCH:
   1488 		if (!ck_pr_icmph(icp, from, cc, 1))
   1489 			return 0;
   1490 		(void)printf("Source Quench");
   1491 		pr_retip(icp, cc);
   1492 		break;
   1493 
   1494 	case ICMP_REDIRECT:
   1495 		if (!ck_pr_icmph(icp, from, cc, 1))
   1496 			return 0;
   1497 		switch (icp->icmp_code) {
   1498 		case ICMP_REDIRECT_NET:
   1499 			(void)printf("Redirect Network");
   1500 			break;
   1501 		case ICMP_REDIRECT_HOST:
   1502 			(void)printf("Redirect Host");
   1503 			break;
   1504 		case ICMP_REDIRECT_TOSNET:
   1505 			(void)printf("Redirect Type of Service and Network");
   1506 			break;
   1507 		case ICMP_REDIRECT_TOSHOST:
   1508 			(void)printf("Redirect Type of Service and Host");
   1509 			break;
   1510 		default:
   1511 			(void)printf("Redirect--Bad Code: %d", icp->icmp_code);
   1512 			break;
   1513 		}
   1514 		(void)printf(" New router addr: %s",
   1515 			     pr_addr(&icp->icmp_hun.ih_gwaddr));
   1516 		pr_retip(icp, cc);
   1517 		break;
   1518 
   1519 	case ICMP_ECHO:
   1520 		if (!ck_pr_icmph(icp, from, cc, 0))
   1521 			return 0;
   1522 		(void)printf("Echo Request: ID=%d seq=%d",
   1523 			     ntohs(icp->icmp_id), ntohs(icp->icmp_seq));
   1524 		break;
   1525 
   1526 	case ICMP_ECHOREPLY:
   1527 		/* displaying other's pings is too noisey */
   1528 #if 0
   1529 		if (!ck_pr_icmph(icp, from, cc, 0))
   1530 			return 0;
   1531 		(void)printf("Echo Reply: ID=%d seq=%d",
   1532 			     ntohs(icp->icmp_id), ntohs(icp->icmp_seq));
   1533 		break;
   1534 #else
   1535 		return 0;
   1536 #endif
   1537 
   1538 	case ICMP_ROUTERADVERT:
   1539 		if (!ck_pr_icmph(icp, from, cc, 0))
   1540 			return 0;
   1541 		(void)printf("Router Discovery Advert");
   1542 		break;
   1543 
   1544 	case ICMP_ROUTERSOLICIT:
   1545 		if (!ck_pr_icmph(icp, from, cc, 0))
   1546 			return 0;
   1547 		(void)printf("Router Discovery Solicit");
   1548 		break;
   1549 
   1550 	case ICMP_TIMXCEED:
   1551 		if (!ck_pr_icmph(icp, from, cc, 1))
   1552 			return 0;
   1553 		switch (icp->icmp_code ) {
   1554 		case ICMP_TIMXCEED_INTRANS:
   1555 			(void)printf("Time To Live exceeded");
   1556 			break;
   1557 		case ICMP_TIMXCEED_REASS:
   1558 			(void)printf("Frag reassembly time exceeded");
   1559 			break;
   1560 		default:
   1561 			(void)printf("Time exceeded, Bad Code: %d",
   1562 				     icp->icmp_code);
   1563 			break;
   1564 		}
   1565 		pr_retip(icp, cc);
   1566 		break;
   1567 
   1568 	case ICMP_PARAMPROB:
   1569 		if (!ck_pr_icmph(icp, from, cc, 1))
   1570 			return 0;
   1571 		(void)printf("Parameter problem: pointer = 0x%02x",
   1572 			     icp->icmp_hun.ih_pptr);
   1573 		pr_retip(icp, cc);
   1574 		break;
   1575 
   1576 	case ICMP_TSTAMP:
   1577 		if (!ck_pr_icmph(icp, from, cc, 0))
   1578 			return 0;
   1579 		(void)printf("Timestamp");
   1580 		break;
   1581 
   1582 	case ICMP_TSTAMPREPLY:
   1583 		if (!ck_pr_icmph(icp, from, cc, 0))
   1584 			return 0;
   1585 		(void)printf("Timestamp Reply");
   1586 		break;
   1587 
   1588 	case ICMP_IREQ:
   1589 		if (!ck_pr_icmph(icp, from, cc, 0))
   1590 			return 0;
   1591 		(void)printf("Information Request");
   1592 		break;
   1593 
   1594 	case ICMP_IREQREPLY:
   1595 		if (!ck_pr_icmph(icp, from, cc, 0))
   1596 			return 0;
   1597 		(void)printf("Information Reply");
   1598 		break;
   1599 
   1600 	case ICMP_MASKREQ:
   1601 		if (!ck_pr_icmph(icp, from, cc, 0))
   1602 			return 0;
   1603 		(void)printf("Address Mask Request");
   1604 		break;
   1605 
   1606 	case ICMP_MASKREPLY:
   1607 		if (!ck_pr_icmph(icp, from, cc, 0))
   1608 			return 0;
   1609 		(void)printf("Address Mask Reply");
   1610 		break;
   1611 
   1612 	default:
   1613 		if (!ck_pr_icmph(icp, from, cc, 0))
   1614 			return 0;
   1615 		(void)printf("Bad ICMP type: %d", icp->icmp_type);
   1616 		if (pingflags & F_VERBOSE)
   1617 			pr_iph(icp, cc);
   1618 	}
   1619 
   1620 	return 1;
   1621 }
   1622 
   1623 
   1624 /*
   1625  *  Print an IP header with options.
   1626  */
   1627 static void
   1628 pr_iph(struct icmp *icp,
   1629        int cc)
   1630 {
   1631 	int	hlen;
   1632 	u_char	*cp;
   1633 	struct ip ipb, *ip = &ipb;
   1634 
   1635 	(void) memcpy(ip, icp->icmp_data, sizeof(*ip));
   1636 
   1637 	hlen = ip->ip_hl << 2;
   1638 	cp = (u_char *) &icp->icmp_data[20];	/* point to options */
   1639 
   1640 	(void)printf("\n Vr HL TOS  Len   ID Flg  off TTL Pro  cks      Src	     Dst\n");
   1641 	(void)printf("  %1x  %1x  %02x %04x %04x",
   1642 		     ip->ip_v, ip->ip_hl, ip->ip_tos, ip->ip_len, ip->ip_id);
   1643 	(void)printf("   %1x %04x",
   1644 		     ((ip->ip_off)&0xe000)>>13, (ip->ip_off)&0x1fff);
   1645 	(void)printf("  %02x  %02x %04x",
   1646 		     ip->ip_ttl, ip->ip_p, ip->ip_sum);
   1647 	(void)printf(" %15s ",
   1648 		     inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr));
   1649 	(void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr));
   1650 	/* dump any option bytes */
   1651 	while (hlen-- > 20 && cp < (u_char*)icp+cc) {
   1652 		(void)printf("%02x", *cp++);
   1653 	}
   1654 }
   1655 
   1656 /*
   1657  * Print an ASCII host address starting from a string of bytes.
   1658  */
   1659 static void
   1660 pr_saddr(u_char *cp)
   1661 {
   1662 	n_long l;
   1663 	struct in_addr addr;
   1664 
   1665 	l = (u_char)*++cp;
   1666 	l = (l<<8) + (u_char)*++cp;
   1667 	l = (l<<8) + (u_char)*++cp;
   1668 	l = (l<<8) + (u_char)*++cp;
   1669 	addr.s_addr = htonl(l);
   1670 	(void)printf("\t%s", (l == 0) ? "0.0.0.0" : pr_addr(&addr));
   1671 }
   1672 
   1673 
   1674 /*
   1675  *  Return an ASCII host address
   1676  *  as a dotted quad and optionally with a hostname
   1677  */
   1678 static char *
   1679 pr_addr(struct in_addr *addr)		/* in network order */
   1680 {
   1681 	struct	hostent	*hp;
   1682 	static	char buf[MAXHOSTNAMELEN+4+16+1];
   1683 
   1684 	if ((pingflags & F_NUMERIC)
   1685 	    || !(hp = gethostbyaddr((char *)addr, sizeof(*addr), AF_INET))) {
   1686 		(void)snprintf(buf, sizeof(buf), "%s", inet_ntoa(*addr));
   1687 	} else {
   1688 		(void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name,
   1689 		    inet_ntoa(*addr));
   1690 	}
   1691 
   1692 	return buf;
   1693 }
   1694 
   1695 /*
   1696  *  Dump some info on a returned (via ICMP) IP packet.
   1697  */
   1698 static void
   1699 pr_retip(struct icmp *icp,
   1700 	 int cc)
   1701 {
   1702 	int	hlen;
   1703 	u_char	*cp;
   1704 	struct ip ipb, *ip = &ipb;
   1705 
   1706 	(void) memcpy(ip, icp->icmp_data, sizeof(*ip));
   1707 
   1708 	if (pingflags & F_VERBOSE)
   1709 		pr_iph(icp, cc);
   1710 
   1711 	hlen = ip->ip_hl << 2;
   1712 	cp = (u_char *) &icp->icmp_data[hlen];
   1713 
   1714 	if (ip->ip_p == IPPROTO_TCP) {
   1715 		if (pingflags & F_VERBOSE)
   1716 			(void)printf("\n  TCP: from port %u, to port %u",
   1717 				     (*cp*256+*(cp+1)), (*(cp+2)*256+*(cp+3)));
   1718 	} else if (ip->ip_p == IPPROTO_UDP) {
   1719 		if (pingflags & F_VERBOSE)
   1720 			(void)printf("\n  UDP: from port %u, to port %u",
   1721 				     (*cp*256+*(cp+1)), (*(cp+2)*256+*(cp+3)));
   1722 	} else if (ip->ip_p == IPPROTO_ICMP) {
   1723 		struct icmp icp2;
   1724 		(void) memcpy(&icp2, cp, sizeof(icp2));
   1725 		if (icp2.icmp_type == ICMP_ECHO) {
   1726 			if (pingflags & F_VERBOSE)
   1727 				(void)printf("\n  ID=%u icmp_seq=%u",
   1728 					     ntohs((u_int16_t)icp2.icmp_id),
   1729 					     ntohs((u_int16_t)icp2.icmp_seq));
   1730 			else
   1731 				(void)printf(" for icmp_seq=%u",
   1732 					     ntohs((u_int16_t)icp2.icmp_seq));
   1733 		}
   1734 	}
   1735 }
   1736 
   1737 static void
   1738 fill(void)
   1739 {
   1740 	int i, j, k;
   1741 	char *cp;
   1742 	int pat[16];
   1743 
   1744 	for (cp = fill_pat; *cp != '\0'; cp++) {
   1745 		if (!isxdigit((unsigned char)*cp))
   1746 			break;
   1747 	}
   1748 	if (cp == fill_pat || *cp != '\0' || (cp-fill_pat) > 16*2) {
   1749 		(void)fflush(stdout);
   1750 		errx(1, "\"-p %s\": patterns must be specified with"
   1751 		     " 1-32 hex digits\n",
   1752 		     fill_pat);
   1753 	}
   1754 
   1755 	i = sscanf(fill_pat,
   1756 		   "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
   1757 		    &pat[0], &pat[1], &pat[2], &pat[3],
   1758 		    &pat[4], &pat[5], &pat[6], &pat[7],
   1759 		    &pat[8], &pat[9], &pat[10], &pat[11],
   1760 		    &pat[12], &pat[13], &pat[14], &pat[15]);
   1761 
   1762 	for (k=PHDR_LEN, j = 0; k <= datalen; k++) {
   1763 		opack_icmp.icmp_data[k] = pat[j];
   1764 		if (++j >= i)
   1765 			j = 0;
   1766 	}
   1767 
   1768 	if (!(pingflags & F_QUIET)) {
   1769 		(void)printf("PATTERN: 0x");
   1770 		for (j=0; j<i; j++)
   1771 			(void)printf("%02x",
   1772 				     (u_char)opack_icmp.icmp_data[PHDR_LEN+j]);
   1773 		(void)printf("\n");
   1774 	}
   1775 
   1776 }
   1777 
   1778 
   1779 static void
   1780 rnd_fill(void)
   1781 {
   1782 	static u_int32_t rnd;
   1783 	int i;
   1784 
   1785 	for (i = PHDR_LEN; i < datalen; i++) {
   1786 		rnd = (3141592621U * rnd + 663896637U);
   1787 		opack_icmp.icmp_data[i] = rnd>>24;
   1788 	}
   1789 }
   1790 
   1791 
   1792 static void
   1793 gethost(const char *arg,
   1794 	const char *name,
   1795 	struct sockaddr_in *sa,
   1796 	char *realname,
   1797 	int realname_len)
   1798 {
   1799 	struct hostent *hp;
   1800 
   1801 	(void)memset(sa, 0, sizeof(*sa));
   1802 	sa->sin_family = AF_INET;
   1803 	sa->sin_len = sizeof(struct sockaddr_in);
   1804 
   1805 	/* If it is an IP address, try to convert it to a name to
   1806 	 * have something nice to display.
   1807 	 */
   1808 	if (inet_aton(name, &sa->sin_addr) != 0) {
   1809 		if (realname) {
   1810 			if (pingflags & F_NUMERIC)
   1811 				hp = 0;
   1812 			else
   1813 				hp = gethostbyaddr((char *)&sa->sin_addr,
   1814 						   sizeof(sa->sin_addr),
   1815 						   AF_INET);
   1816 			(void)strlcpy(realname, hp ? hp->h_name : name,
   1817 			    realname_len);
   1818 		}
   1819 		return;
   1820 	}
   1821 
   1822 	hp = gethostbyname(name);
   1823 	if (!hp)
   1824 		errx(1, "Cannot resolve \"%s\" (%s)",name,hstrerror(h_errno));
   1825 
   1826 	if (hp->h_addrtype != AF_INET)
   1827 		errx(1, "%s only supported with IP", arg);
   1828 
   1829 	(void)memcpy(&sa->sin_addr, hp->h_addr, sizeof(sa->sin_addr));
   1830 
   1831 	if (realname)
   1832 		(void)strlcpy(realname, hp->h_name, realname_len);
   1833 }
   1834 
   1835 
   1836 static void
   1837 usage(void)
   1838 {
   1839 #ifdef IPSEC
   1840 #ifdef IPSEC_POLICY_IPSEC
   1841 #define IPSECOPT	"\n     [-E policy] "
   1842 #else
   1843 #define IPSECOPT	"\n     [-AE] "
   1844 #endif /*IPSEC_POLICY_IPSEC*/
   1845 #else
   1846 #define IPSECOPT	""
   1847 #endif /*IPSEC*/
   1848 
   1849 	(void)fprintf(stderr, "usage: \n"
   1850 	    "%s [-adDfLnoPqQrRv] [-c count] [-g gateway] [-h host]"
   1851 	    " [-i interval] [-I addr]\n"
   1852 	    "     [-l preload] [-p pattern] [-s size] [-t tos] [-T ttl]"
   1853 	    " [-w maxwait] " IPSECOPT "host\n",
   1854 	    getprogname());
   1855 	exit(1);
   1856 }
   1857