ping.c revision 53191
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[] = 4850476Speter "$FreeBSD: head/sbin/ping/ping.c 53191 1999-11-15 20:04:53Z pb $"; 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; 14442337Simpchar *shostname; 1451558Srgrimesint ident; /* process id to identify our packets */ 14623295Simpint uid; /* cached uid for micro-optimization */ 1471558Srgrimes 1481558Srgrimes/* counters */ 1491558Srgrimeslong npackets; /* max packets to transmit */ 1501558Srgrimeslong nreceived; /* # of packets we got back */ 1511558Srgrimeslong nrepeats; /* number of duplicates */ 1521558Srgrimeslong ntransmitted; /* sequence # for outbound packets = #sent */ 15338549Sdillonint interval = 1000; /* interval between packets, ms */ 1541558Srgrimes 1551558Srgrimes/* timing */ 1561558Srgrimesint timing; /* flag to do timing */ 1571558Srgrimesdouble tmin = 999999999.0; /* minimum round trip time */ 1581558Srgrimesdouble tmax = 0.0; /* maximum round trip time */ 1591558Srgrimesdouble tsum = 0.0; /* sum of all times, for doing average */ 16027508Swollmandouble tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 1611558Srgrimes 16227533Sbdevolatile sig_atomic_t finish_up; /* nonzero if we've been told to finish up */ 1633792Ssefint reset_kerninfo; 16427533Sbdevolatile sig_atomic_t siginfo_p; 1653792Ssef 16623247Swollmanstatic void fill(char *, char *); 16723247Swollmanstatic u_short in_cksum(u_short *, int); 16823247Swollmanstatic void check_status(void); 16927533Sbdestatic void finish(void) __dead2; 17023247Swollmanstatic void pinger(void); 17123247Swollmanstatic char *pr_addr(struct in_addr); 17223247Swollmanstatic void pr_icmph(struct icmp *); 17323247Swollmanstatic void pr_iph(struct ip *); 17436378Sfennerstatic void pr_pack(char *, int, struct sockaddr_in *, struct timeval *); 17523247Swollmanstatic void pr_retip(struct ip *); 17623247Swollmanstatic void status(int); 17727533Sbdestatic void stopit(int); 17823247Swollmanstatic void tvsub(struct timeval *, struct timeval *); 17937671Scharnierstatic void usage(void) __dead2; 1801558Srgrimes 18123247Swollmanint 1821558Srgrimesmain(argc, argv) 1831558Srgrimes int argc; 18423247Swollman char *const *argv; 1851558Srgrimes{ 18636378Sfenner struct timeval last, intvl; 1871558Srgrimes struct hostent *hp; 18842337Simp struct sockaddr_in *to, sin; 1893792Ssef struct termios ts; 1901558Srgrimes register int i; 19136378Sfenner int ch, hold, packlen, preload, sockerrno, almost_done = 0; 19220540Sfenner struct in_addr ifaddr; 19320540Sfenner unsigned char ttl, loop; 1941558Srgrimes u_char *datap, *packet; 19542337Simp char *source = NULL, *target, hnamebuf[MAXHOSTNAMELEN]; 19642337Simp char snamebuf[MAXHOSTNAMELEN]; 19723247Swollman char *ep; 19823247Swollman u_long ultmp; 1991558Srgrimes#ifdef IP_OPTIONS 2001558Srgrimes char rspace[3 + 4 * NROUTES + 1]; /* record route space */ 2011558Srgrimes#endif 20220195Ssef struct sigaction si_sa; 20336378Sfenner struct iovec iov; 20436378Sfenner struct msghdr msg; 20536378Sfenner struct sockaddr_in from; 20636378Sfenner char ctrl[sizeof(struct cmsghdr) + sizeof(struct timeval)]; 2071558Srgrimes 20817474Sfenner /* 20917474Sfenner * Do the stuff that we need root priv's for *first*, and 21017474Sfenner * then drop our setuid bit. Save error reporting for 21117474Sfenner * after arg parsing. 21217474Sfenner */ 21323247Swollman s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 21423247Swollman sockerrno = errno; 21517474Sfenner 21617474Sfenner setuid(getuid()); 21723295Simp uid = getuid(); 21817474Sfenner 2191558Srgrimes preload = 0; 2203792Ssef 22136089Sjb datap = &outpack[8 + PHDR_LEN]; 22242337Simp while ((ch = getopt(argc, argv, "I:LQRS:T:c:adfi:l:np:qrs:v")) != -1) { 2231558Srgrimes switch(ch) { 22422417Sdanny case 'a': 22522417Sdanny options |= F_AUDIBLE; 22622417Sdanny break; 2271558Srgrimes case 'c': 22823247Swollman ultmp = strtoul(optarg, &ep, 0); 22923247Swollman if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp) 23023247Swollman errx(EX_USAGE, 23123247Swollman "invalid count of packets to transmit: `%s'", 23223247Swollman optarg); 23323247Swollman npackets = ultmp; 2341558Srgrimes break; 2351558Srgrimes case 'd': 2361558Srgrimes options |= F_SO_DEBUG; 2371558Srgrimes break; 2381558Srgrimes case 'f': 23938549Sdillon if (uid) { 24023247Swollman errno = EPERM; 24123247Swollman err(EX_NOPERM, "-f flag"); 2421558Srgrimes } 2431558Srgrimes options |= F_FLOOD; 2441558Srgrimes setbuf(stdout, (char *)NULL); 2451558Srgrimes break; 2461558Srgrimes case 'i': /* wait between sending packets */ 24738549Sdillon { 24838549Sdillon double t = strtod(optarg, &ep) * 1000.0; 24938549Sdillon 25038549Sdillon if (*ep || ep == optarg || t > (double)INT_MAX) { 25138549Sdillon errx( 25238549Sdillon EX_USAGE, 25338549Sdillon "invalid timing interval: `%s'", 25438549Sdillon optarg 25538549Sdillon ); 25638549Sdillon } 25738549Sdillon options |= F_INTERVAL; 25838549Sdillon interval = (int)t; 25938549Sdillon if (uid && interval < 1000) { 26038549Sdillon errno = EPERM; 26138549Sdillon err(EX_NOPERM, "-i interval too short"); 26238549Sdillon } 26338549Sdillon } 2641558Srgrimes break; 26520540Sfenner case 'I': /* multicast interface */ 26623247Swollman if (inet_aton(optarg, &ifaddr) == 0) 26723247Swollman errx(EX_USAGE, 26823247Swollman "invalid multicast interface: `%s'", 26923247Swollman optarg); 27020540Sfenner options |= F_MIF; 27120540Sfenner break; 2721558Srgrimes case 'l': 27323247Swollman ultmp = strtoul(optarg, &ep, 0); 27423247Swollman if (*ep || ep == optarg || ultmp > INT_MAX) 27523247Swollman errx(EX_USAGE, 27623247Swollman "invalid preload value: `%s'", optarg); 27746643Smckay if (uid) { 27823251Simp errno = EPERM; 27923251Simp err(EX_NOPERM, "-l flag"); 28023251Simp } 28123247Swollman preload = ultmp; 2821558Srgrimes break; 28320540Sfenner case 'L': 28420540Sfenner options |= F_NOLOOP; 28520540Sfenner loop = 0; 28620540Sfenner break; 2871558Srgrimes case 'n': 2881558Srgrimes options |= F_NUMERIC; 2891558Srgrimes break; 2901558Srgrimes case 'p': /* fill buffer with user pattern */ 2911558Srgrimes options |= F_PINGFILLED; 2921558Srgrimes fill((char *)datap, optarg); 2931558Srgrimes break; 29417724Sfenner case 'Q': 29517724Sfenner options |= F_QUIET2; 29617724Sfenner break; 2971558Srgrimes case 'q': 2981558Srgrimes options |= F_QUIET; 2991558Srgrimes break; 3001558Srgrimes case 'R': 3011558Srgrimes options |= F_RROUTE; 3021558Srgrimes break; 3031558Srgrimes case 'r': 3041558Srgrimes options |= F_SO_DONTROUTE; 3051558Srgrimes break; 3061558Srgrimes case 's': /* size of packet to send */ 30738549Sdillon if (uid) { 30838549Sdillon errno = EPERM; 30938549Sdillon err(EX_NOPERM, "-s flag"); 31038549Sdillon } 31123247Swollman ultmp = strtoul(optarg, &ep, 0); 31223247Swollman if (ultmp > MAXPACKET) 31323247Swollman errx(EX_USAGE, "packet size too large: %lu", 31423247Swollman ultmp); 31523247Swollman if (*ep || ep == optarg || !ultmp) 31623247Swollman errx(EX_USAGE, "invalid packet size: `%s'", 31723247Swollman optarg); 31823247Swollman datalen = ultmp; 3191558Srgrimes break; 32042337Simp case 'S': 32142337Simp source = optarg; 32242337Simp break; 32320540Sfenner case 'T': /* multicast TTL */ 32423247Swollman ultmp = strtoul(optarg, &ep, 0); 32523247Swollman if (*ep || ep == optarg || ultmp > 255) 32623247Swollman errx(EX_USAGE, "invalid multicast TTL: `%s'", 32723247Swollman optarg); 32823247Swollman ttl = ultmp; 32920540Sfenner options |= F_MTTL; 33020540Sfenner break; 3311558Srgrimes case 'v': 3321558Srgrimes options |= F_VERBOSE; 3331558Srgrimes break; 3341558Srgrimes default: 33537671Scharnier usage(); 3361558Srgrimes } 33723247Swollman } 3381558Srgrimes 33923247Swollman if (argc - optind != 1) 34037671Scharnier usage(); 34123247Swollman target = argv[optind]; 3421558Srgrimes 34342337Simp if (source) { 34442337Simp bzero((char *)&sin, sizeof(sin)); 34542337Simp sin.sin_family = AF_INET; 34642337Simp if (inet_aton(source, &sin.sin_addr) != 0) { 34742337Simp shostname = source; 34842337Simp } else { 34942337Simp hp = gethostbyname2(source, AF_INET); 35042337Simp if (!hp) 35142337Simp errx(EX_NOHOST, "cannot resolve %s: %s", 35242337Simp source, hstrerror(h_errno)); 35342337Simp 35442337Simp sin.sin_len = sizeof sin; 35542337Simp if (hp->h_length > sizeof(sin.sin_addr)) 35642337Simp errx(1,"gethostbyname2: illegal address"); 35742337Simp memcpy(&sin.sin_addr, hp->h_addr_list[0], 35842337Simp sizeof (sin.sin_addr)); 35942337Simp (void)strncpy(snamebuf, hp->h_name, 36042337Simp sizeof(snamebuf) - 1); 36142337Simp snamebuf[sizeof(snamebuf) - 1] = '\0'; 36242337Simp shostname = snamebuf; 36342337Simp } 36442337Simp if (bind(s, (struct sockaddr *)&sin, sizeof sin) == -1) 36542337Simp err(1, "bind"); 36642337Simp } 36742337Simp 3681558Srgrimes bzero((char *)&whereto, sizeof(struct sockaddr)); 3691558Srgrimes to = (struct sockaddr_in *)&whereto; 3701558Srgrimes to->sin_family = AF_INET; 37123247Swollman if (inet_aton(target, &to->sin_addr) != 0) { 3721558Srgrimes hostname = target; 37323247Swollman } else { 37423247Swollman hp = gethostbyname2(target, AF_INET); 37523247Swollman if (!hp) 37623247Swollman errx(EX_NOHOST, "cannot resolve %s: %s", 37723247Swollman target, hstrerror(h_errno)); 37823247Swollman 37923247Swollman to->sin_len = sizeof *to; 38023327Simp if (hp->h_length > sizeof(to->sin_addr)) 38123327Simp errx(1,"gethostbyname2 returned an illegal address"); 38223247Swollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 3831558Srgrimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 38431956Simp hnamebuf[sizeof(hnamebuf) - 1] = '\0'; 3851558Srgrimes hostname = hnamebuf; 3861558Srgrimes } 3871558Srgrimes 38823247Swollman if (options & F_FLOOD && options & F_INTERVAL) 38923247Swollman errx(EX_USAGE, "-f and -i: incompatible options"); 3901558Srgrimes 39123247Swollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 39223247Swollman errx(EX_USAGE, 39323247Swollman "-f flag cannot be used with multicast destination"); 39423247Swollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 39523247Swollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 39623247Swollman errx(EX_USAGE, 39723247Swollman "-I, -L, -T flags cannot be used with unicast destination"); 39823247Swollman 39936089Sjb if (datalen >= PHDR_LEN) /* can we time transfer */ 4001558Srgrimes timing = 1; 4011558Srgrimes packlen = datalen + MAXIPLEN + MAXICMPLEN; 40223247Swollman if (!(packet = (u_char *)malloc((size_t)packlen))) 40323247Swollman err(EX_UNAVAILABLE, "malloc"); 40423247Swollman 4051558Srgrimes if (!(options & F_PINGFILLED)) 40636089Sjb for (i = PHDR_LEN; i < datalen; ++i) 4071558Srgrimes *datap++ = i; 4081558Srgrimes 4091558Srgrimes ident = getpid() & 0xFFFF; 4101558Srgrimes 41117474Sfenner if (s < 0) { 41217474Sfenner errno = sockerrno; 41323247Swollman err(EX_OSERR, "socket"); 4141558Srgrimes } 4151558Srgrimes hold = 1; 4161558Srgrimes if (options & F_SO_DEBUG) 4171558Srgrimes (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold, 4181558Srgrimes sizeof(hold)); 4191558Srgrimes if (options & F_SO_DONTROUTE) 4201558Srgrimes (void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 4211558Srgrimes sizeof(hold)); 4221558Srgrimes 4231558Srgrimes /* record route option */ 4241558Srgrimes if (options & F_RROUTE) { 4251558Srgrimes#ifdef IP_OPTIONS 42636378Sfenner bzero(rspace, sizeof(rspace)); 4271558Srgrimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 42836378Sfenner rspace[IPOPT_OLEN] = sizeof(rspace) - 1; 4291558Srgrimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 43036378Sfenner rspace[sizeof(rspace) - 1] = IPOPT_EOL; 4311558Srgrimes if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace, 43223247Swollman sizeof(rspace)) < 0) 43323247Swollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 4341558Srgrimes#else 43523247Swollman errx(EX_UNAVAILABLE, 43623247Swollman "record route not available in this implementation"); 4371558Srgrimes#endif /* IP_OPTIONS */ 4381558Srgrimes } 4391558Srgrimes 44020540Sfenner if (options & F_NOLOOP) { 44120540Sfenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 44220540Sfenner sizeof(loop)) < 0) { 44323247Swollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 44420540Sfenner } 44520540Sfenner } 44620540Sfenner if (options & F_MTTL) { 44720540Sfenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, 44820540Sfenner sizeof(ttl)) < 0) { 44923247Swollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 45020540Sfenner } 45120540Sfenner } 45220540Sfenner if (options & F_MIF) { 45320540Sfenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 45420540Sfenner sizeof(ifaddr)) < 0) { 45523247Swollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 45620540Sfenner } 45720540Sfenner } 45836378Sfenner#ifdef SO_TIMESTAMP 45936378Sfenner { int on = 1; 46036378Sfenner if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) < 0) 46136378Sfenner err(EX_OSERR, "setsockopt SO_TIMESTAMP"); 46236378Sfenner } 46336378Sfenner#endif 46420540Sfenner 4651558Srgrimes /* 4661558Srgrimes * When pinging the broadcast address, you can get a lot of answers. 4671558Srgrimes * Doing something so evil is useful if you are trying to stress the 4681558Srgrimes * ethernet, or just want to fill the arp cache to get some stuff for 46923247Swollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 47023247Swollman * or multicast pings if they wish. 4711558Srgrimes */ 4721558Srgrimes hold = 48 * 1024; 4731558Srgrimes (void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 4741558Srgrimes sizeof(hold)); 4751558Srgrimes 47642337Simp if (to->sin_family == AF_INET) { 47742337Simp (void)printf("PING %s (%s)", hostname, 47842337Simp inet_ntoa(to->sin_addr)); 47942337Simp if (source) 48042337Simp (void)printf(" from %s", shostname); 48142337Simp (void)printf(": %d data bytes\n", datalen); 48242337Simp } else 4831558Srgrimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 4841558Srgrimes 48520280Sbde /* 48627354Ssef * Use sigaction() instead of signal() to get unambiguous semantics, 48727354Ssef * in particular with SA_RESTART not set. 48820280Sbde */ 48927354Ssef 49020205Spst sigemptyset(&si_sa.sa_mask); 49120195Ssef si_sa.sa_flags = 0; 49227354Ssef 49327354Ssef si_sa.sa_handler = stopit; 49427354Ssef if (sigaction(SIGINT, &si_sa, 0) == -1) { 49527354Ssef err(EX_OSERR, "sigaction SIGINT"); 49627354Ssef } 49727354Ssef 49827354Ssef si_sa.sa_handler = status; 49920195Ssef if (sigaction(SIGINFO, &si_sa, 0) == -1) { 50023385Simp err(EX_OSERR, "sigaction"); 50120195Ssef } 50220195Ssef 50336378Sfenner bzero(&msg, sizeof(msg)); 50436378Sfenner msg.msg_name = (caddr_t)&from; 50536378Sfenner msg.msg_iov = &iov; 50636378Sfenner msg.msg_iovlen = 1; 50736378Sfenner#ifdef SO_TIMESTAMP 50836378Sfenner msg.msg_control = (caddr_t)ctrl; 50936378Sfenner#endif 51036378Sfenner iov.iov_base = packet; 51136378Sfenner iov.iov_len = packlen; 51236378Sfenner 51319864Ssef if (tcgetattr(STDOUT_FILENO, &ts) != -1) { 51419864Ssef reset_kerninfo = !(ts.c_lflag & NOKERNINFO); 51519864Ssef ts.c_lflag |= NOKERNINFO; 51619864Ssef tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 51719864Ssef } 51819864Ssef 5191558Srgrimes while (preload--) /* fire off them quickies */ 5201558Srgrimes pinger(); 5211558Srgrimes 52236378Sfenner if (options & F_FLOOD) { 52336378Sfenner intvl.tv_sec = 0; 52436378Sfenner intvl.tv_usec = 10000; 52536378Sfenner } else { 52638549Sdillon intvl.tv_sec = interval / 1000; 52738549Sdillon intvl.tv_usec = interval % 1000 * 1000; 52836378Sfenner } 5291558Srgrimes 53036378Sfenner pinger(); /* send the first ping */ 53136378Sfenner (void)gettimeofday(&last, NULL); 53236378Sfenner 53327533Sbde while (!finish_up) { 5341558Srgrimes register int cc; 53536378Sfenner int n; 53636378Sfenner struct timeval timeout, now; 53736378Sfenner fd_set rfds; 5381558Srgrimes 53920280Sbde check_status(); 54036378Sfenner FD_ZERO(&rfds); 54136378Sfenner FD_SET(s, &rfds); 54236378Sfenner (void)gettimeofday(&now, NULL); 54336378Sfenner timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec; 54436378Sfenner timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec; 54536378Sfenner while (timeout.tv_usec < 0) { 54636378Sfenner timeout.tv_usec += 1000000; 54736378Sfenner timeout.tv_sec--; 5481558Srgrimes } 54937671Scharnier while (timeout.tv_usec >= 1000000) { 55036378Sfenner timeout.tv_usec -= 1000000; 55136378Sfenner timeout.tv_sec++; 55236378Sfenner } 55336378Sfenner if (timeout.tv_sec < 0) 55436378Sfenner timeout.tv_sec = timeout.tv_usec = 0; 55536378Sfenner n = select(s + 1, &rfds, NULL, NULL, &timeout); 55646643Smckay if (n < 0) 55746643Smckay continue; /* Must be EINTR. */ 55836378Sfenner if (n == 1) { 55936378Sfenner struct timeval *t = 0; 56036378Sfenner#ifdef SO_TIMESTAMP 56136378Sfenner struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl; 56236378Sfenner 56336378Sfenner msg.msg_controllen = sizeof(ctrl); 56436378Sfenner#endif 56536378Sfenner msg.msg_namelen = sizeof(from); 56636378Sfenner if ((cc = recvmsg(s, &msg, 0)) < 0) { 56736378Sfenner if (errno == EINTR) 56836378Sfenner continue; 56937671Scharnier warn("recvmsg"); 5701558Srgrimes continue; 57136378Sfenner } 57236378Sfenner#ifdef SO_TIMESTAMP 57336378Sfenner if (cmsg->cmsg_level == SOL_SOCKET && 57436378Sfenner cmsg->cmsg_type == SCM_TIMESTAMP && 57536713Sjb cmsg->cmsg_len == (sizeof *cmsg + sizeof *t)) { 57636713Sjb /* Copy to avoid alignment problems: */ 57736713Sjb memcpy(&now,CMSG_DATA(cmsg),sizeof(now)); 57836713Sjb t = &now; 57936713Sjb } 58036378Sfenner#endif 58136378Sfenner if (t == 0) { 58236378Sfenner (void)gettimeofday(&now, NULL); 58336378Sfenner t = &now; 58436378Sfenner } 58536378Sfenner pr_pack((char *)packet, cc, &from, t); 58636378Sfenner if (npackets && nreceived >= npackets) 58736378Sfenner break; 5881558Srgrimes } 58946643Smckay if (n == 0 || options & F_FLOOD) { 59036378Sfenner if (!npackets || ntransmitted < npackets) 59136378Sfenner pinger(); 59236378Sfenner else { 59336378Sfenner if (almost_done) 59436378Sfenner break; 59536378Sfenner almost_done = 1; 59646643Smckay intvl.tv_usec = 0; 59736378Sfenner if (nreceived) { 59836378Sfenner intvl.tv_sec = 2 * tmax / 1000; 59936378Sfenner if (!intvl.tv_sec) 60036378Sfenner intvl.tv_sec = 1; 60136378Sfenner } else 60236378Sfenner intvl.tv_sec = MAXWAIT; 60336378Sfenner } 60436378Sfenner (void)gettimeofday(&last, NULL); 60536378Sfenner } 6061558Srgrimes } 60727533Sbde finish(); 6081558Srgrimes /* NOTREACHED */ 60923251Simp exit(0); /* Make the compiler happy */ 6101558Srgrimes} 6111558Srgrimes 6121558Srgrimes/* 61327533Sbde * stopit -- 61427533Sbde * Set the global bit that causes the main loop to quit. 61527533Sbde * Do NOT call finish() from here, since finish() does far too much 61627533Sbde * to be called from a signal handler. 61727299Sjulian */ 61827299Sjulianvoid 61927533Sbdestopit(sig) 62027533Sbde int sig; 62127299Sjulian{ 62227299Sjulian finish_up = 1; 62327299Sjulian} 62427299Sjulian 62527299Sjulian/* 6261558Srgrimes * pinger -- 6271558Srgrimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 6281558Srgrimes * will be added on by the kernel. The ID field is our UNIX process ID, 6291558Srgrimes * and the sequence number is an ascending integer. The first 8 bytes 63017724Sfenner * of the data portion are used to hold a UNIX "timeval" struct in host 6311558Srgrimes * byte-order, to compute the round-trip time. 6321558Srgrimes */ 63323247Swollmanstatic void 63423247Swollmanpinger(void) 6351558Srgrimes{ 6361558Srgrimes register struct icmp *icp; 6371558Srgrimes register int cc; 6381558Srgrimes int i; 6391558Srgrimes 6401558Srgrimes icp = (struct icmp *)outpack; 6411558Srgrimes icp->icmp_type = ICMP_ECHO; 6421558Srgrimes icp->icmp_code = 0; 6431558Srgrimes icp->icmp_cksum = 0; 64427301Sjulian icp->icmp_seq = ntransmitted; 6451558Srgrimes icp->icmp_id = ident; /* ID */ 6461558Srgrimes 6471558Srgrimes CLR(icp->icmp_seq % mx_dup_ck); 6481558Srgrimes 6491558Srgrimes if (timing) 6501558Srgrimes (void)gettimeofday((struct timeval *)&outpack[8], 6511558Srgrimes (struct timezone *)NULL); 6521558Srgrimes 65336089Sjb cc = datalen + PHDR_LEN; /* skips ICMP portion */ 6541558Srgrimes 6551558Srgrimes /* compute ICMP checksum here */ 6561558Srgrimes icp->icmp_cksum = in_cksum((u_short *)icp, cc); 6571558Srgrimes 6581558Srgrimes i = sendto(s, (char *)outpack, cc, 0, &whereto, 6591558Srgrimes sizeof(struct sockaddr)); 6601558Srgrimes 6611558Srgrimes if (i < 0 || i != cc) { 66223247Swollman if (i < 0) { 66327533Sbde if (options & F_FLOOD && errno == ENOBUFS) { 66427299Sjulian usleep(FLOOD_BACKOFF); 66527299Sjulian return; 66627299Sjulian } 66723247Swollman warn("sendto"); 66823247Swollman } else { 66923247Swollman warn("%s: partial write: %d of %d bytes", 67035216Sphk hostname, i, cc); 67123247Swollman } 67227945Sjulian } 67327945Sjulian ntransmitted++; 6741558Srgrimes if (!(options & F_QUIET) && options & F_FLOOD) 6751558Srgrimes (void)write(STDOUT_FILENO, &DOT, 1); 6761558Srgrimes} 6771558Srgrimes 6781558Srgrimes/* 6791558Srgrimes * pr_pack -- 6801558Srgrimes * Print out the packet, if it came from us. This logic is necessary 6811558Srgrimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 6821558Srgrimes * which arrive ('tis only fair). This permits multiple copies of this 6831558Srgrimes * program to be run without having intermingled output (or statistics!). 6841558Srgrimes */ 68523247Swollmanstatic void 68636378Sfennerpr_pack(buf, cc, from, tv) 6871558Srgrimes char *buf; 6881558Srgrimes int cc; 6891558Srgrimes struct sockaddr_in *from; 69036378Sfenner struct timeval *tv; 6911558Srgrimes{ 6921558Srgrimes register struct icmp *icp; 6931558Srgrimes register u_long l; 6941558Srgrimes register int i, j; 6951558Srgrimes register u_char *cp,*dp; 6961558Srgrimes static int old_rrlen; 6971558Srgrimes static char old_rr[MAX_IPOPTLEN]; 6981558Srgrimes struct ip *ip; 69936378Sfenner struct timeval *tp; 70027533Sbde double triptime; 7011558Srgrimes int hlen, dupflag; 7021558Srgrimes 7031558Srgrimes /* Check the IP header */ 7041558Srgrimes ip = (struct ip *)buf; 7051558Srgrimes hlen = ip->ip_hl << 2; 7061558Srgrimes if (cc < hlen + ICMP_MINLEN) { 7071558Srgrimes if (options & F_VERBOSE) 70823247Swollman warn("packet too short (%d bytes) from %s", cc, 70923247Swollman inet_ntoa(from->sin_addr)); 7101558Srgrimes return; 7111558Srgrimes } 7121558Srgrimes 7131558Srgrimes /* Now the ICMP part */ 7141558Srgrimes cc -= hlen; 7151558Srgrimes icp = (struct icmp *)(buf + hlen); 7161558Srgrimes if (icp->icmp_type == ICMP_ECHOREPLY) { 7171558Srgrimes if (icp->icmp_id != ident) 7181558Srgrimes return; /* 'Twas not our ECHO */ 7191558Srgrimes ++nreceived; 72027533Sbde triptime = 0.0; 7211558Srgrimes if (timing) { 72236089Sjb struct timeval tv1; 7231558Srgrimes#ifndef icmp_data 7241558Srgrimes tp = (struct timeval *)&icp->icmp_ip; 7251558Srgrimes#else 7261558Srgrimes tp = (struct timeval *)icp->icmp_data; 7271558Srgrimes#endif 72836089Sjb /* Avoid unaligned data: */ 72936089Sjb memcpy(&tv1,tp,sizeof(tv1)); 73036378Sfenner tvsub(tv, &tv1); 73136378Sfenner triptime = ((double)tv->tv_sec) * 1000.0 + 73236378Sfenner ((double)tv->tv_usec) / 1000.0; 7331558Srgrimes tsum += triptime; 73427508Swollman tsumsq += triptime * triptime; 7351558Srgrimes if (triptime < tmin) 7361558Srgrimes tmin = triptime; 7371558Srgrimes if (triptime > tmax) 7381558Srgrimes tmax = triptime; 7391558Srgrimes } 7401558Srgrimes 7411558Srgrimes if (TST(icp->icmp_seq % mx_dup_ck)) { 7421558Srgrimes ++nrepeats; 7431558Srgrimes --nreceived; 7441558Srgrimes dupflag = 1; 7451558Srgrimes } else { 7461558Srgrimes SET(icp->icmp_seq % mx_dup_ck); 7471558Srgrimes dupflag = 0; 7481558Srgrimes } 7491558Srgrimes 7501558Srgrimes if (options & F_QUIET) 7511558Srgrimes return; 7521558Srgrimes 7531558Srgrimes if (options & F_FLOOD) 7541558Srgrimes (void)write(STDOUT_FILENO, &BSPACE, 1); 7551558Srgrimes else { 7561558Srgrimes (void)printf("%d bytes from %s: icmp_seq=%u", cc, 7571558Srgrimes inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr), 7581558Srgrimes icp->icmp_seq); 7591558Srgrimes (void)printf(" ttl=%d", ip->ip_ttl); 7601558Srgrimes if (timing) 7611859Sdg (void)printf(" time=%.3f ms", triptime); 7621558Srgrimes if (dupflag) 7631558Srgrimes (void)printf(" (DUP!)"); 76422417Sdanny if (options & F_AUDIBLE) 76522417Sdanny (void)printf("\a"); 7661558Srgrimes /* check the data */ 76736089Sjb cp = (u_char*)&icp->icmp_data[PHDR_LEN]; 76836089Sjb dp = &outpack[8 + PHDR_LEN]; 76936089Sjb for (i = PHDR_LEN; i < datalen; ++i, ++cp, ++dp) { 7701558Srgrimes if (*cp != *dp) { 7711558Srgrimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 7721558Srgrimes i, *dp, *cp); 77336089Sjb printf("\ncp:"); 7741558Srgrimes cp = (u_char*)&icp->icmp_data[0]; 77536089Sjb for (i = 0; i < datalen; ++i, ++cp) { 7761558Srgrimes if ((i % 32) == 8) 7771558Srgrimes (void)printf("\n\t"); 7781558Srgrimes (void)printf("%x ", *cp); 7791558Srgrimes } 78036089Sjb printf("\ndp:"); 78136089Sjb cp = &outpack[8]; 78236089Sjb for (i = 0; i < datalen; ++i, ++cp) { 78336089Sjb if ((i % 32) == 8) 78436089Sjb (void)printf("\n\t"); 78536089Sjb (void)printf("%x ", *cp); 78636089Sjb } 7871558Srgrimes break; 7881558Srgrimes } 7891558Srgrimes } 7901558Srgrimes } 7911558Srgrimes } else { 79217724Sfenner /* 79317724Sfenner * We've got something other than an ECHOREPLY. 79417724Sfenner * See if it's a reply to something that we sent. 79517724Sfenner * We can compare IP destination, protocol, 79617724Sfenner * and ICMP type and ID. 79723251Simp * 79823251Simp * Only print all the error messages if we are running 79923251Simp * as root to avoid leaking information not normally 80023251Simp * available to those not running as root. 80117724Sfenner */ 80217724Sfenner#ifndef icmp_data 80317724Sfenner struct ip *oip = &icp->icmp_ip; 80417724Sfenner#else 80517724Sfenner struct ip *oip = (struct ip *)icp->icmp_data; 80617724Sfenner#endif 80717724Sfenner struct icmp *oicmp = (struct icmp *)(oip + 1); 80817724Sfenner 80923295Simp if (((options & F_VERBOSE) && uid == 0) || 81017724Sfenner (!(options & F_QUIET2) && 81117724Sfenner (oip->ip_dst.s_addr == 81217724Sfenner ((struct sockaddr_in *)&whereto)->sin_addr.s_addr) && 81317724Sfenner (oip->ip_p == IPPROTO_ICMP) && 81417724Sfenner (oicmp->icmp_type == ICMP_ECHO) && 81517724Sfenner (oicmp->icmp_id == ident))) { 81617724Sfenner (void)printf("%d bytes from %s: ", cc, 81723247Swollman pr_addr(from->sin_addr)); 81817724Sfenner pr_icmph(icp); 81917724Sfenner } else 82017724Sfenner return; 8211558Srgrimes } 8221558Srgrimes 8231558Srgrimes /* Display any IP options */ 8241558Srgrimes cp = (u_char *)buf + sizeof(struct ip); 8251558Srgrimes 8261558Srgrimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 8271558Srgrimes switch (*cp) { 8281558Srgrimes case IPOPT_EOL: 8291558Srgrimes hlen = 0; 8301558Srgrimes break; 8311558Srgrimes case IPOPT_LSRR: 8321558Srgrimes (void)printf("\nLSRR: "); 8331558Srgrimes hlen -= 2; 8341558Srgrimes j = *++cp; 8351558Srgrimes ++cp; 8361558Srgrimes if (j > IPOPT_MINOFF) 8371558Srgrimes for (;;) { 8381558Srgrimes l = *++cp; 8391558Srgrimes l = (l<<8) + *++cp; 8401558Srgrimes l = (l<<8) + *++cp; 8411558Srgrimes l = (l<<8) + *++cp; 84223247Swollman if (l == 0) { 84323247Swollman printf("\t0.0.0.0"); 84423247Swollman } else { 84523247Swollman struct in_addr ina; 84623247Swollman ina.s_addr = ntohl(l); 84723247Swollman printf("\t%s", pr_addr(ina)); 84823247Swollman } 8491558Srgrimes hlen -= 4; 8501558Srgrimes j -= 4; 8511558Srgrimes if (j <= IPOPT_MINOFF) 8521558Srgrimes break; 8531558Srgrimes (void)putchar('\n'); 8541558Srgrimes } 8551558Srgrimes break; 8561558Srgrimes case IPOPT_RR: 8571558Srgrimes j = *++cp; /* get length */ 8581558Srgrimes i = *++cp; /* and pointer */ 8591558Srgrimes hlen -= 2; 8601558Srgrimes if (i > j) 8611558Srgrimes i = j; 8621558Srgrimes i -= IPOPT_MINOFF; 8631558Srgrimes if (i <= 0) 8641558Srgrimes continue; 8651558Srgrimes if (i == old_rrlen 8661558Srgrimes && cp == (u_char *)buf + sizeof(struct ip) + 2 8671558Srgrimes && !bcmp((char *)cp, old_rr, i) 8681558Srgrimes && !(options & F_FLOOD)) { 8691558Srgrimes (void)printf("\t(same route)"); 8701558Srgrimes i = ((i + 3) / 4) * 4; 8711558Srgrimes hlen -= i; 8721558Srgrimes cp += i; 8731558Srgrimes break; 8741558Srgrimes } 87534995Seivind if (i < MAX_IPOPTLEN) { 87634976Simp old_rrlen = i; 87734976Simp bcopy((char *)cp, old_rr, i); 87834976Simp } else 87934976Simp old_rrlen = 0; 88034976Simp 8811558Srgrimes (void)printf("\nRR: "); 88234976Simp j = 0; 8831558Srgrimes for (;;) { 8841558Srgrimes l = *++cp; 8851558Srgrimes l = (l<<8) + *++cp; 8861558Srgrimes l = (l<<8) + *++cp; 8871558Srgrimes l = (l<<8) + *++cp; 88823247Swollman if (l == 0) { 88923247Swollman printf("\t0.0.0.0"); 89023247Swollman } else { 89123247Swollman struct in_addr ina; 89223247Swollman ina.s_addr = ntohl(l); 89323247Swollman printf("\t%s", pr_addr(ina)); 89423247Swollman } 8951558Srgrimes hlen -= 4; 8961558Srgrimes i -= 4; 89734976Simp j += 4; 8981558Srgrimes if (i <= 0) 8991558Srgrimes break; 90034976Simp if (j >= MAX_IPOPTLEN) { 90134976Simp (void) printf("\t(truncated route)"); 90234976Simp break; 90334976Simp } 9041558Srgrimes (void)putchar('\n'); 9051558Srgrimes } 9061558Srgrimes break; 9071558Srgrimes case IPOPT_NOP: 9081558Srgrimes (void)printf("\nNOP"); 9091558Srgrimes break; 9101558Srgrimes default: 9111558Srgrimes (void)printf("\nunknown option %x", *cp); 9121558Srgrimes break; 9131558Srgrimes } 9141558Srgrimes if (!(options & F_FLOOD)) { 9151558Srgrimes (void)putchar('\n'); 9161558Srgrimes (void)fflush(stdout); 9171558Srgrimes } 9181558Srgrimes} 9191558Srgrimes 9201558Srgrimes/* 9211558Srgrimes * in_cksum -- 9221558Srgrimes * Checksum routine for Internet Protocol family headers (C Version) 9231558Srgrimes */ 92423247Swollmanu_short 9251558Srgrimesin_cksum(addr, len) 9261558Srgrimes u_short *addr; 9271558Srgrimes int len; 9281558Srgrimes{ 9291558Srgrimes register int nleft = len; 9301558Srgrimes register u_short *w = addr; 9311558Srgrimes register int sum = 0; 93253191Spb union { 93353191Spb u_int16_t us; 93453191Spb u_int8_t uc[2]; 93553191Spb } answer; 9361558Srgrimes 9371558Srgrimes /* 9381558Srgrimes * Our algorithm is simple, using a 32 bit accumulator (sum), we add 9391558Srgrimes * sequential 16 bit words to it, and at the end, fold back all the 9401558Srgrimes * carry bits from the top 16 bits into the lower 16 bits. 9411558Srgrimes */ 9421558Srgrimes while (nleft > 1) { 9431558Srgrimes sum += *w++; 9441558Srgrimes nleft -= 2; 9451558Srgrimes } 9461558Srgrimes 9471558Srgrimes /* mop up an odd byte, if necessary */ 9481558Srgrimes if (nleft == 1) { 94953191Spb answer.uc[0] = *(u_char *)w; 95053191Spb answer.uc[1] = 0; 95153191Spb sum += answer.us; 9521558Srgrimes } 9531558Srgrimes 9541558Srgrimes /* add back carry outs from top 16 bits to low 16 bits */ 9551558Srgrimes sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 9561558Srgrimes sum += (sum >> 16); /* add carry */ 95753191Spb answer.us = ~sum; /* truncate to 16 bits */ 95853191Spb return(answer.us); 9591558Srgrimes} 9601558Srgrimes 9611558Srgrimes/* 9621558Srgrimes * tvsub -- 9631558Srgrimes * Subtract 2 timeval structs: out = out - in. Out is assumed to 9641558Srgrimes * be >= in. 9651558Srgrimes */ 96623247Swollmanstatic void 9671558Srgrimestvsub(out, in) 9681558Srgrimes register struct timeval *out, *in; 9691558Srgrimes{ 9701558Srgrimes if ((out->tv_usec -= in->tv_usec) < 0) { 9711558Srgrimes --out->tv_sec; 9721558Srgrimes out->tv_usec += 1000000; 9731558Srgrimes } 9741558Srgrimes out->tv_sec -= in->tv_sec; 9751558Srgrimes} 9761558Srgrimes 9771558Srgrimes/* 9783792Ssef * status -- 9793792Ssef * Print out statistics when SIGINFO is received. 9803792Ssef */ 9813792Ssef 98223247Swollmanstatic void 98320280Sbdestatus(sig) 98420280Sbde int sig; 98520280Sbde{ 98620195Ssef siginfo_p = 1; 98720195Ssef} 98820195Ssef 98923247Swollmanstatic void 99020195Ssefcheck_status() 9913792Ssef{ 99220195Ssef if (siginfo_p) { 99320195Ssef siginfo_p = 0; 99420280Sbde (void)fprintf(stderr, 99520280Sbde "\r%ld/%ld packets received (%.0f%%) %.3f min / %.3f avg / %.3f max\n", 99620280Sbde nreceived, ntransmitted, 99720280Sbde ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0, 99820280Sbde nreceived ? tmin : 0.0, 99920280Sbde nreceived + nrepeats ? tsum / (nreceived + nrepeats) : tsum, 100020280Sbde tmax); 100120195Ssef } 10023792Ssef} 10033792Ssef 10043792Ssef/* 10051558Srgrimes * finish -- 10061558Srgrimes * Print out statistics, and give up. 10071558Srgrimes */ 100823247Swollmanstatic void 100927533Sbdefinish() 10101558Srgrimes{ 10113792Ssef struct termios ts; 10121558Srgrimes 10131558Srgrimes (void)signal(SIGINT, SIG_IGN); 101427354Ssef (void)signal(SIGALRM, SIG_IGN); 10151558Srgrimes (void)putchar('\n'); 10161558Srgrimes (void)fflush(stdout); 10171558Srgrimes (void)printf("--- %s ping statistics ---\n", hostname); 10181558Srgrimes (void)printf("%ld packets transmitted, ", ntransmitted); 10191558Srgrimes (void)printf("%ld packets received, ", nreceived); 10201558Srgrimes if (nrepeats) 10211558Srgrimes (void)printf("+%ld duplicates, ", nrepeats); 102246080Simp if (ntransmitted) { 10231558Srgrimes if (nreceived > ntransmitted) 10241558Srgrimes (void)printf("-- somebody's printing up packets!"); 10251558Srgrimes else 10261558Srgrimes (void)printf("%d%% packet loss", 10271558Srgrimes (int) (((ntransmitted - nreceived) * 100) / 10281558Srgrimes ntransmitted)); 102946080Simp } 10301558Srgrimes (void)putchar('\n'); 103127508Swollman if (nreceived && timing) { 103227508Swollman double n = nreceived + nrepeats; 103327508Swollman double avg = tsum / n; 103427508Swollman double vari = tsumsq / n - avg * avg; 103527508Swollman printf("round-trip min/avg/max/stddev = " 103627508Swollman "%.3f/%.3f/%.3f/%.3f ms\n", 103727508Swollman tmin, avg, tmax, sqrt(vari)); 103827508Swollman } 103919395Sbde if (reset_kerninfo && tcgetattr(STDOUT_FILENO, &ts) != -1) { 10403792Ssef ts.c_lflag &= ~NOKERNINFO; 104119395Sbde tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 10423792Ssef } 10433792Ssef 10448871Srgrimes if (nreceived) 10454862Sdg exit(0); 10464862Sdg else 10474862Sdg exit(2); 10481558Srgrimes} 10491558Srgrimes 10501558Srgrimes#ifdef notdef 10511558Srgrimesstatic char *ttab[] = { 10521558Srgrimes "Echo Reply", /* ip + seq + udata */ 10531558Srgrimes "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */ 10541558Srgrimes "Source Quench", /* IP */ 10551558Srgrimes "Redirect", /* redirect type, gateway, + IP */ 10561558Srgrimes "Echo", 10571558Srgrimes "Time Exceeded", /* transit, frag reassem + IP */ 10581558Srgrimes "Parameter Problem", /* pointer + IP */ 10591558Srgrimes "Timestamp", /* id + seq + three timestamps */ 10601558Srgrimes "Timestamp Reply", /* " */ 10611558Srgrimes "Info Request", /* id + sq */ 10621558Srgrimes "Info Reply" /* " */ 10631558Srgrimes}; 10641558Srgrimes#endif 10651558Srgrimes 10661558Srgrimes/* 10671558Srgrimes * pr_icmph -- 10681558Srgrimes * Print a descriptive string about an ICMP header. 10691558Srgrimes */ 107023247Swollmanstatic void 10711558Srgrimespr_icmph(icp) 10721558Srgrimes struct icmp *icp; 10731558Srgrimes{ 10741558Srgrimes switch(icp->icmp_type) { 10751558Srgrimes case ICMP_ECHOREPLY: 10761558Srgrimes (void)printf("Echo Reply\n"); 10771558Srgrimes /* XXX ID + Seq + Data */ 10781558Srgrimes break; 10791558Srgrimes case ICMP_UNREACH: 10801558Srgrimes switch(icp->icmp_code) { 10811558Srgrimes case ICMP_UNREACH_NET: 10821558Srgrimes (void)printf("Destination Net Unreachable\n"); 10831558Srgrimes break; 10841558Srgrimes case ICMP_UNREACH_HOST: 10851558Srgrimes (void)printf("Destination Host Unreachable\n"); 10861558Srgrimes break; 10871558Srgrimes case ICMP_UNREACH_PROTOCOL: 10881558Srgrimes (void)printf("Destination Protocol Unreachable\n"); 10891558Srgrimes break; 10901558Srgrimes case ICMP_UNREACH_PORT: 10911558Srgrimes (void)printf("Destination Port Unreachable\n"); 10921558Srgrimes break; 10931558Srgrimes case ICMP_UNREACH_NEEDFRAG: 109417724Sfenner (void)printf("frag needed and DF set (MTU %d)\n", 109528059Sfenner ntohs(icp->icmp_nextmtu)); 10961558Srgrimes break; 10971558Srgrimes case ICMP_UNREACH_SRCFAIL: 10981558Srgrimes (void)printf("Source Route Failed\n"); 10991558Srgrimes break; 110017724Sfenner case ICMP_UNREACH_FILTER_PROHIB: 110117724Sfenner (void)printf("Communication prohibited by filter\n"); 110217724Sfenner break; 11031558Srgrimes default: 11041558Srgrimes (void)printf("Dest Unreachable, Bad Code: %d\n", 11051558Srgrimes icp->icmp_code); 11061558Srgrimes break; 11071558Srgrimes } 11081558Srgrimes /* Print returned IP header information */ 11091558Srgrimes#ifndef icmp_data 11101558Srgrimes pr_retip(&icp->icmp_ip); 11111558Srgrimes#else 11121558Srgrimes pr_retip((struct ip *)icp->icmp_data); 11131558Srgrimes#endif 11141558Srgrimes break; 11151558Srgrimes case ICMP_SOURCEQUENCH: 11161558Srgrimes (void)printf("Source Quench\n"); 11171558Srgrimes#ifndef icmp_data 11181558Srgrimes pr_retip(&icp->icmp_ip); 11191558Srgrimes#else 11201558Srgrimes pr_retip((struct ip *)icp->icmp_data); 11211558Srgrimes#endif 11221558Srgrimes break; 11231558Srgrimes case ICMP_REDIRECT: 11241558Srgrimes switch(icp->icmp_code) { 11251558Srgrimes case ICMP_REDIRECT_NET: 11261558Srgrimes (void)printf("Redirect Network"); 11271558Srgrimes break; 11281558Srgrimes case ICMP_REDIRECT_HOST: 11291558Srgrimes (void)printf("Redirect Host"); 11301558Srgrimes break; 11311558Srgrimes case ICMP_REDIRECT_TOSNET: 11321558Srgrimes (void)printf("Redirect Type of Service and Network"); 11331558Srgrimes break; 11341558Srgrimes case ICMP_REDIRECT_TOSHOST: 11351558Srgrimes (void)printf("Redirect Type of Service and Host"); 11361558Srgrimes break; 11371558Srgrimes default: 11381558Srgrimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 11391558Srgrimes break; 11401558Srgrimes } 114128059Sfenner (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 11421558Srgrimes#ifndef icmp_data 11431558Srgrimes pr_retip(&icp->icmp_ip); 11441558Srgrimes#else 11451558Srgrimes pr_retip((struct ip *)icp->icmp_data); 11461558Srgrimes#endif 11471558Srgrimes break; 11481558Srgrimes case ICMP_ECHO: 11491558Srgrimes (void)printf("Echo Request\n"); 11501558Srgrimes /* XXX ID + Seq + Data */ 11511558Srgrimes break; 11521558Srgrimes case ICMP_TIMXCEED: 11531558Srgrimes switch(icp->icmp_code) { 11541558Srgrimes case ICMP_TIMXCEED_INTRANS: 11551558Srgrimes (void)printf("Time to live exceeded\n"); 11561558Srgrimes break; 11571558Srgrimes case ICMP_TIMXCEED_REASS: 11581558Srgrimes (void)printf("Frag reassembly time exceeded\n"); 11591558Srgrimes break; 11601558Srgrimes default: 11611558Srgrimes (void)printf("Time exceeded, Bad Code: %d\n", 11621558Srgrimes icp->icmp_code); 11631558Srgrimes break; 11641558Srgrimes } 11651558Srgrimes#ifndef icmp_data 11661558Srgrimes pr_retip(&icp->icmp_ip); 11671558Srgrimes#else 11681558Srgrimes pr_retip((struct ip *)icp->icmp_data); 11691558Srgrimes#endif 11701558Srgrimes break; 11711558Srgrimes case ICMP_PARAMPROB: 11721558Srgrimes (void)printf("Parameter problem: pointer = 0x%02x\n", 11731558Srgrimes icp->icmp_hun.ih_pptr); 11741558Srgrimes#ifndef icmp_data 11751558Srgrimes pr_retip(&icp->icmp_ip); 11761558Srgrimes#else 11771558Srgrimes pr_retip((struct ip *)icp->icmp_data); 11781558Srgrimes#endif 11791558Srgrimes break; 11801558Srgrimes case ICMP_TSTAMP: 11811558Srgrimes (void)printf("Timestamp\n"); 11821558Srgrimes /* XXX ID + Seq + 3 timestamps */ 11831558Srgrimes break; 11841558Srgrimes case ICMP_TSTAMPREPLY: 11851558Srgrimes (void)printf("Timestamp Reply\n"); 11861558Srgrimes /* XXX ID + Seq + 3 timestamps */ 11871558Srgrimes break; 11881558Srgrimes case ICMP_IREQ: 11891558Srgrimes (void)printf("Information Request\n"); 11901558Srgrimes /* XXX ID + Seq */ 11911558Srgrimes break; 11921558Srgrimes case ICMP_IREQREPLY: 11931558Srgrimes (void)printf("Information Reply\n"); 11941558Srgrimes /* XXX ID + Seq */ 11951558Srgrimes break; 11961558Srgrimes case ICMP_MASKREQ: 11971558Srgrimes (void)printf("Address Mask Request\n"); 11981558Srgrimes break; 11991558Srgrimes case ICMP_MASKREPLY: 12001558Srgrimes (void)printf("Address Mask Reply\n"); 12011558Srgrimes break; 120217724Sfenner case ICMP_ROUTERADVERT: 120317724Sfenner (void)printf("Router Advertisement\n"); 120417724Sfenner break; 120517724Sfenner case ICMP_ROUTERSOLICIT: 120617724Sfenner (void)printf("Router Solicitation\n"); 120717724Sfenner break; 12081558Srgrimes default: 12091558Srgrimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 12101558Srgrimes } 12111558Srgrimes} 12121558Srgrimes 12131558Srgrimes/* 12141558Srgrimes * pr_iph -- 12151558Srgrimes * Print an IP header with options. 12161558Srgrimes */ 121723247Swollmanstatic void 12181558Srgrimespr_iph(ip) 12191558Srgrimes struct ip *ip; 12201558Srgrimes{ 12211558Srgrimes int hlen; 12221558Srgrimes u_char *cp; 12231558Srgrimes 12241558Srgrimes hlen = ip->ip_hl << 2; 12251558Srgrimes cp = (u_char *)ip + 20; /* point to options */ 12261558Srgrimes 122717724Sfenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 12281558Srgrimes (void)printf(" %1x %1x %02x %04x %04x", 122917724Sfenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 123017724Sfenner ntohs(ip->ip_id)); 123136089Sjb (void)printf(" %1lx %04lx", 123236089Sjb (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13, 123336089Sjb (u_long) ntohl(ip->ip_off) & 0x1fff); 123417724Sfenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 123517724Sfenner ntohs(ip->ip_sum)); 12361558Srgrimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr)); 12371558Srgrimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr)); 123817724Sfenner /* dump any option bytes */ 12391558Srgrimes while (hlen-- > 20) { 12401558Srgrimes (void)printf("%02x", *cp++); 12411558Srgrimes } 12421558Srgrimes (void)putchar('\n'); 12431558Srgrimes} 12441558Srgrimes 12451558Srgrimes/* 12461558Srgrimes * pr_addr -- 12471558Srgrimes * Return an ascii host address as a dotted quad and optionally with 12481558Srgrimes * a hostname. 12491558Srgrimes */ 125023247Swollmanstatic char * 125123247Swollmanpr_addr(ina) 125223247Swollman struct in_addr ina; 12531558Srgrimes{ 12541558Srgrimes struct hostent *hp; 125523251Simp static char buf[16 + 3 + MAXHOSTNAMELEN]; 12561558Srgrimes 12571558Srgrimes if ((options & F_NUMERIC) || 125823247Swollman !(hp = gethostbyaddr((char *)&ina, 4, AF_INET))) 125923247Swollman return inet_ntoa(ina); 12601558Srgrimes else 126117320Speter (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 126223247Swollman inet_ntoa(ina)); 12631558Srgrimes return(buf); 12641558Srgrimes} 12651558Srgrimes 12661558Srgrimes/* 12671558Srgrimes * pr_retip -- 12681558Srgrimes * Dump some info on a returned (via ICMP) IP packet. 12691558Srgrimes */ 127023247Swollmanstatic void 12711558Srgrimespr_retip(ip) 12721558Srgrimes struct ip *ip; 12731558Srgrimes{ 12741558Srgrimes int hlen; 12751558Srgrimes u_char *cp; 12761558Srgrimes 12771558Srgrimes pr_iph(ip); 12781558Srgrimes hlen = ip->ip_hl << 2; 12791558Srgrimes cp = (u_char *)ip + hlen; 12801558Srgrimes 12811558Srgrimes if (ip->ip_p == 6) 12821558Srgrimes (void)printf("TCP: from port %u, to port %u (decimal)\n", 12831558Srgrimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 12841558Srgrimes else if (ip->ip_p == 17) 12851558Srgrimes (void)printf("UDP: from port %u, to port %u (decimal)\n", 12861558Srgrimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 12871558Srgrimes} 12881558Srgrimes 128923247Swollmanstatic void 12901558Srgrimesfill(bp, patp) 12911558Srgrimes char *bp, *patp; 12921558Srgrimes{ 12931558Srgrimes register int ii, jj, kk; 12941558Srgrimes int pat[16]; 12951558Srgrimes char *cp; 12961558Srgrimes 129723247Swollman for (cp = patp; *cp; cp++) { 129823247Swollman if (!isxdigit(*cp)) 129923247Swollman errx(EX_USAGE, 130023247Swollman "patterns must be specified as hex digits"); 130123247Swollman 130223247Swollman } 13031558Srgrimes ii = sscanf(patp, 13041558Srgrimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 13051558Srgrimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 13061558Srgrimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 13071558Srgrimes &pat[13], &pat[14], &pat[15]); 13081558Srgrimes 13091558Srgrimes if (ii > 0) 13101558Srgrimes for (kk = 0; 131136089Sjb kk <= MAXPACKET - (8 + PHDR_LEN + ii); 13121558Srgrimes kk += ii) 13131558Srgrimes for (jj = 0; jj < ii; ++jj) 13141558Srgrimes bp[jj + kk] = pat[jj]; 13151558Srgrimes if (!(options & F_QUIET)) { 13161558Srgrimes (void)printf("PATTERN: 0x"); 13171558Srgrimes for (jj = 0; jj < ii; ++jj) 13181558Srgrimes (void)printf("%02x", bp[jj] & 0xFF); 13191558Srgrimes (void)printf("\n"); 13201558Srgrimes } 13211558Srgrimes} 13221558Srgrimes 132323247Swollmanstatic void 132437671Scharnierusage() 13251558Srgrimes{ 132642337Simp fprintf(stderr, "%s\n%s\n%s\n", 132737671Scharnier"usage: ping [-QRadfnqrv] [-c count] [-i wait] [-l preload] [-p pattern]", 132842337Simp" [-s packetsize] [-S src_addr]", 132942337Simp" [host | [-L] [-I iface] [-T ttl] mcast-group]"); 133023247Swollman exit(EX_USAGE); 13311558Srgrimes} 1332