18a16b7a1SPedro F. Giffuni /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 48fae3551SRodney W. Grimes * Copyright (c) 1989, 1993 58fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 68fae3551SRodney W. Grimes * 78fae3551SRodney W. Grimes * This code is derived from software contributed to Berkeley by 88fae3551SRodney W. Grimes * Mike Muuss. 98fae3551SRodney W. Grimes * 108fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 118fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 128fae3551SRodney W. Grimes * are met: 138fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 148fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 158fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 168fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 178fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 198fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 208fae3551SRodney W. Grimes * without specific prior written permission. 218fae3551SRodney W. Grimes * 228fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 238fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 248fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 258fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 268fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 278fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 288fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 298fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 308fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 318fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 328fae3551SRodney W. Grimes * SUCH DAMAGE. 338fae3551SRodney W. Grimes */ 348fae3551SRodney W. Grimes 35c69284caSDavid E. O'Brien #if 0 368fae3551SRodney W. Grimes #ifndef lint 3743470e3bSGarrett Wollman static const char copyright[] = 388fae3551SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\ 398fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 408fae3551SRodney W. Grimes #endif /* not lint */ 418fae3551SRodney W. Grimes 428fae3551SRodney W. Grimes #ifndef lint 438fae3551SRodney W. Grimes static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93"; 448fae3551SRodney W. Grimes #endif /* not lint */ 45c69284caSDavid E. O'Brien #endif 46c69284caSDavid E. O'Brien #include <sys/cdefs.h> 47c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$"); 488fae3551SRodney W. Grimes 498fae3551SRodney W. Grimes /* 508fae3551SRodney W. Grimes * P I N G . C 518fae3551SRodney W. Grimes * 52e345a80dSPhilippe Charnier * Using the Internet Control Message Protocol (ICMP) "ECHO" facility, 538fae3551SRodney W. Grimes * measure round-trip-delays and packet loss across network paths. 548fae3551SRodney W. Grimes * 558fae3551SRodney W. Grimes * Author - 568fae3551SRodney W. Grimes * Mike Muuss 578fae3551SRodney W. Grimes * U. S. Army Ballistic Research Laboratory 588fae3551SRodney W. Grimes * December, 1983 598fae3551SRodney W. Grimes * 608fae3551SRodney W. Grimes * Status - 618fae3551SRodney W. Grimes * Public Domain. Distribution Unlimited. 628fae3551SRodney W. Grimes * Bugs - 638fae3551SRodney W. Grimes * More statistics could always be gathered. 648fae3551SRodney W. Grimes * This program has to run SUID to ROOT to access the ICMP socket. 658fae3551SRodney W. Grimes */ 668fae3551SRodney W. Grimes 6743470e3bSGarrett Wollman #include <sys/param.h> /* NB: we rely on this for <sys/types.h> */ 68b881b8beSRobert Watson #include <sys/capsicum.h> 698fae3551SRodney W. Grimes #include <sys/socket.h> 70261e59bbSMaxim Konovalov #include <sys/sysctl.h> 718fae3551SRodney W. Grimes #include <sys/time.h> 72039d6aa4SBill Fenner #include <sys/uio.h> 738fae3551SRodney W. Grimes 748fae3551SRodney W. Grimes #include <netinet/in.h> 7543470e3bSGarrett Wollman #include <netinet/in_systm.h> 768fae3551SRodney W. Grimes #include <netinet/ip.h> 778fae3551SRodney W. Grimes #include <netinet/ip_icmp.h> 788fae3551SRodney W. Grimes #include <netinet/ip_var.h> 7943470e3bSGarrett Wollman #include <arpa/inet.h> 80c501d73cSMariusz Zaborski 81c501d73cSMariusz Zaborski #include <libcasper.h> 82c501d73cSMariusz Zaborski #include <casper/cap_dns.h> 838fae3551SRodney W. Grimes 849a4365d0SYoshinobu Inoue #ifdef IPSEC 858409aedfSGeorge V. Neville-Neil #include <netipsec/ipsec.h> 869a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 879a4365d0SYoshinobu Inoue 887bdc3291SMark Johnston #include <capsicum_helpers.h> 89261e59bbSMaxim Konovalov #include <ctype.h> 90261e59bbSMaxim Konovalov #include <err.h> 91261e59bbSMaxim Konovalov #include <errno.h> 92261e59bbSMaxim Konovalov #include <math.h> 93261e59bbSMaxim Konovalov #include <netdb.h> 94d9cacf60SAlan Somers #include <stddef.h> 95261e59bbSMaxim Konovalov #include <signal.h> 96261e59bbSMaxim Konovalov #include <stdio.h> 97261e59bbSMaxim Konovalov #include <stdlib.h> 98261e59bbSMaxim Konovalov #include <string.h> 99261e59bbSMaxim Konovalov #include <sysexits.h> 1001ad76f1bSAlan Somers #include <time.h> 101261e59bbSMaxim Konovalov #include <unistd.h> 102261e59bbSMaxim Konovalov 1033cde9171SAlan Somers #include "main.h" 1043cde9171SAlan Somers #include "ping.h" 105ff77ab83SAlan Somers #include "utils.h" 106ff77ab83SAlan Somers 107301207dfSMaxim Konovalov #define INADDR_LEN ((int)sizeof(in_addr_t)) 10813e3f0b7SMaxim Konovalov #define TIMEVAL_LEN ((int)sizeof(struct tv32)) 109eb1543c6SMatthew N. Dodd #define MASK_LEN (ICMP_MASKLEN - ICMP_MINLEN) 110eb1543c6SMatthew N. Dodd #define TS_LEN (ICMP_TSLEN - ICMP_MINLEN) 111c67c1ce8SMatthew N. Dodd #define DEFDATALEN 56 /* default data length */ 1128f975bb3SBruce Evans #define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */ 1138f975bb3SBruce Evans /* runs out of buffer space */ 1144fba6582SMaxim Konovalov #define MAXIPLEN (sizeof(struct ip) + MAX_IPOPTLEN) 1154fba6582SMaxim Konovalov #define MAXICMPLEN (ICMP_ADVLENMIN + MAX_IPOPTLEN) 116d6cd1497SGleb Smirnoff #define MAXWAIT 10000 /* max ms to wait for response */ 117bf113f1bSBill Fumerola #define MAXALARM (60 * 60) /* max seconds for alarm timeout */ 1180b2f8b3fSMaxim Konovalov #define MAXTOS 255 1198fae3551SRodney W. Grimes 1208fae3551SRodney W. Grimes #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 1218fae3551SRodney W. Grimes #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 1228fae3551SRodney W. Grimes #define SET(bit) (A(bit) |= B(bit)) 1238fae3551SRodney W. Grimes #define CLR(bit) (A(bit) &= (~B(bit))) 1248fae3551SRodney W. Grimes #define TST(bit) (A(bit) & B(bit)) 1258fae3551SRodney W. Grimes 12613e3f0b7SMaxim Konovalov struct tv32 { 12713e3f0b7SMaxim Konovalov int32_t tv32_sec; 1281ad76f1bSAlan Somers int32_t tv32_nsec; 12913e3f0b7SMaxim Konovalov }; 13013e3f0b7SMaxim Konovalov 1318fae3551SRodney W. Grimes /* various options */ 1327e9489e0SHiroki Sato static int options; 13385456935SBill Fenner #define F_FLOOD 0x0001 13485456935SBill Fenner #define F_INTERVAL 0x0002 13585456935SBill Fenner #define F_NUMERIC 0x0004 13685456935SBill Fenner #define F_PINGFILLED 0x0008 13785456935SBill Fenner #define F_QUIET 0x0010 13885456935SBill Fenner #define F_RROUTE 0x0020 13985456935SBill Fenner #define F_SO_DEBUG 0x0040 14085456935SBill Fenner #define F_SO_DONTROUTE 0x0080 14185456935SBill Fenner #define F_VERBOSE 0x0100 14285456935SBill Fenner #define F_QUIET2 0x0200 14385456935SBill Fenner #define F_NOLOOP 0x0400 14485456935SBill Fenner #define F_MTTL 0x0800 14585456935SBill Fenner #define F_MIF 0x1000 146772dfa72SDaniel O'Callaghan #define F_AUDIBLE 0x2000 1479a4365d0SYoshinobu Inoue #ifdef IPSEC 1489a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 1499a4365d0SYoshinobu Inoue #define F_POLICY 0x4000 1509a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 1519a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 152211bfbd2SRuslan Ermilov #define F_TTL 0x8000 153ca517ad8SPoul-Henning Kamp #define F_MISSED 0x10000 1548025c44bSDima Dorfman #define F_ONCE 0x20000 1550b2f8b3fSMaxim Konovalov #define F_HDRINCL 0x40000 156143008a1SMatthew N. Dodd #define F_MASK 0x80000 157eb1543c6SMatthew N. Dodd #define F_TIME 0x100000 1589ff95228SGleb Smirnoff #define F_SWEEP 0x200000 159d6cd1497SGleb Smirnoff #define F_WAITTIME 0x400000 16081a6f4c7SRichard Scheffenegger #define F_IP_VLAN_PCP 0x800000 161d399eb3eSPiotr Pawel Stefaniak #define F_DOT 0x1000000 1628fae3551SRodney W. Grimes 1638fae3551SRodney W. Grimes /* 1648fae3551SRodney W. Grimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 1658fae3551SRodney W. Grimes * number of received sequence numbers we can keep track of. Change 128 1668fae3551SRodney W. Grimes * to 8192 for complete accuracy... 1678fae3551SRodney W. Grimes */ 1688fae3551SRodney W. Grimes #define MAX_DUP_CHK (8 * 128) 1697e9489e0SHiroki Sato static int mx_dup_ck = MAX_DUP_CHK; 1707e9489e0SHiroki Sato static char rcvd_tbl[MAX_DUP_CHK / 8]; 1718fae3551SRodney W. Grimes 1727e9489e0SHiroki Sato static struct sockaddr_in whereto; /* who to ping */ 1737e9489e0SHiroki Sato static int datalen = DEFDATALEN; 1747e9489e0SHiroki Sato static int maxpayload; 1757e9489e0SHiroki Sato static int ssend; /* send socket file descriptor */ 1767e9489e0SHiroki Sato static int srecv; /* receive socket file descriptor */ 1777e9489e0SHiroki Sato static u_char outpackhdr[IP_MAXPACKET], *outpack; 1787e9489e0SHiroki Sato static char BBELL = '\a'; /* characters written for MISSED and AUDIBLE */ 1797e9489e0SHiroki Sato static char BSPACE = '\b'; /* characters written for flood */ 180d399eb3eSPiotr Pawel Stefaniak static const char *DOT = "."; 181d399eb3eSPiotr Pawel Stefaniak static size_t DOTlen = 1; 182d399eb3eSPiotr Pawel Stefaniak static size_t DOTidx = 0; 1837e9489e0SHiroki Sato static char *hostname; 1847e9489e0SHiroki Sato static char *shostname; 1857e9489e0SHiroki Sato static int ident; /* process id to identify our packets */ 1867e9489e0SHiroki Sato static int uid; /* cached uid for micro-optimization */ 1877e9489e0SHiroki Sato static u_char icmp_type = ICMP_ECHO; 1887e9489e0SHiroki Sato static u_char icmp_type_rsp = ICMP_ECHOREPLY; 1897e9489e0SHiroki Sato static int phdr_len = 0; 1907e9489e0SHiroki Sato static int send_len; 1918fae3551SRodney W. Grimes 1928fae3551SRodney W. Grimes /* counters */ 1937e9489e0SHiroki Sato static long nmissedmax; /* max value of ntransmitted - nreceived - 1 */ 1947e9489e0SHiroki Sato static long npackets; /* max packets to transmit */ 1957e9489e0SHiroki Sato static long nreceived; /* # of packets we got back */ 1967e9489e0SHiroki Sato static long nrepeats; /* number of duplicates */ 1977e9489e0SHiroki Sato static long ntransmitted; /* sequence # for outbound packets = #sent */ 1987e9489e0SHiroki Sato static long snpackets; /* max packets to transmit in one sweep */ 1997e9489e0SHiroki Sato static long sntransmitted; /* # of packets we sent in this sweep */ 2007e9489e0SHiroki Sato static int sweepmax; /* max value of payload in sweep */ 2017e9489e0SHiroki Sato static int sweepmin = 0; /* start value of payload in sweep */ 2027e9489e0SHiroki Sato static int sweepincr = 1; /* payload increment in sweep */ 2037e9489e0SHiroki Sato static int interval = 1000; /* interval between packets, ms */ 2047e9489e0SHiroki Sato static int waittime = MAXWAIT; /* timeout for each packet */ 2057e9489e0SHiroki Sato static long nrcvtimeout = 0; /* # of packets we got back after waittime */ 2068fae3551SRodney W. Grimes 2078fae3551SRodney W. Grimes /* timing */ 2087e9489e0SHiroki Sato static int timing; /* flag to do timing */ 2097e9489e0SHiroki Sato static double tmin = 999999999.0; /* minimum round trip time */ 2107e9489e0SHiroki Sato static double tmax = 0.0; /* maximum round trip time */ 2117e9489e0SHiroki Sato static double tsum = 0.0; /* sum of all times, for doing average */ 2127e9489e0SHiroki Sato static double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 2138fae3551SRodney W. Grimes 2147e9489e0SHiroki Sato /* nonzero if we've been told to finish up */ 2157e9489e0SHiroki Sato static volatile sig_atomic_t finish_up; 2167e9489e0SHiroki Sato static volatile sig_atomic_t siginfo_p; 217badd8138SSean Eric Fagan 21849133c6dSPawel Jakub Dawidek static cap_channel_t *capdns; 21949133c6dSPawel Jakub Dawidek 22043470e3bSGarrett Wollman static void fill(char *, char *); 22149133c6dSPawel Jakub Dawidek static cap_channel_t *capdns_setup(void); 22243470e3bSGarrett Wollman static void check_status(void); 2238f975bb3SBruce Evans static void finish(void) __dead2; 22443470e3bSGarrett Wollman static void pinger(void); 22543470e3bSGarrett Wollman static char *pr_addr(struct in_addr); 226eb1543c6SMatthew N. Dodd static char *pr_ntime(n_time); 227d9cacf60SAlan Somers static void pr_icmph(struct icmp *, struct ip *, const u_char *const); 22820b41303SJose Luis Duran static void pr_iph(struct ip *, const u_char *); 229d9cacf60SAlan Somers static void pr_pack(char *, ssize_t, struct sockaddr_in *, struct timespec *); 23043470e3bSGarrett Wollman static void status(int); 2318f975bb3SBruce Evans static void stopit(int); 2328fae3551SRodney W. Grimes 23343470e3bSGarrett Wollman int 2343cde9171SAlan Somers ping(int argc, char *const *argv) 2358fae3551SRodney W. Grimes { 23631eac03bSSean Chittenden struct sockaddr_in from, sock_in; 237efc8588dSDavid E. O'Brien struct in_addr ifaddr; 2381ad76f1bSAlan Somers struct timespec last, intvl; 239efc8588dSDavid E. O'Brien struct iovec iov; 240efc8588dSDavid E. O'Brien struct msghdr msg; 241efc8588dSDavid E. O'Brien struct sigaction si_sa; 2420b2f8b3fSMaxim Konovalov size_t sz; 243e81f5049SOlivier Houchard u_char *datap, packet[IP_MAXPACKET] __aligned(4); 24478e1f68eSMark Johnston const char *errstr; 245d074d39fSMatthew N. Dodd char *ep, *source, *target, *payload; 246261e59bbSMaxim Konovalov struct hostent *hp; 247efc8588dSDavid E. O'Brien #ifdef IPSEC_POLICY_IPSEC 248efc8588dSDavid E. O'Brien char *policy_in, *policy_out; 249efc8588dSDavid E. O'Brien #endif 250261e59bbSMaxim Konovalov struct sockaddr_in *to; 251261e59bbSMaxim Konovalov double t; 252c0a3773aSEugene Grosbein u_long alarmtimeout; 25378e1f68eSMark Johnston long long ltmp; 25449133c6dSPawel Jakub Dawidek int almost_done, ch, df, hold, i, icmp_len, mib[4], preload; 25581a6f4c7SRichard Scheffenegger int ssend_errno, srecv_errno, tos, ttl, pcp; 2561ad76f1bSAlan Somers char ctrl[CMSG_SPACE(sizeof(struct timespec))]; 257efc8588dSDavid E. O'Brien char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN]; 2588fae3551SRodney W. Grimes #ifdef IP_OPTIONS 2594fba6582SMaxim Konovalov char rspace[MAX_IPOPTLEN]; /* record route space */ 2608fae3551SRodney W. Grimes #endif 261261e59bbSMaxim Konovalov unsigned char loop, mttl; 262efc8588dSDavid E. O'Brien 26331eac03bSSean Chittenden payload = source = NULL; 2649a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 265efc8588dSDavid E. O'Brien policy_in = policy_out = NULL; 2669a4365d0SYoshinobu Inoue #endif 26749133c6dSPawel Jakub Dawidek cap_rights_t rights; 2688fae3551SRodney W. Grimes 269299e2c58SAlan Somers options |= F_NUMERIC; 270299e2c58SAlan Somers 271f1284d7aSBill Fenner /* 272f1284d7aSBill Fenner * Do the stuff that we need root priv's for *first*, and 273f1284d7aSBill Fenner * then drop our setuid bit. Save error reporting for 274f1284d7aSBill Fenner * after arg parsing. 27549133c6dSPawel Jakub Dawidek * 27649133c6dSPawel Jakub Dawidek * Historicaly ping was using one socket 's' for sending and for 27749133c6dSPawel Jakub Dawidek * receiving. After capsicum(4) related changes we use two 27849133c6dSPawel Jakub Dawidek * sockets. It was done for special ping use case - when user 27949133c6dSPawel Jakub Dawidek * issue ping on multicast or broadcast address replies come 28049133c6dSPawel Jakub Dawidek * from different addresses, not from the address we 28149133c6dSPawel Jakub Dawidek * connect(2)'ed to, and send socket do not receive those 28249133c6dSPawel Jakub Dawidek * packets. 283f1284d7aSBill Fenner */ 28449133c6dSPawel Jakub Dawidek ssend = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 28549133c6dSPawel Jakub Dawidek ssend_errno = errno; 28649133c6dSPawel Jakub Dawidek srecv = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 28749133c6dSPawel Jakub Dawidek srecv_errno = errno; 288f1284d7aSBill Fenner 2891d1d4a47SEitan Adler if (setuid(getuid()) != 0) 2901d1d4a47SEitan Adler err(EX_NOPERM, "setuid() failed"); 291ee2bf734SWarner Losh uid = getuid(); 292f1284d7aSBill Fenner 293eeb63943SDon Lewis if (ssend < 0) { 294eeb63943SDon Lewis errno = ssend_errno; 295eeb63943SDon Lewis err(EX_OSERR, "ssend socket"); 296eeb63943SDon Lewis } 297eeb63943SDon Lewis 298eeb63943SDon Lewis if (srecv < 0) { 299eeb63943SDon Lewis errno = srecv_errno; 300eeb63943SDon Lewis err(EX_OSERR, "srecv socket"); 301eeb63943SDon Lewis } 302eeb63943SDon Lewis 30381a6f4c7SRichard Scheffenegger alarmtimeout = df = preload = tos = pcp = 0; 304badd8138SSean Eric Fagan 3050b2f8b3fSMaxim Konovalov outpack = outpackhdr + sizeof(struct ip); 3069ce201f2SAlan Somers while ((ch = getopt(argc, argv, PING4OPTS)) != -1) { 3078fae3551SRodney W. Grimes switch(ch) { 308d399eb3eSPiotr Pawel Stefaniak case '.': 309d399eb3eSPiotr Pawel Stefaniak options |= F_DOT; 310d399eb3eSPiotr Pawel Stefaniak if (optarg != NULL) { 311d399eb3eSPiotr Pawel Stefaniak DOT = optarg; 312d399eb3eSPiotr Pawel Stefaniak DOTlen = strlen(optarg); 313d399eb3eSPiotr Pawel Stefaniak } 314d399eb3eSPiotr Pawel Stefaniak break; 3153cde9171SAlan Somers case '4': 3163cde9171SAlan Somers /* This option is processed in main(). */ 3173cde9171SAlan Somers break; 318ca517ad8SPoul-Henning Kamp case 'A': 319ca517ad8SPoul-Henning Kamp options |= F_MISSED; 320ca517ad8SPoul-Henning Kamp break; 321772dfa72SDaniel O'Callaghan case 'a': 322772dfa72SDaniel O'Callaghan options |= F_AUDIBLE; 323772dfa72SDaniel O'Callaghan break; 32481a6f4c7SRichard Scheffenegger case 'C': 32581a6f4c7SRichard Scheffenegger options |= F_IP_VLAN_PCP; 32678e1f68eSMark Johnston ltmp = strtonum(optarg, -1, 7, &errstr); 32778e1f68eSMark Johnston if (errstr != NULL) 32881a6f4c7SRichard Scheffenegger errx(EX_USAGE, "invalid PCP: `%s'", optarg); 32981a6f4c7SRichard Scheffenegger pcp = ltmp; 33081a6f4c7SRichard Scheffenegger break; 3318fae3551SRodney W. Grimes case 'c': 33278e1f68eSMark Johnston ltmp = strtonum(optarg, 1, LONG_MAX, &errstr); 33378e1f68eSMark Johnston if (errstr != NULL) 33443470e3bSGarrett Wollman errx(EX_USAGE, 33543470e3bSGarrett Wollman "invalid count of packets to transmit: `%s'", 33643470e3bSGarrett Wollman optarg); 33778e1f68eSMark Johnston npackets = (long)ltmp; 3388fae3551SRodney W. Grimes break; 3390b2f8b3fSMaxim Konovalov case 'D': 3400b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 3410b2f8b3fSMaxim Konovalov df = 1; 3420b2f8b3fSMaxim Konovalov break; 3438fae3551SRodney W. Grimes case 'd': 3448fae3551SRodney W. Grimes options |= F_SO_DEBUG; 3458fae3551SRodney W. Grimes break; 3468fae3551SRodney W. Grimes case 'f': 347526f06b2SMatthew Dillon if (uid) { 34843470e3bSGarrett Wollman errno = EPERM; 34943470e3bSGarrett Wollman err(EX_NOPERM, "-f flag"); 3508fae3551SRodney W. Grimes } 3518fae3551SRodney W. Grimes options |= F_FLOOD; 352d399eb3eSPiotr Pawel Stefaniak options |= F_DOT; 3538fae3551SRodney W. Grimes setbuf(stdout, (char *)NULL); 3548fae3551SRodney W. Grimes break; 3559ff95228SGleb Smirnoff case 'G': /* Maximum packet size for ping sweep */ 35678e1f68eSMark Johnston ltmp = strtonum(optarg, 1, INT_MAX, &errstr); 35778e1f68eSMark Johnston if (errstr != NULL) { 3589ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3599ff95228SGleb Smirnoff optarg); 36078e1f68eSMark Johnston } 36178e1f68eSMark Johnston sweepmax = (int)ltmp; 36278e1f68eSMark Johnston if (uid != 0 && sweepmax > DEFDATALEN) { 36378e1f68eSMark Johnston errc(EX_NOPERM, EPERM, 36478e1f68eSMark Johnston "packet size too large: %d > %u", 36578e1f68eSMark Johnston sweepmax, DEFDATALEN); 3669ff95228SGleb Smirnoff } 3679ff95228SGleb Smirnoff options |= F_SWEEP; 3689ff95228SGleb Smirnoff break; 3699ff95228SGleb Smirnoff case 'g': /* Minimum packet size for ping sweep */ 37078e1f68eSMark Johnston ltmp = strtonum(optarg, 1, INT_MAX, &errstr); 37178e1f68eSMark Johnston if (errstr != NULL) { 3729ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3739ff95228SGleb Smirnoff optarg); 37478e1f68eSMark Johnston } 37578e1f68eSMark Johnston sweepmin = (int)ltmp; 37678e1f68eSMark Johnston if (uid != 0 && sweepmin > DEFDATALEN) { 37778e1f68eSMark Johnston errc(EX_NOPERM, EPERM, 37878e1f68eSMark Johnston "packet size too large: %d > %u", 37978e1f68eSMark Johnston sweepmin, DEFDATALEN); 3809ff95228SGleb Smirnoff } 3819ff95228SGleb Smirnoff options |= F_SWEEP; 3829ff95228SGleb Smirnoff break; 38399f13ae1SAlan Somers case 'H': 38499f13ae1SAlan Somers options &= ~F_NUMERIC; 38599f13ae1SAlan Somers break; 3869ff95228SGleb Smirnoff case 'h': /* Packet size increment for ping sweep */ 38778e1f68eSMark Johnston ltmp = strtonum(optarg, 1, INT_MAX, &errstr); 38878e1f68eSMark Johnston if (errstr != NULL) { 38978e1f68eSMark Johnston errx(EX_USAGE, "invalid packet size: `%s'", 3909ff95228SGleb Smirnoff optarg); 39178e1f68eSMark Johnston } 39278e1f68eSMark Johnston sweepincr = (int)ltmp; 39378e1f68eSMark Johnston if (uid != 0 && sweepincr > DEFDATALEN) { 39478e1f68eSMark Johnston errc(EX_NOPERM, EPERM, 39578e1f68eSMark Johnston "packet size too large: %d > %u", 39678e1f68eSMark Johnston sweepincr, DEFDATALEN); 3979ff95228SGleb Smirnoff } 3989ff95228SGleb Smirnoff options |= F_SWEEP; 3999ff95228SGleb Smirnoff break; 4001f6a4631SRuslan Ermilov case 'I': /* multicast interface */ 4011f6a4631SRuslan Ermilov if (inet_aton(optarg, &ifaddr) == 0) 4021f6a4631SRuslan Ermilov errx(EX_USAGE, 4031f6a4631SRuslan Ermilov "invalid multicast interface: `%s'", 4041f6a4631SRuslan Ermilov optarg); 4051f6a4631SRuslan Ermilov options |= F_MIF; 4061f6a4631SRuslan Ermilov break; 4078fae3551SRodney W. Grimes case 'i': /* wait between sending packets */ 4081ad0b1beSMaxim Konovalov t = strtod(optarg, &ep) * 1000.0; 4091ad0b1beSMaxim Konovalov if (*ep || ep == optarg || t > (double)INT_MAX) 4101ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid timing interval: `%s'", 4111ad0b1beSMaxim Konovalov optarg); 4128fae3551SRodney W. Grimes options |= F_INTERVAL; 413526f06b2SMatthew Dillon interval = (int)t; 414526f06b2SMatthew Dillon if (uid && interval < 1000) { 415526f06b2SMatthew Dillon errno = EPERM; 416526f06b2SMatthew Dillon err(EX_NOPERM, "-i interval too short"); 417526f06b2SMatthew Dillon } 4188fae3551SRodney W. Grimes break; 4191f6a4631SRuslan Ermilov case 'L': 4201f6a4631SRuslan Ermilov options |= F_NOLOOP; 4211f6a4631SRuslan Ermilov loop = 0; 42285456935SBill Fenner break; 4238fae3551SRodney W. Grimes case 'l': 42478e1f68eSMark Johnston ltmp = strtonum(optarg, 0, INT_MAX, &errstr); 42578e1f68eSMark Johnston if (errstr != NULL) 42643470e3bSGarrett Wollman errx(EX_USAGE, 42743470e3bSGarrett Wollman "invalid preload value: `%s'", optarg); 4285e2cc0f4SStephen McKay if (uid) { 429f78ac61bSWarner Losh errno = EPERM; 430f78ac61bSWarner Losh err(EX_NOPERM, "-l flag"); 431f78ac61bSWarner Losh } 43278e1f68eSMark Johnston preload = (int)ltmp; 4338fae3551SRodney W. Grimes break; 4341f6a4631SRuslan Ermilov case 'M': 435eb1543c6SMatthew N. Dodd switch(optarg[0]) { 436eb1543c6SMatthew N. Dodd case 'M': 437eb1543c6SMatthew N. Dodd case 'm': 4381f6a4631SRuslan Ermilov options |= F_MASK; 43985456935SBill Fenner break; 440eb1543c6SMatthew N. Dodd case 'T': 441eb1543c6SMatthew N. Dodd case 't': 442eb1543c6SMatthew N. Dodd options |= F_TIME; 443eb1543c6SMatthew N. Dodd break; 444eb1543c6SMatthew N. Dodd default: 445eb1543c6SMatthew N. Dodd errx(EX_USAGE, "invalid message: `%c'", optarg[0]); 446eb1543c6SMatthew N. Dodd break; 447eb1543c6SMatthew N. Dodd } 448eb1543c6SMatthew N. Dodd break; 449211bfbd2SRuslan Ermilov case 'm': /* TTL */ 45078e1f68eSMark Johnston ltmp = strtonum(optarg, 0, MAXTTL, &errstr); 45178e1f68eSMark Johnston if (errstr != NULL) 4521ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid TTL: `%s'", optarg); 45378e1f68eSMark Johnston ttl = (int)ltmp; 454211bfbd2SRuslan Ermilov options |= F_TTL; 455211bfbd2SRuslan Ermilov break; 4568fae3551SRodney W. Grimes case 'n': 4578fae3551SRodney W. Grimes options |= F_NUMERIC; 4588fae3551SRodney W. Grimes break; 4598025c44bSDima Dorfman case 'o': 4608025c44bSDima Dorfman options |= F_ONCE; 4618025c44bSDima Dorfman break; 4621f6a4631SRuslan Ermilov #ifdef IPSEC 4631f6a4631SRuslan Ermilov #ifdef IPSEC_POLICY_IPSEC 4641f6a4631SRuslan Ermilov case 'P': 4651f6a4631SRuslan Ermilov options |= F_POLICY; 4661f6a4631SRuslan Ermilov if (!strncmp("in", optarg, 2)) 4671f6a4631SRuslan Ermilov policy_in = strdup(optarg); 4681f6a4631SRuslan Ermilov else if (!strncmp("out", optarg, 3)) 4691f6a4631SRuslan Ermilov policy_out = strdup(optarg); 4701f6a4631SRuslan Ermilov else 4711f6a4631SRuslan Ermilov errx(1, "invalid security policy"); 4721f6a4631SRuslan Ermilov break; 4731f6a4631SRuslan Ermilov #endif /*IPSEC_POLICY_IPSEC*/ 4741f6a4631SRuslan Ermilov #endif /*IPSEC*/ 4758fae3551SRodney W. Grimes case 'p': /* fill buffer with user pattern */ 4768fae3551SRodney W. Grimes options |= F_PINGFILLED; 477d074d39fSMatthew N. Dodd payload = optarg; 4788fae3551SRodney W. Grimes break; 479ef9e6dc7SBill Fenner case 'Q': 480ef9e6dc7SBill Fenner options |= F_QUIET2; 481ef9e6dc7SBill Fenner break; 4828fae3551SRodney W. Grimes case 'q': 4838fae3551SRodney W. Grimes options |= F_QUIET; 4848fae3551SRodney W. Grimes break; 4858fae3551SRodney W. Grimes case 'R': 4868fae3551SRodney W. Grimes options |= F_RROUTE; 4878fae3551SRodney W. Grimes break; 4888fae3551SRodney W. Grimes case 'r': 4898fae3551SRodney W. Grimes options |= F_SO_DONTROUTE; 4908fae3551SRodney W. Grimes break; 4911f6a4631SRuslan Ermilov case 'S': 4921f6a4631SRuslan Ermilov source = optarg; 4931f6a4631SRuslan Ermilov break; 4948fae3551SRodney W. Grimes case 's': /* size of packet to send */ 49578e1f68eSMark Johnston ltmp = strtonum(optarg, 0, INT_MAX, &errstr); 49678e1f68eSMark Johnston if (errstr != NULL) 49743470e3bSGarrett Wollman errx(EX_USAGE, "invalid packet size: `%s'", 49843470e3bSGarrett Wollman optarg); 49978e1f68eSMark Johnston datalen = (int)ltmp; 50078e1f68eSMark Johnston if (uid != 0 && datalen > DEFDATALEN) { 501fb7d32c7SMaxim Konovalov errno = EPERM; 502fb7d32c7SMaxim Konovalov err(EX_NOPERM, 50378e1f68eSMark Johnston "packet size too large: %d > %u", 50478e1f68eSMark Johnston datalen, DEFDATALEN); 505fb7d32c7SMaxim Konovalov } 5068fae3551SRodney W. Grimes break; 5071f6a4631SRuslan Ermilov case 'T': /* multicast TTL */ 50878e1f68eSMark Johnston ltmp = strtonum(optarg, 0, MAXTTL, &errstr); 50978e1f68eSMark Johnston if (errstr != NULL) 5101f6a4631SRuslan Ermilov errx(EX_USAGE, "invalid multicast TTL: `%s'", 5111f6a4631SRuslan Ermilov optarg); 51278e1f68eSMark Johnston mttl = (unsigned char)ltmp; 5131f6a4631SRuslan Ermilov options |= F_MTTL; 51499490edeSWarner Losh break; 5157237fd94SBill Fumerola case 't': 516bf113f1bSBill Fumerola alarmtimeout = strtoul(optarg, &ep, 0); 517bf113f1bSBill Fumerola if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX)) 5187237fd94SBill Fumerola errx(EX_USAGE, "invalid timeout: `%s'", 5197237fd94SBill Fumerola optarg); 520bf113f1bSBill Fumerola if (alarmtimeout > MAXALARM) 521bf113f1bSBill Fumerola errx(EX_USAGE, "invalid timeout: `%s' > %d", 522bf113f1bSBill Fumerola optarg, MAXALARM); 5232eb6acc2SAlan Somers { 5242eb6acc2SAlan Somers struct itimerval itv; 5252eb6acc2SAlan Somers 5262eb6acc2SAlan Somers timerclear(&itv.it_interval); 5272eb6acc2SAlan Somers timerclear(&itv.it_value); 5282eb6acc2SAlan Somers itv.it_value.tv_sec = (time_t)alarmtimeout; 5292eb6acc2SAlan Somers if (setitimer(ITIMER_REAL, &itv, NULL) != 0) 5302eb6acc2SAlan Somers err(1, "setitimer"); 5312eb6acc2SAlan Somers } 5327237fd94SBill Fumerola break; 5338fae3551SRodney W. Grimes case 'v': 5348fae3551SRodney W. Grimes options |= F_VERBOSE; 5358fae3551SRodney W. Grimes break; 536d6cd1497SGleb Smirnoff case 'W': /* wait ms for answer */ 537d6cd1497SGleb Smirnoff t = strtod(optarg, &ep); 538d6cd1497SGleb Smirnoff if (*ep || ep == optarg || t > (double)INT_MAX) 539d6cd1497SGleb Smirnoff errx(EX_USAGE, "invalid timing interval: `%s'", 540d6cd1497SGleb Smirnoff optarg); 541d6cd1497SGleb Smirnoff options |= F_WAITTIME; 542d6cd1497SGleb Smirnoff waittime = (int)t; 543d6cd1497SGleb Smirnoff break; 5440b2f8b3fSMaxim Konovalov case 'z': 5450b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 546c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 547c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > MAXTOS || ltmp < 0) 5480b2f8b3fSMaxim Konovalov errx(EX_USAGE, "invalid TOS: `%s'", optarg); 549c0a3773aSEugene Grosbein tos = ltmp; 5500b2f8b3fSMaxim Konovalov break; 5518fae3551SRodney W. Grimes default: 552e345a80dSPhilippe Charnier usage(); 55343470e3bSGarrett Wollman } 55443470e3bSGarrett Wollman } 55543470e3bSGarrett Wollman 55643470e3bSGarrett Wollman if (argc - optind != 1) 557e345a80dSPhilippe Charnier usage(); 55843470e3bSGarrett Wollman target = argv[optind]; 5598fae3551SRodney W. Grimes 560d829c3dfSMatthew N. Dodd switch (options & (F_MASK|F_TIME)) { 561d829c3dfSMatthew N. Dodd case 0: break; 562d829c3dfSMatthew N. Dodd case F_MASK: 563eb1543c6SMatthew N. Dodd icmp_type = ICMP_MASKREQ; 564eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_MASKREPLY; 565d829c3dfSMatthew N. Dodd phdr_len = MASK_LEN; 566eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 567eb1543c6SMatthew N. Dodd (void)printf("ICMP_MASKREQ\n"); 568d829c3dfSMatthew N. Dodd break; 569d829c3dfSMatthew N. Dodd case F_TIME: 570eb1543c6SMatthew N. Dodd icmp_type = ICMP_TSTAMP; 571eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_TSTAMPREPLY; 572d829c3dfSMatthew N. Dodd phdr_len = TS_LEN; 573eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 574eb1543c6SMatthew N. Dodd (void)printf("ICMP_TSTAMP\n"); 575d829c3dfSMatthew N. Dodd break; 576d829c3dfSMatthew N. Dodd default: 577d829c3dfSMatthew N. Dodd errx(EX_USAGE, "ICMP_TSTAMP and ICMP_MASKREQ are exclusive."); 578d829c3dfSMatthew N. Dodd break; 579eb1543c6SMatthew N. Dodd } 5800fe0c0ccSMaxim Konovalov icmp_len = sizeof(struct ip) + ICMP_MINLEN + phdr_len; 581fb7d32c7SMaxim Konovalov if (options & F_RROUTE) 582e88178ddSMaxim Konovalov icmp_len += MAX_IPOPTLEN; 583e88178ddSMaxim Konovalov maxpayload = IP_MAXPACKET - icmp_len; 584fb7d32c7SMaxim Konovalov if (datalen > maxpayload) 5851104dd84SBruce Evans errx(EX_USAGE, "packet size too large: %d > %d", datalen, 586fb7d32c7SMaxim Konovalov maxpayload); 587e88178ddSMaxim Konovalov send_len = icmp_len + datalen; 5880fe0c0ccSMaxim Konovalov datap = &outpack[ICMP_MINLEN + phdr_len + TIMEVAL_LEN]; 589d074d39fSMatthew N. Dodd if (options & F_PINGFILLED) { 590d074d39fSMatthew N. Dodd fill((char *)datap, payload); 591d074d39fSMatthew N. Dodd } 59249133c6dSPawel Jakub Dawidek capdns = capdns_setup(); 59399490edeSWarner Losh if (source) { 59431eac03bSSean Chittenden bzero((char *)&sock_in, sizeof(sock_in)); 59531eac03bSSean Chittenden sock_in.sin_family = AF_INET; 59631eac03bSSean Chittenden if (inet_aton(source, &sock_in.sin_addr) != 0) { 59799490edeSWarner Losh shostname = source; 59899490edeSWarner Losh } else { 599d68e2c04SMariusz Zaborski hp = cap_gethostbyname2(capdns, source, AF_INET); 60099490edeSWarner Losh if (!hp) 60199490edeSWarner Losh errx(EX_NOHOST, "cannot resolve %s: %s", 60299490edeSWarner Losh source, hstrerror(h_errno)); 60399490edeSWarner Losh 60431eac03bSSean Chittenden sock_in.sin_len = sizeof sock_in; 60531eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(sock_in.sin_addr) || 6064fba6582SMaxim Konovalov hp->h_length < 0) 60799490edeSWarner Losh errx(1, "gethostbyname2: illegal address"); 60831eac03bSSean Chittenden memcpy(&sock_in.sin_addr, hp->h_addr_list[0], 60931eac03bSSean Chittenden sizeof(sock_in.sin_addr)); 61099490edeSWarner Losh (void)strncpy(snamebuf, hp->h_name, 61199490edeSWarner Losh sizeof(snamebuf) - 1); 61299490edeSWarner Losh snamebuf[sizeof(snamebuf) - 1] = '\0'; 61399490edeSWarner Losh shostname = snamebuf; 61499490edeSWarner Losh } 61549133c6dSPawel Jakub Dawidek if (bind(ssend, (struct sockaddr *)&sock_in, sizeof sock_in) == 61649133c6dSPawel Jakub Dawidek -1) 61799490edeSWarner Losh err(1, "bind"); 61899490edeSWarner Losh } 61999490edeSWarner Losh 620d389e86aSMatt Jacob bzero(&whereto, sizeof(whereto)); 621d389e86aSMatt Jacob to = &whereto; 6228fae3551SRodney W. Grimes to->sin_family = AF_INET; 623d389e86aSMatt Jacob to->sin_len = sizeof *to; 62443470e3bSGarrett Wollman if (inet_aton(target, &to->sin_addr) != 0) { 6258fae3551SRodney W. Grimes hostname = target; 62643470e3bSGarrett Wollman } else { 62749133c6dSPawel Jakub Dawidek hp = cap_gethostbyname2(capdns, target, AF_INET); 62843470e3bSGarrett Wollman if (!hp) 62943470e3bSGarrett Wollman errx(EX_NOHOST, "cannot resolve %s: %s", 63043470e3bSGarrett Wollman target, hstrerror(h_errno)); 63143470e3bSGarrett Wollman 63231eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(to->sin_addr)) 6331ffae4a6SWarner Losh errx(1, "gethostbyname2 returned an illegal address"); 63443470e3bSGarrett Wollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 6358fae3551SRodney W. Grimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 636b10d9d5fSWarner Losh hnamebuf[sizeof(hnamebuf) - 1] = '\0'; 6378fae3551SRodney W. Grimes hostname = hnamebuf; 6388fae3551SRodney W. Grimes } 6398fae3551SRodney W. Grimes 64049133c6dSPawel Jakub Dawidek /* From now on we will use only reverse DNS lookups. */ 641a3ce7698SAlan Somers #ifdef WITH_CASPER 64249133c6dSPawel Jakub Dawidek if (capdns != NULL) { 64349133c6dSPawel Jakub Dawidek const char *types[1]; 64449133c6dSPawel Jakub Dawidek 645752d135eSMariusz Zaborski types[0] = "ADDR2NAME"; 64649133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdns, types, 1) < 0) 64749133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 64849133c6dSPawel Jakub Dawidek } 649a3ce7698SAlan Somers #endif 65049133c6dSPawel Jakub Dawidek if (connect(ssend, (struct sockaddr *)&whereto, sizeof(whereto)) != 0) 65149133c6dSPawel Jakub Dawidek err(1, "connect"); 65249133c6dSPawel Jakub Dawidek 65343470e3bSGarrett Wollman if (options & F_FLOOD && options & F_INTERVAL) 65443470e3bSGarrett Wollman errx(EX_USAGE, "-f and -i: incompatible options"); 65543470e3bSGarrett Wollman 65643470e3bSGarrett Wollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 65743470e3bSGarrett Wollman errx(EX_USAGE, 65843470e3bSGarrett Wollman "-f flag cannot be used with multicast destination"); 65943470e3bSGarrett Wollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 66043470e3bSGarrett Wollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 66143470e3bSGarrett Wollman errx(EX_USAGE, 66243470e3bSGarrett Wollman "-I, -L, -T flags cannot be used with unicast destination"); 6638fae3551SRodney W. Grimes 664d829c3dfSMatthew N. Dodd if (datalen >= TIMEVAL_LEN) /* can we time transfer */ 6658fae3551SRodney W. Grimes timing = 1; 66643470e3bSGarrett Wollman 66778e1f68eSMark Johnston if ((options & (F_PINGFILLED | F_SWEEP)) == 0) 668d829c3dfSMatthew N. Dodd for (i = TIMEVAL_LEN; i < datalen; ++i) 6698fae3551SRodney W. Grimes *datap++ = i; 6708fae3551SRodney W. Grimes 6718fae3551SRodney W. Grimes ident = getpid() & 0xFFFF; 6728fae3551SRodney W. Grimes 6738fae3551SRodney W. Grimes hold = 1; 67449133c6dSPawel Jakub Dawidek if (options & F_SO_DEBUG) { 67549133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DEBUG, (char *)&hold, 6768fae3551SRodney W. Grimes sizeof(hold)); 67749133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_DEBUG, (char *)&hold, 67849133c6dSPawel Jakub Dawidek sizeof(hold)); 67949133c6dSPawel Jakub Dawidek } 6808fae3551SRodney W. Grimes if (options & F_SO_DONTROUTE) 68149133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 6828fae3551SRodney W. Grimes sizeof(hold)); 68381a6f4c7SRichard Scheffenegger if (options & F_IP_VLAN_PCP) { 68481a6f4c7SRichard Scheffenegger (void)setsockopt(ssend, IPPROTO_IP, IP_VLAN_PCP, (char *)&pcp, 68581a6f4c7SRichard Scheffenegger sizeof(pcp)); 68681a6f4c7SRichard Scheffenegger } 6879a4365d0SYoshinobu Inoue #ifdef IPSEC 6889a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 6899a4365d0SYoshinobu Inoue if (options & F_POLICY) { 6909a4365d0SYoshinobu Inoue char *buf; 6919a4365d0SYoshinobu Inoue if (policy_in != NULL) { 6929a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_in, strlen(policy_in)); 6939a4365d0SYoshinobu Inoue if (buf == NULL) 694ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 69549133c6dSPawel Jakub Dawidek if (setsockopt(srecv, IPPROTO_IP, IP_IPSEC_POLICY, 6969a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 6971ad0b1beSMaxim Konovalov err(EX_CONFIG, 6981ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 6999a4365d0SYoshinobu Inoue free(buf); 7009a4365d0SYoshinobu Inoue } 7019a4365d0SYoshinobu Inoue 7029a4365d0SYoshinobu Inoue if (policy_out != NULL) { 7039a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_out, strlen(policy_out)); 7049a4365d0SYoshinobu Inoue if (buf == NULL) 705ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 70649133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_IPSEC_POLICY, 7079a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 7081ad0b1beSMaxim Konovalov err(EX_CONFIG, 7091ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 7109a4365d0SYoshinobu Inoue free(buf); 7119a4365d0SYoshinobu Inoue } 7129a4365d0SYoshinobu Inoue } 7139a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 7149a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 7158fae3551SRodney W. Grimes 7160b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 717d9cacf60SAlan Somers struct ip ip; 718d9cacf60SAlan Somers 719d9cacf60SAlan Somers memcpy(&ip, outpackhdr, sizeof(ip)); 7200b2f8b3fSMaxim Konovalov if (!(options & (F_TTL | F_MTTL))) { 7210b2f8b3fSMaxim Konovalov mib[0] = CTL_NET; 7220b2f8b3fSMaxim Konovalov mib[1] = PF_INET; 7230b2f8b3fSMaxim Konovalov mib[2] = IPPROTO_IP; 7240b2f8b3fSMaxim Konovalov mib[3] = IPCTL_DEFTTL; 7250b2f8b3fSMaxim Konovalov sz = sizeof(ttl); 7260b2f8b3fSMaxim Konovalov if (sysctl(mib, 4, &ttl, &sz, NULL, 0) == -1) 7270b2f8b3fSMaxim Konovalov err(1, "sysctl(net.inet.ip.ttl)"); 7280b2f8b3fSMaxim Konovalov } 72949133c6dSPawel Jakub Dawidek setsockopt(ssend, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold)); 730d9cacf60SAlan Somers ip.ip_v = IPVERSION; 731d9cacf60SAlan Somers ip.ip_hl = sizeof(struct ip) >> 2; 732d9cacf60SAlan Somers ip.ip_tos = tos; 733d9cacf60SAlan Somers ip.ip_id = 0; 734d9cacf60SAlan Somers ip.ip_off = htons(df ? IP_DF : 0); 735d9cacf60SAlan Somers ip.ip_ttl = ttl; 736d9cacf60SAlan Somers ip.ip_p = IPPROTO_ICMP; 737d9cacf60SAlan Somers ip.ip_src.s_addr = source ? sock_in.sin_addr.s_addr : INADDR_ANY; 738d9cacf60SAlan Somers ip.ip_dst = to->sin_addr; 739d9cacf60SAlan Somers memcpy(outpackhdr, &ip, sizeof(ip)); 7400b2f8b3fSMaxim Konovalov } 74149133c6dSPawel Jakub Dawidek 74249133c6dSPawel Jakub Dawidek /* 74349133c6dSPawel Jakub Dawidek * Here we enter capability mode. Further down access to global 74449133c6dSPawel Jakub Dawidek * namespaces (e.g filesystem) is restricted (see capsicum(4)). 74549133c6dSPawel Jakub Dawidek * We must connect(2) our socket before this point. 74649133c6dSPawel Jakub Dawidek */ 7477bdc3291SMark Johnston caph_cache_catpages(); 74818fcfaa4SMark Johnston if (caph_enter_casper() < 0) 749301bc9f9SAlan Somers err(1, "caph_enter_casper"); 75049133c6dSPawel Jakub Dawidek 75149133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT, CAP_SETSOCKOPT); 7527bdc3291SMark Johnston if (caph_rights_limit(srecv, &rights) < 0) 75349133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv"); 75449133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND, CAP_SETSOCKOPT); 7557bdc3291SMark Johnston if (caph_rights_limit(ssend, &rights) < 0) 75649133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend"); 75749133c6dSPawel Jakub Dawidek 7588fae3551SRodney W. Grimes /* record route option */ 7598fae3551SRodney W. Grimes if (options & F_RROUTE) { 7608fae3551SRodney W. Grimes #ifdef IP_OPTIONS 761039d6aa4SBill Fenner bzero(rspace, sizeof(rspace)); 7628fae3551SRodney W. Grimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 7638fae3551SRodney W. Grimes rspace[IPOPT_OLEN] = sizeof(rspace) - 1; 7648fae3551SRodney W. Grimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 765039d6aa4SBill Fenner rspace[sizeof(rspace) - 1] = IPOPT_EOL; 76649133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_OPTIONS, rspace, 76743470e3bSGarrett Wollman sizeof(rspace)) < 0) 76843470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 7698fae3551SRodney W. Grimes #else 77043470e3bSGarrett Wollman errx(EX_UNAVAILABLE, 77143470e3bSGarrett Wollman "record route not available in this implementation"); 7728fae3551SRodney W. Grimes #endif /* IP_OPTIONS */ 7738fae3551SRodney W. Grimes } 7748fae3551SRodney W. Grimes 775211bfbd2SRuslan Ermilov if (options & F_TTL) { 77649133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_TTL, &ttl, 777211bfbd2SRuslan Ermilov sizeof(ttl)) < 0) { 778211bfbd2SRuslan Ermilov err(EX_OSERR, "setsockopt IP_TTL"); 779211bfbd2SRuslan Ermilov } 780211bfbd2SRuslan Ermilov } 78185456935SBill Fenner if (options & F_NOLOOP) { 78249133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 78385456935SBill Fenner sizeof(loop)) < 0) { 78443470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 78585456935SBill Fenner } 78685456935SBill Fenner } 78785456935SBill Fenner if (options & F_MTTL) { 78849133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_TTL, &mttl, 789211bfbd2SRuslan Ermilov sizeof(mttl)) < 0) { 79043470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 79185456935SBill Fenner } 79285456935SBill Fenner } 79385456935SBill Fenner if (options & F_MIF) { 79449133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 79585456935SBill Fenner sizeof(ifaddr)) < 0) { 79643470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 79785456935SBill Fenner } 79885456935SBill Fenner } 799039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 80084633ef1SAlan Somers { 80184633ef1SAlan Somers int on = 1; 80284633ef1SAlan Somers int ts_clock = SO_TS_MONOTONIC; 80384633ef1SAlan Somers if (setsockopt(srecv, SOL_SOCKET, SO_TIMESTAMP, &on, 80484633ef1SAlan Somers sizeof(on)) < 0) 805039d6aa4SBill Fenner err(EX_OSERR, "setsockopt SO_TIMESTAMP"); 80684633ef1SAlan Somers if (setsockopt(srecv, SOL_SOCKET, SO_TS_CLOCK, &ts_clock, 80784633ef1SAlan Somers sizeof(ts_clock)) < 0) 80884633ef1SAlan Somers err(EX_OSERR, "setsockopt SO_TS_CLOCK"); 809039d6aa4SBill Fenner } 810039d6aa4SBill Fenner #endif 8119ff95228SGleb Smirnoff if (sweepmax) { 812bb7dfe5eSGleb Smirnoff if (sweepmin > sweepmax) 81378e1f68eSMark Johnston errx(EX_USAGE, 81478e1f68eSMark Johnston "Maximum packet size must be no less than the minimum packet size"); 81578e1f68eSMark Johnston 81678e1f68eSMark Johnston if (sweepmax > maxpayload - TIMEVAL_LEN) 81778e1f68eSMark Johnston errx(EX_USAGE, "Invalid sweep maximum"); 8189ff95228SGleb Smirnoff 8199ff95228SGleb Smirnoff if (datalen != DEFDATALEN) 82078e1f68eSMark Johnston errx(EX_USAGE, 82178e1f68eSMark Johnston "Packet size and ping sweep are mutually exclusive"); 8229ff95228SGleb Smirnoff 8239ff95228SGleb Smirnoff if (npackets > 0) { 8249ff95228SGleb Smirnoff snpackets = npackets; 8259ff95228SGleb Smirnoff npackets = 0; 8269ff95228SGleb Smirnoff } else 8279ff95228SGleb Smirnoff snpackets = 1; 8289ff95228SGleb Smirnoff datalen = sweepmin; 8299ff95228SGleb Smirnoff send_len = icmp_len + sweepmin; 8309ff95228SGleb Smirnoff } 8319ff95228SGleb Smirnoff if (options & F_SWEEP && !sweepmax) 8329ff95228SGleb Smirnoff errx(EX_USAGE, "Maximum sweep size must be specified"); 83385456935SBill Fenner 8348fae3551SRodney W. Grimes /* 8358fae3551SRodney W. Grimes * When pinging the broadcast address, you can get a lot of answers. 8368fae3551SRodney W. Grimes * Doing something so evil is useful if you are trying to stress the 8378fae3551SRodney W. Grimes * ethernet, or just want to fill the arp cache to get some stuff for 83843470e3bSGarrett Wollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 83943470e3bSGarrett Wollman * or multicast pings if they wish. 8408fae3551SRodney W. Grimes */ 8414fba6582SMaxim Konovalov 8424fba6582SMaxim Konovalov /* 8434fba6582SMaxim Konovalov * XXX receive buffer needs undetermined space for mbuf overhead 8444fba6582SMaxim Konovalov * as well. 8454fba6582SMaxim Konovalov */ 8464fba6582SMaxim Konovalov hold = IP_MAXPACKET + 128; 84749133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 8488fae3551SRodney W. Grimes sizeof(hold)); 84949133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 85049133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT); 8517bdc3291SMark Johnston if (caph_rights_limit(srecv, &rights) < 0) 85249133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv setsockopt"); 853261e59bbSMaxim Konovalov if (uid == 0) 85449133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_SNDBUF, (char *)&hold, 855e8bd25ceSRobert Watson sizeof(hold)); 85649133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 85749133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND); 8587bdc3291SMark Johnston if (caph_rights_limit(ssend, &rights) < 0) 85949133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend setsockopt"); 860e8bd25ceSRobert Watson 86199490edeSWarner Losh if (to->sin_family == AF_INET) { 86299490edeSWarner Losh (void)printf("PING %s (%s)", hostname, 86399490edeSWarner Losh inet_ntoa(to->sin_addr)); 86499490edeSWarner Losh if (source) 86599490edeSWarner Losh (void)printf(" from %s", shostname); 8669ff95228SGleb Smirnoff if (sweepmax) 8679ff95228SGleb Smirnoff (void)printf(": (%d ... %d) data bytes\n", 8689ff95228SGleb Smirnoff sweepmin, sweepmax); 8699ff95228SGleb Smirnoff else 87099490edeSWarner Losh (void)printf(": %d data bytes\n", datalen); 8719ff95228SGleb Smirnoff 8729ff95228SGleb Smirnoff } else { 8739ff95228SGleb Smirnoff if (sweepmax) 8749ff95228SGleb Smirnoff (void)printf("PING %s: (%d ... %d) data bytes\n", 8759ff95228SGleb Smirnoff hostname, sweepmin, sweepmax); 8769ff95228SGleb Smirnoff else 8778fae3551SRodney W. Grimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 8789ff95228SGleb Smirnoff } 8798fae3551SRodney W. Grimes 8807d81b35cSBruce Evans /* 881a2a00888SSean Eric Fagan * Use sigaction() instead of signal() to get unambiguous semantics, 882a2a00888SSean Eric Fagan * in particular with SA_RESTART not set. 8837d81b35cSBruce Evans */ 884a2a00888SSean Eric Fagan 885f6bd468bSPaul Traina sigemptyset(&si_sa.sa_mask); 88637e5b2c6SSean Eric Fagan si_sa.sa_flags = 0; 887a2a00888SSean Eric Fagan 888a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 889a2a00888SSean Eric Fagan if (sigaction(SIGINT, &si_sa, 0) == -1) { 890a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGINT"); 891a2a00888SSean Eric Fagan } 892a2a00888SSean Eric Fagan 893a2a00888SSean Eric Fagan si_sa.sa_handler = status; 89437e5b2c6SSean Eric Fagan if (sigaction(SIGINFO, &si_sa, 0) == -1) { 895624ff938SWarner Losh err(EX_OSERR, "sigaction"); 89637e5b2c6SSean Eric Fagan } 897bf113f1bSBill Fumerola 898bf113f1bSBill Fumerola if (alarmtimeout > 0) { 8997237fd94SBill Fumerola si_sa.sa_handler = stopit; 9007237fd94SBill Fumerola if (sigaction(SIGALRM, &si_sa, 0) == -1) 9017237fd94SBill Fumerola err(EX_OSERR, "sigaction SIGALRM"); 902bf113f1bSBill Fumerola } 90337e5b2c6SSean Eric Fagan 904039d6aa4SBill Fenner bzero(&msg, sizeof(msg)); 905039d6aa4SBill Fenner msg.msg_name = (caddr_t)&from; 906039d6aa4SBill Fenner msg.msg_iov = &iov; 907039d6aa4SBill Fenner msg.msg_iovlen = 1; 908039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 909039d6aa4SBill Fenner msg.msg_control = (caddr_t)ctrl; 91067511a4cSAlan Somers msg.msg_controllen = sizeof(ctrl); 911039d6aa4SBill Fenner #endif 912039d6aa4SBill Fenner iov.iov_base = packet; 913e88178ddSMaxim Konovalov iov.iov_len = IP_MAXPACKET; 914039d6aa4SBill Fenner 91532af342fSRuslan Ermilov if (preload == 0) 91632af342fSRuslan Ermilov pinger(); /* send the first ping */ 91732af342fSRuslan Ermilov else { 91832af342fSRuslan Ermilov if (npackets != 0 && preload > npackets) 91932af342fSRuslan Ermilov preload = npackets; 9208fae3551SRodney W. Grimes while (preload--) /* fire off them quickies */ 9218fae3551SRodney W. Grimes pinger(); 92232af342fSRuslan Ermilov } 9231ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &last); 9248fae3551SRodney W. Grimes 925039d6aa4SBill Fenner if (options & F_FLOOD) { 926039d6aa4SBill Fenner intvl.tv_sec = 0; 9271ad76f1bSAlan Somers intvl.tv_nsec = 10000000; 928039d6aa4SBill Fenner } else { 929526f06b2SMatthew Dillon intvl.tv_sec = interval / 1000; 9301ad76f1bSAlan Somers intvl.tv_nsec = interval % 1000 * 1000000; 931039d6aa4SBill Fenner } 932039d6aa4SBill Fenner 933261e59bbSMaxim Konovalov almost_done = 0; 9348f975bb3SBruce Evans while (!finish_up) { 9351ad76f1bSAlan Somers struct timespec now, timeout; 936039d6aa4SBill Fenner fd_set rfds; 937d9cacf60SAlan Somers int n; 938d9cacf60SAlan Somers ssize_t cc; 9398fae3551SRodney W. Grimes 9407d81b35cSBruce Evans check_status(); 94149133c6dSPawel Jakub Dawidek if ((unsigned)srecv >= FD_SETSIZE) 9427e5bbd68SJacques Vidrine errx(EX_OSERR, "descriptor too large"); 943039d6aa4SBill Fenner FD_ZERO(&rfds); 94449133c6dSPawel Jakub Dawidek FD_SET(srecv, &rfds); 9451ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 9461ad76f1bSAlan Somers timespecadd(&last, &intvl, &timeout); 9471ad76f1bSAlan Somers timespecsub(&timeout, &now, &timeout); 948039d6aa4SBill Fenner if (timeout.tv_sec < 0) 9491ad76f1bSAlan Somers timespecclear(&timeout); 9501ad76f1bSAlan Somers n = pselect(srecv + 1, &rfds, NULL, NULL, &timeout, NULL); 9515e2cc0f4SStephen McKay if (n < 0) 9525e2cc0f4SStephen McKay continue; /* Must be EINTR. */ 953039d6aa4SBill Fenner if (n == 1) { 9541ad76f1bSAlan Somers struct timespec *tv = NULL; 955039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 95667511a4cSAlan Somers struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); 957039d6aa4SBill Fenner #endif 958039d6aa4SBill Fenner msg.msg_namelen = sizeof(from); 95949133c6dSPawel Jakub Dawidek if ((cc = recvmsg(srecv, &msg, 0)) < 0) { 9608fae3551SRodney W. Grimes if (errno == EINTR) 9618fae3551SRodney W. Grimes continue; 962e345a80dSPhilippe Charnier warn("recvmsg"); 9638fae3551SRodney W. Grimes continue; 9648fae3551SRodney W. Grimes } 96546d7b45aSTom Jones /* If we have a 0 byte read from recvfrom continue */ 96646d7b45aSTom Jones if (cc == 0) 96746d7b45aSTom Jones continue; 968039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 969b17fb992SAlan Somers if (cmsg != NULL && 970b17fb992SAlan Somers cmsg->cmsg_level == SOL_SOCKET && 971039d6aa4SBill Fenner cmsg->cmsg_type == SCM_TIMESTAMP && 97231eac03bSSean Chittenden cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) { 973fa05a94cSJohn Birrell /* Copy to avoid alignment problems: */ 974fa05a94cSJohn Birrell memcpy(&now, CMSG_DATA(cmsg), sizeof(now)); 97531eac03bSSean Chittenden tv = &now; 976fa05a94cSJohn Birrell } 977039d6aa4SBill Fenner #endif 97831eac03bSSean Chittenden if (tv == NULL) { 9791ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 98031eac03bSSean Chittenden tv = &now; 981039d6aa4SBill Fenner } 98231eac03bSSean Chittenden pr_pack((char *)packet, cc, &from, tv); 98331eac03bSSean Chittenden if ((options & F_ONCE && nreceived) || 98431eac03bSSean Chittenden (npackets && nreceived >= npackets)) 9858fae3551SRodney W. Grimes break; 9868fae3551SRodney W. Grimes } 9875e2cc0f4SStephen McKay if (n == 0 || options & F_FLOOD) { 9889ff95228SGleb Smirnoff if (sweepmax && sntransmitted == snpackets) { 98978e1f68eSMark Johnston if (datalen + sweepincr > sweepmax) 99078e1f68eSMark Johnston break; 99178e1f68eSMark Johnston for (i = 0; i < sweepincr; i++) 9929ff95228SGleb Smirnoff *datap++ = i; 9939ff95228SGleb Smirnoff datalen += sweepincr; 9949ff95228SGleb Smirnoff send_len = icmp_len + datalen; 9959ff95228SGleb Smirnoff sntransmitted = 0; 9969ff95228SGleb Smirnoff } 997039d6aa4SBill Fenner if (!npackets || ntransmitted < npackets) 998039d6aa4SBill Fenner pinger(); 999039d6aa4SBill Fenner else { 1000039d6aa4SBill Fenner if (almost_done) 1001039d6aa4SBill Fenner break; 1002039d6aa4SBill Fenner almost_done = 1; 10031ad76f1bSAlan Somers intvl.tv_nsec = 0; 1004039d6aa4SBill Fenner if (nreceived) { 1005039d6aa4SBill Fenner intvl.tv_sec = 2 * tmax / 1000; 1006039d6aa4SBill Fenner if (!intvl.tv_sec) 1007039d6aa4SBill Fenner intvl.tv_sec = 1; 1008d6cd1497SGleb Smirnoff } else { 1009d6cd1497SGleb Smirnoff intvl.tv_sec = waittime / 1000; 10101ad76f1bSAlan Somers intvl.tv_nsec = waittime % 1000 * 1000000; 1011d6cd1497SGleb Smirnoff } 1012039d6aa4SBill Fenner } 10131ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &last); 101425107197SIan Dowse if (ntransmitted - nreceived - 1 > nmissedmax) { 101525107197SIan Dowse nmissedmax = ntransmitted - nreceived - 1; 101625107197SIan Dowse if (options & F_MISSED) 1017ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 1018039d6aa4SBill Fenner } 1019039d6aa4SBill Fenner } 102025107197SIan Dowse } 10218f975bb3SBruce Evans finish(); 10228fae3551SRodney W. Grimes /* NOTREACHED */ 1023f78ac61bSWarner Losh exit(0); /* Make the compiler happy */ 10248fae3551SRodney W. Grimes } 10258fae3551SRodney W. Grimes 10268fae3551SRodney W. Grimes /* 10278f975bb3SBruce Evans * stopit -- 10288f975bb3SBruce Evans * Set the global bit that causes the main loop to quit. 10298f975bb3SBruce Evans * Do NOT call finish() from here, since finish() does far too much 10308f975bb3SBruce Evans * to be called from a signal handler. 1031515dd2faSJulian Elischer */ 1032515dd2faSJulian Elischer void 1033fafb8f11SEd Schouten stopit(int sig __unused) 1034515dd2faSJulian Elischer { 1035efc8588dSDavid E. O'Brien 1036c8bb99e5SIan Dowse /* 1037c8bb99e5SIan Dowse * When doing reverse DNS lookups, the finish_up flag might not 1038c8bb99e5SIan Dowse * be noticed for a while. Just exit if we get a second SIGINT. 1039c8bb99e5SIan Dowse */ 1040c8bb99e5SIan Dowse if (!(options & F_NUMERIC) && finish_up) 1041c8bb99e5SIan Dowse _exit(nreceived ? 0 : 2); 1042515dd2faSJulian Elischer finish_up = 1; 1043515dd2faSJulian Elischer } 1044515dd2faSJulian Elischer 1045515dd2faSJulian Elischer /* 10468fae3551SRodney W. Grimes * pinger -- 10478fae3551SRodney W. Grimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 10488fae3551SRodney W. Grimes * will be added on by the kernel. The ID field is our UNIX process ID, 1049eb1543c6SMatthew N. Dodd * and the sequence number is an ascending integer. The first TIMEVAL_LEN 10501ad76f1bSAlan Somers * bytes of the data portion are used to hold a UNIX "timespec" struct in 10514fba6582SMaxim Konovalov * host byte-order, to compute the round-trip time. 10528fae3551SRodney W. Grimes */ 105343470e3bSGarrett Wollman static void 105443470e3bSGarrett Wollman pinger(void) 10558fae3551SRodney W. Grimes { 10561ad76f1bSAlan Somers struct timespec now; 105713e3f0b7SMaxim Konovalov struct tv32 tv32; 1058d9cacf60SAlan Somers struct icmp icp; 1059efc8588dSDavid E. O'Brien int cc, i; 10600b2f8b3fSMaxim Konovalov u_char *packet; 10618fae3551SRodney W. Grimes 10620b2f8b3fSMaxim Konovalov packet = outpack; 1063d9cacf60SAlan Somers memcpy(&icp, outpack, ICMP_MINLEN + phdr_len); 1064d9cacf60SAlan Somers icp.icmp_type = icmp_type; 1065d9cacf60SAlan Somers icp.icmp_code = 0; 1066d9cacf60SAlan Somers icp.icmp_cksum = 0; 1067d9cacf60SAlan Somers icp.icmp_seq = htons(ntransmitted); 1068d9cacf60SAlan Somers icp.icmp_id = ident; /* ID */ 10698fae3551SRodney W. Grimes 10705db89bc7SBill Fenner CLR(ntransmitted % mx_dup_ck); 10718fae3551SRodney W. Grimes 1072eb1543c6SMatthew N. Dodd if ((options & F_TIME) || timing) { 10731ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 10741ad76f1bSAlan Somers /* 10751ad76f1bSAlan Somers * Truncate seconds down to 32 bits in order 10761ad76f1bSAlan Somers * to fit the timestamp within 8 bytes of the 10771ad76f1bSAlan Somers * packet. We're only concerned with 10781ad76f1bSAlan Somers * durations, not absolute times. 10791ad76f1bSAlan Somers */ 10801ad76f1bSAlan Somers tv32.tv32_sec = (uint32_t)htonl(now.tv_sec); 10811ad76f1bSAlan Somers tv32.tv32_nsec = (uint32_t)htonl(now.tv_nsec); 1082eb1543c6SMatthew N. Dodd if (options & F_TIME) 1083d9cacf60SAlan Somers icp.icmp_otime = htonl((now.tv_sec % (24*60*60)) 10841ad76f1bSAlan Somers * 1000 + now.tv_nsec / 1000000); 1085eb1543c6SMatthew N. Dodd if (timing) 108613e3f0b7SMaxim Konovalov bcopy((void *)&tv32, 10870fe0c0ccSMaxim Konovalov (void *)&outpack[ICMP_MINLEN + phdr_len], 108813e3f0b7SMaxim Konovalov sizeof(tv32)); 1089eb1543c6SMatthew N. Dodd } 1090eb1543c6SMatthew N. Dodd 1091d9cacf60SAlan Somers memcpy(outpack, &icp, ICMP_MINLEN + phdr_len); 1092d9cacf60SAlan Somers 10930fe0c0ccSMaxim Konovalov cc = ICMP_MINLEN + phdr_len + datalen; 10948fae3551SRodney W. Grimes 10958fae3551SRodney W. Grimes /* compute ICMP checksum here */ 1096d9cacf60SAlan Somers icp.icmp_cksum = in_cksum(outpack, cc); 1097d9cacf60SAlan Somers /* Update icmp_cksum in the raw packet data buffer. */ 1098d9cacf60SAlan Somers memcpy(outpack + offsetof(struct icmp, icmp_cksum), &icp.icmp_cksum, 1099d9cacf60SAlan Somers sizeof(icp.icmp_cksum)); 11008fae3551SRodney W. Grimes 11010b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 1102d9cacf60SAlan Somers struct ip ip; 1103d9cacf60SAlan Somers 11040b2f8b3fSMaxim Konovalov cc += sizeof(struct ip); 1105d9cacf60SAlan Somers ip.ip_len = htons(cc); 1106d9cacf60SAlan Somers /* Update ip_len in the raw packet data buffer. */ 1107d9cacf60SAlan Somers memcpy(outpackhdr + offsetof(struct ip, ip_len), &ip.ip_len, 1108d9cacf60SAlan Somers sizeof(ip.ip_len)); 1109d9cacf60SAlan Somers ip.ip_sum = in_cksum(outpackhdr, cc); 1110d9cacf60SAlan Somers /* Update ip_sum in the raw packet data buffer. */ 1111d9cacf60SAlan Somers memcpy(outpackhdr + offsetof(struct ip, ip_sum), &ip.ip_sum, 1112d9cacf60SAlan Somers sizeof(ip.ip_sum)); 11130b2f8b3fSMaxim Konovalov packet = outpackhdr; 11140b2f8b3fSMaxim Konovalov } 111549133c6dSPawel Jakub Dawidek i = send(ssend, (char *)packet, cc, 0); 11168fae3551SRodney W. Grimes if (i < 0 || i != cc) { 111743470e3bSGarrett Wollman if (i < 0) { 11188f975bb3SBruce Evans if (options & F_FLOOD && errno == ENOBUFS) { 1119515dd2faSJulian Elischer usleep(FLOOD_BACKOFF); 1120515dd2faSJulian Elischer return; 1121515dd2faSJulian Elischer } 112243470e3bSGarrett Wollman warn("sendto"); 112343470e3bSGarrett Wollman } else { 112443470e3bSGarrett Wollman warn("%s: partial write: %d of %d bytes", 1125416aa49bSPoul-Henning Kamp hostname, i, cc); 11268fae3551SRodney W. Grimes } 1127363d7bbeSJulian Elischer } 1128363d7bbeSJulian Elischer ntransmitted++; 11299ff95228SGleb Smirnoff sntransmitted++; 1130d399eb3eSPiotr Pawel Stefaniak if (!(options & F_QUIET) && options & F_DOT) 1131d399eb3eSPiotr Pawel Stefaniak (void)write(STDOUT_FILENO, &DOT[DOTidx++ % DOTlen], 1); 11328fae3551SRodney W. Grimes } 11338fae3551SRodney W. Grimes 11348fae3551SRodney W. Grimes /* 11358fae3551SRodney W. Grimes * pr_pack -- 11368fae3551SRodney W. Grimes * Print out the packet, if it came from us. This logic is necessary 11378fae3551SRodney W. Grimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 11388fae3551SRodney W. Grimes * which arrive ('tis only fair). This permits multiple copies of this 11398fae3551SRodney W. Grimes * program to be run without having intermingled output (or statistics!). 11408fae3551SRodney W. Grimes */ 114143470e3bSGarrett Wollman static void 1142d9cacf60SAlan Somers pr_pack(char *buf, ssize_t cc, struct sockaddr_in *from, struct timespec *tv) 11438fae3551SRodney W. Grimes { 11449b219646SPeter Wemm struct in_addr ina; 1145d9cacf60SAlan Somers u_char *cp, *dp, l; 1146d9cacf60SAlan Somers struct icmp icp; 1147d9cacf60SAlan Somers struct ip ip; 1148d9cacf60SAlan Somers const u_char *icmp_data_raw; 114946d7b45aSTom Jones ssize_t icmp_data_raw_len; 11508f975bb3SBruce Evans double triptime; 115146d7b45aSTom Jones int dupflag, i, j, recv_len; 115270960bb8SCy Schubert int8_t hlen; 11532c29d74cSAlan Somers uint16_t seq; 1154efc8588dSDavid E. O'Brien static int old_rrlen; 1155efc8588dSDavid E. O'Brien static char old_rr[MAX_IPOPTLEN]; 1156d9cacf60SAlan Somers struct ip oip; 1157d9cacf60SAlan Somers u_char oip_header_len; 1158d9cacf60SAlan Somers struct icmp oicmp; 1159d9cacf60SAlan Somers 1160d9cacf60SAlan Somers /* 116146d7b45aSTom Jones * Get size of IP header of the received packet. 116246d7b45aSTom Jones * The header length is contained in the lower four bits of the first 116346d7b45aSTom Jones * byte and represents the number of 4 byte octets the header takes up. 116446d7b45aSTom Jones * 116546d7b45aSTom Jones * The IHL minimum value is 5 (20 bytes) and its maximum value is 15 116646d7b45aSTom Jones * (60 bytes). 1167d9cacf60SAlan Somers */ 1168d9cacf60SAlan Somers memcpy(&l, buf, sizeof(l)); 1169d9cacf60SAlan Somers hlen = (l & 0x0f) << 2; 11708fae3551SRodney W. Grimes 117146d7b45aSTom Jones /* Reject IP packets with a short header */ 117270960bb8SCy Schubert if (hlen < (int8_t) sizeof(struct ip)) { 117346d7b45aSTom Jones if (options & F_VERBOSE) 117446d7b45aSTom Jones warn("IHL too short (%d bytes) from %s", hlen, 117546d7b45aSTom Jones inet_ntoa(from->sin_addr)); 117646d7b45aSTom Jones return; 117746d7b45aSTom Jones } 117846d7b45aSTom Jones 117946d7b45aSTom Jones memcpy(&ip, buf, sizeof(struct ip)); 118046d7b45aSTom Jones 118146d7b45aSTom Jones /* Check packet has enough data to carry a valid ICMP header */ 1182e88178ddSMaxim Konovalov recv_len = cc; 11838fae3551SRodney W. Grimes if (cc < hlen + ICMP_MINLEN) { 11848fae3551SRodney W. Grimes if (options & F_VERBOSE) 1185d9cacf60SAlan Somers warn("packet too short (%zd bytes) from %s", cc, 118643470e3bSGarrett Wollman inet_ntoa(from->sin_addr)); 11878fae3551SRodney W. Grimes return; 11888fae3551SRodney W. Grimes } 11898fae3551SRodney W. Grimes 119046d7b45aSTom Jones icmp_data_raw_len = cc - (hlen + offsetof(struct icmp, icmp_data)); 1191d9cacf60SAlan Somers icmp_data_raw = buf + hlen + offsetof(struct icmp, icmp_data); 1192d9cacf60SAlan Somers 11938fae3551SRodney W. Grimes /* Now the ICMP part */ 11948fae3551SRodney W. Grimes cc -= hlen; 1195d9cacf60SAlan Somers memcpy(&icp, buf + hlen, MIN((ssize_t)sizeof(icp), cc)); 1196d9cacf60SAlan Somers if (icp.icmp_type == icmp_type_rsp) { 1197d9cacf60SAlan Somers if (icp.icmp_id != ident) 11988fae3551SRodney W. Grimes return; /* 'Twas not our ECHO */ 11998fae3551SRodney W. Grimes ++nreceived; 12008f975bb3SBruce Evans triptime = 0.0; 12018fae3551SRodney W. Grimes if (timing) { 12021ad76f1bSAlan Somers struct timespec tv1; 120313e3f0b7SMaxim Konovalov struct tv32 tv32; 1204d9cacf60SAlan Somers const u_char *tp; 1205d9cacf60SAlan Somers 1206d9cacf60SAlan Somers tp = icmp_data_raw + phdr_len; 1207143008a1SMatthew N. Dodd 12087e9489e0SHiroki Sato if ((size_t)(cc - ICMP_MINLEN - phdr_len) >= 12097e9489e0SHiroki Sato sizeof(tv1)) { 12109d2b0ab8SPeter Wemm /* Copy to avoid alignment problems: */ 121113e3f0b7SMaxim Konovalov memcpy(&tv32, tp, sizeof(tv32)); 121213e3f0b7SMaxim Konovalov tv1.tv_sec = ntohl(tv32.tv32_sec); 12131ad76f1bSAlan Somers tv1.tv_nsec = ntohl(tv32.tv32_nsec); 12141ad76f1bSAlan Somers timespecsub(tv, &tv1, tv); 1215039d6aa4SBill Fenner triptime = ((double)tv->tv_sec) * 1000.0 + 12161ad76f1bSAlan Somers ((double)tv->tv_nsec) / 1000000.0; 12178fae3551SRodney W. Grimes tsum += triptime; 12183109a910SGarrett Wollman tsumsq += triptime * triptime; 12198fae3551SRodney W. Grimes if (triptime < tmin) 12208fae3551SRodney W. Grimes tmin = triptime; 12218fae3551SRodney W. Grimes if (triptime > tmax) 12228fae3551SRodney W. Grimes tmax = triptime; 122347e9b3eaSMatthew N. Dodd } else 122447e9b3eaSMatthew N. Dodd timing = 0; 12258fae3551SRodney W. Grimes } 12268fae3551SRodney W. Grimes 1227d9cacf60SAlan Somers seq = ntohs(icp.icmp_seq); 12285db89bc7SBill Fenner 12295db89bc7SBill Fenner if (TST(seq % mx_dup_ck)) { 12308fae3551SRodney W. Grimes ++nrepeats; 12318fae3551SRodney W. Grimes --nreceived; 12328fae3551SRodney W. Grimes dupflag = 1; 12338fae3551SRodney W. Grimes } else { 12345db89bc7SBill Fenner SET(seq % mx_dup_ck); 12358fae3551SRodney W. Grimes dupflag = 0; 12368fae3551SRodney W. Grimes } 12378fae3551SRodney W. Grimes 12388fae3551SRodney W. Grimes if (options & F_QUIET) 12398fae3551SRodney W. Grimes return; 12408fae3551SRodney W. Grimes 1241d6cd1497SGleb Smirnoff if (options & F_WAITTIME && triptime > waittime) { 1242d6cd1497SGleb Smirnoff ++nrcvtimeout; 1243d6cd1497SGleb Smirnoff return; 1244d6cd1497SGleb Smirnoff } 1245d6cd1497SGleb Smirnoff 1246d399eb3eSPiotr Pawel Stefaniak if (options & F_DOT) 12478fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &BSPACE, 1); 12488fae3551SRodney W. Grimes else { 1249d9cacf60SAlan Somers (void)printf("%zd bytes from %s: icmp_seq=%u", cc, 1250229e8bf2SAlan Somers pr_addr(from->sin_addr), seq); 1251d9cacf60SAlan Somers (void)printf(" ttl=%d", ip.ip_ttl); 12528fae3551SRodney W. Grimes if (timing) 1253d410b6f1SDavid Greenman (void)printf(" time=%.3f ms", triptime); 12548fae3551SRodney W. Grimes if (dupflag) 12558fae3551SRodney W. Grimes (void)printf(" (DUP!)"); 1256772dfa72SDaniel O'Callaghan if (options & F_AUDIBLE) 1257ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 1258143008a1SMatthew N. Dodd if (options & F_MASK) { 1259143008a1SMatthew N. Dodd /* Just prentend this cast isn't ugly */ 1260143008a1SMatthew N. Dodd (void)printf(" mask=%s", 1261d9cacf60SAlan Somers inet_ntoa(*(struct in_addr *)&(icp.icmp_mask))); 1262143008a1SMatthew N. Dodd } 1263eb1543c6SMatthew N. Dodd if (options & F_TIME) { 1264d9cacf60SAlan Somers (void)printf(" tso=%s", pr_ntime(icp.icmp_otime)); 1265d9cacf60SAlan Somers (void)printf(" tsr=%s", pr_ntime(icp.icmp_rtime)); 1266d9cacf60SAlan Somers (void)printf(" tst=%s", pr_ntime(icp.icmp_ttime)); 1267eb1543c6SMatthew N. Dodd } 1268e88178ddSMaxim Konovalov if (recv_len != send_len) { 1269e88178ddSMaxim Konovalov (void)printf( 1270e88178ddSMaxim Konovalov "\nwrong total length %d instead of %d", 1271e88178ddSMaxim Konovalov recv_len, send_len); 1272e88178ddSMaxim Konovalov } 12738fae3551SRodney W. Grimes /* check the data */ 1274d9cacf60SAlan Somers cp = (u_char*)(buf + hlen + offsetof(struct icmp, 1275d9cacf60SAlan Somers icmp_data) + phdr_len); 12760fe0c0ccSMaxim Konovalov dp = &outpack[ICMP_MINLEN + phdr_len]; 127747e9b3eaSMatthew N. Dodd cc -= ICMP_MINLEN + phdr_len; 127829dccd6aSMaxim Konovalov i = 0; 127929dccd6aSMaxim Konovalov if (timing) { /* don't check variable timestamp */ 128029dccd6aSMaxim Konovalov cp += TIMEVAL_LEN; 128129dccd6aSMaxim Konovalov dp += TIMEVAL_LEN; 128229dccd6aSMaxim Konovalov cc -= TIMEVAL_LEN; 128329dccd6aSMaxim Konovalov i += TIMEVAL_LEN; 128429dccd6aSMaxim Konovalov } 128529dccd6aSMaxim Konovalov for (; i < datalen && cc > 0; ++i, ++cp, ++dp, --cc) { 12868fae3551SRodney W. Grimes if (*cp != *dp) { 12878fae3551SRodney W. Grimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 12888fae3551SRodney W. Grimes i, *dp, *cp); 12891ad0b1beSMaxim Konovalov (void)printf("\ncp:"); 1290d9cacf60SAlan Somers cp = (u_char*)(buf + hlen + 1291d9cacf60SAlan Somers offsetof(struct icmp, icmp_data)); 1292d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1293e88178ddSMaxim Konovalov if ((i % 16) == 8) 1294d32ff037SJohn Birrell (void)printf("\n\t"); 1295e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 1296d32ff037SJohn Birrell } 12971ad0b1beSMaxim Konovalov (void)printf("\ndp:"); 12980fe0c0ccSMaxim Konovalov cp = &outpack[ICMP_MINLEN]; 1299d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1300e88178ddSMaxim Konovalov if ((i % 16) == 8) 13018fae3551SRodney W. Grimes (void)printf("\n\t"); 1302e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 13038fae3551SRodney W. Grimes } 13048fae3551SRodney W. Grimes break; 13058fae3551SRodney W. Grimes } 13068fae3551SRodney W. Grimes } 13078fae3551SRodney W. Grimes } 13088fae3551SRodney W. Grimes } else { 1309ef9e6dc7SBill Fenner /* 1310ef9e6dc7SBill Fenner * We've got something other than an ECHOREPLY. 1311ef9e6dc7SBill Fenner * See if it's a reply to something that we sent. 1312ef9e6dc7SBill Fenner * We can compare IP destination, protocol, 1313ef9e6dc7SBill Fenner * and ICMP type and ID. 1314f78ac61bSWarner Losh * 1315f78ac61bSWarner Losh * Only print all the error messages if we are running 1316f78ac61bSWarner Losh * as root to avoid leaking information not normally 1317f78ac61bSWarner Losh * available to those not running as root. 1318ef9e6dc7SBill Fenner */ 131946d7b45aSTom Jones 132046d7b45aSTom Jones /* 132146d7b45aSTom Jones * If we don't have enough bytes for a quoted IP header and an 132246d7b45aSTom Jones * ICMP header then stop. 132346d7b45aSTom Jones */ 132446d7b45aSTom Jones if (icmp_data_raw_len < 132546d7b45aSTom Jones (ssize_t)(sizeof(struct ip) + sizeof(struct icmp))) { 132646d7b45aSTom Jones if (options & F_VERBOSE) 132746d7b45aSTom Jones warnx("quoted data too short (%zd bytes) from %s", 132846d7b45aSTom Jones icmp_data_raw_len, inet_ntoa(from->sin_addr)); 132946d7b45aSTom Jones return; 133046d7b45aSTom Jones } 133146d7b45aSTom Jones 1332d9cacf60SAlan Somers memcpy(&oip_header_len, icmp_data_raw, sizeof(oip_header_len)); 1333d9cacf60SAlan Somers oip_header_len = (oip_header_len & 0x0f) << 2; 133446d7b45aSTom Jones 133546d7b45aSTom Jones /* Reject IP packets with a short header */ 133646d7b45aSTom Jones if (oip_header_len < sizeof(struct ip)) { 133746d7b45aSTom Jones if (options & F_VERBOSE) 133846d7b45aSTom Jones warnx("inner IHL too short (%d bytes) from %s", 133946d7b45aSTom Jones oip_header_len, inet_ntoa(from->sin_addr)); 134046d7b45aSTom Jones return; 134146d7b45aSTom Jones } 134246d7b45aSTom Jones 134346d7b45aSTom Jones /* 134446d7b45aSTom Jones * Check against the actual IHL length, to protect against 134546d7b45aSTom Jones * quoated packets carrying IP options. 134646d7b45aSTom Jones */ 134746d7b45aSTom Jones if (icmp_data_raw_len < 134846d7b45aSTom Jones (ssize_t)(oip_header_len + sizeof(struct icmp))) { 134946d7b45aSTom Jones if (options & F_VERBOSE) 135046d7b45aSTom Jones warnx("inner packet too short (%zd bytes) from %s", 135146d7b45aSTom Jones icmp_data_raw_len, inet_ntoa(from->sin_addr)); 135246d7b45aSTom Jones return; 135346d7b45aSTom Jones } 135446d7b45aSTom Jones 135546d7b45aSTom Jones memcpy(&oip, icmp_data_raw, sizeof(struct ip)); 1356ef9e6dc7SBill Fenner 1357ee2bf734SWarner Losh if (((options & F_VERBOSE) && uid == 0) || 1358ef9e6dc7SBill Fenner (!(options & F_QUIET2) && 1359d9cacf60SAlan Somers (oip.ip_dst.s_addr == whereto.sin_addr.s_addr) && 1360d9cacf60SAlan Somers (oip.ip_p == IPPROTO_ICMP) && 1361d9cacf60SAlan Somers (oicmp.icmp_type == ICMP_ECHO) && 1362d9cacf60SAlan Somers (oicmp.icmp_id == ident))) { 1363d9cacf60SAlan Somers (void)printf("%zd bytes from %s: ", cc, 136443470e3bSGarrett Wollman pr_addr(from->sin_addr)); 136520b41303SJose Luis Duran pr_icmph(&icp, &oip, icmp_data_raw); 1366ef9e6dc7SBill Fenner } else 1367ef9e6dc7SBill Fenner return; 13688fae3551SRodney W. Grimes } 13698fae3551SRodney W. Grimes 13708fae3551SRodney W. Grimes /* Display any IP options */ 13718fae3551SRodney W. Grimes cp = (u_char *)buf + sizeof(struct ip); 13728fae3551SRodney W. Grimes 13738fae3551SRodney W. Grimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 13748fae3551SRodney W. Grimes switch (*cp) { 13758fae3551SRodney W. Grimes case IPOPT_EOL: 13768fae3551SRodney W. Grimes hlen = 0; 13778fae3551SRodney W. Grimes break; 13788fae3551SRodney W. Grimes case IPOPT_LSRR: 1379cb75aca7SMaxim Konovalov case IPOPT_SSRR: 1380cb75aca7SMaxim Konovalov (void)printf(*cp == IPOPT_LSRR ? 1381cb75aca7SMaxim Konovalov "\nLSRR: " : "\nSSRR: "); 1382301207dfSMaxim Konovalov j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1; 13838fae3551SRodney W. Grimes hlen -= 2; 1384301207dfSMaxim Konovalov cp += 2; 13853c721ab3SMaxim Konovalov if (j >= INADDR_LEN && 13863c721ab3SMaxim Konovalov j <= hlen - (int)sizeof(struct ip)) { 13878fae3551SRodney W. Grimes for (;;) { 1388301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1389301207dfSMaxim Konovalov if (ina.s_addr == 0) 13901ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1391301207dfSMaxim Konovalov else 13921ad0b1beSMaxim Konovalov (void)printf("\t%s", 13931ad0b1beSMaxim Konovalov pr_addr(ina)); 1394301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1395301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1396301207dfSMaxim Konovalov j -= INADDR_LEN; 1397301207dfSMaxim Konovalov if (j < INADDR_LEN) 13988fae3551SRodney W. Grimes break; 13998fae3551SRodney W. Grimes (void)putchar('\n'); 14008fae3551SRodney W. Grimes } 1401301207dfSMaxim Konovalov } else 1402301207dfSMaxim Konovalov (void)printf("\t(truncated route)\n"); 14038fae3551SRodney W. Grimes break; 14048fae3551SRodney W. Grimes case IPOPT_RR: 1405301207dfSMaxim Konovalov j = cp[IPOPT_OLEN]; /* get length */ 1406301207dfSMaxim Konovalov i = cp[IPOPT_OFFSET]; /* and pointer */ 14078fae3551SRodney W. Grimes hlen -= 2; 1408301207dfSMaxim Konovalov cp += 2; 14098fae3551SRodney W. Grimes if (i > j) 14108fae3551SRodney W. Grimes i = j; 1411301207dfSMaxim Konovalov i = i - IPOPT_MINOFF + 1; 1412301207dfSMaxim Konovalov if (i < 0 || i > (hlen - (int)sizeof(struct ip))) { 1413301207dfSMaxim Konovalov old_rrlen = 0; 14148fae3551SRodney W. Grimes continue; 1415301207dfSMaxim Konovalov } 14168fae3551SRodney W. Grimes if (i == old_rrlen 14178fae3551SRodney W. Grimes && !bcmp((char *)cp, old_rr, i) 1418d399eb3eSPiotr Pawel Stefaniak && !(options & F_DOT)) { 14198fae3551SRodney W. Grimes (void)printf("\t(same route)"); 14208fae3551SRodney W. Grimes hlen -= i; 14218fae3551SRodney W. Grimes cp += i; 14228fae3551SRodney W. Grimes break; 14238fae3551SRodney W. Grimes } 14248fae3551SRodney W. Grimes old_rrlen = i; 14258fae3551SRodney W. Grimes bcopy((char *)cp, old_rr, i); 14268fae3551SRodney W. Grimes (void)printf("\nRR: "); 1427301207dfSMaxim Konovalov if (i >= INADDR_LEN && 1428301207dfSMaxim Konovalov i <= hlen - (int)sizeof(struct ip)) { 14298fae3551SRodney W. Grimes for (;;) { 1430301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1431301207dfSMaxim Konovalov if (ina.s_addr == 0) 14321ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1433301207dfSMaxim Konovalov else 1434301207dfSMaxim Konovalov (void)printf("\t%s", 1435301207dfSMaxim Konovalov pr_addr(ina)); 1436301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1437301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1438301207dfSMaxim Konovalov i -= INADDR_LEN; 1439301207dfSMaxim Konovalov if (i < INADDR_LEN) 14408fae3551SRodney W. Grimes break; 14418fae3551SRodney W. Grimes (void)putchar('\n'); 14428fae3551SRodney W. Grimes } 1443301207dfSMaxim Konovalov } else 1444301207dfSMaxim Konovalov (void)printf("\t(truncated route)"); 14458fae3551SRodney W. Grimes break; 14468fae3551SRodney W. Grimes case IPOPT_NOP: 14478fae3551SRodney W. Grimes (void)printf("\nNOP"); 14488fae3551SRodney W. Grimes break; 14498fae3551SRodney W. Grimes default: 14508fae3551SRodney W. Grimes (void)printf("\nunknown option %x", *cp); 14518fae3551SRodney W. Grimes break; 14528fae3551SRodney W. Grimes } 1453d399eb3eSPiotr Pawel Stefaniak if (!(options & F_DOT)) { 14548fae3551SRodney W. Grimes (void)putchar('\n'); 14558fae3551SRodney W. Grimes (void)fflush(stdout); 14568fae3551SRodney W. Grimes } 14578fae3551SRodney W. Grimes } 14588fae3551SRodney W. Grimes 14598fae3551SRodney W. Grimes /* 1460badd8138SSean Eric Fagan * status -- 1461badd8138SSean Eric Fagan * Print out statistics when SIGINFO is received. 1462badd8138SSean Eric Fagan */ 1463badd8138SSean Eric Fagan 146443470e3bSGarrett Wollman static void 1465fafb8f11SEd Schouten status(int sig __unused) 14667d81b35cSBruce Evans { 1467efc8588dSDavid E. O'Brien 146837e5b2c6SSean Eric Fagan siginfo_p = 1; 146937e5b2c6SSean Eric Fagan } 147037e5b2c6SSean Eric Fagan 147143470e3bSGarrett Wollman static void 1472fafb8f11SEd Schouten check_status(void) 1473badd8138SSean Eric Fagan { 1474efc8588dSDavid E. O'Brien 147537e5b2c6SSean Eric Fagan if (siginfo_p) { 147637e5b2c6SSean Eric Fagan siginfo_p = 0; 1477aa822c39SDima Dorfman (void)fprintf(stderr, "\r%ld/%ld packets received (%.1f%%)", 14787d81b35cSBruce Evans nreceived, ntransmitted, 1479aed98a27SMaxim Konovalov ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0); 1480aed98a27SMaxim Konovalov if (nreceived && timing) 1481aed98a27SMaxim Konovalov (void)fprintf(stderr, " %.3f min / %.3f avg / %.3f max", 1482aed98a27SMaxim Konovalov tmin, tsum / (nreceived + nrepeats), tmax); 1483aed98a27SMaxim Konovalov (void)fprintf(stderr, "\n"); 148437e5b2c6SSean Eric Fagan } 1485badd8138SSean Eric Fagan } 1486badd8138SSean Eric Fagan 1487badd8138SSean Eric Fagan /* 14888fae3551SRodney W. Grimes * finish -- 14898fae3551SRodney W. Grimes * Print out statistics, and give up. 14908fae3551SRodney W. Grimes */ 149143470e3bSGarrett Wollman static void 1492fafb8f11SEd Schouten finish(void) 14938fae3551SRodney W. Grimes { 14948fae3551SRodney W. Grimes 14958fae3551SRodney W. Grimes (void)signal(SIGINT, SIG_IGN); 1496a2a00888SSean Eric Fagan (void)signal(SIGALRM, SIG_IGN); 14978fae3551SRodney W. Grimes (void)putchar('\n'); 14988fae3551SRodney W. Grimes (void)fflush(stdout); 14998fae3551SRodney W. Grimes (void)printf("--- %s ping statistics ---\n", hostname); 15008fae3551SRodney W. Grimes (void)printf("%ld packets transmitted, ", ntransmitted); 15018fae3551SRodney W. Grimes (void)printf("%ld packets received, ", nreceived); 15028fae3551SRodney W. Grimes if (nrepeats) 15038fae3551SRodney W. Grimes (void)printf("+%ld duplicates, ", nrepeats); 1504ebe70c8fSWarner Losh if (ntransmitted) { 15058fae3551SRodney W. Grimes if (nreceived > ntransmitted) 15068fae3551SRodney W. Grimes (void)printf("-- somebody's printing up packets!"); 15078fae3551SRodney W. Grimes else 1508aa822c39SDima Dorfman (void)printf("%.1f%% packet loss", 1509aa822c39SDima Dorfman ((ntransmitted - nreceived) * 100.0) / 1510aa822c39SDima Dorfman ntransmitted); 1511ebe70c8fSWarner Losh } 1512d6cd1497SGleb Smirnoff if (nrcvtimeout) 1513d6cd1497SGleb Smirnoff (void)printf(", %ld packets out of wait time", nrcvtimeout); 15148fae3551SRodney W. Grimes (void)putchar('\n'); 15153109a910SGarrett Wollman if (nreceived && timing) { 15163109a910SGarrett Wollman double n = nreceived + nrepeats; 15173109a910SGarrett Wollman double avg = tsum / n; 15183109a910SGarrett Wollman double vari = tsumsq / n - avg * avg; 15191ad0b1beSMaxim Konovalov (void)printf( 15201ad0b1beSMaxim Konovalov "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", 15213109a910SGarrett Wollman tmin, avg, tmax, sqrt(vari)); 15223109a910SGarrett Wollman } 1523badd8138SSean Eric Fagan 15246e1173dcSDavid Greenman if (nreceived) 15258fae3551SRodney W. Grimes exit(0); 15266e1173dcSDavid Greenman else 15276e1173dcSDavid Greenman exit(2); 15288fae3551SRodney W. Grimes } 15298fae3551SRodney W. Grimes 15308fae3551SRodney W. Grimes /* 15318fae3551SRodney W. Grimes * pr_icmph -- 15328fae3551SRodney W. Grimes * Print a descriptive string about an ICMP header. 15338fae3551SRodney W. Grimes */ 153443470e3bSGarrett Wollman static void 1535d9cacf60SAlan Somers pr_icmph(struct icmp *icp, struct ip *oip, const u_char *const oicmp_raw) 15368fae3551SRodney W. Grimes { 1537efc8588dSDavid E. O'Brien 15388fae3551SRodney W. Grimes switch(icp->icmp_type) { 15398fae3551SRodney W. Grimes case ICMP_ECHOREPLY: 15408fae3551SRodney W. Grimes (void)printf("Echo Reply\n"); 15418fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 15428fae3551SRodney W. Grimes break; 15438fae3551SRodney W. Grimes case ICMP_UNREACH: 15448fae3551SRodney W. Grimes switch(icp->icmp_code) { 15458fae3551SRodney W. Grimes case ICMP_UNREACH_NET: 15468fae3551SRodney W. Grimes (void)printf("Destination Net Unreachable\n"); 15478fae3551SRodney W. Grimes break; 15488fae3551SRodney W. Grimes case ICMP_UNREACH_HOST: 15498fae3551SRodney W. Grimes (void)printf("Destination Host Unreachable\n"); 15508fae3551SRodney W. Grimes break; 15518fae3551SRodney W. Grimes case ICMP_UNREACH_PROTOCOL: 15528fae3551SRodney W. Grimes (void)printf("Destination Protocol Unreachable\n"); 15538fae3551SRodney W. Grimes break; 15548fae3551SRodney W. Grimes case ICMP_UNREACH_PORT: 15558fae3551SRodney W. Grimes (void)printf("Destination Port Unreachable\n"); 15568fae3551SRodney W. Grimes break; 15578fae3551SRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 1558ef9e6dc7SBill Fenner (void)printf("frag needed and DF set (MTU %d)\n", 1559ff49597eSBill Fenner ntohs(icp->icmp_nextmtu)); 15608fae3551SRodney W. Grimes break; 15618fae3551SRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 15628fae3551SRodney W. Grimes (void)printf("Source Route Failed\n"); 15638fae3551SRodney W. Grimes break; 1564ef9e6dc7SBill Fenner case ICMP_UNREACH_FILTER_PROHIB: 1565ef9e6dc7SBill Fenner (void)printf("Communication prohibited by filter\n"); 1566ef9e6dc7SBill Fenner break; 15678fae3551SRodney W. Grimes default: 15688fae3551SRodney W. Grimes (void)printf("Dest Unreachable, Bad Code: %d\n", 15698fae3551SRodney W. Grimes icp->icmp_code); 15708fae3551SRodney W. Grimes break; 15718fae3551SRodney W. Grimes } 15728fae3551SRodney W. Grimes /* Print returned IP header information */ 1573*1dc1f6bdSJose Luis Duran pr_iph(oip, oicmp_raw); 15748fae3551SRodney W. Grimes break; 15758fae3551SRodney W. Grimes case ICMP_SOURCEQUENCH: 15768fae3551SRodney W. Grimes (void)printf("Source Quench\n"); 1577*1dc1f6bdSJose Luis Duran pr_iph(oip, oicmp_raw); 15788fae3551SRodney W. Grimes break; 15798fae3551SRodney W. Grimes case ICMP_REDIRECT: 15808fae3551SRodney W. Grimes switch(icp->icmp_code) { 15818fae3551SRodney W. Grimes case ICMP_REDIRECT_NET: 15828fae3551SRodney W. Grimes (void)printf("Redirect Network"); 15838fae3551SRodney W. Grimes break; 15848fae3551SRodney W. Grimes case ICMP_REDIRECT_HOST: 15858fae3551SRodney W. Grimes (void)printf("Redirect Host"); 15868fae3551SRodney W. Grimes break; 15878fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSNET: 15888fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Network"); 15898fae3551SRodney W. Grimes break; 15908fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSHOST: 15918fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Host"); 15928fae3551SRodney W. Grimes break; 15938fae3551SRodney W. Grimes default: 15948fae3551SRodney W. Grimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 15958fae3551SRodney W. Grimes break; 15968fae3551SRodney W. Grimes } 1597ff49597eSBill Fenner (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 1598*1dc1f6bdSJose Luis Duran pr_iph(oip, oicmp_raw); 15998fae3551SRodney W. Grimes break; 16008fae3551SRodney W. Grimes case ICMP_ECHO: 16018fae3551SRodney W. Grimes (void)printf("Echo Request\n"); 16028fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 16038fae3551SRodney W. Grimes break; 16048fae3551SRodney W. Grimes case ICMP_TIMXCEED: 16058fae3551SRodney W. Grimes switch(icp->icmp_code) { 16068fae3551SRodney W. Grimes case ICMP_TIMXCEED_INTRANS: 16078fae3551SRodney W. Grimes (void)printf("Time to live exceeded\n"); 16088fae3551SRodney W. Grimes break; 16098fae3551SRodney W. Grimes case ICMP_TIMXCEED_REASS: 16108fae3551SRodney W. Grimes (void)printf("Frag reassembly time exceeded\n"); 16118fae3551SRodney W. Grimes break; 16128fae3551SRodney W. Grimes default: 16138fae3551SRodney W. Grimes (void)printf("Time exceeded, Bad Code: %d\n", 16148fae3551SRodney W. Grimes icp->icmp_code); 16158fae3551SRodney W. Grimes break; 16168fae3551SRodney W. Grimes } 1617*1dc1f6bdSJose Luis Duran pr_iph(oip, oicmp_raw); 16188fae3551SRodney W. Grimes break; 16198fae3551SRodney W. Grimes case ICMP_PARAMPROB: 16208fae3551SRodney W. Grimes (void)printf("Parameter problem: pointer = 0x%02x\n", 16218fae3551SRodney W. Grimes icp->icmp_hun.ih_pptr); 1622*1dc1f6bdSJose Luis Duran pr_iph(oip, oicmp_raw); 16238fae3551SRodney W. Grimes break; 16248fae3551SRodney W. Grimes case ICMP_TSTAMP: 16258fae3551SRodney W. Grimes (void)printf("Timestamp\n"); 16268fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 16278fae3551SRodney W. Grimes break; 16288fae3551SRodney W. Grimes case ICMP_TSTAMPREPLY: 16298fae3551SRodney W. Grimes (void)printf("Timestamp Reply\n"); 16308fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 16318fae3551SRodney W. Grimes break; 16328fae3551SRodney W. Grimes case ICMP_IREQ: 16338fae3551SRodney W. Grimes (void)printf("Information Request\n"); 16348fae3551SRodney W. Grimes /* XXX ID + Seq */ 16358fae3551SRodney W. Grimes break; 16368fae3551SRodney W. Grimes case ICMP_IREQREPLY: 16378fae3551SRodney W. Grimes (void)printf("Information Reply\n"); 16388fae3551SRodney W. Grimes /* XXX ID + Seq */ 16398fae3551SRodney W. Grimes break; 16408fae3551SRodney W. Grimes case ICMP_MASKREQ: 16418fae3551SRodney W. Grimes (void)printf("Address Mask Request\n"); 16428fae3551SRodney W. Grimes break; 16438fae3551SRodney W. Grimes case ICMP_MASKREPLY: 16448fae3551SRodney W. Grimes (void)printf("Address Mask Reply\n"); 16458fae3551SRodney W. Grimes break; 1646ef9e6dc7SBill Fenner case ICMP_ROUTERADVERT: 1647ef9e6dc7SBill Fenner (void)printf("Router Advertisement\n"); 1648ef9e6dc7SBill Fenner break; 1649ef9e6dc7SBill Fenner case ICMP_ROUTERSOLICIT: 1650ef9e6dc7SBill Fenner (void)printf("Router Solicitation\n"); 1651ef9e6dc7SBill Fenner break; 16528fae3551SRodney W. Grimes default: 16538fae3551SRodney W. Grimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 16548fae3551SRodney W. Grimes } 16558fae3551SRodney W. Grimes } 16568fae3551SRodney W. Grimes 16578fae3551SRodney W. Grimes /* 16588fae3551SRodney W. Grimes * pr_iph -- 16598fae3551SRodney W. Grimes * Print an IP header with options. 16608fae3551SRodney W. Grimes */ 166143470e3bSGarrett Wollman static void 166220b41303SJose Luis Duran pr_iph(struct ip *ip, const u_char *cp) 16638fae3551SRodney W. Grimes { 1664a94c074dSDimitry Andric struct in_addr ina; 1665efc8588dSDavid E. O'Brien int hlen; 16668fae3551SRodney W. Grimes 16678fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 166820b41303SJose Luis Duran cp = cp + sizeof(struct ip); /* point to options */ 16698fae3551SRodney W. Grimes 1670ef9e6dc7SBill Fenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 16718fae3551SRodney W. Grimes (void)printf(" %1x %1x %02x %04x %04x", 1672ef9e6dc7SBill Fenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1673ef9e6dc7SBill Fenner ntohs(ip->ip_id)); 16749185854dSJose Luis Duran (void)printf(" %1x %04x", 16759185854dSJose Luis Duran (ntohs(ip->ip_off) & 0xe000) >> 13, 16769185854dSJose Luis Duran ntohs(ip->ip_off) & 0x1fff); 1677ef9e6dc7SBill Fenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1678ef9e6dc7SBill Fenner ntohs(ip->ip_sum)); 1679a94c074dSDimitry Andric memcpy(&ina, &ip->ip_src.s_addr, sizeof ina); 1680a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1681a94c074dSDimitry Andric memcpy(&ina, &ip->ip_dst.s_addr, sizeof ina); 1682a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1683ef9e6dc7SBill Fenner /* dump any option bytes */ 1684491263d7SJose Luis Duran while (hlen-- > (int)sizeof(struct ip)) { 16858fae3551SRodney W. Grimes (void)printf("%02x", *cp++); 16868fae3551SRodney W. Grimes } 16878fae3551SRodney W. Grimes (void)putchar('\n'); 16888fae3551SRodney W. Grimes } 16898fae3551SRodney W. Grimes 16908fae3551SRodney W. Grimes /* 16918fae3551SRodney W. Grimes * pr_addr -- 16928fae3551SRodney W. Grimes * Return an ascii host address as a dotted quad and optionally with 16938fae3551SRodney W. Grimes * a hostname. 16948fae3551SRodney W. Grimes */ 169543470e3bSGarrett Wollman static char * 1696fafb8f11SEd Schouten pr_addr(struct in_addr ina) 16978fae3551SRodney W. Grimes { 16988fae3551SRodney W. Grimes struct hostent *hp; 1699f78ac61bSWarner Losh static char buf[16 + 3 + MAXHOSTNAMELEN]; 17008fae3551SRodney W. Grimes 170149133c6dSPawel Jakub Dawidek if (options & F_NUMERIC) 170243470e3bSGarrett Wollman return inet_ntoa(ina); 170349133c6dSPawel Jakub Dawidek 1704491263d7SJose Luis Duran hp = cap_gethostbyaddr(capdns, (char *)&ina, sizeof(ina), AF_INET); 170549133c6dSPawel Jakub Dawidek 170649133c6dSPawel Jakub Dawidek if (hp == NULL) 170749133c6dSPawel Jakub Dawidek return inet_ntoa(ina); 170849133c6dSPawel Jakub Dawidek 1709efa38539SPeter Wemm (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 171043470e3bSGarrett Wollman inet_ntoa(ina)); 17118fae3551SRodney W. Grimes return(buf); 17128fae3551SRodney W. Grimes } 17138fae3551SRodney W. Grimes 1714eb1543c6SMatthew N. Dodd static char * 1715007fe4e3SMaxim Konovalov pr_ntime(n_time timestamp) 1716eb1543c6SMatthew N. Dodd { 17177898770aSAlan Somers static char buf[11]; 1718007fe4e3SMaxim Konovalov int hour, min, sec; 1719eb1543c6SMatthew N. Dodd 1720007fe4e3SMaxim Konovalov sec = ntohl(timestamp) / 1000; 1721007fe4e3SMaxim Konovalov hour = sec / 60 / 60; 1722007fe4e3SMaxim Konovalov min = (sec % (60 * 60)) / 60; 1723007fe4e3SMaxim Konovalov sec = (sec % (60 * 60)) % 60; 1724eb1543c6SMatthew N. Dodd 1725007fe4e3SMaxim Konovalov (void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hour, min, sec); 1726eb1543c6SMatthew N. Dodd 1727eb1543c6SMatthew N. Dodd return (buf); 1728eb1543c6SMatthew N. Dodd } 1729eb1543c6SMatthew N. Dodd 173043470e3bSGarrett Wollman static void 1731fafb8f11SEd Schouten fill(char *bp, char *patp) 17328fae3551SRodney W. Grimes { 17338fae3551SRodney W. Grimes char *cp; 1734efc8588dSDavid E. O'Brien int pat[16]; 17354fba6582SMaxim Konovalov u_int ii, jj, kk; 17368fae3551SRodney W. Grimes 173743470e3bSGarrett Wollman for (cp = patp; *cp; cp++) { 173843470e3bSGarrett Wollman if (!isxdigit(*cp)) 173943470e3bSGarrett Wollman errx(EX_USAGE, 174043470e3bSGarrett Wollman "patterns must be specified as hex digits"); 174143470e3bSGarrett Wollman 17428fae3551SRodney W. Grimes } 17438fae3551SRodney W. Grimes ii = sscanf(patp, 17448fae3551SRodney W. Grimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 17458fae3551SRodney W. Grimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 17468fae3551SRodney W. Grimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 17478fae3551SRodney W. Grimes &pat[13], &pat[14], &pat[15]); 17488fae3551SRodney W. Grimes 17498fae3551SRodney W. Grimes if (ii > 0) 1750d829c3dfSMatthew N. Dodd for (kk = 0; kk <= maxpayload - (TIMEVAL_LEN + ii); kk += ii) 17518fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 17528fae3551SRodney W. Grimes bp[jj + kk] = pat[jj]; 17538fae3551SRodney W. Grimes if (!(options & F_QUIET)) { 17548fae3551SRodney W. Grimes (void)printf("PATTERN: 0x"); 17558fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 17568fae3551SRodney W. Grimes (void)printf("%02x", bp[jj] & 0xFF); 17578fae3551SRodney W. Grimes (void)printf("\n"); 17588fae3551SRodney W. Grimes } 17598fae3551SRodney W. Grimes } 17608fae3551SRodney W. Grimes 176149133c6dSPawel Jakub Dawidek static cap_channel_t * 176249133c6dSPawel Jakub Dawidek capdns_setup(void) 176349133c6dSPawel Jakub Dawidek { 176449133c6dSPawel Jakub Dawidek cap_channel_t *capcas, *capdnsloc; 1765a3ce7698SAlan Somers #ifdef WITH_CASPER 176649133c6dSPawel Jakub Dawidek const char *types[2]; 176749133c6dSPawel Jakub Dawidek int families[1]; 1768a3ce7698SAlan Somers #endif 176949133c6dSPawel Jakub Dawidek capcas = cap_init(); 1770c501d73cSMariusz Zaborski if (capcas == NULL) 1771c501d73cSMariusz Zaborski err(1, "unable to create casper process"); 177249133c6dSPawel Jakub Dawidek capdnsloc = cap_service_open(capcas, "system.dns"); 177349133c6dSPawel Jakub Dawidek /* Casper capability no longer needed. */ 177449133c6dSPawel Jakub Dawidek cap_close(capcas); 177549133c6dSPawel Jakub Dawidek if (capdnsloc == NULL) 177649133c6dSPawel Jakub Dawidek err(1, "unable to open system.dns service"); 1777a3ce7698SAlan Somers #ifdef WITH_CASPER 1778752d135eSMariusz Zaborski types[0] = "NAME2ADDR"; 1779752d135eSMariusz Zaborski types[1] = "ADDR2NAME"; 178049133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdnsloc, types, 2) < 0) 178149133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 178249133c6dSPawel Jakub Dawidek families[0] = AF_INET; 178349133c6dSPawel Jakub Dawidek if (cap_dns_family_limit(capdnsloc, families, 1) < 0) 178449133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 1785a3ce7698SAlan Somers #endif 178649133c6dSPawel Jakub Dawidek return (capdnsloc); 178749133c6dSPawel Jakub Dawidek } 1788