xref: /netbsd-src/sbin/ping/ping.c (revision bbde328be4e75ea9ad02e9715ea13ca54b797ada)
1 /*	$NetBSD: ping.c,v 1.90 2009/11/02 00:47:09 christos 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.90 2009/11/02 00:47:09 christos Exp $");
62 #endif
63 
64 #include <stdio.h>
65 #include <stddef.h>
66 #include <errno.h>
67 #include <signal.h>
68 #include <sys/time.h>
69 #include <sys/types.h>
70 #include <sys/param.h>
71 #include <sys/socket.h>
72 #include <sys/file.h>
73 #include <termios.h>
74 #include <stdlib.h>
75 #include <unistd.h>
76 #include <poll.h>
77 #include <limits.h>
78 #include <math.h>
79 #include <string.h>
80 #include <err.h>
81 
82 #include <netinet/in_systm.h>
83 #include <netinet/in.h>
84 #include <netinet/ip.h>
85 #include <netinet/ip_icmp.h>
86 #include <netinet/ip_var.h>
87 #include <arpa/inet.h>
88 #include <ctype.h>
89 #include <netdb.h>
90 
91 #ifdef IPSEC
92 #include <netinet6/ipsec.h>
93 #endif /*IPSEC*/
94 
95 #define FLOOD_INTVL	0.01		/* default flood output interval */
96 #define	MAXPACKET	(IP_MAXPACKET-60-8)	/* max packet size */
97 
98 #define F_VERBOSE	0x0001
99 #define F_QUIET		0x0002		/* minimize all output */
100 #define F_SEMI_QUIET	0x0004		/* ignore our ICMP errors */
101 #define F_FLOOD		0x0008		/* flood-ping */
102 #define	F_RECORD_ROUTE	0x0010		/* record route */
103 #define F_SOURCE_ROUTE	0x0020		/* loose source route */
104 #define F_PING_FILLED	0x0040		/* is buffer filled with user data? */
105 #define F_PING_RANDOM	0x0080		/* use random data */
106 #define	F_NUMERIC	0x0100		/* do not do gethostbyaddr() calls */
107 #define F_TIMING	0x0200		/* room for a timestamp */
108 #define F_DF		0x0400		/* set IP DF bit */
109 #define F_SOURCE_ADDR	0x0800		/* set source IP address/interface */
110 #define F_ONCE		0x1000		/* exit(0) after receiving 1 reply */
111 #define F_MCAST		0x2000		/* multicast target */
112 #define F_MCAST_NOLOOP	0x4000		/* no multicast loopback */
113 #define F_AUDIBLE	0x8000		/* audible output */
114 #ifdef IPSEC
115 #ifdef IPSEC_POLICY_IPSEC
116 #define F_POLICY	0x10000
117 #else
118 #define	F_AUTHHDR	0x10000
119 #define	F_ENCRYPT	0x20000
120 #endif /*IPSEC_POLICY_IPSEC*/
121 #endif /*IPSEC*/
122 
123 
124 /* MAX_DUP_CHK is the number of bits in received table, the
125  *	maximum number of received sequence numbers we can track to check
126  *	for duplicates.
127  */
128 #define MAX_DUP_CHK     (8 * 2048)
129 u_char	rcvd_tbl[MAX_DUP_CHK/8];
130 int     nrepeats = 0;
131 #define A(seq)	rcvd_tbl[(seq/8)%sizeof(rcvd_tbl)]  /* byte in array */
132 #define B(seq)	(1 << (seq & 0x07))	/* bit in byte */
133 #define SET(seq) (A(seq) |= B(seq))
134 #define CLR(seq) (A(seq) &= (~B(seq)))
135 #define TST(seq) (A(seq) & B(seq))
136 
137 struct tv32 {
138 	int32_t tv32_sec;
139 	int32_t tv32_usec;
140 };
141 
142 
143 u_char	*packet;
144 int	packlen;
145 int	pingflags = 0, options;
146 int	pongflags = 0;
147 char	*fill_pat;
148 
149 int s;					/* Socket file descriptor */
150 int sloop;				/* Socket file descriptor/loopback */
151 
152 #define PHDR_LEN sizeof(struct tv32)	/* size of timestamp header */
153 struct sockaddr_in whereto, send_addr;	/* Who to ping */
154 struct sockaddr_in src_addr;		/* from where */
155 struct sockaddr_in loc_addr;		/* 127.1 */
156 int datalen = 64 - PHDR_LEN;		/* How much data */
157 
158 #ifndef __NetBSD__
159 static char *progname;
160 #define	getprogname()		(progname)
161 #define	setprogname(name)	((void)(progname = (name)))
162 #endif
163 
164 char hostname[MAXHOSTNAMELEN];
165 
166 static struct {
167 	struct ip	o_ip;
168 	char		o_opt[MAX_IPOPTLEN];
169 	union {
170 		u_char	    u_buf[MAXPACKET+offsetof(struct icmp, icmp_data)];
171 		struct icmp u_icmp;
172 	} o_u;
173 } out_pack;
174 #define	opack_icmp	out_pack.o_u.u_icmp
175 struct ip *opack_ip;
176 
177 char optspace[MAX_IPOPTLEN];		/* record route space */
178 int optlen;
179 
180 
181 int npackets;				/* total packets to send */
182 int preload;				/* number of packets to "preload" */
183 int ntransmitted;			/* output sequence # = #sent */
184 int ident;				/* our ID, in network byte order */
185 
186 int nreceived;				/* # of packets we got back */
187 
188 double interval;			/* interval between packets */
189 struct timeval interval_tv;
190 double tmin = 999999999.0;
191 double tmax = 0.0;
192 double tsum = 0.0;			/* sum of all times */
193 double tsumsq = 0.0;
194 double maxwait = 0.0;
195 
196 int bufspace = IP_MAXPACKET;
197 
198 struct timeval now, clear_cache, last_tx, next_tx, first_tx;
199 struct timeval last_rx, first_rx;
200 int lastrcvd = 1;			/* last ping sent has been received */
201 
202 static struct timeval jiggle_time;
203 static int jiggle_cnt, total_jiggled, jiggle_direction = -1;
204 
205 static void doit(void);
206 static void prefinish(int);
207 static void prtsig(int);
208 static void finish(int);
209 static void summary(int);
210 static void pinger(void);
211 static void fill(void);
212 static void rnd_fill(void);
213 static double diffsec(struct timeval *, struct timeval *);
214 static void timevaladd(struct timeval *, struct timeval *);
215 static void sec_to_timeval(const double, struct timeval *);
216 static double timeval_to_sec(const struct timeval *);
217 static void pr_pack(u_char *, int, struct sockaddr_in *);
218 static u_int16_t in_cksum(u_int16_t *, u_int);
219 static void pr_saddr(u_char *);
220 static char *pr_addr(struct in_addr *);
221 static void pr_iph(struct icmp *, int);
222 static void pr_retip(struct icmp *, int);
223 static int pr_icmph(struct icmp *, struct sockaddr_in *, int);
224 static void jiggle(int), jiggle_flush(int);
225 static void gethost(const char *, const char *,
226 		    struct sockaddr_in *, char *, int);
227 static void usage(void);
228 
229 int
230 main(int argc, char *argv[])
231 {
232 	int c, i, on = 1, hostind = 0;
233 	long l;
234 	u_char ttl = 0;
235 	u_long tos = 0;
236 	char *p;
237 #ifdef IPSEC
238 #ifdef IPSEC_POLICY_IPSEC
239 	char *policy_in = NULL;
240 	char *policy_out = NULL;
241 #endif
242 #endif
243 #ifdef SIGINFO
244 	struct sigaction sa;
245 #endif
246 
247 	if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
248 		err(1, "Cannot create socket");
249 	if ((sloop = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
250 		err(1, "Cannot create socket");
251 
252 	/*
253 	 * sloop is never read on.  This prevents packets from
254 	 * queueing in its recv buffer.
255 	 */
256 	if (shutdown(sloop, SHUT_RD) == -1)
257 		warn("Cannot shutdown for read");
258 
259 	if (setuid(getuid()) == -1)
260 		err(1, "setuid");
261 
262 	setprogname(argv[0]);
263 
264 #ifndef IPSEC
265 #define IPSECOPT
266 #else
267 #ifdef IPSEC_POLICY_IPSEC
268 #define IPSECOPT	"E:"
269 #else
270 #define IPSECOPT	"AE"
271 #endif /*IPSEC_POLICY_IPSEC*/
272 #endif
273 	while ((c = getopt(argc, argv,
274 			   "ac:dDfg:h:i:I:l:Lnop:PqQrRs:t:T:vw:" IPSECOPT)) != -1) {
275 #undef IPSECOPT
276 		switch (c) {
277 		case 'a':
278 			pingflags |= F_AUDIBLE;
279 			break;
280 		case 'c':
281 			npackets = strtol(optarg, &p, 0);
282 			if (*p != '\0' || npackets <= 0)
283 				errx(1, "Bad/invalid number of packets");
284 			break;
285 		case 'D':
286 			pingflags |= F_DF;
287 			break;
288 		case 'd':
289 			options |= SO_DEBUG;
290 			break;
291 		case 'f':
292 			pingflags |= F_FLOOD;
293 			break;
294 		case 'h':
295 			hostind = optind-1;
296 			break;
297 		case 'i':		/* wait between sending packets */
298 			interval = strtod(optarg, &p);
299 			if (*p != '\0' || interval <= 0)
300 				errx(1, "Bad/invalid interval %s", optarg);
301 			break;
302 		case 'l':
303 			preload = strtol(optarg, &p, 0);
304 			if (*p != '\0' || preload < 0)
305 				errx(1, "Bad/invalid preload value %s",
306 				     optarg);
307 			break;
308 		case 'n':
309 			pingflags |= F_NUMERIC;
310 			break;
311 		case 'o':
312 			pingflags |= F_ONCE;
313 			break;
314 		case 'p':		/* fill buffer with user pattern */
315 			if (pingflags & F_PING_RANDOM)
316 				errx(1, "Only one of -P and -p allowed");
317 			pingflags |= F_PING_FILLED;
318 			fill_pat = optarg;
319 			break;
320 		case 'P':
321 			if (pingflags & F_PING_FILLED)
322 				errx(1, "Only one of -P and -p allowed");
323 			pingflags |= F_PING_RANDOM;
324 			break;
325 		case 'q':
326 			pingflags |= F_QUIET;
327 			break;
328 		case 'Q':
329 			pingflags |= F_SEMI_QUIET;
330 			break;
331 		case 'r':
332 			options |= SO_DONTROUTE;
333 			break;
334 		case 's':		/* size of packet to send */
335 			l = strtol(optarg, &p, 0);
336 			if (*p != '\0' || l < 0)
337 				errx(1, "Bad/invalid packet size %s", optarg);
338 			if (l > MAXPACKET)
339 				errx(1, "packet size is too large");
340 			datalen = (int)l;
341 			break;
342 		case 'v':
343 			pingflags |= F_VERBOSE;
344 			break;
345 		case 'R':
346 			pingflags |= F_RECORD_ROUTE;
347 			break;
348 		case 'L':
349 			pingflags |= F_MCAST_NOLOOP;
350 			break;
351 		case 't':
352 			tos = strtoul(optarg, &p, 0);
353 			if (*p != '\0' ||  tos > 0xFF)
354 				errx(1, "bad tos value: %s", optarg);
355 			break;
356 		case 'T':
357 			l = strtol(optarg, &p, 0);
358 			if (*p != '\0' || l > 255 || l <= 0)
359 				errx(1, "ttl out of range");
360 			ttl = (u_char)l;    /* cannot check >255 otherwise */
361 			break;
362 		case 'I':
363 			pingflags |= F_SOURCE_ADDR;
364 			gethost("-I", optarg, &src_addr, 0, 0);
365 			break;
366 		case 'g':
367 			pingflags |= F_SOURCE_ROUTE;
368 			gethost("-g", optarg, &send_addr, 0, 0);
369 			break;
370 		case 'w':
371 			maxwait = strtod(optarg, &p);
372 			if (*p != '\0' || maxwait <= 0)
373 				errx(1, "Bad/invalid maxwait time %s", optarg);
374 			break;
375 #ifdef IPSEC
376 #ifdef IPSEC_POLICY_IPSEC
377 		case 'E':
378 			pingflags |= F_POLICY;
379 			if (!strncmp("in", optarg, 2)) {
380 				policy_in = strdup(optarg);
381 				if (!policy_in)
382 					err(1, "strdup");
383 			} else if (!strncmp("out", optarg, 3)) {
384 				policy_out = strdup(optarg);
385 				if (!policy_out)
386 					err(1, "strdup");
387 			} else
388 				errx(1, "invalid security policy");
389 			break;
390 #else
391 		case 'A':
392 			pingflags |= F_AUTHHDR;
393 			break;
394 		case 'E':
395 			pingflags |= F_ENCRYPT;
396 			break;
397 #endif /*IPSEC_POLICY_IPSEC*/
398 #endif /*IPSEC*/
399 		default:
400 			usage();
401 			break;
402 		}
403 	}
404 
405 	if (interval == 0)
406 		interval = (pingflags & F_FLOOD) ? FLOOD_INTVL : 1.0;
407 #ifndef sgi
408 	if (pingflags & F_FLOOD && getuid())
409 		errx(1, "Must be superuser to use -f");
410 	if (interval < 1.0 && getuid())
411 		errx(1, "Must be superuser to use < 1 sec ping interval");
412 	if (preload > 0 && getuid())
413 		errx(1, "Must be superuser to use -l");
414 #endif
415 	sec_to_timeval(interval, &interval_tv);
416 
417 	if ((pingflags & (F_AUDIBLE|F_FLOOD)) == (F_AUDIBLE|F_FLOOD))
418 		warnx("Sorry, no audible output for flood pings");
419 
420 	if (npackets != 0) {
421 		npackets += preload;
422 	} else {
423 		npackets = INT_MAX;
424 	}
425 
426 	if (hostind == 0) {
427 		if (optind != argc-1)
428 			usage();
429 		else
430 			hostind = optind;
431 	}
432 	else if (hostind >= argc - 1)
433 		usage();
434 
435 	gethost("", argv[hostind], &whereto, hostname, sizeof(hostname));
436 	if (IN_MULTICAST(ntohl(whereto.sin_addr.s_addr)))
437 		pingflags |= F_MCAST;
438 	if (!(pingflags & F_SOURCE_ROUTE))
439 		(void) memcpy(&send_addr, &whereto, sizeof(send_addr));
440 
441 	loc_addr.sin_family = AF_INET;
442 	loc_addr.sin_len = sizeof(struct sockaddr_in);
443 	loc_addr.sin_addr.s_addr = htonl((127<<24)+1);
444 
445 	if (datalen >= (int)PHDR_LEN)	/* can we time them? */
446 		pingflags |= F_TIMING;
447 	packlen = datalen + 60 + 76;	/* MAXIP + MAXICMP */
448 	if ((packet = (u_char *)malloc(packlen)) == NULL)
449 		err(1, "Out of memory");
450 
451 	if (pingflags & F_PING_FILLED) {
452 		fill();
453 	} else if (pingflags & F_PING_RANDOM) {
454 		rnd_fill();
455 	} else {
456 		for (i = PHDR_LEN; i < datalen; i++)
457 			opack_icmp.icmp_data[i] = i;
458 	}
459 
460 	ident = arc4random() & 0xFFFF;
461 
462 	if (options & SO_DEBUG) {
463 		if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
464 			       (char *)&on, sizeof(on)) == -1)
465 			warn("Can't turn on socket debugging");
466 	}
467 	if (options & SO_DONTROUTE) {
468 		if (setsockopt(s, SOL_SOCKET, SO_DONTROUTE,
469 			       (char *)&on, sizeof(on)) == -1)
470 			warn("SO_DONTROUTE");
471 	}
472 
473 	if (options & SO_DEBUG) {
474 		if (setsockopt(sloop, SOL_SOCKET, SO_DEBUG,
475 			       (char *)&on, sizeof(on)) == -1)
476 			warn("Can't turn on socket debugging");
477 	}
478 	if (options & SO_DONTROUTE) {
479 		if (setsockopt(sloop, SOL_SOCKET, SO_DONTROUTE,
480 			       (char *)&on, sizeof(on)) == -1)
481 			warn("SO_DONTROUTE");
482 	}
483 
484 	if (pingflags & F_SOURCE_ROUTE) {
485 		optspace[IPOPT_OPTVAL] = IPOPT_LSRR;
486 		optspace[IPOPT_OLEN] = optlen = 7;
487 		optspace[IPOPT_OFFSET] = IPOPT_MINOFF;
488 		(void)memcpy(&optspace[IPOPT_MINOFF-1], &whereto.sin_addr,
489 			     sizeof(whereto.sin_addr));
490 		optspace[optlen++] = IPOPT_NOP;
491 	}
492 	if (pingflags & F_RECORD_ROUTE) {
493 		optspace[optlen+IPOPT_OPTVAL] = IPOPT_RR;
494 		optspace[optlen+IPOPT_OLEN] = (MAX_IPOPTLEN -1-optlen);
495 		optspace[optlen+IPOPT_OFFSET] = IPOPT_MINOFF;
496 		optlen = MAX_IPOPTLEN;
497 	}
498 	/* this leaves opack_ip 0(mod 4) aligned */
499 	opack_ip = (struct ip *)((char *)&out_pack.o_ip
500 				 + sizeof(out_pack.o_opt)
501 				 - optlen);
502 	(void) memcpy(opack_ip + 1, optspace, optlen);
503 
504 	if (setsockopt(s,IPPROTO_IP,IP_HDRINCL, (char *) &on, sizeof(on)) < 0)
505 		err(1, "Can't set special IP header");
506 
507 	opack_ip->ip_v = IPVERSION;
508 	opack_ip->ip_hl = (sizeof(struct ip)+optlen) >> 2;
509 	opack_ip->ip_tos = tos;
510 	opack_ip->ip_off = (pingflags & F_DF) ? IP_DF : 0;
511 	opack_ip->ip_ttl = ttl ? ttl : MAXTTL;
512 	opack_ip->ip_p = IPPROTO_ICMP;
513 	opack_ip->ip_src = src_addr.sin_addr;
514 	opack_ip->ip_dst = send_addr.sin_addr;
515 
516 	if (pingflags & F_MCAST) {
517 		if (pingflags & F_MCAST_NOLOOP) {
518 			u_char loop = 0;
519 			if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP,
520 			    (char *) &loop, 1) < 0)
521 				err(1, "Can't disable multicast loopback");
522 		}
523 
524 		if (ttl != 0
525 		    && setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
526 		    (char *) &ttl, 1) < 0)
527 			err(1, "Can't set multicast time-to-live");
528 
529 		if ((pingflags & F_SOURCE_ADDR)
530 		    && setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
531 				  (char *) &src_addr.sin_addr,
532 				  sizeof(src_addr.sin_addr)) < 0)
533 			err(1, "Can't set multicast source interface");
534 
535 	} else if (pingflags & F_SOURCE_ADDR) {
536 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
537 			       (char *) &src_addr.sin_addr,
538 			       sizeof(src_addr.sin_addr)) < 0)
539 			err(1, "Can't set source interface/address");
540 	}
541 #ifdef IPSEC
542 #ifdef IPSEC_POLICY_IPSEC
543     {
544 	char *buf;
545 	if (pingflags & F_POLICY) {
546 		if (policy_in != NULL) {
547 			buf = ipsec_set_policy(policy_in, strlen(policy_in));
548 			if (buf == NULL)
549 				errx(1, "%s", ipsec_strerror());
550 			if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY,
551 					buf, ipsec_get_policylen(buf)) < 0) {
552 				err(1, "ipsec policy cannot be configured");
553 			}
554 			free(buf);
555 		}
556 		if (policy_out != NULL) {
557 			buf = ipsec_set_policy(policy_out, strlen(policy_out));
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 	}
567 	buf = ipsec_set_policy("out bypass", strlen("out bypass"));
568 	if (buf == NULL)
569 		errx(1, "%s", ipsec_strerror());
570 	if (setsockopt(sloop, IPPROTO_IP, IP_IPSEC_POLICY,
571 			buf, ipsec_get_policylen(buf)) < 0) {
572 #if 0
573 		warnx("ipsec is not configured");
574 #else
575 		/* ignore it, should be okay */
576 #endif
577 	}
578 	free(buf);
579     }
580 #else
581     {
582 	int optval;
583 	if (pingflags & F_AUTHHDR) {
584 		optval = IPSEC_LEVEL_REQUIRE;
585 #ifdef IP_AUTH_TRANS_LEVEL
586 		(void)setsockopt(s, IPPROTO_IP, IP_AUTH_TRANS_LEVEL,
587 			(char *)&optval, sizeof(optval));
588 #else
589 		(void)setsockopt(s, IPPROTO_IP, IP_AUTH_LEVEL,
590 			(char *)&optval, sizeof(optval));
591 #endif
592 	}
593 	if (pingflags & F_ENCRYPT) {
594 		optval = IPSEC_LEVEL_REQUIRE;
595 		(void)setsockopt(s, IPPROTO_IP, IP_ESP_TRANS_LEVEL,
596 			(char *)&optval, sizeof(optval));
597 	}
598 	optval = IPSEC_LEVEL_BYPASS;
599 #ifdef IP_AUTH_TRANS_LEVEL
600 	(void)setsockopt(sloop, IPPROTO_IP, IP_AUTH_TRANS_LEVEL,
601 		(char *)&optval, sizeof(optval));
602 #else
603 	(void)setsockopt(sloop, IPPROTO_IP, IP_AUTH_LEVEL,
604 		(char *)&optval, sizeof(optval));
605 #endif
606 	(void)setsockopt(sloop, IPPROTO_IP, IP_ESP_TRANS_LEVEL,
607 		(char *)&optval, sizeof(optval));
608     }
609 #endif /*IPSEC_POLICY_IPSEC*/
610 #endif /*IPSEC*/
611 
612 	(void)printf("PING %s (%s): %d data bytes\n", hostname,
613 		     inet_ntoa(whereto.sin_addr), datalen);
614 
615 	/* When pinging the broadcast address, you can get a lot
616 	 * of answers.  Doing something so evil is useful if you
617 	 * are trying to stress the ethernet, or just want to
618 	 * fill the arp cache to get some stuff for /etc/ethers.
619 	 */
620 	while (0 > setsockopt(s, SOL_SOCKET, SO_RCVBUF,
621 			      (char*)&bufspace, sizeof(bufspace))) {
622 		if ((bufspace -= 4096) <= 0)
623 			err(1, "Cannot set the receive buffer size");
624 	}
625 
626 	/* make it possible to send giant probes, but do not worry now
627 	 * if it fails, since we probably won't send giant probes.
628 	 */
629 	(void)setsockopt(s, SOL_SOCKET, SO_SNDBUF,
630 			 (char*)&bufspace, sizeof(bufspace));
631 
632 	(void)signal(SIGINT, prefinish);
633 
634 #ifdef SIGINFO
635 	sa.sa_handler = prtsig;
636 	sa.sa_flags = SA_NOKERNINFO;
637 	sigemptyset(&sa.sa_mask);
638 	(void)sigaction(SIGINFO, &sa, NULL);
639 #else
640 	(void)signal(SIGQUIT, prtsig);
641 #endif
642 	(void)signal(SIGCONT, prtsig);
643 
644 	/* fire off them quickies */
645 	for (i = 0; i < preload; i++) {
646 		(void)gettimeofday(&now, 0);
647 		pinger();
648 	}
649 
650 	doit();
651 	return 0;
652 }
653 
654 
655 static void
656 doit(void)
657 {
658 	int cc;
659 	struct sockaddr_in from;
660 	socklen_t fromlen;
661 	double sec, last, d_last;
662 	struct pollfd fdmaskp[1];
663 
664 	(void)gettimeofday(&clear_cache,0);
665 	if (maxwait != 0) {
666 		last = timeval_to_sec(&clear_cache) + maxwait;
667 		d_last = 0;
668 	} else {
669 		last = 0;
670 		d_last = 365*24*60*60;
671 	}
672 
673 	do {
674 		(void)gettimeofday(&now,0);
675 
676 		if (last != 0)
677 			d_last = last - timeval_to_sec(&now);
678 
679 		if (ntransmitted < npackets && d_last > 0) {
680 			/* send if within 100 usec or late for next packet */
681 			sec = diffsec(&next_tx,&now);
682 			if (sec <= 0.0001 ||
683 			    (lastrcvd && (pingflags & F_FLOOD))) {
684 				pinger();
685 				sec = diffsec(&next_tx,&now);
686 			}
687 			if (sec < 0.0)
688 				sec = 0.0;
689 			if (d_last < sec)
690 				sec = d_last;
691 
692 		} else {
693 			/* For the last response, wait twice as long as the
694 			 * worst case seen, or 10 times as long as the
695 			 * maximum interpacket interval, whichever is longer.
696 			 */
697 			sec = MAX(2 * tmax, 10 * interval) -
698 			    diffsec(&now, &last_tx);
699 			if (d_last < sec)
700 				sec = d_last;
701 			if (sec <= 0)
702 				break;
703 		}
704 
705 
706 		fdmaskp[0].fd = s;
707 		fdmaskp[0].events = POLLIN;
708 		cc = poll(fdmaskp, 1, (int)(sec * 1000));
709 		if (cc <= 0) {
710 			if (cc < 0) {
711 				if (errno == EINTR)
712 					continue;
713 				jiggle_flush(1);
714 				err(1, "poll");
715 			}
716 			continue;
717 		}
718 
719 		fromlen  = sizeof(from);
720 		cc = recvfrom(s, (char *) packet, packlen,
721 			      0, (struct sockaddr *)&from,
722 			      &fromlen);
723 		if (cc < 0) {
724 			if (errno != EINTR) {
725 				jiggle_flush(1);
726 				warn("recvfrom");
727 				(void)fflush(stderr);
728 			}
729 			continue;
730 		}
731 		(void)gettimeofday(&now, 0);
732 		pr_pack(packet, cc, &from);
733 
734 	} while (nreceived < npackets
735 		 && (nreceived == 0 || !(pingflags & F_ONCE)));
736 
737 	finish(0);
738 }
739 
740 
741 static void
742 jiggle_flush(int nl)			/* new line if there are dots */
743 {
744 	int serrno = errno;
745 
746 	if (jiggle_cnt > 0) {
747 		total_jiggled += jiggle_cnt;
748 		jiggle_direction = 1;
749 		do {
750 			(void)putchar('.');
751 		} while (--jiggle_cnt > 0);
752 
753 	} else if (jiggle_cnt < 0) {
754 		total_jiggled -= jiggle_cnt;
755 		jiggle_direction = -1;
756 		do {
757 			(void)putchar('\b');
758 		} while (++jiggle_cnt < 0);
759 	}
760 
761 	if (nl) {
762 		if (total_jiggled != 0)
763 			(void)putchar('\n');
764 		total_jiggled = 0;
765 		jiggle_direction = -1;
766 	}
767 
768 	(void)fflush(stdout);
769 	(void)fflush(stderr);
770 	jiggle_time = now;
771 	errno = serrno;
772 }
773 
774 
775 /* jiggle the cursor for flood-ping
776  */
777 static void
778 jiggle(int delta)
779 {
780 	double dt;
781 
782 	if (pingflags & F_QUIET)
783 		return;
784 
785 	/* do not back up into messages */
786 	if (total_jiggled+jiggle_cnt+delta < 0)
787 		return;
788 
789 	jiggle_cnt += delta;
790 
791 	/* flush the FLOOD dots when things are quiet
792 	 * or occassionally to make the cursor jiggle.
793 	 */
794 	dt = diffsec(&last_tx, &jiggle_time);
795 	if (dt > 0.2 || (dt >= 0.15 && delta*jiggle_direction < 0))
796 		jiggle_flush(0);
797 }
798 
799 
800 /*
801  * Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
802  * will be added on by the kernel.  The ID field is our UNIX process ID,
803  * and the sequence number is an ascending integer.  The first PHDR_LEN bytes
804  * of the data portion are used to hold a UNIX "timeval" struct in VAX
805  * byte-order, to compute the round-trip time.
806  */
807 static void
808 pinger(void)
809 {
810 	struct tv32 tv32;
811 	int i, cc, sw;
812 
813 	opack_icmp.icmp_code = 0;
814 	opack_icmp.icmp_seq = htons((u_int16_t)(ntransmitted));
815 
816 	/* clear the cached route in the kernel after an ICMP
817 	 * response such as a Redirect is seen to stop causing
818 	 * more such packets.  Also clear the cached route
819 	 * periodically in case of routing changes that make
820 	 * black holes come and go.
821 	 */
822 	if (clear_cache.tv_sec != now.tv_sec) {
823 		opack_icmp.icmp_type = ICMP_ECHOREPLY;
824 		opack_icmp.icmp_id = ~ident;
825 		opack_icmp.icmp_cksum = 0;
826 		opack_icmp.icmp_cksum = in_cksum((u_int16_t *)&opack_icmp,
827 		    PHDR_LEN);
828 		sw = 0;
829 		if (setsockopt(sloop,IPPROTO_IP,IP_HDRINCL,
830 			       (char *)&sw,sizeof(sw)) < 0)
831 			err(1, "Can't turn off special IP header");
832 		if (sendto(sloop, (char *) &opack_icmp, PHDR_LEN, MSG_DONTROUTE,
833 			   (struct sockaddr *)&loc_addr,
834 			   sizeof(struct sockaddr_in)) < 0) {
835 			/*
836 			 * XXX: we only report this as a warning in verbose
837 			 * mode because people get confused when they see
838 			 * this error when they are running in single user
839 			 * mode and they have not configured lo0
840 			 */
841 			if (pingflags & F_VERBOSE)
842 				warn("failed to clear cached route");
843 		}
844 		sw = 1;
845 		if (setsockopt(sloop,IPPROTO_IP,IP_HDRINCL,
846 			       (char *)&sw, sizeof(sw)) < 0)
847 			err(1, "Can't set special IP header");
848 
849 		(void)gettimeofday(&clear_cache,0);
850 	}
851 
852 	opack_icmp.icmp_type = ICMP_ECHO;
853 	opack_icmp.icmp_id = ident;
854 	tv32.tv32_sec = htonl(now.tv_sec);
855 	tv32.tv32_usec = htonl(now.tv_usec);
856 	if (pingflags & F_TIMING)
857 		(void) memcpy(&opack_icmp.icmp_data[0], &tv32, sizeof(tv32));
858 	cc = datalen + PHDR_LEN;
859 	opack_icmp.icmp_cksum = 0;
860 	opack_icmp.icmp_cksum = in_cksum((u_int16_t *)&opack_icmp, cc);
861 
862 	cc += opack_ip->ip_hl<<2;
863 	opack_ip->ip_len = cc;
864 	i = sendto(s, (char *) opack_ip, cc, 0,
865 		   (struct sockaddr *)&send_addr, sizeof(struct sockaddr_in));
866 	if (i != cc) {
867 		jiggle_flush(1);
868 		if (i < 0)
869 			warn("sendto");
870 		else
871 			warnx("wrote %s %d chars, ret=%d", hostname, cc, i);
872 		(void)fflush(stderr);
873 	}
874 	lastrcvd = 0;
875 
876 	CLR(ntransmitted);
877 	ntransmitted++;
878 
879 	last_tx = now;
880 	if (next_tx.tv_sec == 0) {
881 		first_tx = now;
882 		next_tx = now;
883 	}
884 
885 	/* Transmit regularly, at always the same microsecond in the
886 	 * second when going at one packet per second.
887 	 * If we are at most 100 ms behind, send extras to get caught up.
888 	 * Otherwise, skip packets we were too slow to send.
889 	 */
890 	if (diffsec(&next_tx, &now) <= interval) {
891 		do {
892 			timevaladd(&next_tx, &interval_tv);
893 		} while (diffsec(&next_tx, &now) < -0.1);
894 	}
895 
896 	if (pingflags & F_FLOOD)
897 		jiggle(1);
898 
899 	/* While the packet is going out, ready buffer for the next
900 	 * packet. Use a fast but not very good random number generator.
901 	 */
902 	if (pingflags & F_PING_RANDOM)
903 		rnd_fill();
904 }
905 
906 
907 static void
908 pr_pack_sub(int cc,
909 	    char *addr,
910 	    int seqno,
911 	    int dupflag,
912 	    int ttl,
913 	    double triptime)
914 {
915 	jiggle_flush(1);
916 
917 	if (pingflags & F_FLOOD)
918 		return;
919 
920 	(void)printf("%d bytes from %s: icmp_seq=%u", cc, addr, seqno);
921 	if (dupflag)
922 		(void)printf(" DUP!");
923 	(void)printf(" ttl=%d", ttl);
924 	if (pingflags & F_TIMING)
925 		(void)printf(" time=%.3f ms", triptime*1000.0);
926 
927 	/*
928 	 * Send beep to stderr, since that's more likely than stdout
929 	 * to go to a terminal..
930 	 */
931 	if (pingflags & F_AUDIBLE && !dupflag)
932 		(void)fprintf(stderr,"\a");
933 }
934 
935 
936 /*
937  * Print out the packet, if it came from us.  This logic is necessary
938  * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
939  * which arrive ('tis only fair).  This permits multiple copies of this
940  * program to be run without having intermingled output (or statistics!).
941  */
942 static void
943 pr_pack(u_char *buf,
944 	int tot_len,
945 	struct sockaddr_in *from)
946 {
947 	struct ip *ip;
948 	struct icmp *icp;
949 	int i, j, net_len;
950 	u_char *cp;
951 	static int old_rrlen;
952 	static char old_rr[MAX_IPOPTLEN];
953 	int hlen, dupflag = 0, dumped;
954 	double triptime = 0.0;
955 #define PR_PACK_SUB() {if (!dumped) {			\
956 	dumped = 1;					\
957 	pr_pack_sub(net_len, inet_ntoa(from->sin_addr),	\
958 		    ntohs((u_int16_t)icp->icmp_seq),	\
959 		    dupflag, ip->ip_ttl, triptime);}}
960 
961 	/* Check the IP header */
962 	ip = (struct ip *) buf;
963 	hlen = ip->ip_hl << 2;
964 	if (tot_len < hlen + ICMP_MINLEN) {
965 		if (pingflags & F_VERBOSE) {
966 			jiggle_flush(1);
967 			(void)printf("packet too short (%d bytes) from %s\n",
968 				     tot_len, inet_ntoa(from->sin_addr));
969 		}
970 		return;
971 	}
972 
973 	/* Now the ICMP part */
974 	dumped = 0;
975 	net_len = tot_len - hlen;
976 	icp = (struct icmp *)(buf + hlen);
977 	if (icp->icmp_type == ICMP_ECHOREPLY
978 	    && icp->icmp_id == ident) {
979 		if (icp->icmp_seq == htons((u_int16_t)(ntransmitted-1)))
980 			lastrcvd = 1;
981 		last_rx = now;
982 		if (first_rx.tv_sec == 0)
983 			first_rx = last_rx;
984 		nreceived++;
985 		if (pingflags & F_TIMING) {
986 			struct timeval tv;
987 			struct tv32 tv32;
988 
989 			(void) memcpy(&tv32, icp->icmp_data, sizeof(tv32));
990 			tv.tv_sec = ntohl(tv32.tv32_sec);
991 			tv.tv_usec = ntohl(tv32.tv32_usec);
992 			triptime = diffsec(&last_rx, &tv);
993 			tsum += triptime;
994 			tsumsq += triptime * triptime;
995 			if (triptime < tmin)
996 				tmin = triptime;
997 			if (triptime > tmax)
998 				tmax = triptime;
999 		}
1000 
1001 		if (TST(ntohs((u_int16_t)icp->icmp_seq))) {
1002 			nrepeats++, nreceived--;
1003 			dupflag=1;
1004 		} else {
1005 			SET(ntohs((u_int16_t)icp->icmp_seq));
1006 		}
1007 
1008 		if (tot_len != opack_ip->ip_len) {
1009 			PR_PACK_SUB();
1010 			switch (opack_ip->ip_len - tot_len) {
1011 			case MAX_IPOPTLEN:
1012 				if ((pongflags & F_RECORD_ROUTE) != 0)
1013 					break;
1014 				if ((pingflags & F_RECORD_ROUTE) == 0)
1015 					goto out;
1016 				pongflags |= F_RECORD_ROUTE;
1017 				(void)printf("\nremote host does not "
1018 				    "support record route");
1019 				break;
1020 			case 8:
1021 				if ((pongflags & F_SOURCE_ROUTE) != 0)
1022 					break;
1023 				if ((pingflags & F_SOURCE_ROUTE) == 0)
1024 					goto out;
1025 				pongflags |= F_SOURCE_ROUTE;
1026 				(void)printf("\nremote host does not "
1027 				    "support source route");
1028 				break;
1029 			default:
1030 			out:
1031 				(void)printf("\nwrong total length %d "
1032 				    "instead of %d", tot_len, opack_ip->ip_len);
1033 				break;
1034 			}
1035 		}
1036 
1037 		if (!dupflag) {
1038 			static u_int16_t last_seqno = 0xffff;
1039 			u_int16_t seqno = ntohs((u_int16_t)icp->icmp_seq);
1040 			u_int16_t gap = seqno - (last_seqno + 1);
1041 			if (gap > 0 && gap < 0x8000 &&
1042 			    (pingflags & F_VERBOSE)) {
1043 				(void)printf("[*** sequence gap of %u "
1044 				    "packets from %u ... %u ***]\n", gap,
1045 				    (u_int16_t) (last_seqno + 1),
1046 				    (u_int16_t) (seqno - 1));
1047 				if (pingflags & F_QUIET)
1048 					summary(0);
1049 			}
1050 
1051 			if (gap < 0x8000)
1052 				last_seqno = seqno;
1053 		}
1054 
1055 		if (pingflags & F_QUIET)
1056 			return;
1057 
1058 		if (!(pingflags & F_FLOOD))
1059 			PR_PACK_SUB();
1060 
1061 		/* check the data */
1062 		if (datalen > (int)PHDR_LEN
1063 		    && !(pingflags & F_PING_RANDOM)
1064 		    && memcmp(&icp->icmp_data[PHDR_LEN],
1065 			    &opack_icmp.icmp_data[PHDR_LEN],
1066 			    datalen-PHDR_LEN)) {
1067 			for (i=PHDR_LEN; i<datalen; i++) {
1068 				if (icp->icmp_data[i] !=
1069 				    opack_icmp.icmp_data[i])
1070 					break;
1071 			}
1072 			PR_PACK_SUB();
1073 			(void)printf("\nwrong data byte #%d should have been"
1074 				     " %#x but was %#x", i,
1075 				     (u_char)opack_icmp.icmp_data[i],
1076 				     (u_char)icp->icmp_data[i]);
1077 			for (i=PHDR_LEN; i<datalen; i++) {
1078 				if ((i%16) == PHDR_LEN)
1079 					(void)printf("\n\t");
1080 				(void)printf("%2x ",(u_char)icp->icmp_data[i]);
1081 			}
1082 		}
1083 
1084 	} else {
1085 		if (!pr_icmph(icp, from, net_len))
1086 			return;
1087 		dumped = 2;
1088 	}
1089 
1090 	/* Display any IP options */
1091 	cp = buf + sizeof(struct ip);
1092 	while (hlen > (int)sizeof(struct ip)) {
1093 		switch (*cp) {
1094 		case IPOPT_EOL:
1095 			hlen = 0;
1096 			break;
1097 		case IPOPT_LSRR:
1098 			hlen -= 2;
1099 			j = *++cp;
1100 			++cp;
1101 			j -= IPOPT_MINOFF;
1102 			if (j <= 0)
1103 				continue;
1104 			if (dumped <= 1) {
1105 				j = ((j+3)/4)*4;
1106 				hlen -= j;
1107 				cp += j;
1108 				break;
1109 			}
1110 			PR_PACK_SUB();
1111 			(void)printf("\nLSRR: ");
1112 			for (;;) {
1113 				pr_saddr(cp);
1114 				cp += 4;
1115 				hlen -= 4;
1116 				j -= 4;
1117 				if (j <= 0)
1118 					break;
1119 				(void)putchar('\n');
1120 			}
1121 			break;
1122 		case IPOPT_RR:
1123 			j = *++cp;	/* get length */
1124 			i = *++cp;	/* and pointer */
1125 			hlen -= 2;
1126 			if (i > j)
1127 				i = j;
1128 			i -= IPOPT_MINOFF;
1129 			if (i <= 0)
1130 				continue;
1131 			if (dumped <= 1) {
1132 				if (i == old_rrlen
1133 				    && !memcmp(cp, old_rr, i)) {
1134 					if (dumped)
1135 					    (void)printf("\t(same route)");
1136 					j = ((i+3)/4)*4;
1137 					hlen -= j;
1138 					cp += j;
1139 					break;
1140 				}
1141 				old_rrlen = i;
1142 				(void) memcpy(old_rr, cp, i);
1143 			}
1144 			if (!dumped) {
1145 				jiggle_flush(1);
1146 				(void)printf("RR: ");
1147 				dumped = 1;
1148 			} else {
1149 				(void)printf("\nRR: ");
1150 			}
1151 			for (;;) {
1152 				pr_saddr(cp);
1153 				cp += 4;
1154 				hlen -= 4;
1155 				i -= 4;
1156 				if (i <= 0)
1157 					break;
1158 				(void)putchar('\n');
1159 			}
1160 			break;
1161 		case IPOPT_NOP:
1162 			if (dumped <= 1)
1163 				break;
1164 			PR_PACK_SUB();
1165 			(void)printf("\nNOP");
1166 			break;
1167 #ifdef sgi
1168 		case IPOPT_SECURITY:	/* RFC 1108 RIPSO BSO */
1169 		case IPOPT_ESO:		/* RFC 1108 RIPSO ESO */
1170 		case IPOPT_CIPSO:	/* Commercial IPSO */
1171 			if ((sysconf(_SC_IP_SECOPTS)) > 0) {
1172 				i = (unsigned)cp[1];
1173 				hlen -= i - 1;
1174 				PR_PACK_SUB();
1175 				(void)printf("\nSEC:");
1176 				while (i--) {
1177 					(void)printf(" %02x", *cp++);
1178 				}
1179 				cp--;
1180 				break;
1181 			}
1182 #endif
1183 		default:
1184 			PR_PACK_SUB();
1185 			(void)printf("\nunknown option 0x%x", *cp);
1186 			break;
1187 		}
1188 		hlen--;
1189 		cp++;
1190 	}
1191 
1192 	if (dumped) {
1193 		(void)putchar('\n');
1194 		(void)fflush(stdout);
1195 	} else {
1196 		jiggle(-1);
1197 	}
1198 }
1199 
1200 
1201 /* Compute the IP checksum
1202  *	This assumes the packet is less than 32K long.
1203  */
1204 static u_int16_t
1205 in_cksum(u_int16_t *p, u_int len)
1206 {
1207 	u_int32_t sum = 0;
1208 	int nwords = len >> 1;
1209 
1210 	while (nwords-- != 0)
1211 		sum += *p++;
1212 
1213 	if (len & 1) {
1214 		union {
1215 			u_int16_t w;
1216 			u_int8_t c[2];
1217 		} u;
1218 		u.c[0] = *(u_char *)p;
1219 		u.c[1] = 0;
1220 		sum += u.w;
1221 	}
1222 
1223 	/* end-around-carry */
1224 	sum = (sum >> 16) + (sum & 0xffff);
1225 	sum += (sum >> 16);
1226 	return (~sum);
1227 }
1228 
1229 
1230 /*
1231  * compute the difference of two timevals in seconds
1232  */
1233 static double
1234 diffsec(struct timeval *timenow,
1235 	struct timeval *then)
1236 {
1237 	return ((timenow->tv_sec - then->tv_sec)*1.0
1238 		+ (timenow->tv_usec - then->tv_usec)/1000000.0);
1239 }
1240 
1241 
1242 static void
1243 timevaladd(struct timeval *t1,
1244 	   struct timeval *t2)
1245 {
1246 
1247 	t1->tv_sec += t2->tv_sec;
1248 	if ((t1->tv_usec += t2->tv_usec) >= 1000000) {
1249 		t1->tv_sec++;
1250 		t1->tv_usec -= 1000000;
1251 	}
1252 }
1253 
1254 
1255 static void
1256 sec_to_timeval(const double sec, struct timeval *tp)
1257 {
1258 	tp->tv_sec = sec;
1259 	tp->tv_usec = (sec - tp->tv_sec) * 1000000.0;
1260 }
1261 
1262 
1263 static double
1264 timeval_to_sec(const struct timeval *tp)
1265 {
1266 	return tp->tv_sec + tp->tv_usec / 1000000.0;
1267 }
1268 
1269 
1270 /*
1271  * Print statistics.
1272  * Heavily buffered STDIO is used here, so that all the statistics
1273  * will be written with 1 sys-write call.  This is nice when more
1274  * than one copy of the program is running on a terminal;  it prevents
1275  * the statistics output from becomming intermingled.
1276  */
1277 static void
1278 summary(int header)
1279 {
1280 	jiggle_flush(1);
1281 
1282 	if (header)
1283 		(void)printf("\n----%s PING Statistics----\n", hostname);
1284 	(void)printf("%d packets transmitted, ", ntransmitted);
1285 	(void)printf("%d packets received, ", nreceived);
1286 	if (nrepeats)
1287 		(void)printf("+%d duplicates, ", nrepeats);
1288 	if (ntransmitted) {
1289 		if (nreceived > ntransmitted)
1290 			(void)printf("-- somebody's duplicating packets!");
1291 		else
1292 			(void)printf("%.1f%% packet loss",
1293 				     (((ntransmitted-nreceived)*100.0) /
1294 					    ntransmitted));
1295 	}
1296 	(void)printf("\n");
1297 	if (nreceived && (pingflags & F_TIMING)) {
1298 		double n = nreceived + nrepeats;
1299 		double avg = (tsum / n);
1300 		double variance = 0.0;
1301 		if (n>1)
1302 			variance = (tsumsq - n*avg*avg) /(n-1);
1303 
1304 		printf("round-trip min/avg/max/stddev = "
1305 			"%.3f/%.3f/%.3f/%.3f ms\n",
1306 			tmin * 1000.0, avg * 1000.0,
1307 			tmax * 1000.0, sqrt(variance) * 1000.0);
1308 		if (pingflags & F_FLOOD) {
1309 			double r = diffsec(&last_rx, &first_rx);
1310 			double t = diffsec(&last_tx, &first_tx);
1311 			if (r == 0)
1312 				r = 0.0001;
1313 			if (t == 0)
1314 				t = 0.0001;
1315 			(void)printf("  %.1f packets/sec sent, "
1316 				     " %.1f packets/sec received\n",
1317 				     ntransmitted/t, nreceived/r);
1318 		}
1319 	}
1320 }
1321 
1322 
1323 /*
1324  * Print statistics when SIGINFO is received.
1325  */
1326 /* ARGSUSED */
1327 static void
1328 prtsig(int dummy)
1329 {
1330 
1331 	summary(0);
1332 #ifndef SIGINFO
1333 	(void)signal(SIGQUIT, prtsig);
1334 #endif
1335 }
1336 
1337 
1338 /*
1339  * On the first SIGINT, allow any outstanding packets to dribble in
1340  */
1341 static void
1342 prefinish(int dummy)
1343 {
1344 	if (lastrcvd			/* quit now if caught up */
1345 	    || nreceived == 0)		/* or if remote is dead */
1346 		finish(0);
1347 
1348 	(void)signal(dummy, finish);	/* do this only the 1st time */
1349 
1350 	if (npackets > ntransmitted)	/* let the normal limit work */
1351 		npackets = ntransmitted;
1352 }
1353 
1354 /*
1355  * Print statistics and give up.
1356  */
1357 /* ARGSUSED */
1358 static void
1359 finish(int dummy)
1360 {
1361 #ifdef SIGINFO
1362 	(void)signal(SIGINFO, SIG_DFL);
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