]> git.saurik.com Git - apple/network_cmds.git/blame - ping.tproj/ping.c
network_cmds-329.2.1.tar.gz
[apple/network_cmds.git] / ping.tproj / ping.c
CommitLineData
9c859447
A
1/*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
b7080c8e
A
28/*
29 * Copyright (c) 1989, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * This code is derived from software contributed to Berkeley by
33 * Mike Muuss.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
b7080c8e
A
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 */
59
2b484d24
A
60#if 0
61#ifndef lint
62static const char copyright[] =
63"@(#) Copyright (c) 1989, 1993\n\
64 The Regents of the University of California. All rights reserved.\n";
65#endif /* not lint */
66
67#ifndef lint
68static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93";
69#endif /* not lint */
70#endif
71#include <sys/cdefs.h>
72#ifndef __APPLE__
9c859447 73__FBSDID("$FreeBSD: src/sbin/ping/ping.c,v 1.112 2007/07/01 12:08:06 gnn Exp $");
2b484d24 74#endif
9c859447 75
b7080c8e
A
76/*
77 * P I N G . C
78 *
2b484d24 79 * Using the Internet Control Message Protocol (ICMP) "ECHO" facility,
b7080c8e
A
80 * measure round-trip-delays and packet loss across network paths.
81 *
82 * Author -
83 * Mike Muuss
84 * U. S. Army Ballistic Research Laboratory
85 * December, 1983
86 *
87 * Status -
88 * Public Domain. Distribution Unlimited.
89 * Bugs -
90 * More statistics could always be gathered.
91 * This program has to run SUID to ROOT to access the ICMP socket.
92 */
93
2b484d24 94#include <sys/param.h> /* NB: we rely on this for <sys/types.h> */
b7080c8e 95#include <sys/socket.h>
2b484d24 96#include <sys/sysctl.h>
b7080c8e 97#include <sys/time.h>
2b484d24 98#include <sys/uio.h>
b7080c8e 99
b7080c8e 100#include <netinet/in.h>
2b484d24 101#include <netinet/in_systm.h>
b7080c8e
A
102#include <netinet/ip.h>
103#include <netinet/ip_icmp.h>
104#include <netinet/ip_var.h>
2b484d24 105#include <arpa/inet.h>
9c859447 106#include <net/if.h>
2b484d24
A
107
108#ifdef IPSEC
109#include <netinet6/ipsec.h>
110#endif /*IPSEC*/
111
b7080c8e 112#include <ctype.h>
2b484d24 113#include <err.h>
b7080c8e 114#include <errno.h>
2b484d24
A
115#include <math.h>
116#include <netdb.h>
117#include <signal.h>
118#include <stdio.h>
119#include <stdlib.h>
b7080c8e 120#include <string.h>
2b484d24
A
121#include <sysexits.h>
122#include <unistd.h>
b7080c8e 123
2b484d24 124#define INADDR_LEN ((int)sizeof(in_addr_t))
9c859447 125#define TIMEVAL_LEN ((int)sizeof(struct tv32))
2b484d24
A
126#define MASK_LEN (ICMP_MASKLEN - ICMP_MINLEN)
127#define TS_LEN (ICMP_TSLEN - ICMP_MINLEN)
128#define DEFDATALEN 56 /* default data length */
129#define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */
130 /* runs out of buffer space */
131#define MAXIPLEN (sizeof(struct ip) + MAX_IPOPTLEN)
132#define MAXICMPLEN (ICMP_ADVLENMIN + MAX_IPOPTLEN)
9c859447 133#define MAXWAIT 10000 /* max ms to wait for response */
2b484d24
A
134#define MAXALARM (60 * 60) /* max seconds for alarm timeout */
135#define MAXTOS 255
b7080c8e
A
136
137#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
138#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
139#define SET(bit) (A(bit) |= B(bit))
140#define CLR(bit) (A(bit) &= (~B(bit)))
141#define TST(bit) (A(bit) & B(bit))
142
9c859447
A
143struct tv32 {
144 u_int32_t tv32_sec;
145 u_int32_t tv32_usec;
146};
147
b7080c8e
A
148/* various options */
149int options;
2b484d24
A
150#define F_FLOOD 0x0001
151#define F_INTERVAL 0x0002
152#define F_NUMERIC 0x0004
153#define F_PINGFILLED 0x0008
154#define F_QUIET 0x0010
155#define F_RROUTE 0x0020
156#define F_SO_DEBUG 0x0040
157#define F_SO_DONTROUTE 0x0080
158#define F_VERBOSE 0x0100
159#define F_QUIET2 0x0200
160#define F_NOLOOP 0x0400
161#define F_MTTL 0x0800
162#define F_MIF 0x1000
163#define F_AUDIBLE 0x2000
164#ifdef IPSEC
165#ifdef IPSEC_POLICY_IPSEC
166#define F_POLICY 0x4000
167#endif /*IPSEC_POLICY_IPSEC*/
168#endif /*IPSEC*/
169#define F_TTL 0x8000
170#define F_MISSED 0x10000
171#define F_ONCE 0x20000
172#define F_HDRINCL 0x40000
173#define F_MASK 0x80000
174#define F_TIME 0x100000
9c859447
A
175#define F_SWEEP 0x200000
176#define F_WAITTIME 0x400000
b7080c8e
A
177
178/*
179 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
180 * number of received sequence numbers we can keep track of. Change 128
181 * to 8192 for complete accuracy...
182 */
183#define MAX_DUP_CHK (8 * 128)
184int mx_dup_ck = MAX_DUP_CHK;
185char rcvd_tbl[MAX_DUP_CHK / 8];
186
2b484d24 187struct sockaddr_in whereto; /* who to ping */
b7080c8e 188int datalen = DEFDATALEN;
2b484d24 189int maxpayload;
b7080c8e 190int s; /* socket file descriptor */
2b484d24
A
191u_char outpackhdr[IP_MAXPACKET], *outpack;
192char BBELL = '\a'; /* characters written for MISSED and AUDIBLE */
b7080c8e
A
193char BSPACE = '\b'; /* characters written for flood */
194char DOT = '.';
195char *hostname;
2b484d24 196char *shostname;
b7080c8e 197int ident; /* process id to identify our packets */
2b484d24
A
198int uid; /* cached uid for micro-optimization */
199u_char icmp_type = ICMP_ECHO;
200u_char icmp_type_rsp = ICMP_ECHOREPLY;
201int phdr_len = 0;
202int send_len;
9c859447
A
203char *boundif;
204unsigned int ifscope;
205#if defined(IP_FORCE_OUT_IFP) && TARGET_OS_EMBEDDED
206char boundifname[IFNAMSIZ];
207#endif /* IP_FORCE_OUT_IFP */
b7080c8e
A
208
209/* counters */
2b484d24 210long nmissedmax; /* max value of ntransmitted - nreceived - 1 */
b7080c8e
A
211long npackets; /* max packets to transmit */
212long nreceived; /* # of packets we got back */
213long nrepeats; /* number of duplicates */
214long ntransmitted; /* sequence # for outbound packets = #sent */
9c859447
A
215long snpackets; /* max packets to transmit in one sweep */
216long snreceived; /* # of packets we got back in this sweep */
217long sntransmitted; /* # of packets we sent in this sweep */
218int sweepmax; /* max value of payload in sweep */
219int sweepmin = 0; /* start value of payload in sweep */
220int sweepincr = 1; /* payload increment in sweep */
2b484d24 221int interval = 1000; /* interval between packets, ms */
9c859447
A
222int waittime = MAXWAIT; /* timeout for each packet */
223long nrcvtimeout = 0; /* # of packets we got back after waittime */
b7080c8e
A
224
225/* timing */
226int timing; /* flag to do timing */
227double tmin = 999999999.0; /* minimum round trip time */
228double tmax = 0.0; /* maximum round trip time */
229double tsum = 0.0; /* sum of all times, for doing average */
2b484d24
A
230double tsumsq = 0.0; /* sum of all times squared, for std. dev. */
231
232volatile sig_atomic_t finish_up; /* nonzero if we've been told to finish up */
233volatile sig_atomic_t siginfo_p;
234
235static void fill(char *, char *);
236static u_short in_cksum(u_short *, int);
237static void check_status(void);
238static void finish(void) __dead2;
239static void pinger(void);
240static char *pr_addr(struct in_addr);
241static char *pr_ntime(n_time);
242static void pr_icmph(struct icmp *);
243static void pr_iph(struct ip *);
244static void pr_pack(char *, int, struct sockaddr_in *, struct timeval *);
245static void pr_retip(struct ip *);
246static void status(int);
247static void stopit(int);
248static void tvsub(struct timeval *, struct timeval *);
249static void usage(void) __dead2;
250
251int
b7080c8e
A
252main(argc, argv)
253 int argc;
2b484d24 254 char *const *argv;
b7080c8e 255{
2b484d24
A
256 struct sockaddr_in from, sock_in;
257 struct in_addr ifaddr;
258 struct timeval last, intvl;
259 struct iovec iov;
260 struct ip *ip;
261 struct msghdr msg;
262 struct sigaction si_sa;
263 size_t sz;
264 u_char *datap, packet[IP_MAXPACKET];
265 char *ep, *source, *target, *payload;
b7080c8e 266 struct hostent *hp;
2b484d24
A
267#ifdef IPSEC_POLICY_IPSEC
268 char *policy_in, *policy_out;
269#endif
b7080c8e 270 struct sockaddr_in *to;
2b484d24
A
271 double t;
272 u_long alarmtimeout, ultmp;
273 int almost_done, ch, df, hold, i, icmp_len, mib[4], preload, sockerrno,
274 tos, ttl;
275 char ctrl[CMSG_SPACE(sizeof(struct timeval))];
276 char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN];
b7080c8e 277#ifdef IP_OPTIONS
2b484d24
A
278 char rspace[MAX_IPOPTLEN]; /* record route space */
279#endif
280 unsigned char loop, mttl;
281
282 payload = source = NULL;
283#ifdef IPSEC_POLICY_IPSEC
284 policy_in = policy_out = NULL;
b7080c8e
A
285#endif
286
2b484d24
A
287 /*
288 * Do the stuff that we need root priv's for *first*, and
289 * then drop our setuid bit. Save error reporting for
290 * after arg parsing.
291 */
9c859447
A
292 if (getuid())
293 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
294 else
295 s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
2b484d24
A
296 sockerrno = errno;
297
298 setuid(getuid());
299 uid = getuid();
300
301 alarmtimeout = df = preload = tos = 0;
302
303 outpack = outpackhdr + sizeof(struct ip);
304 while ((ch = getopt(argc, argv,
9c859447 305 "Aab:c:DdfG:g:h:I:i:Ll:M:m:nop:QqRrS:s:T:t:vW:z:"
2b484d24
A
306#ifdef IPSEC
307#ifdef IPSEC_POLICY_IPSEC
308 "P:"
309#endif /*IPSEC_POLICY_IPSEC*/
310#endif /*IPSEC*/
9c859447
A
311#if defined(IP_FORCE_OUT_IFP) && TARGET_OS_EMBEDDED
312 "B:"
313#endif /* IP_FORCE_OUT_IFP */
2b484d24
A
314 )) != -1)
315 {
b7080c8e 316 switch(ch) {
2b484d24
A
317 case 'A':
318 options |= F_MISSED;
319 break;
320 case 'a':
321 options |= F_AUDIBLE;
322 break;
9c859447
A
323#if defined(IP_FORCE_OUT_IFP) && TARGET_OS_EMBEDDED
324 case 'B':
325 (void) snprintf(boundifname, sizeof (boundifname),
326 "%s", optarg);
327 break;
328#endif /* IP_FORCE_OUT_IFP */
329 case 'b':
330 boundif = optarg;
331 break;
b7080c8e 332 case 'c':
2b484d24
A
333 ultmp = strtoul(optarg, &ep, 0);
334 if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp)
335 errx(EX_USAGE,
336 "invalid count of packets to transmit: `%s'",
337 optarg);
338 npackets = ultmp;
339 break;
340 case 'D':
341 options |= F_HDRINCL;
342 df = 1;
b7080c8e
A
343 break;
344 case 'd':
345 options |= F_SO_DEBUG;
346 break;
347 case 'f':
2b484d24
A
348 if (uid) {
349 errno = EPERM;
350 err(EX_NOPERM, "-f flag");
b7080c8e
A
351 }
352 options |= F_FLOOD;
353 setbuf(stdout, (char *)NULL);
354 break;
9c859447
A
355 case 'G': /* Maximum packet size for ping sweep */
356 ultmp = strtoul(optarg, &ep, 0);
357 if (*ep || ep == optarg)
358 errx(EX_USAGE, "invalid packet size: `%s'",
359 optarg);
360#ifndef __APPLE__
361 if (uid != 0 && ultmp > DEFDATALEN) {
362 errno = EPERM;
363 err(EX_NOPERM,
364 "packet size too large: %lu > %u",
365 ultmp, DEFDATALEN);
366 }
367#endif /* __APPLE__ */
368 options |= F_SWEEP;
369 sweepmax = ultmp;
370 break;
371 case 'g': /* Minimum packet size for ping sweep */
372 ultmp = strtoul(optarg, &ep, 0);
373 if (*ep || ep == optarg)
374 errx(EX_USAGE, "invalid packet size: `%s'",
375 optarg);
376#ifndef __APPLE__
377 if (uid != 0 && ultmp > DEFDATALEN) {
378 errno = EPERM;
379 err(EX_NOPERM,
380 "packet size too large: %lu > %u",
381 ultmp, DEFDATALEN);
382 }
383#endif /* __APPLE__ */
384 options |= F_SWEEP;
385 sweepmin = ultmp;
386 break;
387 case 'h': /* Packet size increment for ping sweep */
388 ultmp = strtoul(optarg, &ep, 0);
389 if (*ep || ep == optarg || ultmp < 1)
390 errx(EX_USAGE, "invalid increment size: `%s'",
391 optarg);
392#ifndef __APPLE__
393 if (uid != 0 && ultmp > DEFDATALEN) {
394 errno = EPERM;
395 err(EX_NOPERM,
396 "packet size too large: %lu > %u",
397 ultmp, DEFDATALEN);
398 }
399#endif /* __APPLE__ */
400 options |= F_SWEEP;
401 sweepincr = ultmp;
402 break;
2b484d24
A
403 case 'I': /* multicast interface */
404 if (inet_aton(optarg, &ifaddr) == 0)
405 errx(EX_USAGE,
406 "invalid multicast interface: `%s'",
407 optarg);
408 options |= F_MIF;
409 break;
b7080c8e 410 case 'i': /* wait between sending packets */
2b484d24
A
411 t = strtod(optarg, &ep) * 1000.0;
412 if (*ep || ep == optarg || t > (double)INT_MAX)
413 errx(EX_USAGE, "invalid timing interval: `%s'",
414 optarg);
b7080c8e 415 options |= F_INTERVAL;
2b484d24
A
416 interval = (int)t;
417 if (uid && interval < 1000) {
418 errno = EPERM;
419 err(EX_NOPERM, "-i interval too short");
420 }
421 break;
422 case 'L':
423 options |= F_NOLOOP;
424 loop = 0;
b7080c8e
A
425 break;
426 case 'l':
2b484d24
A
427 ultmp = strtoul(optarg, &ep, 0);
428 if (*ep || ep == optarg || ultmp > INT_MAX)
429 errx(EX_USAGE,
430 "invalid preload value: `%s'", optarg);
431 if (uid) {
432 errno = EPERM;
433 err(EX_NOPERM, "-l flag");
b7080c8e 434 }
2b484d24
A
435 preload = ultmp;
436 break;
437 case 'M':
438 switch(optarg[0]) {
439 case 'M':
440 case 'm':
441 options |= F_MASK;
442 break;
443 case 'T':
444 case 't':
445 options |= F_TIME;
446 break;
447 default:
448 errx(EX_USAGE, "invalid message: `%c'", optarg[0]);
449 break;
b7080c8e
A
450 }
451 break;
2b484d24
A
452 case 'm': /* TTL */
453 ultmp = strtoul(optarg, &ep, 0);
454 if (*ep || ep == optarg || ultmp > MAXTTL)
455 errx(EX_USAGE, "invalid TTL: `%s'", optarg);
456 ttl = ultmp;
457 options |= F_TTL;
458 break;
b7080c8e
A
459 case 'n':
460 options |= F_NUMERIC;
461 break;
2b484d24
A
462 case 'o':
463 options |= F_ONCE;
464 break;
465#ifdef IPSEC
466#ifdef IPSEC_POLICY_IPSEC
467 case 'P':
468 options |= F_POLICY;
469 if (!strncmp("in", optarg, 2))
470 policy_in = strdup(optarg);
471 else if (!strncmp("out", optarg, 3))
472 policy_out = strdup(optarg);
473 else
474 errx(1, "invalid security policy");
475 break;
476#endif /*IPSEC_POLICY_IPSEC*/
477#endif /*IPSEC*/
b7080c8e
A
478 case 'p': /* fill buffer with user pattern */
479 options |= F_PINGFILLED;
2b484d24
A
480 payload = optarg;
481 break;
482 case 'Q':
483 options |= F_QUIET2;
484 break;
b7080c8e
A
485 case 'q':
486 options |= F_QUIET;
487 break;
488 case 'R':
489 options |= F_RROUTE;
490 break;
491 case 'r':
492 options |= F_SO_DONTROUTE;
493 break;
2b484d24
A
494 case 'S':
495 source = optarg;
496 break;
b7080c8e 497 case 's': /* size of packet to send */
2b484d24
A
498 ultmp = strtoul(optarg, &ep, 0);
499 if (*ep || ep == optarg)
500 errx(EX_USAGE, "invalid packet size: `%s'",
501 optarg);
502#ifndef __APPLE__
503 if (uid != 0 && ultmp > DEFDATALEN) {
504 errno = EPERM;
505 err(EX_NOPERM,
506 "packet size too large: %lu > %u",
507 ultmp, DEFDATALEN);
b7080c8e 508 }
9c859447 509#endif /* __APPLE__ */
2b484d24
A
510 datalen = ultmp;
511 break;
512 case 'T': /* multicast TTL */
513 ultmp = strtoul(optarg, &ep, 0);
514 if (*ep || ep == optarg || ultmp > MAXTTL)
515 errx(EX_USAGE, "invalid multicast TTL: `%s'",
516 optarg);
517 mttl = ultmp;
518 options |= F_MTTL;
519 break;
520 case 't':
521 alarmtimeout = strtoul(optarg, &ep, 0);
522 if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX))
523 errx(EX_USAGE, "invalid timeout: `%s'",
524 optarg);
525 if (alarmtimeout > MAXALARM)
526 errx(EX_USAGE, "invalid timeout: `%s' > %d",
527 optarg, MAXALARM);
9c859447 528 alarm((unsigned int)alarmtimeout);
b7080c8e
A
529 break;
530 case 'v':
531 options |= F_VERBOSE;
532 break;
9c859447
A
533 case 'W': /* wait ms for answer */
534 t = strtod(optarg, &ep);
535 if (*ep || ep == optarg || t > (double)INT_MAX)
536 errx(EX_USAGE, "invalid timing interval: `%s'",
537 optarg);
538 options |= F_WAITTIME;
539 waittime = (int)t;
540 break;
2b484d24
A
541 case 'z':
542 options |= F_HDRINCL;
543 ultmp = strtoul(optarg, &ep, 0);
544 if (*ep || ep == optarg || ultmp > MAXTOS)
545 errx(EX_USAGE, "invalid TOS: `%s'", optarg);
546 tos = ultmp;
547 break;
b7080c8e
A
548 default:
549 usage();
550 }
2b484d24 551 }
b7080c8e 552
9c859447
A
553 if (boundif != NULL && (ifscope = if_nametoindex(boundif)) == 0)
554 errx(1, "bad interface name");
555
2b484d24 556 if (argc - optind != 1)
b7080c8e 557 usage();
2b484d24
A
558 target = argv[optind];
559
560 switch (options & (F_MASK|F_TIME)) {
561 case 0: break;
562 case F_MASK:
563 icmp_type = ICMP_MASKREQ;
564 icmp_type_rsp = ICMP_MASKREPLY;
565 phdr_len = MASK_LEN;
566 if (!(options & F_QUIET))
567 (void)printf("ICMP_MASKREQ\n");
568 break;
569 case F_TIME:
570 icmp_type = ICMP_TSTAMP;
571 icmp_type_rsp = ICMP_TSTAMPREPLY;
572 phdr_len = TS_LEN;
573 if (!(options & F_QUIET))
574 (void)printf("ICMP_TSTAMP\n");
575 break;
576 default:
577 errx(EX_USAGE, "ICMP_TSTAMP and ICMP_MASKREQ are exclusive.");
578 break;
579 }
580 icmp_len = sizeof(struct ip) + ICMP_MINLEN + phdr_len;
581 if (options & F_RROUTE)
582 icmp_len += MAX_IPOPTLEN;
583 maxpayload = IP_MAXPACKET - icmp_len;
584 if (datalen > maxpayload)
585 errx(EX_USAGE, "packet size too large: %d > %d", datalen,
586 maxpayload);
587 send_len = icmp_len + datalen;
588 datap = &outpack[ICMP_MINLEN + phdr_len + TIMEVAL_LEN];
589 if (options & F_PINGFILLED) {
590 fill((char *)datap, payload);
591 }
592 if (source) {
593 bzero((char *)&sock_in, sizeof(sock_in));
594 sock_in.sin_family = AF_INET;
595 if (inet_aton(source, &sock_in.sin_addr) != 0) {
596 shostname = source;
597 } else {
598 hp = gethostbyname2(source, AF_INET);
599 if (!hp)
600 errx(EX_NOHOST, "cannot resolve %s: %s",
601 source, hstrerror(h_errno));
602
603 sock_in.sin_len = sizeof sock_in;
604 if ((unsigned)hp->h_length > sizeof(sock_in.sin_addr) ||
605 hp->h_length < 0)
606 errx(1, "gethostbyname2: illegal address");
607 memcpy(&sock_in.sin_addr, hp->h_addr_list[0],
608 sizeof(sock_in.sin_addr));
609 (void)strncpy(snamebuf, hp->h_name,
610 sizeof(snamebuf) - 1);
611 snamebuf[sizeof(snamebuf) - 1] = '\0';
612 shostname = snamebuf;
613 }
614 if (bind(s, (struct sockaddr *)&sock_in, sizeof sock_in) == -1)
615 err(1, "bind");
616 }
b7080c8e 617
2b484d24
A
618 bzero(&whereto, sizeof(whereto));
619 to = &whereto;
b7080c8e 620 to->sin_family = AF_INET;
2b484d24
A
621 to->sin_len = sizeof *to;
622 if (inet_aton(target, &to->sin_addr) != 0) {
b7080c8e 623 hostname = target;
2b484d24
A
624 } else {
625 hp = gethostbyname2(target, AF_INET);
626 if (!hp)
627 errx(EX_NOHOST, "cannot resolve %s: %s",
628 target, hstrerror(h_errno));
629
630 if ((unsigned)hp->h_length > sizeof(to->sin_addr))
631 errx(1, "gethostbyname2 returned an illegal address");
632 memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr);
b7080c8e 633 (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1);
2b484d24 634 hnamebuf[sizeof(hnamebuf) - 1] = '\0';
b7080c8e
A
635 hostname = hnamebuf;
636 }
637
2b484d24
A
638 if (options & F_FLOOD && options & F_INTERVAL)
639 errx(EX_USAGE, "-f and -i: incompatible options");
b7080c8e 640
2b484d24
A
641 if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
642 errx(EX_USAGE,
643 "-f flag cannot be used with multicast destination");
644 if (options & (F_MIF | F_NOLOOP | F_MTTL)
645 && !IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
646 errx(EX_USAGE,
647 "-I, -L, -T flags cannot be used with unicast destination");
648
649 if (datalen >= TIMEVAL_LEN) /* can we time transfer */
b7080c8e 650 timing = 1;
2b484d24 651
b7080c8e 652 if (!(options & F_PINGFILLED))
2b484d24 653 for (i = TIMEVAL_LEN; i < datalen; ++i)
b7080c8e
A
654 *datap++ = i;
655
656 ident = getpid() & 0xFFFF;
657
2b484d24
A
658 if (s < 0) {
659 errno = sockerrno;
660 err(EX_OSERR, "socket");
b7080c8e
A
661 }
662 hold = 1;
9c859447
A
663 if (ifscope != 0) {
664 if (setsockopt(s, IPPROTO_IP, IP_BOUND_IF,
665 (char *)&ifscope, sizeof (ifscope)) != 0)
666 err(EX_OSERR, "setsockopt(IP_BOUND_IF)");
667 }
668#if defined(IP_FORCE_OUT_IFP) && TARGET_OS_EMBEDDED
669 else if (boundifname[0] != 0) {
670 if (setsockopt(s, IPPROTO_IP, IP_FORCE_OUT_IFP, boundifname,
671 sizeof (boundifname)) != 0)
672 err(EX_OSERR, "setsockopt(IP_FORCE_OUT_IFP)");
673 }
674#endif /* IP_FORCE_OUT_IFP */
b7080c8e
A
675 if (options & F_SO_DEBUG)
676 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold,
677 sizeof(hold));
678 if (options & F_SO_DONTROUTE)
679 (void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold,
680 sizeof(hold));
2b484d24
A
681#ifdef IPSEC
682#ifdef IPSEC_POLICY_IPSEC
683 if (options & F_POLICY) {
684 char *buf;
685 if (policy_in != NULL) {
686 buf = ipsec_set_policy(policy_in, strlen(policy_in));
687 if (buf == NULL)
688 errx(EX_CONFIG, "%s", ipsec_strerror());
689 if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY,
690 buf, ipsec_get_policylen(buf)) < 0)
691 err(EX_CONFIG,
692 "ipsec policy cannot be configured");
693 free(buf);
694 }
b7080c8e 695
2b484d24
A
696 if (policy_out != NULL) {
697 buf = ipsec_set_policy(policy_out, strlen(policy_out));
698 if (buf == NULL)
699 errx(EX_CONFIG, "%s", ipsec_strerror());
700 if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY,
701 buf, ipsec_get_policylen(buf)) < 0)
702 err(EX_CONFIG,
703 "ipsec policy cannot be configured");
704 free(buf);
705 }
706 }
707#endif /*IPSEC_POLICY_IPSEC*/
708#endif /*IPSEC*/
709
710 if (options & F_HDRINCL) {
711 ip = (struct ip*)outpackhdr;
712 if (!(options & (F_TTL | F_MTTL))) {
713 mib[0] = CTL_NET;
714 mib[1] = PF_INET;
715 mib[2] = IPPROTO_IP;
716 mib[3] = IPCTL_DEFTTL;
717 sz = sizeof(ttl);
718 if (sysctl(mib, 4, &ttl, &sz, NULL, 0) == -1)
719 err(1, "sysctl(net.inet.ip.ttl)");
720 }
721 setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold));
722 ip->ip_v = IPVERSION;
723 ip->ip_hl = sizeof(struct ip) >> 2;
724 ip->ip_tos = tos;
725 ip->ip_id = 0;
726 ip->ip_off = df ? IP_DF : 0;
727 ip->ip_ttl = ttl;
728 ip->ip_p = IPPROTO_ICMP;
729 ip->ip_src.s_addr = source ? sock_in.sin_addr.s_addr : INADDR_ANY;
730 ip->ip_dst = to->sin_addr;
731 }
b7080c8e
A
732 /* record route option */
733 if (options & F_RROUTE) {
734#ifdef IP_OPTIONS
2b484d24 735 bzero(rspace, sizeof(rspace));
b7080c8e 736 rspace[IPOPT_OPTVAL] = IPOPT_RR;
2b484d24 737 rspace[IPOPT_OLEN] = sizeof(rspace) - 1;
b7080c8e 738 rspace[IPOPT_OFFSET] = IPOPT_MINOFF;
2b484d24 739 rspace[sizeof(rspace) - 1] = IPOPT_EOL;
b7080c8e 740 if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace,
2b484d24
A
741 sizeof(rspace)) < 0)
742 err(EX_OSERR, "setsockopt IP_OPTIONS");
b7080c8e 743#else
2b484d24
A
744 errx(EX_UNAVAILABLE,
745 "record route not available in this implementation");
b7080c8e
A
746#endif /* IP_OPTIONS */
747 }
748
2b484d24
A
749 if (options & F_TTL) {
750 if (setsockopt(s, IPPROTO_IP, IP_TTL, &ttl,
751 sizeof(ttl)) < 0) {
752 err(EX_OSERR, "setsockopt IP_TTL");
753 }
754 }
755 if (options & F_NOLOOP) {
756 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop,
757 sizeof(loop)) < 0) {
758 err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP");
759 }
760 }
761 if (options & F_MTTL) {
762 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &mttl,
763 sizeof(mttl)) < 0) {
764 err(EX_OSERR, "setsockopt IP_MULTICAST_TTL");
765 }
766 }
767 if (options & F_MIF) {
768 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr,
769 sizeof(ifaddr)) < 0) {
770 err(EX_OSERR, "setsockopt IP_MULTICAST_IF");
771 }
772 }
773#ifdef SO_TIMESTAMP
774 { int on = 1;
775 if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) < 0)
776 err(EX_OSERR, "setsockopt SO_TIMESTAMP");
777 }
778#endif
9c859447
A
779 if (sweepmax) {
780 if (sweepmin >= sweepmax)
781 errx(EX_USAGE, "Maximum packet size must be greater than the minimum packet size");
782
783 if (datalen != DEFDATALEN)
784 errx(EX_USAGE, "Packet size and ping sweep are mutually exclusive");
785
786 if (npackets > 0) {
787 snpackets = npackets;
788 npackets = 0;
789 } else
790 snpackets = 1;
791 datalen = sweepmin;
792 send_len = icmp_len + sweepmin;
793 }
794 if (options & F_SWEEP && !sweepmax)
795 errx(EX_USAGE, "Maximum sweep size must be specified");
2b484d24 796
b7080c8e
A
797 /*
798 * When pinging the broadcast address, you can get a lot of answers.
799 * Doing something so evil is useful if you are trying to stress the
800 * ethernet, or just want to fill the arp cache to get some stuff for
2b484d24
A
801 * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast
802 * or multicast pings if they wish.
b7080c8e 803 */
b7080c8e 804
2b484d24
A
805 /*
806 * XXX receive buffer needs undetermined space for mbuf overhead
807 * as well.
808 */
809 hold = IP_MAXPACKET + 128;
810 (void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
811 sizeof(hold));
812 if (uid == 0)
813 (void)setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&hold,
814 sizeof(hold));
815
816 if (to->sin_family == AF_INET) {
817 (void)printf("PING %s (%s)", hostname,
818 inet_ntoa(to->sin_addr));
819 if (source)
820 (void)printf(" from %s", shostname);
9c859447
A
821 if (sweepmax)
822 (void)printf(": (%d ... %d) data bytes\n",
823 sweepmin, sweepmax);
824 else
825 (void)printf(": %d data bytes\n", datalen);
826
827 } else {
828 if (sweepmax)
829 (void)printf("PING %s: (%d ... %d) data bytes\n",
830 hostname, sweepmin, sweepmax);
831 else
832 (void)printf("PING %s: %d data bytes\n", hostname, datalen);
833 }
b7080c8e 834
2b484d24
A
835 /*
836 * Use sigaction() instead of signal() to get unambiguous semantics,
837 * in particular with SA_RESTART not set.
838 */
839
840 sigemptyset(&si_sa.sa_mask);
841 si_sa.sa_flags = 0;
b7080c8e 842
2b484d24
A
843 si_sa.sa_handler = stopit;
844 if (sigaction(SIGINT, &si_sa, 0) == -1) {
845 err(EX_OSERR, "sigaction SIGINT");
846 }
b7080c8e 847
2b484d24
A
848 si_sa.sa_handler = status;
849 if (sigaction(SIGINFO, &si_sa, 0) == -1) {
850 err(EX_OSERR, "sigaction");
851 }
b7080c8e 852
2b484d24
A
853 if (alarmtimeout > 0) {
854 si_sa.sa_handler = stopit;
855 if (sigaction(SIGALRM, &si_sa, 0) == -1)
856 err(EX_OSERR, "sigaction SIGALRM");
857 }
858
859 bzero(&msg, sizeof(msg));
860 msg.msg_name = (caddr_t)&from;
861 msg.msg_iov = &iov;
862 msg.msg_iovlen = 1;
863#ifdef SO_TIMESTAMP
864 msg.msg_control = (caddr_t)ctrl;
865#endif
866 iov.iov_base = packet;
867 iov.iov_len = IP_MAXPACKET;
b7080c8e 868
2b484d24
A
869 if (preload == 0)
870 pinger(); /* send the first ping */
871 else {
872 if (npackets != 0 && preload > npackets)
873 preload = npackets;
874 while (preload--) /* fire off them quickies */
b7080c8e 875 pinger();
2b484d24
A
876 }
877 (void)gettimeofday(&last, NULL);
878
879 if (options & F_FLOOD) {
880 intvl.tv_sec = 0;
881 intvl.tv_usec = 10000;
882 } else {
883 intvl.tv_sec = interval / 1000;
884 intvl.tv_usec = interval % 1000 * 1000;
885 }
886
887 almost_done = 0;
888 while (!finish_up) {
889 struct timeval now, timeout;
890 fd_set rfds;
891 int cc, n;
892
893 check_status();
894 if ((unsigned)s >= FD_SETSIZE)
895 errx(EX_OSERR, "descriptor too large");
896 FD_ZERO(&rfds);
897 FD_SET(s, &rfds);
898 (void)gettimeofday(&now, NULL);
899 timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec;
900 timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec;
901 while (timeout.tv_usec < 0) {
902 timeout.tv_usec += 1000000;
903 timeout.tv_sec--;
b7080c8e 904 }
2b484d24
A
905 while (timeout.tv_usec >= 1000000) {
906 timeout.tv_usec -= 1000000;
907 timeout.tv_sec++;
908 }
909 if (timeout.tv_sec < 0)
910 timeout.tv_sec = timeout.tv_usec = 0;
911 n = select(s + 1, &rfds, NULL, NULL, &timeout);
912 if (n < 0)
913 continue; /* Must be EINTR. */
914 if (n == 1) {
915 struct timeval *tv = NULL;
916#ifdef SO_TIMESTAMP
917 struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl;
918
919 msg.msg_controllen = sizeof(ctrl);
920#endif
921 msg.msg_namelen = sizeof(from);
922 if ((cc = recvmsg(s, &msg, 0)) < 0) {
923 if (errno == EINTR)
924 continue;
925 warn("recvmsg");
b7080c8e 926 continue;
2b484d24
A
927 }
928#ifdef SO_TIMESTAMP
929 if (cmsg->cmsg_level == SOL_SOCKET &&
930 cmsg->cmsg_type == SCM_TIMESTAMP &&
931 cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) {
932 /* Copy to avoid alignment problems: */
933 memcpy(&now, CMSG_DATA(cmsg), sizeof(now));
934 tv = &now;
935 }
936#endif
937 if (tv == NULL) {
938 (void)gettimeofday(&now, NULL);
939 tv = &now;
940 }
941 pr_pack((char *)packet, cc, &from, tv);
942 if ((options & F_ONCE && nreceived) ||
943 (npackets && nreceived >= npackets))
944 break;
945 }
946 if (n == 0 || options & F_FLOOD) {
9c859447
A
947 if (sweepmax && sntransmitted == snpackets) {
948 for (i = 0; i < sweepincr ; ++i)
949 *datap++ = i;
950 datalen += sweepincr;
951 if (datalen > sweepmax)
952 break;
953 send_len = icmp_len + datalen;
954 sntransmitted = 0;
955 }
2b484d24
A
956 if (!npackets || ntransmitted < npackets)
957 pinger();
958 else {
959 if (almost_done)
960 break;
961 almost_done = 1;
962 intvl.tv_usec = 0;
963 if (nreceived) {
964 intvl.tv_sec = 2 * tmax / 1000;
965 if (!intvl.tv_sec)
966 intvl.tv_sec = 1;
9c859447
A
967 } else {
968 intvl.tv_sec = waittime / 1000;
969 intvl.tv_usec = waittime % 1000 * 1000;
970 }
2b484d24
A
971 }
972 (void)gettimeofday(&last, NULL);
973 if (ntransmitted - nreceived - 1 > nmissedmax) {
974 nmissedmax = ntransmitted - nreceived - 1;
975 if (options & F_MISSED)
976 (void)write(STDOUT_FILENO, &BBELL, 1);
9c859447
A
977 if (!(options & F_QUIET))
978 printf("Request timeout for icmp_seq %ld\n", ntransmitted - 2);
2b484d24 979 }
b7080c8e 980 }
b7080c8e
A
981 }
982 finish();
983 /* NOTREACHED */
2b484d24 984 exit(0); /* Make the compiler happy */
b7080c8e
A
985}
986
987/*
2b484d24
A
988 * stopit --
989 * Set the global bit that causes the main loop to quit.
990 * Do NOT call finish() from here, since finish() does far too much
991 * to be called from a signal handler.
b7080c8e
A
992 */
993void
2b484d24
A
994stopit(sig)
995 int sig __unused;
b7080c8e 996{
b7080c8e 997
2b484d24
A
998 /*
999 * When doing reverse DNS lookups, the finish_up flag might not
1000 * be noticed for a while. Just exit if we get a second SIGINT.
1001 */
1002 if (!(options & F_NUMERIC) && finish_up)
1003 _exit(nreceived ? 0 : 2);
1004 finish_up = 1;
b7080c8e
A
1005}
1006
1007/*
1008 * pinger --
1009 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1010 * will be added on by the kernel. The ID field is our UNIX process ID,
2b484d24
A
1011 * and the sequence number is an ascending integer. The first TIMEVAL_LEN
1012 * bytes of the data portion are used to hold a UNIX "timeval" struct in
1013 * host byte-order, to compute the round-trip time.
b7080c8e 1014 */
2b484d24
A
1015static void
1016pinger(void)
b7080c8e 1017{
2b484d24 1018 struct timeval now;
9c859447 1019 struct tv32 tv32;
2b484d24
A
1020 struct ip *ip;
1021 struct icmp *icp;
1022 int cc, i;
1023 u_char *packet;
b7080c8e 1024
2b484d24 1025 packet = outpack;
b7080c8e 1026 icp = (struct icmp *)outpack;
2b484d24 1027 icp->icmp_type = icmp_type;
b7080c8e
A
1028 icp->icmp_code = 0;
1029 icp->icmp_cksum = 0;
2b484d24 1030 icp->icmp_seq = htons(ntransmitted);
b7080c8e
A
1031 icp->icmp_id = ident; /* ID */
1032
2b484d24 1033 CLR(ntransmitted % mx_dup_ck);
b7080c8e 1034
2b484d24
A
1035 if ((options & F_TIME) || timing) {
1036 (void)gettimeofday(&now, NULL);
b7080c8e 1037
9c859447
A
1038 tv32.tv32_sec = htonl(now.tv_sec);
1039 tv32.tv32_usec = htonl(now.tv_usec);
2b484d24
A
1040 if (options & F_TIME)
1041 icp->icmp_otime = htonl((now.tv_sec % (24*60*60))
1042 * 1000 + now.tv_usec / 1000);
1043 if (timing)
9c859447 1044 bcopy((void *)&tv32,
2b484d24 1045 (void *)&outpack[ICMP_MINLEN + phdr_len],
9c859447 1046 sizeof(tv32));
2b484d24
A
1047 }
1048
1049 cc = ICMP_MINLEN + phdr_len + datalen;
b7080c8e
A
1050
1051 /* compute ICMP checksum here */
1052 icp->icmp_cksum = in_cksum((u_short *)icp, cc);
1053
2b484d24
A
1054 if (options & F_HDRINCL) {
1055 cc += sizeof(struct ip);
1056 ip = (struct ip *)outpackhdr;
1057 ip->ip_len = cc;
1058 ip->ip_sum = in_cksum((u_short *)outpackhdr, cc);
1059 packet = outpackhdr;
1060 }
1061 i = sendto(s, (char *)packet, cc, 0, (struct sockaddr *)&whereto,
1062 sizeof(whereto));
b7080c8e
A
1063
1064 if (i < 0 || i != cc) {
2b484d24
A
1065 if (i < 0) {
1066 if (options & F_FLOOD && errno == ENOBUFS) {
1067 usleep(FLOOD_BACKOFF);
1068 return;
1069 }
1070 warn("sendto");
1071 } else {
1072 warn("%s: partial write: %d of %d bytes",
1073 hostname, i, cc);
1074 }
b7080c8e 1075 }
2b484d24 1076 ntransmitted++;
9c859447 1077 sntransmitted++;
b7080c8e
A
1078 if (!(options & F_QUIET) && options & F_FLOOD)
1079 (void)write(STDOUT_FILENO, &DOT, 1);
1080}
1081
1082/*
1083 * pr_pack --
1084 * Print out the packet, if it came from us. This logic is necessary
1085 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1086 * which arrive ('tis only fair). This permits multiple copies of this
1087 * program to be run without having intermingled output (or statistics!).
1088 */
2b484d24
A
1089static void
1090pr_pack(buf, cc, from, tv)
b7080c8e
A
1091 char *buf;
1092 int cc;
1093 struct sockaddr_in *from;
2b484d24 1094 struct timeval *tv;
b7080c8e 1095{
2b484d24
A
1096 struct in_addr ina;
1097 u_char *cp, *dp;
1098 struct icmp *icp;
b7080c8e 1099 struct ip *ip;
2b484d24 1100 const void *tp;
b7080c8e 1101 double triptime;
2b484d24
A
1102 int dupflag, hlen, i, j, recv_len, seq;
1103 static int old_rrlen;
1104 static char old_rr[MAX_IPOPTLEN];
b7080c8e
A
1105
1106 /* Check the IP header */
1107 ip = (struct ip *)buf;
1108 hlen = ip->ip_hl << 2;
2b484d24 1109 recv_len = cc;
b7080c8e
A
1110 if (cc < hlen + ICMP_MINLEN) {
1111 if (options & F_VERBOSE)
2b484d24
A
1112 warn("packet too short (%d bytes) from %s", cc,
1113 inet_ntoa(from->sin_addr));
b7080c8e
A
1114 return;
1115 }
1116
1117 /* Now the ICMP part */
1118 cc -= hlen;
1119 icp = (struct icmp *)(buf + hlen);
2b484d24 1120 if (icp->icmp_type == icmp_type_rsp) {
b7080c8e
A
1121 if (icp->icmp_id != ident)
1122 return; /* 'Twas not our ECHO */
1123 ++nreceived;
2b484d24 1124 triptime = 0.0;
b7080c8e 1125 if (timing) {
2b484d24 1126 struct timeval tv1;
9c859447 1127 struct tv32 tv32;
b7080c8e 1128#ifndef icmp_data
2b484d24 1129 tp = &icp->icmp_ip;
b7080c8e 1130#else
2b484d24 1131 tp = icp->icmp_data;
b7080c8e 1132#endif
2b484d24
A
1133 tp = (const char *)tp + phdr_len;
1134
1135 if (cc - ICMP_MINLEN - phdr_len >= sizeof(tv1)) {
1136 /* Copy to avoid alignment problems: */
9c859447
A
1137 memcpy(&tv32, tp, sizeof(tv32));
1138 tv1.tv_sec = ntohl(tv32.tv32_sec);
1139 tv1.tv_usec = ntohl(tv32.tv32_usec);
2b484d24
A
1140 tvsub(tv, &tv1);
1141 triptime = ((double)tv->tv_sec) * 1000.0 +
1142 ((double)tv->tv_usec) / 1000.0;
1143 tsum += triptime;
1144 tsumsq += triptime * triptime;
1145 if (triptime < tmin)
1146 tmin = triptime;
1147 if (triptime > tmax)
1148 tmax = triptime;
1149 } else
1150 timing = 0;
b7080c8e
A
1151 }
1152
2b484d24
A
1153 seq = ntohs(icp->icmp_seq);
1154
1155 if (TST(seq % mx_dup_ck)) {
b7080c8e
A
1156 ++nrepeats;
1157 --nreceived;
1158 dupflag = 1;
1159 } else {
2b484d24 1160 SET(seq % mx_dup_ck);
b7080c8e
A
1161 dupflag = 0;
1162 }
1163
1164 if (options & F_QUIET)
1165 return;
9c859447
A
1166
1167 if (options & F_WAITTIME && triptime > waittime) {
1168 ++nrcvtimeout;
1169 return;
1170 }
b7080c8e
A
1171
1172 if (options & F_FLOOD)
1173 (void)write(STDOUT_FILENO, &BSPACE, 1);
1174 else {
1175 (void)printf("%d bytes from %s: icmp_seq=%u", cc,
1176 inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr),
2b484d24 1177 seq);
b7080c8e
A
1178 (void)printf(" ttl=%d", ip->ip_ttl);
1179 if (timing)
2b484d24 1180 (void)printf(" time=%.3f ms", triptime);
9c859447
A
1181 if (dupflag) {
1182 if (!IN_MULTICAST(ntohl(whereto.sin_addr.s_addr)))
1183 (void)printf(" (DUP!)");
1184 }
2b484d24
A
1185 if (options & F_AUDIBLE)
1186 (void)write(STDOUT_FILENO, &BBELL, 1);
1187 if (options & F_MASK) {
1188 /* Just prentend this cast isn't ugly */
1189 (void)printf(" mask=%s",
1190 pr_addr(*(struct in_addr *)&(icp->icmp_mask)));
1191 }
1192 if (options & F_TIME) {
1193 (void)printf(" tso=%s", pr_ntime(icp->icmp_otime));
1194 (void)printf(" tsr=%s", pr_ntime(icp->icmp_rtime));
1195 (void)printf(" tst=%s", pr_ntime(icp->icmp_ttime));
1196 }
1197 if (recv_len != send_len) {
1198 (void)printf(
1199 "\nwrong total length %d instead of %d",
1200 recv_len, send_len);
1201 }
b7080c8e 1202 /* check the data */
2b484d24
A
1203 cp = (u_char*)&icp->icmp_data[phdr_len];
1204 dp = &outpack[ICMP_MINLEN + phdr_len];
1205 cc -= ICMP_MINLEN + phdr_len;
1206 i = 0;
1207 if (timing) { /* don't check variable timestamp */
1208 cp += TIMEVAL_LEN;
1209 dp += TIMEVAL_LEN;
1210 cc -= TIMEVAL_LEN;
1211 i += TIMEVAL_LEN;
1212 }
1213 for (; i < datalen && cc > 0; ++i, ++cp, ++dp, --cc) {
b7080c8e
A
1214 if (*cp != *dp) {
1215 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x",
1216 i, *dp, *cp);
2b484d24 1217 (void)printf("\ncp:");
b7080c8e 1218 cp = (u_char*)&icp->icmp_data[0];
2b484d24
A
1219 for (i = 0; i < datalen; ++i, ++cp) {
1220 if ((i % 16) == 8)
1221 (void)printf("\n\t");
1222 (void)printf("%2x ", *cp);
1223 }
1224 (void)printf("\ndp:");
1225 cp = &outpack[ICMP_MINLEN];
1226 for (i = 0; i < datalen; ++i, ++cp) {
1227 if ((i % 16) == 8)
b7080c8e 1228 (void)printf("\n\t");
2b484d24 1229 (void)printf("%2x ", *cp);
b7080c8e
A
1230 }
1231 break;
1232 }
1233 }
1234 }
1235 } else {
2b484d24
A
1236 /*
1237 * We've got something other than an ECHOREPLY.
1238 * See if it's a reply to something that we sent.
1239 * We can compare IP destination, protocol,
1240 * and ICMP type and ID.
1241 *
1242 * Only print all the error messages if we are running
1243 * as root to avoid leaking information not normally
1244 * available to those not running as root.
1245 */
1246#ifndef icmp_data
1247 struct ip *oip = &icp->icmp_ip;
1248#else
1249 struct ip *oip = (struct ip *)icp->icmp_data;
1250#endif
1251 struct icmp *oicmp = (struct icmp *)(oip + 1);
1252
1253 if (((options & F_VERBOSE) && uid == 0) ||
1254 (!(options & F_QUIET2) &&
1255 (oip->ip_dst.s_addr == whereto.sin_addr.s_addr) &&
1256 (oip->ip_p == IPPROTO_ICMP) &&
1257 (oicmp->icmp_type == ICMP_ECHO) &&
1258 (oicmp->icmp_id == ident))) {
1259 (void)printf("%d bytes from %s: ", cc,
1260 pr_addr(from->sin_addr));
1261 pr_icmph(icp);
1262 } else
1263 return;
b7080c8e
A
1264 }
1265
1266 /* Display any IP options */
1267 cp = (u_char *)buf + sizeof(struct ip);
1268
1269 for (; hlen > (int)sizeof(struct ip); --hlen, ++cp)
1270 switch (*cp) {
1271 case IPOPT_EOL:
1272 hlen = 0;
1273 break;
1274 case IPOPT_LSRR:
2b484d24
A
1275 case IPOPT_SSRR:
1276 (void)printf(*cp == IPOPT_LSRR ?
1277 "\nLSRR: " : "\nSSRR: ");
1278 j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1;
b7080c8e 1279 hlen -= 2;
2b484d24
A
1280 cp += 2;
1281 if (j >= INADDR_LEN &&
1282 j <= hlen - (int)sizeof(struct ip)) {
b7080c8e 1283 for (;;) {
2b484d24
A
1284 bcopy(++cp, &ina.s_addr, INADDR_LEN);
1285 if (ina.s_addr == 0)
b7080c8e 1286 (void)printf("\t0.0.0.0");
2b484d24
A
1287 else
1288 (void)printf("\t%s",
1289 pr_addr(ina));
1290 hlen -= INADDR_LEN;
1291 cp += INADDR_LEN - 1;
1292 j -= INADDR_LEN;
1293 if (j < INADDR_LEN)
1294 break;
1295 (void)putchar('\n');
1296 }
1297 } else
1298 (void)printf("\t(truncated route)\n");
b7080c8e
A
1299 break;
1300 case IPOPT_RR:
2b484d24
A
1301 j = cp[IPOPT_OLEN]; /* get length */
1302 i = cp[IPOPT_OFFSET]; /* and pointer */
b7080c8e 1303 hlen -= 2;
2b484d24 1304 cp += 2;
b7080c8e
A
1305 if (i > j)
1306 i = j;
2b484d24
A
1307 i = i - IPOPT_MINOFF + 1;
1308 if (i < 0 || i > (hlen - (int)sizeof(struct ip))) {
1309 old_rrlen = 0;
b7080c8e 1310 continue;
2b484d24 1311 }
b7080c8e 1312 if (i == old_rrlen
2b484d24 1313 && !bcmp((char *)cp, old_rr, i)
b7080c8e
A
1314 && !(options & F_FLOOD)) {
1315 (void)printf("\t(same route)");
b7080c8e
A
1316 hlen -= i;
1317 cp += i;
1318 break;
1319 }
2b484d24
A
1320 old_rrlen = i;
1321 bcopy((char *)cp, old_rr, i);
b7080c8e 1322 (void)printf("\nRR: ");
2b484d24
A
1323 if (i >= INADDR_LEN &&
1324 i <= hlen - (int)sizeof(struct ip)) {
1325 for (;;) {
1326 bcopy(++cp, &ina.s_addr, INADDR_LEN);
1327 if (ina.s_addr == 0)
1328 (void)printf("\t0.0.0.0");
1329 else
1330 (void)printf("\t%s",
1331 pr_addr(ina));
1332 hlen -= INADDR_LEN;
1333 cp += INADDR_LEN - 1;
1334 i -= INADDR_LEN;
1335 if (i < INADDR_LEN)
1336 break;
1337 (void)putchar('\n');
b7080c8e 1338 }
2b484d24
A
1339 } else
1340 (void)printf("\t(truncated route)");
b7080c8e
A
1341 break;
1342 case IPOPT_NOP:
1343 (void)printf("\nNOP");
1344 break;
1345 default:
1346 (void)printf("\nunknown option %x", *cp);
1347 break;
1348 }
1349 if (!(options & F_FLOOD)) {
1350 (void)putchar('\n');
1351 (void)fflush(stdout);
1352 }
1353}
1354
1355/*
1356 * in_cksum --
1357 * Checksum routine for Internet Protocol family headers (C Version)
1358 */
2b484d24 1359u_short
b7080c8e
A
1360in_cksum(addr, len)
1361 u_short *addr;
1362 int len;
1363{
2b484d24
A
1364 int nleft, sum;
1365 u_short *w;
1366 union {
1367 u_short us;
1368 u_char uc[2];
1369 } last;
1370 u_short answer;
1371
1372 nleft = len;
1373 sum = 0;
1374 w = addr;
b7080c8e
A
1375
1376 /*
1377 * Our algorithm is simple, using a 32 bit accumulator (sum), we add
1378 * sequential 16 bit words to it, and at the end, fold back all the
1379 * carry bits from the top 16 bits into the lower 16 bits.
1380 */
1381 while (nleft > 1) {
1382 sum += *w++;
1383 nleft -= 2;
1384 }
1385
1386 /* mop up an odd byte, if necessary */
1387 if (nleft == 1) {
2b484d24
A
1388 last.uc[0] = *(u_char *)w;
1389 last.uc[1] = 0;
1390 sum += last.us;
b7080c8e
A
1391 }
1392
1393 /* add back carry outs from top 16 bits to low 16 bits */
1394 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
1395 sum += (sum >> 16); /* add carry */
1396 answer = ~sum; /* truncate to 16 bits */
1397 return(answer);
1398}
1399
1400/*
1401 * tvsub --
1402 * Subtract 2 timeval structs: out = out - in. Out is assumed to
1403 * be >= in.
1404 */
2b484d24 1405static void
b7080c8e 1406tvsub(out, in)
2b484d24 1407 struct timeval *out, *in;
b7080c8e 1408{
2b484d24 1409
b7080c8e
A
1410 if ((out->tv_usec -= in->tv_usec) < 0) {
1411 --out->tv_sec;
1412 out->tv_usec += 1000000;
1413 }
1414 out->tv_sec -= in->tv_sec;
1415}
1416
2b484d24
A
1417/*
1418 * status --
1419 * Print out statistics when SIGINFO is received.
1420 */
1421
1422static void
1423status(sig)
1424 int sig __unused;
1425{
1426
1427 siginfo_p = 1;
1428}
1429
1430static void
1431check_status()
1432{
1433
1434 if (siginfo_p) {
1435 siginfo_p = 0;
9c859447 1436 (void)fprintf(stderr, "\r%ld/%ld packets received (%.1f%%)",
2b484d24
A
1437 nreceived, ntransmitted,
1438 ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0);
1439 if (nreceived && timing)
1440 (void)fprintf(stderr, " %.3f min / %.3f avg / %.3f max",
1441 tmin, tsum / (nreceived + nrepeats), tmax);
1442 (void)fprintf(stderr, "\n");
1443 }
1444}
1445
b7080c8e
A
1446/*
1447 * finish --
1448 * Print out statistics, and give up.
1449 */
2b484d24 1450static void
b7080c8e
A
1451finish()
1452{
b7080c8e
A
1453
1454 (void)signal(SIGINT, SIG_IGN);
2b484d24 1455 (void)signal(SIGALRM, SIG_IGN);
b7080c8e
A
1456 (void)putchar('\n');
1457 (void)fflush(stdout);
1458 (void)printf("--- %s ping statistics ---\n", hostname);
1459 (void)printf("%ld packets transmitted, ", ntransmitted);
1460 (void)printf("%ld packets received, ", nreceived);
1461 if (nrepeats)
1462 (void)printf("+%ld duplicates, ", nrepeats);
2b484d24 1463 if (ntransmitted) {
b7080c8e
A
1464 if (nreceived > ntransmitted)
1465 (void)printf("-- somebody's printing up packets!");
1466 else
9c859447
A
1467 (void)printf("%.1f%% packet loss",
1468 ((ntransmitted - nreceived) * 100.0) /
1469 ntransmitted);
2b484d24 1470 }
9c859447
A
1471 if (nrcvtimeout)
1472 (void)printf(", %ld packets out of wait time", nrcvtimeout);
b7080c8e
A
1473 (void)putchar('\n');
1474 if (nreceived && timing) {
2b484d24
A
1475 double n = nreceived + nrepeats;
1476 double avg = tsum / n;
1477 double vari = tsumsq / n - avg * avg;
1478 (void)printf(
1479 "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n",
1480 tmin, avg, tmax, sqrt(vari));
b7080c8e 1481 }
2b484d24
A
1482
1483 if (nreceived)
1484 exit(0);
1485 else
1486 exit(2);
b7080c8e
A
1487}
1488
1489#ifdef notdef
1490static char *ttab[] = {
1491 "Echo Reply", /* ip + seq + udata */
1492 "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */
1493 "Source Quench", /* IP */
1494 "Redirect", /* redirect type, gateway, + IP */
1495 "Echo",
1496 "Time Exceeded", /* transit, frag reassem + IP */
1497 "Parameter Problem", /* pointer + IP */
1498 "Timestamp", /* id + seq + three timestamps */
1499 "Timestamp Reply", /* " */
1500 "Info Request", /* id + sq */
1501 "Info Reply" /* " */
1502};
1503#endif
1504
1505/*
1506 * pr_icmph --
1507 * Print a descriptive string about an ICMP header.
1508 */
2b484d24 1509static void
b7080c8e
A
1510pr_icmph(icp)
1511 struct icmp *icp;
1512{
2b484d24 1513
b7080c8e
A
1514 switch(icp->icmp_type) {
1515 case ICMP_ECHOREPLY:
1516 (void)printf("Echo Reply\n");
1517 /* XXX ID + Seq + Data */
1518 break;
1519 case ICMP_UNREACH:
1520 switch(icp->icmp_code) {
1521 case ICMP_UNREACH_NET:
1522 (void)printf("Destination Net Unreachable\n");
1523 break;
1524 case ICMP_UNREACH_HOST:
1525 (void)printf("Destination Host Unreachable\n");
1526 break;
1527 case ICMP_UNREACH_PROTOCOL:
1528 (void)printf("Destination Protocol Unreachable\n");
1529 break;
1530 case ICMP_UNREACH_PORT:
1531 (void)printf("Destination Port Unreachable\n");
1532 break;
1533 case ICMP_UNREACH_NEEDFRAG:
2b484d24
A
1534 (void)printf("frag needed and DF set (MTU %d)\n",
1535 ntohs(icp->icmp_nextmtu));
b7080c8e
A
1536 break;
1537 case ICMP_UNREACH_SRCFAIL:
1538 (void)printf("Source Route Failed\n");
1539 break;
2b484d24
A
1540 case ICMP_UNREACH_FILTER_PROHIB:
1541 (void)printf("Communication prohibited by filter\n");
1542 break;
b7080c8e
A
1543 default:
1544 (void)printf("Dest Unreachable, Bad Code: %d\n",
1545 icp->icmp_code);
1546 break;
1547 }
1548 /* Print returned IP header information */
1549#ifndef icmp_data
1550 pr_retip(&icp->icmp_ip);
1551#else
1552 pr_retip((struct ip *)icp->icmp_data);
1553#endif
1554 break;
1555 case ICMP_SOURCEQUENCH:
1556 (void)printf("Source Quench\n");
1557#ifndef icmp_data
1558 pr_retip(&icp->icmp_ip);
1559#else
1560 pr_retip((struct ip *)icp->icmp_data);
1561#endif
1562 break;
1563 case ICMP_REDIRECT:
1564 switch(icp->icmp_code) {
1565 case ICMP_REDIRECT_NET:
1566 (void)printf("Redirect Network");
1567 break;
1568 case ICMP_REDIRECT_HOST:
1569 (void)printf("Redirect Host");
1570 break;
1571 case ICMP_REDIRECT_TOSNET:
1572 (void)printf("Redirect Type of Service and Network");
1573 break;
1574 case ICMP_REDIRECT_TOSHOST:
1575 (void)printf("Redirect Type of Service and Host");
1576 break;
1577 default:
1578 (void)printf("Redirect, Bad Code: %d", icp->icmp_code);
1579 break;
1580 }
2b484d24 1581 (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr));
b7080c8e
A
1582#ifndef icmp_data
1583 pr_retip(&icp->icmp_ip);
1584#else
1585 pr_retip((struct ip *)icp->icmp_data);
1586#endif
1587 break;
1588 case ICMP_ECHO:
1589 (void)printf("Echo Request\n");
1590 /* XXX ID + Seq + Data */
1591 break;
1592 case ICMP_TIMXCEED:
1593 switch(icp->icmp_code) {
1594 case ICMP_TIMXCEED_INTRANS:
1595 (void)printf("Time to live exceeded\n");
1596 break;
1597 case ICMP_TIMXCEED_REASS:
1598 (void)printf("Frag reassembly time exceeded\n");
1599 break;
1600 default:
1601 (void)printf("Time exceeded, Bad Code: %d\n",
1602 icp->icmp_code);
1603 break;
1604 }
1605#ifndef icmp_data
1606 pr_retip(&icp->icmp_ip);
1607#else
1608 pr_retip((struct ip *)icp->icmp_data);
1609#endif
1610 break;
1611 case ICMP_PARAMPROB:
1612 (void)printf("Parameter problem: pointer = 0x%02x\n",
1613 icp->icmp_hun.ih_pptr);
1614#ifndef icmp_data
1615 pr_retip(&icp->icmp_ip);
1616#else
1617 pr_retip((struct ip *)icp->icmp_data);
1618#endif
1619 break;
1620 case ICMP_TSTAMP:
1621 (void)printf("Timestamp\n");
1622 /* XXX ID + Seq + 3 timestamps */
1623 break;
1624 case ICMP_TSTAMPREPLY:
1625 (void)printf("Timestamp Reply\n");
1626 /* XXX ID + Seq + 3 timestamps */
1627 break;
1628 case ICMP_IREQ:
1629 (void)printf("Information Request\n");
1630 /* XXX ID + Seq */
1631 break;
1632 case ICMP_IREQREPLY:
1633 (void)printf("Information Reply\n");
1634 /* XXX ID + Seq */
1635 break;
b7080c8e
A
1636 case ICMP_MASKREQ:
1637 (void)printf("Address Mask Request\n");
1638 break;
b7080c8e
A
1639 case ICMP_MASKREPLY:
1640 (void)printf("Address Mask Reply\n");
1641 break;
2b484d24
A
1642 case ICMP_ROUTERADVERT:
1643 (void)printf("Router Advertisement\n");
1644 break;
1645 case ICMP_ROUTERSOLICIT:
1646 (void)printf("Router Solicitation\n");
1647 break;
b7080c8e
A
1648 default:
1649 (void)printf("Bad ICMP type: %d\n", icp->icmp_type);
1650 }
1651}
1652
1653/*
1654 * pr_iph --
1655 * Print an IP header with options.
1656 */
2b484d24 1657static void
b7080c8e
A
1658pr_iph(ip)
1659 struct ip *ip;
1660{
b7080c8e 1661 u_char *cp;
2b484d24 1662 int hlen;
b7080c8e
A
1663
1664 hlen = ip->ip_hl << 2;
1665 cp = (u_char *)ip + 20; /* point to options */
1666
2b484d24 1667 (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n");
b7080c8e 1668 (void)printf(" %1x %1x %02x %04x %04x",
2b484d24
A
1669 ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len),
1670 ntohs(ip->ip_id));
1671 (void)printf(" %1lx %04lx",
1672 (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13,
1673 (u_long) ntohl(ip->ip_off) & 0x1fff);
1674 (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p,
1675 ntohs(ip->ip_sum));
b7080c8e
A
1676 (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr));
1677 (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr));
2b484d24 1678 /* dump any option bytes */
b7080c8e
A
1679 while (hlen-- > 20) {
1680 (void)printf("%02x", *cp++);
1681 }
1682 (void)putchar('\n');
1683}
1684
1685/*
1686 * pr_addr --
1687 * Return an ascii host address as a dotted quad and optionally with
1688 * a hostname.
1689 */
2b484d24
A
1690static char *
1691pr_addr(ina)
1692 struct in_addr ina;
b7080c8e
A
1693{
1694 struct hostent *hp;
2b484d24 1695 static char buf[16 + 3 + MAXHOSTNAMELEN];
b7080c8e
A
1696
1697 if ((options & F_NUMERIC) ||
2b484d24
A
1698 !(hp = gethostbyaddr((char *)&ina, 4, AF_INET)))
1699 return inet_ntoa(ina);
b7080c8e 1700 else
2b484d24
A
1701 (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name,
1702 inet_ntoa(ina));
b7080c8e
A
1703 return(buf);
1704}
1705
1706/*
1707 * pr_retip --
1708 * Dump some info on a returned (via ICMP) IP packet.
1709 */
2b484d24 1710static void
b7080c8e
A
1711pr_retip(ip)
1712 struct ip *ip;
1713{
b7080c8e 1714 u_char *cp;
2b484d24 1715 int hlen;
b7080c8e
A
1716
1717 pr_iph(ip);
1718 hlen = ip->ip_hl << 2;
1719 cp = (u_char *)ip + hlen;
1720
1721 if (ip->ip_p == 6)
1722 (void)printf("TCP: from port %u, to port %u (decimal)\n",
1723 (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
1724 else if (ip->ip_p == 17)
1725 (void)printf("UDP: from port %u, to port %u (decimal)\n",
1726 (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
1727}
1728
2b484d24
A
1729static char *
1730pr_ntime (n_time timestamp)
1731{
1732 static char buf[10];
1733 int hour, min, sec;
1734
1735 sec = ntohl(timestamp) / 1000;
1736 hour = sec / 60 / 60;
1737 min = (sec % (60 * 60)) / 60;
1738 sec = (sec % (60 * 60)) % 60;
1739
1740 (void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hour, min, sec);
1741
1742 return (buf);
1743}
1744
1745static void
b7080c8e
A
1746fill(bp, patp)
1747 char *bp, *patp;
1748{
b7080c8e 1749 char *cp;
2b484d24
A
1750 int pat[16];
1751 u_int ii, jj, kk;
b7080c8e 1752
2b484d24
A
1753 for (cp = patp; *cp; cp++) {
1754 if (!isxdigit(*cp))
1755 errx(EX_USAGE,
1756 "patterns must be specified as hex digits");
1757
1758 }
b7080c8e
A
1759 ii = sscanf(patp,
1760 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
1761 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
1762 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
1763 &pat[13], &pat[14], &pat[15]);
1764
1765 if (ii > 0)
2b484d24 1766 for (kk = 0; kk <= maxpayload - (TIMEVAL_LEN + ii); kk += ii)
b7080c8e
A
1767 for (jj = 0; jj < ii; ++jj)
1768 bp[jj + kk] = pat[jj];
1769 if (!(options & F_QUIET)) {
1770 (void)printf("PATTERN: 0x");
1771 for (jj = 0; jj < ii; ++jj)
1772 (void)printf("%02x", bp[jj] & 0xFF);
1773 (void)printf("\n");
1774 }
1775}
1776
2b484d24
A
1777#if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
1778#define SECOPT " [-P policy]"
1779#else
1780#define SECOPT ""
1781#endif
1782static void
b7080c8e
A
1783usage()
1784{
2b484d24 1785
9c859447
A
1786 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1787"usage: ping [-AaDdfnoQqRrv] [-b boundif] [-c count] [-G sweepmaxsize] [-g sweepminsize]",
1788" [-h sweepincrsize] [-i wait] [-l preload] [-M mask | time] [-m ttl]",
1789" " SECOPT " [-p pattern] [-S src_addr] [-s packetsize] [-t timeout]",
1790" [-W waittime] [-z tos] host",
2b484d24
A
1791" ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload]",
1792" [-M mask | time] [-m ttl]" SECOPT " [-p pattern] [-S src_addr]",
9c859447
A
1793" [-s packetsize] [-T ttl] [-t timeout] [-W waittime]",
1794" [-z tos] mcast-group");
2b484d24 1795 exit(EX_USAGE);
b7080c8e 1796}