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