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> */ 7043470e3bSGarrett Wollman 7143470e3bSGarrett Wollman #include <ctype.h> 7243470e3bSGarrett Wollman #include <err.h> 7343470e3bSGarrett Wollman #include <errno.h> 743109a910SGarrett Wollman #include <math.h> 7543470e3bSGarrett Wollman #include <netdb.h> 7643470e3bSGarrett Wollman #include <signal.h> 7743470e3bSGarrett Wollman #include <stdio.h> 7843470e3bSGarrett Wollman #include <stdlib.h> 7943470e3bSGarrett Wollman #include <string.h> 8043470e3bSGarrett Wollman #include <sysexits.h> 8143470e3bSGarrett Wollman #include <termios.h> 8243470e3bSGarrett Wollman #include <unistd.h> 8343470e3bSGarrett Wollman 848fae3551SRodney W. Grimes #include <sys/socket.h> 858fae3551SRodney W. Grimes #include <sys/time.h> 86039d6aa4SBill Fenner #include <sys/uio.h> 878fae3551SRodney W. Grimes 888fae3551SRodney W. Grimes #include <netinet/in.h> 8943470e3bSGarrett Wollman #include <netinet/in_systm.h> 908fae3551SRodney W. Grimes #include <netinet/ip.h> 918fae3551SRodney W. Grimes #include <netinet/ip_icmp.h> 928fae3551SRodney W. Grimes #include <netinet/ip_var.h> 9343470e3bSGarrett Wollman #include <arpa/inet.h> 948fae3551SRodney W. Grimes 959a4365d0SYoshinobu Inoue #ifdef IPSEC 969a4365d0SYoshinobu Inoue #include <netinet6/ipsec.h> 979a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 989a4365d0SYoshinobu Inoue 99d32ff037SJohn Birrell #define PHDR_LEN sizeof(struct timeval) 100d32ff037SJohn Birrell #define DEFDATALEN (64 - PHDR_LEN) /* default data length */ 1018f975bb3SBruce Evans #define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */ 1028f975bb3SBruce Evans /* runs out of buffer space */ 1038fae3551SRodney W. Grimes #define MAXIPLEN 60 1048fae3551SRodney W. Grimes #define MAXICMPLEN 76 1058fae3551SRodney W. Grimes #define MAXPACKET (65536 - 60 - 8)/* max packet size */ 1068fae3551SRodney W. Grimes #define MAXWAIT 10 /* max seconds to wait for response */ 107bf113f1bSBill Fumerola #define MAXALARM (60 * 60) /* max seconds for alarm timeout */ 1088fae3551SRodney W. Grimes #define NROUTES 9 /* number of record route slots */ 1098fae3551SRodney W. Grimes 1108fae3551SRodney W. Grimes #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 1118fae3551SRodney W. Grimes #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 1128fae3551SRodney W. Grimes #define SET(bit) (A(bit) |= B(bit)) 1138fae3551SRodney W. Grimes #define CLR(bit) (A(bit) &= (~B(bit))) 1148fae3551SRodney W. Grimes #define TST(bit) (A(bit) & B(bit)) 1158fae3551SRodney W. Grimes 1168fae3551SRodney W. Grimes /* various options */ 1178fae3551SRodney W. Grimes int options; 11885456935SBill Fenner #define F_FLOOD 0x0001 11985456935SBill Fenner #define F_INTERVAL 0x0002 12085456935SBill Fenner #define F_NUMERIC 0x0004 12185456935SBill Fenner #define F_PINGFILLED 0x0008 12285456935SBill Fenner #define F_QUIET 0x0010 12385456935SBill Fenner #define F_RROUTE 0x0020 12485456935SBill Fenner #define F_SO_DEBUG 0x0040 12585456935SBill Fenner #define F_SO_DONTROUTE 0x0080 12685456935SBill Fenner #define F_VERBOSE 0x0100 12785456935SBill Fenner #define F_QUIET2 0x0200 12885456935SBill Fenner #define F_NOLOOP 0x0400 12985456935SBill Fenner #define F_MTTL 0x0800 13085456935SBill Fenner #define F_MIF 0x1000 131772dfa72SDaniel O'Callaghan #define F_AUDIBLE 0x2000 1329a4365d0SYoshinobu Inoue #ifdef IPSEC 1339a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 1349a4365d0SYoshinobu Inoue #define F_POLICY 0x4000 1359a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 1369a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 1378fae3551SRodney W. Grimes 1388fae3551SRodney W. Grimes /* 1398fae3551SRodney W. Grimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 1408fae3551SRodney W. Grimes * number of received sequence numbers we can keep track of. Change 128 1418fae3551SRodney W. Grimes * to 8192 for complete accuracy... 1428fae3551SRodney W. Grimes */ 1438fae3551SRodney W. Grimes #define MAX_DUP_CHK (8 * 128) 1448fae3551SRodney W. Grimes int mx_dup_ck = MAX_DUP_CHK; 1458fae3551SRodney W. Grimes char rcvd_tbl[MAX_DUP_CHK / 8]; 1468fae3551SRodney W. Grimes 1478fae3551SRodney W. Grimes struct sockaddr whereto; /* who to ping */ 1488fae3551SRodney W. Grimes int datalen = DEFDATALEN; 1498fae3551SRodney W. Grimes int s; /* socket file descriptor */ 1508fae3551SRodney W. Grimes u_char outpack[MAXPACKET]; 1518fae3551SRodney W. Grimes char BSPACE = '\b'; /* characters written for flood */ 1528fae3551SRodney W. Grimes char DOT = '.'; 1538fae3551SRodney W. Grimes char *hostname; 15499490edeSWarner Losh char *shostname; 1558fae3551SRodney W. Grimes int ident; /* process id to identify our packets */ 156ee2bf734SWarner Losh int uid; /* cached uid for micro-optimization */ 1578fae3551SRodney W. Grimes 1588fae3551SRodney W. Grimes /* counters */ 1598fae3551SRodney W. Grimes long npackets; /* max packets to transmit */ 1608fae3551SRodney W. Grimes long nreceived; /* # of packets we got back */ 1618fae3551SRodney W. Grimes long nrepeats; /* number of duplicates */ 1628fae3551SRodney W. Grimes long ntransmitted; /* sequence # for outbound packets = #sent */ 163526f06b2SMatthew Dillon int interval = 1000; /* interval between packets, ms */ 1648fae3551SRodney W. Grimes 1658fae3551SRodney W. Grimes /* timing */ 1668fae3551SRodney W. Grimes int timing; /* flag to do timing */ 1678fae3551SRodney W. Grimes double tmin = 999999999.0; /* minimum round trip time */ 1688fae3551SRodney W. Grimes double tmax = 0.0; /* maximum round trip time */ 1698fae3551SRodney W. Grimes double tsum = 0.0; /* sum of all times, for doing average */ 1703109a910SGarrett Wollman double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 1718fae3551SRodney W. Grimes 1728f975bb3SBruce Evans volatile sig_atomic_t finish_up; /* nonzero if we've been told to finish up */ 173badd8138SSean Eric Fagan int reset_kerninfo; 1748f975bb3SBruce Evans volatile sig_atomic_t siginfo_p; 175badd8138SSean Eric Fagan 17643470e3bSGarrett Wollman static void fill(char *, char *); 17743470e3bSGarrett Wollman static u_short in_cksum(u_short *, int); 17843470e3bSGarrett Wollman static void check_status(void); 1798f975bb3SBruce Evans static void finish(void) __dead2; 18043470e3bSGarrett Wollman static void pinger(void); 18143470e3bSGarrett Wollman static char *pr_addr(struct in_addr); 18243470e3bSGarrett Wollman static void pr_icmph(struct icmp *); 18343470e3bSGarrett Wollman static void pr_iph(struct ip *); 184039d6aa4SBill Fenner static void pr_pack(char *, int, struct sockaddr_in *, struct timeval *); 18543470e3bSGarrett Wollman static void pr_retip(struct ip *); 18643470e3bSGarrett Wollman static void status(int); 1878f975bb3SBruce Evans static void stopit(int); 18843470e3bSGarrett Wollman static void tvsub(struct timeval *, struct timeval *); 189e345a80dSPhilippe Charnier static void usage(void) __dead2; 1908fae3551SRodney W. Grimes 19143470e3bSGarrett Wollman int 1928fae3551SRodney W. Grimes main(argc, argv) 1938fae3551SRodney W. Grimes int argc; 19443470e3bSGarrett Wollman char *const *argv; 1958fae3551SRodney W. Grimes { 196039d6aa4SBill Fenner struct timeval last, intvl; 1978fae3551SRodney W. Grimes struct hostent *hp; 19899490edeSWarner Losh struct sockaddr_in *to, sin; 199badd8138SSean Eric Fagan struct termios ts; 2008fae3551SRodney W. Grimes register int i; 201039d6aa4SBill Fenner int ch, hold, packlen, preload, sockerrno, almost_done = 0; 20285456935SBill Fenner struct in_addr ifaddr; 20385456935SBill Fenner unsigned char ttl, loop; 2048fae3551SRodney W. Grimes u_char *datap, *packet; 20599490edeSWarner Losh char *source = NULL, *target, hnamebuf[MAXHOSTNAMELEN]; 20699490edeSWarner Losh char snamebuf[MAXHOSTNAMELEN]; 20743470e3bSGarrett Wollman char *ep; 20843470e3bSGarrett Wollman u_long ultmp; 2098fae3551SRodney W. Grimes #ifdef IP_OPTIONS 2108fae3551SRodney W. Grimes char rspace[3 + 4 * NROUTES + 1]; /* record route space */ 2118fae3551SRodney W. Grimes #endif 21237e5b2c6SSean Eric Fagan struct sigaction si_sa; 213039d6aa4SBill Fenner struct iovec iov; 214039d6aa4SBill Fenner struct msghdr msg; 215039d6aa4SBill Fenner struct sockaddr_in from; 2167d0d8dc3SYoshinobu Inoue char ctrl[CMSG_SPACE(sizeof(struct timeval))]; 2179a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 2189a4365d0SYoshinobu Inoue char *policy_in = NULL; 2199a4365d0SYoshinobu Inoue char *policy_out = NULL; 2209a4365d0SYoshinobu Inoue #endif 221bf113f1bSBill Fumerola unsigned long alarmtimeout; 2228fae3551SRodney W. Grimes 223f1284d7aSBill Fenner /* 224f1284d7aSBill Fenner * Do the stuff that we need root priv's for *first*, and 225f1284d7aSBill Fenner * then drop our setuid bit. Save error reporting for 226f1284d7aSBill Fenner * after arg parsing. 227f1284d7aSBill Fenner */ 22843470e3bSGarrett Wollman s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 229f1284d7aSBill Fenner sockerrno = errno; 230f1284d7aSBill Fenner 231f1284d7aSBill Fenner setuid(getuid()); 232ee2bf734SWarner Losh uid = getuid(); 233f1284d7aSBill Fenner 234bf113f1bSBill Fumerola alarmtimeout = preload = 0; 235badd8138SSean Eric Fagan 236d32ff037SJohn Birrell datap = &outpack[8 + PHDR_LEN]; 2379a4365d0SYoshinobu Inoue #ifndef IPSEC 238e8707380SYoshinobu Inoue while ((ch = getopt(argc, argv, "I:LQRS:T:c:adfi:l:np:qrs:t:v")) != -1) 2399a4365d0SYoshinobu Inoue #else 2409a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 241e8707380SYoshinobu Inoue while ((ch = getopt(argc, argv, "I:LQRS:T:c:adfi:l:np:qrs:t:vP:")) != -1) 2429a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 2439a4365d0SYoshinobu Inoue #endif 2449a4365d0SYoshinobu Inoue { 2458fae3551SRodney W. Grimes switch(ch) { 246772dfa72SDaniel O'Callaghan case 'a': 247772dfa72SDaniel O'Callaghan options |= F_AUDIBLE; 248772dfa72SDaniel O'Callaghan break; 2498fae3551SRodney W. Grimes case 'c': 25043470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 25143470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp) 25243470e3bSGarrett Wollman errx(EX_USAGE, 25343470e3bSGarrett Wollman "invalid count of packets to transmit: `%s'", 25443470e3bSGarrett Wollman optarg); 25543470e3bSGarrett Wollman npackets = ultmp; 2568fae3551SRodney W. Grimes break; 2578fae3551SRodney W. Grimes case 'd': 2588fae3551SRodney W. Grimes options |= F_SO_DEBUG; 2598fae3551SRodney W. Grimes break; 2608fae3551SRodney W. Grimes case 'f': 261526f06b2SMatthew Dillon if (uid) { 26243470e3bSGarrett Wollman errno = EPERM; 26343470e3bSGarrett Wollman err(EX_NOPERM, "-f flag"); 2648fae3551SRodney W. Grimes } 2658fae3551SRodney W. Grimes options |= F_FLOOD; 2668fae3551SRodney W. Grimes setbuf(stdout, (char *)NULL); 2678fae3551SRodney W. Grimes break; 2688fae3551SRodney W. Grimes case 'i': /* wait between sending packets */ 269526f06b2SMatthew Dillon { 270526f06b2SMatthew Dillon double t = strtod(optarg, &ep) * 1000.0; 271526f06b2SMatthew Dillon 272526f06b2SMatthew Dillon if (*ep || ep == optarg || t > (double)INT_MAX) { 273526f06b2SMatthew Dillon errx( 274526f06b2SMatthew Dillon EX_USAGE, 275526f06b2SMatthew Dillon "invalid timing interval: `%s'", 276526f06b2SMatthew Dillon optarg 277526f06b2SMatthew Dillon ); 278526f06b2SMatthew Dillon } 2798fae3551SRodney W. Grimes options |= F_INTERVAL; 280526f06b2SMatthew Dillon interval = (int)t; 281526f06b2SMatthew Dillon if (uid && interval < 1000) { 282526f06b2SMatthew Dillon errno = EPERM; 283526f06b2SMatthew Dillon err(EX_NOPERM, "-i interval too short"); 284526f06b2SMatthew Dillon } 285526f06b2SMatthew Dillon } 2868fae3551SRodney W. Grimes break; 28785456935SBill Fenner case 'I': /* multicast interface */ 28843470e3bSGarrett Wollman if (inet_aton(optarg, &ifaddr) == 0) 28943470e3bSGarrett Wollman errx(EX_USAGE, 29043470e3bSGarrett Wollman "invalid multicast interface: `%s'", 29143470e3bSGarrett Wollman optarg); 29285456935SBill Fenner options |= F_MIF; 29385456935SBill Fenner break; 2948fae3551SRodney W. Grimes case 'l': 29543470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 29643470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > INT_MAX) 29743470e3bSGarrett Wollman errx(EX_USAGE, 29843470e3bSGarrett Wollman "invalid preload value: `%s'", optarg); 2995e2cc0f4SStephen McKay if (uid) { 300f78ac61bSWarner Losh errno = EPERM; 301f78ac61bSWarner Losh err(EX_NOPERM, "-l flag"); 302f78ac61bSWarner Losh } 30343470e3bSGarrett Wollman preload = ultmp; 3048fae3551SRodney W. Grimes break; 30585456935SBill Fenner case 'L': 30685456935SBill Fenner options |= F_NOLOOP; 30785456935SBill Fenner loop = 0; 30885456935SBill Fenner break; 3098fae3551SRodney W. Grimes case 'n': 3108fae3551SRodney W. Grimes options |= F_NUMERIC; 3118fae3551SRodney W. Grimes break; 3128fae3551SRodney W. Grimes case 'p': /* fill buffer with user pattern */ 3138fae3551SRodney W. Grimes options |= F_PINGFILLED; 3148fae3551SRodney W. Grimes fill((char *)datap, optarg); 3158fae3551SRodney W. Grimes break; 316ef9e6dc7SBill Fenner case 'Q': 317ef9e6dc7SBill Fenner options |= F_QUIET2; 318ef9e6dc7SBill Fenner break; 3198fae3551SRodney W. Grimes case 'q': 3208fae3551SRodney W. Grimes options |= F_QUIET; 3218fae3551SRodney W. Grimes break; 3228fae3551SRodney W. Grimes case 'R': 3238fae3551SRodney W. Grimes options |= F_RROUTE; 3248fae3551SRodney W. Grimes break; 3258fae3551SRodney W. Grimes case 'r': 3268fae3551SRodney W. Grimes options |= F_SO_DONTROUTE; 3278fae3551SRodney W. Grimes break; 3288fae3551SRodney W. Grimes case 's': /* size of packet to send */ 329526f06b2SMatthew Dillon if (uid) { 330526f06b2SMatthew Dillon errno = EPERM; 331526f06b2SMatthew Dillon err(EX_NOPERM, "-s flag"); 332526f06b2SMatthew Dillon } 33343470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 33443470e3bSGarrett Wollman if (ultmp > MAXPACKET) 33543470e3bSGarrett Wollman errx(EX_USAGE, "packet size too large: %lu", 33643470e3bSGarrett Wollman ultmp); 33743470e3bSGarrett Wollman if (*ep || ep == optarg || !ultmp) 33843470e3bSGarrett Wollman errx(EX_USAGE, "invalid packet size: `%s'", 33943470e3bSGarrett Wollman optarg); 34043470e3bSGarrett Wollman datalen = ultmp; 3418fae3551SRodney W. Grimes break; 34299490edeSWarner Losh case 'S': 34399490edeSWarner Losh source = optarg; 34499490edeSWarner Losh break; 3457237fd94SBill Fumerola case 't': 346bf113f1bSBill Fumerola alarmtimeout = strtoul(optarg, &ep, 0); 347bf113f1bSBill Fumerola if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX)) 3487237fd94SBill Fumerola errx(EX_USAGE, "invalid timeout: `%s'", 3497237fd94SBill Fumerola optarg); 350bf113f1bSBill Fumerola if (alarmtimeout > MAXALARM) 351bf113f1bSBill Fumerola errx(EX_USAGE, "invalid timeout: `%s' > %d", 352bf113f1bSBill Fumerola optarg, MAXALARM); 353bf113f1bSBill Fumerola alarm((int)alarmtimeout); 3547237fd94SBill Fumerola break; 35585456935SBill Fenner case 'T': /* multicast TTL */ 35643470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 35743470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > 255) 35843470e3bSGarrett Wollman errx(EX_USAGE, "invalid multicast TTL: `%s'", 35943470e3bSGarrett Wollman optarg); 36043470e3bSGarrett Wollman ttl = ultmp; 36185456935SBill Fenner options |= F_MTTL; 36285456935SBill Fenner break; 3638fae3551SRodney W. Grimes case 'v': 3648fae3551SRodney W. Grimes options |= F_VERBOSE; 3658fae3551SRodney W. Grimes break; 3669a4365d0SYoshinobu Inoue #ifdef IPSEC 3679a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 3689a4365d0SYoshinobu Inoue case 'P': 3699a4365d0SYoshinobu Inoue options |= F_POLICY; 3709a4365d0SYoshinobu Inoue if (!strncmp("in", optarg, 2)) 3719a4365d0SYoshinobu Inoue policy_in = strdup(optarg); 3729a4365d0SYoshinobu Inoue else if (!strncmp("out", optarg, 3)) 3739a4365d0SYoshinobu Inoue policy_out = strdup(optarg); 3749a4365d0SYoshinobu Inoue else 3759a4365d0SYoshinobu Inoue errx(1, "invalid security policy"); 3769a4365d0SYoshinobu Inoue break; 3779a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 3789a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 3798fae3551SRodney W. Grimes default: 380e345a80dSPhilippe Charnier usage(); 38143470e3bSGarrett Wollman } 38243470e3bSGarrett Wollman } 38343470e3bSGarrett Wollman 38443470e3bSGarrett Wollman if (argc - optind != 1) 385e345a80dSPhilippe Charnier usage(); 38643470e3bSGarrett Wollman target = argv[optind]; 3878fae3551SRodney W. Grimes 38899490edeSWarner Losh if (source) { 38999490edeSWarner Losh bzero((char *)&sin, sizeof(sin)); 39099490edeSWarner Losh sin.sin_family = AF_INET; 39199490edeSWarner Losh if (inet_aton(source, &sin.sin_addr) != 0) { 39299490edeSWarner Losh shostname = source; 39399490edeSWarner Losh } else { 39499490edeSWarner Losh hp = gethostbyname2(source, AF_INET); 39599490edeSWarner Losh if (!hp) 39699490edeSWarner Losh errx(EX_NOHOST, "cannot resolve %s: %s", 39799490edeSWarner Losh source, hstrerror(h_errno)); 39899490edeSWarner Losh 39999490edeSWarner Losh sin.sin_len = sizeof sin; 40099490edeSWarner Losh if (hp->h_length > sizeof(sin.sin_addr)) 40199490edeSWarner Losh errx(1,"gethostbyname2: illegal address"); 40299490edeSWarner Losh memcpy(&sin.sin_addr, hp->h_addr_list[0], 40399490edeSWarner Losh sizeof (sin.sin_addr)); 40499490edeSWarner Losh (void)strncpy(snamebuf, hp->h_name, 40599490edeSWarner Losh sizeof(snamebuf) - 1); 40699490edeSWarner Losh snamebuf[sizeof(snamebuf) - 1] = '\0'; 40799490edeSWarner Losh shostname = snamebuf; 40899490edeSWarner Losh } 40999490edeSWarner Losh if (bind(s, (struct sockaddr *)&sin, sizeof sin) == -1) 41099490edeSWarner Losh err(1, "bind"); 41199490edeSWarner Losh } 41299490edeSWarner Losh 4138fae3551SRodney W. Grimes bzero((char *)&whereto, sizeof(struct sockaddr)); 4148fae3551SRodney W. Grimes to = (struct sockaddr_in *)&whereto; 4158fae3551SRodney W. Grimes to->sin_family = AF_INET; 41643470e3bSGarrett Wollman if (inet_aton(target, &to->sin_addr) != 0) { 4178fae3551SRodney W. Grimes hostname = target; 41843470e3bSGarrett Wollman } else { 41943470e3bSGarrett Wollman hp = gethostbyname2(target, AF_INET); 42043470e3bSGarrett Wollman if (!hp) 42143470e3bSGarrett Wollman errx(EX_NOHOST, "cannot resolve %s: %s", 42243470e3bSGarrett Wollman target, hstrerror(h_errno)); 42343470e3bSGarrett Wollman 42443470e3bSGarrett Wollman to->sin_len = sizeof *to; 4251ffae4a6SWarner Losh if (hp->h_length > sizeof(to->sin_addr)) 4261ffae4a6SWarner Losh errx(1,"gethostbyname2 returned an illegal address"); 42743470e3bSGarrett Wollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 4288fae3551SRodney W. Grimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 429b10d9d5fSWarner Losh hnamebuf[sizeof(hnamebuf) - 1] = '\0'; 4308fae3551SRodney W. Grimes hostname = hnamebuf; 4318fae3551SRodney W. Grimes } 4328fae3551SRodney W. Grimes 43343470e3bSGarrett Wollman if (options & F_FLOOD && options & F_INTERVAL) 43443470e3bSGarrett Wollman errx(EX_USAGE, "-f and -i: incompatible options"); 43543470e3bSGarrett Wollman 43643470e3bSGarrett Wollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 43743470e3bSGarrett Wollman errx(EX_USAGE, 43843470e3bSGarrett Wollman "-f flag cannot be used with multicast destination"); 43943470e3bSGarrett Wollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 44043470e3bSGarrett Wollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 44143470e3bSGarrett Wollman errx(EX_USAGE, 44243470e3bSGarrett Wollman "-I, -L, -T flags cannot be used with unicast destination"); 4438fae3551SRodney W. Grimes 444d32ff037SJohn Birrell if (datalen >= PHDR_LEN) /* can we time transfer */ 4458fae3551SRodney W. Grimes timing = 1; 4468fae3551SRodney W. Grimes packlen = datalen + MAXIPLEN + MAXICMPLEN; 44743470e3bSGarrett Wollman if (!(packet = (u_char *)malloc((size_t)packlen))) 44843470e3bSGarrett Wollman err(EX_UNAVAILABLE, "malloc"); 44943470e3bSGarrett Wollman 4508fae3551SRodney W. Grimes if (!(options & F_PINGFILLED)) 451d32ff037SJohn Birrell for (i = PHDR_LEN; i < datalen; ++i) 4528fae3551SRodney W. Grimes *datap++ = i; 4538fae3551SRodney W. Grimes 4548fae3551SRodney W. Grimes ident = getpid() & 0xFFFF; 4558fae3551SRodney W. Grimes 456f1284d7aSBill Fenner if (s < 0) { 457f1284d7aSBill Fenner errno = sockerrno; 45843470e3bSGarrett Wollman err(EX_OSERR, "socket"); 4598fae3551SRodney W. Grimes } 4608fae3551SRodney W. Grimes hold = 1; 4618fae3551SRodney W. Grimes if (options & F_SO_DEBUG) 4628fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold, 4638fae3551SRodney W. Grimes sizeof(hold)); 4648fae3551SRodney W. Grimes if (options & F_SO_DONTROUTE) 4658fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 4668fae3551SRodney W. Grimes sizeof(hold)); 4679a4365d0SYoshinobu Inoue #ifdef IPSEC 4689a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 4699a4365d0SYoshinobu Inoue if (options & F_POLICY) { 4709a4365d0SYoshinobu Inoue char *buf; 4719a4365d0SYoshinobu Inoue if (policy_in != NULL) { 4729a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_in, strlen(policy_in)); 4739a4365d0SYoshinobu Inoue if (buf == NULL) 474ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 4759a4365d0SYoshinobu Inoue if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY, 4769a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 4779a4365d0SYoshinobu Inoue err(EX_CONFIG, "ipsec policy cannot be configured"); 4789a4365d0SYoshinobu Inoue free(buf); 4799a4365d0SYoshinobu Inoue } 4809a4365d0SYoshinobu Inoue 4819a4365d0SYoshinobu Inoue if (policy_out != NULL) { 4829a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_out, strlen(policy_out)); 4839a4365d0SYoshinobu Inoue if (buf == NULL) 484ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 4859a4365d0SYoshinobu Inoue if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY, 4869a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 4879a4365d0SYoshinobu Inoue err(EX_CONFIG, "ipsec policy cannot be configured"); 4889a4365d0SYoshinobu Inoue free(buf); 4899a4365d0SYoshinobu Inoue } 4909a4365d0SYoshinobu Inoue } 4919a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 4929a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 4938fae3551SRodney W. Grimes 4948fae3551SRodney W. Grimes /* record route option */ 4958fae3551SRodney W. Grimes if (options & F_RROUTE) { 4968fae3551SRodney W. Grimes #ifdef IP_OPTIONS 497039d6aa4SBill Fenner bzero(rspace, sizeof(rspace)); 4988fae3551SRodney W. Grimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 4998fae3551SRodney W. Grimes rspace[IPOPT_OLEN] = sizeof(rspace) - 1; 5008fae3551SRodney W. Grimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 501039d6aa4SBill Fenner rspace[sizeof(rspace) - 1] = IPOPT_EOL; 5028fae3551SRodney W. Grimes if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace, 50343470e3bSGarrett Wollman sizeof(rspace)) < 0) 50443470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 5058fae3551SRodney W. Grimes #else 50643470e3bSGarrett Wollman errx(EX_UNAVAILABLE, 50743470e3bSGarrett Wollman "record route not available in this implementation"); 5088fae3551SRodney W. Grimes #endif /* IP_OPTIONS */ 5098fae3551SRodney W. Grimes } 5108fae3551SRodney W. Grimes 51185456935SBill Fenner if (options & F_NOLOOP) { 51285456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 51385456935SBill Fenner sizeof(loop)) < 0) { 51443470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 51585456935SBill Fenner } 51685456935SBill Fenner } 51785456935SBill Fenner if (options & F_MTTL) { 51885456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, 51985456935SBill Fenner sizeof(ttl)) < 0) { 52043470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 52185456935SBill Fenner } 52285456935SBill Fenner } 52385456935SBill Fenner if (options & F_MIF) { 52485456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 52585456935SBill Fenner sizeof(ifaddr)) < 0) { 52643470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 52785456935SBill Fenner } 52885456935SBill Fenner } 529039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 530039d6aa4SBill Fenner { int on = 1; 531039d6aa4SBill Fenner if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) < 0) 532039d6aa4SBill Fenner err(EX_OSERR, "setsockopt SO_TIMESTAMP"); 533039d6aa4SBill Fenner } 534039d6aa4SBill Fenner #endif 53585456935SBill Fenner 5368fae3551SRodney W. Grimes /* 5378fae3551SRodney W. Grimes * When pinging the broadcast address, you can get a lot of answers. 5388fae3551SRodney W. Grimes * Doing something so evil is useful if you are trying to stress the 5398fae3551SRodney W. Grimes * ethernet, or just want to fill the arp cache to get some stuff for 54043470e3bSGarrett Wollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 54143470e3bSGarrett Wollman * or multicast pings if they wish. 5428fae3551SRodney W. Grimes */ 5438fae3551SRodney W. Grimes hold = 48 * 1024; 5448fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 5458fae3551SRodney W. Grimes sizeof(hold)); 5468fae3551SRodney W. Grimes 54799490edeSWarner Losh if (to->sin_family == AF_INET) { 54899490edeSWarner Losh (void)printf("PING %s (%s)", hostname, 54999490edeSWarner Losh inet_ntoa(to->sin_addr)); 55099490edeSWarner Losh if (source) 55199490edeSWarner Losh (void)printf(" from %s", shostname); 55299490edeSWarner Losh (void)printf(": %d data bytes\n", datalen); 55399490edeSWarner Losh } else 5548fae3551SRodney W. Grimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 5558fae3551SRodney W. Grimes 5567d81b35cSBruce Evans /* 557a2a00888SSean Eric Fagan * Use sigaction() instead of signal() to get unambiguous semantics, 558a2a00888SSean Eric Fagan * in particular with SA_RESTART not set. 5597d81b35cSBruce Evans */ 560a2a00888SSean Eric Fagan 561f6bd468bSPaul Traina sigemptyset(&si_sa.sa_mask); 56237e5b2c6SSean Eric Fagan si_sa.sa_flags = 0; 563a2a00888SSean Eric Fagan 564a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 565a2a00888SSean Eric Fagan if (sigaction(SIGINT, &si_sa, 0) == -1) { 566a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGINT"); 567a2a00888SSean Eric Fagan } 568a2a00888SSean Eric Fagan 569a2a00888SSean Eric Fagan si_sa.sa_handler = status; 57037e5b2c6SSean Eric Fagan if (sigaction(SIGINFO, &si_sa, 0) == -1) { 571624ff938SWarner Losh err(EX_OSERR, "sigaction"); 57237e5b2c6SSean Eric Fagan } 573bf113f1bSBill Fumerola 574bf113f1bSBill Fumerola if (alarmtimeout > 0) { 5757237fd94SBill Fumerola si_sa.sa_handler = stopit; 5767237fd94SBill Fumerola if (sigaction(SIGALRM, &si_sa, 0) == -1) 5777237fd94SBill Fumerola err(EX_OSERR, "sigaction SIGALRM"); 578bf113f1bSBill Fumerola } 57937e5b2c6SSean Eric Fagan 580039d6aa4SBill Fenner bzero(&msg, sizeof(msg)); 581039d6aa4SBill Fenner msg.msg_name = (caddr_t)&from; 582039d6aa4SBill Fenner msg.msg_iov = &iov; 583039d6aa4SBill Fenner msg.msg_iovlen = 1; 584039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 585039d6aa4SBill Fenner msg.msg_control = (caddr_t)ctrl; 586039d6aa4SBill Fenner #endif 587039d6aa4SBill Fenner iov.iov_base = packet; 588039d6aa4SBill Fenner iov.iov_len = packlen; 589039d6aa4SBill Fenner 5904055611aSSean Eric Fagan if (tcgetattr(STDOUT_FILENO, &ts) != -1) { 5914055611aSSean Eric Fagan reset_kerninfo = !(ts.c_lflag & NOKERNINFO); 5924055611aSSean Eric Fagan ts.c_lflag |= NOKERNINFO; 5934055611aSSean Eric Fagan tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 5944055611aSSean Eric Fagan } 5954055611aSSean Eric Fagan 5968fae3551SRodney W. Grimes while (preload--) /* fire off them quickies */ 5978fae3551SRodney W. Grimes pinger(); 5988fae3551SRodney W. Grimes 599039d6aa4SBill Fenner if (options & F_FLOOD) { 600039d6aa4SBill Fenner intvl.tv_sec = 0; 601039d6aa4SBill Fenner intvl.tv_usec = 10000; 602039d6aa4SBill Fenner } else { 603526f06b2SMatthew Dillon intvl.tv_sec = interval / 1000; 604526f06b2SMatthew Dillon intvl.tv_usec = interval % 1000 * 1000; 605039d6aa4SBill Fenner } 606039d6aa4SBill Fenner 607039d6aa4SBill Fenner pinger(); /* send the first ping */ 608039d6aa4SBill Fenner (void)gettimeofday(&last, NULL); 6098fae3551SRodney W. Grimes 6108f975bb3SBruce Evans while (!finish_up) { 6118fae3551SRodney W. Grimes register int cc; 612039d6aa4SBill Fenner int n; 613039d6aa4SBill Fenner struct timeval timeout, now; 614039d6aa4SBill Fenner fd_set rfds; 6158fae3551SRodney W. Grimes 6167d81b35cSBruce Evans check_status(); 617039d6aa4SBill Fenner FD_ZERO(&rfds); 618039d6aa4SBill Fenner FD_SET(s, &rfds); 619039d6aa4SBill Fenner (void)gettimeofday(&now, NULL); 620039d6aa4SBill Fenner timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec; 621039d6aa4SBill Fenner timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec; 622039d6aa4SBill Fenner while (timeout.tv_usec < 0) { 623039d6aa4SBill Fenner timeout.tv_usec += 1000000; 624039d6aa4SBill Fenner timeout.tv_sec--; 6258fae3551SRodney W. Grimes } 626e345a80dSPhilippe Charnier while (timeout.tv_usec >= 1000000) { 627039d6aa4SBill Fenner timeout.tv_usec -= 1000000; 628039d6aa4SBill Fenner timeout.tv_sec++; 629039d6aa4SBill Fenner } 630039d6aa4SBill Fenner if (timeout.tv_sec < 0) 631039d6aa4SBill Fenner timeout.tv_sec = timeout.tv_usec = 0; 632039d6aa4SBill Fenner n = select(s + 1, &rfds, NULL, NULL, &timeout); 6335e2cc0f4SStephen McKay if (n < 0) 6345e2cc0f4SStephen McKay continue; /* Must be EINTR. */ 635039d6aa4SBill Fenner if (n == 1) { 636039d6aa4SBill Fenner struct timeval *t = 0; 637039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 638039d6aa4SBill Fenner struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl; 639039d6aa4SBill Fenner 640039d6aa4SBill Fenner msg.msg_controllen = sizeof(ctrl); 641039d6aa4SBill Fenner #endif 642039d6aa4SBill Fenner msg.msg_namelen = sizeof(from); 643039d6aa4SBill Fenner if ((cc = recvmsg(s, &msg, 0)) < 0) { 6448fae3551SRodney W. Grimes if (errno == EINTR) 6458fae3551SRodney W. Grimes continue; 646e345a80dSPhilippe Charnier warn("recvmsg"); 6478fae3551SRodney W. Grimes continue; 6488fae3551SRodney W. Grimes } 649039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 650039d6aa4SBill Fenner if (cmsg->cmsg_level == SOL_SOCKET && 651039d6aa4SBill Fenner cmsg->cmsg_type == SCM_TIMESTAMP && 6526ecbec77SYoshinobu Inoue cmsg->cmsg_len == CMSG_LEN(sizeof *t)) { 653fa05a94cSJohn Birrell /* Copy to avoid alignment problems: */ 654fa05a94cSJohn Birrell memcpy(&now,CMSG_DATA(cmsg),sizeof(now)); 655fa05a94cSJohn Birrell t = &now; 656fa05a94cSJohn Birrell } 657039d6aa4SBill Fenner #endif 658039d6aa4SBill Fenner if (t == 0) { 659039d6aa4SBill Fenner (void)gettimeofday(&now, NULL); 660039d6aa4SBill Fenner t = &now; 661039d6aa4SBill Fenner } 662039d6aa4SBill Fenner pr_pack((char *)packet, cc, &from, t); 6638fae3551SRodney W. Grimes if (npackets && nreceived >= npackets) 6648fae3551SRodney W. Grimes break; 6658fae3551SRodney W. Grimes } 6665e2cc0f4SStephen McKay if (n == 0 || options & F_FLOOD) { 667039d6aa4SBill Fenner if (!npackets || ntransmitted < npackets) 668039d6aa4SBill Fenner pinger(); 669039d6aa4SBill Fenner else { 670039d6aa4SBill Fenner if (almost_done) 671039d6aa4SBill Fenner break; 672039d6aa4SBill Fenner almost_done = 1; 6735e2cc0f4SStephen McKay intvl.tv_usec = 0; 674039d6aa4SBill Fenner if (nreceived) { 675039d6aa4SBill Fenner intvl.tv_sec = 2 * tmax / 1000; 676039d6aa4SBill Fenner if (!intvl.tv_sec) 677039d6aa4SBill Fenner intvl.tv_sec = 1; 678039d6aa4SBill Fenner } else 679039d6aa4SBill Fenner intvl.tv_sec = MAXWAIT; 680039d6aa4SBill Fenner } 681039d6aa4SBill Fenner (void)gettimeofday(&last, NULL); 682039d6aa4SBill Fenner } 683039d6aa4SBill Fenner } 6848f975bb3SBruce Evans finish(); 6858fae3551SRodney W. Grimes /* NOTREACHED */ 686f78ac61bSWarner Losh exit(0); /* Make the compiler happy */ 6878fae3551SRodney W. Grimes } 6888fae3551SRodney W. Grimes 6898fae3551SRodney W. Grimes /* 6908f975bb3SBruce Evans * stopit -- 6918f975bb3SBruce Evans * Set the global bit that causes the main loop to quit. 6928f975bb3SBruce Evans * Do NOT call finish() from here, since finish() does far too much 6938f975bb3SBruce Evans * to be called from a signal handler. 694515dd2faSJulian Elischer */ 695515dd2faSJulian Elischer void 6968f975bb3SBruce Evans stopit(sig) 6978f975bb3SBruce Evans int sig; 698515dd2faSJulian Elischer { 699515dd2faSJulian Elischer finish_up = 1; 700515dd2faSJulian Elischer } 701515dd2faSJulian Elischer 702515dd2faSJulian Elischer /* 7038fae3551SRodney W. Grimes * pinger -- 7048fae3551SRodney W. Grimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 7058fae3551SRodney W. Grimes * will be added on by the kernel. The ID field is our UNIX process ID, 7068fae3551SRodney W. Grimes * and the sequence number is an ascending integer. The first 8 bytes 707ef9e6dc7SBill Fenner * of the data portion are used to hold a UNIX "timeval" struct in host 7088fae3551SRodney W. Grimes * byte-order, to compute the round-trip time. 7098fae3551SRodney W. Grimes */ 71043470e3bSGarrett Wollman static void 71143470e3bSGarrett Wollman pinger(void) 7128fae3551SRodney W. Grimes { 7138fae3551SRodney W. Grimes register struct icmp *icp; 7148fae3551SRodney W. Grimes register int cc; 7158fae3551SRodney W. Grimes int i; 7168fae3551SRodney W. Grimes 7178fae3551SRodney W. Grimes icp = (struct icmp *)outpack; 7188fae3551SRodney W. Grimes icp->icmp_type = ICMP_ECHO; 7198fae3551SRodney W. Grimes icp->icmp_code = 0; 7208fae3551SRodney W. Grimes icp->icmp_cksum = 0; 7210e59c641SJulian Elischer icp->icmp_seq = ntransmitted; 7228fae3551SRodney W. Grimes icp->icmp_id = ident; /* ID */ 7238fae3551SRodney W. Grimes 7248fae3551SRodney W. Grimes CLR(icp->icmp_seq % mx_dup_ck); 7258fae3551SRodney W. Grimes 7268fae3551SRodney W. Grimes if (timing) 7278fae3551SRodney W. Grimes (void)gettimeofday((struct timeval *)&outpack[8], 7288fae3551SRodney W. Grimes (struct timezone *)NULL); 7298fae3551SRodney W. Grimes 730d32ff037SJohn Birrell cc = datalen + PHDR_LEN; /* skips ICMP portion */ 7318fae3551SRodney W. Grimes 7328fae3551SRodney W. Grimes /* compute ICMP checksum here */ 7338fae3551SRodney W. Grimes icp->icmp_cksum = in_cksum((u_short *)icp, cc); 7348fae3551SRodney W. Grimes 7358fae3551SRodney W. Grimes i = sendto(s, (char *)outpack, cc, 0, &whereto, 7368fae3551SRodney W. Grimes sizeof(struct sockaddr)); 7378fae3551SRodney W. Grimes 7388fae3551SRodney W. Grimes if (i < 0 || i != cc) { 73943470e3bSGarrett Wollman if (i < 0) { 7408f975bb3SBruce Evans if (options & F_FLOOD && errno == ENOBUFS) { 741515dd2faSJulian Elischer usleep(FLOOD_BACKOFF); 742515dd2faSJulian Elischer return; 743515dd2faSJulian Elischer } 74443470e3bSGarrett Wollman warn("sendto"); 74543470e3bSGarrett Wollman } else { 74643470e3bSGarrett Wollman warn("%s: partial write: %d of %d bytes", 747416aa49bSPoul-Henning Kamp hostname, i, cc); 7488fae3551SRodney W. Grimes } 749363d7bbeSJulian Elischer } 750363d7bbeSJulian Elischer ntransmitted++; 7518fae3551SRodney W. Grimes if (!(options & F_QUIET) && options & F_FLOOD) 7528fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &DOT, 1); 7538fae3551SRodney W. Grimes } 7548fae3551SRodney W. Grimes 7558fae3551SRodney W. Grimes /* 7568fae3551SRodney W. Grimes * pr_pack -- 7578fae3551SRodney W. Grimes * Print out the packet, if it came from us. This logic is necessary 7588fae3551SRodney W. Grimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 7598fae3551SRodney W. Grimes * which arrive ('tis only fair). This permits multiple copies of this 7608fae3551SRodney W. Grimes * program to be run without having intermingled output (or statistics!). 7618fae3551SRodney W. Grimes */ 76243470e3bSGarrett Wollman static void 763039d6aa4SBill Fenner pr_pack(buf, cc, from, tv) 7648fae3551SRodney W. Grimes char *buf; 7658fae3551SRodney W. Grimes int cc; 7668fae3551SRodney W. Grimes struct sockaddr_in *from; 767039d6aa4SBill Fenner struct timeval *tv; 7688fae3551SRodney W. Grimes { 7698fae3551SRodney W. Grimes register struct icmp *icp; 7708fae3551SRodney W. Grimes register u_long l; 7718fae3551SRodney W. Grimes register int i, j; 7728fae3551SRodney W. Grimes register u_char *cp,*dp; 7738fae3551SRodney W. Grimes static int old_rrlen; 7748fae3551SRodney W. Grimes static char old_rr[MAX_IPOPTLEN]; 7758fae3551SRodney W. Grimes struct ip *ip; 776039d6aa4SBill Fenner struct timeval *tp; 7778f975bb3SBruce Evans double triptime; 7788fae3551SRodney W. Grimes int hlen, dupflag; 7798fae3551SRodney W. Grimes 7808fae3551SRodney W. Grimes /* Check the IP header */ 7818fae3551SRodney W. Grimes ip = (struct ip *)buf; 7828fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 7838fae3551SRodney W. Grimes if (cc < hlen + ICMP_MINLEN) { 7848fae3551SRodney W. Grimes if (options & F_VERBOSE) 78543470e3bSGarrett Wollman warn("packet too short (%d bytes) from %s", cc, 78643470e3bSGarrett Wollman inet_ntoa(from->sin_addr)); 7878fae3551SRodney W. Grimes return; 7888fae3551SRodney W. Grimes } 7898fae3551SRodney W. Grimes 7908fae3551SRodney W. Grimes /* Now the ICMP part */ 7918fae3551SRodney W. Grimes cc -= hlen; 7928fae3551SRodney W. Grimes icp = (struct icmp *)(buf + hlen); 7938fae3551SRodney W. Grimes if (icp->icmp_type == ICMP_ECHOREPLY) { 7948fae3551SRodney W. Grimes if (icp->icmp_id != ident) 7958fae3551SRodney W. Grimes return; /* 'Twas not our ECHO */ 7968fae3551SRodney W. Grimes ++nreceived; 7978f975bb3SBruce Evans triptime = 0.0; 7988fae3551SRodney W. Grimes if (timing) { 799d32ff037SJohn Birrell struct timeval tv1; 8008fae3551SRodney W. Grimes #ifndef icmp_data 8018fae3551SRodney W. Grimes tp = (struct timeval *)&icp->icmp_ip; 8028fae3551SRodney W. Grimes #else 8038fae3551SRodney W. Grimes tp = (struct timeval *)icp->icmp_data; 8048fae3551SRodney W. Grimes #endif 805d32ff037SJohn Birrell /* Avoid unaligned data: */ 806d32ff037SJohn Birrell memcpy(&tv1,tp,sizeof(tv1)); 807039d6aa4SBill Fenner tvsub(tv, &tv1); 808039d6aa4SBill Fenner triptime = ((double)tv->tv_sec) * 1000.0 + 809039d6aa4SBill Fenner ((double)tv->tv_usec) / 1000.0; 8108fae3551SRodney W. Grimes tsum += triptime; 8113109a910SGarrett Wollman tsumsq += triptime * triptime; 8128fae3551SRodney W. Grimes if (triptime < tmin) 8138fae3551SRodney W. Grimes tmin = triptime; 8148fae3551SRodney W. Grimes if (triptime > tmax) 8158fae3551SRodney W. Grimes tmax = triptime; 8168fae3551SRodney W. Grimes } 8178fae3551SRodney W. Grimes 8188fae3551SRodney W. Grimes if (TST(icp->icmp_seq % mx_dup_ck)) { 8198fae3551SRodney W. Grimes ++nrepeats; 8208fae3551SRodney W. Grimes --nreceived; 8218fae3551SRodney W. Grimes dupflag = 1; 8228fae3551SRodney W. Grimes } else { 8238fae3551SRodney W. Grimes SET(icp->icmp_seq % mx_dup_ck); 8248fae3551SRodney W. Grimes dupflag = 0; 8258fae3551SRodney W. Grimes } 8268fae3551SRodney W. Grimes 8278fae3551SRodney W. Grimes if (options & F_QUIET) 8288fae3551SRodney W. Grimes return; 8298fae3551SRodney W. Grimes 8308fae3551SRodney W. Grimes if (options & F_FLOOD) 8318fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &BSPACE, 1); 8328fae3551SRodney W. Grimes else { 8338fae3551SRodney W. Grimes (void)printf("%d bytes from %s: icmp_seq=%u", cc, 8348fae3551SRodney W. Grimes inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr), 8358fae3551SRodney W. Grimes icp->icmp_seq); 8368fae3551SRodney W. Grimes (void)printf(" ttl=%d", ip->ip_ttl); 8378fae3551SRodney W. Grimes if (timing) 838d410b6f1SDavid Greenman (void)printf(" time=%.3f ms", triptime); 8398fae3551SRodney W. Grimes if (dupflag) 8408fae3551SRodney W. Grimes (void)printf(" (DUP!)"); 841772dfa72SDaniel O'Callaghan if (options & F_AUDIBLE) 842772dfa72SDaniel O'Callaghan (void)printf("\a"); 8438fae3551SRodney W. Grimes /* check the data */ 844d32ff037SJohn Birrell cp = (u_char*)&icp->icmp_data[PHDR_LEN]; 845d32ff037SJohn Birrell dp = &outpack[8 + PHDR_LEN]; 846d32ff037SJohn Birrell for (i = PHDR_LEN; i < datalen; ++i, ++cp, ++dp) { 8478fae3551SRodney W. Grimes if (*cp != *dp) { 8488fae3551SRodney W. Grimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 8498fae3551SRodney W. Grimes i, *dp, *cp); 850d32ff037SJohn Birrell printf("\ncp:"); 8518fae3551SRodney W. Grimes cp = (u_char*)&icp->icmp_data[0]; 852d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 853d32ff037SJohn Birrell if ((i % 32) == 8) 854d32ff037SJohn Birrell (void)printf("\n\t"); 855d32ff037SJohn Birrell (void)printf("%x ", *cp); 856d32ff037SJohn Birrell } 857d32ff037SJohn Birrell printf("\ndp:"); 858d32ff037SJohn Birrell cp = &outpack[8]; 859d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 8608fae3551SRodney W. Grimes if ((i % 32) == 8) 8618fae3551SRodney W. Grimes (void)printf("\n\t"); 8628fae3551SRodney W. Grimes (void)printf("%x ", *cp); 8638fae3551SRodney W. Grimes } 8648fae3551SRodney W. Grimes break; 8658fae3551SRodney W. Grimes } 8668fae3551SRodney W. Grimes } 8678fae3551SRodney W. Grimes } 8688fae3551SRodney W. Grimes } else { 869ef9e6dc7SBill Fenner /* 870ef9e6dc7SBill Fenner * We've got something other than an ECHOREPLY. 871ef9e6dc7SBill Fenner * See if it's a reply to something that we sent. 872ef9e6dc7SBill Fenner * We can compare IP destination, protocol, 873ef9e6dc7SBill Fenner * and ICMP type and ID. 874f78ac61bSWarner Losh * 875f78ac61bSWarner Losh * Only print all the error messages if we are running 876f78ac61bSWarner Losh * as root to avoid leaking information not normally 877f78ac61bSWarner Losh * available to those not running as root. 878ef9e6dc7SBill Fenner */ 879ef9e6dc7SBill Fenner #ifndef icmp_data 880ef9e6dc7SBill Fenner struct ip *oip = &icp->icmp_ip; 881ef9e6dc7SBill Fenner #else 882ef9e6dc7SBill Fenner struct ip *oip = (struct ip *)icp->icmp_data; 883ef9e6dc7SBill Fenner #endif 884ef9e6dc7SBill Fenner struct icmp *oicmp = (struct icmp *)(oip + 1); 885ef9e6dc7SBill Fenner 886ee2bf734SWarner Losh if (((options & F_VERBOSE) && uid == 0) || 887ef9e6dc7SBill Fenner (!(options & F_QUIET2) && 888ef9e6dc7SBill Fenner (oip->ip_dst.s_addr == 889ef9e6dc7SBill Fenner ((struct sockaddr_in *)&whereto)->sin_addr.s_addr) && 890ef9e6dc7SBill Fenner (oip->ip_p == IPPROTO_ICMP) && 891ef9e6dc7SBill Fenner (oicmp->icmp_type == ICMP_ECHO) && 892ef9e6dc7SBill Fenner (oicmp->icmp_id == ident))) { 8938fae3551SRodney W. Grimes (void)printf("%d bytes from %s: ", cc, 89443470e3bSGarrett Wollman pr_addr(from->sin_addr)); 8958fae3551SRodney W. Grimes pr_icmph(icp); 896ef9e6dc7SBill Fenner } else 897ef9e6dc7SBill Fenner return; 8988fae3551SRodney W. Grimes } 8998fae3551SRodney W. Grimes 9008fae3551SRodney W. Grimes /* Display any IP options */ 9018fae3551SRodney W. Grimes cp = (u_char *)buf + sizeof(struct ip); 9028fae3551SRodney W. Grimes 9038fae3551SRodney W. Grimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 9048fae3551SRodney W. Grimes switch (*cp) { 9058fae3551SRodney W. Grimes case IPOPT_EOL: 9068fae3551SRodney W. Grimes hlen = 0; 9078fae3551SRodney W. Grimes break; 9088fae3551SRodney W. Grimes case IPOPT_LSRR: 9098fae3551SRodney W. Grimes (void)printf("\nLSRR: "); 9108fae3551SRodney W. Grimes hlen -= 2; 9118fae3551SRodney W. Grimes j = *++cp; 9128fae3551SRodney W. Grimes ++cp; 9138fae3551SRodney W. Grimes if (j > IPOPT_MINOFF) 9148fae3551SRodney W. Grimes for (;;) { 9158fae3551SRodney W. Grimes l = *++cp; 9168fae3551SRodney W. Grimes l = (l<<8) + *++cp; 9178fae3551SRodney W. Grimes l = (l<<8) + *++cp; 9188fae3551SRodney W. Grimes l = (l<<8) + *++cp; 91943470e3bSGarrett Wollman if (l == 0) { 92043470e3bSGarrett Wollman printf("\t0.0.0.0"); 92143470e3bSGarrett Wollman } else { 92243470e3bSGarrett Wollman struct in_addr ina; 92343470e3bSGarrett Wollman ina.s_addr = ntohl(l); 92443470e3bSGarrett Wollman printf("\t%s", pr_addr(ina)); 92543470e3bSGarrett Wollman } 9268fae3551SRodney W. Grimes hlen -= 4; 9278fae3551SRodney W. Grimes j -= 4; 9288fae3551SRodney W. Grimes if (j <= IPOPT_MINOFF) 9298fae3551SRodney W. Grimes break; 9308fae3551SRodney W. Grimes (void)putchar('\n'); 9318fae3551SRodney W. Grimes } 9328fae3551SRodney W. Grimes break; 9338fae3551SRodney W. Grimes case IPOPT_RR: 9348fae3551SRodney W. Grimes j = *++cp; /* get length */ 9358fae3551SRodney W. Grimes i = *++cp; /* and pointer */ 9368fae3551SRodney W. Grimes hlen -= 2; 9378fae3551SRodney W. Grimes if (i > j) 9388fae3551SRodney W. Grimes i = j; 9398fae3551SRodney W. Grimes i -= IPOPT_MINOFF; 9408fae3551SRodney W. Grimes if (i <= 0) 9418fae3551SRodney W. Grimes continue; 9428fae3551SRodney W. Grimes if (i == old_rrlen 9438fae3551SRodney W. Grimes && cp == (u_char *)buf + sizeof(struct ip) + 2 9448fae3551SRodney W. Grimes && !bcmp((char *)cp, old_rr, i) 9458fae3551SRodney W. Grimes && !(options & F_FLOOD)) { 9468fae3551SRodney W. Grimes (void)printf("\t(same route)"); 9478fae3551SRodney W. Grimes i = ((i + 3) / 4) * 4; 9488fae3551SRodney W. Grimes hlen -= i; 9498fae3551SRodney W. Grimes cp += i; 9508fae3551SRodney W. Grimes break; 9518fae3551SRodney W. Grimes } 9523aa4b744SEivind Eklund if (i < MAX_IPOPTLEN) { 9538fae3551SRodney W. Grimes old_rrlen = i; 9548fae3551SRodney W. Grimes bcopy((char *)cp, old_rr, i); 955c03e877aSWarner Losh } else 956c03e877aSWarner Losh old_rrlen = 0; 957c03e877aSWarner Losh 9588fae3551SRodney W. Grimes (void)printf("\nRR: "); 959c03e877aSWarner Losh j = 0; 9608fae3551SRodney W. Grimes for (;;) { 9618fae3551SRodney W. Grimes l = *++cp; 9628fae3551SRodney W. Grimes l = (l<<8) + *++cp; 9638fae3551SRodney W. Grimes l = (l<<8) + *++cp; 9648fae3551SRodney W. Grimes l = (l<<8) + *++cp; 96543470e3bSGarrett Wollman if (l == 0) { 96643470e3bSGarrett Wollman printf("\t0.0.0.0"); 96743470e3bSGarrett Wollman } else { 96843470e3bSGarrett Wollman struct in_addr ina; 96943470e3bSGarrett Wollman ina.s_addr = ntohl(l); 97043470e3bSGarrett Wollman printf("\t%s", pr_addr(ina)); 97143470e3bSGarrett Wollman } 9728fae3551SRodney W. Grimes hlen -= 4; 9738fae3551SRodney W. Grimes i -= 4; 974c03e877aSWarner Losh j += 4; 9758fae3551SRodney W. Grimes if (i <= 0) 9768fae3551SRodney W. Grimes break; 977c03e877aSWarner Losh if (j >= MAX_IPOPTLEN) { 978c03e877aSWarner Losh (void) printf("\t(truncated route)"); 979c03e877aSWarner Losh break; 980c03e877aSWarner Losh } 9818fae3551SRodney W. Grimes (void)putchar('\n'); 9828fae3551SRodney W. Grimes } 9838fae3551SRodney W. Grimes break; 9848fae3551SRodney W. Grimes case IPOPT_NOP: 9858fae3551SRodney W. Grimes (void)printf("\nNOP"); 9868fae3551SRodney W. Grimes break; 9878fae3551SRodney W. Grimes default: 9888fae3551SRodney W. Grimes (void)printf("\nunknown option %x", *cp); 9898fae3551SRodney W. Grimes break; 9908fae3551SRodney W. Grimes } 9918fae3551SRodney W. Grimes if (!(options & F_FLOOD)) { 9928fae3551SRodney W. Grimes (void)putchar('\n'); 9938fae3551SRodney W. Grimes (void)fflush(stdout); 9948fae3551SRodney W. Grimes } 9958fae3551SRodney W. Grimes } 9968fae3551SRodney W. Grimes 9978fae3551SRodney W. Grimes /* 9988fae3551SRodney W. Grimes * in_cksum -- 9998fae3551SRodney W. Grimes * Checksum routine for Internet Protocol family headers (C Version) 10008fae3551SRodney W. Grimes */ 100143470e3bSGarrett Wollman u_short 10028fae3551SRodney W. Grimes in_cksum(addr, len) 10038fae3551SRodney W. Grimes u_short *addr; 10048fae3551SRodney W. Grimes int len; 10058fae3551SRodney W. Grimes { 10068fae3551SRodney W. Grimes register int nleft = len; 10078fae3551SRodney W. Grimes register u_short *w = addr; 10088fae3551SRodney W. Grimes register int sum = 0; 10093ec12efeSPierre Beyssac union { 101009333e78SPierre Beyssac u_short us; 101109333e78SPierre Beyssac u_char uc[2]; 101209333e78SPierre Beyssac } last; 101309333e78SPierre Beyssac u_short answer; 10148fae3551SRodney W. Grimes 10158fae3551SRodney W. Grimes /* 10168fae3551SRodney W. Grimes * Our algorithm is simple, using a 32 bit accumulator (sum), we add 10178fae3551SRodney W. Grimes * sequential 16 bit words to it, and at the end, fold back all the 10188fae3551SRodney W. Grimes * carry bits from the top 16 bits into the lower 16 bits. 10198fae3551SRodney W. Grimes */ 10208fae3551SRodney W. Grimes while (nleft > 1) { 10218fae3551SRodney W. Grimes sum += *w++; 10228fae3551SRodney W. Grimes nleft -= 2; 10238fae3551SRodney W. Grimes } 10248fae3551SRodney W. Grimes 10258fae3551SRodney W. Grimes /* mop up an odd byte, if necessary */ 10268fae3551SRodney W. Grimes if (nleft == 1) { 102709333e78SPierre Beyssac last.uc[0] = *(u_char *)w; 102809333e78SPierre Beyssac last.uc[1] = 0; 102909333e78SPierre Beyssac sum += last.us; 10308fae3551SRodney W. Grimes } 10318fae3551SRodney W. Grimes 10328fae3551SRodney W. Grimes /* add back carry outs from top 16 bits to low 16 bits */ 10338fae3551SRodney W. Grimes sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 10348fae3551SRodney W. Grimes sum += (sum >> 16); /* add carry */ 103509333e78SPierre Beyssac answer = ~sum; /* truncate to 16 bits */ 103609333e78SPierre Beyssac return(answer); 10378fae3551SRodney W. Grimes } 10388fae3551SRodney W. Grimes 10398fae3551SRodney W. Grimes /* 10408fae3551SRodney W. Grimes * tvsub -- 10418fae3551SRodney W. Grimes * Subtract 2 timeval structs: out = out - in. Out is assumed to 10428fae3551SRodney W. Grimes * be >= in. 10438fae3551SRodney W. Grimes */ 104443470e3bSGarrett Wollman static void 10458fae3551SRodney W. Grimes tvsub(out, in) 10468fae3551SRodney W. Grimes register struct timeval *out, *in; 10478fae3551SRodney W. Grimes { 10488fae3551SRodney W. Grimes if ((out->tv_usec -= in->tv_usec) < 0) { 10498fae3551SRodney W. Grimes --out->tv_sec; 10508fae3551SRodney W. Grimes out->tv_usec += 1000000; 10518fae3551SRodney W. Grimes } 10528fae3551SRodney W. Grimes out->tv_sec -= in->tv_sec; 10538fae3551SRodney W. Grimes } 10548fae3551SRodney W. Grimes 10558fae3551SRodney W. Grimes /* 1056badd8138SSean Eric Fagan * status -- 1057badd8138SSean Eric Fagan * Print out statistics when SIGINFO is received. 1058badd8138SSean Eric Fagan */ 1059badd8138SSean Eric Fagan 106043470e3bSGarrett Wollman static void 10617d81b35cSBruce Evans status(sig) 10627d81b35cSBruce Evans int sig; 10637d81b35cSBruce Evans { 106437e5b2c6SSean Eric Fagan siginfo_p = 1; 106537e5b2c6SSean Eric Fagan } 106637e5b2c6SSean Eric Fagan 106743470e3bSGarrett Wollman static void 106837e5b2c6SSean Eric Fagan check_status() 1069badd8138SSean Eric Fagan { 107037e5b2c6SSean Eric Fagan if (siginfo_p) { 107137e5b2c6SSean Eric Fagan siginfo_p = 0; 10727d81b35cSBruce Evans (void)fprintf(stderr, 10737d81b35cSBruce Evans "\r%ld/%ld packets received (%.0f%%) %.3f min / %.3f avg / %.3f max\n", 10747d81b35cSBruce Evans nreceived, ntransmitted, 10757d81b35cSBruce Evans ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0, 10767d81b35cSBruce Evans nreceived ? tmin : 0.0, 10777d81b35cSBruce Evans nreceived + nrepeats ? tsum / (nreceived + nrepeats) : tsum, 10787d81b35cSBruce Evans tmax); 107937e5b2c6SSean Eric Fagan } 1080badd8138SSean Eric Fagan } 1081badd8138SSean Eric Fagan 1082badd8138SSean Eric Fagan /* 10838fae3551SRodney W. Grimes * finish -- 10848fae3551SRodney W. Grimes * Print out statistics, and give up. 10858fae3551SRodney W. Grimes */ 108643470e3bSGarrett Wollman static void 10878f975bb3SBruce Evans finish() 10888fae3551SRodney W. Grimes { 1089badd8138SSean Eric Fagan struct termios ts; 10908fae3551SRodney W. Grimes 10918fae3551SRodney W. Grimes (void)signal(SIGINT, SIG_IGN); 1092a2a00888SSean Eric Fagan (void)signal(SIGALRM, SIG_IGN); 10938fae3551SRodney W. Grimes (void)putchar('\n'); 10948fae3551SRodney W. Grimes (void)fflush(stdout); 10958fae3551SRodney W. Grimes (void)printf("--- %s ping statistics ---\n", hostname); 10968fae3551SRodney W. Grimes (void)printf("%ld packets transmitted, ", ntransmitted); 10978fae3551SRodney W. Grimes (void)printf("%ld packets received, ", nreceived); 10988fae3551SRodney W. Grimes if (nrepeats) 10998fae3551SRodney W. Grimes (void)printf("+%ld duplicates, ", nrepeats); 1100ebe70c8fSWarner Losh if (ntransmitted) { 11018fae3551SRodney W. Grimes if (nreceived > ntransmitted) 11028fae3551SRodney W. Grimes (void)printf("-- somebody's printing up packets!"); 11038fae3551SRodney W. Grimes else 11048fae3551SRodney W. Grimes (void)printf("%d%% packet loss", 11058fae3551SRodney W. Grimes (int) (((ntransmitted - nreceived) * 100) / 11068fae3551SRodney W. Grimes ntransmitted)); 1107ebe70c8fSWarner Losh } 11088fae3551SRodney W. Grimes (void)putchar('\n'); 11093109a910SGarrett Wollman if (nreceived && timing) { 11103109a910SGarrett Wollman double n = nreceived + nrepeats; 11113109a910SGarrett Wollman double avg = tsum / n; 11123109a910SGarrett Wollman double vari = tsumsq / n - avg * avg; 11133109a910SGarrett Wollman printf("round-trip min/avg/max/stddev = " 11143109a910SGarrett Wollman "%.3f/%.3f/%.3f/%.3f ms\n", 11153109a910SGarrett Wollman tmin, avg, tmax, sqrt(vari)); 11163109a910SGarrett Wollman } 11172ceaae21SBruce Evans if (reset_kerninfo && tcgetattr(STDOUT_FILENO, &ts) != -1) { 1118badd8138SSean Eric Fagan ts.c_lflag &= ~NOKERNINFO; 11192ceaae21SBruce Evans tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 1120badd8138SSean Eric Fagan } 1121badd8138SSean Eric Fagan 11226e1173dcSDavid Greenman if (nreceived) 11238fae3551SRodney W. Grimes exit(0); 11246e1173dcSDavid Greenman else 11256e1173dcSDavid Greenman exit(2); 11268fae3551SRodney W. Grimes } 11278fae3551SRodney W. Grimes 11288fae3551SRodney W. Grimes #ifdef notdef 11298fae3551SRodney W. Grimes static char *ttab[] = { 11308fae3551SRodney W. Grimes "Echo Reply", /* ip + seq + udata */ 11318fae3551SRodney W. Grimes "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */ 11328fae3551SRodney W. Grimes "Source Quench", /* IP */ 11338fae3551SRodney W. Grimes "Redirect", /* redirect type, gateway, + IP */ 11348fae3551SRodney W. Grimes "Echo", 11358fae3551SRodney W. Grimes "Time Exceeded", /* transit, frag reassem + IP */ 11368fae3551SRodney W. Grimes "Parameter Problem", /* pointer + IP */ 11378fae3551SRodney W. Grimes "Timestamp", /* id + seq + three timestamps */ 11388fae3551SRodney W. Grimes "Timestamp Reply", /* " */ 11398fae3551SRodney W. Grimes "Info Request", /* id + sq */ 11408fae3551SRodney W. Grimes "Info Reply" /* " */ 11418fae3551SRodney W. Grimes }; 11428fae3551SRodney W. Grimes #endif 11438fae3551SRodney W. Grimes 11448fae3551SRodney W. Grimes /* 11458fae3551SRodney W. Grimes * pr_icmph -- 11468fae3551SRodney W. Grimes * Print a descriptive string about an ICMP header. 11478fae3551SRodney W. Grimes */ 114843470e3bSGarrett Wollman static void 11498fae3551SRodney W. Grimes pr_icmph(icp) 11508fae3551SRodney W. Grimes struct icmp *icp; 11518fae3551SRodney W. Grimes { 11528fae3551SRodney W. Grimes switch(icp->icmp_type) { 11538fae3551SRodney W. Grimes case ICMP_ECHOREPLY: 11548fae3551SRodney W. Grimes (void)printf("Echo Reply\n"); 11558fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 11568fae3551SRodney W. Grimes break; 11578fae3551SRodney W. Grimes case ICMP_UNREACH: 11588fae3551SRodney W. Grimes switch(icp->icmp_code) { 11598fae3551SRodney W. Grimes case ICMP_UNREACH_NET: 11608fae3551SRodney W. Grimes (void)printf("Destination Net Unreachable\n"); 11618fae3551SRodney W. Grimes break; 11628fae3551SRodney W. Grimes case ICMP_UNREACH_HOST: 11638fae3551SRodney W. Grimes (void)printf("Destination Host Unreachable\n"); 11648fae3551SRodney W. Grimes break; 11658fae3551SRodney W. Grimes case ICMP_UNREACH_PROTOCOL: 11668fae3551SRodney W. Grimes (void)printf("Destination Protocol Unreachable\n"); 11678fae3551SRodney W. Grimes break; 11688fae3551SRodney W. Grimes case ICMP_UNREACH_PORT: 11698fae3551SRodney W. Grimes (void)printf("Destination Port Unreachable\n"); 11708fae3551SRodney W. Grimes break; 11718fae3551SRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 1172ef9e6dc7SBill Fenner (void)printf("frag needed and DF set (MTU %d)\n", 1173ff49597eSBill Fenner ntohs(icp->icmp_nextmtu)); 11748fae3551SRodney W. Grimes break; 11758fae3551SRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 11768fae3551SRodney W. Grimes (void)printf("Source Route Failed\n"); 11778fae3551SRodney W. Grimes break; 1178ef9e6dc7SBill Fenner case ICMP_UNREACH_FILTER_PROHIB: 1179ef9e6dc7SBill Fenner (void)printf("Communication prohibited by filter\n"); 1180ef9e6dc7SBill Fenner break; 11818fae3551SRodney W. Grimes default: 11828fae3551SRodney W. Grimes (void)printf("Dest Unreachable, Bad Code: %d\n", 11838fae3551SRodney W. Grimes icp->icmp_code); 11848fae3551SRodney W. Grimes break; 11858fae3551SRodney W. Grimes } 11868fae3551SRodney W. Grimes /* Print returned IP header information */ 11878fae3551SRodney W. Grimes #ifndef icmp_data 11888fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 11898fae3551SRodney W. Grimes #else 11908fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 11918fae3551SRodney W. Grimes #endif 11928fae3551SRodney W. Grimes break; 11938fae3551SRodney W. Grimes case ICMP_SOURCEQUENCH: 11948fae3551SRodney W. Grimes (void)printf("Source Quench\n"); 11958fae3551SRodney W. Grimes #ifndef icmp_data 11968fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 11978fae3551SRodney W. Grimes #else 11988fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 11998fae3551SRodney W. Grimes #endif 12008fae3551SRodney W. Grimes break; 12018fae3551SRodney W. Grimes case ICMP_REDIRECT: 12028fae3551SRodney W. Grimes switch(icp->icmp_code) { 12038fae3551SRodney W. Grimes case ICMP_REDIRECT_NET: 12048fae3551SRodney W. Grimes (void)printf("Redirect Network"); 12058fae3551SRodney W. Grimes break; 12068fae3551SRodney W. Grimes case ICMP_REDIRECT_HOST: 12078fae3551SRodney W. Grimes (void)printf("Redirect Host"); 12088fae3551SRodney W. Grimes break; 12098fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSNET: 12108fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Network"); 12118fae3551SRodney W. Grimes break; 12128fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSHOST: 12138fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Host"); 12148fae3551SRodney W. Grimes break; 12158fae3551SRodney W. Grimes default: 12168fae3551SRodney W. Grimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 12178fae3551SRodney W. Grimes break; 12188fae3551SRodney W. Grimes } 1219ff49597eSBill Fenner (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 12208fae3551SRodney W. Grimes #ifndef icmp_data 12218fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 12228fae3551SRodney W. Grimes #else 12238fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 12248fae3551SRodney W. Grimes #endif 12258fae3551SRodney W. Grimes break; 12268fae3551SRodney W. Grimes case ICMP_ECHO: 12278fae3551SRodney W. Grimes (void)printf("Echo Request\n"); 12288fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 12298fae3551SRodney W. Grimes break; 12308fae3551SRodney W. Grimes case ICMP_TIMXCEED: 12318fae3551SRodney W. Grimes switch(icp->icmp_code) { 12328fae3551SRodney W. Grimes case ICMP_TIMXCEED_INTRANS: 12338fae3551SRodney W. Grimes (void)printf("Time to live exceeded\n"); 12348fae3551SRodney W. Grimes break; 12358fae3551SRodney W. Grimes case ICMP_TIMXCEED_REASS: 12368fae3551SRodney W. Grimes (void)printf("Frag reassembly time exceeded\n"); 12378fae3551SRodney W. Grimes break; 12388fae3551SRodney W. Grimes default: 12398fae3551SRodney W. Grimes (void)printf("Time exceeded, Bad Code: %d\n", 12408fae3551SRodney W. Grimes icp->icmp_code); 12418fae3551SRodney W. Grimes break; 12428fae3551SRodney W. Grimes } 12438fae3551SRodney W. Grimes #ifndef icmp_data 12448fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 12458fae3551SRodney W. Grimes #else 12468fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 12478fae3551SRodney W. Grimes #endif 12488fae3551SRodney W. Grimes break; 12498fae3551SRodney W. Grimes case ICMP_PARAMPROB: 12508fae3551SRodney W. Grimes (void)printf("Parameter problem: pointer = 0x%02x\n", 12518fae3551SRodney W. Grimes icp->icmp_hun.ih_pptr); 12528fae3551SRodney W. Grimes #ifndef icmp_data 12538fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 12548fae3551SRodney W. Grimes #else 12558fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 12568fae3551SRodney W. Grimes #endif 12578fae3551SRodney W. Grimes break; 12588fae3551SRodney W. Grimes case ICMP_TSTAMP: 12598fae3551SRodney W. Grimes (void)printf("Timestamp\n"); 12608fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 12618fae3551SRodney W. Grimes break; 12628fae3551SRodney W. Grimes case ICMP_TSTAMPREPLY: 12638fae3551SRodney W. Grimes (void)printf("Timestamp Reply\n"); 12648fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 12658fae3551SRodney W. Grimes break; 12668fae3551SRodney W. Grimes case ICMP_IREQ: 12678fae3551SRodney W. Grimes (void)printf("Information Request\n"); 12688fae3551SRodney W. Grimes /* XXX ID + Seq */ 12698fae3551SRodney W. Grimes break; 12708fae3551SRodney W. Grimes case ICMP_IREQREPLY: 12718fae3551SRodney W. Grimes (void)printf("Information Reply\n"); 12728fae3551SRodney W. Grimes /* XXX ID + Seq */ 12738fae3551SRodney W. Grimes break; 12748fae3551SRodney W. Grimes case ICMP_MASKREQ: 12758fae3551SRodney W. Grimes (void)printf("Address Mask Request\n"); 12768fae3551SRodney W. Grimes break; 12778fae3551SRodney W. Grimes case ICMP_MASKREPLY: 12788fae3551SRodney W. Grimes (void)printf("Address Mask Reply\n"); 12798fae3551SRodney W. Grimes break; 1280ef9e6dc7SBill Fenner case ICMP_ROUTERADVERT: 1281ef9e6dc7SBill Fenner (void)printf("Router Advertisement\n"); 1282ef9e6dc7SBill Fenner break; 1283ef9e6dc7SBill Fenner case ICMP_ROUTERSOLICIT: 1284ef9e6dc7SBill Fenner (void)printf("Router Solicitation\n"); 1285ef9e6dc7SBill Fenner break; 12868fae3551SRodney W. Grimes default: 12878fae3551SRodney W. Grimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 12888fae3551SRodney W. Grimes } 12898fae3551SRodney W. Grimes } 12908fae3551SRodney W. Grimes 12918fae3551SRodney W. Grimes /* 12928fae3551SRodney W. Grimes * pr_iph -- 12938fae3551SRodney W. Grimes * Print an IP header with options. 12948fae3551SRodney W. Grimes */ 129543470e3bSGarrett Wollman static void 12968fae3551SRodney W. Grimes pr_iph(ip) 12978fae3551SRodney W. Grimes struct ip *ip; 12988fae3551SRodney W. Grimes { 12998fae3551SRodney W. Grimes int hlen; 13008fae3551SRodney W. Grimes u_char *cp; 13018fae3551SRodney W. Grimes 13028fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 13038fae3551SRodney W. Grimes cp = (u_char *)ip + 20; /* point to options */ 13048fae3551SRodney W. Grimes 1305ef9e6dc7SBill Fenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 13068fae3551SRodney W. Grimes (void)printf(" %1x %1x %02x %04x %04x", 1307ef9e6dc7SBill Fenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1308ef9e6dc7SBill Fenner ntohs(ip->ip_id)); 1309d32ff037SJohn Birrell (void)printf(" %1lx %04lx", 1310d32ff037SJohn Birrell (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13, 1311d32ff037SJohn Birrell (u_long) ntohl(ip->ip_off) & 0x1fff); 1312ef9e6dc7SBill Fenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1313ef9e6dc7SBill Fenner ntohs(ip->ip_sum)); 13148fae3551SRodney W. Grimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr)); 13158fae3551SRodney W. Grimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr)); 1316ef9e6dc7SBill Fenner /* dump any option bytes */ 13178fae3551SRodney W. Grimes while (hlen-- > 20) { 13188fae3551SRodney W. Grimes (void)printf("%02x", *cp++); 13198fae3551SRodney W. Grimes } 13208fae3551SRodney W. Grimes (void)putchar('\n'); 13218fae3551SRodney W. Grimes } 13228fae3551SRodney W. Grimes 13238fae3551SRodney W. Grimes /* 13248fae3551SRodney W. Grimes * pr_addr -- 13258fae3551SRodney W. Grimes * Return an ascii host address as a dotted quad and optionally with 13268fae3551SRodney W. Grimes * a hostname. 13278fae3551SRodney W. Grimes */ 132843470e3bSGarrett Wollman static char * 132943470e3bSGarrett Wollman pr_addr(ina) 133043470e3bSGarrett Wollman struct in_addr ina; 13318fae3551SRodney W. Grimes { 13328fae3551SRodney W. Grimes struct hostent *hp; 1333f78ac61bSWarner Losh static char buf[16 + 3 + MAXHOSTNAMELEN]; 13348fae3551SRodney W. Grimes 13358fae3551SRodney W. Grimes if ((options & F_NUMERIC) || 133643470e3bSGarrett Wollman !(hp = gethostbyaddr((char *)&ina, 4, AF_INET))) 133743470e3bSGarrett Wollman return inet_ntoa(ina); 13388fae3551SRodney W. Grimes else 1339efa38539SPeter Wemm (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 134043470e3bSGarrett Wollman inet_ntoa(ina)); 13418fae3551SRodney W. Grimes return(buf); 13428fae3551SRodney W. Grimes } 13438fae3551SRodney W. Grimes 13448fae3551SRodney W. Grimes /* 13458fae3551SRodney W. Grimes * pr_retip -- 13468fae3551SRodney W. Grimes * Dump some info on a returned (via ICMP) IP packet. 13478fae3551SRodney W. Grimes */ 134843470e3bSGarrett Wollman static void 13498fae3551SRodney W. Grimes pr_retip(ip) 13508fae3551SRodney W. Grimes struct ip *ip; 13518fae3551SRodney W. Grimes { 13528fae3551SRodney W. Grimes int hlen; 13538fae3551SRodney W. Grimes u_char *cp; 13548fae3551SRodney W. Grimes 13558fae3551SRodney W. Grimes pr_iph(ip); 13568fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 13578fae3551SRodney W. Grimes cp = (u_char *)ip + hlen; 13588fae3551SRodney W. Grimes 13598fae3551SRodney W. Grimes if (ip->ip_p == 6) 13608fae3551SRodney W. Grimes (void)printf("TCP: from port %u, to port %u (decimal)\n", 13618fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 13628fae3551SRodney W. Grimes else if (ip->ip_p == 17) 13638fae3551SRodney W. Grimes (void)printf("UDP: from port %u, to port %u (decimal)\n", 13648fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 13658fae3551SRodney W. Grimes } 13668fae3551SRodney W. Grimes 136743470e3bSGarrett Wollman static void 13688fae3551SRodney W. Grimes fill(bp, patp) 13698fae3551SRodney W. Grimes char *bp, *patp; 13708fae3551SRodney W. Grimes { 13718fae3551SRodney W. Grimes register int ii, jj, kk; 13728fae3551SRodney W. Grimes int pat[16]; 13738fae3551SRodney W. Grimes char *cp; 13748fae3551SRodney W. Grimes 137543470e3bSGarrett Wollman for (cp = patp; *cp; cp++) { 137643470e3bSGarrett Wollman if (!isxdigit(*cp)) 137743470e3bSGarrett Wollman errx(EX_USAGE, 137843470e3bSGarrett Wollman "patterns must be specified as hex digits"); 137943470e3bSGarrett Wollman 13808fae3551SRodney W. Grimes } 13818fae3551SRodney W. Grimes ii = sscanf(patp, 13828fae3551SRodney W. Grimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 13838fae3551SRodney W. Grimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 13848fae3551SRodney W. Grimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 13858fae3551SRodney W. Grimes &pat[13], &pat[14], &pat[15]); 13868fae3551SRodney W. Grimes 13878fae3551SRodney W. Grimes if (ii > 0) 13888fae3551SRodney W. Grimes for (kk = 0; 1389d32ff037SJohn Birrell kk <= MAXPACKET - (8 + PHDR_LEN + ii); 13908fae3551SRodney W. Grimes kk += ii) 13918fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 13928fae3551SRodney W. Grimes bp[jj + kk] = pat[jj]; 13938fae3551SRodney W. Grimes if (!(options & F_QUIET)) { 13948fae3551SRodney W. Grimes (void)printf("PATTERN: 0x"); 13958fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 13968fae3551SRodney W. Grimes (void)printf("%02x", bp[jj] & 0xFF); 13978fae3551SRodney W. Grimes (void)printf("\n"); 13988fae3551SRodney W. Grimes } 13998fae3551SRodney W. Grimes } 14008fae3551SRodney W. Grimes 140143470e3bSGarrett Wollman static void 1402e345a80dSPhilippe Charnier usage() 14038fae3551SRodney W. Grimes { 140499490edeSWarner Losh fprintf(stderr, "%s\n%s\n%s\n", 1405e345a80dSPhilippe Charnier "usage: ping [-QRadfnqrv] [-c count] [-i wait] [-l preload] [-p pattern]", 14069a4365d0SYoshinobu Inoue " " 14079a4365d0SYoshinobu Inoue #ifdef IPSEC 14089a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 14099a4365d0SYoshinobu Inoue "[-P policy] " 14109a4365d0SYoshinobu Inoue #endif 14119a4365d0SYoshinobu Inoue #endif 14127237fd94SBill Fumerola "[-s packetsize] [-S src_addr] [-t timeout]", 141399490edeSWarner Losh " [host | [-L] [-I iface] [-T ttl] mcast-group]"); 141443470e3bSGarrett Wollman exit(EX_USAGE); 14158fae3551SRodney W. Grimes } 1416