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]; 216d074d39fSMatthew N. Dodd char *ep, *source, *target, *payload; 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); 252211bfbd2SRuslan Ermilov while ((ch = getopt(argc, argv, 253143008a1SMatthew N. Dodd "ADI:LQRS:T:c:adfi:l:m:Mnop:qrs:t:vz:" 254211bfbd2SRuslan Ermilov #ifdef IPSEC 2559a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 256211bfbd2SRuslan Ermilov "P:" 2579a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 258211bfbd2SRuslan Ermilov #endif /*IPSEC*/ 259211bfbd2SRuslan Ermilov )) != -1) 2609a4365d0SYoshinobu Inoue { 2618fae3551SRodney W. Grimes switch(ch) { 262ca517ad8SPoul-Henning Kamp case 'A': 263ca517ad8SPoul-Henning Kamp options |= F_MISSED; 264ca517ad8SPoul-Henning Kamp break; 265772dfa72SDaniel O'Callaghan case 'a': 266772dfa72SDaniel O'Callaghan options |= F_AUDIBLE; 267772dfa72SDaniel O'Callaghan break; 2688fae3551SRodney W. Grimes case 'c': 26943470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 27043470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp) 27143470e3bSGarrett Wollman errx(EX_USAGE, 27243470e3bSGarrett Wollman "invalid count of packets to transmit: `%s'", 27343470e3bSGarrett Wollman optarg); 27443470e3bSGarrett Wollman npackets = ultmp; 2758fae3551SRodney W. Grimes break; 2760b2f8b3fSMaxim Konovalov case 'D': 2770b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 2780b2f8b3fSMaxim Konovalov df = 1; 2790b2f8b3fSMaxim Konovalov break; 2808fae3551SRodney W. Grimes case 'd': 2818fae3551SRodney W. Grimes options |= F_SO_DEBUG; 2828fae3551SRodney W. Grimes break; 2838fae3551SRodney W. Grimes case 'f': 284526f06b2SMatthew Dillon if (uid) { 28543470e3bSGarrett Wollman errno = EPERM; 28643470e3bSGarrett Wollman err(EX_NOPERM, "-f flag"); 2878fae3551SRodney W. Grimes } 2888fae3551SRodney W. Grimes options |= F_FLOOD; 2898fae3551SRodney W. Grimes setbuf(stdout, (char *)NULL); 2908fae3551SRodney W. Grimes break; 2918fae3551SRodney W. Grimes case 'i': /* wait between sending packets */ 2921ad0b1beSMaxim Konovalov t = strtod(optarg, &ep) * 1000.0; 2931ad0b1beSMaxim Konovalov if (*ep || ep == optarg || t > (double)INT_MAX) 2941ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid timing interval: `%s'", 2951ad0b1beSMaxim Konovalov optarg); 2968fae3551SRodney W. Grimes options |= F_INTERVAL; 297526f06b2SMatthew Dillon interval = (int)t; 298526f06b2SMatthew Dillon if (uid && interval < 1000) { 299526f06b2SMatthew Dillon errno = EPERM; 300526f06b2SMatthew Dillon err(EX_NOPERM, "-i interval too short"); 301526f06b2SMatthew Dillon } 3028fae3551SRodney W. Grimes break; 30385456935SBill Fenner case 'I': /* multicast interface */ 30443470e3bSGarrett Wollman if (inet_aton(optarg, &ifaddr) == 0) 30543470e3bSGarrett Wollman errx(EX_USAGE, 30643470e3bSGarrett Wollman "invalid multicast interface: `%s'", 30743470e3bSGarrett Wollman optarg); 30885456935SBill Fenner options |= F_MIF; 30985456935SBill Fenner break; 3108fae3551SRodney W. Grimes case 'l': 31143470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 31243470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > INT_MAX) 31343470e3bSGarrett Wollman errx(EX_USAGE, 31443470e3bSGarrett Wollman "invalid preload value: `%s'", optarg); 3155e2cc0f4SStephen McKay if (uid) { 316f78ac61bSWarner Losh errno = EPERM; 317f78ac61bSWarner Losh err(EX_NOPERM, "-l flag"); 318f78ac61bSWarner Losh } 31943470e3bSGarrett Wollman preload = ultmp; 3208fae3551SRodney W. Grimes break; 32185456935SBill Fenner case 'L': 32285456935SBill Fenner options |= F_NOLOOP; 32385456935SBill Fenner loop = 0; 32485456935SBill Fenner break; 325211bfbd2SRuslan Ermilov case 'm': /* TTL */ 326211bfbd2SRuslan Ermilov ultmp = strtoul(optarg, &ep, 0); 3279bc1a9ecSMaxim Konovalov if (*ep || ep == optarg || ultmp > MAXTTL) 3281ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid TTL: `%s'", optarg); 329211bfbd2SRuslan Ermilov ttl = ultmp; 330211bfbd2SRuslan Ermilov options |= F_TTL; 331211bfbd2SRuslan Ermilov break; 332143008a1SMatthew N. Dodd case 'M': 333143008a1SMatthew N. Dodd options |= F_MASK; 334143008a1SMatthew N. Dodd break; 3358fae3551SRodney W. Grimes case 'n': 3368fae3551SRodney W. Grimes options |= F_NUMERIC; 3378fae3551SRodney W. Grimes break; 3388025c44bSDima Dorfman case 'o': 3398025c44bSDima Dorfman options |= F_ONCE; 3408025c44bSDima Dorfman break; 3418fae3551SRodney W. Grimes case 'p': /* fill buffer with user pattern */ 3428fae3551SRodney W. Grimes options |= F_PINGFILLED; 343d074d39fSMatthew N. Dodd payload = optarg; 3448fae3551SRodney W. Grimes break; 345ef9e6dc7SBill Fenner case 'Q': 346ef9e6dc7SBill Fenner options |= F_QUIET2; 347ef9e6dc7SBill Fenner break; 3488fae3551SRodney W. Grimes case 'q': 3498fae3551SRodney W. Grimes options |= F_QUIET; 3508fae3551SRodney W. Grimes break; 3518fae3551SRodney W. Grimes case 'R': 3528fae3551SRodney W. Grimes options |= F_RROUTE; 3538fae3551SRodney W. Grimes break; 3548fae3551SRodney W. Grimes case 'r': 3558fae3551SRodney W. Grimes options |= F_SO_DONTROUTE; 3568fae3551SRodney W. Grimes break; 3578fae3551SRodney W. Grimes case 's': /* size of packet to send */ 35843470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 3594fba6582SMaxim Konovalov if (*ep || ep == optarg) 36043470e3bSGarrett Wollman errx(EX_USAGE, "invalid packet size: `%s'", 36143470e3bSGarrett Wollman optarg); 362fb7d32c7SMaxim Konovalov if (uid != 0 && ultmp > DEFDATALEN) { 363fb7d32c7SMaxim Konovalov errno = EPERM; 364fb7d32c7SMaxim Konovalov err(EX_NOPERM, 365fb7d32c7SMaxim Konovalov "packet size too large: %lu > %u", 366fb7d32c7SMaxim Konovalov ultmp, DEFDATALEN); 367fb7d32c7SMaxim Konovalov } 36843470e3bSGarrett Wollman datalen = ultmp; 3698fae3551SRodney W. Grimes break; 37099490edeSWarner Losh case 'S': 37199490edeSWarner Losh source = optarg; 37299490edeSWarner Losh break; 3737237fd94SBill Fumerola case 't': 374bf113f1bSBill Fumerola alarmtimeout = strtoul(optarg, &ep, 0); 375bf113f1bSBill Fumerola if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX)) 3767237fd94SBill Fumerola errx(EX_USAGE, "invalid timeout: `%s'", 3777237fd94SBill Fumerola optarg); 378bf113f1bSBill Fumerola if (alarmtimeout > MAXALARM) 379bf113f1bSBill Fumerola errx(EX_USAGE, "invalid timeout: `%s' > %d", 380bf113f1bSBill Fumerola optarg, MAXALARM); 381bf113f1bSBill Fumerola alarm((int)alarmtimeout); 3827237fd94SBill Fumerola break; 38385456935SBill Fenner case 'T': /* multicast TTL */ 38443470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 3859bc1a9ecSMaxim Konovalov if (*ep || ep == optarg || ultmp > MAXTTL) 38643470e3bSGarrett Wollman errx(EX_USAGE, "invalid multicast TTL: `%s'", 38743470e3bSGarrett Wollman optarg); 388211bfbd2SRuslan Ermilov mttl = ultmp; 38985456935SBill Fenner options |= F_MTTL; 39085456935SBill Fenner break; 3918fae3551SRodney W. Grimes case 'v': 3928fae3551SRodney W. Grimes options |= F_VERBOSE; 3938fae3551SRodney W. Grimes break; 3949a4365d0SYoshinobu Inoue #ifdef IPSEC 3959a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 3969a4365d0SYoshinobu Inoue case 'P': 3979a4365d0SYoshinobu Inoue options |= F_POLICY; 3989a4365d0SYoshinobu Inoue if (!strncmp("in", optarg, 2)) 3999a4365d0SYoshinobu Inoue policy_in = strdup(optarg); 4009a4365d0SYoshinobu Inoue else if (!strncmp("out", optarg, 3)) 4019a4365d0SYoshinobu Inoue policy_out = strdup(optarg); 4029a4365d0SYoshinobu Inoue else 4039a4365d0SYoshinobu Inoue errx(1, "invalid security policy"); 4049a4365d0SYoshinobu Inoue break; 4050b2f8b3fSMaxim Konovalov case 'z': 4060b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 4070b2f8b3fSMaxim Konovalov ultmp = strtoul(optarg, &ep, 0); 4080b2f8b3fSMaxim Konovalov if (*ep || ep == optarg || ultmp > MAXTOS) 4090b2f8b3fSMaxim Konovalov errx(EX_USAGE, "invalid TOS: `%s'", optarg); 4100b2f8b3fSMaxim Konovalov tos = ultmp; 4110b2f8b3fSMaxim Konovalov break; 4129a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 4139a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 4148fae3551SRodney W. Grimes default: 415e345a80dSPhilippe Charnier usage(); 41643470e3bSGarrett Wollman } 41743470e3bSGarrett Wollman } 41843470e3bSGarrett Wollman 41943470e3bSGarrett Wollman if (argc - optind != 1) 420e345a80dSPhilippe Charnier usage(); 42143470e3bSGarrett Wollman target = argv[optind]; 4228fae3551SRodney W. Grimes 423fb7d32c7SMaxim Konovalov maxpayload = IP_MAXPACKET - sizeof(struct ip) - MINICMPLEN; 424fb7d32c7SMaxim Konovalov if (options & F_RROUTE) 425fb7d32c7SMaxim Konovalov maxpayload -= MAX_IPOPTLEN; 426fb7d32c7SMaxim Konovalov if (datalen > maxpayload) 427fb7d32c7SMaxim Konovalov errx(EX_USAGE, "packet size too large: %lu > %u", datalen, 428fb7d32c7SMaxim Konovalov maxpayload); 429d074d39fSMatthew N. Dodd datap = &outpack[MINICMPLEN + PHDR_LEN]; 430d074d39fSMatthew N. Dodd if (options & F_PINGFILLED) { 431d074d39fSMatthew N. Dodd fill((char *)datap, payload); 432d074d39fSMatthew N. Dodd } 43399490edeSWarner Losh if (source) { 43499490edeSWarner Losh bzero((char *)&sin, sizeof(sin)); 43599490edeSWarner Losh sin.sin_family = AF_INET; 43699490edeSWarner Losh if (inet_aton(source, &sin.sin_addr) != 0) { 43799490edeSWarner Losh shostname = source; 43899490edeSWarner Losh } else { 43999490edeSWarner Losh hp = gethostbyname2(source, AF_INET); 44099490edeSWarner Losh if (!hp) 44199490edeSWarner Losh errx(EX_NOHOST, "cannot resolve %s: %s", 44299490edeSWarner Losh source, hstrerror(h_errno)); 44399490edeSWarner Losh 44499490edeSWarner Losh sin.sin_len = sizeof sin; 4454fba6582SMaxim Konovalov if (hp->h_length > sizeof(sin.sin_addr) || 4464fba6582SMaxim Konovalov hp->h_length < 0) 44799490edeSWarner Losh errx(1, "gethostbyname2: illegal address"); 44899490edeSWarner Losh memcpy(&sin.sin_addr, hp->h_addr_list[0], 44999490edeSWarner Losh sizeof(sin.sin_addr)); 45099490edeSWarner Losh (void)strncpy(snamebuf, hp->h_name, 45199490edeSWarner Losh sizeof(snamebuf) - 1); 45299490edeSWarner Losh snamebuf[sizeof(snamebuf) - 1] = '\0'; 45399490edeSWarner Losh shostname = snamebuf; 45499490edeSWarner Losh } 45599490edeSWarner Losh if (bind(s, (struct sockaddr *)&sin, sizeof sin) == -1) 45699490edeSWarner Losh err(1, "bind"); 45799490edeSWarner Losh } 45899490edeSWarner Losh 459d389e86aSMatt Jacob bzero(&whereto, sizeof(whereto)); 460d389e86aSMatt Jacob to = &whereto; 4618fae3551SRodney W. Grimes to->sin_family = AF_INET; 462d389e86aSMatt Jacob to->sin_len = sizeof *to; 46343470e3bSGarrett Wollman if (inet_aton(target, &to->sin_addr) != 0) { 4648fae3551SRodney W. Grimes hostname = target; 46543470e3bSGarrett Wollman } else { 46643470e3bSGarrett Wollman hp = gethostbyname2(target, AF_INET); 46743470e3bSGarrett Wollman if (!hp) 46843470e3bSGarrett Wollman errx(EX_NOHOST, "cannot resolve %s: %s", 46943470e3bSGarrett Wollman target, hstrerror(h_errno)); 47043470e3bSGarrett Wollman 4711ffae4a6SWarner Losh if (hp->h_length > sizeof(to->sin_addr)) 4721ffae4a6SWarner Losh errx(1, "gethostbyname2 returned an illegal address"); 47343470e3bSGarrett Wollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 4748fae3551SRodney W. Grimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 475b10d9d5fSWarner Losh hnamebuf[sizeof(hnamebuf) - 1] = '\0'; 4768fae3551SRodney W. Grimes hostname = hnamebuf; 4778fae3551SRodney W. Grimes } 4788fae3551SRodney W. Grimes 47943470e3bSGarrett Wollman if (options & F_FLOOD && options & F_INTERVAL) 48043470e3bSGarrett Wollman errx(EX_USAGE, "-f and -i: incompatible options"); 48143470e3bSGarrett Wollman 48243470e3bSGarrett Wollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 48343470e3bSGarrett Wollman errx(EX_USAGE, 48443470e3bSGarrett Wollman "-f flag cannot be used with multicast destination"); 48543470e3bSGarrett Wollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 48643470e3bSGarrett Wollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 48743470e3bSGarrett Wollman errx(EX_USAGE, 48843470e3bSGarrett Wollman "-I, -L, -T flags cannot be used with unicast destination"); 4898fae3551SRodney W. Grimes 490143008a1SMatthew N. Dodd if (datalen - MASKLEN >= PHDR_LEN) /* can we time transfer */ 4918fae3551SRodney W. Grimes timing = 1; 4924fba6582SMaxim Konovalov packlen = MAXIPLEN + MAXICMPLEN + datalen; 4934fba6582SMaxim Konovalov packlen = packlen > IP_MAXPACKET ? IP_MAXPACKET : packlen; 49443470e3bSGarrett Wollman 4958fae3551SRodney W. Grimes if (!(options & F_PINGFILLED)) 496d32ff037SJohn Birrell for (i = PHDR_LEN; i < datalen; ++i) 4978fae3551SRodney W. Grimes *datap++ = i; 4988fae3551SRodney W. Grimes 4998fae3551SRodney W. Grimes ident = getpid() & 0xFFFF; 5008fae3551SRodney W. Grimes 501f1284d7aSBill Fenner if (s < 0) { 502f1284d7aSBill Fenner errno = sockerrno; 50343470e3bSGarrett Wollman err(EX_OSERR, "socket"); 5048fae3551SRodney W. Grimes } 5058fae3551SRodney W. Grimes hold = 1; 5068fae3551SRodney W. Grimes if (options & F_SO_DEBUG) 5078fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold, 5088fae3551SRodney W. Grimes sizeof(hold)); 5098fae3551SRodney W. Grimes if (options & F_SO_DONTROUTE) 5108fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 5118fae3551SRodney W. Grimes sizeof(hold)); 5129a4365d0SYoshinobu Inoue #ifdef IPSEC 5139a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 5149a4365d0SYoshinobu Inoue if (options & F_POLICY) { 5159a4365d0SYoshinobu Inoue char *buf; 5169a4365d0SYoshinobu Inoue if (policy_in != NULL) { 5179a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_in, strlen(policy_in)); 5189a4365d0SYoshinobu Inoue if (buf == NULL) 519ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 5209a4365d0SYoshinobu Inoue if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY, 5219a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 5221ad0b1beSMaxim Konovalov err(EX_CONFIG, 5231ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 5249a4365d0SYoshinobu Inoue free(buf); 5259a4365d0SYoshinobu Inoue } 5269a4365d0SYoshinobu Inoue 5279a4365d0SYoshinobu Inoue if (policy_out != NULL) { 5289a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_out, strlen(policy_out)); 5299a4365d0SYoshinobu Inoue if (buf == NULL) 530ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 5319a4365d0SYoshinobu Inoue if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY, 5329a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 5331ad0b1beSMaxim Konovalov err(EX_CONFIG, 5341ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 5359a4365d0SYoshinobu Inoue free(buf); 5369a4365d0SYoshinobu Inoue } 5379a4365d0SYoshinobu Inoue } 5389a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 5399a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 5408fae3551SRodney W. Grimes 5410b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 5420b2f8b3fSMaxim Konovalov ip = (struct ip*)outpackhdr; 5430b2f8b3fSMaxim Konovalov if (!(options & (F_TTL | F_MTTL))) { 5440b2f8b3fSMaxim Konovalov mib[0] = CTL_NET; 5450b2f8b3fSMaxim Konovalov mib[1] = PF_INET; 5460b2f8b3fSMaxim Konovalov mib[2] = IPPROTO_IP; 5470b2f8b3fSMaxim Konovalov mib[3] = IPCTL_DEFTTL; 5480b2f8b3fSMaxim Konovalov sz = sizeof(ttl); 5490b2f8b3fSMaxim Konovalov if (sysctl(mib, 4, &ttl, &sz, NULL, 0) == -1) 5500b2f8b3fSMaxim Konovalov err(1, "sysctl(net.inet.ip.ttl)"); 5510b2f8b3fSMaxim Konovalov } 5520b2f8b3fSMaxim Konovalov setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold)); 5530b2f8b3fSMaxim Konovalov ip->ip_v = IPVERSION; 5540b2f8b3fSMaxim Konovalov ip->ip_hl = sizeof(struct ip) >> 2; 5550b2f8b3fSMaxim Konovalov ip->ip_tos = tos; 5560b2f8b3fSMaxim Konovalov ip->ip_id = 0; 5570b2f8b3fSMaxim Konovalov ip->ip_off = df ? IP_DF : 0; 5580b2f8b3fSMaxim Konovalov ip->ip_ttl = ttl; 5590b2f8b3fSMaxim Konovalov ip->ip_p = IPPROTO_ICMP; 5600b2f8b3fSMaxim Konovalov ip->ip_src.s_addr = source ? sin.sin_addr.s_addr : INADDR_ANY; 5610b2f8b3fSMaxim Konovalov ip->ip_dst = to->sin_addr; 5620b2f8b3fSMaxim Konovalov } 5638fae3551SRodney W. Grimes /* record route option */ 5648fae3551SRodney W. Grimes if (options & F_RROUTE) { 5658fae3551SRodney W. Grimes #ifdef IP_OPTIONS 566039d6aa4SBill Fenner bzero(rspace, sizeof(rspace)); 5678fae3551SRodney W. Grimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 5688fae3551SRodney W. Grimes rspace[IPOPT_OLEN] = sizeof(rspace) - 1; 5698fae3551SRodney W. Grimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 570039d6aa4SBill Fenner rspace[sizeof(rspace) - 1] = IPOPT_EOL; 5718fae3551SRodney W. Grimes if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace, 57243470e3bSGarrett Wollman sizeof(rspace)) < 0) 57343470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 5748fae3551SRodney W. Grimes #else 57543470e3bSGarrett Wollman errx(EX_UNAVAILABLE, 57643470e3bSGarrett Wollman "record route not available in this implementation"); 5778fae3551SRodney W. Grimes #endif /* IP_OPTIONS */ 5788fae3551SRodney W. Grimes } 5798fae3551SRodney W. Grimes 580211bfbd2SRuslan Ermilov if (options & F_TTL) { 581211bfbd2SRuslan Ermilov if (setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, 582211bfbd2SRuslan Ermilov sizeof(ttl)) < 0) { 583211bfbd2SRuslan Ermilov err(EX_OSERR, "setsockopt IP_TTL"); 584211bfbd2SRuslan Ermilov } 585211bfbd2SRuslan Ermilov } 58685456935SBill Fenner if (options & F_NOLOOP) { 58785456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 58885456935SBill Fenner sizeof(loop)) < 0) { 58943470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 59085456935SBill Fenner } 59185456935SBill Fenner } 59285456935SBill Fenner if (options & F_MTTL) { 593211bfbd2SRuslan Ermilov if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &mttl, 594211bfbd2SRuslan Ermilov sizeof(mttl)) < 0) { 59543470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 59685456935SBill Fenner } 59785456935SBill Fenner } 59885456935SBill Fenner if (options & F_MIF) { 59985456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 60085456935SBill Fenner sizeof(ifaddr)) < 0) { 60143470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 60285456935SBill Fenner } 60385456935SBill Fenner } 604039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 605039d6aa4SBill Fenner { int on = 1; 606039d6aa4SBill Fenner if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) < 0) 607039d6aa4SBill Fenner err(EX_OSERR, "setsockopt SO_TIMESTAMP"); 608039d6aa4SBill Fenner } 609039d6aa4SBill Fenner #endif 61085456935SBill Fenner 6118fae3551SRodney W. Grimes /* 6128fae3551SRodney W. Grimes * When pinging the broadcast address, you can get a lot of answers. 6138fae3551SRodney W. Grimes * Doing something so evil is useful if you are trying to stress the 6148fae3551SRodney W. Grimes * ethernet, or just want to fill the arp cache to get some stuff for 61543470e3bSGarrett Wollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 61643470e3bSGarrett Wollman * or multicast pings if they wish. 6178fae3551SRodney W. Grimes */ 6184fba6582SMaxim Konovalov 6194fba6582SMaxim Konovalov /* 6204fba6582SMaxim Konovalov * XXX receive buffer needs undetermined space for mbuf overhead 6214fba6582SMaxim Konovalov * as well. 6224fba6582SMaxim Konovalov */ 6234fba6582SMaxim Konovalov hold = IP_MAXPACKET + 128; 6248fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 6258fae3551SRodney W. Grimes sizeof(hold)); 626261e59bbSMaxim Konovalov if (uid == 0) 627e8bd25ceSRobert Watson (void)setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&hold, 628e8bd25ceSRobert Watson sizeof(hold)); 629e8bd25ceSRobert Watson 63099490edeSWarner Losh if (to->sin_family == AF_INET) { 63199490edeSWarner Losh (void)printf("PING %s (%s)", hostname, 63299490edeSWarner Losh inet_ntoa(to->sin_addr)); 63399490edeSWarner Losh if (source) 63499490edeSWarner Losh (void)printf(" from %s", shostname); 63599490edeSWarner Losh (void)printf(": %d data bytes\n", datalen); 63699490edeSWarner Losh } else 6378fae3551SRodney W. Grimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 6388fae3551SRodney W. Grimes 6397d81b35cSBruce Evans /* 640a2a00888SSean Eric Fagan * Use sigaction() instead of signal() to get unambiguous semantics, 641a2a00888SSean Eric Fagan * in particular with SA_RESTART not set. 6427d81b35cSBruce Evans */ 643a2a00888SSean Eric Fagan 644f6bd468bSPaul Traina sigemptyset(&si_sa.sa_mask); 64537e5b2c6SSean Eric Fagan si_sa.sa_flags = 0; 646a2a00888SSean Eric Fagan 647a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 648a2a00888SSean Eric Fagan if (sigaction(SIGINT, &si_sa, 0) == -1) { 649a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGINT"); 650a2a00888SSean Eric Fagan } 651a2a00888SSean Eric Fagan 652a2a00888SSean Eric Fagan si_sa.sa_handler = status; 65337e5b2c6SSean Eric Fagan if (sigaction(SIGINFO, &si_sa, 0) == -1) { 654624ff938SWarner Losh err(EX_OSERR, "sigaction"); 65537e5b2c6SSean Eric Fagan } 656bf113f1bSBill Fumerola 657bf113f1bSBill Fumerola if (alarmtimeout > 0) { 6587237fd94SBill Fumerola si_sa.sa_handler = stopit; 6597237fd94SBill Fumerola if (sigaction(SIGALRM, &si_sa, 0) == -1) 6607237fd94SBill Fumerola err(EX_OSERR, "sigaction SIGALRM"); 661bf113f1bSBill Fumerola } 66237e5b2c6SSean Eric Fagan 663039d6aa4SBill Fenner bzero(&msg, sizeof(msg)); 664039d6aa4SBill Fenner msg.msg_name = (caddr_t)&from; 665039d6aa4SBill Fenner msg.msg_iov = &iov; 666039d6aa4SBill Fenner msg.msg_iovlen = 1; 667039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 668039d6aa4SBill Fenner msg.msg_control = (caddr_t)ctrl; 669039d6aa4SBill Fenner #endif 670039d6aa4SBill Fenner iov.iov_base = packet; 671039d6aa4SBill Fenner iov.iov_len = packlen; 672039d6aa4SBill Fenner 6734055611aSSean Eric Fagan if (tcgetattr(STDOUT_FILENO, &ts) != -1) { 6744055611aSSean Eric Fagan reset_kerninfo = !(ts.c_lflag & NOKERNINFO); 6754055611aSSean Eric Fagan ts.c_lflag |= NOKERNINFO; 6764055611aSSean Eric Fagan tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 6774055611aSSean Eric Fagan } 6784055611aSSean Eric Fagan 67932af342fSRuslan Ermilov if (preload == 0) 68032af342fSRuslan Ermilov pinger(); /* send the first ping */ 68132af342fSRuslan Ermilov else { 68232af342fSRuslan Ermilov if (npackets != 0 && preload > npackets) 68332af342fSRuslan Ermilov preload = npackets; 6848fae3551SRodney W. Grimes while (preload--) /* fire off them quickies */ 6858fae3551SRodney W. Grimes pinger(); 68632af342fSRuslan Ermilov } 68732af342fSRuslan Ermilov (void)gettimeofday(&last, NULL); 6888fae3551SRodney W. Grimes 689039d6aa4SBill Fenner if (options & F_FLOOD) { 690039d6aa4SBill Fenner intvl.tv_sec = 0; 691039d6aa4SBill Fenner intvl.tv_usec = 10000; 692039d6aa4SBill Fenner } else { 693526f06b2SMatthew Dillon intvl.tv_sec = interval / 1000; 694526f06b2SMatthew Dillon intvl.tv_usec = interval % 1000 * 1000; 695039d6aa4SBill Fenner } 696039d6aa4SBill Fenner 697261e59bbSMaxim Konovalov almost_done = 0; 6988f975bb3SBruce Evans while (!finish_up) { 699261e59bbSMaxim Konovalov struct timeval now, timeout; 700039d6aa4SBill Fenner fd_set rfds; 701261e59bbSMaxim Konovalov int cc, n; 7028fae3551SRodney W. Grimes 7037d81b35cSBruce Evans check_status(); 7047e5bbd68SJacques Vidrine if (s >= FD_SETSIZE) 7057e5bbd68SJacques Vidrine errx(EX_OSERR, "descriptor too large"); 706039d6aa4SBill Fenner FD_ZERO(&rfds); 707039d6aa4SBill Fenner FD_SET(s, &rfds); 708039d6aa4SBill Fenner (void)gettimeofday(&now, NULL); 709039d6aa4SBill Fenner timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec; 710039d6aa4SBill Fenner timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec; 711039d6aa4SBill Fenner while (timeout.tv_usec < 0) { 712039d6aa4SBill Fenner timeout.tv_usec += 1000000; 713039d6aa4SBill Fenner timeout.tv_sec--; 7148fae3551SRodney W. Grimes } 715e345a80dSPhilippe Charnier while (timeout.tv_usec >= 1000000) { 716039d6aa4SBill Fenner timeout.tv_usec -= 1000000; 717039d6aa4SBill Fenner timeout.tv_sec++; 718039d6aa4SBill Fenner } 719039d6aa4SBill Fenner if (timeout.tv_sec < 0) 720039d6aa4SBill Fenner timeout.tv_sec = timeout.tv_usec = 0; 721039d6aa4SBill Fenner n = select(s + 1, &rfds, NULL, NULL, &timeout); 7225e2cc0f4SStephen McKay if (n < 0) 7235e2cc0f4SStephen McKay continue; /* Must be EINTR. */ 724039d6aa4SBill Fenner if (n == 1) { 7259b219646SPeter Wemm struct timeval *t = NULL; 726039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 727039d6aa4SBill Fenner struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl; 728039d6aa4SBill Fenner 729039d6aa4SBill Fenner msg.msg_controllen = sizeof(ctrl); 730039d6aa4SBill Fenner #endif 731039d6aa4SBill Fenner msg.msg_namelen = sizeof(from); 732039d6aa4SBill Fenner if ((cc = recvmsg(s, &msg, 0)) < 0) { 7338fae3551SRodney W. Grimes if (errno == EINTR) 7348fae3551SRodney W. Grimes continue; 735e345a80dSPhilippe Charnier warn("recvmsg"); 7368fae3551SRodney W. Grimes continue; 7378fae3551SRodney W. Grimes } 738039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 739039d6aa4SBill Fenner if (cmsg->cmsg_level == SOL_SOCKET && 740039d6aa4SBill Fenner cmsg->cmsg_type == SCM_TIMESTAMP && 7416ecbec77SYoshinobu Inoue cmsg->cmsg_len == CMSG_LEN(sizeof *t)) { 742fa05a94cSJohn Birrell /* Copy to avoid alignment problems: */ 743fa05a94cSJohn Birrell memcpy(&now, CMSG_DATA(cmsg), sizeof(now)); 744fa05a94cSJohn Birrell t = &now; 745fa05a94cSJohn Birrell } 746039d6aa4SBill Fenner #endif 7479b219646SPeter Wemm if (t == NULL) { 748039d6aa4SBill Fenner (void)gettimeofday(&now, NULL); 749039d6aa4SBill Fenner t = &now; 750039d6aa4SBill Fenner } 751039d6aa4SBill Fenner pr_pack((char *)packet, cc, &from, t); 7528025c44bSDima Dorfman if (options & F_ONCE && nreceived || 7538025c44bSDima Dorfman npackets && nreceived >= npackets) 7548fae3551SRodney W. Grimes break; 7558fae3551SRodney W. Grimes } 7565e2cc0f4SStephen McKay if (n == 0 || options & F_FLOOD) { 757039d6aa4SBill Fenner if (!npackets || ntransmitted < npackets) 758039d6aa4SBill Fenner pinger(); 759039d6aa4SBill Fenner else { 760039d6aa4SBill Fenner if (almost_done) 761039d6aa4SBill Fenner break; 762039d6aa4SBill Fenner almost_done = 1; 7635e2cc0f4SStephen McKay intvl.tv_usec = 0; 764039d6aa4SBill Fenner if (nreceived) { 765039d6aa4SBill Fenner intvl.tv_sec = 2 * tmax / 1000; 766039d6aa4SBill Fenner if (!intvl.tv_sec) 767039d6aa4SBill Fenner intvl.tv_sec = 1; 768039d6aa4SBill Fenner } else 769039d6aa4SBill Fenner intvl.tv_sec = MAXWAIT; 770039d6aa4SBill Fenner } 771039d6aa4SBill Fenner (void)gettimeofday(&last, NULL); 77225107197SIan Dowse if (ntransmitted - nreceived - 1 > nmissedmax) { 77325107197SIan Dowse nmissedmax = ntransmitted - nreceived - 1; 77425107197SIan Dowse if (options & F_MISSED) 775ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 776039d6aa4SBill Fenner } 777039d6aa4SBill Fenner } 77825107197SIan Dowse } 7798f975bb3SBruce Evans finish(); 7808fae3551SRodney W. Grimes /* NOTREACHED */ 781f78ac61bSWarner Losh exit(0); /* Make the compiler happy */ 7828fae3551SRodney W. Grimes } 7838fae3551SRodney W. Grimes 7848fae3551SRodney W. Grimes /* 7858f975bb3SBruce Evans * stopit -- 7868f975bb3SBruce Evans * Set the global bit that causes the main loop to quit. 7878f975bb3SBruce Evans * Do NOT call finish() from here, since finish() does far too much 7888f975bb3SBruce Evans * to be called from a signal handler. 789515dd2faSJulian Elischer */ 790515dd2faSJulian Elischer void 7918f975bb3SBruce Evans stopit(sig) 792c6facae4SMaxim Konovalov int sig __unused; 793515dd2faSJulian Elischer { 794efc8588dSDavid E. O'Brien 795515dd2faSJulian Elischer finish_up = 1; 796515dd2faSJulian Elischer } 797515dd2faSJulian Elischer 798515dd2faSJulian Elischer /* 7998fae3551SRodney W. Grimes * pinger -- 8008fae3551SRodney W. Grimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 8018fae3551SRodney W. Grimes * will be added on by the kernel. The ID field is our UNIX process ID, 8024fba6582SMaxim Konovalov * and the sequence number is an ascending integer. The first PHDR_LEN 8034fba6582SMaxim Konovalov * bytes of the data portion are used to hold a UNIX "timeval" struct in 8044fba6582SMaxim Konovalov * host byte-order, to compute the round-trip time. 8058fae3551SRodney W. Grimes */ 80643470e3bSGarrett Wollman static void 80743470e3bSGarrett Wollman pinger(void) 8088fae3551SRodney W. Grimes { 8090b2f8b3fSMaxim Konovalov struct ip *ip; 8103d438ad6SDavid E. O'Brien struct icmp *icp; 811efc8588dSDavid E. O'Brien int cc, i; 8120b2f8b3fSMaxim Konovalov u_char *packet; 8138fae3551SRodney W. Grimes 8140b2f8b3fSMaxim Konovalov packet = outpack; 8158fae3551SRodney W. Grimes icp = (struct icmp *)outpack; 816143008a1SMatthew N. Dodd if (options & F_MASK) 817143008a1SMatthew N. Dodd icp->icmp_type = ICMP_MASKREQ; 818143008a1SMatthew N. Dodd else 8198fae3551SRodney W. Grimes icp->icmp_type = ICMP_ECHO; 8208fae3551SRodney W. Grimes icp->icmp_code = 0; 8218fae3551SRodney W. Grimes icp->icmp_cksum = 0; 8225db89bc7SBill Fenner icp->icmp_seq = htons(ntransmitted); 8238fae3551SRodney W. Grimes icp->icmp_id = ident; /* ID */ 8248fae3551SRodney W. Grimes 8255db89bc7SBill Fenner CLR(ntransmitted % mx_dup_ck); 8268fae3551SRodney W. Grimes 8278fae3551SRodney W. Grimes if (timing) 828143008a1SMatthew N. Dodd (void)gettimeofday((struct timeval *)&outpack[ 829143008a1SMatthew N. Dodd MINICMPLEN + MASKLEN], NULL); 8308fae3551SRodney W. Grimes 8314fba6582SMaxim Konovalov cc = MINICMPLEN + datalen; 8328fae3551SRodney W. Grimes 8338fae3551SRodney W. Grimes /* compute ICMP checksum here */ 8348fae3551SRodney W. Grimes icp->icmp_cksum = in_cksum((u_short *)icp, cc); 8358fae3551SRodney W. Grimes 8360b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 8370b2f8b3fSMaxim Konovalov cc += sizeof(struct ip); 8380b2f8b3fSMaxim Konovalov ip = (struct ip *)outpackhdr; 8390b2f8b3fSMaxim Konovalov ip->ip_len = cc; 8400b2f8b3fSMaxim Konovalov ip->ip_sum = in_cksum((u_short *)outpackhdr, cc); 8410b2f8b3fSMaxim Konovalov packet = outpackhdr; 8420b2f8b3fSMaxim Konovalov } 8430b2f8b3fSMaxim Konovalov i = sendto(s, (char *)packet, cc, 0, (struct sockaddr *)&whereto, 844d389e86aSMatt Jacob sizeof(whereto)); 8458fae3551SRodney W. Grimes 8468fae3551SRodney W. Grimes if (i < 0 || i != cc) { 84743470e3bSGarrett Wollman if (i < 0) { 8488f975bb3SBruce Evans if (options & F_FLOOD && errno == ENOBUFS) { 849515dd2faSJulian Elischer usleep(FLOOD_BACKOFF); 850515dd2faSJulian Elischer return; 851515dd2faSJulian Elischer } 85243470e3bSGarrett Wollman warn("sendto"); 85343470e3bSGarrett Wollman } else { 85443470e3bSGarrett Wollman warn("%s: partial write: %d of %d bytes", 855416aa49bSPoul-Henning Kamp hostname, i, cc); 8568fae3551SRodney W. Grimes } 857363d7bbeSJulian Elischer } 858363d7bbeSJulian Elischer ntransmitted++; 8598fae3551SRodney W. Grimes if (!(options & F_QUIET) && options & F_FLOOD) 8608fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &DOT, 1); 8618fae3551SRodney W. Grimes } 8628fae3551SRodney W. Grimes 8638fae3551SRodney W. Grimes /* 8648fae3551SRodney W. Grimes * pr_pack -- 8658fae3551SRodney W. Grimes * Print out the packet, if it came from us. This logic is necessary 8668fae3551SRodney W. Grimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 8678fae3551SRodney W. Grimes * which arrive ('tis only fair). This permits multiple copies of this 8688fae3551SRodney W. Grimes * program to be run without having intermingled output (or statistics!). 8698fae3551SRodney W. Grimes */ 87043470e3bSGarrett Wollman static void 871039d6aa4SBill Fenner pr_pack(buf, cc, from, tv) 8728fae3551SRodney W. Grimes char *buf; 8738fae3551SRodney W. Grimes int cc; 8748fae3551SRodney W. Grimes struct sockaddr_in *from; 875039d6aa4SBill Fenner struct timeval *tv; 8768fae3551SRodney W. Grimes { 8779b219646SPeter Wemm struct in_addr ina; 8789b219646SPeter Wemm u_char *cp, *dp; 8793d438ad6SDavid E. O'Brien struct icmp *icp; 8808fae3551SRodney W. Grimes struct ip *ip; 8819d2b0ab8SPeter Wemm const void *tp; 8828f975bb3SBruce Evans double triptime; 883efc8588dSDavid E. O'Brien int dupflag, hlen, i, j, seq; 884efc8588dSDavid E. O'Brien static int old_rrlen; 885efc8588dSDavid E. O'Brien static char old_rr[MAX_IPOPTLEN]; 8868fae3551SRodney W. Grimes 8878fae3551SRodney W. Grimes /* Check the IP header */ 8888fae3551SRodney W. Grimes ip = (struct ip *)buf; 8898fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 8908fae3551SRodney W. Grimes if (cc < hlen + ICMP_MINLEN) { 8918fae3551SRodney W. Grimes if (options & F_VERBOSE) 89243470e3bSGarrett Wollman warn("packet too short (%d bytes) from %s", cc, 89343470e3bSGarrett Wollman inet_ntoa(from->sin_addr)); 8948fae3551SRodney W. Grimes return; 8958fae3551SRodney W. Grimes } 8968fae3551SRodney W. Grimes 8978fae3551SRodney W. Grimes /* Now the ICMP part */ 8988fae3551SRodney W. Grimes cc -= hlen; 8998fae3551SRodney W. Grimes icp = (struct icmp *)(buf + hlen); 900143008a1SMatthew N. Dodd if ((icp->icmp_type == ICMP_ECHOREPLY) || 901143008a1SMatthew N. Dodd ((icp->icmp_type == ICMP_MASKREPLY) && (options & F_MASK))) { 9028fae3551SRodney W. Grimes if (icp->icmp_id != ident) 9038fae3551SRodney W. Grimes return; /* 'Twas not our ECHO */ 9048fae3551SRodney W. Grimes ++nreceived; 9058f975bb3SBruce Evans triptime = 0.0; 9068fae3551SRodney W. Grimes if (timing) { 907d32ff037SJohn Birrell struct timeval tv1; 9088fae3551SRodney W. Grimes #ifndef icmp_data 9099d2b0ab8SPeter Wemm tp = &icp->icmp_ip; 9108fae3551SRodney W. Grimes #else 9119d2b0ab8SPeter Wemm tp = icp->icmp_data; 9128fae3551SRodney W. Grimes #endif 913143008a1SMatthew N. Dodd tp+=MASKLEN; 914143008a1SMatthew N. Dodd 9159d2b0ab8SPeter Wemm /* Copy to avoid alignment problems: */ 9169d2b0ab8SPeter Wemm memcpy(&tv1, tp, sizeof(tv1)); 917039d6aa4SBill Fenner tvsub(tv, &tv1); 918039d6aa4SBill Fenner triptime = ((double)tv->tv_sec) * 1000.0 + 919039d6aa4SBill Fenner ((double)tv->tv_usec) / 1000.0; 9208fae3551SRodney W. Grimes tsum += triptime; 9213109a910SGarrett Wollman tsumsq += triptime * triptime; 9228fae3551SRodney W. Grimes if (triptime < tmin) 9238fae3551SRodney W. Grimes tmin = triptime; 9248fae3551SRodney W. Grimes if (triptime > tmax) 9258fae3551SRodney W. Grimes tmax = triptime; 9268fae3551SRodney W. Grimes } 9278fae3551SRodney W. Grimes 9285db89bc7SBill Fenner seq = ntohs(icp->icmp_seq); 9295db89bc7SBill Fenner 9305db89bc7SBill Fenner if (TST(seq % mx_dup_ck)) { 9318fae3551SRodney W. Grimes ++nrepeats; 9328fae3551SRodney W. Grimes --nreceived; 9338fae3551SRodney W. Grimes dupflag = 1; 9348fae3551SRodney W. Grimes } else { 9355db89bc7SBill Fenner SET(seq % mx_dup_ck); 9368fae3551SRodney W. Grimes dupflag = 0; 9378fae3551SRodney W. Grimes } 9388fae3551SRodney W. Grimes 9398fae3551SRodney W. Grimes if (options & F_QUIET) 9408fae3551SRodney W. Grimes return; 9418fae3551SRodney W. Grimes 9428fae3551SRodney W. Grimes if (options & F_FLOOD) 9438fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &BSPACE, 1); 9448fae3551SRodney W. Grimes else { 9458fae3551SRodney W. Grimes (void)printf("%d bytes from %s: icmp_seq=%u", cc, 9468fae3551SRodney W. Grimes inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr), 9475db89bc7SBill Fenner seq); 9488fae3551SRodney W. Grimes (void)printf(" ttl=%d", ip->ip_ttl); 9498fae3551SRodney W. Grimes if (timing) 950d410b6f1SDavid Greenman (void)printf(" time=%.3f ms", triptime); 9518fae3551SRodney W. Grimes if (dupflag) 9528fae3551SRodney W. Grimes (void)printf(" (DUP!)"); 953772dfa72SDaniel O'Callaghan if (options & F_AUDIBLE) 954ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 955143008a1SMatthew N. Dodd if (options & F_MASK) { 956143008a1SMatthew N. Dodd /* Just prentend this cast isn't ugly */ 957143008a1SMatthew N. Dodd (void)printf(" mask=%s", 958143008a1SMatthew N. Dodd pr_addr(*(struct in_addr *)&(icp->icmp_mask))); 959143008a1SMatthew N. Dodd } 9608fae3551SRodney W. Grimes /* check the data */ 961d32ff037SJohn Birrell cp = (u_char*)&icp->icmp_data[PHDR_LEN]; 9624fba6582SMaxim Konovalov dp = &outpack[MINICMPLEN + PHDR_LEN]; 963d32ff037SJohn Birrell for (i = PHDR_LEN; i < datalen; ++i, ++cp, ++dp) { 9648fae3551SRodney W. Grimes if (*cp != *dp) { 9658fae3551SRodney W. Grimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 9668fae3551SRodney W. Grimes i, *dp, *cp); 9671ad0b1beSMaxim Konovalov (void)printf("\ncp:"); 9688fae3551SRodney W. Grimes cp = (u_char*)&icp->icmp_data[0]; 969d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 970d32ff037SJohn Birrell if ((i % 32) == 8) 971d32ff037SJohn Birrell (void)printf("\n\t"); 972d32ff037SJohn Birrell (void)printf("%x ", *cp); 973d32ff037SJohn Birrell } 9741ad0b1beSMaxim Konovalov (void)printf("\ndp:"); 9754fba6582SMaxim Konovalov cp = &outpack[MINICMPLEN]; 976d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 9778fae3551SRodney W. Grimes if ((i % 32) == 8) 9788fae3551SRodney W. Grimes (void)printf("\n\t"); 9798fae3551SRodney W. Grimes (void)printf("%x ", *cp); 9808fae3551SRodney W. Grimes } 9818fae3551SRodney W. Grimes break; 9828fae3551SRodney W. Grimes } 9838fae3551SRodney W. Grimes } 9848fae3551SRodney W. Grimes } 9858fae3551SRodney W. Grimes } else { 986ef9e6dc7SBill Fenner /* 987ef9e6dc7SBill Fenner * We've got something other than an ECHOREPLY. 988ef9e6dc7SBill Fenner * See if it's a reply to something that we sent. 989ef9e6dc7SBill Fenner * We can compare IP destination, protocol, 990ef9e6dc7SBill Fenner * and ICMP type and ID. 991f78ac61bSWarner Losh * 992f78ac61bSWarner Losh * Only print all the error messages if we are running 993f78ac61bSWarner Losh * as root to avoid leaking information not normally 994f78ac61bSWarner Losh * available to those not running as root. 995ef9e6dc7SBill Fenner */ 996ef9e6dc7SBill Fenner #ifndef icmp_data 997ef9e6dc7SBill Fenner struct ip *oip = &icp->icmp_ip; 998ef9e6dc7SBill Fenner #else 999ef9e6dc7SBill Fenner struct ip *oip = (struct ip *)icp->icmp_data; 1000ef9e6dc7SBill Fenner #endif 1001ef9e6dc7SBill Fenner struct icmp *oicmp = (struct icmp *)(oip + 1); 1002ef9e6dc7SBill Fenner 1003ee2bf734SWarner Losh if (((options & F_VERBOSE) && uid == 0) || 1004ef9e6dc7SBill Fenner (!(options & F_QUIET2) && 1005d389e86aSMatt Jacob (oip->ip_dst.s_addr == whereto.sin_addr.s_addr) && 1006ef9e6dc7SBill Fenner (oip->ip_p == IPPROTO_ICMP) && 1007ef9e6dc7SBill Fenner (oicmp->icmp_type == ICMP_ECHO) && 1008ef9e6dc7SBill Fenner (oicmp->icmp_id == ident))) { 10098fae3551SRodney W. Grimes (void)printf("%d bytes from %s: ", cc, 101043470e3bSGarrett Wollman pr_addr(from->sin_addr)); 10118fae3551SRodney W. Grimes pr_icmph(icp); 1012ef9e6dc7SBill Fenner } else 1013ef9e6dc7SBill Fenner return; 10148fae3551SRodney W. Grimes } 10158fae3551SRodney W. Grimes 10168fae3551SRodney W. Grimes /* Display any IP options */ 10178fae3551SRodney W. Grimes cp = (u_char *)buf + sizeof(struct ip); 10188fae3551SRodney W. Grimes 10198fae3551SRodney W. Grimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 10208fae3551SRodney W. Grimes switch (*cp) { 10218fae3551SRodney W. Grimes case IPOPT_EOL: 10228fae3551SRodney W. Grimes hlen = 0; 10238fae3551SRodney W. Grimes break; 10248fae3551SRodney W. Grimes case IPOPT_LSRR: 1025cb75aca7SMaxim Konovalov case IPOPT_SSRR: 1026cb75aca7SMaxim Konovalov (void)printf(*cp == IPOPT_LSRR ? 1027cb75aca7SMaxim Konovalov "\nLSRR: " : "\nSSRR: "); 1028301207dfSMaxim Konovalov j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1; 10298fae3551SRodney W. Grimes hlen -= 2; 1030301207dfSMaxim Konovalov cp += 2; 10313c721ab3SMaxim Konovalov if (j >= INADDR_LEN && 10323c721ab3SMaxim Konovalov j <= hlen - (int)sizeof(struct ip)) { 10338fae3551SRodney W. Grimes for (;;) { 1034301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1035301207dfSMaxim Konovalov if (ina.s_addr == 0) 10361ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1037301207dfSMaxim Konovalov else 10381ad0b1beSMaxim Konovalov (void)printf("\t%s", 10391ad0b1beSMaxim Konovalov pr_addr(ina)); 1040301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1041301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1042301207dfSMaxim Konovalov j -= INADDR_LEN; 1043301207dfSMaxim Konovalov if (j < INADDR_LEN) 10448fae3551SRodney W. Grimes break; 10458fae3551SRodney W. Grimes (void)putchar('\n'); 10468fae3551SRodney W. Grimes } 1047301207dfSMaxim Konovalov } else 1048301207dfSMaxim Konovalov (void)printf("\t(truncated route)\n"); 10498fae3551SRodney W. Grimes break; 10508fae3551SRodney W. Grimes case IPOPT_RR: 1051301207dfSMaxim Konovalov j = cp[IPOPT_OLEN]; /* get length */ 1052301207dfSMaxim Konovalov i = cp[IPOPT_OFFSET]; /* and pointer */ 10538fae3551SRodney W. Grimes hlen -= 2; 1054301207dfSMaxim Konovalov cp += 2; 10558fae3551SRodney W. Grimes if (i > j) 10568fae3551SRodney W. Grimes i = j; 1057301207dfSMaxim Konovalov i = i - IPOPT_MINOFF + 1; 1058301207dfSMaxim Konovalov if (i < 0 || i > (hlen - (int)sizeof(struct ip))) { 1059301207dfSMaxim Konovalov old_rrlen = 0; 10608fae3551SRodney W. Grimes continue; 1061301207dfSMaxim Konovalov } 10628fae3551SRodney W. Grimes if (i == old_rrlen 10638fae3551SRodney W. Grimes && !bcmp((char *)cp, old_rr, i) 10648fae3551SRodney W. Grimes && !(options & F_FLOOD)) { 10658fae3551SRodney W. Grimes (void)printf("\t(same route)"); 10668fae3551SRodney W. Grimes hlen -= i; 10678fae3551SRodney W. Grimes cp += i; 10688fae3551SRodney W. Grimes break; 10698fae3551SRodney W. Grimes } 10708fae3551SRodney W. Grimes old_rrlen = i; 10718fae3551SRodney W. Grimes bcopy((char *)cp, old_rr, i); 10728fae3551SRodney W. Grimes (void)printf("\nRR: "); 1073301207dfSMaxim Konovalov if (i >= INADDR_LEN && 1074301207dfSMaxim Konovalov i <= hlen - (int)sizeof(struct ip)) { 10758fae3551SRodney W. Grimes for (;;) { 1076301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1077301207dfSMaxim Konovalov if (ina.s_addr == 0) 10781ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1079301207dfSMaxim Konovalov else 1080301207dfSMaxim Konovalov (void)printf("\t%s", 1081301207dfSMaxim Konovalov pr_addr(ina)); 1082301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1083301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1084301207dfSMaxim Konovalov i -= INADDR_LEN; 1085301207dfSMaxim Konovalov if (i < INADDR_LEN) 10868fae3551SRodney W. Grimes break; 10878fae3551SRodney W. Grimes (void)putchar('\n'); 10888fae3551SRodney W. Grimes } 1089301207dfSMaxim Konovalov } else 1090301207dfSMaxim Konovalov (void)printf("\t(truncated route)"); 10918fae3551SRodney W. Grimes break; 10928fae3551SRodney W. Grimes case IPOPT_NOP: 10938fae3551SRodney W. Grimes (void)printf("\nNOP"); 10948fae3551SRodney W. Grimes break; 10958fae3551SRodney W. Grimes default: 10968fae3551SRodney W. Grimes (void)printf("\nunknown option %x", *cp); 10978fae3551SRodney W. Grimes break; 10988fae3551SRodney W. Grimes } 10998fae3551SRodney W. Grimes if (!(options & F_FLOOD)) { 11008fae3551SRodney W. Grimes (void)putchar('\n'); 11018fae3551SRodney W. Grimes (void)fflush(stdout); 11028fae3551SRodney W. Grimes } 11038fae3551SRodney W. Grimes } 11048fae3551SRodney W. Grimes 11058fae3551SRodney W. Grimes /* 11068fae3551SRodney W. Grimes * in_cksum -- 11078fae3551SRodney W. Grimes * Checksum routine for Internet Protocol family headers (C Version) 11088fae3551SRodney W. Grimes */ 110943470e3bSGarrett Wollman u_short 11108fae3551SRodney W. Grimes in_cksum(addr, len) 11118fae3551SRodney W. Grimes u_short *addr; 11128fae3551SRodney W. Grimes int len; 11138fae3551SRodney W. Grimes { 1114efc8588dSDavid E. O'Brien int nleft, sum; 1115efc8588dSDavid E. O'Brien u_short *w; 11163ec12efeSPierre Beyssac union { 111709333e78SPierre Beyssac u_short us; 111809333e78SPierre Beyssac u_char uc[2]; 111909333e78SPierre Beyssac } last; 112009333e78SPierre Beyssac u_short answer; 11218fae3551SRodney W. Grimes 1122efc8588dSDavid E. O'Brien nleft = len; 1123efc8588dSDavid E. O'Brien sum = 0; 1124efc8588dSDavid E. O'Brien w = addr; 1125efc8588dSDavid E. O'Brien 11268fae3551SRodney W. Grimes /* 11278fae3551SRodney W. Grimes * Our algorithm is simple, using a 32 bit accumulator (sum), we add 11288fae3551SRodney W. Grimes * sequential 16 bit words to it, and at the end, fold back all the 11298fae3551SRodney W. Grimes * carry bits from the top 16 bits into the lower 16 bits. 11308fae3551SRodney W. Grimes */ 11318fae3551SRodney W. Grimes while (nleft > 1) { 11328fae3551SRodney W. Grimes sum += *w++; 11338fae3551SRodney W. Grimes nleft -= 2; 11348fae3551SRodney W. Grimes } 11358fae3551SRodney W. Grimes 11368fae3551SRodney W. Grimes /* mop up an odd byte, if necessary */ 11378fae3551SRodney W. Grimes if (nleft == 1) { 113809333e78SPierre Beyssac last.uc[0] = *(u_char *)w; 113909333e78SPierre Beyssac last.uc[1] = 0; 114009333e78SPierre Beyssac sum += last.us; 11418fae3551SRodney W. Grimes } 11428fae3551SRodney W. Grimes 11438fae3551SRodney W. Grimes /* add back carry outs from top 16 bits to low 16 bits */ 11448fae3551SRodney W. Grimes sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 11458fae3551SRodney W. Grimes sum += (sum >> 16); /* add carry */ 114609333e78SPierre Beyssac answer = ~sum; /* truncate to 16 bits */ 114709333e78SPierre Beyssac return(answer); 11488fae3551SRodney W. Grimes } 11498fae3551SRodney W. Grimes 11508fae3551SRodney W. Grimes /* 11518fae3551SRodney W. Grimes * tvsub -- 11528fae3551SRodney W. Grimes * Subtract 2 timeval structs: out = out - in. Out is assumed to 11538fae3551SRodney W. Grimes * be >= in. 11548fae3551SRodney W. Grimes */ 115543470e3bSGarrett Wollman static void 11568fae3551SRodney W. Grimes tvsub(out, in) 11573d438ad6SDavid E. O'Brien struct timeval *out, *in; 11588fae3551SRodney W. Grimes { 1159efc8588dSDavid E. O'Brien 11608fae3551SRodney W. Grimes if ((out->tv_usec -= in->tv_usec) < 0) { 11618fae3551SRodney W. Grimes --out->tv_sec; 11628fae3551SRodney W. Grimes out->tv_usec += 1000000; 11638fae3551SRodney W. Grimes } 11648fae3551SRodney W. Grimes out->tv_sec -= in->tv_sec; 11658fae3551SRodney W. Grimes } 11668fae3551SRodney W. Grimes 11678fae3551SRodney W. Grimes /* 1168badd8138SSean Eric Fagan * status -- 1169badd8138SSean Eric Fagan * Print out statistics when SIGINFO is received. 1170badd8138SSean Eric Fagan */ 1171badd8138SSean Eric Fagan 117243470e3bSGarrett Wollman static void 11737d81b35cSBruce Evans status(sig) 1174c6facae4SMaxim Konovalov int sig __unused; 11757d81b35cSBruce Evans { 1176efc8588dSDavid E. O'Brien 117737e5b2c6SSean Eric Fagan siginfo_p = 1; 117837e5b2c6SSean Eric Fagan } 117937e5b2c6SSean Eric Fagan 118043470e3bSGarrett Wollman static void 118137e5b2c6SSean Eric Fagan check_status() 1182badd8138SSean Eric Fagan { 1183efc8588dSDavid E. O'Brien 118437e5b2c6SSean Eric Fagan if (siginfo_p) { 118537e5b2c6SSean Eric Fagan siginfo_p = 0; 11867d81b35cSBruce Evans (void)fprintf(stderr, 11877d81b35cSBruce Evans "\r%ld/%ld packets received (%.0f%%) %.3f min / %.3f avg / %.3f max\n", 11887d81b35cSBruce Evans nreceived, ntransmitted, 11897d81b35cSBruce Evans ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0, 11907d81b35cSBruce Evans nreceived ? tmin : 0.0, 11917d81b35cSBruce Evans nreceived + nrepeats ? tsum / (nreceived + nrepeats) : tsum, 11927d81b35cSBruce Evans tmax); 119337e5b2c6SSean Eric Fagan } 1194badd8138SSean Eric Fagan } 1195badd8138SSean Eric Fagan 1196badd8138SSean Eric Fagan /* 11978fae3551SRodney W. Grimes * finish -- 11988fae3551SRodney W. Grimes * Print out statistics, and give up. 11998fae3551SRodney W. Grimes */ 120043470e3bSGarrett Wollman static void 12018f975bb3SBruce Evans finish() 12028fae3551SRodney W. Grimes { 1203badd8138SSean Eric Fagan struct termios ts; 12048fae3551SRodney W. Grimes 12058fae3551SRodney W. Grimes (void)signal(SIGINT, SIG_IGN); 1206a2a00888SSean Eric Fagan (void)signal(SIGALRM, SIG_IGN); 12078fae3551SRodney W. Grimes (void)putchar('\n'); 12088fae3551SRodney W. Grimes (void)fflush(stdout); 12098fae3551SRodney W. Grimes (void)printf("--- %s ping statistics ---\n", hostname); 12108fae3551SRodney W. Grimes (void)printf("%ld packets transmitted, ", ntransmitted); 12118fae3551SRodney W. Grimes (void)printf("%ld packets received, ", nreceived); 12128fae3551SRodney W. Grimes if (nrepeats) 12138fae3551SRodney W. Grimes (void)printf("+%ld duplicates, ", nrepeats); 1214ebe70c8fSWarner Losh if (ntransmitted) { 12158fae3551SRodney W. Grimes if (nreceived > ntransmitted) 12168fae3551SRodney W. Grimes (void)printf("-- somebody's printing up packets!"); 12178fae3551SRodney W. Grimes else 12188fae3551SRodney W. Grimes (void)printf("%d%% packet loss", 12198fae3551SRodney W. Grimes (int)(((ntransmitted - nreceived) * 100) / 12208fae3551SRodney W. Grimes ntransmitted)); 1221ebe70c8fSWarner Losh } 12228fae3551SRodney W. Grimes (void)putchar('\n'); 12233109a910SGarrett Wollman if (nreceived && timing) { 12243109a910SGarrett Wollman double n = nreceived + nrepeats; 12253109a910SGarrett Wollman double avg = tsum / n; 12263109a910SGarrett Wollman double vari = tsumsq / n - avg * avg; 12271ad0b1beSMaxim Konovalov (void)printf( 12281ad0b1beSMaxim Konovalov "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", 12293109a910SGarrett Wollman tmin, avg, tmax, sqrt(vari)); 12303109a910SGarrett Wollman } 12312ceaae21SBruce Evans if (reset_kerninfo && tcgetattr(STDOUT_FILENO, &ts) != -1) { 1232badd8138SSean Eric Fagan ts.c_lflag &= ~NOKERNINFO; 12332ceaae21SBruce Evans tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 1234badd8138SSean Eric Fagan } 1235badd8138SSean Eric Fagan 12366e1173dcSDavid Greenman if (nreceived) 12378fae3551SRodney W. Grimes exit(0); 12386e1173dcSDavid Greenman else 12396e1173dcSDavid Greenman exit(2); 12408fae3551SRodney W. Grimes } 12418fae3551SRodney W. Grimes 12428fae3551SRodney W. Grimes #ifdef notdef 12438fae3551SRodney W. Grimes static char *ttab[] = { 12448fae3551SRodney W. Grimes "Echo Reply", /* ip + seq + udata */ 12458fae3551SRodney W. Grimes "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */ 12468fae3551SRodney W. Grimes "Source Quench", /* IP */ 12478fae3551SRodney W. Grimes "Redirect", /* redirect type, gateway, + IP */ 12488fae3551SRodney W. Grimes "Echo", 12498fae3551SRodney W. Grimes "Time Exceeded", /* transit, frag reassem + IP */ 12508fae3551SRodney W. Grimes "Parameter Problem", /* pointer + IP */ 12518fae3551SRodney W. Grimes "Timestamp", /* id + seq + three timestamps */ 12528fae3551SRodney W. Grimes "Timestamp Reply", /* " */ 12538fae3551SRodney W. Grimes "Info Request", /* id + sq */ 12548fae3551SRodney W. Grimes "Info Reply" /* " */ 12558fae3551SRodney W. Grimes }; 12568fae3551SRodney W. Grimes #endif 12578fae3551SRodney W. Grimes 12588fae3551SRodney W. Grimes /* 12598fae3551SRodney W. Grimes * pr_icmph -- 12608fae3551SRodney W. Grimes * Print a descriptive string about an ICMP header. 12618fae3551SRodney W. Grimes */ 126243470e3bSGarrett Wollman static void 12638fae3551SRodney W. Grimes pr_icmph(icp) 12648fae3551SRodney W. Grimes struct icmp *icp; 12658fae3551SRodney W. Grimes { 1266efc8588dSDavid E. O'Brien 12678fae3551SRodney W. Grimes switch(icp->icmp_type) { 12688fae3551SRodney W. Grimes case ICMP_ECHOREPLY: 12698fae3551SRodney W. Grimes (void)printf("Echo Reply\n"); 12708fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 12718fae3551SRodney W. Grimes break; 12728fae3551SRodney W. Grimes case ICMP_UNREACH: 12738fae3551SRodney W. Grimes switch(icp->icmp_code) { 12748fae3551SRodney W. Grimes case ICMP_UNREACH_NET: 12758fae3551SRodney W. Grimes (void)printf("Destination Net Unreachable\n"); 12768fae3551SRodney W. Grimes break; 12778fae3551SRodney W. Grimes case ICMP_UNREACH_HOST: 12788fae3551SRodney W. Grimes (void)printf("Destination Host Unreachable\n"); 12798fae3551SRodney W. Grimes break; 12808fae3551SRodney W. Grimes case ICMP_UNREACH_PROTOCOL: 12818fae3551SRodney W. Grimes (void)printf("Destination Protocol Unreachable\n"); 12828fae3551SRodney W. Grimes break; 12838fae3551SRodney W. Grimes case ICMP_UNREACH_PORT: 12848fae3551SRodney W. Grimes (void)printf("Destination Port Unreachable\n"); 12858fae3551SRodney W. Grimes break; 12868fae3551SRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 1287ef9e6dc7SBill Fenner (void)printf("frag needed and DF set (MTU %d)\n", 1288ff49597eSBill Fenner ntohs(icp->icmp_nextmtu)); 12898fae3551SRodney W. Grimes break; 12908fae3551SRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 12918fae3551SRodney W. Grimes (void)printf("Source Route Failed\n"); 12928fae3551SRodney W. Grimes break; 1293ef9e6dc7SBill Fenner case ICMP_UNREACH_FILTER_PROHIB: 1294ef9e6dc7SBill Fenner (void)printf("Communication prohibited by filter\n"); 1295ef9e6dc7SBill Fenner break; 12968fae3551SRodney W. Grimes default: 12978fae3551SRodney W. Grimes (void)printf("Dest Unreachable, Bad Code: %d\n", 12988fae3551SRodney W. Grimes icp->icmp_code); 12998fae3551SRodney W. Grimes break; 13008fae3551SRodney W. Grimes } 13018fae3551SRodney W. Grimes /* Print returned IP header information */ 13028fae3551SRodney W. Grimes #ifndef icmp_data 13038fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 13048fae3551SRodney W. Grimes #else 13058fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 13068fae3551SRodney W. Grimes #endif 13078fae3551SRodney W. Grimes break; 13088fae3551SRodney W. Grimes case ICMP_SOURCEQUENCH: 13098fae3551SRodney W. Grimes (void)printf("Source Quench\n"); 13108fae3551SRodney W. Grimes #ifndef icmp_data 13118fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 13128fae3551SRodney W. Grimes #else 13138fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 13148fae3551SRodney W. Grimes #endif 13158fae3551SRodney W. Grimes break; 13168fae3551SRodney W. Grimes case ICMP_REDIRECT: 13178fae3551SRodney W. Grimes switch(icp->icmp_code) { 13188fae3551SRodney W. Grimes case ICMP_REDIRECT_NET: 13198fae3551SRodney W. Grimes (void)printf("Redirect Network"); 13208fae3551SRodney W. Grimes break; 13218fae3551SRodney W. Grimes case ICMP_REDIRECT_HOST: 13228fae3551SRodney W. Grimes (void)printf("Redirect Host"); 13238fae3551SRodney W. Grimes break; 13248fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSNET: 13258fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Network"); 13268fae3551SRodney W. Grimes break; 13278fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSHOST: 13288fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Host"); 13298fae3551SRodney W. Grimes break; 13308fae3551SRodney W. Grimes default: 13318fae3551SRodney W. Grimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 13328fae3551SRodney W. Grimes break; 13338fae3551SRodney W. Grimes } 1334ff49597eSBill Fenner (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 13358fae3551SRodney W. Grimes #ifndef icmp_data 13368fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 13378fae3551SRodney W. Grimes #else 13388fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 13398fae3551SRodney W. Grimes #endif 13408fae3551SRodney W. Grimes break; 13418fae3551SRodney W. Grimes case ICMP_ECHO: 13428fae3551SRodney W. Grimes (void)printf("Echo Request\n"); 13438fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 13448fae3551SRodney W. Grimes break; 13458fae3551SRodney W. Grimes case ICMP_TIMXCEED: 13468fae3551SRodney W. Grimes switch(icp->icmp_code) { 13478fae3551SRodney W. Grimes case ICMP_TIMXCEED_INTRANS: 13488fae3551SRodney W. Grimes (void)printf("Time to live exceeded\n"); 13498fae3551SRodney W. Grimes break; 13508fae3551SRodney W. Grimes case ICMP_TIMXCEED_REASS: 13518fae3551SRodney W. Grimes (void)printf("Frag reassembly time exceeded\n"); 13528fae3551SRodney W. Grimes break; 13538fae3551SRodney W. Grimes default: 13548fae3551SRodney W. Grimes (void)printf("Time exceeded, Bad Code: %d\n", 13558fae3551SRodney W. Grimes icp->icmp_code); 13568fae3551SRodney W. Grimes break; 13578fae3551SRodney W. Grimes } 13588fae3551SRodney W. Grimes #ifndef icmp_data 13598fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 13608fae3551SRodney W. Grimes #else 13618fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 13628fae3551SRodney W. Grimes #endif 13638fae3551SRodney W. Grimes break; 13648fae3551SRodney W. Grimes case ICMP_PARAMPROB: 13658fae3551SRodney W. Grimes (void)printf("Parameter problem: pointer = 0x%02x\n", 13668fae3551SRodney W. Grimes icp->icmp_hun.ih_pptr); 13678fae3551SRodney W. Grimes #ifndef icmp_data 13688fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 13698fae3551SRodney W. Grimes #else 13708fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 13718fae3551SRodney W. Grimes #endif 13728fae3551SRodney W. Grimes break; 13738fae3551SRodney W. Grimes case ICMP_TSTAMP: 13748fae3551SRodney W. Grimes (void)printf("Timestamp\n"); 13758fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 13768fae3551SRodney W. Grimes break; 13778fae3551SRodney W. Grimes case ICMP_TSTAMPREPLY: 13788fae3551SRodney W. Grimes (void)printf("Timestamp Reply\n"); 13798fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 13808fae3551SRodney W. Grimes break; 13818fae3551SRodney W. Grimes case ICMP_IREQ: 13828fae3551SRodney W. Grimes (void)printf("Information Request\n"); 13838fae3551SRodney W. Grimes /* XXX ID + Seq */ 13848fae3551SRodney W. Grimes break; 13858fae3551SRodney W. Grimes case ICMP_IREQREPLY: 13868fae3551SRodney W. Grimes (void)printf("Information Reply\n"); 13878fae3551SRodney W. Grimes /* XXX ID + Seq */ 13888fae3551SRodney W. Grimes break; 13898fae3551SRodney W. Grimes case ICMP_MASKREQ: 13908fae3551SRodney W. Grimes (void)printf("Address Mask Request\n"); 13918fae3551SRodney W. Grimes break; 13928fae3551SRodney W. Grimes case ICMP_MASKREPLY: 13938fae3551SRodney W. Grimes (void)printf("Address Mask Reply\n"); 13948fae3551SRodney W. Grimes break; 1395ef9e6dc7SBill Fenner case ICMP_ROUTERADVERT: 1396ef9e6dc7SBill Fenner (void)printf("Router Advertisement\n"); 1397ef9e6dc7SBill Fenner break; 1398ef9e6dc7SBill Fenner case ICMP_ROUTERSOLICIT: 1399ef9e6dc7SBill Fenner (void)printf("Router Solicitation\n"); 1400ef9e6dc7SBill Fenner break; 14018fae3551SRodney W. Grimes default: 14028fae3551SRodney W. Grimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 14038fae3551SRodney W. Grimes } 14048fae3551SRodney W. Grimes } 14058fae3551SRodney W. Grimes 14068fae3551SRodney W. Grimes /* 14078fae3551SRodney W. Grimes * pr_iph -- 14088fae3551SRodney W. Grimes * Print an IP header with options. 14098fae3551SRodney W. Grimes */ 141043470e3bSGarrett Wollman static void 14118fae3551SRodney W. Grimes pr_iph(ip) 14128fae3551SRodney W. Grimes struct ip *ip; 14138fae3551SRodney W. Grimes { 14148fae3551SRodney W. Grimes u_char *cp; 1415efc8588dSDavid E. O'Brien int hlen; 14168fae3551SRodney W. Grimes 14178fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 14188fae3551SRodney W. Grimes cp = (u_char *)ip + 20; /* point to options */ 14198fae3551SRodney W. Grimes 1420ef9e6dc7SBill Fenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 14218fae3551SRodney W. Grimes (void)printf(" %1x %1x %02x %04x %04x", 1422ef9e6dc7SBill Fenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1423ef9e6dc7SBill Fenner ntohs(ip->ip_id)); 1424d32ff037SJohn Birrell (void)printf(" %1lx %04lx", 1425d32ff037SJohn Birrell (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13, 1426d32ff037SJohn Birrell (u_long) ntohl(ip->ip_off) & 0x1fff); 1427ef9e6dc7SBill Fenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1428ef9e6dc7SBill Fenner ntohs(ip->ip_sum)); 14298fae3551SRodney W. Grimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr)); 14308fae3551SRodney W. Grimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr)); 1431ef9e6dc7SBill Fenner /* dump any option bytes */ 14328fae3551SRodney W. Grimes while (hlen-- > 20) { 14338fae3551SRodney W. Grimes (void)printf("%02x", *cp++); 14348fae3551SRodney W. Grimes } 14358fae3551SRodney W. Grimes (void)putchar('\n'); 14368fae3551SRodney W. Grimes } 14378fae3551SRodney W. Grimes 14388fae3551SRodney W. Grimes /* 14398fae3551SRodney W. Grimes * pr_addr -- 14408fae3551SRodney W. Grimes * Return an ascii host address as a dotted quad and optionally with 14418fae3551SRodney W. Grimes * a hostname. 14428fae3551SRodney W. Grimes */ 144343470e3bSGarrett Wollman static char * 144443470e3bSGarrett Wollman pr_addr(ina) 144543470e3bSGarrett Wollman struct in_addr ina; 14468fae3551SRodney W. Grimes { 14478fae3551SRodney W. Grimes struct hostent *hp; 1448f78ac61bSWarner Losh static char buf[16 + 3 + MAXHOSTNAMELEN]; 14498fae3551SRodney W. Grimes 14508fae3551SRodney W. Grimes if ((options & F_NUMERIC) || 145143470e3bSGarrett Wollman !(hp = gethostbyaddr((char *)&ina, 4, AF_INET))) 145243470e3bSGarrett Wollman return inet_ntoa(ina); 14538fae3551SRodney W. Grimes else 1454efa38539SPeter Wemm (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 145543470e3bSGarrett Wollman inet_ntoa(ina)); 14568fae3551SRodney W. Grimes return(buf); 14578fae3551SRodney W. Grimes } 14588fae3551SRodney W. Grimes 14598fae3551SRodney W. Grimes /* 14608fae3551SRodney W. Grimes * pr_retip -- 14618fae3551SRodney W. Grimes * Dump some info on a returned (via ICMP) IP packet. 14628fae3551SRodney W. Grimes */ 146343470e3bSGarrett Wollman static void 14648fae3551SRodney W. Grimes pr_retip(ip) 14658fae3551SRodney W. Grimes struct ip *ip; 14668fae3551SRodney W. Grimes { 14678fae3551SRodney W. Grimes u_char *cp; 1468efc8588dSDavid E. O'Brien int hlen; 14698fae3551SRodney W. Grimes 14708fae3551SRodney W. Grimes pr_iph(ip); 14718fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 14728fae3551SRodney W. Grimes cp = (u_char *)ip + hlen; 14738fae3551SRodney W. Grimes 14748fae3551SRodney W. Grimes if (ip->ip_p == 6) 14758fae3551SRodney W. Grimes (void)printf("TCP: from port %u, to port %u (decimal)\n", 14768fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 14778fae3551SRodney W. Grimes else if (ip->ip_p == 17) 14788fae3551SRodney W. Grimes (void)printf("UDP: from port %u, to port %u (decimal)\n", 14798fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 14808fae3551SRodney W. Grimes } 14818fae3551SRodney W. Grimes 148243470e3bSGarrett Wollman static void 14838fae3551SRodney W. Grimes fill(bp, patp) 14848fae3551SRodney W. Grimes char *bp, *patp; 14858fae3551SRodney W. Grimes { 14868fae3551SRodney W. Grimes char *cp; 1487efc8588dSDavid E. O'Brien int pat[16]; 14884fba6582SMaxim Konovalov u_int ii, jj, kk; 14898fae3551SRodney W. Grimes 149043470e3bSGarrett Wollman for (cp = patp; *cp; cp++) { 149143470e3bSGarrett Wollman if (!isxdigit(*cp)) 149243470e3bSGarrett Wollman errx(EX_USAGE, 149343470e3bSGarrett Wollman "patterns must be specified as hex digits"); 149443470e3bSGarrett Wollman 14958fae3551SRodney W. Grimes } 14968fae3551SRodney W. Grimes ii = sscanf(patp, 14978fae3551SRodney W. Grimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 14988fae3551SRodney W. Grimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 14998fae3551SRodney W. Grimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 15008fae3551SRodney W. Grimes &pat[13], &pat[14], &pat[15]); 15018fae3551SRodney W. Grimes 15028fae3551SRodney W. Grimes if (ii > 0) 1503fb7d32c7SMaxim Konovalov for (kk = 0; kk <= maxpayload - (PHDR_LEN + ii); kk += ii) 15048fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 15058fae3551SRodney W. Grimes bp[jj + kk] = pat[jj]; 15068fae3551SRodney W. Grimes if (!(options & F_QUIET)) { 15078fae3551SRodney W. Grimes (void)printf("PATTERN: 0x"); 15088fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 15098fae3551SRodney W. Grimes (void)printf("%02x", bp[jj] & 0xFF); 15108fae3551SRodney W. Grimes (void)printf("\n"); 15118fae3551SRodney W. Grimes } 15128fae3551SRodney W. Grimes } 15138fae3551SRodney W. Grimes 151443470e3bSGarrett Wollman static void 1515e345a80dSPhilippe Charnier usage() 15168fae3551SRodney W. Grimes { 15171ad0b1beSMaxim Konovalov (void)fprintf(stderr, "%s\n%s\n%s\n", 15180b2f8b3fSMaxim Konovalov "usage: ping [-ADQRadfnoqrv] [-c count] [-i wait] [-l preload] [-m ttl]", 1519211bfbd2SRuslan Ermilov " [-p pattern] " 15209a4365d0SYoshinobu Inoue #ifdef IPSEC 15219a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 15229a4365d0SYoshinobu Inoue "[-P policy] " 15239a4365d0SYoshinobu Inoue #endif 15249a4365d0SYoshinobu Inoue #endif 15257237fd94SBill Fumerola "[-s packetsize] [-S src_addr] [-t timeout]", 15260b2f8b3fSMaxim Konovalov " [-z tos ] [host | [-L] [-I iface] [-T ttl] mcast-group]"); 152743470e3bSGarrett Wollman exit(EX_USAGE); 15288fae3551SRodney W. Grimes } 1529