ping.c revision 92806
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1989, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * This code is derived from software contributed to Berkeley by
61558Srgrimes * Mike Muuss.
71558Srgrimes *
81558Srgrimes * Redistribution and use in source and binary forms, with or without
91558Srgrimes * modification, are permitted provided that the following conditions
101558Srgrimes * are met:
111558Srgrimes * 1. Redistributions of source code must retain the above copyright
121558Srgrimes *    notice, this list of conditions and the following disclaimer.
131558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141558Srgrimes *    notice, this list of conditions and the following disclaimer in the
151558Srgrimes *    documentation and/or other materials provided with the distribution.
161558Srgrimes * 3. All advertising materials mentioning features or use of this software
171558Srgrimes *    must display the following acknowledgement:
181558Srgrimes *	This product includes software developed by the University of
191558Srgrimes *	California, Berkeley and its contributors.
201558Srgrimes * 4. Neither the name of the University nor the names of its contributors
211558Srgrimes *    may be used to endorse or promote products derived from this software
221558Srgrimes *    without specific prior written permission.
231558Srgrimes *
241558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341558Srgrimes * SUCH DAMAGE.
351558Srgrimes */
361558Srgrimes
371558Srgrimes#ifndef lint
3823247Swollmanstatic const char copyright[] =
391558Srgrimes"@(#) Copyright (c) 1989, 1993\n\
401558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
411558Srgrimes#endif /* not lint */
421558Srgrimes
431558Srgrimes#ifndef lint
4437671Scharnier#if 0
451558Srgrimesstatic char sccsid[] = "@(#)ping.c	8.1 (Berkeley) 6/5/93";
4637671Scharnier#endif
4723247Swollmanstatic const char rcsid[] =
4850476Speter  "$FreeBSD: head/sbin/ping/ping.c 92806 2002-03-20 17:55:10Z obrien $";
491558Srgrimes#endif /* not lint */
501558Srgrimes
511558Srgrimes/*
521558Srgrimes *			P I N G . C
531558Srgrimes *
5437671Scharnier * Using the Internet Control Message Protocol (ICMP) "ECHO" facility,
551558Srgrimes * measure round-trip-delays and packet loss across network paths.
561558Srgrimes *
571558Srgrimes * Author -
581558Srgrimes *	Mike Muuss
591558Srgrimes *	U. S. Army Ballistic Research Laboratory
601558Srgrimes *	December, 1983
611558Srgrimes *
621558Srgrimes * Status -
631558Srgrimes *	Public Domain.  Distribution Unlimited.
641558Srgrimes * Bugs -
651558Srgrimes *	More statistics could always be gathered.
661558Srgrimes *	This program has to run SUID to ROOT to access the ICMP socket.
671558Srgrimes */
681558Srgrimes
6923247Swollman#include <sys/param.h>		/* NB: we rely on this for <sys/types.h> */
7023247Swollman
7123247Swollman#include <ctype.h>
7223247Swollman#include <err.h>
7323247Swollman#include <errno.h>
7427508Swollman#include <math.h>
7523247Swollman#include <netdb.h>
7623247Swollman#include <signal.h>
7723247Swollman#include <stdio.h>
7823247Swollman#include <stdlib.h>
7923247Swollman#include <string.h>
8023247Swollman#include <sysexits.h>
8123247Swollman#include <termios.h>
8223247Swollman#include <unistd.h>
8323247Swollman
841558Srgrimes#include <sys/socket.h>
851558Srgrimes#include <sys/time.h>
8636378Sfenner#include <sys/uio.h>
871558Srgrimes
8823247Swollman#include <netinet/in.h>
891558Srgrimes#include <netinet/in_systm.h>
901558Srgrimes#include <netinet/ip.h>
911558Srgrimes#include <netinet/ip_icmp.h>
921558Srgrimes#include <netinet/ip_var.h>
9323247Swollman#include <arpa/inet.h>
941558Srgrimes
9555505Sshin#ifdef IPSEC
9655505Sshin#include <netinet6/ipsec.h>
9755505Sshin#endif /*IPSEC*/
9855505Sshin
9936089Sjb#define	PHDR_LEN	sizeof(struct timeval)
10036089Sjb#define	DEFDATALEN	(64 - PHDR_LEN)	/* default data length */
10127533Sbde#define	FLOOD_BACKOFF	20000		/* usecs to back off if F_FLOOD mode */
10227533Sbde					/* runs out of buffer space */
1031558Srgrimes#define	MAXIPLEN	60
1041558Srgrimes#define	MAXICMPLEN	76
1051558Srgrimes#define	MAXPACKET	(65536 - 60 - 8)/* max packet size */
1061558Srgrimes#define	MAXWAIT		10		/* max seconds to wait for response */
10756342Sbillf#define	MAXALARM	(60 * 60)	/* max seconds for alarm timeout */
1081558Srgrimes#define	NROUTES		9		/* number of record route slots */
1091558Srgrimes
1101558Srgrimes#define	A(bit)		rcvd_tbl[(bit)>>3]	/* identify byte in array */
1111558Srgrimes#define	B(bit)		(1 << ((bit) & 0x07))	/* identify bit in byte */
1121558Srgrimes#define	SET(bit)	(A(bit) |= B(bit))
1131558Srgrimes#define	CLR(bit)	(A(bit) &= (~B(bit)))
1141558Srgrimes#define	TST(bit)	(A(bit) & B(bit))
1151558Srgrimes
1161558Srgrimes/* various options */
1171558Srgrimesint options;
11820540Sfenner#define	F_FLOOD		0x0001
11920540Sfenner#define	F_INTERVAL	0x0002
12020540Sfenner#define	F_NUMERIC	0x0004
12120540Sfenner#define	F_PINGFILLED	0x0008
12220540Sfenner#define	F_QUIET		0x0010
12320540Sfenner#define	F_RROUTE	0x0020
12420540Sfenner#define	F_SO_DEBUG	0x0040
12520540Sfenner#define	F_SO_DONTROUTE	0x0080
12620540Sfenner#define	F_VERBOSE	0x0100
12720540Sfenner#define	F_QUIET2	0x0200
12820540Sfenner#define	F_NOLOOP	0x0400
12920540Sfenner#define	F_MTTL		0x0800
13020540Sfenner#define	F_MIF		0x1000
13122417Sdanny#define	F_AUDIBLE	0x2000
13255505Sshin#ifdef IPSEC
13355505Sshin#ifdef IPSEC_POLICY_IPSEC
13455505Sshin#define F_POLICY	0x4000
13555505Sshin#endif /*IPSEC_POLICY_IPSEC*/
13655505Sshin#endif /*IPSEC*/
13774029Sru#define	F_TTL		0x8000
13877119Sphk#define	F_MISSED	0x10000
1391558Srgrimes
1401558Srgrimes/*
1411558Srgrimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
1421558Srgrimes * number of received sequence numbers we can keep track of.  Change 128
1431558Srgrimes * to 8192 for complete accuracy...
1441558Srgrimes */
1451558Srgrimes#define	MAX_DUP_CHK	(8 * 128)
1461558Srgrimesint mx_dup_ck = MAX_DUP_CHK;
1471558Srgrimeschar rcvd_tbl[MAX_DUP_CHK / 8];
1481558Srgrimes
14979403Smjacobstruct sockaddr_in whereto;	/* who to ping */
1501558Srgrimesint datalen = DEFDATALEN;
1511558Srgrimesint s;				/* socket file descriptor */
1521558Srgrimesu_char outpack[MAXPACKET];
1531558Srgrimeschar BSPACE = '\b';		/* characters written for flood */
15477119Sphkchar BBELL = '\a';		/* characters written for MISSED and AUDIBLE */
1551558Srgrimeschar DOT = '.';
1561558Srgrimeschar *hostname;
15742337Simpchar *shostname;
1581558Srgrimesint ident;			/* process id to identify our packets */
15923295Simpint uid;			/* cached uid for micro-optimization */
1601558Srgrimes
1611558Srgrimes/* counters */
1621558Srgrimeslong npackets;			/* max packets to transmit */
1631558Srgrimeslong nreceived;			/* # of packets we got back */
1641558Srgrimeslong nrepeats;			/* number of duplicates */
1651558Srgrimeslong ntransmitted;		/* sequence # for outbound packets = #sent */
16683940Siedowselong nmissedmax;		/* max value of ntransmitted - nreceived - 1 */
16738549Sdillonint interval = 1000;		/* interval between packets, ms */
1681558Srgrimes
1691558Srgrimes/* timing */
1701558Srgrimesint timing;			/* flag to do timing */
1711558Srgrimesdouble tmin = 999999999.0;	/* minimum round trip time */
1721558Srgrimesdouble tmax = 0.0;		/* maximum round trip time */
1731558Srgrimesdouble tsum = 0.0;		/* sum of all times, for doing average */
17427508Swollmandouble tsumsq = 0.0;		/* sum of all times squared, for std. dev. */
1751558Srgrimes
17627533Sbdevolatile sig_atomic_t finish_up;  /* nonzero if we've been told to finish up */
1773792Ssefint reset_kerninfo;
17827533Sbdevolatile sig_atomic_t siginfo_p;
1793792Ssef
18023247Swollmanstatic void fill(char *, char *);
18123247Swollmanstatic u_short in_cksum(u_short *, int);
18223247Swollmanstatic void check_status(void);
18327533Sbdestatic void finish(void) __dead2;
18423247Swollmanstatic void pinger(void);
18523247Swollmanstatic char *pr_addr(struct in_addr);
18623247Swollmanstatic void pr_icmph(struct icmp *);
18723247Swollmanstatic void pr_iph(struct ip *);
18836378Sfennerstatic void pr_pack(char *, int, struct sockaddr_in *, struct timeval *);
18923247Swollmanstatic void pr_retip(struct ip *);
19023247Swollmanstatic void status(int);
19127533Sbdestatic void stopit(int);
19223247Swollmanstatic void tvsub(struct timeval *, struct timeval *);
19337671Scharnierstatic void usage(void) __dead2;
1941558Srgrimes
19523247Swollmanint
1961558Srgrimesmain(argc, argv)
1971558Srgrimes	int argc;
19823247Swollman	char *const *argv;
1991558Srgrimes{
20036378Sfenner	struct timeval last, intvl;
2011558Srgrimes	struct hostent *hp;
20242337Simp	struct sockaddr_in *to, sin;
2033792Ssef	struct termios ts;
20492806Sobrien	int i;
20574029Sru	int ch, hold, packlen, preload, sockerrno, almost_done = 0, ttl;
20620540Sfenner	struct in_addr ifaddr;
20774029Sru	unsigned char mttl, loop;
2081558Srgrimes	u_char *datap, *packet;
20942337Simp	char *source = NULL, *target, hnamebuf[MAXHOSTNAMELEN];
21042337Simp	char snamebuf[MAXHOSTNAMELEN];
21123247Swollman	char *ep;
21223247Swollman	u_long ultmp;
2131558Srgrimes#ifdef IP_OPTIONS
2141558Srgrimes	char rspace[3 + 4 * NROUTES + 1];	/* record route space */
2151558Srgrimes#endif
21620195Ssef	struct sigaction si_sa;
21736378Sfenner	struct iovec iov;
21836378Sfenner	struct msghdr msg;
21936378Sfenner	struct sockaddr_in from;
22057719Sshin	char ctrl[CMSG_SPACE(sizeof(struct timeval))];
22155505Sshin#ifdef IPSEC_POLICY_IPSEC
22255505Sshin	char *policy_in = NULL;
22355505Sshin	char *policy_out = NULL;
22455505Sshin#endif
22556342Sbillf	unsigned long alarmtimeout;
2261558Srgrimes
22717474Sfenner	/*
22817474Sfenner	 * Do the stuff that we need root priv's for *first*, and
22917474Sfenner	 * then drop our setuid bit.  Save error reporting for
23017474Sfenner	 * after arg parsing.
23117474Sfenner	 */
23223247Swollman	s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
23323247Swollman	sockerrno = errno;
23417474Sfenner
23517474Sfenner	setuid(getuid());
23623295Simp	uid = getuid();
23717474Sfenner
23856342Sbillf	alarmtimeout = preload = 0;
2393792Ssef
24036089Sjb	datap = &outpack[8 + PHDR_LEN];
24174029Sru	while ((ch = getopt(argc, argv,
24277119Sphk		"AI:LQRS:T:c:adfi:l:m:np:qrs:t:v"
24374029Sru#ifdef IPSEC
24455505Sshin#ifdef IPSEC_POLICY_IPSEC
24574029Sru		"P:"
24655505Sshin#endif /*IPSEC_POLICY_IPSEC*/
24774029Sru#endif /*IPSEC*/
24874029Sru		)) != -1)
24955505Sshin	{
2501558Srgrimes		switch(ch) {
25177119Sphk		case 'A':
25277119Sphk			options |= F_MISSED;
25377119Sphk			break;
25422417Sdanny		case 'a':
25522417Sdanny			options |= F_AUDIBLE;
25622417Sdanny			break;
2571558Srgrimes		case 'c':
25823247Swollman			ultmp = strtoul(optarg, &ep, 0);
25923247Swollman			if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp)
26023247Swollman				errx(EX_USAGE,
26123247Swollman				    "invalid count of packets to transmit: `%s'",
26223247Swollman				    optarg);
26323247Swollman			npackets = ultmp;
2641558Srgrimes			break;
2651558Srgrimes		case 'd':
2661558Srgrimes			options |= F_SO_DEBUG;
2671558Srgrimes			break;
2681558Srgrimes		case 'f':
26938549Sdillon			if (uid) {
27023247Swollman				errno = EPERM;
27123247Swollman				err(EX_NOPERM, "-f flag");
2721558Srgrimes			}
2731558Srgrimes			options |= F_FLOOD;
2741558Srgrimes			setbuf(stdout, (char *)NULL);
2751558Srgrimes			break;
2761558Srgrimes		case 'i':		/* wait between sending packets */
27738549Sdillon			{
27838549Sdillon			    double t = strtod(optarg, &ep) * 1000.0;
27938549Sdillon
28038549Sdillon			    if (*ep || ep == optarg || t > (double)INT_MAX) {
28138549Sdillon				    errx(
28238549Sdillon					    EX_USAGE,
28338549Sdillon					     "invalid timing interval: `%s'",
28438549Sdillon					     optarg
28538549Sdillon				    );
28638549Sdillon			    }
28738549Sdillon			    options |= F_INTERVAL;
28838549Sdillon			    interval = (int)t;
28938549Sdillon			    if (uid && interval < 1000) {
29038549Sdillon				    errno = EPERM;
29138549Sdillon				    err(EX_NOPERM, "-i interval too short");
29238549Sdillon			    }
29338549Sdillon			}
2941558Srgrimes			break;
29520540Sfenner		case 'I':		/* multicast interface */
29623247Swollman			if (inet_aton(optarg, &ifaddr) == 0)
29723247Swollman				errx(EX_USAGE,
29823247Swollman				     "invalid multicast interface: `%s'",
29923247Swollman				     optarg);
30020540Sfenner			options |= F_MIF;
30120540Sfenner			break;
3021558Srgrimes		case 'l':
30323247Swollman			ultmp = strtoul(optarg, &ep, 0);
30423247Swollman			if (*ep || ep == optarg || ultmp > INT_MAX)
30523247Swollman				errx(EX_USAGE,
30623247Swollman				     "invalid preload value: `%s'", optarg);
30746643Smckay			if (uid) {
30823251Simp				errno = EPERM;
30923251Simp				err(EX_NOPERM, "-l flag");
31023251Simp			}
31123247Swollman			preload = ultmp;
3121558Srgrimes			break;
31320540Sfenner		case 'L':
31420540Sfenner			options |= F_NOLOOP;
31520540Sfenner			loop = 0;
31620540Sfenner			break;
31774029Sru		case 'm':		/* TTL */
31874029Sru			ultmp = strtoul(optarg, &ep, 0);
31974029Sru			if (*ep || ep == optarg || ultmp > 255)
32074029Sru				errx(EX_USAGE, "invalid TTL: `%s'",
32174029Sru				     optarg);
32274029Sru			ttl = ultmp;
32374029Sru			options |= F_TTL;
32474029Sru			break;
3251558Srgrimes		case 'n':
3261558Srgrimes			options |= F_NUMERIC;
3271558Srgrimes			break;
3281558Srgrimes		case 'p':		/* fill buffer with user pattern */
3291558Srgrimes			options |= F_PINGFILLED;
3301558Srgrimes			fill((char *)datap, optarg);
3311558Srgrimes				break;
33217724Sfenner		case 'Q':
33317724Sfenner			options |= F_QUIET2;
33417724Sfenner			break;
3351558Srgrimes		case 'q':
3361558Srgrimes			options |= F_QUIET;
3371558Srgrimes			break;
3381558Srgrimes		case 'R':
3391558Srgrimes			options |= F_RROUTE;
3401558Srgrimes			break;
3411558Srgrimes		case 'r':
3421558Srgrimes			options |= F_SO_DONTROUTE;
3431558Srgrimes			break;
3441558Srgrimes		case 's':		/* size of packet to send */
34538549Sdillon			if (uid) {
34638549Sdillon				errno = EPERM;
34738549Sdillon				err(EX_NOPERM, "-s flag");
34838549Sdillon			}
34923247Swollman			ultmp = strtoul(optarg, &ep, 0);
35023247Swollman			if (ultmp > MAXPACKET)
35123247Swollman				errx(EX_USAGE, "packet size too large: %lu",
35223247Swollman				     ultmp);
35323247Swollman			if (*ep || ep == optarg || !ultmp)
35423247Swollman				errx(EX_USAGE, "invalid packet size: `%s'",
35523247Swollman				     optarg);
35623247Swollman			datalen = ultmp;
3571558Srgrimes			break;
35842337Simp		case 'S':
35942337Simp			source = optarg;
36042337Simp			break;
36155996Sbillf		case 't':
36256342Sbillf			alarmtimeout = strtoul(optarg, &ep, 0);
36356342Sbillf			if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX))
36455996Sbillf				errx(EX_USAGE, "invalid timeout: `%s'",
36555996Sbillf				    optarg);
36656342Sbillf			if (alarmtimeout > MAXALARM)
36756342Sbillf				errx(EX_USAGE, "invalid timeout: `%s' > %d",
36856342Sbillf				    optarg, MAXALARM);
36956342Sbillf			alarm((int)alarmtimeout);
37055996Sbillf			break;
37120540Sfenner		case 'T':		/* multicast TTL */
37223247Swollman			ultmp = strtoul(optarg, &ep, 0);
37323247Swollman			if (*ep || ep == optarg || ultmp > 255)
37423247Swollman				errx(EX_USAGE, "invalid multicast TTL: `%s'",
37523247Swollman				     optarg);
37674029Sru			mttl = ultmp;
37720540Sfenner			options |= F_MTTL;
37820540Sfenner			break;
3791558Srgrimes		case 'v':
3801558Srgrimes			options |= F_VERBOSE;
3811558Srgrimes			break;
38255505Sshin#ifdef IPSEC
38355505Sshin#ifdef IPSEC_POLICY_IPSEC
38455505Sshin		case 'P':
38555505Sshin			options |= F_POLICY;
38655505Sshin			if (!strncmp("in", optarg, 2))
38755505Sshin				policy_in = strdup(optarg);
38855505Sshin			else if (!strncmp("out", optarg, 3))
38955505Sshin				policy_out = strdup(optarg);
39055505Sshin			else
39155505Sshin				errx(1, "invalid security policy");
39255505Sshin			break;
39355505Sshin#endif /*IPSEC_POLICY_IPSEC*/
39455505Sshin#endif /*IPSEC*/
3951558Srgrimes		default:
39637671Scharnier			usage();
3971558Srgrimes		}
39823247Swollman	}
3991558Srgrimes
40023247Swollman	if (argc - optind != 1)
40137671Scharnier		usage();
40223247Swollman	target = argv[optind];
4031558Srgrimes
40442337Simp	if (source) {
40542337Simp		bzero((char *)&sin, sizeof(sin));
40642337Simp		sin.sin_family = AF_INET;
40742337Simp		if (inet_aton(source, &sin.sin_addr) != 0) {
40842337Simp			shostname = source;
40942337Simp		} else {
41042337Simp			hp = gethostbyname2(source, AF_INET);
41142337Simp			if (!hp)
41242337Simp				errx(EX_NOHOST, "cannot resolve %s: %s",
41342337Simp				     source, hstrerror(h_errno));
41442337Simp
41542337Simp			sin.sin_len = sizeof sin;
41642337Simp			if (hp->h_length > sizeof(sin.sin_addr))
41742337Simp				errx(1,"gethostbyname2: illegal address");
41842337Simp			memcpy(&sin.sin_addr, hp->h_addr_list[0],
41942337Simp				sizeof (sin.sin_addr));
42042337Simp			(void)strncpy(snamebuf, hp->h_name,
42142337Simp				sizeof(snamebuf) - 1);
42242337Simp			snamebuf[sizeof(snamebuf) - 1] = '\0';
42342337Simp			shostname = snamebuf;
42442337Simp		}
42542337Simp		if (bind(s, (struct sockaddr *)&sin, sizeof sin) == -1)
42642337Simp			err(1, "bind");
42742337Simp	}
42842337Simp
42979403Smjacob	bzero(&whereto, sizeof(whereto));
43079403Smjacob	to = &whereto;
4311558Srgrimes	to->sin_family = AF_INET;
43279403Smjacob	to->sin_len = sizeof *to;
43323247Swollman	if (inet_aton(target, &to->sin_addr) != 0) {
4341558Srgrimes		hostname = target;
43523247Swollman	} else {
43623247Swollman		hp = gethostbyname2(target, AF_INET);
43723247Swollman		if (!hp)
43823247Swollman			errx(EX_NOHOST, "cannot resolve %s: %s",
43923247Swollman			     target, hstrerror(h_errno));
44023247Swollman
44123327Simp		if (hp->h_length > sizeof(to->sin_addr))
44223327Simp			errx(1,"gethostbyname2 returned an illegal address");
44323247Swollman		memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr);
4441558Srgrimes		(void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1);
44531956Simp		hnamebuf[sizeof(hnamebuf) - 1] = '\0';
4461558Srgrimes		hostname = hnamebuf;
4471558Srgrimes	}
4481558Srgrimes
44923247Swollman	if (options & F_FLOOD && options & F_INTERVAL)
45023247Swollman		errx(EX_USAGE, "-f and -i: incompatible options");
4511558Srgrimes
45223247Swollman	if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
45323247Swollman		errx(EX_USAGE,
45423247Swollman		     "-f flag cannot be used with multicast destination");
45523247Swollman	if (options & (F_MIF | F_NOLOOP | F_MTTL)
45623247Swollman	    && !IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
45723247Swollman		errx(EX_USAGE,
45823247Swollman		     "-I, -L, -T flags cannot be used with unicast destination");
45923247Swollman
46036089Sjb	if (datalen >= PHDR_LEN)	/* can we time transfer */
4611558Srgrimes		timing = 1;
4621558Srgrimes	packlen = datalen + MAXIPLEN + MAXICMPLEN;
46323247Swollman	if (!(packet = (u_char *)malloc((size_t)packlen)))
46423247Swollman		err(EX_UNAVAILABLE, "malloc");
46523247Swollman
4661558Srgrimes	if (!(options & F_PINGFILLED))
46736089Sjb		for (i = PHDR_LEN; i < datalen; ++i)
4681558Srgrimes			*datap++ = i;
4691558Srgrimes
4701558Srgrimes	ident = getpid() & 0xFFFF;
4711558Srgrimes
47217474Sfenner	if (s < 0) {
47317474Sfenner		errno = sockerrno;
47423247Swollman		err(EX_OSERR, "socket");
4751558Srgrimes	}
4761558Srgrimes	hold = 1;
4771558Srgrimes	if (options & F_SO_DEBUG)
4781558Srgrimes		(void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
4791558Srgrimes		    sizeof(hold));
4801558Srgrimes	if (options & F_SO_DONTROUTE)
4811558Srgrimes		(void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold,
4821558Srgrimes		    sizeof(hold));
48355505Sshin#ifdef IPSEC
48455505Sshin#ifdef IPSEC_POLICY_IPSEC
48555505Sshin	if (options & F_POLICY) {
48655505Sshin		char *buf;
48755505Sshin		if (policy_in != NULL) {
48855505Sshin			buf = ipsec_set_policy(policy_in, strlen(policy_in));
48955505Sshin			if (buf == NULL)
49068905Skris				errx(EX_CONFIG, "%s", ipsec_strerror());
49155505Sshin			if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY,
49255505Sshin					buf, ipsec_get_policylen(buf)) < 0)
49355505Sshin				err(EX_CONFIG, "ipsec policy cannot be configured");
49455505Sshin			free(buf);
49555505Sshin		}
4961558Srgrimes
49755505Sshin		if (policy_out != NULL) {
49855505Sshin			buf = ipsec_set_policy(policy_out, strlen(policy_out));
49955505Sshin			if (buf == NULL)
50068905Skris				errx(EX_CONFIG, "%s", ipsec_strerror());
50155505Sshin			if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY,
50255505Sshin					buf, ipsec_get_policylen(buf)) < 0)
50355505Sshin				err(EX_CONFIG, "ipsec policy cannot be configured");
50455505Sshin			free(buf);
50555505Sshin		}
50655505Sshin	}
50755505Sshin#endif /*IPSEC_POLICY_IPSEC*/
50855505Sshin#endif /*IPSEC*/
50955505Sshin
5101558Srgrimes	/* record route option */
5111558Srgrimes	if (options & F_RROUTE) {
5121558Srgrimes#ifdef IP_OPTIONS
51336378Sfenner		bzero(rspace, sizeof(rspace));
5141558Srgrimes		rspace[IPOPT_OPTVAL] = IPOPT_RR;
51536378Sfenner		rspace[IPOPT_OLEN] = sizeof(rspace) - 1;
5161558Srgrimes		rspace[IPOPT_OFFSET] = IPOPT_MINOFF;
51736378Sfenner		rspace[sizeof(rspace) - 1] = IPOPT_EOL;
5181558Srgrimes		if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace,
51923247Swollman		    sizeof(rspace)) < 0)
52023247Swollman			err(EX_OSERR, "setsockopt IP_OPTIONS");
5211558Srgrimes#else
52223247Swollman		errx(EX_UNAVAILABLE,
52323247Swollman		  "record route not available in this implementation");
5241558Srgrimes#endif /* IP_OPTIONS */
5251558Srgrimes	}
5261558Srgrimes
52774029Sru	if (options & F_TTL) {
52874029Sru		if (setsockopt(s, IPPROTO_IP, IP_TTL, &ttl,
52974029Sru		    sizeof(ttl)) < 0) {
53074029Sru			err(EX_OSERR, "setsockopt IP_TTL");
53174029Sru		}
53274029Sru	}
53320540Sfenner	if (options & F_NOLOOP) {
53420540Sfenner		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop,
53520540Sfenner		    sizeof(loop)) < 0) {
53623247Swollman			err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP");
53720540Sfenner		}
53820540Sfenner	}
53920540Sfenner	if (options & F_MTTL) {
54074029Sru		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &mttl,
54174029Sru		    sizeof(mttl)) < 0) {
54223247Swollman			err(EX_OSERR, "setsockopt IP_MULTICAST_TTL");
54320540Sfenner		}
54420540Sfenner	}
54520540Sfenner	if (options & F_MIF) {
54620540Sfenner		if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr,
54720540Sfenner		    sizeof(ifaddr)) < 0) {
54823247Swollman			err(EX_OSERR, "setsockopt IP_MULTICAST_IF");
54920540Sfenner		}
55020540Sfenner	}
55136378Sfenner#ifdef SO_TIMESTAMP
55236378Sfenner	{ int on = 1;
55336378Sfenner	if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) < 0)
55436378Sfenner		err(EX_OSERR, "setsockopt SO_TIMESTAMP");
55536378Sfenner	}
55636378Sfenner#endif
55720540Sfenner
5581558Srgrimes	/*
5591558Srgrimes	 * When pinging the broadcast address, you can get a lot of answers.
5601558Srgrimes	 * Doing something so evil is useful if you are trying to stress the
5611558Srgrimes	 * ethernet, or just want to fill the arp cache to get some stuff for
56223247Swollman	 * /etc/ethers.  But beware: RFC 1122 allows hosts to ignore broadcast
56323247Swollman	 * or multicast pings if they wish.
5641558Srgrimes	 */
5651558Srgrimes	hold = 48 * 1024;
5661558Srgrimes	(void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
5671558Srgrimes	    sizeof(hold));
5681558Srgrimes
56979018Srwatson	if (!uid) {
57079018Srwatson		(void)setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&hold,
57179018Srwatson		    sizeof(hold));
57279018Srwatson	}
57379018Srwatson
57442337Simp	if (to->sin_family == AF_INET) {
57542337Simp		(void)printf("PING %s (%s)", hostname,
57642337Simp		    inet_ntoa(to->sin_addr));
57742337Simp		if (source)
57842337Simp			(void)printf(" from %s", shostname);
57942337Simp		(void)printf(": %d data bytes\n", datalen);
58042337Simp	} else
5811558Srgrimes		(void)printf("PING %s: %d data bytes\n", hostname, datalen);
5821558Srgrimes
58320280Sbde	/*
58427354Ssef	 * Use sigaction() instead of signal() to get unambiguous semantics,
58527354Ssef	 * in particular with SA_RESTART not set.
58620280Sbde	 */
58727354Ssef
58820205Spst	sigemptyset(&si_sa.sa_mask);
58920195Ssef	si_sa.sa_flags = 0;
59027354Ssef
59127354Ssef	si_sa.sa_handler = stopit;
59227354Ssef	if (sigaction(SIGINT, &si_sa, 0) == -1) {
59327354Ssef		err(EX_OSERR, "sigaction SIGINT");
59427354Ssef	}
59527354Ssef
59627354Ssef	si_sa.sa_handler = status;
59720195Ssef	if (sigaction(SIGINFO, &si_sa, 0) == -1) {
59823385Simp		err(EX_OSERR, "sigaction");
59920195Ssef	}
60020195Ssef
60156342Sbillf        if (alarmtimeout > 0) {
60256342Sbillf		si_sa.sa_handler = stopit;
60356342Sbillf		if (sigaction(SIGALRM, &si_sa, 0) == -1)
60456342Sbillf			err(EX_OSERR, "sigaction SIGALRM");
60556342Sbillf        }
60656342Sbillf
60736378Sfenner	bzero(&msg, sizeof(msg));
60836378Sfenner	msg.msg_name = (caddr_t)&from;
60936378Sfenner	msg.msg_iov = &iov;
61036378Sfenner	msg.msg_iovlen = 1;
61136378Sfenner#ifdef SO_TIMESTAMP
61236378Sfenner	msg.msg_control = (caddr_t)ctrl;
61336378Sfenner#endif
61436378Sfenner	iov.iov_base = packet;
61536378Sfenner	iov.iov_len = packlen;
61636378Sfenner
61719864Ssef	if (tcgetattr(STDOUT_FILENO, &ts) != -1) {
61819864Ssef		reset_kerninfo = !(ts.c_lflag & NOKERNINFO);
61919864Ssef		ts.c_lflag |= NOKERNINFO;
62019864Ssef		tcsetattr(STDOUT_FILENO, TCSANOW, &ts);
62119864Ssef	}
62219864Ssef
62389349Sru	if (preload == 0)
62489349Sru		pinger();		/* send the first ping */
62589349Sru	else {
62689349Sru		if (npackets != 0 && preload > npackets)
62789349Sru			preload = npackets;
62889349Sru		while (preload--)	/* fire off them quickies */
62989349Sru			pinger();
63089349Sru	}
63189349Sru	(void)gettimeofday(&last, NULL);
6321558Srgrimes
63336378Sfenner	if (options & F_FLOOD) {
63436378Sfenner		intvl.tv_sec = 0;
63536378Sfenner		intvl.tv_usec = 10000;
63636378Sfenner	} else {
63738549Sdillon		intvl.tv_sec = interval / 1000;
63838549Sdillon		intvl.tv_usec = interval % 1000 * 1000;
63936378Sfenner	}
6401558Srgrimes
64127533Sbde	while (!finish_up) {
64292806Sobrien		int cc;
64336378Sfenner		int n;
64436378Sfenner		struct timeval timeout, now;
64536378Sfenner		fd_set rfds;
6461558Srgrimes
64720280Sbde		check_status();
64836378Sfenner		FD_ZERO(&rfds);
64936378Sfenner		FD_SET(s, &rfds);
65036378Sfenner		(void)gettimeofday(&now, NULL);
65136378Sfenner		timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec;
65236378Sfenner		timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec;
65336378Sfenner		while (timeout.tv_usec < 0) {
65436378Sfenner			timeout.tv_usec += 1000000;
65536378Sfenner			timeout.tv_sec--;
6561558Srgrimes		}
65737671Scharnier		while (timeout.tv_usec >= 1000000) {
65836378Sfenner			timeout.tv_usec -= 1000000;
65936378Sfenner			timeout.tv_sec++;
66036378Sfenner		}
66136378Sfenner		if (timeout.tv_sec < 0)
66236378Sfenner			timeout.tv_sec = timeout.tv_usec = 0;
66336378Sfenner		n = select(s + 1, &rfds, NULL, NULL, &timeout);
66446643Smckay		if (n < 0)
66546643Smckay			continue;	/* Must be EINTR. */
66636378Sfenner		if (n == 1) {
66736378Sfenner			struct timeval *t = 0;
66836378Sfenner#ifdef SO_TIMESTAMP
66936378Sfenner			struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl;
67036378Sfenner
67136378Sfenner			msg.msg_controllen = sizeof(ctrl);
67236378Sfenner#endif
67336378Sfenner			msg.msg_namelen = sizeof(from);
67436378Sfenner			if ((cc = recvmsg(s, &msg, 0)) < 0) {
67536378Sfenner				if (errno == EINTR)
67636378Sfenner					continue;
67737671Scharnier				warn("recvmsg");
6781558Srgrimes				continue;
67936378Sfenner			}
68036378Sfenner#ifdef SO_TIMESTAMP
68136378Sfenner			if (cmsg->cmsg_level == SOL_SOCKET &&
68236378Sfenner			    cmsg->cmsg_type == SCM_TIMESTAMP &&
68358068Sshin			    cmsg->cmsg_len == CMSG_LEN(sizeof *t)) {
68436713Sjb				/* Copy to avoid alignment problems: */
68536713Sjb				memcpy(&now,CMSG_DATA(cmsg),sizeof(now));
68636713Sjb				t = &now;
68736713Sjb			}
68836378Sfenner#endif
68936378Sfenner			if (t == 0) {
69036378Sfenner				(void)gettimeofday(&now, NULL);
69136378Sfenner				t = &now;
69236378Sfenner			}
69336378Sfenner			pr_pack((char *)packet, cc, &from, t);
69436378Sfenner			if (npackets && nreceived >= npackets)
69536378Sfenner				break;
6961558Srgrimes		}
69746643Smckay		if (n == 0 || options & F_FLOOD) {
69836378Sfenner			if (!npackets || ntransmitted < npackets)
69936378Sfenner				pinger();
70036378Sfenner			else {
70136378Sfenner				if (almost_done)
70236378Sfenner					break;
70336378Sfenner				almost_done = 1;
70446643Smckay				intvl.tv_usec = 0;
70536378Sfenner				if (nreceived) {
70636378Sfenner					intvl.tv_sec = 2 * tmax / 1000;
70736378Sfenner					if (!intvl.tv_sec)
70836378Sfenner						intvl.tv_sec = 1;
70936378Sfenner				} else
71036378Sfenner					intvl.tv_sec = MAXWAIT;
71136378Sfenner			}
71236378Sfenner			(void)gettimeofday(&last, NULL);
71377119Sphk
71483940Siedowse			if (ntransmitted - nreceived - 1 > nmissedmax) {
71583940Siedowse				nmissedmax = ntransmitted - nreceived - 1;
71683940Siedowse				if (options & F_MISSED)
71783940Siedowse					(void)write(STDOUT_FILENO, &BBELL, 1);
71883940Siedowse			}
71936378Sfenner		}
7201558Srgrimes	}
72127533Sbde	finish();
7221558Srgrimes	/* NOTREACHED */
72323251Simp	exit(0);	/* Make the compiler happy */
7241558Srgrimes}
7251558Srgrimes
7261558Srgrimes/*
72727533Sbde * stopit --
72827533Sbde *	Set the global bit that causes the main loop to quit.
72927533Sbde * Do NOT call finish() from here, since finish() does far too much
73027533Sbde * to be called from a signal handler.
73127299Sjulian */
73227299Sjulianvoid
73327533Sbdestopit(sig)
73427533Sbde	int sig;
73527299Sjulian{
73627299Sjulian	finish_up = 1;
73727299Sjulian}
73827299Sjulian
73927299Sjulian/*
7401558Srgrimes * pinger --
7411558Srgrimes *	Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
7421558Srgrimes * will be added on by the kernel.  The ID field is our UNIX process ID,
7431558Srgrimes * and the sequence number is an ascending integer.  The first 8 bytes
74417724Sfenner * of the data portion are used to hold a UNIX "timeval" struct in host
7451558Srgrimes * byte-order, to compute the round-trip time.
7461558Srgrimes */
74723247Swollmanstatic void
74823247Swollmanpinger(void)
7491558Srgrimes{
75092806Sobrien	struct icmp *icp;
75192806Sobrien	int cc;
7521558Srgrimes	int i;
7531558Srgrimes
7541558Srgrimes	icp = (struct icmp *)outpack;
7551558Srgrimes	icp->icmp_type = ICMP_ECHO;
7561558Srgrimes	icp->icmp_code = 0;
7571558Srgrimes	icp->icmp_cksum = 0;
75891432Sfenner	icp->icmp_seq = htons(ntransmitted);
7591558Srgrimes	icp->icmp_id = ident;			/* ID */
7601558Srgrimes
76191432Sfenner	CLR(ntransmitted % mx_dup_ck);
7621558Srgrimes
7631558Srgrimes	if (timing)
7641558Srgrimes		(void)gettimeofday((struct timeval *)&outpack[8],
7651558Srgrimes		    (struct timezone *)NULL);
7661558Srgrimes
76736089Sjb	cc = datalen + PHDR_LEN;		/* skips ICMP portion */
7681558Srgrimes
7691558Srgrimes	/* compute ICMP checksum here */
7701558Srgrimes	icp->icmp_cksum = in_cksum((u_short *)icp, cc);
7711558Srgrimes
77279403Smjacob	i = sendto(s, (char *)outpack, cc, 0, (struct sockaddr *)&whereto,
77379403Smjacob	    sizeof(whereto));
7741558Srgrimes
7751558Srgrimes	if (i < 0 || i != cc)  {
77623247Swollman		if (i < 0) {
77727533Sbde			if (options & F_FLOOD && errno == ENOBUFS) {
77827299Sjulian				usleep(FLOOD_BACKOFF);
77927299Sjulian				return;
78027299Sjulian			}
78123247Swollman			warn("sendto");
78223247Swollman		} else {
78323247Swollman			warn("%s: partial write: %d of %d bytes",
78435216Sphk			     hostname, i, cc);
78523247Swollman		}
78627945Sjulian	}
78727945Sjulian	ntransmitted++;
7881558Srgrimes	if (!(options & F_QUIET) && options & F_FLOOD)
7891558Srgrimes		(void)write(STDOUT_FILENO, &DOT, 1);
7901558Srgrimes}
7911558Srgrimes
7921558Srgrimes/*
7931558Srgrimes * pr_pack --
7941558Srgrimes *	Print out the packet, if it came from us.  This logic is necessary
7951558Srgrimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
7961558Srgrimes * which arrive ('tis only fair).  This permits multiple copies of this
7971558Srgrimes * program to be run without having intermingled output (or statistics!).
7981558Srgrimes */
79923247Swollmanstatic void
80036378Sfennerpr_pack(buf, cc, from, tv)
8011558Srgrimes	char *buf;
8021558Srgrimes	int cc;
8031558Srgrimes	struct sockaddr_in *from;
80436378Sfenner	struct timeval *tv;
8051558Srgrimes{
80692806Sobrien	struct icmp *icp;
80792806Sobrien	u_long l;
80892806Sobrien	int i, j;
80992806Sobrien	u_char *cp,*dp;
8101558Srgrimes	static int old_rrlen;
8111558Srgrimes	static char old_rr[MAX_IPOPTLEN];
8121558Srgrimes	struct ip *ip;
81336378Sfenner	struct timeval *tp;
81427533Sbde	double triptime;
81591432Sfenner	int hlen, dupflag, seq;
8161558Srgrimes
8171558Srgrimes	/* Check the IP header */
8181558Srgrimes	ip = (struct ip *)buf;
8191558Srgrimes	hlen = ip->ip_hl << 2;
8201558Srgrimes	if (cc < hlen + ICMP_MINLEN) {
8211558Srgrimes		if (options & F_VERBOSE)
82223247Swollman			warn("packet too short (%d bytes) from %s", cc,
82323247Swollman			     inet_ntoa(from->sin_addr));
8241558Srgrimes		return;
8251558Srgrimes	}
8261558Srgrimes
8271558Srgrimes	/* Now the ICMP part */
8281558Srgrimes	cc -= hlen;
8291558Srgrimes	icp = (struct icmp *)(buf + hlen);
8301558Srgrimes	if (icp->icmp_type == ICMP_ECHOREPLY) {
8311558Srgrimes		if (icp->icmp_id != ident)
8321558Srgrimes			return;			/* 'Twas not our ECHO */
8331558Srgrimes		++nreceived;
83427533Sbde		triptime = 0.0;
8351558Srgrimes		if (timing) {
83636089Sjb			struct timeval tv1;
8371558Srgrimes#ifndef icmp_data
8381558Srgrimes			tp = (struct timeval *)&icp->icmp_ip;
8391558Srgrimes#else
8401558Srgrimes			tp = (struct timeval *)icp->icmp_data;
8411558Srgrimes#endif
84236089Sjb			/* Avoid unaligned data: */
84336089Sjb			memcpy(&tv1,tp,sizeof(tv1));
84436378Sfenner			tvsub(tv, &tv1);
84536378Sfenner 			triptime = ((double)tv->tv_sec) * 1000.0 +
84636378Sfenner 			    ((double)tv->tv_usec) / 1000.0;
8471558Srgrimes			tsum += triptime;
84827508Swollman			tsumsq += triptime * triptime;
8491558Srgrimes			if (triptime < tmin)
8501558Srgrimes				tmin = triptime;
8511558Srgrimes			if (triptime > tmax)
8521558Srgrimes				tmax = triptime;
8531558Srgrimes		}
8541558Srgrimes
85591432Sfenner		seq = ntohs(icp->icmp_seq);
85691432Sfenner
85791432Sfenner		if (TST(seq % mx_dup_ck)) {
8581558Srgrimes			++nrepeats;
8591558Srgrimes			--nreceived;
8601558Srgrimes			dupflag = 1;
8611558Srgrimes		} else {
86291432Sfenner			SET(seq % mx_dup_ck);
8631558Srgrimes			dupflag = 0;
8641558Srgrimes		}
8651558Srgrimes
8661558Srgrimes		if (options & F_QUIET)
8671558Srgrimes			return;
8681558Srgrimes
8691558Srgrimes		if (options & F_FLOOD)
8701558Srgrimes			(void)write(STDOUT_FILENO, &BSPACE, 1);
8711558Srgrimes		else {
8721558Srgrimes			(void)printf("%d bytes from %s: icmp_seq=%u", cc,
8731558Srgrimes			   inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr),
87491432Sfenner			   seq);
8751558Srgrimes			(void)printf(" ttl=%d", ip->ip_ttl);
8761558Srgrimes			if (timing)
8771859Sdg				(void)printf(" time=%.3f ms", triptime);
8781558Srgrimes			if (dupflag)
8791558Srgrimes				(void)printf(" (DUP!)");
88022417Sdanny			if (options & F_AUDIBLE)
88177119Sphk				(void)write(STDOUT_FILENO, &BBELL, 1);
8821558Srgrimes			/* check the data */
88336089Sjb			cp = (u_char*)&icp->icmp_data[PHDR_LEN];
88436089Sjb			dp = &outpack[8 + PHDR_LEN];
88536089Sjb			for (i = PHDR_LEN; i < datalen; ++i, ++cp, ++dp) {
8861558Srgrimes				if (*cp != *dp) {
8871558Srgrimes	(void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x",
8881558Srgrimes	    i, *dp, *cp);
88936089Sjb					printf("\ncp:");
8901558Srgrimes					cp = (u_char*)&icp->icmp_data[0];
89136089Sjb					for (i = 0; i < datalen; ++i, ++cp) {
8921558Srgrimes						if ((i % 32) == 8)
8931558Srgrimes							(void)printf("\n\t");
8941558Srgrimes						(void)printf("%x ", *cp);
8951558Srgrimes					}
89636089Sjb					printf("\ndp:");
89736089Sjb					cp = &outpack[8];
89836089Sjb					for (i = 0; i < datalen; ++i, ++cp) {
89936089Sjb						if ((i % 32) == 8)
90036089Sjb							(void)printf("\n\t");
90136089Sjb						(void)printf("%x ", *cp);
90236089Sjb					}
9031558Srgrimes					break;
9041558Srgrimes				}
9051558Srgrimes			}
9061558Srgrimes		}
9071558Srgrimes	} else {
90817724Sfenner		/*
90917724Sfenner		 * We've got something other than an ECHOREPLY.
91017724Sfenner		 * See if it's a reply to something that we sent.
91117724Sfenner		 * We can compare IP destination, protocol,
91217724Sfenner		 * and ICMP type and ID.
91323251Simp		 *
91423251Simp		 * Only print all the error messages if we are running
91523251Simp		 * as root to avoid leaking information not normally
91623251Simp		 * available to those not running as root.
91717724Sfenner		 */
91817724Sfenner#ifndef icmp_data
91917724Sfenner		struct ip *oip = &icp->icmp_ip;
92017724Sfenner#else
92117724Sfenner		struct ip *oip = (struct ip *)icp->icmp_data;
92217724Sfenner#endif
92317724Sfenner		struct icmp *oicmp = (struct icmp *)(oip + 1);
92417724Sfenner
92523295Simp		if (((options & F_VERBOSE) && uid == 0) ||
92617724Sfenner		    (!(options & F_QUIET2) &&
92779403Smjacob		     (oip->ip_dst.s_addr == whereto.sin_addr.s_addr) &&
92817724Sfenner		     (oip->ip_p == IPPROTO_ICMP) &&
92917724Sfenner		     (oicmp->icmp_type == ICMP_ECHO) &&
93017724Sfenner		     (oicmp->icmp_id == ident))) {
93117724Sfenner		    (void)printf("%d bytes from %s: ", cc,
93223247Swollman			pr_addr(from->sin_addr));
93317724Sfenner		    pr_icmph(icp);
93417724Sfenner		} else
93517724Sfenner		    return;
9361558Srgrimes	}
9371558Srgrimes
9381558Srgrimes	/* Display any IP options */
9391558Srgrimes	cp = (u_char *)buf + sizeof(struct ip);
9401558Srgrimes
9411558Srgrimes	for (; hlen > (int)sizeof(struct ip); --hlen, ++cp)
9421558Srgrimes		switch (*cp) {
9431558Srgrimes		case IPOPT_EOL:
9441558Srgrimes			hlen = 0;
9451558Srgrimes			break;
9461558Srgrimes		case IPOPT_LSRR:
9471558Srgrimes			(void)printf("\nLSRR: ");
9481558Srgrimes			hlen -= 2;
9491558Srgrimes			j = *++cp;
9501558Srgrimes			++cp;
9511558Srgrimes			if (j > IPOPT_MINOFF)
9521558Srgrimes				for (;;) {
9531558Srgrimes					l = *++cp;
9541558Srgrimes					l = (l<<8) + *++cp;
9551558Srgrimes					l = (l<<8) + *++cp;
9561558Srgrimes					l = (l<<8) + *++cp;
95723247Swollman					if (l == 0) {
95823247Swollman						printf("\t0.0.0.0");
95923247Swollman					} else {
96023247Swollman						struct in_addr ina;
96123247Swollman						ina.s_addr = ntohl(l);
96223247Swollman						printf("\t%s", pr_addr(ina));
96323247Swollman					}
9641558Srgrimes				hlen -= 4;
9651558Srgrimes				j -= 4;
9661558Srgrimes				if (j <= IPOPT_MINOFF)
9671558Srgrimes					break;
9681558Srgrimes				(void)putchar('\n');
9691558Srgrimes			}
9701558Srgrimes			break;
9711558Srgrimes		case IPOPT_RR:
9721558Srgrimes			j = *++cp;		/* get length */
9731558Srgrimes			i = *++cp;		/* and pointer */
9741558Srgrimes			hlen -= 2;
9751558Srgrimes			if (i > j)
9761558Srgrimes				i = j;
9771558Srgrimes			i -= IPOPT_MINOFF;
9781558Srgrimes			if (i <= 0)
9791558Srgrimes				continue;
9801558Srgrimes			if (i == old_rrlen
9811558Srgrimes			    && cp == (u_char *)buf + sizeof(struct ip) + 2
9821558Srgrimes			    && !bcmp((char *)cp, old_rr, i)
9831558Srgrimes			    && !(options & F_FLOOD)) {
9841558Srgrimes				(void)printf("\t(same route)");
9851558Srgrimes				i = ((i + 3) / 4) * 4;
9861558Srgrimes				hlen -= i;
9871558Srgrimes				cp += i;
9881558Srgrimes				break;
9891558Srgrimes			}
99034995Seivind			if (i < MAX_IPOPTLEN) {
99134976Simp				old_rrlen = i;
99234976Simp				bcopy((char *)cp, old_rr, i);
99334976Simp			} else
99434976Simp				old_rrlen = 0;
99534976Simp
9961558Srgrimes			(void)printf("\nRR: ");
99734976Simp			j = 0;
9981558Srgrimes			for (;;) {
9991558Srgrimes				l = *++cp;
10001558Srgrimes				l = (l<<8) + *++cp;
10011558Srgrimes				l = (l<<8) + *++cp;
10021558Srgrimes				l = (l<<8) + *++cp;
100323247Swollman				if (l == 0) {
100423247Swollman					printf("\t0.0.0.0");
100523247Swollman				} else {
100623247Swollman					struct in_addr ina;
100723247Swollman					ina.s_addr = ntohl(l);
100823247Swollman					printf("\t%s", pr_addr(ina));
100923247Swollman				}
10101558Srgrimes				hlen -= 4;
10111558Srgrimes				i -= 4;
101234976Simp				j += 4;
10131558Srgrimes				if (i <= 0)
10141558Srgrimes					break;
101534976Simp				if (j >= MAX_IPOPTLEN) {
101634976Simp					(void) printf("\t(truncated route)");
101734976Simp					break;
101834976Simp				}
10191558Srgrimes				(void)putchar('\n');
10201558Srgrimes			}
10211558Srgrimes			break;
10221558Srgrimes		case IPOPT_NOP:
10231558Srgrimes			(void)printf("\nNOP");
10241558Srgrimes			break;
10251558Srgrimes		default:
10261558Srgrimes			(void)printf("\nunknown option %x", *cp);
10271558Srgrimes			break;
10281558Srgrimes		}
10291558Srgrimes	if (!(options & F_FLOOD)) {
10301558Srgrimes		(void)putchar('\n');
10311558Srgrimes		(void)fflush(stdout);
10321558Srgrimes	}
10331558Srgrimes}
10341558Srgrimes
10351558Srgrimes/*
10361558Srgrimes * in_cksum --
10371558Srgrimes *	Checksum routine for Internet Protocol family headers (C Version)
10381558Srgrimes */
103923247Swollmanu_short
10401558Srgrimesin_cksum(addr, len)
10411558Srgrimes	u_short *addr;
10421558Srgrimes	int len;
10431558Srgrimes{
104492806Sobrien	int nleft = len;
104592806Sobrien	u_short *w = addr;
104692806Sobrien	int sum = 0;
104753191Spb	union {
104853369Spb		u_short	us;
104953369Spb		u_char	uc[2];
105053369Spb	} last;
105153369Spb	u_short answer;
10521558Srgrimes
10531558Srgrimes	/*
10541558Srgrimes	 * Our algorithm is simple, using a 32 bit accumulator (sum), we add
10551558Srgrimes	 * sequential 16 bit words to it, and at the end, fold back all the
10561558Srgrimes	 * carry bits from the top 16 bits into the lower 16 bits.
10571558Srgrimes	 */
10581558Srgrimes	while (nleft > 1)  {
10591558Srgrimes		sum += *w++;
10601558Srgrimes		nleft -= 2;
10611558Srgrimes	}
10621558Srgrimes
10631558Srgrimes	/* mop up an odd byte, if necessary */
10641558Srgrimes	if (nleft == 1) {
106553369Spb		last.uc[0] = *(u_char *)w;
106653369Spb		last.uc[1] = 0;
106753369Spb		sum += last.us;
10681558Srgrimes	}
10691558Srgrimes
10701558Srgrimes	/* add back carry outs from top 16 bits to low 16 bits */
10711558Srgrimes	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
10721558Srgrimes	sum += (sum >> 16);			/* add carry */
107353369Spb	answer = ~sum;				/* truncate to 16 bits */
107453369Spb	return(answer);
10751558Srgrimes}
10761558Srgrimes
10771558Srgrimes/*
10781558Srgrimes * tvsub --
10791558Srgrimes *	Subtract 2 timeval structs:  out = out - in.  Out is assumed to
10801558Srgrimes * be >= in.
10811558Srgrimes */
108223247Swollmanstatic void
10831558Srgrimestvsub(out, in)
108492806Sobrien	struct timeval *out, *in;
10851558Srgrimes{
10861558Srgrimes	if ((out->tv_usec -= in->tv_usec) < 0) {
10871558Srgrimes		--out->tv_sec;
10881558Srgrimes		out->tv_usec += 1000000;
10891558Srgrimes	}
10901558Srgrimes	out->tv_sec -= in->tv_sec;
10911558Srgrimes}
10921558Srgrimes
10931558Srgrimes/*
10943792Ssef * status --
10953792Ssef *	Print out statistics when SIGINFO is received.
10963792Ssef */
10973792Ssef
109823247Swollmanstatic void
109920280Sbdestatus(sig)
110020280Sbde	int sig;
110120280Sbde{
110220195Ssef	siginfo_p = 1;
110320195Ssef}
110420195Ssef
110523247Swollmanstatic void
110620195Ssefcheck_status()
11073792Ssef{
110820195Ssef	if (siginfo_p) {
110920195Ssef		siginfo_p = 0;
111020280Sbde		(void)fprintf(stderr,
111120280Sbde	"\r%ld/%ld packets received (%.0f%%) %.3f min / %.3f avg / %.3f max\n",
111220280Sbde		    nreceived, ntransmitted,
111320280Sbde		    ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0,
111420280Sbde		    nreceived ? tmin : 0.0,
111520280Sbde		    nreceived + nrepeats ? tsum / (nreceived + nrepeats) : tsum,
111620280Sbde		    tmax);
111720195Ssef	}
11183792Ssef}
11193792Ssef
11203792Ssef/*
11211558Srgrimes * finish --
11221558Srgrimes *	Print out statistics, and give up.
11231558Srgrimes */
112423247Swollmanstatic void
112527533Sbdefinish()
11261558Srgrimes{
11273792Ssef	struct termios ts;
11281558Srgrimes
11291558Srgrimes	(void)signal(SIGINT, SIG_IGN);
113027354Ssef	(void)signal(SIGALRM, SIG_IGN);
11311558Srgrimes	(void)putchar('\n');
11321558Srgrimes	(void)fflush(stdout);
11331558Srgrimes	(void)printf("--- %s ping statistics ---\n", hostname);
11341558Srgrimes	(void)printf("%ld packets transmitted, ", ntransmitted);
11351558Srgrimes	(void)printf("%ld packets received, ", nreceived);
11361558Srgrimes	if (nrepeats)
11371558Srgrimes		(void)printf("+%ld duplicates, ", nrepeats);
113846080Simp	if (ntransmitted) {
11391558Srgrimes		if (nreceived > ntransmitted)
11401558Srgrimes			(void)printf("-- somebody's printing up packets!");
11411558Srgrimes		else
11421558Srgrimes			(void)printf("%d%% packet loss",
11431558Srgrimes			    (int) (((ntransmitted - nreceived) * 100) /
11441558Srgrimes			    ntransmitted));
114546080Simp	}
11461558Srgrimes	(void)putchar('\n');
114727508Swollman	if (nreceived && timing) {
114827508Swollman		double n = nreceived + nrepeats;
114927508Swollman		double avg = tsum / n;
115027508Swollman		double vari = tsumsq / n - avg * avg;
115127508Swollman		printf("round-trip min/avg/max/stddev = "
115227508Swollman		       "%.3f/%.3f/%.3f/%.3f ms\n",
115327508Swollman		    tmin, avg, tmax, sqrt(vari));
115427508Swollman	}
115519395Sbde	if (reset_kerninfo && tcgetattr(STDOUT_FILENO, &ts) != -1) {
11563792Ssef		ts.c_lflag &= ~NOKERNINFO;
115719395Sbde		tcsetattr(STDOUT_FILENO, TCSANOW, &ts);
11583792Ssef	}
11593792Ssef
11608871Srgrimes	if (nreceived)
11614862Sdg		exit(0);
11624862Sdg	else
11634862Sdg		exit(2);
11641558Srgrimes}
11651558Srgrimes
11661558Srgrimes#ifdef notdef
11671558Srgrimesstatic char *ttab[] = {
11681558Srgrimes	"Echo Reply",		/* ip + seq + udata */
11691558Srgrimes	"Dest Unreachable",	/* net, host, proto, port, frag, sr + IP */
11701558Srgrimes	"Source Quench",	/* IP */
11711558Srgrimes	"Redirect",		/* redirect type, gateway, + IP  */
11721558Srgrimes	"Echo",
11731558Srgrimes	"Time Exceeded",	/* transit, frag reassem + IP */
11741558Srgrimes	"Parameter Problem",	/* pointer + IP */
11751558Srgrimes	"Timestamp",		/* id + seq + three timestamps */
11761558Srgrimes	"Timestamp Reply",	/* " */
11771558Srgrimes	"Info Request",		/* id + sq */
11781558Srgrimes	"Info Reply"		/* " */
11791558Srgrimes};
11801558Srgrimes#endif
11811558Srgrimes
11821558Srgrimes/*
11831558Srgrimes * pr_icmph --
11841558Srgrimes *	Print a descriptive string about an ICMP header.
11851558Srgrimes */
118623247Swollmanstatic void
11871558Srgrimespr_icmph(icp)
11881558Srgrimes	struct icmp *icp;
11891558Srgrimes{
11901558Srgrimes	switch(icp->icmp_type) {
11911558Srgrimes	case ICMP_ECHOREPLY:
11921558Srgrimes		(void)printf("Echo Reply\n");
11931558Srgrimes		/* XXX ID + Seq + Data */
11941558Srgrimes		break;
11951558Srgrimes	case ICMP_UNREACH:
11961558Srgrimes		switch(icp->icmp_code) {
11971558Srgrimes		case ICMP_UNREACH_NET:
11981558Srgrimes			(void)printf("Destination Net Unreachable\n");
11991558Srgrimes			break;
12001558Srgrimes		case ICMP_UNREACH_HOST:
12011558Srgrimes			(void)printf("Destination Host Unreachable\n");
12021558Srgrimes			break;
12031558Srgrimes		case ICMP_UNREACH_PROTOCOL:
12041558Srgrimes			(void)printf("Destination Protocol Unreachable\n");
12051558Srgrimes			break;
12061558Srgrimes		case ICMP_UNREACH_PORT:
12071558Srgrimes			(void)printf("Destination Port Unreachable\n");
12081558Srgrimes			break;
12091558Srgrimes		case ICMP_UNREACH_NEEDFRAG:
121017724Sfenner			(void)printf("frag needed and DF set (MTU %d)\n",
121128059Sfenner					ntohs(icp->icmp_nextmtu));
12121558Srgrimes			break;
12131558Srgrimes		case ICMP_UNREACH_SRCFAIL:
12141558Srgrimes			(void)printf("Source Route Failed\n");
12151558Srgrimes			break;
121617724Sfenner		case ICMP_UNREACH_FILTER_PROHIB:
121717724Sfenner			(void)printf("Communication prohibited by filter\n");
121817724Sfenner			break;
12191558Srgrimes		default:
12201558Srgrimes			(void)printf("Dest Unreachable, Bad Code: %d\n",
12211558Srgrimes			    icp->icmp_code);
12221558Srgrimes			break;
12231558Srgrimes		}
12241558Srgrimes		/* Print returned IP header information */
12251558Srgrimes#ifndef icmp_data
12261558Srgrimes		pr_retip(&icp->icmp_ip);
12271558Srgrimes#else
12281558Srgrimes		pr_retip((struct ip *)icp->icmp_data);
12291558Srgrimes#endif
12301558Srgrimes		break;
12311558Srgrimes	case ICMP_SOURCEQUENCH:
12321558Srgrimes		(void)printf("Source Quench\n");
12331558Srgrimes#ifndef icmp_data
12341558Srgrimes		pr_retip(&icp->icmp_ip);
12351558Srgrimes#else
12361558Srgrimes		pr_retip((struct ip *)icp->icmp_data);
12371558Srgrimes#endif
12381558Srgrimes		break;
12391558Srgrimes	case ICMP_REDIRECT:
12401558Srgrimes		switch(icp->icmp_code) {
12411558Srgrimes		case ICMP_REDIRECT_NET:
12421558Srgrimes			(void)printf("Redirect Network");
12431558Srgrimes			break;
12441558Srgrimes		case ICMP_REDIRECT_HOST:
12451558Srgrimes			(void)printf("Redirect Host");
12461558Srgrimes			break;
12471558Srgrimes		case ICMP_REDIRECT_TOSNET:
12481558Srgrimes			(void)printf("Redirect Type of Service and Network");
12491558Srgrimes			break;
12501558Srgrimes		case ICMP_REDIRECT_TOSHOST:
12511558Srgrimes			(void)printf("Redirect Type of Service and Host");
12521558Srgrimes			break;
12531558Srgrimes		default:
12541558Srgrimes			(void)printf("Redirect, Bad Code: %d", icp->icmp_code);
12551558Srgrimes			break;
12561558Srgrimes		}
125728059Sfenner		(void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr));
12581558Srgrimes#ifndef icmp_data
12591558Srgrimes		pr_retip(&icp->icmp_ip);
12601558Srgrimes#else
12611558Srgrimes		pr_retip((struct ip *)icp->icmp_data);
12621558Srgrimes#endif
12631558Srgrimes		break;
12641558Srgrimes	case ICMP_ECHO:
12651558Srgrimes		(void)printf("Echo Request\n");
12661558Srgrimes		/* XXX ID + Seq + Data */
12671558Srgrimes		break;
12681558Srgrimes	case ICMP_TIMXCEED:
12691558Srgrimes		switch(icp->icmp_code) {
12701558Srgrimes		case ICMP_TIMXCEED_INTRANS:
12711558Srgrimes			(void)printf("Time to live exceeded\n");
12721558Srgrimes			break;
12731558Srgrimes		case ICMP_TIMXCEED_REASS:
12741558Srgrimes			(void)printf("Frag reassembly time exceeded\n");
12751558Srgrimes			break;
12761558Srgrimes		default:
12771558Srgrimes			(void)printf("Time exceeded, Bad Code: %d\n",
12781558Srgrimes			    icp->icmp_code);
12791558Srgrimes			break;
12801558Srgrimes		}
12811558Srgrimes#ifndef icmp_data
12821558Srgrimes		pr_retip(&icp->icmp_ip);
12831558Srgrimes#else
12841558Srgrimes		pr_retip((struct ip *)icp->icmp_data);
12851558Srgrimes#endif
12861558Srgrimes		break;
12871558Srgrimes	case ICMP_PARAMPROB:
12881558Srgrimes		(void)printf("Parameter problem: pointer = 0x%02x\n",
12891558Srgrimes		    icp->icmp_hun.ih_pptr);
12901558Srgrimes#ifndef icmp_data
12911558Srgrimes		pr_retip(&icp->icmp_ip);
12921558Srgrimes#else
12931558Srgrimes		pr_retip((struct ip *)icp->icmp_data);
12941558Srgrimes#endif
12951558Srgrimes		break;
12961558Srgrimes	case ICMP_TSTAMP:
12971558Srgrimes		(void)printf("Timestamp\n");
12981558Srgrimes		/* XXX ID + Seq + 3 timestamps */
12991558Srgrimes		break;
13001558Srgrimes	case ICMP_TSTAMPREPLY:
13011558Srgrimes		(void)printf("Timestamp Reply\n");
13021558Srgrimes		/* XXX ID + Seq + 3 timestamps */
13031558Srgrimes		break;
13041558Srgrimes	case ICMP_IREQ:
13051558Srgrimes		(void)printf("Information Request\n");
13061558Srgrimes		/* XXX ID + Seq */
13071558Srgrimes		break;
13081558Srgrimes	case ICMP_IREQREPLY:
13091558Srgrimes		(void)printf("Information Reply\n");
13101558Srgrimes		/* XXX ID + Seq */
13111558Srgrimes		break;
13121558Srgrimes	case ICMP_MASKREQ:
13131558Srgrimes		(void)printf("Address Mask Request\n");
13141558Srgrimes		break;
13151558Srgrimes	case ICMP_MASKREPLY:
13161558Srgrimes		(void)printf("Address Mask Reply\n");
13171558Srgrimes		break;
131817724Sfenner	case ICMP_ROUTERADVERT:
131917724Sfenner		(void)printf("Router Advertisement\n");
132017724Sfenner		break;
132117724Sfenner	case ICMP_ROUTERSOLICIT:
132217724Sfenner		(void)printf("Router Solicitation\n");
132317724Sfenner		break;
13241558Srgrimes	default:
13251558Srgrimes		(void)printf("Bad ICMP type: %d\n", icp->icmp_type);
13261558Srgrimes	}
13271558Srgrimes}
13281558Srgrimes
13291558Srgrimes/*
13301558Srgrimes * pr_iph --
13311558Srgrimes *	Print an IP header with options.
13321558Srgrimes */
133323247Swollmanstatic void
13341558Srgrimespr_iph(ip)
13351558Srgrimes	struct ip *ip;
13361558Srgrimes{
13371558Srgrimes	int hlen;
13381558Srgrimes	u_char *cp;
13391558Srgrimes
13401558Srgrimes	hlen = ip->ip_hl << 2;
13411558Srgrimes	cp = (u_char *)ip + 20;		/* point to options */
13421558Srgrimes
134317724Sfenner	(void)printf("Vr HL TOS  Len   ID Flg  off TTL Pro  cks      Src      Dst\n");
13441558Srgrimes	(void)printf(" %1x  %1x  %02x %04x %04x",
134517724Sfenner	    ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len),
134617724Sfenner	    ntohs(ip->ip_id));
134736089Sjb	(void)printf("   %1lx %04lx",
134836089Sjb	    (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13,
134936089Sjb	    (u_long) ntohl(ip->ip_off) & 0x1fff);
135017724Sfenner	(void)printf("  %02x  %02x %04x", ip->ip_ttl, ip->ip_p,
135117724Sfenner							    ntohs(ip->ip_sum));
13521558Srgrimes	(void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr));
13531558Srgrimes	(void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr));
135417724Sfenner	/* dump any option bytes */
13551558Srgrimes	while (hlen-- > 20) {
13561558Srgrimes		(void)printf("%02x", *cp++);
13571558Srgrimes	}
13581558Srgrimes	(void)putchar('\n');
13591558Srgrimes}
13601558Srgrimes
13611558Srgrimes/*
13621558Srgrimes * pr_addr --
13631558Srgrimes *	Return an ascii host address as a dotted quad and optionally with
13641558Srgrimes * a hostname.
13651558Srgrimes */
136623247Swollmanstatic char *
136723247Swollmanpr_addr(ina)
136823247Swollman	struct in_addr ina;
13691558Srgrimes{
13701558Srgrimes	struct hostent *hp;
137123251Simp	static char buf[16 + 3 + MAXHOSTNAMELEN];
13721558Srgrimes
13731558Srgrimes	if ((options & F_NUMERIC) ||
137423247Swollman	    !(hp = gethostbyaddr((char *)&ina, 4, AF_INET)))
137523247Swollman		return inet_ntoa(ina);
13761558Srgrimes	else
137717320Speter		(void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name,
137823247Swollman		    inet_ntoa(ina));
13791558Srgrimes	return(buf);
13801558Srgrimes}
13811558Srgrimes
13821558Srgrimes/*
13831558Srgrimes * pr_retip --
13841558Srgrimes *	Dump some info on a returned (via ICMP) IP packet.
13851558Srgrimes */
138623247Swollmanstatic void
13871558Srgrimespr_retip(ip)
13881558Srgrimes	struct ip *ip;
13891558Srgrimes{
13901558Srgrimes	int hlen;
13911558Srgrimes	u_char *cp;
13921558Srgrimes
13931558Srgrimes	pr_iph(ip);
13941558Srgrimes	hlen = ip->ip_hl << 2;
13951558Srgrimes	cp = (u_char *)ip + hlen;
13961558Srgrimes
13971558Srgrimes	if (ip->ip_p == 6)
13981558Srgrimes		(void)printf("TCP: from port %u, to port %u (decimal)\n",
13991558Srgrimes		    (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
14001558Srgrimes	else if (ip->ip_p == 17)
14011558Srgrimes		(void)printf("UDP: from port %u, to port %u (decimal)\n",
14021558Srgrimes			(*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
14031558Srgrimes}
14041558Srgrimes
140523247Swollmanstatic void
14061558Srgrimesfill(bp, patp)
14071558Srgrimes	char *bp, *patp;
14081558Srgrimes{
140992806Sobrien	int ii, jj, kk;
14101558Srgrimes	int pat[16];
14111558Srgrimes	char *cp;
14121558Srgrimes
141323247Swollman	for (cp = patp; *cp; cp++) {
141423247Swollman		if (!isxdigit(*cp))
141523247Swollman			errx(EX_USAGE,
141623247Swollman			     "patterns must be specified as hex digits");
141723247Swollman
141823247Swollman	}
14191558Srgrimes	ii = sscanf(patp,
14201558Srgrimes	    "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
14211558Srgrimes	    &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
14221558Srgrimes	    &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
14231558Srgrimes	    &pat[13], &pat[14], &pat[15]);
14241558Srgrimes
14251558Srgrimes	if (ii > 0)
14261558Srgrimes		for (kk = 0;
142736089Sjb		    kk <= MAXPACKET - (8 + PHDR_LEN + ii);
14281558Srgrimes		    kk += ii)
14291558Srgrimes			for (jj = 0; jj < ii; ++jj)
14301558Srgrimes				bp[jj + kk] = pat[jj];
14311558Srgrimes	if (!(options & F_QUIET)) {
14321558Srgrimes		(void)printf("PATTERN: 0x");
14331558Srgrimes		for (jj = 0; jj < ii; ++jj)
14341558Srgrimes			(void)printf("%02x", bp[jj] & 0xFF);
14351558Srgrimes		(void)printf("\n");
14361558Srgrimes	}
14371558Srgrimes}
14381558Srgrimes
143923247Swollmanstatic void
144037671Scharnierusage()
14411558Srgrimes{
144242337Simp	fprintf(stderr, "%s\n%s\n%s\n",
144374029Sru"usage: ping [-QRadfnqrv] [-c count] [-i wait] [-l preload] [-m ttl]",
144474029Sru"            [-p pattern] "
144555505Sshin#ifdef IPSEC
144655505Sshin#ifdef IPSEC_POLICY_IPSEC
144755505Sshin"[-P policy] "
144855505Sshin#endif
144955505Sshin#endif
145055996Sbillf"[-s packetsize] [-S src_addr] [-t timeout]",
145155996Sbillf"            [host | [-L] [-I iface] [-T ttl] mcast-group]");
145223247Swollman	exit(EX_USAGE);
14531558Srgrimes}
1454