ping.c revision 37671
11558Srgrimes/* 21558Srgrimes * Copyright (c) 1989, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * This code is derived from software contributed to Berkeley by 61558Srgrimes * Mike Muuss. 71558Srgrimes * 81558Srgrimes * Redistribution and use in source and binary forms, with or without 91558Srgrimes * modification, are permitted provided that the following conditions 101558Srgrimes * are met: 111558Srgrimes * 1. Redistributions of source code must retain the above copyright 121558Srgrimes * notice, this list of conditions and the following disclaimer. 131558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141558Srgrimes * notice, this list of conditions and the following disclaimer in the 151558Srgrimes * documentation and/or other materials provided with the distribution. 161558Srgrimes * 3. All advertising materials mentioning features or use of this software 171558Srgrimes * must display the following acknowledgement: 181558Srgrimes * This product includes software developed by the University of 191558Srgrimes * California, Berkeley and its contributors. 201558Srgrimes * 4. Neither the name of the University nor the names of its contributors 211558Srgrimes * may be used to endorse or promote products derived from this software 221558Srgrimes * without specific prior written permission. 231558Srgrimes * 241558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341558Srgrimes * SUCH DAMAGE. 351558Srgrimes */ 361558Srgrimes 371558Srgrimes#ifndef lint 3823247Swollmanstatic const char copyright[] = 391558Srgrimes"@(#) Copyright (c) 1989, 1993\n\ 401558Srgrimes The Regents of the University of California. All rights reserved.\n"; 411558Srgrimes#endif /* not lint */ 421558Srgrimes 431558Srgrimes#ifndef lint 4437671Scharnier#if 0 451558Srgrimesstatic char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93"; 4637671Scharnier#endif 4723247Swollmanstatic const char rcsid[] = 4837671Scharnier "$Id$"; 491558Srgrimes#endif /* not lint */ 501558Srgrimes 511558Srgrimes/* 521558Srgrimes * P I N G . C 531558Srgrimes * 5437671Scharnier * Using the Internet Control Message Protocol (ICMP) "ECHO" facility, 551558Srgrimes * measure round-trip-delays and packet loss across network paths. 561558Srgrimes * 571558Srgrimes * Author - 581558Srgrimes * Mike Muuss 591558Srgrimes * U. S. Army Ballistic Research Laboratory 601558Srgrimes * December, 1983 611558Srgrimes * 621558Srgrimes * Status - 631558Srgrimes * Public Domain. Distribution Unlimited. 641558Srgrimes * Bugs - 651558Srgrimes * More statistics could always be gathered. 661558Srgrimes * This program has to run SUID to ROOT to access the ICMP socket. 671558Srgrimes */ 681558Srgrimes 6923247Swollman#include <sys/param.h> /* NB: we rely on this for <sys/types.h> */ 7023247Swollman 7123247Swollman#include <ctype.h> 7223247Swollman#include <err.h> 7323247Swollman#include <errno.h> 7427508Swollman#include <math.h> 7523247Swollman#include <netdb.h> 7623247Swollman#include <signal.h> 7723247Swollman#include <stdio.h> 7823247Swollman#include <stdlib.h> 7923247Swollman#include <string.h> 8023247Swollman#include <sysexits.h> 8123247Swollman#include <termios.h> 8223247Swollman#include <unistd.h> 8323247Swollman 841558Srgrimes#include <sys/socket.h> 851558Srgrimes#include <sys/time.h> 8636378Sfenner#include <sys/uio.h> 871558Srgrimes 8823247Swollman#include <netinet/in.h> 891558Srgrimes#include <netinet/in_systm.h> 901558Srgrimes#include <netinet/ip.h> 911558Srgrimes#include <netinet/ip_icmp.h> 921558Srgrimes#include <netinet/ip_var.h> 9323247Swollman#include <arpa/inet.h> 941558Srgrimes 9536089Sjb#define PHDR_LEN sizeof(struct timeval) 9636089Sjb#define DEFDATALEN (64 - PHDR_LEN) /* default data length */ 9727533Sbde#define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */ 9827533Sbde /* runs out of buffer space */ 991558Srgrimes#define MAXIPLEN 60 1001558Srgrimes#define MAXICMPLEN 76 1011558Srgrimes#define MAXPACKET (65536 - 60 - 8)/* max packet size */ 1021558Srgrimes#define MAXWAIT 10 /* max seconds to wait for response */ 1031558Srgrimes#define NROUTES 9 /* number of record route slots */ 1041558Srgrimes 1051558Srgrimes#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 1061558Srgrimes#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 1071558Srgrimes#define SET(bit) (A(bit) |= B(bit)) 1081558Srgrimes#define CLR(bit) (A(bit) &= (~B(bit))) 1091558Srgrimes#define TST(bit) (A(bit) & B(bit)) 1101558Srgrimes 1111558Srgrimes/* various options */ 1121558Srgrimesint options; 11320540Sfenner#define F_FLOOD 0x0001 11420540Sfenner#define F_INTERVAL 0x0002 11520540Sfenner#define F_NUMERIC 0x0004 11620540Sfenner#define F_PINGFILLED 0x0008 11720540Sfenner#define F_QUIET 0x0010 11820540Sfenner#define F_RROUTE 0x0020 11920540Sfenner#define F_SO_DEBUG 0x0040 12020540Sfenner#define F_SO_DONTROUTE 0x0080 12120540Sfenner#define F_VERBOSE 0x0100 12220540Sfenner#define F_QUIET2 0x0200 12320540Sfenner#define F_NOLOOP 0x0400 12420540Sfenner#define F_MTTL 0x0800 12520540Sfenner#define F_MIF 0x1000 12622417Sdanny#define F_AUDIBLE 0x2000 1271558Srgrimes 1281558Srgrimes/* 1291558Srgrimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 1301558Srgrimes * number of received sequence numbers we can keep track of. Change 128 1311558Srgrimes * to 8192 for complete accuracy... 1321558Srgrimes */ 1331558Srgrimes#define MAX_DUP_CHK (8 * 128) 1341558Srgrimesint mx_dup_ck = MAX_DUP_CHK; 1351558Srgrimeschar rcvd_tbl[MAX_DUP_CHK / 8]; 1361558Srgrimes 1371558Srgrimesstruct sockaddr whereto; /* who to ping */ 1381558Srgrimesint datalen = DEFDATALEN; 1391558Srgrimesint s; /* socket file descriptor */ 1401558Srgrimesu_char outpack[MAXPACKET]; 1411558Srgrimeschar BSPACE = '\b'; /* characters written for flood */ 1421558Srgrimeschar DOT = '.'; 1431558Srgrimeschar *hostname; 1441558Srgrimesint ident; /* process id to identify our packets */ 14523295Simpint uid; /* cached uid for micro-optimization */ 1461558Srgrimes 1471558Srgrimes/* counters */ 1481558Srgrimeslong npackets; /* max packets to transmit */ 1491558Srgrimeslong nreceived; /* # of packets we got back */ 1501558Srgrimeslong nrepeats; /* number of duplicates */ 1511558Srgrimeslong ntransmitted; /* sequence # for outbound packets = #sent */ 1521558Srgrimesint interval = 1; /* interval between packets */ 1531558Srgrimes 1541558Srgrimes/* timing */ 1551558Srgrimesint timing; /* flag to do timing */ 1561558Srgrimesdouble tmin = 999999999.0; /* minimum round trip time */ 1571558Srgrimesdouble tmax = 0.0; /* maximum round trip time */ 1581558Srgrimesdouble tsum = 0.0; /* sum of all times, for doing average */ 15927508Swollmandouble tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 1601558Srgrimes 16127533Sbdevolatile sig_atomic_t finish_up; /* nonzero if we've been told to finish up */ 1623792Ssefint reset_kerninfo; 16327533Sbdevolatile sig_atomic_t siginfo_p; 1643792Ssef 16523247Swollmanstatic void fill(char *, char *); 16623247Swollmanstatic u_short in_cksum(u_short *, int); 16723247Swollmanstatic void check_status(void); 16827533Sbdestatic void finish(void) __dead2; 16923247Swollmanstatic void pinger(void); 17023247Swollmanstatic char *pr_addr(struct in_addr); 17123247Swollmanstatic void pr_icmph(struct icmp *); 17223247Swollmanstatic void pr_iph(struct ip *); 17336378Sfennerstatic void pr_pack(char *, int, struct sockaddr_in *, struct timeval *); 17423247Swollmanstatic void pr_retip(struct ip *); 17523247Swollmanstatic void status(int); 17627533Sbdestatic void stopit(int); 17723247Swollmanstatic void tvsub(struct timeval *, struct timeval *); 17837671Scharnierstatic void usage(void) __dead2; 1791558Srgrimes 18023247Swollmanint 1811558Srgrimesmain(argc, argv) 1821558Srgrimes int argc; 18323247Swollman char *const *argv; 1841558Srgrimes{ 18536378Sfenner struct timeval last, intvl; 1861558Srgrimes struct hostent *hp; 1871558Srgrimes struct sockaddr_in *to; 1883792Ssef struct termios ts; 1891558Srgrimes register int i; 19036378Sfenner int ch, hold, packlen, preload, sockerrno, almost_done = 0; 19120540Sfenner struct in_addr ifaddr; 19220540Sfenner unsigned char ttl, loop; 1931558Srgrimes u_char *datap, *packet; 19423247Swollman char *target, hnamebuf[MAXHOSTNAMELEN]; 19523247Swollman char *ep; 19623247Swollman u_long ultmp; 1971558Srgrimes#ifdef IP_OPTIONS 1981558Srgrimes char rspace[3 + 4 * NROUTES + 1]; /* record route space */ 1991558Srgrimes#endif 20020195Ssef struct sigaction si_sa; 20136378Sfenner struct iovec iov; 20236378Sfenner struct msghdr msg; 20336378Sfenner struct sockaddr_in from; 20436378Sfenner char ctrl[sizeof(struct cmsghdr) + sizeof(struct timeval)]; 2051558Srgrimes 20617474Sfenner /* 20717474Sfenner * Do the stuff that we need root priv's for *first*, and 20817474Sfenner * then drop our setuid bit. Save error reporting for 20917474Sfenner * after arg parsing. 21017474Sfenner */ 21123247Swollman s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 21223247Swollman sockerrno = errno; 21317474Sfenner 21417474Sfenner setuid(getuid()); 21523295Simp uid = getuid(); 21617474Sfenner 2171558Srgrimes preload = 0; 2183792Ssef 21936089Sjb datap = &outpack[8 + PHDR_LEN]; 22023251Simp while ((ch = getopt(argc, argv, "I:LQRT:c:adfi:l:np:qrs:v")) != -1) { 2211558Srgrimes switch(ch) { 22222417Sdanny case 'a': 22322417Sdanny options |= F_AUDIBLE; 22422417Sdanny break; 2251558Srgrimes case 'c': 22623247Swollman ultmp = strtoul(optarg, &ep, 0); 22723247Swollman if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp) 22823247Swollman errx(EX_USAGE, 22923247Swollman "invalid count of packets to transmit: `%s'", 23023247Swollman optarg); 23123247Swollman npackets = ultmp; 2321558Srgrimes break; 2331558Srgrimes case 'd': 2341558Srgrimes options |= F_SO_DEBUG; 2351558Srgrimes break; 2361558Srgrimes case 'f': 2371558Srgrimes if (getuid()) { 23823247Swollman errno = EPERM; 23923247Swollman err(EX_NOPERM, "-f flag"); 2401558Srgrimes } 2411558Srgrimes options |= F_FLOOD; 2421558Srgrimes setbuf(stdout, (char *)NULL); 2431558Srgrimes break; 2441558Srgrimes case 'i': /* wait between sending packets */ 24523247Swollman ultmp = strtoul(optarg, &ep, 0); 24623247Swollman if (*ep || ep == optarg || ultmp > INT_MAX) 24723247Swollman errx(EX_USAGE, 24823247Swollman "invalid timing interval: `%s'", optarg); 2491558Srgrimes options |= F_INTERVAL; 25023247Swollman interval = ultmp; 2511558Srgrimes break; 25220540Sfenner case 'I': /* multicast interface */ 25323247Swollman if (inet_aton(optarg, &ifaddr) == 0) 25423247Swollman errx(EX_USAGE, 25523247Swollman "invalid multicast interface: `%s'", 25623247Swollman optarg); 25720540Sfenner options |= F_MIF; 25820540Sfenner break; 2591558Srgrimes case 'l': 26023247Swollman ultmp = strtoul(optarg, &ep, 0); 26123247Swollman if (*ep || ep == optarg || ultmp > INT_MAX) 26223247Swollman errx(EX_USAGE, 26323247Swollman "invalid preload value: `%s'", optarg); 26423251Simp if (getuid()) { 26523251Simp errno = EPERM; 26623251Simp err(EX_NOPERM, "-l flag"); 26723251Simp } 26834998Simp options |= F_FLOOD; 26923247Swollman preload = ultmp; 2701558Srgrimes break; 27120540Sfenner case 'L': 27220540Sfenner options |= F_NOLOOP; 27320540Sfenner loop = 0; 27420540Sfenner break; 2751558Srgrimes case 'n': 2761558Srgrimes options |= F_NUMERIC; 2771558Srgrimes break; 2781558Srgrimes case 'p': /* fill buffer with user pattern */ 2791558Srgrimes options |= F_PINGFILLED; 2801558Srgrimes fill((char *)datap, optarg); 2811558Srgrimes break; 28217724Sfenner case 'Q': 28317724Sfenner options |= F_QUIET2; 28417724Sfenner break; 2851558Srgrimes case 'q': 2861558Srgrimes options |= F_QUIET; 2871558Srgrimes break; 2881558Srgrimes case 'R': 2891558Srgrimes options |= F_RROUTE; 2901558Srgrimes break; 2911558Srgrimes case 'r': 2921558Srgrimes options |= F_SO_DONTROUTE; 2931558Srgrimes break; 2941558Srgrimes case 's': /* size of packet to send */ 29523247Swollman ultmp = strtoul(optarg, &ep, 0); 29623247Swollman if (ultmp > MAXPACKET) 29723247Swollman errx(EX_USAGE, "packet size too large: %lu", 29823247Swollman ultmp); 29923247Swollman if (*ep || ep == optarg || !ultmp) 30023247Swollman errx(EX_USAGE, "invalid packet size: `%s'", 30123247Swollman optarg); 30223247Swollman datalen = ultmp; 3031558Srgrimes break; 30420540Sfenner case 'T': /* multicast TTL */ 30523247Swollman ultmp = strtoul(optarg, &ep, 0); 30623247Swollman if (*ep || ep == optarg || ultmp > 255) 30723247Swollman errx(EX_USAGE, "invalid multicast TTL: `%s'", 30823247Swollman optarg); 30923247Swollman ttl = ultmp; 31020540Sfenner options |= F_MTTL; 31120540Sfenner break; 3121558Srgrimes case 'v': 3131558Srgrimes options |= F_VERBOSE; 3141558Srgrimes break; 3151558Srgrimes default: 31637671Scharnier usage(); 3171558Srgrimes } 31823247Swollman } 3191558Srgrimes 32023247Swollman if (argc - optind != 1) 32137671Scharnier usage(); 32223247Swollman target = argv[optind]; 3231558Srgrimes 3241558Srgrimes bzero((char *)&whereto, sizeof(struct sockaddr)); 3251558Srgrimes to = (struct sockaddr_in *)&whereto; 3261558Srgrimes to->sin_family = AF_INET; 32723247Swollman if (inet_aton(target, &to->sin_addr) != 0) { 3281558Srgrimes hostname = target; 32923247Swollman } else { 33023247Swollman hp = gethostbyname2(target, AF_INET); 33123247Swollman if (!hp) 33223247Swollman errx(EX_NOHOST, "cannot resolve %s: %s", 33323247Swollman target, hstrerror(h_errno)); 33423247Swollman 33523247Swollman to->sin_len = sizeof *to; 33623327Simp if (hp->h_length > sizeof(to->sin_addr)) 33723327Simp errx(1,"gethostbyname2 returned an illegal address"); 33823247Swollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 3391558Srgrimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 34031956Simp hnamebuf[sizeof(hnamebuf) - 1] = '\0'; 3411558Srgrimes hostname = hnamebuf; 3421558Srgrimes } 3431558Srgrimes 34423247Swollman if (options & F_FLOOD && options & F_INTERVAL) 34523247Swollman errx(EX_USAGE, "-f and -i: incompatible options"); 3461558Srgrimes 34723247Swollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 34823247Swollman errx(EX_USAGE, 34923247Swollman "-f flag cannot be used with multicast destination"); 35023247Swollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 35123247Swollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 35223247Swollman errx(EX_USAGE, 35323247Swollman "-I, -L, -T flags cannot be used with unicast destination"); 35423247Swollman 35536089Sjb if (datalen >= PHDR_LEN) /* can we time transfer */ 3561558Srgrimes timing = 1; 3571558Srgrimes packlen = datalen + MAXIPLEN + MAXICMPLEN; 35823247Swollman if (!(packet = (u_char *)malloc((size_t)packlen))) 35923247Swollman err(EX_UNAVAILABLE, "malloc"); 36023247Swollman 3611558Srgrimes if (!(options & F_PINGFILLED)) 36236089Sjb for (i = PHDR_LEN; i < datalen; ++i) 3631558Srgrimes *datap++ = i; 3641558Srgrimes 3651558Srgrimes ident = getpid() & 0xFFFF; 3661558Srgrimes 36717474Sfenner if (s < 0) { 36817474Sfenner errno = sockerrno; 36923247Swollman err(EX_OSERR, "socket"); 3701558Srgrimes } 3711558Srgrimes hold = 1; 3721558Srgrimes if (options & F_SO_DEBUG) 3731558Srgrimes (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold, 3741558Srgrimes sizeof(hold)); 3751558Srgrimes if (options & F_SO_DONTROUTE) 3761558Srgrimes (void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 3771558Srgrimes sizeof(hold)); 3781558Srgrimes 3791558Srgrimes /* record route option */ 3801558Srgrimes if (options & F_RROUTE) { 3811558Srgrimes#ifdef IP_OPTIONS 38236378Sfenner bzero(rspace, sizeof(rspace)); 3831558Srgrimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 38436378Sfenner rspace[IPOPT_OLEN] = sizeof(rspace) - 1; 3851558Srgrimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 38636378Sfenner rspace[sizeof(rspace) - 1] = IPOPT_EOL; 3871558Srgrimes if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace, 38823247Swollman sizeof(rspace)) < 0) 38923247Swollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 3901558Srgrimes#else 39123247Swollman errx(EX_UNAVAILABLE, 39223247Swollman "record route not available in this implementation"); 3931558Srgrimes#endif /* IP_OPTIONS */ 3941558Srgrimes } 3951558Srgrimes 39620540Sfenner if (options & F_NOLOOP) { 39720540Sfenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 39820540Sfenner sizeof(loop)) < 0) { 39923247Swollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 40020540Sfenner } 40120540Sfenner } 40220540Sfenner if (options & F_MTTL) { 40320540Sfenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, 40420540Sfenner sizeof(ttl)) < 0) { 40523247Swollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 40620540Sfenner } 40720540Sfenner } 40820540Sfenner if (options & F_MIF) { 40920540Sfenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 41020540Sfenner sizeof(ifaddr)) < 0) { 41123247Swollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 41220540Sfenner } 41320540Sfenner } 41436378Sfenner#ifdef SO_TIMESTAMP 41536378Sfenner { int on = 1; 41636378Sfenner if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) < 0) 41736378Sfenner err(EX_OSERR, "setsockopt SO_TIMESTAMP"); 41836378Sfenner } 41936378Sfenner#endif 42020540Sfenner 4211558Srgrimes /* 4221558Srgrimes * When pinging the broadcast address, you can get a lot of answers. 4231558Srgrimes * Doing something so evil is useful if you are trying to stress the 4241558Srgrimes * ethernet, or just want to fill the arp cache to get some stuff for 42523247Swollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 42623247Swollman * or multicast pings if they wish. 4271558Srgrimes */ 4281558Srgrimes hold = 48 * 1024; 4291558Srgrimes (void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 4301558Srgrimes sizeof(hold)); 4311558Srgrimes 4321558Srgrimes if (to->sin_family == AF_INET) 4331558Srgrimes (void)printf("PING %s (%s): %d data bytes\n", hostname, 43423247Swollman inet_ntoa(to->sin_addr), 4351558Srgrimes datalen); 4361558Srgrimes else 4371558Srgrimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 4381558Srgrimes 43920280Sbde /* 44027354Ssef * Use sigaction() instead of signal() to get unambiguous semantics, 44127354Ssef * in particular with SA_RESTART not set. 44220280Sbde */ 44327354Ssef 44420205Spst sigemptyset(&si_sa.sa_mask); 44520195Ssef si_sa.sa_flags = 0; 44627354Ssef 44727354Ssef si_sa.sa_handler = stopit; 44827354Ssef if (sigaction(SIGINT, &si_sa, 0) == -1) { 44927354Ssef err(EX_OSERR, "sigaction SIGINT"); 45027354Ssef } 45127354Ssef 45227354Ssef si_sa.sa_handler = status; 45320195Ssef if (sigaction(SIGINFO, &si_sa, 0) == -1) { 45423385Simp err(EX_OSERR, "sigaction"); 45520195Ssef } 45620195Ssef 45736378Sfenner bzero(&msg, sizeof(msg)); 45836378Sfenner msg.msg_name = (caddr_t)&from; 45936378Sfenner msg.msg_iov = &iov; 46036378Sfenner msg.msg_iovlen = 1; 46136378Sfenner#ifdef SO_TIMESTAMP 46236378Sfenner msg.msg_control = (caddr_t)ctrl; 46336378Sfenner#endif 46436378Sfenner iov.iov_base = packet; 46536378Sfenner iov.iov_len = packlen; 46636378Sfenner 46719864Ssef if (tcgetattr(STDOUT_FILENO, &ts) != -1) { 46819864Ssef reset_kerninfo = !(ts.c_lflag & NOKERNINFO); 46919864Ssef ts.c_lflag |= NOKERNINFO; 47019864Ssef tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 47119864Ssef } 47219864Ssef 4731558Srgrimes while (preload--) /* fire off them quickies */ 4741558Srgrimes pinger(); 4751558Srgrimes 47636378Sfenner if (options & F_FLOOD) { 47736378Sfenner intvl.tv_sec = 0; 47836378Sfenner intvl.tv_usec = 10000; 47936378Sfenner } else { 48036378Sfenner intvl.tv_sec = interval; 48136378Sfenner intvl.tv_usec = 0; 48236378Sfenner } 4831558Srgrimes 48436378Sfenner pinger(); /* send the first ping */ 48536378Sfenner (void)gettimeofday(&last, NULL); 48636378Sfenner 48727533Sbde while (!finish_up) { 4881558Srgrimes register int cc; 48936378Sfenner int n; 49036378Sfenner struct timeval timeout, now; 49136378Sfenner fd_set rfds; 4921558Srgrimes 49320280Sbde check_status(); 49436378Sfenner FD_ZERO(&rfds); 49536378Sfenner FD_SET(s, &rfds); 49636378Sfenner (void)gettimeofday(&now, NULL); 49736378Sfenner timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec; 49836378Sfenner timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec; 49936378Sfenner while (timeout.tv_usec < 0) { 50036378Sfenner timeout.tv_usec += 1000000; 50136378Sfenner timeout.tv_sec--; 5021558Srgrimes } 50337671Scharnier while (timeout.tv_usec >= 1000000) { 50436378Sfenner timeout.tv_usec -= 1000000; 50536378Sfenner timeout.tv_sec++; 50636378Sfenner } 50736378Sfenner if (timeout.tv_sec < 0) 50836378Sfenner timeout.tv_sec = timeout.tv_usec = 0; 50936378Sfenner n = select(s + 1, &rfds, NULL, NULL, &timeout); 51036378Sfenner if (n == 1) { 51136378Sfenner struct timeval *t = 0; 51236378Sfenner#ifdef SO_TIMESTAMP 51336378Sfenner struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl; 51436378Sfenner 51536378Sfenner msg.msg_controllen = sizeof(ctrl); 51636378Sfenner#endif 51736378Sfenner msg.msg_namelen = sizeof(from); 51836378Sfenner if ((cc = recvmsg(s, &msg, 0)) < 0) { 51936378Sfenner if (errno == EINTR) 52036378Sfenner continue; 52137671Scharnier warn("recvmsg"); 5221558Srgrimes continue; 52336378Sfenner } 52436378Sfenner#ifdef SO_TIMESTAMP 52536378Sfenner if (cmsg->cmsg_level == SOL_SOCKET && 52636378Sfenner cmsg->cmsg_type == SCM_TIMESTAMP && 52736713Sjb cmsg->cmsg_len == (sizeof *cmsg + sizeof *t)) { 52836713Sjb /* Copy to avoid alignment problems: */ 52936713Sjb memcpy(&now,CMSG_DATA(cmsg),sizeof(now)); 53036713Sjb t = &now; 53136713Sjb } 53236378Sfenner#endif 53336378Sfenner if (t == 0) { 53436378Sfenner (void)gettimeofday(&now, NULL); 53536378Sfenner t = &now; 53636378Sfenner } 53736378Sfenner pr_pack((char *)packet, cc, &from, t); 53836378Sfenner if (npackets && nreceived >= npackets) 53936378Sfenner break; 5401558Srgrimes } 54136378Sfenner if (n == 0) { 54236378Sfenner if (!npackets || ntransmitted < npackets) 54336378Sfenner pinger(); 54436378Sfenner else { 54536378Sfenner if (almost_done) 54636378Sfenner break; 54736378Sfenner almost_done = 1; 54836378Sfenner if (nreceived) { 54936378Sfenner intvl.tv_sec = 2 * tmax / 1000; 55036378Sfenner if (!intvl.tv_sec) 55136378Sfenner intvl.tv_sec = 1; 55236378Sfenner } else 55336378Sfenner intvl.tv_sec = MAXWAIT; 55436378Sfenner } 55536378Sfenner (void)gettimeofday(&last, NULL); 55636378Sfenner } 5571558Srgrimes } 55827533Sbde finish(); 5591558Srgrimes /* NOTREACHED */ 56023251Simp exit(0); /* Make the compiler happy */ 5611558Srgrimes} 5621558Srgrimes 5631558Srgrimes/* 56427533Sbde * stopit -- 56527533Sbde * Set the global bit that causes the main loop to quit. 56627533Sbde * Do NOT call finish() from here, since finish() does far too much 56727533Sbde * to be called from a signal handler. 56827299Sjulian */ 56927299Sjulianvoid 57027533Sbdestopit(sig) 57127533Sbde int sig; 57227299Sjulian{ 57327299Sjulian finish_up = 1; 57427299Sjulian} 57527299Sjulian 57627299Sjulian/* 5771558Srgrimes * pinger -- 5781558Srgrimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 5791558Srgrimes * will be added on by the kernel. The ID field is our UNIX process ID, 5801558Srgrimes * and the sequence number is an ascending integer. The first 8 bytes 58117724Sfenner * of the data portion are used to hold a UNIX "timeval" struct in host 5821558Srgrimes * byte-order, to compute the round-trip time. 5831558Srgrimes */ 58423247Swollmanstatic void 58523247Swollmanpinger(void) 5861558Srgrimes{ 5871558Srgrimes register struct icmp *icp; 5881558Srgrimes register int cc; 5891558Srgrimes int i; 5901558Srgrimes 5911558Srgrimes icp = (struct icmp *)outpack; 5921558Srgrimes icp->icmp_type = ICMP_ECHO; 5931558Srgrimes icp->icmp_code = 0; 5941558Srgrimes icp->icmp_cksum = 0; 59527301Sjulian icp->icmp_seq = ntransmitted; 5961558Srgrimes icp->icmp_id = ident; /* ID */ 5971558Srgrimes 5981558Srgrimes CLR(icp->icmp_seq % mx_dup_ck); 5991558Srgrimes 6001558Srgrimes if (timing) 6011558Srgrimes (void)gettimeofday((struct timeval *)&outpack[8], 6021558Srgrimes (struct timezone *)NULL); 6031558Srgrimes 60436089Sjb cc = datalen + PHDR_LEN; /* skips ICMP portion */ 6051558Srgrimes 6061558Srgrimes /* compute ICMP checksum here */ 6071558Srgrimes icp->icmp_cksum = in_cksum((u_short *)icp, cc); 6081558Srgrimes 6091558Srgrimes i = sendto(s, (char *)outpack, cc, 0, &whereto, 6101558Srgrimes sizeof(struct sockaddr)); 6111558Srgrimes 6121558Srgrimes if (i < 0 || i != cc) { 61323247Swollman if (i < 0) { 61427533Sbde if (options & F_FLOOD && errno == ENOBUFS) { 61527299Sjulian usleep(FLOOD_BACKOFF); 61627299Sjulian return; 61727299Sjulian } 61823247Swollman warn("sendto"); 61923247Swollman } else { 62023247Swollman warn("%s: partial write: %d of %d bytes", 62135216Sphk hostname, i, cc); 62223247Swollman } 62327945Sjulian } 62427945Sjulian ntransmitted++; 6251558Srgrimes if (!(options & F_QUIET) && options & F_FLOOD) 6261558Srgrimes (void)write(STDOUT_FILENO, &DOT, 1); 6271558Srgrimes} 6281558Srgrimes 6291558Srgrimes/* 6301558Srgrimes * pr_pack -- 6311558Srgrimes * Print out the packet, if it came from us. This logic is necessary 6321558Srgrimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 6331558Srgrimes * which arrive ('tis only fair). This permits multiple copies of this 6341558Srgrimes * program to be run without having intermingled output (or statistics!). 6351558Srgrimes */ 63623247Swollmanstatic void 63736378Sfennerpr_pack(buf, cc, from, tv) 6381558Srgrimes char *buf; 6391558Srgrimes int cc; 6401558Srgrimes struct sockaddr_in *from; 64136378Sfenner struct timeval *tv; 6421558Srgrimes{ 6431558Srgrimes register struct icmp *icp; 6441558Srgrimes register u_long l; 6451558Srgrimes register int i, j; 6461558Srgrimes register u_char *cp,*dp; 6471558Srgrimes static int old_rrlen; 6481558Srgrimes static char old_rr[MAX_IPOPTLEN]; 6491558Srgrimes struct ip *ip; 65036378Sfenner struct timeval *tp; 65127533Sbde double triptime; 6521558Srgrimes int hlen, dupflag; 6531558Srgrimes 6541558Srgrimes /* Check the IP header */ 6551558Srgrimes ip = (struct ip *)buf; 6561558Srgrimes hlen = ip->ip_hl << 2; 6571558Srgrimes if (cc < hlen + ICMP_MINLEN) { 6581558Srgrimes if (options & F_VERBOSE) 65923247Swollman warn("packet too short (%d bytes) from %s", cc, 66023247Swollman inet_ntoa(from->sin_addr)); 6611558Srgrimes return; 6621558Srgrimes } 6631558Srgrimes 6641558Srgrimes /* Now the ICMP part */ 6651558Srgrimes cc -= hlen; 6661558Srgrimes icp = (struct icmp *)(buf + hlen); 6671558Srgrimes if (icp->icmp_type == ICMP_ECHOREPLY) { 6681558Srgrimes if (icp->icmp_id != ident) 6691558Srgrimes return; /* 'Twas not our ECHO */ 6701558Srgrimes ++nreceived; 67127533Sbde triptime = 0.0; 6721558Srgrimes if (timing) { 67336089Sjb struct timeval tv1; 6741558Srgrimes#ifndef icmp_data 6751558Srgrimes tp = (struct timeval *)&icp->icmp_ip; 6761558Srgrimes#else 6771558Srgrimes tp = (struct timeval *)icp->icmp_data; 6781558Srgrimes#endif 67936089Sjb /* Avoid unaligned data: */ 68036089Sjb memcpy(&tv1,tp,sizeof(tv1)); 68136378Sfenner tvsub(tv, &tv1); 68236378Sfenner triptime = ((double)tv->tv_sec) * 1000.0 + 68336378Sfenner ((double)tv->tv_usec) / 1000.0; 6841558Srgrimes tsum += triptime; 68527508Swollman tsumsq += triptime * triptime; 6861558Srgrimes if (triptime < tmin) 6871558Srgrimes tmin = triptime; 6881558Srgrimes if (triptime > tmax) 6891558Srgrimes tmax = triptime; 6901558Srgrimes } 6911558Srgrimes 6921558Srgrimes if (TST(icp->icmp_seq % mx_dup_ck)) { 6931558Srgrimes ++nrepeats; 6941558Srgrimes --nreceived; 6951558Srgrimes dupflag = 1; 6961558Srgrimes } else { 6971558Srgrimes SET(icp->icmp_seq % mx_dup_ck); 6981558Srgrimes dupflag = 0; 6991558Srgrimes } 7001558Srgrimes 7011558Srgrimes if (options & F_QUIET) 7021558Srgrimes return; 7031558Srgrimes 7041558Srgrimes if (options & F_FLOOD) 7051558Srgrimes (void)write(STDOUT_FILENO, &BSPACE, 1); 7061558Srgrimes else { 7071558Srgrimes (void)printf("%d bytes from %s: icmp_seq=%u", cc, 7081558Srgrimes inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr), 7091558Srgrimes icp->icmp_seq); 7101558Srgrimes (void)printf(" ttl=%d", ip->ip_ttl); 7111558Srgrimes if (timing) 7121859Sdg (void)printf(" time=%.3f ms", triptime); 7131558Srgrimes if (dupflag) 7141558Srgrimes (void)printf(" (DUP!)"); 71522417Sdanny if (options & F_AUDIBLE) 71622417Sdanny (void)printf("\a"); 7171558Srgrimes /* check the data */ 71836089Sjb cp = (u_char*)&icp->icmp_data[PHDR_LEN]; 71936089Sjb dp = &outpack[8 + PHDR_LEN]; 72036089Sjb for (i = PHDR_LEN; i < datalen; ++i, ++cp, ++dp) { 7211558Srgrimes if (*cp != *dp) { 7221558Srgrimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 7231558Srgrimes i, *dp, *cp); 72436089Sjb printf("\ncp:"); 7251558Srgrimes cp = (u_char*)&icp->icmp_data[0]; 72636089Sjb for (i = 0; i < datalen; ++i, ++cp) { 7271558Srgrimes if ((i % 32) == 8) 7281558Srgrimes (void)printf("\n\t"); 7291558Srgrimes (void)printf("%x ", *cp); 7301558Srgrimes } 73136089Sjb printf("\ndp:"); 73236089Sjb cp = &outpack[8]; 73336089Sjb for (i = 0; i < datalen; ++i, ++cp) { 73436089Sjb if ((i % 32) == 8) 73536089Sjb (void)printf("\n\t"); 73636089Sjb (void)printf("%x ", *cp); 73736089Sjb } 7381558Srgrimes break; 7391558Srgrimes } 7401558Srgrimes } 7411558Srgrimes } 7421558Srgrimes } else { 74317724Sfenner /* 74417724Sfenner * We've got something other than an ECHOREPLY. 74517724Sfenner * See if it's a reply to something that we sent. 74617724Sfenner * We can compare IP destination, protocol, 74717724Sfenner * and ICMP type and ID. 74823251Simp * 74923251Simp * Only print all the error messages if we are running 75023251Simp * as root to avoid leaking information not normally 75123251Simp * available to those not running as root. 75217724Sfenner */ 75317724Sfenner#ifndef icmp_data 75417724Sfenner struct ip *oip = &icp->icmp_ip; 75517724Sfenner#else 75617724Sfenner struct ip *oip = (struct ip *)icp->icmp_data; 75717724Sfenner#endif 75817724Sfenner struct icmp *oicmp = (struct icmp *)(oip + 1); 75917724Sfenner 76023295Simp if (((options & F_VERBOSE) && uid == 0) || 76117724Sfenner (!(options & F_QUIET2) && 76217724Sfenner (oip->ip_dst.s_addr == 76317724Sfenner ((struct sockaddr_in *)&whereto)->sin_addr.s_addr) && 76417724Sfenner (oip->ip_p == IPPROTO_ICMP) && 76517724Sfenner (oicmp->icmp_type == ICMP_ECHO) && 76617724Sfenner (oicmp->icmp_id == ident))) { 76717724Sfenner (void)printf("%d bytes from %s: ", cc, 76823247Swollman pr_addr(from->sin_addr)); 76917724Sfenner pr_icmph(icp); 77017724Sfenner } else 77117724Sfenner return; 7721558Srgrimes } 7731558Srgrimes 7741558Srgrimes /* Display any IP options */ 7751558Srgrimes cp = (u_char *)buf + sizeof(struct ip); 7761558Srgrimes 7771558Srgrimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 7781558Srgrimes switch (*cp) { 7791558Srgrimes case IPOPT_EOL: 7801558Srgrimes hlen = 0; 7811558Srgrimes break; 7821558Srgrimes case IPOPT_LSRR: 7831558Srgrimes (void)printf("\nLSRR: "); 7841558Srgrimes hlen -= 2; 7851558Srgrimes j = *++cp; 7861558Srgrimes ++cp; 7871558Srgrimes if (j > IPOPT_MINOFF) 7881558Srgrimes for (;;) { 7891558Srgrimes l = *++cp; 7901558Srgrimes l = (l<<8) + *++cp; 7911558Srgrimes l = (l<<8) + *++cp; 7921558Srgrimes l = (l<<8) + *++cp; 79323247Swollman if (l == 0) { 79423247Swollman printf("\t0.0.0.0"); 79523247Swollman } else { 79623247Swollman struct in_addr ina; 79723247Swollman ina.s_addr = ntohl(l); 79823247Swollman printf("\t%s", pr_addr(ina)); 79923247Swollman } 8001558Srgrimes hlen -= 4; 8011558Srgrimes j -= 4; 8021558Srgrimes if (j <= IPOPT_MINOFF) 8031558Srgrimes break; 8041558Srgrimes (void)putchar('\n'); 8051558Srgrimes } 8061558Srgrimes break; 8071558Srgrimes case IPOPT_RR: 8081558Srgrimes j = *++cp; /* get length */ 8091558Srgrimes i = *++cp; /* and pointer */ 8101558Srgrimes hlen -= 2; 8111558Srgrimes if (i > j) 8121558Srgrimes i = j; 8131558Srgrimes i -= IPOPT_MINOFF; 8141558Srgrimes if (i <= 0) 8151558Srgrimes continue; 8161558Srgrimes if (i == old_rrlen 8171558Srgrimes && cp == (u_char *)buf + sizeof(struct ip) + 2 8181558Srgrimes && !bcmp((char *)cp, old_rr, i) 8191558Srgrimes && !(options & F_FLOOD)) { 8201558Srgrimes (void)printf("\t(same route)"); 8211558Srgrimes i = ((i + 3) / 4) * 4; 8221558Srgrimes hlen -= i; 8231558Srgrimes cp += i; 8241558Srgrimes break; 8251558Srgrimes } 82634995Seivind if (i < MAX_IPOPTLEN) { 82734976Simp old_rrlen = i; 82834976Simp bcopy((char *)cp, old_rr, i); 82934976Simp } else 83034976Simp old_rrlen = 0; 83134976Simp 8321558Srgrimes (void)printf("\nRR: "); 83334976Simp j = 0; 8341558Srgrimes for (;;) { 8351558Srgrimes l = *++cp; 8361558Srgrimes l = (l<<8) + *++cp; 8371558Srgrimes l = (l<<8) + *++cp; 8381558Srgrimes l = (l<<8) + *++cp; 83923247Swollman if (l == 0) { 84023247Swollman printf("\t0.0.0.0"); 84123247Swollman } else { 84223247Swollman struct in_addr ina; 84323247Swollman ina.s_addr = ntohl(l); 84423247Swollman printf("\t%s", pr_addr(ina)); 84523247Swollman } 8461558Srgrimes hlen -= 4; 8471558Srgrimes i -= 4; 84834976Simp j += 4; 8491558Srgrimes if (i <= 0) 8501558Srgrimes break; 85134976Simp if (j >= MAX_IPOPTLEN) { 85234976Simp (void) printf("\t(truncated route)"); 85334976Simp break; 85434976Simp } 8551558Srgrimes (void)putchar('\n'); 8561558Srgrimes } 8571558Srgrimes break; 8581558Srgrimes case IPOPT_NOP: 8591558Srgrimes (void)printf("\nNOP"); 8601558Srgrimes break; 8611558Srgrimes default: 8621558Srgrimes (void)printf("\nunknown option %x", *cp); 8631558Srgrimes break; 8641558Srgrimes } 8651558Srgrimes if (!(options & F_FLOOD)) { 8661558Srgrimes (void)putchar('\n'); 8671558Srgrimes (void)fflush(stdout); 8681558Srgrimes } 8691558Srgrimes} 8701558Srgrimes 8711558Srgrimes/* 8721558Srgrimes * in_cksum -- 8731558Srgrimes * Checksum routine for Internet Protocol family headers (C Version) 8741558Srgrimes */ 87523247Swollmanu_short 8761558Srgrimesin_cksum(addr, len) 8771558Srgrimes u_short *addr; 8781558Srgrimes int len; 8791558Srgrimes{ 8801558Srgrimes register int nleft = len; 8811558Srgrimes register u_short *w = addr; 8821558Srgrimes register int sum = 0; 8831558Srgrimes u_short answer = 0; 8841558Srgrimes 8851558Srgrimes /* 8861558Srgrimes * Our algorithm is simple, using a 32 bit accumulator (sum), we add 8871558Srgrimes * sequential 16 bit words to it, and at the end, fold back all the 8881558Srgrimes * carry bits from the top 16 bits into the lower 16 bits. 8891558Srgrimes */ 8901558Srgrimes while (nleft > 1) { 8911558Srgrimes sum += *w++; 8921558Srgrimes nleft -= 2; 8931558Srgrimes } 8941558Srgrimes 8951558Srgrimes /* mop up an odd byte, if necessary */ 8961558Srgrimes if (nleft == 1) { 8971558Srgrimes *(u_char *)(&answer) = *(u_char *)w ; 8981558Srgrimes sum += answer; 8991558Srgrimes } 9001558Srgrimes 9011558Srgrimes /* add back carry outs from top 16 bits to low 16 bits */ 9021558Srgrimes sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 9031558Srgrimes sum += (sum >> 16); /* add carry */ 9041558Srgrimes answer = ~sum; /* truncate to 16 bits */ 9051558Srgrimes return(answer); 9061558Srgrimes} 9071558Srgrimes 9081558Srgrimes/* 9091558Srgrimes * tvsub -- 9101558Srgrimes * Subtract 2 timeval structs: out = out - in. Out is assumed to 9111558Srgrimes * be >= in. 9121558Srgrimes */ 91323247Swollmanstatic void 9141558Srgrimestvsub(out, in) 9151558Srgrimes register struct timeval *out, *in; 9161558Srgrimes{ 9171558Srgrimes if ((out->tv_usec -= in->tv_usec) < 0) { 9181558Srgrimes --out->tv_sec; 9191558Srgrimes out->tv_usec += 1000000; 9201558Srgrimes } 9211558Srgrimes out->tv_sec -= in->tv_sec; 9221558Srgrimes} 9231558Srgrimes 9241558Srgrimes/* 9253792Ssef * status -- 9263792Ssef * Print out statistics when SIGINFO is received. 9273792Ssef */ 9283792Ssef 92923247Swollmanstatic void 93020280Sbdestatus(sig) 93120280Sbde int sig; 93220280Sbde{ 93320195Ssef siginfo_p = 1; 93420195Ssef} 93520195Ssef 93623247Swollmanstatic void 93720195Ssefcheck_status() 9383792Ssef{ 93920195Ssef if (siginfo_p) { 94020195Ssef siginfo_p = 0; 94120280Sbde (void)fprintf(stderr, 94220280Sbde "\r%ld/%ld packets received (%.0f%%) %.3f min / %.3f avg / %.3f max\n", 94320280Sbde nreceived, ntransmitted, 94420280Sbde ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0, 94520280Sbde nreceived ? tmin : 0.0, 94620280Sbde nreceived + nrepeats ? tsum / (nreceived + nrepeats) : tsum, 94720280Sbde tmax); 94820195Ssef } 9493792Ssef} 9503792Ssef 9513792Ssef/* 9521558Srgrimes * finish -- 9531558Srgrimes * Print out statistics, and give up. 9541558Srgrimes */ 95523247Swollmanstatic void 95627533Sbdefinish() 9571558Srgrimes{ 9583792Ssef struct termios ts; 9591558Srgrimes 9601558Srgrimes (void)signal(SIGINT, SIG_IGN); 96127354Ssef (void)signal(SIGALRM, SIG_IGN); 9621558Srgrimes (void)putchar('\n'); 9631558Srgrimes (void)fflush(stdout); 9641558Srgrimes (void)printf("--- %s ping statistics ---\n", hostname); 9651558Srgrimes (void)printf("%ld packets transmitted, ", ntransmitted); 9661558Srgrimes (void)printf("%ld packets received, ", nreceived); 9671558Srgrimes if (nrepeats) 9681558Srgrimes (void)printf("+%ld duplicates, ", nrepeats); 9691558Srgrimes if (ntransmitted) 9701558Srgrimes if (nreceived > ntransmitted) 9711558Srgrimes (void)printf("-- somebody's printing up packets!"); 9721558Srgrimes else 9731558Srgrimes (void)printf("%d%% packet loss", 9741558Srgrimes (int) (((ntransmitted - nreceived) * 100) / 9751558Srgrimes ntransmitted)); 9761558Srgrimes (void)putchar('\n'); 97727508Swollman if (nreceived && timing) { 97827508Swollman double n = nreceived + nrepeats; 97927508Swollman double avg = tsum / n; 98027508Swollman double vari = tsumsq / n - avg * avg; 98127508Swollman printf("round-trip min/avg/max/stddev = " 98227508Swollman "%.3f/%.3f/%.3f/%.3f ms\n", 98327508Swollman tmin, avg, tmax, sqrt(vari)); 98427508Swollman } 98519395Sbde if (reset_kerninfo && tcgetattr(STDOUT_FILENO, &ts) != -1) { 9863792Ssef ts.c_lflag &= ~NOKERNINFO; 98719395Sbde tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 9883792Ssef } 9893792Ssef 9908871Srgrimes if (nreceived) 9914862Sdg exit(0); 9924862Sdg else 9934862Sdg exit(2); 9941558Srgrimes} 9951558Srgrimes 9961558Srgrimes#ifdef notdef 9971558Srgrimesstatic char *ttab[] = { 9981558Srgrimes "Echo Reply", /* ip + seq + udata */ 9991558Srgrimes "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */ 10001558Srgrimes "Source Quench", /* IP */ 10011558Srgrimes "Redirect", /* redirect type, gateway, + IP */ 10021558Srgrimes "Echo", 10031558Srgrimes "Time Exceeded", /* transit, frag reassem + IP */ 10041558Srgrimes "Parameter Problem", /* pointer + IP */ 10051558Srgrimes "Timestamp", /* id + seq + three timestamps */ 10061558Srgrimes "Timestamp Reply", /* " */ 10071558Srgrimes "Info Request", /* id + sq */ 10081558Srgrimes "Info Reply" /* " */ 10091558Srgrimes}; 10101558Srgrimes#endif 10111558Srgrimes 10121558Srgrimes/* 10131558Srgrimes * pr_icmph -- 10141558Srgrimes * Print a descriptive string about an ICMP header. 10151558Srgrimes */ 101623247Swollmanstatic void 10171558Srgrimespr_icmph(icp) 10181558Srgrimes struct icmp *icp; 10191558Srgrimes{ 10201558Srgrimes switch(icp->icmp_type) { 10211558Srgrimes case ICMP_ECHOREPLY: 10221558Srgrimes (void)printf("Echo Reply\n"); 10231558Srgrimes /* XXX ID + Seq + Data */ 10241558Srgrimes break; 10251558Srgrimes case ICMP_UNREACH: 10261558Srgrimes switch(icp->icmp_code) { 10271558Srgrimes case ICMP_UNREACH_NET: 10281558Srgrimes (void)printf("Destination Net Unreachable\n"); 10291558Srgrimes break; 10301558Srgrimes case ICMP_UNREACH_HOST: 10311558Srgrimes (void)printf("Destination Host Unreachable\n"); 10321558Srgrimes break; 10331558Srgrimes case ICMP_UNREACH_PROTOCOL: 10341558Srgrimes (void)printf("Destination Protocol Unreachable\n"); 10351558Srgrimes break; 10361558Srgrimes case ICMP_UNREACH_PORT: 10371558Srgrimes (void)printf("Destination Port Unreachable\n"); 10381558Srgrimes break; 10391558Srgrimes case ICMP_UNREACH_NEEDFRAG: 104017724Sfenner (void)printf("frag needed and DF set (MTU %d)\n", 104128059Sfenner ntohs(icp->icmp_nextmtu)); 10421558Srgrimes break; 10431558Srgrimes case ICMP_UNREACH_SRCFAIL: 10441558Srgrimes (void)printf("Source Route Failed\n"); 10451558Srgrimes break; 104617724Sfenner case ICMP_UNREACH_FILTER_PROHIB: 104717724Sfenner (void)printf("Communication prohibited by filter\n"); 104817724Sfenner break; 10491558Srgrimes default: 10501558Srgrimes (void)printf("Dest Unreachable, Bad Code: %d\n", 10511558Srgrimes icp->icmp_code); 10521558Srgrimes break; 10531558Srgrimes } 10541558Srgrimes /* Print returned IP header information */ 10551558Srgrimes#ifndef icmp_data 10561558Srgrimes pr_retip(&icp->icmp_ip); 10571558Srgrimes#else 10581558Srgrimes pr_retip((struct ip *)icp->icmp_data); 10591558Srgrimes#endif 10601558Srgrimes break; 10611558Srgrimes case ICMP_SOURCEQUENCH: 10621558Srgrimes (void)printf("Source Quench\n"); 10631558Srgrimes#ifndef icmp_data 10641558Srgrimes pr_retip(&icp->icmp_ip); 10651558Srgrimes#else 10661558Srgrimes pr_retip((struct ip *)icp->icmp_data); 10671558Srgrimes#endif 10681558Srgrimes break; 10691558Srgrimes case ICMP_REDIRECT: 10701558Srgrimes switch(icp->icmp_code) { 10711558Srgrimes case ICMP_REDIRECT_NET: 10721558Srgrimes (void)printf("Redirect Network"); 10731558Srgrimes break; 10741558Srgrimes case ICMP_REDIRECT_HOST: 10751558Srgrimes (void)printf("Redirect Host"); 10761558Srgrimes break; 10771558Srgrimes case ICMP_REDIRECT_TOSNET: 10781558Srgrimes (void)printf("Redirect Type of Service and Network"); 10791558Srgrimes break; 10801558Srgrimes case ICMP_REDIRECT_TOSHOST: 10811558Srgrimes (void)printf("Redirect Type of Service and Host"); 10821558Srgrimes break; 10831558Srgrimes default: 10841558Srgrimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 10851558Srgrimes break; 10861558Srgrimes } 108728059Sfenner (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 10881558Srgrimes#ifndef icmp_data 10891558Srgrimes pr_retip(&icp->icmp_ip); 10901558Srgrimes#else 10911558Srgrimes pr_retip((struct ip *)icp->icmp_data); 10921558Srgrimes#endif 10931558Srgrimes break; 10941558Srgrimes case ICMP_ECHO: 10951558Srgrimes (void)printf("Echo Request\n"); 10961558Srgrimes /* XXX ID + Seq + Data */ 10971558Srgrimes break; 10981558Srgrimes case ICMP_TIMXCEED: 10991558Srgrimes switch(icp->icmp_code) { 11001558Srgrimes case ICMP_TIMXCEED_INTRANS: 11011558Srgrimes (void)printf("Time to live exceeded\n"); 11021558Srgrimes break; 11031558Srgrimes case ICMP_TIMXCEED_REASS: 11041558Srgrimes (void)printf("Frag reassembly time exceeded\n"); 11051558Srgrimes break; 11061558Srgrimes default: 11071558Srgrimes (void)printf("Time exceeded, Bad Code: %d\n", 11081558Srgrimes icp->icmp_code); 11091558Srgrimes break; 11101558Srgrimes } 11111558Srgrimes#ifndef icmp_data 11121558Srgrimes pr_retip(&icp->icmp_ip); 11131558Srgrimes#else 11141558Srgrimes pr_retip((struct ip *)icp->icmp_data); 11151558Srgrimes#endif 11161558Srgrimes break; 11171558Srgrimes case ICMP_PARAMPROB: 11181558Srgrimes (void)printf("Parameter problem: pointer = 0x%02x\n", 11191558Srgrimes icp->icmp_hun.ih_pptr); 11201558Srgrimes#ifndef icmp_data 11211558Srgrimes pr_retip(&icp->icmp_ip); 11221558Srgrimes#else 11231558Srgrimes pr_retip((struct ip *)icp->icmp_data); 11241558Srgrimes#endif 11251558Srgrimes break; 11261558Srgrimes case ICMP_TSTAMP: 11271558Srgrimes (void)printf("Timestamp\n"); 11281558Srgrimes /* XXX ID + Seq + 3 timestamps */ 11291558Srgrimes break; 11301558Srgrimes case ICMP_TSTAMPREPLY: 11311558Srgrimes (void)printf("Timestamp Reply\n"); 11321558Srgrimes /* XXX ID + Seq + 3 timestamps */ 11331558Srgrimes break; 11341558Srgrimes case ICMP_IREQ: 11351558Srgrimes (void)printf("Information Request\n"); 11361558Srgrimes /* XXX ID + Seq */ 11371558Srgrimes break; 11381558Srgrimes case ICMP_IREQREPLY: 11391558Srgrimes (void)printf("Information Reply\n"); 11401558Srgrimes /* XXX ID + Seq */ 11411558Srgrimes break; 11421558Srgrimes case ICMP_MASKREQ: 11431558Srgrimes (void)printf("Address Mask Request\n"); 11441558Srgrimes break; 11451558Srgrimes case ICMP_MASKREPLY: 11461558Srgrimes (void)printf("Address Mask Reply\n"); 11471558Srgrimes break; 114817724Sfenner case ICMP_ROUTERADVERT: 114917724Sfenner (void)printf("Router Advertisement\n"); 115017724Sfenner break; 115117724Sfenner case ICMP_ROUTERSOLICIT: 115217724Sfenner (void)printf("Router Solicitation\n"); 115317724Sfenner break; 11541558Srgrimes default: 11551558Srgrimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 11561558Srgrimes } 11571558Srgrimes} 11581558Srgrimes 11591558Srgrimes/* 11601558Srgrimes * pr_iph -- 11611558Srgrimes * Print an IP header with options. 11621558Srgrimes */ 116323247Swollmanstatic void 11641558Srgrimespr_iph(ip) 11651558Srgrimes struct ip *ip; 11661558Srgrimes{ 11671558Srgrimes int hlen; 11681558Srgrimes u_char *cp; 11691558Srgrimes 11701558Srgrimes hlen = ip->ip_hl << 2; 11711558Srgrimes cp = (u_char *)ip + 20; /* point to options */ 11721558Srgrimes 117317724Sfenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 11741558Srgrimes (void)printf(" %1x %1x %02x %04x %04x", 117517724Sfenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 117617724Sfenner ntohs(ip->ip_id)); 117736089Sjb (void)printf(" %1lx %04lx", 117836089Sjb (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13, 117936089Sjb (u_long) ntohl(ip->ip_off) & 0x1fff); 118017724Sfenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 118117724Sfenner ntohs(ip->ip_sum)); 11821558Srgrimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr)); 11831558Srgrimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr)); 118417724Sfenner /* dump any option bytes */ 11851558Srgrimes while (hlen-- > 20) { 11861558Srgrimes (void)printf("%02x", *cp++); 11871558Srgrimes } 11881558Srgrimes (void)putchar('\n'); 11891558Srgrimes} 11901558Srgrimes 11911558Srgrimes/* 11921558Srgrimes * pr_addr -- 11931558Srgrimes * Return an ascii host address as a dotted quad and optionally with 11941558Srgrimes * a hostname. 11951558Srgrimes */ 119623247Swollmanstatic char * 119723247Swollmanpr_addr(ina) 119823247Swollman struct in_addr ina; 11991558Srgrimes{ 12001558Srgrimes struct hostent *hp; 120123251Simp static char buf[16 + 3 + MAXHOSTNAMELEN]; 12021558Srgrimes 12031558Srgrimes if ((options & F_NUMERIC) || 120423247Swollman !(hp = gethostbyaddr((char *)&ina, 4, AF_INET))) 120523247Swollman return inet_ntoa(ina); 12061558Srgrimes else 120717320Speter (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 120823247Swollman inet_ntoa(ina)); 12091558Srgrimes return(buf); 12101558Srgrimes} 12111558Srgrimes 12121558Srgrimes/* 12131558Srgrimes * pr_retip -- 12141558Srgrimes * Dump some info on a returned (via ICMP) IP packet. 12151558Srgrimes */ 121623247Swollmanstatic void 12171558Srgrimespr_retip(ip) 12181558Srgrimes struct ip *ip; 12191558Srgrimes{ 12201558Srgrimes int hlen; 12211558Srgrimes u_char *cp; 12221558Srgrimes 12231558Srgrimes pr_iph(ip); 12241558Srgrimes hlen = ip->ip_hl << 2; 12251558Srgrimes cp = (u_char *)ip + hlen; 12261558Srgrimes 12271558Srgrimes if (ip->ip_p == 6) 12281558Srgrimes (void)printf("TCP: from port %u, to port %u (decimal)\n", 12291558Srgrimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 12301558Srgrimes else if (ip->ip_p == 17) 12311558Srgrimes (void)printf("UDP: from port %u, to port %u (decimal)\n", 12321558Srgrimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 12331558Srgrimes} 12341558Srgrimes 123523247Swollmanstatic void 12361558Srgrimesfill(bp, patp) 12371558Srgrimes char *bp, *patp; 12381558Srgrimes{ 12391558Srgrimes register int ii, jj, kk; 12401558Srgrimes int pat[16]; 12411558Srgrimes char *cp; 12421558Srgrimes 124323247Swollman for (cp = patp; *cp; cp++) { 124423247Swollman if (!isxdigit(*cp)) 124523247Swollman errx(EX_USAGE, 124623247Swollman "patterns must be specified as hex digits"); 124723247Swollman 124823247Swollman } 12491558Srgrimes ii = sscanf(patp, 12501558Srgrimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 12511558Srgrimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 12521558Srgrimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 12531558Srgrimes &pat[13], &pat[14], &pat[15]); 12541558Srgrimes 12551558Srgrimes if (ii > 0) 12561558Srgrimes for (kk = 0; 125736089Sjb kk <= MAXPACKET - (8 + PHDR_LEN + ii); 12581558Srgrimes kk += ii) 12591558Srgrimes for (jj = 0; jj < ii; ++jj) 12601558Srgrimes bp[jj + kk] = pat[jj]; 12611558Srgrimes if (!(options & F_QUIET)) { 12621558Srgrimes (void)printf("PATTERN: 0x"); 12631558Srgrimes for (jj = 0; jj < ii; ++jj) 12641558Srgrimes (void)printf("%02x", bp[jj] & 0xFF); 12651558Srgrimes (void)printf("\n"); 12661558Srgrimes } 12671558Srgrimes} 12681558Srgrimes 126923247Swollmanstatic void 127037671Scharnierusage() 12711558Srgrimes{ 127237671Scharnier fprintf(stderr, "%s\n%s\n", 127337671Scharnier"usage: ping [-QRadfnqrv] [-c count] [-i wait] [-l preload] [-p pattern]", 127437671Scharnier" [-s packetsize] [host | [-L] [-I iface] [-T ttl] mcast-group]"); 127523247Swollman exit(EX_USAGE); 12761558Srgrimes} 1277