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[] = 48a2a00888SSean Eric Fagan "$Id: ping.c,v 1.23 1997/07/09 20:33:58 julian 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> 7443470e3bSGarrett Wollman #include <netdb.h> 7543470e3bSGarrett Wollman #include <signal.h> 7643470e3bSGarrett Wollman #include <stdio.h> 7743470e3bSGarrett Wollman #include <stdlib.h> 7843470e3bSGarrett Wollman #include <string.h> 7943470e3bSGarrett Wollman #include <sysexits.h> 8043470e3bSGarrett Wollman #include <termios.h> 8143470e3bSGarrett Wollman #include <unistd.h> 8243470e3bSGarrett Wollman 838fae3551SRodney W. Grimes #include <sys/socket.h> 848fae3551SRodney W. Grimes #include <sys/file.h> 858fae3551SRodney W. Grimes #include <sys/time.h> 868fae3551SRodney W. Grimes 878fae3551SRodney W. Grimes #include <netinet/in.h> 8843470e3bSGarrett Wollman #include <netinet/in_systm.h> 898fae3551SRodney W. Grimes #include <netinet/ip.h> 908fae3551SRodney W. Grimes #include <netinet/ip_icmp.h> 918fae3551SRodney W. Grimes #include <netinet/ip_var.h> 9243470e3bSGarrett Wollman #include <arpa/inet.h> 938fae3551SRodney W. Grimes 948fae3551SRodney W. Grimes #define DEFDATALEN (64 - 8) /* default data length */ 958fae3551SRodney W. Grimes #define MAXIPLEN 60 968fae3551SRodney W. Grimes #define MAXICMPLEN 76 978fae3551SRodney W. Grimes #define MAXPACKET (65536 - 60 - 8)/* max packet size */ 988fae3551SRodney W. Grimes #define MAXWAIT 10 /* max seconds to wait for response */ 998fae3551SRodney W. Grimes #define NROUTES 9 /* number of record route slots */ 100515dd2faSJulian Elischer #define FLOOD_BACKOFF 20000 /* usecs to back off if flooding */ 101515dd2faSJulian Elischer /* reports we are out of buffer space */ 1028fae3551SRodney W. Grimes 1038fae3551SRodney W. Grimes #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 1048fae3551SRodney W. Grimes #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 1058fae3551SRodney W. Grimes #define SET(bit) (A(bit) |= B(bit)) 1068fae3551SRodney W. Grimes #define CLR(bit) (A(bit) &= (~B(bit))) 1078fae3551SRodney W. Grimes #define TST(bit) (A(bit) & B(bit)) 1088fae3551SRodney W. Grimes 1098fae3551SRodney W. Grimes /* various options */ 1108fae3551SRodney W. Grimes int options; 11185456935SBill Fenner #define F_FLOOD 0x0001 11285456935SBill Fenner #define F_INTERVAL 0x0002 11385456935SBill Fenner #define F_NUMERIC 0x0004 11485456935SBill Fenner #define F_PINGFILLED 0x0008 11585456935SBill Fenner #define F_QUIET 0x0010 11685456935SBill Fenner #define F_RROUTE 0x0020 11785456935SBill Fenner #define F_SO_DEBUG 0x0040 11885456935SBill Fenner #define F_SO_DONTROUTE 0x0080 11985456935SBill Fenner #define F_VERBOSE 0x0100 12085456935SBill Fenner #define F_QUIET2 0x0200 12185456935SBill Fenner #define F_NOLOOP 0x0400 12285456935SBill Fenner #define F_MTTL 0x0800 12385456935SBill Fenner #define F_MIF 0x1000 124772dfa72SDaniel O'Callaghan #define F_AUDIBLE 0x2000 1258fae3551SRodney W. Grimes 1268fae3551SRodney W. Grimes /* 1278fae3551SRodney W. Grimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 1288fae3551SRodney W. Grimes * number of received sequence numbers we can keep track of. Change 128 1298fae3551SRodney W. Grimes * to 8192 for complete accuracy... 1308fae3551SRodney W. Grimes */ 1318fae3551SRodney W. Grimes #define MAX_DUP_CHK (8 * 128) 1328fae3551SRodney W. Grimes int mx_dup_ck = MAX_DUP_CHK; 1338fae3551SRodney W. Grimes char rcvd_tbl[MAX_DUP_CHK / 8]; 1348fae3551SRodney W. Grimes 1358fae3551SRodney W. Grimes struct sockaddr whereto; /* who to ping */ 1368fae3551SRodney W. Grimes int datalen = DEFDATALEN; 1378fae3551SRodney W. Grimes int s; /* socket file descriptor */ 1388fae3551SRodney W. Grimes u_char outpack[MAXPACKET]; 1398fae3551SRodney W. Grimes char BSPACE = '\b'; /* characters written for flood */ 1408fae3551SRodney W. Grimes char DOT = '.'; 1418fae3551SRodney W. Grimes char *hostname; 1428fae3551SRodney W. Grimes int ident; /* process id to identify our packets */ 143ee2bf734SWarner Losh int uid; /* cached uid for micro-optimization */ 1448fae3551SRodney W. Grimes 1458fae3551SRodney W. Grimes /* counters */ 1468fae3551SRodney W. Grimes long npackets; /* max packets to transmit */ 1478fae3551SRodney W. Grimes long nreceived; /* # of packets we got back */ 1488fae3551SRodney W. Grimes long nrepeats; /* number of duplicates */ 1498fae3551SRodney W. Grimes long ntransmitted; /* sequence # for outbound packets = #sent */ 1508fae3551SRodney W. Grimes int interval = 1; /* interval between packets */ 151515dd2faSJulian Elischer int finish_up = 0; /* We've been told to finish up */ 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 */ 1588fae3551SRodney W. Grimes 159badd8138SSean Eric Fagan int reset_kerninfo; 16037e5b2c6SSean Eric Fagan sig_atomic_t siginfo_p; 161badd8138SSean Eric Fagan 16243470e3bSGarrett Wollman static void fill(char *, char *); 16343470e3bSGarrett Wollman static u_short in_cksum(u_short *, int); 16443470e3bSGarrett Wollman static void catcher(int sig); 16543470e3bSGarrett Wollman static void check_status(void); 166515dd2faSJulian Elischer static void stopit(int); 16743470e3bSGarrett Wollman static void finish(int) __dead2; 16843470e3bSGarrett Wollman static void pinger(void); 16943470e3bSGarrett Wollman static char *pr_addr(struct in_addr); 17043470e3bSGarrett Wollman static void pr_icmph(struct icmp *); 17143470e3bSGarrett Wollman static void pr_iph(struct ip *); 17243470e3bSGarrett Wollman static void pr_pack(char *, int, struct sockaddr_in *); 17343470e3bSGarrett Wollman static void pr_retip(struct ip *); 17443470e3bSGarrett Wollman static void status(int); 17543470e3bSGarrett Wollman static void tvsub(struct timeval *, struct timeval *); 17643470e3bSGarrett Wollman static void usage(const char *) __dead2; 1778fae3551SRodney W. Grimes 17843470e3bSGarrett Wollman int 1798fae3551SRodney W. Grimes main(argc, argv) 1808fae3551SRodney W. Grimes int argc; 18143470e3bSGarrett Wollman char *const *argv; 1828fae3551SRodney W. Grimes { 1838fae3551SRodney W. Grimes struct timeval timeout; 1848fae3551SRodney W. Grimes struct hostent *hp; 1858fae3551SRodney W. Grimes struct sockaddr_in *to; 186badd8138SSean Eric Fagan struct termios ts; 1878fae3551SRodney W. Grimes register int i; 188f1284d7aSBill Fenner int ch, fdmask, hold, packlen, preload, sockerrno; 18985456935SBill Fenner struct in_addr ifaddr; 19085456935SBill Fenner unsigned char ttl, loop; 1918fae3551SRodney W. Grimes u_char *datap, *packet; 19243470e3bSGarrett Wollman char *target, hnamebuf[MAXHOSTNAMELEN]; 19343470e3bSGarrett Wollman char *ep; 19443470e3bSGarrett Wollman u_long ultmp; 1958fae3551SRodney W. Grimes #ifdef IP_OPTIONS 1968fae3551SRodney W. Grimes char rspace[3 + 4 * NROUTES + 1]; /* record route space */ 1978fae3551SRodney W. Grimes #endif 19837e5b2c6SSean Eric Fagan struct sigaction si_sa; 1998fae3551SRodney W. Grimes 200f1284d7aSBill Fenner /* 201f1284d7aSBill Fenner * Do the stuff that we need root priv's for *first*, and 202f1284d7aSBill Fenner * then drop our setuid bit. Save error reporting for 203f1284d7aSBill Fenner * after arg parsing. 204f1284d7aSBill Fenner */ 20543470e3bSGarrett Wollman s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 206f1284d7aSBill Fenner sockerrno = errno; 207f1284d7aSBill Fenner 208f1284d7aSBill Fenner setuid(getuid()); 209ee2bf734SWarner Losh uid = getuid(); 210f1284d7aSBill Fenner 2118fae3551SRodney W. Grimes preload = 0; 212badd8138SSean Eric Fagan 2138fae3551SRodney W. Grimes datap = &outpack[8 + sizeof(struct timeval)]; 214f78ac61bSWarner Losh while ((ch = getopt(argc, argv, "I:LQRT:c:adfi:l:np:qrs:v")) != -1) { 2158fae3551SRodney W. Grimes switch(ch) { 216772dfa72SDaniel O'Callaghan case 'a': 217772dfa72SDaniel O'Callaghan options |= F_AUDIBLE; 218772dfa72SDaniel O'Callaghan break; 2198fae3551SRodney W. Grimes case 'c': 22043470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 22143470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp) 22243470e3bSGarrett Wollman errx(EX_USAGE, 22343470e3bSGarrett Wollman "invalid count of packets to transmit: `%s'", 22443470e3bSGarrett Wollman optarg); 22543470e3bSGarrett Wollman npackets = ultmp; 2268fae3551SRodney W. Grimes break; 2278fae3551SRodney W. Grimes case 'd': 2288fae3551SRodney W. Grimes options |= F_SO_DEBUG; 2298fae3551SRodney W. Grimes break; 2308fae3551SRodney W. Grimes case 'f': 2318fae3551SRodney W. Grimes if (getuid()) { 23243470e3bSGarrett Wollman errno = EPERM; 23343470e3bSGarrett Wollman err(EX_NOPERM, "-f flag"); 2348fae3551SRodney W. Grimes } 2358fae3551SRodney W. Grimes options |= F_FLOOD; 2368fae3551SRodney W. Grimes setbuf(stdout, (char *)NULL); 2378fae3551SRodney W. Grimes break; 2388fae3551SRodney W. Grimes case 'i': /* wait between sending packets */ 23943470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 24043470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > INT_MAX) 24143470e3bSGarrett Wollman errx(EX_USAGE, 24243470e3bSGarrett Wollman "invalid timing interval: `%s'", optarg); 2438fae3551SRodney W. Grimes options |= F_INTERVAL; 24443470e3bSGarrett Wollman interval = ultmp; 2458fae3551SRodney W. Grimes break; 24685456935SBill Fenner case 'I': /* multicast interface */ 24743470e3bSGarrett Wollman if (inet_aton(optarg, &ifaddr) == 0) 24843470e3bSGarrett Wollman errx(EX_USAGE, 24943470e3bSGarrett Wollman "invalid multicast interface: `%s'", 25043470e3bSGarrett Wollman optarg); 25185456935SBill Fenner options |= F_MIF; 25285456935SBill Fenner break; 2538fae3551SRodney W. Grimes case 'l': 25443470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 25543470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > INT_MAX) 25643470e3bSGarrett Wollman errx(EX_USAGE, 25743470e3bSGarrett Wollman "invalid preload value: `%s'", optarg); 258f78ac61bSWarner Losh if (getuid()) { 259f78ac61bSWarner Losh errno = EPERM; 260f78ac61bSWarner Losh err(EX_NOPERM, "-l flag"); 261f78ac61bSWarner Losh } 262f78ac61bSWarner Losh options |= F_FLOOD; 26343470e3bSGarrett Wollman preload = ultmp; 2648fae3551SRodney W. Grimes break; 26585456935SBill Fenner case 'L': 26685456935SBill Fenner options |= F_NOLOOP; 26785456935SBill Fenner loop = 0; 26885456935SBill Fenner break; 2698fae3551SRodney W. Grimes case 'n': 2708fae3551SRodney W. Grimes options |= F_NUMERIC; 2718fae3551SRodney W. Grimes break; 2728fae3551SRodney W. Grimes case 'p': /* fill buffer with user pattern */ 2738fae3551SRodney W. Grimes options |= F_PINGFILLED; 2748fae3551SRodney W. Grimes fill((char *)datap, optarg); 2758fae3551SRodney W. Grimes break; 276ef9e6dc7SBill Fenner case 'Q': 277ef9e6dc7SBill Fenner options |= F_QUIET2; 278ef9e6dc7SBill Fenner break; 2798fae3551SRodney W. Grimes case 'q': 2808fae3551SRodney W. Grimes options |= F_QUIET; 2818fae3551SRodney W. Grimes break; 2828fae3551SRodney W. Grimes case 'R': 2838fae3551SRodney W. Grimes options |= F_RROUTE; 2848fae3551SRodney W. Grimes break; 2858fae3551SRodney W. Grimes case 'r': 2868fae3551SRodney W. Grimes options |= F_SO_DONTROUTE; 2878fae3551SRodney W. Grimes break; 2888fae3551SRodney W. Grimes case 's': /* size of packet to send */ 28943470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 29043470e3bSGarrett Wollman if (ultmp > MAXPACKET) 29143470e3bSGarrett Wollman errx(EX_USAGE, "packet size too large: %lu", 29243470e3bSGarrett Wollman ultmp); 29343470e3bSGarrett Wollman if (*ep || ep == optarg || !ultmp) 29443470e3bSGarrett Wollman errx(EX_USAGE, "invalid packet size: `%s'", 29543470e3bSGarrett Wollman optarg); 29643470e3bSGarrett Wollman datalen = ultmp; 2978fae3551SRodney W. Grimes break; 29885456935SBill Fenner case 'T': /* multicast TTL */ 29943470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 30043470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > 255) 30143470e3bSGarrett Wollman errx(EX_USAGE, "invalid multicast TTL: `%s'", 30243470e3bSGarrett Wollman optarg); 30343470e3bSGarrett Wollman ttl = ultmp; 30485456935SBill Fenner options |= F_MTTL; 30585456935SBill Fenner break; 3068fae3551SRodney W. Grimes case 'v': 3078fae3551SRodney W. Grimes options |= F_VERBOSE; 3088fae3551SRodney W. Grimes break; 3098fae3551SRodney W. Grimes default: 3108fae3551SRodney W. Grimes 31143470e3bSGarrett Wollman usage(argv[0]); 31243470e3bSGarrett Wollman } 31343470e3bSGarrett Wollman } 31443470e3bSGarrett Wollman 31543470e3bSGarrett Wollman if (argc - optind != 1) 31643470e3bSGarrett Wollman usage(argv[0]); 31743470e3bSGarrett Wollman target = argv[optind]; 3188fae3551SRodney W. Grimes 3198fae3551SRodney W. Grimes bzero((char *)&whereto, sizeof(struct sockaddr)); 3208fae3551SRodney W. Grimes to = (struct sockaddr_in *)&whereto; 3218fae3551SRodney W. Grimes to->sin_family = AF_INET; 32243470e3bSGarrett Wollman if (inet_aton(target, &to->sin_addr) != 0) { 3238fae3551SRodney W. Grimes hostname = target; 32443470e3bSGarrett Wollman } else { 32543470e3bSGarrett Wollman hp = gethostbyname2(target, AF_INET); 32643470e3bSGarrett Wollman if (!hp) 32743470e3bSGarrett Wollman errx(EX_NOHOST, "cannot resolve %s: %s", 32843470e3bSGarrett Wollman target, hstrerror(h_errno)); 32943470e3bSGarrett Wollman 33043470e3bSGarrett Wollman to->sin_len = sizeof *to; 3311ffae4a6SWarner Losh if (hp->h_length > sizeof(to->sin_addr)) 3321ffae4a6SWarner Losh errx(1,"gethostbyname2 returned an illegal address"); 33343470e3bSGarrett Wollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 3348fae3551SRodney W. Grimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 33543470e3bSGarrett Wollman hnamebuf[(sizeof hnamebuf) - 1] = '\0'; 3368fae3551SRodney W. Grimes hostname = hnamebuf; 3378fae3551SRodney W. Grimes } 3388fae3551SRodney W. Grimes 33943470e3bSGarrett Wollman if (options & F_FLOOD && options & F_INTERVAL) 34043470e3bSGarrett Wollman errx(EX_USAGE, "-f and -i: incompatible options"); 34143470e3bSGarrett Wollman 34243470e3bSGarrett Wollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 34343470e3bSGarrett Wollman errx(EX_USAGE, 34443470e3bSGarrett Wollman "-f flag cannot be used with multicast destination"); 34543470e3bSGarrett Wollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 34643470e3bSGarrett Wollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 34743470e3bSGarrett Wollman errx(EX_USAGE, 34843470e3bSGarrett Wollman "-I, -L, -T flags cannot be used with unicast destination"); 3498fae3551SRodney W. Grimes 3508fae3551SRodney W. Grimes if (datalen >= sizeof(struct timeval)) /* can we time transfer */ 3518fae3551SRodney W. Grimes timing = 1; 3528fae3551SRodney W. Grimes packlen = datalen + MAXIPLEN + MAXICMPLEN; 35343470e3bSGarrett Wollman if (!(packet = (u_char *)malloc((size_t)packlen))) 35443470e3bSGarrett Wollman err(EX_UNAVAILABLE, "malloc"); 35543470e3bSGarrett Wollman 3568fae3551SRodney W. Grimes if (!(options & F_PINGFILLED)) 3578fae3551SRodney W. Grimes for (i = 8; i < datalen; ++i) 3588fae3551SRodney W. Grimes *datap++ = i; 3598fae3551SRodney W. Grimes 3608fae3551SRodney W. Grimes ident = getpid() & 0xFFFF; 3618fae3551SRodney W. Grimes 362f1284d7aSBill Fenner if (s < 0) { 363f1284d7aSBill Fenner errno = sockerrno; 36443470e3bSGarrett Wollman err(EX_OSERR, "socket"); 3658fae3551SRodney W. Grimes } 3668fae3551SRodney W. Grimes hold = 1; 3678fae3551SRodney W. Grimes if (options & F_SO_DEBUG) 3688fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold, 3698fae3551SRodney W. Grimes sizeof(hold)); 3708fae3551SRodney W. Grimes if (options & F_SO_DONTROUTE) 3718fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 3728fae3551SRodney W. Grimes sizeof(hold)); 3738fae3551SRodney W. Grimes 3748fae3551SRodney W. Grimes /* record route option */ 3758fae3551SRodney W. Grimes if (options & F_RROUTE) { 3768fae3551SRodney W. Grimes #ifdef IP_OPTIONS 3778fae3551SRodney W. Grimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 3788fae3551SRodney W. Grimes rspace[IPOPT_OLEN] = sizeof(rspace)-1; 3798fae3551SRodney W. Grimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 3808fae3551SRodney W. Grimes if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace, 38143470e3bSGarrett Wollman sizeof(rspace)) < 0) 38243470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 3838fae3551SRodney W. Grimes #else 38443470e3bSGarrett Wollman errx(EX_UNAVAILABLE, 38543470e3bSGarrett Wollman "record route not available in this implementation"); 3868fae3551SRodney W. Grimes #endif /* IP_OPTIONS */ 3878fae3551SRodney W. Grimes } 3888fae3551SRodney W. Grimes 38985456935SBill Fenner if (options & F_NOLOOP) { 39085456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 39185456935SBill Fenner sizeof(loop)) < 0) { 39243470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 39385456935SBill Fenner } 39485456935SBill Fenner } 39585456935SBill Fenner if (options & F_MTTL) { 39685456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, 39785456935SBill Fenner sizeof(ttl)) < 0) { 39843470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 39985456935SBill Fenner } 40085456935SBill Fenner } 40185456935SBill Fenner if (options & F_MIF) { 40285456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 40385456935SBill Fenner sizeof(ifaddr)) < 0) { 40443470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 40585456935SBill Fenner } 40685456935SBill Fenner } 40785456935SBill Fenner 4088fae3551SRodney W. Grimes /* 4098fae3551SRodney W. Grimes * When pinging the broadcast address, you can get a lot of answers. 4108fae3551SRodney W. Grimes * Doing something so evil is useful if you are trying to stress the 4118fae3551SRodney W. Grimes * ethernet, or just want to fill the arp cache to get some stuff for 41243470e3bSGarrett Wollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 41343470e3bSGarrett Wollman * or multicast pings if they wish. 4148fae3551SRodney W. Grimes */ 4158fae3551SRodney W. Grimes hold = 48 * 1024; 4168fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 4178fae3551SRodney W. Grimes sizeof(hold)); 4188fae3551SRodney W. Grimes 4198fae3551SRodney W. Grimes if (to->sin_family == AF_INET) 4208fae3551SRodney W. Grimes (void)printf("PING %s (%s): %d data bytes\n", hostname, 42143470e3bSGarrett Wollman inet_ntoa(to->sin_addr), 4228fae3551SRodney W. Grimes datalen); 4238fae3551SRodney W. Grimes else 4248fae3551SRodney W. Grimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 4258fae3551SRodney W. Grimes 4267d81b35cSBruce Evans /* 427a2a00888SSean Eric Fagan * Use sigaction() instead of signal() to get unambiguous semantics, 428a2a00888SSean Eric Fagan * in particular with SA_RESTART not set. 4297d81b35cSBruce Evans */ 430a2a00888SSean Eric Fagan 431f6bd468bSPaul Traina sigemptyset(&si_sa.sa_mask); 43237e5b2c6SSean Eric Fagan si_sa.sa_flags = 0; 433a2a00888SSean Eric Fagan 434a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 435a2a00888SSean Eric Fagan if (sigaction(SIGINT, &si_sa, 0) == -1) { 436a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGINT"); 437a2a00888SSean Eric Fagan } 438a2a00888SSean Eric Fagan 439a2a00888SSean Eric Fagan si_sa.sa_handler = catcher; 440a2a00888SSean Eric Fagan if (sigaction(SIGALRM, &si_sa, 0) == -1) { 441a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGALRM"); 442a2a00888SSean Eric Fagan } 443a2a00888SSean Eric Fagan 444a2a00888SSean Eric Fagan si_sa.sa_handler = status; 44537e5b2c6SSean Eric Fagan if (sigaction(SIGINFO, &si_sa, 0) == -1) { 446624ff938SWarner Losh err(EX_OSERR, "sigaction"); 44737e5b2c6SSean Eric Fagan } 44837e5b2c6SSean Eric Fagan 4494055611aSSean Eric Fagan if (tcgetattr(STDOUT_FILENO, &ts) != -1) { 4504055611aSSean Eric Fagan reset_kerninfo = !(ts.c_lflag & NOKERNINFO); 4514055611aSSean Eric Fagan ts.c_lflag |= NOKERNINFO; 4524055611aSSean Eric Fagan tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 4534055611aSSean Eric Fagan } 4544055611aSSean Eric Fagan 4558fae3551SRodney W. Grimes while (preload--) /* fire off them quickies */ 4568fae3551SRodney W. Grimes pinger(); 4578fae3551SRodney W. Grimes 4588fae3551SRodney W. Grimes if ((options & F_FLOOD) == 0) 45943470e3bSGarrett Wollman catcher(0); /* start things going */ 4608fae3551SRodney W. Grimes 461515dd2faSJulian Elischer while (finish_up == 0) { 4628fae3551SRodney W. Grimes struct sockaddr_in from; 4638fae3551SRodney W. Grimes register int cc; 4648fae3551SRodney W. Grimes int fromlen; 4658fae3551SRodney W. Grimes 4667d81b35cSBruce Evans check_status(); 4678fae3551SRodney W. Grimes if (options & F_FLOOD) { 4688fae3551SRodney W. Grimes pinger(); 4698fae3551SRodney W. Grimes timeout.tv_sec = 0; 4708fae3551SRodney W. Grimes timeout.tv_usec = 10000; 4718fae3551SRodney W. Grimes fdmask = 1 << s; 4728fae3551SRodney W. Grimes if (select(s + 1, (fd_set *)&fdmask, (fd_set *)NULL, 4738fae3551SRodney W. Grimes (fd_set *)NULL, &timeout) < 1) 4748fae3551SRodney W. Grimes continue; 4758fae3551SRodney W. Grimes } 4768fae3551SRodney W. Grimes fromlen = sizeof(from); 4778fae3551SRodney W. Grimes if ((cc = recvfrom(s, (char *)packet, packlen, 0, 4788fae3551SRodney W. Grimes (struct sockaddr *)&from, &fromlen)) < 0) { 4798fae3551SRodney W. Grimes if (errno == EINTR) 4808fae3551SRodney W. Grimes continue; 4818fae3551SRodney W. Grimes perror("ping: recvfrom"); 4828fae3551SRodney W. Grimes continue; 4838fae3551SRodney W. Grimes } 4848fae3551SRodney W. Grimes pr_pack((char *)packet, cc, &from); 4858fae3551SRodney W. Grimes if (npackets && nreceived >= npackets) 4868fae3551SRodney W. Grimes break; 4878fae3551SRodney W. Grimes } 48843470e3bSGarrett Wollman finish(0); 4898fae3551SRodney W. Grimes /* NOTREACHED */ 490f78ac61bSWarner Losh exit(0); /* Make the compiler happy */ 4918fae3551SRodney W. Grimes } 4928fae3551SRodney W. Grimes 4938fae3551SRodney W. Grimes /* 494515dd2faSJulian Elischer * Stopit -- 495515dd2faSJulian Elischer * 496515dd2faSJulian Elischer * set the global bit that cause everything to quit.. 497515dd2faSJulian Elischer * do rNOT quit and exit from the signal handler! 498515dd2faSJulian Elischer */ 499515dd2faSJulian Elischer void 500515dd2faSJulian Elischer stopit(int ignored) 501515dd2faSJulian Elischer { 502515dd2faSJulian Elischer finish_up = 1; 503515dd2faSJulian Elischer } 504515dd2faSJulian Elischer 505515dd2faSJulian Elischer 506515dd2faSJulian Elischer /* 5078fae3551SRodney W. Grimes * catcher -- 5088fae3551SRodney W. Grimes * This routine causes another PING to be transmitted, and then 5098fae3551SRodney W. Grimes * schedules another SIGALRM for 1 second from now. 5108fae3551SRodney W. Grimes * 5118fae3551SRodney W. Grimes * bug -- 5128fae3551SRodney W. Grimes * Our sense of time will slowly skew (i.e., packets will not be 5138fae3551SRodney W. Grimes * launched exactly at 1-second intervals). This does not affect the 5148fae3551SRodney W. Grimes * quality of the delay and loss statistics. 5158fae3551SRodney W. Grimes */ 51643470e3bSGarrett Wollman static void 51743470e3bSGarrett Wollman catcher(int sig) 5188fae3551SRodney W. Grimes { 5198fae3551SRodney W. Grimes int waittime; 520a2a00888SSean Eric Fagan struct sigaction si_sa; 5218fae3551SRodney W. Grimes 5228fae3551SRodney W. Grimes pinger(); 523a2a00888SSean Eric Fagan 5248fae3551SRodney W. Grimes if (!npackets || ntransmitted < npackets) 525a2a00888SSean Eric Fagan (void)alarm((u_int)interval); 5268fae3551SRodney W. Grimes else { 5278fae3551SRodney W. Grimes if (nreceived) { 5288fae3551SRodney W. Grimes waittime = 2 * tmax / 1000; 5298fae3551SRodney W. Grimes if (!waittime) 5308fae3551SRodney W. Grimes waittime = 1; 5318fae3551SRodney W. Grimes } else 5328fae3551SRodney W. Grimes waittime = MAXWAIT; 533a2a00888SSean Eric Fagan 534a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 535a2a00888SSean Eric Fagan sigemptyset(&si_sa.sa_mask); 536a2a00888SSean Eric Fagan si_sa.sa_flags = 0; 537a2a00888SSean Eric Fagan if (sigaction(SIGALRM, &si_sa, 0) == -1) { 538a2a00888SSean Eric Fagan finish_up = 1; 539a2a00888SSean Eric Fagan return; 540a2a00888SSean Eric Fagan } 5418fae3551SRodney W. Grimes (void)alarm((u_int)waittime); 5428fae3551SRodney W. Grimes } 5438fae3551SRodney W. Grimes } 5448fae3551SRodney W. Grimes 5458fae3551SRodney W. Grimes /* 5468fae3551SRodney W. Grimes * pinger -- 5478fae3551SRodney W. Grimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 5488fae3551SRodney W. Grimes * will be added on by the kernel. The ID field is our UNIX process ID, 5498fae3551SRodney W. Grimes * and the sequence number is an ascending integer. The first 8 bytes 550ef9e6dc7SBill Fenner * of the data portion are used to hold a UNIX "timeval" struct in host 5518fae3551SRodney W. Grimes * byte-order, to compute the round-trip time. 5528fae3551SRodney W. Grimes */ 55343470e3bSGarrett Wollman static void 55443470e3bSGarrett Wollman pinger(void) 5558fae3551SRodney W. Grimes { 5568fae3551SRodney W. Grimes register struct icmp *icp; 5578fae3551SRodney W. Grimes register int cc; 5588fae3551SRodney W. Grimes int i; 5598fae3551SRodney W. Grimes 5608fae3551SRodney W. Grimes icp = (struct icmp *)outpack; 5618fae3551SRodney W. Grimes icp->icmp_type = ICMP_ECHO; 5628fae3551SRodney W. Grimes icp->icmp_code = 0; 5638fae3551SRodney W. Grimes icp->icmp_cksum = 0; 5640e59c641SJulian Elischer icp->icmp_seq = ntransmitted; 5658fae3551SRodney W. Grimes icp->icmp_id = ident; /* ID */ 5668fae3551SRodney W. Grimes 5678fae3551SRodney W. Grimes CLR(icp->icmp_seq % mx_dup_ck); 5688fae3551SRodney W. Grimes 5698fae3551SRodney W. Grimes if (timing) 5708fae3551SRodney W. Grimes (void)gettimeofday((struct timeval *)&outpack[8], 5718fae3551SRodney W. Grimes (struct timezone *)NULL); 5728fae3551SRodney W. Grimes 5738fae3551SRodney W. Grimes cc = datalen + 8; /* skips ICMP portion */ 5748fae3551SRodney W. Grimes 5758fae3551SRodney W. Grimes /* compute ICMP checksum here */ 5768fae3551SRodney W. Grimes icp->icmp_cksum = in_cksum((u_short *)icp, cc); 5778fae3551SRodney W. Grimes 5788fae3551SRodney W. Grimes i = sendto(s, (char *)outpack, cc, 0, &whereto, 5798fae3551SRodney W. Grimes sizeof(struct sockaddr)); 5808fae3551SRodney W. Grimes 5818fae3551SRodney W. Grimes if (i < 0 || i != cc) { 58243470e3bSGarrett Wollman if (i < 0) { 583515dd2faSJulian Elischer if ((options & F_FLOOD) && (errno == ENOBUFS)) { 584515dd2faSJulian Elischer usleep(FLOOD_BACKOFF); 585515dd2faSJulian Elischer return; 586515dd2faSJulian Elischer } 58743470e3bSGarrett Wollman warn("sendto"); 58843470e3bSGarrett Wollman } else { 58943470e3bSGarrett Wollman warn("%s: partial write: %d of %d bytes", 5908fae3551SRodney W. Grimes hostname, cc, i); 5918fae3551SRodney W. Grimes } 5920e59c641SJulian Elischer } else { 5930e59c641SJulian Elischer ntransmitted++; /* only count those that made it out */ 59443470e3bSGarrett Wollman } 5958fae3551SRodney W. Grimes if (!(options & F_QUIET) && options & F_FLOOD) 5968fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &DOT, 1); 5978fae3551SRodney W. Grimes } 5988fae3551SRodney W. Grimes 5998fae3551SRodney W. Grimes /* 6008fae3551SRodney W. Grimes * pr_pack -- 6018fae3551SRodney W. Grimes * Print out the packet, if it came from us. This logic is necessary 6028fae3551SRodney W. Grimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 6038fae3551SRodney W. Grimes * which arrive ('tis only fair). This permits multiple copies of this 6048fae3551SRodney W. Grimes * program to be run without having intermingled output (or statistics!). 6058fae3551SRodney W. Grimes */ 60643470e3bSGarrett Wollman static void 6078fae3551SRodney W. Grimes pr_pack(buf, cc, from) 6088fae3551SRodney W. Grimes char *buf; 6098fae3551SRodney W. Grimes int cc; 6108fae3551SRodney W. Grimes struct sockaddr_in *from; 6118fae3551SRodney W. Grimes { 6128fae3551SRodney W. Grimes register struct icmp *icp; 6138fae3551SRodney W. Grimes register u_long l; 6148fae3551SRodney W. Grimes register int i, j; 6158fae3551SRodney W. Grimes register u_char *cp,*dp; 6168fae3551SRodney W. Grimes static int old_rrlen; 6178fae3551SRodney W. Grimes static char old_rr[MAX_IPOPTLEN]; 6188fae3551SRodney W. Grimes struct ip *ip; 6198fae3551SRodney W. Grimes struct timeval tv, *tp; 620515dd2faSJulian Elischer double triptime = 0.0; 6218fae3551SRodney W. Grimes int hlen, dupflag; 6228fae3551SRodney W. Grimes 6238fae3551SRodney W. Grimes (void)gettimeofday(&tv, (struct timezone *)NULL); 6248fae3551SRodney W. Grimes 6258fae3551SRodney W. Grimes /* Check the IP header */ 6268fae3551SRodney W. Grimes ip = (struct ip *)buf; 6278fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 6288fae3551SRodney W. Grimes if (cc < hlen + ICMP_MINLEN) { 6298fae3551SRodney W. Grimes if (options & F_VERBOSE) 63043470e3bSGarrett Wollman warn("packet too short (%d bytes) from %s", cc, 63143470e3bSGarrett Wollman inet_ntoa(from->sin_addr)); 6328fae3551SRodney W. Grimes return; 6338fae3551SRodney W. Grimes } 6348fae3551SRodney W. Grimes 6358fae3551SRodney W. Grimes /* Now the ICMP part */ 6368fae3551SRodney W. Grimes cc -= hlen; 6378fae3551SRodney W. Grimes icp = (struct icmp *)(buf + hlen); 6388fae3551SRodney W. Grimes if (icp->icmp_type == ICMP_ECHOREPLY) { 6398fae3551SRodney W. Grimes if (icp->icmp_id != ident) 6408fae3551SRodney W. Grimes return; /* 'Twas not our ECHO */ 6418fae3551SRodney W. Grimes ++nreceived; 6428fae3551SRodney W. Grimes if (timing) { 6438fae3551SRodney W. Grimes #ifndef icmp_data 6448fae3551SRodney W. Grimes tp = (struct timeval *)&icp->icmp_ip; 6458fae3551SRodney W. Grimes #else 6468fae3551SRodney W. Grimes tp = (struct timeval *)icp->icmp_data; 6478fae3551SRodney W. Grimes #endif 6488fae3551SRodney W. Grimes tvsub(&tv, tp); 6498fae3551SRodney W. Grimes triptime = ((double)tv.tv_sec) * 1000.0 + 6508fae3551SRodney W. Grimes ((double)tv.tv_usec) / 1000.0; 6518fae3551SRodney W. Grimes tsum += triptime; 6528fae3551SRodney W. Grimes if (triptime < tmin) 6538fae3551SRodney W. Grimes tmin = triptime; 6548fae3551SRodney W. Grimes if (triptime > tmax) 6558fae3551SRodney W. Grimes tmax = triptime; 6568fae3551SRodney W. Grimes } 6578fae3551SRodney W. Grimes 6588fae3551SRodney W. Grimes if (TST(icp->icmp_seq % mx_dup_ck)) { 6598fae3551SRodney W. Grimes ++nrepeats; 6608fae3551SRodney W. Grimes --nreceived; 6618fae3551SRodney W. Grimes dupflag = 1; 6628fae3551SRodney W. Grimes } else { 6638fae3551SRodney W. Grimes SET(icp->icmp_seq % mx_dup_ck); 6648fae3551SRodney W. Grimes dupflag = 0; 6658fae3551SRodney W. Grimes } 6668fae3551SRodney W. Grimes 6678fae3551SRodney W. Grimes if (options & F_QUIET) 6688fae3551SRodney W. Grimes return; 6698fae3551SRodney W. Grimes 6708fae3551SRodney W. Grimes if (options & F_FLOOD) 6718fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &BSPACE, 1); 6728fae3551SRodney W. Grimes else { 6738fae3551SRodney W. Grimes (void)printf("%d bytes from %s: icmp_seq=%u", cc, 6748fae3551SRodney W. Grimes inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr), 6758fae3551SRodney W. Grimes icp->icmp_seq); 6768fae3551SRodney W. Grimes (void)printf(" ttl=%d", ip->ip_ttl); 6778fae3551SRodney W. Grimes if (timing) 678d410b6f1SDavid Greenman (void)printf(" time=%.3f ms", triptime); 6798fae3551SRodney W. Grimes if (dupflag) 6808fae3551SRodney W. Grimes (void)printf(" (DUP!)"); 681772dfa72SDaniel O'Callaghan if (options & F_AUDIBLE) 682772dfa72SDaniel O'Callaghan (void)printf("\a"); 6838fae3551SRodney W. Grimes /* check the data */ 6848fae3551SRodney W. Grimes cp = (u_char*)&icp->icmp_data[8]; 6858fae3551SRodney W. Grimes dp = &outpack[8 + sizeof(struct timeval)]; 6868fae3551SRodney W. Grimes for (i = 8; i < datalen; ++i, ++cp, ++dp) { 6878fae3551SRodney W. Grimes if (*cp != *dp) { 6888fae3551SRodney W. Grimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 6898fae3551SRodney W. Grimes i, *dp, *cp); 6908fae3551SRodney W. Grimes cp = (u_char*)&icp->icmp_data[0]; 6918fae3551SRodney W. Grimes for (i = 8; i < datalen; ++i, ++cp) { 6928fae3551SRodney W. Grimes if ((i % 32) == 8) 6938fae3551SRodney W. Grimes (void)printf("\n\t"); 6948fae3551SRodney W. Grimes (void)printf("%x ", *cp); 6958fae3551SRodney W. Grimes } 6968fae3551SRodney W. Grimes break; 6978fae3551SRodney W. Grimes } 6988fae3551SRodney W. Grimes } 6998fae3551SRodney W. Grimes } 7008fae3551SRodney W. Grimes } else { 701ef9e6dc7SBill Fenner /* 702ef9e6dc7SBill Fenner * We've got something other than an ECHOREPLY. 703ef9e6dc7SBill Fenner * See if it's a reply to something that we sent. 704ef9e6dc7SBill Fenner * We can compare IP destination, protocol, 705ef9e6dc7SBill Fenner * and ICMP type and ID. 706f78ac61bSWarner Losh * 707f78ac61bSWarner Losh * Only print all the error messages if we are running 708f78ac61bSWarner Losh * as root to avoid leaking information not normally 709f78ac61bSWarner Losh * available to those not running as root. 710ef9e6dc7SBill Fenner */ 711ef9e6dc7SBill Fenner #ifndef icmp_data 712ef9e6dc7SBill Fenner struct ip *oip = &icp->icmp_ip; 713ef9e6dc7SBill Fenner #else 714ef9e6dc7SBill Fenner struct ip *oip = (struct ip *)icp->icmp_data; 715ef9e6dc7SBill Fenner #endif 716ef9e6dc7SBill Fenner struct icmp *oicmp = (struct icmp *)(oip + 1); 717ef9e6dc7SBill Fenner 718ee2bf734SWarner Losh if (((options & F_VERBOSE) && uid == 0) || 719ef9e6dc7SBill Fenner (!(options & F_QUIET2) && 720ef9e6dc7SBill Fenner (oip->ip_dst.s_addr == 721ef9e6dc7SBill Fenner ((struct sockaddr_in *)&whereto)->sin_addr.s_addr) && 722ef9e6dc7SBill Fenner (oip->ip_p == IPPROTO_ICMP) && 723ef9e6dc7SBill Fenner (oicmp->icmp_type == ICMP_ECHO) && 724ef9e6dc7SBill Fenner (oicmp->icmp_id == ident))) { 7258fae3551SRodney W. Grimes (void)printf("%d bytes from %s: ", cc, 72643470e3bSGarrett Wollman pr_addr(from->sin_addr)); 7278fae3551SRodney W. Grimes pr_icmph(icp); 728ef9e6dc7SBill Fenner } else 729ef9e6dc7SBill Fenner return; 7308fae3551SRodney W. Grimes } 7318fae3551SRodney W. Grimes 7328fae3551SRodney W. Grimes /* Display any IP options */ 7338fae3551SRodney W. Grimes cp = (u_char *)buf + sizeof(struct ip); 7348fae3551SRodney W. Grimes 7358fae3551SRodney W. Grimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 7368fae3551SRodney W. Grimes switch (*cp) { 7378fae3551SRodney W. Grimes case IPOPT_EOL: 7388fae3551SRodney W. Grimes hlen = 0; 7398fae3551SRodney W. Grimes break; 7408fae3551SRodney W. Grimes case IPOPT_LSRR: 7418fae3551SRodney W. Grimes (void)printf("\nLSRR: "); 7428fae3551SRodney W. Grimes hlen -= 2; 7438fae3551SRodney W. Grimes j = *++cp; 7448fae3551SRodney W. Grimes ++cp; 7458fae3551SRodney W. Grimes if (j > IPOPT_MINOFF) 7468fae3551SRodney W. Grimes for (;;) { 7478fae3551SRodney W. Grimes l = *++cp; 7488fae3551SRodney W. Grimes l = (l<<8) + *++cp; 7498fae3551SRodney W. Grimes l = (l<<8) + *++cp; 7508fae3551SRodney W. Grimes l = (l<<8) + *++cp; 75143470e3bSGarrett Wollman if (l == 0) { 75243470e3bSGarrett Wollman printf("\t0.0.0.0"); 75343470e3bSGarrett Wollman } else { 75443470e3bSGarrett Wollman struct in_addr ina; 75543470e3bSGarrett Wollman ina.s_addr = ntohl(l); 75643470e3bSGarrett Wollman printf("\t%s", pr_addr(ina)); 75743470e3bSGarrett Wollman } 7588fae3551SRodney W. Grimes hlen -= 4; 7598fae3551SRodney W. Grimes j -= 4; 7608fae3551SRodney W. Grimes if (j <= IPOPT_MINOFF) 7618fae3551SRodney W. Grimes break; 7628fae3551SRodney W. Grimes (void)putchar('\n'); 7638fae3551SRodney W. Grimes } 7648fae3551SRodney W. Grimes break; 7658fae3551SRodney W. Grimes case IPOPT_RR: 7668fae3551SRodney W. Grimes j = *++cp; /* get length */ 7678fae3551SRodney W. Grimes i = *++cp; /* and pointer */ 7688fae3551SRodney W. Grimes hlen -= 2; 7698fae3551SRodney W. Grimes if (i > j) 7708fae3551SRodney W. Grimes i = j; 7718fae3551SRodney W. Grimes i -= IPOPT_MINOFF; 7728fae3551SRodney W. Grimes if (i <= 0) 7738fae3551SRodney W. Grimes continue; 7748fae3551SRodney W. Grimes if (i == old_rrlen 7758fae3551SRodney W. Grimes && cp == (u_char *)buf + sizeof(struct ip) + 2 7768fae3551SRodney W. Grimes && !bcmp((char *)cp, old_rr, i) 7778fae3551SRodney W. Grimes && !(options & F_FLOOD)) { 7788fae3551SRodney W. Grimes (void)printf("\t(same route)"); 7798fae3551SRodney W. Grimes i = ((i + 3) / 4) * 4; 7808fae3551SRodney W. Grimes hlen -= i; 7818fae3551SRodney W. Grimes cp += i; 7828fae3551SRodney W. Grimes break; 7838fae3551SRodney W. Grimes } 7848fae3551SRodney W. Grimes old_rrlen = i; 7858fae3551SRodney W. Grimes bcopy((char *)cp, old_rr, i); 7868fae3551SRodney W. Grimes (void)printf("\nRR: "); 7878fae3551SRodney W. Grimes for (;;) { 7888fae3551SRodney W. Grimes l = *++cp; 7898fae3551SRodney W. Grimes l = (l<<8) + *++cp; 7908fae3551SRodney W. Grimes l = (l<<8) + *++cp; 7918fae3551SRodney W. Grimes l = (l<<8) + *++cp; 79243470e3bSGarrett Wollman if (l == 0) { 79343470e3bSGarrett Wollman printf("\t0.0.0.0"); 79443470e3bSGarrett Wollman } else { 79543470e3bSGarrett Wollman struct in_addr ina; 79643470e3bSGarrett Wollman ina.s_addr = ntohl(l); 79743470e3bSGarrett Wollman printf("\t%s", pr_addr(ina)); 79843470e3bSGarrett Wollman } 7998fae3551SRodney W. Grimes hlen -= 4; 8008fae3551SRodney W. Grimes i -= 4; 8018fae3551SRodney W. Grimes if (i <= 0) 8028fae3551SRodney W. Grimes break; 8038fae3551SRodney W. Grimes (void)putchar('\n'); 8048fae3551SRodney W. Grimes } 8058fae3551SRodney W. Grimes break; 8068fae3551SRodney W. Grimes case IPOPT_NOP: 8078fae3551SRodney W. Grimes (void)printf("\nNOP"); 8088fae3551SRodney W. Grimes break; 8098fae3551SRodney W. Grimes default: 8108fae3551SRodney W. Grimes (void)printf("\nunknown option %x", *cp); 8118fae3551SRodney W. Grimes break; 8128fae3551SRodney W. Grimes } 8138fae3551SRodney W. Grimes if (!(options & F_FLOOD)) { 8148fae3551SRodney W. Grimes (void)putchar('\n'); 8158fae3551SRodney W. Grimes (void)fflush(stdout); 8168fae3551SRodney W. Grimes } 8178fae3551SRodney W. Grimes } 8188fae3551SRodney W. Grimes 8198fae3551SRodney W. Grimes /* 8208fae3551SRodney W. Grimes * in_cksum -- 8218fae3551SRodney W. Grimes * Checksum routine for Internet Protocol family headers (C Version) 8228fae3551SRodney W. Grimes */ 82343470e3bSGarrett Wollman u_short 8248fae3551SRodney W. Grimes in_cksum(addr, len) 8258fae3551SRodney W. Grimes u_short *addr; 8268fae3551SRodney W. Grimes int len; 8278fae3551SRodney W. Grimes { 8288fae3551SRodney W. Grimes register int nleft = len; 8298fae3551SRodney W. Grimes register u_short *w = addr; 8308fae3551SRodney W. Grimes register int sum = 0; 8318fae3551SRodney W. Grimes u_short answer = 0; 8328fae3551SRodney W. Grimes 8338fae3551SRodney W. Grimes /* 8348fae3551SRodney W. Grimes * Our algorithm is simple, using a 32 bit accumulator (sum), we add 8358fae3551SRodney W. Grimes * sequential 16 bit words to it, and at the end, fold back all the 8368fae3551SRodney W. Grimes * carry bits from the top 16 bits into the lower 16 bits. 8378fae3551SRodney W. Grimes */ 8388fae3551SRodney W. Grimes while (nleft > 1) { 8398fae3551SRodney W. Grimes sum += *w++; 8408fae3551SRodney W. Grimes nleft -= 2; 8418fae3551SRodney W. Grimes } 8428fae3551SRodney W. Grimes 8438fae3551SRodney W. Grimes /* mop up an odd byte, if necessary */ 8448fae3551SRodney W. Grimes if (nleft == 1) { 8458fae3551SRodney W. Grimes *(u_char *)(&answer) = *(u_char *)w ; 8468fae3551SRodney W. Grimes sum += answer; 8478fae3551SRodney W. Grimes } 8488fae3551SRodney W. Grimes 8498fae3551SRodney W. Grimes /* add back carry outs from top 16 bits to low 16 bits */ 8508fae3551SRodney W. Grimes sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 8518fae3551SRodney W. Grimes sum += (sum >> 16); /* add carry */ 8528fae3551SRodney W. Grimes answer = ~sum; /* truncate to 16 bits */ 8538fae3551SRodney W. Grimes return(answer); 8548fae3551SRodney W. Grimes } 8558fae3551SRodney W. Grimes 8568fae3551SRodney W. Grimes /* 8578fae3551SRodney W. Grimes * tvsub -- 8588fae3551SRodney W. Grimes * Subtract 2 timeval structs: out = out - in. Out is assumed to 8598fae3551SRodney W. Grimes * be >= in. 8608fae3551SRodney W. Grimes */ 86143470e3bSGarrett Wollman static void 8628fae3551SRodney W. Grimes tvsub(out, in) 8638fae3551SRodney W. Grimes register struct timeval *out, *in; 8648fae3551SRodney W. Grimes { 8658fae3551SRodney W. Grimes if ((out->tv_usec -= in->tv_usec) < 0) { 8668fae3551SRodney W. Grimes --out->tv_sec; 8678fae3551SRodney W. Grimes out->tv_usec += 1000000; 8688fae3551SRodney W. Grimes } 8698fae3551SRodney W. Grimes out->tv_sec -= in->tv_sec; 8708fae3551SRodney W. Grimes } 8718fae3551SRodney W. Grimes 8728fae3551SRodney W. Grimes /* 873badd8138SSean Eric Fagan * status -- 874badd8138SSean Eric Fagan * Print out statistics when SIGINFO is received. 875badd8138SSean Eric Fagan */ 876badd8138SSean Eric Fagan 87743470e3bSGarrett Wollman static void 8787d81b35cSBruce Evans status(sig) 8797d81b35cSBruce Evans int sig; 8807d81b35cSBruce Evans { 88137e5b2c6SSean Eric Fagan siginfo_p = 1; 88237e5b2c6SSean Eric Fagan } 88337e5b2c6SSean Eric Fagan 88443470e3bSGarrett Wollman static void 88537e5b2c6SSean Eric Fagan check_status() 886badd8138SSean Eric Fagan { 88737e5b2c6SSean Eric Fagan if (siginfo_p) { 88837e5b2c6SSean Eric Fagan siginfo_p = 0; 8897d81b35cSBruce Evans (void)fprintf(stderr, 8907d81b35cSBruce Evans "\r%ld/%ld packets received (%.0f%%) %.3f min / %.3f avg / %.3f max\n", 8917d81b35cSBruce Evans nreceived, ntransmitted, 8927d81b35cSBruce Evans ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0, 8937d81b35cSBruce Evans nreceived ? tmin : 0.0, 8947d81b35cSBruce Evans nreceived + nrepeats ? tsum / (nreceived + nrepeats) : tsum, 8957d81b35cSBruce Evans tmax); 89637e5b2c6SSean Eric Fagan } 897badd8138SSean Eric Fagan } 898badd8138SSean Eric Fagan 899badd8138SSean Eric Fagan /* 9008fae3551SRodney W. Grimes * finish -- 9018fae3551SRodney W. Grimes * Print out statistics, and give up. 9028fae3551SRodney W. Grimes */ 90343470e3bSGarrett Wollman static void 90443470e3bSGarrett Wollman finish(int sig) 9058fae3551SRodney W. Grimes { 906badd8138SSean Eric Fagan struct termios ts; 9078fae3551SRodney W. Grimes 9088fae3551SRodney W. Grimes (void)signal(SIGINT, SIG_IGN); 909a2a00888SSean Eric Fagan (void)signal(SIGALRM, SIG_IGN); 9108fae3551SRodney W. Grimes (void)putchar('\n'); 9118fae3551SRodney W. Grimes (void)fflush(stdout); 9128fae3551SRodney W. Grimes (void)printf("--- %s ping statistics ---\n", hostname); 9138fae3551SRodney W. Grimes (void)printf("%ld packets transmitted, ", ntransmitted); 9148fae3551SRodney W. Grimes (void)printf("%ld packets received, ", nreceived); 9158fae3551SRodney W. Grimes if (nrepeats) 9168fae3551SRodney W. Grimes (void)printf("+%ld duplicates, ", nrepeats); 9178fae3551SRodney W. Grimes if (ntransmitted) 9188fae3551SRodney W. Grimes if (nreceived > ntransmitted) 9198fae3551SRodney W. Grimes (void)printf("-- somebody's printing up packets!"); 9208fae3551SRodney W. Grimes else 9218fae3551SRodney W. Grimes (void)printf("%d%% packet loss", 9228fae3551SRodney W. Grimes (int) (((ntransmitted - nreceived) * 100) / 9238fae3551SRodney W. Grimes ntransmitted)); 9248fae3551SRodney W. Grimes (void)putchar('\n'); 9257d81b35cSBruce Evans if (nreceived && timing) 926d410b6f1SDavid Greenman (void)printf("round-trip min/avg/max = %.3f/%.3f/%.3f ms\n", 9277d81b35cSBruce Evans tmin, tsum / (nreceived + nrepeats), tmax); 9282ceaae21SBruce Evans if (reset_kerninfo && tcgetattr(STDOUT_FILENO, &ts) != -1) { 929badd8138SSean Eric Fagan ts.c_lflag &= ~NOKERNINFO; 9302ceaae21SBruce Evans tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 931badd8138SSean Eric Fagan } 932badd8138SSean Eric Fagan 9336e1173dcSDavid Greenman if (nreceived) 9348fae3551SRodney W. Grimes exit(0); 9356e1173dcSDavid Greenman else 9366e1173dcSDavid Greenman exit(2); 9378fae3551SRodney W. Grimes } 9388fae3551SRodney W. Grimes 9398fae3551SRodney W. Grimes #ifdef notdef 9408fae3551SRodney W. Grimes static char *ttab[] = { 9418fae3551SRodney W. Grimes "Echo Reply", /* ip + seq + udata */ 9428fae3551SRodney W. Grimes "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */ 9438fae3551SRodney W. Grimes "Source Quench", /* IP */ 9448fae3551SRodney W. Grimes "Redirect", /* redirect type, gateway, + IP */ 9458fae3551SRodney W. Grimes "Echo", 9468fae3551SRodney W. Grimes "Time Exceeded", /* transit, frag reassem + IP */ 9478fae3551SRodney W. Grimes "Parameter Problem", /* pointer + IP */ 9488fae3551SRodney W. Grimes "Timestamp", /* id + seq + three timestamps */ 9498fae3551SRodney W. Grimes "Timestamp Reply", /* " */ 9508fae3551SRodney W. Grimes "Info Request", /* id + sq */ 9518fae3551SRodney W. Grimes "Info Reply" /* " */ 9528fae3551SRodney W. Grimes }; 9538fae3551SRodney W. Grimes #endif 9548fae3551SRodney W. Grimes 9558fae3551SRodney W. Grimes /* 9568fae3551SRodney W. Grimes * pr_icmph -- 9578fae3551SRodney W. Grimes * Print a descriptive string about an ICMP header. 9588fae3551SRodney W. Grimes */ 95943470e3bSGarrett Wollman static void 9608fae3551SRodney W. Grimes pr_icmph(icp) 9618fae3551SRodney W. Grimes struct icmp *icp; 9628fae3551SRodney W. Grimes { 9638fae3551SRodney W. Grimes switch(icp->icmp_type) { 9648fae3551SRodney W. Grimes case ICMP_ECHOREPLY: 9658fae3551SRodney W. Grimes (void)printf("Echo Reply\n"); 9668fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 9678fae3551SRodney W. Grimes break; 9688fae3551SRodney W. Grimes case ICMP_UNREACH: 9698fae3551SRodney W. Grimes switch(icp->icmp_code) { 9708fae3551SRodney W. Grimes case ICMP_UNREACH_NET: 9718fae3551SRodney W. Grimes (void)printf("Destination Net Unreachable\n"); 9728fae3551SRodney W. Grimes break; 9738fae3551SRodney W. Grimes case ICMP_UNREACH_HOST: 9748fae3551SRodney W. Grimes (void)printf("Destination Host Unreachable\n"); 9758fae3551SRodney W. Grimes break; 9768fae3551SRodney W. Grimes case ICMP_UNREACH_PROTOCOL: 9778fae3551SRodney W. Grimes (void)printf("Destination Protocol Unreachable\n"); 9788fae3551SRodney W. Grimes break; 9798fae3551SRodney W. Grimes case ICMP_UNREACH_PORT: 9808fae3551SRodney W. Grimes (void)printf("Destination Port Unreachable\n"); 9818fae3551SRodney W. Grimes break; 9828fae3551SRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 983ef9e6dc7SBill Fenner (void)printf("frag needed and DF set (MTU %d)\n", 984ef9e6dc7SBill Fenner icp->icmp_nextmtu); 9858fae3551SRodney W. Grimes break; 9868fae3551SRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 9878fae3551SRodney W. Grimes (void)printf("Source Route Failed\n"); 9888fae3551SRodney W. Grimes break; 989ef9e6dc7SBill Fenner case ICMP_UNREACH_FILTER_PROHIB: 990ef9e6dc7SBill Fenner (void)printf("Communication prohibited by filter\n"); 991ef9e6dc7SBill Fenner break; 9928fae3551SRodney W. Grimes default: 9938fae3551SRodney W. Grimes (void)printf("Dest Unreachable, Bad Code: %d\n", 9948fae3551SRodney W. Grimes icp->icmp_code); 9958fae3551SRodney W. Grimes break; 9968fae3551SRodney W. Grimes } 9978fae3551SRodney W. Grimes /* Print returned IP header information */ 9988fae3551SRodney W. Grimes #ifndef icmp_data 9998fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 10008fae3551SRodney W. Grimes #else 10018fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 10028fae3551SRodney W. Grimes #endif 10038fae3551SRodney W. Grimes break; 10048fae3551SRodney W. Grimes case ICMP_SOURCEQUENCH: 10058fae3551SRodney W. Grimes (void)printf("Source Quench\n"); 10068fae3551SRodney W. Grimes #ifndef icmp_data 10078fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 10088fae3551SRodney W. Grimes #else 10098fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 10108fae3551SRodney W. Grimes #endif 10118fae3551SRodney W. Grimes break; 10128fae3551SRodney W. Grimes case ICMP_REDIRECT: 10138fae3551SRodney W. Grimes switch(icp->icmp_code) { 10148fae3551SRodney W. Grimes case ICMP_REDIRECT_NET: 10158fae3551SRodney W. Grimes (void)printf("Redirect Network"); 10168fae3551SRodney W. Grimes break; 10178fae3551SRodney W. Grimes case ICMP_REDIRECT_HOST: 10188fae3551SRodney W. Grimes (void)printf("Redirect Host"); 10198fae3551SRodney W. Grimes break; 10208fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSNET: 10218fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Network"); 10228fae3551SRodney W. Grimes break; 10238fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSHOST: 10248fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Host"); 10258fae3551SRodney W. Grimes break; 10268fae3551SRodney W. Grimes default: 10278fae3551SRodney W. Grimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 10288fae3551SRodney W. Grimes break; 10298fae3551SRodney W. Grimes } 10308fae3551SRodney W. Grimes (void)printf("(New addr: 0x%08lx)\n", icp->icmp_gwaddr.s_addr); 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_ECHO: 10388fae3551SRodney W. Grimes (void)printf("Echo Request\n"); 10398fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 10408fae3551SRodney W. Grimes break; 10418fae3551SRodney W. Grimes case ICMP_TIMXCEED: 10428fae3551SRodney W. Grimes switch(icp->icmp_code) { 10438fae3551SRodney W. Grimes case ICMP_TIMXCEED_INTRANS: 10448fae3551SRodney W. Grimes (void)printf("Time to live exceeded\n"); 10458fae3551SRodney W. Grimes break; 10468fae3551SRodney W. Grimes case ICMP_TIMXCEED_REASS: 10478fae3551SRodney W. Grimes (void)printf("Frag reassembly time exceeded\n"); 10488fae3551SRodney W. Grimes break; 10498fae3551SRodney W. Grimes default: 10508fae3551SRodney W. Grimes (void)printf("Time exceeded, Bad Code: %d\n", 10518fae3551SRodney W. Grimes icp->icmp_code); 10528fae3551SRodney W. Grimes break; 10538fae3551SRodney W. Grimes } 10548fae3551SRodney W. Grimes #ifndef icmp_data 10558fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 10568fae3551SRodney W. Grimes #else 10578fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 10588fae3551SRodney W. Grimes #endif 10598fae3551SRodney W. Grimes break; 10608fae3551SRodney W. Grimes case ICMP_PARAMPROB: 10618fae3551SRodney W. Grimes (void)printf("Parameter problem: pointer = 0x%02x\n", 10628fae3551SRodney W. Grimes icp->icmp_hun.ih_pptr); 10638fae3551SRodney W. Grimes #ifndef icmp_data 10648fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 10658fae3551SRodney W. Grimes #else 10668fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 10678fae3551SRodney W. Grimes #endif 10688fae3551SRodney W. Grimes break; 10698fae3551SRodney W. Grimes case ICMP_TSTAMP: 10708fae3551SRodney W. Grimes (void)printf("Timestamp\n"); 10718fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 10728fae3551SRodney W. Grimes break; 10738fae3551SRodney W. Grimes case ICMP_TSTAMPREPLY: 10748fae3551SRodney W. Grimes (void)printf("Timestamp Reply\n"); 10758fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 10768fae3551SRodney W. Grimes break; 10778fae3551SRodney W. Grimes case ICMP_IREQ: 10788fae3551SRodney W. Grimes (void)printf("Information Request\n"); 10798fae3551SRodney W. Grimes /* XXX ID + Seq */ 10808fae3551SRodney W. Grimes break; 10818fae3551SRodney W. Grimes case ICMP_IREQREPLY: 10828fae3551SRodney W. Grimes (void)printf("Information Reply\n"); 10838fae3551SRodney W. Grimes /* XXX ID + Seq */ 10848fae3551SRodney W. Grimes break; 10858fae3551SRodney W. Grimes case ICMP_MASKREQ: 10868fae3551SRodney W. Grimes (void)printf("Address Mask Request\n"); 10878fae3551SRodney W. Grimes break; 10888fae3551SRodney W. Grimes case ICMP_MASKREPLY: 10898fae3551SRodney W. Grimes (void)printf("Address Mask Reply\n"); 10908fae3551SRodney W. Grimes break; 1091ef9e6dc7SBill Fenner case ICMP_ROUTERADVERT: 1092ef9e6dc7SBill Fenner (void)printf("Router Advertisement\n"); 1093ef9e6dc7SBill Fenner break; 1094ef9e6dc7SBill Fenner case ICMP_ROUTERSOLICIT: 1095ef9e6dc7SBill Fenner (void)printf("Router Solicitation\n"); 1096ef9e6dc7SBill Fenner break; 10978fae3551SRodney W. Grimes default: 10988fae3551SRodney W. Grimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 10998fae3551SRodney W. Grimes } 11008fae3551SRodney W. Grimes } 11018fae3551SRodney W. Grimes 11028fae3551SRodney W. Grimes /* 11038fae3551SRodney W. Grimes * pr_iph -- 11048fae3551SRodney W. Grimes * Print an IP header with options. 11058fae3551SRodney W. Grimes */ 110643470e3bSGarrett Wollman static void 11078fae3551SRodney W. Grimes pr_iph(ip) 11088fae3551SRodney W. Grimes struct ip *ip; 11098fae3551SRodney W. Grimes { 11108fae3551SRodney W. Grimes int hlen; 11118fae3551SRodney W. Grimes u_char *cp; 11128fae3551SRodney W. Grimes 11138fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 11148fae3551SRodney W. Grimes cp = (u_char *)ip + 20; /* point to options */ 11158fae3551SRodney W. Grimes 1116ef9e6dc7SBill Fenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 11178fae3551SRodney W. Grimes (void)printf(" %1x %1x %02x %04x %04x", 1118ef9e6dc7SBill Fenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1119ef9e6dc7SBill Fenner ntohs(ip->ip_id)); 112043470e3bSGarrett Wollman (void)printf(" %1lx %04lx", (ntohl(ip->ip_off) & 0xe000) >> 13, 1121ef9e6dc7SBill Fenner ntohl(ip->ip_off) & 0x1fff); 1122ef9e6dc7SBill Fenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1123ef9e6dc7SBill Fenner ntohs(ip->ip_sum)); 11248fae3551SRodney W. Grimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr)); 11258fae3551SRodney W. Grimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr)); 1126ef9e6dc7SBill Fenner /* dump any option bytes */ 11278fae3551SRodney W. Grimes while (hlen-- > 20) { 11288fae3551SRodney W. Grimes (void)printf("%02x", *cp++); 11298fae3551SRodney W. Grimes } 11308fae3551SRodney W. Grimes (void)putchar('\n'); 11318fae3551SRodney W. Grimes } 11328fae3551SRodney W. Grimes 11338fae3551SRodney W. Grimes /* 11348fae3551SRodney W. Grimes * pr_addr -- 11358fae3551SRodney W. Grimes * Return an ascii host address as a dotted quad and optionally with 11368fae3551SRodney W. Grimes * a hostname. 11378fae3551SRodney W. Grimes */ 113843470e3bSGarrett Wollman static char * 113943470e3bSGarrett Wollman pr_addr(ina) 114043470e3bSGarrett Wollman struct in_addr ina; 11418fae3551SRodney W. Grimes { 11428fae3551SRodney W. Grimes struct hostent *hp; 1143f78ac61bSWarner Losh static char buf[16 + 3 + MAXHOSTNAMELEN]; 11448fae3551SRodney W. Grimes 11458fae3551SRodney W. Grimes if ((options & F_NUMERIC) || 114643470e3bSGarrett Wollman !(hp = gethostbyaddr((char *)&ina, 4, AF_INET))) 114743470e3bSGarrett Wollman return inet_ntoa(ina); 11488fae3551SRodney W. Grimes else 1149efa38539SPeter Wemm (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 115043470e3bSGarrett Wollman inet_ntoa(ina)); 11518fae3551SRodney W. Grimes return(buf); 11528fae3551SRodney W. Grimes } 11538fae3551SRodney W. Grimes 11548fae3551SRodney W. Grimes /* 11558fae3551SRodney W. Grimes * pr_retip -- 11568fae3551SRodney W. Grimes * Dump some info on a returned (via ICMP) IP packet. 11578fae3551SRodney W. Grimes */ 115843470e3bSGarrett Wollman static void 11598fae3551SRodney W. Grimes pr_retip(ip) 11608fae3551SRodney W. Grimes struct ip *ip; 11618fae3551SRodney W. Grimes { 11628fae3551SRodney W. Grimes int hlen; 11638fae3551SRodney W. Grimes u_char *cp; 11648fae3551SRodney W. Grimes 11658fae3551SRodney W. Grimes pr_iph(ip); 11668fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 11678fae3551SRodney W. Grimes cp = (u_char *)ip + hlen; 11688fae3551SRodney W. Grimes 11698fae3551SRodney W. Grimes if (ip->ip_p == 6) 11708fae3551SRodney W. Grimes (void)printf("TCP: from port %u, to port %u (decimal)\n", 11718fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 11728fae3551SRodney W. Grimes else if (ip->ip_p == 17) 11738fae3551SRodney W. Grimes (void)printf("UDP: from port %u, to port %u (decimal)\n", 11748fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 11758fae3551SRodney W. Grimes } 11768fae3551SRodney W. Grimes 117743470e3bSGarrett Wollman static void 11788fae3551SRodney W. Grimes fill(bp, patp) 11798fae3551SRodney W. Grimes char *bp, *patp; 11808fae3551SRodney W. Grimes { 11818fae3551SRodney W. Grimes register int ii, jj, kk; 11828fae3551SRodney W. Grimes int pat[16]; 11838fae3551SRodney W. Grimes char *cp; 11848fae3551SRodney W. Grimes 118543470e3bSGarrett Wollman for (cp = patp; *cp; cp++) { 118643470e3bSGarrett Wollman if (!isxdigit(*cp)) 118743470e3bSGarrett Wollman errx(EX_USAGE, 118843470e3bSGarrett Wollman "patterns must be specified as hex digits"); 118943470e3bSGarrett Wollman 11908fae3551SRodney W. Grimes } 11918fae3551SRodney W. Grimes ii = sscanf(patp, 11928fae3551SRodney W. Grimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 11938fae3551SRodney W. Grimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 11948fae3551SRodney W. Grimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 11958fae3551SRodney W. Grimes &pat[13], &pat[14], &pat[15]); 11968fae3551SRodney W. Grimes 11978fae3551SRodney W. Grimes if (ii > 0) 11988fae3551SRodney W. Grimes for (kk = 0; 11998fae3551SRodney W. Grimes kk <= MAXPACKET - (8 + sizeof(struct timeval) + ii); 12008fae3551SRodney W. Grimes kk += ii) 12018fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 12028fae3551SRodney W. Grimes bp[jj + kk] = pat[jj]; 12038fae3551SRodney W. Grimes if (!(options & F_QUIET)) { 12048fae3551SRodney W. Grimes (void)printf("PATTERN: 0x"); 12058fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 12068fae3551SRodney W. Grimes (void)printf("%02x", bp[jj] & 0xFF); 12078fae3551SRodney W. Grimes (void)printf("\n"); 12088fae3551SRodney W. Grimes } 12098fae3551SRodney W. Grimes } 12108fae3551SRodney W. Grimes 121143470e3bSGarrett Wollman static void 121243470e3bSGarrett Wollman usage(argv0) 121343470e3bSGarrett Wollman const char *argv0; 12148fae3551SRodney W. Grimes { 1215f78ac61bSWarner Losh if (strrchr(argv0,'/')) 1216f78ac61bSWarner Losh argv0 = strrchr(argv0,'/') + 1; 121743470e3bSGarrett Wollman fprintf(stderr, 1218f78ac61bSWarner Losh "usage: %s [-QRadfnqrv] [-c count] [-i wait] [-l preload] " 1219ee2bf734SWarner Losh "[-p pattern]\n [-s packetsize] " 122043470e3bSGarrett Wollman "[host | [-L] [-I iface] [-T ttl] mcast-group]\n", 122143470e3bSGarrett Wollman argv0); 122243470e3bSGarrett Wollman exit(EX_USAGE); 12238fae3551SRodney W. Grimes } 1224