source: rtems-libbsd/freebsd/sbin/ping/ping.c @ 8ee0aa1

55-freebsd-126-freebsd-12
Last change on this file since 8ee0aa1 was 8ee0aa1, checked in by Sebastian Huber <sebastian.huber@…>, on 10/10/18 at 12:40:47

PING(8): Honour file descriptor maximum

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