xref: /freebsd/sbin/ping/ping.c (revision 9d2b0ab82ad1236efa76d644fcc586d6be680fe1)
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[] =
487f3dea24SPeter Wemm   "$FreeBSD$";
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 
959a4365d0SYoshinobu Inoue #ifdef IPSEC
969a4365d0SYoshinobu Inoue #include <netinet6/ipsec.h>
979a4365d0SYoshinobu Inoue #endif /*IPSEC*/
989a4365d0SYoshinobu Inoue 
99301207dfSMaxim Konovalov #define	INADDR_LEN	((int)sizeof(in_addr_t))
100c6facae4SMaxim Konovalov #define	PHDR_LEN	((int)sizeof(struct timeval))
101d32ff037SJohn Birrell #define	DEFDATALEN	(64 - PHDR_LEN)	/* default data length */
1028f975bb3SBruce Evans #define	FLOOD_BACKOFF	20000		/* usecs to back off if F_FLOOD mode */
1038f975bb3SBruce Evans 					/* runs out of buffer space */
1044fba6582SMaxim Konovalov #define	MAXIPLEN	(sizeof(struct ip) + MAX_IPOPTLEN)
1054fba6582SMaxim Konovalov #define	MAXICMPLEN	(ICMP_ADVLENMIN + MAX_IPOPTLEN)
1064fba6582SMaxim Konovalov #define	MINICMPLEN	ICMP_MINLEN
1074fba6582SMaxim Konovalov #define	MAXPAYLOAD	(IP_MAXPACKET - MAXIPLEN - MINICMPLEN)
1088fae3551SRodney W. Grimes #define	MAXWAIT		10		/* max seconds to wait for response */
109bf113f1bSBill Fumerola #define	MAXALARM	(60 * 60)	/* max seconds for alarm timeout */
1108fae3551SRodney W. Grimes 
1118fae3551SRodney W. Grimes #define	A(bit)		rcvd_tbl[(bit)>>3]	/* identify byte in array */
1128fae3551SRodney W. Grimes #define	B(bit)		(1 << ((bit) & 0x07))	/* identify bit in byte */
1138fae3551SRodney W. Grimes #define	SET(bit)	(A(bit) |= B(bit))
1148fae3551SRodney W. Grimes #define	CLR(bit)	(A(bit) &= (~B(bit)))
1158fae3551SRodney W. Grimes #define	TST(bit)	(A(bit) & B(bit))
1168fae3551SRodney W. Grimes 
1178fae3551SRodney W. Grimes /* various options */
1188fae3551SRodney W. Grimes int options;
11985456935SBill Fenner #define	F_FLOOD		0x0001
12085456935SBill Fenner #define	F_INTERVAL	0x0002
12185456935SBill Fenner #define	F_NUMERIC	0x0004
12285456935SBill Fenner #define	F_PINGFILLED	0x0008
12385456935SBill Fenner #define	F_QUIET		0x0010
12485456935SBill Fenner #define	F_RROUTE	0x0020
12585456935SBill Fenner #define	F_SO_DEBUG	0x0040
12685456935SBill Fenner #define	F_SO_DONTROUTE	0x0080
12785456935SBill Fenner #define	F_VERBOSE	0x0100
12885456935SBill Fenner #define	F_QUIET2	0x0200
12985456935SBill Fenner #define	F_NOLOOP	0x0400
13085456935SBill Fenner #define	F_MTTL		0x0800
13185456935SBill Fenner #define	F_MIF		0x1000
132772dfa72SDaniel O'Callaghan #define	F_AUDIBLE	0x2000
1339a4365d0SYoshinobu Inoue #ifdef IPSEC
1349a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
1359a4365d0SYoshinobu Inoue #define F_POLICY	0x4000
1369a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/
1379a4365d0SYoshinobu Inoue #endif /*IPSEC*/
138211bfbd2SRuslan Ermilov #define	F_TTL		0x8000
139ca517ad8SPoul-Henning Kamp #define	F_MISSED	0x10000
1408fae3551SRodney W. Grimes 
1418fae3551SRodney W. Grimes /*
1428fae3551SRodney W. Grimes  * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
1438fae3551SRodney W. Grimes  * number of received sequence numbers we can keep track of.  Change 128
1448fae3551SRodney W. Grimes  * to 8192 for complete accuracy...
1458fae3551SRodney W. Grimes  */
1468fae3551SRodney W. Grimes #define	MAX_DUP_CHK	(8 * 128)
1478fae3551SRodney W. Grimes int mx_dup_ck = MAX_DUP_CHK;
1488fae3551SRodney W. Grimes char rcvd_tbl[MAX_DUP_CHK / 8];
1498fae3551SRodney W. Grimes 
150d389e86aSMatt Jacob struct sockaddr_in whereto;	/* who to ping */
1518fae3551SRodney W. Grimes int datalen = DEFDATALEN;
1528fae3551SRodney W. Grimes int s;				/* socket file descriptor */
1534fba6582SMaxim Konovalov u_char outpack[MINICMPLEN + MAXPAYLOAD];
1548fae3551SRodney W. Grimes char BSPACE = '\b';		/* characters written for flood */
155ca517ad8SPoul-Henning Kamp char BBELL = '\a';		/* characters written for MISSED and AUDIBLE */
1568fae3551SRodney W. Grimes char DOT = '.';
1578fae3551SRodney W. Grimes char *hostname;
15899490edeSWarner Losh char *shostname;
1598fae3551SRodney W. Grimes int ident;			/* process id to identify our packets */
160ee2bf734SWarner Losh int uid;			/* cached uid for micro-optimization */
1618fae3551SRodney W. Grimes 
1628fae3551SRodney W. Grimes /* counters */
1638fae3551SRodney W. Grimes long npackets;			/* max packets to transmit */
1648fae3551SRodney W. Grimes long nreceived;			/* # of packets we got back */
1658fae3551SRodney W. Grimes long nrepeats;			/* number of duplicates */
1668fae3551SRodney W. Grimes long ntransmitted;		/* sequence # for outbound packets = #sent */
16725107197SIan Dowse long nmissedmax;		/* max value of ntransmitted - nreceived - 1 */
168526f06b2SMatthew Dillon int interval = 1000;		/* interval between packets, ms */
1698fae3551SRodney W. Grimes 
1708fae3551SRodney W. Grimes /* timing */
1718fae3551SRodney W. Grimes int timing;			/* flag to do timing */
1728fae3551SRodney W. Grimes double tmin = 999999999.0;	/* minimum round trip time */
1738fae3551SRodney W. Grimes double tmax = 0.0;		/* maximum round trip time */
1748fae3551SRodney W. Grimes double tsum = 0.0;		/* sum of all times, for doing average */
1753109a910SGarrett Wollman double tsumsq = 0.0;		/* sum of all times squared, for std. dev. */
1768fae3551SRodney W. Grimes 
1778f975bb3SBruce Evans volatile sig_atomic_t finish_up;  /* nonzero if we've been told to finish up */
178badd8138SSean Eric Fagan int reset_kerninfo;
1798f975bb3SBruce Evans volatile sig_atomic_t siginfo_p;
180badd8138SSean Eric Fagan 
18143470e3bSGarrett Wollman static void fill(char *, char *);
18243470e3bSGarrett Wollman static u_short in_cksum(u_short *, int);
18343470e3bSGarrett Wollman static void check_status(void);
1848f975bb3SBruce Evans static void finish(void) __dead2;
18543470e3bSGarrett Wollman static void pinger(void);
18643470e3bSGarrett Wollman static char *pr_addr(struct in_addr);
18743470e3bSGarrett Wollman static void pr_icmph(struct icmp *);
18843470e3bSGarrett Wollman static void pr_iph(struct ip *);
189039d6aa4SBill Fenner static void pr_pack(char *, int, struct sockaddr_in *, struct timeval *);
19043470e3bSGarrett Wollman static void pr_retip(struct ip *);
19143470e3bSGarrett Wollman static void status(int);
1928f975bb3SBruce Evans static void stopit(int);
19343470e3bSGarrett Wollman static void tvsub(struct timeval *, struct timeval *);
194e345a80dSPhilippe Charnier static void usage(void) __dead2;
1958fae3551SRodney W. Grimes 
19643470e3bSGarrett Wollman int
1978fae3551SRodney W. Grimes main(argc, argv)
1988fae3551SRodney W. Grimes 	int argc;
19943470e3bSGarrett Wollman 	char *const *argv;
2008fae3551SRodney W. Grimes {
201efc8588dSDavid E. O'Brien 	struct in_addr ifaddr;
202efc8588dSDavid E. O'Brien 	struct iovec iov;
203efc8588dSDavid E. O'Brien 	struct msghdr msg;
204efc8588dSDavid E. O'Brien 	struct sigaction si_sa;
205efc8588dSDavid E. O'Brien 	struct sockaddr_in from, sin;
206efc8588dSDavid E. O'Brien 	struct termios ts;
207039d6aa4SBill Fenner 	struct timeval last, intvl;
2088fae3551SRodney W. Grimes 	struct hostent *hp;
209efc8588dSDavid E. O'Brien 	struct sockaddr_in *to;
2101ad0b1beSMaxim Konovalov 	double t;
2114fba6582SMaxim Konovalov 	u_char *datap, packet[IP_MAXPACKET];
212efc8588dSDavid E. O'Brien 	char *ep, *source, *target;
213efc8588dSDavid E. O'Brien #ifdef IPSEC_POLICY_IPSEC
214efc8588dSDavid E. O'Brien 	char *policy_in, *policy_out;
215efc8588dSDavid E. O'Brien #endif
216efc8588dSDavid E. O'Brien 	u_long alarmtimeout, ultmp;
217efc8588dSDavid E. O'Brien 	int ch, hold, i, packlen, preload, sockerrno, almost_done = 0, ttl;
218efc8588dSDavid E. O'Brien 	char ctrl[CMSG_SPACE(sizeof(struct timeval))];
219efc8588dSDavid E. O'Brien 	char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN];
2208fae3551SRodney W. Grimes #ifdef IP_OPTIONS
2214fba6582SMaxim Konovalov 	char rspace[MAX_IPOPTLEN];	/* record route space */
2228fae3551SRodney W. Grimes #endif
223efc8588dSDavid E. O'Brien 	unsigned char mttl, loop;
224efc8588dSDavid E. O'Brien 
225efc8588dSDavid E. O'Brien 	source = NULL;
2269a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
227efc8588dSDavid E. O'Brien 	policy_in = policy_out = NULL;
2289a4365d0SYoshinobu Inoue #endif
2298fae3551SRodney W. Grimes 
230f1284d7aSBill Fenner 	/*
231f1284d7aSBill Fenner 	 * Do the stuff that we need root priv's for *first*, and
232f1284d7aSBill Fenner 	 * then drop our setuid bit.  Save error reporting for
233f1284d7aSBill Fenner 	 * after arg parsing.
234f1284d7aSBill Fenner 	 */
23543470e3bSGarrett Wollman 	s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
236f1284d7aSBill Fenner 	sockerrno = errno;
237f1284d7aSBill Fenner 
238f1284d7aSBill Fenner 	setuid(getuid());
239ee2bf734SWarner Losh 	uid = getuid();
240f1284d7aSBill Fenner 
241bf113f1bSBill Fumerola 	alarmtimeout = preload = 0;
242badd8138SSean Eric Fagan 
2434fba6582SMaxim Konovalov 	datap = &outpack[MINICMPLEN + PHDR_LEN];
244211bfbd2SRuslan Ermilov 	while ((ch = getopt(argc, argv,
245ca517ad8SPoul-Henning Kamp 		"AI:LQRS:T:c:adfi:l:m:np:qrs:t:v"
246211bfbd2SRuslan Ermilov #ifdef IPSEC
2479a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
248211bfbd2SRuslan Ermilov 		"P:"
2499a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/
250211bfbd2SRuslan Ermilov #endif /*IPSEC*/
251211bfbd2SRuslan Ermilov 		)) != -1)
2529a4365d0SYoshinobu Inoue 	{
2538fae3551SRodney W. Grimes 		switch(ch) {
254ca517ad8SPoul-Henning Kamp 		case 'A':
255ca517ad8SPoul-Henning Kamp 			options |= F_MISSED;
256ca517ad8SPoul-Henning Kamp 			break;
257772dfa72SDaniel O'Callaghan 		case 'a':
258772dfa72SDaniel O'Callaghan 			options |= F_AUDIBLE;
259772dfa72SDaniel O'Callaghan 			break;
2608fae3551SRodney W. Grimes 		case 'c':
26143470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
26243470e3bSGarrett Wollman 			if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp)
26343470e3bSGarrett Wollman 				errx(EX_USAGE,
26443470e3bSGarrett Wollman 				    "invalid count of packets to transmit: `%s'",
26543470e3bSGarrett Wollman 				    optarg);
26643470e3bSGarrett Wollman 			npackets = ultmp;
2678fae3551SRodney W. Grimes 			break;
2688fae3551SRodney W. Grimes 		case 'd':
2698fae3551SRodney W. Grimes 			options |= F_SO_DEBUG;
2708fae3551SRodney W. Grimes 			break;
2718fae3551SRodney W. Grimes 		case 'f':
272526f06b2SMatthew Dillon 			if (uid) {
27343470e3bSGarrett Wollman 				errno = EPERM;
27443470e3bSGarrett Wollman 				err(EX_NOPERM, "-f flag");
2758fae3551SRodney W. Grimes 			}
2768fae3551SRodney W. Grimes 			options |= F_FLOOD;
2778fae3551SRodney W. Grimes 			setbuf(stdout, (char *)NULL);
2788fae3551SRodney W. Grimes 			break;
2798fae3551SRodney W. Grimes 		case 'i':		/* wait between sending packets */
2801ad0b1beSMaxim Konovalov 			t = strtod(optarg, &ep) * 1000.0;
2811ad0b1beSMaxim Konovalov 			if (*ep || ep == optarg || t > (double)INT_MAX)
2821ad0b1beSMaxim Konovalov 				errx(EX_USAGE, "invalid timing interval: `%s'",
2831ad0b1beSMaxim Konovalov 				    optarg);
2848fae3551SRodney W. Grimes 			options |= F_INTERVAL;
285526f06b2SMatthew Dillon 			interval = (int)t;
286526f06b2SMatthew Dillon 			if (uid && interval < 1000) {
287526f06b2SMatthew Dillon 				errno = EPERM;
288526f06b2SMatthew Dillon 				err(EX_NOPERM, "-i interval too short");
289526f06b2SMatthew Dillon 			}
2908fae3551SRodney W. Grimes 			break;
29185456935SBill Fenner 		case 'I':		/* multicast interface */
29243470e3bSGarrett Wollman 			if (inet_aton(optarg, &ifaddr) == 0)
29343470e3bSGarrett Wollman 				errx(EX_USAGE,
29443470e3bSGarrett Wollman 				    "invalid multicast interface: `%s'",
29543470e3bSGarrett Wollman 				    optarg);
29685456935SBill Fenner 			options |= F_MIF;
29785456935SBill Fenner 			break;
2988fae3551SRodney W. Grimes 		case 'l':
29943470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
30043470e3bSGarrett Wollman 			if (*ep || ep == optarg || ultmp > INT_MAX)
30143470e3bSGarrett Wollman 				errx(EX_USAGE,
30243470e3bSGarrett Wollman 				    "invalid preload value: `%s'", optarg);
3035e2cc0f4SStephen McKay 			if (uid) {
304f78ac61bSWarner Losh 				errno = EPERM;
305f78ac61bSWarner Losh 				err(EX_NOPERM, "-l flag");
306f78ac61bSWarner Losh 			}
30743470e3bSGarrett Wollman 			preload = ultmp;
3088fae3551SRodney W. Grimes 			break;
30985456935SBill Fenner 		case 'L':
31085456935SBill Fenner 			options |= F_NOLOOP;
31185456935SBill Fenner 			loop = 0;
31285456935SBill Fenner 			break;
313211bfbd2SRuslan Ermilov 		case 'm':		/* TTL */
314211bfbd2SRuslan Ermilov 			ultmp = strtoul(optarg, &ep, 0);
315211bfbd2SRuslan Ermilov 			if (*ep || ep == optarg || ultmp > 255)
3161ad0b1beSMaxim Konovalov 				errx(EX_USAGE, "invalid TTL: `%s'", optarg);
317211bfbd2SRuslan Ermilov 			ttl = ultmp;
318211bfbd2SRuslan Ermilov 			options |= F_TTL;
319211bfbd2SRuslan Ermilov 			break;
3208fae3551SRodney W. Grimes 		case 'n':
3218fae3551SRodney W. Grimes 			options |= F_NUMERIC;
3228fae3551SRodney W. Grimes 			break;
3238fae3551SRodney W. Grimes 		case 'p':		/* fill buffer with user pattern */
3248fae3551SRodney W. Grimes 			options |= F_PINGFILLED;
3258fae3551SRodney W. Grimes 			fill((char *)datap, optarg);
3268fae3551SRodney W. Grimes 				break;
327ef9e6dc7SBill Fenner 		case 'Q':
328ef9e6dc7SBill Fenner 			options |= F_QUIET2;
329ef9e6dc7SBill Fenner 			break;
3308fae3551SRodney W. Grimes 		case 'q':
3318fae3551SRodney W. Grimes 			options |= F_QUIET;
3328fae3551SRodney W. Grimes 			break;
3338fae3551SRodney W. Grimes 		case 'R':
3348fae3551SRodney W. Grimes 			options |= F_RROUTE;
3358fae3551SRodney W. Grimes 			break;
3368fae3551SRodney W. Grimes 		case 'r':
3378fae3551SRodney W. Grimes 			options |= F_SO_DONTROUTE;
3388fae3551SRodney W. Grimes 			break;
3398fae3551SRodney W. Grimes 		case 's':		/* size of packet to send */
340526f06b2SMatthew Dillon 			if (uid) {
341526f06b2SMatthew Dillon 				errno = EPERM;
342526f06b2SMatthew Dillon 				err(EX_NOPERM, "-s flag");
343526f06b2SMatthew Dillon 			}
34443470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
3454fba6582SMaxim Konovalov 			if (ultmp > MAXPAYLOAD)
3464fba6582SMaxim Konovalov 				errx(EX_USAGE,
3474fba6582SMaxim Konovalov 				    "packet size too large: %lu > %u",
3484fba6582SMaxim Konovalov 				    ultmp, MAXPAYLOAD);
3494fba6582SMaxim Konovalov 			if (*ep || ep == optarg)
35043470e3bSGarrett Wollman 				errx(EX_USAGE, "invalid packet size: `%s'",
35143470e3bSGarrett Wollman 				    optarg);
35243470e3bSGarrett Wollman 			datalen = ultmp;
3538fae3551SRodney W. Grimes 			break;
35499490edeSWarner Losh 		case 'S':
35599490edeSWarner Losh 			source = optarg;
35699490edeSWarner Losh 			break;
3577237fd94SBill Fumerola 		case 't':
358bf113f1bSBill Fumerola 			alarmtimeout = strtoul(optarg, &ep, 0);
359bf113f1bSBill Fumerola 			if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX))
3607237fd94SBill Fumerola 				errx(EX_USAGE, "invalid timeout: `%s'",
3617237fd94SBill Fumerola 				    optarg);
362bf113f1bSBill Fumerola 			if (alarmtimeout > MAXALARM)
363bf113f1bSBill Fumerola 				errx(EX_USAGE, "invalid timeout: `%s' > %d",
364bf113f1bSBill Fumerola 				    optarg, MAXALARM);
365bf113f1bSBill Fumerola 			alarm((int)alarmtimeout);
3667237fd94SBill Fumerola 			break;
36785456935SBill Fenner 		case 'T':		/* multicast TTL */
36843470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
36943470e3bSGarrett Wollman 			if (*ep || ep == optarg || ultmp > 255)
37043470e3bSGarrett Wollman 				errx(EX_USAGE, "invalid multicast TTL: `%s'",
37143470e3bSGarrett Wollman 				    optarg);
372211bfbd2SRuslan Ermilov 			mttl = ultmp;
37385456935SBill Fenner 			options |= F_MTTL;
37485456935SBill Fenner 			break;
3758fae3551SRodney W. Grimes 		case 'v':
3768fae3551SRodney W. Grimes 			options |= F_VERBOSE;
3778fae3551SRodney W. Grimes 			break;
3789a4365d0SYoshinobu Inoue #ifdef IPSEC
3799a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
3809a4365d0SYoshinobu Inoue 		case 'P':
3819a4365d0SYoshinobu Inoue 			options |= F_POLICY;
3829a4365d0SYoshinobu Inoue 			if (!strncmp("in", optarg, 2))
3839a4365d0SYoshinobu Inoue 				policy_in = strdup(optarg);
3849a4365d0SYoshinobu Inoue 			else if (!strncmp("out", optarg, 3))
3859a4365d0SYoshinobu Inoue 				policy_out = strdup(optarg);
3869a4365d0SYoshinobu Inoue 			else
3879a4365d0SYoshinobu Inoue 				errx(1, "invalid security policy");
3889a4365d0SYoshinobu Inoue 			break;
3899a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/
3909a4365d0SYoshinobu Inoue #endif /*IPSEC*/
3918fae3551SRodney W. Grimes 		default:
392e345a80dSPhilippe Charnier 			usage();
39343470e3bSGarrett Wollman 		}
39443470e3bSGarrett Wollman 	}
39543470e3bSGarrett Wollman 
39643470e3bSGarrett Wollman 	if (argc - optind != 1)
397e345a80dSPhilippe Charnier 		usage();
39843470e3bSGarrett Wollman 	target = argv[optind];
3998fae3551SRodney W. Grimes 
40099490edeSWarner Losh 	if (source) {
40199490edeSWarner Losh 		bzero((char *)&sin, sizeof(sin));
40299490edeSWarner Losh 		sin.sin_family = AF_INET;
40399490edeSWarner Losh 		if (inet_aton(source, &sin.sin_addr) != 0) {
40499490edeSWarner Losh 			shostname = source;
40599490edeSWarner Losh 		} else {
40699490edeSWarner Losh 			hp = gethostbyname2(source, AF_INET);
40799490edeSWarner Losh 			if (!hp)
40899490edeSWarner Losh 				errx(EX_NOHOST, "cannot resolve %s: %s",
40999490edeSWarner Losh 				    source, hstrerror(h_errno));
41099490edeSWarner Losh 
41199490edeSWarner Losh 			sin.sin_len = sizeof sin;
4124fba6582SMaxim Konovalov 			if (hp->h_length > sizeof(sin.sin_addr) ||
4134fba6582SMaxim Konovalov 			    hp->h_length < 0)
41499490edeSWarner Losh 				errx(1, "gethostbyname2: illegal address");
41599490edeSWarner Losh 			memcpy(&sin.sin_addr, hp->h_addr_list[0],
41699490edeSWarner Losh 			    sizeof(sin.sin_addr));
41799490edeSWarner Losh 			(void)strncpy(snamebuf, hp->h_name,
41899490edeSWarner Losh 			    sizeof(snamebuf) - 1);
41999490edeSWarner Losh 			snamebuf[sizeof(snamebuf) - 1] = '\0';
42099490edeSWarner Losh 			shostname = snamebuf;
42199490edeSWarner Losh 		}
42299490edeSWarner Losh 		if (bind(s, (struct sockaddr *)&sin, sizeof sin) == -1)
42399490edeSWarner Losh 			err(1, "bind");
42499490edeSWarner Losh 	}
42599490edeSWarner Losh 
426d389e86aSMatt Jacob 	bzero(&whereto, sizeof(whereto));
427d389e86aSMatt Jacob 	to = &whereto;
4288fae3551SRodney W. Grimes 	to->sin_family = AF_INET;
429d389e86aSMatt Jacob 	to->sin_len = sizeof *to;
43043470e3bSGarrett Wollman 	if (inet_aton(target, &to->sin_addr) != 0) {
4318fae3551SRodney W. Grimes 		hostname = target;
43243470e3bSGarrett Wollman 	} else {
43343470e3bSGarrett Wollman 		hp = gethostbyname2(target, AF_INET);
43443470e3bSGarrett Wollman 		if (!hp)
43543470e3bSGarrett Wollman 			errx(EX_NOHOST, "cannot resolve %s: %s",
43643470e3bSGarrett Wollman 			    target, hstrerror(h_errno));
43743470e3bSGarrett Wollman 
4381ffae4a6SWarner Losh 		if (hp->h_length > sizeof(to->sin_addr))
4391ffae4a6SWarner Losh 			errx(1, "gethostbyname2 returned an illegal address");
44043470e3bSGarrett Wollman 		memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr);
4418fae3551SRodney W. Grimes 		(void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1);
442b10d9d5fSWarner Losh 		hnamebuf[sizeof(hnamebuf) - 1] = '\0';
4438fae3551SRodney W. Grimes 		hostname = hnamebuf;
4448fae3551SRodney W. Grimes 	}
4458fae3551SRodney W. Grimes 
44643470e3bSGarrett Wollman 	if (options & F_FLOOD && options & F_INTERVAL)
44743470e3bSGarrett Wollman 		errx(EX_USAGE, "-f and -i: incompatible options");
44843470e3bSGarrett Wollman 
44943470e3bSGarrett Wollman 	if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
45043470e3bSGarrett Wollman 		errx(EX_USAGE,
45143470e3bSGarrett Wollman 		    "-f flag cannot be used with multicast destination");
45243470e3bSGarrett Wollman 	if (options & (F_MIF | F_NOLOOP | F_MTTL)
45343470e3bSGarrett Wollman 	    && !IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
45443470e3bSGarrett Wollman 		errx(EX_USAGE,
45543470e3bSGarrett Wollman 		    "-I, -L, -T flags cannot be used with unicast destination");
4568fae3551SRodney W. Grimes 
457d32ff037SJohn Birrell 	if (datalen >= PHDR_LEN)	/* can we time transfer */
4588fae3551SRodney W. Grimes 		timing = 1;
4594fba6582SMaxim Konovalov 	packlen = MAXIPLEN + MAXICMPLEN + datalen;
4604fba6582SMaxim Konovalov 	packlen = packlen > IP_MAXPACKET ? IP_MAXPACKET : packlen;
46143470e3bSGarrett Wollman 
4628fae3551SRodney W. Grimes 	if (!(options & F_PINGFILLED))
463d32ff037SJohn Birrell 		for (i = PHDR_LEN; i < datalen; ++i)
4648fae3551SRodney W. Grimes 			*datap++ = i;
4658fae3551SRodney W. Grimes 
4668fae3551SRodney W. Grimes 	ident = getpid() & 0xFFFF;
4678fae3551SRodney W. Grimes 
468f1284d7aSBill Fenner 	if (s < 0) {
469f1284d7aSBill Fenner 		errno = sockerrno;
47043470e3bSGarrett Wollman 		err(EX_OSERR, "socket");
4718fae3551SRodney W. Grimes 	}
4728fae3551SRodney W. Grimes 	hold = 1;
4738fae3551SRodney W. Grimes 	if (options & F_SO_DEBUG)
4748fae3551SRodney W. Grimes 		(void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
4758fae3551SRodney W. Grimes 		    sizeof(hold));
4768fae3551SRodney W. Grimes 	if (options & F_SO_DONTROUTE)
4778fae3551SRodney W. Grimes 		(void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold,
4788fae3551SRodney W. Grimes 		    sizeof(hold));
4799a4365d0SYoshinobu Inoue #ifdef IPSEC
4809a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
4819a4365d0SYoshinobu Inoue 	if (options & F_POLICY) {
4829a4365d0SYoshinobu Inoue 		char *buf;
4839a4365d0SYoshinobu Inoue 		if (policy_in != NULL) {
4849a4365d0SYoshinobu Inoue 			buf = ipsec_set_policy(policy_in, strlen(policy_in));
4859a4365d0SYoshinobu Inoue 			if (buf == NULL)
486ffd40070SKris Kennaway 				errx(EX_CONFIG, "%s", ipsec_strerror());
4879a4365d0SYoshinobu Inoue 			if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY,
4889a4365d0SYoshinobu Inoue 					buf, ipsec_get_policylen(buf)) < 0)
4891ad0b1beSMaxim Konovalov 				err(EX_CONFIG,
4901ad0b1beSMaxim Konovalov 				    "ipsec policy cannot be configured");
4919a4365d0SYoshinobu Inoue 			free(buf);
4929a4365d0SYoshinobu Inoue 		}
4939a4365d0SYoshinobu Inoue 
4949a4365d0SYoshinobu Inoue 		if (policy_out != NULL) {
4959a4365d0SYoshinobu Inoue 			buf = ipsec_set_policy(policy_out, strlen(policy_out));
4969a4365d0SYoshinobu Inoue 			if (buf == NULL)
497ffd40070SKris Kennaway 				errx(EX_CONFIG, "%s", ipsec_strerror());
4989a4365d0SYoshinobu Inoue 			if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY,
4999a4365d0SYoshinobu Inoue 					buf, ipsec_get_policylen(buf)) < 0)
5001ad0b1beSMaxim Konovalov 				err(EX_CONFIG,
5011ad0b1beSMaxim Konovalov 				    "ipsec policy cannot be configured");
5029a4365d0SYoshinobu Inoue 			free(buf);
5039a4365d0SYoshinobu Inoue 		}
5049a4365d0SYoshinobu Inoue 	}
5059a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/
5069a4365d0SYoshinobu Inoue #endif /*IPSEC*/
5078fae3551SRodney W. Grimes 
5088fae3551SRodney W. Grimes 	/* record route option */
5098fae3551SRodney W. Grimes 	if (options & F_RROUTE) {
5108fae3551SRodney W. Grimes #ifdef IP_OPTIONS
511039d6aa4SBill Fenner 		bzero(rspace, sizeof(rspace));
5128fae3551SRodney W. Grimes 		rspace[IPOPT_OPTVAL] = IPOPT_RR;
5138fae3551SRodney W. Grimes 		rspace[IPOPT_OLEN] = sizeof(rspace) - 1;
5148fae3551SRodney W. Grimes 		rspace[IPOPT_OFFSET] = IPOPT_MINOFF;
515039d6aa4SBill Fenner 		rspace[sizeof(rspace) - 1] = IPOPT_EOL;
5168fae3551SRodney W. Grimes 		if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace,
51743470e3bSGarrett Wollman 		    sizeof(rspace)) < 0)
51843470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_OPTIONS");
5198fae3551SRodney W. Grimes #else
52043470e3bSGarrett Wollman 		errx(EX_UNAVAILABLE,
52143470e3bSGarrett Wollman 		    "record route not available in this implementation");
5228fae3551SRodney W. Grimes #endif /* IP_OPTIONS */
5238fae3551SRodney W. Grimes 	}
5248fae3551SRodney W. Grimes 
525211bfbd2SRuslan Ermilov 	if (options & F_TTL) {
526211bfbd2SRuslan Ermilov 		if (setsockopt(s, IPPROTO_IP, IP_TTL, &ttl,
527211bfbd2SRuslan Ermilov 		    sizeof(ttl)) < 0) {
528211bfbd2SRuslan Ermilov 			err(EX_OSERR, "setsockopt IP_TTL");
529211bfbd2SRuslan Ermilov 		}
530211bfbd2SRuslan Ermilov 	}
53185456935SBill Fenner 	if (options & F_NOLOOP) {
53285456935SBill Fenner 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop,
53385456935SBill Fenner 		    sizeof(loop)) < 0) {
53443470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP");
53585456935SBill Fenner 		}
53685456935SBill Fenner 	}
53785456935SBill Fenner 	if (options & F_MTTL) {
538211bfbd2SRuslan Ermilov 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &mttl,
539211bfbd2SRuslan Ermilov 		    sizeof(mttl)) < 0) {
54043470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_TTL");
54185456935SBill Fenner 		}
54285456935SBill Fenner 	}
54385456935SBill Fenner 	if (options & F_MIF) {
54485456935SBill Fenner 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr,
54585456935SBill Fenner 		    sizeof(ifaddr)) < 0) {
54643470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_IF");
54785456935SBill Fenner 		}
54885456935SBill Fenner 	}
549039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
550039d6aa4SBill Fenner 	{ int on = 1;
551039d6aa4SBill Fenner 	if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) < 0)
552039d6aa4SBill Fenner 		err(EX_OSERR, "setsockopt SO_TIMESTAMP");
553039d6aa4SBill Fenner 	}
554039d6aa4SBill Fenner #endif
55585456935SBill Fenner 
5568fae3551SRodney W. Grimes 	/*
5578fae3551SRodney W. Grimes 	 * When pinging the broadcast address, you can get a lot of answers.
5588fae3551SRodney W. Grimes 	 * Doing something so evil is useful if you are trying to stress the
5598fae3551SRodney W. Grimes 	 * ethernet, or just want to fill the arp cache to get some stuff for
56043470e3bSGarrett Wollman 	 * /etc/ethers.  But beware: RFC 1122 allows hosts to ignore broadcast
56143470e3bSGarrett Wollman 	 * or multicast pings if they wish.
5628fae3551SRodney W. Grimes 	 */
5634fba6582SMaxim Konovalov 
5644fba6582SMaxim Konovalov 	/*
5654fba6582SMaxim Konovalov 	 * XXX receive buffer needs undetermined space for mbuf overhead
5664fba6582SMaxim Konovalov 	 * as well.
5674fba6582SMaxim Konovalov 	 */
5684fba6582SMaxim Konovalov 	hold = IP_MAXPACKET + 128;
5698fae3551SRodney W. Grimes 	(void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
5708fae3551SRodney W. Grimes 	    sizeof(hold));
5718fae3551SRodney W. Grimes 
572e8bd25ceSRobert Watson 	if (!uid) {
573e8bd25ceSRobert Watson 		(void)setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&hold,
574e8bd25ceSRobert Watson 		    sizeof(hold));
575e8bd25ceSRobert Watson 	}
576e8bd25ceSRobert Watson 
57799490edeSWarner Losh 	if (to->sin_family == AF_INET) {
57899490edeSWarner Losh 		(void)printf("PING %s (%s)", hostname,
57999490edeSWarner Losh 		    inet_ntoa(to->sin_addr));
58099490edeSWarner Losh 		if (source)
58199490edeSWarner Losh 			(void)printf(" from %s", shostname);
58299490edeSWarner Losh 		(void)printf(": %d data bytes\n", datalen);
58399490edeSWarner Losh 	} else
5848fae3551SRodney W. Grimes 		(void)printf("PING %s: %d data bytes\n", hostname, datalen);
5858fae3551SRodney W. Grimes 
5867d81b35cSBruce Evans 	/*
587a2a00888SSean Eric Fagan 	 * Use sigaction() instead of signal() to get unambiguous semantics,
588a2a00888SSean Eric Fagan 	 * in particular with SA_RESTART not set.
5897d81b35cSBruce Evans 	 */
590a2a00888SSean Eric Fagan 
591f6bd468bSPaul Traina 	sigemptyset(&si_sa.sa_mask);
59237e5b2c6SSean Eric Fagan 	si_sa.sa_flags = 0;
593a2a00888SSean Eric Fagan 
594a2a00888SSean Eric Fagan 	si_sa.sa_handler = stopit;
595a2a00888SSean Eric Fagan 	if (sigaction(SIGINT, &si_sa, 0) == -1) {
596a2a00888SSean Eric Fagan 		err(EX_OSERR, "sigaction SIGINT");
597a2a00888SSean Eric Fagan 	}
598a2a00888SSean Eric Fagan 
599a2a00888SSean Eric Fagan 	si_sa.sa_handler = status;
60037e5b2c6SSean Eric Fagan 	if (sigaction(SIGINFO, &si_sa, 0) == -1) {
601624ff938SWarner Losh 		err(EX_OSERR, "sigaction");
60237e5b2c6SSean Eric Fagan 	}
603bf113f1bSBill Fumerola 
604bf113f1bSBill Fumerola         if (alarmtimeout > 0) {
6057237fd94SBill Fumerola 		si_sa.sa_handler = stopit;
6067237fd94SBill Fumerola 		if (sigaction(SIGALRM, &si_sa, 0) == -1)
6077237fd94SBill Fumerola 			err(EX_OSERR, "sigaction SIGALRM");
608bf113f1bSBill Fumerola         }
60937e5b2c6SSean Eric Fagan 
610039d6aa4SBill Fenner 	bzero(&msg, sizeof(msg));
611039d6aa4SBill Fenner 	msg.msg_name = (caddr_t)&from;
612039d6aa4SBill Fenner 	msg.msg_iov = &iov;
613039d6aa4SBill Fenner 	msg.msg_iovlen = 1;
614039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
615039d6aa4SBill Fenner 	msg.msg_control = (caddr_t)ctrl;
616039d6aa4SBill Fenner #endif
617039d6aa4SBill Fenner 	iov.iov_base = packet;
618039d6aa4SBill Fenner 	iov.iov_len = packlen;
619039d6aa4SBill Fenner 
6204055611aSSean Eric Fagan 	if (tcgetattr(STDOUT_FILENO, &ts) != -1) {
6214055611aSSean Eric Fagan 		reset_kerninfo = !(ts.c_lflag & NOKERNINFO);
6224055611aSSean Eric Fagan 		ts.c_lflag |= NOKERNINFO;
6234055611aSSean Eric Fagan 		tcsetattr(STDOUT_FILENO, TCSANOW, &ts);
6244055611aSSean Eric Fagan 	}
6254055611aSSean Eric Fagan 
62632af342fSRuslan Ermilov 	if (preload == 0)
62732af342fSRuslan Ermilov 		pinger();		/* send the first ping */
62832af342fSRuslan Ermilov 	else {
62932af342fSRuslan Ermilov 		if (npackets != 0 && preload > npackets)
63032af342fSRuslan Ermilov 			preload = npackets;
6318fae3551SRodney W. Grimes 		while (preload--)	/* fire off them quickies */
6328fae3551SRodney W. Grimes 			pinger();
63332af342fSRuslan Ermilov 	}
63432af342fSRuslan Ermilov 	(void)gettimeofday(&last, NULL);
6358fae3551SRodney W. Grimes 
636039d6aa4SBill Fenner 	if (options & F_FLOOD) {
637039d6aa4SBill Fenner 		intvl.tv_sec = 0;
638039d6aa4SBill Fenner 		intvl.tv_usec = 10000;
639039d6aa4SBill Fenner 	} else {
640526f06b2SMatthew Dillon 		intvl.tv_sec = interval / 1000;
641526f06b2SMatthew Dillon 		intvl.tv_usec = interval % 1000 * 1000;
642039d6aa4SBill Fenner 	}
643039d6aa4SBill Fenner 
6448f975bb3SBruce Evans 	while (!finish_up) {
6453d438ad6SDavid E. O'Brien 		int cc;
646039d6aa4SBill Fenner 		int n;
647039d6aa4SBill Fenner 		struct timeval timeout, now;
648039d6aa4SBill Fenner 		fd_set rfds;
6498fae3551SRodney W. Grimes 
6507d81b35cSBruce Evans 		check_status();
6517e5bbd68SJacques Vidrine 		if (s >= FD_SETSIZE)
6527e5bbd68SJacques Vidrine 			errx(EX_OSERR, "descriptor too large");
653039d6aa4SBill Fenner 		FD_ZERO(&rfds);
654039d6aa4SBill Fenner 		FD_SET(s, &rfds);
655039d6aa4SBill Fenner 		(void)gettimeofday(&now, NULL);
656039d6aa4SBill Fenner 		timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec;
657039d6aa4SBill Fenner 		timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec;
658039d6aa4SBill Fenner 		while (timeout.tv_usec < 0) {
659039d6aa4SBill Fenner 			timeout.tv_usec += 1000000;
660039d6aa4SBill Fenner 			timeout.tv_sec--;
6618fae3551SRodney W. Grimes 		}
662e345a80dSPhilippe Charnier 		while (timeout.tv_usec >= 1000000) {
663039d6aa4SBill Fenner 			timeout.tv_usec -= 1000000;
664039d6aa4SBill Fenner 			timeout.tv_sec++;
665039d6aa4SBill Fenner 		}
666039d6aa4SBill Fenner 		if (timeout.tv_sec < 0)
667039d6aa4SBill Fenner 			timeout.tv_sec = timeout.tv_usec = 0;
668039d6aa4SBill Fenner 		n = select(s + 1, &rfds, NULL, NULL, &timeout);
6695e2cc0f4SStephen McKay 		if (n < 0)
6705e2cc0f4SStephen McKay 			continue;	/* Must be EINTR. */
671039d6aa4SBill Fenner 		if (n == 1) {
672039d6aa4SBill Fenner 			struct timeval *t = 0;
673039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
674039d6aa4SBill Fenner 			struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl;
675039d6aa4SBill Fenner 
676039d6aa4SBill Fenner 			msg.msg_controllen = sizeof(ctrl);
677039d6aa4SBill Fenner #endif
678039d6aa4SBill Fenner 			msg.msg_namelen = sizeof(from);
679039d6aa4SBill Fenner 			if ((cc = recvmsg(s, &msg, 0)) < 0) {
6808fae3551SRodney W. Grimes 				if (errno == EINTR)
6818fae3551SRodney W. Grimes 					continue;
682e345a80dSPhilippe Charnier 				warn("recvmsg");
6838fae3551SRodney W. Grimes 				continue;
6848fae3551SRodney W. Grimes 			}
685039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
686039d6aa4SBill Fenner 			if (cmsg->cmsg_level == SOL_SOCKET &&
687039d6aa4SBill Fenner 			    cmsg->cmsg_type == SCM_TIMESTAMP &&
6886ecbec77SYoshinobu Inoue 			    cmsg->cmsg_len == CMSG_LEN(sizeof *t)) {
689fa05a94cSJohn Birrell 				/* Copy to avoid alignment problems: */
690fa05a94cSJohn Birrell 				memcpy(&now,CMSG_DATA(cmsg),sizeof(now));
691fa05a94cSJohn Birrell 				t = &now;
692fa05a94cSJohn Birrell 			}
693039d6aa4SBill Fenner #endif
694039d6aa4SBill Fenner 			if (t == 0) {
695039d6aa4SBill Fenner 				(void)gettimeofday(&now, NULL);
696039d6aa4SBill Fenner 				t = &now;
697039d6aa4SBill Fenner 			}
698039d6aa4SBill Fenner 			pr_pack((char *)packet, cc, &from, t);
6998fae3551SRodney W. Grimes 			if (npackets && nreceived >= npackets)
7008fae3551SRodney W. Grimes 				break;
7018fae3551SRodney W. Grimes 		}
7025e2cc0f4SStephen McKay 		if (n == 0 || options & F_FLOOD) {
703039d6aa4SBill Fenner 			if (!npackets || ntransmitted < npackets)
704039d6aa4SBill Fenner 				pinger();
705039d6aa4SBill Fenner 			else {
706039d6aa4SBill Fenner 				if (almost_done)
707039d6aa4SBill Fenner 					break;
708039d6aa4SBill Fenner 				almost_done = 1;
7095e2cc0f4SStephen McKay 				intvl.tv_usec = 0;
710039d6aa4SBill Fenner 				if (nreceived) {
711039d6aa4SBill Fenner 					intvl.tv_sec = 2 * tmax / 1000;
712039d6aa4SBill Fenner 					if (!intvl.tv_sec)
713039d6aa4SBill Fenner 						intvl.tv_sec = 1;
714039d6aa4SBill Fenner 				} else
715039d6aa4SBill Fenner 					intvl.tv_sec = MAXWAIT;
716039d6aa4SBill Fenner 			}
717039d6aa4SBill Fenner 			(void)gettimeofday(&last, NULL);
718ca517ad8SPoul-Henning Kamp 
71925107197SIan Dowse 			if (ntransmitted - nreceived - 1 > nmissedmax) {
72025107197SIan Dowse 				nmissedmax = ntransmitted - nreceived - 1;
72125107197SIan Dowse 				if (options & F_MISSED)
722ca517ad8SPoul-Henning Kamp 					(void)write(STDOUT_FILENO, &BBELL, 1);
723039d6aa4SBill Fenner 			}
724039d6aa4SBill Fenner 		}
72525107197SIan Dowse 	}
7268f975bb3SBruce Evans 	finish();
7278fae3551SRodney W. Grimes 	/* NOTREACHED */
728f78ac61bSWarner Losh 	exit(0);	/* Make the compiler happy */
7298fae3551SRodney W. Grimes }
7308fae3551SRodney W. Grimes 
7318fae3551SRodney W. Grimes /*
7328f975bb3SBruce Evans  * stopit --
7338f975bb3SBruce Evans  *	Set the global bit that causes the main loop to quit.
7348f975bb3SBruce Evans  * Do NOT call finish() from here, since finish() does far too much
7358f975bb3SBruce Evans  * to be called from a signal handler.
736515dd2faSJulian Elischer  */
737515dd2faSJulian Elischer void
7388f975bb3SBruce Evans stopit(sig)
739c6facae4SMaxim Konovalov 	int sig __unused;
740515dd2faSJulian Elischer {
741efc8588dSDavid E. O'Brien 
742515dd2faSJulian Elischer 	finish_up = 1;
743515dd2faSJulian Elischer }
744515dd2faSJulian Elischer 
745515dd2faSJulian Elischer /*
7468fae3551SRodney W. Grimes  * pinger --
7478fae3551SRodney W. Grimes  *	Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
7488fae3551SRodney W. Grimes  * will be added on by the kernel.  The ID field is our UNIX process ID,
7494fba6582SMaxim Konovalov  * and the sequence number is an ascending integer.  The first PHDR_LEN
7504fba6582SMaxim Konovalov  * bytes of the data portion are used to hold a UNIX "timeval" struct in
7514fba6582SMaxim Konovalov  * host byte-order, to compute the round-trip time.
7528fae3551SRodney W. Grimes  */
75343470e3bSGarrett Wollman static void
75443470e3bSGarrett Wollman pinger(void)
7558fae3551SRodney W. Grimes {
7563d438ad6SDavid E. O'Brien 	struct icmp *icp;
757efc8588dSDavid E. O'Brien 	int cc, i;
7588fae3551SRodney W. Grimes 
7598fae3551SRodney W. Grimes 	icp = (struct icmp *)outpack;
7608fae3551SRodney W. Grimes 	icp->icmp_type = ICMP_ECHO;
7618fae3551SRodney W. Grimes 	icp->icmp_code = 0;
7628fae3551SRodney W. Grimes 	icp->icmp_cksum = 0;
7635db89bc7SBill Fenner 	icp->icmp_seq = htons(ntransmitted);
7648fae3551SRodney W. Grimes 	icp->icmp_id = ident;			/* ID */
7658fae3551SRodney W. Grimes 
7665db89bc7SBill Fenner 	CLR(ntransmitted % mx_dup_ck);
7678fae3551SRodney W. Grimes 
7688fae3551SRodney W. Grimes 	if (timing)
7694fba6582SMaxim Konovalov 		(void)gettimeofday((struct timeval *)&outpack[MINICMPLEN],
7708fae3551SRodney W. Grimes 		    (struct timezone *)NULL);
7718fae3551SRodney W. Grimes 
7724fba6582SMaxim Konovalov 	cc = MINICMPLEN + datalen;
7738fae3551SRodney W. Grimes 
7748fae3551SRodney W. Grimes 	/* compute ICMP checksum here */
7758fae3551SRodney W. Grimes 	icp->icmp_cksum = in_cksum((u_short *)icp, cc);
7768fae3551SRodney W. Grimes 
777d389e86aSMatt Jacob 	i = sendto(s, (char *)outpack, cc, 0, (struct sockaddr *)&whereto,
778d389e86aSMatt Jacob 	    sizeof(whereto));
7798fae3551SRodney W. Grimes 
7808fae3551SRodney W. Grimes 	if (i < 0 || i != cc)  {
78143470e3bSGarrett Wollman 		if (i < 0) {
7828f975bb3SBruce Evans 			if (options & F_FLOOD && errno == ENOBUFS) {
783515dd2faSJulian Elischer 				usleep(FLOOD_BACKOFF);
784515dd2faSJulian Elischer 				return;
785515dd2faSJulian Elischer 			}
78643470e3bSGarrett Wollman 			warn("sendto");
78743470e3bSGarrett Wollman 		} else {
78843470e3bSGarrett Wollman 			warn("%s: partial write: %d of %d bytes",
789416aa49bSPoul-Henning Kamp 			     hostname, i, cc);
7908fae3551SRodney W. Grimes 		}
791363d7bbeSJulian Elischer 	}
792363d7bbeSJulian Elischer 	ntransmitted++;
7938fae3551SRodney W. Grimes 	if (!(options & F_QUIET) && options & F_FLOOD)
7948fae3551SRodney W. Grimes 		(void)write(STDOUT_FILENO, &DOT, 1);
7958fae3551SRodney W. Grimes }
7968fae3551SRodney W. Grimes 
7978fae3551SRodney W. Grimes /*
7988fae3551SRodney W. Grimes  * pr_pack --
7998fae3551SRodney W. Grimes  *	Print out the packet, if it came from us.  This logic is necessary
8008fae3551SRodney W. Grimes  * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
8018fae3551SRodney W. Grimes  * which arrive ('tis only fair).  This permits multiple copies of this
8028fae3551SRodney W. Grimes  * program to be run without having intermingled output (or statistics!).
8038fae3551SRodney W. Grimes  */
80443470e3bSGarrett Wollman static void
805039d6aa4SBill Fenner pr_pack(buf, cc, from, tv)
8068fae3551SRodney W. Grimes 	char *buf;
8078fae3551SRodney W. Grimes 	int cc;
8088fae3551SRodney W. Grimes 	struct sockaddr_in *from;
809039d6aa4SBill Fenner 	struct timeval *tv;
8108fae3551SRodney W. Grimes {
8113d438ad6SDavid E. O'Brien 	struct icmp *icp;
8128fae3551SRodney W. Grimes 	struct ip *ip;
813301207dfSMaxim Konovalov 	struct in_addr ina;
8149d2b0ab8SPeter Wemm 	const void *tp;
815efc8588dSDavid E. O'Brien 	u_char *cp, *dp;
8168f975bb3SBruce Evans 	double triptime;
817efc8588dSDavid E. O'Brien 	int dupflag, hlen, i, j, seq;
818efc8588dSDavid E. O'Brien 	static int old_rrlen;
819efc8588dSDavid E. O'Brien 	static char old_rr[MAX_IPOPTLEN];
8208fae3551SRodney W. Grimes 
8218fae3551SRodney W. Grimes 	/* Check the IP header */
8228fae3551SRodney W. Grimes 	ip = (struct ip *)buf;
8238fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
8248fae3551SRodney W. Grimes 	if (cc < hlen + ICMP_MINLEN) {
8258fae3551SRodney W. Grimes 		if (options & F_VERBOSE)
82643470e3bSGarrett Wollman 			warn("packet too short (%d bytes) from %s", cc,
82743470e3bSGarrett Wollman 			     inet_ntoa(from->sin_addr));
8288fae3551SRodney W. Grimes 		return;
8298fae3551SRodney W. Grimes 	}
8308fae3551SRodney W. Grimes 
8318fae3551SRodney W. Grimes 	/* Now the ICMP part */
8328fae3551SRodney W. Grimes 	cc -= hlen;
8338fae3551SRodney W. Grimes 	icp = (struct icmp *)(buf + hlen);
8348fae3551SRodney W. Grimes 	if (icp->icmp_type == ICMP_ECHOREPLY) {
8358fae3551SRodney W. Grimes 		if (icp->icmp_id != ident)
8368fae3551SRodney W. Grimes 			return;			/* 'Twas not our ECHO */
8378fae3551SRodney W. Grimes 		++nreceived;
8388f975bb3SBruce Evans 		triptime = 0.0;
8398fae3551SRodney W. Grimes 		if (timing) {
840d32ff037SJohn Birrell 			struct timeval tv1;
8418fae3551SRodney W. Grimes #ifndef icmp_data
8429d2b0ab8SPeter Wemm 			tp = &icp->icmp_ip;
8438fae3551SRodney W. Grimes #else
8449d2b0ab8SPeter Wemm 			tp = icp->icmp_data;
8458fae3551SRodney W. Grimes #endif
8469d2b0ab8SPeter Wemm 			/* Copy to avoid alignment problems: */
8479d2b0ab8SPeter Wemm 			memcpy(&tv1, tp, sizeof(tv1));
848039d6aa4SBill Fenner 			tvsub(tv, &tv1);
849039d6aa4SBill Fenner  			triptime = ((double)tv->tv_sec) * 1000.0 +
850039d6aa4SBill Fenner  			    ((double)tv->tv_usec) / 1000.0;
8518fae3551SRodney W. Grimes 			tsum += triptime;
8523109a910SGarrett Wollman 			tsumsq += triptime * triptime;
8538fae3551SRodney W. Grimes 			if (triptime < tmin)
8548fae3551SRodney W. Grimes 				tmin = triptime;
8558fae3551SRodney W. Grimes 			if (triptime > tmax)
8568fae3551SRodney W. Grimes 				tmax = triptime;
8578fae3551SRodney W. Grimes 		}
8588fae3551SRodney W. Grimes 
8595db89bc7SBill Fenner 		seq = ntohs(icp->icmp_seq);
8605db89bc7SBill Fenner 
8615db89bc7SBill Fenner 		if (TST(seq % mx_dup_ck)) {
8628fae3551SRodney W. Grimes 			++nrepeats;
8638fae3551SRodney W. Grimes 			--nreceived;
8648fae3551SRodney W. Grimes 			dupflag = 1;
8658fae3551SRodney W. Grimes 		} else {
8665db89bc7SBill Fenner 			SET(seq % mx_dup_ck);
8678fae3551SRodney W. Grimes 			dupflag = 0;
8688fae3551SRodney W. Grimes 		}
8698fae3551SRodney W. Grimes 
8708fae3551SRodney W. Grimes 		if (options & F_QUIET)
8718fae3551SRodney W. Grimes 			return;
8728fae3551SRodney W. Grimes 
8738fae3551SRodney W. Grimes 		if (options & F_FLOOD)
8748fae3551SRodney W. Grimes 			(void)write(STDOUT_FILENO, &BSPACE, 1);
8758fae3551SRodney W. Grimes 		else {
8768fae3551SRodney W. Grimes 			(void)printf("%d bytes from %s: icmp_seq=%u", cc,
8778fae3551SRodney W. Grimes 			   inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr),
8785db89bc7SBill Fenner 			   seq);
8798fae3551SRodney W. Grimes 			(void)printf(" ttl=%d", ip->ip_ttl);
8808fae3551SRodney W. Grimes 			if (timing)
881d410b6f1SDavid Greenman 				(void)printf(" time=%.3f ms", triptime);
8828fae3551SRodney W. Grimes 			if (dupflag)
8838fae3551SRodney W. Grimes 				(void)printf(" (DUP!)");
884772dfa72SDaniel O'Callaghan 			if (options & F_AUDIBLE)
885ca517ad8SPoul-Henning Kamp 				(void)write(STDOUT_FILENO, &BBELL, 1);
8868fae3551SRodney W. Grimes 			/* check the data */
887d32ff037SJohn Birrell 			cp = (u_char*)&icp->icmp_data[PHDR_LEN];
8884fba6582SMaxim Konovalov 			dp = &outpack[MINICMPLEN + PHDR_LEN];
889d32ff037SJohn Birrell 			for (i = PHDR_LEN; i < datalen; ++i, ++cp, ++dp) {
8908fae3551SRodney W. Grimes 				if (*cp != *dp) {
8918fae3551SRodney W. Grimes 	(void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x",
8928fae3551SRodney W. Grimes 	    i, *dp, *cp);
8931ad0b1beSMaxim Konovalov 					(void)printf("\ncp:");
8948fae3551SRodney W. Grimes 					cp = (u_char*)&icp->icmp_data[0];
895d32ff037SJohn Birrell 					for (i = 0; i < datalen; ++i, ++cp) {
896d32ff037SJohn Birrell 						if ((i % 32) == 8)
897d32ff037SJohn Birrell 							(void)printf("\n\t");
898d32ff037SJohn Birrell 						(void)printf("%x ", *cp);
899d32ff037SJohn Birrell 					}
9001ad0b1beSMaxim Konovalov 					(void)printf("\ndp:");
9014fba6582SMaxim Konovalov 					cp = &outpack[MINICMPLEN];
902d32ff037SJohn Birrell 					for (i = 0; i < datalen; ++i, ++cp) {
9038fae3551SRodney W. Grimes 						if ((i % 32) == 8)
9048fae3551SRodney W. Grimes 							(void)printf("\n\t");
9058fae3551SRodney W. Grimes 						(void)printf("%x ", *cp);
9068fae3551SRodney W. Grimes 					}
9078fae3551SRodney W. Grimes 					break;
9088fae3551SRodney W. Grimes 				}
9098fae3551SRodney W. Grimes 			}
9108fae3551SRodney W. Grimes 		}
9118fae3551SRodney W. Grimes 	} else {
912ef9e6dc7SBill Fenner 		/*
913ef9e6dc7SBill Fenner 		 * We've got something other than an ECHOREPLY.
914ef9e6dc7SBill Fenner 		 * See if it's a reply to something that we sent.
915ef9e6dc7SBill Fenner 		 * We can compare IP destination, protocol,
916ef9e6dc7SBill Fenner 		 * and ICMP type and ID.
917f78ac61bSWarner Losh 		 *
918f78ac61bSWarner Losh 		 * Only print all the error messages if we are running
919f78ac61bSWarner Losh 		 * as root to avoid leaking information not normally
920f78ac61bSWarner Losh 		 * available to those not running as root.
921ef9e6dc7SBill Fenner 		 */
922ef9e6dc7SBill Fenner #ifndef icmp_data
923ef9e6dc7SBill Fenner 		struct ip *oip = &icp->icmp_ip;
924ef9e6dc7SBill Fenner #else
925ef9e6dc7SBill Fenner 		struct ip *oip = (struct ip *)icp->icmp_data;
926ef9e6dc7SBill Fenner #endif
927ef9e6dc7SBill Fenner 		struct icmp *oicmp = (struct icmp *)(oip + 1);
928ef9e6dc7SBill Fenner 
929ee2bf734SWarner Losh 		if (((options & F_VERBOSE) && uid == 0) ||
930ef9e6dc7SBill Fenner 		    (!(options & F_QUIET2) &&
931d389e86aSMatt Jacob 		     (oip->ip_dst.s_addr == whereto.sin_addr.s_addr) &&
932ef9e6dc7SBill Fenner 		     (oip->ip_p == IPPROTO_ICMP) &&
933ef9e6dc7SBill Fenner 		     (oicmp->icmp_type == ICMP_ECHO) &&
934ef9e6dc7SBill Fenner 		     (oicmp->icmp_id == ident))) {
9358fae3551SRodney W. Grimes 		    (void)printf("%d bytes from %s: ", cc,
93643470e3bSGarrett Wollman 			pr_addr(from->sin_addr));
9378fae3551SRodney W. Grimes 		    pr_icmph(icp);
938ef9e6dc7SBill Fenner 		} else
939ef9e6dc7SBill Fenner 		    return;
9408fae3551SRodney W. Grimes 	}
9418fae3551SRodney W. Grimes 
9428fae3551SRodney W. Grimes 	/* Display any IP options */
9438fae3551SRodney W. Grimes 	cp = (u_char *)buf + sizeof(struct ip);
9448fae3551SRodney W. Grimes 
9458fae3551SRodney W. Grimes 	for (; hlen > (int)sizeof(struct ip); --hlen, ++cp)
9468fae3551SRodney W. Grimes 		switch (*cp) {
9478fae3551SRodney W. Grimes 		case IPOPT_EOL:
9488fae3551SRodney W. Grimes 			hlen = 0;
9498fae3551SRodney W. Grimes 			break;
9508fae3551SRodney W. Grimes 		case IPOPT_LSRR:
9518fae3551SRodney W. Grimes 			(void)printf("\nLSRR: ");
952301207dfSMaxim Konovalov 			j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1;
9538fae3551SRodney W. Grimes 			hlen -= 2;
954301207dfSMaxim Konovalov 			cp += 2;
955301207dfSMaxim Konovalov 			if (j >= INADDR_LEN && j <= hlen - INADDR_LEN) {
9568fae3551SRodney W. Grimes 				for (;;) {
957301207dfSMaxim Konovalov 					bcopy(++cp, &ina.s_addr, INADDR_LEN);
958301207dfSMaxim Konovalov 					if (ina.s_addr == 0)
9591ad0b1beSMaxim Konovalov 						(void)printf("\t0.0.0.0");
960301207dfSMaxim Konovalov 					else
9611ad0b1beSMaxim Konovalov 						(void)printf("\t%s",
9621ad0b1beSMaxim Konovalov 						     pr_addr(ina));
963301207dfSMaxim Konovalov 					hlen -= INADDR_LEN;
964301207dfSMaxim Konovalov 					cp += INADDR_LEN - 1;
965301207dfSMaxim Konovalov 					j -= INADDR_LEN;
966301207dfSMaxim Konovalov 					if (j < INADDR_LEN)
9678fae3551SRodney W. Grimes 						break;
9688fae3551SRodney W. Grimes 					(void)putchar('\n');
9698fae3551SRodney W. Grimes 				}
970301207dfSMaxim Konovalov 			} else
971301207dfSMaxim Konovalov 				(void)printf("\t(truncated route)\n");
9728fae3551SRodney W. Grimes 			break;
9738fae3551SRodney W. Grimes 		case IPOPT_RR:
974301207dfSMaxim Konovalov 			j = cp[IPOPT_OLEN];		/* get length */
975301207dfSMaxim Konovalov 			i = cp[IPOPT_OFFSET];		/* and pointer */
9768fae3551SRodney W. Grimes 			hlen -= 2;
977301207dfSMaxim Konovalov 			cp += 2;
9788fae3551SRodney W. Grimes 			if (i > j)
9798fae3551SRodney W. Grimes 				i = j;
980301207dfSMaxim Konovalov 			i = i - IPOPT_MINOFF + 1;
981301207dfSMaxim Konovalov 			if (i < 0 || i > (hlen - (int)sizeof(struct ip))) {
982301207dfSMaxim Konovalov 				old_rrlen = 0;
9838fae3551SRodney W. Grimes 				continue;
984301207dfSMaxim Konovalov 			}
9858fae3551SRodney W. Grimes 			if (i == old_rrlen
9868fae3551SRodney W. Grimes 			    && !bcmp((char *)cp, old_rr, i)
9878fae3551SRodney W. Grimes 			    && !(options & F_FLOOD)) {
9888fae3551SRodney W. Grimes 				(void)printf("\t(same route)");
9898fae3551SRodney W. Grimes 				hlen -= i;
9908fae3551SRodney W. Grimes 				cp += i;
9918fae3551SRodney W. Grimes 				break;
9928fae3551SRodney W. Grimes 			}
9938fae3551SRodney W. Grimes 			old_rrlen = i;
9948fae3551SRodney W. Grimes 			bcopy((char *)cp, old_rr, i);
995c03e877aSWarner Losh 
9968fae3551SRodney W. Grimes 			(void)printf("\nRR: ");
997301207dfSMaxim Konovalov 
998301207dfSMaxim Konovalov 			if (i >= INADDR_LEN &&
999301207dfSMaxim Konovalov 			    i <= hlen - (int)sizeof(struct ip)) {
10008fae3551SRodney W. Grimes 				for (;;) {
1001301207dfSMaxim Konovalov 					bcopy(++cp, &ina.s_addr, INADDR_LEN);
1002301207dfSMaxim Konovalov 					if (ina.s_addr == 0)
10031ad0b1beSMaxim Konovalov 						(void)printf("\t0.0.0.0");
1004301207dfSMaxim Konovalov 					else
1005301207dfSMaxim Konovalov 						(void)printf("\t%s",
1006301207dfSMaxim Konovalov 						     pr_addr(ina));
1007301207dfSMaxim Konovalov 					hlen -= INADDR_LEN;
1008301207dfSMaxim Konovalov 					cp += INADDR_LEN - 1;
1009301207dfSMaxim Konovalov 					i -= INADDR_LEN;
1010301207dfSMaxim Konovalov 					if (i < INADDR_LEN)
10118fae3551SRodney W. Grimes 						break;
10128fae3551SRodney W. Grimes 					(void)putchar('\n');
10138fae3551SRodney W. Grimes 				}
1014301207dfSMaxim Konovalov 			} else
1015301207dfSMaxim Konovalov 				(void)printf("\t(truncated route)");
10168fae3551SRodney W. Grimes 			break;
10178fae3551SRodney W. Grimes 		case IPOPT_NOP:
10188fae3551SRodney W. Grimes 			(void)printf("\nNOP");
10198fae3551SRodney W. Grimes 			break;
10208fae3551SRodney W. Grimes 		default:
10218fae3551SRodney W. Grimes 			(void)printf("\nunknown option %x", *cp);
10228fae3551SRodney W. Grimes 			break;
10238fae3551SRodney W. Grimes 		}
10248fae3551SRodney W. Grimes 	if (!(options & F_FLOOD)) {
10258fae3551SRodney W. Grimes 		(void)putchar('\n');
10268fae3551SRodney W. Grimes 		(void)fflush(stdout);
10278fae3551SRodney W. Grimes 	}
10288fae3551SRodney W. Grimes }
10298fae3551SRodney W. Grimes 
10308fae3551SRodney W. Grimes /*
10318fae3551SRodney W. Grimes  * in_cksum --
10328fae3551SRodney W. Grimes  *	Checksum routine for Internet Protocol family headers (C Version)
10338fae3551SRodney W. Grimes  */
103443470e3bSGarrett Wollman u_short
10358fae3551SRodney W. Grimes in_cksum(addr, len)
10368fae3551SRodney W. Grimes 	u_short *addr;
10378fae3551SRodney W. Grimes 	int len;
10388fae3551SRodney W. Grimes {
1039efc8588dSDavid E. O'Brien 	int nleft, sum;
1040efc8588dSDavid E. O'Brien 	u_short *w;
10413ec12efeSPierre Beyssac 	union {
104209333e78SPierre Beyssac 		u_short	us;
104309333e78SPierre Beyssac 		u_char	uc[2];
104409333e78SPierre Beyssac 	} last;
104509333e78SPierre Beyssac 	u_short answer;
10468fae3551SRodney W. Grimes 
1047efc8588dSDavid E. O'Brien 	nleft = len;
1048efc8588dSDavid E. O'Brien 	sum = 0;
1049efc8588dSDavid E. O'Brien 	w = addr;
1050efc8588dSDavid E. O'Brien 
10518fae3551SRodney W. Grimes 	/*
10528fae3551SRodney W. Grimes 	 * Our algorithm is simple, using a 32 bit accumulator (sum), we add
10538fae3551SRodney W. Grimes 	 * sequential 16 bit words to it, and at the end, fold back all the
10548fae3551SRodney W. Grimes 	 * carry bits from the top 16 bits into the lower 16 bits.
10558fae3551SRodney W. Grimes 	 */
10568fae3551SRodney W. Grimes 	while (nleft > 1)  {
10578fae3551SRodney W. Grimes 		sum += *w++;
10588fae3551SRodney W. Grimes 		nleft -= 2;
10598fae3551SRodney W. Grimes 	}
10608fae3551SRodney W. Grimes 
10618fae3551SRodney W. Grimes 	/* mop up an odd byte, if necessary */
10628fae3551SRodney W. Grimes 	if (nleft == 1) {
106309333e78SPierre Beyssac 		last.uc[0] = *(u_char *)w;
106409333e78SPierre Beyssac 		last.uc[1] = 0;
106509333e78SPierre Beyssac 		sum += last.us;
10668fae3551SRodney W. Grimes 	}
10678fae3551SRodney W. Grimes 
10688fae3551SRodney W. Grimes 	/* add back carry outs from top 16 bits to low 16 bits */
10698fae3551SRodney W. Grimes 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
10708fae3551SRodney W. Grimes 	sum += (sum >> 16);			/* add carry */
107109333e78SPierre Beyssac 	answer = ~sum;				/* truncate to 16 bits */
107209333e78SPierre Beyssac 	return(answer);
10738fae3551SRodney W. Grimes }
10748fae3551SRodney W. Grimes 
10758fae3551SRodney W. Grimes /*
10768fae3551SRodney W. Grimes  * tvsub --
10778fae3551SRodney W. Grimes  *	Subtract 2 timeval structs:  out = out - in.  Out is assumed to
10788fae3551SRodney W. Grimes  * be >= in.
10798fae3551SRodney W. Grimes  */
108043470e3bSGarrett Wollman static void
10818fae3551SRodney W. Grimes tvsub(out, in)
10823d438ad6SDavid E. O'Brien 	struct timeval *out, *in;
10838fae3551SRodney W. Grimes {
1084efc8588dSDavid E. O'Brien 
10858fae3551SRodney W. Grimes 	if ((out->tv_usec -= in->tv_usec) < 0) {
10868fae3551SRodney W. Grimes 		--out->tv_sec;
10878fae3551SRodney W. Grimes 		out->tv_usec += 1000000;
10888fae3551SRodney W. Grimes 	}
10898fae3551SRodney W. Grimes 	out->tv_sec -= in->tv_sec;
10908fae3551SRodney W. Grimes }
10918fae3551SRodney W. Grimes 
10928fae3551SRodney W. Grimes /*
1093badd8138SSean Eric Fagan  * status --
1094badd8138SSean Eric Fagan  *	Print out statistics when SIGINFO is received.
1095badd8138SSean Eric Fagan  */
1096badd8138SSean Eric Fagan 
109743470e3bSGarrett Wollman static void
10987d81b35cSBruce Evans status(sig)
1099c6facae4SMaxim Konovalov 	int sig __unused;
11007d81b35cSBruce Evans {
1101efc8588dSDavid E. O'Brien 
110237e5b2c6SSean Eric Fagan 	siginfo_p = 1;
110337e5b2c6SSean Eric Fagan }
110437e5b2c6SSean Eric Fagan 
110543470e3bSGarrett Wollman static void
110637e5b2c6SSean Eric Fagan check_status()
1107badd8138SSean Eric Fagan {
1108efc8588dSDavid E. O'Brien 
110937e5b2c6SSean Eric Fagan 	if (siginfo_p) {
111037e5b2c6SSean Eric Fagan 		siginfo_p = 0;
11117d81b35cSBruce Evans 		(void)fprintf(stderr,
11127d81b35cSBruce Evans 	"\r%ld/%ld packets received (%.0f%%) %.3f min / %.3f avg / %.3f max\n",
11137d81b35cSBruce Evans 		    nreceived, ntransmitted,
11147d81b35cSBruce Evans 		    ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0,
11157d81b35cSBruce Evans 		    nreceived ? tmin : 0.0,
11167d81b35cSBruce Evans 		    nreceived + nrepeats ? tsum / (nreceived + nrepeats) : tsum,
11177d81b35cSBruce Evans 		    tmax);
111837e5b2c6SSean Eric Fagan 	}
1119badd8138SSean Eric Fagan }
1120badd8138SSean Eric Fagan 
1121badd8138SSean Eric Fagan /*
11228fae3551SRodney W. Grimes  * finish --
11238fae3551SRodney W. Grimes  *	Print out statistics, and give up.
11248fae3551SRodney W. Grimes  */
112543470e3bSGarrett Wollman static void
11268f975bb3SBruce Evans finish()
11278fae3551SRodney W. Grimes {
1128efc8588dSDavid E. O'Brien 
1129badd8138SSean Eric Fagan 	struct termios ts;
11308fae3551SRodney W. Grimes 
11318fae3551SRodney W. Grimes 	(void)signal(SIGINT, SIG_IGN);
1132a2a00888SSean Eric Fagan 	(void)signal(SIGALRM, SIG_IGN);
11338fae3551SRodney W. Grimes 	(void)putchar('\n');
11348fae3551SRodney W. Grimes 	(void)fflush(stdout);
11358fae3551SRodney W. Grimes 	(void)printf("--- %s ping statistics ---\n", hostname);
11368fae3551SRodney W. Grimes 	(void)printf("%ld packets transmitted, ", ntransmitted);
11378fae3551SRodney W. Grimes 	(void)printf("%ld packets received, ", nreceived);
11388fae3551SRodney W. Grimes 	if (nrepeats)
11398fae3551SRodney W. Grimes 		(void)printf("+%ld duplicates, ", nrepeats);
1140ebe70c8fSWarner Losh 	if (ntransmitted) {
11418fae3551SRodney W. Grimes 		if (nreceived > ntransmitted)
11428fae3551SRodney W. Grimes 			(void)printf("-- somebody's printing up packets!");
11438fae3551SRodney W. Grimes 		else
11448fae3551SRodney W. Grimes 			(void)printf("%d%% packet loss",
11458fae3551SRodney W. Grimes 			    (int)(((ntransmitted - nreceived) * 100) /
11468fae3551SRodney W. Grimes 			    ntransmitted));
1147ebe70c8fSWarner Losh 	}
11488fae3551SRodney W. Grimes 	(void)putchar('\n');
11493109a910SGarrett Wollman 	if (nreceived && timing) {
11503109a910SGarrett Wollman 		double n = nreceived + nrepeats;
11513109a910SGarrett Wollman 		double avg = tsum / n;
11523109a910SGarrett Wollman 		double vari = tsumsq / n - avg * avg;
11531ad0b1beSMaxim Konovalov 		(void)printf(
11541ad0b1beSMaxim Konovalov 		    "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n",
11553109a910SGarrett Wollman 		    tmin, avg, tmax, sqrt(vari));
11563109a910SGarrett Wollman 	}
11572ceaae21SBruce Evans 	if (reset_kerninfo && tcgetattr(STDOUT_FILENO, &ts) != -1) {
1158badd8138SSean Eric Fagan 		ts.c_lflag &= ~NOKERNINFO;
11592ceaae21SBruce Evans 		tcsetattr(STDOUT_FILENO, TCSANOW, &ts);
1160badd8138SSean Eric Fagan 	}
1161badd8138SSean Eric Fagan 
11626e1173dcSDavid Greenman 	if (nreceived)
11638fae3551SRodney W. Grimes 		exit(0);
11646e1173dcSDavid Greenman 	else
11656e1173dcSDavid Greenman 		exit(2);
11668fae3551SRodney W. Grimes }
11678fae3551SRodney W. Grimes 
11688fae3551SRodney W. Grimes #ifdef notdef
11698fae3551SRodney W. Grimes static char *ttab[] = {
11708fae3551SRodney W. Grimes 	"Echo Reply",		/* ip + seq + udata */
11718fae3551SRodney W. Grimes 	"Dest Unreachable",	/* net, host, proto, port, frag, sr + IP */
11728fae3551SRodney W. Grimes 	"Source Quench",	/* IP */
11738fae3551SRodney W. Grimes 	"Redirect",		/* redirect type, gateway, + IP  */
11748fae3551SRodney W. Grimes 	"Echo",
11758fae3551SRodney W. Grimes 	"Time Exceeded",	/* transit, frag reassem + IP */
11768fae3551SRodney W. Grimes 	"Parameter Problem",	/* pointer + IP */
11778fae3551SRodney W. Grimes 	"Timestamp",		/* id + seq + three timestamps */
11788fae3551SRodney W. Grimes 	"Timestamp Reply",	/* " */
11798fae3551SRodney W. Grimes 	"Info Request",		/* id + sq */
11808fae3551SRodney W. Grimes 	"Info Reply"		/* " */
11818fae3551SRodney W. Grimes };
11828fae3551SRodney W. Grimes #endif
11838fae3551SRodney W. Grimes 
11848fae3551SRodney W. Grimes /*
11858fae3551SRodney W. Grimes  * pr_icmph --
11868fae3551SRodney W. Grimes  *	Print a descriptive string about an ICMP header.
11878fae3551SRodney W. Grimes  */
118843470e3bSGarrett Wollman static void
11898fae3551SRodney W. Grimes pr_icmph(icp)
11908fae3551SRodney W. Grimes 	struct icmp *icp;
11918fae3551SRodney W. Grimes {
1192efc8588dSDavid E. O'Brien 
11938fae3551SRodney W. Grimes 	switch(icp->icmp_type) {
11948fae3551SRodney W. Grimes 	case ICMP_ECHOREPLY:
11958fae3551SRodney W. Grimes 		(void)printf("Echo Reply\n");
11968fae3551SRodney W. Grimes 		/* XXX ID + Seq + Data */
11978fae3551SRodney W. Grimes 		break;
11988fae3551SRodney W. Grimes 	case ICMP_UNREACH:
11998fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
12008fae3551SRodney W. Grimes 		case ICMP_UNREACH_NET:
12018fae3551SRodney W. Grimes 			(void)printf("Destination Net Unreachable\n");
12028fae3551SRodney W. Grimes 			break;
12038fae3551SRodney W. Grimes 		case ICMP_UNREACH_HOST:
12048fae3551SRodney W. Grimes 			(void)printf("Destination Host Unreachable\n");
12058fae3551SRodney W. Grimes 			break;
12068fae3551SRodney W. Grimes 		case ICMP_UNREACH_PROTOCOL:
12078fae3551SRodney W. Grimes 			(void)printf("Destination Protocol Unreachable\n");
12088fae3551SRodney W. Grimes 			break;
12098fae3551SRodney W. Grimes 		case ICMP_UNREACH_PORT:
12108fae3551SRodney W. Grimes 			(void)printf("Destination Port Unreachable\n");
12118fae3551SRodney W. Grimes 			break;
12128fae3551SRodney W. Grimes 		case ICMP_UNREACH_NEEDFRAG:
1213ef9e6dc7SBill Fenner 			(void)printf("frag needed and DF set (MTU %d)\n",
1214ff49597eSBill Fenner 					ntohs(icp->icmp_nextmtu));
12158fae3551SRodney W. Grimes 			break;
12168fae3551SRodney W. Grimes 		case ICMP_UNREACH_SRCFAIL:
12178fae3551SRodney W. Grimes 			(void)printf("Source Route Failed\n");
12188fae3551SRodney W. Grimes 			break;
1219ef9e6dc7SBill Fenner 		case ICMP_UNREACH_FILTER_PROHIB:
1220ef9e6dc7SBill Fenner 			(void)printf("Communication prohibited by filter\n");
1221ef9e6dc7SBill Fenner 			break;
12228fae3551SRodney W. Grimes 		default:
12238fae3551SRodney W. Grimes 			(void)printf("Dest Unreachable, Bad Code: %d\n",
12248fae3551SRodney W. Grimes 			    icp->icmp_code);
12258fae3551SRodney W. Grimes 			break;
12268fae3551SRodney W. Grimes 		}
12278fae3551SRodney W. Grimes 		/* Print returned IP header information */
12288fae3551SRodney W. Grimes #ifndef icmp_data
12298fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
12308fae3551SRodney W. Grimes #else
12318fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
12328fae3551SRodney W. Grimes #endif
12338fae3551SRodney W. Grimes 		break;
12348fae3551SRodney W. Grimes 	case ICMP_SOURCEQUENCH:
12358fae3551SRodney W. Grimes 		(void)printf("Source Quench\n");
12368fae3551SRodney W. Grimes #ifndef icmp_data
12378fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
12388fae3551SRodney W. Grimes #else
12398fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
12408fae3551SRodney W. Grimes #endif
12418fae3551SRodney W. Grimes 		break;
12428fae3551SRodney W. Grimes 	case ICMP_REDIRECT:
12438fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
12448fae3551SRodney W. Grimes 		case ICMP_REDIRECT_NET:
12458fae3551SRodney W. Grimes 			(void)printf("Redirect Network");
12468fae3551SRodney W. Grimes 			break;
12478fae3551SRodney W. Grimes 		case ICMP_REDIRECT_HOST:
12488fae3551SRodney W. Grimes 			(void)printf("Redirect Host");
12498fae3551SRodney W. Grimes 			break;
12508fae3551SRodney W. Grimes 		case ICMP_REDIRECT_TOSNET:
12518fae3551SRodney W. Grimes 			(void)printf("Redirect Type of Service and Network");
12528fae3551SRodney W. Grimes 			break;
12538fae3551SRodney W. Grimes 		case ICMP_REDIRECT_TOSHOST:
12548fae3551SRodney W. Grimes 			(void)printf("Redirect Type of Service and Host");
12558fae3551SRodney W. Grimes 			break;
12568fae3551SRodney W. Grimes 		default:
12578fae3551SRodney W. Grimes 			(void)printf("Redirect, Bad Code: %d", icp->icmp_code);
12588fae3551SRodney W. Grimes 			break;
12598fae3551SRodney W. Grimes 		}
1260ff49597eSBill Fenner 		(void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr));
12618fae3551SRodney W. Grimes #ifndef icmp_data
12628fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
12638fae3551SRodney W. Grimes #else
12648fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
12658fae3551SRodney W. Grimes #endif
12668fae3551SRodney W. Grimes 		break;
12678fae3551SRodney W. Grimes 	case ICMP_ECHO:
12688fae3551SRodney W. Grimes 		(void)printf("Echo Request\n");
12698fae3551SRodney W. Grimes 		/* XXX ID + Seq + Data */
12708fae3551SRodney W. Grimes 		break;
12718fae3551SRodney W. Grimes 	case ICMP_TIMXCEED:
12728fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
12738fae3551SRodney W. Grimes 		case ICMP_TIMXCEED_INTRANS:
12748fae3551SRodney W. Grimes 			(void)printf("Time to live exceeded\n");
12758fae3551SRodney W. Grimes 			break;
12768fae3551SRodney W. Grimes 		case ICMP_TIMXCEED_REASS:
12778fae3551SRodney W. Grimes 			(void)printf("Frag reassembly time exceeded\n");
12788fae3551SRodney W. Grimes 			break;
12798fae3551SRodney W. Grimes 		default:
12808fae3551SRodney W. Grimes 			(void)printf("Time exceeded, Bad Code: %d\n",
12818fae3551SRodney W. Grimes 			    icp->icmp_code);
12828fae3551SRodney W. Grimes 			break;
12838fae3551SRodney W. Grimes 		}
12848fae3551SRodney W. Grimes #ifndef icmp_data
12858fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
12868fae3551SRodney W. Grimes #else
12878fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
12888fae3551SRodney W. Grimes #endif
12898fae3551SRodney W. Grimes 		break;
12908fae3551SRodney W. Grimes 	case ICMP_PARAMPROB:
12918fae3551SRodney W. Grimes 		(void)printf("Parameter problem: pointer = 0x%02x\n",
12928fae3551SRodney W. Grimes 		    icp->icmp_hun.ih_pptr);
12938fae3551SRodney W. Grimes #ifndef icmp_data
12948fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
12958fae3551SRodney W. Grimes #else
12968fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
12978fae3551SRodney W. Grimes #endif
12988fae3551SRodney W. Grimes 		break;
12998fae3551SRodney W. Grimes 	case ICMP_TSTAMP:
13008fae3551SRodney W. Grimes 		(void)printf("Timestamp\n");
13018fae3551SRodney W. Grimes 		/* XXX ID + Seq + 3 timestamps */
13028fae3551SRodney W. Grimes 		break;
13038fae3551SRodney W. Grimes 	case ICMP_TSTAMPREPLY:
13048fae3551SRodney W. Grimes 		(void)printf("Timestamp Reply\n");
13058fae3551SRodney W. Grimes 		/* XXX ID + Seq + 3 timestamps */
13068fae3551SRodney W. Grimes 		break;
13078fae3551SRodney W. Grimes 	case ICMP_IREQ:
13088fae3551SRodney W. Grimes 		(void)printf("Information Request\n");
13098fae3551SRodney W. Grimes 		/* XXX ID + Seq */
13108fae3551SRodney W. Grimes 		break;
13118fae3551SRodney W. Grimes 	case ICMP_IREQREPLY:
13128fae3551SRodney W. Grimes 		(void)printf("Information Reply\n");
13138fae3551SRodney W. Grimes 		/* XXX ID + Seq */
13148fae3551SRodney W. Grimes 		break;
13158fae3551SRodney W. Grimes 	case ICMP_MASKREQ:
13168fae3551SRodney W. Grimes 		(void)printf("Address Mask Request\n");
13178fae3551SRodney W. Grimes 		break;
13188fae3551SRodney W. Grimes 	case ICMP_MASKREPLY:
13198fae3551SRodney W. Grimes 		(void)printf("Address Mask Reply\n");
13208fae3551SRodney W. Grimes 		break;
1321ef9e6dc7SBill Fenner 	case ICMP_ROUTERADVERT:
1322ef9e6dc7SBill Fenner 		(void)printf("Router Advertisement\n");
1323ef9e6dc7SBill Fenner 		break;
1324ef9e6dc7SBill Fenner 	case ICMP_ROUTERSOLICIT:
1325ef9e6dc7SBill Fenner 		(void)printf("Router Solicitation\n");
1326ef9e6dc7SBill Fenner 		break;
13278fae3551SRodney W. Grimes 	default:
13288fae3551SRodney W. Grimes 		(void)printf("Bad ICMP type: %d\n", icp->icmp_type);
13298fae3551SRodney W. Grimes 	}
13308fae3551SRodney W. Grimes }
13318fae3551SRodney W. Grimes 
13328fae3551SRodney W. Grimes /*
13338fae3551SRodney W. Grimes  * pr_iph --
13348fae3551SRodney W. Grimes  *	Print an IP header with options.
13358fae3551SRodney W. Grimes  */
133643470e3bSGarrett Wollman static void
13378fae3551SRodney W. Grimes pr_iph(ip)
13388fae3551SRodney W. Grimes 	struct ip *ip;
13398fae3551SRodney W. Grimes {
13408fae3551SRodney W. Grimes 	u_char *cp;
1341efc8588dSDavid E. O'Brien 	int hlen;
13428fae3551SRodney W. Grimes 
13438fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
13448fae3551SRodney W. Grimes 	cp = (u_char *)ip + 20;		/* point to options */
13458fae3551SRodney W. Grimes 
1346ef9e6dc7SBill Fenner 	(void)printf("Vr HL TOS  Len   ID Flg  off TTL Pro  cks      Src      Dst\n");
13478fae3551SRodney W. Grimes 	(void)printf(" %1x  %1x  %02x %04x %04x",
1348ef9e6dc7SBill Fenner 	    ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len),
1349ef9e6dc7SBill Fenner 	    ntohs(ip->ip_id));
1350d32ff037SJohn Birrell 	(void)printf("   %1lx %04lx",
1351d32ff037SJohn Birrell 	    (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13,
1352d32ff037SJohn Birrell 	    (u_long) ntohl(ip->ip_off) & 0x1fff);
1353ef9e6dc7SBill Fenner 	(void)printf("  %02x  %02x %04x", ip->ip_ttl, ip->ip_p,
1354ef9e6dc7SBill Fenner 							    ntohs(ip->ip_sum));
13558fae3551SRodney W. Grimes 	(void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr));
13568fae3551SRodney W. Grimes 	(void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr));
1357ef9e6dc7SBill Fenner 	/* dump any option bytes */
13588fae3551SRodney W. Grimes 	while (hlen-- > 20) {
13598fae3551SRodney W. Grimes 		(void)printf("%02x", *cp++);
13608fae3551SRodney W. Grimes 	}
13618fae3551SRodney W. Grimes 	(void)putchar('\n');
13628fae3551SRodney W. Grimes }
13638fae3551SRodney W. Grimes 
13648fae3551SRodney W. Grimes /*
13658fae3551SRodney W. Grimes  * pr_addr --
13668fae3551SRodney W. Grimes  *	Return an ascii host address as a dotted quad and optionally with
13678fae3551SRodney W. Grimes  * a hostname.
13688fae3551SRodney W. Grimes  */
136943470e3bSGarrett Wollman static char *
137043470e3bSGarrett Wollman pr_addr(ina)
137143470e3bSGarrett Wollman 	struct in_addr ina;
13728fae3551SRodney W. Grimes {
13738fae3551SRodney W. Grimes 	struct hostent *hp;
1374f78ac61bSWarner Losh 	static char buf[16 + 3 + MAXHOSTNAMELEN];
13758fae3551SRodney W. Grimes 
13768fae3551SRodney W. Grimes 	if ((options & F_NUMERIC) ||
137743470e3bSGarrett Wollman 	    !(hp = gethostbyaddr((char *)&ina, 4, AF_INET)))
137843470e3bSGarrett Wollman 		return inet_ntoa(ina);
13798fae3551SRodney W. Grimes 	else
1380efa38539SPeter Wemm 		(void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name,
138143470e3bSGarrett Wollman 		    inet_ntoa(ina));
13828fae3551SRodney W. Grimes 	return(buf);
13838fae3551SRodney W. Grimes }
13848fae3551SRodney W. Grimes 
13858fae3551SRodney W. Grimes /*
13868fae3551SRodney W. Grimes  * pr_retip --
13878fae3551SRodney W. Grimes  *	Dump some info on a returned (via ICMP) IP packet.
13888fae3551SRodney W. Grimes  */
138943470e3bSGarrett Wollman static void
13908fae3551SRodney W. Grimes pr_retip(ip)
13918fae3551SRodney W. Grimes 	struct ip *ip;
13928fae3551SRodney W. Grimes {
13938fae3551SRodney W. Grimes 	u_char *cp;
1394efc8588dSDavid E. O'Brien 	int hlen;
13958fae3551SRodney W. Grimes 
13968fae3551SRodney W. Grimes 	pr_iph(ip);
13978fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
13988fae3551SRodney W. Grimes 	cp = (u_char *)ip + hlen;
13998fae3551SRodney W. Grimes 
14008fae3551SRodney W. Grimes 	if (ip->ip_p == 6)
14018fae3551SRodney W. Grimes 		(void)printf("TCP: from port %u, to port %u (decimal)\n",
14028fae3551SRodney W. Grimes 		    (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
14038fae3551SRodney W. Grimes 	else if (ip->ip_p == 17)
14048fae3551SRodney W. Grimes 		(void)printf("UDP: from port %u, to port %u (decimal)\n",
14058fae3551SRodney W. Grimes 			(*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
14068fae3551SRodney W. Grimes }
14078fae3551SRodney W. Grimes 
140843470e3bSGarrett Wollman static void
14098fae3551SRodney W. Grimes fill(bp, patp)
14108fae3551SRodney W. Grimes 	char *bp, *patp;
14118fae3551SRodney W. Grimes {
14128fae3551SRodney W. Grimes 	char *cp;
1413efc8588dSDavid E. O'Brien 	int pat[16];
14144fba6582SMaxim Konovalov 	u_int ii, jj, kk;
14158fae3551SRodney W. Grimes 
141643470e3bSGarrett Wollman 	for (cp = patp; *cp; cp++) {
141743470e3bSGarrett Wollman 		if (!isxdigit(*cp))
141843470e3bSGarrett Wollman 			errx(EX_USAGE,
141943470e3bSGarrett Wollman 			    "patterns must be specified as hex digits");
142043470e3bSGarrett Wollman 
14218fae3551SRodney W. Grimes 	}
14228fae3551SRodney W. Grimes 	ii = sscanf(patp,
14238fae3551SRodney W. Grimes 	    "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
14248fae3551SRodney W. Grimes 	    &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
14258fae3551SRodney W. Grimes 	    &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
14268fae3551SRodney W. Grimes 	    &pat[13], &pat[14], &pat[15]);
14278fae3551SRodney W. Grimes 
14288fae3551SRodney W. Grimes 	if (ii > 0)
14294fba6582SMaxim Konovalov 		for (kk = 0; kk <= MAXPAYLOAD - (PHDR_LEN + ii); kk += ii)
14308fae3551SRodney W. Grimes 			for (jj = 0; jj < ii; ++jj)
14318fae3551SRodney W. Grimes 				bp[jj + kk] = pat[jj];
14328fae3551SRodney W. Grimes 	if (!(options & F_QUIET)) {
14338fae3551SRodney W. Grimes 		(void)printf("PATTERN: 0x");
14348fae3551SRodney W. Grimes 		for (jj = 0; jj < ii; ++jj)
14358fae3551SRodney W. Grimes 			(void)printf("%02x", bp[jj] & 0xFF);
14368fae3551SRodney W. Grimes 		(void)printf("\n");
14378fae3551SRodney W. Grimes 	}
14388fae3551SRodney W. Grimes }
14398fae3551SRodney W. Grimes 
144043470e3bSGarrett Wollman static void
1441e345a80dSPhilippe Charnier usage()
14428fae3551SRodney W. Grimes {
14431ad0b1beSMaxim Konovalov 	(void)fprintf(stderr, "%s\n%s\n%s\n",
14449c1b8868SIan Dowse "usage: ping [-AQRadfnqrv] [-c count] [-i wait] [-l preload] [-m ttl]",
1445211bfbd2SRuslan Ermilov "            [-p pattern] "
14469a4365d0SYoshinobu Inoue #ifdef IPSEC
14479a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
14489a4365d0SYoshinobu Inoue "[-P policy] "
14499a4365d0SYoshinobu Inoue #endif
14509a4365d0SYoshinobu Inoue #endif
14517237fd94SBill Fumerola "[-s packetsize] [-S src_addr] [-t timeout]",
145299490edeSWarner Losh "            [host | [-L] [-I iface] [-T ttl] mcast-group]");
145343470e3bSGarrett Wollman 	exit(EX_USAGE);
14548fae3551SRodney W. Grimes }
1455