xref: /freebsd/sbin/ping/ping.c (revision f78ac61b3a420bfb759adc5f71b19b8d2c131ae9)
18fae3551SRodney W. Grimes /*
28fae3551SRodney W. Grimes  * Copyright (c) 1989, 1993
38fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
48fae3551SRodney W. Grimes  *
58fae3551SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
68fae3551SRodney W. Grimes  * Mike Muuss.
78fae3551SRodney W. Grimes  *
88fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
98fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
108fae3551SRodney W. Grimes  * are met:
118fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
128fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
138fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
148fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
158fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
168fae3551SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
178fae3551SRodney W. Grimes  *    must display the following acknowledgement:
188fae3551SRodney W. Grimes  *	This product includes software developed by the University of
198fae3551SRodney W. Grimes  *	California, Berkeley and its contributors.
208fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
218fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
228fae3551SRodney W. Grimes  *    without specific prior written permission.
238fae3551SRodney W. Grimes  *
248fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
258fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
268fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
278fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
288fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
298fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
308fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
318fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
328fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
338fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
348fae3551SRodney W. Grimes  * SUCH DAMAGE.
358fae3551SRodney W. Grimes  */
368fae3551SRodney W. Grimes 
378fae3551SRodney W. Grimes #ifndef lint
3843470e3bSGarrett Wollman static const char copyright[] =
398fae3551SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\
408fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
418fae3551SRodney W. Grimes #endif /* not lint */
428fae3551SRodney W. Grimes 
438fae3551SRodney W. Grimes #ifndef lint
4443470e3bSGarrett Wollman /*
458fae3551SRodney W. Grimes static char sccsid[] = "@(#)ping.c	8.1 (Berkeley) 6/5/93";
4643470e3bSGarrett Wollman */
4743470e3bSGarrett Wollman static const char rcsid[] =
48f78ac61bSWarner Losh 	"$Id: ping.c,v 1.17 1997/03/01 20:19:18 wollman Exp $";
498fae3551SRodney W. Grimes #endif /* not lint */
508fae3551SRodney W. Grimes 
518fae3551SRodney W. Grimes /*
528fae3551SRodney W. Grimes  *			P I N G . C
538fae3551SRodney W. Grimes  *
548fae3551SRodney W. Grimes  * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
558fae3551SRodney W. Grimes  * measure round-trip-delays and packet loss across network paths.
568fae3551SRodney W. Grimes  *
578fae3551SRodney W. Grimes  * Author -
588fae3551SRodney W. Grimes  *	Mike Muuss
598fae3551SRodney W. Grimes  *	U. S. Army Ballistic Research Laboratory
608fae3551SRodney W. Grimes  *	December, 1983
618fae3551SRodney W. Grimes  *
628fae3551SRodney W. Grimes  * Status -
638fae3551SRodney W. Grimes  *	Public Domain.  Distribution Unlimited.
648fae3551SRodney W. Grimes  * Bugs -
658fae3551SRodney W. Grimes  *	More statistics could always be gathered.
668fae3551SRodney W. Grimes  *	This program has to run SUID to ROOT to access the ICMP socket.
678fae3551SRodney W. Grimes  */
688fae3551SRodney W. Grimes 
6943470e3bSGarrett Wollman #include <sys/param.h>		/* NB: we rely on this for <sys/types.h> */
7043470e3bSGarrett Wollman 
7143470e3bSGarrett Wollman #include <ctype.h>
7243470e3bSGarrett Wollman #include <err.h>
7343470e3bSGarrett Wollman #include <errno.h>
7443470e3bSGarrett Wollman #include <netdb.h>
7543470e3bSGarrett Wollman #include <signal.h>
7643470e3bSGarrett Wollman #include <stdio.h>
7743470e3bSGarrett Wollman #include <stdlib.h>
7843470e3bSGarrett Wollman #include <string.h>
7943470e3bSGarrett Wollman #include <sysexits.h>
8043470e3bSGarrett Wollman #include <termios.h>
8143470e3bSGarrett Wollman #include <unistd.h>
8243470e3bSGarrett Wollman 
838fae3551SRodney W. Grimes #include <sys/socket.h>
848fae3551SRodney W. Grimes #include <sys/file.h>
858fae3551SRodney W. Grimes #include <sys/time.h>
868fae3551SRodney W. Grimes 
878fae3551SRodney W. Grimes #include <netinet/in.h>
8843470e3bSGarrett Wollman #include <netinet/in_systm.h>
898fae3551SRodney W. Grimes #include <netinet/ip.h>
908fae3551SRodney W. Grimes #include <netinet/ip_icmp.h>
918fae3551SRodney W. Grimes #include <netinet/ip_var.h>
9243470e3bSGarrett Wollman #include <arpa/inet.h>
938fae3551SRodney W. Grimes 
948fae3551SRodney W. Grimes #define	DEFDATALEN	(64 - 8)	/* default data length */
958fae3551SRodney W. Grimes #define	MAXIPLEN	60
968fae3551SRodney W. Grimes #define	MAXICMPLEN	76
978fae3551SRodney W. Grimes #define	MAXPACKET	(65536 - 60 - 8)/* max packet size */
988fae3551SRodney W. Grimes #define	MAXWAIT		10		/* max seconds to wait for response */
998fae3551SRodney W. Grimes #define	NROUTES		9		/* number of record route slots */
1008fae3551SRodney W. Grimes 
1018fae3551SRodney W. Grimes #define	A(bit)		rcvd_tbl[(bit)>>3]	/* identify byte in array */
1028fae3551SRodney W. Grimes #define	B(bit)		(1 << ((bit) & 0x07))	/* identify bit in byte */
1038fae3551SRodney W. Grimes #define	SET(bit)	(A(bit) |= B(bit))
1048fae3551SRodney W. Grimes #define	CLR(bit)	(A(bit) &= (~B(bit)))
1058fae3551SRodney W. Grimes #define	TST(bit)	(A(bit) & B(bit))
1068fae3551SRodney W. Grimes 
1078fae3551SRodney W. Grimes /* various options */
1088fae3551SRodney W. Grimes int options;
10985456935SBill Fenner #define	F_FLOOD		0x0001
11085456935SBill Fenner #define	F_INTERVAL	0x0002
11185456935SBill Fenner #define	F_NUMERIC	0x0004
11285456935SBill Fenner #define	F_PINGFILLED	0x0008
11385456935SBill Fenner #define	F_QUIET		0x0010
11485456935SBill Fenner #define	F_RROUTE	0x0020
11585456935SBill Fenner #define	F_SO_DEBUG	0x0040
11685456935SBill Fenner #define	F_SO_DONTROUTE	0x0080
11785456935SBill Fenner #define	F_VERBOSE	0x0100
11885456935SBill Fenner #define	F_QUIET2	0x0200
11985456935SBill Fenner #define	F_NOLOOP	0x0400
12085456935SBill Fenner #define	F_MTTL		0x0800
12185456935SBill Fenner #define	F_MIF		0x1000
122772dfa72SDaniel O'Callaghan #define	F_AUDIBLE	0x2000
1238fae3551SRodney W. Grimes 
1248fae3551SRodney W. Grimes /*
1258fae3551SRodney W. Grimes  * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
1268fae3551SRodney W. Grimes  * number of received sequence numbers we can keep track of.  Change 128
1278fae3551SRodney W. Grimes  * to 8192 for complete accuracy...
1288fae3551SRodney W. Grimes  */
1298fae3551SRodney W. Grimes #define	MAX_DUP_CHK	(8 * 128)
1308fae3551SRodney W. Grimes int mx_dup_ck = MAX_DUP_CHK;
1318fae3551SRodney W. Grimes char rcvd_tbl[MAX_DUP_CHK / 8];
1328fae3551SRodney W. Grimes 
1338fae3551SRodney W. Grimes struct sockaddr whereto;	/* who to ping */
1348fae3551SRodney W. Grimes int datalen = DEFDATALEN;
1358fae3551SRodney W. Grimes int s;				/* socket file descriptor */
1368fae3551SRodney W. Grimes u_char outpack[MAXPACKET];
1378fae3551SRodney W. Grimes char BSPACE = '\b';		/* characters written for flood */
1388fae3551SRodney W. Grimes char DOT = '.';
1398fae3551SRodney W. Grimes char *hostname;
1408fae3551SRodney W. Grimes int ident;			/* process id to identify our packets */
1418fae3551SRodney W. Grimes 
1428fae3551SRodney W. Grimes /* counters */
1438fae3551SRodney W. Grimes long npackets;			/* max packets to transmit */
1448fae3551SRodney W. Grimes long nreceived;			/* # of packets we got back */
1458fae3551SRodney W. Grimes long nrepeats;			/* number of duplicates */
1468fae3551SRodney W. Grimes long ntransmitted;		/* sequence # for outbound packets = #sent */
1478fae3551SRodney W. Grimes int interval = 1;		/* interval between packets */
1488fae3551SRodney W. Grimes 
1498fae3551SRodney W. Grimes /* timing */
1508fae3551SRodney W. Grimes int timing;			/* flag to do timing */
1518fae3551SRodney W. Grimes double tmin = 999999999.0;	/* minimum round trip time */
1528fae3551SRodney W. Grimes double tmax = 0.0;		/* maximum round trip time */
1538fae3551SRodney W. Grimes double tsum = 0.0;		/* sum of all times, for doing average */
1548fae3551SRodney W. Grimes 
155badd8138SSean Eric Fagan int reset_kerninfo;
15637e5b2c6SSean Eric Fagan sig_atomic_t siginfo_p;
157badd8138SSean Eric Fagan 
15843470e3bSGarrett Wollman static void fill(char *, char *);
15943470e3bSGarrett Wollman static u_short in_cksum(u_short *, int);
16043470e3bSGarrett Wollman static void catcher(int sig);
16143470e3bSGarrett Wollman static void check_status(void);
16243470e3bSGarrett Wollman static void finish(int) __dead2;
16343470e3bSGarrett Wollman static void pinger(void);
16443470e3bSGarrett Wollman static char *pr_addr(struct in_addr);
16543470e3bSGarrett Wollman static void pr_icmph(struct icmp *);
16643470e3bSGarrett Wollman static void pr_iph(struct ip *);
16743470e3bSGarrett Wollman static void pr_pack(char *, int, struct sockaddr_in *);
16843470e3bSGarrett Wollman static void pr_retip(struct ip *);
16943470e3bSGarrett Wollman static void status(int);
17043470e3bSGarrett Wollman static void tvsub(struct timeval *, struct timeval *);
17143470e3bSGarrett Wollman static void usage(const char *) __dead2;
1728fae3551SRodney W. Grimes 
17343470e3bSGarrett Wollman int
1748fae3551SRodney W. Grimes main(argc, argv)
1758fae3551SRodney W. Grimes 	int argc;
17643470e3bSGarrett Wollman 	char *const *argv;
1778fae3551SRodney W. Grimes {
1788fae3551SRodney W. Grimes 	struct timeval timeout;
1798fae3551SRodney W. Grimes 	struct hostent *hp;
1808fae3551SRodney W. Grimes 	struct sockaddr_in *to;
181badd8138SSean Eric Fagan 	struct termios ts;
1828fae3551SRodney W. Grimes 	register int i;
183f1284d7aSBill Fenner 	int ch, fdmask, hold, packlen, preload, sockerrno;
18485456935SBill Fenner 	struct in_addr ifaddr;
18585456935SBill Fenner 	unsigned char ttl, loop;
1868fae3551SRodney W. Grimes 	u_char *datap, *packet;
18743470e3bSGarrett Wollman 	char *target, hnamebuf[MAXHOSTNAMELEN];
18843470e3bSGarrett Wollman 	char *ep;
18943470e3bSGarrett Wollman 	u_long ultmp;
1908fae3551SRodney W. Grimes #ifdef IP_OPTIONS
1918fae3551SRodney W. Grimes 	char rspace[3 + 4 * NROUTES + 1];	/* record route space */
1928fae3551SRodney W. Grimes #endif
19337e5b2c6SSean Eric Fagan 	struct sigaction si_sa;
1948fae3551SRodney W. Grimes 
195f1284d7aSBill Fenner 	/*
196f1284d7aSBill Fenner 	 * Do the stuff that we need root priv's for *first*, and
197f1284d7aSBill Fenner 	 * then drop our setuid bit.  Save error reporting for
198f1284d7aSBill Fenner 	 * after arg parsing.
199f1284d7aSBill Fenner 	 */
20043470e3bSGarrett Wollman 	s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
201f1284d7aSBill Fenner 	sockerrno = errno;
202f1284d7aSBill Fenner 
203f1284d7aSBill Fenner 	setuid(getuid());
204f1284d7aSBill Fenner 
2058fae3551SRodney W. Grimes 	preload = 0;
206badd8138SSean Eric Fagan 
2078fae3551SRodney W. Grimes 	datap = &outpack[8 + sizeof(struct timeval)];
208f78ac61bSWarner Losh 	while ((ch = getopt(argc, argv, "I:LQRT:c:adfi:l:np:qrs:v")) != -1) {
2098fae3551SRodney W. Grimes 		switch(ch) {
210772dfa72SDaniel O'Callaghan 		case 'a':
211772dfa72SDaniel O'Callaghan 			options |= F_AUDIBLE;
212772dfa72SDaniel O'Callaghan 			break;
2138fae3551SRodney W. Grimes 		case 'c':
21443470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
21543470e3bSGarrett Wollman 			if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp)
21643470e3bSGarrett Wollman 				errx(EX_USAGE,
21743470e3bSGarrett Wollman 				    "invalid count of packets to transmit: `%s'",
21843470e3bSGarrett Wollman 				    optarg);
21943470e3bSGarrett Wollman 			npackets = ultmp;
2208fae3551SRodney W. Grimes 			break;
2218fae3551SRodney W. Grimes 		case 'd':
2228fae3551SRodney W. Grimes 			options |= F_SO_DEBUG;
2238fae3551SRodney W. Grimes 			break;
2248fae3551SRodney W. Grimes 		case 'f':
2258fae3551SRodney W. Grimes 			if (getuid()) {
22643470e3bSGarrett Wollman 				errno = EPERM;
22743470e3bSGarrett Wollman 				err(EX_NOPERM, "-f flag");
2288fae3551SRodney W. Grimes 			}
2298fae3551SRodney W. Grimes 			options |= F_FLOOD;
2308fae3551SRodney W. Grimes 			setbuf(stdout, (char *)NULL);
2318fae3551SRodney W. Grimes 			break;
2328fae3551SRodney W. Grimes 		case 'i':		/* wait between sending packets */
23343470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
23443470e3bSGarrett Wollman 			if (*ep || ep == optarg || ultmp > INT_MAX)
23543470e3bSGarrett Wollman 				errx(EX_USAGE,
23643470e3bSGarrett Wollman 				     "invalid timing interval: `%s'", optarg);
2378fae3551SRodney W. Grimes 			options |= F_INTERVAL;
23843470e3bSGarrett Wollman 			interval = ultmp;
2398fae3551SRodney W. Grimes 			break;
24085456935SBill Fenner 		case 'I':		/* multicast interface */
24143470e3bSGarrett Wollman 			if (inet_aton(optarg, &ifaddr) == 0)
24243470e3bSGarrett Wollman 				errx(EX_USAGE,
24343470e3bSGarrett Wollman 				     "invalid multicast interface: `%s'",
24443470e3bSGarrett Wollman 				     optarg);
24585456935SBill Fenner 			options |= F_MIF;
24685456935SBill Fenner 			break;
2478fae3551SRodney W. Grimes 		case 'l':
24843470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
24943470e3bSGarrett Wollman 			if (*ep || ep == optarg || ultmp > INT_MAX)
25043470e3bSGarrett Wollman 				errx(EX_USAGE,
25143470e3bSGarrett Wollman 				     "invalid preload value: `%s'", optarg);
252f78ac61bSWarner Losh 			if (getuid()) {
253f78ac61bSWarner Losh 				errno = EPERM;
254f78ac61bSWarner Losh 				err(EX_NOPERM, "-l flag");
255f78ac61bSWarner Losh 			}
256f78ac61bSWarner Losh 			options |= F_FLOOD;
25743470e3bSGarrett Wollman 			preload = ultmp;
2588fae3551SRodney W. Grimes 			break;
25985456935SBill Fenner 		case 'L':
26085456935SBill Fenner 			options |= F_NOLOOP;
26185456935SBill Fenner 			loop = 0;
26285456935SBill Fenner 			break;
2638fae3551SRodney W. Grimes 		case 'n':
2648fae3551SRodney W. Grimes 			options |= F_NUMERIC;
2658fae3551SRodney W. Grimes 			break;
2668fae3551SRodney W. Grimes 		case 'p':		/* fill buffer with user pattern */
2678fae3551SRodney W. Grimes 			options |= F_PINGFILLED;
2688fae3551SRodney W. Grimes 			fill((char *)datap, optarg);
2698fae3551SRodney W. Grimes 				break;
270ef9e6dc7SBill Fenner 		case 'Q':
271ef9e6dc7SBill Fenner 			options |= F_QUIET2;
272ef9e6dc7SBill Fenner 			break;
2738fae3551SRodney W. Grimes 		case 'q':
2748fae3551SRodney W. Grimes 			options |= F_QUIET;
2758fae3551SRodney W. Grimes 			break;
2768fae3551SRodney W. Grimes 		case 'R':
2778fae3551SRodney W. Grimes 			options |= F_RROUTE;
2788fae3551SRodney W. Grimes 			break;
2798fae3551SRodney W. Grimes 		case 'r':
2808fae3551SRodney W. Grimes 			options |= F_SO_DONTROUTE;
2818fae3551SRodney W. Grimes 			break;
2828fae3551SRodney W. Grimes 		case 's':		/* size of packet to send */
28343470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
28443470e3bSGarrett Wollman 			if (ultmp > MAXPACKET)
28543470e3bSGarrett Wollman 				errx(EX_USAGE, "packet size too large: %lu",
28643470e3bSGarrett Wollman 				     ultmp);
28743470e3bSGarrett Wollman 			if (*ep || ep == optarg || !ultmp)
28843470e3bSGarrett Wollman 				errx(EX_USAGE, "invalid packet size: `%s'",
28943470e3bSGarrett Wollman 				     optarg);
29043470e3bSGarrett Wollman 			datalen = ultmp;
2918fae3551SRodney W. Grimes 			break;
29285456935SBill Fenner 		case 'T':		/* multicast TTL */
29343470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
29443470e3bSGarrett Wollman 			if (*ep || ep == optarg || ultmp > 255)
29543470e3bSGarrett Wollman 				errx(EX_USAGE, "invalid multicast TTL: `%s'",
29643470e3bSGarrett Wollman 				     optarg);
29743470e3bSGarrett Wollman 			ttl = ultmp;
29885456935SBill Fenner 			options |= F_MTTL;
29985456935SBill Fenner 			break;
3008fae3551SRodney W. Grimes 		case 'v':
3018fae3551SRodney W. Grimes 			options |= F_VERBOSE;
3028fae3551SRodney W. Grimes 			break;
3038fae3551SRodney W. Grimes 		default:
3048fae3551SRodney W. Grimes 
30543470e3bSGarrett Wollman 			usage(argv[0]);
30643470e3bSGarrett Wollman 		}
30743470e3bSGarrett Wollman 	}
30843470e3bSGarrett Wollman 
30943470e3bSGarrett Wollman 	if (argc - optind != 1)
31043470e3bSGarrett Wollman 		usage(argv[0]);
31143470e3bSGarrett Wollman 	target = argv[optind];
3128fae3551SRodney W. Grimes 
3138fae3551SRodney W. Grimes 	bzero((char *)&whereto, sizeof(struct sockaddr));
3148fae3551SRodney W. Grimes 	to = (struct sockaddr_in *)&whereto;
3158fae3551SRodney W. Grimes 	to->sin_family = AF_INET;
31643470e3bSGarrett Wollman 	if (inet_aton(target, &to->sin_addr) != 0) {
3178fae3551SRodney W. Grimes 		hostname = target;
31843470e3bSGarrett Wollman 	} else {
31943470e3bSGarrett Wollman 		hp = gethostbyname2(target, AF_INET);
32043470e3bSGarrett Wollman 		if (!hp)
32143470e3bSGarrett Wollman 			errx(EX_NOHOST, "cannot resolve %s: %s",
32243470e3bSGarrett Wollman 			     target, hstrerror(h_errno));
32343470e3bSGarrett Wollman 
32443470e3bSGarrett Wollman 		to->sin_len = sizeof *to;
32543470e3bSGarrett Wollman 		memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr);
3268fae3551SRodney W. Grimes 		(void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1);
32743470e3bSGarrett Wollman 		hnamebuf[(sizeof hnamebuf) - 1] = '\0';
3288fae3551SRodney W. Grimes 		hostname = hnamebuf;
3298fae3551SRodney W. Grimes 	}
3308fae3551SRodney W. Grimes 
33143470e3bSGarrett Wollman 	if (options & F_FLOOD && options & F_INTERVAL)
33243470e3bSGarrett Wollman 		errx(EX_USAGE, "-f and -i: incompatible options");
33343470e3bSGarrett Wollman 
33443470e3bSGarrett Wollman 	if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
33543470e3bSGarrett Wollman 		errx(EX_USAGE,
33643470e3bSGarrett Wollman 		     "-f flag cannot be used with multicast destination");
33743470e3bSGarrett Wollman 	if (options & (F_MIF | F_NOLOOP | F_MTTL)
33843470e3bSGarrett Wollman 	    && !IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
33943470e3bSGarrett Wollman 		errx(EX_USAGE,
34043470e3bSGarrett Wollman 		     "-I, -L, -T flags cannot be used with unicast destination");
3418fae3551SRodney W. Grimes 
3428fae3551SRodney W. Grimes 	if (datalen >= sizeof(struct timeval))	/* can we time transfer */
3438fae3551SRodney W. Grimes 		timing = 1;
3448fae3551SRodney W. Grimes 	packlen = datalen + MAXIPLEN + MAXICMPLEN;
34543470e3bSGarrett Wollman 	if (!(packet = (u_char *)malloc((size_t)packlen)))
34643470e3bSGarrett Wollman 		err(EX_UNAVAILABLE, "malloc");
34743470e3bSGarrett Wollman 
3488fae3551SRodney W. Grimes 	if (!(options & F_PINGFILLED))
3498fae3551SRodney W. Grimes 		for (i = 8; i < datalen; ++i)
3508fae3551SRodney W. Grimes 			*datap++ = i;
3518fae3551SRodney W. Grimes 
3528fae3551SRodney W. Grimes 	ident = getpid() & 0xFFFF;
3538fae3551SRodney W. Grimes 
354f1284d7aSBill Fenner 	if (s < 0) {
355f1284d7aSBill Fenner 		errno = sockerrno;
35643470e3bSGarrett Wollman 		err(EX_OSERR, "socket");
3578fae3551SRodney W. Grimes 	}
3588fae3551SRodney W. Grimes 	hold = 1;
3598fae3551SRodney W. Grimes 	if (options & F_SO_DEBUG)
3608fae3551SRodney W. Grimes 		(void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
3618fae3551SRodney W. Grimes 		    sizeof(hold));
3628fae3551SRodney W. Grimes 	if (options & F_SO_DONTROUTE)
3638fae3551SRodney W. Grimes 		(void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold,
3648fae3551SRodney W. Grimes 		    sizeof(hold));
3658fae3551SRodney W. Grimes 
3668fae3551SRodney W. Grimes 	/* record route option */
3678fae3551SRodney W. Grimes 	if (options & F_RROUTE) {
3688fae3551SRodney W. Grimes #ifdef IP_OPTIONS
3698fae3551SRodney W. Grimes 		rspace[IPOPT_OPTVAL] = IPOPT_RR;
3708fae3551SRodney W. Grimes 		rspace[IPOPT_OLEN] = sizeof(rspace)-1;
3718fae3551SRodney W. Grimes 		rspace[IPOPT_OFFSET] = IPOPT_MINOFF;
3728fae3551SRodney W. Grimes 		if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace,
37343470e3bSGarrett Wollman 		    sizeof(rspace)) < 0)
37443470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_OPTIONS");
3758fae3551SRodney W. Grimes #else
37643470e3bSGarrett Wollman 		errx(EX_UNAVAILABLE,
37743470e3bSGarrett Wollman 		  "record route not available in this implementation");
3788fae3551SRodney W. Grimes #endif /* IP_OPTIONS */
3798fae3551SRodney W. Grimes 	}
3808fae3551SRodney W. Grimes 
38185456935SBill Fenner 	if (options & F_NOLOOP) {
38285456935SBill Fenner 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop,
38385456935SBill Fenner 		    sizeof(loop)) < 0) {
38443470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP");
38585456935SBill Fenner 		}
38685456935SBill Fenner 	}
38785456935SBill Fenner 	if (options & F_MTTL) {
38885456935SBill Fenner 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
38985456935SBill Fenner 		    sizeof(ttl)) < 0) {
39043470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_TTL");
39185456935SBill Fenner 		}
39285456935SBill Fenner 	}
39385456935SBill Fenner 	if (options & F_MIF) {
39485456935SBill Fenner 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr,
39585456935SBill Fenner 		    sizeof(ifaddr)) < 0) {
39643470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_IF");
39785456935SBill Fenner 		}
39885456935SBill Fenner 	}
39985456935SBill Fenner 
4008fae3551SRodney W. Grimes 	/*
4018fae3551SRodney W. Grimes 	 * When pinging the broadcast address, you can get a lot of answers.
4028fae3551SRodney W. Grimes 	 * Doing something so evil is useful if you are trying to stress the
4038fae3551SRodney W. Grimes 	 * ethernet, or just want to fill the arp cache to get some stuff for
40443470e3bSGarrett Wollman 	 * /etc/ethers.  But beware: RFC 1122 allows hosts to ignore broadcast
40543470e3bSGarrett Wollman 	 * or multicast pings if they wish.
4068fae3551SRodney W. Grimes 	 */
4078fae3551SRodney W. Grimes 	hold = 48 * 1024;
4088fae3551SRodney W. Grimes 	(void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
4098fae3551SRodney W. Grimes 	    sizeof(hold));
4108fae3551SRodney W. Grimes 
4118fae3551SRodney W. Grimes 	if (to->sin_family == AF_INET)
4128fae3551SRodney W. Grimes 		(void)printf("PING %s (%s): %d data bytes\n", hostname,
41343470e3bSGarrett Wollman 		    inet_ntoa(to->sin_addr),
4148fae3551SRodney W. Grimes 		    datalen);
4158fae3551SRodney W. Grimes 	else
4168fae3551SRodney W. Grimes 		(void)printf("PING %s: %d data bytes\n", hostname, datalen);
4178fae3551SRodney W. Grimes 
4188fae3551SRodney W. Grimes 	(void)signal(SIGINT, finish);
4198fae3551SRodney W. Grimes 	(void)signal(SIGALRM, catcher);
4208fae3551SRodney W. Grimes 
4217d81b35cSBruce Evans 	/*
4227d81b35cSBruce Evans 	 * Use sigaction instead of signal() to get unambiguous semantics
4237d81b35cSBruce Evans 	 * for SIGINFO, in particular with SA_RESTART not set.
4247d81b35cSBruce Evans 	 */
42537e5b2c6SSean Eric Fagan 	si_sa.sa_handler = status;
426f6bd468bSPaul Traina 	sigemptyset(&si_sa.sa_mask);
42737e5b2c6SSean Eric Fagan 	si_sa.sa_flags = 0;
42837e5b2c6SSean Eric Fagan 	if (sigaction(SIGINFO, &si_sa, 0) == -1) {
42943470e3bSGarrett Wollman 		err(EX_OSERR, "sigsction");
43037e5b2c6SSean Eric Fagan 	}
43137e5b2c6SSean Eric Fagan 
4324055611aSSean Eric Fagan 	if (tcgetattr(STDOUT_FILENO, &ts) != -1) {
4334055611aSSean Eric Fagan 		reset_kerninfo = !(ts.c_lflag & NOKERNINFO);
4344055611aSSean Eric Fagan 		ts.c_lflag |= NOKERNINFO;
4354055611aSSean Eric Fagan 		tcsetattr(STDOUT_FILENO, TCSANOW, &ts);
4364055611aSSean Eric Fagan 	}
4374055611aSSean Eric Fagan 
4388fae3551SRodney W. Grimes 	while (preload--)		/* fire off them quickies */
4398fae3551SRodney W. Grimes 		pinger();
4408fae3551SRodney W. Grimes 
4418fae3551SRodney W. Grimes 	if ((options & F_FLOOD) == 0)
44243470e3bSGarrett Wollman 		catcher(0);		/* start things going */
4438fae3551SRodney W. Grimes 
4447d81b35cSBruce Evans 	for (;;) {
4458fae3551SRodney W. Grimes 		struct sockaddr_in from;
4468fae3551SRodney W. Grimes 		register int cc;
4478fae3551SRodney W. Grimes 		int fromlen;
4488fae3551SRodney W. Grimes 
4497d81b35cSBruce Evans 		check_status();
4508fae3551SRodney W. Grimes 		if (options & F_FLOOD) {
4518fae3551SRodney W. Grimes 			pinger();
4528fae3551SRodney W. Grimes 			timeout.tv_sec = 0;
4538fae3551SRodney W. Grimes 			timeout.tv_usec = 10000;
4548fae3551SRodney W. Grimes 			fdmask = 1 << s;
4558fae3551SRodney W. Grimes 			if (select(s + 1, (fd_set *)&fdmask, (fd_set *)NULL,
4568fae3551SRodney W. Grimes 			    (fd_set *)NULL, &timeout) < 1)
4578fae3551SRodney W. Grimes 				continue;
4588fae3551SRodney W. Grimes 		}
4598fae3551SRodney W. Grimes 		fromlen = sizeof(from);
4608fae3551SRodney W. Grimes 		if ((cc = recvfrom(s, (char *)packet, packlen, 0,
4618fae3551SRodney W. Grimes 		    (struct sockaddr *)&from, &fromlen)) < 0) {
4628fae3551SRodney W. Grimes 			if (errno == EINTR)
4638fae3551SRodney W. Grimes 				continue;
4648fae3551SRodney W. Grimes 			perror("ping: recvfrom");
4658fae3551SRodney W. Grimes 			continue;
4668fae3551SRodney W. Grimes 		}
4678fae3551SRodney W. Grimes 		pr_pack((char *)packet, cc, &from);
4688fae3551SRodney W. Grimes 		if (npackets && nreceived >= npackets)
4698fae3551SRodney W. Grimes 			break;
4708fae3551SRodney W. Grimes 	}
47143470e3bSGarrett Wollman 	finish(0);
4728fae3551SRodney W. Grimes 	/* NOTREACHED */
473f78ac61bSWarner Losh 	exit(0);	/* Make the compiler happy */
4748fae3551SRodney W. Grimes }
4758fae3551SRodney W. Grimes 
4768fae3551SRodney W. Grimes /*
4778fae3551SRodney W. Grimes  * catcher --
4788fae3551SRodney W. Grimes  *	This routine causes another PING to be transmitted, and then
4798fae3551SRodney W. Grimes  * schedules another SIGALRM for 1 second from now.
4808fae3551SRodney W. Grimes  *
4818fae3551SRodney W. Grimes  * bug --
4828fae3551SRodney W. Grimes  *	Our sense of time will slowly skew (i.e., packets will not be
4838fae3551SRodney W. Grimes  * launched exactly at 1-second intervals).  This does not affect the
4848fae3551SRodney W. Grimes  * quality of the delay and loss statistics.
4858fae3551SRodney W. Grimes  */
48643470e3bSGarrett Wollman static void
48743470e3bSGarrett Wollman catcher(int sig)
4888fae3551SRodney W. Grimes {
4898fae3551SRodney W. Grimes 	int waittime;
4908fae3551SRodney W. Grimes 
4918fae3551SRodney W. Grimes 	pinger();
4928fae3551SRodney W. Grimes 	(void)signal(SIGALRM, catcher);
4938fae3551SRodney W. Grimes 	if (!npackets || ntransmitted < npackets)
4948fae3551SRodney W. Grimes 		alarm((u_int)interval);
4958fae3551SRodney W. Grimes 	else {
4968fae3551SRodney W. Grimes 		if (nreceived) {
4978fae3551SRodney W. Grimes 			waittime = 2 * tmax / 1000;
4988fae3551SRodney W. Grimes 			if (!waittime)
4998fae3551SRodney W. Grimes 				waittime = 1;
5008fae3551SRodney W. Grimes 		} else
5018fae3551SRodney W. Grimes 			waittime = MAXWAIT;
5028fae3551SRodney W. Grimes 		(void)signal(SIGALRM, finish);
5038fae3551SRodney W. Grimes 		(void)alarm((u_int)waittime);
5048fae3551SRodney W. Grimes 	}
5058fae3551SRodney W. Grimes }
5068fae3551SRodney W. Grimes 
5078fae3551SRodney W. Grimes /*
5088fae3551SRodney W. Grimes  * pinger --
5098fae3551SRodney W. Grimes  *	Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
5108fae3551SRodney W. Grimes  * will be added on by the kernel.  The ID field is our UNIX process ID,
5118fae3551SRodney W. Grimes  * and the sequence number is an ascending integer.  The first 8 bytes
512ef9e6dc7SBill Fenner  * of the data portion are used to hold a UNIX "timeval" struct in host
5138fae3551SRodney W. Grimes  * byte-order, to compute the round-trip time.
5148fae3551SRodney W. Grimes  */
51543470e3bSGarrett Wollman static void
51643470e3bSGarrett Wollman pinger(void)
5178fae3551SRodney W. Grimes {
5188fae3551SRodney W. Grimes 	register struct icmp *icp;
5198fae3551SRodney W. Grimes 	register int cc;
5208fae3551SRodney W. Grimes 	int i;
5218fae3551SRodney W. Grimes 
5228fae3551SRodney W. Grimes 	icp = (struct icmp *)outpack;
5238fae3551SRodney W. Grimes 	icp->icmp_type = ICMP_ECHO;
5248fae3551SRodney W. Grimes 	icp->icmp_code = 0;
5258fae3551SRodney W. Grimes 	icp->icmp_cksum = 0;
5268fae3551SRodney W. Grimes 	icp->icmp_seq = ntransmitted++;
5278fae3551SRodney W. Grimes 	icp->icmp_id = ident;			/* ID */
5288fae3551SRodney W. Grimes 
5298fae3551SRodney W. Grimes 	CLR(icp->icmp_seq % mx_dup_ck);
5308fae3551SRodney W. Grimes 
5318fae3551SRodney W. Grimes 	if (timing)
5328fae3551SRodney W. Grimes 		(void)gettimeofday((struct timeval *)&outpack[8],
5338fae3551SRodney W. Grimes 		    (struct timezone *)NULL);
5348fae3551SRodney W. Grimes 
5358fae3551SRodney W. Grimes 	cc = datalen + 8;			/* skips ICMP portion */
5368fae3551SRodney W. Grimes 
5378fae3551SRodney W. Grimes 	/* compute ICMP checksum here */
5388fae3551SRodney W. Grimes 	icp->icmp_cksum = in_cksum((u_short *)icp, cc);
5398fae3551SRodney W. Grimes 
5408fae3551SRodney W. Grimes 	i = sendto(s, (char *)outpack, cc, 0, &whereto,
5418fae3551SRodney W. Grimes 	    sizeof(struct sockaddr));
5428fae3551SRodney W. Grimes 
5438fae3551SRodney W. Grimes 	if (i < 0 || i != cc)  {
54443470e3bSGarrett Wollman 		if (i < 0) {
54543470e3bSGarrett Wollman 			warn("sendto");
54643470e3bSGarrett Wollman 		} else {
54743470e3bSGarrett Wollman 			warn("%s: partial write: %d of %d bytes",
5488fae3551SRodney W. Grimes 			     hostname, cc, i);
5498fae3551SRodney W. Grimes 		}
55043470e3bSGarrett Wollman 	}
5518fae3551SRodney W. Grimes 	if (!(options & F_QUIET) && options & F_FLOOD)
5528fae3551SRodney W. Grimes 		(void)write(STDOUT_FILENO, &DOT, 1);
5538fae3551SRodney W. Grimes }
5548fae3551SRodney W. Grimes 
5558fae3551SRodney W. Grimes /*
5568fae3551SRodney W. Grimes  * pr_pack --
5578fae3551SRodney W. Grimes  *	Print out the packet, if it came from us.  This logic is necessary
5588fae3551SRodney W. Grimes  * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
5598fae3551SRodney W. Grimes  * which arrive ('tis only fair).  This permits multiple copies of this
5608fae3551SRodney W. Grimes  * program to be run without having intermingled output (or statistics!).
5618fae3551SRodney W. Grimes  */
56243470e3bSGarrett Wollman static void
5638fae3551SRodney W. Grimes pr_pack(buf, cc, from)
5648fae3551SRodney W. Grimes 	char *buf;
5658fae3551SRodney W. Grimes 	int cc;
5668fae3551SRodney W. Grimes 	struct sockaddr_in *from;
5678fae3551SRodney W. Grimes {
5688fae3551SRodney W. Grimes 	register struct icmp *icp;
5698fae3551SRodney W. Grimes 	register u_long l;
5708fae3551SRodney W. Grimes 	register int i, j;
5718fae3551SRodney W. Grimes 	register u_char *cp,*dp;
5728fae3551SRodney W. Grimes 	static int old_rrlen;
5738fae3551SRodney W. Grimes 	static char old_rr[MAX_IPOPTLEN];
5748fae3551SRodney W. Grimes 	struct ip *ip;
5758fae3551SRodney W. Grimes 	struct timeval tv, *tp;
5768fae3551SRodney W. Grimes 	double triptime;
5778fae3551SRodney W. Grimes 	int hlen, dupflag;
5788fae3551SRodney W. Grimes 
5798fae3551SRodney W. Grimes 	(void)gettimeofday(&tv, (struct timezone *)NULL);
5808fae3551SRodney W. Grimes 
5818fae3551SRodney W. Grimes 	/* Check the IP header */
5828fae3551SRodney W. Grimes 	ip = (struct ip *)buf;
5838fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
5848fae3551SRodney W. Grimes 	if (cc < hlen + ICMP_MINLEN) {
5858fae3551SRodney W. Grimes 		if (options & F_VERBOSE)
58643470e3bSGarrett Wollman 			warn("packet too short (%d bytes) from %s", cc,
58743470e3bSGarrett Wollman 			     inet_ntoa(from->sin_addr));
5888fae3551SRodney W. Grimes 		return;
5898fae3551SRodney W. Grimes 	}
5908fae3551SRodney W. Grimes 
5918fae3551SRodney W. Grimes 	/* Now the ICMP part */
5928fae3551SRodney W. Grimes 	cc -= hlen;
5938fae3551SRodney W. Grimes 	icp = (struct icmp *)(buf + hlen);
5948fae3551SRodney W. Grimes 	if (icp->icmp_type == ICMP_ECHOREPLY) {
5958fae3551SRodney W. Grimes 		if (icp->icmp_id != ident)
5968fae3551SRodney W. Grimes 			return;			/* 'Twas not our ECHO */
5978fae3551SRodney W. Grimes 		++nreceived;
5988fae3551SRodney W. Grimes 		if (timing) {
5998fae3551SRodney W. Grimes #ifndef icmp_data
6008fae3551SRodney W. Grimes 			tp = (struct timeval *)&icp->icmp_ip;
6018fae3551SRodney W. Grimes #else
6028fae3551SRodney W. Grimes 			tp = (struct timeval *)icp->icmp_data;
6038fae3551SRodney W. Grimes #endif
6048fae3551SRodney W. Grimes 			tvsub(&tv, tp);
6058fae3551SRodney W. Grimes 			triptime = ((double)tv.tv_sec) * 1000.0 +
6068fae3551SRodney W. Grimes 			    ((double)tv.tv_usec) / 1000.0;
6078fae3551SRodney W. Grimes 			tsum += triptime;
6088fae3551SRodney W. Grimes 			if (triptime < tmin)
6098fae3551SRodney W. Grimes 				tmin = triptime;
6108fae3551SRodney W. Grimes 			if (triptime > tmax)
6118fae3551SRodney W. Grimes 				tmax = triptime;
6128fae3551SRodney W. Grimes 		}
6138fae3551SRodney W. Grimes 
6148fae3551SRodney W. Grimes 		if (TST(icp->icmp_seq % mx_dup_ck)) {
6158fae3551SRodney W. Grimes 			++nrepeats;
6168fae3551SRodney W. Grimes 			--nreceived;
6178fae3551SRodney W. Grimes 			dupflag = 1;
6188fae3551SRodney W. Grimes 		} else {
6198fae3551SRodney W. Grimes 			SET(icp->icmp_seq % mx_dup_ck);
6208fae3551SRodney W. Grimes 			dupflag = 0;
6218fae3551SRodney W. Grimes 		}
6228fae3551SRodney W. Grimes 
6238fae3551SRodney W. Grimes 		if (options & F_QUIET)
6248fae3551SRodney W. Grimes 			return;
6258fae3551SRodney W. Grimes 
6268fae3551SRodney W. Grimes 		if (options & F_FLOOD)
6278fae3551SRodney W. Grimes 			(void)write(STDOUT_FILENO, &BSPACE, 1);
6288fae3551SRodney W. Grimes 		else {
6298fae3551SRodney W. Grimes 			(void)printf("%d bytes from %s: icmp_seq=%u", cc,
6308fae3551SRodney W. Grimes 			   inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr),
6318fae3551SRodney W. Grimes 			   icp->icmp_seq);
6328fae3551SRodney W. Grimes 			(void)printf(" ttl=%d", ip->ip_ttl);
6338fae3551SRodney W. Grimes 			if (timing)
634d410b6f1SDavid Greenman 				(void)printf(" time=%.3f ms", triptime);
6358fae3551SRodney W. Grimes 			if (dupflag)
6368fae3551SRodney W. Grimes 				(void)printf(" (DUP!)");
637772dfa72SDaniel O'Callaghan 			if (options & F_AUDIBLE)
638772dfa72SDaniel O'Callaghan 				(void)printf("\a");
6398fae3551SRodney W. Grimes 			/* check the data */
6408fae3551SRodney W. Grimes 			cp = (u_char*)&icp->icmp_data[8];
6418fae3551SRodney W. Grimes 			dp = &outpack[8 + sizeof(struct timeval)];
6428fae3551SRodney W. Grimes 			for (i = 8; i < datalen; ++i, ++cp, ++dp) {
6438fae3551SRodney W. Grimes 				if (*cp != *dp) {
6448fae3551SRodney W. Grimes 	(void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x",
6458fae3551SRodney W. Grimes 	    i, *dp, *cp);
6468fae3551SRodney W. Grimes 					cp = (u_char*)&icp->icmp_data[0];
6478fae3551SRodney W. Grimes 					for (i = 8; i < datalen; ++i, ++cp) {
6488fae3551SRodney W. Grimes 						if ((i % 32) == 8)
6498fae3551SRodney W. Grimes 							(void)printf("\n\t");
6508fae3551SRodney W. Grimes 						(void)printf("%x ", *cp);
6518fae3551SRodney W. Grimes 					}
6528fae3551SRodney W. Grimes 					break;
6538fae3551SRodney W. Grimes 				}
6548fae3551SRodney W. Grimes 			}
6558fae3551SRodney W. Grimes 		}
6568fae3551SRodney W. Grimes 	} else {
657ef9e6dc7SBill Fenner 		/*
658ef9e6dc7SBill Fenner 		 * We've got something other than an ECHOREPLY.
659ef9e6dc7SBill Fenner 		 * See if it's a reply to something that we sent.
660ef9e6dc7SBill Fenner 		 * We can compare IP destination, protocol,
661ef9e6dc7SBill Fenner 		 * and ICMP type and ID.
662f78ac61bSWarner Losh 		 *
663f78ac61bSWarner Losh 		 * Only print all the error messages if we are running
664f78ac61bSWarner Losh 		 * as root to avoid leaking information not normally
665f78ac61bSWarner Losh 		 * available to those not running as root.
666ef9e6dc7SBill Fenner 		 */
667ef9e6dc7SBill Fenner #ifndef icmp_data
668ef9e6dc7SBill Fenner 		struct ip *oip = &icp->icmp_ip;
669ef9e6dc7SBill Fenner #else
670ef9e6dc7SBill Fenner 		struct ip *oip = (struct ip *)icp->icmp_data;
671ef9e6dc7SBill Fenner #endif
672ef9e6dc7SBill Fenner 		struct icmp *oicmp = (struct icmp *)(oip + 1);
673ef9e6dc7SBill Fenner 
674f78ac61bSWarner Losh 		if (((options & F_VERBOSE) && getuid() == 0) ||
675ef9e6dc7SBill Fenner 		    (!(options & F_QUIET2) &&
676ef9e6dc7SBill Fenner 		     (oip->ip_dst.s_addr ==
677ef9e6dc7SBill Fenner 			 ((struct sockaddr_in *)&whereto)->sin_addr.s_addr) &&
678ef9e6dc7SBill Fenner 		     (oip->ip_p == IPPROTO_ICMP) &&
679ef9e6dc7SBill Fenner 		     (oicmp->icmp_type == ICMP_ECHO) &&
680ef9e6dc7SBill Fenner 		     (oicmp->icmp_id == ident))) {
6818fae3551SRodney W. Grimes 		    (void)printf("%d bytes from %s: ", cc,
68243470e3bSGarrett Wollman 			pr_addr(from->sin_addr));
6838fae3551SRodney W. Grimes 		    pr_icmph(icp);
684ef9e6dc7SBill Fenner 		} else
685ef9e6dc7SBill Fenner 		    return;
6868fae3551SRodney W. Grimes 	}
6878fae3551SRodney W. Grimes 
6888fae3551SRodney W. Grimes 	/* Display any IP options */
6898fae3551SRodney W. Grimes 	cp = (u_char *)buf + sizeof(struct ip);
6908fae3551SRodney W. Grimes 
6918fae3551SRodney W. Grimes 	for (; hlen > (int)sizeof(struct ip); --hlen, ++cp)
6928fae3551SRodney W. Grimes 		switch (*cp) {
6938fae3551SRodney W. Grimes 		case IPOPT_EOL:
6948fae3551SRodney W. Grimes 			hlen = 0;
6958fae3551SRodney W. Grimes 			break;
6968fae3551SRodney W. Grimes 		case IPOPT_LSRR:
6978fae3551SRodney W. Grimes 			(void)printf("\nLSRR: ");
6988fae3551SRodney W. Grimes 			hlen -= 2;
6998fae3551SRodney W. Grimes 			j = *++cp;
7008fae3551SRodney W. Grimes 			++cp;
7018fae3551SRodney W. Grimes 			if (j > IPOPT_MINOFF)
7028fae3551SRodney W. Grimes 				for (;;) {
7038fae3551SRodney W. Grimes 					l = *++cp;
7048fae3551SRodney W. Grimes 					l = (l<<8) + *++cp;
7058fae3551SRodney W. Grimes 					l = (l<<8) + *++cp;
7068fae3551SRodney W. Grimes 					l = (l<<8) + *++cp;
70743470e3bSGarrett Wollman 					if (l == 0) {
70843470e3bSGarrett Wollman 						printf("\t0.0.0.0");
70943470e3bSGarrett Wollman 					} else {
71043470e3bSGarrett Wollman 						struct in_addr ina;
71143470e3bSGarrett Wollman 						ina.s_addr = ntohl(l);
71243470e3bSGarrett Wollman 						printf("\t%s", pr_addr(ina));
71343470e3bSGarrett Wollman 					}
7148fae3551SRodney W. Grimes 				hlen -= 4;
7158fae3551SRodney W. Grimes 				j -= 4;
7168fae3551SRodney W. Grimes 				if (j <= IPOPT_MINOFF)
7178fae3551SRodney W. Grimes 					break;
7188fae3551SRodney W. Grimes 				(void)putchar('\n');
7198fae3551SRodney W. Grimes 			}
7208fae3551SRodney W. Grimes 			break;
7218fae3551SRodney W. Grimes 		case IPOPT_RR:
7228fae3551SRodney W. Grimes 			j = *++cp;		/* get length */
7238fae3551SRodney W. Grimes 			i = *++cp;		/* and pointer */
7248fae3551SRodney W. Grimes 			hlen -= 2;
7258fae3551SRodney W. Grimes 			if (i > j)
7268fae3551SRodney W. Grimes 				i = j;
7278fae3551SRodney W. Grimes 			i -= IPOPT_MINOFF;
7288fae3551SRodney W. Grimes 			if (i <= 0)
7298fae3551SRodney W. Grimes 				continue;
7308fae3551SRodney W. Grimes 			if (i == old_rrlen
7318fae3551SRodney W. Grimes 			    && cp == (u_char *)buf + sizeof(struct ip) + 2
7328fae3551SRodney W. Grimes 			    && !bcmp((char *)cp, old_rr, i)
7338fae3551SRodney W. Grimes 			    && !(options & F_FLOOD)) {
7348fae3551SRodney W. Grimes 				(void)printf("\t(same route)");
7358fae3551SRodney W. Grimes 				i = ((i + 3) / 4) * 4;
7368fae3551SRodney W. Grimes 				hlen -= i;
7378fae3551SRodney W. Grimes 				cp += i;
7388fae3551SRodney W. Grimes 				break;
7398fae3551SRodney W. Grimes 			}
7408fae3551SRodney W. Grimes 			old_rrlen = i;
7418fae3551SRodney W. Grimes 			bcopy((char *)cp, old_rr, i);
7428fae3551SRodney W. Grimes 			(void)printf("\nRR: ");
7438fae3551SRodney W. Grimes 			for (;;) {
7448fae3551SRodney W. Grimes 				l = *++cp;
7458fae3551SRodney W. Grimes 				l = (l<<8) + *++cp;
7468fae3551SRodney W. Grimes 				l = (l<<8) + *++cp;
7478fae3551SRodney W. Grimes 				l = (l<<8) + *++cp;
74843470e3bSGarrett Wollman 				if (l == 0) {
74943470e3bSGarrett Wollman 					printf("\t0.0.0.0");
75043470e3bSGarrett Wollman 				} else {
75143470e3bSGarrett Wollman 					struct in_addr ina;
75243470e3bSGarrett Wollman 					ina.s_addr = ntohl(l);
75343470e3bSGarrett Wollman 					printf("\t%s", pr_addr(ina));
75443470e3bSGarrett Wollman 				}
7558fae3551SRodney W. Grimes 				hlen -= 4;
7568fae3551SRodney W. Grimes 				i -= 4;
7578fae3551SRodney W. Grimes 				if (i <= 0)
7588fae3551SRodney W. Grimes 					break;
7598fae3551SRodney W. Grimes 				(void)putchar('\n');
7608fae3551SRodney W. Grimes 			}
7618fae3551SRodney W. Grimes 			break;
7628fae3551SRodney W. Grimes 		case IPOPT_NOP:
7638fae3551SRodney W. Grimes 			(void)printf("\nNOP");
7648fae3551SRodney W. Grimes 			break;
7658fae3551SRodney W. Grimes 		default:
7668fae3551SRodney W. Grimes 			(void)printf("\nunknown option %x", *cp);
7678fae3551SRodney W. Grimes 			break;
7688fae3551SRodney W. Grimes 		}
7698fae3551SRodney W. Grimes 	if (!(options & F_FLOOD)) {
7708fae3551SRodney W. Grimes 		(void)putchar('\n');
7718fae3551SRodney W. Grimes 		(void)fflush(stdout);
7728fae3551SRodney W. Grimes 	}
7738fae3551SRodney W. Grimes }
7748fae3551SRodney W. Grimes 
7758fae3551SRodney W. Grimes /*
7768fae3551SRodney W. Grimes  * in_cksum --
7778fae3551SRodney W. Grimes  *	Checksum routine for Internet Protocol family headers (C Version)
7788fae3551SRodney W. Grimes  */
77943470e3bSGarrett Wollman u_short
7808fae3551SRodney W. Grimes in_cksum(addr, len)
7818fae3551SRodney W. Grimes 	u_short *addr;
7828fae3551SRodney W. Grimes 	int len;
7838fae3551SRodney W. Grimes {
7848fae3551SRodney W. Grimes 	register int nleft = len;
7858fae3551SRodney W. Grimes 	register u_short *w = addr;
7868fae3551SRodney W. Grimes 	register int sum = 0;
7878fae3551SRodney W. Grimes 	u_short answer = 0;
7888fae3551SRodney W. Grimes 
7898fae3551SRodney W. Grimes 	/*
7908fae3551SRodney W. Grimes 	 * Our algorithm is simple, using a 32 bit accumulator (sum), we add
7918fae3551SRodney W. Grimes 	 * sequential 16 bit words to it, and at the end, fold back all the
7928fae3551SRodney W. Grimes 	 * carry bits from the top 16 bits into the lower 16 bits.
7938fae3551SRodney W. Grimes 	 */
7948fae3551SRodney W. Grimes 	while (nleft > 1)  {
7958fae3551SRodney W. Grimes 		sum += *w++;
7968fae3551SRodney W. Grimes 		nleft -= 2;
7978fae3551SRodney W. Grimes 	}
7988fae3551SRodney W. Grimes 
7998fae3551SRodney W. Grimes 	/* mop up an odd byte, if necessary */
8008fae3551SRodney W. Grimes 	if (nleft == 1) {
8018fae3551SRodney W. Grimes 		*(u_char *)(&answer) = *(u_char *)w ;
8028fae3551SRodney W. Grimes 		sum += answer;
8038fae3551SRodney W. Grimes 	}
8048fae3551SRodney W. Grimes 
8058fae3551SRodney W. Grimes 	/* add back carry outs from top 16 bits to low 16 bits */
8068fae3551SRodney W. Grimes 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
8078fae3551SRodney W. Grimes 	sum += (sum >> 16);			/* add carry */
8088fae3551SRodney W. Grimes 	answer = ~sum;				/* truncate to 16 bits */
8098fae3551SRodney W. Grimes 	return(answer);
8108fae3551SRodney W. Grimes }
8118fae3551SRodney W. Grimes 
8128fae3551SRodney W. Grimes /*
8138fae3551SRodney W. Grimes  * tvsub --
8148fae3551SRodney W. Grimes  *	Subtract 2 timeval structs:  out = out - in.  Out is assumed to
8158fae3551SRodney W. Grimes  * be >= in.
8168fae3551SRodney W. Grimes  */
81743470e3bSGarrett Wollman static void
8188fae3551SRodney W. Grimes tvsub(out, in)
8198fae3551SRodney W. Grimes 	register struct timeval *out, *in;
8208fae3551SRodney W. Grimes {
8218fae3551SRodney W. Grimes 	if ((out->tv_usec -= in->tv_usec) < 0) {
8228fae3551SRodney W. Grimes 		--out->tv_sec;
8238fae3551SRodney W. Grimes 		out->tv_usec += 1000000;
8248fae3551SRodney W. Grimes 	}
8258fae3551SRodney W. Grimes 	out->tv_sec -= in->tv_sec;
8268fae3551SRodney W. Grimes }
8278fae3551SRodney W. Grimes 
8288fae3551SRodney W. Grimes /*
829badd8138SSean Eric Fagan  * status --
830badd8138SSean Eric Fagan  *	Print out statistics when SIGINFO is received.
831badd8138SSean Eric Fagan  */
832badd8138SSean Eric Fagan 
83343470e3bSGarrett Wollman static void
8347d81b35cSBruce Evans status(sig)
8357d81b35cSBruce Evans 	int sig;
8367d81b35cSBruce Evans {
83737e5b2c6SSean Eric Fagan 	siginfo_p = 1;
83837e5b2c6SSean Eric Fagan }
83937e5b2c6SSean Eric Fagan 
84043470e3bSGarrett Wollman static void
84137e5b2c6SSean Eric Fagan check_status()
842badd8138SSean Eric Fagan {
84337e5b2c6SSean Eric Fagan 	if (siginfo_p) {
84437e5b2c6SSean Eric Fagan 		siginfo_p = 0;
8457d81b35cSBruce Evans 		(void)fprintf(stderr,
8467d81b35cSBruce Evans 	"\r%ld/%ld packets received (%.0f%%) %.3f min / %.3f avg / %.3f max\n",
8477d81b35cSBruce Evans 		    nreceived, ntransmitted,
8487d81b35cSBruce Evans 		    ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0,
8497d81b35cSBruce Evans 		    nreceived ? tmin : 0.0,
8507d81b35cSBruce Evans 		    nreceived + nrepeats ? tsum / (nreceived + nrepeats) : tsum,
8517d81b35cSBruce Evans 		    tmax);
85237e5b2c6SSean Eric Fagan 	}
853badd8138SSean Eric Fagan }
854badd8138SSean Eric Fagan 
855badd8138SSean Eric Fagan /*
8568fae3551SRodney W. Grimes  * finish --
8578fae3551SRodney W. Grimes  *	Print out statistics, and give up.
8588fae3551SRodney W. Grimes  */
85943470e3bSGarrett Wollman static void
86043470e3bSGarrett Wollman finish(int sig)
8618fae3551SRodney W. Grimes {
862badd8138SSean Eric Fagan 	struct termios ts;
8638fae3551SRodney W. Grimes 
8648fae3551SRodney W. Grimes 	(void)signal(SIGINT, SIG_IGN);
8658fae3551SRodney W. Grimes 	(void)putchar('\n');
8668fae3551SRodney W. Grimes 	(void)fflush(stdout);
8678fae3551SRodney W. Grimes 	(void)printf("--- %s ping statistics ---\n", hostname);
8688fae3551SRodney W. Grimes 	(void)printf("%ld packets transmitted, ", ntransmitted);
8698fae3551SRodney W. Grimes 	(void)printf("%ld packets received, ", nreceived);
8708fae3551SRodney W. Grimes 	if (nrepeats)
8718fae3551SRodney W. Grimes 		(void)printf("+%ld duplicates, ", nrepeats);
8728fae3551SRodney W. Grimes 	if (ntransmitted)
8738fae3551SRodney W. Grimes 		if (nreceived > ntransmitted)
8748fae3551SRodney W. Grimes 			(void)printf("-- somebody's printing up packets!");
8758fae3551SRodney W. Grimes 		else
8768fae3551SRodney W. Grimes 			(void)printf("%d%% packet loss",
8778fae3551SRodney W. Grimes 			    (int) (((ntransmitted - nreceived) * 100) /
8788fae3551SRodney W. Grimes 			    ntransmitted));
8798fae3551SRodney W. Grimes 	(void)putchar('\n');
8807d81b35cSBruce Evans 	if (nreceived && timing)
881d410b6f1SDavid Greenman 		(void)printf("round-trip min/avg/max = %.3f/%.3f/%.3f ms\n",
8827d81b35cSBruce Evans 		    tmin, tsum / (nreceived + nrepeats), tmax);
8832ceaae21SBruce Evans 	if (reset_kerninfo && tcgetattr(STDOUT_FILENO, &ts) != -1) {
884badd8138SSean Eric Fagan 		ts.c_lflag &= ~NOKERNINFO;
8852ceaae21SBruce Evans 		tcsetattr(STDOUT_FILENO, TCSANOW, &ts);
886badd8138SSean Eric Fagan 	}
887badd8138SSean Eric Fagan 
8886e1173dcSDavid Greenman 	if (nreceived)
8898fae3551SRodney W. Grimes 		exit(0);
8906e1173dcSDavid Greenman 	else
8916e1173dcSDavid Greenman 		exit(2);
8928fae3551SRodney W. Grimes }
8938fae3551SRodney W. Grimes 
8948fae3551SRodney W. Grimes #ifdef notdef
8958fae3551SRodney W. Grimes static char *ttab[] = {
8968fae3551SRodney W. Grimes 	"Echo Reply",		/* ip + seq + udata */
8978fae3551SRodney W. Grimes 	"Dest Unreachable",	/* net, host, proto, port, frag, sr + IP */
8988fae3551SRodney W. Grimes 	"Source Quench",	/* IP */
8998fae3551SRodney W. Grimes 	"Redirect",		/* redirect type, gateway, + IP  */
9008fae3551SRodney W. Grimes 	"Echo",
9018fae3551SRodney W. Grimes 	"Time Exceeded",	/* transit, frag reassem + IP */
9028fae3551SRodney W. Grimes 	"Parameter Problem",	/* pointer + IP */
9038fae3551SRodney W. Grimes 	"Timestamp",		/* id + seq + three timestamps */
9048fae3551SRodney W. Grimes 	"Timestamp Reply",	/* " */
9058fae3551SRodney W. Grimes 	"Info Request",		/* id + sq */
9068fae3551SRodney W. Grimes 	"Info Reply"		/* " */
9078fae3551SRodney W. Grimes };
9088fae3551SRodney W. Grimes #endif
9098fae3551SRodney W. Grimes 
9108fae3551SRodney W. Grimes /*
9118fae3551SRodney W. Grimes  * pr_icmph --
9128fae3551SRodney W. Grimes  *	Print a descriptive string about an ICMP header.
9138fae3551SRodney W. Grimes  */
91443470e3bSGarrett Wollman static void
9158fae3551SRodney W. Grimes pr_icmph(icp)
9168fae3551SRodney W. Grimes 	struct icmp *icp;
9178fae3551SRodney W. Grimes {
9188fae3551SRodney W. Grimes 	switch(icp->icmp_type) {
9198fae3551SRodney W. Grimes 	case ICMP_ECHOREPLY:
9208fae3551SRodney W. Grimes 		(void)printf("Echo Reply\n");
9218fae3551SRodney W. Grimes 		/* XXX ID + Seq + Data */
9228fae3551SRodney W. Grimes 		break;
9238fae3551SRodney W. Grimes 	case ICMP_UNREACH:
9248fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
9258fae3551SRodney W. Grimes 		case ICMP_UNREACH_NET:
9268fae3551SRodney W. Grimes 			(void)printf("Destination Net Unreachable\n");
9278fae3551SRodney W. Grimes 			break;
9288fae3551SRodney W. Grimes 		case ICMP_UNREACH_HOST:
9298fae3551SRodney W. Grimes 			(void)printf("Destination Host Unreachable\n");
9308fae3551SRodney W. Grimes 			break;
9318fae3551SRodney W. Grimes 		case ICMP_UNREACH_PROTOCOL:
9328fae3551SRodney W. Grimes 			(void)printf("Destination Protocol Unreachable\n");
9338fae3551SRodney W. Grimes 			break;
9348fae3551SRodney W. Grimes 		case ICMP_UNREACH_PORT:
9358fae3551SRodney W. Grimes 			(void)printf("Destination Port Unreachable\n");
9368fae3551SRodney W. Grimes 			break;
9378fae3551SRodney W. Grimes 		case ICMP_UNREACH_NEEDFRAG:
938ef9e6dc7SBill Fenner 			(void)printf("frag needed and DF set (MTU %d)\n",
939ef9e6dc7SBill Fenner 					icp->icmp_nextmtu);
9408fae3551SRodney W. Grimes 			break;
9418fae3551SRodney W. Grimes 		case ICMP_UNREACH_SRCFAIL:
9428fae3551SRodney W. Grimes 			(void)printf("Source Route Failed\n");
9438fae3551SRodney W. Grimes 			break;
944ef9e6dc7SBill Fenner 		case ICMP_UNREACH_FILTER_PROHIB:
945ef9e6dc7SBill Fenner 			(void)printf("Communication prohibited by filter\n");
946ef9e6dc7SBill Fenner 			break;
9478fae3551SRodney W. Grimes 		default:
9488fae3551SRodney W. Grimes 			(void)printf("Dest Unreachable, Bad Code: %d\n",
9498fae3551SRodney W. Grimes 			    icp->icmp_code);
9508fae3551SRodney W. Grimes 			break;
9518fae3551SRodney W. Grimes 		}
9528fae3551SRodney W. Grimes 		/* Print returned IP header information */
9538fae3551SRodney W. Grimes #ifndef icmp_data
9548fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
9558fae3551SRodney W. Grimes #else
9568fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
9578fae3551SRodney W. Grimes #endif
9588fae3551SRodney W. Grimes 		break;
9598fae3551SRodney W. Grimes 	case ICMP_SOURCEQUENCH:
9608fae3551SRodney W. Grimes 		(void)printf("Source Quench\n");
9618fae3551SRodney W. Grimes #ifndef icmp_data
9628fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
9638fae3551SRodney W. Grimes #else
9648fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
9658fae3551SRodney W. Grimes #endif
9668fae3551SRodney W. Grimes 		break;
9678fae3551SRodney W. Grimes 	case ICMP_REDIRECT:
9688fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
9698fae3551SRodney W. Grimes 		case ICMP_REDIRECT_NET:
9708fae3551SRodney W. Grimes 			(void)printf("Redirect Network");
9718fae3551SRodney W. Grimes 			break;
9728fae3551SRodney W. Grimes 		case ICMP_REDIRECT_HOST:
9738fae3551SRodney W. Grimes 			(void)printf("Redirect Host");
9748fae3551SRodney W. Grimes 			break;
9758fae3551SRodney W. Grimes 		case ICMP_REDIRECT_TOSNET:
9768fae3551SRodney W. Grimes 			(void)printf("Redirect Type of Service and Network");
9778fae3551SRodney W. Grimes 			break;
9788fae3551SRodney W. Grimes 		case ICMP_REDIRECT_TOSHOST:
9798fae3551SRodney W. Grimes 			(void)printf("Redirect Type of Service and Host");
9808fae3551SRodney W. Grimes 			break;
9818fae3551SRodney W. Grimes 		default:
9828fae3551SRodney W. Grimes 			(void)printf("Redirect, Bad Code: %d", icp->icmp_code);
9838fae3551SRodney W. Grimes 			break;
9848fae3551SRodney W. Grimes 		}
9858fae3551SRodney W. Grimes 		(void)printf("(New addr: 0x%08lx)\n", icp->icmp_gwaddr.s_addr);
9868fae3551SRodney W. Grimes #ifndef icmp_data
9878fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
9888fae3551SRodney W. Grimes #else
9898fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
9908fae3551SRodney W. Grimes #endif
9918fae3551SRodney W. Grimes 		break;
9928fae3551SRodney W. Grimes 	case ICMP_ECHO:
9938fae3551SRodney W. Grimes 		(void)printf("Echo Request\n");
9948fae3551SRodney W. Grimes 		/* XXX ID + Seq + Data */
9958fae3551SRodney W. Grimes 		break;
9968fae3551SRodney W. Grimes 	case ICMP_TIMXCEED:
9978fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
9988fae3551SRodney W. Grimes 		case ICMP_TIMXCEED_INTRANS:
9998fae3551SRodney W. Grimes 			(void)printf("Time to live exceeded\n");
10008fae3551SRodney W. Grimes 			break;
10018fae3551SRodney W. Grimes 		case ICMP_TIMXCEED_REASS:
10028fae3551SRodney W. Grimes 			(void)printf("Frag reassembly time exceeded\n");
10038fae3551SRodney W. Grimes 			break;
10048fae3551SRodney W. Grimes 		default:
10058fae3551SRodney W. Grimes 			(void)printf("Time exceeded, Bad Code: %d\n",
10068fae3551SRodney W. Grimes 			    icp->icmp_code);
10078fae3551SRodney W. Grimes 			break;
10088fae3551SRodney W. Grimes 		}
10098fae3551SRodney W. Grimes #ifndef icmp_data
10108fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
10118fae3551SRodney W. Grimes #else
10128fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
10138fae3551SRodney W. Grimes #endif
10148fae3551SRodney W. Grimes 		break;
10158fae3551SRodney W. Grimes 	case ICMP_PARAMPROB:
10168fae3551SRodney W. Grimes 		(void)printf("Parameter problem: pointer = 0x%02x\n",
10178fae3551SRodney W. Grimes 		    icp->icmp_hun.ih_pptr);
10188fae3551SRodney W. Grimes #ifndef icmp_data
10198fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
10208fae3551SRodney W. Grimes #else
10218fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
10228fae3551SRodney W. Grimes #endif
10238fae3551SRodney W. Grimes 		break;
10248fae3551SRodney W. Grimes 	case ICMP_TSTAMP:
10258fae3551SRodney W. Grimes 		(void)printf("Timestamp\n");
10268fae3551SRodney W. Grimes 		/* XXX ID + Seq + 3 timestamps */
10278fae3551SRodney W. Grimes 		break;
10288fae3551SRodney W. Grimes 	case ICMP_TSTAMPREPLY:
10298fae3551SRodney W. Grimes 		(void)printf("Timestamp Reply\n");
10308fae3551SRodney W. Grimes 		/* XXX ID + Seq + 3 timestamps */
10318fae3551SRodney W. Grimes 		break;
10328fae3551SRodney W. Grimes 	case ICMP_IREQ:
10338fae3551SRodney W. Grimes 		(void)printf("Information Request\n");
10348fae3551SRodney W. Grimes 		/* XXX ID + Seq */
10358fae3551SRodney W. Grimes 		break;
10368fae3551SRodney W. Grimes 	case ICMP_IREQREPLY:
10378fae3551SRodney W. Grimes 		(void)printf("Information Reply\n");
10388fae3551SRodney W. Grimes 		/* XXX ID + Seq */
10398fae3551SRodney W. Grimes 		break;
10408fae3551SRodney W. Grimes 	case ICMP_MASKREQ:
10418fae3551SRodney W. Grimes 		(void)printf("Address Mask Request\n");
10428fae3551SRodney W. Grimes 		break;
10438fae3551SRodney W. Grimes 	case ICMP_MASKREPLY:
10448fae3551SRodney W. Grimes 		(void)printf("Address Mask Reply\n");
10458fae3551SRodney W. Grimes 		break;
1046ef9e6dc7SBill Fenner 	case ICMP_ROUTERADVERT:
1047ef9e6dc7SBill Fenner 		(void)printf("Router Advertisement\n");
1048ef9e6dc7SBill Fenner 		break;
1049ef9e6dc7SBill Fenner 	case ICMP_ROUTERSOLICIT:
1050ef9e6dc7SBill Fenner 		(void)printf("Router Solicitation\n");
1051ef9e6dc7SBill Fenner 		break;
10528fae3551SRodney W. Grimes 	default:
10538fae3551SRodney W. Grimes 		(void)printf("Bad ICMP type: %d\n", icp->icmp_type);
10548fae3551SRodney W. Grimes 	}
10558fae3551SRodney W. Grimes }
10568fae3551SRodney W. Grimes 
10578fae3551SRodney W. Grimes /*
10588fae3551SRodney W. Grimes  * pr_iph --
10598fae3551SRodney W. Grimes  *	Print an IP header with options.
10608fae3551SRodney W. Grimes  */
106143470e3bSGarrett Wollman static void
10628fae3551SRodney W. Grimes pr_iph(ip)
10638fae3551SRodney W. Grimes 	struct ip *ip;
10648fae3551SRodney W. Grimes {
10658fae3551SRodney W. Grimes 	int hlen;
10668fae3551SRodney W. Grimes 	u_char *cp;
10678fae3551SRodney W. Grimes 
10688fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
10698fae3551SRodney W. Grimes 	cp = (u_char *)ip + 20;		/* point to options */
10708fae3551SRodney W. Grimes 
1071ef9e6dc7SBill Fenner 	(void)printf("Vr HL TOS  Len   ID Flg  off TTL Pro  cks      Src      Dst\n");
10728fae3551SRodney W. Grimes 	(void)printf(" %1x  %1x  %02x %04x %04x",
1073ef9e6dc7SBill Fenner 	    ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len),
1074ef9e6dc7SBill Fenner 	    ntohs(ip->ip_id));
107543470e3bSGarrett Wollman 	(void)printf("   %1lx %04lx", (ntohl(ip->ip_off) & 0xe000) >> 13,
1076ef9e6dc7SBill Fenner 	    ntohl(ip->ip_off) & 0x1fff);
1077ef9e6dc7SBill Fenner 	(void)printf("  %02x  %02x %04x", ip->ip_ttl, ip->ip_p,
1078ef9e6dc7SBill Fenner 							    ntohs(ip->ip_sum));
10798fae3551SRodney W. Grimes 	(void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr));
10808fae3551SRodney W. Grimes 	(void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr));
1081ef9e6dc7SBill Fenner 	/* dump any option bytes */
10828fae3551SRodney W. Grimes 	while (hlen-- > 20) {
10838fae3551SRodney W. Grimes 		(void)printf("%02x", *cp++);
10848fae3551SRodney W. Grimes 	}
10858fae3551SRodney W. Grimes 	(void)putchar('\n');
10868fae3551SRodney W. Grimes }
10878fae3551SRodney W. Grimes 
10888fae3551SRodney W. Grimes /*
10898fae3551SRodney W. Grimes  * pr_addr --
10908fae3551SRodney W. Grimes  *	Return an ascii host address as a dotted quad and optionally with
10918fae3551SRodney W. Grimes  * a hostname.
10928fae3551SRodney W. Grimes  */
109343470e3bSGarrett Wollman static char *
109443470e3bSGarrett Wollman pr_addr(ina)
109543470e3bSGarrett Wollman 	struct in_addr ina;
10968fae3551SRodney W. Grimes {
10978fae3551SRodney W. Grimes 	struct hostent *hp;
1098f78ac61bSWarner Losh 	static char buf[16 + 3 + MAXHOSTNAMELEN];
10998fae3551SRodney W. Grimes 
11008fae3551SRodney W. Grimes 	if ((options & F_NUMERIC) ||
110143470e3bSGarrett Wollman 	    !(hp = gethostbyaddr((char *)&ina, 4, AF_INET)))
110243470e3bSGarrett Wollman 		return inet_ntoa(ina);
11038fae3551SRodney W. Grimes 	else
1104efa38539SPeter Wemm 		(void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name,
110543470e3bSGarrett Wollman 		    inet_ntoa(ina));
11068fae3551SRodney W. Grimes 	return(buf);
11078fae3551SRodney W. Grimes }
11088fae3551SRodney W. Grimes 
11098fae3551SRodney W. Grimes /*
11108fae3551SRodney W. Grimes  * pr_retip --
11118fae3551SRodney W. Grimes  *	Dump some info on a returned (via ICMP) IP packet.
11128fae3551SRodney W. Grimes  */
111343470e3bSGarrett Wollman static void
11148fae3551SRodney W. Grimes pr_retip(ip)
11158fae3551SRodney W. Grimes 	struct ip *ip;
11168fae3551SRodney W. Grimes {
11178fae3551SRodney W. Grimes 	int hlen;
11188fae3551SRodney W. Grimes 	u_char *cp;
11198fae3551SRodney W. Grimes 
11208fae3551SRodney W. Grimes 	pr_iph(ip);
11218fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
11228fae3551SRodney W. Grimes 	cp = (u_char *)ip + hlen;
11238fae3551SRodney W. Grimes 
11248fae3551SRodney W. Grimes 	if (ip->ip_p == 6)
11258fae3551SRodney W. Grimes 		(void)printf("TCP: from port %u, to port %u (decimal)\n",
11268fae3551SRodney W. Grimes 		    (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
11278fae3551SRodney W. Grimes 	else if (ip->ip_p == 17)
11288fae3551SRodney W. Grimes 		(void)printf("UDP: from port %u, to port %u (decimal)\n",
11298fae3551SRodney W. Grimes 			(*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
11308fae3551SRodney W. Grimes }
11318fae3551SRodney W. Grimes 
113243470e3bSGarrett Wollman static void
11338fae3551SRodney W. Grimes fill(bp, patp)
11348fae3551SRodney W. Grimes 	char *bp, *patp;
11358fae3551SRodney W. Grimes {
11368fae3551SRodney W. Grimes 	register int ii, jj, kk;
11378fae3551SRodney W. Grimes 	int pat[16];
11388fae3551SRodney W. Grimes 	char *cp;
11398fae3551SRodney W. Grimes 
114043470e3bSGarrett Wollman 	for (cp = patp; *cp; cp++) {
114143470e3bSGarrett Wollman 		if (!isxdigit(*cp))
114243470e3bSGarrett Wollman 			errx(EX_USAGE,
114343470e3bSGarrett Wollman 			     "patterns must be specified as hex digits");
114443470e3bSGarrett Wollman 
11458fae3551SRodney W. Grimes 	}
11468fae3551SRodney W. Grimes 	ii = sscanf(patp,
11478fae3551SRodney W. Grimes 	    "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
11488fae3551SRodney W. Grimes 	    &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
11498fae3551SRodney W. Grimes 	    &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
11508fae3551SRodney W. Grimes 	    &pat[13], &pat[14], &pat[15]);
11518fae3551SRodney W. Grimes 
11528fae3551SRodney W. Grimes 	if (ii > 0)
11538fae3551SRodney W. Grimes 		for (kk = 0;
11548fae3551SRodney W. Grimes 		    kk <= MAXPACKET - (8 + sizeof(struct timeval) + ii);
11558fae3551SRodney W. Grimes 		    kk += ii)
11568fae3551SRodney W. Grimes 			for (jj = 0; jj < ii; ++jj)
11578fae3551SRodney W. Grimes 				bp[jj + kk] = pat[jj];
11588fae3551SRodney W. Grimes 	if (!(options & F_QUIET)) {
11598fae3551SRodney W. Grimes 		(void)printf("PATTERN: 0x");
11608fae3551SRodney W. Grimes 		for (jj = 0; jj < ii; ++jj)
11618fae3551SRodney W. Grimes 			(void)printf("%02x", bp[jj] & 0xFF);
11628fae3551SRodney W. Grimes 		(void)printf("\n");
11638fae3551SRodney W. Grimes 	}
11648fae3551SRodney W. Grimes }
11658fae3551SRodney W. Grimes 
116643470e3bSGarrett Wollman static void
116743470e3bSGarrett Wollman usage(argv0)
116843470e3bSGarrett Wollman 	const char *argv0;
11698fae3551SRodney W. Grimes {
1170f78ac61bSWarner Losh 	if (strrchr(argv0,'/'))
1171f78ac61bSWarner Losh 		argv0 = strrchr(argv0,'/') + 1;
117243470e3bSGarrett Wollman 	fprintf(stderr,
1173f78ac61bSWarner Losh 		"usage: %s [-QRadfnqrv] [-c count] [-i wait] [-l preload] "
117443470e3bSGarrett Wollman 		"[-p pattern]\n\t\t[-s packetsize] "
117543470e3bSGarrett Wollman 		"[host | [-L] [-I iface] [-T ttl] mcast-group]\n",
117643470e3bSGarrett Wollman 		argv0);
117743470e3bSGarrett Wollman 	exit(EX_USAGE);
11788fae3551SRodney W. Grimes }
1179