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