xref: /freebsd/sbin/ping/ping.c (revision 9ff95228e8c027710cce6b7f8dbd7b13d579565c)
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  * 4. Neither the name of the University nor the names of its contributors
178fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
188fae3551SRodney W. Grimes  *    without specific prior written permission.
198fae3551SRodney W. Grimes  *
208fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
218fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
228fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
238fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
248fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
258fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
268fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
278fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
288fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
298fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
308fae3551SRodney W. Grimes  * SUCH DAMAGE.
318fae3551SRodney W. Grimes  */
328fae3551SRodney W. Grimes 
33c69284caSDavid E. O'Brien #if 0
348fae3551SRodney W. Grimes #ifndef lint
3543470e3bSGarrett Wollman static const char copyright[] =
368fae3551SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\
378fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
388fae3551SRodney W. Grimes #endif /* not lint */
398fae3551SRodney W. Grimes 
408fae3551SRodney W. Grimes #ifndef lint
418fae3551SRodney W. Grimes static char sccsid[] = "@(#)ping.c	8.1 (Berkeley) 6/5/93";
428fae3551SRodney W. Grimes #endif /* not lint */
43c69284caSDavid E. O'Brien #endif
44c69284caSDavid E. O'Brien #include <sys/cdefs.h>
45c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$");
468fae3551SRodney W. Grimes 
478fae3551SRodney W. Grimes /*
488fae3551SRodney W. Grimes  *			P I N G . C
498fae3551SRodney W. Grimes  *
50e345a80dSPhilippe Charnier  * Using the Internet Control Message Protocol (ICMP) "ECHO" facility,
518fae3551SRodney W. Grimes  * measure round-trip-delays and packet loss across network paths.
528fae3551SRodney W. Grimes  *
538fae3551SRodney W. Grimes  * Author -
548fae3551SRodney W. Grimes  *	Mike Muuss
558fae3551SRodney W. Grimes  *	U. S. Army Ballistic Research Laboratory
568fae3551SRodney W. Grimes  *	December, 1983
578fae3551SRodney W. Grimes  *
588fae3551SRodney W. Grimes  * Status -
598fae3551SRodney W. Grimes  *	Public Domain.  Distribution Unlimited.
608fae3551SRodney W. Grimes  * Bugs -
618fae3551SRodney W. Grimes  *	More statistics could always be gathered.
628fae3551SRodney W. Grimes  *	This program has to run SUID to ROOT to access the ICMP socket.
638fae3551SRodney W. Grimes  */
648fae3551SRodney W. Grimes 
6543470e3bSGarrett Wollman #include <sys/param.h>		/* NB: we rely on this for <sys/types.h> */
668fae3551SRodney W. Grimes #include <sys/socket.h>
67261e59bbSMaxim Konovalov #include <sys/sysctl.h>
688fae3551SRodney W. Grimes #include <sys/time.h>
69039d6aa4SBill Fenner #include <sys/uio.h>
708fae3551SRodney W. Grimes 
718fae3551SRodney W. Grimes #include <netinet/in.h>
7243470e3bSGarrett Wollman #include <netinet/in_systm.h>
738fae3551SRodney W. Grimes #include <netinet/ip.h>
748fae3551SRodney W. Grimes #include <netinet/ip_icmp.h>
758fae3551SRodney W. Grimes #include <netinet/ip_var.h>
7643470e3bSGarrett Wollman #include <arpa/inet.h>
778fae3551SRodney W. Grimes 
789a4365d0SYoshinobu Inoue #ifdef IPSEC
799a4365d0SYoshinobu Inoue #include <netinet6/ipsec.h>
809a4365d0SYoshinobu Inoue #endif /*IPSEC*/
819a4365d0SYoshinobu Inoue 
82261e59bbSMaxim Konovalov #include <ctype.h>
83261e59bbSMaxim Konovalov #include <err.h>
84261e59bbSMaxim Konovalov #include <errno.h>
85261e59bbSMaxim Konovalov #include <math.h>
86261e59bbSMaxim Konovalov #include <netdb.h>
87261e59bbSMaxim Konovalov #include <signal.h>
88261e59bbSMaxim Konovalov #include <stdio.h>
89261e59bbSMaxim Konovalov #include <stdlib.h>
90261e59bbSMaxim Konovalov #include <string.h>
91261e59bbSMaxim Konovalov #include <sysexits.h>
92261e59bbSMaxim Konovalov #include <unistd.h>
93261e59bbSMaxim Konovalov 
94301207dfSMaxim Konovalov #define	INADDR_LEN	((int)sizeof(in_addr_t))
9513e3f0b7SMaxim Konovalov #define	TIMEVAL_LEN	((int)sizeof(struct tv32))
96eb1543c6SMatthew N. Dodd #define	MASK_LEN	(ICMP_MASKLEN - ICMP_MINLEN)
97eb1543c6SMatthew N. Dodd #define	TS_LEN		(ICMP_TSLEN - ICMP_MINLEN)
98c67c1ce8SMatthew N. Dodd #define	DEFDATALEN	56		/* default data length */
998f975bb3SBruce Evans #define	FLOOD_BACKOFF	20000		/* usecs to back off if F_FLOOD mode */
1008f975bb3SBruce Evans 					/* runs out of buffer space */
1014fba6582SMaxim Konovalov #define	MAXIPLEN	(sizeof(struct ip) + MAX_IPOPTLEN)
1024fba6582SMaxim Konovalov #define	MAXICMPLEN	(ICMP_ADVLENMIN + MAX_IPOPTLEN)
1038fae3551SRodney W. Grimes #define	MAXWAIT		10		/* max seconds to wait for response */
104bf113f1bSBill Fumerola #define	MAXALARM	(60 * 60)	/* max seconds for alarm timeout */
1050b2f8b3fSMaxim Konovalov #define	MAXTOS		255
1068fae3551SRodney W. Grimes 
1078fae3551SRodney W. Grimes #define	A(bit)		rcvd_tbl[(bit)>>3]	/* identify byte in array */
1088fae3551SRodney W. Grimes #define	B(bit)		(1 << ((bit) & 0x07))	/* identify bit in byte */
1098fae3551SRodney W. Grimes #define	SET(bit)	(A(bit) |= B(bit))
1108fae3551SRodney W. Grimes #define	CLR(bit)	(A(bit) &= (~B(bit)))
1118fae3551SRodney W. Grimes #define	TST(bit)	(A(bit) & B(bit))
1128fae3551SRodney W. Grimes 
11313e3f0b7SMaxim Konovalov struct tv32 {
11413e3f0b7SMaxim Konovalov 	int32_t tv32_sec;
11513e3f0b7SMaxim Konovalov 	int32_t tv32_usec;
11613e3f0b7SMaxim Konovalov };
11713e3f0b7SMaxim Konovalov 
1188fae3551SRodney W. Grimes /* various options */
1198fae3551SRodney W. Grimes int options;
12085456935SBill Fenner #define	F_FLOOD		0x0001
12185456935SBill Fenner #define	F_INTERVAL	0x0002
12285456935SBill Fenner #define	F_NUMERIC	0x0004
12385456935SBill Fenner #define	F_PINGFILLED	0x0008
12485456935SBill Fenner #define	F_QUIET		0x0010
12585456935SBill Fenner #define	F_RROUTE	0x0020
12685456935SBill Fenner #define	F_SO_DEBUG	0x0040
12785456935SBill Fenner #define	F_SO_DONTROUTE	0x0080
12885456935SBill Fenner #define	F_VERBOSE	0x0100
12985456935SBill Fenner #define	F_QUIET2	0x0200
13085456935SBill Fenner #define	F_NOLOOP	0x0400
13185456935SBill Fenner #define	F_MTTL		0x0800
13285456935SBill Fenner #define	F_MIF		0x1000
133772dfa72SDaniel O'Callaghan #define	F_AUDIBLE	0x2000
1349a4365d0SYoshinobu Inoue #ifdef IPSEC
1359a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
1369a4365d0SYoshinobu Inoue #define F_POLICY	0x4000
1379a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/
1389a4365d0SYoshinobu Inoue #endif /*IPSEC*/
139211bfbd2SRuslan Ermilov #define	F_TTL		0x8000
140ca517ad8SPoul-Henning Kamp #define	F_MISSED	0x10000
1418025c44bSDima Dorfman #define	F_ONCE		0x20000
1420b2f8b3fSMaxim Konovalov #define	F_HDRINCL	0x40000
143143008a1SMatthew N. Dodd #define	F_MASK		0x80000
144eb1543c6SMatthew N. Dodd #define	F_TIME		0x100000
1459ff95228SGleb Smirnoff #define	F_SWEEP		0x200000
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;
171e88178ddSMaxim Konovalov int send_len;
1728fae3551SRodney W. Grimes 
1738fae3551SRodney W. Grimes /* counters */
174261e59bbSMaxim Konovalov long nmissedmax;		/* max value of ntransmitted - nreceived - 1 */
1758fae3551SRodney W. Grimes long npackets;			/* max packets to transmit */
1768fae3551SRodney W. Grimes long nreceived;			/* # of packets we got back */
1778fae3551SRodney W. Grimes long nrepeats;			/* number of duplicates */
1788fae3551SRodney W. Grimes long ntransmitted;		/* sequence # for outbound packets = #sent */
1799ff95228SGleb Smirnoff long snpackets;			/* max packets to transmit in one sweep */
1809ff95228SGleb Smirnoff long snreceived;		/* # of packets we got back in this sweep */
1819ff95228SGleb Smirnoff long sntransmitted;		/* # of packets we sent in this sweep */
1829ff95228SGleb Smirnoff int sweepmax;			/* max value of payload in sweep */
1839ff95228SGleb Smirnoff int sweepmin = 0;		/* start value of payload in sweep */
1849ff95228SGleb Smirnoff int sweepincr = 1;		/* payload increment in sweep */
185526f06b2SMatthew Dillon int interval = 1000;		/* interval between packets, ms */
1868fae3551SRodney W. Grimes 
1878fae3551SRodney W. Grimes /* timing */
1888fae3551SRodney W. Grimes int timing;			/* flag to do timing */
1898fae3551SRodney W. Grimes double tmin = 999999999.0;	/* minimum round trip time */
1908fae3551SRodney W. Grimes double tmax = 0.0;		/* maximum round trip time */
1918fae3551SRodney W. Grimes double tsum = 0.0;		/* sum of all times, for doing average */
1923109a910SGarrett Wollman double tsumsq = 0.0;		/* sum of all times squared, for std. dev. */
1938fae3551SRodney W. Grimes 
1948f975bb3SBruce Evans volatile sig_atomic_t finish_up;  /* nonzero if we've been told to finish up */
1958f975bb3SBruce Evans volatile sig_atomic_t siginfo_p;
196badd8138SSean Eric Fagan 
19743470e3bSGarrett Wollman static void fill(char *, char *);
19843470e3bSGarrett Wollman static u_short in_cksum(u_short *, int);
19943470e3bSGarrett Wollman static void check_status(void);
2008f975bb3SBruce Evans static void finish(void) __dead2;
20143470e3bSGarrett Wollman static void pinger(void);
20243470e3bSGarrett Wollman static char *pr_addr(struct in_addr);
203eb1543c6SMatthew N. Dodd static char *pr_ntime(n_time);
20443470e3bSGarrett Wollman static void pr_icmph(struct icmp *);
20543470e3bSGarrett Wollman static void pr_iph(struct ip *);
206039d6aa4SBill Fenner static void pr_pack(char *, int, struct sockaddr_in *, struct timeval *);
20743470e3bSGarrett Wollman static void pr_retip(struct ip *);
20843470e3bSGarrett Wollman static void status(int);
2098f975bb3SBruce Evans static void stopit(int);
21043470e3bSGarrett Wollman static void tvsub(struct timeval *, struct timeval *);
211e345a80dSPhilippe Charnier static void usage(void) __dead2;
2128fae3551SRodney W. Grimes 
21343470e3bSGarrett Wollman int
2148fae3551SRodney W. Grimes main(argc, argv)
2158fae3551SRodney W. Grimes 	int argc;
21643470e3bSGarrett Wollman 	char *const *argv;
2178fae3551SRodney W. Grimes {
21831eac03bSSean Chittenden 	struct sockaddr_in from, sock_in;
219efc8588dSDavid E. O'Brien 	struct in_addr ifaddr;
220261e59bbSMaxim Konovalov 	struct timeval last, intvl;
221efc8588dSDavid E. O'Brien 	struct iovec iov;
2220b2f8b3fSMaxim Konovalov 	struct ip *ip;
223efc8588dSDavid E. O'Brien 	struct msghdr msg;
224efc8588dSDavid E. O'Brien 	struct sigaction si_sa;
2250b2f8b3fSMaxim Konovalov 	size_t sz;
2264fba6582SMaxim Konovalov 	u_char *datap, packet[IP_MAXPACKET];
227d074d39fSMatthew N. Dodd 	char *ep, *source, *target, *payload;
228261e59bbSMaxim Konovalov 	struct hostent *hp;
229efc8588dSDavid E. O'Brien #ifdef IPSEC_POLICY_IPSEC
230efc8588dSDavid E. O'Brien 	char *policy_in, *policy_out;
231efc8588dSDavid E. O'Brien #endif
232261e59bbSMaxim Konovalov 	struct sockaddr_in *to;
233261e59bbSMaxim Konovalov 	double t;
234efc8588dSDavid E. O'Brien 	u_long alarmtimeout, ultmp;
235e88178ddSMaxim Konovalov 	int almost_done, ch, df, hold, i, icmp_len, mib[4], preload, sockerrno,
236261e59bbSMaxim Konovalov 	    tos, ttl;
237efc8588dSDavid E. O'Brien 	char ctrl[CMSG_SPACE(sizeof(struct timeval))];
238efc8588dSDavid E. O'Brien 	char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN];
2398fae3551SRodney W. Grimes #ifdef IP_OPTIONS
2404fba6582SMaxim Konovalov 	char rspace[MAX_IPOPTLEN];	/* record route space */
2418fae3551SRodney W. Grimes #endif
242261e59bbSMaxim Konovalov 	unsigned char loop, mttl;
243efc8588dSDavid E. O'Brien 
24431eac03bSSean Chittenden 	payload = source = NULL;
2459a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
246efc8588dSDavid E. O'Brien 	policy_in = policy_out = NULL;
2479a4365d0SYoshinobu Inoue #endif
2488fae3551SRodney W. Grimes 
249f1284d7aSBill Fenner 	/*
250f1284d7aSBill Fenner 	 * Do the stuff that we need root priv's for *first*, and
251f1284d7aSBill Fenner 	 * then drop our setuid bit.  Save error reporting for
252f1284d7aSBill Fenner 	 * after arg parsing.
253f1284d7aSBill Fenner 	 */
25443470e3bSGarrett Wollman 	s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
255f1284d7aSBill Fenner 	sockerrno = errno;
256f1284d7aSBill Fenner 
257f1284d7aSBill Fenner 	setuid(getuid());
258ee2bf734SWarner Losh 	uid = getuid();
259f1284d7aSBill Fenner 
2600b2f8b3fSMaxim Konovalov 	alarmtimeout = df = preload = tos = 0;
261badd8138SSean Eric Fagan 
2620b2f8b3fSMaxim Konovalov 	outpack = outpackhdr + sizeof(struct ip);
263211bfbd2SRuslan Ermilov 	while ((ch = getopt(argc, argv,
2649ff95228SGleb Smirnoff 		"Aac:DdfG:g:h:I:i:Ll:M:m:nop:QqRrS:s:T:t:vz:"
265211bfbd2SRuslan Ermilov #ifdef IPSEC
2669a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
267211bfbd2SRuslan Ermilov 		"P:"
2689a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/
269211bfbd2SRuslan Ermilov #endif /*IPSEC*/
270211bfbd2SRuslan Ermilov 		)) != -1)
2719a4365d0SYoshinobu Inoue 	{
2728fae3551SRodney W. Grimes 		switch(ch) {
273ca517ad8SPoul-Henning Kamp 		case 'A':
274ca517ad8SPoul-Henning Kamp 			options |= F_MISSED;
275ca517ad8SPoul-Henning Kamp 			break;
276772dfa72SDaniel O'Callaghan 		case 'a':
277772dfa72SDaniel O'Callaghan 			options |= F_AUDIBLE;
278772dfa72SDaniel O'Callaghan 			break;
2798fae3551SRodney W. Grimes 		case 'c':
28043470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
28143470e3bSGarrett Wollman 			if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp)
28243470e3bSGarrett Wollman 				errx(EX_USAGE,
28343470e3bSGarrett Wollman 				    "invalid count of packets to transmit: `%s'",
28443470e3bSGarrett Wollman 				    optarg);
28543470e3bSGarrett Wollman 			npackets = ultmp;
2868fae3551SRodney W. Grimes 			break;
2870b2f8b3fSMaxim Konovalov 		case 'D':
2880b2f8b3fSMaxim Konovalov 			options |= F_HDRINCL;
2890b2f8b3fSMaxim Konovalov 			df = 1;
2900b2f8b3fSMaxim Konovalov 			break;
2918fae3551SRodney W. Grimes 		case 'd':
2928fae3551SRodney W. Grimes 			options |= F_SO_DEBUG;
2938fae3551SRodney W. Grimes 			break;
2948fae3551SRodney W. Grimes 		case 'f':
295526f06b2SMatthew Dillon 			if (uid) {
29643470e3bSGarrett Wollman 				errno = EPERM;
29743470e3bSGarrett Wollman 				err(EX_NOPERM, "-f flag");
2988fae3551SRodney W. Grimes 			}
2998fae3551SRodney W. Grimes 			options |= F_FLOOD;
3008fae3551SRodney W. Grimes 			setbuf(stdout, (char *)NULL);
3018fae3551SRodney W. Grimes 			break;
3029ff95228SGleb Smirnoff 		case 'G': /* Maximum packet size for ping sweep */
3039ff95228SGleb Smirnoff 			ultmp = strtoul(optarg, &ep, 0);
3049ff95228SGleb Smirnoff 			if (*ep || ep == optarg)
3059ff95228SGleb Smirnoff 				errx(EX_USAGE, "invalid packet size: `%s'",
3069ff95228SGleb Smirnoff 				    optarg);
3079ff95228SGleb Smirnoff 			if (uid != 0 && ultmp > DEFDATALEN) {
3089ff95228SGleb Smirnoff 				errno = EPERM;
3099ff95228SGleb Smirnoff 				err(EX_NOPERM,
3109ff95228SGleb Smirnoff 				    "packet size too large: %lu > %u",
3119ff95228SGleb Smirnoff 				    ultmp, DEFDATALEN);
3129ff95228SGleb Smirnoff 			}
3139ff95228SGleb Smirnoff 			options |= F_SWEEP;
3149ff95228SGleb Smirnoff 			sweepmax = ultmp;
3159ff95228SGleb Smirnoff 			break;
3169ff95228SGleb Smirnoff 		case 'g': /* Minimum packet size for ping sweep */
3179ff95228SGleb Smirnoff 			ultmp = strtoul(optarg, &ep, 0);
3189ff95228SGleb Smirnoff 			if (*ep || ep == optarg)
3199ff95228SGleb Smirnoff 				errx(EX_USAGE, "invalid packet size: `%s'",
3209ff95228SGleb Smirnoff 				    optarg);
3219ff95228SGleb Smirnoff 			if (uid != 0 && ultmp > DEFDATALEN) {
3229ff95228SGleb Smirnoff 				errno = EPERM;
3239ff95228SGleb Smirnoff 				err(EX_NOPERM,
3249ff95228SGleb Smirnoff 				    "packet size too large: %lu > %u",
3259ff95228SGleb Smirnoff 				    ultmp, DEFDATALEN);
3269ff95228SGleb Smirnoff 			}
3279ff95228SGleb Smirnoff 			options |= F_SWEEP;
3289ff95228SGleb Smirnoff 			sweepmin = ultmp;
3299ff95228SGleb Smirnoff 			break;
3309ff95228SGleb Smirnoff 		case 'h': /* Packet size increment for ping sweep */
3319ff95228SGleb Smirnoff 			ultmp = strtoul(optarg, &ep, 0);
3329ff95228SGleb Smirnoff 			if (*ep || ep == optarg || ultmp < 1)
3339ff95228SGleb Smirnoff 				errx(EX_USAGE, "invalid increment size: `%s'",
3349ff95228SGleb Smirnoff 				    optarg);
3359ff95228SGleb Smirnoff 			if (uid != 0 && ultmp > DEFDATALEN) {
3369ff95228SGleb Smirnoff 				errno = EPERM;
3379ff95228SGleb Smirnoff 				err(EX_NOPERM,
3389ff95228SGleb Smirnoff 				    "packet size too large: %lu > %u",
3399ff95228SGleb Smirnoff 				    ultmp, DEFDATALEN);
3409ff95228SGleb Smirnoff 			}
3419ff95228SGleb Smirnoff 			options |= F_SWEEP;
3429ff95228SGleb Smirnoff 			sweepincr = ultmp;
3439ff95228SGleb Smirnoff 			break;
3441f6a4631SRuslan Ermilov 		case 'I':		/* multicast interface */
3451f6a4631SRuslan Ermilov 			if (inet_aton(optarg, &ifaddr) == 0)
3461f6a4631SRuslan Ermilov 				errx(EX_USAGE,
3471f6a4631SRuslan Ermilov 				    "invalid multicast interface: `%s'",
3481f6a4631SRuslan Ermilov 				    optarg);
3491f6a4631SRuslan Ermilov 			options |= F_MIF;
3501f6a4631SRuslan Ermilov 			break;
3518fae3551SRodney W. Grimes 		case 'i':		/* wait between sending packets */
3521ad0b1beSMaxim Konovalov 			t = strtod(optarg, &ep) * 1000.0;
3531ad0b1beSMaxim Konovalov 			if (*ep || ep == optarg || t > (double)INT_MAX)
3541ad0b1beSMaxim Konovalov 				errx(EX_USAGE, "invalid timing interval: `%s'",
3551ad0b1beSMaxim Konovalov 				    optarg);
3568fae3551SRodney W. Grimes 			options |= F_INTERVAL;
357526f06b2SMatthew Dillon 			interval = (int)t;
358526f06b2SMatthew Dillon 			if (uid && interval < 1000) {
359526f06b2SMatthew Dillon 				errno = EPERM;
360526f06b2SMatthew Dillon 				err(EX_NOPERM, "-i interval too short");
361526f06b2SMatthew Dillon 			}
3628fae3551SRodney W. Grimes 			break;
3631f6a4631SRuslan Ermilov 		case 'L':
3641f6a4631SRuslan Ermilov 			options |= F_NOLOOP;
3651f6a4631SRuslan Ermilov 			loop = 0;
36685456935SBill Fenner 			break;
3678fae3551SRodney W. Grimes 		case 'l':
36843470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
36943470e3bSGarrett Wollman 			if (*ep || ep == optarg || ultmp > INT_MAX)
37043470e3bSGarrett Wollman 				errx(EX_USAGE,
37143470e3bSGarrett Wollman 				    "invalid preload value: `%s'", optarg);
3725e2cc0f4SStephen McKay 			if (uid) {
373f78ac61bSWarner Losh 				errno = EPERM;
374f78ac61bSWarner Losh 				err(EX_NOPERM, "-l flag");
375f78ac61bSWarner Losh 			}
37643470e3bSGarrett Wollman 			preload = ultmp;
3778fae3551SRodney W. Grimes 			break;
3781f6a4631SRuslan Ermilov 		case 'M':
379eb1543c6SMatthew N. Dodd 			switch(optarg[0]) {
380eb1543c6SMatthew N. Dodd 			case 'M':
381eb1543c6SMatthew N. Dodd 			case 'm':
3821f6a4631SRuslan Ermilov 				options |= F_MASK;
38385456935SBill Fenner 				break;
384eb1543c6SMatthew N. Dodd 			case 'T':
385eb1543c6SMatthew N. Dodd 			case 't':
386eb1543c6SMatthew N. Dodd 				options |= F_TIME;
387eb1543c6SMatthew N. Dodd 				break;
388eb1543c6SMatthew N. Dodd 			default:
389eb1543c6SMatthew N. Dodd 				errx(EX_USAGE, "invalid message: `%c'", optarg[0]);
390eb1543c6SMatthew N. Dodd 				break;
391eb1543c6SMatthew N. Dodd 			}
392eb1543c6SMatthew N. Dodd 			break;
393211bfbd2SRuslan Ermilov 		case 'm':		/* TTL */
394211bfbd2SRuslan Ermilov 			ultmp = strtoul(optarg, &ep, 0);
3959bc1a9ecSMaxim Konovalov 			if (*ep || ep == optarg || ultmp > MAXTTL)
3961ad0b1beSMaxim Konovalov 				errx(EX_USAGE, "invalid TTL: `%s'", optarg);
397211bfbd2SRuslan Ermilov 			ttl = ultmp;
398211bfbd2SRuslan Ermilov 			options |= F_TTL;
399211bfbd2SRuslan Ermilov 			break;
4008fae3551SRodney W. Grimes 		case 'n':
4018fae3551SRodney W. Grimes 			options |= F_NUMERIC;
4028fae3551SRodney W. Grimes 			break;
4038025c44bSDima Dorfman 		case 'o':
4048025c44bSDima Dorfman 			options |= F_ONCE;
4058025c44bSDima Dorfman 			break;
4061f6a4631SRuslan Ermilov #ifdef IPSEC
4071f6a4631SRuslan Ermilov #ifdef IPSEC_POLICY_IPSEC
4081f6a4631SRuslan Ermilov 		case 'P':
4091f6a4631SRuslan Ermilov 			options |= F_POLICY;
4101f6a4631SRuslan Ermilov 			if (!strncmp("in", optarg, 2))
4111f6a4631SRuslan Ermilov 				policy_in = strdup(optarg);
4121f6a4631SRuslan Ermilov 			else if (!strncmp("out", optarg, 3))
4131f6a4631SRuslan Ermilov 				policy_out = strdup(optarg);
4141f6a4631SRuslan Ermilov 			else
4151f6a4631SRuslan Ermilov 				errx(1, "invalid security policy");
4161f6a4631SRuslan Ermilov 			break;
4171f6a4631SRuslan Ermilov #endif /*IPSEC_POLICY_IPSEC*/
4181f6a4631SRuslan Ermilov #endif /*IPSEC*/
4198fae3551SRodney W. Grimes 		case 'p':		/* fill buffer with user pattern */
4208fae3551SRodney W. Grimes 			options |= F_PINGFILLED;
421d074d39fSMatthew N. Dodd 			payload = optarg;
4228fae3551SRodney W. Grimes 			break;
423ef9e6dc7SBill Fenner 		case 'Q':
424ef9e6dc7SBill Fenner 			options |= F_QUIET2;
425ef9e6dc7SBill Fenner 			break;
4268fae3551SRodney W. Grimes 		case 'q':
4278fae3551SRodney W. Grimes 			options |= F_QUIET;
4288fae3551SRodney W. Grimes 			break;
4298fae3551SRodney W. Grimes 		case 'R':
4308fae3551SRodney W. Grimes 			options |= F_RROUTE;
4318fae3551SRodney W. Grimes 			break;
4328fae3551SRodney W. Grimes 		case 'r':
4338fae3551SRodney W. Grimes 			options |= F_SO_DONTROUTE;
4348fae3551SRodney W. Grimes 			break;
4351f6a4631SRuslan Ermilov 		case 'S':
4361f6a4631SRuslan Ermilov 			source = optarg;
4371f6a4631SRuslan Ermilov 			break;
4388fae3551SRodney W. Grimes 		case 's':		/* size of packet to send */
43943470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
4404fba6582SMaxim Konovalov 			if (*ep || ep == optarg)
44143470e3bSGarrett Wollman 				errx(EX_USAGE, "invalid packet size: `%s'",
44243470e3bSGarrett Wollman 				    optarg);
443fb7d32c7SMaxim Konovalov 			if (uid != 0 && ultmp > DEFDATALEN) {
444fb7d32c7SMaxim Konovalov 				errno = EPERM;
445fb7d32c7SMaxim Konovalov 				err(EX_NOPERM,
446fb7d32c7SMaxim Konovalov 				    "packet size too large: %lu > %u",
447fb7d32c7SMaxim Konovalov 				    ultmp, DEFDATALEN);
448fb7d32c7SMaxim Konovalov 			}
44943470e3bSGarrett Wollman 			datalen = ultmp;
4508fae3551SRodney W. Grimes 			break;
4511f6a4631SRuslan Ermilov 		case 'T':		/* multicast TTL */
4521f6a4631SRuslan Ermilov 			ultmp = strtoul(optarg, &ep, 0);
4531f6a4631SRuslan Ermilov 			if (*ep || ep == optarg || ultmp > MAXTTL)
4541f6a4631SRuslan Ermilov 				errx(EX_USAGE, "invalid multicast TTL: `%s'",
4551f6a4631SRuslan Ermilov 				    optarg);
4561f6a4631SRuslan Ermilov 			mttl = ultmp;
4571f6a4631SRuslan Ermilov 			options |= F_MTTL;
45899490edeSWarner Losh 			break;
4597237fd94SBill Fumerola 		case 't':
460bf113f1bSBill Fumerola 			alarmtimeout = strtoul(optarg, &ep, 0);
461bf113f1bSBill Fumerola 			if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX))
4627237fd94SBill Fumerola 				errx(EX_USAGE, "invalid timeout: `%s'",
4637237fd94SBill Fumerola 				    optarg);
464bf113f1bSBill Fumerola 			if (alarmtimeout > MAXALARM)
465bf113f1bSBill Fumerola 				errx(EX_USAGE, "invalid timeout: `%s' > %d",
466bf113f1bSBill Fumerola 				    optarg, MAXALARM);
467bf113f1bSBill Fumerola 			alarm((int)alarmtimeout);
4687237fd94SBill Fumerola 			break;
4698fae3551SRodney W. Grimes 		case 'v':
4708fae3551SRodney W. Grimes 			options |= F_VERBOSE;
4718fae3551SRodney W. Grimes 			break;
4720b2f8b3fSMaxim Konovalov 		case 'z':
4730b2f8b3fSMaxim Konovalov 			options |= F_HDRINCL;
4740b2f8b3fSMaxim Konovalov 			ultmp = strtoul(optarg, &ep, 0);
4750b2f8b3fSMaxim Konovalov 			if (*ep || ep == optarg || ultmp > MAXTOS)
4760b2f8b3fSMaxim Konovalov 				errx(EX_USAGE, "invalid TOS: `%s'", optarg);
4770b2f8b3fSMaxim Konovalov 			tos = ultmp;
4780b2f8b3fSMaxim Konovalov 			break;
4798fae3551SRodney W. Grimes 		default:
480e345a80dSPhilippe Charnier 			usage();
48143470e3bSGarrett Wollman 		}
48243470e3bSGarrett Wollman 	}
48343470e3bSGarrett Wollman 
48443470e3bSGarrett Wollman 	if (argc - optind != 1)
485e345a80dSPhilippe Charnier 		usage();
48643470e3bSGarrett Wollman 	target = argv[optind];
4878fae3551SRodney W. Grimes 
488d829c3dfSMatthew N. Dodd 	switch (options & (F_MASK|F_TIME)) {
489d829c3dfSMatthew N. Dodd 	case 0: break;
490d829c3dfSMatthew N. Dodd 	case F_MASK:
491eb1543c6SMatthew N. Dodd 		icmp_type = ICMP_MASKREQ;
492eb1543c6SMatthew N. Dodd 		icmp_type_rsp = ICMP_MASKREPLY;
493d829c3dfSMatthew N. Dodd 		phdr_len = MASK_LEN;
494eb1543c6SMatthew N. Dodd 		if (!(options & F_QUIET))
495eb1543c6SMatthew N. Dodd 			(void)printf("ICMP_MASKREQ\n");
496d829c3dfSMatthew N. Dodd 		break;
497d829c3dfSMatthew N. Dodd 	case F_TIME:
498eb1543c6SMatthew N. Dodd 		icmp_type = ICMP_TSTAMP;
499eb1543c6SMatthew N. Dodd 		icmp_type_rsp = ICMP_TSTAMPREPLY;
500d829c3dfSMatthew N. Dodd 		phdr_len = TS_LEN;
501eb1543c6SMatthew N. Dodd 		if (!(options & F_QUIET))
502eb1543c6SMatthew N. Dodd 			(void)printf("ICMP_TSTAMP\n");
503d829c3dfSMatthew N. Dodd 		break;
504d829c3dfSMatthew N. Dodd 	default:
505d829c3dfSMatthew N. Dodd 		errx(EX_USAGE, "ICMP_TSTAMP and ICMP_MASKREQ are exclusive.");
506d829c3dfSMatthew N. Dodd 		break;
507eb1543c6SMatthew N. Dodd 	}
5080fe0c0ccSMaxim Konovalov 	icmp_len = sizeof(struct ip) + ICMP_MINLEN + phdr_len;
509fb7d32c7SMaxim Konovalov 	if (options & F_RROUTE)
510e88178ddSMaxim Konovalov 		icmp_len += MAX_IPOPTLEN;
511e88178ddSMaxim Konovalov 	maxpayload = IP_MAXPACKET - icmp_len;
512fb7d32c7SMaxim Konovalov 	if (datalen > maxpayload)
5131104dd84SBruce Evans 		errx(EX_USAGE, "packet size too large: %d > %d", datalen,
514fb7d32c7SMaxim Konovalov 		    maxpayload);
515e88178ddSMaxim Konovalov 	send_len = icmp_len + datalen;
5160fe0c0ccSMaxim Konovalov 	datap = &outpack[ICMP_MINLEN + phdr_len + TIMEVAL_LEN];
517d074d39fSMatthew N. Dodd 	if (options & F_PINGFILLED) {
518d074d39fSMatthew N. Dodd 		fill((char *)datap, payload);
519d074d39fSMatthew N. Dodd 	}
52099490edeSWarner Losh 	if (source) {
52131eac03bSSean Chittenden 		bzero((char *)&sock_in, sizeof(sock_in));
52231eac03bSSean Chittenden 		sock_in.sin_family = AF_INET;
52331eac03bSSean Chittenden 		if (inet_aton(source, &sock_in.sin_addr) != 0) {
52499490edeSWarner Losh 			shostname = source;
52599490edeSWarner Losh 		} else {
52699490edeSWarner Losh 			hp = gethostbyname2(source, AF_INET);
52799490edeSWarner Losh 			if (!hp)
52899490edeSWarner Losh 				errx(EX_NOHOST, "cannot resolve %s: %s",
52999490edeSWarner Losh 				    source, hstrerror(h_errno));
53099490edeSWarner Losh 
53131eac03bSSean Chittenden 			sock_in.sin_len = sizeof sock_in;
53231eac03bSSean Chittenden 			if ((unsigned)hp->h_length > sizeof(sock_in.sin_addr) ||
5334fba6582SMaxim Konovalov 			    hp->h_length < 0)
53499490edeSWarner Losh 				errx(1, "gethostbyname2: illegal address");
53531eac03bSSean Chittenden 			memcpy(&sock_in.sin_addr, hp->h_addr_list[0],
53631eac03bSSean Chittenden 			    sizeof(sock_in.sin_addr));
53799490edeSWarner Losh 			(void)strncpy(snamebuf, hp->h_name,
53899490edeSWarner Losh 			    sizeof(snamebuf) - 1);
53999490edeSWarner Losh 			snamebuf[sizeof(snamebuf) - 1] = '\0';
54099490edeSWarner Losh 			shostname = snamebuf;
54199490edeSWarner Losh 		}
54231eac03bSSean Chittenden 		if (bind(s, (struct sockaddr *)&sock_in, sizeof sock_in) == -1)
54399490edeSWarner Losh 			err(1, "bind");
54499490edeSWarner Losh 	}
54599490edeSWarner Losh 
546d389e86aSMatt Jacob 	bzero(&whereto, sizeof(whereto));
547d389e86aSMatt Jacob 	to = &whereto;
5488fae3551SRodney W. Grimes 	to->sin_family = AF_INET;
549d389e86aSMatt Jacob 	to->sin_len = sizeof *to;
55043470e3bSGarrett Wollman 	if (inet_aton(target, &to->sin_addr) != 0) {
5518fae3551SRodney W. Grimes 		hostname = target;
55243470e3bSGarrett Wollman 	} else {
55343470e3bSGarrett Wollman 		hp = gethostbyname2(target, AF_INET);
55443470e3bSGarrett Wollman 		if (!hp)
55543470e3bSGarrett Wollman 			errx(EX_NOHOST, "cannot resolve %s: %s",
55643470e3bSGarrett Wollman 			    target, hstrerror(h_errno));
55743470e3bSGarrett Wollman 
55831eac03bSSean Chittenden 		if ((unsigned)hp->h_length > sizeof(to->sin_addr))
5591ffae4a6SWarner Losh 			errx(1, "gethostbyname2 returned an illegal address");
56043470e3bSGarrett Wollman 		memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr);
5618fae3551SRodney W. Grimes 		(void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1);
562b10d9d5fSWarner Losh 		hnamebuf[sizeof(hnamebuf) - 1] = '\0';
5638fae3551SRodney W. Grimes 		hostname = hnamebuf;
5648fae3551SRodney W. Grimes 	}
5658fae3551SRodney W. Grimes 
56643470e3bSGarrett Wollman 	if (options & F_FLOOD && options & F_INTERVAL)
56743470e3bSGarrett Wollman 		errx(EX_USAGE, "-f and -i: incompatible options");
56843470e3bSGarrett Wollman 
56943470e3bSGarrett Wollman 	if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
57043470e3bSGarrett Wollman 		errx(EX_USAGE,
57143470e3bSGarrett Wollman 		    "-f flag cannot be used with multicast destination");
57243470e3bSGarrett Wollman 	if (options & (F_MIF | F_NOLOOP | F_MTTL)
57343470e3bSGarrett Wollman 	    && !IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
57443470e3bSGarrett Wollman 		errx(EX_USAGE,
57543470e3bSGarrett Wollman 		    "-I, -L, -T flags cannot be used with unicast destination");
5768fae3551SRodney W. Grimes 
577d829c3dfSMatthew N. Dodd 	if (datalen >= TIMEVAL_LEN)	/* can we time transfer */
5788fae3551SRodney W. Grimes 		timing = 1;
57943470e3bSGarrett Wollman 
5808fae3551SRodney W. Grimes 	if (!(options & F_PINGFILLED))
581d829c3dfSMatthew N. Dodd 		for (i = TIMEVAL_LEN; i < datalen; ++i)
5828fae3551SRodney W. Grimes 			*datap++ = i;
5838fae3551SRodney W. Grimes 
5848fae3551SRodney W. Grimes 	ident = getpid() & 0xFFFF;
5858fae3551SRodney W. Grimes 
586f1284d7aSBill Fenner 	if (s < 0) {
587f1284d7aSBill Fenner 		errno = sockerrno;
58843470e3bSGarrett Wollman 		err(EX_OSERR, "socket");
5898fae3551SRodney W. Grimes 	}
5908fae3551SRodney W. Grimes 	hold = 1;
5918fae3551SRodney W. Grimes 	if (options & F_SO_DEBUG)
5928fae3551SRodney W. Grimes 		(void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
5938fae3551SRodney W. Grimes 		    sizeof(hold));
5948fae3551SRodney W. Grimes 	if (options & F_SO_DONTROUTE)
5958fae3551SRodney W. Grimes 		(void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold,
5968fae3551SRodney W. Grimes 		    sizeof(hold));
5979a4365d0SYoshinobu Inoue #ifdef IPSEC
5989a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
5999a4365d0SYoshinobu Inoue 	if (options & F_POLICY) {
6009a4365d0SYoshinobu Inoue 		char *buf;
6019a4365d0SYoshinobu Inoue 		if (policy_in != NULL) {
6029a4365d0SYoshinobu Inoue 			buf = ipsec_set_policy(policy_in, strlen(policy_in));
6039a4365d0SYoshinobu Inoue 			if (buf == NULL)
604ffd40070SKris Kennaway 				errx(EX_CONFIG, "%s", ipsec_strerror());
6059a4365d0SYoshinobu Inoue 			if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY,
6069a4365d0SYoshinobu Inoue 					buf, ipsec_get_policylen(buf)) < 0)
6071ad0b1beSMaxim Konovalov 				err(EX_CONFIG,
6081ad0b1beSMaxim Konovalov 				    "ipsec policy cannot be configured");
6099a4365d0SYoshinobu Inoue 			free(buf);
6109a4365d0SYoshinobu Inoue 		}
6119a4365d0SYoshinobu Inoue 
6129a4365d0SYoshinobu Inoue 		if (policy_out != NULL) {
6139a4365d0SYoshinobu Inoue 			buf = ipsec_set_policy(policy_out, strlen(policy_out));
6149a4365d0SYoshinobu Inoue 			if (buf == NULL)
615ffd40070SKris Kennaway 				errx(EX_CONFIG, "%s", ipsec_strerror());
6169a4365d0SYoshinobu Inoue 			if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY,
6179a4365d0SYoshinobu Inoue 					buf, ipsec_get_policylen(buf)) < 0)
6181ad0b1beSMaxim Konovalov 				err(EX_CONFIG,
6191ad0b1beSMaxim Konovalov 				    "ipsec policy cannot be configured");
6209a4365d0SYoshinobu Inoue 			free(buf);
6219a4365d0SYoshinobu Inoue 		}
6229a4365d0SYoshinobu Inoue 	}
6239a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/
6249a4365d0SYoshinobu Inoue #endif /*IPSEC*/
6258fae3551SRodney W. Grimes 
6260b2f8b3fSMaxim Konovalov 	if (options & F_HDRINCL) {
6270b2f8b3fSMaxim Konovalov 		ip = (struct ip*)outpackhdr;
6280b2f8b3fSMaxim Konovalov 		if (!(options & (F_TTL | F_MTTL))) {
6290b2f8b3fSMaxim Konovalov 			mib[0] = CTL_NET;
6300b2f8b3fSMaxim Konovalov 			mib[1] = PF_INET;
6310b2f8b3fSMaxim Konovalov 			mib[2] = IPPROTO_IP;
6320b2f8b3fSMaxim Konovalov 			mib[3] = IPCTL_DEFTTL;
6330b2f8b3fSMaxim Konovalov 			sz = sizeof(ttl);
6340b2f8b3fSMaxim Konovalov 			if (sysctl(mib, 4, &ttl, &sz, NULL, 0) == -1)
6350b2f8b3fSMaxim Konovalov 				err(1, "sysctl(net.inet.ip.ttl)");
6360b2f8b3fSMaxim Konovalov 		}
6370b2f8b3fSMaxim Konovalov 		setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold));
6380b2f8b3fSMaxim Konovalov 		ip->ip_v = IPVERSION;
6390b2f8b3fSMaxim Konovalov 		ip->ip_hl = sizeof(struct ip) >> 2;
6400b2f8b3fSMaxim Konovalov 		ip->ip_tos = tos;
6410b2f8b3fSMaxim Konovalov 		ip->ip_id = 0;
6420b2f8b3fSMaxim Konovalov 		ip->ip_off = df ? IP_DF : 0;
6430b2f8b3fSMaxim Konovalov 		ip->ip_ttl = ttl;
6440b2f8b3fSMaxim Konovalov 		ip->ip_p = IPPROTO_ICMP;
64531eac03bSSean Chittenden 		ip->ip_src.s_addr = source ? sock_in.sin_addr.s_addr : INADDR_ANY;
6460b2f8b3fSMaxim Konovalov 		ip->ip_dst = to->sin_addr;
6470b2f8b3fSMaxim Konovalov         }
6488fae3551SRodney W. Grimes 	/* record route option */
6498fae3551SRodney W. Grimes 	if (options & F_RROUTE) {
6508fae3551SRodney W. Grimes #ifdef IP_OPTIONS
651039d6aa4SBill Fenner 		bzero(rspace, sizeof(rspace));
6528fae3551SRodney W. Grimes 		rspace[IPOPT_OPTVAL] = IPOPT_RR;
6538fae3551SRodney W. Grimes 		rspace[IPOPT_OLEN] = sizeof(rspace) - 1;
6548fae3551SRodney W. Grimes 		rspace[IPOPT_OFFSET] = IPOPT_MINOFF;
655039d6aa4SBill Fenner 		rspace[sizeof(rspace) - 1] = IPOPT_EOL;
6568fae3551SRodney W. Grimes 		if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace,
65743470e3bSGarrett Wollman 		    sizeof(rspace)) < 0)
65843470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_OPTIONS");
6598fae3551SRodney W. Grimes #else
66043470e3bSGarrett Wollman 		errx(EX_UNAVAILABLE,
66143470e3bSGarrett Wollman 		    "record route not available in this implementation");
6628fae3551SRodney W. Grimes #endif /* IP_OPTIONS */
6638fae3551SRodney W. Grimes 	}
6648fae3551SRodney W. Grimes 
665211bfbd2SRuslan Ermilov 	if (options & F_TTL) {
666211bfbd2SRuslan Ermilov 		if (setsockopt(s, IPPROTO_IP, IP_TTL, &ttl,
667211bfbd2SRuslan Ermilov 		    sizeof(ttl)) < 0) {
668211bfbd2SRuslan Ermilov 			err(EX_OSERR, "setsockopt IP_TTL");
669211bfbd2SRuslan Ermilov 		}
670211bfbd2SRuslan Ermilov 	}
67185456935SBill Fenner 	if (options & F_NOLOOP) {
67285456935SBill Fenner 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop,
67385456935SBill Fenner 		    sizeof(loop)) < 0) {
67443470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP");
67585456935SBill Fenner 		}
67685456935SBill Fenner 	}
67785456935SBill Fenner 	if (options & F_MTTL) {
678211bfbd2SRuslan Ermilov 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &mttl,
679211bfbd2SRuslan Ermilov 		    sizeof(mttl)) < 0) {
68043470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_TTL");
68185456935SBill Fenner 		}
68285456935SBill Fenner 	}
68385456935SBill Fenner 	if (options & F_MIF) {
68485456935SBill Fenner 		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr,
68585456935SBill Fenner 		    sizeof(ifaddr)) < 0) {
68643470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_IF");
68785456935SBill Fenner 		}
68885456935SBill Fenner 	}
689039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
690039d6aa4SBill Fenner 	{ int on = 1;
691039d6aa4SBill Fenner 	if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) < 0)
692039d6aa4SBill Fenner 		err(EX_OSERR, "setsockopt SO_TIMESTAMP");
693039d6aa4SBill Fenner 	}
694039d6aa4SBill Fenner #endif
6959ff95228SGleb Smirnoff 	if (sweepmax) {
6969ff95228SGleb Smirnoff 		if (sweepmin >= sweepmax)
6979ff95228SGleb Smirnoff 			errx(EX_USAGE, "Maximum packet size must be greater than the minimum packet size");
6989ff95228SGleb Smirnoff 
6999ff95228SGleb Smirnoff 		if (datalen != DEFDATALEN)
7009ff95228SGleb Smirnoff 			errx(EX_USAGE, "Packet size and ping sweep are mutually exclusive");
7019ff95228SGleb Smirnoff 
7029ff95228SGleb Smirnoff 		if (npackets > 0) {
7039ff95228SGleb Smirnoff 			snpackets = npackets;
7049ff95228SGleb Smirnoff 			npackets = 0;
7059ff95228SGleb Smirnoff 		} else
7069ff95228SGleb Smirnoff 			snpackets = 1;
7079ff95228SGleb Smirnoff 		datalen = sweepmin;
7089ff95228SGleb Smirnoff 		send_len = icmp_len + sweepmin;
7099ff95228SGleb Smirnoff 	}
7109ff95228SGleb Smirnoff 	if (options & F_SWEEP && !sweepmax)
7119ff95228SGleb Smirnoff 		errx(EX_USAGE, "Maximum sweep size must be specified");
71285456935SBill Fenner 
7138fae3551SRodney W. Grimes 	/*
7148fae3551SRodney W. Grimes 	 * When pinging the broadcast address, you can get a lot of answers.
7158fae3551SRodney W. Grimes 	 * Doing something so evil is useful if you are trying to stress the
7168fae3551SRodney W. Grimes 	 * ethernet, or just want to fill the arp cache to get some stuff for
71743470e3bSGarrett Wollman 	 * /etc/ethers.  But beware: RFC 1122 allows hosts to ignore broadcast
71843470e3bSGarrett Wollman 	 * or multicast pings if they wish.
7198fae3551SRodney W. Grimes 	 */
7204fba6582SMaxim Konovalov 
7214fba6582SMaxim Konovalov 	/*
7224fba6582SMaxim Konovalov 	 * XXX receive buffer needs undetermined space for mbuf overhead
7234fba6582SMaxim Konovalov 	 * as well.
7244fba6582SMaxim Konovalov 	 */
7254fba6582SMaxim Konovalov 	hold = IP_MAXPACKET + 128;
7268fae3551SRodney W. Grimes 	(void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
7278fae3551SRodney W. Grimes 	    sizeof(hold));
728261e59bbSMaxim Konovalov 	if (uid == 0)
729e8bd25ceSRobert Watson 		(void)setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&hold,
730e8bd25ceSRobert Watson 		    sizeof(hold));
731e8bd25ceSRobert Watson 
73299490edeSWarner Losh 	if (to->sin_family == AF_INET) {
73399490edeSWarner Losh 		(void)printf("PING %s (%s)", hostname,
73499490edeSWarner Losh 		    inet_ntoa(to->sin_addr));
73599490edeSWarner Losh 		if (source)
73699490edeSWarner Losh 			(void)printf(" from %s", shostname);
7379ff95228SGleb Smirnoff 		if (sweepmax)
7389ff95228SGleb Smirnoff 			(void)printf(": (%d ... %d) data bytes\n",
7399ff95228SGleb Smirnoff 			    sweepmin, sweepmax);
7409ff95228SGleb Smirnoff 		else
74199490edeSWarner Losh 			(void)printf(": %d data bytes\n", datalen);
7429ff95228SGleb Smirnoff 
7439ff95228SGleb Smirnoff 	} else {
7449ff95228SGleb Smirnoff 		if (sweepmax)
7459ff95228SGleb Smirnoff 			(void)printf("PING %s: (%d ... %d) data bytes\n",
7469ff95228SGleb Smirnoff 			    hostname, sweepmin, sweepmax);
7479ff95228SGleb Smirnoff 		else
7488fae3551SRodney W. Grimes 			(void)printf("PING %s: %d data bytes\n", hostname, datalen);
7499ff95228SGleb Smirnoff 	}
7508fae3551SRodney W. Grimes 
7517d81b35cSBruce Evans 	/*
752a2a00888SSean Eric Fagan 	 * Use sigaction() instead of signal() to get unambiguous semantics,
753a2a00888SSean Eric Fagan 	 * in particular with SA_RESTART not set.
7547d81b35cSBruce Evans 	 */
755a2a00888SSean Eric Fagan 
756f6bd468bSPaul Traina 	sigemptyset(&si_sa.sa_mask);
75737e5b2c6SSean Eric Fagan 	si_sa.sa_flags = 0;
758a2a00888SSean Eric Fagan 
759a2a00888SSean Eric Fagan 	si_sa.sa_handler = stopit;
760a2a00888SSean Eric Fagan 	if (sigaction(SIGINT, &si_sa, 0) == -1) {
761a2a00888SSean Eric Fagan 		err(EX_OSERR, "sigaction SIGINT");
762a2a00888SSean Eric Fagan 	}
763a2a00888SSean Eric Fagan 
764a2a00888SSean Eric Fagan 	si_sa.sa_handler = status;
76537e5b2c6SSean Eric Fagan 	if (sigaction(SIGINFO, &si_sa, 0) == -1) {
766624ff938SWarner Losh 		err(EX_OSERR, "sigaction");
76737e5b2c6SSean Eric Fagan 	}
768bf113f1bSBill Fumerola 
769bf113f1bSBill Fumerola         if (alarmtimeout > 0) {
7707237fd94SBill Fumerola 		si_sa.sa_handler = stopit;
7717237fd94SBill Fumerola 		if (sigaction(SIGALRM, &si_sa, 0) == -1)
7727237fd94SBill Fumerola 			err(EX_OSERR, "sigaction SIGALRM");
773bf113f1bSBill Fumerola         }
77437e5b2c6SSean Eric Fagan 
775039d6aa4SBill Fenner 	bzero(&msg, sizeof(msg));
776039d6aa4SBill Fenner 	msg.msg_name = (caddr_t)&from;
777039d6aa4SBill Fenner 	msg.msg_iov = &iov;
778039d6aa4SBill Fenner 	msg.msg_iovlen = 1;
779039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
780039d6aa4SBill Fenner 	msg.msg_control = (caddr_t)ctrl;
781039d6aa4SBill Fenner #endif
782039d6aa4SBill Fenner 	iov.iov_base = packet;
783e88178ddSMaxim Konovalov 	iov.iov_len = IP_MAXPACKET;
784039d6aa4SBill Fenner 
78532af342fSRuslan Ermilov 	if (preload == 0)
78632af342fSRuslan Ermilov 		pinger();		/* send the first ping */
78732af342fSRuslan Ermilov 	else {
78832af342fSRuslan Ermilov 		if (npackets != 0 && preload > npackets)
78932af342fSRuslan Ermilov 			preload = npackets;
7908fae3551SRodney W. Grimes 		while (preload--)	/* fire off them quickies */
7918fae3551SRodney W. Grimes 			pinger();
79232af342fSRuslan Ermilov 	}
79332af342fSRuslan Ermilov 	(void)gettimeofday(&last, NULL);
7948fae3551SRodney W. Grimes 
795039d6aa4SBill Fenner 	if (options & F_FLOOD) {
796039d6aa4SBill Fenner 		intvl.tv_sec = 0;
797039d6aa4SBill Fenner 		intvl.tv_usec = 10000;
798039d6aa4SBill Fenner 	} else {
799526f06b2SMatthew Dillon 		intvl.tv_sec = interval / 1000;
800526f06b2SMatthew Dillon 		intvl.tv_usec = interval % 1000 * 1000;
801039d6aa4SBill Fenner 	}
802039d6aa4SBill Fenner 
803261e59bbSMaxim Konovalov 	almost_done = 0;
8048f975bb3SBruce Evans 	while (!finish_up) {
805261e59bbSMaxim Konovalov 		struct timeval now, timeout;
806039d6aa4SBill Fenner 		fd_set rfds;
807261e59bbSMaxim Konovalov 		int cc, n;
8088fae3551SRodney W. Grimes 
8097d81b35cSBruce Evans 		check_status();
81031eac03bSSean Chittenden 		if ((unsigned)s >= FD_SETSIZE)
8117e5bbd68SJacques Vidrine 			errx(EX_OSERR, "descriptor too large");
812039d6aa4SBill Fenner 		FD_ZERO(&rfds);
813039d6aa4SBill Fenner 		FD_SET(s, &rfds);
814039d6aa4SBill Fenner 		(void)gettimeofday(&now, NULL);
815039d6aa4SBill Fenner 		timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec;
816039d6aa4SBill Fenner 		timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec;
817039d6aa4SBill Fenner 		while (timeout.tv_usec < 0) {
818039d6aa4SBill Fenner 			timeout.tv_usec += 1000000;
819039d6aa4SBill Fenner 			timeout.tv_sec--;
8208fae3551SRodney W. Grimes 		}
821e345a80dSPhilippe Charnier 		while (timeout.tv_usec >= 1000000) {
822039d6aa4SBill Fenner 			timeout.tv_usec -= 1000000;
823039d6aa4SBill Fenner 			timeout.tv_sec++;
824039d6aa4SBill Fenner 		}
825039d6aa4SBill Fenner 		if (timeout.tv_sec < 0)
826039d6aa4SBill Fenner 			timeout.tv_sec = timeout.tv_usec = 0;
827039d6aa4SBill Fenner 		n = select(s + 1, &rfds, NULL, NULL, &timeout);
8285e2cc0f4SStephen McKay 		if (n < 0)
8295e2cc0f4SStephen McKay 			continue;	/* Must be EINTR. */
830039d6aa4SBill Fenner 		if (n == 1) {
83131eac03bSSean Chittenden 			struct timeval *tv = NULL;
832039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
833039d6aa4SBill Fenner 			struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl;
834039d6aa4SBill Fenner 
835039d6aa4SBill Fenner 			msg.msg_controllen = sizeof(ctrl);
836039d6aa4SBill Fenner #endif
837039d6aa4SBill Fenner 			msg.msg_namelen = sizeof(from);
838039d6aa4SBill Fenner 			if ((cc = recvmsg(s, &msg, 0)) < 0) {
8398fae3551SRodney W. Grimes 				if (errno == EINTR)
8408fae3551SRodney W. Grimes 					continue;
841e345a80dSPhilippe Charnier 				warn("recvmsg");
8428fae3551SRodney W. Grimes 				continue;
8438fae3551SRodney W. Grimes 			}
844039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
845039d6aa4SBill Fenner 			if (cmsg->cmsg_level == SOL_SOCKET &&
846039d6aa4SBill Fenner 			    cmsg->cmsg_type == SCM_TIMESTAMP &&
84731eac03bSSean Chittenden 			    cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) {
848fa05a94cSJohn Birrell 				/* Copy to avoid alignment problems: */
849fa05a94cSJohn Birrell 				memcpy(&now, CMSG_DATA(cmsg), sizeof(now));
85031eac03bSSean Chittenden 				tv = &now;
851fa05a94cSJohn Birrell 			}
852039d6aa4SBill Fenner #endif
85331eac03bSSean Chittenden 			if (tv == NULL) {
854039d6aa4SBill Fenner 				(void)gettimeofday(&now, NULL);
85531eac03bSSean Chittenden 				tv = &now;
856039d6aa4SBill Fenner 			}
85731eac03bSSean Chittenden 			pr_pack((char *)packet, cc, &from, tv);
85831eac03bSSean Chittenden 			if ((options & F_ONCE && nreceived) ||
85931eac03bSSean Chittenden 			    (npackets && nreceived >= npackets))
8608fae3551SRodney W. Grimes 				break;
8618fae3551SRodney W. Grimes 		}
8625e2cc0f4SStephen McKay 		if (n == 0 || options & F_FLOOD) {
8639ff95228SGleb Smirnoff 			if (sweepmax && sntransmitted == snpackets) {
8649ff95228SGleb Smirnoff 				for (i = 0; i < sweepincr ; ++i)
8659ff95228SGleb Smirnoff 					*datap++ = i;
8669ff95228SGleb Smirnoff 				datalen += sweepincr;
8679ff95228SGleb Smirnoff 				if (datalen > sweepmax)
8689ff95228SGleb Smirnoff 					break;
8699ff95228SGleb Smirnoff 				send_len = icmp_len + datalen;
8709ff95228SGleb Smirnoff 				sntransmitted = 0;
8719ff95228SGleb Smirnoff 			}
872039d6aa4SBill Fenner 			if (!npackets || ntransmitted < npackets)
873039d6aa4SBill Fenner 				pinger();
874039d6aa4SBill Fenner 			else {
875039d6aa4SBill Fenner 				if (almost_done)
876039d6aa4SBill Fenner 					break;
877039d6aa4SBill Fenner 				almost_done = 1;
8785e2cc0f4SStephen McKay 				intvl.tv_usec = 0;
879039d6aa4SBill Fenner 				if (nreceived) {
880039d6aa4SBill Fenner 					intvl.tv_sec = 2 * tmax / 1000;
881039d6aa4SBill Fenner 					if (!intvl.tv_sec)
882039d6aa4SBill Fenner 						intvl.tv_sec = 1;
883039d6aa4SBill Fenner 				} else
884039d6aa4SBill Fenner 					intvl.tv_sec = MAXWAIT;
885039d6aa4SBill Fenner 			}
886039d6aa4SBill Fenner 			(void)gettimeofday(&last, NULL);
88725107197SIan Dowse 			if (ntransmitted - nreceived - 1 > nmissedmax) {
88825107197SIan Dowse 				nmissedmax = ntransmitted - nreceived - 1;
88925107197SIan Dowse 				if (options & F_MISSED)
890ca517ad8SPoul-Henning Kamp 					(void)write(STDOUT_FILENO, &BBELL, 1);
891039d6aa4SBill Fenner 			}
892039d6aa4SBill Fenner 		}
89325107197SIan Dowse 	}
8948f975bb3SBruce Evans 	finish();
8958fae3551SRodney W. Grimes 	/* NOTREACHED */
896f78ac61bSWarner Losh 	exit(0);	/* Make the compiler happy */
8978fae3551SRodney W. Grimes }
8988fae3551SRodney W. Grimes 
8998fae3551SRodney W. Grimes /*
9008f975bb3SBruce Evans  * stopit --
9018f975bb3SBruce Evans  *	Set the global bit that causes the main loop to quit.
9028f975bb3SBruce Evans  * Do NOT call finish() from here, since finish() does far too much
9038f975bb3SBruce Evans  * to be called from a signal handler.
904515dd2faSJulian Elischer  */
905515dd2faSJulian Elischer void
9068f975bb3SBruce Evans stopit(sig)
907c6facae4SMaxim Konovalov 	int sig __unused;
908515dd2faSJulian Elischer {
909efc8588dSDavid E. O'Brien 
910c8bb99e5SIan Dowse 	/*
911c8bb99e5SIan Dowse 	 * When doing reverse DNS lookups, the finish_up flag might not
912c8bb99e5SIan Dowse 	 * be noticed for a while.  Just exit if we get a second SIGINT.
913c8bb99e5SIan Dowse 	 */
914c8bb99e5SIan Dowse 	if (!(options & F_NUMERIC) && finish_up)
915c8bb99e5SIan Dowse 		_exit(nreceived ? 0 : 2);
916515dd2faSJulian Elischer 	finish_up = 1;
917515dd2faSJulian Elischer }
918515dd2faSJulian Elischer 
919515dd2faSJulian Elischer /*
9208fae3551SRodney W. Grimes  * pinger --
9218fae3551SRodney W. Grimes  *	Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
9228fae3551SRodney W. Grimes  * will be added on by the kernel.  The ID field is our UNIX process ID,
923eb1543c6SMatthew N. Dodd  * and the sequence number is an ascending integer.  The first TIMEVAL_LEN
9244fba6582SMaxim Konovalov  * bytes of the data portion are used to hold a UNIX "timeval" struct in
9254fba6582SMaxim Konovalov  * host byte-order, to compute the round-trip time.
9268fae3551SRodney W. Grimes  */
92743470e3bSGarrett Wollman static void
92843470e3bSGarrett Wollman pinger(void)
9298fae3551SRodney W. Grimes {
930eb1543c6SMatthew N. Dodd 	struct timeval now;
93113e3f0b7SMaxim Konovalov 	struct tv32 tv32;
9320b2f8b3fSMaxim Konovalov 	struct ip *ip;
9333d438ad6SDavid E. O'Brien 	struct icmp *icp;
934efc8588dSDavid E. O'Brien 	int cc, i;
9350b2f8b3fSMaxim Konovalov 	u_char *packet;
9368fae3551SRodney W. Grimes 
9370b2f8b3fSMaxim Konovalov 	packet = outpack;
9388fae3551SRodney W. Grimes 	icp = (struct icmp *)outpack;
939eb1543c6SMatthew N. Dodd 	icp->icmp_type = icmp_type;
9408fae3551SRodney W. Grimes 	icp->icmp_code = 0;
9418fae3551SRodney W. Grimes 	icp->icmp_cksum = 0;
9425db89bc7SBill Fenner 	icp->icmp_seq = htons(ntransmitted);
9438fae3551SRodney W. Grimes 	icp->icmp_id = ident;			/* ID */
9448fae3551SRodney W. Grimes 
9455db89bc7SBill Fenner 	CLR(ntransmitted % mx_dup_ck);
9468fae3551SRodney W. Grimes 
947eb1543c6SMatthew N. Dodd 	if ((options & F_TIME) || timing) {
948eb1543c6SMatthew N. Dodd 		(void)gettimeofday(&now, NULL);
9498fae3551SRodney W. Grimes 
95013e3f0b7SMaxim Konovalov 		tv32.tv32_sec = htonl(now.tv_sec);
95113e3f0b7SMaxim Konovalov 		tv32.tv32_usec = htonl(now.tv_usec);
952eb1543c6SMatthew N. Dodd 		if (options & F_TIME)
953eb1543c6SMatthew N. Dodd 			icp->icmp_otime = htonl((now.tv_sec % (24*60*60))
954eb1543c6SMatthew N. Dodd 				* 1000 + now.tv_usec / 1000);
955eb1543c6SMatthew N. Dodd 		if (timing)
95613e3f0b7SMaxim Konovalov 			bcopy((void *)&tv32,
9570fe0c0ccSMaxim Konovalov 			    (void *)&outpack[ICMP_MINLEN + phdr_len],
95813e3f0b7SMaxim Konovalov 			    sizeof(tv32));
959eb1543c6SMatthew N. Dodd 	}
960eb1543c6SMatthew N. Dodd 
9610fe0c0ccSMaxim Konovalov 	cc = ICMP_MINLEN + phdr_len + datalen;
9628fae3551SRodney W. Grimes 
9638fae3551SRodney W. Grimes 	/* compute ICMP checksum here */
9648fae3551SRodney W. Grimes 	icp->icmp_cksum = in_cksum((u_short *)icp, cc);
9658fae3551SRodney W. Grimes 
9660b2f8b3fSMaxim Konovalov 	if (options & F_HDRINCL) {
9670b2f8b3fSMaxim Konovalov 		cc += sizeof(struct ip);
9680b2f8b3fSMaxim Konovalov 		ip = (struct ip *)outpackhdr;
9690b2f8b3fSMaxim Konovalov 		ip->ip_len = cc;
9700b2f8b3fSMaxim Konovalov 		ip->ip_sum = in_cksum((u_short *)outpackhdr, cc);
9710b2f8b3fSMaxim Konovalov 		packet = outpackhdr;
9720b2f8b3fSMaxim Konovalov 	}
9730b2f8b3fSMaxim Konovalov 	i = sendto(s, (char *)packet, cc, 0, (struct sockaddr *)&whereto,
974d389e86aSMatt Jacob 	    sizeof(whereto));
9758fae3551SRodney W. Grimes 
9768fae3551SRodney W. Grimes 	if (i < 0 || i != cc)  {
97743470e3bSGarrett Wollman 		if (i < 0) {
9788f975bb3SBruce Evans 			if (options & F_FLOOD && errno == ENOBUFS) {
979515dd2faSJulian Elischer 				usleep(FLOOD_BACKOFF);
980515dd2faSJulian Elischer 				return;
981515dd2faSJulian Elischer 			}
98243470e3bSGarrett Wollman 			warn("sendto");
98343470e3bSGarrett Wollman 		} else {
98443470e3bSGarrett Wollman 			warn("%s: partial write: %d of %d bytes",
985416aa49bSPoul-Henning Kamp 			     hostname, i, cc);
9868fae3551SRodney W. Grimes 		}
987363d7bbeSJulian Elischer 	}
988363d7bbeSJulian Elischer 	ntransmitted++;
9899ff95228SGleb Smirnoff 	sntransmitted++;
9908fae3551SRodney W. Grimes 	if (!(options & F_QUIET) && options & F_FLOOD)
9918fae3551SRodney W. Grimes 		(void)write(STDOUT_FILENO, &DOT, 1);
9928fae3551SRodney W. Grimes }
9938fae3551SRodney W. Grimes 
9948fae3551SRodney W. Grimes /*
9958fae3551SRodney W. Grimes  * pr_pack --
9968fae3551SRodney W. Grimes  *	Print out the packet, if it came from us.  This logic is necessary
9978fae3551SRodney W. Grimes  * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
9988fae3551SRodney W. Grimes  * which arrive ('tis only fair).  This permits multiple copies of this
9998fae3551SRodney W. Grimes  * program to be run without having intermingled output (or statistics!).
10008fae3551SRodney W. Grimes  */
100143470e3bSGarrett Wollman static void
1002039d6aa4SBill Fenner pr_pack(buf, cc, from, tv)
10038fae3551SRodney W. Grimes 	char *buf;
10048fae3551SRodney W. Grimes 	int cc;
10058fae3551SRodney W. Grimes 	struct sockaddr_in *from;
1006039d6aa4SBill Fenner 	struct timeval *tv;
10078fae3551SRodney W. Grimes {
10089b219646SPeter Wemm 	struct in_addr ina;
10099b219646SPeter Wemm 	u_char *cp, *dp;
10103d438ad6SDavid E. O'Brien 	struct icmp *icp;
10118fae3551SRodney W. Grimes 	struct ip *ip;
10129d2b0ab8SPeter Wemm 	const void *tp;
10138f975bb3SBruce Evans 	double triptime;
1014e88178ddSMaxim Konovalov 	int dupflag, hlen, i, j, recv_len, seq;
1015efc8588dSDavid E. O'Brien 	static int old_rrlen;
1016efc8588dSDavid E. O'Brien 	static char old_rr[MAX_IPOPTLEN];
10178fae3551SRodney W. Grimes 
10188fae3551SRodney W. Grimes 	/* Check the IP header */
10198fae3551SRodney W. Grimes 	ip = (struct ip *)buf;
10208fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
1021e88178ddSMaxim Konovalov 	recv_len = cc;
10228fae3551SRodney W. Grimes 	if (cc < hlen + ICMP_MINLEN) {
10238fae3551SRodney W. Grimes 		if (options & F_VERBOSE)
102443470e3bSGarrett Wollman 			warn("packet too short (%d bytes) from %s", cc,
102543470e3bSGarrett Wollman 			     inet_ntoa(from->sin_addr));
10268fae3551SRodney W. Grimes 		return;
10278fae3551SRodney W. Grimes 	}
10288fae3551SRodney W. Grimes 
10298fae3551SRodney W. Grimes 	/* Now the ICMP part */
10308fae3551SRodney W. Grimes 	cc -= hlen;
10318fae3551SRodney W. Grimes 	icp = (struct icmp *)(buf + hlen);
1032eb1543c6SMatthew N. Dodd 	if (icp->icmp_type == icmp_type_rsp) {
10338fae3551SRodney W. Grimes 		if (icp->icmp_id != ident)
10348fae3551SRodney W. Grimes 			return;			/* 'Twas not our ECHO */
10358fae3551SRodney W. Grimes 		++nreceived;
10368f975bb3SBruce Evans 		triptime = 0.0;
10378fae3551SRodney W. Grimes 		if (timing) {
1038d32ff037SJohn Birrell 			struct timeval tv1;
103913e3f0b7SMaxim Konovalov 			struct tv32 tv32;
10408fae3551SRodney W. Grimes #ifndef icmp_data
10419d2b0ab8SPeter Wemm 			tp = &icp->icmp_ip;
10428fae3551SRodney W. Grimes #else
10439d2b0ab8SPeter Wemm 			tp = icp->icmp_data;
10448fae3551SRodney W. Grimes #endif
10454eae39bfSStefan Farfeleder 			tp = (const char *)tp + phdr_len;
1046143008a1SMatthew N. Dodd 
104747e9b3eaSMatthew N. Dodd 			if (cc - ICMP_MINLEN - phdr_len >= sizeof(tv1)) {
10489d2b0ab8SPeter Wemm 				/* Copy to avoid alignment problems: */
104913e3f0b7SMaxim Konovalov 				memcpy(&tv32, tp, sizeof(tv32));
105013e3f0b7SMaxim Konovalov 				tv1.tv_sec = ntohl(tv32.tv32_sec);
105113e3f0b7SMaxim Konovalov 				tv1.tv_usec = ntohl(tv32.tv32_usec);
1052039d6aa4SBill Fenner 				tvsub(tv, &tv1);
1053039d6aa4SBill Fenner  				triptime = ((double)tv->tv_sec) * 1000.0 +
1054039d6aa4SBill Fenner  				    ((double)tv->tv_usec) / 1000.0;
10558fae3551SRodney W. Grimes 				tsum += triptime;
10563109a910SGarrett Wollman 				tsumsq += triptime * triptime;
10578fae3551SRodney W. Grimes 				if (triptime < tmin)
10588fae3551SRodney W. Grimes 					tmin = triptime;
10598fae3551SRodney W. Grimes 				if (triptime > tmax)
10608fae3551SRodney W. Grimes 					tmax = triptime;
106147e9b3eaSMatthew N. Dodd 			} else
106247e9b3eaSMatthew N. Dodd 				timing = 0;
10638fae3551SRodney W. Grimes 		}
10648fae3551SRodney W. Grimes 
10655db89bc7SBill Fenner 		seq = ntohs(icp->icmp_seq);
10665db89bc7SBill Fenner 
10675db89bc7SBill Fenner 		if (TST(seq % mx_dup_ck)) {
10688fae3551SRodney W. Grimes 			++nrepeats;
10698fae3551SRodney W. Grimes 			--nreceived;
10708fae3551SRodney W. Grimes 			dupflag = 1;
10718fae3551SRodney W. Grimes 		} else {
10725db89bc7SBill Fenner 			SET(seq % mx_dup_ck);
10738fae3551SRodney W. Grimes 			dupflag = 0;
10748fae3551SRodney W. Grimes 		}
10758fae3551SRodney W. Grimes 
10768fae3551SRodney W. Grimes 		if (options & F_QUIET)
10778fae3551SRodney W. Grimes 			return;
10788fae3551SRodney W. Grimes 
10798fae3551SRodney W. Grimes 		if (options & F_FLOOD)
10808fae3551SRodney W. Grimes 			(void)write(STDOUT_FILENO, &BSPACE, 1);
10818fae3551SRodney W. Grimes 		else {
10828fae3551SRodney W. Grimes 			(void)printf("%d bytes from %s: icmp_seq=%u", cc,
10838fae3551SRodney W. Grimes 			   inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr),
10845db89bc7SBill Fenner 			   seq);
10858fae3551SRodney W. Grimes 			(void)printf(" ttl=%d", ip->ip_ttl);
10868fae3551SRodney W. Grimes 			if (timing)
1087d410b6f1SDavid Greenman 				(void)printf(" time=%.3f ms", triptime);
10888fae3551SRodney W. Grimes 			if (dupflag)
10898fae3551SRodney W. Grimes 				(void)printf(" (DUP!)");
1090772dfa72SDaniel O'Callaghan 			if (options & F_AUDIBLE)
1091ca517ad8SPoul-Henning Kamp 				(void)write(STDOUT_FILENO, &BBELL, 1);
1092143008a1SMatthew N. Dodd 			if (options & F_MASK) {
1093143008a1SMatthew N. Dodd 				/* Just prentend this cast isn't ugly */
1094143008a1SMatthew N. Dodd 				(void)printf(" mask=%s",
1095143008a1SMatthew N. Dodd 					pr_addr(*(struct in_addr *)&(icp->icmp_mask)));
1096143008a1SMatthew N. Dodd 			}
1097eb1543c6SMatthew N. Dodd 			if (options & F_TIME) {
1098eb1543c6SMatthew N. Dodd 				(void)printf(" tso=%s", pr_ntime(icp->icmp_otime));
1099eb1543c6SMatthew N. Dodd 				(void)printf(" tsr=%s", pr_ntime(icp->icmp_rtime));
1100eb1543c6SMatthew N. Dodd 				(void)printf(" tst=%s", pr_ntime(icp->icmp_ttime));
1101eb1543c6SMatthew N. Dodd 			}
1102e88178ddSMaxim Konovalov 			if (recv_len != send_len) {
1103e88178ddSMaxim Konovalov                         	(void)printf(
1104e88178ddSMaxim Konovalov 				     "\nwrong total length %d instead of %d",
1105e88178ddSMaxim Konovalov 				     recv_len, send_len);
1106e88178ddSMaxim Konovalov 			}
11078fae3551SRodney W. Grimes 			/* check the data */
1108eb1543c6SMatthew N. Dodd 			cp = (u_char*)&icp->icmp_data[phdr_len];
11090fe0c0ccSMaxim Konovalov 			dp = &outpack[ICMP_MINLEN + phdr_len];
111047e9b3eaSMatthew N. Dodd 			cc -= ICMP_MINLEN + phdr_len;
111129dccd6aSMaxim Konovalov 			i = 0;
111229dccd6aSMaxim Konovalov 			if (timing) {   /* don't check variable timestamp */
111329dccd6aSMaxim Konovalov 				cp += TIMEVAL_LEN;
111429dccd6aSMaxim Konovalov 				dp += TIMEVAL_LEN;
111529dccd6aSMaxim Konovalov 				cc -= TIMEVAL_LEN;
111629dccd6aSMaxim Konovalov 				i += TIMEVAL_LEN;
111729dccd6aSMaxim Konovalov 			}
111829dccd6aSMaxim Konovalov 			for (; i < datalen && cc > 0; ++i, ++cp, ++dp, --cc) {
11198fae3551SRodney W. Grimes 				if (*cp != *dp) {
11208fae3551SRodney W. Grimes 	(void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x",
11218fae3551SRodney W. Grimes 	    i, *dp, *cp);
11221ad0b1beSMaxim Konovalov 					(void)printf("\ncp:");
11238fae3551SRodney W. Grimes 					cp = (u_char*)&icp->icmp_data[0];
1124d32ff037SJohn Birrell 					for (i = 0; i < datalen; ++i, ++cp) {
1125e88178ddSMaxim Konovalov 						if ((i % 16) == 8)
1126d32ff037SJohn Birrell 							(void)printf("\n\t");
1127e88178ddSMaxim Konovalov 						(void)printf("%2x ", *cp);
1128d32ff037SJohn Birrell 					}
11291ad0b1beSMaxim Konovalov 					(void)printf("\ndp:");
11300fe0c0ccSMaxim Konovalov 					cp = &outpack[ICMP_MINLEN];
1131d32ff037SJohn Birrell 					for (i = 0; i < datalen; ++i, ++cp) {
1132e88178ddSMaxim Konovalov 						if ((i % 16) == 8)
11338fae3551SRodney W. Grimes 							(void)printf("\n\t");
1134e88178ddSMaxim Konovalov 						(void)printf("%2x ", *cp);
11358fae3551SRodney W. Grimes 					}
11368fae3551SRodney W. Grimes 					break;
11378fae3551SRodney W. Grimes 				}
11388fae3551SRodney W. Grimes 			}
11398fae3551SRodney W. Grimes 		}
11408fae3551SRodney W. Grimes 	} else {
1141ef9e6dc7SBill Fenner 		/*
1142ef9e6dc7SBill Fenner 		 * We've got something other than an ECHOREPLY.
1143ef9e6dc7SBill Fenner 		 * See if it's a reply to something that we sent.
1144ef9e6dc7SBill Fenner 		 * We can compare IP destination, protocol,
1145ef9e6dc7SBill Fenner 		 * and ICMP type and ID.
1146f78ac61bSWarner Losh 		 *
1147f78ac61bSWarner Losh 		 * Only print all the error messages if we are running
1148f78ac61bSWarner Losh 		 * as root to avoid leaking information not normally
1149f78ac61bSWarner Losh 		 * available to those not running as root.
1150ef9e6dc7SBill Fenner 		 */
1151ef9e6dc7SBill Fenner #ifndef icmp_data
1152ef9e6dc7SBill Fenner 		struct ip *oip = &icp->icmp_ip;
1153ef9e6dc7SBill Fenner #else
1154ef9e6dc7SBill Fenner 		struct ip *oip = (struct ip *)icp->icmp_data;
1155ef9e6dc7SBill Fenner #endif
1156ef9e6dc7SBill Fenner 		struct icmp *oicmp = (struct icmp *)(oip + 1);
1157ef9e6dc7SBill Fenner 
1158ee2bf734SWarner Losh 		if (((options & F_VERBOSE) && uid == 0) ||
1159ef9e6dc7SBill Fenner 		    (!(options & F_QUIET2) &&
1160d389e86aSMatt Jacob 		     (oip->ip_dst.s_addr == whereto.sin_addr.s_addr) &&
1161ef9e6dc7SBill Fenner 		     (oip->ip_p == IPPROTO_ICMP) &&
1162ef9e6dc7SBill Fenner 		     (oicmp->icmp_type == ICMP_ECHO) &&
1163ef9e6dc7SBill Fenner 		     (oicmp->icmp_id == ident))) {
11648fae3551SRodney W. Grimes 		    (void)printf("%d bytes from %s: ", cc,
116543470e3bSGarrett Wollman 			pr_addr(from->sin_addr));
11668fae3551SRodney W. Grimes 		    pr_icmph(icp);
1167ef9e6dc7SBill Fenner 		} else
1168ef9e6dc7SBill Fenner 		    return;
11698fae3551SRodney W. Grimes 	}
11708fae3551SRodney W. Grimes 
11718fae3551SRodney W. Grimes 	/* Display any IP options */
11728fae3551SRodney W. Grimes 	cp = (u_char *)buf + sizeof(struct ip);
11738fae3551SRodney W. Grimes 
11748fae3551SRodney W. Grimes 	for (; hlen > (int)sizeof(struct ip); --hlen, ++cp)
11758fae3551SRodney W. Grimes 		switch (*cp) {
11768fae3551SRodney W. Grimes 		case IPOPT_EOL:
11778fae3551SRodney W. Grimes 			hlen = 0;
11788fae3551SRodney W. Grimes 			break;
11798fae3551SRodney W. Grimes 		case IPOPT_LSRR:
1180cb75aca7SMaxim Konovalov 		case IPOPT_SSRR:
1181cb75aca7SMaxim Konovalov 			(void)printf(*cp == IPOPT_LSRR ?
1182cb75aca7SMaxim Konovalov 			    "\nLSRR: " : "\nSSRR: ");
1183301207dfSMaxim Konovalov 			j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1;
11848fae3551SRodney W. Grimes 			hlen -= 2;
1185301207dfSMaxim Konovalov 			cp += 2;
11863c721ab3SMaxim Konovalov 			if (j >= INADDR_LEN &&
11873c721ab3SMaxim Konovalov 			    j <= hlen - (int)sizeof(struct ip)) {
11888fae3551SRodney W. Grimes 				for (;;) {
1189301207dfSMaxim Konovalov 					bcopy(++cp, &ina.s_addr, INADDR_LEN);
1190301207dfSMaxim Konovalov 					if (ina.s_addr == 0)
11911ad0b1beSMaxim Konovalov 						(void)printf("\t0.0.0.0");
1192301207dfSMaxim Konovalov 					else
11931ad0b1beSMaxim Konovalov 						(void)printf("\t%s",
11941ad0b1beSMaxim Konovalov 						     pr_addr(ina));
1195301207dfSMaxim Konovalov 					hlen -= INADDR_LEN;
1196301207dfSMaxim Konovalov 					cp += INADDR_LEN - 1;
1197301207dfSMaxim Konovalov 					j -= INADDR_LEN;
1198301207dfSMaxim Konovalov 					if (j < INADDR_LEN)
11998fae3551SRodney W. Grimes 						break;
12008fae3551SRodney W. Grimes 					(void)putchar('\n');
12018fae3551SRodney W. Grimes 				}
1202301207dfSMaxim Konovalov 			} else
1203301207dfSMaxim Konovalov 				(void)printf("\t(truncated route)\n");
12048fae3551SRodney W. Grimes 			break;
12058fae3551SRodney W. Grimes 		case IPOPT_RR:
1206301207dfSMaxim Konovalov 			j = cp[IPOPT_OLEN];		/* get length */
1207301207dfSMaxim Konovalov 			i = cp[IPOPT_OFFSET];		/* and pointer */
12088fae3551SRodney W. Grimes 			hlen -= 2;
1209301207dfSMaxim Konovalov 			cp += 2;
12108fae3551SRodney W. Grimes 			if (i > j)
12118fae3551SRodney W. Grimes 				i = j;
1212301207dfSMaxim Konovalov 			i = i - IPOPT_MINOFF + 1;
1213301207dfSMaxim Konovalov 			if (i < 0 || i > (hlen - (int)sizeof(struct ip))) {
1214301207dfSMaxim Konovalov 				old_rrlen = 0;
12158fae3551SRodney W. Grimes 				continue;
1216301207dfSMaxim Konovalov 			}
12178fae3551SRodney W. Grimes 			if (i == old_rrlen
12188fae3551SRodney W. Grimes 			    && !bcmp((char *)cp, old_rr, i)
12198fae3551SRodney W. Grimes 			    && !(options & F_FLOOD)) {
12208fae3551SRodney W. Grimes 				(void)printf("\t(same route)");
12218fae3551SRodney W. Grimes 				hlen -= i;
12228fae3551SRodney W. Grimes 				cp += i;
12238fae3551SRodney W. Grimes 				break;
12248fae3551SRodney W. Grimes 			}
12258fae3551SRodney W. Grimes 			old_rrlen = i;
12268fae3551SRodney W. Grimes 			bcopy((char *)cp, old_rr, i);
12278fae3551SRodney W. Grimes 			(void)printf("\nRR: ");
1228301207dfSMaxim Konovalov 			if (i >= INADDR_LEN &&
1229301207dfSMaxim Konovalov 			    i <= hlen - (int)sizeof(struct ip)) {
12308fae3551SRodney W. Grimes 				for (;;) {
1231301207dfSMaxim Konovalov 					bcopy(++cp, &ina.s_addr, INADDR_LEN);
1232301207dfSMaxim Konovalov 					if (ina.s_addr == 0)
12331ad0b1beSMaxim Konovalov 						(void)printf("\t0.0.0.0");
1234301207dfSMaxim Konovalov 					else
1235301207dfSMaxim Konovalov 						(void)printf("\t%s",
1236301207dfSMaxim Konovalov 						     pr_addr(ina));
1237301207dfSMaxim Konovalov 					hlen -= INADDR_LEN;
1238301207dfSMaxim Konovalov 					cp += INADDR_LEN - 1;
1239301207dfSMaxim Konovalov 					i -= INADDR_LEN;
1240301207dfSMaxim Konovalov 					if (i < INADDR_LEN)
12418fae3551SRodney W. Grimes 						break;
12428fae3551SRodney W. Grimes 					(void)putchar('\n');
12438fae3551SRodney W. Grimes 				}
1244301207dfSMaxim Konovalov 			} else
1245301207dfSMaxim Konovalov 				(void)printf("\t(truncated route)");
12468fae3551SRodney W. Grimes 			break;
12478fae3551SRodney W. Grimes 		case IPOPT_NOP:
12488fae3551SRodney W. Grimes 			(void)printf("\nNOP");
12498fae3551SRodney W. Grimes 			break;
12508fae3551SRodney W. Grimes 		default:
12518fae3551SRodney W. Grimes 			(void)printf("\nunknown option %x", *cp);
12528fae3551SRodney W. Grimes 			break;
12538fae3551SRodney W. Grimes 		}
12548fae3551SRodney W. Grimes 	if (!(options & F_FLOOD)) {
12558fae3551SRodney W. Grimes 		(void)putchar('\n');
12568fae3551SRodney W. Grimes 		(void)fflush(stdout);
12578fae3551SRodney W. Grimes 	}
12588fae3551SRodney W. Grimes }
12598fae3551SRodney W. Grimes 
12608fae3551SRodney W. Grimes /*
12618fae3551SRodney W. Grimes  * in_cksum --
12628fae3551SRodney W. Grimes  *	Checksum routine for Internet Protocol family headers (C Version)
12638fae3551SRodney W. Grimes  */
126443470e3bSGarrett Wollman u_short
12658fae3551SRodney W. Grimes in_cksum(addr, len)
12668fae3551SRodney W. Grimes 	u_short *addr;
12678fae3551SRodney W. Grimes 	int len;
12688fae3551SRodney W. Grimes {
1269efc8588dSDavid E. O'Brien 	int nleft, sum;
1270efc8588dSDavid E. O'Brien 	u_short *w;
12713ec12efeSPierre Beyssac 	union {
127209333e78SPierre Beyssac 		u_short	us;
127309333e78SPierre Beyssac 		u_char	uc[2];
127409333e78SPierre Beyssac 	} last;
127509333e78SPierre Beyssac 	u_short answer;
12768fae3551SRodney W. Grimes 
1277efc8588dSDavid E. O'Brien 	nleft = len;
1278efc8588dSDavid E. O'Brien 	sum = 0;
1279efc8588dSDavid E. O'Brien 	w = addr;
1280efc8588dSDavid E. O'Brien 
12818fae3551SRodney W. Grimes 	/*
12828fae3551SRodney W. Grimes 	 * Our algorithm is simple, using a 32 bit accumulator (sum), we add
12838fae3551SRodney W. Grimes 	 * sequential 16 bit words to it, and at the end, fold back all the
12848fae3551SRodney W. Grimes 	 * carry bits from the top 16 bits into the lower 16 bits.
12858fae3551SRodney W. Grimes 	 */
12868fae3551SRodney W. Grimes 	while (nleft > 1)  {
12878fae3551SRodney W. Grimes 		sum += *w++;
12888fae3551SRodney W. Grimes 		nleft -= 2;
12898fae3551SRodney W. Grimes 	}
12908fae3551SRodney W. Grimes 
12918fae3551SRodney W. Grimes 	/* mop up an odd byte, if necessary */
12928fae3551SRodney W. Grimes 	if (nleft == 1) {
129309333e78SPierre Beyssac 		last.uc[0] = *(u_char *)w;
129409333e78SPierre Beyssac 		last.uc[1] = 0;
129509333e78SPierre Beyssac 		sum += last.us;
12968fae3551SRodney W. Grimes 	}
12978fae3551SRodney W. Grimes 
12988fae3551SRodney W. Grimes 	/* add back carry outs from top 16 bits to low 16 bits */
12998fae3551SRodney W. Grimes 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
13008fae3551SRodney W. Grimes 	sum += (sum >> 16);			/* add carry */
130109333e78SPierre Beyssac 	answer = ~sum;				/* truncate to 16 bits */
130209333e78SPierre Beyssac 	return(answer);
13038fae3551SRodney W. Grimes }
13048fae3551SRodney W. Grimes 
13058fae3551SRodney W. Grimes /*
13068fae3551SRodney W. Grimes  * tvsub --
13078fae3551SRodney W. Grimes  *	Subtract 2 timeval structs:  out = out - in.  Out is assumed to
13088fae3551SRodney W. Grimes  * be >= in.
13098fae3551SRodney W. Grimes  */
131043470e3bSGarrett Wollman static void
13118fae3551SRodney W. Grimes tvsub(out, in)
13123d438ad6SDavid E. O'Brien 	struct timeval *out, *in;
13138fae3551SRodney W. Grimes {
1314efc8588dSDavid E. O'Brien 
13158fae3551SRodney W. Grimes 	if ((out->tv_usec -= in->tv_usec) < 0) {
13168fae3551SRodney W. Grimes 		--out->tv_sec;
13178fae3551SRodney W. Grimes 		out->tv_usec += 1000000;
13188fae3551SRodney W. Grimes 	}
13198fae3551SRodney W. Grimes 	out->tv_sec -= in->tv_sec;
13208fae3551SRodney W. Grimes }
13218fae3551SRodney W. Grimes 
13228fae3551SRodney W. Grimes /*
1323badd8138SSean Eric Fagan  * status --
1324badd8138SSean Eric Fagan  *	Print out statistics when SIGINFO is received.
1325badd8138SSean Eric Fagan  */
1326badd8138SSean Eric Fagan 
132743470e3bSGarrett Wollman static void
13287d81b35cSBruce Evans status(sig)
1329c6facae4SMaxim Konovalov 	int sig __unused;
13307d81b35cSBruce Evans {
1331efc8588dSDavid E. O'Brien 
133237e5b2c6SSean Eric Fagan 	siginfo_p = 1;
133337e5b2c6SSean Eric Fagan }
133437e5b2c6SSean Eric Fagan 
133543470e3bSGarrett Wollman static void
133637e5b2c6SSean Eric Fagan check_status()
1337badd8138SSean Eric Fagan {
1338efc8588dSDavid E. O'Brien 
133937e5b2c6SSean Eric Fagan 	if (siginfo_p) {
134037e5b2c6SSean Eric Fagan 		siginfo_p = 0;
1341aed98a27SMaxim Konovalov 		(void)fprintf(stderr, "\r%ld/%ld packets received (%.0f%%)",
13427d81b35cSBruce Evans 		    nreceived, ntransmitted,
1343aed98a27SMaxim Konovalov 		    ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0);
1344aed98a27SMaxim Konovalov 		if (nreceived && timing)
1345aed98a27SMaxim Konovalov 			(void)fprintf(stderr, " %.3f min / %.3f avg / %.3f max",
1346aed98a27SMaxim Konovalov 			    tmin, tsum / (nreceived + nrepeats), tmax);
1347aed98a27SMaxim Konovalov 		(void)fprintf(stderr, "\n");
134837e5b2c6SSean Eric Fagan 	}
1349badd8138SSean Eric Fagan }
1350badd8138SSean Eric Fagan 
1351badd8138SSean Eric Fagan /*
13528fae3551SRodney W. Grimes  * finish --
13538fae3551SRodney W. Grimes  *	Print out statistics, and give up.
13548fae3551SRodney W. Grimes  */
135543470e3bSGarrett Wollman static void
13568f975bb3SBruce Evans finish()
13578fae3551SRodney W. Grimes {
13588fae3551SRodney W. Grimes 
13598fae3551SRodney W. Grimes 	(void)signal(SIGINT, SIG_IGN);
1360a2a00888SSean Eric Fagan 	(void)signal(SIGALRM, SIG_IGN);
13618fae3551SRodney W. Grimes 	(void)putchar('\n');
13628fae3551SRodney W. Grimes 	(void)fflush(stdout);
13638fae3551SRodney W. Grimes 	(void)printf("--- %s ping statistics ---\n", hostname);
13648fae3551SRodney W. Grimes 	(void)printf("%ld packets transmitted, ", ntransmitted);
13658fae3551SRodney W. Grimes 	(void)printf("%ld packets received, ", nreceived);
13668fae3551SRodney W. Grimes 	if (nrepeats)
13678fae3551SRodney W. Grimes 		(void)printf("+%ld duplicates, ", nrepeats);
1368ebe70c8fSWarner Losh 	if (ntransmitted) {
13698fae3551SRodney W. Grimes 		if (nreceived > ntransmitted)
13708fae3551SRodney W. Grimes 			(void)printf("-- somebody's printing up packets!");
13718fae3551SRodney W. Grimes 		else
13728fae3551SRodney W. Grimes 			(void)printf("%d%% packet loss",
13738fae3551SRodney W. Grimes 			    (int)(((ntransmitted - nreceived) * 100) /
13748fae3551SRodney W. Grimes 			    ntransmitted));
1375ebe70c8fSWarner Losh 	}
13768fae3551SRodney W. Grimes 	(void)putchar('\n');
13773109a910SGarrett Wollman 	if (nreceived && timing) {
13783109a910SGarrett Wollman 		double n = nreceived + nrepeats;
13793109a910SGarrett Wollman 		double avg = tsum / n;
13803109a910SGarrett Wollman 		double vari = tsumsq / n - avg * avg;
13811ad0b1beSMaxim Konovalov 		(void)printf(
13821ad0b1beSMaxim Konovalov 		    "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n",
13833109a910SGarrett Wollman 		    tmin, avg, tmax, sqrt(vari));
13843109a910SGarrett Wollman 	}
1385badd8138SSean Eric Fagan 
13866e1173dcSDavid Greenman 	if (nreceived)
13878fae3551SRodney W. Grimes 		exit(0);
13886e1173dcSDavid Greenman 	else
13896e1173dcSDavid Greenman 		exit(2);
13908fae3551SRodney W. Grimes }
13918fae3551SRodney W. Grimes 
13928fae3551SRodney W. Grimes #ifdef notdef
13938fae3551SRodney W. Grimes static char *ttab[] = {
13948fae3551SRodney W. Grimes 	"Echo Reply",		/* ip + seq + udata */
13958fae3551SRodney W. Grimes 	"Dest Unreachable",	/* net, host, proto, port, frag, sr + IP */
13968fae3551SRodney W. Grimes 	"Source Quench",	/* IP */
13978fae3551SRodney W. Grimes 	"Redirect",		/* redirect type, gateway, + IP  */
13988fae3551SRodney W. Grimes 	"Echo",
13998fae3551SRodney W. Grimes 	"Time Exceeded",	/* transit, frag reassem + IP */
14008fae3551SRodney W. Grimes 	"Parameter Problem",	/* pointer + IP */
14018fae3551SRodney W. Grimes 	"Timestamp",		/* id + seq + three timestamps */
14028fae3551SRodney W. Grimes 	"Timestamp Reply",	/* " */
14038fae3551SRodney W. Grimes 	"Info Request",		/* id + sq */
14048fae3551SRodney W. Grimes 	"Info Reply"		/* " */
14058fae3551SRodney W. Grimes };
14068fae3551SRodney W. Grimes #endif
14078fae3551SRodney W. Grimes 
14088fae3551SRodney W. Grimes /*
14098fae3551SRodney W. Grimes  * pr_icmph --
14108fae3551SRodney W. Grimes  *	Print a descriptive string about an ICMP header.
14118fae3551SRodney W. Grimes  */
141243470e3bSGarrett Wollman static void
14138fae3551SRodney W. Grimes pr_icmph(icp)
14148fae3551SRodney W. Grimes 	struct icmp *icp;
14158fae3551SRodney W. Grimes {
1416efc8588dSDavid E. O'Brien 
14178fae3551SRodney W. Grimes 	switch(icp->icmp_type) {
14188fae3551SRodney W. Grimes 	case ICMP_ECHOREPLY:
14198fae3551SRodney W. Grimes 		(void)printf("Echo Reply\n");
14208fae3551SRodney W. Grimes 		/* XXX ID + Seq + Data */
14218fae3551SRodney W. Grimes 		break;
14228fae3551SRodney W. Grimes 	case ICMP_UNREACH:
14238fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
14248fae3551SRodney W. Grimes 		case ICMP_UNREACH_NET:
14258fae3551SRodney W. Grimes 			(void)printf("Destination Net Unreachable\n");
14268fae3551SRodney W. Grimes 			break;
14278fae3551SRodney W. Grimes 		case ICMP_UNREACH_HOST:
14288fae3551SRodney W. Grimes 			(void)printf("Destination Host Unreachable\n");
14298fae3551SRodney W. Grimes 			break;
14308fae3551SRodney W. Grimes 		case ICMP_UNREACH_PROTOCOL:
14318fae3551SRodney W. Grimes 			(void)printf("Destination Protocol Unreachable\n");
14328fae3551SRodney W. Grimes 			break;
14338fae3551SRodney W. Grimes 		case ICMP_UNREACH_PORT:
14348fae3551SRodney W. Grimes 			(void)printf("Destination Port Unreachable\n");
14358fae3551SRodney W. Grimes 			break;
14368fae3551SRodney W. Grimes 		case ICMP_UNREACH_NEEDFRAG:
1437ef9e6dc7SBill Fenner 			(void)printf("frag needed and DF set (MTU %d)\n",
1438ff49597eSBill Fenner 					ntohs(icp->icmp_nextmtu));
14398fae3551SRodney W. Grimes 			break;
14408fae3551SRodney W. Grimes 		case ICMP_UNREACH_SRCFAIL:
14418fae3551SRodney W. Grimes 			(void)printf("Source Route Failed\n");
14428fae3551SRodney W. Grimes 			break;
1443ef9e6dc7SBill Fenner 		case ICMP_UNREACH_FILTER_PROHIB:
1444ef9e6dc7SBill Fenner 			(void)printf("Communication prohibited by filter\n");
1445ef9e6dc7SBill Fenner 			break;
14468fae3551SRodney W. Grimes 		default:
14478fae3551SRodney W. Grimes 			(void)printf("Dest Unreachable, Bad Code: %d\n",
14488fae3551SRodney W. Grimes 			    icp->icmp_code);
14498fae3551SRodney W. Grimes 			break;
14508fae3551SRodney W. Grimes 		}
14518fae3551SRodney W. Grimes 		/* Print returned IP header information */
14528fae3551SRodney W. Grimes #ifndef icmp_data
14538fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
14548fae3551SRodney W. Grimes #else
14558fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
14568fae3551SRodney W. Grimes #endif
14578fae3551SRodney W. Grimes 		break;
14588fae3551SRodney W. Grimes 	case ICMP_SOURCEQUENCH:
14598fae3551SRodney W. Grimes 		(void)printf("Source Quench\n");
14608fae3551SRodney W. Grimes #ifndef icmp_data
14618fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
14628fae3551SRodney W. Grimes #else
14638fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
14648fae3551SRodney W. Grimes #endif
14658fae3551SRodney W. Grimes 		break;
14668fae3551SRodney W. Grimes 	case ICMP_REDIRECT:
14678fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
14688fae3551SRodney W. Grimes 		case ICMP_REDIRECT_NET:
14698fae3551SRodney W. Grimes 			(void)printf("Redirect Network");
14708fae3551SRodney W. Grimes 			break;
14718fae3551SRodney W. Grimes 		case ICMP_REDIRECT_HOST:
14728fae3551SRodney W. Grimes 			(void)printf("Redirect Host");
14738fae3551SRodney W. Grimes 			break;
14748fae3551SRodney W. Grimes 		case ICMP_REDIRECT_TOSNET:
14758fae3551SRodney W. Grimes 			(void)printf("Redirect Type of Service and Network");
14768fae3551SRodney W. Grimes 			break;
14778fae3551SRodney W. Grimes 		case ICMP_REDIRECT_TOSHOST:
14788fae3551SRodney W. Grimes 			(void)printf("Redirect Type of Service and Host");
14798fae3551SRodney W. Grimes 			break;
14808fae3551SRodney W. Grimes 		default:
14818fae3551SRodney W. Grimes 			(void)printf("Redirect, Bad Code: %d", icp->icmp_code);
14828fae3551SRodney W. Grimes 			break;
14838fae3551SRodney W. Grimes 		}
1484ff49597eSBill Fenner 		(void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr));
14858fae3551SRodney W. Grimes #ifndef icmp_data
14868fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
14878fae3551SRodney W. Grimes #else
14888fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
14898fae3551SRodney W. Grimes #endif
14908fae3551SRodney W. Grimes 		break;
14918fae3551SRodney W. Grimes 	case ICMP_ECHO:
14928fae3551SRodney W. Grimes 		(void)printf("Echo Request\n");
14938fae3551SRodney W. Grimes 		/* XXX ID + Seq + Data */
14948fae3551SRodney W. Grimes 		break;
14958fae3551SRodney W. Grimes 	case ICMP_TIMXCEED:
14968fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
14978fae3551SRodney W. Grimes 		case ICMP_TIMXCEED_INTRANS:
14988fae3551SRodney W. Grimes 			(void)printf("Time to live exceeded\n");
14998fae3551SRodney W. Grimes 			break;
15008fae3551SRodney W. Grimes 		case ICMP_TIMXCEED_REASS:
15018fae3551SRodney W. Grimes 			(void)printf("Frag reassembly time exceeded\n");
15028fae3551SRodney W. Grimes 			break;
15038fae3551SRodney W. Grimes 		default:
15048fae3551SRodney W. Grimes 			(void)printf("Time exceeded, Bad Code: %d\n",
15058fae3551SRodney W. Grimes 			    icp->icmp_code);
15068fae3551SRodney W. Grimes 			break;
15078fae3551SRodney W. Grimes 		}
15088fae3551SRodney W. Grimes #ifndef icmp_data
15098fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
15108fae3551SRodney W. Grimes #else
15118fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
15128fae3551SRodney W. Grimes #endif
15138fae3551SRodney W. Grimes 		break;
15148fae3551SRodney W. Grimes 	case ICMP_PARAMPROB:
15158fae3551SRodney W. Grimes 		(void)printf("Parameter problem: pointer = 0x%02x\n",
15168fae3551SRodney W. Grimes 		    icp->icmp_hun.ih_pptr);
15178fae3551SRodney W. Grimes #ifndef icmp_data
15188fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
15198fae3551SRodney W. Grimes #else
15208fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
15218fae3551SRodney W. Grimes #endif
15228fae3551SRodney W. Grimes 		break;
15238fae3551SRodney W. Grimes 	case ICMP_TSTAMP:
15248fae3551SRodney W. Grimes 		(void)printf("Timestamp\n");
15258fae3551SRodney W. Grimes 		/* XXX ID + Seq + 3 timestamps */
15268fae3551SRodney W. Grimes 		break;
15278fae3551SRodney W. Grimes 	case ICMP_TSTAMPREPLY:
15288fae3551SRodney W. Grimes 		(void)printf("Timestamp Reply\n");
15298fae3551SRodney W. Grimes 		/* XXX ID + Seq + 3 timestamps */
15308fae3551SRodney W. Grimes 		break;
15318fae3551SRodney W. Grimes 	case ICMP_IREQ:
15328fae3551SRodney W. Grimes 		(void)printf("Information Request\n");
15338fae3551SRodney W. Grimes 		/* XXX ID + Seq */
15348fae3551SRodney W. Grimes 		break;
15358fae3551SRodney W. Grimes 	case ICMP_IREQREPLY:
15368fae3551SRodney W. Grimes 		(void)printf("Information Reply\n");
15378fae3551SRodney W. Grimes 		/* XXX ID + Seq */
15388fae3551SRodney W. Grimes 		break;
15398fae3551SRodney W. Grimes 	case ICMP_MASKREQ:
15408fae3551SRodney W. Grimes 		(void)printf("Address Mask Request\n");
15418fae3551SRodney W. Grimes 		break;
15428fae3551SRodney W. Grimes 	case ICMP_MASKREPLY:
15438fae3551SRodney W. Grimes 		(void)printf("Address Mask Reply\n");
15448fae3551SRodney W. Grimes 		break;
1545ef9e6dc7SBill Fenner 	case ICMP_ROUTERADVERT:
1546ef9e6dc7SBill Fenner 		(void)printf("Router Advertisement\n");
1547ef9e6dc7SBill Fenner 		break;
1548ef9e6dc7SBill Fenner 	case ICMP_ROUTERSOLICIT:
1549ef9e6dc7SBill Fenner 		(void)printf("Router Solicitation\n");
1550ef9e6dc7SBill Fenner 		break;
15518fae3551SRodney W. Grimes 	default:
15528fae3551SRodney W. Grimes 		(void)printf("Bad ICMP type: %d\n", icp->icmp_type);
15538fae3551SRodney W. Grimes 	}
15548fae3551SRodney W. Grimes }
15558fae3551SRodney W. Grimes 
15568fae3551SRodney W. Grimes /*
15578fae3551SRodney W. Grimes  * pr_iph --
15588fae3551SRodney W. Grimes  *	Print an IP header with options.
15598fae3551SRodney W. Grimes  */
156043470e3bSGarrett Wollman static void
15618fae3551SRodney W. Grimes pr_iph(ip)
15628fae3551SRodney W. Grimes 	struct ip *ip;
15638fae3551SRodney W. Grimes {
15648fae3551SRodney W. Grimes 	u_char *cp;
1565efc8588dSDavid E. O'Brien 	int hlen;
15668fae3551SRodney W. Grimes 
15678fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
15688fae3551SRodney W. Grimes 	cp = (u_char *)ip + 20;		/* point to options */
15698fae3551SRodney W. Grimes 
1570ef9e6dc7SBill Fenner 	(void)printf("Vr HL TOS  Len   ID Flg  off TTL Pro  cks      Src      Dst\n");
15718fae3551SRodney W. Grimes 	(void)printf(" %1x  %1x  %02x %04x %04x",
1572ef9e6dc7SBill Fenner 	    ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len),
1573ef9e6dc7SBill Fenner 	    ntohs(ip->ip_id));
1574d32ff037SJohn Birrell 	(void)printf("   %1lx %04lx",
1575d32ff037SJohn Birrell 	    (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13,
1576d32ff037SJohn Birrell 	    (u_long) ntohl(ip->ip_off) & 0x1fff);
1577ef9e6dc7SBill Fenner 	(void)printf("  %02x  %02x %04x", ip->ip_ttl, ip->ip_p,
1578ef9e6dc7SBill Fenner 							    ntohs(ip->ip_sum));
15798fae3551SRodney W. Grimes 	(void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr));
15808fae3551SRodney W. Grimes 	(void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr));
1581ef9e6dc7SBill Fenner 	/* dump any option bytes */
15828fae3551SRodney W. Grimes 	while (hlen-- > 20) {
15838fae3551SRodney W. Grimes 		(void)printf("%02x", *cp++);
15848fae3551SRodney W. Grimes 	}
15858fae3551SRodney W. Grimes 	(void)putchar('\n');
15868fae3551SRodney W. Grimes }
15878fae3551SRodney W. Grimes 
15888fae3551SRodney W. Grimes /*
15898fae3551SRodney W. Grimes  * pr_addr --
15908fae3551SRodney W. Grimes  *	Return an ascii host address as a dotted quad and optionally with
15918fae3551SRodney W. Grimes  * a hostname.
15928fae3551SRodney W. Grimes  */
159343470e3bSGarrett Wollman static char *
159443470e3bSGarrett Wollman pr_addr(ina)
159543470e3bSGarrett Wollman 	struct in_addr ina;
15968fae3551SRodney W. Grimes {
15978fae3551SRodney W. Grimes 	struct hostent *hp;
1598f78ac61bSWarner Losh 	static char buf[16 + 3 + MAXHOSTNAMELEN];
15998fae3551SRodney W. Grimes 
16008fae3551SRodney W. Grimes 	if ((options & F_NUMERIC) ||
160143470e3bSGarrett Wollman 	    !(hp = gethostbyaddr((char *)&ina, 4, AF_INET)))
160243470e3bSGarrett Wollman 		return inet_ntoa(ina);
16038fae3551SRodney W. Grimes 	else
1604efa38539SPeter Wemm 		(void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name,
160543470e3bSGarrett Wollman 		    inet_ntoa(ina));
16068fae3551SRodney W. Grimes 	return(buf);
16078fae3551SRodney W. Grimes }
16088fae3551SRodney W. Grimes 
16098fae3551SRodney W. Grimes /*
16108fae3551SRodney W. Grimes  * pr_retip --
16118fae3551SRodney W. Grimes  *	Dump some info on a returned (via ICMP) IP packet.
16128fae3551SRodney W. Grimes  */
161343470e3bSGarrett Wollman static void
16148fae3551SRodney W. Grimes pr_retip(ip)
16158fae3551SRodney W. Grimes 	struct ip *ip;
16168fae3551SRodney W. Grimes {
16178fae3551SRodney W. Grimes 	u_char *cp;
1618efc8588dSDavid E. O'Brien 	int hlen;
16198fae3551SRodney W. Grimes 
16208fae3551SRodney W. Grimes 	pr_iph(ip);
16218fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
16228fae3551SRodney W. Grimes 	cp = (u_char *)ip + hlen;
16238fae3551SRodney W. Grimes 
16248fae3551SRodney W. Grimes 	if (ip->ip_p == 6)
16258fae3551SRodney W. Grimes 		(void)printf("TCP: from port %u, to port %u (decimal)\n",
16268fae3551SRodney W. Grimes 		    (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
16278fae3551SRodney W. Grimes 	else if (ip->ip_p == 17)
16288fae3551SRodney W. Grimes 		(void)printf("UDP: from port %u, to port %u (decimal)\n",
16298fae3551SRodney W. Grimes 			(*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
16308fae3551SRodney W. Grimes }
16318fae3551SRodney W. Grimes 
1632eb1543c6SMatthew N. Dodd static char *
1633007fe4e3SMaxim Konovalov pr_ntime (n_time timestamp)
1634eb1543c6SMatthew N. Dodd {
1635eb1543c6SMatthew N. Dodd 	static char buf[10];
1636007fe4e3SMaxim Konovalov 	int hour, min, sec;
1637eb1543c6SMatthew N. Dodd 
1638007fe4e3SMaxim Konovalov 	sec = ntohl(timestamp) / 1000;
1639007fe4e3SMaxim Konovalov 	hour = sec / 60 / 60;
1640007fe4e3SMaxim Konovalov 	min = (sec % (60 * 60)) / 60;
1641007fe4e3SMaxim Konovalov 	sec = (sec % (60 * 60)) % 60;
1642eb1543c6SMatthew N. Dodd 
1643007fe4e3SMaxim Konovalov 	(void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hour, min, sec);
1644eb1543c6SMatthew N. Dodd 
1645eb1543c6SMatthew N. Dodd 	return (buf);
1646eb1543c6SMatthew N. Dodd }
1647eb1543c6SMatthew N. Dodd 
164843470e3bSGarrett Wollman static void
16498fae3551SRodney W. Grimes fill(bp, patp)
16508fae3551SRodney W. Grimes 	char *bp, *patp;
16518fae3551SRodney W. Grimes {
16528fae3551SRodney W. Grimes 	char *cp;
1653efc8588dSDavid E. O'Brien 	int pat[16];
16544fba6582SMaxim Konovalov 	u_int ii, jj, kk;
16558fae3551SRodney W. Grimes 
165643470e3bSGarrett Wollman 	for (cp = patp; *cp; cp++) {
165743470e3bSGarrett Wollman 		if (!isxdigit(*cp))
165843470e3bSGarrett Wollman 			errx(EX_USAGE,
165943470e3bSGarrett Wollman 			    "patterns must be specified as hex digits");
166043470e3bSGarrett Wollman 
16618fae3551SRodney W. Grimes 	}
16628fae3551SRodney W. Grimes 	ii = sscanf(patp,
16638fae3551SRodney W. Grimes 	    "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
16648fae3551SRodney W. Grimes 	    &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
16658fae3551SRodney W. Grimes 	    &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
16668fae3551SRodney W. Grimes 	    &pat[13], &pat[14], &pat[15]);
16678fae3551SRodney W. Grimes 
16688fae3551SRodney W. Grimes 	if (ii > 0)
1669d829c3dfSMatthew N. Dodd 		for (kk = 0; kk <= maxpayload - (TIMEVAL_LEN + ii); kk += ii)
16708fae3551SRodney W. Grimes 			for (jj = 0; jj < ii; ++jj)
16718fae3551SRodney W. Grimes 				bp[jj + kk] = pat[jj];
16728fae3551SRodney W. Grimes 	if (!(options & F_QUIET)) {
16738fae3551SRodney W. Grimes 		(void)printf("PATTERN: 0x");
16748fae3551SRodney W. Grimes 		for (jj = 0; jj < ii; ++jj)
16758fae3551SRodney W. Grimes 			(void)printf("%02x", bp[jj] & 0xFF);
16768fae3551SRodney W. Grimes 		(void)printf("\n");
16778fae3551SRodney W. Grimes 	}
16788fae3551SRodney W. Grimes }
16798fae3551SRodney W. Grimes 
1680120b4a93SRuslan Ermilov #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
1681120b4a93SRuslan Ermilov #define	SECOPT		" [-P policy]"
1682120b4a93SRuslan Ermilov #else
1683120b4a93SRuslan Ermilov #define	SECOPT		""
1684120b4a93SRuslan Ermilov #endif
168543470e3bSGarrett Wollman static void
1686e345a80dSPhilippe Charnier usage()
16878fae3551SRodney W. Grimes {
168831eac03bSSean Chittenden 
16899ff95228SGleb Smirnoff 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
16901bd10ba2SRuslan Ermilov "usage: ping [-AaDdfnoQqRrv] [-c count] [-i wait] [-l preload] [-M mask | time]",
1691120b4a93SRuslan Ermilov "            [-m ttl]" SECOPT " [-p pattern] [-S src_addr] [-s packetsize]",
16929ff95228SGleb Smirnoff "            [-t timeout] [-z tos] [-G sweepmaxsize ] [-g sweepminsize ]",
16939ff95228SGleb Smirnoff "            [-h sweepincrsize ] host",
16941bd10ba2SRuslan Ermilov "       ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload]",
1695120b4a93SRuslan Ermilov "            [-M mask | time] [-m ttl]" SECOPT " [-p pattern] [-S src_addr]",
16961bd10ba2SRuslan Ermilov "            [-s packetsize] [-T ttl] [-t timeout] [-z tos] mcast-group");
169743470e3bSGarrett Wollman 	exit(EX_USAGE);
16988fae3551SRodney W. Grimes }
1699