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> 478fae3551SRodney W. Grimes /* 488fae3551SRodney W. Grimes * P I N G . C 498fae3551SRodney W. Grimes * 50e345a80dSPhilippe Charnier * Using the Internet Control Message Protocol (ICMP) "ECHO" facility, 518fae3551SRodney W. Grimes * measure round-trip-delays and packet loss across network paths. 528fae3551SRodney W. Grimes * 538fae3551SRodney W. Grimes * Author - 548fae3551SRodney W. Grimes * Mike Muuss 558fae3551SRodney W. Grimes * U. S. Army Ballistic Research Laboratory 568fae3551SRodney W. Grimes * December, 1983 578fae3551SRodney W. Grimes * 588fae3551SRodney W. Grimes * Status - 598fae3551SRodney W. Grimes * Public Domain. Distribution Unlimited. 608fae3551SRodney W. Grimes * Bugs - 618fae3551SRodney W. Grimes * More statistics could always be gathered. 628fae3551SRodney W. Grimes * This program has to run SUID to ROOT to access the ICMP socket. 638fae3551SRodney W. Grimes */ 648fae3551SRodney W. Grimes 6543470e3bSGarrett Wollman #include <sys/param.h> /* NB: we rely on this for <sys/types.h> */ 66b881b8beSRobert Watson #include <sys/capsicum.h> 678fae3551SRodney W. Grimes #include <sys/socket.h> 68261e59bbSMaxim Konovalov #include <sys/sysctl.h> 698fae3551SRodney W. Grimes #include <sys/time.h> 70039d6aa4SBill Fenner #include <sys/uio.h> 718fae3551SRodney W. Grimes 728fae3551SRodney W. Grimes #include <netinet/in.h> 7343470e3bSGarrett Wollman #include <netinet/in_systm.h> 748fae3551SRodney W. Grimes #include <netinet/ip.h> 758fae3551SRodney W. Grimes #include <netinet/ip_icmp.h> 768fae3551SRodney W. Grimes #include <netinet/ip_var.h> 7743470e3bSGarrett Wollman #include <arpa/inet.h> 78c501d73cSMariusz Zaborski 79c501d73cSMariusz Zaborski #include <libcasper.h> 80c501d73cSMariusz Zaborski #include <casper/cap_dns.h> 818fae3551SRodney W. Grimes 829a4365d0SYoshinobu Inoue #ifdef IPSEC 838409aedfSGeorge V. Neville-Neil #include <netipsec/ipsec.h> 849a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 859a4365d0SYoshinobu Inoue 867bdc3291SMark Johnston #include <capsicum_helpers.h> 87261e59bbSMaxim Konovalov #include <ctype.h> 88261e59bbSMaxim Konovalov #include <err.h> 89261e59bbSMaxim Konovalov #include <errno.h> 90261e59bbSMaxim Konovalov #include <math.h> 91261e59bbSMaxim Konovalov #include <netdb.h> 92d9cacf60SAlan Somers #include <stddef.h> 93261e59bbSMaxim Konovalov #include <signal.h> 94261e59bbSMaxim Konovalov #include <stdio.h> 95261e59bbSMaxim Konovalov #include <stdlib.h> 96261e59bbSMaxim Konovalov #include <string.h> 97261e59bbSMaxim Konovalov #include <sysexits.h> 981ad76f1bSAlan Somers #include <time.h> 99261e59bbSMaxim Konovalov #include <unistd.h> 100261e59bbSMaxim Konovalov 1013cde9171SAlan Somers #include "main.h" 1023cde9171SAlan Somers #include "ping.h" 103ff77ab83SAlan Somers #include "utils.h" 104ff77ab83SAlan Somers 105301207dfSMaxim Konovalov #define INADDR_LEN ((int)sizeof(in_addr_t)) 10613e3f0b7SMaxim Konovalov #define TIMEVAL_LEN ((int)sizeof(struct tv32)) 107eb1543c6SMatthew N. Dodd #define MASK_LEN (ICMP_MASKLEN - ICMP_MINLEN) 108eb1543c6SMatthew N. Dodd #define TS_LEN (ICMP_TSLEN - ICMP_MINLEN) 109c67c1ce8SMatthew N. Dodd #define DEFDATALEN 56 /* default data length */ 1108f975bb3SBruce Evans #define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */ 1118f975bb3SBruce Evans /* runs out of buffer space */ 1124fba6582SMaxim Konovalov #define MAXIPLEN (sizeof(struct ip) + MAX_IPOPTLEN) 1134fba6582SMaxim Konovalov #define MAXICMPLEN (ICMP_ADVLENMIN + MAX_IPOPTLEN) 114d6cd1497SGleb Smirnoff #define MAXWAIT 10000 /* max ms to wait for response */ 115bf113f1bSBill Fumerola #define MAXALARM (60 * 60) /* max seconds for alarm timeout */ 1160b2f8b3fSMaxim Konovalov #define MAXTOS 255 1178fae3551SRodney W. Grimes 1188fae3551SRodney W. Grimes #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 1198fae3551SRodney W. Grimes #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 1208fae3551SRodney W. Grimes #define SET(bit) (A(bit) |= B(bit)) 1218fae3551SRodney W. Grimes #define CLR(bit) (A(bit) &= (~B(bit))) 1228fae3551SRodney W. Grimes #define TST(bit) (A(bit) & B(bit)) 1238fae3551SRodney W. Grimes 12413e3f0b7SMaxim Konovalov struct tv32 { 12513e3f0b7SMaxim Konovalov int32_t tv32_sec; 1261ad76f1bSAlan Somers int32_t tv32_nsec; 12713e3f0b7SMaxim Konovalov }; 12813e3f0b7SMaxim Konovalov 1298fae3551SRodney W. Grimes /* various options */ 1307e9489e0SHiroki Sato static int options; 13185456935SBill Fenner #define F_FLOOD 0x0001 13285456935SBill Fenner #define F_INTERVAL 0x0002 13385456935SBill Fenner #define F_NUMERIC 0x0004 13485456935SBill Fenner #define F_PINGFILLED 0x0008 13585456935SBill Fenner #define F_QUIET 0x0010 13685456935SBill Fenner #define F_RROUTE 0x0020 13785456935SBill Fenner #define F_SO_DEBUG 0x0040 13885456935SBill Fenner #define F_SO_DONTROUTE 0x0080 13985456935SBill Fenner #define F_VERBOSE 0x0100 14085456935SBill Fenner #define F_QUIET2 0x0200 14185456935SBill Fenner #define F_NOLOOP 0x0400 14285456935SBill Fenner #define F_MTTL 0x0800 14385456935SBill Fenner #define F_MIF 0x1000 144772dfa72SDaniel O'Callaghan #define F_AUDIBLE 0x2000 1459a4365d0SYoshinobu Inoue #ifdef IPSEC 1469a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 1479a4365d0SYoshinobu Inoue #define F_POLICY 0x4000 1489a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 1499a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 150211bfbd2SRuslan Ermilov #define F_TTL 0x8000 151ca517ad8SPoul-Henning Kamp #define F_MISSED 0x10000 1528025c44bSDima Dorfman #define F_ONCE 0x20000 1530b2f8b3fSMaxim Konovalov #define F_HDRINCL 0x40000 154143008a1SMatthew N. Dodd #define F_MASK 0x80000 155eb1543c6SMatthew N. Dodd #define F_TIME 0x100000 1569ff95228SGleb Smirnoff #define F_SWEEP 0x200000 157d6cd1497SGleb Smirnoff #define F_WAITTIME 0x400000 15881a6f4c7SRichard Scheffenegger #define F_IP_VLAN_PCP 0x800000 159d399eb3eSPiotr Pawel Stefaniak #define F_DOT 0x1000000 1608fae3551SRodney W. Grimes 1618fae3551SRodney W. Grimes /* 1628fae3551SRodney W. Grimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 1638fae3551SRodney W. Grimes * number of received sequence numbers we can keep track of. Change 128 1648fae3551SRodney W. Grimes * to 8192 for complete accuracy... 1658fae3551SRodney W. Grimes */ 1668fae3551SRodney W. Grimes #define MAX_DUP_CHK (8 * 128) 1677e9489e0SHiroki Sato static int mx_dup_ck = MAX_DUP_CHK; 1687e9489e0SHiroki Sato static char rcvd_tbl[MAX_DUP_CHK / 8]; 1698fae3551SRodney W. Grimes 1707e9489e0SHiroki Sato static struct sockaddr_in whereto; /* who to ping */ 1717e9489e0SHiroki Sato static int datalen = DEFDATALEN; 1727e9489e0SHiroki Sato static int maxpayload; 1737e9489e0SHiroki Sato static int ssend; /* send socket file descriptor */ 1747e9489e0SHiroki Sato static int srecv; /* receive socket file descriptor */ 1757e9489e0SHiroki Sato static u_char outpackhdr[IP_MAXPACKET], *outpack; 1767e9489e0SHiroki Sato static char BBELL = '\a'; /* characters written for MISSED and AUDIBLE */ 1777e9489e0SHiroki Sato static char BSPACE = '\b'; /* characters written for flood */ 178d399eb3eSPiotr Pawel Stefaniak static const char *DOT = "."; 179d399eb3eSPiotr Pawel Stefaniak static size_t DOTlen = 1; 180d399eb3eSPiotr Pawel Stefaniak static size_t DOTidx = 0; 1817e9489e0SHiroki Sato static char *hostname; 1827e9489e0SHiroki Sato static char *shostname; 1837e9489e0SHiroki Sato static int ident; /* process id to identify our packets */ 1847e9489e0SHiroki Sato static int uid; /* cached uid for micro-optimization */ 1857e9489e0SHiroki Sato static u_char icmp_type = ICMP_ECHO; 1867e9489e0SHiroki Sato static u_char icmp_type_rsp = ICMP_ECHOREPLY; 1877e9489e0SHiroki Sato static int phdr_len = 0; 1887e9489e0SHiroki Sato static int send_len; 1898fae3551SRodney W. Grimes 1908fae3551SRodney W. Grimes /* counters */ 1917e9489e0SHiroki Sato static long nmissedmax; /* max value of ntransmitted - nreceived - 1 */ 1927e9489e0SHiroki Sato static long npackets; /* max packets to transmit */ 1937e9489e0SHiroki Sato static long nreceived; /* # of packets we got back */ 1947e9489e0SHiroki Sato static long nrepeats; /* number of duplicates */ 1957e9489e0SHiroki Sato static long ntransmitted; /* sequence # for outbound packets = #sent */ 1967e9489e0SHiroki Sato static long snpackets; /* max packets to transmit in one sweep */ 1977e9489e0SHiroki Sato static long sntransmitted; /* # of packets we sent in this sweep */ 1987e9489e0SHiroki Sato static int sweepmax; /* max value of payload in sweep */ 1997e9489e0SHiroki Sato static int sweepmin = 0; /* start value of payload in sweep */ 2007e9489e0SHiroki Sato static int sweepincr = 1; /* payload increment in sweep */ 2017e9489e0SHiroki Sato static int interval = 1000; /* interval between packets, ms */ 2027e9489e0SHiroki Sato static int waittime = MAXWAIT; /* timeout for each packet */ 2037e9489e0SHiroki Sato static long nrcvtimeout = 0; /* # of packets we got back after waittime */ 2048fae3551SRodney W. Grimes 2058fae3551SRodney W. Grimes /* timing */ 2067e9489e0SHiroki Sato static int timing; /* flag to do timing */ 2077e9489e0SHiroki Sato static double tmin = 999999999.0; /* minimum round trip time */ 2087e9489e0SHiroki Sato static double tmax = 0.0; /* maximum round trip time */ 2097e9489e0SHiroki Sato static double tsum = 0.0; /* sum of all times, for doing average */ 2107e9489e0SHiroki Sato static double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 2118fae3551SRodney W. Grimes 2127e9489e0SHiroki Sato /* nonzero if we've been told to finish up */ 2137e9489e0SHiroki Sato static volatile sig_atomic_t finish_up; 2147e9489e0SHiroki Sato static volatile sig_atomic_t siginfo_p; 215badd8138SSean Eric Fagan 21649133c6dSPawel Jakub Dawidek static cap_channel_t *capdns; 21749133c6dSPawel Jakub Dawidek 21843470e3bSGarrett Wollman static void fill(char *, char *); 21949133c6dSPawel Jakub Dawidek static cap_channel_t *capdns_setup(void); 22043470e3bSGarrett Wollman static void check_status(void); 2218f975bb3SBruce Evans static void finish(void) __dead2; 22243470e3bSGarrett Wollman static void pinger(void); 22343470e3bSGarrett Wollman static char *pr_addr(struct in_addr); 224eb1543c6SMatthew N. Dodd static char *pr_ntime(n_time); 225d9cacf60SAlan Somers static void pr_icmph(struct icmp *, struct ip *, const u_char *const); 22620b41303SJose Luis Duran static void pr_iph(struct ip *, const u_char *); 227d9cacf60SAlan Somers static void pr_pack(char *, ssize_t, struct sockaddr_in *, struct timespec *); 22843470e3bSGarrett Wollman static void status(int); 2298f975bb3SBruce Evans static void stopit(int); 2308fae3551SRodney W. Grimes 23143470e3bSGarrett Wollman int 2323cde9171SAlan Somers ping(int argc, char *const *argv) 2338fae3551SRodney W. Grimes { 23431eac03bSSean Chittenden struct sockaddr_in from, sock_in; 235efc8588dSDavid E. O'Brien struct in_addr ifaddr; 2361ad76f1bSAlan Somers struct timespec last, intvl; 237efc8588dSDavid E. O'Brien struct iovec iov; 238efc8588dSDavid E. O'Brien struct msghdr msg; 239efc8588dSDavid E. O'Brien struct sigaction si_sa; 2400b2f8b3fSMaxim Konovalov size_t sz; 241e81f5049SOlivier Houchard u_char *datap, packet[IP_MAXPACKET] __aligned(4); 24278e1f68eSMark Johnston const char *errstr; 243d074d39fSMatthew N. Dodd char *ep, *source, *target, *payload; 244261e59bbSMaxim Konovalov struct hostent *hp; 245efc8588dSDavid E. O'Brien #ifdef IPSEC_POLICY_IPSEC 246efc8588dSDavid E. O'Brien char *policy_in, *policy_out; 247efc8588dSDavid E. O'Brien #endif 248261e59bbSMaxim Konovalov struct sockaddr_in *to; 249261e59bbSMaxim Konovalov double t; 250c0a3773aSEugene Grosbein u_long alarmtimeout; 25178e1f68eSMark Johnston long long ltmp; 25249133c6dSPawel Jakub Dawidek int almost_done, ch, df, hold, i, icmp_len, mib[4], preload; 25381a6f4c7SRichard Scheffenegger int ssend_errno, srecv_errno, tos, ttl, pcp; 2541ad76f1bSAlan Somers char ctrl[CMSG_SPACE(sizeof(struct timespec))]; 255efc8588dSDavid E. O'Brien char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN]; 2568fae3551SRodney W. Grimes #ifdef IP_OPTIONS 2574fba6582SMaxim Konovalov char rspace[MAX_IPOPTLEN]; /* record route space */ 2588fae3551SRodney W. Grimes #endif 259261e59bbSMaxim Konovalov unsigned char loop, mttl; 260efc8588dSDavid E. O'Brien 26131eac03bSSean Chittenden payload = source = NULL; 2629a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 263efc8588dSDavid E. O'Brien policy_in = policy_out = NULL; 2649a4365d0SYoshinobu Inoue #endif 26549133c6dSPawel Jakub Dawidek cap_rights_t rights; 2668fae3551SRodney W. Grimes 267299e2c58SAlan Somers options |= F_NUMERIC; 268299e2c58SAlan Somers 269f1284d7aSBill Fenner /* 270f1284d7aSBill Fenner * Do the stuff that we need root priv's for *first*, and 271f1284d7aSBill Fenner * then drop our setuid bit. Save error reporting for 272f1284d7aSBill Fenner * after arg parsing. 27349133c6dSPawel Jakub Dawidek * 27449133c6dSPawel Jakub Dawidek * Historicaly ping was using one socket 's' for sending and for 27549133c6dSPawel Jakub Dawidek * receiving. After capsicum(4) related changes we use two 27649133c6dSPawel Jakub Dawidek * sockets. It was done for special ping use case - when user 27749133c6dSPawel Jakub Dawidek * issue ping on multicast or broadcast address replies come 27849133c6dSPawel Jakub Dawidek * from different addresses, not from the address we 27949133c6dSPawel Jakub Dawidek * connect(2)'ed to, and send socket do not receive those 28049133c6dSPawel Jakub Dawidek * packets. 281f1284d7aSBill Fenner */ 28249133c6dSPawel Jakub Dawidek ssend = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 28349133c6dSPawel Jakub Dawidek ssend_errno = errno; 28449133c6dSPawel Jakub Dawidek srecv = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 28549133c6dSPawel Jakub Dawidek srecv_errno = errno; 286f1284d7aSBill Fenner 2871d1d4a47SEitan Adler if (setuid(getuid()) != 0) 2881d1d4a47SEitan Adler err(EX_NOPERM, "setuid() failed"); 289ee2bf734SWarner Losh uid = getuid(); 290f1284d7aSBill Fenner 291eeb63943SDon Lewis if (ssend < 0) { 292eeb63943SDon Lewis errno = ssend_errno; 293eeb63943SDon Lewis err(EX_OSERR, "ssend socket"); 294eeb63943SDon Lewis } 295eeb63943SDon Lewis 296eeb63943SDon Lewis if (srecv < 0) { 297eeb63943SDon Lewis errno = srecv_errno; 298eeb63943SDon Lewis err(EX_OSERR, "srecv socket"); 299eeb63943SDon Lewis } 300eeb63943SDon Lewis 30181a6f4c7SRichard Scheffenegger alarmtimeout = df = preload = tos = pcp = 0; 302badd8138SSean Eric Fagan 3030b2f8b3fSMaxim Konovalov outpack = outpackhdr + sizeof(struct ip); 3049ce201f2SAlan Somers while ((ch = getopt(argc, argv, PING4OPTS)) != -1) { 3058fae3551SRodney W. Grimes switch(ch) { 306d399eb3eSPiotr Pawel Stefaniak case '.': 307d399eb3eSPiotr Pawel Stefaniak options |= F_DOT; 308d399eb3eSPiotr Pawel Stefaniak if (optarg != NULL) { 309d399eb3eSPiotr Pawel Stefaniak DOT = optarg; 310d399eb3eSPiotr Pawel Stefaniak DOTlen = strlen(optarg); 311d399eb3eSPiotr Pawel Stefaniak } 312d399eb3eSPiotr Pawel Stefaniak break; 3133cde9171SAlan Somers case '4': 3143cde9171SAlan Somers /* This option is processed in main(). */ 3153cde9171SAlan Somers break; 316ca517ad8SPoul-Henning Kamp case 'A': 317ca517ad8SPoul-Henning Kamp options |= F_MISSED; 318ca517ad8SPoul-Henning Kamp break; 319772dfa72SDaniel O'Callaghan case 'a': 320772dfa72SDaniel O'Callaghan options |= F_AUDIBLE; 321772dfa72SDaniel O'Callaghan break; 32281a6f4c7SRichard Scheffenegger case 'C': 32381a6f4c7SRichard Scheffenegger options |= F_IP_VLAN_PCP; 32478e1f68eSMark Johnston ltmp = strtonum(optarg, -1, 7, &errstr); 32578e1f68eSMark Johnston if (errstr != NULL) 32681a6f4c7SRichard Scheffenegger errx(EX_USAGE, "invalid PCP: `%s'", optarg); 32781a6f4c7SRichard Scheffenegger pcp = ltmp; 32881a6f4c7SRichard Scheffenegger break; 3298fae3551SRodney W. Grimes case 'c': 33078e1f68eSMark Johnston ltmp = strtonum(optarg, 1, LONG_MAX, &errstr); 33178e1f68eSMark Johnston if (errstr != NULL) 33243470e3bSGarrett Wollman errx(EX_USAGE, 33343470e3bSGarrett Wollman "invalid count of packets to transmit: `%s'", 33443470e3bSGarrett Wollman optarg); 33578e1f68eSMark Johnston npackets = (long)ltmp; 3368fae3551SRodney W. Grimes break; 3370b2f8b3fSMaxim Konovalov case 'D': 3380b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 3390b2f8b3fSMaxim Konovalov df = 1; 3400b2f8b3fSMaxim Konovalov break; 3418fae3551SRodney W. Grimes case 'd': 3428fae3551SRodney W. Grimes options |= F_SO_DEBUG; 3438fae3551SRodney W. Grimes break; 3448fae3551SRodney W. Grimes case 'f': 345526f06b2SMatthew Dillon if (uid) { 34643470e3bSGarrett Wollman errno = EPERM; 34743470e3bSGarrett Wollman err(EX_NOPERM, "-f flag"); 3488fae3551SRodney W. Grimes } 3498fae3551SRodney W. Grimes options |= F_FLOOD; 350d399eb3eSPiotr Pawel Stefaniak options |= F_DOT; 3518fae3551SRodney W. Grimes setbuf(stdout, (char *)NULL); 3528fae3551SRodney W. Grimes break; 3539ff95228SGleb Smirnoff case 'G': /* Maximum packet size for ping sweep */ 35478e1f68eSMark Johnston ltmp = strtonum(optarg, 1, INT_MAX, &errstr); 35578e1f68eSMark Johnston if (errstr != NULL) { 3569ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3579ff95228SGleb Smirnoff optarg); 35878e1f68eSMark Johnston } 35978e1f68eSMark Johnston sweepmax = (int)ltmp; 36078e1f68eSMark Johnston if (uid != 0 && sweepmax > DEFDATALEN) { 36178e1f68eSMark Johnston errc(EX_NOPERM, EPERM, 36278e1f68eSMark Johnston "packet size too large: %d > %u", 36378e1f68eSMark Johnston sweepmax, DEFDATALEN); 3649ff95228SGleb Smirnoff } 3659ff95228SGleb Smirnoff options |= F_SWEEP; 3669ff95228SGleb Smirnoff break; 3679ff95228SGleb Smirnoff case 'g': /* Minimum packet size for ping sweep */ 36878e1f68eSMark Johnston ltmp = strtonum(optarg, 1, INT_MAX, &errstr); 36978e1f68eSMark Johnston if (errstr != NULL) { 3709ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3719ff95228SGleb Smirnoff optarg); 37278e1f68eSMark Johnston } 37378e1f68eSMark Johnston sweepmin = (int)ltmp; 37478e1f68eSMark Johnston if (uid != 0 && sweepmin > DEFDATALEN) { 37578e1f68eSMark Johnston errc(EX_NOPERM, EPERM, 37678e1f68eSMark Johnston "packet size too large: %d > %u", 37778e1f68eSMark Johnston sweepmin, DEFDATALEN); 3789ff95228SGleb Smirnoff } 3799ff95228SGleb Smirnoff options |= F_SWEEP; 3809ff95228SGleb Smirnoff break; 38199f13ae1SAlan Somers case 'H': 38299f13ae1SAlan Somers options &= ~F_NUMERIC; 38399f13ae1SAlan Somers break; 3849ff95228SGleb Smirnoff case 'h': /* Packet size increment for ping sweep */ 38578e1f68eSMark Johnston ltmp = strtonum(optarg, 1, INT_MAX, &errstr); 38678e1f68eSMark Johnston if (errstr != NULL) { 38778e1f68eSMark Johnston errx(EX_USAGE, "invalid packet size: `%s'", 3889ff95228SGleb Smirnoff optarg); 38978e1f68eSMark Johnston } 39078e1f68eSMark Johnston sweepincr = (int)ltmp; 39178e1f68eSMark Johnston if (uid != 0 && sweepincr > DEFDATALEN) { 39278e1f68eSMark Johnston errc(EX_NOPERM, EPERM, 39378e1f68eSMark Johnston "packet size too large: %d > %u", 39478e1f68eSMark Johnston sweepincr, DEFDATALEN); 3959ff95228SGleb Smirnoff } 3969ff95228SGleb Smirnoff options |= F_SWEEP; 3979ff95228SGleb Smirnoff break; 3981f6a4631SRuslan Ermilov case 'I': /* multicast interface */ 3991f6a4631SRuslan Ermilov if (inet_aton(optarg, &ifaddr) == 0) 4001f6a4631SRuslan Ermilov errx(EX_USAGE, 4011f6a4631SRuslan Ermilov "invalid multicast interface: `%s'", 4021f6a4631SRuslan Ermilov optarg); 4031f6a4631SRuslan Ermilov options |= F_MIF; 4041f6a4631SRuslan Ermilov break; 4058fae3551SRodney W. Grimes case 'i': /* wait between sending packets */ 4061ad0b1beSMaxim Konovalov t = strtod(optarg, &ep) * 1000.0; 4071ad0b1beSMaxim Konovalov if (*ep || ep == optarg || t > (double)INT_MAX) 4081ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid timing interval: `%s'", 4091ad0b1beSMaxim Konovalov optarg); 4108fae3551SRodney W. Grimes options |= F_INTERVAL; 411526f06b2SMatthew Dillon interval = (int)t; 412526f06b2SMatthew Dillon if (uid && interval < 1000) { 413526f06b2SMatthew Dillon errno = EPERM; 414526f06b2SMatthew Dillon err(EX_NOPERM, "-i interval too short"); 415526f06b2SMatthew Dillon } 4168fae3551SRodney W. Grimes break; 4171f6a4631SRuslan Ermilov case 'L': 4181f6a4631SRuslan Ermilov options |= F_NOLOOP; 4191f6a4631SRuslan Ermilov loop = 0; 42085456935SBill Fenner break; 4218fae3551SRodney W. Grimes case 'l': 42278e1f68eSMark Johnston ltmp = strtonum(optarg, 0, INT_MAX, &errstr); 42378e1f68eSMark Johnston if (errstr != NULL) 42443470e3bSGarrett Wollman errx(EX_USAGE, 42543470e3bSGarrett Wollman "invalid preload value: `%s'", optarg); 4265e2cc0f4SStephen McKay if (uid) { 427f78ac61bSWarner Losh errno = EPERM; 428f78ac61bSWarner Losh err(EX_NOPERM, "-l flag"); 429f78ac61bSWarner Losh } 43078e1f68eSMark Johnston preload = (int)ltmp; 4318fae3551SRodney W. Grimes break; 4321f6a4631SRuslan Ermilov case 'M': 433eb1543c6SMatthew N. Dodd switch(optarg[0]) { 434eb1543c6SMatthew N. Dodd case 'M': 435eb1543c6SMatthew N. Dodd case 'm': 4361f6a4631SRuslan Ermilov options |= F_MASK; 43785456935SBill Fenner break; 438eb1543c6SMatthew N. Dodd case 'T': 439eb1543c6SMatthew N. Dodd case 't': 440eb1543c6SMatthew N. Dodd options |= F_TIME; 441eb1543c6SMatthew N. Dodd break; 442eb1543c6SMatthew N. Dodd default: 443eb1543c6SMatthew N. Dodd errx(EX_USAGE, "invalid message: `%c'", optarg[0]); 444eb1543c6SMatthew N. Dodd break; 445eb1543c6SMatthew N. Dodd } 446eb1543c6SMatthew N. Dodd break; 447211bfbd2SRuslan Ermilov case 'm': /* TTL */ 44878e1f68eSMark Johnston ltmp = strtonum(optarg, 0, MAXTTL, &errstr); 44978e1f68eSMark Johnston if (errstr != NULL) 4501ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid TTL: `%s'", optarg); 45178e1f68eSMark Johnston ttl = (int)ltmp; 452211bfbd2SRuslan Ermilov options |= F_TTL; 453211bfbd2SRuslan Ermilov break; 4548fae3551SRodney W. Grimes case 'n': 4558fae3551SRodney W. Grimes options |= F_NUMERIC; 4568fae3551SRodney W. Grimes break; 4578025c44bSDima Dorfman case 'o': 4588025c44bSDima Dorfman options |= F_ONCE; 4598025c44bSDima Dorfman break; 4601f6a4631SRuslan Ermilov #ifdef IPSEC 4611f6a4631SRuslan Ermilov #ifdef IPSEC_POLICY_IPSEC 4621f6a4631SRuslan Ermilov case 'P': 4631f6a4631SRuslan Ermilov options |= F_POLICY; 4641f6a4631SRuslan Ermilov if (!strncmp("in", optarg, 2)) 4651f6a4631SRuslan Ermilov policy_in = strdup(optarg); 4661f6a4631SRuslan Ermilov else if (!strncmp("out", optarg, 3)) 4671f6a4631SRuslan Ermilov policy_out = strdup(optarg); 4681f6a4631SRuslan Ermilov else 4691f6a4631SRuslan Ermilov errx(1, "invalid security policy"); 4701f6a4631SRuslan Ermilov break; 4711f6a4631SRuslan Ermilov #endif /*IPSEC_POLICY_IPSEC*/ 4721f6a4631SRuslan Ermilov #endif /*IPSEC*/ 4738fae3551SRodney W. Grimes case 'p': /* fill buffer with user pattern */ 4748fae3551SRodney W. Grimes options |= F_PINGFILLED; 475d074d39fSMatthew N. Dodd payload = optarg; 4768fae3551SRodney W. Grimes break; 477ef9e6dc7SBill Fenner case 'Q': 478ef9e6dc7SBill Fenner options |= F_QUIET2; 479ef9e6dc7SBill Fenner break; 4808fae3551SRodney W. Grimes case 'q': 4818fae3551SRodney W. Grimes options |= F_QUIET; 4828fae3551SRodney W. Grimes break; 4838fae3551SRodney W. Grimes case 'R': 4848fae3551SRodney W. Grimes options |= F_RROUTE; 4858fae3551SRodney W. Grimes break; 4868fae3551SRodney W. Grimes case 'r': 4878fae3551SRodney W. Grimes options |= F_SO_DONTROUTE; 4888fae3551SRodney W. Grimes break; 4891f6a4631SRuslan Ermilov case 'S': 4901f6a4631SRuslan Ermilov source = optarg; 4911f6a4631SRuslan Ermilov break; 4928fae3551SRodney W. Grimes case 's': /* size of packet to send */ 49378e1f68eSMark Johnston ltmp = strtonum(optarg, 0, INT_MAX, &errstr); 49478e1f68eSMark Johnston if (errstr != NULL) 49543470e3bSGarrett Wollman errx(EX_USAGE, "invalid packet size: `%s'", 49643470e3bSGarrett Wollman optarg); 49778e1f68eSMark Johnston datalen = (int)ltmp; 49878e1f68eSMark Johnston if (uid != 0 && datalen > DEFDATALEN) { 499fb7d32c7SMaxim Konovalov errno = EPERM; 500fb7d32c7SMaxim Konovalov err(EX_NOPERM, 50178e1f68eSMark Johnston "packet size too large: %d > %u", 50278e1f68eSMark Johnston datalen, DEFDATALEN); 503fb7d32c7SMaxim Konovalov } 5048fae3551SRodney W. Grimes break; 5051f6a4631SRuslan Ermilov case 'T': /* multicast TTL */ 50678e1f68eSMark Johnston ltmp = strtonum(optarg, 0, MAXTTL, &errstr); 50778e1f68eSMark Johnston if (errstr != NULL) 5081f6a4631SRuslan Ermilov errx(EX_USAGE, "invalid multicast TTL: `%s'", 5091f6a4631SRuslan Ermilov optarg); 51078e1f68eSMark Johnston mttl = (unsigned char)ltmp; 5111f6a4631SRuslan Ermilov options |= F_MTTL; 51299490edeSWarner Losh break; 5137237fd94SBill Fumerola case 't': 514bf113f1bSBill Fumerola alarmtimeout = strtoul(optarg, &ep, 0); 515bf113f1bSBill Fumerola if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX)) 5167237fd94SBill Fumerola errx(EX_USAGE, "invalid timeout: `%s'", 5177237fd94SBill Fumerola optarg); 518bf113f1bSBill Fumerola if (alarmtimeout > MAXALARM) 519bf113f1bSBill Fumerola errx(EX_USAGE, "invalid timeout: `%s' > %d", 520bf113f1bSBill Fumerola optarg, MAXALARM); 5212eb6acc2SAlan Somers { 5222eb6acc2SAlan Somers struct itimerval itv; 5232eb6acc2SAlan Somers 5242eb6acc2SAlan Somers timerclear(&itv.it_interval); 5252eb6acc2SAlan Somers timerclear(&itv.it_value); 5262eb6acc2SAlan Somers itv.it_value.tv_sec = (time_t)alarmtimeout; 5272eb6acc2SAlan Somers if (setitimer(ITIMER_REAL, &itv, NULL) != 0) 5282eb6acc2SAlan Somers err(1, "setitimer"); 5292eb6acc2SAlan Somers } 5307237fd94SBill Fumerola break; 5318fae3551SRodney W. Grimes case 'v': 5328fae3551SRodney W. Grimes options |= F_VERBOSE; 5338fae3551SRodney W. Grimes break; 534d6cd1497SGleb Smirnoff case 'W': /* wait ms for answer */ 535d6cd1497SGleb Smirnoff t = strtod(optarg, &ep); 536d6cd1497SGleb Smirnoff if (*ep || ep == optarg || t > (double)INT_MAX) 537d6cd1497SGleb Smirnoff errx(EX_USAGE, "invalid timing interval: `%s'", 538d6cd1497SGleb Smirnoff optarg); 539d6cd1497SGleb Smirnoff options |= F_WAITTIME; 540d6cd1497SGleb Smirnoff waittime = (int)t; 541d6cd1497SGleb Smirnoff break; 5420b2f8b3fSMaxim Konovalov case 'z': 5430b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 544c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 545c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > MAXTOS || ltmp < 0) 5460b2f8b3fSMaxim Konovalov errx(EX_USAGE, "invalid TOS: `%s'", optarg); 547c0a3773aSEugene Grosbein tos = ltmp; 5480b2f8b3fSMaxim Konovalov break; 5498fae3551SRodney W. Grimes default: 550e345a80dSPhilippe Charnier usage(); 55143470e3bSGarrett Wollman } 55243470e3bSGarrett Wollman } 55343470e3bSGarrett Wollman 55443470e3bSGarrett Wollman if (argc - optind != 1) 555e345a80dSPhilippe Charnier usage(); 55643470e3bSGarrett Wollman target = argv[optind]; 5578fae3551SRodney W. Grimes 558d829c3dfSMatthew N. Dodd switch (options & (F_MASK|F_TIME)) { 559d829c3dfSMatthew N. Dodd case 0: break; 560d829c3dfSMatthew N. Dodd case F_MASK: 561eb1543c6SMatthew N. Dodd icmp_type = ICMP_MASKREQ; 562eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_MASKREPLY; 563d829c3dfSMatthew N. Dodd phdr_len = MASK_LEN; 564eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 565eb1543c6SMatthew N. Dodd (void)printf("ICMP_MASKREQ\n"); 566d829c3dfSMatthew N. Dodd break; 567d829c3dfSMatthew N. Dodd case F_TIME: 568eb1543c6SMatthew N. Dodd icmp_type = ICMP_TSTAMP; 569eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_TSTAMPREPLY; 570d829c3dfSMatthew N. Dodd phdr_len = TS_LEN; 571eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 572eb1543c6SMatthew N. Dodd (void)printf("ICMP_TSTAMP\n"); 573d829c3dfSMatthew N. Dodd break; 574d829c3dfSMatthew N. Dodd default: 575d829c3dfSMatthew N. Dodd errx(EX_USAGE, "ICMP_TSTAMP and ICMP_MASKREQ are exclusive."); 576d829c3dfSMatthew N. Dodd break; 577eb1543c6SMatthew N. Dodd } 5780fe0c0ccSMaxim Konovalov icmp_len = sizeof(struct ip) + ICMP_MINLEN + phdr_len; 579fb7d32c7SMaxim Konovalov if (options & F_RROUTE) 580e88178ddSMaxim Konovalov icmp_len += MAX_IPOPTLEN; 581e88178ddSMaxim Konovalov maxpayload = IP_MAXPACKET - icmp_len; 582fb7d32c7SMaxim Konovalov if (datalen > maxpayload) 5831104dd84SBruce Evans errx(EX_USAGE, "packet size too large: %d > %d", datalen, 584fb7d32c7SMaxim Konovalov maxpayload); 585e88178ddSMaxim Konovalov send_len = icmp_len + datalen; 5860fe0c0ccSMaxim Konovalov datap = &outpack[ICMP_MINLEN + phdr_len + TIMEVAL_LEN]; 587d074d39fSMatthew N. Dodd if (options & F_PINGFILLED) { 588d074d39fSMatthew N. Dodd fill((char *)datap, payload); 589d074d39fSMatthew N. Dodd } 59049133c6dSPawel Jakub Dawidek capdns = capdns_setup(); 59199490edeSWarner Losh if (source) { 59231eac03bSSean Chittenden bzero((char *)&sock_in, sizeof(sock_in)); 59331eac03bSSean Chittenden sock_in.sin_family = AF_INET; 59431eac03bSSean Chittenden if (inet_aton(source, &sock_in.sin_addr) != 0) { 59599490edeSWarner Losh shostname = source; 59699490edeSWarner Losh } else { 597d68e2c04SMariusz Zaborski hp = cap_gethostbyname2(capdns, source, AF_INET); 59899490edeSWarner Losh if (!hp) 59999490edeSWarner Losh errx(EX_NOHOST, "cannot resolve %s: %s", 60099490edeSWarner Losh source, hstrerror(h_errno)); 60199490edeSWarner Losh 60231eac03bSSean Chittenden sock_in.sin_len = sizeof sock_in; 60331eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(sock_in.sin_addr) || 6044fba6582SMaxim Konovalov hp->h_length < 0) 60599490edeSWarner Losh errx(1, "gethostbyname2: illegal address"); 60631eac03bSSean Chittenden memcpy(&sock_in.sin_addr, hp->h_addr_list[0], 60731eac03bSSean Chittenden sizeof(sock_in.sin_addr)); 60899490edeSWarner Losh (void)strncpy(snamebuf, hp->h_name, 60999490edeSWarner Losh sizeof(snamebuf) - 1); 61099490edeSWarner Losh snamebuf[sizeof(snamebuf) - 1] = '\0'; 61199490edeSWarner Losh shostname = snamebuf; 61299490edeSWarner Losh } 61349133c6dSPawel Jakub Dawidek if (bind(ssend, (struct sockaddr *)&sock_in, sizeof sock_in) == 61449133c6dSPawel Jakub Dawidek -1) 61599490edeSWarner Losh err(1, "bind"); 61699490edeSWarner Losh } 61799490edeSWarner Losh 618d389e86aSMatt Jacob bzero(&whereto, sizeof(whereto)); 619d389e86aSMatt Jacob to = &whereto; 6208fae3551SRodney W. Grimes to->sin_family = AF_INET; 621d389e86aSMatt Jacob to->sin_len = sizeof *to; 62243470e3bSGarrett Wollman if (inet_aton(target, &to->sin_addr) != 0) { 6238fae3551SRodney W. Grimes hostname = target; 62443470e3bSGarrett Wollman } else { 62549133c6dSPawel Jakub Dawidek hp = cap_gethostbyname2(capdns, target, AF_INET); 62643470e3bSGarrett Wollman if (!hp) 62743470e3bSGarrett Wollman errx(EX_NOHOST, "cannot resolve %s: %s", 62843470e3bSGarrett Wollman target, hstrerror(h_errno)); 62943470e3bSGarrett Wollman 63031eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(to->sin_addr)) 6311ffae4a6SWarner Losh errx(1, "gethostbyname2 returned an illegal address"); 63243470e3bSGarrett Wollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 6338fae3551SRodney W. Grimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 634b10d9d5fSWarner Losh hnamebuf[sizeof(hnamebuf) - 1] = '\0'; 6358fae3551SRodney W. Grimes hostname = hnamebuf; 6368fae3551SRodney W. Grimes } 6378fae3551SRodney W. Grimes 63849133c6dSPawel Jakub Dawidek /* From now on we will use only reverse DNS lookups. */ 639a3ce7698SAlan Somers #ifdef WITH_CASPER 64049133c6dSPawel Jakub Dawidek if (capdns != NULL) { 64149133c6dSPawel Jakub Dawidek const char *types[1]; 64249133c6dSPawel Jakub Dawidek 643752d135eSMariusz Zaborski types[0] = "ADDR2NAME"; 64449133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdns, types, 1) < 0) 64549133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 64649133c6dSPawel Jakub Dawidek } 647a3ce7698SAlan Somers #endif 64849133c6dSPawel Jakub Dawidek if (connect(ssend, (struct sockaddr *)&whereto, sizeof(whereto)) != 0) 64949133c6dSPawel Jakub Dawidek err(1, "connect"); 65049133c6dSPawel Jakub Dawidek 65143470e3bSGarrett Wollman if (options & F_FLOOD && options & F_INTERVAL) 65243470e3bSGarrett Wollman errx(EX_USAGE, "-f and -i: incompatible options"); 65343470e3bSGarrett Wollman 65443470e3bSGarrett Wollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 65543470e3bSGarrett Wollman errx(EX_USAGE, 65643470e3bSGarrett Wollman "-f flag cannot be used with multicast destination"); 65743470e3bSGarrett Wollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 65843470e3bSGarrett Wollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 65943470e3bSGarrett Wollman errx(EX_USAGE, 66043470e3bSGarrett Wollman "-I, -L, -T flags cannot be used with unicast destination"); 6618fae3551SRodney W. Grimes 662d829c3dfSMatthew N. Dodd if (datalen >= TIMEVAL_LEN) /* can we time transfer */ 6638fae3551SRodney W. Grimes timing = 1; 66443470e3bSGarrett Wollman 66578e1f68eSMark Johnston if ((options & (F_PINGFILLED | F_SWEEP)) == 0) 666d829c3dfSMatthew N. Dodd for (i = TIMEVAL_LEN; i < datalen; ++i) 6678fae3551SRodney W. Grimes *datap++ = i; 6688fae3551SRodney W. Grimes 6698fae3551SRodney W. Grimes ident = getpid() & 0xFFFF; 6708fae3551SRodney W. Grimes 6718fae3551SRodney W. Grimes hold = 1; 67249133c6dSPawel Jakub Dawidek if (options & F_SO_DEBUG) { 67349133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DEBUG, (char *)&hold, 6748fae3551SRodney W. Grimes sizeof(hold)); 67549133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_DEBUG, (char *)&hold, 67649133c6dSPawel Jakub Dawidek sizeof(hold)); 67749133c6dSPawel Jakub Dawidek } 6788fae3551SRodney W. Grimes if (options & F_SO_DONTROUTE) 67949133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 6808fae3551SRodney W. Grimes sizeof(hold)); 68181a6f4c7SRichard Scheffenegger if (options & F_IP_VLAN_PCP) { 68281a6f4c7SRichard Scheffenegger (void)setsockopt(ssend, IPPROTO_IP, IP_VLAN_PCP, (char *)&pcp, 68381a6f4c7SRichard Scheffenegger sizeof(pcp)); 68481a6f4c7SRichard Scheffenegger } 6859a4365d0SYoshinobu Inoue #ifdef IPSEC 6869a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 6879a4365d0SYoshinobu Inoue if (options & F_POLICY) { 6889a4365d0SYoshinobu Inoue char *buf; 6899a4365d0SYoshinobu Inoue if (policy_in != NULL) { 6909a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_in, strlen(policy_in)); 6919a4365d0SYoshinobu Inoue if (buf == NULL) 692ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 69349133c6dSPawel Jakub Dawidek if (setsockopt(srecv, IPPROTO_IP, IP_IPSEC_POLICY, 6949a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 6951ad0b1beSMaxim Konovalov err(EX_CONFIG, 6961ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 6979a4365d0SYoshinobu Inoue free(buf); 6989a4365d0SYoshinobu Inoue } 6999a4365d0SYoshinobu Inoue 7009a4365d0SYoshinobu Inoue if (policy_out != NULL) { 7019a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_out, strlen(policy_out)); 7029a4365d0SYoshinobu Inoue if (buf == NULL) 703ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 70449133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_IPSEC_POLICY, 7059a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 7061ad0b1beSMaxim Konovalov err(EX_CONFIG, 7071ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 7089a4365d0SYoshinobu Inoue free(buf); 7099a4365d0SYoshinobu Inoue } 7109a4365d0SYoshinobu Inoue } 7119a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 7129a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 7138fae3551SRodney W. Grimes 7140b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 715d9cacf60SAlan Somers struct ip ip; 716d9cacf60SAlan Somers 717d9cacf60SAlan Somers memcpy(&ip, outpackhdr, sizeof(ip)); 7180b2f8b3fSMaxim Konovalov if (!(options & (F_TTL | F_MTTL))) { 7190b2f8b3fSMaxim Konovalov mib[0] = CTL_NET; 7200b2f8b3fSMaxim Konovalov mib[1] = PF_INET; 7210b2f8b3fSMaxim Konovalov mib[2] = IPPROTO_IP; 7220b2f8b3fSMaxim Konovalov mib[3] = IPCTL_DEFTTL; 7230b2f8b3fSMaxim Konovalov sz = sizeof(ttl); 7240b2f8b3fSMaxim Konovalov if (sysctl(mib, 4, &ttl, &sz, NULL, 0) == -1) 7250b2f8b3fSMaxim Konovalov err(1, "sysctl(net.inet.ip.ttl)"); 7260b2f8b3fSMaxim Konovalov } 72749133c6dSPawel Jakub Dawidek setsockopt(ssend, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold)); 728d9cacf60SAlan Somers ip.ip_v = IPVERSION; 729d9cacf60SAlan Somers ip.ip_hl = sizeof(struct ip) >> 2; 730d9cacf60SAlan Somers ip.ip_tos = tos; 731d9cacf60SAlan Somers ip.ip_id = 0; 732d9cacf60SAlan Somers ip.ip_off = htons(df ? IP_DF : 0); 733d9cacf60SAlan Somers ip.ip_ttl = ttl; 734d9cacf60SAlan Somers ip.ip_p = IPPROTO_ICMP; 735d9cacf60SAlan Somers ip.ip_src.s_addr = source ? sock_in.sin_addr.s_addr : INADDR_ANY; 736d9cacf60SAlan Somers ip.ip_dst = to->sin_addr; 737d9cacf60SAlan Somers memcpy(outpackhdr, &ip, sizeof(ip)); 7380b2f8b3fSMaxim Konovalov } 73949133c6dSPawel Jakub Dawidek 74049133c6dSPawel Jakub Dawidek /* 74149133c6dSPawel Jakub Dawidek * Here we enter capability mode. Further down access to global 74249133c6dSPawel Jakub Dawidek * namespaces (e.g filesystem) is restricted (see capsicum(4)). 74349133c6dSPawel Jakub Dawidek * We must connect(2) our socket before this point. 74449133c6dSPawel Jakub Dawidek */ 7457bdc3291SMark Johnston caph_cache_catpages(); 74618fcfaa4SMark Johnston if (caph_enter_casper() < 0) 747301bc9f9SAlan Somers err(1, "caph_enter_casper"); 74849133c6dSPawel Jakub Dawidek 74949133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT, CAP_SETSOCKOPT); 7507bdc3291SMark Johnston if (caph_rights_limit(srecv, &rights) < 0) 75149133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv"); 75249133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND, CAP_SETSOCKOPT); 7537bdc3291SMark Johnston if (caph_rights_limit(ssend, &rights) < 0) 75449133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend"); 75549133c6dSPawel Jakub Dawidek 7568fae3551SRodney W. Grimes /* record route option */ 7578fae3551SRodney W. Grimes if (options & F_RROUTE) { 7588fae3551SRodney W. Grimes #ifdef IP_OPTIONS 759039d6aa4SBill Fenner bzero(rspace, sizeof(rspace)); 7608fae3551SRodney W. Grimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 7618fae3551SRodney W. Grimes rspace[IPOPT_OLEN] = sizeof(rspace) - 1; 7628fae3551SRodney W. Grimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 763039d6aa4SBill Fenner rspace[sizeof(rspace) - 1] = IPOPT_EOL; 76449133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_OPTIONS, rspace, 76543470e3bSGarrett Wollman sizeof(rspace)) < 0) 76643470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 7678fae3551SRodney W. Grimes #else 76843470e3bSGarrett Wollman errx(EX_UNAVAILABLE, 76943470e3bSGarrett Wollman "record route not available in this implementation"); 7708fae3551SRodney W. Grimes #endif /* IP_OPTIONS */ 7718fae3551SRodney W. Grimes } 7728fae3551SRodney W. Grimes 773211bfbd2SRuslan Ermilov if (options & F_TTL) { 77449133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_TTL, &ttl, 775211bfbd2SRuslan Ermilov sizeof(ttl)) < 0) { 776211bfbd2SRuslan Ermilov err(EX_OSERR, "setsockopt IP_TTL"); 777211bfbd2SRuslan Ermilov } 778211bfbd2SRuslan Ermilov } 77985456935SBill Fenner if (options & F_NOLOOP) { 78049133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 78185456935SBill Fenner sizeof(loop)) < 0) { 78243470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 78385456935SBill Fenner } 78485456935SBill Fenner } 78585456935SBill Fenner if (options & F_MTTL) { 78649133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_TTL, &mttl, 787211bfbd2SRuslan Ermilov sizeof(mttl)) < 0) { 78843470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 78985456935SBill Fenner } 79085456935SBill Fenner } 79185456935SBill Fenner if (options & F_MIF) { 79249133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 79385456935SBill Fenner sizeof(ifaddr)) < 0) { 79443470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 79585456935SBill Fenner } 79685456935SBill Fenner } 797039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 79884633ef1SAlan Somers { 79984633ef1SAlan Somers int on = 1; 80084633ef1SAlan Somers int ts_clock = SO_TS_MONOTONIC; 80184633ef1SAlan Somers if (setsockopt(srecv, SOL_SOCKET, SO_TIMESTAMP, &on, 80284633ef1SAlan Somers sizeof(on)) < 0) 803039d6aa4SBill Fenner err(EX_OSERR, "setsockopt SO_TIMESTAMP"); 80484633ef1SAlan Somers if (setsockopt(srecv, SOL_SOCKET, SO_TS_CLOCK, &ts_clock, 80584633ef1SAlan Somers sizeof(ts_clock)) < 0) 80684633ef1SAlan Somers err(EX_OSERR, "setsockopt SO_TS_CLOCK"); 807039d6aa4SBill Fenner } 808039d6aa4SBill Fenner #endif 8099ff95228SGleb Smirnoff if (sweepmax) { 810bb7dfe5eSGleb Smirnoff if (sweepmin > sweepmax) 81178e1f68eSMark Johnston errx(EX_USAGE, 81278e1f68eSMark Johnston "Maximum packet size must be no less than the minimum packet size"); 81378e1f68eSMark Johnston 81478e1f68eSMark Johnston if (sweepmax > maxpayload - TIMEVAL_LEN) 81578e1f68eSMark Johnston errx(EX_USAGE, "Invalid sweep maximum"); 8169ff95228SGleb Smirnoff 8179ff95228SGleb Smirnoff if (datalen != DEFDATALEN) 81878e1f68eSMark Johnston errx(EX_USAGE, 81978e1f68eSMark Johnston "Packet size and ping sweep are mutually exclusive"); 8209ff95228SGleb Smirnoff 8219ff95228SGleb Smirnoff if (npackets > 0) { 8229ff95228SGleb Smirnoff snpackets = npackets; 8239ff95228SGleb Smirnoff npackets = 0; 8249ff95228SGleb Smirnoff } else 8259ff95228SGleb Smirnoff snpackets = 1; 8269ff95228SGleb Smirnoff datalen = sweepmin; 8279ff95228SGleb Smirnoff send_len = icmp_len + sweepmin; 8289ff95228SGleb Smirnoff } 8299ff95228SGleb Smirnoff if (options & F_SWEEP && !sweepmax) 8309ff95228SGleb Smirnoff errx(EX_USAGE, "Maximum sweep size must be specified"); 83185456935SBill Fenner 8328fae3551SRodney W. Grimes /* 8338fae3551SRodney W. Grimes * When pinging the broadcast address, you can get a lot of answers. 8348fae3551SRodney W. Grimes * Doing something so evil is useful if you are trying to stress the 8358fae3551SRodney W. Grimes * ethernet, or just want to fill the arp cache to get some stuff for 83643470e3bSGarrett Wollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 83743470e3bSGarrett Wollman * or multicast pings if they wish. 8388fae3551SRodney W. Grimes */ 8394fba6582SMaxim Konovalov 8404fba6582SMaxim Konovalov /* 8414fba6582SMaxim Konovalov * XXX receive buffer needs undetermined space for mbuf overhead 8424fba6582SMaxim Konovalov * as well. 8434fba6582SMaxim Konovalov */ 8444fba6582SMaxim Konovalov hold = IP_MAXPACKET + 128; 84549133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 8468fae3551SRodney W. Grimes sizeof(hold)); 84749133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 84849133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT); 8497bdc3291SMark Johnston if (caph_rights_limit(srecv, &rights) < 0) 85049133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv setsockopt"); 851261e59bbSMaxim Konovalov if (uid == 0) 85249133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_SNDBUF, (char *)&hold, 853e8bd25ceSRobert Watson sizeof(hold)); 85449133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 85549133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND); 8567bdc3291SMark Johnston if (caph_rights_limit(ssend, &rights) < 0) 85749133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend setsockopt"); 858e8bd25ceSRobert Watson 85999490edeSWarner Losh if (to->sin_family == AF_INET) { 86099490edeSWarner Losh (void)printf("PING %s (%s)", hostname, 86199490edeSWarner Losh inet_ntoa(to->sin_addr)); 86299490edeSWarner Losh if (source) 86399490edeSWarner Losh (void)printf(" from %s", shostname); 8649ff95228SGleb Smirnoff if (sweepmax) 8659ff95228SGleb Smirnoff (void)printf(": (%d ... %d) data bytes\n", 8669ff95228SGleb Smirnoff sweepmin, sweepmax); 8679ff95228SGleb Smirnoff else 86899490edeSWarner Losh (void)printf(": %d data bytes\n", datalen); 8699ff95228SGleb Smirnoff 8709ff95228SGleb Smirnoff } else { 8719ff95228SGleb Smirnoff if (sweepmax) 8729ff95228SGleb Smirnoff (void)printf("PING %s: (%d ... %d) data bytes\n", 8739ff95228SGleb Smirnoff hostname, sweepmin, sweepmax); 8749ff95228SGleb Smirnoff else 8758fae3551SRodney W. Grimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 8769ff95228SGleb Smirnoff } 8778fae3551SRodney W. Grimes 8787d81b35cSBruce Evans /* 879a2a00888SSean Eric Fagan * Use sigaction() instead of signal() to get unambiguous semantics, 880a2a00888SSean Eric Fagan * in particular with SA_RESTART not set. 8817d81b35cSBruce Evans */ 882a2a00888SSean Eric Fagan 883f6bd468bSPaul Traina sigemptyset(&si_sa.sa_mask); 88437e5b2c6SSean Eric Fagan si_sa.sa_flags = 0; 885a2a00888SSean Eric Fagan 886a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 887a2a00888SSean Eric Fagan if (sigaction(SIGINT, &si_sa, 0) == -1) { 888a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGINT"); 889a2a00888SSean Eric Fagan } 890a2a00888SSean Eric Fagan 891a2a00888SSean Eric Fagan si_sa.sa_handler = status; 89237e5b2c6SSean Eric Fagan if (sigaction(SIGINFO, &si_sa, 0) == -1) { 893*72d3e667SJose Luis Duran err(EX_OSERR, "sigaction SIGINFO"); 89437e5b2c6SSean Eric Fagan } 895bf113f1bSBill Fumerola 896bf113f1bSBill Fumerola if (alarmtimeout > 0) { 8977237fd94SBill Fumerola si_sa.sa_handler = stopit; 8987237fd94SBill Fumerola if (sigaction(SIGALRM, &si_sa, 0) == -1) 8997237fd94SBill Fumerola err(EX_OSERR, "sigaction SIGALRM"); 900bf113f1bSBill Fumerola } 90137e5b2c6SSean Eric Fagan 902039d6aa4SBill Fenner bzero(&msg, sizeof(msg)); 903039d6aa4SBill Fenner msg.msg_name = (caddr_t)&from; 904039d6aa4SBill Fenner msg.msg_iov = &iov; 905039d6aa4SBill Fenner msg.msg_iovlen = 1; 906039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 907039d6aa4SBill Fenner msg.msg_control = (caddr_t)ctrl; 90867511a4cSAlan Somers msg.msg_controllen = sizeof(ctrl); 909039d6aa4SBill Fenner #endif 910039d6aa4SBill Fenner iov.iov_base = packet; 911e88178ddSMaxim Konovalov iov.iov_len = IP_MAXPACKET; 912039d6aa4SBill Fenner 91332af342fSRuslan Ermilov if (preload == 0) 91432af342fSRuslan Ermilov pinger(); /* send the first ping */ 91532af342fSRuslan Ermilov else { 91632af342fSRuslan Ermilov if (npackets != 0 && preload > npackets) 91732af342fSRuslan Ermilov preload = npackets; 9188fae3551SRodney W. Grimes while (preload--) /* fire off them quickies */ 9198fae3551SRodney W. Grimes pinger(); 92032af342fSRuslan Ermilov } 9211ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &last); 9228fae3551SRodney W. Grimes 923039d6aa4SBill Fenner if (options & F_FLOOD) { 924039d6aa4SBill Fenner intvl.tv_sec = 0; 9251ad76f1bSAlan Somers intvl.tv_nsec = 10000000; 926039d6aa4SBill Fenner } else { 927526f06b2SMatthew Dillon intvl.tv_sec = interval / 1000; 9281ad76f1bSAlan Somers intvl.tv_nsec = interval % 1000 * 1000000; 929039d6aa4SBill Fenner } 930039d6aa4SBill Fenner 931261e59bbSMaxim Konovalov almost_done = 0; 9328f975bb3SBruce Evans while (!finish_up) { 9331ad76f1bSAlan Somers struct timespec now, timeout; 934039d6aa4SBill Fenner fd_set rfds; 935d9cacf60SAlan Somers int n; 936d9cacf60SAlan Somers ssize_t cc; 9378fae3551SRodney W. Grimes 9387d81b35cSBruce Evans check_status(); 93949133c6dSPawel Jakub Dawidek if ((unsigned)srecv >= FD_SETSIZE) 9407e5bbd68SJacques Vidrine errx(EX_OSERR, "descriptor too large"); 941039d6aa4SBill Fenner FD_ZERO(&rfds); 94249133c6dSPawel Jakub Dawidek FD_SET(srecv, &rfds); 9431ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 9441ad76f1bSAlan Somers timespecadd(&last, &intvl, &timeout); 9451ad76f1bSAlan Somers timespecsub(&timeout, &now, &timeout); 946039d6aa4SBill Fenner if (timeout.tv_sec < 0) 9471ad76f1bSAlan Somers timespecclear(&timeout); 9481ad76f1bSAlan Somers n = pselect(srecv + 1, &rfds, NULL, NULL, &timeout, NULL); 9495e2cc0f4SStephen McKay if (n < 0) 9505e2cc0f4SStephen McKay continue; /* Must be EINTR. */ 951039d6aa4SBill Fenner if (n == 1) { 9521ad76f1bSAlan Somers struct timespec *tv = NULL; 953039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 95467511a4cSAlan Somers struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); 955039d6aa4SBill Fenner #endif 956039d6aa4SBill Fenner msg.msg_namelen = sizeof(from); 95749133c6dSPawel Jakub Dawidek if ((cc = recvmsg(srecv, &msg, 0)) < 0) { 9588fae3551SRodney W. Grimes if (errno == EINTR) 9598fae3551SRodney W. Grimes continue; 960e345a80dSPhilippe Charnier warn("recvmsg"); 9618fae3551SRodney W. Grimes continue; 9628fae3551SRodney W. Grimes } 96346d7b45aSTom Jones /* If we have a 0 byte read from recvfrom continue */ 96446d7b45aSTom Jones if (cc == 0) 96546d7b45aSTom Jones continue; 966039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 967b17fb992SAlan Somers if (cmsg != NULL && 968b17fb992SAlan Somers cmsg->cmsg_level == SOL_SOCKET && 969039d6aa4SBill Fenner cmsg->cmsg_type == SCM_TIMESTAMP && 97031eac03bSSean Chittenden cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) { 971fa05a94cSJohn Birrell /* Copy to avoid alignment problems: */ 972fa05a94cSJohn Birrell memcpy(&now, CMSG_DATA(cmsg), sizeof(now)); 97331eac03bSSean Chittenden tv = &now; 974fa05a94cSJohn Birrell } 975039d6aa4SBill Fenner #endif 97631eac03bSSean Chittenden if (tv == NULL) { 9771ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 97831eac03bSSean Chittenden tv = &now; 979039d6aa4SBill Fenner } 98031eac03bSSean Chittenden pr_pack((char *)packet, cc, &from, tv); 98131eac03bSSean Chittenden if ((options & F_ONCE && nreceived) || 98231eac03bSSean Chittenden (npackets && nreceived >= npackets)) 9838fae3551SRodney W. Grimes break; 9848fae3551SRodney W. Grimes } 9855e2cc0f4SStephen McKay if (n == 0 || options & F_FLOOD) { 9869ff95228SGleb Smirnoff if (sweepmax && sntransmitted == snpackets) { 98778e1f68eSMark Johnston if (datalen + sweepincr > sweepmax) 98878e1f68eSMark Johnston break; 98978e1f68eSMark Johnston for (i = 0; i < sweepincr; i++) 9909ff95228SGleb Smirnoff *datap++ = i; 9919ff95228SGleb Smirnoff datalen += sweepincr; 9929ff95228SGleb Smirnoff send_len = icmp_len + datalen; 9939ff95228SGleb Smirnoff sntransmitted = 0; 9949ff95228SGleb Smirnoff } 995039d6aa4SBill Fenner if (!npackets || ntransmitted < npackets) 996039d6aa4SBill Fenner pinger(); 997039d6aa4SBill Fenner else { 998039d6aa4SBill Fenner if (almost_done) 999039d6aa4SBill Fenner break; 1000039d6aa4SBill Fenner almost_done = 1; 10011ad76f1bSAlan Somers intvl.tv_nsec = 0; 1002039d6aa4SBill Fenner if (nreceived) { 1003039d6aa4SBill Fenner intvl.tv_sec = 2 * tmax / 1000; 1004039d6aa4SBill Fenner if (!intvl.tv_sec) 1005039d6aa4SBill Fenner intvl.tv_sec = 1; 1006d6cd1497SGleb Smirnoff } else { 1007d6cd1497SGleb Smirnoff intvl.tv_sec = waittime / 1000; 10081ad76f1bSAlan Somers intvl.tv_nsec = waittime % 1000 * 1000000; 1009d6cd1497SGleb Smirnoff } 1010039d6aa4SBill Fenner } 10111ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &last); 101225107197SIan Dowse if (ntransmitted - nreceived - 1 > nmissedmax) { 101325107197SIan Dowse nmissedmax = ntransmitted - nreceived - 1; 101425107197SIan Dowse if (options & F_MISSED) 1015ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 1016039d6aa4SBill Fenner } 1017039d6aa4SBill Fenner } 101825107197SIan Dowse } 10198f975bb3SBruce Evans finish(); 10208fae3551SRodney W. Grimes /* NOTREACHED */ 1021f78ac61bSWarner Losh exit(0); /* Make the compiler happy */ 10228fae3551SRodney W. Grimes } 10238fae3551SRodney W. Grimes 10248fae3551SRodney W. Grimes /* 10258f975bb3SBruce Evans * stopit -- 10268f975bb3SBruce Evans * Set the global bit that causes the main loop to quit. 10278f975bb3SBruce Evans * Do NOT call finish() from here, since finish() does far too much 10288f975bb3SBruce Evans * to be called from a signal handler. 1029515dd2faSJulian Elischer */ 1030515dd2faSJulian Elischer void 1031fafb8f11SEd Schouten stopit(int sig __unused) 1032515dd2faSJulian Elischer { 1033efc8588dSDavid E. O'Brien 1034c8bb99e5SIan Dowse /* 1035c8bb99e5SIan Dowse * When doing reverse DNS lookups, the finish_up flag might not 1036c8bb99e5SIan Dowse * be noticed for a while. Just exit if we get a second SIGINT. 1037c8bb99e5SIan Dowse */ 1038c8bb99e5SIan Dowse if (!(options & F_NUMERIC) && finish_up) 1039c8bb99e5SIan Dowse _exit(nreceived ? 0 : 2); 1040515dd2faSJulian Elischer finish_up = 1; 1041515dd2faSJulian Elischer } 1042515dd2faSJulian Elischer 1043515dd2faSJulian Elischer /* 10448fae3551SRodney W. Grimes * pinger -- 10458fae3551SRodney W. Grimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 10468fae3551SRodney W. Grimes * will be added on by the kernel. The ID field is our UNIX process ID, 1047eb1543c6SMatthew N. Dodd * and the sequence number is an ascending integer. The first TIMEVAL_LEN 10481ad76f1bSAlan Somers * bytes of the data portion are used to hold a UNIX "timespec" struct in 10494fba6582SMaxim Konovalov * host byte-order, to compute the round-trip time. 10508fae3551SRodney W. Grimes */ 105143470e3bSGarrett Wollman static void 105243470e3bSGarrett Wollman pinger(void) 10538fae3551SRodney W. Grimes { 10541ad76f1bSAlan Somers struct timespec now; 105513e3f0b7SMaxim Konovalov struct tv32 tv32; 1056d9cacf60SAlan Somers struct icmp icp; 1057efc8588dSDavid E. O'Brien int cc, i; 10580b2f8b3fSMaxim Konovalov u_char *packet; 10598fae3551SRodney W. Grimes 10600b2f8b3fSMaxim Konovalov packet = outpack; 1061d9cacf60SAlan Somers memcpy(&icp, outpack, ICMP_MINLEN + phdr_len); 1062d9cacf60SAlan Somers icp.icmp_type = icmp_type; 1063d9cacf60SAlan Somers icp.icmp_code = 0; 1064d9cacf60SAlan Somers icp.icmp_cksum = 0; 1065d9cacf60SAlan Somers icp.icmp_seq = htons(ntransmitted); 1066d9cacf60SAlan Somers icp.icmp_id = ident; /* ID */ 10678fae3551SRodney W. Grimes 10685db89bc7SBill Fenner CLR(ntransmitted % mx_dup_ck); 10698fae3551SRodney W. Grimes 1070eb1543c6SMatthew N. Dodd if ((options & F_TIME) || timing) { 10711ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 10721ad76f1bSAlan Somers /* 10731ad76f1bSAlan Somers * Truncate seconds down to 32 bits in order 10741ad76f1bSAlan Somers * to fit the timestamp within 8 bytes of the 10751ad76f1bSAlan Somers * packet. We're only concerned with 10761ad76f1bSAlan Somers * durations, not absolute times. 10771ad76f1bSAlan Somers */ 10781ad76f1bSAlan Somers tv32.tv32_sec = (uint32_t)htonl(now.tv_sec); 10791ad76f1bSAlan Somers tv32.tv32_nsec = (uint32_t)htonl(now.tv_nsec); 1080eb1543c6SMatthew N. Dodd if (options & F_TIME) 1081d9cacf60SAlan Somers icp.icmp_otime = htonl((now.tv_sec % (24*60*60)) 10821ad76f1bSAlan Somers * 1000 + now.tv_nsec / 1000000); 1083eb1543c6SMatthew N. Dodd if (timing) 108413e3f0b7SMaxim Konovalov bcopy((void *)&tv32, 10850fe0c0ccSMaxim Konovalov (void *)&outpack[ICMP_MINLEN + phdr_len], 108613e3f0b7SMaxim Konovalov sizeof(tv32)); 1087eb1543c6SMatthew N. Dodd } 1088eb1543c6SMatthew N. Dodd 1089d9cacf60SAlan Somers memcpy(outpack, &icp, ICMP_MINLEN + phdr_len); 1090d9cacf60SAlan Somers 10910fe0c0ccSMaxim Konovalov cc = ICMP_MINLEN + phdr_len + datalen; 10928fae3551SRodney W. Grimes 10938fae3551SRodney W. Grimes /* compute ICMP checksum here */ 1094d9cacf60SAlan Somers icp.icmp_cksum = in_cksum(outpack, cc); 1095d9cacf60SAlan Somers /* Update icmp_cksum in the raw packet data buffer. */ 1096d9cacf60SAlan Somers memcpy(outpack + offsetof(struct icmp, icmp_cksum), &icp.icmp_cksum, 1097d9cacf60SAlan Somers sizeof(icp.icmp_cksum)); 10988fae3551SRodney W. Grimes 10990b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 1100d9cacf60SAlan Somers struct ip ip; 1101d9cacf60SAlan Somers 11020b2f8b3fSMaxim Konovalov cc += sizeof(struct ip); 1103d9cacf60SAlan Somers ip.ip_len = htons(cc); 1104d9cacf60SAlan Somers /* Update ip_len in the raw packet data buffer. */ 1105d9cacf60SAlan Somers memcpy(outpackhdr + offsetof(struct ip, ip_len), &ip.ip_len, 1106d9cacf60SAlan Somers sizeof(ip.ip_len)); 1107d9cacf60SAlan Somers ip.ip_sum = in_cksum(outpackhdr, cc); 1108d9cacf60SAlan Somers /* Update ip_sum in the raw packet data buffer. */ 1109d9cacf60SAlan Somers memcpy(outpackhdr + offsetof(struct ip, ip_sum), &ip.ip_sum, 1110d9cacf60SAlan Somers sizeof(ip.ip_sum)); 11110b2f8b3fSMaxim Konovalov packet = outpackhdr; 11120b2f8b3fSMaxim Konovalov } 111349133c6dSPawel Jakub Dawidek i = send(ssend, (char *)packet, cc, 0); 11148fae3551SRodney W. Grimes if (i < 0 || i != cc) { 111543470e3bSGarrett Wollman if (i < 0) { 11168f975bb3SBruce Evans if (options & F_FLOOD && errno == ENOBUFS) { 1117515dd2faSJulian Elischer usleep(FLOOD_BACKOFF); 1118515dd2faSJulian Elischer return; 1119515dd2faSJulian Elischer } 112043470e3bSGarrett Wollman warn("sendto"); 112143470e3bSGarrett Wollman } else { 112243470e3bSGarrett Wollman warn("%s: partial write: %d of %d bytes", 1123416aa49bSPoul-Henning Kamp hostname, i, cc); 11248fae3551SRodney W. Grimes } 1125363d7bbeSJulian Elischer } 1126363d7bbeSJulian Elischer ntransmitted++; 11279ff95228SGleb Smirnoff sntransmitted++; 1128d399eb3eSPiotr Pawel Stefaniak if (!(options & F_QUIET) && options & F_DOT) 1129d399eb3eSPiotr Pawel Stefaniak (void)write(STDOUT_FILENO, &DOT[DOTidx++ % DOTlen], 1); 11308fae3551SRodney W. Grimes } 11318fae3551SRodney W. Grimes 11328fae3551SRodney W. Grimes /* 11338fae3551SRodney W. Grimes * pr_pack -- 11348fae3551SRodney W. Grimes * Print out the packet, if it came from us. This logic is necessary 11358fae3551SRodney W. Grimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 11368fae3551SRodney W. Grimes * which arrive ('tis only fair). This permits multiple copies of this 11378fae3551SRodney W. Grimes * program to be run without having intermingled output (or statistics!). 11388fae3551SRodney W. Grimes */ 113943470e3bSGarrett Wollman static void 1140d9cacf60SAlan Somers pr_pack(char *buf, ssize_t cc, struct sockaddr_in *from, struct timespec *tv) 11418fae3551SRodney W. Grimes { 11429b219646SPeter Wemm struct in_addr ina; 1143d9cacf60SAlan Somers u_char *cp, *dp, l; 1144d9cacf60SAlan Somers struct icmp icp; 1145d9cacf60SAlan Somers struct ip ip; 1146d9cacf60SAlan Somers const u_char *icmp_data_raw; 114746d7b45aSTom Jones ssize_t icmp_data_raw_len; 11488f975bb3SBruce Evans double triptime; 114946d7b45aSTom Jones int dupflag, i, j, recv_len; 115070960bb8SCy Schubert int8_t hlen; 11512c29d74cSAlan Somers uint16_t seq; 1152efc8588dSDavid E. O'Brien static int old_rrlen; 1153efc8588dSDavid E. O'Brien static char old_rr[MAX_IPOPTLEN]; 1154d9cacf60SAlan Somers struct ip oip; 1155d9cacf60SAlan Somers u_char oip_header_len; 1156d9cacf60SAlan Somers struct icmp oicmp; 11574630a325SJose Luis Duran const u_char *oicmp_raw; 1158d9cacf60SAlan Somers 1159d9cacf60SAlan Somers /* 116046d7b45aSTom Jones * Get size of IP header of the received packet. 116146d7b45aSTom Jones * The header length is contained in the lower four bits of the first 116246d7b45aSTom Jones * byte and represents the number of 4 byte octets the header takes up. 116346d7b45aSTom Jones * 116446d7b45aSTom Jones * The IHL minimum value is 5 (20 bytes) and its maximum value is 15 116546d7b45aSTom Jones * (60 bytes). 1166d9cacf60SAlan Somers */ 1167d9cacf60SAlan Somers memcpy(&l, buf, sizeof(l)); 1168d9cacf60SAlan Somers hlen = (l & 0x0f) << 2; 11698fae3551SRodney W. Grimes 117046d7b45aSTom Jones /* Reject IP packets with a short header */ 117170960bb8SCy Schubert if (hlen < (int8_t) sizeof(struct ip)) { 117246d7b45aSTom Jones if (options & F_VERBOSE) 117346d7b45aSTom Jones warn("IHL too short (%d bytes) from %s", hlen, 117446d7b45aSTom Jones inet_ntoa(from->sin_addr)); 117546d7b45aSTom Jones return; 117646d7b45aSTom Jones } 117746d7b45aSTom Jones 117846d7b45aSTom Jones memcpy(&ip, buf, sizeof(struct ip)); 117946d7b45aSTom Jones 118046d7b45aSTom Jones /* Check packet has enough data to carry a valid ICMP header */ 1181e88178ddSMaxim Konovalov recv_len = cc; 11828fae3551SRodney W. Grimes if (cc < hlen + ICMP_MINLEN) { 11838fae3551SRodney W. Grimes if (options & F_VERBOSE) 1184d9cacf60SAlan Somers warn("packet too short (%zd bytes) from %s", cc, 118543470e3bSGarrett Wollman inet_ntoa(from->sin_addr)); 11868fae3551SRodney W. Grimes return; 11878fae3551SRodney W. Grimes } 11888fae3551SRodney W. Grimes 118946d7b45aSTom Jones icmp_data_raw_len = cc - (hlen + offsetof(struct icmp, icmp_data)); 1190d9cacf60SAlan Somers icmp_data_raw = buf + hlen + offsetof(struct icmp, icmp_data); 1191d9cacf60SAlan Somers 11928fae3551SRodney W. Grimes /* Now the ICMP part */ 11938fae3551SRodney W. Grimes cc -= hlen; 1194d9cacf60SAlan Somers memcpy(&icp, buf + hlen, MIN((ssize_t)sizeof(icp), cc)); 1195d9cacf60SAlan Somers if (icp.icmp_type == icmp_type_rsp) { 1196d9cacf60SAlan Somers if (icp.icmp_id != ident) 11978fae3551SRodney W. Grimes return; /* 'Twas not our ECHO */ 11988fae3551SRodney W. Grimes ++nreceived; 11998f975bb3SBruce Evans triptime = 0.0; 12008fae3551SRodney W. Grimes if (timing) { 12011ad76f1bSAlan Somers struct timespec tv1; 120213e3f0b7SMaxim Konovalov struct tv32 tv32; 1203d9cacf60SAlan Somers const u_char *tp; 1204d9cacf60SAlan Somers 1205d9cacf60SAlan Somers tp = icmp_data_raw + phdr_len; 1206143008a1SMatthew N. Dodd 12077e9489e0SHiroki Sato if ((size_t)(cc - ICMP_MINLEN - phdr_len) >= 12087e9489e0SHiroki Sato sizeof(tv1)) { 12099d2b0ab8SPeter Wemm /* Copy to avoid alignment problems: */ 121013e3f0b7SMaxim Konovalov memcpy(&tv32, tp, sizeof(tv32)); 121113e3f0b7SMaxim Konovalov tv1.tv_sec = ntohl(tv32.tv32_sec); 12121ad76f1bSAlan Somers tv1.tv_nsec = ntohl(tv32.tv32_nsec); 12131ad76f1bSAlan Somers timespecsub(tv, &tv1, tv); 1214039d6aa4SBill Fenner triptime = ((double)tv->tv_sec) * 1000.0 + 12151ad76f1bSAlan Somers ((double)tv->tv_nsec) / 1000000.0; 1216ea6d1692SJose Luis Duran if (triptime < 0) { 1217ea6d1692SJose Luis Duran warnx("time of day goes back (%.3f ms)," 1218ea6d1692SJose Luis Duran " clamping time to 0", 1219ea6d1692SJose Luis Duran triptime); 1220ea6d1692SJose Luis Duran triptime = 0; 1221ea6d1692SJose Luis Duran } 12228fae3551SRodney W. Grimes tsum += triptime; 12233109a910SGarrett Wollman tsumsq += triptime * triptime; 12248fae3551SRodney W. Grimes if (triptime < tmin) 12258fae3551SRodney W. Grimes tmin = triptime; 12268fae3551SRodney W. Grimes if (triptime > tmax) 12278fae3551SRodney W. Grimes tmax = triptime; 122847e9b3eaSMatthew N. Dodd } else 122947e9b3eaSMatthew N. Dodd timing = 0; 12308fae3551SRodney W. Grimes } 12318fae3551SRodney W. Grimes 1232d9cacf60SAlan Somers seq = ntohs(icp.icmp_seq); 12335db89bc7SBill Fenner 12345db89bc7SBill Fenner if (TST(seq % mx_dup_ck)) { 12358fae3551SRodney W. Grimes ++nrepeats; 12368fae3551SRodney W. Grimes --nreceived; 12378fae3551SRodney W. Grimes dupflag = 1; 12388fae3551SRodney W. Grimes } else { 12395db89bc7SBill Fenner SET(seq % mx_dup_ck); 12408fae3551SRodney W. Grimes dupflag = 0; 12418fae3551SRodney W. Grimes } 12428fae3551SRodney W. Grimes 12438fae3551SRodney W. Grimes if (options & F_QUIET) 12448fae3551SRodney W. Grimes return; 12458fae3551SRodney W. Grimes 1246d6cd1497SGleb Smirnoff if (options & F_WAITTIME && triptime > waittime) { 1247d6cd1497SGleb Smirnoff ++nrcvtimeout; 1248d6cd1497SGleb Smirnoff return; 1249d6cd1497SGleb Smirnoff } 1250d6cd1497SGleb Smirnoff 1251d399eb3eSPiotr Pawel Stefaniak if (options & F_DOT) 12528fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &BSPACE, 1); 12538fae3551SRodney W. Grimes else { 1254d9cacf60SAlan Somers (void)printf("%zd bytes from %s: icmp_seq=%u", cc, 1255229e8bf2SAlan Somers pr_addr(from->sin_addr), seq); 1256d9cacf60SAlan Somers (void)printf(" ttl=%d", ip.ip_ttl); 12578fae3551SRodney W. Grimes if (timing) 1258d410b6f1SDavid Greenman (void)printf(" time=%.3f ms", triptime); 12598fae3551SRodney W. Grimes if (dupflag) 12608fae3551SRodney W. Grimes (void)printf(" (DUP!)"); 1261772dfa72SDaniel O'Callaghan if (options & F_AUDIBLE) 1262ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 1263143008a1SMatthew N. Dodd if (options & F_MASK) { 1264143008a1SMatthew N. Dodd /* Just prentend this cast isn't ugly */ 1265143008a1SMatthew N. Dodd (void)printf(" mask=%s", 1266d9cacf60SAlan Somers inet_ntoa(*(struct in_addr *)&(icp.icmp_mask))); 1267143008a1SMatthew N. Dodd } 1268eb1543c6SMatthew N. Dodd if (options & F_TIME) { 1269d9cacf60SAlan Somers (void)printf(" tso=%s", pr_ntime(icp.icmp_otime)); 1270d9cacf60SAlan Somers (void)printf(" tsr=%s", pr_ntime(icp.icmp_rtime)); 1271d9cacf60SAlan Somers (void)printf(" tst=%s", pr_ntime(icp.icmp_ttime)); 1272eb1543c6SMatthew N. Dodd } 1273e88178ddSMaxim Konovalov if (recv_len != send_len) { 1274e88178ddSMaxim Konovalov (void)printf( 1275e88178ddSMaxim Konovalov "\nwrong total length %d instead of %d", 1276e88178ddSMaxim Konovalov recv_len, send_len); 1277e88178ddSMaxim Konovalov } 12788fae3551SRodney W. Grimes /* check the data */ 1279d9cacf60SAlan Somers cp = (u_char*)(buf + hlen + offsetof(struct icmp, 1280d9cacf60SAlan Somers icmp_data) + phdr_len); 12810fe0c0ccSMaxim Konovalov dp = &outpack[ICMP_MINLEN + phdr_len]; 128247e9b3eaSMatthew N. Dodd cc -= ICMP_MINLEN + phdr_len; 128329dccd6aSMaxim Konovalov i = 0; 128429dccd6aSMaxim Konovalov if (timing) { /* don't check variable timestamp */ 128529dccd6aSMaxim Konovalov cp += TIMEVAL_LEN; 128629dccd6aSMaxim Konovalov dp += TIMEVAL_LEN; 128729dccd6aSMaxim Konovalov cc -= TIMEVAL_LEN; 128829dccd6aSMaxim Konovalov i += TIMEVAL_LEN; 128929dccd6aSMaxim Konovalov } 129029dccd6aSMaxim Konovalov for (; i < datalen && cc > 0; ++i, ++cp, ++dp, --cc) { 12918fae3551SRodney W. Grimes if (*cp != *dp) { 12928fae3551SRodney W. Grimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 12938fae3551SRodney W. Grimes i, *dp, *cp); 12941ad0b1beSMaxim Konovalov (void)printf("\ncp:"); 1295d9cacf60SAlan Somers cp = (u_char*)(buf + hlen + 1296d9cacf60SAlan Somers offsetof(struct icmp, icmp_data)); 1297d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1298e88178ddSMaxim Konovalov if ((i % 16) == 8) 1299d32ff037SJohn Birrell (void)printf("\n\t"); 1300e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 1301d32ff037SJohn Birrell } 13021ad0b1beSMaxim Konovalov (void)printf("\ndp:"); 13030fe0c0ccSMaxim Konovalov cp = &outpack[ICMP_MINLEN]; 1304d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1305e88178ddSMaxim Konovalov if ((i % 16) == 8) 13068fae3551SRodney W. Grimes (void)printf("\n\t"); 1307e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 13088fae3551SRodney W. Grimes } 13098fae3551SRodney W. Grimes break; 13108fae3551SRodney W. Grimes } 13118fae3551SRodney W. Grimes } 13128fae3551SRodney W. Grimes } 13138fae3551SRodney W. Grimes } else { 1314ef9e6dc7SBill Fenner /* 1315ef9e6dc7SBill Fenner * We've got something other than an ECHOREPLY. 1316ef9e6dc7SBill Fenner * See if it's a reply to something that we sent. 1317ef9e6dc7SBill Fenner * We can compare IP destination, protocol, 1318ef9e6dc7SBill Fenner * and ICMP type and ID. 1319f78ac61bSWarner Losh * 1320f78ac61bSWarner Losh * Only print all the error messages if we are running 1321f78ac61bSWarner Losh * as root to avoid leaking information not normally 1322f78ac61bSWarner Losh * available to those not running as root. 1323ef9e6dc7SBill Fenner */ 132446d7b45aSTom Jones 132546d7b45aSTom Jones /* 132646d7b45aSTom Jones * If we don't have enough bytes for a quoted IP header and an 132746d7b45aSTom Jones * ICMP header then stop. 132846d7b45aSTom Jones */ 132946d7b45aSTom Jones if (icmp_data_raw_len < 133046d7b45aSTom Jones (ssize_t)(sizeof(struct ip) + sizeof(struct icmp))) { 133146d7b45aSTom Jones if (options & F_VERBOSE) 133246d7b45aSTom Jones warnx("quoted data too short (%zd bytes) from %s", 133346d7b45aSTom Jones icmp_data_raw_len, inet_ntoa(from->sin_addr)); 133446d7b45aSTom Jones return; 133546d7b45aSTom Jones } 133646d7b45aSTom Jones 1337d9cacf60SAlan Somers memcpy(&oip_header_len, icmp_data_raw, sizeof(oip_header_len)); 1338d9cacf60SAlan Somers oip_header_len = (oip_header_len & 0x0f) << 2; 133946d7b45aSTom Jones 134046d7b45aSTom Jones /* Reject IP packets with a short header */ 134146d7b45aSTom Jones if (oip_header_len < sizeof(struct ip)) { 134246d7b45aSTom Jones if (options & F_VERBOSE) 134346d7b45aSTom Jones warnx("inner IHL too short (%d bytes) from %s", 134446d7b45aSTom Jones oip_header_len, inet_ntoa(from->sin_addr)); 134546d7b45aSTom Jones return; 134646d7b45aSTom Jones } 134746d7b45aSTom Jones 134846d7b45aSTom Jones /* 134946d7b45aSTom Jones * Check against the actual IHL length, to protect against 135046d7b45aSTom Jones * quoated packets carrying IP options. 135146d7b45aSTom Jones */ 135246d7b45aSTom Jones if (icmp_data_raw_len < 135346d7b45aSTom Jones (ssize_t)(oip_header_len + sizeof(struct icmp))) { 135446d7b45aSTom Jones if (options & F_VERBOSE) 135546d7b45aSTom Jones warnx("inner packet too short (%zd bytes) from %s", 135646d7b45aSTom Jones icmp_data_raw_len, inet_ntoa(from->sin_addr)); 135746d7b45aSTom Jones return; 135846d7b45aSTom Jones } 135946d7b45aSTom Jones 136046d7b45aSTom Jones memcpy(&oip, icmp_data_raw, sizeof(struct ip)); 13614630a325SJose Luis Duran oicmp_raw = icmp_data_raw + oip_header_len; 13624630a325SJose Luis Duran memcpy(&oicmp, oicmp_raw, sizeof(struct icmp)); 1363ef9e6dc7SBill Fenner 1364ee2bf734SWarner Losh if (((options & F_VERBOSE) && uid == 0) || 1365ef9e6dc7SBill Fenner (!(options & F_QUIET2) && 1366d9cacf60SAlan Somers (oip.ip_dst.s_addr == whereto.sin_addr.s_addr) && 1367d9cacf60SAlan Somers (oip.ip_p == IPPROTO_ICMP) && 1368d9cacf60SAlan Somers (oicmp.icmp_type == ICMP_ECHO) && 1369d9cacf60SAlan Somers (oicmp.icmp_id == ident))) { 1370d9cacf60SAlan Somers (void)printf("%zd bytes from %s: ", cc, 137143470e3bSGarrett Wollman pr_addr(from->sin_addr)); 137220b41303SJose Luis Duran pr_icmph(&icp, &oip, icmp_data_raw); 1373ef9e6dc7SBill Fenner } else 1374ef9e6dc7SBill Fenner return; 13758fae3551SRodney W. Grimes } 13768fae3551SRodney W. Grimes 13778fae3551SRodney W. Grimes /* Display any IP options */ 13788fae3551SRodney W. Grimes cp = (u_char *)buf + sizeof(struct ip); 13798fae3551SRodney W. Grimes 13808fae3551SRodney W. Grimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 13818fae3551SRodney W. Grimes switch (*cp) { 13828fae3551SRodney W. Grimes case IPOPT_EOL: 13838fae3551SRodney W. Grimes hlen = 0; 13848fae3551SRodney W. Grimes break; 13858fae3551SRodney W. Grimes case IPOPT_LSRR: 1386cb75aca7SMaxim Konovalov case IPOPT_SSRR: 1387cb75aca7SMaxim Konovalov (void)printf(*cp == IPOPT_LSRR ? 1388cb75aca7SMaxim Konovalov "\nLSRR: " : "\nSSRR: "); 1389301207dfSMaxim Konovalov j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1; 13908fae3551SRodney W. Grimes hlen -= 2; 1391301207dfSMaxim Konovalov cp += 2; 13923c721ab3SMaxim Konovalov if (j >= INADDR_LEN && 13933c721ab3SMaxim Konovalov j <= hlen - (int)sizeof(struct ip)) { 13948fae3551SRodney W. Grimes for (;;) { 1395301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1396301207dfSMaxim Konovalov if (ina.s_addr == 0) 13971ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1398301207dfSMaxim Konovalov else 13991ad0b1beSMaxim Konovalov (void)printf("\t%s", 14001ad0b1beSMaxim Konovalov pr_addr(ina)); 1401301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1402301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1403301207dfSMaxim Konovalov j -= INADDR_LEN; 1404301207dfSMaxim Konovalov if (j < INADDR_LEN) 14058fae3551SRodney W. Grimes break; 14068fae3551SRodney W. Grimes (void)putchar('\n'); 14078fae3551SRodney W. Grimes } 1408301207dfSMaxim Konovalov } else 1409d2c9a140SJose Luis Duran (void)printf("\t(truncated route)"); 14108fae3551SRodney W. Grimes break; 14118fae3551SRodney W. Grimes case IPOPT_RR: 1412301207dfSMaxim Konovalov j = cp[IPOPT_OLEN]; /* get length */ 1413301207dfSMaxim Konovalov i = cp[IPOPT_OFFSET]; /* and pointer */ 14148fae3551SRodney W. Grimes hlen -= 2; 1415301207dfSMaxim Konovalov cp += 2; 14168fae3551SRodney W. Grimes if (i > j) 14178fae3551SRodney W. Grimes i = j; 1418301207dfSMaxim Konovalov i = i - IPOPT_MINOFF + 1; 1419301207dfSMaxim Konovalov if (i < 0 || i > (hlen - (int)sizeof(struct ip))) { 1420301207dfSMaxim Konovalov old_rrlen = 0; 14218fae3551SRodney W. Grimes continue; 1422301207dfSMaxim Konovalov } 14238fae3551SRodney W. Grimes if (i == old_rrlen 14248fae3551SRodney W. Grimes && !bcmp((char *)cp, old_rr, i) 1425d399eb3eSPiotr Pawel Stefaniak && !(options & F_DOT)) { 14268fae3551SRodney W. Grimes (void)printf("\t(same route)"); 14278fae3551SRodney W. Grimes hlen -= i; 14288fae3551SRodney W. Grimes cp += i; 14298fae3551SRodney W. Grimes break; 14308fae3551SRodney W. Grimes } 14318fae3551SRodney W. Grimes old_rrlen = i; 14328fae3551SRodney W. Grimes bcopy((char *)cp, old_rr, i); 14338fae3551SRodney W. Grimes (void)printf("\nRR: "); 1434301207dfSMaxim Konovalov if (i >= INADDR_LEN && 1435301207dfSMaxim Konovalov i <= hlen - (int)sizeof(struct ip)) { 14368fae3551SRodney W. Grimes for (;;) { 1437301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1438301207dfSMaxim Konovalov if (ina.s_addr == 0) 14391ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1440301207dfSMaxim Konovalov else 1441301207dfSMaxim Konovalov (void)printf("\t%s", 1442301207dfSMaxim Konovalov pr_addr(ina)); 1443301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1444301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1445301207dfSMaxim Konovalov i -= INADDR_LEN; 1446301207dfSMaxim Konovalov if (i < INADDR_LEN) 14478fae3551SRodney W. Grimes break; 14488fae3551SRodney W. Grimes (void)putchar('\n'); 14498fae3551SRodney W. Grimes } 1450301207dfSMaxim Konovalov } else 1451301207dfSMaxim Konovalov (void)printf("\t(truncated route)"); 14528fae3551SRodney W. Grimes break; 14538fae3551SRodney W. Grimes case IPOPT_NOP: 14548fae3551SRodney W. Grimes (void)printf("\nNOP"); 14558fae3551SRodney W. Grimes break; 14568fae3551SRodney W. Grimes default: 14578fae3551SRodney W. Grimes (void)printf("\nunknown option %x", *cp); 14588fae3551SRodney W. Grimes break; 14598fae3551SRodney W. Grimes } 1460d399eb3eSPiotr Pawel Stefaniak if (!(options & F_DOT)) { 14618fae3551SRodney W. Grimes (void)putchar('\n'); 14628fae3551SRodney W. Grimes (void)fflush(stdout); 14638fae3551SRodney W. Grimes } 14648fae3551SRodney W. Grimes } 14658fae3551SRodney W. Grimes 14668fae3551SRodney W. Grimes /* 1467badd8138SSean Eric Fagan * status -- 1468badd8138SSean Eric Fagan * Print out statistics when SIGINFO is received. 1469badd8138SSean Eric Fagan */ 1470badd8138SSean Eric Fagan 147143470e3bSGarrett Wollman static void 1472fafb8f11SEd Schouten status(int sig __unused) 14737d81b35cSBruce Evans { 1474efc8588dSDavid E. O'Brien 147537e5b2c6SSean Eric Fagan siginfo_p = 1; 147637e5b2c6SSean Eric Fagan } 147737e5b2c6SSean Eric Fagan 147843470e3bSGarrett Wollman static void 1479fafb8f11SEd Schouten check_status(void) 1480badd8138SSean Eric Fagan { 1481efc8588dSDavid E. O'Brien 148237e5b2c6SSean Eric Fagan if (siginfo_p) { 148337e5b2c6SSean Eric Fagan siginfo_p = 0; 1484aa822c39SDima Dorfman (void)fprintf(stderr, "\r%ld/%ld packets received (%.1f%%)", 14857d81b35cSBruce Evans nreceived, ntransmitted, 1486aed98a27SMaxim Konovalov ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0); 1487aed98a27SMaxim Konovalov if (nreceived && timing) 1488aed98a27SMaxim Konovalov (void)fprintf(stderr, " %.3f min / %.3f avg / %.3f max", 1489aed98a27SMaxim Konovalov tmin, tsum / (nreceived + nrepeats), tmax); 1490aed98a27SMaxim Konovalov (void)fprintf(stderr, "\n"); 149137e5b2c6SSean Eric Fagan } 1492badd8138SSean Eric Fagan } 1493badd8138SSean Eric Fagan 1494badd8138SSean Eric Fagan /* 14958fae3551SRodney W. Grimes * finish -- 14968fae3551SRodney W. Grimes * Print out statistics, and give up. 14978fae3551SRodney W. Grimes */ 149843470e3bSGarrett Wollman static void 1499fafb8f11SEd Schouten finish(void) 15008fae3551SRodney W. Grimes { 15018fae3551SRodney W. Grimes 15028fae3551SRodney W. Grimes (void)signal(SIGINT, SIG_IGN); 1503a2a00888SSean Eric Fagan (void)signal(SIGALRM, SIG_IGN); 15048fae3551SRodney W. Grimes (void)putchar('\n'); 15058fae3551SRodney W. Grimes (void)fflush(stdout); 15068fae3551SRodney W. Grimes (void)printf("--- %s ping statistics ---\n", hostname); 15078fae3551SRodney W. Grimes (void)printf("%ld packets transmitted, ", ntransmitted); 15088fae3551SRodney W. Grimes (void)printf("%ld packets received, ", nreceived); 15098fae3551SRodney W. Grimes if (nrepeats) 15108fae3551SRodney W. Grimes (void)printf("+%ld duplicates, ", nrepeats); 1511ebe70c8fSWarner Losh if (ntransmitted) { 15128fae3551SRodney W. Grimes if (nreceived > ntransmitted) 15138fae3551SRodney W. Grimes (void)printf("-- somebody's printing up packets!"); 15148fae3551SRodney W. Grimes else 1515aa822c39SDima Dorfman (void)printf("%.1f%% packet loss", 1516aa822c39SDima Dorfman ((ntransmitted - nreceived) * 100.0) / 1517aa822c39SDima Dorfman ntransmitted); 1518ebe70c8fSWarner Losh } 1519d6cd1497SGleb Smirnoff if (nrcvtimeout) 1520d6cd1497SGleb Smirnoff (void)printf(", %ld packets out of wait time", nrcvtimeout); 15218fae3551SRodney W. Grimes (void)putchar('\n'); 15223109a910SGarrett Wollman if (nreceived && timing) { 15233109a910SGarrett Wollman double n = nreceived + nrepeats; 15243109a910SGarrett Wollman double avg = tsum / n; 15254d348e83SJose Luis Duran double stddev = sqrt(fmax(0, tsumsq / n - avg * avg)); 15261ad0b1beSMaxim Konovalov (void)printf( 15271ad0b1beSMaxim Konovalov "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", 15284d348e83SJose Luis Duran tmin, avg, tmax, stddev); 15293109a910SGarrett Wollman } 1530badd8138SSean Eric Fagan 15316e1173dcSDavid Greenman if (nreceived) 15328fae3551SRodney W. Grimes exit(0); 15336e1173dcSDavid Greenman else 15346e1173dcSDavid Greenman exit(2); 15358fae3551SRodney W. Grimes } 15368fae3551SRodney W. Grimes 15378fae3551SRodney W. Grimes /* 15388fae3551SRodney W. Grimes * pr_icmph -- 15398fae3551SRodney W. Grimes * Print a descriptive string about an ICMP header. 15408fae3551SRodney W. Grimes */ 154143470e3bSGarrett Wollman static void 1542d9cacf60SAlan Somers pr_icmph(struct icmp *icp, struct ip *oip, const u_char *const oicmp_raw) 15438fae3551SRodney W. Grimes { 1544efc8588dSDavid E. O'Brien 15458fae3551SRodney W. Grimes switch(icp->icmp_type) { 15468fae3551SRodney W. Grimes case ICMP_ECHOREPLY: 15478fae3551SRodney W. Grimes (void)printf("Echo Reply\n"); 15488fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 15498fae3551SRodney W. Grimes break; 15508fae3551SRodney W. Grimes case ICMP_UNREACH: 15518fae3551SRodney W. Grimes switch(icp->icmp_code) { 15528fae3551SRodney W. Grimes case ICMP_UNREACH_NET: 15538fae3551SRodney W. Grimes (void)printf("Destination Net Unreachable\n"); 15548fae3551SRodney W. Grimes break; 15558fae3551SRodney W. Grimes case ICMP_UNREACH_HOST: 15568fae3551SRodney W. Grimes (void)printf("Destination Host Unreachable\n"); 15578fae3551SRodney W. Grimes break; 15588fae3551SRodney W. Grimes case ICMP_UNREACH_PROTOCOL: 15598fae3551SRodney W. Grimes (void)printf("Destination Protocol Unreachable\n"); 15608fae3551SRodney W. Grimes break; 15618fae3551SRodney W. Grimes case ICMP_UNREACH_PORT: 15628fae3551SRodney W. Grimes (void)printf("Destination Port Unreachable\n"); 15638fae3551SRodney W. Grimes break; 15648fae3551SRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 1565ef9e6dc7SBill Fenner (void)printf("frag needed and DF set (MTU %d)\n", 1566ff49597eSBill Fenner ntohs(icp->icmp_nextmtu)); 15678fae3551SRodney W. Grimes break; 15688fae3551SRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 15698fae3551SRodney W. Grimes (void)printf("Source Route Failed\n"); 15708fae3551SRodney W. Grimes break; 1571ef9e6dc7SBill Fenner case ICMP_UNREACH_FILTER_PROHIB: 1572ef9e6dc7SBill Fenner (void)printf("Communication prohibited by filter\n"); 1573ef9e6dc7SBill Fenner break; 15748fae3551SRodney W. Grimes default: 15758fae3551SRodney W. Grimes (void)printf("Dest Unreachable, Bad Code: %d\n", 15768fae3551SRodney W. Grimes icp->icmp_code); 15778fae3551SRodney W. Grimes break; 15788fae3551SRodney W. Grimes } 15798fae3551SRodney W. Grimes /* Print returned IP header information */ 15801dc1f6bdSJose Luis Duran pr_iph(oip, oicmp_raw); 15818fae3551SRodney W. Grimes break; 15828fae3551SRodney W. Grimes case ICMP_SOURCEQUENCH: 15838fae3551SRodney W. Grimes (void)printf("Source Quench\n"); 15841dc1f6bdSJose Luis Duran pr_iph(oip, oicmp_raw); 15858fae3551SRodney W. Grimes break; 15868fae3551SRodney W. Grimes case ICMP_REDIRECT: 15878fae3551SRodney W. Grimes switch(icp->icmp_code) { 15888fae3551SRodney W. Grimes case ICMP_REDIRECT_NET: 15898fae3551SRodney W. Grimes (void)printf("Redirect Network"); 15908fae3551SRodney W. Grimes break; 15918fae3551SRodney W. Grimes case ICMP_REDIRECT_HOST: 15928fae3551SRodney W. Grimes (void)printf("Redirect Host"); 15938fae3551SRodney W. Grimes break; 15948fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSNET: 15958fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Network"); 15968fae3551SRodney W. Grimes break; 15978fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSHOST: 15988fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Host"); 15998fae3551SRodney W. Grimes break; 16008fae3551SRodney W. Grimes default: 16018fae3551SRodney W. Grimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 16028fae3551SRodney W. Grimes break; 16038fae3551SRodney W. Grimes } 1604ff49597eSBill Fenner (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 16051dc1f6bdSJose Luis Duran pr_iph(oip, oicmp_raw); 16068fae3551SRodney W. Grimes break; 16078fae3551SRodney W. Grimes case ICMP_ECHO: 16088fae3551SRodney W. Grimes (void)printf("Echo Request\n"); 16098fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 16108fae3551SRodney W. Grimes break; 16118fae3551SRodney W. Grimes case ICMP_TIMXCEED: 16128fae3551SRodney W. Grimes switch(icp->icmp_code) { 16138fae3551SRodney W. Grimes case ICMP_TIMXCEED_INTRANS: 16148fae3551SRodney W. Grimes (void)printf("Time to live exceeded\n"); 16158fae3551SRodney W. Grimes break; 16168fae3551SRodney W. Grimes case ICMP_TIMXCEED_REASS: 16178fae3551SRodney W. Grimes (void)printf("Frag reassembly time exceeded\n"); 16188fae3551SRodney W. Grimes break; 16198fae3551SRodney W. Grimes default: 16208fae3551SRodney W. Grimes (void)printf("Time exceeded, Bad Code: %d\n", 16218fae3551SRodney W. Grimes icp->icmp_code); 16228fae3551SRodney W. Grimes break; 16238fae3551SRodney W. Grimes } 16241dc1f6bdSJose Luis Duran pr_iph(oip, oicmp_raw); 16258fae3551SRodney W. Grimes break; 16268fae3551SRodney W. Grimes case ICMP_PARAMPROB: 16278fae3551SRodney W. Grimes (void)printf("Parameter problem: pointer = 0x%02x\n", 16288fae3551SRodney W. Grimes icp->icmp_hun.ih_pptr); 16291dc1f6bdSJose Luis Duran pr_iph(oip, oicmp_raw); 16308fae3551SRodney W. Grimes break; 16318fae3551SRodney W. Grimes case ICMP_TSTAMP: 16328fae3551SRodney W. Grimes (void)printf("Timestamp\n"); 16338fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 16348fae3551SRodney W. Grimes break; 16358fae3551SRodney W. Grimes case ICMP_TSTAMPREPLY: 16368fae3551SRodney W. Grimes (void)printf("Timestamp Reply\n"); 16378fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 16388fae3551SRodney W. Grimes break; 16398fae3551SRodney W. Grimes case ICMP_IREQ: 16408fae3551SRodney W. Grimes (void)printf("Information Request\n"); 16418fae3551SRodney W. Grimes /* XXX ID + Seq */ 16428fae3551SRodney W. Grimes break; 16438fae3551SRodney W. Grimes case ICMP_IREQREPLY: 16448fae3551SRodney W. Grimes (void)printf("Information Reply\n"); 16458fae3551SRodney W. Grimes /* XXX ID + Seq */ 16468fae3551SRodney W. Grimes break; 16478fae3551SRodney W. Grimes case ICMP_MASKREQ: 16488fae3551SRodney W. Grimes (void)printf("Address Mask Request\n"); 16498fae3551SRodney W. Grimes break; 16508fae3551SRodney W. Grimes case ICMP_MASKREPLY: 16518fae3551SRodney W. Grimes (void)printf("Address Mask Reply\n"); 16528fae3551SRodney W. Grimes break; 1653ef9e6dc7SBill Fenner case ICMP_ROUTERADVERT: 1654ef9e6dc7SBill Fenner (void)printf("Router Advertisement\n"); 1655ef9e6dc7SBill Fenner break; 1656ef9e6dc7SBill Fenner case ICMP_ROUTERSOLICIT: 1657ef9e6dc7SBill Fenner (void)printf("Router Solicitation\n"); 1658ef9e6dc7SBill Fenner break; 16598fae3551SRodney W. Grimes default: 16608fae3551SRodney W. Grimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 16618fae3551SRodney W. Grimes } 16628fae3551SRodney W. Grimes } 16638fae3551SRodney W. Grimes 16648fae3551SRodney W. Grimes /* 16658fae3551SRodney W. Grimes * pr_iph -- 16668fae3551SRodney W. Grimes * Print an IP header with options. 16678fae3551SRodney W. Grimes */ 166843470e3bSGarrett Wollman static void 166920b41303SJose Luis Duran pr_iph(struct ip *ip, const u_char *cp) 16708fae3551SRodney W. Grimes { 1671a94c074dSDimitry Andric struct in_addr ina; 1672efc8588dSDavid E. O'Brien int hlen; 16738fae3551SRodney W. Grimes 16748fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 167520b41303SJose Luis Duran cp = cp + sizeof(struct ip); /* point to options */ 16768fae3551SRodney W. Grimes 1677ef9e6dc7SBill Fenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 16788fae3551SRodney W. Grimes (void)printf(" %1x %1x %02x %04x %04x", 1679ef9e6dc7SBill Fenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1680ef9e6dc7SBill Fenner ntohs(ip->ip_id)); 16819185854dSJose Luis Duran (void)printf(" %1x %04x", 16829185854dSJose Luis Duran (ntohs(ip->ip_off) & 0xe000) >> 13, 16839185854dSJose Luis Duran ntohs(ip->ip_off) & 0x1fff); 1684ef9e6dc7SBill Fenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1685ef9e6dc7SBill Fenner ntohs(ip->ip_sum)); 1686a94c074dSDimitry Andric memcpy(&ina, &ip->ip_src.s_addr, sizeof ina); 1687a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1688a94c074dSDimitry Andric memcpy(&ina, &ip->ip_dst.s_addr, sizeof ina); 1689a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1690ef9e6dc7SBill Fenner /* dump any option bytes */ 1691491263d7SJose Luis Duran while (hlen-- > (int)sizeof(struct ip)) { 16928fae3551SRodney W. Grimes (void)printf("%02x", *cp++); 16938fae3551SRodney W. Grimes } 16948fae3551SRodney W. Grimes (void)putchar('\n'); 16958fae3551SRodney W. Grimes } 16968fae3551SRodney W. Grimes 16978fae3551SRodney W. Grimes /* 16988fae3551SRodney W. Grimes * pr_addr -- 16998fae3551SRodney W. Grimes * Return an ascii host address as a dotted quad and optionally with 17008fae3551SRodney W. Grimes * a hostname. 17018fae3551SRodney W. Grimes */ 170243470e3bSGarrett Wollman static char * 1703fafb8f11SEd Schouten pr_addr(struct in_addr ina) 17048fae3551SRodney W. Grimes { 17058fae3551SRodney W. Grimes struct hostent *hp; 1706f78ac61bSWarner Losh static char buf[16 + 3 + MAXHOSTNAMELEN]; 17078fae3551SRodney W. Grimes 170849133c6dSPawel Jakub Dawidek if (options & F_NUMERIC) 170943470e3bSGarrett Wollman return inet_ntoa(ina); 171049133c6dSPawel Jakub Dawidek 1711491263d7SJose Luis Duran hp = cap_gethostbyaddr(capdns, (char *)&ina, sizeof(ina), AF_INET); 171249133c6dSPawel Jakub Dawidek 171349133c6dSPawel Jakub Dawidek if (hp == NULL) 171449133c6dSPawel Jakub Dawidek return inet_ntoa(ina); 171549133c6dSPawel Jakub Dawidek 1716efa38539SPeter Wemm (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 171743470e3bSGarrett Wollman inet_ntoa(ina)); 17188fae3551SRodney W. Grimes return(buf); 17198fae3551SRodney W. Grimes } 17208fae3551SRodney W. Grimes 1721eb1543c6SMatthew N. Dodd static char * 1722007fe4e3SMaxim Konovalov pr_ntime(n_time timestamp) 1723eb1543c6SMatthew N. Dodd { 17247898770aSAlan Somers static char buf[11]; 1725007fe4e3SMaxim Konovalov int hour, min, sec; 1726eb1543c6SMatthew N. Dodd 1727007fe4e3SMaxim Konovalov sec = ntohl(timestamp) / 1000; 1728007fe4e3SMaxim Konovalov hour = sec / 60 / 60; 1729007fe4e3SMaxim Konovalov min = (sec % (60 * 60)) / 60; 1730007fe4e3SMaxim Konovalov sec = (sec % (60 * 60)) % 60; 1731eb1543c6SMatthew N. Dodd 1732007fe4e3SMaxim Konovalov (void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hour, min, sec); 1733eb1543c6SMatthew N. Dodd 1734eb1543c6SMatthew N. Dodd return (buf); 1735eb1543c6SMatthew N. Dodd } 1736eb1543c6SMatthew N. Dodd 173743470e3bSGarrett Wollman static void 1738fafb8f11SEd Schouten fill(char *bp, char *patp) 17398fae3551SRodney W. Grimes { 17408fae3551SRodney W. Grimes char *cp; 1741efc8588dSDavid E. O'Brien int pat[16]; 17424fba6582SMaxim Konovalov u_int ii, jj, kk; 17438fae3551SRodney W. Grimes 174443470e3bSGarrett Wollman for (cp = patp; *cp; cp++) { 174543470e3bSGarrett Wollman if (!isxdigit(*cp)) 174643470e3bSGarrett Wollman errx(EX_USAGE, 174743470e3bSGarrett Wollman "patterns must be specified as hex digits"); 174843470e3bSGarrett Wollman 17498fae3551SRodney W. Grimes } 17508fae3551SRodney W. Grimes ii = sscanf(patp, 17518fae3551SRodney W. Grimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 17528fae3551SRodney W. Grimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 17538fae3551SRodney W. Grimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 17548fae3551SRodney W. Grimes &pat[13], &pat[14], &pat[15]); 17558fae3551SRodney W. Grimes 17568fae3551SRodney W. Grimes if (ii > 0) 1757d829c3dfSMatthew N. Dodd for (kk = 0; kk <= maxpayload - (TIMEVAL_LEN + ii); kk += ii) 17588fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 17598fae3551SRodney W. Grimes bp[jj + kk] = pat[jj]; 17608fae3551SRodney W. Grimes if (!(options & F_QUIET)) { 17618fae3551SRodney W. Grimes (void)printf("PATTERN: 0x"); 17628fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 17638fae3551SRodney W. Grimes (void)printf("%02x", bp[jj] & 0xFF); 17648fae3551SRodney W. Grimes (void)printf("\n"); 17658fae3551SRodney W. Grimes } 17668fae3551SRodney W. Grimes } 17678fae3551SRodney W. Grimes 176849133c6dSPawel Jakub Dawidek static cap_channel_t * 176949133c6dSPawel Jakub Dawidek capdns_setup(void) 177049133c6dSPawel Jakub Dawidek { 177149133c6dSPawel Jakub Dawidek cap_channel_t *capcas, *capdnsloc; 1772a3ce7698SAlan Somers #ifdef WITH_CASPER 177349133c6dSPawel Jakub Dawidek const char *types[2]; 177449133c6dSPawel Jakub Dawidek int families[1]; 1775a3ce7698SAlan Somers #endif 177649133c6dSPawel Jakub Dawidek capcas = cap_init(); 1777c501d73cSMariusz Zaborski if (capcas == NULL) 1778c501d73cSMariusz Zaborski err(1, "unable to create casper process"); 177949133c6dSPawel Jakub Dawidek capdnsloc = cap_service_open(capcas, "system.dns"); 178049133c6dSPawel Jakub Dawidek /* Casper capability no longer needed. */ 178149133c6dSPawel Jakub Dawidek cap_close(capcas); 178249133c6dSPawel Jakub Dawidek if (capdnsloc == NULL) 178349133c6dSPawel Jakub Dawidek err(1, "unable to open system.dns service"); 1784a3ce7698SAlan Somers #ifdef WITH_CASPER 1785752d135eSMariusz Zaborski types[0] = "NAME2ADDR"; 1786752d135eSMariusz Zaborski types[1] = "ADDR2NAME"; 178749133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdnsloc, types, 2) < 0) 178849133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 178949133c6dSPawel Jakub Dawidek families[0] = AF_INET; 179049133c6dSPawel Jakub Dawidek if (cap_dns_family_limit(capdnsloc, families, 1) < 0) 179149133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 1792a3ce7698SAlan Somers #endif 179349133c6dSPawel Jakub Dawidek return (capdnsloc); 179449133c6dSPawel Jakub Dawidek } 1795