18fae3551SRodney W. Grimes /* 28fae3551SRodney W. Grimes * Copyright (c) 1989, 1993 38fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 48fae3551SRodney W. Grimes * 58fae3551SRodney W. Grimes * This code is derived from software contributed to Berkeley by 68fae3551SRodney W. Grimes * Mike Muuss. 78fae3551SRodney W. Grimes * 88fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 98fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 108fae3551SRodney W. Grimes * are met: 118fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 128fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 138fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 148fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 158fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 168fae3551SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 178fae3551SRodney W. Grimes * must display the following acknowledgement: 188fae3551SRodney W. Grimes * This product includes software developed by the University of 198fae3551SRodney W. Grimes * California, Berkeley and its contributors. 208fae3551SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 218fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 228fae3551SRodney W. Grimes * without specific prior written permission. 238fae3551SRodney W. Grimes * 248fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 258fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 268fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 278fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 288fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 298fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 308fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 318fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 328fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 338fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 348fae3551SRodney W. Grimes * SUCH DAMAGE. 358fae3551SRodney W. Grimes */ 368fae3551SRodney W. Grimes 378fae3551SRodney W. Grimes #ifndef lint 3843470e3bSGarrett Wollman static const char copyright[] = 398fae3551SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\ 408fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 418fae3551SRodney W. Grimes #endif /* not lint */ 428fae3551SRodney W. Grimes 438fae3551SRodney W. Grimes #ifndef lint 44e345a80dSPhilippe Charnier #if 0 458fae3551SRodney W. Grimes static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93"; 46e345a80dSPhilippe Charnier #endif 4743470e3bSGarrett Wollman static const char rcsid[] = 487f3dea24SPeter Wemm "$FreeBSD$"; 498fae3551SRodney W. Grimes #endif /* not lint */ 508fae3551SRodney W. Grimes 518fae3551SRodney W. Grimes /* 528fae3551SRodney W. Grimes * P I N G . C 538fae3551SRodney W. Grimes * 54e345a80dSPhilippe Charnier * Using the Internet Control Message Protocol (ICMP) "ECHO" facility, 558fae3551SRodney W. Grimes * measure round-trip-delays and packet loss across network paths. 568fae3551SRodney W. Grimes * 578fae3551SRodney W. Grimes * Author - 588fae3551SRodney W. Grimes * Mike Muuss 598fae3551SRodney W. Grimes * U. S. Army Ballistic Research Laboratory 608fae3551SRodney W. Grimes * December, 1983 618fae3551SRodney W. Grimes * 628fae3551SRodney W. Grimes * Status - 638fae3551SRodney W. Grimes * Public Domain. Distribution Unlimited. 648fae3551SRodney W. Grimes * Bugs - 658fae3551SRodney W. Grimes * More statistics could always be gathered. 668fae3551SRodney W. Grimes * This program has to run SUID to ROOT to access the ICMP socket. 678fae3551SRodney W. Grimes */ 688fae3551SRodney W. Grimes 6943470e3bSGarrett Wollman #include <sys/param.h> /* NB: we rely on this for <sys/types.h> */ 708fae3551SRodney W. Grimes #include <sys/socket.h> 71261e59bbSMaxim Konovalov #include <sys/sysctl.h> 728fae3551SRodney W. Grimes #include <sys/time.h> 73039d6aa4SBill Fenner #include <sys/uio.h> 748fae3551SRodney W. Grimes 758fae3551SRodney W. Grimes #include <netinet/in.h> 7643470e3bSGarrett Wollman #include <netinet/in_systm.h> 778fae3551SRodney W. Grimes #include <netinet/ip.h> 788fae3551SRodney W. Grimes #include <netinet/ip_icmp.h> 798fae3551SRodney W. Grimes #include <netinet/ip_var.h> 8043470e3bSGarrett Wollman #include <arpa/inet.h> 818fae3551SRodney W. Grimes 829a4365d0SYoshinobu Inoue #ifdef IPSEC 839a4365d0SYoshinobu Inoue #include <netinet6/ipsec.h> 849a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 859a4365d0SYoshinobu Inoue 86261e59bbSMaxim Konovalov #include <ctype.h> 87261e59bbSMaxim Konovalov #include <err.h> 88261e59bbSMaxim Konovalov #include <errno.h> 89261e59bbSMaxim Konovalov #include <math.h> 90261e59bbSMaxim Konovalov #include <netdb.h> 91261e59bbSMaxim Konovalov #include <signal.h> 92261e59bbSMaxim Konovalov #include <stdio.h> 93261e59bbSMaxim Konovalov #include <stdlib.h> 94261e59bbSMaxim Konovalov #include <string.h> 95261e59bbSMaxim Konovalov #include <sysexits.h> 96261e59bbSMaxim Konovalov #include <termios.h> 97261e59bbSMaxim Konovalov #include <unistd.h> 98261e59bbSMaxim Konovalov 99301207dfSMaxim Konovalov #define INADDR_LEN ((int)sizeof(in_addr_t)) 100c6facae4SMaxim Konovalov #define PHDR_LEN ((int)sizeof(struct timeval)) 101d32ff037SJohn Birrell #define DEFDATALEN (64 - PHDR_LEN) /* default data length */ 1028f975bb3SBruce Evans #define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */ 1038f975bb3SBruce Evans /* runs out of buffer space */ 1044fba6582SMaxim Konovalov #define MAXIPLEN (sizeof(struct ip) + MAX_IPOPTLEN) 1054fba6582SMaxim Konovalov #define MAXICMPLEN (ICMP_ADVLENMIN + MAX_IPOPTLEN) 1064fba6582SMaxim Konovalov #define MINICMPLEN ICMP_MINLEN 107143008a1SMatthew N. Dodd #define MASKLEN (options & F_MASK ? 4 : 0) 1088fae3551SRodney W. Grimes #define MAXWAIT 10 /* max seconds to wait for response */ 109bf113f1bSBill Fumerola #define MAXALARM (60 * 60) /* max seconds for alarm timeout */ 1100b2f8b3fSMaxim Konovalov #define MAXTOS 255 1118fae3551SRodney W. Grimes 1128fae3551SRodney W. Grimes #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 1138fae3551SRodney W. Grimes #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 1148fae3551SRodney W. Grimes #define SET(bit) (A(bit) |= B(bit)) 1158fae3551SRodney W. Grimes #define CLR(bit) (A(bit) &= (~B(bit))) 1168fae3551SRodney W. Grimes #define TST(bit) (A(bit) & B(bit)) 1178fae3551SRodney W. Grimes 1188fae3551SRodney W. Grimes /* various options */ 1198fae3551SRodney W. Grimes int options; 12085456935SBill Fenner #define F_FLOOD 0x0001 12185456935SBill Fenner #define F_INTERVAL 0x0002 12285456935SBill Fenner #define F_NUMERIC 0x0004 12385456935SBill Fenner #define F_PINGFILLED 0x0008 12485456935SBill Fenner #define F_QUIET 0x0010 12585456935SBill Fenner #define F_RROUTE 0x0020 12685456935SBill Fenner #define F_SO_DEBUG 0x0040 12785456935SBill Fenner #define F_SO_DONTROUTE 0x0080 12885456935SBill Fenner #define F_VERBOSE 0x0100 12985456935SBill Fenner #define F_QUIET2 0x0200 13085456935SBill Fenner #define F_NOLOOP 0x0400 13185456935SBill Fenner #define F_MTTL 0x0800 13285456935SBill Fenner #define F_MIF 0x1000 133772dfa72SDaniel O'Callaghan #define F_AUDIBLE 0x2000 1349a4365d0SYoshinobu Inoue #ifdef IPSEC 1359a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 1369a4365d0SYoshinobu Inoue #define F_POLICY 0x4000 1379a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 1389a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 139211bfbd2SRuslan Ermilov #define F_TTL 0x8000 140ca517ad8SPoul-Henning Kamp #define F_MISSED 0x10000 1418025c44bSDima Dorfman #define F_ONCE 0x20000 1420b2f8b3fSMaxim Konovalov #define F_HDRINCL 0x40000 143143008a1SMatthew N. Dodd #define F_MASK 0x80000 1448fae3551SRodney W. Grimes 1458fae3551SRodney W. Grimes /* 1468fae3551SRodney W. Grimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 1478fae3551SRodney W. Grimes * number of received sequence numbers we can keep track of. Change 128 1488fae3551SRodney W. Grimes * to 8192 for complete accuracy... 1498fae3551SRodney W. Grimes */ 1508fae3551SRodney W. Grimes #define MAX_DUP_CHK (8 * 128) 1518fae3551SRodney W. Grimes int mx_dup_ck = MAX_DUP_CHK; 1528fae3551SRodney W. Grimes char rcvd_tbl[MAX_DUP_CHK / 8]; 1538fae3551SRodney W. Grimes 154d389e86aSMatt Jacob struct sockaddr_in whereto; /* who to ping */ 155fb7d32c7SMaxim Konovalov long maxpayload; 1568fae3551SRodney W. Grimes int datalen = DEFDATALEN; 1578fae3551SRodney W. Grimes int s; /* socket file descriptor */ 1580b2f8b3fSMaxim Konovalov u_char outpackhdr[IP_MAXPACKET], *outpack; 159ca517ad8SPoul-Henning Kamp char BBELL = '\a'; /* characters written for MISSED and AUDIBLE */ 160261e59bbSMaxim Konovalov char BSPACE = '\b'; /* characters written for flood */ 1618fae3551SRodney W. Grimes char DOT = '.'; 1628fae3551SRodney W. Grimes char *hostname; 16399490edeSWarner Losh char *shostname; 1648fae3551SRodney W. Grimes int ident; /* process id to identify our packets */ 165ee2bf734SWarner Losh int uid; /* cached uid for micro-optimization */ 1668fae3551SRodney W. Grimes 1678fae3551SRodney W. Grimes /* counters */ 168261e59bbSMaxim Konovalov long nmissedmax; /* max value of ntransmitted - nreceived - 1 */ 1698fae3551SRodney W. Grimes long npackets; /* max packets to transmit */ 1708fae3551SRodney W. Grimes long nreceived; /* # of packets we got back */ 1718fae3551SRodney W. Grimes long nrepeats; /* number of duplicates */ 1728fae3551SRodney W. Grimes long ntransmitted; /* sequence # for outbound packets = #sent */ 173526f06b2SMatthew Dillon int interval = 1000; /* interval between packets, ms */ 1748fae3551SRodney W. Grimes 1758fae3551SRodney W. Grimes /* timing */ 1768fae3551SRodney W. Grimes int timing; /* flag to do timing */ 1778fae3551SRodney W. Grimes double tmin = 999999999.0; /* minimum round trip time */ 1788fae3551SRodney W. Grimes double tmax = 0.0; /* maximum round trip time */ 1798fae3551SRodney W. Grimes double tsum = 0.0; /* sum of all times, for doing average */ 1803109a910SGarrett Wollman double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 1818fae3551SRodney W. Grimes 1828f975bb3SBruce Evans volatile sig_atomic_t finish_up; /* nonzero if we've been told to finish up */ 183badd8138SSean Eric Fagan int reset_kerninfo; 1848f975bb3SBruce Evans volatile sig_atomic_t siginfo_p; 185badd8138SSean Eric Fagan 18643470e3bSGarrett Wollman static void fill(char *, char *); 18743470e3bSGarrett Wollman static u_short in_cksum(u_short *, int); 18843470e3bSGarrett Wollman static void check_status(void); 1898f975bb3SBruce Evans static void finish(void) __dead2; 19043470e3bSGarrett Wollman static void pinger(void); 19143470e3bSGarrett Wollman static char *pr_addr(struct in_addr); 19243470e3bSGarrett Wollman static void pr_icmph(struct icmp *); 19343470e3bSGarrett Wollman static void pr_iph(struct ip *); 194039d6aa4SBill Fenner static void pr_pack(char *, int, struct sockaddr_in *, struct timeval *); 19543470e3bSGarrett Wollman static void pr_retip(struct ip *); 19643470e3bSGarrett Wollman static void status(int); 1978f975bb3SBruce Evans static void stopit(int); 19843470e3bSGarrett Wollman static void tvsub(struct timeval *, struct timeval *); 199e345a80dSPhilippe Charnier static void usage(void) __dead2; 2008fae3551SRodney W. Grimes 20143470e3bSGarrett Wollman int 2028fae3551SRodney W. Grimes main(argc, argv) 2038fae3551SRodney W. Grimes int argc; 20443470e3bSGarrett Wollman char *const *argv; 2058fae3551SRodney W. Grimes { 206261e59bbSMaxim Konovalov struct sockaddr_in from, sin; 207efc8588dSDavid E. O'Brien struct in_addr ifaddr; 208261e59bbSMaxim Konovalov struct timeval last, intvl; 209efc8588dSDavid E. O'Brien struct iovec iov; 2100b2f8b3fSMaxim Konovalov struct ip *ip; 211efc8588dSDavid E. O'Brien struct msghdr msg; 212efc8588dSDavid E. O'Brien struct sigaction si_sa; 213efc8588dSDavid E. O'Brien struct termios ts; 2140b2f8b3fSMaxim Konovalov size_t sz; 2154fba6582SMaxim Konovalov u_char *datap, packet[IP_MAXPACKET]; 216efc8588dSDavid E. O'Brien char *ep, *source, *target; 217261e59bbSMaxim Konovalov struct hostent *hp; 218efc8588dSDavid E. O'Brien #ifdef IPSEC_POLICY_IPSEC 219efc8588dSDavid E. O'Brien char *policy_in, *policy_out; 220efc8588dSDavid E. O'Brien #endif 221261e59bbSMaxim Konovalov struct sockaddr_in *to; 222261e59bbSMaxim Konovalov double t; 223efc8588dSDavid E. O'Brien u_long alarmtimeout, ultmp; 224261e59bbSMaxim Konovalov int almost_done, ch, df, hold, i, mib[4], packlen, preload, sockerrno, 225261e59bbSMaxim Konovalov tos, ttl; 226efc8588dSDavid E. O'Brien char ctrl[CMSG_SPACE(sizeof(struct timeval))]; 227efc8588dSDavid E. O'Brien char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN]; 2288fae3551SRodney W. Grimes #ifdef IP_OPTIONS 2294fba6582SMaxim Konovalov char rspace[MAX_IPOPTLEN]; /* record route space */ 2308fae3551SRodney W. Grimes #endif 231261e59bbSMaxim Konovalov unsigned char loop, mttl; 232efc8588dSDavid E. O'Brien 233efc8588dSDavid E. O'Brien source = NULL; 2349a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 235efc8588dSDavid E. O'Brien policy_in = policy_out = NULL; 2369a4365d0SYoshinobu Inoue #endif 2378fae3551SRodney W. Grimes 238f1284d7aSBill Fenner /* 239f1284d7aSBill Fenner * Do the stuff that we need root priv's for *first*, and 240f1284d7aSBill Fenner * then drop our setuid bit. Save error reporting for 241f1284d7aSBill Fenner * after arg parsing. 242f1284d7aSBill Fenner */ 24343470e3bSGarrett Wollman s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 244f1284d7aSBill Fenner sockerrno = errno; 245f1284d7aSBill Fenner 246f1284d7aSBill Fenner setuid(getuid()); 247ee2bf734SWarner Losh uid = getuid(); 248f1284d7aSBill Fenner 2490b2f8b3fSMaxim Konovalov alarmtimeout = df = preload = tos = 0; 250badd8138SSean Eric Fagan 2510b2f8b3fSMaxim Konovalov outpack = outpackhdr + sizeof(struct ip); 2524fba6582SMaxim Konovalov datap = &outpack[MINICMPLEN + PHDR_LEN]; 253211bfbd2SRuslan Ermilov while ((ch = getopt(argc, argv, 254143008a1SMatthew N. Dodd "ADI:LQRS:T:c:adfi:l:m:Mnop:qrs:t:vz:" 255211bfbd2SRuslan Ermilov #ifdef IPSEC 2569a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 257211bfbd2SRuslan Ermilov "P:" 2589a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 259211bfbd2SRuslan Ermilov #endif /*IPSEC*/ 260211bfbd2SRuslan Ermilov )) != -1) 2619a4365d0SYoshinobu Inoue { 2628fae3551SRodney W. Grimes switch(ch) { 263ca517ad8SPoul-Henning Kamp case 'A': 264ca517ad8SPoul-Henning Kamp options |= F_MISSED; 265ca517ad8SPoul-Henning Kamp break; 266772dfa72SDaniel O'Callaghan case 'a': 267772dfa72SDaniel O'Callaghan options |= F_AUDIBLE; 268772dfa72SDaniel O'Callaghan break; 2698fae3551SRodney W. Grimes case 'c': 27043470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 27143470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp) 27243470e3bSGarrett Wollman errx(EX_USAGE, 27343470e3bSGarrett Wollman "invalid count of packets to transmit: `%s'", 27443470e3bSGarrett Wollman optarg); 27543470e3bSGarrett Wollman npackets = ultmp; 2768fae3551SRodney W. Grimes break; 2770b2f8b3fSMaxim Konovalov case 'D': 2780b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 2790b2f8b3fSMaxim Konovalov df = 1; 2800b2f8b3fSMaxim Konovalov break; 2818fae3551SRodney W. Grimes case 'd': 2828fae3551SRodney W. Grimes options |= F_SO_DEBUG; 2838fae3551SRodney W. Grimes break; 2848fae3551SRodney W. Grimes case 'f': 285526f06b2SMatthew Dillon if (uid) { 28643470e3bSGarrett Wollman errno = EPERM; 28743470e3bSGarrett Wollman err(EX_NOPERM, "-f flag"); 2888fae3551SRodney W. Grimes } 2898fae3551SRodney W. Grimes options |= F_FLOOD; 2908fae3551SRodney W. Grimes setbuf(stdout, (char *)NULL); 2918fae3551SRodney W. Grimes break; 2928fae3551SRodney W. Grimes case 'i': /* wait between sending packets */ 2931ad0b1beSMaxim Konovalov t = strtod(optarg, &ep) * 1000.0; 2941ad0b1beSMaxim Konovalov if (*ep || ep == optarg || t > (double)INT_MAX) 2951ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid timing interval: `%s'", 2961ad0b1beSMaxim Konovalov optarg); 2978fae3551SRodney W. Grimes options |= F_INTERVAL; 298526f06b2SMatthew Dillon interval = (int)t; 299526f06b2SMatthew Dillon if (uid && interval < 1000) { 300526f06b2SMatthew Dillon errno = EPERM; 301526f06b2SMatthew Dillon err(EX_NOPERM, "-i interval too short"); 302526f06b2SMatthew Dillon } 3038fae3551SRodney W. Grimes break; 30485456935SBill Fenner case 'I': /* multicast interface */ 30543470e3bSGarrett Wollman if (inet_aton(optarg, &ifaddr) == 0) 30643470e3bSGarrett Wollman errx(EX_USAGE, 30743470e3bSGarrett Wollman "invalid multicast interface: `%s'", 30843470e3bSGarrett Wollman optarg); 30985456935SBill Fenner options |= F_MIF; 31085456935SBill Fenner break; 3118fae3551SRodney W. Grimes case 'l': 31243470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 31343470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > INT_MAX) 31443470e3bSGarrett Wollman errx(EX_USAGE, 31543470e3bSGarrett Wollman "invalid preload value: `%s'", optarg); 3165e2cc0f4SStephen McKay if (uid) { 317f78ac61bSWarner Losh errno = EPERM; 318f78ac61bSWarner Losh err(EX_NOPERM, "-l flag"); 319f78ac61bSWarner Losh } 32043470e3bSGarrett Wollman preload = ultmp; 3218fae3551SRodney W. Grimes break; 32285456935SBill Fenner case 'L': 32385456935SBill Fenner options |= F_NOLOOP; 32485456935SBill Fenner loop = 0; 32585456935SBill Fenner break; 326211bfbd2SRuslan Ermilov case 'm': /* TTL */ 327211bfbd2SRuslan Ermilov ultmp = strtoul(optarg, &ep, 0); 3289bc1a9ecSMaxim Konovalov if (*ep || ep == optarg || ultmp > MAXTTL) 3291ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid TTL: `%s'", optarg); 330211bfbd2SRuslan Ermilov ttl = ultmp; 331211bfbd2SRuslan Ermilov options |= F_TTL; 332211bfbd2SRuslan Ermilov break; 333143008a1SMatthew N. Dodd case 'M': 334143008a1SMatthew N. Dodd options |= F_MASK; 335143008a1SMatthew N. Dodd break; 3368fae3551SRodney W. Grimes case 'n': 3378fae3551SRodney W. Grimes options |= F_NUMERIC; 3388fae3551SRodney W. Grimes break; 3398025c44bSDima Dorfman case 'o': 3408025c44bSDima Dorfman options |= F_ONCE; 3418025c44bSDima Dorfman break; 3428fae3551SRodney W. Grimes case 'p': /* fill buffer with user pattern */ 3438fae3551SRodney W. Grimes options |= F_PINGFILLED; 3448fae3551SRodney W. Grimes fill((char *)datap, optarg); 3458fae3551SRodney W. Grimes break; 346ef9e6dc7SBill Fenner case 'Q': 347ef9e6dc7SBill Fenner options |= F_QUIET2; 348ef9e6dc7SBill Fenner break; 3498fae3551SRodney W. Grimes case 'q': 3508fae3551SRodney W. Grimes options |= F_QUIET; 3518fae3551SRodney W. Grimes break; 3528fae3551SRodney W. Grimes case 'R': 3538fae3551SRodney W. Grimes options |= F_RROUTE; 3548fae3551SRodney W. Grimes break; 3558fae3551SRodney W. Grimes case 'r': 3568fae3551SRodney W. Grimes options |= F_SO_DONTROUTE; 3578fae3551SRodney W. Grimes break; 3588fae3551SRodney W. Grimes case 's': /* size of packet to send */ 35943470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 3604fba6582SMaxim Konovalov if (*ep || ep == optarg) 36143470e3bSGarrett Wollman errx(EX_USAGE, "invalid packet size: `%s'", 36243470e3bSGarrett Wollman optarg); 363fb7d32c7SMaxim Konovalov if (uid != 0 && ultmp > DEFDATALEN) { 364fb7d32c7SMaxim Konovalov errno = EPERM; 365fb7d32c7SMaxim Konovalov err(EX_NOPERM, 366fb7d32c7SMaxim Konovalov "packet size too large: %lu > %u", 367fb7d32c7SMaxim Konovalov ultmp, DEFDATALEN); 368fb7d32c7SMaxim Konovalov } 36943470e3bSGarrett Wollman datalen = ultmp; 3708fae3551SRodney W. Grimes break; 37199490edeSWarner Losh case 'S': 37299490edeSWarner Losh source = optarg; 37399490edeSWarner Losh break; 3747237fd94SBill Fumerola case 't': 375bf113f1bSBill Fumerola alarmtimeout = strtoul(optarg, &ep, 0); 376bf113f1bSBill Fumerola if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX)) 3777237fd94SBill Fumerola errx(EX_USAGE, "invalid timeout: `%s'", 3787237fd94SBill Fumerola optarg); 379bf113f1bSBill Fumerola if (alarmtimeout > MAXALARM) 380bf113f1bSBill Fumerola errx(EX_USAGE, "invalid timeout: `%s' > %d", 381bf113f1bSBill Fumerola optarg, MAXALARM); 382bf113f1bSBill Fumerola alarm((int)alarmtimeout); 3837237fd94SBill Fumerola break; 38485456935SBill Fenner case 'T': /* multicast TTL */ 38543470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 3869bc1a9ecSMaxim Konovalov if (*ep || ep == optarg || ultmp > MAXTTL) 38743470e3bSGarrett Wollman errx(EX_USAGE, "invalid multicast TTL: `%s'", 38843470e3bSGarrett Wollman optarg); 389211bfbd2SRuslan Ermilov mttl = ultmp; 39085456935SBill Fenner options |= F_MTTL; 39185456935SBill Fenner break; 3928fae3551SRodney W. Grimes case 'v': 3938fae3551SRodney W. Grimes options |= F_VERBOSE; 3948fae3551SRodney W. Grimes break; 3959a4365d0SYoshinobu Inoue #ifdef IPSEC 3969a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 3979a4365d0SYoshinobu Inoue case 'P': 3989a4365d0SYoshinobu Inoue options |= F_POLICY; 3999a4365d0SYoshinobu Inoue if (!strncmp("in", optarg, 2)) 4009a4365d0SYoshinobu Inoue policy_in = strdup(optarg); 4019a4365d0SYoshinobu Inoue else if (!strncmp("out", optarg, 3)) 4029a4365d0SYoshinobu Inoue policy_out = strdup(optarg); 4039a4365d0SYoshinobu Inoue else 4049a4365d0SYoshinobu Inoue errx(1, "invalid security policy"); 4059a4365d0SYoshinobu Inoue break; 4060b2f8b3fSMaxim Konovalov case 'z': 4070b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 4080b2f8b3fSMaxim Konovalov ultmp = strtoul(optarg, &ep, 0); 4090b2f8b3fSMaxim Konovalov if (*ep || ep == optarg || ultmp > MAXTOS) 4100b2f8b3fSMaxim Konovalov errx(EX_USAGE, "invalid TOS: `%s'", optarg); 4110b2f8b3fSMaxim Konovalov tos = ultmp; 4120b2f8b3fSMaxim Konovalov break; 4139a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 4149a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 4158fae3551SRodney W. Grimes default: 416e345a80dSPhilippe Charnier usage(); 41743470e3bSGarrett Wollman } 41843470e3bSGarrett Wollman } 41943470e3bSGarrett Wollman 42043470e3bSGarrett Wollman if (argc - optind != 1) 421e345a80dSPhilippe Charnier usage(); 42243470e3bSGarrett Wollman target = argv[optind]; 4238fae3551SRodney W. Grimes 424fb7d32c7SMaxim Konovalov maxpayload = IP_MAXPACKET - sizeof(struct ip) - MINICMPLEN; 425fb7d32c7SMaxim Konovalov if (options & F_RROUTE) 426fb7d32c7SMaxim Konovalov maxpayload -= MAX_IPOPTLEN; 427fb7d32c7SMaxim Konovalov if (datalen > maxpayload) 428fb7d32c7SMaxim Konovalov errx(EX_USAGE, "packet size too large: %lu > %u", datalen, 429fb7d32c7SMaxim Konovalov maxpayload); 43099490edeSWarner Losh if (source) { 43199490edeSWarner Losh bzero((char *)&sin, sizeof(sin)); 43299490edeSWarner Losh sin.sin_family = AF_INET; 43399490edeSWarner Losh if (inet_aton(source, &sin.sin_addr) != 0) { 43499490edeSWarner Losh shostname = source; 43599490edeSWarner Losh } else { 43699490edeSWarner Losh hp = gethostbyname2(source, AF_INET); 43799490edeSWarner Losh if (!hp) 43899490edeSWarner Losh errx(EX_NOHOST, "cannot resolve %s: %s", 43999490edeSWarner Losh source, hstrerror(h_errno)); 44099490edeSWarner Losh 44199490edeSWarner Losh sin.sin_len = sizeof sin; 4424fba6582SMaxim Konovalov if (hp->h_length > sizeof(sin.sin_addr) || 4434fba6582SMaxim Konovalov hp->h_length < 0) 44499490edeSWarner Losh errx(1, "gethostbyname2: illegal address"); 44599490edeSWarner Losh memcpy(&sin.sin_addr, hp->h_addr_list[0], 44699490edeSWarner Losh sizeof(sin.sin_addr)); 44799490edeSWarner Losh (void)strncpy(snamebuf, hp->h_name, 44899490edeSWarner Losh sizeof(snamebuf) - 1); 44999490edeSWarner Losh snamebuf[sizeof(snamebuf) - 1] = '\0'; 45099490edeSWarner Losh shostname = snamebuf; 45199490edeSWarner Losh } 45299490edeSWarner Losh if (bind(s, (struct sockaddr *)&sin, sizeof sin) == -1) 45399490edeSWarner Losh err(1, "bind"); 45499490edeSWarner Losh } 45599490edeSWarner Losh 456d389e86aSMatt Jacob bzero(&whereto, sizeof(whereto)); 457d389e86aSMatt Jacob to = &whereto; 4588fae3551SRodney W. Grimes to->sin_family = AF_INET; 459d389e86aSMatt Jacob to->sin_len = sizeof *to; 46043470e3bSGarrett Wollman if (inet_aton(target, &to->sin_addr) != 0) { 4618fae3551SRodney W. Grimes hostname = target; 46243470e3bSGarrett Wollman } else { 46343470e3bSGarrett Wollman hp = gethostbyname2(target, AF_INET); 46443470e3bSGarrett Wollman if (!hp) 46543470e3bSGarrett Wollman errx(EX_NOHOST, "cannot resolve %s: %s", 46643470e3bSGarrett Wollman target, hstrerror(h_errno)); 46743470e3bSGarrett Wollman 4681ffae4a6SWarner Losh if (hp->h_length > sizeof(to->sin_addr)) 4691ffae4a6SWarner Losh errx(1, "gethostbyname2 returned an illegal address"); 47043470e3bSGarrett Wollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 4718fae3551SRodney W. Grimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 472b10d9d5fSWarner Losh hnamebuf[sizeof(hnamebuf) - 1] = '\0'; 4738fae3551SRodney W. Grimes hostname = hnamebuf; 4748fae3551SRodney W. Grimes } 4758fae3551SRodney W. Grimes 47643470e3bSGarrett Wollman if (options & F_FLOOD && options & F_INTERVAL) 47743470e3bSGarrett Wollman errx(EX_USAGE, "-f and -i: incompatible options"); 47843470e3bSGarrett Wollman 47943470e3bSGarrett Wollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 48043470e3bSGarrett Wollman errx(EX_USAGE, 48143470e3bSGarrett Wollman "-f flag cannot be used with multicast destination"); 48243470e3bSGarrett Wollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 48343470e3bSGarrett Wollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 48443470e3bSGarrett Wollman errx(EX_USAGE, 48543470e3bSGarrett Wollman "-I, -L, -T flags cannot be used with unicast destination"); 4868fae3551SRodney W. Grimes 487143008a1SMatthew N. Dodd if (datalen - MASKLEN >= PHDR_LEN) /* can we time transfer */ 4888fae3551SRodney W. Grimes timing = 1; 4894fba6582SMaxim Konovalov packlen = MAXIPLEN + MAXICMPLEN + datalen; 4904fba6582SMaxim Konovalov packlen = packlen > IP_MAXPACKET ? IP_MAXPACKET : packlen; 49143470e3bSGarrett Wollman 4928fae3551SRodney W. Grimes if (!(options & F_PINGFILLED)) 493d32ff037SJohn Birrell for (i = PHDR_LEN; i < datalen; ++i) 4948fae3551SRodney W. Grimes *datap++ = i; 4958fae3551SRodney W. Grimes 4968fae3551SRodney W. Grimes ident = getpid() & 0xFFFF; 4978fae3551SRodney W. Grimes 498f1284d7aSBill Fenner if (s < 0) { 499f1284d7aSBill Fenner errno = sockerrno; 50043470e3bSGarrett Wollman err(EX_OSERR, "socket"); 5018fae3551SRodney W. Grimes } 5028fae3551SRodney W. Grimes hold = 1; 5038fae3551SRodney W. Grimes if (options & F_SO_DEBUG) 5048fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold, 5058fae3551SRodney W. Grimes sizeof(hold)); 5068fae3551SRodney W. Grimes if (options & F_SO_DONTROUTE) 5078fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 5088fae3551SRodney W. Grimes sizeof(hold)); 5099a4365d0SYoshinobu Inoue #ifdef IPSEC 5109a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 5119a4365d0SYoshinobu Inoue if (options & F_POLICY) { 5129a4365d0SYoshinobu Inoue char *buf; 5139a4365d0SYoshinobu Inoue if (policy_in != NULL) { 5149a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_in, strlen(policy_in)); 5159a4365d0SYoshinobu Inoue if (buf == NULL) 516ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 5179a4365d0SYoshinobu Inoue if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY, 5189a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 5191ad0b1beSMaxim Konovalov err(EX_CONFIG, 5201ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 5219a4365d0SYoshinobu Inoue free(buf); 5229a4365d0SYoshinobu Inoue } 5239a4365d0SYoshinobu Inoue 5249a4365d0SYoshinobu Inoue if (policy_out != NULL) { 5259a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_out, strlen(policy_out)); 5269a4365d0SYoshinobu Inoue if (buf == NULL) 527ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 5289a4365d0SYoshinobu Inoue if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY, 5299a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 5301ad0b1beSMaxim Konovalov err(EX_CONFIG, 5311ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 5329a4365d0SYoshinobu Inoue free(buf); 5339a4365d0SYoshinobu Inoue } 5349a4365d0SYoshinobu Inoue } 5359a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 5369a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 5378fae3551SRodney W. Grimes 5380b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 5390b2f8b3fSMaxim Konovalov ip = (struct ip*)outpackhdr; 5400b2f8b3fSMaxim Konovalov if (!(options & (F_TTL | F_MTTL))) { 5410b2f8b3fSMaxim Konovalov mib[0] = CTL_NET; 5420b2f8b3fSMaxim Konovalov mib[1] = PF_INET; 5430b2f8b3fSMaxim Konovalov mib[2] = IPPROTO_IP; 5440b2f8b3fSMaxim Konovalov mib[3] = IPCTL_DEFTTL; 5450b2f8b3fSMaxim Konovalov sz = sizeof(ttl); 5460b2f8b3fSMaxim Konovalov if (sysctl(mib, 4, &ttl, &sz, NULL, 0) == -1) 5470b2f8b3fSMaxim Konovalov err(1, "sysctl(net.inet.ip.ttl)"); 5480b2f8b3fSMaxim Konovalov } 5490b2f8b3fSMaxim Konovalov setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold)); 5500b2f8b3fSMaxim Konovalov ip->ip_v = IPVERSION; 5510b2f8b3fSMaxim Konovalov ip->ip_hl = sizeof(struct ip) >> 2; 5520b2f8b3fSMaxim Konovalov ip->ip_tos = tos; 5530b2f8b3fSMaxim Konovalov ip->ip_id = 0; 5540b2f8b3fSMaxim Konovalov ip->ip_off = df ? IP_DF : 0; 5550b2f8b3fSMaxim Konovalov ip->ip_ttl = ttl; 5560b2f8b3fSMaxim Konovalov ip->ip_p = IPPROTO_ICMP; 5570b2f8b3fSMaxim Konovalov ip->ip_src.s_addr = source ? sin.sin_addr.s_addr : INADDR_ANY; 5580b2f8b3fSMaxim Konovalov ip->ip_dst = to->sin_addr; 5590b2f8b3fSMaxim Konovalov } 5608fae3551SRodney W. Grimes /* record route option */ 5618fae3551SRodney W. Grimes if (options & F_RROUTE) { 5628fae3551SRodney W. Grimes #ifdef IP_OPTIONS 563039d6aa4SBill Fenner bzero(rspace, sizeof(rspace)); 5648fae3551SRodney W. Grimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 5658fae3551SRodney W. Grimes rspace[IPOPT_OLEN] = sizeof(rspace) - 1; 5668fae3551SRodney W. Grimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 567039d6aa4SBill Fenner rspace[sizeof(rspace) - 1] = IPOPT_EOL; 5688fae3551SRodney W. Grimes if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace, 56943470e3bSGarrett Wollman sizeof(rspace)) < 0) 57043470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 5718fae3551SRodney W. Grimes #else 57243470e3bSGarrett Wollman errx(EX_UNAVAILABLE, 57343470e3bSGarrett Wollman "record route not available in this implementation"); 5748fae3551SRodney W. Grimes #endif /* IP_OPTIONS */ 5758fae3551SRodney W. Grimes } 5768fae3551SRodney W. Grimes 577211bfbd2SRuslan Ermilov if (options & F_TTL) { 578211bfbd2SRuslan Ermilov if (setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, 579211bfbd2SRuslan Ermilov sizeof(ttl)) < 0) { 580211bfbd2SRuslan Ermilov err(EX_OSERR, "setsockopt IP_TTL"); 581211bfbd2SRuslan Ermilov } 582211bfbd2SRuslan Ermilov } 58385456935SBill Fenner if (options & F_NOLOOP) { 58485456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 58585456935SBill Fenner sizeof(loop)) < 0) { 58643470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 58785456935SBill Fenner } 58885456935SBill Fenner } 58985456935SBill Fenner if (options & F_MTTL) { 590211bfbd2SRuslan Ermilov if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &mttl, 591211bfbd2SRuslan Ermilov sizeof(mttl)) < 0) { 59243470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 59385456935SBill Fenner } 59485456935SBill Fenner } 59585456935SBill Fenner if (options & F_MIF) { 59685456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 59785456935SBill Fenner sizeof(ifaddr)) < 0) { 59843470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 59985456935SBill Fenner } 60085456935SBill Fenner } 601039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 602039d6aa4SBill Fenner { int on = 1; 603039d6aa4SBill Fenner if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) < 0) 604039d6aa4SBill Fenner err(EX_OSERR, "setsockopt SO_TIMESTAMP"); 605039d6aa4SBill Fenner } 606039d6aa4SBill Fenner #endif 60785456935SBill Fenner 6088fae3551SRodney W. Grimes /* 6098fae3551SRodney W. Grimes * When pinging the broadcast address, you can get a lot of answers. 6108fae3551SRodney W. Grimes * Doing something so evil is useful if you are trying to stress the 6118fae3551SRodney W. Grimes * ethernet, or just want to fill the arp cache to get some stuff for 61243470e3bSGarrett Wollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 61343470e3bSGarrett Wollman * or multicast pings if they wish. 6148fae3551SRodney W. Grimes */ 6154fba6582SMaxim Konovalov 6164fba6582SMaxim Konovalov /* 6174fba6582SMaxim Konovalov * XXX receive buffer needs undetermined space for mbuf overhead 6184fba6582SMaxim Konovalov * as well. 6194fba6582SMaxim Konovalov */ 6204fba6582SMaxim Konovalov hold = IP_MAXPACKET + 128; 6218fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 6228fae3551SRodney W. Grimes sizeof(hold)); 623261e59bbSMaxim Konovalov if (uid == 0) 624e8bd25ceSRobert Watson (void)setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&hold, 625e8bd25ceSRobert Watson sizeof(hold)); 626e8bd25ceSRobert Watson 62799490edeSWarner Losh if (to->sin_family == AF_INET) { 62899490edeSWarner Losh (void)printf("PING %s (%s)", hostname, 62999490edeSWarner Losh inet_ntoa(to->sin_addr)); 63099490edeSWarner Losh if (source) 63199490edeSWarner Losh (void)printf(" from %s", shostname); 63299490edeSWarner Losh (void)printf(": %d data bytes\n", datalen); 63399490edeSWarner Losh } else 6348fae3551SRodney W. Grimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 6358fae3551SRodney W. Grimes 6367d81b35cSBruce Evans /* 637a2a00888SSean Eric Fagan * Use sigaction() instead of signal() to get unambiguous semantics, 638a2a00888SSean Eric Fagan * in particular with SA_RESTART not set. 6397d81b35cSBruce Evans */ 640a2a00888SSean Eric Fagan 641f6bd468bSPaul Traina sigemptyset(&si_sa.sa_mask); 64237e5b2c6SSean Eric Fagan si_sa.sa_flags = 0; 643a2a00888SSean Eric Fagan 644a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 645a2a00888SSean Eric Fagan if (sigaction(SIGINT, &si_sa, 0) == -1) { 646a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGINT"); 647a2a00888SSean Eric Fagan } 648a2a00888SSean Eric Fagan 649a2a00888SSean Eric Fagan si_sa.sa_handler = status; 65037e5b2c6SSean Eric Fagan if (sigaction(SIGINFO, &si_sa, 0) == -1) { 651624ff938SWarner Losh err(EX_OSERR, "sigaction"); 65237e5b2c6SSean Eric Fagan } 653bf113f1bSBill Fumerola 654bf113f1bSBill Fumerola if (alarmtimeout > 0) { 6557237fd94SBill Fumerola si_sa.sa_handler = stopit; 6567237fd94SBill Fumerola if (sigaction(SIGALRM, &si_sa, 0) == -1) 6577237fd94SBill Fumerola err(EX_OSERR, "sigaction SIGALRM"); 658bf113f1bSBill Fumerola } 65937e5b2c6SSean Eric Fagan 660039d6aa4SBill Fenner bzero(&msg, sizeof(msg)); 661039d6aa4SBill Fenner msg.msg_name = (caddr_t)&from; 662039d6aa4SBill Fenner msg.msg_iov = &iov; 663039d6aa4SBill Fenner msg.msg_iovlen = 1; 664039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 665039d6aa4SBill Fenner msg.msg_control = (caddr_t)ctrl; 666039d6aa4SBill Fenner #endif 667039d6aa4SBill Fenner iov.iov_base = packet; 668039d6aa4SBill Fenner iov.iov_len = packlen; 669039d6aa4SBill Fenner 6704055611aSSean Eric Fagan if (tcgetattr(STDOUT_FILENO, &ts) != -1) { 6714055611aSSean Eric Fagan reset_kerninfo = !(ts.c_lflag & NOKERNINFO); 6724055611aSSean Eric Fagan ts.c_lflag |= NOKERNINFO; 6734055611aSSean Eric Fagan tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 6744055611aSSean Eric Fagan } 6754055611aSSean Eric Fagan 67632af342fSRuslan Ermilov if (preload == 0) 67732af342fSRuslan Ermilov pinger(); /* send the first ping */ 67832af342fSRuslan Ermilov else { 67932af342fSRuslan Ermilov if (npackets != 0 && preload > npackets) 68032af342fSRuslan Ermilov preload = npackets; 6818fae3551SRodney W. Grimes while (preload--) /* fire off them quickies */ 6828fae3551SRodney W. Grimes pinger(); 68332af342fSRuslan Ermilov } 68432af342fSRuslan Ermilov (void)gettimeofday(&last, NULL); 6858fae3551SRodney W. Grimes 686039d6aa4SBill Fenner if (options & F_FLOOD) { 687039d6aa4SBill Fenner intvl.tv_sec = 0; 688039d6aa4SBill Fenner intvl.tv_usec = 10000; 689039d6aa4SBill Fenner } else { 690526f06b2SMatthew Dillon intvl.tv_sec = interval / 1000; 691526f06b2SMatthew Dillon intvl.tv_usec = interval % 1000 * 1000; 692039d6aa4SBill Fenner } 693039d6aa4SBill Fenner 694261e59bbSMaxim Konovalov almost_done = 0; 6958f975bb3SBruce Evans while (!finish_up) { 696261e59bbSMaxim Konovalov struct timeval now, timeout; 697039d6aa4SBill Fenner fd_set rfds; 698261e59bbSMaxim Konovalov int cc, n; 6998fae3551SRodney W. Grimes 7007d81b35cSBruce Evans check_status(); 7017e5bbd68SJacques Vidrine if (s >= FD_SETSIZE) 7027e5bbd68SJacques Vidrine errx(EX_OSERR, "descriptor too large"); 703039d6aa4SBill Fenner FD_ZERO(&rfds); 704039d6aa4SBill Fenner FD_SET(s, &rfds); 705039d6aa4SBill Fenner (void)gettimeofday(&now, NULL); 706039d6aa4SBill Fenner timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec; 707039d6aa4SBill Fenner timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec; 708039d6aa4SBill Fenner while (timeout.tv_usec < 0) { 709039d6aa4SBill Fenner timeout.tv_usec += 1000000; 710039d6aa4SBill Fenner timeout.tv_sec--; 7118fae3551SRodney W. Grimes } 712e345a80dSPhilippe Charnier while (timeout.tv_usec >= 1000000) { 713039d6aa4SBill Fenner timeout.tv_usec -= 1000000; 714039d6aa4SBill Fenner timeout.tv_sec++; 715039d6aa4SBill Fenner } 716039d6aa4SBill Fenner if (timeout.tv_sec < 0) 717039d6aa4SBill Fenner timeout.tv_sec = timeout.tv_usec = 0; 718039d6aa4SBill Fenner n = select(s + 1, &rfds, NULL, NULL, &timeout); 7195e2cc0f4SStephen McKay if (n < 0) 7205e2cc0f4SStephen McKay continue; /* Must be EINTR. */ 721039d6aa4SBill Fenner if (n == 1) { 7229b219646SPeter Wemm struct timeval *t = NULL; 723039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 724039d6aa4SBill Fenner struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl; 725039d6aa4SBill Fenner 726039d6aa4SBill Fenner msg.msg_controllen = sizeof(ctrl); 727039d6aa4SBill Fenner #endif 728039d6aa4SBill Fenner msg.msg_namelen = sizeof(from); 729039d6aa4SBill Fenner if ((cc = recvmsg(s, &msg, 0)) < 0) { 7308fae3551SRodney W. Grimes if (errno == EINTR) 7318fae3551SRodney W. Grimes continue; 732e345a80dSPhilippe Charnier warn("recvmsg"); 7338fae3551SRodney W. Grimes continue; 7348fae3551SRodney W. Grimes } 735039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 736039d6aa4SBill Fenner if (cmsg->cmsg_level == SOL_SOCKET && 737039d6aa4SBill Fenner cmsg->cmsg_type == SCM_TIMESTAMP && 7386ecbec77SYoshinobu Inoue cmsg->cmsg_len == CMSG_LEN(sizeof *t)) { 739fa05a94cSJohn Birrell /* Copy to avoid alignment problems: */ 740fa05a94cSJohn Birrell memcpy(&now, CMSG_DATA(cmsg), sizeof(now)); 741fa05a94cSJohn Birrell t = &now; 742fa05a94cSJohn Birrell } 743039d6aa4SBill Fenner #endif 7449b219646SPeter Wemm if (t == NULL) { 745039d6aa4SBill Fenner (void)gettimeofday(&now, NULL); 746039d6aa4SBill Fenner t = &now; 747039d6aa4SBill Fenner } 748039d6aa4SBill Fenner pr_pack((char *)packet, cc, &from, t); 7498025c44bSDima Dorfman if (options & F_ONCE && nreceived || 7508025c44bSDima Dorfman npackets && nreceived >= npackets) 7518fae3551SRodney W. Grimes break; 7528fae3551SRodney W. Grimes } 7535e2cc0f4SStephen McKay if (n == 0 || options & F_FLOOD) { 754039d6aa4SBill Fenner if (!npackets || ntransmitted < npackets) 755039d6aa4SBill Fenner pinger(); 756039d6aa4SBill Fenner else { 757039d6aa4SBill Fenner if (almost_done) 758039d6aa4SBill Fenner break; 759039d6aa4SBill Fenner almost_done = 1; 7605e2cc0f4SStephen McKay intvl.tv_usec = 0; 761039d6aa4SBill Fenner if (nreceived) { 762039d6aa4SBill Fenner intvl.tv_sec = 2 * tmax / 1000; 763039d6aa4SBill Fenner if (!intvl.tv_sec) 764039d6aa4SBill Fenner intvl.tv_sec = 1; 765039d6aa4SBill Fenner } else 766039d6aa4SBill Fenner intvl.tv_sec = MAXWAIT; 767039d6aa4SBill Fenner } 768039d6aa4SBill Fenner (void)gettimeofday(&last, NULL); 76925107197SIan Dowse if (ntransmitted - nreceived - 1 > nmissedmax) { 77025107197SIan Dowse nmissedmax = ntransmitted - nreceived - 1; 77125107197SIan Dowse if (options & F_MISSED) 772ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 773039d6aa4SBill Fenner } 774039d6aa4SBill Fenner } 77525107197SIan Dowse } 7768f975bb3SBruce Evans finish(); 7778fae3551SRodney W. Grimes /* NOTREACHED */ 778f78ac61bSWarner Losh exit(0); /* Make the compiler happy */ 7798fae3551SRodney W. Grimes } 7808fae3551SRodney W. Grimes 7818fae3551SRodney W. Grimes /* 7828f975bb3SBruce Evans * stopit -- 7838f975bb3SBruce Evans * Set the global bit that causes the main loop to quit. 7848f975bb3SBruce Evans * Do NOT call finish() from here, since finish() does far too much 7858f975bb3SBruce Evans * to be called from a signal handler. 786515dd2faSJulian Elischer */ 787515dd2faSJulian Elischer void 7888f975bb3SBruce Evans stopit(sig) 789c6facae4SMaxim Konovalov int sig __unused; 790515dd2faSJulian Elischer { 791efc8588dSDavid E. O'Brien 792515dd2faSJulian Elischer finish_up = 1; 793515dd2faSJulian Elischer } 794515dd2faSJulian Elischer 795515dd2faSJulian Elischer /* 7968fae3551SRodney W. Grimes * pinger -- 7978fae3551SRodney W. Grimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 7988fae3551SRodney W. Grimes * will be added on by the kernel. The ID field is our UNIX process ID, 7994fba6582SMaxim Konovalov * and the sequence number is an ascending integer. The first PHDR_LEN 8004fba6582SMaxim Konovalov * bytes of the data portion are used to hold a UNIX "timeval" struct in 8014fba6582SMaxim Konovalov * host byte-order, to compute the round-trip time. 8028fae3551SRodney W. Grimes */ 80343470e3bSGarrett Wollman static void 80443470e3bSGarrett Wollman pinger(void) 8058fae3551SRodney W. Grimes { 8060b2f8b3fSMaxim Konovalov struct ip *ip; 8073d438ad6SDavid E. O'Brien struct icmp *icp; 808efc8588dSDavid E. O'Brien int cc, i; 8090b2f8b3fSMaxim Konovalov u_char *packet; 8108fae3551SRodney W. Grimes 8110b2f8b3fSMaxim Konovalov packet = outpack; 8128fae3551SRodney W. Grimes icp = (struct icmp *)outpack; 813143008a1SMatthew N. Dodd if (options & F_MASK) 814143008a1SMatthew N. Dodd icp->icmp_type = ICMP_MASKREQ; 815143008a1SMatthew N. Dodd else 8168fae3551SRodney W. Grimes icp->icmp_type = ICMP_ECHO; 8178fae3551SRodney W. Grimes icp->icmp_code = 0; 8188fae3551SRodney W. Grimes icp->icmp_cksum = 0; 8195db89bc7SBill Fenner icp->icmp_seq = htons(ntransmitted); 8208fae3551SRodney W. Grimes icp->icmp_id = ident; /* ID */ 8218fae3551SRodney W. Grimes 8225db89bc7SBill Fenner CLR(ntransmitted % mx_dup_ck); 8238fae3551SRodney W. Grimes 8248fae3551SRodney W. Grimes if (timing) 825143008a1SMatthew N. Dodd (void)gettimeofday((struct timeval *)&outpack[ 826143008a1SMatthew N. Dodd MINICMPLEN + MASKLEN], NULL); 8278fae3551SRodney W. Grimes 8284fba6582SMaxim Konovalov cc = MINICMPLEN + datalen; 8298fae3551SRodney W. Grimes 8308fae3551SRodney W. Grimes /* compute ICMP checksum here */ 8318fae3551SRodney W. Grimes icp->icmp_cksum = in_cksum((u_short *)icp, cc); 8328fae3551SRodney W. Grimes 8330b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 8340b2f8b3fSMaxim Konovalov cc += sizeof(struct ip); 8350b2f8b3fSMaxim Konovalov ip = (struct ip *)outpackhdr; 8360b2f8b3fSMaxim Konovalov ip->ip_len = cc; 8370b2f8b3fSMaxim Konovalov ip->ip_sum = in_cksum((u_short *)outpackhdr, cc); 8380b2f8b3fSMaxim Konovalov packet = outpackhdr; 8390b2f8b3fSMaxim Konovalov } 8400b2f8b3fSMaxim Konovalov i = sendto(s, (char *)packet, cc, 0, (struct sockaddr *)&whereto, 841d389e86aSMatt Jacob sizeof(whereto)); 8428fae3551SRodney W. Grimes 8438fae3551SRodney W. Grimes if (i < 0 || i != cc) { 84443470e3bSGarrett Wollman if (i < 0) { 8458f975bb3SBruce Evans if (options & F_FLOOD && errno == ENOBUFS) { 846515dd2faSJulian Elischer usleep(FLOOD_BACKOFF); 847515dd2faSJulian Elischer return; 848515dd2faSJulian Elischer } 84943470e3bSGarrett Wollman warn("sendto"); 85043470e3bSGarrett Wollman } else { 85143470e3bSGarrett Wollman warn("%s: partial write: %d of %d bytes", 852416aa49bSPoul-Henning Kamp hostname, i, cc); 8538fae3551SRodney W. Grimes } 854363d7bbeSJulian Elischer } 855363d7bbeSJulian Elischer ntransmitted++; 8568fae3551SRodney W. Grimes if (!(options & F_QUIET) && options & F_FLOOD) 8578fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &DOT, 1); 8588fae3551SRodney W. Grimes } 8598fae3551SRodney W. Grimes 8608fae3551SRodney W. Grimes /* 8618fae3551SRodney W. Grimes * pr_pack -- 8628fae3551SRodney W. Grimes * Print out the packet, if it came from us. This logic is necessary 8638fae3551SRodney W. Grimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 8648fae3551SRodney W. Grimes * which arrive ('tis only fair). This permits multiple copies of this 8658fae3551SRodney W. Grimes * program to be run without having intermingled output (or statistics!). 8668fae3551SRodney W. Grimes */ 86743470e3bSGarrett Wollman static void 868039d6aa4SBill Fenner pr_pack(buf, cc, from, tv) 8698fae3551SRodney W. Grimes char *buf; 8708fae3551SRodney W. Grimes int cc; 8718fae3551SRodney W. Grimes struct sockaddr_in *from; 872039d6aa4SBill Fenner struct timeval *tv; 8738fae3551SRodney W. Grimes { 8749b219646SPeter Wemm struct in_addr ina; 8759b219646SPeter Wemm u_char *cp, *dp; 8763d438ad6SDavid E. O'Brien struct icmp *icp; 8778fae3551SRodney W. Grimes struct ip *ip; 8789d2b0ab8SPeter Wemm const void *tp; 8798f975bb3SBruce Evans double triptime; 880efc8588dSDavid E. O'Brien int dupflag, hlen, i, j, seq; 881efc8588dSDavid E. O'Brien static int old_rrlen; 882efc8588dSDavid E. O'Brien static char old_rr[MAX_IPOPTLEN]; 8838fae3551SRodney W. Grimes 8848fae3551SRodney W. Grimes /* Check the IP header */ 8858fae3551SRodney W. Grimes ip = (struct ip *)buf; 8868fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 8878fae3551SRodney W. Grimes if (cc < hlen + ICMP_MINLEN) { 8888fae3551SRodney W. Grimes if (options & F_VERBOSE) 88943470e3bSGarrett Wollman warn("packet too short (%d bytes) from %s", cc, 89043470e3bSGarrett Wollman inet_ntoa(from->sin_addr)); 8918fae3551SRodney W. Grimes return; 8928fae3551SRodney W. Grimes } 8938fae3551SRodney W. Grimes 8948fae3551SRodney W. Grimes /* Now the ICMP part */ 8958fae3551SRodney W. Grimes cc -= hlen; 8968fae3551SRodney W. Grimes icp = (struct icmp *)(buf + hlen); 897143008a1SMatthew N. Dodd if ((icp->icmp_type == ICMP_ECHOREPLY) || 898143008a1SMatthew N. Dodd ((icp->icmp_type == ICMP_MASKREPLY) && (options & F_MASK))) { 8998fae3551SRodney W. Grimes if (icp->icmp_id != ident) 9008fae3551SRodney W. Grimes return; /* 'Twas not our ECHO */ 9018fae3551SRodney W. Grimes ++nreceived; 9028f975bb3SBruce Evans triptime = 0.0; 9038fae3551SRodney W. Grimes if (timing) { 904d32ff037SJohn Birrell struct timeval tv1; 9058fae3551SRodney W. Grimes #ifndef icmp_data 9069d2b0ab8SPeter Wemm tp = &icp->icmp_ip; 9078fae3551SRodney W. Grimes #else 9089d2b0ab8SPeter Wemm tp = icp->icmp_data; 9098fae3551SRodney W. Grimes #endif 910143008a1SMatthew N. Dodd tp+=MASKLEN; 911143008a1SMatthew N. Dodd 9129d2b0ab8SPeter Wemm /* Copy to avoid alignment problems: */ 9139d2b0ab8SPeter Wemm memcpy(&tv1, tp, sizeof(tv1)); 914039d6aa4SBill Fenner tvsub(tv, &tv1); 915039d6aa4SBill Fenner triptime = ((double)tv->tv_sec) * 1000.0 + 916039d6aa4SBill Fenner ((double)tv->tv_usec) / 1000.0; 9178fae3551SRodney W. Grimes tsum += triptime; 9183109a910SGarrett Wollman tsumsq += triptime * triptime; 9198fae3551SRodney W. Grimes if (triptime < tmin) 9208fae3551SRodney W. Grimes tmin = triptime; 9218fae3551SRodney W. Grimes if (triptime > tmax) 9228fae3551SRodney W. Grimes tmax = triptime; 9238fae3551SRodney W. Grimes } 9248fae3551SRodney W. Grimes 9255db89bc7SBill Fenner seq = ntohs(icp->icmp_seq); 9265db89bc7SBill Fenner 9275db89bc7SBill Fenner if (TST(seq % mx_dup_ck)) { 9288fae3551SRodney W. Grimes ++nrepeats; 9298fae3551SRodney W. Grimes --nreceived; 9308fae3551SRodney W. Grimes dupflag = 1; 9318fae3551SRodney W. Grimes } else { 9325db89bc7SBill Fenner SET(seq % mx_dup_ck); 9338fae3551SRodney W. Grimes dupflag = 0; 9348fae3551SRodney W. Grimes } 9358fae3551SRodney W. Grimes 9368fae3551SRodney W. Grimes if (options & F_QUIET) 9378fae3551SRodney W. Grimes return; 9388fae3551SRodney W. Grimes 9398fae3551SRodney W. Grimes if (options & F_FLOOD) 9408fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &BSPACE, 1); 9418fae3551SRodney W. Grimes else { 9428fae3551SRodney W. Grimes (void)printf("%d bytes from %s: icmp_seq=%u", cc, 9438fae3551SRodney W. Grimes inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr), 9445db89bc7SBill Fenner seq); 9458fae3551SRodney W. Grimes (void)printf(" ttl=%d", ip->ip_ttl); 9468fae3551SRodney W. Grimes if (timing) 947d410b6f1SDavid Greenman (void)printf(" time=%.3f ms", triptime); 9488fae3551SRodney W. Grimes if (dupflag) 9498fae3551SRodney W. Grimes (void)printf(" (DUP!)"); 950772dfa72SDaniel O'Callaghan if (options & F_AUDIBLE) 951ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 952143008a1SMatthew N. Dodd if (options & F_MASK) { 953143008a1SMatthew N. Dodd /* Just prentend this cast isn't ugly */ 954143008a1SMatthew N. Dodd (void)printf(" mask=%s", 955143008a1SMatthew N. Dodd pr_addr(*(struct in_addr *)&(icp->icmp_mask))); 956143008a1SMatthew N. Dodd } 9578fae3551SRodney W. Grimes /* check the data */ 958d32ff037SJohn Birrell cp = (u_char*)&icp->icmp_data[PHDR_LEN]; 9594fba6582SMaxim Konovalov dp = &outpack[MINICMPLEN + PHDR_LEN]; 960d32ff037SJohn Birrell for (i = PHDR_LEN; i < datalen; ++i, ++cp, ++dp) { 9618fae3551SRodney W. Grimes if (*cp != *dp) { 9628fae3551SRodney W. Grimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 9638fae3551SRodney W. Grimes i, *dp, *cp); 9641ad0b1beSMaxim Konovalov (void)printf("\ncp:"); 9658fae3551SRodney W. Grimes cp = (u_char*)&icp->icmp_data[0]; 966d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 967d32ff037SJohn Birrell if ((i % 32) == 8) 968d32ff037SJohn Birrell (void)printf("\n\t"); 969d32ff037SJohn Birrell (void)printf("%x ", *cp); 970d32ff037SJohn Birrell } 9711ad0b1beSMaxim Konovalov (void)printf("\ndp:"); 9724fba6582SMaxim Konovalov cp = &outpack[MINICMPLEN]; 973d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 9748fae3551SRodney W. Grimes if ((i % 32) == 8) 9758fae3551SRodney W. Grimes (void)printf("\n\t"); 9768fae3551SRodney W. Grimes (void)printf("%x ", *cp); 9778fae3551SRodney W. Grimes } 9788fae3551SRodney W. Grimes break; 9798fae3551SRodney W. Grimes } 9808fae3551SRodney W. Grimes } 9818fae3551SRodney W. Grimes } 9828fae3551SRodney W. Grimes } else { 983ef9e6dc7SBill Fenner /* 984ef9e6dc7SBill Fenner * We've got something other than an ECHOREPLY. 985ef9e6dc7SBill Fenner * See if it's a reply to something that we sent. 986ef9e6dc7SBill Fenner * We can compare IP destination, protocol, 987ef9e6dc7SBill Fenner * and ICMP type and ID. 988f78ac61bSWarner Losh * 989f78ac61bSWarner Losh * Only print all the error messages if we are running 990f78ac61bSWarner Losh * as root to avoid leaking information not normally 991f78ac61bSWarner Losh * available to those not running as root. 992ef9e6dc7SBill Fenner */ 993ef9e6dc7SBill Fenner #ifndef icmp_data 994ef9e6dc7SBill Fenner struct ip *oip = &icp->icmp_ip; 995ef9e6dc7SBill Fenner #else 996ef9e6dc7SBill Fenner struct ip *oip = (struct ip *)icp->icmp_data; 997ef9e6dc7SBill Fenner #endif 998ef9e6dc7SBill Fenner struct icmp *oicmp = (struct icmp *)(oip + 1); 999ef9e6dc7SBill Fenner 1000ee2bf734SWarner Losh if (((options & F_VERBOSE) && uid == 0) || 1001ef9e6dc7SBill Fenner (!(options & F_QUIET2) && 1002d389e86aSMatt Jacob (oip->ip_dst.s_addr == whereto.sin_addr.s_addr) && 1003ef9e6dc7SBill Fenner (oip->ip_p == IPPROTO_ICMP) && 1004ef9e6dc7SBill Fenner (oicmp->icmp_type == ICMP_ECHO) && 1005ef9e6dc7SBill Fenner (oicmp->icmp_id == ident))) { 10068fae3551SRodney W. Grimes (void)printf("%d bytes from %s: ", cc, 100743470e3bSGarrett Wollman pr_addr(from->sin_addr)); 10088fae3551SRodney W. Grimes pr_icmph(icp); 1009ef9e6dc7SBill Fenner } else 1010ef9e6dc7SBill Fenner return; 10118fae3551SRodney W. Grimes } 10128fae3551SRodney W. Grimes 10138fae3551SRodney W. Grimes /* Display any IP options */ 10148fae3551SRodney W. Grimes cp = (u_char *)buf + sizeof(struct ip); 10158fae3551SRodney W. Grimes 10168fae3551SRodney W. Grimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 10178fae3551SRodney W. Grimes switch (*cp) { 10188fae3551SRodney W. Grimes case IPOPT_EOL: 10198fae3551SRodney W. Grimes hlen = 0; 10208fae3551SRodney W. Grimes break; 10218fae3551SRodney W. Grimes case IPOPT_LSRR: 1022cb75aca7SMaxim Konovalov case IPOPT_SSRR: 1023cb75aca7SMaxim Konovalov (void)printf(*cp == IPOPT_LSRR ? 1024cb75aca7SMaxim Konovalov "\nLSRR: " : "\nSSRR: "); 1025301207dfSMaxim Konovalov j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1; 10268fae3551SRodney W. Grimes hlen -= 2; 1027301207dfSMaxim Konovalov cp += 2; 10283c721ab3SMaxim Konovalov if (j >= INADDR_LEN && 10293c721ab3SMaxim Konovalov j <= hlen - (int)sizeof(struct ip)) { 10308fae3551SRodney W. Grimes for (;;) { 1031301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1032301207dfSMaxim Konovalov if (ina.s_addr == 0) 10331ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1034301207dfSMaxim Konovalov else 10351ad0b1beSMaxim Konovalov (void)printf("\t%s", 10361ad0b1beSMaxim Konovalov pr_addr(ina)); 1037301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1038301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1039301207dfSMaxim Konovalov j -= INADDR_LEN; 1040301207dfSMaxim Konovalov if (j < INADDR_LEN) 10418fae3551SRodney W. Grimes break; 10428fae3551SRodney W. Grimes (void)putchar('\n'); 10438fae3551SRodney W. Grimes } 1044301207dfSMaxim Konovalov } else 1045301207dfSMaxim Konovalov (void)printf("\t(truncated route)\n"); 10468fae3551SRodney W. Grimes break; 10478fae3551SRodney W. Grimes case IPOPT_RR: 1048301207dfSMaxim Konovalov j = cp[IPOPT_OLEN]; /* get length */ 1049301207dfSMaxim Konovalov i = cp[IPOPT_OFFSET]; /* and pointer */ 10508fae3551SRodney W. Grimes hlen -= 2; 1051301207dfSMaxim Konovalov cp += 2; 10528fae3551SRodney W. Grimes if (i > j) 10538fae3551SRodney W. Grimes i = j; 1054301207dfSMaxim Konovalov i = i - IPOPT_MINOFF + 1; 1055301207dfSMaxim Konovalov if (i < 0 || i > (hlen - (int)sizeof(struct ip))) { 1056301207dfSMaxim Konovalov old_rrlen = 0; 10578fae3551SRodney W. Grimes continue; 1058301207dfSMaxim Konovalov } 10598fae3551SRodney W. Grimes if (i == old_rrlen 10608fae3551SRodney W. Grimes && !bcmp((char *)cp, old_rr, i) 10618fae3551SRodney W. Grimes && !(options & F_FLOOD)) { 10628fae3551SRodney W. Grimes (void)printf("\t(same route)"); 10638fae3551SRodney W. Grimes hlen -= i; 10648fae3551SRodney W. Grimes cp += i; 10658fae3551SRodney W. Grimes break; 10668fae3551SRodney W. Grimes } 10678fae3551SRodney W. Grimes old_rrlen = i; 10688fae3551SRodney W. Grimes bcopy((char *)cp, old_rr, i); 10698fae3551SRodney W. Grimes (void)printf("\nRR: "); 1070301207dfSMaxim Konovalov if (i >= INADDR_LEN && 1071301207dfSMaxim Konovalov i <= hlen - (int)sizeof(struct ip)) { 10728fae3551SRodney W. Grimes for (;;) { 1073301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1074301207dfSMaxim Konovalov if (ina.s_addr == 0) 10751ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1076301207dfSMaxim Konovalov else 1077301207dfSMaxim Konovalov (void)printf("\t%s", 1078301207dfSMaxim Konovalov pr_addr(ina)); 1079301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1080301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1081301207dfSMaxim Konovalov i -= INADDR_LEN; 1082301207dfSMaxim Konovalov if (i < INADDR_LEN) 10838fae3551SRodney W. Grimes break; 10848fae3551SRodney W. Grimes (void)putchar('\n'); 10858fae3551SRodney W. Grimes } 1086301207dfSMaxim Konovalov } else 1087301207dfSMaxim Konovalov (void)printf("\t(truncated route)"); 10888fae3551SRodney W. Grimes break; 10898fae3551SRodney W. Grimes case IPOPT_NOP: 10908fae3551SRodney W. Grimes (void)printf("\nNOP"); 10918fae3551SRodney W. Grimes break; 10928fae3551SRodney W. Grimes default: 10938fae3551SRodney W. Grimes (void)printf("\nunknown option %x", *cp); 10948fae3551SRodney W. Grimes break; 10958fae3551SRodney W. Grimes } 10968fae3551SRodney W. Grimes if (!(options & F_FLOOD)) { 10978fae3551SRodney W. Grimes (void)putchar('\n'); 10988fae3551SRodney W. Grimes (void)fflush(stdout); 10998fae3551SRodney W. Grimes } 11008fae3551SRodney W. Grimes } 11018fae3551SRodney W. Grimes 11028fae3551SRodney W. Grimes /* 11038fae3551SRodney W. Grimes * in_cksum -- 11048fae3551SRodney W. Grimes * Checksum routine for Internet Protocol family headers (C Version) 11058fae3551SRodney W. Grimes */ 110643470e3bSGarrett Wollman u_short 11078fae3551SRodney W. Grimes in_cksum(addr, len) 11088fae3551SRodney W. Grimes u_short *addr; 11098fae3551SRodney W. Grimes int len; 11108fae3551SRodney W. Grimes { 1111efc8588dSDavid E. O'Brien int nleft, sum; 1112efc8588dSDavid E. O'Brien u_short *w; 11133ec12efeSPierre Beyssac union { 111409333e78SPierre Beyssac u_short us; 111509333e78SPierre Beyssac u_char uc[2]; 111609333e78SPierre Beyssac } last; 111709333e78SPierre Beyssac u_short answer; 11188fae3551SRodney W. Grimes 1119efc8588dSDavid E. O'Brien nleft = len; 1120efc8588dSDavid E. O'Brien sum = 0; 1121efc8588dSDavid E. O'Brien w = addr; 1122efc8588dSDavid E. O'Brien 11238fae3551SRodney W. Grimes /* 11248fae3551SRodney W. Grimes * Our algorithm is simple, using a 32 bit accumulator (sum), we add 11258fae3551SRodney W. Grimes * sequential 16 bit words to it, and at the end, fold back all the 11268fae3551SRodney W. Grimes * carry bits from the top 16 bits into the lower 16 bits. 11278fae3551SRodney W. Grimes */ 11288fae3551SRodney W. Grimes while (nleft > 1) { 11298fae3551SRodney W. Grimes sum += *w++; 11308fae3551SRodney W. Grimes nleft -= 2; 11318fae3551SRodney W. Grimes } 11328fae3551SRodney W. Grimes 11338fae3551SRodney W. Grimes /* mop up an odd byte, if necessary */ 11348fae3551SRodney W. Grimes if (nleft == 1) { 113509333e78SPierre Beyssac last.uc[0] = *(u_char *)w; 113609333e78SPierre Beyssac last.uc[1] = 0; 113709333e78SPierre Beyssac sum += last.us; 11388fae3551SRodney W. Grimes } 11398fae3551SRodney W. Grimes 11408fae3551SRodney W. Grimes /* add back carry outs from top 16 bits to low 16 bits */ 11418fae3551SRodney W. Grimes sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 11428fae3551SRodney W. Grimes sum += (sum >> 16); /* add carry */ 114309333e78SPierre Beyssac answer = ~sum; /* truncate to 16 bits */ 114409333e78SPierre Beyssac return(answer); 11458fae3551SRodney W. Grimes } 11468fae3551SRodney W. Grimes 11478fae3551SRodney W. Grimes /* 11488fae3551SRodney W. Grimes * tvsub -- 11498fae3551SRodney W. Grimes * Subtract 2 timeval structs: out = out - in. Out is assumed to 11508fae3551SRodney W. Grimes * be >= in. 11518fae3551SRodney W. Grimes */ 115243470e3bSGarrett Wollman static void 11538fae3551SRodney W. Grimes tvsub(out, in) 11543d438ad6SDavid E. O'Brien struct timeval *out, *in; 11558fae3551SRodney W. Grimes { 1156efc8588dSDavid E. O'Brien 11578fae3551SRodney W. Grimes if ((out->tv_usec -= in->tv_usec) < 0) { 11588fae3551SRodney W. Grimes --out->tv_sec; 11598fae3551SRodney W. Grimes out->tv_usec += 1000000; 11608fae3551SRodney W. Grimes } 11618fae3551SRodney W. Grimes out->tv_sec -= in->tv_sec; 11628fae3551SRodney W. Grimes } 11638fae3551SRodney W. Grimes 11648fae3551SRodney W. Grimes /* 1165badd8138SSean Eric Fagan * status -- 1166badd8138SSean Eric Fagan * Print out statistics when SIGINFO is received. 1167badd8138SSean Eric Fagan */ 1168badd8138SSean Eric Fagan 116943470e3bSGarrett Wollman static void 11707d81b35cSBruce Evans status(sig) 1171c6facae4SMaxim Konovalov int sig __unused; 11727d81b35cSBruce Evans { 1173efc8588dSDavid E. O'Brien 117437e5b2c6SSean Eric Fagan siginfo_p = 1; 117537e5b2c6SSean Eric Fagan } 117637e5b2c6SSean Eric Fagan 117743470e3bSGarrett Wollman static void 117837e5b2c6SSean Eric Fagan check_status() 1179badd8138SSean Eric Fagan { 1180efc8588dSDavid E. O'Brien 118137e5b2c6SSean Eric Fagan if (siginfo_p) { 118237e5b2c6SSean Eric Fagan siginfo_p = 0; 11837d81b35cSBruce Evans (void)fprintf(stderr, 11847d81b35cSBruce Evans "\r%ld/%ld packets received (%.0f%%) %.3f min / %.3f avg / %.3f max\n", 11857d81b35cSBruce Evans nreceived, ntransmitted, 11867d81b35cSBruce Evans ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0, 11877d81b35cSBruce Evans nreceived ? tmin : 0.0, 11887d81b35cSBruce Evans nreceived + nrepeats ? tsum / (nreceived + nrepeats) : tsum, 11897d81b35cSBruce Evans tmax); 119037e5b2c6SSean Eric Fagan } 1191badd8138SSean Eric Fagan } 1192badd8138SSean Eric Fagan 1193badd8138SSean Eric Fagan /* 11948fae3551SRodney W. Grimes * finish -- 11958fae3551SRodney W. Grimes * Print out statistics, and give up. 11968fae3551SRodney W. Grimes */ 119743470e3bSGarrett Wollman static void 11988f975bb3SBruce Evans finish() 11998fae3551SRodney W. Grimes { 1200badd8138SSean Eric Fagan struct termios ts; 12018fae3551SRodney W. Grimes 12028fae3551SRodney W. Grimes (void)signal(SIGINT, SIG_IGN); 1203a2a00888SSean Eric Fagan (void)signal(SIGALRM, SIG_IGN); 12048fae3551SRodney W. Grimes (void)putchar('\n'); 12058fae3551SRodney W. Grimes (void)fflush(stdout); 12068fae3551SRodney W. Grimes (void)printf("--- %s ping statistics ---\n", hostname); 12078fae3551SRodney W. Grimes (void)printf("%ld packets transmitted, ", ntransmitted); 12088fae3551SRodney W. Grimes (void)printf("%ld packets received, ", nreceived); 12098fae3551SRodney W. Grimes if (nrepeats) 12108fae3551SRodney W. Grimes (void)printf("+%ld duplicates, ", nrepeats); 1211ebe70c8fSWarner Losh if (ntransmitted) { 12128fae3551SRodney W. Grimes if (nreceived > ntransmitted) 12138fae3551SRodney W. Grimes (void)printf("-- somebody's printing up packets!"); 12148fae3551SRodney W. Grimes else 12158fae3551SRodney W. Grimes (void)printf("%d%% packet loss", 12168fae3551SRodney W. Grimes (int)(((ntransmitted - nreceived) * 100) / 12178fae3551SRodney W. Grimes ntransmitted)); 1218ebe70c8fSWarner Losh } 12198fae3551SRodney W. Grimes (void)putchar('\n'); 12203109a910SGarrett Wollman if (nreceived && timing) { 12213109a910SGarrett Wollman double n = nreceived + nrepeats; 12223109a910SGarrett Wollman double avg = tsum / n; 12233109a910SGarrett Wollman double vari = tsumsq / n - avg * avg; 12241ad0b1beSMaxim Konovalov (void)printf( 12251ad0b1beSMaxim Konovalov "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", 12263109a910SGarrett Wollman tmin, avg, tmax, sqrt(vari)); 12273109a910SGarrett Wollman } 12282ceaae21SBruce Evans if (reset_kerninfo && tcgetattr(STDOUT_FILENO, &ts) != -1) { 1229badd8138SSean Eric Fagan ts.c_lflag &= ~NOKERNINFO; 12302ceaae21SBruce Evans tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 1231badd8138SSean Eric Fagan } 1232badd8138SSean Eric Fagan 12336e1173dcSDavid Greenman if (nreceived) 12348fae3551SRodney W. Grimes exit(0); 12356e1173dcSDavid Greenman else 12366e1173dcSDavid Greenman exit(2); 12378fae3551SRodney W. Grimes } 12388fae3551SRodney W. Grimes 12398fae3551SRodney W. Grimes #ifdef notdef 12408fae3551SRodney W. Grimes static char *ttab[] = { 12418fae3551SRodney W. Grimes "Echo Reply", /* ip + seq + udata */ 12428fae3551SRodney W. Grimes "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */ 12438fae3551SRodney W. Grimes "Source Quench", /* IP */ 12448fae3551SRodney W. Grimes "Redirect", /* redirect type, gateway, + IP */ 12458fae3551SRodney W. Grimes "Echo", 12468fae3551SRodney W. Grimes "Time Exceeded", /* transit, frag reassem + IP */ 12478fae3551SRodney W. Grimes "Parameter Problem", /* pointer + IP */ 12488fae3551SRodney W. Grimes "Timestamp", /* id + seq + three timestamps */ 12498fae3551SRodney W. Grimes "Timestamp Reply", /* " */ 12508fae3551SRodney W. Grimes "Info Request", /* id + sq */ 12518fae3551SRodney W. Grimes "Info Reply" /* " */ 12528fae3551SRodney W. Grimes }; 12538fae3551SRodney W. Grimes #endif 12548fae3551SRodney W. Grimes 12558fae3551SRodney W. Grimes /* 12568fae3551SRodney W. Grimes * pr_icmph -- 12578fae3551SRodney W. Grimes * Print a descriptive string about an ICMP header. 12588fae3551SRodney W. Grimes */ 125943470e3bSGarrett Wollman static void 12608fae3551SRodney W. Grimes pr_icmph(icp) 12618fae3551SRodney W. Grimes struct icmp *icp; 12628fae3551SRodney W. Grimes { 1263efc8588dSDavid E. O'Brien 12648fae3551SRodney W. Grimes switch(icp->icmp_type) { 12658fae3551SRodney W. Grimes case ICMP_ECHOREPLY: 12668fae3551SRodney W. Grimes (void)printf("Echo Reply\n"); 12678fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 12688fae3551SRodney W. Grimes break; 12698fae3551SRodney W. Grimes case ICMP_UNREACH: 12708fae3551SRodney W. Grimes switch(icp->icmp_code) { 12718fae3551SRodney W. Grimes case ICMP_UNREACH_NET: 12728fae3551SRodney W. Grimes (void)printf("Destination Net Unreachable\n"); 12738fae3551SRodney W. Grimes break; 12748fae3551SRodney W. Grimes case ICMP_UNREACH_HOST: 12758fae3551SRodney W. Grimes (void)printf("Destination Host Unreachable\n"); 12768fae3551SRodney W. Grimes break; 12778fae3551SRodney W. Grimes case ICMP_UNREACH_PROTOCOL: 12788fae3551SRodney W. Grimes (void)printf("Destination Protocol Unreachable\n"); 12798fae3551SRodney W. Grimes break; 12808fae3551SRodney W. Grimes case ICMP_UNREACH_PORT: 12818fae3551SRodney W. Grimes (void)printf("Destination Port Unreachable\n"); 12828fae3551SRodney W. Grimes break; 12838fae3551SRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 1284ef9e6dc7SBill Fenner (void)printf("frag needed and DF set (MTU %d)\n", 1285ff49597eSBill Fenner ntohs(icp->icmp_nextmtu)); 12868fae3551SRodney W. Grimes break; 12878fae3551SRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 12888fae3551SRodney W. Grimes (void)printf("Source Route Failed\n"); 12898fae3551SRodney W. Grimes break; 1290ef9e6dc7SBill Fenner case ICMP_UNREACH_FILTER_PROHIB: 1291ef9e6dc7SBill Fenner (void)printf("Communication prohibited by filter\n"); 1292ef9e6dc7SBill Fenner break; 12938fae3551SRodney W. Grimes default: 12948fae3551SRodney W. Grimes (void)printf("Dest Unreachable, Bad Code: %d\n", 12958fae3551SRodney W. Grimes icp->icmp_code); 12968fae3551SRodney W. Grimes break; 12978fae3551SRodney W. Grimes } 12988fae3551SRodney W. Grimes /* Print returned IP header information */ 12998fae3551SRodney W. Grimes #ifndef icmp_data 13008fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 13018fae3551SRodney W. Grimes #else 13028fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 13038fae3551SRodney W. Grimes #endif 13048fae3551SRodney W. Grimes break; 13058fae3551SRodney W. Grimes case ICMP_SOURCEQUENCH: 13068fae3551SRodney W. Grimes (void)printf("Source Quench\n"); 13078fae3551SRodney W. Grimes #ifndef icmp_data 13088fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 13098fae3551SRodney W. Grimes #else 13108fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 13118fae3551SRodney W. Grimes #endif 13128fae3551SRodney W. Grimes break; 13138fae3551SRodney W. Grimes case ICMP_REDIRECT: 13148fae3551SRodney W. Grimes switch(icp->icmp_code) { 13158fae3551SRodney W. Grimes case ICMP_REDIRECT_NET: 13168fae3551SRodney W. Grimes (void)printf("Redirect Network"); 13178fae3551SRodney W. Grimes break; 13188fae3551SRodney W. Grimes case ICMP_REDIRECT_HOST: 13198fae3551SRodney W. Grimes (void)printf("Redirect Host"); 13208fae3551SRodney W. Grimes break; 13218fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSNET: 13228fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Network"); 13238fae3551SRodney W. Grimes break; 13248fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSHOST: 13258fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Host"); 13268fae3551SRodney W. Grimes break; 13278fae3551SRodney W. Grimes default: 13288fae3551SRodney W. Grimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 13298fae3551SRodney W. Grimes break; 13308fae3551SRodney W. Grimes } 1331ff49597eSBill Fenner (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 13328fae3551SRodney W. Grimes #ifndef icmp_data 13338fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 13348fae3551SRodney W. Grimes #else 13358fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 13368fae3551SRodney W. Grimes #endif 13378fae3551SRodney W. Grimes break; 13388fae3551SRodney W. Grimes case ICMP_ECHO: 13398fae3551SRodney W. Grimes (void)printf("Echo Request\n"); 13408fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 13418fae3551SRodney W. Grimes break; 13428fae3551SRodney W. Grimes case ICMP_TIMXCEED: 13438fae3551SRodney W. Grimes switch(icp->icmp_code) { 13448fae3551SRodney W. Grimes case ICMP_TIMXCEED_INTRANS: 13458fae3551SRodney W. Grimes (void)printf("Time to live exceeded\n"); 13468fae3551SRodney W. Grimes break; 13478fae3551SRodney W. Grimes case ICMP_TIMXCEED_REASS: 13488fae3551SRodney W. Grimes (void)printf("Frag reassembly time exceeded\n"); 13498fae3551SRodney W. Grimes break; 13508fae3551SRodney W. Grimes default: 13518fae3551SRodney W. Grimes (void)printf("Time exceeded, Bad Code: %d\n", 13528fae3551SRodney W. Grimes icp->icmp_code); 13538fae3551SRodney W. Grimes break; 13548fae3551SRodney W. Grimes } 13558fae3551SRodney W. Grimes #ifndef icmp_data 13568fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 13578fae3551SRodney W. Grimes #else 13588fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 13598fae3551SRodney W. Grimes #endif 13608fae3551SRodney W. Grimes break; 13618fae3551SRodney W. Grimes case ICMP_PARAMPROB: 13628fae3551SRodney W. Grimes (void)printf("Parameter problem: pointer = 0x%02x\n", 13638fae3551SRodney W. Grimes icp->icmp_hun.ih_pptr); 13648fae3551SRodney W. Grimes #ifndef icmp_data 13658fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 13668fae3551SRodney W. Grimes #else 13678fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 13688fae3551SRodney W. Grimes #endif 13698fae3551SRodney W. Grimes break; 13708fae3551SRodney W. Grimes case ICMP_TSTAMP: 13718fae3551SRodney W. Grimes (void)printf("Timestamp\n"); 13728fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 13738fae3551SRodney W. Grimes break; 13748fae3551SRodney W. Grimes case ICMP_TSTAMPREPLY: 13758fae3551SRodney W. Grimes (void)printf("Timestamp Reply\n"); 13768fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 13778fae3551SRodney W. Grimes break; 13788fae3551SRodney W. Grimes case ICMP_IREQ: 13798fae3551SRodney W. Grimes (void)printf("Information Request\n"); 13808fae3551SRodney W. Grimes /* XXX ID + Seq */ 13818fae3551SRodney W. Grimes break; 13828fae3551SRodney W. Grimes case ICMP_IREQREPLY: 13838fae3551SRodney W. Grimes (void)printf("Information Reply\n"); 13848fae3551SRodney W. Grimes /* XXX ID + Seq */ 13858fae3551SRodney W. Grimes break; 13868fae3551SRodney W. Grimes case ICMP_MASKREQ: 13878fae3551SRodney W. Grimes (void)printf("Address Mask Request\n"); 13888fae3551SRodney W. Grimes break; 13898fae3551SRodney W. Grimes case ICMP_MASKREPLY: 13908fae3551SRodney W. Grimes (void)printf("Address Mask Reply\n"); 13918fae3551SRodney W. Grimes break; 1392ef9e6dc7SBill Fenner case ICMP_ROUTERADVERT: 1393ef9e6dc7SBill Fenner (void)printf("Router Advertisement\n"); 1394ef9e6dc7SBill Fenner break; 1395ef9e6dc7SBill Fenner case ICMP_ROUTERSOLICIT: 1396ef9e6dc7SBill Fenner (void)printf("Router Solicitation\n"); 1397ef9e6dc7SBill Fenner break; 13988fae3551SRodney W. Grimes default: 13998fae3551SRodney W. Grimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 14008fae3551SRodney W. Grimes } 14018fae3551SRodney W. Grimes } 14028fae3551SRodney W. Grimes 14038fae3551SRodney W. Grimes /* 14048fae3551SRodney W. Grimes * pr_iph -- 14058fae3551SRodney W. Grimes * Print an IP header with options. 14068fae3551SRodney W. Grimes */ 140743470e3bSGarrett Wollman static void 14088fae3551SRodney W. Grimes pr_iph(ip) 14098fae3551SRodney W. Grimes struct ip *ip; 14108fae3551SRodney W. Grimes { 14118fae3551SRodney W. Grimes u_char *cp; 1412efc8588dSDavid E. O'Brien int hlen; 14138fae3551SRodney W. Grimes 14148fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 14158fae3551SRodney W. Grimes cp = (u_char *)ip + 20; /* point to options */ 14168fae3551SRodney W. Grimes 1417ef9e6dc7SBill Fenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 14188fae3551SRodney W. Grimes (void)printf(" %1x %1x %02x %04x %04x", 1419ef9e6dc7SBill Fenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1420ef9e6dc7SBill Fenner ntohs(ip->ip_id)); 1421d32ff037SJohn Birrell (void)printf(" %1lx %04lx", 1422d32ff037SJohn Birrell (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13, 1423d32ff037SJohn Birrell (u_long) ntohl(ip->ip_off) & 0x1fff); 1424ef9e6dc7SBill Fenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1425ef9e6dc7SBill Fenner ntohs(ip->ip_sum)); 14268fae3551SRodney W. Grimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr)); 14278fae3551SRodney W. Grimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr)); 1428ef9e6dc7SBill Fenner /* dump any option bytes */ 14298fae3551SRodney W. Grimes while (hlen-- > 20) { 14308fae3551SRodney W. Grimes (void)printf("%02x", *cp++); 14318fae3551SRodney W. Grimes } 14328fae3551SRodney W. Grimes (void)putchar('\n'); 14338fae3551SRodney W. Grimes } 14348fae3551SRodney W. Grimes 14358fae3551SRodney W. Grimes /* 14368fae3551SRodney W. Grimes * pr_addr -- 14378fae3551SRodney W. Grimes * Return an ascii host address as a dotted quad and optionally with 14388fae3551SRodney W. Grimes * a hostname. 14398fae3551SRodney W. Grimes */ 144043470e3bSGarrett Wollman static char * 144143470e3bSGarrett Wollman pr_addr(ina) 144243470e3bSGarrett Wollman struct in_addr ina; 14438fae3551SRodney W. Grimes { 14448fae3551SRodney W. Grimes struct hostent *hp; 1445f78ac61bSWarner Losh static char buf[16 + 3 + MAXHOSTNAMELEN]; 14468fae3551SRodney W. Grimes 14478fae3551SRodney W. Grimes if ((options & F_NUMERIC) || 144843470e3bSGarrett Wollman !(hp = gethostbyaddr((char *)&ina, 4, AF_INET))) 144943470e3bSGarrett Wollman return inet_ntoa(ina); 14508fae3551SRodney W. Grimes else 1451efa38539SPeter Wemm (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 145243470e3bSGarrett Wollman inet_ntoa(ina)); 14538fae3551SRodney W. Grimes return(buf); 14548fae3551SRodney W. Grimes } 14558fae3551SRodney W. Grimes 14568fae3551SRodney W. Grimes /* 14578fae3551SRodney W. Grimes * pr_retip -- 14588fae3551SRodney W. Grimes * Dump some info on a returned (via ICMP) IP packet. 14598fae3551SRodney W. Grimes */ 146043470e3bSGarrett Wollman static void 14618fae3551SRodney W. Grimes pr_retip(ip) 14628fae3551SRodney W. Grimes struct ip *ip; 14638fae3551SRodney W. Grimes { 14648fae3551SRodney W. Grimes u_char *cp; 1465efc8588dSDavid E. O'Brien int hlen; 14668fae3551SRodney W. Grimes 14678fae3551SRodney W. Grimes pr_iph(ip); 14688fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 14698fae3551SRodney W. Grimes cp = (u_char *)ip + hlen; 14708fae3551SRodney W. Grimes 14718fae3551SRodney W. Grimes if (ip->ip_p == 6) 14728fae3551SRodney W. Grimes (void)printf("TCP: from port %u, to port %u (decimal)\n", 14738fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 14748fae3551SRodney W. Grimes else if (ip->ip_p == 17) 14758fae3551SRodney W. Grimes (void)printf("UDP: from port %u, to port %u (decimal)\n", 14768fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 14778fae3551SRodney W. Grimes } 14788fae3551SRodney W. Grimes 147943470e3bSGarrett Wollman static void 14808fae3551SRodney W. Grimes fill(bp, patp) 14818fae3551SRodney W. Grimes char *bp, *patp; 14828fae3551SRodney W. Grimes { 14838fae3551SRodney W. Grimes char *cp; 1484efc8588dSDavid E. O'Brien int pat[16]; 14854fba6582SMaxim Konovalov u_int ii, jj, kk; 14868fae3551SRodney W. Grimes 148743470e3bSGarrett Wollman for (cp = patp; *cp; cp++) { 148843470e3bSGarrett Wollman if (!isxdigit(*cp)) 148943470e3bSGarrett Wollman errx(EX_USAGE, 149043470e3bSGarrett Wollman "patterns must be specified as hex digits"); 149143470e3bSGarrett Wollman 14928fae3551SRodney W. Grimes } 14938fae3551SRodney W. Grimes ii = sscanf(patp, 14948fae3551SRodney W. Grimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 14958fae3551SRodney W. Grimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 14968fae3551SRodney W. Grimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 14978fae3551SRodney W. Grimes &pat[13], &pat[14], &pat[15]); 14988fae3551SRodney W. Grimes 14998fae3551SRodney W. Grimes if (ii > 0) 1500fb7d32c7SMaxim Konovalov for (kk = 0; kk <= maxpayload - (PHDR_LEN + ii); kk += ii) 15018fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 15028fae3551SRodney W. Grimes bp[jj + kk] = pat[jj]; 15038fae3551SRodney W. Grimes if (!(options & F_QUIET)) { 15048fae3551SRodney W. Grimes (void)printf("PATTERN: 0x"); 15058fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 15068fae3551SRodney W. Grimes (void)printf("%02x", bp[jj] & 0xFF); 15078fae3551SRodney W. Grimes (void)printf("\n"); 15088fae3551SRodney W. Grimes } 15098fae3551SRodney W. Grimes } 15108fae3551SRodney W. Grimes 151143470e3bSGarrett Wollman static void 1512e345a80dSPhilippe Charnier usage() 15138fae3551SRodney W. Grimes { 15141ad0b1beSMaxim Konovalov (void)fprintf(stderr, "%s\n%s\n%s\n", 15150b2f8b3fSMaxim Konovalov "usage: ping [-ADQRadfnoqrv] [-c count] [-i wait] [-l preload] [-m ttl]", 1516211bfbd2SRuslan Ermilov " [-p pattern] " 15179a4365d0SYoshinobu Inoue #ifdef IPSEC 15189a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 15199a4365d0SYoshinobu Inoue "[-P policy] " 15209a4365d0SYoshinobu Inoue #endif 15219a4365d0SYoshinobu Inoue #endif 15227237fd94SBill Fumerola "[-s packetsize] [-S src_addr] [-t timeout]", 15230b2f8b3fSMaxim Konovalov " [-z tos ] [host | [-L] [-I iface] [-T ttl] mcast-group]"); 152443470e3bSGarrett Wollman exit(EX_USAGE); 15258fae3551SRodney W. Grimes } 1526