xref: /freebsd/sbin/ping/ping.c (revision 5e2cc0f4b037fa5c97d8d7dc0bb8edb9b21a24ba)
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
44e345a80dSPhilippe Charnier #if 0
458fae3551SRodney W. Grimes static char sccsid[] = "@(#)ping.c	8.1 (Berkeley) 6/5/93";
46e345a80dSPhilippe Charnier #endif
4743470e3bSGarrett Wollman static const char rcsid[] =
485e2cc0f4SStephen McKay 	"$Id: ping.c,v 1.43 1999/04/25 22:33:30 imp Exp $";
498fae3551SRodney W. Grimes #endif /* not lint */
508fae3551SRodney W. Grimes 
518fae3551SRodney W. Grimes /*
528fae3551SRodney W. Grimes  *			P I N G . C
538fae3551SRodney W. Grimes  *
54e345a80dSPhilippe Charnier  * 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>
743109a910SGarrett Wollman #include <math.h>
7543470e3bSGarrett Wollman #include <netdb.h>
7643470e3bSGarrett Wollman #include <signal.h>
7743470e3bSGarrett Wollman #include <stdio.h>
7843470e3bSGarrett Wollman #include <stdlib.h>
7943470e3bSGarrett Wollman #include <string.h>
8043470e3bSGarrett Wollman #include <sysexits.h>
8143470e3bSGarrett Wollman #include <termios.h>
8243470e3bSGarrett Wollman #include <unistd.h>
8343470e3bSGarrett Wollman 
848fae3551SRodney W. Grimes #include <sys/socket.h>
858fae3551SRodney W. Grimes #include <sys/time.h>
86039d6aa4SBill Fenner #include <sys/uio.h>
878fae3551SRodney W. Grimes 
888fae3551SRodney W. Grimes #include <netinet/in.h>
8943470e3bSGarrett Wollman #include <netinet/in_systm.h>
908fae3551SRodney W. Grimes #include <netinet/ip.h>
918fae3551SRodney W. Grimes #include <netinet/ip_icmp.h>
928fae3551SRodney W. Grimes #include <netinet/ip_var.h>
9343470e3bSGarrett Wollman #include <arpa/inet.h>
948fae3551SRodney W. Grimes 
95d32ff037SJohn Birrell #define	PHDR_LEN	sizeof(struct timeval)
96d32ff037SJohn Birrell #define	DEFDATALEN	(64 - PHDR_LEN)	/* default data length */
978f975bb3SBruce Evans #define	FLOOD_BACKOFF	20000		/* usecs to back off if F_FLOOD mode */
988f975bb3SBruce Evans 					/* runs out of buffer space */
998fae3551SRodney W. Grimes #define	MAXIPLEN	60
1008fae3551SRodney W. Grimes #define	MAXICMPLEN	76
1018fae3551SRodney W. Grimes #define	MAXPACKET	(65536 - 60 - 8)/* max packet size */
1028fae3551SRodney W. Grimes #define	MAXWAIT		10		/* max seconds to wait for response */
1038fae3551SRodney W. Grimes #define	NROUTES		9		/* number of record route slots */
1048fae3551SRodney W. Grimes 
1058fae3551SRodney W. Grimes #define	A(bit)		rcvd_tbl[(bit)>>3]	/* identify byte in array */
1068fae3551SRodney W. Grimes #define	B(bit)		(1 << ((bit) & 0x07))	/* identify bit in byte */
1078fae3551SRodney W. Grimes #define	SET(bit)	(A(bit) |= B(bit))
1088fae3551SRodney W. Grimes #define	CLR(bit)	(A(bit) &= (~B(bit)))
1098fae3551SRodney W. Grimes #define	TST(bit)	(A(bit) & B(bit))
1108fae3551SRodney W. Grimes 
1118fae3551SRodney W. Grimes /* various options */
1128fae3551SRodney W. Grimes int options;
11385456935SBill Fenner #define	F_FLOOD		0x0001
11485456935SBill Fenner #define	F_INTERVAL	0x0002
11585456935SBill Fenner #define	F_NUMERIC	0x0004
11685456935SBill Fenner #define	F_PINGFILLED	0x0008
11785456935SBill Fenner #define	F_QUIET		0x0010
11885456935SBill Fenner #define	F_RROUTE	0x0020
11985456935SBill Fenner #define	F_SO_DEBUG	0x0040
12085456935SBill Fenner #define	F_SO_DONTROUTE	0x0080
12185456935SBill Fenner #define	F_VERBOSE	0x0100
12285456935SBill Fenner #define	F_QUIET2	0x0200
12385456935SBill Fenner #define	F_NOLOOP	0x0400
12485456935SBill Fenner #define	F_MTTL		0x0800
12585456935SBill Fenner #define	F_MIF		0x1000
126772dfa72SDaniel O'Callaghan #define	F_AUDIBLE	0x2000
1278fae3551SRodney W. Grimes 
1288fae3551SRodney W. Grimes /*
1298fae3551SRodney W. Grimes  * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
1308fae3551SRodney W. Grimes  * number of received sequence numbers we can keep track of.  Change 128
1318fae3551SRodney W. Grimes  * to 8192 for complete accuracy...
1328fae3551SRodney W. Grimes  */
1338fae3551SRodney W. Grimes #define	MAX_DUP_CHK	(8 * 128)
1348fae3551SRodney W. Grimes int mx_dup_ck = MAX_DUP_CHK;
1358fae3551SRodney W. Grimes char rcvd_tbl[MAX_DUP_CHK / 8];
1368fae3551SRodney W. Grimes 
1378fae3551SRodney W. Grimes struct sockaddr whereto;	/* who to ping */
1388fae3551SRodney W. Grimes int datalen = DEFDATALEN;
1398fae3551SRodney W. Grimes int s;				/* socket file descriptor */
1408fae3551SRodney W. Grimes u_char outpack[MAXPACKET];
1418fae3551SRodney W. Grimes char BSPACE = '\b';		/* characters written for flood */
1428fae3551SRodney W. Grimes char DOT = '.';
1438fae3551SRodney W. Grimes char *hostname;
14499490edeSWarner Losh char *shostname;
1458fae3551SRodney W. Grimes int ident;			/* process id to identify our packets */
146ee2bf734SWarner Losh int uid;			/* cached uid for micro-optimization */
1478fae3551SRodney W. Grimes 
1488fae3551SRodney W. Grimes /* counters */
1498fae3551SRodney W. Grimes long npackets;			/* max packets to transmit */
1508fae3551SRodney W. Grimes long nreceived;			/* # of packets we got back */
1518fae3551SRodney W. Grimes long nrepeats;			/* number of duplicates */
1528fae3551SRodney W. Grimes long ntransmitted;		/* sequence # for outbound packets = #sent */
153526f06b2SMatthew Dillon int interval = 1000;		/* interval between packets, ms */
1548fae3551SRodney W. Grimes 
1558fae3551SRodney W. Grimes /* timing */
1568fae3551SRodney W. Grimes int timing;			/* flag to do timing */
1578fae3551SRodney W. Grimes double tmin = 999999999.0;	/* minimum round trip time */
1588fae3551SRodney W. Grimes double tmax = 0.0;		/* maximum round trip time */
1598fae3551SRodney W. Grimes double tsum = 0.0;		/* sum of all times, for doing average */
1603109a910SGarrett Wollman double tsumsq = 0.0;		/* sum of all times squared, for std. dev. */
1618fae3551SRodney W. Grimes 
1628f975bb3SBruce Evans volatile sig_atomic_t finish_up;  /* nonzero if we've been told to finish up */
163badd8138SSean Eric Fagan int reset_kerninfo;
1648f975bb3SBruce Evans volatile sig_atomic_t siginfo_p;
165badd8138SSean Eric Fagan 
16643470e3bSGarrett Wollman static void fill(char *, char *);
16743470e3bSGarrett Wollman static u_short in_cksum(u_short *, int);
16843470e3bSGarrett Wollman static void check_status(void);
1698f975bb3SBruce Evans static void finish(void) __dead2;
17043470e3bSGarrett Wollman static void pinger(void);
17143470e3bSGarrett Wollman static char *pr_addr(struct in_addr);
17243470e3bSGarrett Wollman static void pr_icmph(struct icmp *);
17343470e3bSGarrett Wollman static void pr_iph(struct ip *);
174039d6aa4SBill Fenner static void pr_pack(char *, int, struct sockaddr_in *, struct timeval *);
17543470e3bSGarrett Wollman static void pr_retip(struct ip *);
17643470e3bSGarrett Wollman static void status(int);
1778f975bb3SBruce Evans static void stopit(int);
17843470e3bSGarrett Wollman static void tvsub(struct timeval *, struct timeval *);
179e345a80dSPhilippe Charnier static void usage(void) __dead2;
1808fae3551SRodney W. Grimes 
18143470e3bSGarrett Wollman int
1828fae3551SRodney W. Grimes main(argc, argv)
1838fae3551SRodney W. Grimes 	int argc;
18443470e3bSGarrett Wollman 	char *const *argv;
1858fae3551SRodney W. Grimes {
186039d6aa4SBill Fenner 	struct timeval last, intvl;
1878fae3551SRodney W. Grimes 	struct hostent *hp;
18899490edeSWarner Losh 	struct sockaddr_in *to, sin;
189badd8138SSean Eric Fagan 	struct termios ts;
1908fae3551SRodney W. Grimes 	register int i;
191039d6aa4SBill Fenner 	int ch, hold, packlen, preload, sockerrno, almost_done = 0;
19285456935SBill Fenner 	struct in_addr ifaddr;
19385456935SBill Fenner 	unsigned char ttl, loop;
1948fae3551SRodney W. Grimes 	u_char *datap, *packet;
19599490edeSWarner Losh 	char *source = NULL, *target, hnamebuf[MAXHOSTNAMELEN];
19699490edeSWarner Losh 	char snamebuf[MAXHOSTNAMELEN];
19743470e3bSGarrett Wollman 	char *ep;
19843470e3bSGarrett Wollman 	u_long ultmp;
1998fae3551SRodney W. Grimes #ifdef IP_OPTIONS
2008fae3551SRodney W. Grimes 	char rspace[3 + 4 * NROUTES + 1];	/* record route space */
2018fae3551SRodney W. Grimes #endif
20237e5b2c6SSean Eric Fagan 	struct sigaction si_sa;
203039d6aa4SBill Fenner 	struct iovec iov;
204039d6aa4SBill Fenner 	struct msghdr msg;
205039d6aa4SBill Fenner 	struct sockaddr_in from;
206039d6aa4SBill Fenner 	char ctrl[sizeof(struct cmsghdr) + sizeof(struct timeval)];
2078fae3551SRodney W. Grimes 
208f1284d7aSBill Fenner 	/*
209f1284d7aSBill Fenner 	 * Do the stuff that we need root priv's for *first*, and
210f1284d7aSBill Fenner 	 * then drop our setuid bit.  Save error reporting for
211f1284d7aSBill Fenner 	 * after arg parsing.
212f1284d7aSBill Fenner 	 */
21343470e3bSGarrett Wollman 	s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
214f1284d7aSBill Fenner 	sockerrno = errno;
215f1284d7aSBill Fenner 
216f1284d7aSBill Fenner 	setuid(getuid());
217ee2bf734SWarner Losh 	uid = getuid();
218f1284d7aSBill Fenner 
2198fae3551SRodney W. Grimes 	preload = 0;
220badd8138SSean Eric Fagan 
221d32ff037SJohn Birrell 	datap = &outpack[8 + PHDR_LEN];
22299490edeSWarner Losh 	while ((ch = getopt(argc, argv, "I:LQRS:T:c:adfi:l:np:qrs:v")) != -1) {
2238fae3551SRodney W. Grimes 		switch(ch) {
224772dfa72SDaniel O'Callaghan 		case 'a':
225772dfa72SDaniel O'Callaghan 			options |= F_AUDIBLE;
226772dfa72SDaniel O'Callaghan 			break;
2278fae3551SRodney W. Grimes 		case 'c':
22843470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
22943470e3bSGarrett Wollman 			if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp)
23043470e3bSGarrett Wollman 				errx(EX_USAGE,
23143470e3bSGarrett Wollman 				    "invalid count of packets to transmit: `%s'",
23243470e3bSGarrett Wollman 				    optarg);
23343470e3bSGarrett Wollman 			npackets = ultmp;
2348fae3551SRodney W. Grimes 			break;
2358fae3551SRodney W. Grimes 		case 'd':
2368fae3551SRodney W. Grimes 			options |= F_SO_DEBUG;
2378fae3551SRodney W. Grimes 			break;
2388fae3551SRodney W. Grimes 		case 'f':
239526f06b2SMatthew Dillon 			if (uid) {
24043470e3bSGarrett Wollman 				errno = EPERM;
24143470e3bSGarrett Wollman 				err(EX_NOPERM, "-f flag");
2428fae3551SRodney W. Grimes 			}
2438fae3551SRodney W. Grimes 			options |= F_FLOOD;
2448fae3551SRodney W. Grimes 			setbuf(stdout, (char *)NULL);
2458fae3551SRodney W. Grimes 			break;
2468fae3551SRodney W. Grimes 		case 'i':		/* wait between sending packets */
247526f06b2SMatthew Dillon 			{
248526f06b2SMatthew Dillon 			    double t = strtod(optarg, &ep) * 1000.0;
249526f06b2SMatthew Dillon 
250526f06b2SMatthew Dillon 			    if (*ep || ep == optarg || t > (double)INT_MAX) {
251526f06b2SMatthew Dillon 				    errx(
252526f06b2SMatthew Dillon 					    EX_USAGE,
253526f06b2SMatthew Dillon 					     "invalid timing interval: `%s'",
254526f06b2SMatthew Dillon 					     optarg
255526f06b2SMatthew Dillon 				    );
256526f06b2SMatthew Dillon 			    }
2578fae3551SRodney W. Grimes 			    options |= F_INTERVAL;
258526f06b2SMatthew Dillon 			    interval = (int)t;
259526f06b2SMatthew Dillon 			    if (uid && interval < 1000) {
260526f06b2SMatthew Dillon 				    errno = EPERM;
261526f06b2SMatthew Dillon 				    err(EX_NOPERM, "-i interval too short");
262526f06b2SMatthew Dillon 			    }
263526f06b2SMatthew Dillon 			}
2648fae3551SRodney W. Grimes 			break;
26585456935SBill Fenner 		case 'I':		/* multicast interface */
26643470e3bSGarrett Wollman 			if (inet_aton(optarg, &ifaddr) == 0)
26743470e3bSGarrett Wollman 				errx(EX_USAGE,
26843470e3bSGarrett Wollman 				     "invalid multicast interface: `%s'",
26943470e3bSGarrett Wollman 				     optarg);
27085456935SBill Fenner 			options |= F_MIF;
27185456935SBill Fenner 			break;
2728fae3551SRodney W. Grimes 		case 'l':
27343470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
27443470e3bSGarrett Wollman 			if (*ep || ep == optarg || ultmp > INT_MAX)
27543470e3bSGarrett Wollman 				errx(EX_USAGE,
27643470e3bSGarrett Wollman 				     "invalid preload value: `%s'", optarg);
2775e2cc0f4SStephen McKay 			if (uid) {
278f78ac61bSWarner Losh 				errno = EPERM;
279f78ac61bSWarner Losh 				err(EX_NOPERM, "-l flag");
280f78ac61bSWarner Losh 			}
28143470e3bSGarrett Wollman 			preload = ultmp;
2828fae3551SRodney W. Grimes 			break;
28385456935SBill Fenner 		case 'L':
28485456935SBill Fenner 			options |= F_NOLOOP;
28585456935SBill Fenner 			loop = 0;
28685456935SBill Fenner 			break;
2878fae3551SRodney W. Grimes 		case 'n':
2888fae3551SRodney W. Grimes 			options |= F_NUMERIC;
2898fae3551SRodney W. Grimes 			break;
2908fae3551SRodney W. Grimes 		case 'p':		/* fill buffer with user pattern */
2918fae3551SRodney W. Grimes 			options |= F_PINGFILLED;
2928fae3551SRodney W. Grimes 			fill((char *)datap, optarg);
2938fae3551SRodney W. Grimes 				break;
294ef9e6dc7SBill Fenner 		case 'Q':
295ef9e6dc7SBill Fenner 			options |= F_QUIET2;
296ef9e6dc7SBill Fenner 			break;
2978fae3551SRodney W. Grimes 		case 'q':
2988fae3551SRodney W. Grimes 			options |= F_QUIET;
2998fae3551SRodney W. Grimes 			break;
3008fae3551SRodney W. Grimes 		case 'R':
3018fae3551SRodney W. Grimes 			options |= F_RROUTE;
3028fae3551SRodney W. Grimes 			break;
3038fae3551SRodney W. Grimes 		case 'r':
3048fae3551SRodney W. Grimes 			options |= F_SO_DONTROUTE;
3058fae3551SRodney W. Grimes 			break;
3068fae3551SRodney W. Grimes 		case 's':		/* size of packet to send */
307526f06b2SMatthew Dillon 			if (uid) {
308526f06b2SMatthew Dillon 				errno = EPERM;
309526f06b2SMatthew Dillon 				err(EX_NOPERM, "-s flag");
310526f06b2SMatthew Dillon 			}
31143470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
31243470e3bSGarrett Wollman 			if (ultmp > MAXPACKET)
31343470e3bSGarrett Wollman 				errx(EX_USAGE, "packet size too large: %lu",
31443470e3bSGarrett Wollman 				     ultmp);
31543470e3bSGarrett Wollman 			if (*ep || ep == optarg || !ultmp)
31643470e3bSGarrett Wollman 				errx(EX_USAGE, "invalid packet size: `%s'",
31743470e3bSGarrett Wollman 				     optarg);
31843470e3bSGarrett Wollman 			datalen = ultmp;
3198fae3551SRodney W. Grimes 			break;
32099490edeSWarner Losh 		case 'S':
32199490edeSWarner Losh 			source = optarg;
32299490edeSWarner Losh 			break;
32385456935SBill Fenner 		case 'T':		/* multicast TTL */
32443470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
32543470e3bSGarrett Wollman 			if (*ep || ep == optarg || ultmp > 255)
32643470e3bSGarrett Wollman 				errx(EX_USAGE, "invalid multicast TTL: `%s'",
32743470e3bSGarrett Wollman 				     optarg);
32843470e3bSGarrett Wollman 			ttl = ultmp;
32985456935SBill Fenner 			options |= F_MTTL;
33085456935SBill Fenner 			break;
3318fae3551SRodney W. Grimes 		case 'v':
3328fae3551SRodney W. Grimes 			options |= F_VERBOSE;
3338fae3551SRodney W. Grimes 			break;
3348fae3551SRodney W. Grimes 		default:
335e345a80dSPhilippe Charnier 			usage();
33643470e3bSGarrett Wollman 		}
33743470e3bSGarrett Wollman 	}
33843470e3bSGarrett Wollman 
33943470e3bSGarrett Wollman 	if (argc - optind != 1)
340e345a80dSPhilippe Charnier 		usage();
34143470e3bSGarrett Wollman 	target = argv[optind];
3428fae3551SRodney W. Grimes 
34399490edeSWarner Losh 	if (source) {
34499490edeSWarner Losh 		bzero((char *)&sin, sizeof(sin));
34599490edeSWarner Losh 		sin.sin_family = AF_INET;
34699490edeSWarner Losh 		if (inet_aton(source, &sin.sin_addr) != 0) {
34799490edeSWarner Losh 			shostname = source;
34899490edeSWarner Losh 		} else {
34999490edeSWarner Losh 			hp = gethostbyname2(source, AF_INET);
35099490edeSWarner Losh 			if (!hp)
35199490edeSWarner Losh 				errx(EX_NOHOST, "cannot resolve %s: %s",
35299490edeSWarner Losh 				     source, hstrerror(h_errno));
35399490edeSWarner Losh 
35499490edeSWarner Losh 			sin.sin_len = sizeof sin;
35599490edeSWarner Losh 			if (hp->h_length > sizeof(sin.sin_addr))
35699490edeSWarner Losh 				errx(1,"gethostbyname2: illegal address");
35799490edeSWarner Losh 			memcpy(&sin.sin_addr, hp->h_addr_list[0],
35899490edeSWarner Losh 				sizeof (sin.sin_addr));
35999490edeSWarner Losh 			(void)strncpy(snamebuf, hp->h_name,
36099490edeSWarner Losh 				sizeof(snamebuf) - 1);
36199490edeSWarner Losh 			snamebuf[sizeof(snamebuf) - 1] = '\0';
36299490edeSWarner Losh 			shostname = snamebuf;
36399490edeSWarner Losh 		}
36499490edeSWarner Losh 		if (bind(s, (struct sockaddr *)&sin, sizeof sin) == -1)
36599490edeSWarner Losh 			err(1, "bind");
36699490edeSWarner Losh 	}
36799490edeSWarner Losh 
3688fae3551SRodney W. Grimes 	bzero((char *)&whereto, sizeof(struct sockaddr));
3698fae3551SRodney W. Grimes 	to = (struct sockaddr_in *)&whereto;
3708fae3551SRodney W. Grimes 	to->sin_family = AF_INET;
37143470e3bSGarrett Wollman 	if (inet_aton(target, &to->sin_addr) != 0) {
3728fae3551SRodney W. Grimes 		hostname = target;
37343470e3bSGarrett Wollman 	} else {
37443470e3bSGarrett Wollman 		hp = gethostbyname2(target, AF_INET);
37543470e3bSGarrett Wollman 		if (!hp)
37643470e3bSGarrett Wollman 			errx(EX_NOHOST, "cannot resolve %s: %s",
37743470e3bSGarrett Wollman 			     target, hstrerror(h_errno));
37843470e3bSGarrett Wollman 
37943470e3bSGarrett Wollman 		to->sin_len = sizeof *to;
3801ffae4a6SWarner Losh 		if (hp->h_length > sizeof(to->sin_addr))
3811ffae4a6SWarner Losh 			errx(1,"gethostbyname2 returned an illegal address");
38243470e3bSGarrett Wollman 		memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr);
3838fae3551SRodney W. Grimes 		(void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1);
384b10d9d5fSWarner Losh 		hnamebuf[sizeof(hnamebuf) - 1] = '\0';
3858fae3551SRodney W. Grimes 		hostname = hnamebuf;
3868fae3551SRodney W. Grimes 	}
3878fae3551SRodney W. Grimes 
38843470e3bSGarrett Wollman 	if (options & F_FLOOD && options & F_INTERVAL)
38943470e3bSGarrett Wollman 		errx(EX_USAGE, "-f and -i: incompatible options");
39043470e3bSGarrett Wollman 
39143470e3bSGarrett Wollman 	if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
39243470e3bSGarrett Wollman 		errx(EX_USAGE,
39343470e3bSGarrett Wollman 		     "-f flag cannot be used with multicast destination");
39443470e3bSGarrett Wollman 	if (options & (F_MIF | F_NOLOOP | F_MTTL)
39543470e3bSGarrett Wollman 	    && !IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
39643470e3bSGarrett Wollman 		errx(EX_USAGE,
39743470e3bSGarrett Wollman 		     "-I, -L, -T flags cannot be used with unicast destination");
3988fae3551SRodney W. Grimes 
399d32ff037SJohn Birrell 	if (datalen >= PHDR_LEN)	/* can we time transfer */
4008fae3551SRodney W. Grimes 		timing = 1;
4018fae3551SRodney W. Grimes 	packlen = datalen + MAXIPLEN + MAXICMPLEN;
40243470e3bSGarrett Wollman 	if (!(packet = (u_char *)malloc((size_t)packlen)))
40343470e3bSGarrett Wollman 		err(EX_UNAVAILABLE, "malloc");
40443470e3bSGarrett Wollman 
4058fae3551SRodney W. Grimes 	if (!(options & F_PINGFILLED))
406d32ff037SJohn Birrell 		for (i = PHDR_LEN; i < datalen; ++i)
4078fae3551SRodney W. Grimes 			*datap++ = i;
4088fae3551SRodney W. Grimes 
4098fae3551SRodney W. Grimes 	ident = getpid() & 0xFFFF;
4108fae3551SRodney W. Grimes 
411f1284d7aSBill Fenner 	if (s < 0) {
412f1284d7aSBill Fenner 		errno = sockerrno;
41343470e3bSGarrett Wollman 		err(EX_OSERR, "socket");
4148fae3551SRodney W. Grimes 	}
4158fae3551SRodney W. Grimes 	hold = 1;
4168fae3551SRodney W. Grimes 	if (options & F_SO_DEBUG)
4178fae3551SRodney W. Grimes 		(void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
4188fae3551SRodney W. Grimes 		    sizeof(hold));
4198fae3551SRodney W. Grimes 	if (options & F_SO_DONTROUTE)
4208fae3551SRodney W. Grimes 		(void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold,
4218fae3551SRodney W. Grimes 		    sizeof(hold));
4228fae3551SRodney W. Grimes 
4238fae3551SRodney W. Grimes 	/* record route option */
4248fae3551SRodney W. Grimes 	if (options & F_RROUTE) {
4258fae3551SRodney W. Grimes #ifdef IP_OPTIONS
426039d6aa4SBill Fenner 		bzero(rspace, sizeof(rspace));
4278fae3551SRodney W. Grimes 		rspace[IPOPT_OPTVAL] = IPOPT_RR;
4288fae3551SRodney W. Grimes 		rspace[IPOPT_OLEN] = sizeof(rspace) - 1;
4298fae3551SRodney W. Grimes 		rspace[IPOPT_OFFSET] = IPOPT_MINOFF;
430039d6aa4SBill Fenner 		rspace[sizeof(rspace) - 1] = IPOPT_EOL;
4318fae3551SRodney W. Grimes 		if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace,
43243470e3bSGarrett Wollman 		    sizeof(rspace)) < 0)
43343470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_OPTIONS");
4348fae3551SRodney W. Grimes #else
43543470e3bSGarrett Wollman 		errx(EX_UNAVAILABLE,
43643470e3bSGarrett Wollman 		  "record route not available in this implementation");
4378fae3551SRodney W. Grimes #endif /* IP_OPTIONS */
4388fae3551SRodney W. Grimes 	}
4398fae3551SRodney W. Grimes 
44085456935SBill Fenner 	if (options & F_NOLOOP) {
44185456935SBill Fenner 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop,
44285456935SBill Fenner 		    sizeof(loop)) < 0) {
44343470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP");
44485456935SBill Fenner 		}
44585456935SBill Fenner 	}
44685456935SBill Fenner 	if (options & F_MTTL) {
44785456935SBill Fenner 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,
44885456935SBill Fenner 		    sizeof(ttl)) < 0) {
44943470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_TTL");
45085456935SBill Fenner 		}
45185456935SBill Fenner 	}
45285456935SBill Fenner 	if (options & F_MIF) {
45385456935SBill Fenner 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr,
45485456935SBill Fenner 		    sizeof(ifaddr)) < 0) {
45543470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_IF");
45685456935SBill Fenner 		}
45785456935SBill Fenner 	}
458039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
459039d6aa4SBill Fenner 	{ int on = 1;
460039d6aa4SBill Fenner 	if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) < 0)
461039d6aa4SBill Fenner 		err(EX_OSERR, "setsockopt SO_TIMESTAMP");
462039d6aa4SBill Fenner 	}
463039d6aa4SBill Fenner #endif
46485456935SBill Fenner 
4658fae3551SRodney W. Grimes 	/*
4668fae3551SRodney W. Grimes 	 * When pinging the broadcast address, you can get a lot of answers.
4678fae3551SRodney W. Grimes 	 * Doing something so evil is useful if you are trying to stress the
4688fae3551SRodney W. Grimes 	 * ethernet, or just want to fill the arp cache to get some stuff for
46943470e3bSGarrett Wollman 	 * /etc/ethers.  But beware: RFC 1122 allows hosts to ignore broadcast
47043470e3bSGarrett Wollman 	 * or multicast pings if they wish.
4718fae3551SRodney W. Grimes 	 */
4728fae3551SRodney W. Grimes 	hold = 48 * 1024;
4738fae3551SRodney W. Grimes 	(void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
4748fae3551SRodney W. Grimes 	    sizeof(hold));
4758fae3551SRodney W. Grimes 
47699490edeSWarner Losh 	if (to->sin_family == AF_INET) {
47799490edeSWarner Losh 		(void)printf("PING %s (%s)", hostname,
47899490edeSWarner Losh 		    inet_ntoa(to->sin_addr));
47999490edeSWarner Losh 		if (source)
48099490edeSWarner Losh 			(void)printf(" from %s", shostname);
48199490edeSWarner Losh 		(void)printf(": %d data bytes\n", datalen);
48299490edeSWarner Losh 	} else
4838fae3551SRodney W. Grimes 		(void)printf("PING %s: %d data bytes\n", hostname, datalen);
4848fae3551SRodney W. Grimes 
4857d81b35cSBruce Evans 	/*
486a2a00888SSean Eric Fagan 	 * Use sigaction() instead of signal() to get unambiguous semantics,
487a2a00888SSean Eric Fagan 	 * in particular with SA_RESTART not set.
4887d81b35cSBruce Evans 	 */
489a2a00888SSean Eric Fagan 
490f6bd468bSPaul Traina 	sigemptyset(&si_sa.sa_mask);
49137e5b2c6SSean Eric Fagan 	si_sa.sa_flags = 0;
492a2a00888SSean Eric Fagan 
493a2a00888SSean Eric Fagan 	si_sa.sa_handler = stopit;
494a2a00888SSean Eric Fagan 	if (sigaction(SIGINT, &si_sa, 0) == -1) {
495a2a00888SSean Eric Fagan 		err(EX_OSERR, "sigaction SIGINT");
496a2a00888SSean Eric Fagan 	}
497a2a00888SSean Eric Fagan 
498a2a00888SSean Eric Fagan 	si_sa.sa_handler = status;
49937e5b2c6SSean Eric Fagan 	if (sigaction(SIGINFO, &si_sa, 0) == -1) {
500624ff938SWarner Losh 		err(EX_OSERR, "sigaction");
50137e5b2c6SSean Eric Fagan 	}
50237e5b2c6SSean Eric Fagan 
503039d6aa4SBill Fenner 	bzero(&msg, sizeof(msg));
504039d6aa4SBill Fenner 	msg.msg_name = (caddr_t)&from;
505039d6aa4SBill Fenner 	msg.msg_iov = &iov;
506039d6aa4SBill Fenner 	msg.msg_iovlen = 1;
507039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
508039d6aa4SBill Fenner 	msg.msg_control = (caddr_t)ctrl;
509039d6aa4SBill Fenner #endif
510039d6aa4SBill Fenner 	iov.iov_base = packet;
511039d6aa4SBill Fenner 	iov.iov_len = packlen;
512039d6aa4SBill Fenner 
5134055611aSSean Eric Fagan 	if (tcgetattr(STDOUT_FILENO, &ts) != -1) {
5144055611aSSean Eric Fagan 		reset_kerninfo = !(ts.c_lflag & NOKERNINFO);
5154055611aSSean Eric Fagan 		ts.c_lflag |= NOKERNINFO;
5164055611aSSean Eric Fagan 		tcsetattr(STDOUT_FILENO, TCSANOW, &ts);
5174055611aSSean Eric Fagan 	}
5184055611aSSean Eric Fagan 
5198fae3551SRodney W. Grimes 	while (preload--)		/* fire off them quickies */
5208fae3551SRodney W. Grimes 		pinger();
5218fae3551SRodney W. Grimes 
522039d6aa4SBill Fenner 	if (options & F_FLOOD) {
523039d6aa4SBill Fenner 		intvl.tv_sec = 0;
524039d6aa4SBill Fenner 		intvl.tv_usec = 10000;
525039d6aa4SBill Fenner 	} else {
526526f06b2SMatthew Dillon 		intvl.tv_sec = interval / 1000;
527526f06b2SMatthew Dillon 		intvl.tv_usec = interval % 1000 * 1000;
528039d6aa4SBill Fenner 	}
529039d6aa4SBill Fenner 
530039d6aa4SBill Fenner 	pinger();			/* send the first ping */
531039d6aa4SBill Fenner 	(void)gettimeofday(&last, NULL);
5328fae3551SRodney W. Grimes 
5338f975bb3SBruce Evans 	while (!finish_up) {
5348fae3551SRodney W. Grimes 		register int cc;
535039d6aa4SBill Fenner 		int n;
536039d6aa4SBill Fenner 		struct timeval timeout, now;
537039d6aa4SBill Fenner 		fd_set rfds;
5388fae3551SRodney W. Grimes 
5397d81b35cSBruce Evans 		check_status();
540039d6aa4SBill Fenner 		FD_ZERO(&rfds);
541039d6aa4SBill Fenner 		FD_SET(s, &rfds);
542039d6aa4SBill Fenner 		(void)gettimeofday(&now, NULL);
543039d6aa4SBill Fenner 		timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec;
544039d6aa4SBill Fenner 		timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec;
545039d6aa4SBill Fenner 		while (timeout.tv_usec < 0) {
546039d6aa4SBill Fenner 			timeout.tv_usec += 1000000;
547039d6aa4SBill Fenner 			timeout.tv_sec--;
5488fae3551SRodney W. Grimes 		}
549e345a80dSPhilippe Charnier 		while (timeout.tv_usec >= 1000000) {
550039d6aa4SBill Fenner 			timeout.tv_usec -= 1000000;
551039d6aa4SBill Fenner 			timeout.tv_sec++;
552039d6aa4SBill Fenner 		}
553039d6aa4SBill Fenner 		if (timeout.tv_sec < 0)
554039d6aa4SBill Fenner 			timeout.tv_sec = timeout.tv_usec = 0;
555039d6aa4SBill Fenner 		n = select(s + 1, &rfds, NULL, NULL, &timeout);
5565e2cc0f4SStephen McKay 		if (n < 0)
5575e2cc0f4SStephen McKay 			continue;	/* Must be EINTR. */
558039d6aa4SBill Fenner 		if (n == 1) {
559039d6aa4SBill Fenner 			struct timeval *t = 0;
560039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
561039d6aa4SBill Fenner 			struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl;
562039d6aa4SBill Fenner 
563039d6aa4SBill Fenner 			msg.msg_controllen = sizeof(ctrl);
564039d6aa4SBill Fenner #endif
565039d6aa4SBill Fenner 			msg.msg_namelen = sizeof(from);
566039d6aa4SBill Fenner 			if ((cc = recvmsg(s, &msg, 0)) < 0) {
5678fae3551SRodney W. Grimes 				if (errno == EINTR)
5688fae3551SRodney W. Grimes 					continue;
569e345a80dSPhilippe Charnier 				warn("recvmsg");
5708fae3551SRodney W. Grimes 				continue;
5718fae3551SRodney W. Grimes 			}
572039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
573039d6aa4SBill Fenner 			if (cmsg->cmsg_level == SOL_SOCKET &&
574039d6aa4SBill Fenner 			    cmsg->cmsg_type == SCM_TIMESTAMP &&
575fa05a94cSJohn Birrell 			    cmsg->cmsg_len == (sizeof *cmsg + sizeof *t)) {
576fa05a94cSJohn Birrell 				/* Copy to avoid alignment problems: */
577fa05a94cSJohn Birrell 				memcpy(&now,CMSG_DATA(cmsg),sizeof(now));
578fa05a94cSJohn Birrell 				t = &now;
579fa05a94cSJohn Birrell 			}
580039d6aa4SBill Fenner #endif
581039d6aa4SBill Fenner 			if (t == 0) {
582039d6aa4SBill Fenner 				(void)gettimeofday(&now, NULL);
583039d6aa4SBill Fenner 				t = &now;
584039d6aa4SBill Fenner 			}
585039d6aa4SBill Fenner 			pr_pack((char *)packet, cc, &from, t);
5868fae3551SRodney W. Grimes 			if (npackets && nreceived >= npackets)
5878fae3551SRodney W. Grimes 				break;
5888fae3551SRodney W. Grimes 		}
5895e2cc0f4SStephen McKay 		if (n == 0 || options & F_FLOOD) {
590039d6aa4SBill Fenner 			if (!npackets || ntransmitted < npackets)
591039d6aa4SBill Fenner 				pinger();
592039d6aa4SBill Fenner 			else {
593039d6aa4SBill Fenner 				if (almost_done)
594039d6aa4SBill Fenner 					break;
595039d6aa4SBill Fenner 				almost_done = 1;
5965e2cc0f4SStephen McKay 				intvl.tv_usec = 0;
597039d6aa4SBill Fenner 				if (nreceived) {
598039d6aa4SBill Fenner 					intvl.tv_sec = 2 * tmax / 1000;
599039d6aa4SBill Fenner 					if (!intvl.tv_sec)
600039d6aa4SBill Fenner 						intvl.tv_sec = 1;
601039d6aa4SBill Fenner 				} else
602039d6aa4SBill Fenner 					intvl.tv_sec = MAXWAIT;
603039d6aa4SBill Fenner 			}
604039d6aa4SBill Fenner 			(void)gettimeofday(&last, NULL);
605039d6aa4SBill Fenner 		}
606039d6aa4SBill Fenner 	}
6078f975bb3SBruce Evans 	finish();
6088fae3551SRodney W. Grimes 	/* NOTREACHED */
609f78ac61bSWarner Losh 	exit(0);	/* Make the compiler happy */
6108fae3551SRodney W. Grimes }
6118fae3551SRodney W. Grimes 
6128fae3551SRodney W. Grimes /*
6138f975bb3SBruce Evans  * stopit --
6148f975bb3SBruce Evans  *	Set the global bit that causes the main loop to quit.
6158f975bb3SBruce Evans  * Do NOT call finish() from here, since finish() does far too much
6168f975bb3SBruce Evans  * to be called from a signal handler.
617515dd2faSJulian Elischer  */
618515dd2faSJulian Elischer void
6198f975bb3SBruce Evans stopit(sig)
6208f975bb3SBruce Evans 	int sig;
621515dd2faSJulian Elischer {
622515dd2faSJulian Elischer 	finish_up = 1;
623515dd2faSJulian Elischer }
624515dd2faSJulian Elischer 
625515dd2faSJulian Elischer /*
6268fae3551SRodney W. Grimes  * pinger --
6278fae3551SRodney W. Grimes  *	Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
6288fae3551SRodney W. Grimes  * will be added on by the kernel.  The ID field is our UNIX process ID,
6298fae3551SRodney W. Grimes  * and the sequence number is an ascending integer.  The first 8 bytes
630ef9e6dc7SBill Fenner  * of the data portion are used to hold a UNIX "timeval" struct in host
6318fae3551SRodney W. Grimes  * byte-order, to compute the round-trip time.
6328fae3551SRodney W. Grimes  */
63343470e3bSGarrett Wollman static void
63443470e3bSGarrett Wollman pinger(void)
6358fae3551SRodney W. Grimes {
6368fae3551SRodney W. Grimes 	register struct icmp *icp;
6378fae3551SRodney W. Grimes 	register int cc;
6388fae3551SRodney W. Grimes 	int i;
6398fae3551SRodney W. Grimes 
6408fae3551SRodney W. Grimes 	icp = (struct icmp *)outpack;
6418fae3551SRodney W. Grimes 	icp->icmp_type = ICMP_ECHO;
6428fae3551SRodney W. Grimes 	icp->icmp_code = 0;
6438fae3551SRodney W. Grimes 	icp->icmp_cksum = 0;
6440e59c641SJulian Elischer 	icp->icmp_seq = ntransmitted;
6458fae3551SRodney W. Grimes 	icp->icmp_id = ident;			/* ID */
6468fae3551SRodney W. Grimes 
6478fae3551SRodney W. Grimes 	CLR(icp->icmp_seq % mx_dup_ck);
6488fae3551SRodney W. Grimes 
6498fae3551SRodney W. Grimes 	if (timing)
6508fae3551SRodney W. Grimes 		(void)gettimeofday((struct timeval *)&outpack[8],
6518fae3551SRodney W. Grimes 		    (struct timezone *)NULL);
6528fae3551SRodney W. Grimes 
653d32ff037SJohn Birrell 	cc = datalen + PHDR_LEN;		/* skips ICMP portion */
6548fae3551SRodney W. Grimes 
6558fae3551SRodney W. Grimes 	/* compute ICMP checksum here */
6568fae3551SRodney W. Grimes 	icp->icmp_cksum = in_cksum((u_short *)icp, cc);
6578fae3551SRodney W. Grimes 
6588fae3551SRodney W. Grimes 	i = sendto(s, (char *)outpack, cc, 0, &whereto,
6598fae3551SRodney W. Grimes 	    sizeof(struct sockaddr));
6608fae3551SRodney W. Grimes 
6618fae3551SRodney W. Grimes 	if (i < 0 || i != cc)  {
66243470e3bSGarrett Wollman 		if (i < 0) {
6638f975bb3SBruce Evans 			if (options & F_FLOOD && errno == ENOBUFS) {
664515dd2faSJulian Elischer 				usleep(FLOOD_BACKOFF);
665515dd2faSJulian Elischer 				return;
666515dd2faSJulian Elischer 			}
66743470e3bSGarrett Wollman 			warn("sendto");
66843470e3bSGarrett Wollman 		} else {
66943470e3bSGarrett Wollman 			warn("%s: partial write: %d of %d bytes",
670416aa49bSPoul-Henning Kamp 			     hostname, i, cc);
6718fae3551SRodney W. Grimes 		}
672363d7bbeSJulian Elischer 	}
673363d7bbeSJulian Elischer 	ntransmitted++;
6748fae3551SRodney W. Grimes 	if (!(options & F_QUIET) && options & F_FLOOD)
6758fae3551SRodney W. Grimes 		(void)write(STDOUT_FILENO, &DOT, 1);
6768fae3551SRodney W. Grimes }
6778fae3551SRodney W. Grimes 
6788fae3551SRodney W. Grimes /*
6798fae3551SRodney W. Grimes  * pr_pack --
6808fae3551SRodney W. Grimes  *	Print out the packet, if it came from us.  This logic is necessary
6818fae3551SRodney W. Grimes  * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
6828fae3551SRodney W. Grimes  * which arrive ('tis only fair).  This permits multiple copies of this
6838fae3551SRodney W. Grimes  * program to be run without having intermingled output (or statistics!).
6848fae3551SRodney W. Grimes  */
68543470e3bSGarrett Wollman static void
686039d6aa4SBill Fenner pr_pack(buf, cc, from, tv)
6878fae3551SRodney W. Grimes 	char *buf;
6888fae3551SRodney W. Grimes 	int cc;
6898fae3551SRodney W. Grimes 	struct sockaddr_in *from;
690039d6aa4SBill Fenner 	struct timeval *tv;
6918fae3551SRodney W. Grimes {
6928fae3551SRodney W. Grimes 	register struct icmp *icp;
6938fae3551SRodney W. Grimes 	register u_long l;
6948fae3551SRodney W. Grimes 	register int i, j;
6958fae3551SRodney W. Grimes 	register u_char *cp,*dp;
6968fae3551SRodney W. Grimes 	static int old_rrlen;
6978fae3551SRodney W. Grimes 	static char old_rr[MAX_IPOPTLEN];
6988fae3551SRodney W. Grimes 	struct ip *ip;
699039d6aa4SBill Fenner 	struct timeval *tp;
7008f975bb3SBruce Evans 	double triptime;
7018fae3551SRodney W. Grimes 	int hlen, dupflag;
7028fae3551SRodney W. Grimes 
7038fae3551SRodney W. Grimes 	/* Check the IP header */
7048fae3551SRodney W. Grimes 	ip = (struct ip *)buf;
7058fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
7068fae3551SRodney W. Grimes 	if (cc < hlen + ICMP_MINLEN) {
7078fae3551SRodney W. Grimes 		if (options & F_VERBOSE)
70843470e3bSGarrett Wollman 			warn("packet too short (%d bytes) from %s", cc,
70943470e3bSGarrett Wollman 			     inet_ntoa(from->sin_addr));
7108fae3551SRodney W. Grimes 		return;
7118fae3551SRodney W. Grimes 	}
7128fae3551SRodney W. Grimes 
7138fae3551SRodney W. Grimes 	/* Now the ICMP part */
7148fae3551SRodney W. Grimes 	cc -= hlen;
7158fae3551SRodney W. Grimes 	icp = (struct icmp *)(buf + hlen);
7168fae3551SRodney W. Grimes 	if (icp->icmp_type == ICMP_ECHOREPLY) {
7178fae3551SRodney W. Grimes 		if (icp->icmp_id != ident)
7188fae3551SRodney W. Grimes 			return;			/* 'Twas not our ECHO */
7198fae3551SRodney W. Grimes 		++nreceived;
7208f975bb3SBruce Evans 		triptime = 0.0;
7218fae3551SRodney W. Grimes 		if (timing) {
722d32ff037SJohn Birrell 			struct timeval tv1;
7238fae3551SRodney W. Grimes #ifndef icmp_data
7248fae3551SRodney W. Grimes 			tp = (struct timeval *)&icp->icmp_ip;
7258fae3551SRodney W. Grimes #else
7268fae3551SRodney W. Grimes 			tp = (struct timeval *)icp->icmp_data;
7278fae3551SRodney W. Grimes #endif
728d32ff037SJohn Birrell 			/* Avoid unaligned data: */
729d32ff037SJohn Birrell 			memcpy(&tv1,tp,sizeof(tv1));
730039d6aa4SBill Fenner 			tvsub(tv, &tv1);
731039d6aa4SBill Fenner  			triptime = ((double)tv->tv_sec) * 1000.0 +
732039d6aa4SBill Fenner  			    ((double)tv->tv_usec) / 1000.0;
7338fae3551SRodney W. Grimes 			tsum += triptime;
7343109a910SGarrett Wollman 			tsumsq += triptime * triptime;
7358fae3551SRodney W. Grimes 			if (triptime < tmin)
7368fae3551SRodney W. Grimes 				tmin = triptime;
7378fae3551SRodney W. Grimes 			if (triptime > tmax)
7388fae3551SRodney W. Grimes 				tmax = triptime;
7398fae3551SRodney W. Grimes 		}
7408fae3551SRodney W. Grimes 
7418fae3551SRodney W. Grimes 		if (TST(icp->icmp_seq % mx_dup_ck)) {
7428fae3551SRodney W. Grimes 			++nrepeats;
7438fae3551SRodney W. Grimes 			--nreceived;
7448fae3551SRodney W. Grimes 			dupflag = 1;
7458fae3551SRodney W. Grimes 		} else {
7468fae3551SRodney W. Grimes 			SET(icp->icmp_seq % mx_dup_ck);
7478fae3551SRodney W. Grimes 			dupflag = 0;
7488fae3551SRodney W. Grimes 		}
7498fae3551SRodney W. Grimes 
7508fae3551SRodney W. Grimes 		if (options & F_QUIET)
7518fae3551SRodney W. Grimes 			return;
7528fae3551SRodney W. Grimes 
7538fae3551SRodney W. Grimes 		if (options & F_FLOOD)
7548fae3551SRodney W. Grimes 			(void)write(STDOUT_FILENO, &BSPACE, 1);
7558fae3551SRodney W. Grimes 		else {
7568fae3551SRodney W. Grimes 			(void)printf("%d bytes from %s: icmp_seq=%u", cc,
7578fae3551SRodney W. Grimes 			   inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr),
7588fae3551SRodney W. Grimes 			   icp->icmp_seq);
7598fae3551SRodney W. Grimes 			(void)printf(" ttl=%d", ip->ip_ttl);
7608fae3551SRodney W. Grimes 			if (timing)
761d410b6f1SDavid Greenman 				(void)printf(" time=%.3f ms", triptime);
7628fae3551SRodney W. Grimes 			if (dupflag)
7638fae3551SRodney W. Grimes 				(void)printf(" (DUP!)");
764772dfa72SDaniel O'Callaghan 			if (options & F_AUDIBLE)
765772dfa72SDaniel O'Callaghan 				(void)printf("\a");
7668fae3551SRodney W. Grimes 			/* check the data */
767d32ff037SJohn Birrell 			cp = (u_char*)&icp->icmp_data[PHDR_LEN];
768d32ff037SJohn Birrell 			dp = &outpack[8 + PHDR_LEN];
769d32ff037SJohn Birrell 			for (i = PHDR_LEN; i < datalen; ++i, ++cp, ++dp) {
7708fae3551SRodney W. Grimes 				if (*cp != *dp) {
7718fae3551SRodney W. Grimes 	(void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x",
7728fae3551SRodney W. Grimes 	    i, *dp, *cp);
773d32ff037SJohn Birrell 					printf("\ncp:");
7748fae3551SRodney W. Grimes 					cp = (u_char*)&icp->icmp_data[0];
775d32ff037SJohn Birrell 					for (i = 0; i < datalen; ++i, ++cp) {
776d32ff037SJohn Birrell 						if ((i % 32) == 8)
777d32ff037SJohn Birrell 							(void)printf("\n\t");
778d32ff037SJohn Birrell 						(void)printf("%x ", *cp);
779d32ff037SJohn Birrell 					}
780d32ff037SJohn Birrell 					printf("\ndp:");
781d32ff037SJohn Birrell 					cp = &outpack[8];
782d32ff037SJohn Birrell 					for (i = 0; i < datalen; ++i, ++cp) {
7838fae3551SRodney W. Grimes 						if ((i % 32) == 8)
7848fae3551SRodney W. Grimes 							(void)printf("\n\t");
7858fae3551SRodney W. Grimes 						(void)printf("%x ", *cp);
7868fae3551SRodney W. Grimes 					}
7878fae3551SRodney W. Grimes 					break;
7888fae3551SRodney W. Grimes 				}
7898fae3551SRodney W. Grimes 			}
7908fae3551SRodney W. Grimes 		}
7918fae3551SRodney W. Grimes 	} else {
792ef9e6dc7SBill Fenner 		/*
793ef9e6dc7SBill Fenner 		 * We've got something other than an ECHOREPLY.
794ef9e6dc7SBill Fenner 		 * See if it's a reply to something that we sent.
795ef9e6dc7SBill Fenner 		 * We can compare IP destination, protocol,
796ef9e6dc7SBill Fenner 		 * and ICMP type and ID.
797f78ac61bSWarner Losh 		 *
798f78ac61bSWarner Losh 		 * Only print all the error messages if we are running
799f78ac61bSWarner Losh 		 * as root to avoid leaking information not normally
800f78ac61bSWarner Losh 		 * available to those not running as root.
801ef9e6dc7SBill Fenner 		 */
802ef9e6dc7SBill Fenner #ifndef icmp_data
803ef9e6dc7SBill Fenner 		struct ip *oip = &icp->icmp_ip;
804ef9e6dc7SBill Fenner #else
805ef9e6dc7SBill Fenner 		struct ip *oip = (struct ip *)icp->icmp_data;
806ef9e6dc7SBill Fenner #endif
807ef9e6dc7SBill Fenner 		struct icmp *oicmp = (struct icmp *)(oip + 1);
808ef9e6dc7SBill Fenner 
809ee2bf734SWarner Losh 		if (((options & F_VERBOSE) && uid == 0) ||
810ef9e6dc7SBill Fenner 		    (!(options & F_QUIET2) &&
811ef9e6dc7SBill Fenner 		     (oip->ip_dst.s_addr ==
812ef9e6dc7SBill Fenner 			 ((struct sockaddr_in *)&whereto)->sin_addr.s_addr) &&
813ef9e6dc7SBill Fenner 		     (oip->ip_p == IPPROTO_ICMP) &&
814ef9e6dc7SBill Fenner 		     (oicmp->icmp_type == ICMP_ECHO) &&
815ef9e6dc7SBill Fenner 		     (oicmp->icmp_id == ident))) {
8168fae3551SRodney W. Grimes 		    (void)printf("%d bytes from %s: ", cc,
81743470e3bSGarrett Wollman 			pr_addr(from->sin_addr));
8188fae3551SRodney W. Grimes 		    pr_icmph(icp);
819ef9e6dc7SBill Fenner 		} else
820ef9e6dc7SBill Fenner 		    return;
8218fae3551SRodney W. Grimes 	}
8228fae3551SRodney W. Grimes 
8238fae3551SRodney W. Grimes 	/* Display any IP options */
8248fae3551SRodney W. Grimes 	cp = (u_char *)buf + sizeof(struct ip);
8258fae3551SRodney W. Grimes 
8268fae3551SRodney W. Grimes 	for (; hlen > (int)sizeof(struct ip); --hlen, ++cp)
8278fae3551SRodney W. Grimes 		switch (*cp) {
8288fae3551SRodney W. Grimes 		case IPOPT_EOL:
8298fae3551SRodney W. Grimes 			hlen = 0;
8308fae3551SRodney W. Grimes 			break;
8318fae3551SRodney W. Grimes 		case IPOPT_LSRR:
8328fae3551SRodney W. Grimes 			(void)printf("\nLSRR: ");
8338fae3551SRodney W. Grimes 			hlen -= 2;
8348fae3551SRodney W. Grimes 			j = *++cp;
8358fae3551SRodney W. Grimes 			++cp;
8368fae3551SRodney W. Grimes 			if (j > IPOPT_MINOFF)
8378fae3551SRodney W. Grimes 				for (;;) {
8388fae3551SRodney W. Grimes 					l = *++cp;
8398fae3551SRodney W. Grimes 					l = (l<<8) + *++cp;
8408fae3551SRodney W. Grimes 					l = (l<<8) + *++cp;
8418fae3551SRodney W. Grimes 					l = (l<<8) + *++cp;
84243470e3bSGarrett Wollman 					if (l == 0) {
84343470e3bSGarrett Wollman 						printf("\t0.0.0.0");
84443470e3bSGarrett Wollman 					} else {
84543470e3bSGarrett Wollman 						struct in_addr ina;
84643470e3bSGarrett Wollman 						ina.s_addr = ntohl(l);
84743470e3bSGarrett Wollman 						printf("\t%s", pr_addr(ina));
84843470e3bSGarrett Wollman 					}
8498fae3551SRodney W. Grimes 				hlen -= 4;
8508fae3551SRodney W. Grimes 				j -= 4;
8518fae3551SRodney W. Grimes 				if (j <= IPOPT_MINOFF)
8528fae3551SRodney W. Grimes 					break;
8538fae3551SRodney W. Grimes 				(void)putchar('\n');
8548fae3551SRodney W. Grimes 			}
8558fae3551SRodney W. Grimes 			break;
8568fae3551SRodney W. Grimes 		case IPOPT_RR:
8578fae3551SRodney W. Grimes 			j = *++cp;		/* get length */
8588fae3551SRodney W. Grimes 			i = *++cp;		/* and pointer */
8598fae3551SRodney W. Grimes 			hlen -= 2;
8608fae3551SRodney W. Grimes 			if (i > j)
8618fae3551SRodney W. Grimes 				i = j;
8628fae3551SRodney W. Grimes 			i -= IPOPT_MINOFF;
8638fae3551SRodney W. Grimes 			if (i <= 0)
8648fae3551SRodney W. Grimes 				continue;
8658fae3551SRodney W. Grimes 			if (i == old_rrlen
8668fae3551SRodney W. Grimes 			    && cp == (u_char *)buf + sizeof(struct ip) + 2
8678fae3551SRodney W. Grimes 			    && !bcmp((char *)cp, old_rr, i)
8688fae3551SRodney W. Grimes 			    && !(options & F_FLOOD)) {
8698fae3551SRodney W. Grimes 				(void)printf("\t(same route)");
8708fae3551SRodney W. Grimes 				i = ((i + 3) / 4) * 4;
8718fae3551SRodney W. Grimes 				hlen -= i;
8728fae3551SRodney W. Grimes 				cp += i;
8738fae3551SRodney W. Grimes 				break;
8748fae3551SRodney W. Grimes 			}
8753aa4b744SEivind Eklund 			if (i < MAX_IPOPTLEN) {
8768fae3551SRodney W. Grimes 				old_rrlen = i;
8778fae3551SRodney W. Grimes 				bcopy((char *)cp, old_rr, i);
878c03e877aSWarner Losh 			} else
879c03e877aSWarner Losh 				old_rrlen = 0;
880c03e877aSWarner Losh 
8818fae3551SRodney W. Grimes 			(void)printf("\nRR: ");
882c03e877aSWarner Losh 			j = 0;
8838fae3551SRodney W. Grimes 			for (;;) {
8848fae3551SRodney W. Grimes 				l = *++cp;
8858fae3551SRodney W. Grimes 				l = (l<<8) + *++cp;
8868fae3551SRodney W. Grimes 				l = (l<<8) + *++cp;
8878fae3551SRodney W. Grimes 				l = (l<<8) + *++cp;
88843470e3bSGarrett Wollman 				if (l == 0) {
88943470e3bSGarrett Wollman 					printf("\t0.0.0.0");
89043470e3bSGarrett Wollman 				} else {
89143470e3bSGarrett Wollman 					struct in_addr ina;
89243470e3bSGarrett Wollman 					ina.s_addr = ntohl(l);
89343470e3bSGarrett Wollman 					printf("\t%s", pr_addr(ina));
89443470e3bSGarrett Wollman 				}
8958fae3551SRodney W. Grimes 				hlen -= 4;
8968fae3551SRodney W. Grimes 				i -= 4;
897c03e877aSWarner Losh 				j += 4;
8988fae3551SRodney W. Grimes 				if (i <= 0)
8998fae3551SRodney W. Grimes 					break;
900c03e877aSWarner Losh 				if (j >= MAX_IPOPTLEN) {
901c03e877aSWarner Losh 					(void) printf("\t(truncated route)");
902c03e877aSWarner Losh 					break;
903c03e877aSWarner Losh 				}
9048fae3551SRodney W. Grimes 				(void)putchar('\n');
9058fae3551SRodney W. Grimes 			}
9068fae3551SRodney W. Grimes 			break;
9078fae3551SRodney W. Grimes 		case IPOPT_NOP:
9088fae3551SRodney W. Grimes 			(void)printf("\nNOP");
9098fae3551SRodney W. Grimes 			break;
9108fae3551SRodney W. Grimes 		default:
9118fae3551SRodney W. Grimes 			(void)printf("\nunknown option %x", *cp);
9128fae3551SRodney W. Grimes 			break;
9138fae3551SRodney W. Grimes 		}
9148fae3551SRodney W. Grimes 	if (!(options & F_FLOOD)) {
9158fae3551SRodney W. Grimes 		(void)putchar('\n');
9168fae3551SRodney W. Grimes 		(void)fflush(stdout);
9178fae3551SRodney W. Grimes 	}
9188fae3551SRodney W. Grimes }
9198fae3551SRodney W. Grimes 
9208fae3551SRodney W. Grimes /*
9218fae3551SRodney W. Grimes  * in_cksum --
9228fae3551SRodney W. Grimes  *	Checksum routine for Internet Protocol family headers (C Version)
9238fae3551SRodney W. Grimes  */
92443470e3bSGarrett Wollman u_short
9258fae3551SRodney W. Grimes in_cksum(addr, len)
9268fae3551SRodney W. Grimes 	u_short *addr;
9278fae3551SRodney W. Grimes 	int len;
9288fae3551SRodney W. Grimes {
9298fae3551SRodney W. Grimes 	register int nleft = len;
9308fae3551SRodney W. Grimes 	register u_short *w = addr;
9318fae3551SRodney W. Grimes 	register int sum = 0;
9328fae3551SRodney W. Grimes 	u_short answer = 0;
9338fae3551SRodney W. Grimes 
9348fae3551SRodney W. Grimes 	/*
9358fae3551SRodney W. Grimes 	 * Our algorithm is simple, using a 32 bit accumulator (sum), we add
9368fae3551SRodney W. Grimes 	 * sequential 16 bit words to it, and at the end, fold back all the
9378fae3551SRodney W. Grimes 	 * carry bits from the top 16 bits into the lower 16 bits.
9388fae3551SRodney W. Grimes 	 */
9398fae3551SRodney W. Grimes 	while (nleft > 1)  {
9408fae3551SRodney W. Grimes 		sum += *w++;
9418fae3551SRodney W. Grimes 		nleft -= 2;
9428fae3551SRodney W. Grimes 	}
9438fae3551SRodney W. Grimes 
9448fae3551SRodney W. Grimes 	/* mop up an odd byte, if necessary */
9458fae3551SRodney W. Grimes 	if (nleft == 1) {
9468fae3551SRodney W. Grimes 		*(u_char *)(&answer) = *(u_char *)w ;
9478fae3551SRodney W. Grimes 		sum += answer;
9488fae3551SRodney W. Grimes 	}
9498fae3551SRodney W. Grimes 
9508fae3551SRodney W. Grimes 	/* add back carry outs from top 16 bits to low 16 bits */
9518fae3551SRodney W. Grimes 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
9528fae3551SRodney W. Grimes 	sum += (sum >> 16);			/* add carry */
9538fae3551SRodney W. Grimes 	answer = ~sum;				/* truncate to 16 bits */
9548fae3551SRodney W. Grimes 	return(answer);
9558fae3551SRodney W. Grimes }
9568fae3551SRodney W. Grimes 
9578fae3551SRodney W. Grimes /*
9588fae3551SRodney W. Grimes  * tvsub --
9598fae3551SRodney W. Grimes  *	Subtract 2 timeval structs:  out = out - in.  Out is assumed to
9608fae3551SRodney W. Grimes  * be >= in.
9618fae3551SRodney W. Grimes  */
96243470e3bSGarrett Wollman static void
9638fae3551SRodney W. Grimes tvsub(out, in)
9648fae3551SRodney W. Grimes 	register struct timeval *out, *in;
9658fae3551SRodney W. Grimes {
9668fae3551SRodney W. Grimes 	if ((out->tv_usec -= in->tv_usec) < 0) {
9678fae3551SRodney W. Grimes 		--out->tv_sec;
9688fae3551SRodney W. Grimes 		out->tv_usec += 1000000;
9698fae3551SRodney W. Grimes 	}
9708fae3551SRodney W. Grimes 	out->tv_sec -= in->tv_sec;
9718fae3551SRodney W. Grimes }
9728fae3551SRodney W. Grimes 
9738fae3551SRodney W. Grimes /*
974badd8138SSean Eric Fagan  * status --
975badd8138SSean Eric Fagan  *	Print out statistics when SIGINFO is received.
976badd8138SSean Eric Fagan  */
977badd8138SSean Eric Fagan 
97843470e3bSGarrett Wollman static void
9797d81b35cSBruce Evans status(sig)
9807d81b35cSBruce Evans 	int sig;
9817d81b35cSBruce Evans {
98237e5b2c6SSean Eric Fagan 	siginfo_p = 1;
98337e5b2c6SSean Eric Fagan }
98437e5b2c6SSean Eric Fagan 
98543470e3bSGarrett Wollman static void
98637e5b2c6SSean Eric Fagan check_status()
987badd8138SSean Eric Fagan {
98837e5b2c6SSean Eric Fagan 	if (siginfo_p) {
98937e5b2c6SSean Eric Fagan 		siginfo_p = 0;
9907d81b35cSBruce Evans 		(void)fprintf(stderr,
9917d81b35cSBruce Evans 	"\r%ld/%ld packets received (%.0f%%) %.3f min / %.3f avg / %.3f max\n",
9927d81b35cSBruce Evans 		    nreceived, ntransmitted,
9937d81b35cSBruce Evans 		    ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0,
9947d81b35cSBruce Evans 		    nreceived ? tmin : 0.0,
9957d81b35cSBruce Evans 		    nreceived + nrepeats ? tsum / (nreceived + nrepeats) : tsum,
9967d81b35cSBruce Evans 		    tmax);
99737e5b2c6SSean Eric Fagan 	}
998badd8138SSean Eric Fagan }
999badd8138SSean Eric Fagan 
1000badd8138SSean Eric Fagan /*
10018fae3551SRodney W. Grimes  * finish --
10028fae3551SRodney W. Grimes  *	Print out statistics, and give up.
10038fae3551SRodney W. Grimes  */
100443470e3bSGarrett Wollman static void
10058f975bb3SBruce Evans finish()
10068fae3551SRodney W. Grimes {
1007badd8138SSean Eric Fagan 	struct termios ts;
10088fae3551SRodney W. Grimes 
10098fae3551SRodney W. Grimes 	(void)signal(SIGINT, SIG_IGN);
1010a2a00888SSean Eric Fagan 	(void)signal(SIGALRM, SIG_IGN);
10118fae3551SRodney W. Grimes 	(void)putchar('\n');
10128fae3551SRodney W. Grimes 	(void)fflush(stdout);
10138fae3551SRodney W. Grimes 	(void)printf("--- %s ping statistics ---\n", hostname);
10148fae3551SRodney W. Grimes 	(void)printf("%ld packets transmitted, ", ntransmitted);
10158fae3551SRodney W. Grimes 	(void)printf("%ld packets received, ", nreceived);
10168fae3551SRodney W. Grimes 	if (nrepeats)
10178fae3551SRodney W. Grimes 		(void)printf("+%ld duplicates, ", nrepeats);
1018ebe70c8fSWarner Losh 	if (ntransmitted) {
10198fae3551SRodney W. Grimes 		if (nreceived > ntransmitted)
10208fae3551SRodney W. Grimes 			(void)printf("-- somebody's printing up packets!");
10218fae3551SRodney W. Grimes 		else
10228fae3551SRodney W. Grimes 			(void)printf("%d%% packet loss",
10238fae3551SRodney W. Grimes 			    (int) (((ntransmitted - nreceived) * 100) /
10248fae3551SRodney W. Grimes 			    ntransmitted));
1025ebe70c8fSWarner Losh 	}
10268fae3551SRodney W. Grimes 	(void)putchar('\n');
10273109a910SGarrett Wollman 	if (nreceived && timing) {
10283109a910SGarrett Wollman 		double n = nreceived + nrepeats;
10293109a910SGarrett Wollman 		double avg = tsum / n;
10303109a910SGarrett Wollman 		double vari = tsumsq / n - avg * avg;
10313109a910SGarrett Wollman 		printf("round-trip min/avg/max/stddev = "
10323109a910SGarrett Wollman 		       "%.3f/%.3f/%.3f/%.3f ms\n",
10333109a910SGarrett Wollman 		    tmin, avg, tmax, sqrt(vari));
10343109a910SGarrett Wollman 	}
10352ceaae21SBruce Evans 	if (reset_kerninfo && tcgetattr(STDOUT_FILENO, &ts) != -1) {
1036badd8138SSean Eric Fagan 		ts.c_lflag &= ~NOKERNINFO;
10372ceaae21SBruce Evans 		tcsetattr(STDOUT_FILENO, TCSANOW, &ts);
1038badd8138SSean Eric Fagan 	}
1039badd8138SSean Eric Fagan 
10406e1173dcSDavid Greenman 	if (nreceived)
10418fae3551SRodney W. Grimes 		exit(0);
10426e1173dcSDavid Greenman 	else
10436e1173dcSDavid Greenman 		exit(2);
10448fae3551SRodney W. Grimes }
10458fae3551SRodney W. Grimes 
10468fae3551SRodney W. Grimes #ifdef notdef
10478fae3551SRodney W. Grimes static char *ttab[] = {
10488fae3551SRodney W. Grimes 	"Echo Reply",		/* ip + seq + udata */
10498fae3551SRodney W. Grimes 	"Dest Unreachable",	/* net, host, proto, port, frag, sr + IP */
10508fae3551SRodney W. Grimes 	"Source Quench",	/* IP */
10518fae3551SRodney W. Grimes 	"Redirect",		/* redirect type, gateway, + IP  */
10528fae3551SRodney W. Grimes 	"Echo",
10538fae3551SRodney W. Grimes 	"Time Exceeded",	/* transit, frag reassem + IP */
10548fae3551SRodney W. Grimes 	"Parameter Problem",	/* pointer + IP */
10558fae3551SRodney W. Grimes 	"Timestamp",		/* id + seq + three timestamps */
10568fae3551SRodney W. Grimes 	"Timestamp Reply",	/* " */
10578fae3551SRodney W. Grimes 	"Info Request",		/* id + sq */
10588fae3551SRodney W. Grimes 	"Info Reply"		/* " */
10598fae3551SRodney W. Grimes };
10608fae3551SRodney W. Grimes #endif
10618fae3551SRodney W. Grimes 
10628fae3551SRodney W. Grimes /*
10638fae3551SRodney W. Grimes  * pr_icmph --
10648fae3551SRodney W. Grimes  *	Print a descriptive string about an ICMP header.
10658fae3551SRodney W. Grimes  */
106643470e3bSGarrett Wollman static void
10678fae3551SRodney W. Grimes pr_icmph(icp)
10688fae3551SRodney W. Grimes 	struct icmp *icp;
10698fae3551SRodney W. Grimes {
10708fae3551SRodney W. Grimes 	switch(icp->icmp_type) {
10718fae3551SRodney W. Grimes 	case ICMP_ECHOREPLY:
10728fae3551SRodney W. Grimes 		(void)printf("Echo Reply\n");
10738fae3551SRodney W. Grimes 		/* XXX ID + Seq + Data */
10748fae3551SRodney W. Grimes 		break;
10758fae3551SRodney W. Grimes 	case ICMP_UNREACH:
10768fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
10778fae3551SRodney W. Grimes 		case ICMP_UNREACH_NET:
10788fae3551SRodney W. Grimes 			(void)printf("Destination Net Unreachable\n");
10798fae3551SRodney W. Grimes 			break;
10808fae3551SRodney W. Grimes 		case ICMP_UNREACH_HOST:
10818fae3551SRodney W. Grimes 			(void)printf("Destination Host Unreachable\n");
10828fae3551SRodney W. Grimes 			break;
10838fae3551SRodney W. Grimes 		case ICMP_UNREACH_PROTOCOL:
10848fae3551SRodney W. Grimes 			(void)printf("Destination Protocol Unreachable\n");
10858fae3551SRodney W. Grimes 			break;
10868fae3551SRodney W. Grimes 		case ICMP_UNREACH_PORT:
10878fae3551SRodney W. Grimes 			(void)printf("Destination Port Unreachable\n");
10888fae3551SRodney W. Grimes 			break;
10898fae3551SRodney W. Grimes 		case ICMP_UNREACH_NEEDFRAG:
1090ef9e6dc7SBill Fenner 			(void)printf("frag needed and DF set (MTU %d)\n",
1091ff49597eSBill Fenner 					ntohs(icp->icmp_nextmtu));
10928fae3551SRodney W. Grimes 			break;
10938fae3551SRodney W. Grimes 		case ICMP_UNREACH_SRCFAIL:
10948fae3551SRodney W. Grimes 			(void)printf("Source Route Failed\n");
10958fae3551SRodney W. Grimes 			break;
1096ef9e6dc7SBill Fenner 		case ICMP_UNREACH_FILTER_PROHIB:
1097ef9e6dc7SBill Fenner 			(void)printf("Communication prohibited by filter\n");
1098ef9e6dc7SBill Fenner 			break;
10998fae3551SRodney W. Grimes 		default:
11008fae3551SRodney W. Grimes 			(void)printf("Dest Unreachable, Bad Code: %d\n",
11018fae3551SRodney W. Grimes 			    icp->icmp_code);
11028fae3551SRodney W. Grimes 			break;
11038fae3551SRodney W. Grimes 		}
11048fae3551SRodney W. Grimes 		/* Print returned IP header information */
11058fae3551SRodney W. Grimes #ifndef icmp_data
11068fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
11078fae3551SRodney W. Grimes #else
11088fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
11098fae3551SRodney W. Grimes #endif
11108fae3551SRodney W. Grimes 		break;
11118fae3551SRodney W. Grimes 	case ICMP_SOURCEQUENCH:
11128fae3551SRodney W. Grimes 		(void)printf("Source Quench\n");
11138fae3551SRodney W. Grimes #ifndef icmp_data
11148fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
11158fae3551SRodney W. Grimes #else
11168fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
11178fae3551SRodney W. Grimes #endif
11188fae3551SRodney W. Grimes 		break;
11198fae3551SRodney W. Grimes 	case ICMP_REDIRECT:
11208fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
11218fae3551SRodney W. Grimes 		case ICMP_REDIRECT_NET:
11228fae3551SRodney W. Grimes 			(void)printf("Redirect Network");
11238fae3551SRodney W. Grimes 			break;
11248fae3551SRodney W. Grimes 		case ICMP_REDIRECT_HOST:
11258fae3551SRodney W. Grimes 			(void)printf("Redirect Host");
11268fae3551SRodney W. Grimes 			break;
11278fae3551SRodney W. Grimes 		case ICMP_REDIRECT_TOSNET:
11288fae3551SRodney W. Grimes 			(void)printf("Redirect Type of Service and Network");
11298fae3551SRodney W. Grimes 			break;
11308fae3551SRodney W. Grimes 		case ICMP_REDIRECT_TOSHOST:
11318fae3551SRodney W. Grimes 			(void)printf("Redirect Type of Service and Host");
11328fae3551SRodney W. Grimes 			break;
11338fae3551SRodney W. Grimes 		default:
11348fae3551SRodney W. Grimes 			(void)printf("Redirect, Bad Code: %d", icp->icmp_code);
11358fae3551SRodney W. Grimes 			break;
11368fae3551SRodney W. Grimes 		}
1137ff49597eSBill Fenner 		(void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr));
11388fae3551SRodney W. Grimes #ifndef icmp_data
11398fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
11408fae3551SRodney W. Grimes #else
11418fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
11428fae3551SRodney W. Grimes #endif
11438fae3551SRodney W. Grimes 		break;
11448fae3551SRodney W. Grimes 	case ICMP_ECHO:
11458fae3551SRodney W. Grimes 		(void)printf("Echo Request\n");
11468fae3551SRodney W. Grimes 		/* XXX ID + Seq + Data */
11478fae3551SRodney W. Grimes 		break;
11488fae3551SRodney W. Grimes 	case ICMP_TIMXCEED:
11498fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
11508fae3551SRodney W. Grimes 		case ICMP_TIMXCEED_INTRANS:
11518fae3551SRodney W. Grimes 			(void)printf("Time to live exceeded\n");
11528fae3551SRodney W. Grimes 			break;
11538fae3551SRodney W. Grimes 		case ICMP_TIMXCEED_REASS:
11548fae3551SRodney W. Grimes 			(void)printf("Frag reassembly time exceeded\n");
11558fae3551SRodney W. Grimes 			break;
11568fae3551SRodney W. Grimes 		default:
11578fae3551SRodney W. Grimes 			(void)printf("Time exceeded, Bad Code: %d\n",
11588fae3551SRodney W. Grimes 			    icp->icmp_code);
11598fae3551SRodney W. Grimes 			break;
11608fae3551SRodney W. Grimes 		}
11618fae3551SRodney W. Grimes #ifndef icmp_data
11628fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
11638fae3551SRodney W. Grimes #else
11648fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
11658fae3551SRodney W. Grimes #endif
11668fae3551SRodney W. Grimes 		break;
11678fae3551SRodney W. Grimes 	case ICMP_PARAMPROB:
11688fae3551SRodney W. Grimes 		(void)printf("Parameter problem: pointer = 0x%02x\n",
11698fae3551SRodney W. Grimes 		    icp->icmp_hun.ih_pptr);
11708fae3551SRodney W. Grimes #ifndef icmp_data
11718fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
11728fae3551SRodney W. Grimes #else
11738fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
11748fae3551SRodney W. Grimes #endif
11758fae3551SRodney W. Grimes 		break;
11768fae3551SRodney W. Grimes 	case ICMP_TSTAMP:
11778fae3551SRodney W. Grimes 		(void)printf("Timestamp\n");
11788fae3551SRodney W. Grimes 		/* XXX ID + Seq + 3 timestamps */
11798fae3551SRodney W. Grimes 		break;
11808fae3551SRodney W. Grimes 	case ICMP_TSTAMPREPLY:
11818fae3551SRodney W. Grimes 		(void)printf("Timestamp Reply\n");
11828fae3551SRodney W. Grimes 		/* XXX ID + Seq + 3 timestamps */
11838fae3551SRodney W. Grimes 		break;
11848fae3551SRodney W. Grimes 	case ICMP_IREQ:
11858fae3551SRodney W. Grimes 		(void)printf("Information Request\n");
11868fae3551SRodney W. Grimes 		/* XXX ID + Seq */
11878fae3551SRodney W. Grimes 		break;
11888fae3551SRodney W. Grimes 	case ICMP_IREQREPLY:
11898fae3551SRodney W. Grimes 		(void)printf("Information Reply\n");
11908fae3551SRodney W. Grimes 		/* XXX ID + Seq */
11918fae3551SRodney W. Grimes 		break;
11928fae3551SRodney W. Grimes 	case ICMP_MASKREQ:
11938fae3551SRodney W. Grimes 		(void)printf("Address Mask Request\n");
11948fae3551SRodney W. Grimes 		break;
11958fae3551SRodney W. Grimes 	case ICMP_MASKREPLY:
11968fae3551SRodney W. Grimes 		(void)printf("Address Mask Reply\n");
11978fae3551SRodney W. Grimes 		break;
1198ef9e6dc7SBill Fenner 	case ICMP_ROUTERADVERT:
1199ef9e6dc7SBill Fenner 		(void)printf("Router Advertisement\n");
1200ef9e6dc7SBill Fenner 		break;
1201ef9e6dc7SBill Fenner 	case ICMP_ROUTERSOLICIT:
1202ef9e6dc7SBill Fenner 		(void)printf("Router Solicitation\n");
1203ef9e6dc7SBill Fenner 		break;
12048fae3551SRodney W. Grimes 	default:
12058fae3551SRodney W. Grimes 		(void)printf("Bad ICMP type: %d\n", icp->icmp_type);
12068fae3551SRodney W. Grimes 	}
12078fae3551SRodney W. Grimes }
12088fae3551SRodney W. Grimes 
12098fae3551SRodney W. Grimes /*
12108fae3551SRodney W. Grimes  * pr_iph --
12118fae3551SRodney W. Grimes  *	Print an IP header with options.
12128fae3551SRodney W. Grimes  */
121343470e3bSGarrett Wollman static void
12148fae3551SRodney W. Grimes pr_iph(ip)
12158fae3551SRodney W. Grimes 	struct ip *ip;
12168fae3551SRodney W. Grimes {
12178fae3551SRodney W. Grimes 	int hlen;
12188fae3551SRodney W. Grimes 	u_char *cp;
12198fae3551SRodney W. Grimes 
12208fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
12218fae3551SRodney W. Grimes 	cp = (u_char *)ip + 20;		/* point to options */
12228fae3551SRodney W. Grimes 
1223ef9e6dc7SBill Fenner 	(void)printf("Vr HL TOS  Len   ID Flg  off TTL Pro  cks      Src      Dst\n");
12248fae3551SRodney W. Grimes 	(void)printf(" %1x  %1x  %02x %04x %04x",
1225ef9e6dc7SBill Fenner 	    ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len),
1226ef9e6dc7SBill Fenner 	    ntohs(ip->ip_id));
1227d32ff037SJohn Birrell 	(void)printf("   %1lx %04lx",
1228d32ff037SJohn Birrell 	    (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13,
1229d32ff037SJohn Birrell 	    (u_long) ntohl(ip->ip_off) & 0x1fff);
1230ef9e6dc7SBill Fenner 	(void)printf("  %02x  %02x %04x", ip->ip_ttl, ip->ip_p,
1231ef9e6dc7SBill Fenner 							    ntohs(ip->ip_sum));
12328fae3551SRodney W. Grimes 	(void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr));
12338fae3551SRodney W. Grimes 	(void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr));
1234ef9e6dc7SBill Fenner 	/* dump any option bytes */
12358fae3551SRodney W. Grimes 	while (hlen-- > 20) {
12368fae3551SRodney W. Grimes 		(void)printf("%02x", *cp++);
12378fae3551SRodney W. Grimes 	}
12388fae3551SRodney W. Grimes 	(void)putchar('\n');
12398fae3551SRodney W. Grimes }
12408fae3551SRodney W. Grimes 
12418fae3551SRodney W. Grimes /*
12428fae3551SRodney W. Grimes  * pr_addr --
12438fae3551SRodney W. Grimes  *	Return an ascii host address as a dotted quad and optionally with
12448fae3551SRodney W. Grimes  * a hostname.
12458fae3551SRodney W. Grimes  */
124643470e3bSGarrett Wollman static char *
124743470e3bSGarrett Wollman pr_addr(ina)
124843470e3bSGarrett Wollman 	struct in_addr ina;
12498fae3551SRodney W. Grimes {
12508fae3551SRodney W. Grimes 	struct hostent *hp;
1251f78ac61bSWarner Losh 	static char buf[16 + 3 + MAXHOSTNAMELEN];
12528fae3551SRodney W. Grimes 
12538fae3551SRodney W. Grimes 	if ((options & F_NUMERIC) ||
125443470e3bSGarrett Wollman 	    !(hp = gethostbyaddr((char *)&ina, 4, AF_INET)))
125543470e3bSGarrett Wollman 		return inet_ntoa(ina);
12568fae3551SRodney W. Grimes 	else
1257efa38539SPeter Wemm 		(void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name,
125843470e3bSGarrett Wollman 		    inet_ntoa(ina));
12598fae3551SRodney W. Grimes 	return(buf);
12608fae3551SRodney W. Grimes }
12618fae3551SRodney W. Grimes 
12628fae3551SRodney W. Grimes /*
12638fae3551SRodney W. Grimes  * pr_retip --
12648fae3551SRodney W. Grimes  *	Dump some info on a returned (via ICMP) IP packet.
12658fae3551SRodney W. Grimes  */
126643470e3bSGarrett Wollman static void
12678fae3551SRodney W. Grimes pr_retip(ip)
12688fae3551SRodney W. Grimes 	struct ip *ip;
12698fae3551SRodney W. Grimes {
12708fae3551SRodney W. Grimes 	int hlen;
12718fae3551SRodney W. Grimes 	u_char *cp;
12728fae3551SRodney W. Grimes 
12738fae3551SRodney W. Grimes 	pr_iph(ip);
12748fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
12758fae3551SRodney W. Grimes 	cp = (u_char *)ip + hlen;
12768fae3551SRodney W. Grimes 
12778fae3551SRodney W. Grimes 	if (ip->ip_p == 6)
12788fae3551SRodney W. Grimes 		(void)printf("TCP: from port %u, to port %u (decimal)\n",
12798fae3551SRodney W. Grimes 		    (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
12808fae3551SRodney W. Grimes 	else if (ip->ip_p == 17)
12818fae3551SRodney W. Grimes 		(void)printf("UDP: from port %u, to port %u (decimal)\n",
12828fae3551SRodney W. Grimes 			(*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
12838fae3551SRodney W. Grimes }
12848fae3551SRodney W. Grimes 
128543470e3bSGarrett Wollman static void
12868fae3551SRodney W. Grimes fill(bp, patp)
12878fae3551SRodney W. Grimes 	char *bp, *patp;
12888fae3551SRodney W. Grimes {
12898fae3551SRodney W. Grimes 	register int ii, jj, kk;
12908fae3551SRodney W. Grimes 	int pat[16];
12918fae3551SRodney W. Grimes 	char *cp;
12928fae3551SRodney W. Grimes 
129343470e3bSGarrett Wollman 	for (cp = patp; *cp; cp++) {
129443470e3bSGarrett Wollman 		if (!isxdigit(*cp))
129543470e3bSGarrett Wollman 			errx(EX_USAGE,
129643470e3bSGarrett Wollman 			     "patterns must be specified as hex digits");
129743470e3bSGarrett Wollman 
12988fae3551SRodney W. Grimes 	}
12998fae3551SRodney W. Grimes 	ii = sscanf(patp,
13008fae3551SRodney W. Grimes 	    "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
13018fae3551SRodney W. Grimes 	    &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
13028fae3551SRodney W. Grimes 	    &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
13038fae3551SRodney W. Grimes 	    &pat[13], &pat[14], &pat[15]);
13048fae3551SRodney W. Grimes 
13058fae3551SRodney W. Grimes 	if (ii > 0)
13068fae3551SRodney W. Grimes 		for (kk = 0;
1307d32ff037SJohn Birrell 		    kk <= MAXPACKET - (8 + PHDR_LEN + ii);
13088fae3551SRodney W. Grimes 		    kk += ii)
13098fae3551SRodney W. Grimes 			for (jj = 0; jj < ii; ++jj)
13108fae3551SRodney W. Grimes 				bp[jj + kk] = pat[jj];
13118fae3551SRodney W. Grimes 	if (!(options & F_QUIET)) {
13128fae3551SRodney W. Grimes 		(void)printf("PATTERN: 0x");
13138fae3551SRodney W. Grimes 		for (jj = 0; jj < ii; ++jj)
13148fae3551SRodney W. Grimes 			(void)printf("%02x", bp[jj] & 0xFF);
13158fae3551SRodney W. Grimes 		(void)printf("\n");
13168fae3551SRodney W. Grimes 	}
13178fae3551SRodney W. Grimes }
13188fae3551SRodney W. Grimes 
131943470e3bSGarrett Wollman static void
1320e345a80dSPhilippe Charnier usage()
13218fae3551SRodney W. Grimes {
132299490edeSWarner Losh 	fprintf(stderr, "%s\n%s\n%s\n",
1323e345a80dSPhilippe Charnier "usage: ping [-QRadfnqrv] [-c count] [-i wait] [-l preload] [-p pattern]",
132499490edeSWarner Losh "            [-s packetsize] [-S src_addr]",
132599490edeSWarner Losh "            [host | [-L] [-I iface] [-T ttl] mcast-group]");
132643470e3bSGarrett Wollman 	exit(EX_USAGE);
13278fae3551SRodney W. Grimes }
1328