xref: /freebsd/sbin/ping/ping.c (revision 29dccd6ae105f3999712b5defee8b2a08a7e4f5a)
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> */
708fae3551SRodney W. Grimes #include <sys/socket.h>
71261e59bbSMaxim Konovalov #include <sys/sysctl.h>
728fae3551SRodney W. Grimes #include <sys/time.h>
73039d6aa4SBill Fenner #include <sys/uio.h>
748fae3551SRodney W. Grimes 
758fae3551SRodney W. Grimes #include <netinet/in.h>
7643470e3bSGarrett Wollman #include <netinet/in_systm.h>
778fae3551SRodney W. Grimes #include <netinet/ip.h>
788fae3551SRodney W. Grimes #include <netinet/ip_icmp.h>
798fae3551SRodney W. Grimes #include <netinet/ip_var.h>
8043470e3bSGarrett Wollman #include <arpa/inet.h>
818fae3551SRodney W. Grimes 
829a4365d0SYoshinobu Inoue #ifdef IPSEC
839a4365d0SYoshinobu Inoue #include <netinet6/ipsec.h>
849a4365d0SYoshinobu Inoue #endif /*IPSEC*/
859a4365d0SYoshinobu Inoue 
86261e59bbSMaxim Konovalov #include <ctype.h>
87261e59bbSMaxim Konovalov #include <err.h>
88261e59bbSMaxim Konovalov #include <errno.h>
89261e59bbSMaxim Konovalov #include <math.h>
90261e59bbSMaxim Konovalov #include <netdb.h>
91261e59bbSMaxim Konovalov #include <signal.h>
92261e59bbSMaxim Konovalov #include <stdio.h>
93261e59bbSMaxim Konovalov #include <stdlib.h>
94261e59bbSMaxim Konovalov #include <string.h>
95261e59bbSMaxim Konovalov #include <sysexits.h>
96261e59bbSMaxim Konovalov #include <termios.h>
97261e59bbSMaxim Konovalov #include <unistd.h>
98261e59bbSMaxim Konovalov 
99301207dfSMaxim Konovalov #define	INADDR_LEN	((int)sizeof(in_addr_t))
100eb1543c6SMatthew N. Dodd #define	TIMEVAL_LEN	((int)sizeof(struct timeval))
101eb1543c6SMatthew N. Dodd #define	MASK_LEN	(ICMP_MASKLEN - ICMP_MINLEN)
102eb1543c6SMatthew N. Dodd #define	TS_LEN		(ICMP_TSLEN - ICMP_MINLEN)
103c67c1ce8SMatthew N. Dodd #define	DEFDATALEN	56		/* default data length */
1048f975bb3SBruce Evans #define	FLOOD_BACKOFF	20000		/* usecs to back off if F_FLOOD mode */
1058f975bb3SBruce Evans 					/* runs out of buffer space */
1064fba6582SMaxim Konovalov #define	MAXIPLEN	(sizeof(struct ip) + MAX_IPOPTLEN)
1074fba6582SMaxim Konovalov #define	MAXICMPLEN	(ICMP_ADVLENMIN + MAX_IPOPTLEN)
1084fba6582SMaxim Konovalov #define	MINICMPLEN	ICMP_MINLEN
1098fae3551SRodney W. Grimes #define	MAXWAIT		10		/* max seconds to wait for response */
110bf113f1bSBill Fumerola #define	MAXALARM	(60 * 60)	/* max seconds for alarm timeout */
1110b2f8b3fSMaxim Konovalov #define	MAXTOS		255
1128fae3551SRodney W. Grimes 
1138fae3551SRodney W. Grimes #define	A(bit)		rcvd_tbl[(bit)>>3]	/* identify byte in array */
1148fae3551SRodney W. Grimes #define	B(bit)		(1 << ((bit) & 0x07))	/* identify bit in byte */
1158fae3551SRodney W. Grimes #define	SET(bit)	(A(bit) |= B(bit))
1168fae3551SRodney W. Grimes #define	CLR(bit)	(A(bit) &= (~B(bit)))
1178fae3551SRodney W. Grimes #define	TST(bit)	(A(bit) & B(bit))
1188fae3551SRodney W. Grimes 
1198fae3551SRodney W. Grimes /* various options */
1208fae3551SRodney W. Grimes int options;
12185456935SBill Fenner #define	F_FLOOD		0x0001
12285456935SBill Fenner #define	F_INTERVAL	0x0002
12385456935SBill Fenner #define	F_NUMERIC	0x0004
12485456935SBill Fenner #define	F_PINGFILLED	0x0008
12585456935SBill Fenner #define	F_QUIET		0x0010
12685456935SBill Fenner #define	F_RROUTE	0x0020
12785456935SBill Fenner #define	F_SO_DEBUG	0x0040
12885456935SBill Fenner #define	F_SO_DONTROUTE	0x0080
12985456935SBill Fenner #define	F_VERBOSE	0x0100
13085456935SBill Fenner #define	F_QUIET2	0x0200
13185456935SBill Fenner #define	F_NOLOOP	0x0400
13285456935SBill Fenner #define	F_MTTL		0x0800
13385456935SBill Fenner #define	F_MIF		0x1000
134772dfa72SDaniel O'Callaghan #define	F_AUDIBLE	0x2000
1359a4365d0SYoshinobu Inoue #ifdef IPSEC
1369a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
1379a4365d0SYoshinobu Inoue #define F_POLICY	0x4000
1389a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/
1399a4365d0SYoshinobu Inoue #endif /*IPSEC*/
140211bfbd2SRuslan Ermilov #define	F_TTL		0x8000
141ca517ad8SPoul-Henning Kamp #define	F_MISSED	0x10000
1428025c44bSDima Dorfman #define	F_ONCE		0x20000
1430b2f8b3fSMaxim Konovalov #define	F_HDRINCL	0x40000
144143008a1SMatthew N. Dodd #define	F_MASK		0x80000
145eb1543c6SMatthew N. Dodd #define	F_TIME		0x100000
1468fae3551SRodney W. Grimes 
1478fae3551SRodney W. Grimes /*
1488fae3551SRodney W. Grimes  * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
1498fae3551SRodney W. Grimes  * number of received sequence numbers we can keep track of.  Change 128
1508fae3551SRodney W. Grimes  * to 8192 for complete accuracy...
1518fae3551SRodney W. Grimes  */
1528fae3551SRodney W. Grimes #define	MAX_DUP_CHK	(8 * 128)
1538fae3551SRodney W. Grimes int mx_dup_ck = MAX_DUP_CHK;
1548fae3551SRodney W. Grimes char rcvd_tbl[MAX_DUP_CHK / 8];
1558fae3551SRodney W. Grimes 
156d389e86aSMatt Jacob struct sockaddr_in whereto;	/* who to ping */
1578fae3551SRodney W. Grimes int datalen = DEFDATALEN;
1581104dd84SBruce Evans int maxpayload;
1598fae3551SRodney W. Grimes int s;				/* socket file descriptor */
1600b2f8b3fSMaxim Konovalov u_char outpackhdr[IP_MAXPACKET], *outpack;
161ca517ad8SPoul-Henning Kamp char BBELL = '\a';		/* characters written for MISSED and AUDIBLE */
162261e59bbSMaxim Konovalov char BSPACE = '\b';		/* characters written for flood */
1638fae3551SRodney W. Grimes char DOT = '.';
1648fae3551SRodney W. Grimes char *hostname;
16599490edeSWarner Losh char *shostname;
1668fae3551SRodney W. Grimes int ident;			/* process id to identify our packets */
167ee2bf734SWarner Losh int uid;			/* cached uid for micro-optimization */
168eb1543c6SMatthew N. Dodd u_char icmp_type = ICMP_ECHO;
169eb1543c6SMatthew N. Dodd u_char icmp_type_rsp = ICMP_ECHOREPLY;
170eb1543c6SMatthew N. Dodd int phdr_len = 0;
1718fae3551SRodney W. Grimes 
1728fae3551SRodney W. Grimes /* counters */
173261e59bbSMaxim Konovalov long nmissedmax;		/* max value of ntransmitted - nreceived - 1 */
1748fae3551SRodney W. Grimes long npackets;			/* max packets to transmit */
1758fae3551SRodney W. Grimes long nreceived;			/* # of packets we got back */
1768fae3551SRodney W. Grimes long nrepeats;			/* number of duplicates */
1778fae3551SRodney W. Grimes long ntransmitted;		/* sequence # for outbound packets = #sent */
178526f06b2SMatthew Dillon int interval = 1000;		/* interval between packets, ms */
1798fae3551SRodney W. Grimes 
1808fae3551SRodney W. Grimes /* timing */
1818fae3551SRodney W. Grimes int timing;			/* flag to do timing */
1828fae3551SRodney W. Grimes double tmin = 999999999.0;	/* minimum round trip time */
1838fae3551SRodney W. Grimes double tmax = 0.0;		/* maximum round trip time */
1848fae3551SRodney W. Grimes double tsum = 0.0;		/* sum of all times, for doing average */
1853109a910SGarrett Wollman double tsumsq = 0.0;		/* sum of all times squared, for std. dev. */
1868fae3551SRodney W. Grimes 
1878f975bb3SBruce Evans volatile sig_atomic_t finish_up;  /* nonzero if we've been told to finish up */
188badd8138SSean Eric Fagan int reset_kerninfo;
1898f975bb3SBruce Evans volatile sig_atomic_t siginfo_p;
190badd8138SSean Eric Fagan 
19143470e3bSGarrett Wollman static void fill(char *, char *);
19243470e3bSGarrett Wollman static u_short in_cksum(u_short *, int);
19343470e3bSGarrett Wollman static void check_status(void);
1948f975bb3SBruce Evans static void finish(void) __dead2;
19543470e3bSGarrett Wollman static void pinger(void);
19643470e3bSGarrett Wollman static char *pr_addr(struct in_addr);
197eb1543c6SMatthew N. Dodd static char *pr_ntime(n_time);
19843470e3bSGarrett Wollman static void pr_icmph(struct icmp *);
19943470e3bSGarrett Wollman static void pr_iph(struct ip *);
200039d6aa4SBill Fenner static void pr_pack(char *, int, struct sockaddr_in *, struct timeval *);
20143470e3bSGarrett Wollman static void pr_retip(struct ip *);
20243470e3bSGarrett Wollman static void status(int);
2038f975bb3SBruce Evans static void stopit(int);
20443470e3bSGarrett Wollman static void tvsub(struct timeval *, struct timeval *);
205e345a80dSPhilippe Charnier static void usage(void) __dead2;
2068fae3551SRodney W. Grimes 
20743470e3bSGarrett Wollman int
2088fae3551SRodney W. Grimes main(argc, argv)
2098fae3551SRodney W. Grimes 	int argc;
21043470e3bSGarrett Wollman 	char *const *argv;
2118fae3551SRodney W. Grimes {
21231eac03bSSean Chittenden 	struct sockaddr_in from, sock_in;
213efc8588dSDavid E. O'Brien 	struct in_addr ifaddr;
214261e59bbSMaxim Konovalov 	struct timeval last, intvl;
215efc8588dSDavid E. O'Brien 	struct iovec iov;
2160b2f8b3fSMaxim Konovalov 	struct ip *ip;
217efc8588dSDavid E. O'Brien 	struct msghdr msg;
218efc8588dSDavid E. O'Brien 	struct sigaction si_sa;
219efc8588dSDavid E. O'Brien 	struct termios ts;
2200b2f8b3fSMaxim Konovalov 	size_t sz;
2214fba6582SMaxim Konovalov 	u_char *datap, packet[IP_MAXPACKET];
222d074d39fSMatthew N. Dodd 	char *ep, *source, *target, *payload;
223261e59bbSMaxim Konovalov 	struct hostent *hp;
224efc8588dSDavid E. O'Brien #ifdef IPSEC_POLICY_IPSEC
225efc8588dSDavid E. O'Brien 	char *policy_in, *policy_out;
226efc8588dSDavid E. O'Brien #endif
227261e59bbSMaxim Konovalov 	struct sockaddr_in *to;
228261e59bbSMaxim Konovalov 	double t;
229efc8588dSDavid E. O'Brien 	u_long alarmtimeout, ultmp;
230261e59bbSMaxim Konovalov 	int almost_done, ch, df, hold, i, mib[4], packlen, preload, sockerrno,
231261e59bbSMaxim Konovalov 	    tos, ttl;
232efc8588dSDavid E. O'Brien 	char ctrl[CMSG_SPACE(sizeof(struct timeval))];
233efc8588dSDavid E. O'Brien 	char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN];
2348fae3551SRodney W. Grimes #ifdef IP_OPTIONS
2354fba6582SMaxim Konovalov 	char rspace[MAX_IPOPTLEN];	/* record route space */
2368fae3551SRodney W. Grimes #endif
237261e59bbSMaxim Konovalov 	unsigned char loop, mttl;
238efc8588dSDavid E. O'Brien 
23931eac03bSSean Chittenden 	payload = source = NULL;
2409a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
241efc8588dSDavid E. O'Brien 	policy_in = policy_out = NULL;
2429a4365d0SYoshinobu Inoue #endif
2438fae3551SRodney W. Grimes 
244f1284d7aSBill Fenner 	/*
245f1284d7aSBill Fenner 	 * Do the stuff that we need root priv's for *first*, and
246f1284d7aSBill Fenner 	 * then drop our setuid bit.  Save error reporting for
247f1284d7aSBill Fenner 	 * after arg parsing.
248f1284d7aSBill Fenner 	 */
24943470e3bSGarrett Wollman 	s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
250f1284d7aSBill Fenner 	sockerrno = errno;
251f1284d7aSBill Fenner 
252f1284d7aSBill Fenner 	setuid(getuid());
253ee2bf734SWarner Losh 	uid = getuid();
254f1284d7aSBill Fenner 
2550b2f8b3fSMaxim Konovalov 	alarmtimeout = df = preload = tos = 0;
256badd8138SSean Eric Fagan 
2570b2f8b3fSMaxim Konovalov 	outpack = outpackhdr + sizeof(struct ip);
258211bfbd2SRuslan Ermilov 	while ((ch = getopt(argc, argv,
2592c56e246SMatthew N. Dodd 		"Aac:DdfI:i:Ll:M:m:nop:QqRrS:s:T:t:vz:"
260211bfbd2SRuslan Ermilov #ifdef IPSEC
2619a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
262211bfbd2SRuslan Ermilov 		"P:"
2639a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/
264211bfbd2SRuslan Ermilov #endif /*IPSEC*/
265211bfbd2SRuslan Ermilov 		)) != -1)
2669a4365d0SYoshinobu Inoue 	{
2678fae3551SRodney W. Grimes 		switch(ch) {
268ca517ad8SPoul-Henning Kamp 		case 'A':
269ca517ad8SPoul-Henning Kamp 			options |= F_MISSED;
270ca517ad8SPoul-Henning Kamp 			break;
271772dfa72SDaniel O'Callaghan 		case 'a':
272772dfa72SDaniel O'Callaghan 			options |= F_AUDIBLE;
273772dfa72SDaniel O'Callaghan 			break;
2748fae3551SRodney W. Grimes 		case 'c':
27543470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
27643470e3bSGarrett Wollman 			if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp)
27743470e3bSGarrett Wollman 				errx(EX_USAGE,
27843470e3bSGarrett Wollman 				    "invalid count of packets to transmit: `%s'",
27943470e3bSGarrett Wollman 				    optarg);
28043470e3bSGarrett Wollman 			npackets = ultmp;
2818fae3551SRodney W. Grimes 			break;
2820b2f8b3fSMaxim Konovalov 		case 'D':
2830b2f8b3fSMaxim Konovalov 			options |= F_HDRINCL;
2840b2f8b3fSMaxim Konovalov 			df = 1;
2850b2f8b3fSMaxim Konovalov 			break;
2868fae3551SRodney W. Grimes 		case 'd':
2878fae3551SRodney W. Grimes 			options |= F_SO_DEBUG;
2888fae3551SRodney W. Grimes 			break;
2898fae3551SRodney W. Grimes 		case 'f':
290526f06b2SMatthew Dillon 			if (uid) {
29143470e3bSGarrett Wollman 				errno = EPERM;
29243470e3bSGarrett Wollman 				err(EX_NOPERM, "-f flag");
2938fae3551SRodney W. Grimes 			}
2948fae3551SRodney W. Grimes 			options |= F_FLOOD;
2958fae3551SRodney W. Grimes 			setbuf(stdout, (char *)NULL);
2968fae3551SRodney W. Grimes 			break;
2971f6a4631SRuslan Ermilov 		case 'I':		/* multicast interface */
2981f6a4631SRuslan Ermilov 			if (inet_aton(optarg, &ifaddr) == 0)
2991f6a4631SRuslan Ermilov 				errx(EX_USAGE,
3001f6a4631SRuslan Ermilov 				    "invalid multicast interface: `%s'",
3011f6a4631SRuslan Ermilov 				    optarg);
3021f6a4631SRuslan Ermilov 			options |= F_MIF;
3031f6a4631SRuslan Ermilov 			break;
3048fae3551SRodney W. Grimes 		case 'i':		/* wait between sending packets */
3051ad0b1beSMaxim Konovalov 			t = strtod(optarg, &ep) * 1000.0;
3061ad0b1beSMaxim Konovalov 			if (*ep || ep == optarg || t > (double)INT_MAX)
3071ad0b1beSMaxim Konovalov 				errx(EX_USAGE, "invalid timing interval: `%s'",
3081ad0b1beSMaxim Konovalov 				    optarg);
3098fae3551SRodney W. Grimes 			options |= F_INTERVAL;
310526f06b2SMatthew Dillon 			interval = (int)t;
311526f06b2SMatthew Dillon 			if (uid && interval < 1000) {
312526f06b2SMatthew Dillon 				errno = EPERM;
313526f06b2SMatthew Dillon 				err(EX_NOPERM, "-i interval too short");
314526f06b2SMatthew Dillon 			}
3158fae3551SRodney W. Grimes 			break;
3161f6a4631SRuslan Ermilov 		case 'L':
3171f6a4631SRuslan Ermilov 			options |= F_NOLOOP;
3181f6a4631SRuslan Ermilov 			loop = 0;
31985456935SBill Fenner 			break;
3208fae3551SRodney W. Grimes 		case 'l':
32143470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
32243470e3bSGarrett Wollman 			if (*ep || ep == optarg || ultmp > INT_MAX)
32343470e3bSGarrett Wollman 				errx(EX_USAGE,
32443470e3bSGarrett Wollman 				    "invalid preload value: `%s'", optarg);
3255e2cc0f4SStephen McKay 			if (uid) {
326f78ac61bSWarner Losh 				errno = EPERM;
327f78ac61bSWarner Losh 				err(EX_NOPERM, "-l flag");
328f78ac61bSWarner Losh 			}
32943470e3bSGarrett Wollman 			preload = ultmp;
3308fae3551SRodney W. Grimes 			break;
3311f6a4631SRuslan Ermilov 		case 'M':
332eb1543c6SMatthew N. Dodd 			switch(optarg[0]) {
333eb1543c6SMatthew N. Dodd 			case 'M':
334eb1543c6SMatthew N. Dodd 			case 'm':
3351f6a4631SRuslan Ermilov 				options |= F_MASK;
33685456935SBill Fenner 				break;
337eb1543c6SMatthew N. Dodd 			case 'T':
338eb1543c6SMatthew N. Dodd 			case 't':
339eb1543c6SMatthew N. Dodd 				options |= F_TIME;
340eb1543c6SMatthew N. Dodd 				break;
341eb1543c6SMatthew N. Dodd 			default:
342eb1543c6SMatthew N. Dodd 				errx(EX_USAGE, "invalid message: `%c'", optarg[0]);
343eb1543c6SMatthew N. Dodd 				break;
344eb1543c6SMatthew N. Dodd 			}
345eb1543c6SMatthew N. Dodd 			break;
346211bfbd2SRuslan Ermilov 		case 'm':		/* TTL */
347211bfbd2SRuslan Ermilov 			ultmp = strtoul(optarg, &ep, 0);
3489bc1a9ecSMaxim Konovalov 			if (*ep || ep == optarg || ultmp > MAXTTL)
3491ad0b1beSMaxim Konovalov 				errx(EX_USAGE, "invalid TTL: `%s'", optarg);
350211bfbd2SRuslan Ermilov 			ttl = ultmp;
351211bfbd2SRuslan Ermilov 			options |= F_TTL;
352211bfbd2SRuslan Ermilov 			break;
3538fae3551SRodney W. Grimes 		case 'n':
3548fae3551SRodney W. Grimes 			options |= F_NUMERIC;
3558fae3551SRodney W. Grimes 			break;
3568025c44bSDima Dorfman 		case 'o':
3578025c44bSDima Dorfman 			options |= F_ONCE;
3588025c44bSDima Dorfman 			break;
3591f6a4631SRuslan Ermilov #ifdef IPSEC
3601f6a4631SRuslan Ermilov #ifdef IPSEC_POLICY_IPSEC
3611f6a4631SRuslan Ermilov 		case 'P':
3621f6a4631SRuslan Ermilov 			options |= F_POLICY;
3631f6a4631SRuslan Ermilov 			if (!strncmp("in", optarg, 2))
3641f6a4631SRuslan Ermilov 				policy_in = strdup(optarg);
3651f6a4631SRuslan Ermilov 			else if (!strncmp("out", optarg, 3))
3661f6a4631SRuslan Ermilov 				policy_out = strdup(optarg);
3671f6a4631SRuslan Ermilov 			else
3681f6a4631SRuslan Ermilov 				errx(1, "invalid security policy");
3691f6a4631SRuslan Ermilov 			break;
3701f6a4631SRuslan Ermilov #endif /*IPSEC_POLICY_IPSEC*/
3711f6a4631SRuslan Ermilov #endif /*IPSEC*/
3728fae3551SRodney W. Grimes 		case 'p':		/* fill buffer with user pattern */
3738fae3551SRodney W. Grimes 			options |= F_PINGFILLED;
374d074d39fSMatthew N. Dodd 			payload = optarg;
3758fae3551SRodney W. Grimes 			break;
376ef9e6dc7SBill Fenner 		case 'Q':
377ef9e6dc7SBill Fenner 			options |= F_QUIET2;
378ef9e6dc7SBill Fenner 			break;
3798fae3551SRodney W. Grimes 		case 'q':
3808fae3551SRodney W. Grimes 			options |= F_QUIET;
3818fae3551SRodney W. Grimes 			break;
3828fae3551SRodney W. Grimes 		case 'R':
3838fae3551SRodney W. Grimes 			options |= F_RROUTE;
3848fae3551SRodney W. Grimes 			break;
3858fae3551SRodney W. Grimes 		case 'r':
3868fae3551SRodney W. Grimes 			options |= F_SO_DONTROUTE;
3878fae3551SRodney W. Grimes 			break;
3881f6a4631SRuslan Ermilov 		case 'S':
3891f6a4631SRuslan Ermilov 			source = optarg;
3901f6a4631SRuslan Ermilov 			break;
3918fae3551SRodney W. Grimes 		case 's':		/* size of packet to send */
39243470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
3934fba6582SMaxim Konovalov 			if (*ep || ep == optarg)
39443470e3bSGarrett Wollman 				errx(EX_USAGE, "invalid packet size: `%s'",
39543470e3bSGarrett Wollman 				    optarg);
396fb7d32c7SMaxim Konovalov 			if (uid != 0 && ultmp > DEFDATALEN) {
397fb7d32c7SMaxim Konovalov 				errno = EPERM;
398fb7d32c7SMaxim Konovalov 				err(EX_NOPERM,
399fb7d32c7SMaxim Konovalov 				    "packet size too large: %lu > %u",
400fb7d32c7SMaxim Konovalov 				    ultmp, DEFDATALEN);
401fb7d32c7SMaxim Konovalov 			}
40243470e3bSGarrett Wollman 			datalen = ultmp;
4038fae3551SRodney W. Grimes 			break;
4041f6a4631SRuslan Ermilov 		case 'T':		/* multicast TTL */
4051f6a4631SRuslan Ermilov 			ultmp = strtoul(optarg, &ep, 0);
4061f6a4631SRuslan Ermilov 			if (*ep || ep == optarg || ultmp > MAXTTL)
4071f6a4631SRuslan Ermilov 				errx(EX_USAGE, "invalid multicast TTL: `%s'",
4081f6a4631SRuslan Ermilov 				    optarg);
4091f6a4631SRuslan Ermilov 			mttl = ultmp;
4101f6a4631SRuslan Ermilov 			options |= F_MTTL;
41199490edeSWarner Losh 			break;
4127237fd94SBill Fumerola 		case 't':
413bf113f1bSBill Fumerola 			alarmtimeout = strtoul(optarg, &ep, 0);
414bf113f1bSBill Fumerola 			if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX))
4157237fd94SBill Fumerola 				errx(EX_USAGE, "invalid timeout: `%s'",
4167237fd94SBill Fumerola 				    optarg);
417bf113f1bSBill Fumerola 			if (alarmtimeout > MAXALARM)
418bf113f1bSBill Fumerola 				errx(EX_USAGE, "invalid timeout: `%s' > %d",
419bf113f1bSBill Fumerola 				    optarg, MAXALARM);
420bf113f1bSBill Fumerola 			alarm((int)alarmtimeout);
4217237fd94SBill Fumerola 			break;
4228fae3551SRodney W. Grimes 		case 'v':
4238fae3551SRodney W. Grimes 			options |= F_VERBOSE;
4248fae3551SRodney W. Grimes 			break;
4250b2f8b3fSMaxim Konovalov 		case 'z':
4260b2f8b3fSMaxim Konovalov 			options |= F_HDRINCL;
4270b2f8b3fSMaxim Konovalov 			ultmp = strtoul(optarg, &ep, 0);
4280b2f8b3fSMaxim Konovalov 			if (*ep || ep == optarg || ultmp > MAXTOS)
4290b2f8b3fSMaxim Konovalov 				errx(EX_USAGE, "invalid TOS: `%s'", optarg);
4300b2f8b3fSMaxim Konovalov 			tos = ultmp;
4310b2f8b3fSMaxim Konovalov 			break;
4328fae3551SRodney W. Grimes 		default:
433e345a80dSPhilippe Charnier 			usage();
43443470e3bSGarrett Wollman 		}
43543470e3bSGarrett Wollman 	}
43643470e3bSGarrett Wollman 
43743470e3bSGarrett Wollman 	if (argc - optind != 1)
438e345a80dSPhilippe Charnier 		usage();
43943470e3bSGarrett Wollman 	target = argv[optind];
4408fae3551SRodney W. Grimes 
441d829c3dfSMatthew N. Dodd 	switch (options & (F_MASK|F_TIME)) {
442d829c3dfSMatthew N. Dodd 	case 0: break;
443d829c3dfSMatthew N. Dodd 	case F_MASK:
444eb1543c6SMatthew N. Dodd 		icmp_type = ICMP_MASKREQ;
445eb1543c6SMatthew N. Dodd 		icmp_type_rsp = ICMP_MASKREPLY;
446d829c3dfSMatthew N. Dodd 		phdr_len = MASK_LEN;
447eb1543c6SMatthew N. Dodd 		if (!(options & F_QUIET))
448eb1543c6SMatthew N. Dodd 			(void)printf("ICMP_MASKREQ\n");
449d829c3dfSMatthew N. Dodd 		break;
450d829c3dfSMatthew N. Dodd 	case F_TIME:
451eb1543c6SMatthew N. Dodd 		icmp_type = ICMP_TSTAMP;
452eb1543c6SMatthew N. Dodd 		icmp_type_rsp = ICMP_TSTAMPREPLY;
453d829c3dfSMatthew N. Dodd 		phdr_len = TS_LEN;
454eb1543c6SMatthew N. Dodd 		if (!(options & F_QUIET))
455eb1543c6SMatthew N. Dodd 			(void)printf("ICMP_TSTAMP\n");
456d829c3dfSMatthew N. Dodd 		break;
457d829c3dfSMatthew N. Dodd 	default:
458d829c3dfSMatthew N. Dodd 		errx(EX_USAGE, "ICMP_TSTAMP and ICMP_MASKREQ are exclusive.");
459d829c3dfSMatthew N. Dodd 		break;
460eb1543c6SMatthew N. Dodd 	}
461d829c3dfSMatthew N. Dodd 	maxpayload = IP_MAXPACKET - sizeof(struct ip) - MINICMPLEN - phdr_len;
462fb7d32c7SMaxim Konovalov 	if (options & F_RROUTE)
463fb7d32c7SMaxim Konovalov 		maxpayload -= MAX_IPOPTLEN;
464fb7d32c7SMaxim Konovalov 	if (datalen > maxpayload)
4651104dd84SBruce Evans 		errx(EX_USAGE, "packet size too large: %d > %d", datalen,
466fb7d32c7SMaxim Konovalov 		    maxpayload);
467d829c3dfSMatthew N. Dodd 	datap = &outpack[MINICMPLEN + phdr_len + TIMEVAL_LEN];
468d074d39fSMatthew N. Dodd 	if (options & F_PINGFILLED) {
469d074d39fSMatthew N. Dodd 		fill((char *)datap, payload);
470d074d39fSMatthew N. Dodd 	}
47199490edeSWarner Losh 	if (source) {
47231eac03bSSean Chittenden 		bzero((char *)&sock_in, sizeof(sock_in));
47331eac03bSSean Chittenden 		sock_in.sin_family = AF_INET;
47431eac03bSSean Chittenden 		if (inet_aton(source, &sock_in.sin_addr) != 0) {
47599490edeSWarner Losh 			shostname = source;
47699490edeSWarner Losh 		} else {
47799490edeSWarner Losh 			hp = gethostbyname2(source, AF_INET);
47899490edeSWarner Losh 			if (!hp)
47999490edeSWarner Losh 				errx(EX_NOHOST, "cannot resolve %s: %s",
48099490edeSWarner Losh 				    source, hstrerror(h_errno));
48199490edeSWarner Losh 
48231eac03bSSean Chittenden 			sock_in.sin_len = sizeof sock_in;
48331eac03bSSean Chittenden 			if ((unsigned)hp->h_length > sizeof(sock_in.sin_addr) ||
4844fba6582SMaxim Konovalov 			    hp->h_length < 0)
48599490edeSWarner Losh 				errx(1, "gethostbyname2: illegal address");
48631eac03bSSean Chittenden 			memcpy(&sock_in.sin_addr, hp->h_addr_list[0],
48731eac03bSSean Chittenden 			    sizeof(sock_in.sin_addr));
48899490edeSWarner Losh 			(void)strncpy(snamebuf, hp->h_name,
48999490edeSWarner Losh 			    sizeof(snamebuf) - 1);
49099490edeSWarner Losh 			snamebuf[sizeof(snamebuf) - 1] = '\0';
49199490edeSWarner Losh 			shostname = snamebuf;
49299490edeSWarner Losh 		}
49331eac03bSSean Chittenden 		if (bind(s, (struct sockaddr *)&sock_in, sizeof sock_in) == -1)
49499490edeSWarner Losh 			err(1, "bind");
49599490edeSWarner Losh 	}
49699490edeSWarner Losh 
497d389e86aSMatt Jacob 	bzero(&whereto, sizeof(whereto));
498d389e86aSMatt Jacob 	to = &whereto;
4998fae3551SRodney W. Grimes 	to->sin_family = AF_INET;
500d389e86aSMatt Jacob 	to->sin_len = sizeof *to;
50143470e3bSGarrett Wollman 	if (inet_aton(target, &to->sin_addr) != 0) {
5028fae3551SRodney W. Grimes 		hostname = target;
50343470e3bSGarrett Wollman 	} else {
50443470e3bSGarrett Wollman 		hp = gethostbyname2(target, AF_INET);
50543470e3bSGarrett Wollman 		if (!hp)
50643470e3bSGarrett Wollman 			errx(EX_NOHOST, "cannot resolve %s: %s",
50743470e3bSGarrett Wollman 			    target, hstrerror(h_errno));
50843470e3bSGarrett Wollman 
50931eac03bSSean Chittenden 		if ((unsigned)hp->h_length > sizeof(to->sin_addr))
5101ffae4a6SWarner Losh 			errx(1, "gethostbyname2 returned an illegal address");
51143470e3bSGarrett Wollman 		memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr);
5128fae3551SRodney W. Grimes 		(void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1);
513b10d9d5fSWarner Losh 		hnamebuf[sizeof(hnamebuf) - 1] = '\0';
5148fae3551SRodney W. Grimes 		hostname = hnamebuf;
5158fae3551SRodney W. Grimes 	}
5168fae3551SRodney W. Grimes 
51743470e3bSGarrett Wollman 	if (options & F_FLOOD && options & F_INTERVAL)
51843470e3bSGarrett Wollman 		errx(EX_USAGE, "-f and -i: incompatible options");
51943470e3bSGarrett Wollman 
52043470e3bSGarrett Wollman 	if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
52143470e3bSGarrett Wollman 		errx(EX_USAGE,
52243470e3bSGarrett Wollman 		    "-f flag cannot be used with multicast destination");
52343470e3bSGarrett Wollman 	if (options & (F_MIF | F_NOLOOP | F_MTTL)
52443470e3bSGarrett Wollman 	    && !IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
52543470e3bSGarrett Wollman 		errx(EX_USAGE,
52643470e3bSGarrett Wollman 		    "-I, -L, -T flags cannot be used with unicast destination");
5278fae3551SRodney W. Grimes 
528d829c3dfSMatthew N. Dodd 	if (datalen >= TIMEVAL_LEN)	/* can we time transfer */
5298fae3551SRodney W. Grimes 		timing = 1;
5304fba6582SMaxim Konovalov 	packlen = MAXIPLEN + MAXICMPLEN + datalen;
5314fba6582SMaxim Konovalov 	packlen = packlen > IP_MAXPACKET ? IP_MAXPACKET : packlen;
53243470e3bSGarrett Wollman 
5338fae3551SRodney W. Grimes 	if (!(options & F_PINGFILLED))
534d829c3dfSMatthew N. Dodd 		for (i = TIMEVAL_LEN; i < datalen; ++i)
5358fae3551SRodney W. Grimes 			*datap++ = i;
5368fae3551SRodney W. Grimes 
5378fae3551SRodney W. Grimes 	ident = getpid() & 0xFFFF;
5388fae3551SRodney W. Grimes 
539f1284d7aSBill Fenner 	if (s < 0) {
540f1284d7aSBill Fenner 		errno = sockerrno;
54143470e3bSGarrett Wollman 		err(EX_OSERR, "socket");
5428fae3551SRodney W. Grimes 	}
5438fae3551SRodney W. Grimes 	hold = 1;
5448fae3551SRodney W. Grimes 	if (options & F_SO_DEBUG)
5458fae3551SRodney W. Grimes 		(void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
5468fae3551SRodney W. Grimes 		    sizeof(hold));
5478fae3551SRodney W. Grimes 	if (options & F_SO_DONTROUTE)
5488fae3551SRodney W. Grimes 		(void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold,
5498fae3551SRodney W. Grimes 		    sizeof(hold));
5509a4365d0SYoshinobu Inoue #ifdef IPSEC
5519a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
5529a4365d0SYoshinobu Inoue 	if (options & F_POLICY) {
5539a4365d0SYoshinobu Inoue 		char *buf;
5549a4365d0SYoshinobu Inoue 		if (policy_in != NULL) {
5559a4365d0SYoshinobu Inoue 			buf = ipsec_set_policy(policy_in, strlen(policy_in));
5569a4365d0SYoshinobu Inoue 			if (buf == NULL)
557ffd40070SKris Kennaway 				errx(EX_CONFIG, "%s", ipsec_strerror());
5589a4365d0SYoshinobu Inoue 			if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY,
5599a4365d0SYoshinobu Inoue 					buf, ipsec_get_policylen(buf)) < 0)
5601ad0b1beSMaxim Konovalov 				err(EX_CONFIG,
5611ad0b1beSMaxim Konovalov 				    "ipsec policy cannot be configured");
5629a4365d0SYoshinobu Inoue 			free(buf);
5639a4365d0SYoshinobu Inoue 		}
5649a4365d0SYoshinobu Inoue 
5659a4365d0SYoshinobu Inoue 		if (policy_out != NULL) {
5669a4365d0SYoshinobu Inoue 			buf = ipsec_set_policy(policy_out, strlen(policy_out));
5679a4365d0SYoshinobu Inoue 			if (buf == NULL)
568ffd40070SKris Kennaway 				errx(EX_CONFIG, "%s", ipsec_strerror());
5699a4365d0SYoshinobu Inoue 			if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY,
5709a4365d0SYoshinobu Inoue 					buf, ipsec_get_policylen(buf)) < 0)
5711ad0b1beSMaxim Konovalov 				err(EX_CONFIG,
5721ad0b1beSMaxim Konovalov 				    "ipsec policy cannot be configured");
5739a4365d0SYoshinobu Inoue 			free(buf);
5749a4365d0SYoshinobu Inoue 		}
5759a4365d0SYoshinobu Inoue 	}
5769a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/
5779a4365d0SYoshinobu Inoue #endif /*IPSEC*/
5788fae3551SRodney W. Grimes 
5790b2f8b3fSMaxim Konovalov 	if (options & F_HDRINCL) {
5800b2f8b3fSMaxim Konovalov 		ip = (struct ip*)outpackhdr;
5810b2f8b3fSMaxim Konovalov 		if (!(options & (F_TTL | F_MTTL))) {
5820b2f8b3fSMaxim Konovalov 			mib[0] = CTL_NET;
5830b2f8b3fSMaxim Konovalov 			mib[1] = PF_INET;
5840b2f8b3fSMaxim Konovalov 			mib[2] = IPPROTO_IP;
5850b2f8b3fSMaxim Konovalov 			mib[3] = IPCTL_DEFTTL;
5860b2f8b3fSMaxim Konovalov 			sz = sizeof(ttl);
5870b2f8b3fSMaxim Konovalov 			if (sysctl(mib, 4, &ttl, &sz, NULL, 0) == -1)
5880b2f8b3fSMaxim Konovalov 				err(1, "sysctl(net.inet.ip.ttl)");
5890b2f8b3fSMaxim Konovalov 		}
5900b2f8b3fSMaxim Konovalov 		setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold));
5910b2f8b3fSMaxim Konovalov 		ip->ip_v = IPVERSION;
5920b2f8b3fSMaxim Konovalov 		ip->ip_hl = sizeof(struct ip) >> 2;
5930b2f8b3fSMaxim Konovalov 		ip->ip_tos = tos;
5940b2f8b3fSMaxim Konovalov 		ip->ip_id = 0;
5950b2f8b3fSMaxim Konovalov 		ip->ip_off = df ? IP_DF : 0;
5960b2f8b3fSMaxim Konovalov 		ip->ip_ttl = ttl;
5970b2f8b3fSMaxim Konovalov 		ip->ip_p = IPPROTO_ICMP;
59831eac03bSSean Chittenden 		ip->ip_src.s_addr = source ? sock_in.sin_addr.s_addr : INADDR_ANY;
5990b2f8b3fSMaxim Konovalov 		ip->ip_dst = to->sin_addr;
6000b2f8b3fSMaxim Konovalov         }
6018fae3551SRodney W. Grimes 	/* record route option */
6028fae3551SRodney W. Grimes 	if (options & F_RROUTE) {
6038fae3551SRodney W. Grimes #ifdef IP_OPTIONS
604039d6aa4SBill Fenner 		bzero(rspace, sizeof(rspace));
6058fae3551SRodney W. Grimes 		rspace[IPOPT_OPTVAL] = IPOPT_RR;
6068fae3551SRodney W. Grimes 		rspace[IPOPT_OLEN] = sizeof(rspace) - 1;
6078fae3551SRodney W. Grimes 		rspace[IPOPT_OFFSET] = IPOPT_MINOFF;
608039d6aa4SBill Fenner 		rspace[sizeof(rspace) - 1] = IPOPT_EOL;
6098fae3551SRodney W. Grimes 		if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace,
61043470e3bSGarrett Wollman 		    sizeof(rspace)) < 0)
61143470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_OPTIONS");
6128fae3551SRodney W. Grimes #else
61343470e3bSGarrett Wollman 		errx(EX_UNAVAILABLE,
61443470e3bSGarrett Wollman 		    "record route not available in this implementation");
6158fae3551SRodney W. Grimes #endif /* IP_OPTIONS */
6168fae3551SRodney W. Grimes 	}
6178fae3551SRodney W. Grimes 
618211bfbd2SRuslan Ermilov 	if (options & F_TTL) {
619211bfbd2SRuslan Ermilov 		if (setsockopt(s, IPPROTO_IP, IP_TTL, &ttl,
620211bfbd2SRuslan Ermilov 		    sizeof(ttl)) < 0) {
621211bfbd2SRuslan Ermilov 			err(EX_OSERR, "setsockopt IP_TTL");
622211bfbd2SRuslan Ermilov 		}
623211bfbd2SRuslan Ermilov 	}
62485456935SBill Fenner 	if (options & F_NOLOOP) {
62585456935SBill Fenner 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop,
62685456935SBill Fenner 		    sizeof(loop)) < 0) {
62743470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP");
62885456935SBill Fenner 		}
62985456935SBill Fenner 	}
63085456935SBill Fenner 	if (options & F_MTTL) {
631211bfbd2SRuslan Ermilov 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &mttl,
632211bfbd2SRuslan Ermilov 		    sizeof(mttl)) < 0) {
63343470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_TTL");
63485456935SBill Fenner 		}
63585456935SBill Fenner 	}
63685456935SBill Fenner 	if (options & F_MIF) {
63785456935SBill Fenner 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr,
63885456935SBill Fenner 		    sizeof(ifaddr)) < 0) {
63943470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_IF");
64085456935SBill Fenner 		}
64185456935SBill Fenner 	}
642039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
643039d6aa4SBill Fenner 	{ int on = 1;
644039d6aa4SBill Fenner 	if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) < 0)
645039d6aa4SBill Fenner 		err(EX_OSERR, "setsockopt SO_TIMESTAMP");
646039d6aa4SBill Fenner 	}
647039d6aa4SBill Fenner #endif
64885456935SBill Fenner 
6498fae3551SRodney W. Grimes 	/*
6508fae3551SRodney W. Grimes 	 * When pinging the broadcast address, you can get a lot of answers.
6518fae3551SRodney W. Grimes 	 * Doing something so evil is useful if you are trying to stress the
6528fae3551SRodney W. Grimes 	 * ethernet, or just want to fill the arp cache to get some stuff for
65343470e3bSGarrett Wollman 	 * /etc/ethers.  But beware: RFC 1122 allows hosts to ignore broadcast
65443470e3bSGarrett Wollman 	 * or multicast pings if they wish.
6558fae3551SRodney W. Grimes 	 */
6564fba6582SMaxim Konovalov 
6574fba6582SMaxim Konovalov 	/*
6584fba6582SMaxim Konovalov 	 * XXX receive buffer needs undetermined space for mbuf overhead
6594fba6582SMaxim Konovalov 	 * as well.
6604fba6582SMaxim Konovalov 	 */
6614fba6582SMaxim Konovalov 	hold = IP_MAXPACKET + 128;
6628fae3551SRodney W. Grimes 	(void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
6638fae3551SRodney W. Grimes 	    sizeof(hold));
664261e59bbSMaxim Konovalov 	if (uid == 0)
665e8bd25ceSRobert Watson 		(void)setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&hold,
666e8bd25ceSRobert Watson 		    sizeof(hold));
667e8bd25ceSRobert Watson 
66899490edeSWarner Losh 	if (to->sin_family == AF_INET) {
66999490edeSWarner Losh 		(void)printf("PING %s (%s)", hostname,
67099490edeSWarner Losh 		    inet_ntoa(to->sin_addr));
67199490edeSWarner Losh 		if (source)
67299490edeSWarner Losh 			(void)printf(" from %s", shostname);
67399490edeSWarner Losh 		(void)printf(": %d data bytes\n", datalen);
67499490edeSWarner Losh 	} else
6758fae3551SRodney W. Grimes 		(void)printf("PING %s: %d data bytes\n", hostname, datalen);
6768fae3551SRodney W. Grimes 
6777d81b35cSBruce Evans 	/*
678a2a00888SSean Eric Fagan 	 * Use sigaction() instead of signal() to get unambiguous semantics,
679a2a00888SSean Eric Fagan 	 * in particular with SA_RESTART not set.
6807d81b35cSBruce Evans 	 */
681a2a00888SSean Eric Fagan 
682f6bd468bSPaul Traina 	sigemptyset(&si_sa.sa_mask);
68337e5b2c6SSean Eric Fagan 	si_sa.sa_flags = 0;
684a2a00888SSean Eric Fagan 
685a2a00888SSean Eric Fagan 	si_sa.sa_handler = stopit;
686a2a00888SSean Eric Fagan 	if (sigaction(SIGINT, &si_sa, 0) == -1) {
687a2a00888SSean Eric Fagan 		err(EX_OSERR, "sigaction SIGINT");
688a2a00888SSean Eric Fagan 	}
689a2a00888SSean Eric Fagan 
690a2a00888SSean Eric Fagan 	si_sa.sa_handler = status;
69137e5b2c6SSean Eric Fagan 	if (sigaction(SIGINFO, &si_sa, 0) == -1) {
692624ff938SWarner Losh 		err(EX_OSERR, "sigaction");
69337e5b2c6SSean Eric Fagan 	}
694bf113f1bSBill Fumerola 
695bf113f1bSBill Fumerola         if (alarmtimeout > 0) {
6967237fd94SBill Fumerola 		si_sa.sa_handler = stopit;
6977237fd94SBill Fumerola 		if (sigaction(SIGALRM, &si_sa, 0) == -1)
6987237fd94SBill Fumerola 			err(EX_OSERR, "sigaction SIGALRM");
699bf113f1bSBill Fumerola         }
70037e5b2c6SSean Eric Fagan 
701039d6aa4SBill Fenner 	bzero(&msg, sizeof(msg));
702039d6aa4SBill Fenner 	msg.msg_name = (caddr_t)&from;
703039d6aa4SBill Fenner 	msg.msg_iov = &iov;
704039d6aa4SBill Fenner 	msg.msg_iovlen = 1;
705039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
706039d6aa4SBill Fenner 	msg.msg_control = (caddr_t)ctrl;
707039d6aa4SBill Fenner #endif
708039d6aa4SBill Fenner 	iov.iov_base = packet;
709039d6aa4SBill Fenner 	iov.iov_len = packlen;
710039d6aa4SBill Fenner 
7114055611aSSean Eric Fagan 	if (tcgetattr(STDOUT_FILENO, &ts) != -1) {
7124055611aSSean Eric Fagan 		reset_kerninfo = !(ts.c_lflag & NOKERNINFO);
7134055611aSSean Eric Fagan 		ts.c_lflag |= NOKERNINFO;
7144055611aSSean Eric Fagan 		tcsetattr(STDOUT_FILENO, TCSANOW, &ts);
7154055611aSSean Eric Fagan 	}
7164055611aSSean Eric Fagan 
71732af342fSRuslan Ermilov 	if (preload == 0)
71832af342fSRuslan Ermilov 		pinger();		/* send the first ping */
71932af342fSRuslan Ermilov 	else {
72032af342fSRuslan Ermilov 		if (npackets != 0 && preload > npackets)
72132af342fSRuslan Ermilov 			preload = npackets;
7228fae3551SRodney W. Grimes 		while (preload--)	/* fire off them quickies */
7238fae3551SRodney W. Grimes 			pinger();
72432af342fSRuslan Ermilov 	}
72532af342fSRuslan Ermilov 	(void)gettimeofday(&last, NULL);
7268fae3551SRodney W. Grimes 
727039d6aa4SBill Fenner 	if (options & F_FLOOD) {
728039d6aa4SBill Fenner 		intvl.tv_sec = 0;
729039d6aa4SBill Fenner 		intvl.tv_usec = 10000;
730039d6aa4SBill Fenner 	} else {
731526f06b2SMatthew Dillon 		intvl.tv_sec = interval / 1000;
732526f06b2SMatthew Dillon 		intvl.tv_usec = interval % 1000 * 1000;
733039d6aa4SBill Fenner 	}
734039d6aa4SBill Fenner 
735261e59bbSMaxim Konovalov 	almost_done = 0;
7368f975bb3SBruce Evans 	while (!finish_up) {
737261e59bbSMaxim Konovalov 		struct timeval now, timeout;
738039d6aa4SBill Fenner 		fd_set rfds;
739261e59bbSMaxim Konovalov 		int cc, n;
7408fae3551SRodney W. Grimes 
7417d81b35cSBruce Evans 		check_status();
74231eac03bSSean Chittenden 		if ((unsigned)s >= FD_SETSIZE)
7437e5bbd68SJacques Vidrine 			errx(EX_OSERR, "descriptor too large");
744039d6aa4SBill Fenner 		FD_ZERO(&rfds);
745039d6aa4SBill Fenner 		FD_SET(s, &rfds);
746039d6aa4SBill Fenner 		(void)gettimeofday(&now, NULL);
747039d6aa4SBill Fenner 		timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec;
748039d6aa4SBill Fenner 		timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec;
749039d6aa4SBill Fenner 		while (timeout.tv_usec < 0) {
750039d6aa4SBill Fenner 			timeout.tv_usec += 1000000;
751039d6aa4SBill Fenner 			timeout.tv_sec--;
7528fae3551SRodney W. Grimes 		}
753e345a80dSPhilippe Charnier 		while (timeout.tv_usec >= 1000000) {
754039d6aa4SBill Fenner 			timeout.tv_usec -= 1000000;
755039d6aa4SBill Fenner 			timeout.tv_sec++;
756039d6aa4SBill Fenner 		}
757039d6aa4SBill Fenner 		if (timeout.tv_sec < 0)
758039d6aa4SBill Fenner 			timeout.tv_sec = timeout.tv_usec = 0;
759039d6aa4SBill Fenner 		n = select(s + 1, &rfds, NULL, NULL, &timeout);
7605e2cc0f4SStephen McKay 		if (n < 0)
7615e2cc0f4SStephen McKay 			continue;	/* Must be EINTR. */
762039d6aa4SBill Fenner 		if (n == 1) {
76331eac03bSSean Chittenden 			struct timeval *tv = NULL;
764039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
765039d6aa4SBill Fenner 			struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl;
766039d6aa4SBill Fenner 
767039d6aa4SBill Fenner 			msg.msg_controllen = sizeof(ctrl);
768039d6aa4SBill Fenner #endif
769039d6aa4SBill Fenner 			msg.msg_namelen = sizeof(from);
770039d6aa4SBill Fenner 			if ((cc = recvmsg(s, &msg, 0)) < 0) {
7718fae3551SRodney W. Grimes 				if (errno == EINTR)
7728fae3551SRodney W. Grimes 					continue;
773e345a80dSPhilippe Charnier 				warn("recvmsg");
7748fae3551SRodney W. Grimes 				continue;
7758fae3551SRodney W. Grimes 			}
776039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
777039d6aa4SBill Fenner 			if (cmsg->cmsg_level == SOL_SOCKET &&
778039d6aa4SBill Fenner 			    cmsg->cmsg_type == SCM_TIMESTAMP &&
77931eac03bSSean Chittenden 			    cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) {
780fa05a94cSJohn Birrell 				/* Copy to avoid alignment problems: */
781fa05a94cSJohn Birrell 				memcpy(&now, CMSG_DATA(cmsg), sizeof(now));
78231eac03bSSean Chittenden 				tv = &now;
783fa05a94cSJohn Birrell 			}
784039d6aa4SBill Fenner #endif
78531eac03bSSean Chittenden 			if (tv == NULL) {
786039d6aa4SBill Fenner 				(void)gettimeofday(&now, NULL);
78731eac03bSSean Chittenden 				tv = &now;
788039d6aa4SBill Fenner 			}
78931eac03bSSean Chittenden 			pr_pack((char *)packet, cc, &from, tv);
79031eac03bSSean Chittenden 			if ((options & F_ONCE && nreceived) ||
79131eac03bSSean Chittenden 			    (npackets && nreceived >= npackets))
7928fae3551SRodney W. Grimes 				break;
7938fae3551SRodney W. Grimes 		}
7945e2cc0f4SStephen McKay 		if (n == 0 || options & F_FLOOD) {
795039d6aa4SBill Fenner 			if (!npackets || ntransmitted < npackets)
796039d6aa4SBill Fenner 				pinger();
797039d6aa4SBill Fenner 			else {
798039d6aa4SBill Fenner 				if (almost_done)
799039d6aa4SBill Fenner 					break;
800039d6aa4SBill Fenner 				almost_done = 1;
8015e2cc0f4SStephen McKay 				intvl.tv_usec = 0;
802039d6aa4SBill Fenner 				if (nreceived) {
803039d6aa4SBill Fenner 					intvl.tv_sec = 2 * tmax / 1000;
804039d6aa4SBill Fenner 					if (!intvl.tv_sec)
805039d6aa4SBill Fenner 						intvl.tv_sec = 1;
806039d6aa4SBill Fenner 				} else
807039d6aa4SBill Fenner 					intvl.tv_sec = MAXWAIT;
808039d6aa4SBill Fenner 			}
809039d6aa4SBill Fenner 			(void)gettimeofday(&last, NULL);
81025107197SIan Dowse 			if (ntransmitted - nreceived - 1 > nmissedmax) {
81125107197SIan Dowse 				nmissedmax = ntransmitted - nreceived - 1;
81225107197SIan Dowse 				if (options & F_MISSED)
813ca517ad8SPoul-Henning Kamp 					(void)write(STDOUT_FILENO, &BBELL, 1);
814039d6aa4SBill Fenner 			}
815039d6aa4SBill Fenner 		}
81625107197SIan Dowse 	}
8178f975bb3SBruce Evans 	finish();
8188fae3551SRodney W. Grimes 	/* NOTREACHED */
819f78ac61bSWarner Losh 	exit(0);	/* Make the compiler happy */
8208fae3551SRodney W. Grimes }
8218fae3551SRodney W. Grimes 
8228fae3551SRodney W. Grimes /*
8238f975bb3SBruce Evans  * stopit --
8248f975bb3SBruce Evans  *	Set the global bit that causes the main loop to quit.
8258f975bb3SBruce Evans  * Do NOT call finish() from here, since finish() does far too much
8268f975bb3SBruce Evans  * to be called from a signal handler.
827515dd2faSJulian Elischer  */
828515dd2faSJulian Elischer void
8298f975bb3SBruce Evans stopit(sig)
830c6facae4SMaxim Konovalov 	int sig __unused;
831515dd2faSJulian Elischer {
832efc8588dSDavid E. O'Brien 
833515dd2faSJulian Elischer 	finish_up = 1;
834515dd2faSJulian Elischer }
835515dd2faSJulian Elischer 
836515dd2faSJulian Elischer /*
8378fae3551SRodney W. Grimes  * pinger --
8388fae3551SRodney W. Grimes  *	Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
8398fae3551SRodney W. Grimes  * will be added on by the kernel.  The ID field is our UNIX process ID,
840eb1543c6SMatthew N. Dodd  * and the sequence number is an ascending integer.  The first TIMEVAL_LEN
8414fba6582SMaxim Konovalov  * bytes of the data portion are used to hold a UNIX "timeval" struct in
8424fba6582SMaxim Konovalov  * host byte-order, to compute the round-trip time.
8438fae3551SRodney W. Grimes  */
84443470e3bSGarrett Wollman static void
84543470e3bSGarrett Wollman pinger(void)
8468fae3551SRodney W. Grimes {
847eb1543c6SMatthew N. Dodd 	struct timeval now;
8480b2f8b3fSMaxim Konovalov 	struct ip *ip;
8493d438ad6SDavid E. O'Brien 	struct icmp *icp;
850efc8588dSDavid E. O'Brien 	int cc, i;
8510b2f8b3fSMaxim Konovalov 	u_char *packet;
8528fae3551SRodney W. Grimes 
8530b2f8b3fSMaxim Konovalov 	packet = outpack;
8548fae3551SRodney W. Grimes 	icp = (struct icmp *)outpack;
855eb1543c6SMatthew N. Dodd 	icp->icmp_type = icmp_type;
8568fae3551SRodney W. Grimes 	icp->icmp_code = 0;
8578fae3551SRodney W. Grimes 	icp->icmp_cksum = 0;
8585db89bc7SBill Fenner 	icp->icmp_seq = htons(ntransmitted);
8598fae3551SRodney W. Grimes 	icp->icmp_id = ident;			/* ID */
8608fae3551SRodney W. Grimes 
8615db89bc7SBill Fenner 	CLR(ntransmitted % mx_dup_ck);
8628fae3551SRodney W. Grimes 
863eb1543c6SMatthew N. Dodd 	if ((options & F_TIME) || timing) {
864eb1543c6SMatthew N. Dodd 		(void)gettimeofday(&now, NULL);
8658fae3551SRodney W. Grimes 
866eb1543c6SMatthew N. Dodd 		if (options & F_TIME)
867eb1543c6SMatthew N. Dodd 			icp->icmp_otime = htonl((now.tv_sec % (24*60*60))
868eb1543c6SMatthew N. Dodd 				* 1000 + now.tv_usec / 1000);
869eb1543c6SMatthew N. Dodd 		if (timing)
870d829c3dfSMatthew N. Dodd 			bcopy((void *)&now,
871d829c3dfSMatthew N. Dodd 			    (void *)&outpack[MINICMPLEN + phdr_len],
872eb1543c6SMatthew N. Dodd 			    sizeof(struct timeval));
873eb1543c6SMatthew N. Dodd 	}
874eb1543c6SMatthew N. Dodd 
875d829c3dfSMatthew N. Dodd 	cc = MINICMPLEN + phdr_len + datalen;
8768fae3551SRodney W. Grimes 
8778fae3551SRodney W. Grimes 	/* compute ICMP checksum here */
8788fae3551SRodney W. Grimes 	icp->icmp_cksum = in_cksum((u_short *)icp, cc);
8798fae3551SRodney W. Grimes 
8800b2f8b3fSMaxim Konovalov 	if (options & F_HDRINCL) {
8810b2f8b3fSMaxim Konovalov 		cc += sizeof(struct ip);
8820b2f8b3fSMaxim Konovalov 		ip = (struct ip *)outpackhdr;
8830b2f8b3fSMaxim Konovalov 		ip->ip_len = cc;
8840b2f8b3fSMaxim Konovalov 		ip->ip_sum = in_cksum((u_short *)outpackhdr, cc);
8850b2f8b3fSMaxim Konovalov 		packet = outpackhdr;
8860b2f8b3fSMaxim Konovalov 	}
8870b2f8b3fSMaxim Konovalov 	i = sendto(s, (char *)packet, cc, 0, (struct sockaddr *)&whereto,
888d389e86aSMatt Jacob 	    sizeof(whereto));
8898fae3551SRodney W. Grimes 
8908fae3551SRodney W. Grimes 	if (i < 0 || i != cc)  {
89143470e3bSGarrett Wollman 		if (i < 0) {
8928f975bb3SBruce Evans 			if (options & F_FLOOD && errno == ENOBUFS) {
893515dd2faSJulian Elischer 				usleep(FLOOD_BACKOFF);
894515dd2faSJulian Elischer 				return;
895515dd2faSJulian Elischer 			}
89643470e3bSGarrett Wollman 			warn("sendto");
89743470e3bSGarrett Wollman 		} else {
89843470e3bSGarrett Wollman 			warn("%s: partial write: %d of %d bytes",
899416aa49bSPoul-Henning Kamp 			     hostname, i, cc);
9008fae3551SRodney W. Grimes 		}
901363d7bbeSJulian Elischer 	}
902363d7bbeSJulian Elischer 	ntransmitted++;
9038fae3551SRodney W. Grimes 	if (!(options & F_QUIET) && options & F_FLOOD)
9048fae3551SRodney W. Grimes 		(void)write(STDOUT_FILENO, &DOT, 1);
9058fae3551SRodney W. Grimes }
9068fae3551SRodney W. Grimes 
9078fae3551SRodney W. Grimes /*
9088fae3551SRodney W. Grimes  * pr_pack --
9098fae3551SRodney W. Grimes  *	Print out the packet, if it came from us.  This logic is necessary
9108fae3551SRodney W. Grimes  * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
9118fae3551SRodney W. Grimes  * which arrive ('tis only fair).  This permits multiple copies of this
9128fae3551SRodney W. Grimes  * program to be run without having intermingled output (or statistics!).
9138fae3551SRodney W. Grimes  */
91443470e3bSGarrett Wollman static void
915039d6aa4SBill Fenner pr_pack(buf, cc, from, tv)
9168fae3551SRodney W. Grimes 	char *buf;
9178fae3551SRodney W. Grimes 	int cc;
9188fae3551SRodney W. Grimes 	struct sockaddr_in *from;
919039d6aa4SBill Fenner 	struct timeval *tv;
9208fae3551SRodney W. Grimes {
9219b219646SPeter Wemm 	struct in_addr ina;
9229b219646SPeter Wemm 	u_char *cp, *dp;
9233d438ad6SDavid E. O'Brien 	struct icmp *icp;
9248fae3551SRodney W. Grimes 	struct ip *ip;
9259d2b0ab8SPeter Wemm 	const void *tp;
9268f975bb3SBruce Evans 	double triptime;
927efc8588dSDavid E. O'Brien 	int dupflag, hlen, i, j, seq;
928efc8588dSDavid E. O'Brien 	static int old_rrlen;
929efc8588dSDavid E. O'Brien 	static char old_rr[MAX_IPOPTLEN];
9308fae3551SRodney W. Grimes 
9318fae3551SRodney W. Grimes 	/* Check the IP header */
9328fae3551SRodney W. Grimes 	ip = (struct ip *)buf;
9338fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
9348fae3551SRodney W. Grimes 	if (cc < hlen + ICMP_MINLEN) {
9358fae3551SRodney W. Grimes 		if (options & F_VERBOSE)
93643470e3bSGarrett Wollman 			warn("packet too short (%d bytes) from %s", cc,
93743470e3bSGarrett Wollman 			     inet_ntoa(from->sin_addr));
9388fae3551SRodney W. Grimes 		return;
9398fae3551SRodney W. Grimes 	}
9408fae3551SRodney W. Grimes 
9418fae3551SRodney W. Grimes 	/* Now the ICMP part */
9428fae3551SRodney W. Grimes 	cc -= hlen;
9438fae3551SRodney W. Grimes 	icp = (struct icmp *)(buf + hlen);
944eb1543c6SMatthew N. Dodd 	if (icp->icmp_type == icmp_type_rsp) {
9458fae3551SRodney W. Grimes 		if (icp->icmp_id != ident)
9468fae3551SRodney W. Grimes 			return;			/* 'Twas not our ECHO */
9478fae3551SRodney W. Grimes 		++nreceived;
9488f975bb3SBruce Evans 		triptime = 0.0;
9498fae3551SRodney W. Grimes 		if (timing) {
950d32ff037SJohn Birrell 			struct timeval tv1;
9518fae3551SRodney W. Grimes #ifndef icmp_data
9529d2b0ab8SPeter Wemm 			tp = &icp->icmp_ip;
9538fae3551SRodney W. Grimes #else
9549d2b0ab8SPeter Wemm 			tp = icp->icmp_data;
9558fae3551SRodney W. Grimes #endif
956d829c3dfSMatthew N. Dodd 			tp += phdr_len;
957143008a1SMatthew N. Dodd 
95847e9b3eaSMatthew N. Dodd 			if (cc - ICMP_MINLEN - phdr_len >= sizeof(tv1)) {
9599d2b0ab8SPeter Wemm 				/* Copy to avoid alignment problems: */
9609d2b0ab8SPeter Wemm 				memcpy(&tv1, tp, sizeof(tv1));
961039d6aa4SBill Fenner 				tvsub(tv, &tv1);
962039d6aa4SBill Fenner  				triptime = ((double)tv->tv_sec) * 1000.0 +
963039d6aa4SBill Fenner  				    ((double)tv->tv_usec) / 1000.0;
9648fae3551SRodney W. Grimes 				tsum += triptime;
9653109a910SGarrett Wollman 				tsumsq += triptime * triptime;
9668fae3551SRodney W. Grimes 				if (triptime < tmin)
9678fae3551SRodney W. Grimes 					tmin = triptime;
9688fae3551SRodney W. Grimes 				if (triptime > tmax)
9698fae3551SRodney W. Grimes 					tmax = triptime;
97047e9b3eaSMatthew N. Dodd 			} else
97147e9b3eaSMatthew N. Dodd 				timing = 0;
9728fae3551SRodney W. Grimes 		}
9738fae3551SRodney W. Grimes 
9745db89bc7SBill Fenner 		seq = ntohs(icp->icmp_seq);
9755db89bc7SBill Fenner 
9765db89bc7SBill Fenner 		if (TST(seq % mx_dup_ck)) {
9778fae3551SRodney W. Grimes 			++nrepeats;
9788fae3551SRodney W. Grimes 			--nreceived;
9798fae3551SRodney W. Grimes 			dupflag = 1;
9808fae3551SRodney W. Grimes 		} else {
9815db89bc7SBill Fenner 			SET(seq % mx_dup_ck);
9828fae3551SRodney W. Grimes 			dupflag = 0;
9838fae3551SRodney W. Grimes 		}
9848fae3551SRodney W. Grimes 
9858fae3551SRodney W. Grimes 		if (options & F_QUIET)
9868fae3551SRodney W. Grimes 			return;
9878fae3551SRodney W. Grimes 
9888fae3551SRodney W. Grimes 		if (options & F_FLOOD)
9898fae3551SRodney W. Grimes 			(void)write(STDOUT_FILENO, &BSPACE, 1);
9908fae3551SRodney W. Grimes 		else {
9918fae3551SRodney W. Grimes 			(void)printf("%d bytes from %s: icmp_seq=%u", cc,
9928fae3551SRodney W. Grimes 			   inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr),
9935db89bc7SBill Fenner 			   seq);
9948fae3551SRodney W. Grimes 			(void)printf(" ttl=%d", ip->ip_ttl);
9958fae3551SRodney W. Grimes 			if (timing)
996d410b6f1SDavid Greenman 				(void)printf(" time=%.3f ms", triptime);
9978fae3551SRodney W. Grimes 			if (dupflag)
9988fae3551SRodney W. Grimes 				(void)printf(" (DUP!)");
999772dfa72SDaniel O'Callaghan 			if (options & F_AUDIBLE)
1000ca517ad8SPoul-Henning Kamp 				(void)write(STDOUT_FILENO, &BBELL, 1);
1001143008a1SMatthew N. Dodd 			if (options & F_MASK) {
1002143008a1SMatthew N. Dodd 				/* Just prentend this cast isn't ugly */
1003143008a1SMatthew N. Dodd 				(void)printf(" mask=%s",
1004143008a1SMatthew N. Dodd 					pr_addr(*(struct in_addr *)&(icp->icmp_mask)));
1005143008a1SMatthew N. Dodd 			}
1006eb1543c6SMatthew N. Dodd 			if (options & F_TIME) {
1007eb1543c6SMatthew N. Dodd 				(void)printf(" tso=%s", pr_ntime(icp->icmp_otime));
1008eb1543c6SMatthew N. Dodd 				(void)printf(" tsr=%s", pr_ntime(icp->icmp_rtime));
1009eb1543c6SMatthew N. Dodd 				(void)printf(" tst=%s", pr_ntime(icp->icmp_ttime));
1010eb1543c6SMatthew N. Dodd 			}
10118fae3551SRodney W. Grimes 			/* check the data */
1012eb1543c6SMatthew N. Dodd 			cp = (u_char*)&icp->icmp_data[phdr_len];
1013eb1543c6SMatthew N. Dodd 			dp = &outpack[MINICMPLEN + phdr_len];
101447e9b3eaSMatthew N. Dodd 			cc -= ICMP_MINLEN + phdr_len;
101529dccd6aSMaxim Konovalov 			i = 0;
101629dccd6aSMaxim Konovalov 			if (timing) {   /* don't check variable timestamp */
101729dccd6aSMaxim Konovalov 				cp += TIMEVAL_LEN;
101829dccd6aSMaxim Konovalov 				dp += TIMEVAL_LEN;
101929dccd6aSMaxim Konovalov 				cc -= TIMEVAL_LEN;
102029dccd6aSMaxim Konovalov 				i += TIMEVAL_LEN;
102129dccd6aSMaxim Konovalov 			}
102229dccd6aSMaxim Konovalov 			for (; i < datalen && cc > 0; ++i, ++cp, ++dp, --cc) {
10238fae3551SRodney W. Grimes 				if (*cp != *dp) {
10248fae3551SRodney W. Grimes 	(void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x",
10258fae3551SRodney W. Grimes 	    i, *dp, *cp);
10261ad0b1beSMaxim Konovalov 					(void)printf("\ncp:");
10278fae3551SRodney W. Grimes 					cp = (u_char*)&icp->icmp_data[0];
1028d32ff037SJohn Birrell 					for (i = 0; i < datalen; ++i, ++cp) {
1029d32ff037SJohn Birrell 						if ((i % 32) == 8)
1030d32ff037SJohn Birrell 							(void)printf("\n\t");
1031d32ff037SJohn Birrell 						(void)printf("%x ", *cp);
1032d32ff037SJohn Birrell 					}
10331ad0b1beSMaxim Konovalov 					(void)printf("\ndp:");
10344fba6582SMaxim Konovalov 					cp = &outpack[MINICMPLEN];
1035d32ff037SJohn Birrell 					for (i = 0; i < datalen; ++i, ++cp) {
10368fae3551SRodney W. Grimes 						if ((i % 32) == 8)
10378fae3551SRodney W. Grimes 							(void)printf("\n\t");
10388fae3551SRodney W. Grimes 						(void)printf("%x ", *cp);
10398fae3551SRodney W. Grimes 					}
10408fae3551SRodney W. Grimes 					break;
10418fae3551SRodney W. Grimes 				}
10428fae3551SRodney W. Grimes 			}
10438fae3551SRodney W. Grimes 		}
10448fae3551SRodney W. Grimes 	} else {
1045ef9e6dc7SBill Fenner 		/*
1046ef9e6dc7SBill Fenner 		 * We've got something other than an ECHOREPLY.
1047ef9e6dc7SBill Fenner 		 * See if it's a reply to something that we sent.
1048ef9e6dc7SBill Fenner 		 * We can compare IP destination, protocol,
1049ef9e6dc7SBill Fenner 		 * and ICMP type and ID.
1050f78ac61bSWarner Losh 		 *
1051f78ac61bSWarner Losh 		 * Only print all the error messages if we are running
1052f78ac61bSWarner Losh 		 * as root to avoid leaking information not normally
1053f78ac61bSWarner Losh 		 * available to those not running as root.
1054ef9e6dc7SBill Fenner 		 */
1055ef9e6dc7SBill Fenner #ifndef icmp_data
1056ef9e6dc7SBill Fenner 		struct ip *oip = &icp->icmp_ip;
1057ef9e6dc7SBill Fenner #else
1058ef9e6dc7SBill Fenner 		struct ip *oip = (struct ip *)icp->icmp_data;
1059ef9e6dc7SBill Fenner #endif
1060ef9e6dc7SBill Fenner 		struct icmp *oicmp = (struct icmp *)(oip + 1);
1061ef9e6dc7SBill Fenner 
1062ee2bf734SWarner Losh 		if (((options & F_VERBOSE) && uid == 0) ||
1063ef9e6dc7SBill Fenner 		    (!(options & F_QUIET2) &&
1064d389e86aSMatt Jacob 		     (oip->ip_dst.s_addr == whereto.sin_addr.s_addr) &&
1065ef9e6dc7SBill Fenner 		     (oip->ip_p == IPPROTO_ICMP) &&
1066ef9e6dc7SBill Fenner 		     (oicmp->icmp_type == ICMP_ECHO) &&
1067ef9e6dc7SBill Fenner 		     (oicmp->icmp_id == ident))) {
10688fae3551SRodney W. Grimes 		    (void)printf("%d bytes from %s: ", cc,
106943470e3bSGarrett Wollman 			pr_addr(from->sin_addr));
10708fae3551SRodney W. Grimes 		    pr_icmph(icp);
1071ef9e6dc7SBill Fenner 		} else
1072ef9e6dc7SBill Fenner 		    return;
10738fae3551SRodney W. Grimes 	}
10748fae3551SRodney W. Grimes 
10758fae3551SRodney W. Grimes 	/* Display any IP options */
10768fae3551SRodney W. Grimes 	cp = (u_char *)buf + sizeof(struct ip);
10778fae3551SRodney W. Grimes 
10788fae3551SRodney W. Grimes 	for (; hlen > (int)sizeof(struct ip); --hlen, ++cp)
10798fae3551SRodney W. Grimes 		switch (*cp) {
10808fae3551SRodney W. Grimes 		case IPOPT_EOL:
10818fae3551SRodney W. Grimes 			hlen = 0;
10828fae3551SRodney W. Grimes 			break;
10838fae3551SRodney W. Grimes 		case IPOPT_LSRR:
1084cb75aca7SMaxim Konovalov 		case IPOPT_SSRR:
1085cb75aca7SMaxim Konovalov 			(void)printf(*cp == IPOPT_LSRR ?
1086cb75aca7SMaxim Konovalov 			    "\nLSRR: " : "\nSSRR: ");
1087301207dfSMaxim Konovalov 			j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1;
10888fae3551SRodney W. Grimes 			hlen -= 2;
1089301207dfSMaxim Konovalov 			cp += 2;
10903c721ab3SMaxim Konovalov 			if (j >= INADDR_LEN &&
10913c721ab3SMaxim Konovalov 			    j <= hlen - (int)sizeof(struct ip)) {
10928fae3551SRodney W. Grimes 				for (;;) {
1093301207dfSMaxim Konovalov 					bcopy(++cp, &ina.s_addr, INADDR_LEN);
1094301207dfSMaxim Konovalov 					if (ina.s_addr == 0)
10951ad0b1beSMaxim Konovalov 						(void)printf("\t0.0.0.0");
1096301207dfSMaxim Konovalov 					else
10971ad0b1beSMaxim Konovalov 						(void)printf("\t%s",
10981ad0b1beSMaxim Konovalov 						     pr_addr(ina));
1099301207dfSMaxim Konovalov 					hlen -= INADDR_LEN;
1100301207dfSMaxim Konovalov 					cp += INADDR_LEN - 1;
1101301207dfSMaxim Konovalov 					j -= INADDR_LEN;
1102301207dfSMaxim Konovalov 					if (j < INADDR_LEN)
11038fae3551SRodney W. Grimes 						break;
11048fae3551SRodney W. Grimes 					(void)putchar('\n');
11058fae3551SRodney W. Grimes 				}
1106301207dfSMaxim Konovalov 			} else
1107301207dfSMaxim Konovalov 				(void)printf("\t(truncated route)\n");
11088fae3551SRodney W. Grimes 			break;
11098fae3551SRodney W. Grimes 		case IPOPT_RR:
1110301207dfSMaxim Konovalov 			j = cp[IPOPT_OLEN];		/* get length */
1111301207dfSMaxim Konovalov 			i = cp[IPOPT_OFFSET];		/* and pointer */
11128fae3551SRodney W. Grimes 			hlen -= 2;
1113301207dfSMaxim Konovalov 			cp += 2;
11148fae3551SRodney W. Grimes 			if (i > j)
11158fae3551SRodney W. Grimes 				i = j;
1116301207dfSMaxim Konovalov 			i = i - IPOPT_MINOFF + 1;
1117301207dfSMaxim Konovalov 			if (i < 0 || i > (hlen - (int)sizeof(struct ip))) {
1118301207dfSMaxim Konovalov 				old_rrlen = 0;
11198fae3551SRodney W. Grimes 				continue;
1120301207dfSMaxim Konovalov 			}
11218fae3551SRodney W. Grimes 			if (i == old_rrlen
11228fae3551SRodney W. Grimes 			    && !bcmp((char *)cp, old_rr, i)
11238fae3551SRodney W. Grimes 			    && !(options & F_FLOOD)) {
11248fae3551SRodney W. Grimes 				(void)printf("\t(same route)");
11258fae3551SRodney W. Grimes 				hlen -= i;
11268fae3551SRodney W. Grimes 				cp += i;
11278fae3551SRodney W. Grimes 				break;
11288fae3551SRodney W. Grimes 			}
11298fae3551SRodney W. Grimes 			old_rrlen = i;
11308fae3551SRodney W. Grimes 			bcopy((char *)cp, old_rr, i);
11318fae3551SRodney W. Grimes 			(void)printf("\nRR: ");
1132301207dfSMaxim Konovalov 			if (i >= INADDR_LEN &&
1133301207dfSMaxim Konovalov 			    i <= hlen - (int)sizeof(struct ip)) {
11348fae3551SRodney W. Grimes 				for (;;) {
1135301207dfSMaxim Konovalov 					bcopy(++cp, &ina.s_addr, INADDR_LEN);
1136301207dfSMaxim Konovalov 					if (ina.s_addr == 0)
11371ad0b1beSMaxim Konovalov 						(void)printf("\t0.0.0.0");
1138301207dfSMaxim Konovalov 					else
1139301207dfSMaxim Konovalov 						(void)printf("\t%s",
1140301207dfSMaxim Konovalov 						     pr_addr(ina));
1141301207dfSMaxim Konovalov 					hlen -= INADDR_LEN;
1142301207dfSMaxim Konovalov 					cp += INADDR_LEN - 1;
1143301207dfSMaxim Konovalov 					i -= INADDR_LEN;
1144301207dfSMaxim Konovalov 					if (i < INADDR_LEN)
11458fae3551SRodney W. Grimes 						break;
11468fae3551SRodney W. Grimes 					(void)putchar('\n');
11478fae3551SRodney W. Grimes 				}
1148301207dfSMaxim Konovalov 			} else
1149301207dfSMaxim Konovalov 				(void)printf("\t(truncated route)");
11508fae3551SRodney W. Grimes 			break;
11518fae3551SRodney W. Grimes 		case IPOPT_NOP:
11528fae3551SRodney W. Grimes 			(void)printf("\nNOP");
11538fae3551SRodney W. Grimes 			break;
11548fae3551SRodney W. Grimes 		default:
11558fae3551SRodney W. Grimes 			(void)printf("\nunknown option %x", *cp);
11568fae3551SRodney W. Grimes 			break;
11578fae3551SRodney W. Grimes 		}
11588fae3551SRodney W. Grimes 	if (!(options & F_FLOOD)) {
11598fae3551SRodney W. Grimes 		(void)putchar('\n');
11608fae3551SRodney W. Grimes 		(void)fflush(stdout);
11618fae3551SRodney W. Grimes 	}
11628fae3551SRodney W. Grimes }
11638fae3551SRodney W. Grimes 
11648fae3551SRodney W. Grimes /*
11658fae3551SRodney W. Grimes  * in_cksum --
11668fae3551SRodney W. Grimes  *	Checksum routine for Internet Protocol family headers (C Version)
11678fae3551SRodney W. Grimes  */
116843470e3bSGarrett Wollman u_short
11698fae3551SRodney W. Grimes in_cksum(addr, len)
11708fae3551SRodney W. Grimes 	u_short *addr;
11718fae3551SRodney W. Grimes 	int len;
11728fae3551SRodney W. Grimes {
1173efc8588dSDavid E. O'Brien 	int nleft, sum;
1174efc8588dSDavid E. O'Brien 	u_short *w;
11753ec12efeSPierre Beyssac 	union {
117609333e78SPierre Beyssac 		u_short	us;
117709333e78SPierre Beyssac 		u_char	uc[2];
117809333e78SPierre Beyssac 	} last;
117909333e78SPierre Beyssac 	u_short answer;
11808fae3551SRodney W. Grimes 
1181efc8588dSDavid E. O'Brien 	nleft = len;
1182efc8588dSDavid E. O'Brien 	sum = 0;
1183efc8588dSDavid E. O'Brien 	w = addr;
1184efc8588dSDavid E. O'Brien 
11858fae3551SRodney W. Grimes 	/*
11868fae3551SRodney W. Grimes 	 * Our algorithm is simple, using a 32 bit accumulator (sum), we add
11878fae3551SRodney W. Grimes 	 * sequential 16 bit words to it, and at the end, fold back all the
11888fae3551SRodney W. Grimes 	 * carry bits from the top 16 bits into the lower 16 bits.
11898fae3551SRodney W. Grimes 	 */
11908fae3551SRodney W. Grimes 	while (nleft > 1)  {
11918fae3551SRodney W. Grimes 		sum += *w++;
11928fae3551SRodney W. Grimes 		nleft -= 2;
11938fae3551SRodney W. Grimes 	}
11948fae3551SRodney W. Grimes 
11958fae3551SRodney W. Grimes 	/* mop up an odd byte, if necessary */
11968fae3551SRodney W. Grimes 	if (nleft == 1) {
119709333e78SPierre Beyssac 		last.uc[0] = *(u_char *)w;
119809333e78SPierre Beyssac 		last.uc[1] = 0;
119909333e78SPierre Beyssac 		sum += last.us;
12008fae3551SRodney W. Grimes 	}
12018fae3551SRodney W. Grimes 
12028fae3551SRodney W. Grimes 	/* add back carry outs from top 16 bits to low 16 bits */
12038fae3551SRodney W. Grimes 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
12048fae3551SRodney W. Grimes 	sum += (sum >> 16);			/* add carry */
120509333e78SPierre Beyssac 	answer = ~sum;				/* truncate to 16 bits */
120609333e78SPierre Beyssac 	return(answer);
12078fae3551SRodney W. Grimes }
12088fae3551SRodney W. Grimes 
12098fae3551SRodney W. Grimes /*
12108fae3551SRodney W. Grimes  * tvsub --
12118fae3551SRodney W. Grimes  *	Subtract 2 timeval structs:  out = out - in.  Out is assumed to
12128fae3551SRodney W. Grimes  * be >= in.
12138fae3551SRodney W. Grimes  */
121443470e3bSGarrett Wollman static void
12158fae3551SRodney W. Grimes tvsub(out, in)
12163d438ad6SDavid E. O'Brien 	struct timeval *out, *in;
12178fae3551SRodney W. Grimes {
1218efc8588dSDavid E. O'Brien 
12198fae3551SRodney W. Grimes 	if ((out->tv_usec -= in->tv_usec) < 0) {
12208fae3551SRodney W. Grimes 		--out->tv_sec;
12218fae3551SRodney W. Grimes 		out->tv_usec += 1000000;
12228fae3551SRodney W. Grimes 	}
12238fae3551SRodney W. Grimes 	out->tv_sec -= in->tv_sec;
12248fae3551SRodney W. Grimes }
12258fae3551SRodney W. Grimes 
12268fae3551SRodney W. Grimes /*
1227badd8138SSean Eric Fagan  * status --
1228badd8138SSean Eric Fagan  *	Print out statistics when SIGINFO is received.
1229badd8138SSean Eric Fagan  */
1230badd8138SSean Eric Fagan 
123143470e3bSGarrett Wollman static void
12327d81b35cSBruce Evans status(sig)
1233c6facae4SMaxim Konovalov 	int sig __unused;
12347d81b35cSBruce Evans {
1235efc8588dSDavid E. O'Brien 
123637e5b2c6SSean Eric Fagan 	siginfo_p = 1;
123737e5b2c6SSean Eric Fagan }
123837e5b2c6SSean Eric Fagan 
123943470e3bSGarrett Wollman static void
124037e5b2c6SSean Eric Fagan check_status()
1241badd8138SSean Eric Fagan {
1242efc8588dSDavid E. O'Brien 
124337e5b2c6SSean Eric Fagan 	if (siginfo_p) {
124437e5b2c6SSean Eric Fagan 		siginfo_p = 0;
12457d81b35cSBruce Evans 		(void)fprintf(stderr,
12467d81b35cSBruce Evans 	"\r%ld/%ld packets received (%.0f%%) %.3f min / %.3f avg / %.3f max\n",
12477d81b35cSBruce Evans 		    nreceived, ntransmitted,
12487d81b35cSBruce Evans 		    ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0,
12497d81b35cSBruce Evans 		    nreceived ? tmin : 0.0,
12507d81b35cSBruce Evans 		    nreceived + nrepeats ? tsum / (nreceived + nrepeats) : tsum,
12517d81b35cSBruce Evans 		    tmax);
125237e5b2c6SSean Eric Fagan 	}
1253badd8138SSean Eric Fagan }
1254badd8138SSean Eric Fagan 
1255badd8138SSean Eric Fagan /*
12568fae3551SRodney W. Grimes  * finish --
12578fae3551SRodney W. Grimes  *	Print out statistics, and give up.
12588fae3551SRodney W. Grimes  */
125943470e3bSGarrett Wollman static void
12608f975bb3SBruce Evans finish()
12618fae3551SRodney W. Grimes {
1262badd8138SSean Eric Fagan 	struct termios ts;
12638fae3551SRodney W. Grimes 
12648fae3551SRodney W. Grimes 	(void)signal(SIGINT, SIG_IGN);
1265a2a00888SSean Eric Fagan 	(void)signal(SIGALRM, SIG_IGN);
12668fae3551SRodney W. Grimes 	(void)putchar('\n');
12678fae3551SRodney W. Grimes 	(void)fflush(stdout);
12688fae3551SRodney W. Grimes 	(void)printf("--- %s ping statistics ---\n", hostname);
12698fae3551SRodney W. Grimes 	(void)printf("%ld packets transmitted, ", ntransmitted);
12708fae3551SRodney W. Grimes 	(void)printf("%ld packets received, ", nreceived);
12718fae3551SRodney W. Grimes 	if (nrepeats)
12728fae3551SRodney W. Grimes 		(void)printf("+%ld duplicates, ", nrepeats);
1273ebe70c8fSWarner Losh 	if (ntransmitted) {
12748fae3551SRodney W. Grimes 		if (nreceived > ntransmitted)
12758fae3551SRodney W. Grimes 			(void)printf("-- somebody's printing up packets!");
12768fae3551SRodney W. Grimes 		else
12778fae3551SRodney W. Grimes 			(void)printf("%d%% packet loss",
12788fae3551SRodney W. Grimes 			    (int)(((ntransmitted - nreceived) * 100) /
12798fae3551SRodney W. Grimes 			    ntransmitted));
1280ebe70c8fSWarner Losh 	}
12818fae3551SRodney W. Grimes 	(void)putchar('\n');
12823109a910SGarrett Wollman 	if (nreceived && timing) {
12833109a910SGarrett Wollman 		double n = nreceived + nrepeats;
12843109a910SGarrett Wollman 		double avg = tsum / n;
12853109a910SGarrett Wollman 		double vari = tsumsq / n - avg * avg;
12861ad0b1beSMaxim Konovalov 		(void)printf(
12871ad0b1beSMaxim Konovalov 		    "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n",
12883109a910SGarrett Wollman 		    tmin, avg, tmax, sqrt(vari));
12893109a910SGarrett Wollman 	}
12902ceaae21SBruce Evans 	if (reset_kerninfo && tcgetattr(STDOUT_FILENO, &ts) != -1) {
1291badd8138SSean Eric Fagan 		ts.c_lflag &= ~NOKERNINFO;
12922ceaae21SBruce Evans 		tcsetattr(STDOUT_FILENO, TCSANOW, &ts);
1293badd8138SSean Eric Fagan 	}
1294badd8138SSean Eric Fagan 
12956e1173dcSDavid Greenman 	if (nreceived)
12968fae3551SRodney W. Grimes 		exit(0);
12976e1173dcSDavid Greenman 	else
12986e1173dcSDavid Greenman 		exit(2);
12998fae3551SRodney W. Grimes }
13008fae3551SRodney W. Grimes 
13018fae3551SRodney W. Grimes #ifdef notdef
13028fae3551SRodney W. Grimes static char *ttab[] = {
13038fae3551SRodney W. Grimes 	"Echo Reply",		/* ip + seq + udata */
13048fae3551SRodney W. Grimes 	"Dest Unreachable",	/* net, host, proto, port, frag, sr + IP */
13058fae3551SRodney W. Grimes 	"Source Quench",	/* IP */
13068fae3551SRodney W. Grimes 	"Redirect",		/* redirect type, gateway, + IP  */
13078fae3551SRodney W. Grimes 	"Echo",
13088fae3551SRodney W. Grimes 	"Time Exceeded",	/* transit, frag reassem + IP */
13098fae3551SRodney W. Grimes 	"Parameter Problem",	/* pointer + IP */
13108fae3551SRodney W. Grimes 	"Timestamp",		/* id + seq + three timestamps */
13118fae3551SRodney W. Grimes 	"Timestamp Reply",	/* " */
13128fae3551SRodney W. Grimes 	"Info Request",		/* id + sq */
13138fae3551SRodney W. Grimes 	"Info Reply"		/* " */
13148fae3551SRodney W. Grimes };
13158fae3551SRodney W. Grimes #endif
13168fae3551SRodney W. Grimes 
13178fae3551SRodney W. Grimes /*
13188fae3551SRodney W. Grimes  * pr_icmph --
13198fae3551SRodney W. Grimes  *	Print a descriptive string about an ICMP header.
13208fae3551SRodney W. Grimes  */
132143470e3bSGarrett Wollman static void
13228fae3551SRodney W. Grimes pr_icmph(icp)
13238fae3551SRodney W. Grimes 	struct icmp *icp;
13248fae3551SRodney W. Grimes {
1325efc8588dSDavid E. O'Brien 
13268fae3551SRodney W. Grimes 	switch(icp->icmp_type) {
13278fae3551SRodney W. Grimes 	case ICMP_ECHOREPLY:
13288fae3551SRodney W. Grimes 		(void)printf("Echo Reply\n");
13298fae3551SRodney W. Grimes 		/* XXX ID + Seq + Data */
13308fae3551SRodney W. Grimes 		break;
13318fae3551SRodney W. Grimes 	case ICMP_UNREACH:
13328fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
13338fae3551SRodney W. Grimes 		case ICMP_UNREACH_NET:
13348fae3551SRodney W. Grimes 			(void)printf("Destination Net Unreachable\n");
13358fae3551SRodney W. Grimes 			break;
13368fae3551SRodney W. Grimes 		case ICMP_UNREACH_HOST:
13378fae3551SRodney W. Grimes 			(void)printf("Destination Host Unreachable\n");
13388fae3551SRodney W. Grimes 			break;
13398fae3551SRodney W. Grimes 		case ICMP_UNREACH_PROTOCOL:
13408fae3551SRodney W. Grimes 			(void)printf("Destination Protocol Unreachable\n");
13418fae3551SRodney W. Grimes 			break;
13428fae3551SRodney W. Grimes 		case ICMP_UNREACH_PORT:
13438fae3551SRodney W. Grimes 			(void)printf("Destination Port Unreachable\n");
13448fae3551SRodney W. Grimes 			break;
13458fae3551SRodney W. Grimes 		case ICMP_UNREACH_NEEDFRAG:
1346ef9e6dc7SBill Fenner 			(void)printf("frag needed and DF set (MTU %d)\n",
1347ff49597eSBill Fenner 					ntohs(icp->icmp_nextmtu));
13488fae3551SRodney W. Grimes 			break;
13498fae3551SRodney W. Grimes 		case ICMP_UNREACH_SRCFAIL:
13508fae3551SRodney W. Grimes 			(void)printf("Source Route Failed\n");
13518fae3551SRodney W. Grimes 			break;
1352ef9e6dc7SBill Fenner 		case ICMP_UNREACH_FILTER_PROHIB:
1353ef9e6dc7SBill Fenner 			(void)printf("Communication prohibited by filter\n");
1354ef9e6dc7SBill Fenner 			break;
13558fae3551SRodney W. Grimes 		default:
13568fae3551SRodney W. Grimes 			(void)printf("Dest Unreachable, Bad Code: %d\n",
13578fae3551SRodney W. Grimes 			    icp->icmp_code);
13588fae3551SRodney W. Grimes 			break;
13598fae3551SRodney W. Grimes 		}
13608fae3551SRodney W. Grimes 		/* Print returned IP header information */
13618fae3551SRodney W. Grimes #ifndef icmp_data
13628fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
13638fae3551SRodney W. Grimes #else
13648fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
13658fae3551SRodney W. Grimes #endif
13668fae3551SRodney W. Grimes 		break;
13678fae3551SRodney W. Grimes 	case ICMP_SOURCEQUENCH:
13688fae3551SRodney W. Grimes 		(void)printf("Source Quench\n");
13698fae3551SRodney W. Grimes #ifndef icmp_data
13708fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
13718fae3551SRodney W. Grimes #else
13728fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
13738fae3551SRodney W. Grimes #endif
13748fae3551SRodney W. Grimes 		break;
13758fae3551SRodney W. Grimes 	case ICMP_REDIRECT:
13768fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
13778fae3551SRodney W. Grimes 		case ICMP_REDIRECT_NET:
13788fae3551SRodney W. Grimes 			(void)printf("Redirect Network");
13798fae3551SRodney W. Grimes 			break;
13808fae3551SRodney W. Grimes 		case ICMP_REDIRECT_HOST:
13818fae3551SRodney W. Grimes 			(void)printf("Redirect Host");
13828fae3551SRodney W. Grimes 			break;
13838fae3551SRodney W. Grimes 		case ICMP_REDIRECT_TOSNET:
13848fae3551SRodney W. Grimes 			(void)printf("Redirect Type of Service and Network");
13858fae3551SRodney W. Grimes 			break;
13868fae3551SRodney W. Grimes 		case ICMP_REDIRECT_TOSHOST:
13878fae3551SRodney W. Grimes 			(void)printf("Redirect Type of Service and Host");
13888fae3551SRodney W. Grimes 			break;
13898fae3551SRodney W. Grimes 		default:
13908fae3551SRodney W. Grimes 			(void)printf("Redirect, Bad Code: %d", icp->icmp_code);
13918fae3551SRodney W. Grimes 			break;
13928fae3551SRodney W. Grimes 		}
1393ff49597eSBill Fenner 		(void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr));
13948fae3551SRodney W. Grimes #ifndef icmp_data
13958fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
13968fae3551SRodney W. Grimes #else
13978fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
13988fae3551SRodney W. Grimes #endif
13998fae3551SRodney W. Grimes 		break;
14008fae3551SRodney W. Grimes 	case ICMP_ECHO:
14018fae3551SRodney W. Grimes 		(void)printf("Echo Request\n");
14028fae3551SRodney W. Grimes 		/* XXX ID + Seq + Data */
14038fae3551SRodney W. Grimes 		break;
14048fae3551SRodney W. Grimes 	case ICMP_TIMXCEED:
14058fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
14068fae3551SRodney W. Grimes 		case ICMP_TIMXCEED_INTRANS:
14078fae3551SRodney W. Grimes 			(void)printf("Time to live exceeded\n");
14088fae3551SRodney W. Grimes 			break;
14098fae3551SRodney W. Grimes 		case ICMP_TIMXCEED_REASS:
14108fae3551SRodney W. Grimes 			(void)printf("Frag reassembly time exceeded\n");
14118fae3551SRodney W. Grimes 			break;
14128fae3551SRodney W. Grimes 		default:
14138fae3551SRodney W. Grimes 			(void)printf("Time exceeded, Bad Code: %d\n",
14148fae3551SRodney W. Grimes 			    icp->icmp_code);
14158fae3551SRodney W. Grimes 			break;
14168fae3551SRodney W. Grimes 		}
14178fae3551SRodney W. Grimes #ifndef icmp_data
14188fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
14198fae3551SRodney W. Grimes #else
14208fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
14218fae3551SRodney W. Grimes #endif
14228fae3551SRodney W. Grimes 		break;
14238fae3551SRodney W. Grimes 	case ICMP_PARAMPROB:
14248fae3551SRodney W. Grimes 		(void)printf("Parameter problem: pointer = 0x%02x\n",
14258fae3551SRodney W. Grimes 		    icp->icmp_hun.ih_pptr);
14268fae3551SRodney W. Grimes #ifndef icmp_data
14278fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
14288fae3551SRodney W. Grimes #else
14298fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
14308fae3551SRodney W. Grimes #endif
14318fae3551SRodney W. Grimes 		break;
14328fae3551SRodney W. Grimes 	case ICMP_TSTAMP:
14338fae3551SRodney W. Grimes 		(void)printf("Timestamp\n");
14348fae3551SRodney W. Grimes 		/* XXX ID + Seq + 3 timestamps */
14358fae3551SRodney W. Grimes 		break;
14368fae3551SRodney W. Grimes 	case ICMP_TSTAMPREPLY:
14378fae3551SRodney W. Grimes 		(void)printf("Timestamp Reply\n");
14388fae3551SRodney W. Grimes 		/* XXX ID + Seq + 3 timestamps */
14398fae3551SRodney W. Grimes 		break;
14408fae3551SRodney W. Grimes 	case ICMP_IREQ:
14418fae3551SRodney W. Grimes 		(void)printf("Information Request\n");
14428fae3551SRodney W. Grimes 		/* XXX ID + Seq */
14438fae3551SRodney W. Grimes 		break;
14448fae3551SRodney W. Grimes 	case ICMP_IREQREPLY:
14458fae3551SRodney W. Grimes 		(void)printf("Information Reply\n");
14468fae3551SRodney W. Grimes 		/* XXX ID + Seq */
14478fae3551SRodney W. Grimes 		break;
14488fae3551SRodney W. Grimes 	case ICMP_MASKREQ:
14498fae3551SRodney W. Grimes 		(void)printf("Address Mask Request\n");
14508fae3551SRodney W. Grimes 		break;
14518fae3551SRodney W. Grimes 	case ICMP_MASKREPLY:
14528fae3551SRodney W. Grimes 		(void)printf("Address Mask Reply\n");
14538fae3551SRodney W. Grimes 		break;
1454ef9e6dc7SBill Fenner 	case ICMP_ROUTERADVERT:
1455ef9e6dc7SBill Fenner 		(void)printf("Router Advertisement\n");
1456ef9e6dc7SBill Fenner 		break;
1457ef9e6dc7SBill Fenner 	case ICMP_ROUTERSOLICIT:
1458ef9e6dc7SBill Fenner 		(void)printf("Router Solicitation\n");
1459ef9e6dc7SBill Fenner 		break;
14608fae3551SRodney W. Grimes 	default:
14618fae3551SRodney W. Grimes 		(void)printf("Bad ICMP type: %d\n", icp->icmp_type);
14628fae3551SRodney W. Grimes 	}
14638fae3551SRodney W. Grimes }
14648fae3551SRodney W. Grimes 
14658fae3551SRodney W. Grimes /*
14668fae3551SRodney W. Grimes  * pr_iph --
14678fae3551SRodney W. Grimes  *	Print an IP header with options.
14688fae3551SRodney W. Grimes  */
146943470e3bSGarrett Wollman static void
14708fae3551SRodney W. Grimes pr_iph(ip)
14718fae3551SRodney W. Grimes 	struct ip *ip;
14728fae3551SRodney W. Grimes {
14738fae3551SRodney W. Grimes 	u_char *cp;
1474efc8588dSDavid E. O'Brien 	int hlen;
14758fae3551SRodney W. Grimes 
14768fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
14778fae3551SRodney W. Grimes 	cp = (u_char *)ip + 20;		/* point to options */
14788fae3551SRodney W. Grimes 
1479ef9e6dc7SBill Fenner 	(void)printf("Vr HL TOS  Len   ID Flg  off TTL Pro  cks      Src      Dst\n");
14808fae3551SRodney W. Grimes 	(void)printf(" %1x  %1x  %02x %04x %04x",
1481ef9e6dc7SBill Fenner 	    ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len),
1482ef9e6dc7SBill Fenner 	    ntohs(ip->ip_id));
1483d32ff037SJohn Birrell 	(void)printf("   %1lx %04lx",
1484d32ff037SJohn Birrell 	    (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13,
1485d32ff037SJohn Birrell 	    (u_long) ntohl(ip->ip_off) & 0x1fff);
1486ef9e6dc7SBill Fenner 	(void)printf("  %02x  %02x %04x", ip->ip_ttl, ip->ip_p,
1487ef9e6dc7SBill Fenner 							    ntohs(ip->ip_sum));
14888fae3551SRodney W. Grimes 	(void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr));
14898fae3551SRodney W. Grimes 	(void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr));
1490ef9e6dc7SBill Fenner 	/* dump any option bytes */
14918fae3551SRodney W. Grimes 	while (hlen-- > 20) {
14928fae3551SRodney W. Grimes 		(void)printf("%02x", *cp++);
14938fae3551SRodney W. Grimes 	}
14948fae3551SRodney W. Grimes 	(void)putchar('\n');
14958fae3551SRodney W. Grimes }
14968fae3551SRodney W. Grimes 
14978fae3551SRodney W. Grimes /*
14988fae3551SRodney W. Grimes  * pr_addr --
14998fae3551SRodney W. Grimes  *	Return an ascii host address as a dotted quad and optionally with
15008fae3551SRodney W. Grimes  * a hostname.
15018fae3551SRodney W. Grimes  */
150243470e3bSGarrett Wollman static char *
150343470e3bSGarrett Wollman pr_addr(ina)
150443470e3bSGarrett Wollman 	struct in_addr ina;
15058fae3551SRodney W. Grimes {
15068fae3551SRodney W. Grimes 	struct hostent *hp;
1507f78ac61bSWarner Losh 	static char buf[16 + 3 + MAXHOSTNAMELEN];
15088fae3551SRodney W. Grimes 
15098fae3551SRodney W. Grimes 	if ((options & F_NUMERIC) ||
151043470e3bSGarrett Wollman 	    !(hp = gethostbyaddr((char *)&ina, 4, AF_INET)))
151143470e3bSGarrett Wollman 		return inet_ntoa(ina);
15128fae3551SRodney W. Grimes 	else
1513efa38539SPeter Wemm 		(void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name,
151443470e3bSGarrett Wollman 		    inet_ntoa(ina));
15158fae3551SRodney W. Grimes 	return(buf);
15168fae3551SRodney W. Grimes }
15178fae3551SRodney W. Grimes 
15188fae3551SRodney W. Grimes /*
15198fae3551SRodney W. Grimes  * pr_retip --
15208fae3551SRodney W. Grimes  *	Dump some info on a returned (via ICMP) IP packet.
15218fae3551SRodney W. Grimes  */
152243470e3bSGarrett Wollman static void
15238fae3551SRodney W. Grimes pr_retip(ip)
15248fae3551SRodney W. Grimes 	struct ip *ip;
15258fae3551SRodney W. Grimes {
15268fae3551SRodney W. Grimes 	u_char *cp;
1527efc8588dSDavid E. O'Brien 	int hlen;
15288fae3551SRodney W. Grimes 
15298fae3551SRodney W. Grimes 	pr_iph(ip);
15308fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
15318fae3551SRodney W. Grimes 	cp = (u_char *)ip + hlen;
15328fae3551SRodney W. Grimes 
15338fae3551SRodney W. Grimes 	if (ip->ip_p == 6)
15348fae3551SRodney W. Grimes 		(void)printf("TCP: from port %u, to port %u (decimal)\n",
15358fae3551SRodney W. Grimes 		    (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
15368fae3551SRodney W. Grimes 	else if (ip->ip_p == 17)
15378fae3551SRodney W. Grimes 		(void)printf("UDP: from port %u, to port %u (decimal)\n",
15388fae3551SRodney W. Grimes 			(*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
15398fae3551SRodney W. Grimes }
15408fae3551SRodney W. Grimes 
1541eb1543c6SMatthew N. Dodd static char *
1542eb1543c6SMatthew N. Dodd pr_ntime (n_time time)
1543eb1543c6SMatthew N. Dodd {
1544eb1543c6SMatthew N. Dodd 	static char buf[10];
1545eb1543c6SMatthew N. Dodd 	int h, m, s;
1546eb1543c6SMatthew N. Dodd 
1547eb1543c6SMatthew N. Dodd 	s = ntohl(time) / 1000;
1548eb1543c6SMatthew N. Dodd 	h = s / 60 / 60;
1549eb1543c6SMatthew N. Dodd 	m = (s % (60 * 60)) / 60;
1550eb1543c6SMatthew N. Dodd 	s = (s % (60 * 60)) % 60;
1551eb1543c6SMatthew N. Dodd 
1552eb1543c6SMatthew N. Dodd 	(void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d", h, m, s);
1553eb1543c6SMatthew N. Dodd 
1554eb1543c6SMatthew N. Dodd 	return (buf);
1555eb1543c6SMatthew N. Dodd }
1556eb1543c6SMatthew N. Dodd 
155743470e3bSGarrett Wollman static void
15588fae3551SRodney W. Grimes fill(bp, patp)
15598fae3551SRodney W. Grimes 	char *bp, *patp;
15608fae3551SRodney W. Grimes {
15618fae3551SRodney W. Grimes 	char *cp;
1562efc8588dSDavid E. O'Brien 	int pat[16];
15634fba6582SMaxim Konovalov 	u_int ii, jj, kk;
15648fae3551SRodney W. Grimes 
156543470e3bSGarrett Wollman 	for (cp = patp; *cp; cp++) {
156643470e3bSGarrett Wollman 		if (!isxdigit(*cp))
156743470e3bSGarrett Wollman 			errx(EX_USAGE,
156843470e3bSGarrett Wollman 			    "patterns must be specified as hex digits");
156943470e3bSGarrett Wollman 
15708fae3551SRodney W. Grimes 	}
15718fae3551SRodney W. Grimes 	ii = sscanf(patp,
15728fae3551SRodney W. Grimes 	    "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
15738fae3551SRodney W. Grimes 	    &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
15748fae3551SRodney W. Grimes 	    &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
15758fae3551SRodney W. Grimes 	    &pat[13], &pat[14], &pat[15]);
15768fae3551SRodney W. Grimes 
15778fae3551SRodney W. Grimes 	if (ii > 0)
1578d829c3dfSMatthew N. Dodd 		for (kk = 0; kk <= maxpayload - (TIMEVAL_LEN + ii); kk += ii)
15798fae3551SRodney W. Grimes 			for (jj = 0; jj < ii; ++jj)
15808fae3551SRodney W. Grimes 				bp[jj + kk] = pat[jj];
15818fae3551SRodney W. Grimes 	if (!(options & F_QUIET)) {
15828fae3551SRodney W. Grimes 		(void)printf("PATTERN: 0x");
15838fae3551SRodney W. Grimes 		for (jj = 0; jj < ii; ++jj)
15848fae3551SRodney W. Grimes 			(void)printf("%02x", bp[jj] & 0xFF);
15858fae3551SRodney W. Grimes 		(void)printf("\n");
15868fae3551SRodney W. Grimes 	}
15878fae3551SRodney W. Grimes }
15888fae3551SRodney W. Grimes 
1589120b4a93SRuslan Ermilov #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
1590120b4a93SRuslan Ermilov #define	SECOPT		" [-P policy]"
1591120b4a93SRuslan Ermilov #else
1592120b4a93SRuslan Ermilov #define	SECOPT		""
1593120b4a93SRuslan Ermilov #endif
159443470e3bSGarrett Wollman static void
1595e345a80dSPhilippe Charnier usage()
15968fae3551SRodney W. Grimes {
159731eac03bSSean Chittenden 
1598120b4a93SRuslan Ermilov 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
15991bd10ba2SRuslan Ermilov "usage: ping [-AaDdfnoQqRrv] [-c count] [-i wait] [-l preload] [-M mask | time]",
1600120b4a93SRuslan Ermilov "            [-m ttl]" SECOPT " [-p pattern] [-S src_addr] [-s packetsize]",
16011bd10ba2SRuslan Ermilov "            [-t timeout] [-z tos] host",
16021bd10ba2SRuslan Ermilov "       ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload]",
1603120b4a93SRuslan Ermilov "            [-M mask | time] [-m ttl]" SECOPT " [-p pattern] [-S src_addr]",
16041bd10ba2SRuslan Ermilov "            [-s packetsize] [-T ttl] [-t timeout] [-z tos] mcast-group");
160543470e3bSGarrett Wollman 	exit(EX_USAGE);
16068fae3551SRodney W. Grimes }
1607