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[] = 48363d7bbeSJulian Elischer "$Id: ping.c,v 1.26 1997/07/20 06:09:55 bde 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 958fae3551SRodney W. Grimes #define DEFDATALEN (64 - 8) /* default data length */ 968f975bb3SBruce Evans #define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */ 978f975bb3SBruce Evans /* runs out of buffer space */ 988fae3551SRodney W. Grimes #define MAXIPLEN 60 998fae3551SRodney W. Grimes #define MAXICMPLEN 76 1008fae3551SRodney W. Grimes #define MAXPACKET (65536 - 60 - 8)/* max packet size */ 1018fae3551SRodney W. Grimes #define MAXWAIT 10 /* max seconds to wait for response */ 1028fae3551SRodney W. Grimes #define NROUTES 9 /* number of record route slots */ 1038fae3551SRodney W. Grimes 1048fae3551SRodney W. Grimes #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 1058fae3551SRodney W. Grimes #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 1068fae3551SRodney W. Grimes #define SET(bit) (A(bit) |= B(bit)) 1078fae3551SRodney W. Grimes #define CLR(bit) (A(bit) &= (~B(bit))) 1088fae3551SRodney W. Grimes #define TST(bit) (A(bit) & B(bit)) 1098fae3551SRodney W. Grimes 1108fae3551SRodney W. Grimes /* various options */ 1118fae3551SRodney W. Grimes int options; 11285456935SBill Fenner #define F_FLOOD 0x0001 11385456935SBill Fenner #define F_INTERVAL 0x0002 11485456935SBill Fenner #define F_NUMERIC 0x0004 11585456935SBill Fenner #define F_PINGFILLED 0x0008 11685456935SBill Fenner #define F_QUIET 0x0010 11785456935SBill Fenner #define F_RROUTE 0x0020 11885456935SBill Fenner #define F_SO_DEBUG 0x0040 11985456935SBill Fenner #define F_SO_DONTROUTE 0x0080 12085456935SBill Fenner #define F_VERBOSE 0x0100 12185456935SBill Fenner #define F_QUIET2 0x0200 12285456935SBill Fenner #define F_NOLOOP 0x0400 12385456935SBill Fenner #define F_MTTL 0x0800 12485456935SBill Fenner #define F_MIF 0x1000 125772dfa72SDaniel O'Callaghan #define F_AUDIBLE 0x2000 1268fae3551SRodney W. Grimes 1278fae3551SRodney W. Grimes /* 1288fae3551SRodney W. Grimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 1298fae3551SRodney W. Grimes * number of received sequence numbers we can keep track of. Change 128 1308fae3551SRodney W. Grimes * to 8192 for complete accuracy... 1318fae3551SRodney W. Grimes */ 1328fae3551SRodney W. Grimes #define MAX_DUP_CHK (8 * 128) 1338fae3551SRodney W. Grimes int mx_dup_ck = MAX_DUP_CHK; 1348fae3551SRodney W. Grimes char rcvd_tbl[MAX_DUP_CHK / 8]; 1358fae3551SRodney W. Grimes 1368fae3551SRodney W. Grimes struct sockaddr whereto; /* who to ping */ 1378fae3551SRodney W. Grimes int datalen = DEFDATALEN; 1388fae3551SRodney W. Grimes int s; /* socket file descriptor */ 1398fae3551SRodney W. Grimes u_char outpack[MAXPACKET]; 1408fae3551SRodney W. Grimes char BSPACE = '\b'; /* characters written for flood */ 1418fae3551SRodney W. Grimes char DOT = '.'; 1428fae3551SRodney W. Grimes char *hostname; 1438fae3551SRodney W. Grimes int ident; /* process id to identify our packets */ 144ee2bf734SWarner Losh int uid; /* cached uid for micro-optimization */ 1458fae3551SRodney W. Grimes 1468fae3551SRodney W. Grimes /* counters */ 1478fae3551SRodney W. Grimes long npackets; /* max packets to transmit */ 1488fae3551SRodney W. Grimes long nreceived; /* # of packets we got back */ 1498fae3551SRodney W. Grimes long nrepeats; /* number of duplicates */ 1508fae3551SRodney W. Grimes long ntransmitted; /* sequence # for outbound packets = #sent */ 1518fae3551SRodney W. Grimes int interval = 1; /* interval between packets */ 1528fae3551SRodney W. Grimes 1538fae3551SRodney W. Grimes /* timing */ 1548fae3551SRodney W. Grimes int timing; /* flag to do timing */ 1558fae3551SRodney W. Grimes double tmin = 999999999.0; /* minimum round trip time */ 1568fae3551SRodney W. Grimes double tmax = 0.0; /* maximum round trip time */ 1578fae3551SRodney W. Grimes double tsum = 0.0; /* sum of all times, for doing average */ 1583109a910SGarrett Wollman double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 1598fae3551SRodney W. Grimes 1608f975bb3SBruce Evans volatile sig_atomic_t finish_up; /* nonzero if we've been told to finish up */ 161badd8138SSean Eric Fagan int reset_kerninfo; 1628f975bb3SBruce Evans volatile sig_atomic_t siginfo_p; 163badd8138SSean Eric Fagan 16443470e3bSGarrett Wollman static void fill(char *, char *); 16543470e3bSGarrett Wollman static u_short in_cksum(u_short *, int); 16643470e3bSGarrett Wollman static void catcher(int sig); 16743470e3bSGarrett Wollman static void check_status(void); 1688f975bb3SBruce Evans static void finish(void) __dead2; 16943470e3bSGarrett Wollman static void pinger(void); 17043470e3bSGarrett Wollman static char *pr_addr(struct in_addr); 17143470e3bSGarrett Wollman static void pr_icmph(struct icmp *); 17243470e3bSGarrett Wollman static void pr_iph(struct ip *); 17343470e3bSGarrett Wollman static void pr_pack(char *, int, struct sockaddr_in *); 17443470e3bSGarrett Wollman static void pr_retip(struct ip *); 17543470e3bSGarrett Wollman static void status(int); 1768f975bb3SBruce Evans static void stopit(int); 17743470e3bSGarrett Wollman static void tvsub(struct timeval *, struct timeval *); 17843470e3bSGarrett Wollman static void usage(const char *) __dead2; 1798fae3551SRodney W. Grimes 18043470e3bSGarrett Wollman int 1818fae3551SRodney W. Grimes main(argc, argv) 1828fae3551SRodney W. Grimes int argc; 18343470e3bSGarrett Wollman char *const *argv; 1848fae3551SRodney W. Grimes { 1858fae3551SRodney W. Grimes struct timeval timeout; 1868fae3551SRodney W. Grimes struct hostent *hp; 1878fae3551SRodney W. Grimes struct sockaddr_in *to; 188badd8138SSean Eric Fagan struct termios ts; 1898fae3551SRodney W. Grimes register int i; 190f1284d7aSBill Fenner int ch, fdmask, hold, packlen, preload, sockerrno; 19185456935SBill Fenner struct in_addr ifaddr; 19285456935SBill Fenner unsigned char ttl, loop; 1938fae3551SRodney W. Grimes u_char *datap, *packet; 19443470e3bSGarrett Wollman char *target, hnamebuf[MAXHOSTNAMELEN]; 19543470e3bSGarrett Wollman char *ep; 19643470e3bSGarrett Wollman u_long ultmp; 1978fae3551SRodney W. Grimes #ifdef IP_OPTIONS 1988fae3551SRodney W. Grimes char rspace[3 + 4 * NROUTES + 1]; /* record route space */ 1998fae3551SRodney W. Grimes #endif 20037e5b2c6SSean Eric Fagan struct sigaction si_sa; 2018fae3551SRodney W. Grimes 202f1284d7aSBill Fenner /* 203f1284d7aSBill Fenner * Do the stuff that we need root priv's for *first*, and 204f1284d7aSBill Fenner * then drop our setuid bit. Save error reporting for 205f1284d7aSBill Fenner * after arg parsing. 206f1284d7aSBill Fenner */ 20743470e3bSGarrett Wollman s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 208f1284d7aSBill Fenner sockerrno = errno; 209f1284d7aSBill Fenner 210f1284d7aSBill Fenner setuid(getuid()); 211ee2bf734SWarner Losh uid = getuid(); 212f1284d7aSBill Fenner 2138fae3551SRodney W. Grimes preload = 0; 214badd8138SSean Eric Fagan 2158fae3551SRodney W. Grimes datap = &outpack[8 + sizeof(struct timeval)]; 216f78ac61bSWarner Losh while ((ch = getopt(argc, argv, "I:LQRT:c:adfi:l:np:qrs:v")) != -1) { 2178fae3551SRodney W. Grimes switch(ch) { 218772dfa72SDaniel O'Callaghan case 'a': 219772dfa72SDaniel O'Callaghan options |= F_AUDIBLE; 220772dfa72SDaniel O'Callaghan break; 2218fae3551SRodney W. Grimes case 'c': 22243470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 22343470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp) 22443470e3bSGarrett Wollman errx(EX_USAGE, 22543470e3bSGarrett Wollman "invalid count of packets to transmit: `%s'", 22643470e3bSGarrett Wollman optarg); 22743470e3bSGarrett Wollman npackets = ultmp; 2288fae3551SRodney W. Grimes break; 2298fae3551SRodney W. Grimes case 'd': 2308fae3551SRodney W. Grimes options |= F_SO_DEBUG; 2318fae3551SRodney W. Grimes break; 2328fae3551SRodney W. Grimes case 'f': 2338fae3551SRodney W. Grimes if (getuid()) { 23443470e3bSGarrett Wollman errno = EPERM; 23543470e3bSGarrett Wollman err(EX_NOPERM, "-f flag"); 2368fae3551SRodney W. Grimes } 2378fae3551SRodney W. Grimes options |= F_FLOOD; 2388fae3551SRodney W. Grimes setbuf(stdout, (char *)NULL); 2398fae3551SRodney W. Grimes break; 2408fae3551SRodney W. Grimes case 'i': /* wait between sending packets */ 24143470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 24243470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > INT_MAX) 24343470e3bSGarrett Wollman errx(EX_USAGE, 24443470e3bSGarrett Wollman "invalid timing interval: `%s'", optarg); 2458fae3551SRodney W. Grimes options |= F_INTERVAL; 24643470e3bSGarrett Wollman interval = ultmp; 2478fae3551SRodney W. Grimes break; 24885456935SBill Fenner case 'I': /* multicast interface */ 24943470e3bSGarrett Wollman if (inet_aton(optarg, &ifaddr) == 0) 25043470e3bSGarrett Wollman errx(EX_USAGE, 25143470e3bSGarrett Wollman "invalid multicast interface: `%s'", 25243470e3bSGarrett Wollman optarg); 25385456935SBill Fenner options |= F_MIF; 25485456935SBill Fenner break; 2558fae3551SRodney W. Grimes case 'l': 25643470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 25743470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > INT_MAX) 25843470e3bSGarrett Wollman errx(EX_USAGE, 25943470e3bSGarrett Wollman "invalid preload value: `%s'", optarg); 260f78ac61bSWarner Losh if (getuid()) { 261f78ac61bSWarner Losh errno = EPERM; 262f78ac61bSWarner Losh err(EX_NOPERM, "-l flag"); 263f78ac61bSWarner Losh } 264f78ac61bSWarner Losh options |= F_FLOOD; 26543470e3bSGarrett Wollman preload = ultmp; 2668fae3551SRodney W. Grimes break; 26785456935SBill Fenner case 'L': 26885456935SBill Fenner options |= F_NOLOOP; 26985456935SBill Fenner loop = 0; 27085456935SBill Fenner break; 2718fae3551SRodney W. Grimes case 'n': 2728fae3551SRodney W. Grimes options |= F_NUMERIC; 2738fae3551SRodney W. Grimes break; 2748fae3551SRodney W. Grimes case 'p': /* fill buffer with user pattern */ 2758fae3551SRodney W. Grimes options |= F_PINGFILLED; 2768fae3551SRodney W. Grimes fill((char *)datap, optarg); 2778fae3551SRodney W. Grimes break; 278ef9e6dc7SBill Fenner case 'Q': 279ef9e6dc7SBill Fenner options |= F_QUIET2; 280ef9e6dc7SBill Fenner break; 2818fae3551SRodney W. Grimes case 'q': 2828fae3551SRodney W. Grimes options |= F_QUIET; 2838fae3551SRodney W. Grimes break; 2848fae3551SRodney W. Grimes case 'R': 2858fae3551SRodney W. Grimes options |= F_RROUTE; 2868fae3551SRodney W. Grimes break; 2878fae3551SRodney W. Grimes case 'r': 2888fae3551SRodney W. Grimes options |= F_SO_DONTROUTE; 2898fae3551SRodney W. Grimes break; 2908fae3551SRodney W. Grimes case 's': /* size of packet to send */ 29143470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 29243470e3bSGarrett Wollman if (ultmp > MAXPACKET) 29343470e3bSGarrett Wollman errx(EX_USAGE, "packet size too large: %lu", 29443470e3bSGarrett Wollman ultmp); 29543470e3bSGarrett Wollman if (*ep || ep == optarg || !ultmp) 29643470e3bSGarrett Wollman errx(EX_USAGE, "invalid packet size: `%s'", 29743470e3bSGarrett Wollman optarg); 29843470e3bSGarrett Wollman datalen = ultmp; 2998fae3551SRodney W. Grimes break; 30085456935SBill Fenner case 'T': /* multicast TTL */ 30143470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 30243470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > 255) 30343470e3bSGarrett Wollman errx(EX_USAGE, "invalid multicast TTL: `%s'", 30443470e3bSGarrett Wollman optarg); 30543470e3bSGarrett Wollman ttl = ultmp; 30685456935SBill Fenner options |= F_MTTL; 30785456935SBill Fenner break; 3088fae3551SRodney W. Grimes case 'v': 3098fae3551SRodney W. Grimes options |= F_VERBOSE; 3108fae3551SRodney W. Grimes break; 3118fae3551SRodney W. Grimes default: 3128fae3551SRodney W. Grimes 31343470e3bSGarrett Wollman usage(argv[0]); 31443470e3bSGarrett Wollman } 31543470e3bSGarrett Wollman } 31643470e3bSGarrett Wollman 31743470e3bSGarrett Wollman if (argc - optind != 1) 31843470e3bSGarrett Wollman usage(argv[0]); 31943470e3bSGarrett Wollman target = argv[optind]; 3208fae3551SRodney W. Grimes 3218fae3551SRodney W. Grimes bzero((char *)&whereto, sizeof(struct sockaddr)); 3228fae3551SRodney W. Grimes to = (struct sockaddr_in *)&whereto; 3238fae3551SRodney W. Grimes to->sin_family = AF_INET; 32443470e3bSGarrett Wollman if (inet_aton(target, &to->sin_addr) != 0) { 3258fae3551SRodney W. Grimes hostname = target; 32643470e3bSGarrett Wollman } else { 32743470e3bSGarrett Wollman hp = gethostbyname2(target, AF_INET); 32843470e3bSGarrett Wollman if (!hp) 32943470e3bSGarrett Wollman errx(EX_NOHOST, "cannot resolve %s: %s", 33043470e3bSGarrett Wollman target, hstrerror(h_errno)); 33143470e3bSGarrett Wollman 33243470e3bSGarrett Wollman to->sin_len = sizeof *to; 3331ffae4a6SWarner Losh if (hp->h_length > sizeof(to->sin_addr)) 3341ffae4a6SWarner Losh errx(1,"gethostbyname2 returned an illegal address"); 33543470e3bSGarrett Wollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 3368fae3551SRodney W. Grimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 33743470e3bSGarrett Wollman hnamebuf[(sizeof hnamebuf) - 1] = '\0'; 3388fae3551SRodney W. Grimes hostname = hnamebuf; 3398fae3551SRodney W. Grimes } 3408fae3551SRodney W. Grimes 34143470e3bSGarrett Wollman if (options & F_FLOOD && options & F_INTERVAL) 34243470e3bSGarrett Wollman errx(EX_USAGE, "-f and -i: incompatible options"); 34343470e3bSGarrett Wollman 34443470e3bSGarrett Wollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 34543470e3bSGarrett Wollman errx(EX_USAGE, 34643470e3bSGarrett Wollman "-f flag cannot be used with multicast destination"); 34743470e3bSGarrett Wollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 34843470e3bSGarrett Wollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 34943470e3bSGarrett Wollman errx(EX_USAGE, 35043470e3bSGarrett Wollman "-I, -L, -T flags cannot be used with unicast destination"); 3518fae3551SRodney W. Grimes 3528fae3551SRodney W. Grimes if (datalen >= sizeof(struct timeval)) /* can we time transfer */ 3538fae3551SRodney W. Grimes timing = 1; 3548fae3551SRodney W. Grimes packlen = datalen + MAXIPLEN + MAXICMPLEN; 35543470e3bSGarrett Wollman if (!(packet = (u_char *)malloc((size_t)packlen))) 35643470e3bSGarrett Wollman err(EX_UNAVAILABLE, "malloc"); 35743470e3bSGarrett Wollman 3588fae3551SRodney W. Grimes if (!(options & F_PINGFILLED)) 3598fae3551SRodney W. Grimes for (i = 8; i < datalen; ++i) 3608fae3551SRodney W. Grimes *datap++ = i; 3618fae3551SRodney W. Grimes 3628fae3551SRodney W. Grimes ident = getpid() & 0xFFFF; 3638fae3551SRodney W. Grimes 364f1284d7aSBill Fenner if (s < 0) { 365f1284d7aSBill Fenner errno = sockerrno; 36643470e3bSGarrett Wollman err(EX_OSERR, "socket"); 3678fae3551SRodney W. Grimes } 3688fae3551SRodney W. Grimes hold = 1; 3698fae3551SRodney W. Grimes if (options & F_SO_DEBUG) 3708fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold, 3718fae3551SRodney W. Grimes sizeof(hold)); 3728fae3551SRodney W. Grimes if (options & F_SO_DONTROUTE) 3738fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 3748fae3551SRodney W. Grimes sizeof(hold)); 3758fae3551SRodney W. Grimes 3768fae3551SRodney W. Grimes /* record route option */ 3778fae3551SRodney W. Grimes if (options & F_RROUTE) { 3788fae3551SRodney W. Grimes #ifdef IP_OPTIONS 3798fae3551SRodney W. Grimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 3808fae3551SRodney W. Grimes rspace[IPOPT_OLEN] = sizeof(rspace)-1; 3818fae3551SRodney W. Grimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 3828fae3551SRodney W. Grimes if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace, 38343470e3bSGarrett Wollman sizeof(rspace)) < 0) 38443470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 3858fae3551SRodney W. Grimes #else 38643470e3bSGarrett Wollman errx(EX_UNAVAILABLE, 38743470e3bSGarrett Wollman "record route not available in this implementation"); 3888fae3551SRodney W. Grimes #endif /* IP_OPTIONS */ 3898fae3551SRodney W. Grimes } 3908fae3551SRodney W. Grimes 39185456935SBill Fenner if (options & F_NOLOOP) { 39285456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 39385456935SBill Fenner sizeof(loop)) < 0) { 39443470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 39585456935SBill Fenner } 39685456935SBill Fenner } 39785456935SBill Fenner if (options & F_MTTL) { 39885456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, 39985456935SBill Fenner sizeof(ttl)) < 0) { 40043470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 40185456935SBill Fenner } 40285456935SBill Fenner } 40385456935SBill Fenner if (options & F_MIF) { 40485456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 40585456935SBill Fenner sizeof(ifaddr)) < 0) { 40643470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 40785456935SBill Fenner } 40885456935SBill Fenner } 40985456935SBill Fenner 4108fae3551SRodney W. Grimes /* 4118fae3551SRodney W. Grimes * When pinging the broadcast address, you can get a lot of answers. 4128fae3551SRodney W. Grimes * Doing something so evil is useful if you are trying to stress the 4138fae3551SRodney W. Grimes * ethernet, or just want to fill the arp cache to get some stuff for 41443470e3bSGarrett Wollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 41543470e3bSGarrett Wollman * or multicast pings if they wish. 4168fae3551SRodney W. Grimes */ 4178fae3551SRodney W. Grimes hold = 48 * 1024; 4188fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 4198fae3551SRodney W. Grimes sizeof(hold)); 4208fae3551SRodney W. Grimes 4218fae3551SRodney W. Grimes if (to->sin_family == AF_INET) 4228fae3551SRodney W. Grimes (void)printf("PING %s (%s): %d data bytes\n", hostname, 42343470e3bSGarrett Wollman inet_ntoa(to->sin_addr), 4248fae3551SRodney W. Grimes datalen); 4258fae3551SRodney W. Grimes else 4268fae3551SRodney W. Grimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 4278fae3551SRodney W. Grimes 4287d81b35cSBruce Evans /* 429a2a00888SSean Eric Fagan * Use sigaction() instead of signal() to get unambiguous semantics, 430a2a00888SSean Eric Fagan * in particular with SA_RESTART not set. 4317d81b35cSBruce Evans */ 432a2a00888SSean Eric Fagan 433f6bd468bSPaul Traina sigemptyset(&si_sa.sa_mask); 43437e5b2c6SSean Eric Fagan si_sa.sa_flags = 0; 435a2a00888SSean Eric Fagan 436a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 437a2a00888SSean Eric Fagan if (sigaction(SIGINT, &si_sa, 0) == -1) { 438a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGINT"); 439a2a00888SSean Eric Fagan } 440a2a00888SSean Eric Fagan 441a2a00888SSean Eric Fagan si_sa.sa_handler = catcher; 442a2a00888SSean Eric Fagan if (sigaction(SIGALRM, &si_sa, 0) == -1) { 443a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGALRM"); 444a2a00888SSean Eric Fagan } 445a2a00888SSean Eric Fagan 446a2a00888SSean Eric Fagan si_sa.sa_handler = status; 44737e5b2c6SSean Eric Fagan if (sigaction(SIGINFO, &si_sa, 0) == -1) { 448624ff938SWarner Losh err(EX_OSERR, "sigaction"); 44937e5b2c6SSean Eric Fagan } 45037e5b2c6SSean Eric Fagan 4514055611aSSean Eric Fagan if (tcgetattr(STDOUT_FILENO, &ts) != -1) { 4524055611aSSean Eric Fagan reset_kerninfo = !(ts.c_lflag & NOKERNINFO); 4534055611aSSean Eric Fagan ts.c_lflag |= NOKERNINFO; 4544055611aSSean Eric Fagan tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 4554055611aSSean Eric Fagan } 4564055611aSSean Eric Fagan 4578fae3551SRodney W. Grimes while (preload--) /* fire off them quickies */ 4588fae3551SRodney W. Grimes pinger(); 4598fae3551SRodney W. Grimes 4608fae3551SRodney W. Grimes if ((options & F_FLOOD) == 0) 46143470e3bSGarrett Wollman catcher(0); /* start things going */ 4628fae3551SRodney W. Grimes 4638f975bb3SBruce Evans while (!finish_up) { 4648fae3551SRodney W. Grimes struct sockaddr_in from; 4658fae3551SRodney W. Grimes register int cc; 4668fae3551SRodney W. Grimes int fromlen; 4678fae3551SRodney W. Grimes 4687d81b35cSBruce Evans check_status(); 4698fae3551SRodney W. Grimes if (options & F_FLOOD) { 4708fae3551SRodney W. Grimes pinger(); 4718fae3551SRodney W. Grimes timeout.tv_sec = 0; 4728fae3551SRodney W. Grimes timeout.tv_usec = 10000; 4738fae3551SRodney W. Grimes fdmask = 1 << s; 4748fae3551SRodney W. Grimes if (select(s + 1, (fd_set *)&fdmask, (fd_set *)NULL, 4758fae3551SRodney W. Grimes (fd_set *)NULL, &timeout) < 1) 4768fae3551SRodney W. Grimes continue; 4778fae3551SRodney W. Grimes } 4788fae3551SRodney W. Grimes fromlen = sizeof(from); 4798fae3551SRodney W. Grimes if ((cc = recvfrom(s, (char *)packet, packlen, 0, 4808fae3551SRodney W. Grimes (struct sockaddr *)&from, &fromlen)) < 0) { 4818fae3551SRodney W. Grimes if (errno == EINTR) 4828fae3551SRodney W. Grimes continue; 4838fae3551SRodney W. Grimes perror("ping: recvfrom"); 4848fae3551SRodney W. Grimes continue; 4858fae3551SRodney W. Grimes } 4868fae3551SRodney W. Grimes pr_pack((char *)packet, cc, &from); 4878fae3551SRodney W. Grimes if (npackets && nreceived >= npackets) 4888fae3551SRodney W. Grimes break; 4898fae3551SRodney W. Grimes } 4908f975bb3SBruce Evans finish(); 4918fae3551SRodney W. Grimes /* NOTREACHED */ 492f78ac61bSWarner Losh exit(0); /* Make the compiler happy */ 4938fae3551SRodney W. Grimes } 4948fae3551SRodney W. Grimes 4958fae3551SRodney W. Grimes /* 4968f975bb3SBruce Evans * stopit -- 4978f975bb3SBruce Evans * Set the global bit that causes the main loop to quit. 4988f975bb3SBruce Evans * Do NOT call finish() from here, since finish() does far too much 4998f975bb3SBruce Evans * to be called from a signal handler. 500515dd2faSJulian Elischer */ 501515dd2faSJulian Elischer void 5028f975bb3SBruce Evans stopit(sig) 5038f975bb3SBruce Evans int sig; 504515dd2faSJulian Elischer { 505515dd2faSJulian Elischer finish_up = 1; 506515dd2faSJulian Elischer } 507515dd2faSJulian Elischer 508515dd2faSJulian Elischer /* 5098fae3551SRodney W. Grimes * catcher -- 5108fae3551SRodney W. Grimes * This routine causes another PING to be transmitted, and then 5118fae3551SRodney W. Grimes * schedules another SIGALRM for 1 second from now. 5128fae3551SRodney W. Grimes * 5138fae3551SRodney W. Grimes * bug -- 5148fae3551SRodney W. Grimes * Our sense of time will slowly skew (i.e., packets will not be 5158fae3551SRodney W. Grimes * launched exactly at 1-second intervals). This does not affect the 5168fae3551SRodney W. Grimes * quality of the delay and loss statistics. 5178fae3551SRodney W. Grimes */ 51843470e3bSGarrett Wollman static void 51943470e3bSGarrett Wollman catcher(int sig) 5208fae3551SRodney W. Grimes { 5218fae3551SRodney W. Grimes int waittime; 522a2a00888SSean Eric Fagan struct sigaction si_sa; 5238fae3551SRodney W. Grimes 5248fae3551SRodney W. Grimes pinger(); 525a2a00888SSean Eric Fagan 5268fae3551SRodney W. Grimes if (!npackets || ntransmitted < npackets) 527a2a00888SSean Eric Fagan (void)alarm((u_int)interval); 5288fae3551SRodney W. Grimes else { 5298fae3551SRodney W. Grimes if (nreceived) { 5308fae3551SRodney W. Grimes waittime = 2 * tmax / 1000; 5318fae3551SRodney W. Grimes if (!waittime) 5328fae3551SRodney W. Grimes waittime = 1; 5338fae3551SRodney W. Grimes } else 5348fae3551SRodney W. Grimes waittime = MAXWAIT; 535a2a00888SSean Eric Fagan 536a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 537a2a00888SSean Eric Fagan sigemptyset(&si_sa.sa_mask); 538a2a00888SSean Eric Fagan si_sa.sa_flags = 0; 539a2a00888SSean Eric Fagan if (sigaction(SIGALRM, &si_sa, 0) == -1) { 540a2a00888SSean Eric Fagan finish_up = 1; 541a2a00888SSean Eric Fagan return; 542a2a00888SSean Eric Fagan } 5438fae3551SRodney W. Grimes (void)alarm((u_int)waittime); 5448fae3551SRodney W. Grimes } 5458fae3551SRodney W. Grimes } 5468fae3551SRodney W. Grimes 5478fae3551SRodney W. Grimes /* 5488fae3551SRodney W. Grimes * pinger -- 5498fae3551SRodney W. Grimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 5508fae3551SRodney W. Grimes * will be added on by the kernel. The ID field is our UNIX process ID, 5518fae3551SRodney W. Grimes * and the sequence number is an ascending integer. The first 8 bytes 552ef9e6dc7SBill Fenner * of the data portion are used to hold a UNIX "timeval" struct in host 5538fae3551SRodney W. Grimes * byte-order, to compute the round-trip time. 5548f975bb3SBruce Evans * 5558f975bb3SBruce Evans * bug -- 5568f975bb3SBruce Evans * this does far too much to be called from a signal handler. 5578fae3551SRodney W. Grimes */ 55843470e3bSGarrett Wollman static void 55943470e3bSGarrett Wollman pinger(void) 5608fae3551SRodney W. Grimes { 5618fae3551SRodney W. Grimes register struct icmp *icp; 5628fae3551SRodney W. Grimes register int cc; 5638fae3551SRodney W. Grimes int i; 5648fae3551SRodney W. Grimes 5658fae3551SRodney W. Grimes icp = (struct icmp *)outpack; 5668fae3551SRodney W. Grimes icp->icmp_type = ICMP_ECHO; 5678fae3551SRodney W. Grimes icp->icmp_code = 0; 5688fae3551SRodney W. Grimes icp->icmp_cksum = 0; 5690e59c641SJulian Elischer icp->icmp_seq = ntransmitted; 5708fae3551SRodney W. Grimes icp->icmp_id = ident; /* ID */ 5718fae3551SRodney W. Grimes 5728fae3551SRodney W. Grimes CLR(icp->icmp_seq % mx_dup_ck); 5738fae3551SRodney W. Grimes 5748fae3551SRodney W. Grimes if (timing) 5758fae3551SRodney W. Grimes (void)gettimeofday((struct timeval *)&outpack[8], 5768fae3551SRodney W. Grimes (struct timezone *)NULL); 5778fae3551SRodney W. Grimes 5788fae3551SRodney W. Grimes cc = datalen + 8; /* skips ICMP portion */ 5798fae3551SRodney W. Grimes 5808fae3551SRodney W. Grimes /* compute ICMP checksum here */ 5818fae3551SRodney W. Grimes icp->icmp_cksum = in_cksum((u_short *)icp, cc); 5828fae3551SRodney W. Grimes 5838fae3551SRodney W. Grimes i = sendto(s, (char *)outpack, cc, 0, &whereto, 5848fae3551SRodney W. Grimes sizeof(struct sockaddr)); 5858fae3551SRodney W. Grimes 5868fae3551SRodney W. Grimes if (i < 0 || i != cc) { 58743470e3bSGarrett Wollman if (i < 0) { 5888f975bb3SBruce Evans if (options & F_FLOOD && errno == ENOBUFS) { 589515dd2faSJulian Elischer usleep(FLOOD_BACKOFF); 590515dd2faSJulian Elischer return; 591515dd2faSJulian Elischer } 59243470e3bSGarrett Wollman warn("sendto"); 59343470e3bSGarrett Wollman } else { 59443470e3bSGarrett Wollman warn("%s: partial write: %d of %d bytes", 5958fae3551SRodney W. Grimes hostname, cc, i); 5968fae3551SRodney W. Grimes } 597363d7bbeSJulian Elischer } 598363d7bbeSJulian Elischer ntransmitted++; 5998fae3551SRodney W. Grimes if (!(options & F_QUIET) && options & F_FLOOD) 6008fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &DOT, 1); 6018fae3551SRodney W. Grimes } 6028fae3551SRodney W. Grimes 6038fae3551SRodney W. Grimes /* 6048fae3551SRodney W. Grimes * pr_pack -- 6058fae3551SRodney W. Grimes * Print out the packet, if it came from us. This logic is necessary 6068fae3551SRodney W. Grimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 6078fae3551SRodney W. Grimes * which arrive ('tis only fair). This permits multiple copies of this 6088fae3551SRodney W. Grimes * program to be run without having intermingled output (or statistics!). 6098fae3551SRodney W. Grimes */ 61043470e3bSGarrett Wollman static void 6118fae3551SRodney W. Grimes pr_pack(buf, cc, from) 6128fae3551SRodney W. Grimes char *buf; 6138fae3551SRodney W. Grimes int cc; 6148fae3551SRodney W. Grimes struct sockaddr_in *from; 6158fae3551SRodney W. Grimes { 6168fae3551SRodney W. Grimes register struct icmp *icp; 6178fae3551SRodney W. Grimes register u_long l; 6188fae3551SRodney W. Grimes register int i, j; 6198fae3551SRodney W. Grimes register u_char *cp,*dp; 6208fae3551SRodney W. Grimes static int old_rrlen; 6218fae3551SRodney W. Grimes static char old_rr[MAX_IPOPTLEN]; 6228fae3551SRodney W. Grimes struct ip *ip; 6238fae3551SRodney W. Grimes struct timeval tv, *tp; 6248f975bb3SBruce Evans double triptime; 6258fae3551SRodney W. Grimes int hlen, dupflag; 6268fae3551SRodney W. Grimes 6278fae3551SRodney W. Grimes (void)gettimeofday(&tv, (struct timezone *)NULL); 6288fae3551SRodney W. Grimes 6298fae3551SRodney W. Grimes /* Check the IP header */ 6308fae3551SRodney W. Grimes ip = (struct ip *)buf; 6318fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 6328fae3551SRodney W. Grimes if (cc < hlen + ICMP_MINLEN) { 6338fae3551SRodney W. Grimes if (options & F_VERBOSE) 63443470e3bSGarrett Wollman warn("packet too short (%d bytes) from %s", cc, 63543470e3bSGarrett Wollman inet_ntoa(from->sin_addr)); 6368fae3551SRodney W. Grimes return; 6378fae3551SRodney W. Grimes } 6388fae3551SRodney W. Grimes 6398fae3551SRodney W. Grimes /* Now the ICMP part */ 6408fae3551SRodney W. Grimes cc -= hlen; 6418fae3551SRodney W. Grimes icp = (struct icmp *)(buf + hlen); 6428fae3551SRodney W. Grimes if (icp->icmp_type == ICMP_ECHOREPLY) { 6438fae3551SRodney W. Grimes if (icp->icmp_id != ident) 6448fae3551SRodney W. Grimes return; /* 'Twas not our ECHO */ 6458fae3551SRodney W. Grimes ++nreceived; 6468f975bb3SBruce Evans triptime = 0.0; 6478fae3551SRodney W. Grimes if (timing) { 6488fae3551SRodney W. Grimes #ifndef icmp_data 6498fae3551SRodney W. Grimes tp = (struct timeval *)&icp->icmp_ip; 6508fae3551SRodney W. Grimes #else 6518fae3551SRodney W. Grimes tp = (struct timeval *)icp->icmp_data; 6528fae3551SRodney W. Grimes #endif 6538fae3551SRodney W. Grimes tvsub(&tv, tp); 6548fae3551SRodney W. Grimes triptime = ((double)tv.tv_sec) * 1000.0 + 6558fae3551SRodney W. Grimes ((double)tv.tv_usec) / 1000.0; 6568fae3551SRodney W. Grimes tsum += triptime; 6573109a910SGarrett Wollman tsumsq += triptime * triptime; 6588fae3551SRodney W. Grimes if (triptime < tmin) 6598fae3551SRodney W. Grimes tmin = triptime; 6608fae3551SRodney W. Grimes if (triptime > tmax) 6618fae3551SRodney W. Grimes tmax = triptime; 6628fae3551SRodney W. Grimes } 6638fae3551SRodney W. Grimes 6648fae3551SRodney W. Grimes if (TST(icp->icmp_seq % mx_dup_ck)) { 6658fae3551SRodney W. Grimes ++nrepeats; 6668fae3551SRodney W. Grimes --nreceived; 6678fae3551SRodney W. Grimes dupflag = 1; 6688fae3551SRodney W. Grimes } else { 6698fae3551SRodney W. Grimes SET(icp->icmp_seq % mx_dup_ck); 6708fae3551SRodney W. Grimes dupflag = 0; 6718fae3551SRodney W. Grimes } 6728fae3551SRodney W. Grimes 6738fae3551SRodney W. Grimes if (options & F_QUIET) 6748fae3551SRodney W. Grimes return; 6758fae3551SRodney W. Grimes 6768fae3551SRodney W. Grimes if (options & F_FLOOD) 6778fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &BSPACE, 1); 6788fae3551SRodney W. Grimes else { 6798fae3551SRodney W. Grimes (void)printf("%d bytes from %s: icmp_seq=%u", cc, 6808fae3551SRodney W. Grimes inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr), 6818fae3551SRodney W. Grimes icp->icmp_seq); 6828fae3551SRodney W. Grimes (void)printf(" ttl=%d", ip->ip_ttl); 6838fae3551SRodney W. Grimes if (timing) 684d410b6f1SDavid Greenman (void)printf(" time=%.3f ms", triptime); 6858fae3551SRodney W. Grimes if (dupflag) 6868fae3551SRodney W. Grimes (void)printf(" (DUP!)"); 687772dfa72SDaniel O'Callaghan if (options & F_AUDIBLE) 688772dfa72SDaniel O'Callaghan (void)printf("\a"); 6898fae3551SRodney W. Grimes /* check the data */ 6908fae3551SRodney W. Grimes cp = (u_char*)&icp->icmp_data[8]; 6918fae3551SRodney W. Grimes dp = &outpack[8 + sizeof(struct timeval)]; 6928fae3551SRodney W. Grimes for (i = 8; i < datalen; ++i, ++cp, ++dp) { 6938fae3551SRodney W. Grimes if (*cp != *dp) { 6948fae3551SRodney W. Grimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 6958fae3551SRodney W. Grimes i, *dp, *cp); 6968fae3551SRodney W. Grimes cp = (u_char*)&icp->icmp_data[0]; 6978fae3551SRodney W. Grimes for (i = 8; i < datalen; ++i, ++cp) { 6988fae3551SRodney W. Grimes if ((i % 32) == 8) 6998fae3551SRodney W. Grimes (void)printf("\n\t"); 7008fae3551SRodney W. Grimes (void)printf("%x ", *cp); 7018fae3551SRodney W. Grimes } 7028fae3551SRodney W. Grimes break; 7038fae3551SRodney W. Grimes } 7048fae3551SRodney W. Grimes } 7058fae3551SRodney W. Grimes } 7068fae3551SRodney W. Grimes } else { 707ef9e6dc7SBill Fenner /* 708ef9e6dc7SBill Fenner * We've got something other than an ECHOREPLY. 709ef9e6dc7SBill Fenner * See if it's a reply to something that we sent. 710ef9e6dc7SBill Fenner * We can compare IP destination, protocol, 711ef9e6dc7SBill Fenner * and ICMP type and ID. 712f78ac61bSWarner Losh * 713f78ac61bSWarner Losh * Only print all the error messages if we are running 714f78ac61bSWarner Losh * as root to avoid leaking information not normally 715f78ac61bSWarner Losh * available to those not running as root. 716ef9e6dc7SBill Fenner */ 717ef9e6dc7SBill Fenner #ifndef icmp_data 718ef9e6dc7SBill Fenner struct ip *oip = &icp->icmp_ip; 719ef9e6dc7SBill Fenner #else 720ef9e6dc7SBill Fenner struct ip *oip = (struct ip *)icp->icmp_data; 721ef9e6dc7SBill Fenner #endif 722ef9e6dc7SBill Fenner struct icmp *oicmp = (struct icmp *)(oip + 1); 723ef9e6dc7SBill Fenner 724ee2bf734SWarner Losh if (((options & F_VERBOSE) && uid == 0) || 725ef9e6dc7SBill Fenner (!(options & F_QUIET2) && 726ef9e6dc7SBill Fenner (oip->ip_dst.s_addr == 727ef9e6dc7SBill Fenner ((struct sockaddr_in *)&whereto)->sin_addr.s_addr) && 728ef9e6dc7SBill Fenner (oip->ip_p == IPPROTO_ICMP) && 729ef9e6dc7SBill Fenner (oicmp->icmp_type == ICMP_ECHO) && 730ef9e6dc7SBill Fenner (oicmp->icmp_id == ident))) { 7318fae3551SRodney W. Grimes (void)printf("%d bytes from %s: ", cc, 73243470e3bSGarrett Wollman pr_addr(from->sin_addr)); 7338fae3551SRodney W. Grimes pr_icmph(icp); 734ef9e6dc7SBill Fenner } else 735ef9e6dc7SBill Fenner return; 7368fae3551SRodney W. Grimes } 7378fae3551SRodney W. Grimes 7388fae3551SRodney W. Grimes /* Display any IP options */ 7398fae3551SRodney W. Grimes cp = (u_char *)buf + sizeof(struct ip); 7408fae3551SRodney W. Grimes 7418fae3551SRodney W. Grimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 7428fae3551SRodney W. Grimes switch (*cp) { 7438fae3551SRodney W. Grimes case IPOPT_EOL: 7448fae3551SRodney W. Grimes hlen = 0; 7458fae3551SRodney W. Grimes break; 7468fae3551SRodney W. Grimes case IPOPT_LSRR: 7478fae3551SRodney W. Grimes (void)printf("\nLSRR: "); 7488fae3551SRodney W. Grimes hlen -= 2; 7498fae3551SRodney W. Grimes j = *++cp; 7508fae3551SRodney W. Grimes ++cp; 7518fae3551SRodney W. Grimes if (j > IPOPT_MINOFF) 7528fae3551SRodney W. Grimes for (;;) { 7538fae3551SRodney W. Grimes l = *++cp; 7548fae3551SRodney W. Grimes l = (l<<8) + *++cp; 7558fae3551SRodney W. Grimes l = (l<<8) + *++cp; 7568fae3551SRodney W. Grimes l = (l<<8) + *++cp; 75743470e3bSGarrett Wollman if (l == 0) { 75843470e3bSGarrett Wollman printf("\t0.0.0.0"); 75943470e3bSGarrett Wollman } else { 76043470e3bSGarrett Wollman struct in_addr ina; 76143470e3bSGarrett Wollman ina.s_addr = ntohl(l); 76243470e3bSGarrett Wollman printf("\t%s", pr_addr(ina)); 76343470e3bSGarrett Wollman } 7648fae3551SRodney W. Grimes hlen -= 4; 7658fae3551SRodney W. Grimes j -= 4; 7668fae3551SRodney W. Grimes if (j <= IPOPT_MINOFF) 7678fae3551SRodney W. Grimes break; 7688fae3551SRodney W. Grimes (void)putchar('\n'); 7698fae3551SRodney W. Grimes } 7708fae3551SRodney W. Grimes break; 7718fae3551SRodney W. Grimes case IPOPT_RR: 7728fae3551SRodney W. Grimes j = *++cp; /* get length */ 7738fae3551SRodney W. Grimes i = *++cp; /* and pointer */ 7748fae3551SRodney W. Grimes hlen -= 2; 7758fae3551SRodney W. Grimes if (i > j) 7768fae3551SRodney W. Grimes i = j; 7778fae3551SRodney W. Grimes i -= IPOPT_MINOFF; 7788fae3551SRodney W. Grimes if (i <= 0) 7798fae3551SRodney W. Grimes continue; 7808fae3551SRodney W. Grimes if (i == old_rrlen 7818fae3551SRodney W. Grimes && cp == (u_char *)buf + sizeof(struct ip) + 2 7828fae3551SRodney W. Grimes && !bcmp((char *)cp, old_rr, i) 7838fae3551SRodney W. Grimes && !(options & F_FLOOD)) { 7848fae3551SRodney W. Grimes (void)printf("\t(same route)"); 7858fae3551SRodney W. Grimes i = ((i + 3) / 4) * 4; 7868fae3551SRodney W. Grimes hlen -= i; 7878fae3551SRodney W. Grimes cp += i; 7888fae3551SRodney W. Grimes break; 7898fae3551SRodney W. Grimes } 7908fae3551SRodney W. Grimes old_rrlen = i; 7918fae3551SRodney W. Grimes bcopy((char *)cp, old_rr, i); 7928fae3551SRodney W. Grimes (void)printf("\nRR: "); 7938fae3551SRodney W. Grimes for (;;) { 7948fae3551SRodney W. Grimes l = *++cp; 7958fae3551SRodney W. Grimes l = (l<<8) + *++cp; 7968fae3551SRodney W. Grimes l = (l<<8) + *++cp; 7978fae3551SRodney W. Grimes l = (l<<8) + *++cp; 79843470e3bSGarrett Wollman if (l == 0) { 79943470e3bSGarrett Wollman printf("\t0.0.0.0"); 80043470e3bSGarrett Wollman } else { 80143470e3bSGarrett Wollman struct in_addr ina; 80243470e3bSGarrett Wollman ina.s_addr = ntohl(l); 80343470e3bSGarrett Wollman printf("\t%s", pr_addr(ina)); 80443470e3bSGarrett Wollman } 8058fae3551SRodney W. Grimes hlen -= 4; 8068fae3551SRodney W. Grimes i -= 4; 8078fae3551SRodney W. Grimes if (i <= 0) 8088fae3551SRodney W. Grimes break; 8098fae3551SRodney W. Grimes (void)putchar('\n'); 8108fae3551SRodney W. Grimes } 8118fae3551SRodney W. Grimes break; 8128fae3551SRodney W. Grimes case IPOPT_NOP: 8138fae3551SRodney W. Grimes (void)printf("\nNOP"); 8148fae3551SRodney W. Grimes break; 8158fae3551SRodney W. Grimes default: 8168fae3551SRodney W. Grimes (void)printf("\nunknown option %x", *cp); 8178fae3551SRodney W. Grimes break; 8188fae3551SRodney W. Grimes } 8198fae3551SRodney W. Grimes if (!(options & F_FLOOD)) { 8208fae3551SRodney W. Grimes (void)putchar('\n'); 8218fae3551SRodney W. Grimes (void)fflush(stdout); 8228fae3551SRodney W. Grimes } 8238fae3551SRodney W. Grimes } 8248fae3551SRodney W. Grimes 8258fae3551SRodney W. Grimes /* 8268fae3551SRodney W. Grimes * in_cksum -- 8278fae3551SRodney W. Grimes * Checksum routine for Internet Protocol family headers (C Version) 8288fae3551SRodney W. Grimes */ 82943470e3bSGarrett Wollman u_short 8308fae3551SRodney W. Grimes in_cksum(addr, len) 8318fae3551SRodney W. Grimes u_short *addr; 8328fae3551SRodney W. Grimes int len; 8338fae3551SRodney W. Grimes { 8348fae3551SRodney W. Grimes register int nleft = len; 8358fae3551SRodney W. Grimes register u_short *w = addr; 8368fae3551SRodney W. Grimes register int sum = 0; 8378fae3551SRodney W. Grimes u_short answer = 0; 8388fae3551SRodney W. Grimes 8398fae3551SRodney W. Grimes /* 8408fae3551SRodney W. Grimes * Our algorithm is simple, using a 32 bit accumulator (sum), we add 8418fae3551SRodney W. Grimes * sequential 16 bit words to it, and at the end, fold back all the 8428fae3551SRodney W. Grimes * carry bits from the top 16 bits into the lower 16 bits. 8438fae3551SRodney W. Grimes */ 8448fae3551SRodney W. Grimes while (nleft > 1) { 8458fae3551SRodney W. Grimes sum += *w++; 8468fae3551SRodney W. Grimes nleft -= 2; 8478fae3551SRodney W. Grimes } 8488fae3551SRodney W. Grimes 8498fae3551SRodney W. Grimes /* mop up an odd byte, if necessary */ 8508fae3551SRodney W. Grimes if (nleft == 1) { 8518fae3551SRodney W. Grimes *(u_char *)(&answer) = *(u_char *)w ; 8528fae3551SRodney W. Grimes sum += answer; 8538fae3551SRodney W. Grimes } 8548fae3551SRodney W. Grimes 8558fae3551SRodney W. Grimes /* add back carry outs from top 16 bits to low 16 bits */ 8568fae3551SRodney W. Grimes sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 8578fae3551SRodney W. Grimes sum += (sum >> 16); /* add carry */ 8588fae3551SRodney W. Grimes answer = ~sum; /* truncate to 16 bits */ 8598fae3551SRodney W. Grimes return(answer); 8608fae3551SRodney W. Grimes } 8618fae3551SRodney W. Grimes 8628fae3551SRodney W. Grimes /* 8638fae3551SRodney W. Grimes * tvsub -- 8648fae3551SRodney W. Grimes * Subtract 2 timeval structs: out = out - in. Out is assumed to 8658fae3551SRodney W. Grimes * be >= in. 8668fae3551SRodney W. Grimes */ 86743470e3bSGarrett Wollman static void 8688fae3551SRodney W. Grimes tvsub(out, in) 8698fae3551SRodney W. Grimes register struct timeval *out, *in; 8708fae3551SRodney W. Grimes { 8718fae3551SRodney W. Grimes if ((out->tv_usec -= in->tv_usec) < 0) { 8728fae3551SRodney W. Grimes --out->tv_sec; 8738fae3551SRodney W. Grimes out->tv_usec += 1000000; 8748fae3551SRodney W. Grimes } 8758fae3551SRodney W. Grimes out->tv_sec -= in->tv_sec; 8768fae3551SRodney W. Grimes } 8778fae3551SRodney W. Grimes 8788fae3551SRodney W. Grimes /* 879badd8138SSean Eric Fagan * status -- 880badd8138SSean Eric Fagan * Print out statistics when SIGINFO is received. 881badd8138SSean Eric Fagan */ 882badd8138SSean Eric Fagan 88343470e3bSGarrett Wollman static void 8847d81b35cSBruce Evans status(sig) 8857d81b35cSBruce Evans int sig; 8867d81b35cSBruce Evans { 88737e5b2c6SSean Eric Fagan siginfo_p = 1; 88837e5b2c6SSean Eric Fagan } 88937e5b2c6SSean Eric Fagan 89043470e3bSGarrett Wollman static void 89137e5b2c6SSean Eric Fagan check_status() 892badd8138SSean Eric Fagan { 89337e5b2c6SSean Eric Fagan if (siginfo_p) { 89437e5b2c6SSean Eric Fagan siginfo_p = 0; 8957d81b35cSBruce Evans (void)fprintf(stderr, 8967d81b35cSBruce Evans "\r%ld/%ld packets received (%.0f%%) %.3f min / %.3f avg / %.3f max\n", 8977d81b35cSBruce Evans nreceived, ntransmitted, 8987d81b35cSBruce Evans ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0, 8997d81b35cSBruce Evans nreceived ? tmin : 0.0, 9007d81b35cSBruce Evans nreceived + nrepeats ? tsum / (nreceived + nrepeats) : tsum, 9017d81b35cSBruce Evans tmax); 90237e5b2c6SSean Eric Fagan } 903badd8138SSean Eric Fagan } 904badd8138SSean Eric Fagan 905badd8138SSean Eric Fagan /* 9068fae3551SRodney W. Grimes * finish -- 9078fae3551SRodney W. Grimes * Print out statistics, and give up. 9088fae3551SRodney W. Grimes */ 90943470e3bSGarrett Wollman static void 9108f975bb3SBruce Evans finish() 9118fae3551SRodney W. Grimes { 912badd8138SSean Eric Fagan struct termios ts; 9138fae3551SRodney W. Grimes 9148fae3551SRodney W. Grimes (void)signal(SIGINT, SIG_IGN); 915a2a00888SSean Eric Fagan (void)signal(SIGALRM, SIG_IGN); 9168fae3551SRodney W. Grimes (void)putchar('\n'); 9178fae3551SRodney W. Grimes (void)fflush(stdout); 9188fae3551SRodney W. Grimes (void)printf("--- %s ping statistics ---\n", hostname); 9198fae3551SRodney W. Grimes (void)printf("%ld packets transmitted, ", ntransmitted); 9208fae3551SRodney W. Grimes (void)printf("%ld packets received, ", nreceived); 9218fae3551SRodney W. Grimes if (nrepeats) 9228fae3551SRodney W. Grimes (void)printf("+%ld duplicates, ", nrepeats); 9238fae3551SRodney W. Grimes if (ntransmitted) 9248fae3551SRodney W. Grimes if (nreceived > ntransmitted) 9258fae3551SRodney W. Grimes (void)printf("-- somebody's printing up packets!"); 9268fae3551SRodney W. Grimes else 9278fae3551SRodney W. Grimes (void)printf("%d%% packet loss", 9288fae3551SRodney W. Grimes (int) (((ntransmitted - nreceived) * 100) / 9298fae3551SRodney W. Grimes ntransmitted)); 9308fae3551SRodney W. Grimes (void)putchar('\n'); 9313109a910SGarrett Wollman if (nreceived && timing) { 9323109a910SGarrett Wollman double n = nreceived + nrepeats; 9333109a910SGarrett Wollman double avg = tsum / n; 9343109a910SGarrett Wollman double vari = tsumsq / n - avg * avg; 9353109a910SGarrett Wollman printf("round-trip min/avg/max/stddev = " 9363109a910SGarrett Wollman "%.3f/%.3f/%.3f/%.3f ms\n", 9373109a910SGarrett Wollman tmin, avg, tmax, sqrt(vari)); 9383109a910SGarrett Wollman } 9392ceaae21SBruce Evans if (reset_kerninfo && tcgetattr(STDOUT_FILENO, &ts) != -1) { 940badd8138SSean Eric Fagan ts.c_lflag &= ~NOKERNINFO; 9412ceaae21SBruce Evans tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 942badd8138SSean Eric Fagan } 943badd8138SSean Eric Fagan 9446e1173dcSDavid Greenman if (nreceived) 9458fae3551SRodney W. Grimes exit(0); 9466e1173dcSDavid Greenman else 9476e1173dcSDavid Greenman exit(2); 9488fae3551SRodney W. Grimes } 9498fae3551SRodney W. Grimes 9508fae3551SRodney W. Grimes #ifdef notdef 9518fae3551SRodney W. Grimes static char *ttab[] = { 9528fae3551SRodney W. Grimes "Echo Reply", /* ip + seq + udata */ 9538fae3551SRodney W. Grimes "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */ 9548fae3551SRodney W. Grimes "Source Quench", /* IP */ 9558fae3551SRodney W. Grimes "Redirect", /* redirect type, gateway, + IP */ 9568fae3551SRodney W. Grimes "Echo", 9578fae3551SRodney W. Grimes "Time Exceeded", /* transit, frag reassem + IP */ 9588fae3551SRodney W. Grimes "Parameter Problem", /* pointer + IP */ 9598fae3551SRodney W. Grimes "Timestamp", /* id + seq + three timestamps */ 9608fae3551SRodney W. Grimes "Timestamp Reply", /* " */ 9618fae3551SRodney W. Grimes "Info Request", /* id + sq */ 9628fae3551SRodney W. Grimes "Info Reply" /* " */ 9638fae3551SRodney W. Grimes }; 9648fae3551SRodney W. Grimes #endif 9658fae3551SRodney W. Grimes 9668fae3551SRodney W. Grimes /* 9678fae3551SRodney W. Grimes * pr_icmph -- 9688fae3551SRodney W. Grimes * Print a descriptive string about an ICMP header. 9698fae3551SRodney W. Grimes */ 97043470e3bSGarrett Wollman static void 9718fae3551SRodney W. Grimes pr_icmph(icp) 9728fae3551SRodney W. Grimes struct icmp *icp; 9738fae3551SRodney W. Grimes { 9748fae3551SRodney W. Grimes switch(icp->icmp_type) { 9758fae3551SRodney W. Grimes case ICMP_ECHOREPLY: 9768fae3551SRodney W. Grimes (void)printf("Echo Reply\n"); 9778fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 9788fae3551SRodney W. Grimes break; 9798fae3551SRodney W. Grimes case ICMP_UNREACH: 9808fae3551SRodney W. Grimes switch(icp->icmp_code) { 9818fae3551SRodney W. Grimes case ICMP_UNREACH_NET: 9828fae3551SRodney W. Grimes (void)printf("Destination Net Unreachable\n"); 9838fae3551SRodney W. Grimes break; 9848fae3551SRodney W. Grimes case ICMP_UNREACH_HOST: 9858fae3551SRodney W. Grimes (void)printf("Destination Host Unreachable\n"); 9868fae3551SRodney W. Grimes break; 9878fae3551SRodney W. Grimes case ICMP_UNREACH_PROTOCOL: 9888fae3551SRodney W. Grimes (void)printf("Destination Protocol Unreachable\n"); 9898fae3551SRodney W. Grimes break; 9908fae3551SRodney W. Grimes case ICMP_UNREACH_PORT: 9918fae3551SRodney W. Grimes (void)printf("Destination Port Unreachable\n"); 9928fae3551SRodney W. Grimes break; 9938fae3551SRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 994ef9e6dc7SBill Fenner (void)printf("frag needed and DF set (MTU %d)\n", 995ef9e6dc7SBill Fenner icp->icmp_nextmtu); 9968fae3551SRodney W. Grimes break; 9978fae3551SRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 9988fae3551SRodney W. Grimes (void)printf("Source Route Failed\n"); 9998fae3551SRodney W. Grimes break; 1000ef9e6dc7SBill Fenner case ICMP_UNREACH_FILTER_PROHIB: 1001ef9e6dc7SBill Fenner (void)printf("Communication prohibited by filter\n"); 1002ef9e6dc7SBill Fenner break; 10038fae3551SRodney W. Grimes default: 10048fae3551SRodney W. Grimes (void)printf("Dest Unreachable, Bad Code: %d\n", 10058fae3551SRodney W. Grimes icp->icmp_code); 10068fae3551SRodney W. Grimes break; 10078fae3551SRodney W. Grimes } 10088fae3551SRodney W. Grimes /* Print returned IP header information */ 10098fae3551SRodney W. Grimes #ifndef icmp_data 10108fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 10118fae3551SRodney W. Grimes #else 10128fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 10138fae3551SRodney W. Grimes #endif 10148fae3551SRodney W. Grimes break; 10158fae3551SRodney W. Grimes case ICMP_SOURCEQUENCH: 10168fae3551SRodney W. Grimes (void)printf("Source Quench\n"); 10178fae3551SRodney W. Grimes #ifndef icmp_data 10188fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 10198fae3551SRodney W. Grimes #else 10208fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 10218fae3551SRodney W. Grimes #endif 10228fae3551SRodney W. Grimes break; 10238fae3551SRodney W. Grimes case ICMP_REDIRECT: 10248fae3551SRodney W. Grimes switch(icp->icmp_code) { 10258fae3551SRodney W. Grimes case ICMP_REDIRECT_NET: 10268fae3551SRodney W. Grimes (void)printf("Redirect Network"); 10278fae3551SRodney W. Grimes break; 10288fae3551SRodney W. Grimes case ICMP_REDIRECT_HOST: 10298fae3551SRodney W. Grimes (void)printf("Redirect Host"); 10308fae3551SRodney W. Grimes break; 10318fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSNET: 10328fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Network"); 10338fae3551SRodney W. Grimes break; 10348fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSHOST: 10358fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Host"); 10368fae3551SRodney W. Grimes break; 10378fae3551SRodney W. Grimes default: 10388fae3551SRodney W. Grimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 10398fae3551SRodney W. Grimes break; 10408fae3551SRodney W. Grimes } 10418fae3551SRodney W. Grimes (void)printf("(New addr: 0x%08lx)\n", icp->icmp_gwaddr.s_addr); 10428fae3551SRodney W. Grimes #ifndef icmp_data 10438fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 10448fae3551SRodney W. Grimes #else 10458fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 10468fae3551SRodney W. Grimes #endif 10478fae3551SRodney W. Grimes break; 10488fae3551SRodney W. Grimes case ICMP_ECHO: 10498fae3551SRodney W. Grimes (void)printf("Echo Request\n"); 10508fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 10518fae3551SRodney W. Grimes break; 10528fae3551SRodney W. Grimes case ICMP_TIMXCEED: 10538fae3551SRodney W. Grimes switch(icp->icmp_code) { 10548fae3551SRodney W. Grimes case ICMP_TIMXCEED_INTRANS: 10558fae3551SRodney W. Grimes (void)printf("Time to live exceeded\n"); 10568fae3551SRodney W. Grimes break; 10578fae3551SRodney W. Grimes case ICMP_TIMXCEED_REASS: 10588fae3551SRodney W. Grimes (void)printf("Frag reassembly time exceeded\n"); 10598fae3551SRodney W. Grimes break; 10608fae3551SRodney W. Grimes default: 10618fae3551SRodney W. Grimes (void)printf("Time exceeded, Bad Code: %d\n", 10628fae3551SRodney W. Grimes icp->icmp_code); 10638fae3551SRodney W. Grimes break; 10648fae3551SRodney W. Grimes } 10658fae3551SRodney W. Grimes #ifndef icmp_data 10668fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 10678fae3551SRodney W. Grimes #else 10688fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 10698fae3551SRodney W. Grimes #endif 10708fae3551SRodney W. Grimes break; 10718fae3551SRodney W. Grimes case ICMP_PARAMPROB: 10728fae3551SRodney W. Grimes (void)printf("Parameter problem: pointer = 0x%02x\n", 10738fae3551SRodney W. Grimes icp->icmp_hun.ih_pptr); 10748fae3551SRodney W. Grimes #ifndef icmp_data 10758fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 10768fae3551SRodney W. Grimes #else 10778fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 10788fae3551SRodney W. Grimes #endif 10798fae3551SRodney W. Grimes break; 10808fae3551SRodney W. Grimes case ICMP_TSTAMP: 10818fae3551SRodney W. Grimes (void)printf("Timestamp\n"); 10828fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 10838fae3551SRodney W. Grimes break; 10848fae3551SRodney W. Grimes case ICMP_TSTAMPREPLY: 10858fae3551SRodney W. Grimes (void)printf("Timestamp Reply\n"); 10868fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 10878fae3551SRodney W. Grimes break; 10888fae3551SRodney W. Grimes case ICMP_IREQ: 10898fae3551SRodney W. Grimes (void)printf("Information Request\n"); 10908fae3551SRodney W. Grimes /* XXX ID + Seq */ 10918fae3551SRodney W. Grimes break; 10928fae3551SRodney W. Grimes case ICMP_IREQREPLY: 10938fae3551SRodney W. Grimes (void)printf("Information Reply\n"); 10948fae3551SRodney W. Grimes /* XXX ID + Seq */ 10958fae3551SRodney W. Grimes break; 10968fae3551SRodney W. Grimes case ICMP_MASKREQ: 10978fae3551SRodney W. Grimes (void)printf("Address Mask Request\n"); 10988fae3551SRodney W. Grimes break; 10998fae3551SRodney W. Grimes case ICMP_MASKREPLY: 11008fae3551SRodney W. Grimes (void)printf("Address Mask Reply\n"); 11018fae3551SRodney W. Grimes break; 1102ef9e6dc7SBill Fenner case ICMP_ROUTERADVERT: 1103ef9e6dc7SBill Fenner (void)printf("Router Advertisement\n"); 1104ef9e6dc7SBill Fenner break; 1105ef9e6dc7SBill Fenner case ICMP_ROUTERSOLICIT: 1106ef9e6dc7SBill Fenner (void)printf("Router Solicitation\n"); 1107ef9e6dc7SBill Fenner break; 11088fae3551SRodney W. Grimes default: 11098fae3551SRodney W. Grimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 11108fae3551SRodney W. Grimes } 11118fae3551SRodney W. Grimes } 11128fae3551SRodney W. Grimes 11138fae3551SRodney W. Grimes /* 11148fae3551SRodney W. Grimes * pr_iph -- 11158fae3551SRodney W. Grimes * Print an IP header with options. 11168fae3551SRodney W. Grimes */ 111743470e3bSGarrett Wollman static void 11188fae3551SRodney W. Grimes pr_iph(ip) 11198fae3551SRodney W. Grimes struct ip *ip; 11208fae3551SRodney W. Grimes { 11218fae3551SRodney W. Grimes int hlen; 11228fae3551SRodney W. Grimes u_char *cp; 11238fae3551SRodney W. Grimes 11248fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 11258fae3551SRodney W. Grimes cp = (u_char *)ip + 20; /* point to options */ 11268fae3551SRodney W. Grimes 1127ef9e6dc7SBill Fenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 11288fae3551SRodney W. Grimes (void)printf(" %1x %1x %02x %04x %04x", 1129ef9e6dc7SBill Fenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1130ef9e6dc7SBill Fenner ntohs(ip->ip_id)); 113143470e3bSGarrett Wollman (void)printf(" %1lx %04lx", (ntohl(ip->ip_off) & 0xe000) >> 13, 1132ef9e6dc7SBill Fenner ntohl(ip->ip_off) & 0x1fff); 1133ef9e6dc7SBill Fenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1134ef9e6dc7SBill Fenner ntohs(ip->ip_sum)); 11358fae3551SRodney W. Grimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr)); 11368fae3551SRodney W. Grimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr)); 1137ef9e6dc7SBill Fenner /* dump any option bytes */ 11388fae3551SRodney W. Grimes while (hlen-- > 20) { 11398fae3551SRodney W. Grimes (void)printf("%02x", *cp++); 11408fae3551SRodney W. Grimes } 11418fae3551SRodney W. Grimes (void)putchar('\n'); 11428fae3551SRodney W. Grimes } 11438fae3551SRodney W. Grimes 11448fae3551SRodney W. Grimes /* 11458fae3551SRodney W. Grimes * pr_addr -- 11468fae3551SRodney W. Grimes * Return an ascii host address as a dotted quad and optionally with 11478fae3551SRodney W. Grimes * a hostname. 11488fae3551SRodney W. Grimes */ 114943470e3bSGarrett Wollman static char * 115043470e3bSGarrett Wollman pr_addr(ina) 115143470e3bSGarrett Wollman struct in_addr ina; 11528fae3551SRodney W. Grimes { 11538fae3551SRodney W. Grimes struct hostent *hp; 1154f78ac61bSWarner Losh static char buf[16 + 3 + MAXHOSTNAMELEN]; 11558fae3551SRodney W. Grimes 11568fae3551SRodney W. Grimes if ((options & F_NUMERIC) || 115743470e3bSGarrett Wollman !(hp = gethostbyaddr((char *)&ina, 4, AF_INET))) 115843470e3bSGarrett Wollman return inet_ntoa(ina); 11598fae3551SRodney W. Grimes else 1160efa38539SPeter Wemm (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 116143470e3bSGarrett Wollman inet_ntoa(ina)); 11628fae3551SRodney W. Grimes return(buf); 11638fae3551SRodney W. Grimes } 11648fae3551SRodney W. Grimes 11658fae3551SRodney W. Grimes /* 11668fae3551SRodney W. Grimes * pr_retip -- 11678fae3551SRodney W. Grimes * Dump some info on a returned (via ICMP) IP packet. 11688fae3551SRodney W. Grimes */ 116943470e3bSGarrett Wollman static void 11708fae3551SRodney W. Grimes pr_retip(ip) 11718fae3551SRodney W. Grimes struct ip *ip; 11728fae3551SRodney W. Grimes { 11738fae3551SRodney W. Grimes int hlen; 11748fae3551SRodney W. Grimes u_char *cp; 11758fae3551SRodney W. Grimes 11768fae3551SRodney W. Grimes pr_iph(ip); 11778fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 11788fae3551SRodney W. Grimes cp = (u_char *)ip + hlen; 11798fae3551SRodney W. Grimes 11808fae3551SRodney W. Grimes if (ip->ip_p == 6) 11818fae3551SRodney W. Grimes (void)printf("TCP: from port %u, to port %u (decimal)\n", 11828fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 11838fae3551SRodney W. Grimes else if (ip->ip_p == 17) 11848fae3551SRodney W. Grimes (void)printf("UDP: from port %u, to port %u (decimal)\n", 11858fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 11868fae3551SRodney W. Grimes } 11878fae3551SRodney W. Grimes 118843470e3bSGarrett Wollman static void 11898fae3551SRodney W. Grimes fill(bp, patp) 11908fae3551SRodney W. Grimes char *bp, *patp; 11918fae3551SRodney W. Grimes { 11928fae3551SRodney W. Grimes register int ii, jj, kk; 11938fae3551SRodney W. Grimes int pat[16]; 11948fae3551SRodney W. Grimes char *cp; 11958fae3551SRodney W. Grimes 119643470e3bSGarrett Wollman for (cp = patp; *cp; cp++) { 119743470e3bSGarrett Wollman if (!isxdigit(*cp)) 119843470e3bSGarrett Wollman errx(EX_USAGE, 119943470e3bSGarrett Wollman "patterns must be specified as hex digits"); 120043470e3bSGarrett Wollman 12018fae3551SRodney W. Grimes } 12028fae3551SRodney W. Grimes ii = sscanf(patp, 12038fae3551SRodney W. Grimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 12048fae3551SRodney W. Grimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 12058fae3551SRodney W. Grimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 12068fae3551SRodney W. Grimes &pat[13], &pat[14], &pat[15]); 12078fae3551SRodney W. Grimes 12088fae3551SRodney W. Grimes if (ii > 0) 12098fae3551SRodney W. Grimes for (kk = 0; 12108fae3551SRodney W. Grimes kk <= MAXPACKET - (8 + sizeof(struct timeval) + ii); 12118fae3551SRodney W. Grimes kk += ii) 12128fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 12138fae3551SRodney W. Grimes bp[jj + kk] = pat[jj]; 12148fae3551SRodney W. Grimes if (!(options & F_QUIET)) { 12158fae3551SRodney W. Grimes (void)printf("PATTERN: 0x"); 12168fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 12178fae3551SRodney W. Grimes (void)printf("%02x", bp[jj] & 0xFF); 12188fae3551SRodney W. Grimes (void)printf("\n"); 12198fae3551SRodney W. Grimes } 12208fae3551SRodney W. Grimes } 12218fae3551SRodney W. Grimes 122243470e3bSGarrett Wollman static void 122343470e3bSGarrett Wollman usage(argv0) 122443470e3bSGarrett Wollman const char *argv0; 12258fae3551SRodney W. Grimes { 1226f78ac61bSWarner Losh if (strrchr(argv0,'/')) 1227f78ac61bSWarner Losh argv0 = strrchr(argv0,'/') + 1; 122843470e3bSGarrett Wollman fprintf(stderr, 1229f78ac61bSWarner Losh "usage: %s [-QRadfnqrv] [-c count] [-i wait] [-l preload] " 1230ee2bf734SWarner Losh "[-p pattern]\n [-s packetsize] " 123143470e3bSGarrett Wollman "[host | [-L] [-I iface] [-T ttl] mcast-group]\n", 123243470e3bSGarrett Wollman argv0); 123343470e3bSGarrett Wollman exit(EX_USAGE); 12348fae3551SRodney W. Grimes } 1235