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