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 4443470e3bSGarrett Wollman /* 458fae3551SRodney W. Grimes static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93"; 4643470e3bSGarrett Wollman */ 4743470e3bSGarrett Wollman static const char rcsid[] = 48d32ff037SJohn Birrell "$Id: ping.c,v 1.33 1998/04/15 19:55:14 phk Exp $"; 498fae3551SRodney W. Grimes #endif /* not lint */ 508fae3551SRodney W. Grimes 518fae3551SRodney W. Grimes /* 528fae3551SRodney W. Grimes * P I N G . C 538fae3551SRodney W. Grimes * 548fae3551SRodney W. Grimes * 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/file.h> 868fae3551SRodney W. Grimes #include <sys/time.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 95d32ff037SJohn Birrell #define PHDR_LEN sizeof(struct timeval) 96d32ff037SJohn Birrell #define DEFDATALEN (64 - PHDR_LEN) /* default data length */ 978f975bb3SBruce Evans #define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */ 988f975bb3SBruce Evans /* runs out of buffer space */ 998fae3551SRodney W. Grimes #define MAXIPLEN 60 1008fae3551SRodney W. Grimes #define MAXICMPLEN 76 1018fae3551SRodney W. Grimes #define MAXPACKET (65536 - 60 - 8)/* max packet size */ 1028fae3551SRodney W. Grimes #define MAXWAIT 10 /* max seconds to wait for response */ 1038fae3551SRodney W. Grimes #define NROUTES 9 /* number of record route slots */ 1048fae3551SRodney W. Grimes 1058fae3551SRodney W. Grimes #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 1068fae3551SRodney W. Grimes #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 1078fae3551SRodney W. Grimes #define SET(bit) (A(bit) |= B(bit)) 1088fae3551SRodney W. Grimes #define CLR(bit) (A(bit) &= (~B(bit))) 1098fae3551SRodney W. Grimes #define TST(bit) (A(bit) & B(bit)) 1108fae3551SRodney W. Grimes 1118fae3551SRodney W. Grimes /* various options */ 1128fae3551SRodney W. Grimes int options; 11385456935SBill Fenner #define F_FLOOD 0x0001 11485456935SBill Fenner #define F_INTERVAL 0x0002 11585456935SBill Fenner #define F_NUMERIC 0x0004 11685456935SBill Fenner #define F_PINGFILLED 0x0008 11785456935SBill Fenner #define F_QUIET 0x0010 11885456935SBill Fenner #define F_RROUTE 0x0020 11985456935SBill Fenner #define F_SO_DEBUG 0x0040 12085456935SBill Fenner #define F_SO_DONTROUTE 0x0080 12185456935SBill Fenner #define F_VERBOSE 0x0100 12285456935SBill Fenner #define F_QUIET2 0x0200 12385456935SBill Fenner #define F_NOLOOP 0x0400 12485456935SBill Fenner #define F_MTTL 0x0800 12585456935SBill Fenner #define F_MIF 0x1000 126772dfa72SDaniel O'Callaghan #define F_AUDIBLE 0x2000 1278fae3551SRodney W. Grimes 1288fae3551SRodney W. Grimes /* 1298fae3551SRodney W. Grimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 1308fae3551SRodney W. Grimes * number of received sequence numbers we can keep track of. Change 128 1318fae3551SRodney W. Grimes * to 8192 for complete accuracy... 1328fae3551SRodney W. Grimes */ 1338fae3551SRodney W. Grimes #define MAX_DUP_CHK (8 * 128) 1348fae3551SRodney W. Grimes int mx_dup_ck = MAX_DUP_CHK; 1358fae3551SRodney W. Grimes char rcvd_tbl[MAX_DUP_CHK / 8]; 1368fae3551SRodney W. Grimes 1378fae3551SRodney W. Grimes struct sockaddr whereto; /* who to ping */ 1388fae3551SRodney W. Grimes int datalen = DEFDATALEN; 1398fae3551SRodney W. Grimes int s; /* socket file descriptor */ 1408fae3551SRodney W. Grimes u_char outpack[MAXPACKET]; 1418fae3551SRodney W. Grimes char BSPACE = '\b'; /* characters written for flood */ 1428fae3551SRodney W. Grimes char DOT = '.'; 1438fae3551SRodney W. Grimes char *hostname; 1448fae3551SRodney W. Grimes int ident; /* process id to identify our packets */ 145ee2bf734SWarner Losh int uid; /* cached uid for micro-optimization */ 1468fae3551SRodney W. Grimes 1478fae3551SRodney W. Grimes /* counters */ 1488fae3551SRodney W. Grimes long npackets; /* max packets to transmit */ 1498fae3551SRodney W. Grimes long nreceived; /* # of packets we got back */ 1508fae3551SRodney W. Grimes long nrepeats; /* number of duplicates */ 1518fae3551SRodney W. Grimes long ntransmitted; /* sequence # for outbound packets = #sent */ 1528fae3551SRodney W. Grimes int interval = 1; /* interval between packets */ 1538fae3551SRodney W. Grimes 1548fae3551SRodney W. Grimes /* timing */ 1558fae3551SRodney W. Grimes int timing; /* flag to do timing */ 1568fae3551SRodney W. Grimes double tmin = 999999999.0; /* minimum round trip time */ 1578fae3551SRodney W. Grimes double tmax = 0.0; /* maximum round trip time */ 1588fae3551SRodney W. Grimes double tsum = 0.0; /* sum of all times, for doing average */ 1593109a910SGarrett Wollman double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 1608fae3551SRodney W. Grimes 1618f975bb3SBruce Evans volatile sig_atomic_t finish_up; /* nonzero if we've been told to finish up */ 162badd8138SSean Eric Fagan int reset_kerninfo; 1638f975bb3SBruce Evans volatile sig_atomic_t siginfo_p; 164badd8138SSean Eric Fagan 16543470e3bSGarrett Wollman static void fill(char *, char *); 16643470e3bSGarrett Wollman static u_short in_cksum(u_short *, int); 16743470e3bSGarrett Wollman static void catcher(int sig); 16843470e3bSGarrett Wollman static void check_status(void); 1698f975bb3SBruce Evans static void finish(void) __dead2; 17043470e3bSGarrett Wollman static void pinger(void); 17143470e3bSGarrett Wollman static char *pr_addr(struct in_addr); 17243470e3bSGarrett Wollman static void pr_icmph(struct icmp *); 17343470e3bSGarrett Wollman static void pr_iph(struct ip *); 17443470e3bSGarrett Wollman static void pr_pack(char *, int, struct sockaddr_in *); 17543470e3bSGarrett Wollman static void pr_retip(struct ip *); 17643470e3bSGarrett Wollman static void status(int); 1778f975bb3SBruce Evans static void stopit(int); 17843470e3bSGarrett Wollman static void tvsub(struct timeval *, struct timeval *); 17943470e3bSGarrett Wollman static void usage(const char *) __dead2; 1808fae3551SRodney W. Grimes 18143470e3bSGarrett Wollman int 1828fae3551SRodney W. Grimes main(argc, argv) 1838fae3551SRodney W. Grimes int argc; 18443470e3bSGarrett Wollman char *const *argv; 1858fae3551SRodney W. Grimes { 1868fae3551SRodney W. Grimes struct timeval timeout; 1878fae3551SRodney W. Grimes struct hostent *hp; 1888fae3551SRodney W. Grimes struct sockaddr_in *to; 189badd8138SSean Eric Fagan struct termios ts; 1908fae3551SRodney W. Grimes register int i; 191f1284d7aSBill Fenner int ch, fdmask, hold, packlen, preload, sockerrno; 19285456935SBill Fenner struct in_addr ifaddr; 19385456935SBill Fenner unsigned char ttl, loop; 1948fae3551SRodney W. Grimes u_char *datap, *packet; 19543470e3bSGarrett Wollman char *target, hnamebuf[MAXHOSTNAMELEN]; 19643470e3bSGarrett Wollman char *ep; 19743470e3bSGarrett Wollman u_long ultmp; 1988fae3551SRodney W. Grimes #ifdef IP_OPTIONS 1998fae3551SRodney W. Grimes char rspace[3 + 4 * NROUTES + 1]; /* record route space */ 2008fae3551SRodney W. Grimes #endif 20137e5b2c6SSean Eric Fagan struct sigaction si_sa; 2028fae3551SRodney W. Grimes 203f1284d7aSBill Fenner /* 204f1284d7aSBill Fenner * Do the stuff that we need root priv's for *first*, and 205f1284d7aSBill Fenner * then drop our setuid bit. Save error reporting for 206f1284d7aSBill Fenner * after arg parsing. 207f1284d7aSBill Fenner */ 20843470e3bSGarrett Wollman s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 209f1284d7aSBill Fenner sockerrno = errno; 210f1284d7aSBill Fenner 211f1284d7aSBill Fenner setuid(getuid()); 212ee2bf734SWarner Losh uid = getuid(); 213f1284d7aSBill Fenner 2148fae3551SRodney W. Grimes preload = 0; 215badd8138SSean Eric Fagan 216d32ff037SJohn Birrell datap = &outpack[8 + PHDR_LEN]; 217f78ac61bSWarner Losh while ((ch = getopt(argc, argv, "I:LQRT:c:adfi:l:np:qrs:v")) != -1) { 2188fae3551SRodney W. Grimes switch(ch) { 219772dfa72SDaniel O'Callaghan case 'a': 220772dfa72SDaniel O'Callaghan options |= F_AUDIBLE; 221772dfa72SDaniel O'Callaghan break; 2228fae3551SRodney W. Grimes case 'c': 22343470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 22443470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp) 22543470e3bSGarrett Wollman errx(EX_USAGE, 22643470e3bSGarrett Wollman "invalid count of packets to transmit: `%s'", 22743470e3bSGarrett Wollman optarg); 22843470e3bSGarrett Wollman npackets = ultmp; 2298fae3551SRodney W. Grimes break; 2308fae3551SRodney W. Grimes case 'd': 2318fae3551SRodney W. Grimes options |= F_SO_DEBUG; 2328fae3551SRodney W. Grimes break; 2338fae3551SRodney W. Grimes case 'f': 2348fae3551SRodney W. Grimes if (getuid()) { 23543470e3bSGarrett Wollman errno = EPERM; 23643470e3bSGarrett Wollman err(EX_NOPERM, "-f flag"); 2378fae3551SRodney W. Grimes } 2388fae3551SRodney W. Grimes options |= F_FLOOD; 2398fae3551SRodney W. Grimes setbuf(stdout, (char *)NULL); 2408fae3551SRodney W. Grimes break; 2418fae3551SRodney W. Grimes case 'i': /* wait between sending packets */ 24243470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 24343470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > INT_MAX) 24443470e3bSGarrett Wollman errx(EX_USAGE, 24543470e3bSGarrett Wollman "invalid timing interval: `%s'", optarg); 2468fae3551SRodney W. Grimes options |= F_INTERVAL; 24743470e3bSGarrett Wollman interval = ultmp; 2488fae3551SRodney W. Grimes break; 24985456935SBill Fenner case 'I': /* multicast interface */ 25043470e3bSGarrett Wollman if (inet_aton(optarg, &ifaddr) == 0) 25143470e3bSGarrett Wollman errx(EX_USAGE, 25243470e3bSGarrett Wollman "invalid multicast interface: `%s'", 25343470e3bSGarrett Wollman optarg); 25485456935SBill Fenner options |= F_MIF; 25585456935SBill Fenner break; 2568fae3551SRodney W. Grimes case 'l': 25743470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 25843470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > INT_MAX) 25943470e3bSGarrett Wollman errx(EX_USAGE, 26043470e3bSGarrett Wollman "invalid preload value: `%s'", optarg); 261f78ac61bSWarner Losh if (getuid()) { 262f78ac61bSWarner Losh errno = EPERM; 263f78ac61bSWarner Losh err(EX_NOPERM, "-l flag"); 264f78ac61bSWarner Losh } 26547b630b9SWarner Losh options |= F_FLOOD; 26643470e3bSGarrett Wollman preload = ultmp; 2678fae3551SRodney W. Grimes break; 26885456935SBill Fenner case 'L': 26985456935SBill Fenner options |= F_NOLOOP; 27085456935SBill Fenner loop = 0; 27185456935SBill Fenner break; 2728fae3551SRodney W. Grimes case 'n': 2738fae3551SRodney W. Grimes options |= F_NUMERIC; 2748fae3551SRodney W. Grimes break; 2758fae3551SRodney W. Grimes case 'p': /* fill buffer with user pattern */ 2768fae3551SRodney W. Grimes options |= F_PINGFILLED; 2778fae3551SRodney W. Grimes fill((char *)datap, optarg); 2788fae3551SRodney W. Grimes break; 279ef9e6dc7SBill Fenner case 'Q': 280ef9e6dc7SBill Fenner options |= F_QUIET2; 281ef9e6dc7SBill Fenner break; 2828fae3551SRodney W. Grimes case 'q': 2838fae3551SRodney W. Grimes options |= F_QUIET; 2848fae3551SRodney W. Grimes break; 2858fae3551SRodney W. Grimes case 'R': 2868fae3551SRodney W. Grimes options |= F_RROUTE; 2878fae3551SRodney W. Grimes break; 2888fae3551SRodney W. Grimes case 'r': 2898fae3551SRodney W. Grimes options |= F_SO_DONTROUTE; 2908fae3551SRodney W. Grimes break; 2918fae3551SRodney W. Grimes case 's': /* size of packet to send */ 29243470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 29343470e3bSGarrett Wollman if (ultmp > MAXPACKET) 29443470e3bSGarrett Wollman errx(EX_USAGE, "packet size too large: %lu", 29543470e3bSGarrett Wollman ultmp); 29643470e3bSGarrett Wollman if (*ep || ep == optarg || !ultmp) 29743470e3bSGarrett Wollman errx(EX_USAGE, "invalid packet size: `%s'", 29843470e3bSGarrett Wollman optarg); 29943470e3bSGarrett Wollman datalen = ultmp; 3008fae3551SRodney W. Grimes break; 30185456935SBill Fenner case 'T': /* multicast TTL */ 30243470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 30343470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > 255) 30443470e3bSGarrett Wollman errx(EX_USAGE, "invalid multicast TTL: `%s'", 30543470e3bSGarrett Wollman optarg); 30643470e3bSGarrett Wollman ttl = ultmp; 30785456935SBill Fenner options |= F_MTTL; 30885456935SBill Fenner break; 3098fae3551SRodney W. Grimes case 'v': 3108fae3551SRodney W. Grimes options |= F_VERBOSE; 3118fae3551SRodney W. Grimes break; 3128fae3551SRodney W. Grimes default: 3138fae3551SRodney W. Grimes 31443470e3bSGarrett Wollman usage(argv[0]); 31543470e3bSGarrett Wollman } 31643470e3bSGarrett Wollman } 31743470e3bSGarrett Wollman 31843470e3bSGarrett Wollman if (argc - optind != 1) 31943470e3bSGarrett Wollman usage(argv[0]); 32043470e3bSGarrett Wollman target = argv[optind]; 3218fae3551SRodney W. Grimes 3228fae3551SRodney W. Grimes bzero((char *)&whereto, sizeof(struct sockaddr)); 3238fae3551SRodney W. Grimes to = (struct sockaddr_in *)&whereto; 3248fae3551SRodney W. Grimes to->sin_family = AF_INET; 32543470e3bSGarrett Wollman if (inet_aton(target, &to->sin_addr) != 0) { 3268fae3551SRodney W. Grimes hostname = target; 32743470e3bSGarrett Wollman } else { 32843470e3bSGarrett Wollman hp = gethostbyname2(target, AF_INET); 32943470e3bSGarrett Wollman if (!hp) 33043470e3bSGarrett Wollman errx(EX_NOHOST, "cannot resolve %s: %s", 33143470e3bSGarrett Wollman target, hstrerror(h_errno)); 33243470e3bSGarrett Wollman 33343470e3bSGarrett Wollman to->sin_len = sizeof *to; 3341ffae4a6SWarner Losh if (hp->h_length > sizeof(to->sin_addr)) 3351ffae4a6SWarner Losh errx(1,"gethostbyname2 returned an illegal address"); 33643470e3bSGarrett Wollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 3378fae3551SRodney W. Grimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 338b10d9d5fSWarner Losh hnamebuf[sizeof(hnamebuf) - 1] = '\0'; 3398fae3551SRodney W. Grimes hostname = hnamebuf; 3408fae3551SRodney W. Grimes } 3418fae3551SRodney W. Grimes 34243470e3bSGarrett Wollman if (options & F_FLOOD && options & F_INTERVAL) 34343470e3bSGarrett Wollman errx(EX_USAGE, "-f and -i: incompatible options"); 34443470e3bSGarrett Wollman 34543470e3bSGarrett Wollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 34643470e3bSGarrett Wollman errx(EX_USAGE, 34743470e3bSGarrett Wollman "-f flag cannot be used with multicast destination"); 34843470e3bSGarrett Wollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 34943470e3bSGarrett Wollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 35043470e3bSGarrett Wollman errx(EX_USAGE, 35143470e3bSGarrett Wollman "-I, -L, -T flags cannot be used with unicast destination"); 3528fae3551SRodney W. Grimes 353d32ff037SJohn Birrell if (datalen >= PHDR_LEN) /* can we time transfer */ 3548fae3551SRodney W. Grimes timing = 1; 3558fae3551SRodney W. Grimes packlen = datalen + MAXIPLEN + MAXICMPLEN; 35643470e3bSGarrett Wollman if (!(packet = (u_char *)malloc((size_t)packlen))) 35743470e3bSGarrett Wollman err(EX_UNAVAILABLE, "malloc"); 35843470e3bSGarrett Wollman 3598fae3551SRodney W. Grimes if (!(options & F_PINGFILLED)) 360d32ff037SJohn Birrell for (i = PHDR_LEN; i < datalen; ++i) 3618fae3551SRodney W. Grimes *datap++ = i; 3628fae3551SRodney W. Grimes 3638fae3551SRodney W. Grimes ident = getpid() & 0xFFFF; 3648fae3551SRodney W. Grimes 365f1284d7aSBill Fenner if (s < 0) { 366f1284d7aSBill Fenner errno = sockerrno; 36743470e3bSGarrett Wollman err(EX_OSERR, "socket"); 3688fae3551SRodney W. Grimes } 3698fae3551SRodney W. Grimes hold = 1; 3708fae3551SRodney W. Grimes if (options & F_SO_DEBUG) 3718fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold, 3728fae3551SRodney W. Grimes sizeof(hold)); 3738fae3551SRodney W. Grimes if (options & F_SO_DONTROUTE) 3748fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 3758fae3551SRodney W. Grimes sizeof(hold)); 3768fae3551SRodney W. Grimes 3778fae3551SRodney W. Grimes /* record route option */ 3788fae3551SRodney W. Grimes if (options & F_RROUTE) { 3798fae3551SRodney W. Grimes #ifdef IP_OPTIONS 3808fae3551SRodney W. Grimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 3818fae3551SRodney W. Grimes rspace[IPOPT_OLEN] = sizeof(rspace)-1; 3828fae3551SRodney W. Grimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 3838fae3551SRodney W. Grimes if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace, 38443470e3bSGarrett Wollman sizeof(rspace)) < 0) 38543470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 3868fae3551SRodney W. Grimes #else 38743470e3bSGarrett Wollman errx(EX_UNAVAILABLE, 38843470e3bSGarrett Wollman "record route not available in this implementation"); 3898fae3551SRodney W. Grimes #endif /* IP_OPTIONS */ 3908fae3551SRodney W. Grimes } 3918fae3551SRodney W. Grimes 39285456935SBill Fenner if (options & F_NOLOOP) { 39385456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 39485456935SBill Fenner sizeof(loop)) < 0) { 39543470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 39685456935SBill Fenner } 39785456935SBill Fenner } 39885456935SBill Fenner if (options & F_MTTL) { 39985456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, 40085456935SBill Fenner sizeof(ttl)) < 0) { 40143470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 40285456935SBill Fenner } 40385456935SBill Fenner } 40485456935SBill Fenner if (options & F_MIF) { 40585456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 40685456935SBill Fenner sizeof(ifaddr)) < 0) { 40743470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 40885456935SBill Fenner } 40985456935SBill Fenner } 41085456935SBill Fenner 4118fae3551SRodney W. Grimes /* 4128fae3551SRodney W. Grimes * When pinging the broadcast address, you can get a lot of answers. 4138fae3551SRodney W. Grimes * Doing something so evil is useful if you are trying to stress the 4148fae3551SRodney W. Grimes * ethernet, or just want to fill the arp cache to get some stuff for 41543470e3bSGarrett Wollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 41643470e3bSGarrett Wollman * or multicast pings if they wish. 4178fae3551SRodney W. Grimes */ 4188fae3551SRodney W. Grimes hold = 48 * 1024; 4198fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 4208fae3551SRodney W. Grimes sizeof(hold)); 4218fae3551SRodney W. Grimes 4228fae3551SRodney W. Grimes if (to->sin_family == AF_INET) 4238fae3551SRodney W. Grimes (void)printf("PING %s (%s): %d data bytes\n", hostname, 42443470e3bSGarrett Wollman inet_ntoa(to->sin_addr), 4258fae3551SRodney W. Grimes datalen); 4268fae3551SRodney W. Grimes else 4278fae3551SRodney W. Grimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 4288fae3551SRodney W. Grimes 4297d81b35cSBruce Evans /* 430a2a00888SSean Eric Fagan * Use sigaction() instead of signal() to get unambiguous semantics, 431a2a00888SSean Eric Fagan * in particular with SA_RESTART not set. 4327d81b35cSBruce Evans */ 433a2a00888SSean Eric Fagan 434f6bd468bSPaul Traina sigemptyset(&si_sa.sa_mask); 43537e5b2c6SSean Eric Fagan si_sa.sa_flags = 0; 436a2a00888SSean Eric Fagan 437a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 438a2a00888SSean Eric Fagan if (sigaction(SIGINT, &si_sa, 0) == -1) { 439a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGINT"); 440a2a00888SSean Eric Fagan } 441a2a00888SSean Eric Fagan 442a2a00888SSean Eric Fagan si_sa.sa_handler = catcher; 443a2a00888SSean Eric Fagan if (sigaction(SIGALRM, &si_sa, 0) == -1) { 444a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGALRM"); 445a2a00888SSean Eric Fagan } 446a2a00888SSean Eric Fagan 447a2a00888SSean Eric Fagan si_sa.sa_handler = status; 44837e5b2c6SSean Eric Fagan if (sigaction(SIGINFO, &si_sa, 0) == -1) { 449624ff938SWarner Losh err(EX_OSERR, "sigaction"); 45037e5b2c6SSean Eric Fagan } 45137e5b2c6SSean Eric Fagan 4524055611aSSean Eric Fagan if (tcgetattr(STDOUT_FILENO, &ts) != -1) { 4534055611aSSean Eric Fagan reset_kerninfo = !(ts.c_lflag & NOKERNINFO); 4544055611aSSean Eric Fagan ts.c_lflag |= NOKERNINFO; 4554055611aSSean Eric Fagan tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 4564055611aSSean Eric Fagan } 4574055611aSSean Eric Fagan 4588fae3551SRodney W. Grimes while (preload--) /* fire off them quickies */ 4598fae3551SRodney W. Grimes pinger(); 4608fae3551SRodney W. Grimes 4618fae3551SRodney W. Grimes if ((options & F_FLOOD) == 0) 46243470e3bSGarrett Wollman catcher(0); /* start things going */ 4638fae3551SRodney W. Grimes 4648f975bb3SBruce Evans while (!finish_up) { 4658fae3551SRodney W. Grimes struct sockaddr_in from; 4668fae3551SRodney W. Grimes register int cc; 4678fae3551SRodney W. Grimes int fromlen; 4688fae3551SRodney W. Grimes 4697d81b35cSBruce Evans check_status(); 4708fae3551SRodney W. Grimes if (options & F_FLOOD) { 4718fae3551SRodney W. Grimes pinger(); 4728fae3551SRodney W. Grimes timeout.tv_sec = 0; 4738fae3551SRodney W. Grimes timeout.tv_usec = 10000; 4748fae3551SRodney W. Grimes fdmask = 1 << s; 4758fae3551SRodney W. Grimes if (select(s + 1, (fd_set *)&fdmask, (fd_set *)NULL, 4768fae3551SRodney W. Grimes (fd_set *)NULL, &timeout) < 1) 4778fae3551SRodney W. Grimes continue; 4788fae3551SRodney W. Grimes } 4798fae3551SRodney W. Grimes fromlen = sizeof(from); 4808fae3551SRodney W. Grimes if ((cc = recvfrom(s, (char *)packet, packlen, 0, 4818fae3551SRodney W. Grimes (struct sockaddr *)&from, &fromlen)) < 0) { 4828fae3551SRodney W. Grimes if (errno == EINTR) 4838fae3551SRodney W. Grimes continue; 4848fae3551SRodney W. Grimes perror("ping: recvfrom"); 4858fae3551SRodney W. Grimes continue; 4868fae3551SRodney W. Grimes } 4878fae3551SRodney W. Grimes pr_pack((char *)packet, cc, &from); 4888fae3551SRodney W. Grimes if (npackets && nreceived >= npackets) 4898fae3551SRodney W. Grimes break; 4908fae3551SRodney W. Grimes } 4918f975bb3SBruce Evans finish(); 4928fae3551SRodney W. Grimes /* NOTREACHED */ 493f78ac61bSWarner Losh exit(0); /* Make the compiler happy */ 4948fae3551SRodney W. Grimes } 4958fae3551SRodney W. Grimes 4968fae3551SRodney W. Grimes /* 4978f975bb3SBruce Evans * stopit -- 4988f975bb3SBruce Evans * Set the global bit that causes the main loop to quit. 4998f975bb3SBruce Evans * Do NOT call finish() from here, since finish() does far too much 5008f975bb3SBruce Evans * to be called from a signal handler. 501515dd2faSJulian Elischer */ 502515dd2faSJulian Elischer void 5038f975bb3SBruce Evans stopit(sig) 5048f975bb3SBruce Evans int sig; 505515dd2faSJulian Elischer { 506515dd2faSJulian Elischer finish_up = 1; 507515dd2faSJulian Elischer } 508515dd2faSJulian Elischer 509515dd2faSJulian Elischer /* 5108fae3551SRodney W. Grimes * catcher -- 5118fae3551SRodney W. Grimes * This routine causes another PING to be transmitted, and then 5128fae3551SRodney W. Grimes * schedules another SIGALRM for 1 second from now. 5138fae3551SRodney W. Grimes * 5148fae3551SRodney W. Grimes * bug -- 5158fae3551SRodney W. Grimes * Our sense of time will slowly skew (i.e., packets will not be 5168fae3551SRodney W. Grimes * launched exactly at 1-second intervals). This does not affect the 5178fae3551SRodney W. Grimes * quality of the delay and loss statistics. 5188fae3551SRodney W. Grimes */ 51943470e3bSGarrett Wollman static void 52043470e3bSGarrett Wollman catcher(int sig) 5218fae3551SRodney W. Grimes { 5228fae3551SRodney W. Grimes int waittime; 523a2a00888SSean Eric Fagan struct sigaction si_sa; 5248fae3551SRodney W. Grimes 5258fae3551SRodney W. Grimes pinger(); 526a2a00888SSean Eric Fagan 5278fae3551SRodney W. Grimes if (!npackets || ntransmitted < npackets) 528a2a00888SSean Eric Fagan (void)alarm((u_int)interval); 5298fae3551SRodney W. Grimes else { 5308fae3551SRodney W. Grimes if (nreceived) { 5318fae3551SRodney W. Grimes waittime = 2 * tmax / 1000; 5328fae3551SRodney W. Grimes if (!waittime) 5338fae3551SRodney W. Grimes waittime = 1; 5348fae3551SRodney W. Grimes } else 5358fae3551SRodney W. Grimes waittime = MAXWAIT; 536a2a00888SSean Eric Fagan 537a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 538a2a00888SSean Eric Fagan sigemptyset(&si_sa.sa_mask); 539a2a00888SSean Eric Fagan si_sa.sa_flags = 0; 540a2a00888SSean Eric Fagan if (sigaction(SIGALRM, &si_sa, 0) == -1) { 541a2a00888SSean Eric Fagan finish_up = 1; 542a2a00888SSean Eric Fagan return; 543a2a00888SSean Eric Fagan } 5448fae3551SRodney W. Grimes (void)alarm((u_int)waittime); 5458fae3551SRodney W. Grimes } 5468fae3551SRodney W. Grimes } 5478fae3551SRodney W. Grimes 5488fae3551SRodney W. Grimes /* 5498fae3551SRodney W. Grimes * pinger -- 5508fae3551SRodney W. Grimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 5518fae3551SRodney W. Grimes * will be added on by the kernel. The ID field is our UNIX process ID, 5528fae3551SRodney W. Grimes * and the sequence number is an ascending integer. The first 8 bytes 553ef9e6dc7SBill Fenner * of the data portion are used to hold a UNIX "timeval" struct in host 5548fae3551SRodney W. Grimes * byte-order, to compute the round-trip time. 5558f975bb3SBruce Evans * 5568f975bb3SBruce Evans * bug -- 5578f975bb3SBruce Evans * this does far too much to be called from a signal handler. 5588fae3551SRodney W. Grimes */ 55943470e3bSGarrett Wollman static void 56043470e3bSGarrett Wollman pinger(void) 5618fae3551SRodney W. Grimes { 5628fae3551SRodney W. Grimes register struct icmp *icp; 5638fae3551SRodney W. Grimes register int cc; 5648fae3551SRodney W. Grimes int i; 5658fae3551SRodney W. Grimes 5668fae3551SRodney W. Grimes icp = (struct icmp *)outpack; 5678fae3551SRodney W. Grimes icp->icmp_type = ICMP_ECHO; 5688fae3551SRodney W. Grimes icp->icmp_code = 0; 5698fae3551SRodney W. Grimes icp->icmp_cksum = 0; 5700e59c641SJulian Elischer icp->icmp_seq = ntransmitted; 5718fae3551SRodney W. Grimes icp->icmp_id = ident; /* ID */ 5728fae3551SRodney W. Grimes 5738fae3551SRodney W. Grimes CLR(icp->icmp_seq % mx_dup_ck); 5748fae3551SRodney W. Grimes 5758fae3551SRodney W. Grimes if (timing) 5768fae3551SRodney W. Grimes (void)gettimeofday((struct timeval *)&outpack[8], 5778fae3551SRodney W. Grimes (struct timezone *)NULL); 5788fae3551SRodney W. Grimes 579d32ff037SJohn Birrell cc = datalen + PHDR_LEN; /* skips ICMP portion */ 5808fae3551SRodney W. Grimes 5818fae3551SRodney W. Grimes /* compute ICMP checksum here */ 5828fae3551SRodney W. Grimes icp->icmp_cksum = in_cksum((u_short *)icp, cc); 5838fae3551SRodney W. Grimes 5848fae3551SRodney W. Grimes i = sendto(s, (char *)outpack, cc, 0, &whereto, 5858fae3551SRodney W. Grimes sizeof(struct sockaddr)); 5868fae3551SRodney W. Grimes 5878fae3551SRodney W. Grimes if (i < 0 || i != cc) { 58843470e3bSGarrett Wollman if (i < 0) { 5898f975bb3SBruce Evans if (options & F_FLOOD && errno == ENOBUFS) { 590515dd2faSJulian Elischer usleep(FLOOD_BACKOFF); 591515dd2faSJulian Elischer return; 592515dd2faSJulian Elischer } 59343470e3bSGarrett Wollman warn("sendto"); 59443470e3bSGarrett Wollman } else { 59543470e3bSGarrett Wollman warn("%s: partial write: %d of %d bytes", 596416aa49bSPoul-Henning Kamp hostname, i, cc); 5978fae3551SRodney W. Grimes } 598363d7bbeSJulian Elischer } 599363d7bbeSJulian Elischer ntransmitted++; 6008fae3551SRodney W. Grimes if (!(options & F_QUIET) && options & F_FLOOD) 6018fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &DOT, 1); 6028fae3551SRodney W. Grimes } 6038fae3551SRodney W. Grimes 6048fae3551SRodney W. Grimes /* 6058fae3551SRodney W. Grimes * pr_pack -- 6068fae3551SRodney W. Grimes * Print out the packet, if it came from us. This logic is necessary 6078fae3551SRodney W. Grimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 6088fae3551SRodney W. Grimes * which arrive ('tis only fair). This permits multiple copies of this 6098fae3551SRodney W. Grimes * program to be run without having intermingled output (or statistics!). 6108fae3551SRodney W. Grimes */ 61143470e3bSGarrett Wollman static void 6128fae3551SRodney W. Grimes pr_pack(buf, cc, from) 6138fae3551SRodney W. Grimes char *buf; 6148fae3551SRodney W. Grimes int cc; 6158fae3551SRodney W. Grimes struct sockaddr_in *from; 6168fae3551SRodney W. Grimes { 6178fae3551SRodney W. Grimes register struct icmp *icp; 6188fae3551SRodney W. Grimes register u_long l; 6198fae3551SRodney W. Grimes register int i, j; 6208fae3551SRodney W. Grimes register u_char *cp,*dp; 6218fae3551SRodney W. Grimes static int old_rrlen; 6228fae3551SRodney W. Grimes static char old_rr[MAX_IPOPTLEN]; 6238fae3551SRodney W. Grimes struct ip *ip; 6248fae3551SRodney W. Grimes struct timeval tv, *tp; 6258f975bb3SBruce Evans double triptime; 6268fae3551SRodney W. Grimes int hlen, dupflag; 6278fae3551SRodney W. Grimes 6288fae3551SRodney W. Grimes (void)gettimeofday(&tv, (struct timezone *)NULL); 6298fae3551SRodney W. Grimes 6308fae3551SRodney W. Grimes /* Check the IP header */ 6318fae3551SRodney W. Grimes ip = (struct ip *)buf; 6328fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 6338fae3551SRodney W. Grimes if (cc < hlen + ICMP_MINLEN) { 6348fae3551SRodney W. Grimes if (options & F_VERBOSE) 63543470e3bSGarrett Wollman warn("packet too short (%d bytes) from %s", cc, 63643470e3bSGarrett Wollman inet_ntoa(from->sin_addr)); 6378fae3551SRodney W. Grimes return; 6388fae3551SRodney W. Grimes } 6398fae3551SRodney W. Grimes 6408fae3551SRodney W. Grimes /* Now the ICMP part */ 6418fae3551SRodney W. Grimes cc -= hlen; 6428fae3551SRodney W. Grimes icp = (struct icmp *)(buf + hlen); 6438fae3551SRodney W. Grimes if (icp->icmp_type == ICMP_ECHOREPLY) { 6448fae3551SRodney W. Grimes if (icp->icmp_id != ident) 6458fae3551SRodney W. Grimes return; /* 'Twas not our ECHO */ 6468fae3551SRodney W. Grimes ++nreceived; 6478f975bb3SBruce Evans triptime = 0.0; 6488fae3551SRodney W. Grimes if (timing) { 649d32ff037SJohn Birrell struct timeval tv1; 6508fae3551SRodney W. Grimes #ifndef icmp_data 6518fae3551SRodney W. Grimes tp = (struct timeval *)&icp->icmp_ip; 6528fae3551SRodney W. Grimes #else 6538fae3551SRodney W. Grimes tp = (struct timeval *)icp->icmp_data; 6548fae3551SRodney W. Grimes #endif 655d32ff037SJohn Birrell /* Avoid unaligned data: */ 656d32ff037SJohn Birrell memcpy(&tv1,tp,sizeof(tv1)); 657d32ff037SJohn Birrell tvsub(&tv, &tv1); 6588fae3551SRodney W. Grimes triptime = ((double)tv.tv_sec) * 1000.0 + 6598fae3551SRodney W. Grimes ((double)tv.tv_usec) / 1000.0; 6608fae3551SRodney W. Grimes tsum += triptime; 6613109a910SGarrett Wollman tsumsq += triptime * triptime; 6628fae3551SRodney W. Grimes if (triptime < tmin) 6638fae3551SRodney W. Grimes tmin = triptime; 6648fae3551SRodney W. Grimes if (triptime > tmax) 6658fae3551SRodney W. Grimes tmax = triptime; 6668fae3551SRodney W. Grimes } 6678fae3551SRodney W. Grimes 6688fae3551SRodney W. Grimes if (TST(icp->icmp_seq % mx_dup_ck)) { 6698fae3551SRodney W. Grimes ++nrepeats; 6708fae3551SRodney W. Grimes --nreceived; 6718fae3551SRodney W. Grimes dupflag = 1; 6728fae3551SRodney W. Grimes } else { 6738fae3551SRodney W. Grimes SET(icp->icmp_seq % mx_dup_ck); 6748fae3551SRodney W. Grimes dupflag = 0; 6758fae3551SRodney W. Grimes } 6768fae3551SRodney W. Grimes 6778fae3551SRodney W. Grimes if (options & F_QUIET) 6788fae3551SRodney W. Grimes return; 6798fae3551SRodney W. Grimes 6808fae3551SRodney W. Grimes if (options & F_FLOOD) 6818fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &BSPACE, 1); 6828fae3551SRodney W. Grimes else { 6838fae3551SRodney W. Grimes (void)printf("%d bytes from %s: icmp_seq=%u", cc, 6848fae3551SRodney W. Grimes inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr), 6858fae3551SRodney W. Grimes icp->icmp_seq); 6868fae3551SRodney W. Grimes (void)printf(" ttl=%d", ip->ip_ttl); 6878fae3551SRodney W. Grimes if (timing) 688d410b6f1SDavid Greenman (void)printf(" time=%.3f ms", triptime); 6898fae3551SRodney W. Grimes if (dupflag) 6908fae3551SRodney W. Grimes (void)printf(" (DUP!)"); 691772dfa72SDaniel O'Callaghan if (options & F_AUDIBLE) 692772dfa72SDaniel O'Callaghan (void)printf("\a"); 6938fae3551SRodney W. Grimes /* check the data */ 694d32ff037SJohn Birrell cp = (u_char*)&icp->icmp_data[PHDR_LEN]; 695d32ff037SJohn Birrell dp = &outpack[8 + PHDR_LEN]; 696d32ff037SJohn Birrell for (i = PHDR_LEN; i < datalen; ++i, ++cp, ++dp) { 6978fae3551SRodney W. Grimes if (*cp != *dp) { 6988fae3551SRodney W. Grimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 6998fae3551SRodney W. Grimes i, *dp, *cp); 700d32ff037SJohn Birrell printf("\ncp:"); 7018fae3551SRodney W. Grimes cp = (u_char*)&icp->icmp_data[0]; 702d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 703d32ff037SJohn Birrell if ((i % 32) == 8) 704d32ff037SJohn Birrell (void)printf("\n\t"); 705d32ff037SJohn Birrell (void)printf("%x ", *cp); 706d32ff037SJohn Birrell } 707d32ff037SJohn Birrell printf("\ndp:"); 708d32ff037SJohn Birrell cp = &outpack[8]; 709d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 7108fae3551SRodney W. Grimes if ((i % 32) == 8) 7118fae3551SRodney W. Grimes (void)printf("\n\t"); 7128fae3551SRodney W. Grimes (void)printf("%x ", *cp); 7138fae3551SRodney W. Grimes } 7148fae3551SRodney W. Grimes break; 7158fae3551SRodney W. Grimes } 7168fae3551SRodney W. Grimes } 7178fae3551SRodney W. Grimes } 7188fae3551SRodney W. Grimes } else { 719ef9e6dc7SBill Fenner /* 720ef9e6dc7SBill Fenner * We've got something other than an ECHOREPLY. 721ef9e6dc7SBill Fenner * See if it's a reply to something that we sent. 722ef9e6dc7SBill Fenner * We can compare IP destination, protocol, 723ef9e6dc7SBill Fenner * and ICMP type and ID. 724f78ac61bSWarner Losh * 725f78ac61bSWarner Losh * Only print all the error messages if we are running 726f78ac61bSWarner Losh * as root to avoid leaking information not normally 727f78ac61bSWarner Losh * available to those not running as root. 728ef9e6dc7SBill Fenner */ 729ef9e6dc7SBill Fenner #ifndef icmp_data 730ef9e6dc7SBill Fenner struct ip *oip = &icp->icmp_ip; 731ef9e6dc7SBill Fenner #else 732ef9e6dc7SBill Fenner struct ip *oip = (struct ip *)icp->icmp_data; 733ef9e6dc7SBill Fenner #endif 734ef9e6dc7SBill Fenner struct icmp *oicmp = (struct icmp *)(oip + 1); 735ef9e6dc7SBill Fenner 736ee2bf734SWarner Losh if (((options & F_VERBOSE) && uid == 0) || 737ef9e6dc7SBill Fenner (!(options & F_QUIET2) && 738ef9e6dc7SBill Fenner (oip->ip_dst.s_addr == 739ef9e6dc7SBill Fenner ((struct sockaddr_in *)&whereto)->sin_addr.s_addr) && 740ef9e6dc7SBill Fenner (oip->ip_p == IPPROTO_ICMP) && 741ef9e6dc7SBill Fenner (oicmp->icmp_type == ICMP_ECHO) && 742ef9e6dc7SBill Fenner (oicmp->icmp_id == ident))) { 7438fae3551SRodney W. Grimes (void)printf("%d bytes from %s: ", cc, 74443470e3bSGarrett Wollman pr_addr(from->sin_addr)); 7458fae3551SRodney W. Grimes pr_icmph(icp); 746ef9e6dc7SBill Fenner } else 747ef9e6dc7SBill Fenner return; 7488fae3551SRodney W. Grimes } 7498fae3551SRodney W. Grimes 7508fae3551SRodney W. Grimes /* Display any IP options */ 7518fae3551SRodney W. Grimes cp = (u_char *)buf + sizeof(struct ip); 7528fae3551SRodney W. Grimes 7538fae3551SRodney W. Grimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 7548fae3551SRodney W. Grimes switch (*cp) { 7558fae3551SRodney W. Grimes case IPOPT_EOL: 7568fae3551SRodney W. Grimes hlen = 0; 7578fae3551SRodney W. Grimes break; 7588fae3551SRodney W. Grimes case IPOPT_LSRR: 7598fae3551SRodney W. Grimes (void)printf("\nLSRR: "); 7608fae3551SRodney W. Grimes hlen -= 2; 7618fae3551SRodney W. Grimes j = *++cp; 7628fae3551SRodney W. Grimes ++cp; 7638fae3551SRodney W. Grimes if (j > IPOPT_MINOFF) 7648fae3551SRodney W. Grimes for (;;) { 7658fae3551SRodney W. Grimes l = *++cp; 7668fae3551SRodney W. Grimes l = (l<<8) + *++cp; 7678fae3551SRodney W. Grimes l = (l<<8) + *++cp; 7688fae3551SRodney W. Grimes l = (l<<8) + *++cp; 76943470e3bSGarrett Wollman if (l == 0) { 77043470e3bSGarrett Wollman printf("\t0.0.0.0"); 77143470e3bSGarrett Wollman } else { 77243470e3bSGarrett Wollman struct in_addr ina; 77343470e3bSGarrett Wollman ina.s_addr = ntohl(l); 77443470e3bSGarrett Wollman printf("\t%s", pr_addr(ina)); 77543470e3bSGarrett Wollman } 7768fae3551SRodney W. Grimes hlen -= 4; 7778fae3551SRodney W. Grimes j -= 4; 7788fae3551SRodney W. Grimes if (j <= IPOPT_MINOFF) 7798fae3551SRodney W. Grimes break; 7808fae3551SRodney W. Grimes (void)putchar('\n'); 7818fae3551SRodney W. Grimes } 7828fae3551SRodney W. Grimes break; 7838fae3551SRodney W. Grimes case IPOPT_RR: 7848fae3551SRodney W. Grimes j = *++cp; /* get length */ 7858fae3551SRodney W. Grimes i = *++cp; /* and pointer */ 7868fae3551SRodney W. Grimes hlen -= 2; 7878fae3551SRodney W. Grimes if (i > j) 7888fae3551SRodney W. Grimes i = j; 7898fae3551SRodney W. Grimes i -= IPOPT_MINOFF; 7908fae3551SRodney W. Grimes if (i <= 0) 7918fae3551SRodney W. Grimes continue; 7928fae3551SRodney W. Grimes if (i == old_rrlen 7938fae3551SRodney W. Grimes && cp == (u_char *)buf + sizeof(struct ip) + 2 7948fae3551SRodney W. Grimes && !bcmp((char *)cp, old_rr, i) 7958fae3551SRodney W. Grimes && !(options & F_FLOOD)) { 7968fae3551SRodney W. Grimes (void)printf("\t(same route)"); 7978fae3551SRodney W. Grimes i = ((i + 3) / 4) * 4; 7988fae3551SRodney W. Grimes hlen -= i; 7998fae3551SRodney W. Grimes cp += i; 8008fae3551SRodney W. Grimes break; 8018fae3551SRodney W. Grimes } 8023aa4b744SEivind Eklund if (i < MAX_IPOPTLEN) { 8038fae3551SRodney W. Grimes old_rrlen = i; 8048fae3551SRodney W. Grimes bcopy((char *)cp, old_rr, i); 805c03e877aSWarner Losh } else 806c03e877aSWarner Losh old_rrlen = 0; 807c03e877aSWarner Losh 8088fae3551SRodney W. Grimes (void)printf("\nRR: "); 809c03e877aSWarner Losh j = 0; 8108fae3551SRodney W. Grimes for (;;) { 8118fae3551SRodney W. Grimes l = *++cp; 8128fae3551SRodney W. Grimes l = (l<<8) + *++cp; 8138fae3551SRodney W. Grimes l = (l<<8) + *++cp; 8148fae3551SRodney W. Grimes l = (l<<8) + *++cp; 81543470e3bSGarrett Wollman if (l == 0) { 81643470e3bSGarrett Wollman printf("\t0.0.0.0"); 81743470e3bSGarrett Wollman } else { 81843470e3bSGarrett Wollman struct in_addr ina; 81943470e3bSGarrett Wollman ina.s_addr = ntohl(l); 82043470e3bSGarrett Wollman printf("\t%s", pr_addr(ina)); 82143470e3bSGarrett Wollman } 8228fae3551SRodney W. Grimes hlen -= 4; 8238fae3551SRodney W. Grimes i -= 4; 824c03e877aSWarner Losh j += 4; 8258fae3551SRodney W. Grimes if (i <= 0) 8268fae3551SRodney W. Grimes break; 827c03e877aSWarner Losh if (j >= MAX_IPOPTLEN) { 828c03e877aSWarner Losh (void) printf("\t(truncated route)"); 829c03e877aSWarner Losh break; 830c03e877aSWarner Losh } 8318fae3551SRodney W. Grimes (void)putchar('\n'); 8328fae3551SRodney W. Grimes } 8338fae3551SRodney W. Grimes break; 8348fae3551SRodney W. Grimes case IPOPT_NOP: 8358fae3551SRodney W. Grimes (void)printf("\nNOP"); 8368fae3551SRodney W. Grimes break; 8378fae3551SRodney W. Grimes default: 8388fae3551SRodney W. Grimes (void)printf("\nunknown option %x", *cp); 8398fae3551SRodney W. Grimes break; 8408fae3551SRodney W. Grimes } 8418fae3551SRodney W. Grimes if (!(options & F_FLOOD)) { 8428fae3551SRodney W. Grimes (void)putchar('\n'); 8438fae3551SRodney W. Grimes (void)fflush(stdout); 8448fae3551SRodney W. Grimes } 8458fae3551SRodney W. Grimes } 8468fae3551SRodney W. Grimes 8478fae3551SRodney W. Grimes /* 8488fae3551SRodney W. Grimes * in_cksum -- 8498fae3551SRodney W. Grimes * Checksum routine for Internet Protocol family headers (C Version) 8508fae3551SRodney W. Grimes */ 85143470e3bSGarrett Wollman u_short 8528fae3551SRodney W. Grimes in_cksum(addr, len) 8538fae3551SRodney W. Grimes u_short *addr; 8548fae3551SRodney W. Grimes int len; 8558fae3551SRodney W. Grimes { 8568fae3551SRodney W. Grimes register int nleft = len; 8578fae3551SRodney W. Grimes register u_short *w = addr; 8588fae3551SRodney W. Grimes register int sum = 0; 8598fae3551SRodney W. Grimes u_short answer = 0; 8608fae3551SRodney W. Grimes 8618fae3551SRodney W. Grimes /* 8628fae3551SRodney W. Grimes * Our algorithm is simple, using a 32 bit accumulator (sum), we add 8638fae3551SRodney W. Grimes * sequential 16 bit words to it, and at the end, fold back all the 8648fae3551SRodney W. Grimes * carry bits from the top 16 bits into the lower 16 bits. 8658fae3551SRodney W. Grimes */ 8668fae3551SRodney W. Grimes while (nleft > 1) { 8678fae3551SRodney W. Grimes sum += *w++; 8688fae3551SRodney W. Grimes nleft -= 2; 8698fae3551SRodney W. Grimes } 8708fae3551SRodney W. Grimes 8718fae3551SRodney W. Grimes /* mop up an odd byte, if necessary */ 8728fae3551SRodney W. Grimes if (nleft == 1) { 8738fae3551SRodney W. Grimes *(u_char *)(&answer) = *(u_char *)w ; 8748fae3551SRodney W. Grimes sum += answer; 8758fae3551SRodney W. Grimes } 8768fae3551SRodney W. Grimes 8778fae3551SRodney W. Grimes /* add back carry outs from top 16 bits to low 16 bits */ 8788fae3551SRodney W. Grimes sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 8798fae3551SRodney W. Grimes sum += (sum >> 16); /* add carry */ 8808fae3551SRodney W. Grimes answer = ~sum; /* truncate to 16 bits */ 8818fae3551SRodney W. Grimes return(answer); 8828fae3551SRodney W. Grimes } 8838fae3551SRodney W. Grimes 8848fae3551SRodney W. Grimes /* 8858fae3551SRodney W. Grimes * tvsub -- 8868fae3551SRodney W. Grimes * Subtract 2 timeval structs: out = out - in. Out is assumed to 8878fae3551SRodney W. Grimes * be >= in. 8888fae3551SRodney W. Grimes */ 88943470e3bSGarrett Wollman static void 8908fae3551SRodney W. Grimes tvsub(out, in) 8918fae3551SRodney W. Grimes register struct timeval *out, *in; 8928fae3551SRodney W. Grimes { 8938fae3551SRodney W. Grimes if ((out->tv_usec -= in->tv_usec) < 0) { 8948fae3551SRodney W. Grimes --out->tv_sec; 8958fae3551SRodney W. Grimes out->tv_usec += 1000000; 8968fae3551SRodney W. Grimes } 8978fae3551SRodney W. Grimes out->tv_sec -= in->tv_sec; 8988fae3551SRodney W. Grimes } 8998fae3551SRodney W. Grimes 9008fae3551SRodney W. Grimes /* 901badd8138SSean Eric Fagan * status -- 902badd8138SSean Eric Fagan * Print out statistics when SIGINFO is received. 903badd8138SSean Eric Fagan */ 904badd8138SSean Eric Fagan 90543470e3bSGarrett Wollman static void 9067d81b35cSBruce Evans status(sig) 9077d81b35cSBruce Evans int sig; 9087d81b35cSBruce Evans { 90937e5b2c6SSean Eric Fagan siginfo_p = 1; 91037e5b2c6SSean Eric Fagan } 91137e5b2c6SSean Eric Fagan 91243470e3bSGarrett Wollman static void 91337e5b2c6SSean Eric Fagan check_status() 914badd8138SSean Eric Fagan { 91537e5b2c6SSean Eric Fagan if (siginfo_p) { 91637e5b2c6SSean Eric Fagan siginfo_p = 0; 9177d81b35cSBruce Evans (void)fprintf(stderr, 9187d81b35cSBruce Evans "\r%ld/%ld packets received (%.0f%%) %.3f min / %.3f avg / %.3f max\n", 9197d81b35cSBruce Evans nreceived, ntransmitted, 9207d81b35cSBruce Evans ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0, 9217d81b35cSBruce Evans nreceived ? tmin : 0.0, 9227d81b35cSBruce Evans nreceived + nrepeats ? tsum / (nreceived + nrepeats) : tsum, 9237d81b35cSBruce Evans tmax); 92437e5b2c6SSean Eric Fagan } 925badd8138SSean Eric Fagan } 926badd8138SSean Eric Fagan 927badd8138SSean Eric Fagan /* 9288fae3551SRodney W. Grimes * finish -- 9298fae3551SRodney W. Grimes * Print out statistics, and give up. 9308fae3551SRodney W. Grimes */ 93143470e3bSGarrett Wollman static void 9328f975bb3SBruce Evans finish() 9338fae3551SRodney W. Grimes { 934badd8138SSean Eric Fagan struct termios ts; 9358fae3551SRodney W. Grimes 9368fae3551SRodney W. Grimes (void)signal(SIGINT, SIG_IGN); 937a2a00888SSean Eric Fagan (void)signal(SIGALRM, SIG_IGN); 9388fae3551SRodney W. Grimes (void)putchar('\n'); 9398fae3551SRodney W. Grimes (void)fflush(stdout); 9408fae3551SRodney W. Grimes (void)printf("--- %s ping statistics ---\n", hostname); 9418fae3551SRodney W. Grimes (void)printf("%ld packets transmitted, ", ntransmitted); 9428fae3551SRodney W. Grimes (void)printf("%ld packets received, ", nreceived); 9438fae3551SRodney W. Grimes if (nrepeats) 9448fae3551SRodney W. Grimes (void)printf("+%ld duplicates, ", nrepeats); 9458fae3551SRodney W. Grimes if (ntransmitted) 9468fae3551SRodney W. Grimes if (nreceived > ntransmitted) 9478fae3551SRodney W. Grimes (void)printf("-- somebody's printing up packets!"); 9488fae3551SRodney W. Grimes else 9498fae3551SRodney W. Grimes (void)printf("%d%% packet loss", 9508fae3551SRodney W. Grimes (int) (((ntransmitted - nreceived) * 100) / 9518fae3551SRodney W. Grimes ntransmitted)); 9528fae3551SRodney W. Grimes (void)putchar('\n'); 9533109a910SGarrett Wollman if (nreceived && timing) { 9543109a910SGarrett Wollman double n = nreceived + nrepeats; 9553109a910SGarrett Wollman double avg = tsum / n; 9563109a910SGarrett Wollman double vari = tsumsq / n - avg * avg; 9573109a910SGarrett Wollman printf("round-trip min/avg/max/stddev = " 9583109a910SGarrett Wollman "%.3f/%.3f/%.3f/%.3f ms\n", 9593109a910SGarrett Wollman tmin, avg, tmax, sqrt(vari)); 9603109a910SGarrett Wollman } 9612ceaae21SBruce Evans if (reset_kerninfo && tcgetattr(STDOUT_FILENO, &ts) != -1) { 962badd8138SSean Eric Fagan ts.c_lflag &= ~NOKERNINFO; 9632ceaae21SBruce Evans tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 964badd8138SSean Eric Fagan } 965badd8138SSean Eric Fagan 9666e1173dcSDavid Greenman if (nreceived) 9678fae3551SRodney W. Grimes exit(0); 9686e1173dcSDavid Greenman else 9696e1173dcSDavid Greenman exit(2); 9708fae3551SRodney W. Grimes } 9718fae3551SRodney W. Grimes 9728fae3551SRodney W. Grimes #ifdef notdef 9738fae3551SRodney W. Grimes static char *ttab[] = { 9748fae3551SRodney W. Grimes "Echo Reply", /* ip + seq + udata */ 9758fae3551SRodney W. Grimes "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */ 9768fae3551SRodney W. Grimes "Source Quench", /* IP */ 9778fae3551SRodney W. Grimes "Redirect", /* redirect type, gateway, + IP */ 9788fae3551SRodney W. Grimes "Echo", 9798fae3551SRodney W. Grimes "Time Exceeded", /* transit, frag reassem + IP */ 9808fae3551SRodney W. Grimes "Parameter Problem", /* pointer + IP */ 9818fae3551SRodney W. Grimes "Timestamp", /* id + seq + three timestamps */ 9828fae3551SRodney W. Grimes "Timestamp Reply", /* " */ 9838fae3551SRodney W. Grimes "Info Request", /* id + sq */ 9848fae3551SRodney W. Grimes "Info Reply" /* " */ 9858fae3551SRodney W. Grimes }; 9868fae3551SRodney W. Grimes #endif 9878fae3551SRodney W. Grimes 9888fae3551SRodney W. Grimes /* 9898fae3551SRodney W. Grimes * pr_icmph -- 9908fae3551SRodney W. Grimes * Print a descriptive string about an ICMP header. 9918fae3551SRodney W. Grimes */ 99243470e3bSGarrett Wollman static void 9938fae3551SRodney W. Grimes pr_icmph(icp) 9948fae3551SRodney W. Grimes struct icmp *icp; 9958fae3551SRodney W. Grimes { 9968fae3551SRodney W. Grimes switch(icp->icmp_type) { 9978fae3551SRodney W. Grimes case ICMP_ECHOREPLY: 9988fae3551SRodney W. Grimes (void)printf("Echo Reply\n"); 9998fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 10008fae3551SRodney W. Grimes break; 10018fae3551SRodney W. Grimes case ICMP_UNREACH: 10028fae3551SRodney W. Grimes switch(icp->icmp_code) { 10038fae3551SRodney W. Grimes case ICMP_UNREACH_NET: 10048fae3551SRodney W. Grimes (void)printf("Destination Net Unreachable\n"); 10058fae3551SRodney W. Grimes break; 10068fae3551SRodney W. Grimes case ICMP_UNREACH_HOST: 10078fae3551SRodney W. Grimes (void)printf("Destination Host Unreachable\n"); 10088fae3551SRodney W. Grimes break; 10098fae3551SRodney W. Grimes case ICMP_UNREACH_PROTOCOL: 10108fae3551SRodney W. Grimes (void)printf("Destination Protocol Unreachable\n"); 10118fae3551SRodney W. Grimes break; 10128fae3551SRodney W. Grimes case ICMP_UNREACH_PORT: 10138fae3551SRodney W. Grimes (void)printf("Destination Port Unreachable\n"); 10148fae3551SRodney W. Grimes break; 10158fae3551SRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 1016ef9e6dc7SBill Fenner (void)printf("frag needed and DF set (MTU %d)\n", 1017ff49597eSBill Fenner ntohs(icp->icmp_nextmtu)); 10188fae3551SRodney W. Grimes break; 10198fae3551SRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 10208fae3551SRodney W. Grimes (void)printf("Source Route Failed\n"); 10218fae3551SRodney W. Grimes break; 1022ef9e6dc7SBill Fenner case ICMP_UNREACH_FILTER_PROHIB: 1023ef9e6dc7SBill Fenner (void)printf("Communication prohibited by filter\n"); 1024ef9e6dc7SBill Fenner break; 10258fae3551SRodney W. Grimes default: 10268fae3551SRodney W. Grimes (void)printf("Dest Unreachable, Bad Code: %d\n", 10278fae3551SRodney W. Grimes icp->icmp_code); 10288fae3551SRodney W. Grimes break; 10298fae3551SRodney W. Grimes } 10308fae3551SRodney W. Grimes /* Print returned IP header information */ 10318fae3551SRodney W. Grimes #ifndef icmp_data 10328fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 10338fae3551SRodney W. Grimes #else 10348fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 10358fae3551SRodney W. Grimes #endif 10368fae3551SRodney W. Grimes break; 10378fae3551SRodney W. Grimes case ICMP_SOURCEQUENCH: 10388fae3551SRodney W. Grimes (void)printf("Source Quench\n"); 10398fae3551SRodney W. Grimes #ifndef icmp_data 10408fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 10418fae3551SRodney W. Grimes #else 10428fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 10438fae3551SRodney W. Grimes #endif 10448fae3551SRodney W. Grimes break; 10458fae3551SRodney W. Grimes case ICMP_REDIRECT: 10468fae3551SRodney W. Grimes switch(icp->icmp_code) { 10478fae3551SRodney W. Grimes case ICMP_REDIRECT_NET: 10488fae3551SRodney W. Grimes (void)printf("Redirect Network"); 10498fae3551SRodney W. Grimes break; 10508fae3551SRodney W. Grimes case ICMP_REDIRECT_HOST: 10518fae3551SRodney W. Grimes (void)printf("Redirect Host"); 10528fae3551SRodney W. Grimes break; 10538fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSNET: 10548fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Network"); 10558fae3551SRodney W. Grimes break; 10568fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSHOST: 10578fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Host"); 10588fae3551SRodney W. Grimes break; 10598fae3551SRodney W. Grimes default: 10608fae3551SRodney W. Grimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 10618fae3551SRodney W. Grimes break; 10628fae3551SRodney W. Grimes } 1063ff49597eSBill Fenner (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 10648fae3551SRodney W. Grimes #ifndef icmp_data 10658fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 10668fae3551SRodney W. Grimes #else 10678fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 10688fae3551SRodney W. Grimes #endif 10698fae3551SRodney W. Grimes break; 10708fae3551SRodney W. Grimes case ICMP_ECHO: 10718fae3551SRodney W. Grimes (void)printf("Echo Request\n"); 10728fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 10738fae3551SRodney W. Grimes break; 10748fae3551SRodney W. Grimes case ICMP_TIMXCEED: 10758fae3551SRodney W. Grimes switch(icp->icmp_code) { 10768fae3551SRodney W. Grimes case ICMP_TIMXCEED_INTRANS: 10778fae3551SRodney W. Grimes (void)printf("Time to live exceeded\n"); 10788fae3551SRodney W. Grimes break; 10798fae3551SRodney W. Grimes case ICMP_TIMXCEED_REASS: 10808fae3551SRodney W. Grimes (void)printf("Frag reassembly time exceeded\n"); 10818fae3551SRodney W. Grimes break; 10828fae3551SRodney W. Grimes default: 10838fae3551SRodney W. Grimes (void)printf("Time exceeded, Bad Code: %d\n", 10848fae3551SRodney W. Grimes icp->icmp_code); 10858fae3551SRodney W. Grimes break; 10868fae3551SRodney W. Grimes } 10878fae3551SRodney W. Grimes #ifndef icmp_data 10888fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 10898fae3551SRodney W. Grimes #else 10908fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 10918fae3551SRodney W. Grimes #endif 10928fae3551SRodney W. Grimes break; 10938fae3551SRodney W. Grimes case ICMP_PARAMPROB: 10948fae3551SRodney W. Grimes (void)printf("Parameter problem: pointer = 0x%02x\n", 10958fae3551SRodney W. Grimes icp->icmp_hun.ih_pptr); 10968fae3551SRodney W. Grimes #ifndef icmp_data 10978fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 10988fae3551SRodney W. Grimes #else 10998fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 11008fae3551SRodney W. Grimes #endif 11018fae3551SRodney W. Grimes break; 11028fae3551SRodney W. Grimes case ICMP_TSTAMP: 11038fae3551SRodney W. Grimes (void)printf("Timestamp\n"); 11048fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 11058fae3551SRodney W. Grimes break; 11068fae3551SRodney W. Grimes case ICMP_TSTAMPREPLY: 11078fae3551SRodney W. Grimes (void)printf("Timestamp Reply\n"); 11088fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 11098fae3551SRodney W. Grimes break; 11108fae3551SRodney W. Grimes case ICMP_IREQ: 11118fae3551SRodney W. Grimes (void)printf("Information Request\n"); 11128fae3551SRodney W. Grimes /* XXX ID + Seq */ 11138fae3551SRodney W. Grimes break; 11148fae3551SRodney W. Grimes case ICMP_IREQREPLY: 11158fae3551SRodney W. Grimes (void)printf("Information Reply\n"); 11168fae3551SRodney W. Grimes /* XXX ID + Seq */ 11178fae3551SRodney W. Grimes break; 11188fae3551SRodney W. Grimes case ICMP_MASKREQ: 11198fae3551SRodney W. Grimes (void)printf("Address Mask Request\n"); 11208fae3551SRodney W. Grimes break; 11218fae3551SRodney W. Grimes case ICMP_MASKREPLY: 11228fae3551SRodney W. Grimes (void)printf("Address Mask Reply\n"); 11238fae3551SRodney W. Grimes break; 1124ef9e6dc7SBill Fenner case ICMP_ROUTERADVERT: 1125ef9e6dc7SBill Fenner (void)printf("Router Advertisement\n"); 1126ef9e6dc7SBill Fenner break; 1127ef9e6dc7SBill Fenner case ICMP_ROUTERSOLICIT: 1128ef9e6dc7SBill Fenner (void)printf("Router Solicitation\n"); 1129ef9e6dc7SBill Fenner break; 11308fae3551SRodney W. Grimes default: 11318fae3551SRodney W. Grimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 11328fae3551SRodney W. Grimes } 11338fae3551SRodney W. Grimes } 11348fae3551SRodney W. Grimes 11358fae3551SRodney W. Grimes /* 11368fae3551SRodney W. Grimes * pr_iph -- 11378fae3551SRodney W. Grimes * Print an IP header with options. 11388fae3551SRodney W. Grimes */ 113943470e3bSGarrett Wollman static void 11408fae3551SRodney W. Grimes pr_iph(ip) 11418fae3551SRodney W. Grimes struct ip *ip; 11428fae3551SRodney W. Grimes { 11438fae3551SRodney W. Grimes int hlen; 11448fae3551SRodney W. Grimes u_char *cp; 11458fae3551SRodney W. Grimes 11468fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 11478fae3551SRodney W. Grimes cp = (u_char *)ip + 20; /* point to options */ 11488fae3551SRodney W. Grimes 1149ef9e6dc7SBill Fenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 11508fae3551SRodney W. Grimes (void)printf(" %1x %1x %02x %04x %04x", 1151ef9e6dc7SBill Fenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1152ef9e6dc7SBill Fenner ntohs(ip->ip_id)); 1153d32ff037SJohn Birrell (void)printf(" %1lx %04lx", 1154d32ff037SJohn Birrell (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13, 1155d32ff037SJohn Birrell (u_long) ntohl(ip->ip_off) & 0x1fff); 1156ef9e6dc7SBill Fenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1157ef9e6dc7SBill Fenner ntohs(ip->ip_sum)); 11588fae3551SRodney W. Grimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr)); 11598fae3551SRodney W. Grimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr)); 1160ef9e6dc7SBill Fenner /* dump any option bytes */ 11618fae3551SRodney W. Grimes while (hlen-- > 20) { 11628fae3551SRodney W. Grimes (void)printf("%02x", *cp++); 11638fae3551SRodney W. Grimes } 11648fae3551SRodney W. Grimes (void)putchar('\n'); 11658fae3551SRodney W. Grimes } 11668fae3551SRodney W. Grimes 11678fae3551SRodney W. Grimes /* 11688fae3551SRodney W. Grimes * pr_addr -- 11698fae3551SRodney W. Grimes * Return an ascii host address as a dotted quad and optionally with 11708fae3551SRodney W. Grimes * a hostname. 11718fae3551SRodney W. Grimes */ 117243470e3bSGarrett Wollman static char * 117343470e3bSGarrett Wollman pr_addr(ina) 117443470e3bSGarrett Wollman struct in_addr ina; 11758fae3551SRodney W. Grimes { 11768fae3551SRodney W. Grimes struct hostent *hp; 1177f78ac61bSWarner Losh static char buf[16 + 3 + MAXHOSTNAMELEN]; 11788fae3551SRodney W. Grimes 11798fae3551SRodney W. Grimes if ((options & F_NUMERIC) || 118043470e3bSGarrett Wollman !(hp = gethostbyaddr((char *)&ina, 4, AF_INET))) 118143470e3bSGarrett Wollman return inet_ntoa(ina); 11828fae3551SRodney W. Grimes else 1183efa38539SPeter Wemm (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 118443470e3bSGarrett Wollman inet_ntoa(ina)); 11858fae3551SRodney W. Grimes return(buf); 11868fae3551SRodney W. Grimes } 11878fae3551SRodney W. Grimes 11888fae3551SRodney W. Grimes /* 11898fae3551SRodney W. Grimes * pr_retip -- 11908fae3551SRodney W. Grimes * Dump some info on a returned (via ICMP) IP packet. 11918fae3551SRodney W. Grimes */ 119243470e3bSGarrett Wollman static void 11938fae3551SRodney W. Grimes pr_retip(ip) 11948fae3551SRodney W. Grimes struct ip *ip; 11958fae3551SRodney W. Grimes { 11968fae3551SRodney W. Grimes int hlen; 11978fae3551SRodney W. Grimes u_char *cp; 11988fae3551SRodney W. Grimes 11998fae3551SRodney W. Grimes pr_iph(ip); 12008fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 12018fae3551SRodney W. Grimes cp = (u_char *)ip + hlen; 12028fae3551SRodney W. Grimes 12038fae3551SRodney W. Grimes if (ip->ip_p == 6) 12048fae3551SRodney W. Grimes (void)printf("TCP: from port %u, to port %u (decimal)\n", 12058fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 12068fae3551SRodney W. Grimes else if (ip->ip_p == 17) 12078fae3551SRodney W. Grimes (void)printf("UDP: from port %u, to port %u (decimal)\n", 12088fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 12098fae3551SRodney W. Grimes } 12108fae3551SRodney W. Grimes 121143470e3bSGarrett Wollman static void 12128fae3551SRodney W. Grimes fill(bp, patp) 12138fae3551SRodney W. Grimes char *bp, *patp; 12148fae3551SRodney W. Grimes { 12158fae3551SRodney W. Grimes register int ii, jj, kk; 12168fae3551SRodney W. Grimes int pat[16]; 12178fae3551SRodney W. Grimes char *cp; 12188fae3551SRodney W. Grimes 121943470e3bSGarrett Wollman for (cp = patp; *cp; cp++) { 122043470e3bSGarrett Wollman if (!isxdigit(*cp)) 122143470e3bSGarrett Wollman errx(EX_USAGE, 122243470e3bSGarrett Wollman "patterns must be specified as hex digits"); 122343470e3bSGarrett Wollman 12248fae3551SRodney W. Grimes } 12258fae3551SRodney W. Grimes ii = sscanf(patp, 12268fae3551SRodney W. Grimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 12278fae3551SRodney W. Grimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 12288fae3551SRodney W. Grimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 12298fae3551SRodney W. Grimes &pat[13], &pat[14], &pat[15]); 12308fae3551SRodney W. Grimes 12318fae3551SRodney W. Grimes if (ii > 0) 12328fae3551SRodney W. Grimes for (kk = 0; 1233d32ff037SJohn Birrell kk <= MAXPACKET - (8 + PHDR_LEN + ii); 12348fae3551SRodney W. Grimes kk += ii) 12358fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 12368fae3551SRodney W. Grimes bp[jj + kk] = pat[jj]; 12378fae3551SRodney W. Grimes if (!(options & F_QUIET)) { 12388fae3551SRodney W. Grimes (void)printf("PATTERN: 0x"); 12398fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 12408fae3551SRodney W. Grimes (void)printf("%02x", bp[jj] & 0xFF); 12418fae3551SRodney W. Grimes (void)printf("\n"); 12428fae3551SRodney W. Grimes } 12438fae3551SRodney W. Grimes } 12448fae3551SRodney W. Grimes 124543470e3bSGarrett Wollman static void 124643470e3bSGarrett Wollman usage(argv0) 124743470e3bSGarrett Wollman const char *argv0; 12488fae3551SRodney W. Grimes { 1249f78ac61bSWarner Losh if (strrchr(argv0,'/')) 1250f78ac61bSWarner Losh argv0 = strrchr(argv0,'/') + 1; 125143470e3bSGarrett Wollman fprintf(stderr, 1252f78ac61bSWarner Losh "usage: %s [-QRadfnqrv] [-c count] [-i wait] [-l preload] " 1253ee2bf734SWarner Losh "[-p pattern]\n [-s packetsize] " 125443470e3bSGarrett Wollman "[host | [-L] [-I iface] [-T ttl] mcast-group]\n", 125543470e3bSGarrett Wollman argv0); 125643470e3bSGarrett Wollman exit(EX_USAGE); 12578fae3551SRodney W. Grimes } 1258