1997-12-01 11:41:48 +03:00
|
|
|
/* $NetBSD: ping.c,v 1.35 1997/12/01 08:41:48 christos Exp $ */
|
1995-03-18 17:54:19 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-09-23 05:38:45 +04:00
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Mike Muuss.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* P I N G . C
|
|
|
|
*
|
1997-06-01 23:34:46 +04:00
|
|
|
* Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
|
1993-03-21 12:45:37 +03:00
|
|
|
* measure round-trip-delays and packet loss across network paths.
|
|
|
|
*
|
|
|
|
* Author -
|
|
|
|
* Mike Muuss
|
|
|
|
* U. S. Army Ballistic Research Laboratory
|
|
|
|
* December, 1983
|
1997-03-12 00:22:49 +03:00
|
|
|
* Modified at Uc Berkeley
|
|
|
|
* Record Route and verbose headers - Phil Dykstra, BRL, March 1988.
|
|
|
|
* Multicast options (ttl, if, loop) - Steve Deering, Stanford, August 1988.
|
|
|
|
* ttl, duplicate detection - Cliff Frost, UCB, April 1989
|
|
|
|
* Pad pattern - Cliff Frost (from Tom Ferrin, UCSF), April 1989
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Status -
|
|
|
|
* Public Domain. Distribution Unlimited.
|
1997-03-12 00:22:49 +03:00
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Bugs -
|
|
|
|
* More statistics could always be gathered.
|
|
|
|
* This program has to run SUID to ROOT to access the ICMP socket.
|
|
|
|
*/
|
|
|
|
|
1997-09-15 11:21:09 +04:00
|
|
|
#include <sys/cdefs.h>
|
1997-03-12 00:22:49 +03:00
|
|
|
#ifndef lint
|
1997-12-01 11:41:48 +03:00
|
|
|
__RCSID("$NetBSD: ping.c,v 1.35 1997/12/01 08:41:48 christos Exp $");
|
1997-03-12 00:22:49 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/signal.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/file.h>
|
1997-03-12 00:22:49 +03:00
|
|
|
#include <termios.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <err.h>
|
|
|
|
#ifdef sgi
|
|
|
|
#include <bstring.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#include <sys/schedctl.h>
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_icmp.h>
|
|
|
|
#include <netinet/ip_var.h>
|
1994-12-18 03:15:01 +03:00
|
|
|
#include <arpa/inet.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <ctype.h>
|
1997-03-12 00:22:49 +03:00
|
|
|
#include <netdb.h>
|
1993-12-15 07:46:13 +03:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
#define FLOOD_INTVL 0.01 /* default flood output interval */
|
|
|
|
#define MAXPACKET (65536-60-8) /* max packet size */
|
|
|
|
|
|
|
|
#define F_VERBOSE 0x0001
|
|
|
|
#define F_QUIET 0x0002 /* minimize all output */
|
|
|
|
#define F_SEMI_QUIET 0x0004 /* ignore our ICMP errors */
|
|
|
|
#define F_FLOOD 0x0008 /* flood-ping */
|
|
|
|
#define F_RECORD_ROUTE 0x0010 /* record route */
|
|
|
|
#define F_SOURCE_ROUTE 0x0020 /* loose source route */
|
|
|
|
#define F_PING_FILLED 0x0040 /* is buffer filled with user data? */
|
|
|
|
#define F_PING_RANDOM 0x0080 /* use random data */
|
|
|
|
#define F_NUMERIC 0x0100 /* do not do gethostbyaddr() calls */
|
|
|
|
#define F_TIMING 0x0200 /* room for a timestamp */
|
1997-06-01 23:34:46 +04:00
|
|
|
#define F_DF 0x0400 /* set IP DF bit */
|
|
|
|
#define F_SOURCE_ADDR 0x0800 /* set source IP address/interface */
|
|
|
|
#define F_ONCE 0x1000 /* exit(0) after receiving 1 reply */
|
|
|
|
#define F_MCAST 0x2000 /* multicast target */
|
|
|
|
#define F_MCAST_NOLOOP 0x4000 /* no multicast loopback */
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
/* MAX_DUP_CHK is the number of bits in received table, the
|
|
|
|
* maximum number of received sequence numbers we can track to check
|
|
|
|
* for duplicates.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1997-03-12 00:22:49 +03:00
|
|
|
#define MAX_DUP_CHK (8 * 2048)
|
|
|
|
u_char rcvd_tbl[MAX_DUP_CHK/8];
|
|
|
|
int nrepeats = 0;
|
|
|
|
#define A(seq) rcvd_tbl[(seq/8)%sizeof(rcvd_tbl)] /* byte in array */
|
|
|
|
#define B(seq) (1 << (seq & 0x07)) /* bit in byte */
|
|
|
|
#define SET(seq) (A(seq) |= B(seq))
|
|
|
|
#define CLR(seq) (A(seq) &= (~B(seq)))
|
|
|
|
#define TST(seq) (A(seq) & B(seq))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
u_char *packet;
|
|
|
|
int packlen;
|
1997-06-01 23:34:46 +04:00
|
|
|
int pingflags = 0, options;
|
1997-03-12 00:22:49 +03:00
|
|
|
char *fill_pat;
|
|
|
|
|
|
|
|
int s; /* Socket file descriptor */
|
|
|
|
|
|
|
|
#define PHDR_LEN sizeof(struct timeval) /* size of timestamp header */
|
|
|
|
struct sockaddr_in whereto, send_addr; /* Who to ping */
|
1997-06-01 23:34:46 +04:00
|
|
|
struct sockaddr_in src_addr; /* from where */
|
1997-03-12 00:22:49 +03:00
|
|
|
struct sockaddr_in loc_addr; /* 127.1 */
|
|
|
|
int datalen = 64-PHDR_LEN; /* How much data */
|
|
|
|
|
1997-06-01 23:34:46 +04:00
|
|
|
#ifdef sgi
|
|
|
|
static char *__progname;
|
|
|
|
#else
|
1997-03-12 00:22:49 +03:00
|
|
|
extern char *__progname;
|
1997-06-01 23:34:46 +04:00
|
|
|
#endif
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
char hostname[MAXHOSTNAMELEN];
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
struct ip o_ip;
|
1997-06-01 23:34:46 +04:00
|
|
|
char o_opt[MAX_IPOPTLEN];
|
1997-03-12 00:22:49 +03:00
|
|
|
union {
|
1997-06-01 23:34:46 +04:00
|
|
|
u_char u_buf[MAXPACKET];
|
|
|
|
struct icmp u_icmp;
|
1997-03-12 00:22:49 +03:00
|
|
|
} o_u;
|
|
|
|
} out_pack;
|
1997-06-01 23:34:46 +04:00
|
|
|
#define opack_icmp out_pack.o_u.u_icmp
|
|
|
|
struct ip *opack_ip;
|
|
|
|
|
|
|
|
char optspace[MAX_IPOPTLEN]; /* record route space */
|
|
|
|
int optlen;
|
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
int npackets; /* total packets to send */
|
|
|
|
int preload; /* number of packets to "preload" */
|
|
|
|
int ntransmitted; /* output sequence # = #sent */
|
|
|
|
int ident;
|
|
|
|
|
|
|
|
int nreceived; /* # of packets we got back */
|
|
|
|
|
|
|
|
double interval; /* interval between packets */
|
|
|
|
struct timeval interval_tv;
|
|
|
|
double tmin = 999999999;
|
|
|
|
double tmax = 0;
|
|
|
|
double tsum = 0; /* sum of all times */
|
|
|
|
double maxwait = 0;
|
|
|
|
|
|
|
|
#ifdef SIGINFO
|
|
|
|
int reset_kerninfo;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int bufspace = 60*1024;
|
|
|
|
|
|
|
|
struct timeval now, clear_cache, last_tx, next_tx, first_tx;
|
|
|
|
struct timeval last_rx, first_rx;
|
|
|
|
int lastrcvd = 1; /* last ping sent has been received */
|
|
|
|
|
|
|
|
static struct timeval jiggle_time;
|
|
|
|
static int jiggle_cnt, total_jiggled, jiggle_direction = -1;
|
|
|
|
|
|
|
|
static void doit(void);
|
|
|
|
static void prefinish(int);
|
|
|
|
static void prtsig(int);
|
|
|
|
static void finish(int);
|
|
|
|
static void summary(int);
|
|
|
|
static void pinger(void);
|
|
|
|
static void fill(void);
|
|
|
|
static void rnd_fill(void);
|
|
|
|
static double diffsec(struct timeval *, struct timeval *);
|
|
|
|
static void timevaladd(struct timeval *, struct timeval *);
|
|
|
|
static void sec_to_timeval(const double, struct timeval *);
|
|
|
|
static double timeval_to_sec(const struct timeval *);
|
|
|
|
static void pr_pack(u_char *, int, struct sockaddr_in *);
|
|
|
|
static u_short in_cksum(u_short *, u_int);
|
|
|
|
static void pr_saddr(char *, u_char *);
|
|
|
|
static char *pr_addr(struct in_addr *);
|
|
|
|
static void pr_iph(struct icmp *, int);
|
|
|
|
static void pr_retip(struct icmp *, int);
|
|
|
|
static int pr_icmph(struct icmp *, struct sockaddr_in *, int);
|
|
|
|
static void jiggle(int), jiggle_flush(int);
|
1997-06-01 23:34:46 +04:00
|
|
|
static void gethost(const char *, const char *,
|
|
|
|
struct sockaddr_in *, char *, int);
|
1997-03-12 00:22:49 +03:00
|
|
|
static void usage(void);
|
|
|
|
|
1994-12-18 03:15:01 +03:00
|
|
|
|
|
|
|
int
|
1997-03-12 00:22:49 +03:00
|
|
|
main(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-06-01 23:34:46 +04:00
|
|
|
int c, i, on = 1, hostind = 0;
|
|
|
|
long l;
|
|
|
|
u_char ttl = 0;
|
|
|
|
u_long tos = 0;
|
1997-03-12 00:22:49 +03:00
|
|
|
char *p;
|
|
|
|
#ifdef SIGINFO
|
|
|
|
struct termios ts;
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1997-06-01 23:34:46 +04:00
|
|
|
#if defined(SIGINFO) && defined(NOKERNINFO)
|
1997-03-12 00:22:49 +03:00
|
|
|
if (tcgetattr (0, &ts) != -1) {
|
|
|
|
reset_kerninfo = !(ts.c_lflag & NOKERNINFO);
|
|
|
|
ts.c_lflag |= NOKERNINFO;
|
1997-11-06 00:29:29 +03:00
|
|
|
(void)tcsetattr(0, TCSANOW, &ts);
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
|
|
|
#endif
|
1997-06-01 23:34:46 +04:00
|
|
|
while ((c = getopt(argc, argv,
|
|
|
|
"c:dDfg:h:i:I:l:Lnop:PqQrRs:t:T:vw:")) != -1) {
|
1997-03-12 00:22:49 +03:00
|
|
|
switch (c) {
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'c':
|
1997-03-12 00:22:49 +03:00
|
|
|
npackets = strtol(optarg, &p, 0);
|
|
|
|
if (*p != '\0' || npackets <= 0)
|
|
|
|
errx(1, "Bad/invalid number of packets");
|
|
|
|
break;
|
|
|
|
case 'D':
|
1997-06-01 23:34:46 +04:00
|
|
|
pingflags |= F_DF;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case 'd':
|
1997-03-12 00:22:49 +03:00
|
|
|
options |= SO_DEBUG;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case 'f':
|
1997-03-12 00:22:49 +03:00
|
|
|
pingflags |= F_FLOOD;
|
1994-09-23 05:38:45 +04:00
|
|
|
break;
|
1997-06-01 23:34:46 +04:00
|
|
|
case 'h':
|
|
|
|
hostind = optind-1;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'i': /* wait between sending packets */
|
1997-03-12 00:22:49 +03:00
|
|
|
interval = strtod(optarg, &p);
|
|
|
|
if (*p != '\0' || interval <= 0)
|
1997-06-01 23:34:46 +04:00
|
|
|
errx(1, "Bad/invalid interval %s", optarg);
|
1994-09-23 05:38:45 +04:00
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'l':
|
1997-03-12 00:22:49 +03:00
|
|
|
preload = strtol(optarg, &p, 0);
|
|
|
|
if (*p != '\0' || preload < 0)
|
1997-06-01 23:34:46 +04:00
|
|
|
errx(1, "Bad/invalid preload value %s",
|
|
|
|
optarg);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case 'n':
|
1997-03-12 00:22:49 +03:00
|
|
|
pingflags |= F_NUMERIC;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1997-03-18 22:06:51 +03:00
|
|
|
case 'o':
|
|
|
|
pingflags |= F_ONCE;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'p': /* fill buffer with user pattern */
|
1997-03-12 00:22:49 +03:00
|
|
|
if (pingflags & F_PING_RANDOM)
|
|
|
|
errx(1, "Only one of -P and -p allowed");
|
|
|
|
pingflags |= F_PING_FILLED;
|
|
|
|
fill_pat = optarg;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
if (pingflags & F_PING_FILLED)
|
|
|
|
errx(1, "Only one of -P and -p allowed");
|
|
|
|
pingflags |= F_PING_RANDOM;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'q':
|
1997-03-12 00:22:49 +03:00
|
|
|
pingflags |= F_QUIET;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1997-03-12 00:22:49 +03:00
|
|
|
case 'Q':
|
|
|
|
pingflags |= F_SEMI_QUIET;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case 'r':
|
1997-03-12 00:22:49 +03:00
|
|
|
options |= SO_DONTROUTE;
|
1995-07-28 03:49:07 +04:00
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 's': /* size of packet to send */
|
1997-03-12 00:22:49 +03:00
|
|
|
datalen = strtol(optarg, &p, 0);
|
|
|
|
if (*p != '\0' || datalen <= 0)
|
1997-06-01 23:34:46 +04:00
|
|
|
errx(1, "Bad/invalid packet size %s", optarg);
|
1994-09-23 05:38:45 +04:00
|
|
|
if (datalen > MAXPACKET)
|
1997-03-12 00:22:49 +03:00
|
|
|
errx(1, "packet size is too large");
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
pingflags |= F_VERBOSE;
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
pingflags |= F_RECORD_ROUTE;
|
|
|
|
break;
|
|
|
|
case 'L':
|
1997-06-01 23:34:46 +04:00
|
|
|
pingflags |= F_MCAST_NOLOOP;
|
1993-12-15 07:46:13 +03:00
|
|
|
break;
|
|
|
|
case 't':
|
1997-06-01 23:34:46 +04:00
|
|
|
tos = strtoul(optarg, &p, 0);
|
|
|
|
if (*p != '\0' || tos > 0xFF)
|
|
|
|
errx(1, "bad tos value: %s", optarg);
|
1993-12-15 07:46:13 +03:00
|
|
|
break;
|
1997-03-12 00:22:49 +03:00
|
|
|
case 'T':
|
1997-06-01 23:34:46 +04:00
|
|
|
l = strtol(optarg, &p, 0);
|
|
|
|
if (*p != '\0' || l > 255 || l <= 0)
|
|
|
|
errx(1, "ttl out of range");
|
|
|
|
ttl = (u_char)l; /* cannot check >255 otherwise */
|
1997-03-12 00:22:49 +03:00
|
|
|
break;
|
|
|
|
case 'I':
|
1997-06-01 23:34:46 +04:00
|
|
|
pingflags |= F_SOURCE_ADDR;
|
|
|
|
gethost("-I", optarg, &src_addr, 0, 0);
|
1997-03-12 00:22:49 +03:00
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
pingflags |= F_SOURCE_ROUTE;
|
1997-06-01 23:34:46 +04:00
|
|
|
gethost("-g", optarg, &send_addr, 0, 0);
|
1993-12-15 07:46:13 +03:00
|
|
|
break;
|
1995-07-28 03:49:07 +04:00
|
|
|
case 'w':
|
1997-03-12 00:22:49 +03:00
|
|
|
maxwait = strtod(optarg, &p);
|
|
|
|
if (*p != '\0' || maxwait <= 0)
|
1997-06-01 23:34:46 +04:00
|
|
|
errx(1, "Bad/invalid maxwait time %s", optarg);
|
1995-07-28 03:49:07 +04:00
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
default:
|
|
|
|
usage();
|
1997-03-12 00:22:49 +03:00
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
1994-09-23 05:38:45 +04:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
if (interval == 0)
|
|
|
|
interval = (pingflags & F_FLOOD) ? FLOOD_INTVL : 1.0;
|
1997-03-24 06:34:26 +03:00
|
|
|
#ifndef sgi
|
|
|
|
if (interval < 1.0 && getuid())
|
|
|
|
errx(1, "Must be superuser to use < 1 sec ping interval");
|
|
|
|
#endif
|
1997-03-12 00:22:49 +03:00
|
|
|
sec_to_timeval(interval, &interval_tv);
|
|
|
|
|
|
|
|
if (npackets != 0) {
|
|
|
|
npackets += preload;
|
|
|
|
} else {
|
|
|
|
npackets = INT_MAX;
|
|
|
|
}
|
|
|
|
|
1997-06-01 23:34:46 +04:00
|
|
|
if (hostind == 0) {
|
|
|
|
if (optind != argc-1)
|
|
|
|
usage();
|
1997-03-12 00:22:49 +03:00
|
|
|
else
|
1997-06-01 23:34:46 +04:00
|
|
|
hostind = optind;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-06-01 23:34:46 +04:00
|
|
|
else if (hostind >= argc - 1)
|
|
|
|
usage();
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-06-01 23:34:46 +04:00
|
|
|
gethost("", argv[hostind], &whereto, hostname, sizeof(hostname));
|
|
|
|
if (IN_MULTICAST(ntohl(whereto.sin_addr.s_addr)))
|
|
|
|
pingflags |= F_MCAST;
|
1997-03-12 00:22:49 +03:00
|
|
|
if (!(pingflags & F_SOURCE_ROUTE))
|
|
|
|
(void) memcpy(&send_addr, &whereto, sizeof(send_addr));
|
|
|
|
|
|
|
|
loc_addr.sin_family = AF_INET;
|
|
|
|
loc_addr.sin_addr.s_addr = htonl((127<<24)+1);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
if (datalen >= PHDR_LEN) /* can we time them? */
|
|
|
|
pingflags |= F_TIMING;
|
|
|
|
packlen = datalen + 60 + 76; /* MAXIP + MAXICMP */
|
|
|
|
if ((packet = (u_char *)malloc(packlen)) == NULL)
|
|
|
|
err(1, "Out of memory");
|
|
|
|
|
|
|
|
if (pingflags & F_PING_FILLED) {
|
|
|
|
fill();
|
|
|
|
} else if (pingflags & F_PING_RANDOM) {
|
|
|
|
rnd_fill();
|
|
|
|
} else {
|
|
|
|
for (i = PHDR_LEN; i < datalen; i++)
|
|
|
|
opack_icmp.icmp_data[i] = i;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
ident = getpid() & 0xFFFF;
|
|
|
|
|
1997-06-01 23:34:46 +04:00
|
|
|
if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
|
1997-03-12 00:22:49 +03:00
|
|
|
err(1, "Cannot create socket");
|
|
|
|
if (options & SO_DEBUG) {
|
1997-03-12 00:53:41 +03:00
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *) &on,
|
|
|
|
sizeof(on)) == -1)
|
1997-06-01 23:34:46 +04:00
|
|
|
warn("Can't turn on socket debugging");
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
|
|
|
if (options & SO_DONTROUTE) {
|
1997-06-01 23:34:46 +04:00
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *) &on,
|
1997-03-12 00:22:49 +03:00
|
|
|
sizeof(on)) == -1)
|
1997-06-01 23:34:46 +04:00
|
|
|
warn("SO_DONTROUTE");
|
1995-07-28 03:49:07 +04:00
|
|
|
}
|
|
|
|
|
1997-06-01 23:34:46 +04:00
|
|
|
if (pingflags & F_SOURCE_ROUTE) {
|
|
|
|
optspace[IPOPT_OPTVAL] = IPOPT_LSRR;
|
|
|
|
optspace[IPOPT_OLEN] = optlen = 7;
|
|
|
|
optspace[IPOPT_OFFSET] = IPOPT_MINOFF;
|
|
|
|
(void) memcpy(&whereto.sin_addr, &optspace[IPOPT_MINOFF-1],
|
|
|
|
sizeof(whereto.sin_addr));
|
|
|
|
optspace[optlen++] = IPOPT_NOP;
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
1997-06-01 23:34:46 +04:00
|
|
|
if (pingflags & F_RECORD_ROUTE) {
|
|
|
|
optspace[optlen+IPOPT_OPTVAL] = IPOPT_RR;
|
|
|
|
optspace[optlen+IPOPT_OLEN] = (MAX_IPOPTLEN -1-optlen);
|
|
|
|
optspace[optlen+IPOPT_OFFSET] = IPOPT_MINOFF;
|
|
|
|
optlen = MAX_IPOPTLEN;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-06-01 23:34:46 +04:00
|
|
|
/* this leaves opack_ip 0(mod 4) aligned */
|
|
|
|
opack_ip = (struct ip *)((char *)&out_pack.o_ip
|
|
|
|
+ sizeof(out_pack.o_opt)
|
|
|
|
- optlen);
|
|
|
|
(void) memcpy(opack_ip + 1, optspace, optlen);
|
|
|
|
|
|
|
|
if (setsockopt(s,IPPROTO_IP,IP_HDRINCL, (char *) &on, sizeof(on)) < 0)
|
|
|
|
err(1, "Can't set special IP header");
|
|
|
|
|
|
|
|
opack_ip->ip_v = IPVERSION;
|
|
|
|
opack_ip->ip_hl = (sizeof(struct ip)+optlen) >> 2;
|
|
|
|
opack_ip->ip_tos = tos;
|
|
|
|
opack_ip->ip_off = (pingflags & F_DF) ? IP_DF : 0;
|
|
|
|
opack_ip->ip_ttl = ttl ? ttl : MAXTTL;
|
|
|
|
opack_ip->ip_p = IPPROTO_ICMP;
|
|
|
|
opack_ip->ip_src = src_addr.sin_addr;
|
|
|
|
opack_ip->ip_dst = send_addr.sin_addr;
|
|
|
|
|
|
|
|
if (pingflags & F_MCAST) {
|
|
|
|
if (pingflags & F_MCAST_NOLOOP) {
|
|
|
|
u_char loop = 0;
|
|
|
|
if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP,
|
|
|
|
(char *) &loop, 1) < 0)
|
|
|
|
err(1, "Can't disable multicast loopback");
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-06-01 23:34:46 +04:00
|
|
|
if (ttl != 0
|
|
|
|
&& setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
|
|
|
|
(char *) &ttl, 1) < 0)
|
1997-03-12 00:22:49 +03:00
|
|
|
err(1, "Can't set multicast time-to-live");
|
1997-06-01 23:34:46 +04:00
|
|
|
|
|
|
|
if ((pingflags & F_SOURCE_ADDR)
|
|
|
|
&& setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
|
|
|
|
(char *) &src_addr.sin_addr,
|
|
|
|
sizeof(src_addr.sin_addr)) < 0)
|
1997-03-12 00:22:49 +03:00
|
|
|
err(1, "Can't set multicast source interface");
|
1997-06-01 23:34:46 +04:00
|
|
|
|
|
|
|
} else if (pingflags & F_SOURCE_ADDR) {
|
|
|
|
if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
|
|
|
|
(char *) &src_addr.sin_addr,
|
|
|
|
sizeof(src_addr.sin_addr)) < 0)
|
|
|
|
err(1, "Can't set source interface/address");
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
1994-09-23 05:38:45 +04:00
|
|
|
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf("PING %s (%s): %d data bytes\n", hostname,
|
|
|
|
inet_ntoa(whereto.sin_addr), datalen);
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
/* When pinging the broadcast address, you can get a lot
|
|
|
|
* of answers. Doing something so evil is useful if you
|
|
|
|
* are trying to stress the ethernet, or just want to
|
|
|
|
* fill the arp cache to get some stuff for /etc/ethers.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1997-06-01 23:34:46 +04:00
|
|
|
while (0 > setsockopt(s, SOL_SOCKET, SO_RCVBUF,
|
1997-03-12 00:22:49 +03:00
|
|
|
(char*)&bufspace, sizeof(bufspace))) {
|
|
|
|
if ((bufspace -= 4096) == 0)
|
|
|
|
err(1, "Cannot set the receive buffer size");
|
|
|
|
}
|
|
|
|
|
1997-06-01 23:34:46 +04:00
|
|
|
/* make it possible to send giant probes, but do not worry now
|
|
|
|
* if it fails, since we probably won't send giant probes.
|
|
|
|
*/
|
|
|
|
(void)setsockopt(s, SOL_SOCKET, SO_SNDBUF,
|
|
|
|
(char*)&bufspace, sizeof(bufspace));
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
(void)signal(SIGINT, prefinish);
|
|
|
|
#ifdef SIGINFO
|
|
|
|
(void)signal(SIGINFO, prtsig);
|
|
|
|
#else
|
|
|
|
(void)signal(SIGQUIT, prtsig);
|
|
|
|
#endif
|
|
|
|
(void)signal(SIGCONT, prtsig);
|
|
|
|
|
|
|
|
#ifdef sgi
|
|
|
|
/* run with a non-degrading priority to improve the delay values. */
|
1997-06-01 23:34:46 +04:00
|
|
|
(void) cap_schedctl(NDPRI, 0, NDPHIMAX);
|
1997-03-12 00:22:49 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* fire off them quickies */
|
|
|
|
for (i = 0; i < preload; i++) {
|
|
|
|
(void)gettimeofday(&now, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
pinger();
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
doit();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
static void
|
|
|
|
doit(void)
|
|
|
|
{
|
|
|
|
int cc;
|
|
|
|
struct sockaddr_in from;
|
|
|
|
int fromlen;
|
1997-06-01 23:34:46 +04:00
|
|
|
double sec, last, d_last;
|
1997-03-12 00:22:49 +03:00
|
|
|
struct timeval timeout;
|
|
|
|
fd_set fdmask;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)gettimeofday(&clear_cache,0);
|
|
|
|
if (maxwait != 0) {
|
|
|
|
last = timeval_to_sec(&clear_cache) + maxwait;
|
|
|
|
d_last = 0;
|
|
|
|
} else {
|
|
|
|
last = 0;
|
|
|
|
d_last = 365*24*60*60;
|
|
|
|
}
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
FD_ZERO(&fdmask);
|
1997-06-01 23:34:46 +04:00
|
|
|
do {
|
|
|
|
(void)gettimeofday(&now,0);
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1997-06-01 23:34:46 +04:00
|
|
|
if (last != 0)
|
|
|
|
d_last = last - timeval_to_sec(&now);
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1997-06-01 23:34:46 +04:00
|
|
|
if (ntransmitted < npackets && d_last > 0) {
|
1997-03-12 00:22:49 +03:00
|
|
|
/* send if within 100 usec or late for next packet */
|
1997-06-01 23:34:46 +04:00
|
|
|
sec = diffsec(&next_tx,&now);
|
1997-03-12 00:22:49 +03:00
|
|
|
if (sec <= 0.0001
|
|
|
|
|| (lastrcvd && (pingflags & F_FLOOD))) {
|
|
|
|
pinger();
|
1997-06-01 23:34:46 +04:00
|
|
|
sec = diffsec(&next_tx,&now);
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
|
|
|
if (sec < 0.0)
|
|
|
|
sec = 0.0;
|
1997-06-01 23:34:46 +04:00
|
|
|
if (d_last < sec)
|
|
|
|
sec = d_last;
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
} else {
|
|
|
|
/* For the last response, wait twice as long as the
|
|
|
|
* worst case seen, or 10 times as long as the
|
|
|
|
* maximum interpacket interval, whichever is longer.
|
|
|
|
*/
|
1997-06-01 23:34:46 +04:00
|
|
|
sec = MAX(2*tmax,10*interval) - diffsec(&now,&last_tx);
|
|
|
|
if (d_last < sec)
|
|
|
|
sec = d_last;
|
1997-03-12 00:22:49 +03:00
|
|
|
if (sec <= 0)
|
1997-06-01 23:34:46 +04:00
|
|
|
break;
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sec_to_timeval(sec, &timeout);
|
|
|
|
|
|
|
|
FD_SET(s, &fdmask);
|
|
|
|
cc = select(s+1, &fdmask, 0, 0, &timeout);
|
|
|
|
if (cc <= 0) {
|
|
|
|
if (cc < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
jiggle_flush(1);
|
1997-06-01 23:34:46 +04:00
|
|
|
err(1, "select");
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
fromlen = sizeof(from);
|
1997-06-01 23:34:46 +04:00
|
|
|
cc = recvfrom(s, (char *) packet, packlen,
|
|
|
|
0, (struct sockaddr *)&from,
|
1997-03-12 00:22:49 +03:00
|
|
|
&fromlen);
|
|
|
|
if (cc < 0) {
|
|
|
|
if (errno != EINTR) {
|
|
|
|
jiggle_flush(1);
|
1997-06-01 23:34:46 +04:00
|
|
|
warn("recvfrom");
|
1997-03-12 00:22:49 +03:00
|
|
|
(void)fflush(stderr);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(void)gettimeofday(&now, 0);
|
|
|
|
pr_pack(packet, cc, &from);
|
1997-06-01 23:34:46 +04:00
|
|
|
|
|
|
|
} while (nreceived < npackets
|
|
|
|
&& (nreceived == 0 || !(pingflags & F_ONCE)));
|
|
|
|
|
|
|
|
finish(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
static void
|
|
|
|
jiggle_flush(int nl) /* new line if there are dots */
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-03-12 00:22:49 +03:00
|
|
|
int serrno = errno;
|
|
|
|
|
|
|
|
if (jiggle_cnt > 0) {
|
|
|
|
total_jiggled += jiggle_cnt;
|
|
|
|
jiggle_direction = 1;
|
|
|
|
do {
|
|
|
|
(void)putchar('.');
|
|
|
|
} while (--jiggle_cnt > 0);
|
|
|
|
|
|
|
|
} else if (jiggle_cnt < 0) {
|
|
|
|
total_jiggled -= jiggle_cnt;
|
|
|
|
jiggle_direction = -1;
|
|
|
|
do {
|
|
|
|
(void)putchar('\b');
|
|
|
|
} while (++jiggle_cnt < 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nl) {
|
|
|
|
if (total_jiggled != 0)
|
|
|
|
(void)putchar('\n');
|
|
|
|
total_jiggled = 0;
|
|
|
|
jiggle_direction = -1;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
(void)fflush(stdout);
|
|
|
|
(void)fflush(stderr);
|
|
|
|
jiggle_time = now;
|
|
|
|
errno = serrno;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* jiggle the cursor for flood-ping
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
jiggle(int delta)
|
|
|
|
{
|
|
|
|
double dt;
|
|
|
|
|
|
|
|
if (pingflags & F_QUIET)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* do not back up into messages */
|
|
|
|
if (total_jiggled+jiggle_cnt+delta < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
jiggle_cnt += delta;
|
|
|
|
|
|
|
|
/* flush the FLOOD dots when things are quiet
|
|
|
|
* or occassionally to make the cursor jiggle.
|
|
|
|
*/
|
|
|
|
dt = diffsec(&last_tx, &jiggle_time);
|
|
|
|
if (dt > 0.2 || (dt >= 0.15 && delta*jiggle_direction < 0))
|
|
|
|
jiggle_flush(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1997-03-12 00:22:49 +03:00
|
|
|
* Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
|
1997-06-01 23:34:46 +04:00
|
|
|
* will be added on by the kernel. The ID field is our UNIX process ID,
|
1997-03-12 00:22:49 +03:00
|
|
|
* and the sequence number is an ascending integer. The first PHDR_LEN bytes
|
1993-03-21 12:45:37 +03:00
|
|
|
* of the data portion are used to hold a UNIX "timeval" struct in VAX
|
|
|
|
* byte-order, to compute the round-trip time.
|
|
|
|
*/
|
1997-03-12 00:22:49 +03:00
|
|
|
static void
|
|
|
|
pinger(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-06-01 23:34:46 +04:00
|
|
|
int i, cc, sw;
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
opack_icmp.icmp_code = 0;
|
|
|
|
opack_icmp.icmp_seq = htons((u_short)(ntransmitted));
|
1997-06-01 23:34:46 +04:00
|
|
|
|
|
|
|
/* clear the cached route in the kernel after an ICMP
|
|
|
|
* response such as a Redirect is seen to stop causing
|
|
|
|
* more such packets. Also clear the cached route
|
|
|
|
* periodically in case of routing changes that make
|
|
|
|
* black holes come and go.
|
|
|
|
*/
|
1997-03-12 00:22:49 +03:00
|
|
|
if (clear_cache.tv_sec != now.tv_sec) {
|
|
|
|
opack_icmp.icmp_type = ICMP_ECHOREPLY;
|
|
|
|
opack_icmp.icmp_id = ~ident;
|
|
|
|
opack_icmp.icmp_cksum = 0;
|
|
|
|
opack_icmp.icmp_cksum = in_cksum((u_short*)&opack_icmp,
|
1997-06-01 23:34:46 +04:00
|
|
|
PHDR_LEN);
|
|
|
|
sw = 0;
|
|
|
|
if (setsockopt(s,IPPROTO_IP,IP_HDRINCL,
|
|
|
|
(char *)&sw,sizeof(sw)) < 0)
|
|
|
|
err(1, "Can't turn off special IP header");
|
|
|
|
if (sendto(s, (char *) &opack_icmp, PHDR_LEN, MSG_DONTROUTE,
|
|
|
|
(struct sockaddr *)&loc_addr,
|
1997-12-01 01:33:30 +03:00
|
|
|
sizeof(struct sockaddr_in)) < 0) {
|
|
|
|
/*
|
|
|
|
* XXX: we only report this as a warning in verbose
|
|
|
|
* mode because people get confused when they see
|
|
|
|
* this error when they are running in single user
|
|
|
|
* mode and they have not configured lo0
|
|
|
|
*/
|
|
|
|
if (pingflags & F_VERBOSE)
|
|
|
|
warn("failed to clear cached route");
|
|
|
|
}
|
1997-06-01 23:34:46 +04:00
|
|
|
sw = 1;
|
|
|
|
if (setsockopt(s,IPPROTO_IP,IP_HDRINCL,
|
|
|
|
(char *)&sw, sizeof(sw)) < 0)
|
|
|
|
err(1, "Can't set special IP header");
|
|
|
|
|
|
|
|
(void)gettimeofday(&clear_cache,0);
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
1997-06-01 23:34:46 +04:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
opack_icmp.icmp_type = ICMP_ECHO;
|
|
|
|
opack_icmp.icmp_id = ident;
|
|
|
|
if (pingflags & F_TIMING)
|
1997-06-01 23:34:46 +04:00
|
|
|
(void) memcpy(&opack_icmp.icmp_data[0], &now, sizeof(now));
|
1997-03-12 00:22:49 +03:00
|
|
|
cc = datalen+PHDR_LEN;
|
|
|
|
opack_icmp.icmp_cksum = 0;
|
|
|
|
opack_icmp.icmp_cksum = in_cksum((u_short*)&opack_icmp, cc);
|
|
|
|
|
1997-06-01 23:34:46 +04:00
|
|
|
cc += opack_ip->ip_hl<<2;
|
|
|
|
opack_ip->ip_len = cc;
|
|
|
|
i = sendto(s, (char *) opack_ip, cc, 0,
|
|
|
|
(struct sockaddr *)&send_addr, sizeof(struct sockaddr_in));
|
1997-03-12 00:22:49 +03:00
|
|
|
if (i != cc) {
|
|
|
|
jiggle_flush(1);
|
|
|
|
if (i < 0)
|
1997-06-01 23:34:46 +04:00
|
|
|
warn("sendto");
|
1997-03-12 00:22:49 +03:00
|
|
|
else
|
1997-06-01 23:34:46 +04:00
|
|
|
warnx("wrote %s %d chars, ret=%d", hostname, cc, i);
|
1997-03-12 00:22:49 +03:00
|
|
|
(void)fflush(stderr);
|
|
|
|
}
|
|
|
|
lastrcvd = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
CLR(ntransmitted);
|
|
|
|
ntransmitted++;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
last_tx = now;
|
|
|
|
if (next_tx.tv_sec == 0) {
|
|
|
|
first_tx = now;
|
|
|
|
next_tx = now;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
/* Transmit regularly, at always the same microsecond in the
|
|
|
|
* second when going at one packet per second.
|
|
|
|
* If we are at most 100 ms behind, send extras to get caught up.
|
|
|
|
* Otherwise, skip packets we were too slow to send.
|
|
|
|
*/
|
|
|
|
if (diffsec(&next_tx, &now) <= interval) {
|
|
|
|
do {
|
|
|
|
timevaladd(&next_tx, &interval_tv);
|
|
|
|
} while (diffsec(&next_tx, &now) < -0.1);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
if (pingflags & F_FLOOD)
|
|
|
|
jiggle(1);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
/* While the packet is going out, ready buffer for the next
|
|
|
|
* packet. Use a fast but not very good random number generator.
|
|
|
|
*/
|
|
|
|
if (pingflags & F_PING_RANDOM)
|
|
|
|
rnd_fill();
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
static void
|
1997-06-01 23:34:46 +04:00
|
|
|
pr_pack_sub(int cc,
|
|
|
|
char *addr,
|
|
|
|
int seqno,
|
|
|
|
int dupflag,
|
|
|
|
int ttl,
|
1997-03-12 00:22:49 +03:00
|
|
|
double triptime)
|
|
|
|
{
|
|
|
|
jiggle_flush(1);
|
|
|
|
|
|
|
|
if (pingflags & F_FLOOD)
|
|
|
|
return;
|
|
|
|
|
|
|
|
(void)printf("%d bytes from %s: icmp_seq=%u", cc, addr, seqno);
|
1997-06-01 23:34:46 +04:00
|
|
|
if (dupflag)
|
|
|
|
(void)printf(" DUP!");
|
1997-03-12 00:22:49 +03:00
|
|
|
(void)printf(" ttl=%d", ttl);
|
|
|
|
if (pingflags & F_TIMING)
|
|
|
|
(void)printf(" time=%.3f ms", triptime*1000.0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1997-03-12 00:22:49 +03:00
|
|
|
* Print out the packet, if it came from us. This logic is necessary
|
1993-03-21 12:45:37 +03:00
|
|
|
* because ALL readers of the ICMP socket get a copy of ALL ICMP packets
|
|
|
|
* which arrive ('tis only fair). This permits multiple copies of this
|
|
|
|
* program to be run without having intermingled output (or statistics!).
|
|
|
|
*/
|
1997-03-12 00:22:49 +03:00
|
|
|
static void
|
1997-06-01 23:34:46 +04:00
|
|
|
pr_pack(u_char *buf,
|
|
|
|
int cc,
|
1997-03-12 00:22:49 +03:00
|
|
|
struct sockaddr_in *from)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-03-12 00:22:49 +03:00
|
|
|
struct ip *ip;
|
1997-09-15 11:21:09 +04:00
|
|
|
struct icmp *icp;
|
|
|
|
int i, j;
|
|
|
|
u_char *cp;
|
1993-03-21 12:45:37 +03:00
|
|
|
static int old_rrlen;
|
|
|
|
static char old_rr[MAX_IPOPTLEN];
|
1997-03-12 00:22:49 +03:00
|
|
|
int hlen, dupflag = 0, dumped;
|
|
|
|
double triptime = 0.0;
|
|
|
|
#define PR_PACK_SUB() {if (!dumped) { \
|
|
|
|
dumped = 1; \
|
1997-06-01 23:34:46 +04:00
|
|
|
pr_pack_sub(cc, inet_ntoa(from->sin_addr), \
|
|
|
|
ntohs((u_short)icp->icmp_seq), \
|
1997-03-12 00:22:49 +03:00
|
|
|
dupflag, ip->ip_ttl, triptime);}}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/* Check the IP header */
|
1997-03-12 00:22:49 +03:00
|
|
|
ip = (struct ip *) buf;
|
1993-03-21 12:45:37 +03:00
|
|
|
hlen = ip->ip_hl << 2;
|
|
|
|
if (cc < hlen + ICMP_MINLEN) {
|
1997-03-12 00:22:49 +03:00
|
|
|
if (pingflags & F_VERBOSE) {
|
|
|
|
jiggle_flush(1);
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf("packet too short (%d bytes) from %s\n",
|
1997-03-12 00:22:49 +03:00
|
|
|
cc, inet_ntoa(from->sin_addr));
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now the ICMP part */
|
1997-03-12 00:22:49 +03:00
|
|
|
dumped = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
cc -= hlen;
|
|
|
|
icp = (struct icmp *)(buf + hlen);
|
1997-03-12 00:22:49 +03:00
|
|
|
if (icp->icmp_type == ICMP_ECHOREPLY
|
|
|
|
&& icp->icmp_id == ident) {
|
|
|
|
|
|
|
|
if (icp->icmp_seq == htons((u_short)(ntransmitted-1)))
|
|
|
|
lastrcvd = 1;
|
|
|
|
last_rx = now;
|
|
|
|
if (first_rx.tv_sec == 0)
|
|
|
|
first_rx = last_rx;
|
|
|
|
nreceived++;
|
|
|
|
if (pingflags & F_TIMING) {
|
1997-03-19 15:36:03 +03:00
|
|
|
struct timeval tv;
|
|
|
|
(void) memcpy(&tv, icp->icmp_data, sizeof(tv));
|
|
|
|
triptime = diffsec(&last_rx, &tv);
|
1993-03-21 12:45:37 +03:00
|
|
|
tsum += triptime;
|
|
|
|
if (triptime < tmin)
|
|
|
|
tmin = triptime;
|
|
|
|
if (triptime > tmax)
|
|
|
|
tmax = triptime;
|
|
|
|
}
|
|
|
|
|
1997-03-13 19:19:10 +03:00
|
|
|
if (TST(ntohs((u_short)icp->icmp_seq))) {
|
1997-03-12 00:22:49 +03:00
|
|
|
nrepeats++, nreceived--;
|
|
|
|
dupflag=1;
|
1997-06-01 23:34:46 +04:00
|
|
|
} else {
|
1997-03-13 19:19:10 +03:00
|
|
|
SET(ntohs((u_short)icp->icmp_seq));
|
1997-06-01 23:34:46 +04:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
if (pingflags & F_QUIET)
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
if (!(pingflags & F_FLOOD))
|
|
|
|
PR_PACK_SUB();
|
|
|
|
|
|
|
|
/* check the data */
|
|
|
|
if (datalen > PHDR_LEN
|
|
|
|
&& !(pingflags & F_PING_RANDOM)
|
1997-06-01 23:34:46 +04:00
|
|
|
&& memcmp(&icp->icmp_data[PHDR_LEN],
|
|
|
|
&opack_icmp.icmp_data[PHDR_LEN],
|
1997-03-12 00:22:49 +03:00
|
|
|
datalen-PHDR_LEN)) {
|
|
|
|
for (i=PHDR_LEN; i<datalen; i++) {
|
|
|
|
if (icp->icmp_data[PHDR_LEN+i]
|
|
|
|
!= opack_icmp.icmp_data[PHDR_LEN+i])
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
|
|
|
PR_PACK_SUB();
|
|
|
|
(void)printf("\nwrong data byte #%d should have been"
|
1997-06-01 23:34:46 +04:00
|
|
|
" %#x but was %#x",
|
|
|
|
i, (u_char)opack_icmp.icmp_data[i],
|
1997-03-12 00:22:49 +03:00
|
|
|
(u_char)icp->icmp_data[i]);
|
|
|
|
for (i=PHDR_LEN; i<datalen; i++) {
|
|
|
|
if ((i%16) == PHDR_LEN)
|
|
|
|
(void)printf("\n\t");
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf("%2x ",(u_char)icp->icmp_data[i]);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
} else {
|
1997-03-12 00:22:49 +03:00
|
|
|
if (!pr_icmph(icp, from, cc))
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
1997-03-12 00:22:49 +03:00
|
|
|
dumped = 2;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Display any IP options */
|
1997-03-12 00:22:49 +03:00
|
|
|
cp = buf + sizeof(struct ip);
|
|
|
|
while (hlen > (int)sizeof(struct ip)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
switch (*cp) {
|
|
|
|
case IPOPT_EOL:
|
|
|
|
hlen = 0;
|
|
|
|
break;
|
|
|
|
case IPOPT_LSRR:
|
|
|
|
hlen -= 2;
|
|
|
|
j = *++cp;
|
|
|
|
++cp;
|
1997-03-12 00:22:49 +03:00
|
|
|
j -= IPOPT_MINOFF;
|
|
|
|
if (j <= 0)
|
|
|
|
continue;
|
|
|
|
if (dumped <= 1) {
|
|
|
|
j = ((j+3)/4)*4;
|
|
|
|
hlen -= j;
|
|
|
|
cp += j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
PR_PACK_SUB();
|
|
|
|
(void)printf("\nLSRR: ");
|
|
|
|
for (;;) {
|
|
|
|
pr_saddr("\t%s", cp);
|
|
|
|
cp += 4;
|
1993-03-21 12:45:37 +03:00
|
|
|
hlen -= 4;
|
|
|
|
j -= 4;
|
1997-03-12 00:22:49 +03:00
|
|
|
if (j <= 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
(void)putchar('\n');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IPOPT_RR:
|
1997-03-12 00:22:49 +03:00
|
|
|
j = *++cp; /* get length */
|
|
|
|
i = *++cp; /* and pointer */
|
1993-03-21 12:45:37 +03:00
|
|
|
hlen -= 2;
|
|
|
|
if (i > j)
|
|
|
|
i = j;
|
|
|
|
i -= IPOPT_MINOFF;
|
|
|
|
if (i <= 0)
|
|
|
|
continue;
|
1997-03-12 00:22:49 +03:00
|
|
|
if (dumped <= 1) {
|
|
|
|
if (i == old_rrlen
|
1997-06-01 23:34:46 +04:00
|
|
|
&& !memcmp(cp, old_rr, i)) {
|
1997-03-12 00:22:49 +03:00
|
|
|
if (dumped)
|
|
|
|
(void)printf("\t(same route)");
|
|
|
|
j = ((i+3)/4)*4;
|
|
|
|
hlen -= j;
|
|
|
|
cp += j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
old_rrlen = i;
|
1997-06-01 23:34:46 +04:00
|
|
|
(void) memcpy(old_rr, cp, i);
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
|
|
|
if (!dumped) {
|
|
|
|
jiggle_flush(1);
|
|
|
|
(void)printf("RR: ");
|
|
|
|
dumped = 1;
|
|
|
|
} else {
|
|
|
|
(void)printf("\nRR: ");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
for (;;) {
|
1997-03-12 00:22:49 +03:00
|
|
|
pr_saddr("\t%s", cp);
|
|
|
|
cp += 4;
|
1993-03-21 12:45:37 +03:00
|
|
|
hlen -= 4;
|
|
|
|
i -= 4;
|
|
|
|
if (i <= 0)
|
|
|
|
break;
|
|
|
|
(void)putchar('\n');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IPOPT_NOP:
|
1997-03-12 00:22:49 +03:00
|
|
|
if (dumped <= 1)
|
|
|
|
break;
|
|
|
|
PR_PACK_SUB();
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)printf("\nNOP");
|
|
|
|
break;
|
1997-03-12 00:22:49 +03:00
|
|
|
#ifdef sgi
|
|
|
|
case IPOPT_SECURITY: /* RFC 1108 RIPSO BSO */
|
|
|
|
case IPOPT_ESO: /* RFC 1108 RIPSO ESO */
|
|
|
|
case IPOPT_CIPSO: /* Commercial IPSO */
|
|
|
|
if ((sysconf(_SC_IP_SECOPTS)) > 0) {
|
|
|
|
i = (unsigned)cp[1];
|
|
|
|
hlen -= i - 1;
|
|
|
|
PR_PACK_SUB();
|
|
|
|
(void)printf("\nSEC:");
|
|
|
|
while (i--) {
|
|
|
|
(void)printf(" %02x", *cp++);
|
|
|
|
}
|
|
|
|
cp--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
default:
|
1997-03-12 00:22:49 +03:00
|
|
|
PR_PACK_SUB();
|
1997-09-15 11:21:09 +04:00
|
|
|
(void)printf("\nunknown option 0x%x", *cp);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
1997-03-12 00:22:49 +03:00
|
|
|
hlen--;
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dumped) {
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)putchar('\n');
|
|
|
|
(void)fflush(stdout);
|
1997-03-12 00:22:49 +03:00
|
|
|
} else {
|
|
|
|
jiggle(-1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
/* Compute the IP checksum
|
|
|
|
* This assumes the packet is less than 32K long.
|
|
|
|
*/
|
|
|
|
static u_short
|
1997-06-01 23:34:46 +04:00
|
|
|
in_cksum(u_short *p,
|
1997-03-12 00:22:49 +03:00
|
|
|
u_int len)
|
|
|
|
{
|
|
|
|
u_int sum = 0;
|
|
|
|
int nwords = len >> 1;
|
|
|
|
|
|
|
|
while (nwords-- != 0)
|
|
|
|
sum += *p++;
|
|
|
|
|
|
|
|
if (len & 1) {
|
|
|
|
union {
|
|
|
|
u_short w;
|
|
|
|
u_char c[2];
|
|
|
|
} u;
|
|
|
|
u.c[0] = *(u_char *)p;
|
|
|
|
u.c[1] = 0;
|
|
|
|
sum += u.w;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* end-around-carry */
|
|
|
|
sum = (sum >> 16) + (sum & 0xffff);
|
|
|
|
sum += (sum >> 16);
|
|
|
|
return (~sum);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1997-03-12 00:22:49 +03:00
|
|
|
* compute the difference of two timevals in seconds
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1997-03-12 00:22:49 +03:00
|
|
|
static double
|
1997-06-01 23:34:46 +04:00
|
|
|
diffsec(struct timeval *now,
|
1997-03-12 00:22:49 +03:00
|
|
|
struct timeval *then)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-03-12 00:22:49 +03:00
|
|
|
return ((now->tv_sec - then->tv_sec)*1.0
|
|
|
|
+ (now->tv_usec - then->tv_usec)/1000000.0);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
static void
|
1997-06-01 23:34:46 +04:00
|
|
|
timevaladd(struct timeval *t1,
|
1997-03-12 00:22:49 +03:00
|
|
|
struct timeval *t2)
|
|
|
|
{
|
|
|
|
|
|
|
|
t1->tv_sec += t2->tv_sec;
|
|
|
|
if ((t1->tv_usec += t2->tv_usec) > 1000000) {
|
|
|
|
t1->tv_sec++;
|
|
|
|
t1->tv_usec -= 1000000;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
static void
|
|
|
|
sec_to_timeval(const double sec, struct timeval *tp)
|
|
|
|
{
|
1997-06-01 23:34:46 +04:00
|
|
|
tp->tv_sec = sec;
|
1997-03-12 00:22:49 +03:00
|
|
|
tp->tv_usec = (sec - tp->tv_sec) * 1000000.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
timeval_to_sec(const struct timeval *tp)
|
|
|
|
{
|
|
|
|
return tp->tv_sec + tp->tv_usec / 1000000.0;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1997-03-12 00:22:49 +03:00
|
|
|
* Print statistics.
|
|
|
|
* Heavily buffered STDIO is used here, so that all the statistics
|
|
|
|
* will be written with 1 sys-write call. This is nice when more
|
|
|
|
* than one copy of the program is running on a terminal; it prevents
|
|
|
|
* the statistics output from becomming intermingled.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1997-03-12 00:22:49 +03:00
|
|
|
static void
|
|
|
|
summary(int header)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-03-12 00:22:49 +03:00
|
|
|
jiggle_flush(1);
|
1994-09-23 05:38:45 +04:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
if (header)
|
|
|
|
(void)printf("\n----%s PING Statistics----\n", hostname);
|
|
|
|
(void)printf("%d packets transmitted, ", ntransmitted);
|
|
|
|
(void)printf("%d packets received, ", nreceived);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (nrepeats)
|
1997-03-12 00:22:49 +03:00
|
|
|
(void)printf("+%d duplicates, ", nrepeats);
|
|
|
|
if (ntransmitted) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (nreceived > ntransmitted)
|
|
|
|
(void)printf("-- somebody's printing up packets!");
|
|
|
|
else
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf("%d%% packet loss",
|
1997-03-12 00:22:49 +03:00
|
|
|
(int) (((ntransmitted-nreceived)*100) /
|
|
|
|
ntransmitted));
|
1994-09-23 05:38:45 +04:00
|
|
|
}
|
1997-03-12 00:22:49 +03:00
|
|
|
(void)printf("\n");
|
|
|
|
if (nreceived && (pingflags & F_TIMING)) {
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf("round-trip min/avg/max = %.3f/%.3f/%.3f ms\n",
|
|
|
|
tmin*1000.0,
|
|
|
|
(tsum/(nreceived+nrepeats))*1000.0,
|
1997-03-12 00:22:49 +03:00
|
|
|
tmax*1000.0);
|
|
|
|
if (pingflags & F_FLOOD) {
|
|
|
|
double r = diffsec(&last_rx, &first_rx);
|
|
|
|
double t = diffsec(&last_tx, &first_tx);
|
|
|
|
if (r == 0)
|
|
|
|
r = 0.0001;
|
|
|
|
if (t == 0)
|
|
|
|
t = 0.0001;
|
|
|
|
(void)printf(" %.1f packets/sec sent, "
|
1997-06-01 23:34:46 +04:00
|
|
|
" %.1f packets/sec received\n",
|
1997-03-12 00:22:49 +03:00
|
|
|
ntransmitted/t, nreceived/r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print statistics when SIGINFO is received.
|
|
|
|
*/
|
|
|
|
/* ARGSUSED */
|
|
|
|
static void
|
|
|
|
prtsig(int s)
|
|
|
|
{
|
|
|
|
summary(0);
|
|
|
|
#ifdef SIGINFO
|
|
|
|
(void)signal(SIGINFO, prtsig);
|
|
|
|
#else
|
|
|
|
(void)signal(SIGQUIT, prtsig);
|
|
|
|
#endif
|
1996-11-06 23:42:14 +03:00
|
|
|
}
|
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1996-11-06 23:42:14 +03:00
|
|
|
/*
|
1997-03-12 00:22:49 +03:00
|
|
|
* On the first SIGINT, allow any outstanding packets to dribble in
|
1996-11-06 23:42:14 +03:00
|
|
|
*/
|
1997-03-12 00:22:49 +03:00
|
|
|
static void
|
|
|
|
prefinish(int s)
|
1996-11-06 23:42:14 +03:00
|
|
|
{
|
1997-03-12 00:22:49 +03:00
|
|
|
if (lastrcvd /* quit now if caught up */
|
|
|
|
|| nreceived == 0) /* or if remote is dead */
|
|
|
|
finish(0);
|
1996-11-06 23:42:14 +03:00
|
|
|
|
1997-11-06 00:29:29 +03:00
|
|
|
(void)signal(s, finish); /* do this only the 1st time */
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
if (npackets > ntransmitted) /* let the normal limit work */
|
|
|
|
npackets = ntransmitted;
|
1996-11-06 23:42:14 +03:00
|
|
|
}
|
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1996-11-06 23:42:14 +03:00
|
|
|
/*
|
1997-03-12 00:22:49 +03:00
|
|
|
* Print statistics and give up.
|
1996-11-06 23:42:14 +03:00
|
|
|
*/
|
1997-03-12 00:22:49 +03:00
|
|
|
/* ARGSUSED */
|
|
|
|
static void
|
|
|
|
finish(int s)
|
1996-11-06 23:42:14 +03:00
|
|
|
{
|
1997-06-01 23:34:46 +04:00
|
|
|
#if defined(SIGINFO) && defined(NOKERNINFO)
|
1997-03-12 00:22:49 +03:00
|
|
|
struct termios ts;
|
1996-11-06 23:42:14 +03:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
if (reset_kerninfo && tcgetattr (0, &ts) != -1) {
|
|
|
|
ts.c_lflag &= ~NOKERNINFO;
|
1997-11-06 00:29:29 +03:00
|
|
|
(void)tcsetattr(0, TCSANOW, &ts);
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
1996-11-06 23:42:14 +03:00
|
|
|
(void)signal(SIGINFO, SIG_IGN);
|
1997-03-12 00:22:49 +03:00
|
|
|
#else
|
|
|
|
(void)signal(SIGQUIT, SIG_DFL);
|
|
|
|
#endif
|
1996-11-06 23:42:14 +03:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
summary(1);
|
1997-06-01 23:34:46 +04:00
|
|
|
exit(nreceived > 0 ? 0 : 2);
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int /* 0=do not print it */
|
1997-06-01 23:34:46 +04:00
|
|
|
ck_pr_icmph(struct icmp *icp,
|
|
|
|
struct sockaddr_in *from,
|
|
|
|
int cc,
|
1997-03-12 00:22:49 +03:00
|
|
|
int override) /* 1=override VERBOSE if interesting */
|
|
|
|
{
|
|
|
|
int hlen;
|
1997-03-19 15:36:03 +03:00
|
|
|
struct ip ip;
|
|
|
|
struct icmp icp2;
|
1997-03-12 00:22:49 +03:00
|
|
|
int res;
|
|
|
|
|
|
|
|
if (pingflags & F_VERBOSE) {
|
|
|
|
res = 1;
|
|
|
|
jiggle_flush(1);
|
|
|
|
} else {
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
|
1997-03-19 15:36:03 +03:00
|
|
|
(void) memcpy(&ip, icp->icmp_data, sizeof(ip));
|
|
|
|
hlen = ip.ip_hl << 2;
|
|
|
|
if (ip.ip_p == IPPROTO_ICMP
|
1997-03-12 00:22:49 +03:00
|
|
|
&& hlen + 6 <= cc) {
|
1997-03-19 15:36:03 +03:00
|
|
|
(void) memcpy(&icp2, &icp->icmp_data[hlen], sizeof(icp2));
|
|
|
|
if (icp2.icmp_id == ident) {
|
1997-03-12 00:22:49 +03:00
|
|
|
/* remember to clear route cached in kernel
|
|
|
|
* if this ICMP message was for one of our packet.
|
|
|
|
*/
|
|
|
|
clear_cache.tv_sec = 0;
|
|
|
|
|
|
|
|
if (!res && override
|
|
|
|
&& (pingflags & (F_QUIET|F_SEMI_QUIET)) == 0) {
|
|
|
|
jiggle_flush(1);
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf("%d bytes from %s: ",
|
1997-03-12 00:22:49 +03:00
|
|
|
cc, pr_addr(&from->sin_addr));
|
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1996-11-06 23:42:14 +03:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
return res;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
1997-03-12 00:22:49 +03:00
|
|
|
* Print a descriptive string about an ICMP header other than an echo reply.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1997-03-12 00:22:49 +03:00
|
|
|
static int /* 0=printed nothing */
|
1997-06-01 23:34:46 +04:00
|
|
|
pr_icmph(struct icmp *icp,
|
|
|
|
struct sockaddr_in *from,
|
1997-03-12 00:22:49 +03:00
|
|
|
int cc)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-03-12 00:22:49 +03:00
|
|
|
switch (icp->icmp_type ) {
|
1993-03-21 12:45:37 +03:00
|
|
|
case ICMP_UNREACH:
|
1997-03-12 00:22:49 +03:00
|
|
|
if (!ck_pr_icmph(icp, from, cc, 1))
|
|
|
|
return 0;
|
|
|
|
switch (icp->icmp_code) {
|
1993-03-21 12:45:37 +03:00
|
|
|
case ICMP_UNREACH_NET:
|
1997-03-12 00:22:49 +03:00
|
|
|
(void)printf("Destination Net Unreachable");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case ICMP_UNREACH_HOST:
|
1997-03-12 00:22:49 +03:00
|
|
|
(void)printf("Destination Host Unreachable");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case ICMP_UNREACH_PROTOCOL:
|
1997-03-12 00:22:49 +03:00
|
|
|
(void)printf("Destination Protocol Unreachable");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case ICMP_UNREACH_PORT:
|
1997-03-12 00:22:49 +03:00
|
|
|
(void)printf("Destination Port Unreachable");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case ICMP_UNREACH_NEEDFRAG:
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf("frag needed and DF set. Next MTU=%d",
|
1997-03-12 00:22:49 +03:00
|
|
|
icp->icmp_nextmtu);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case ICMP_UNREACH_SRCFAIL:
|
1997-03-12 00:22:49 +03:00
|
|
|
(void)printf("Source Route Failed");
|
|
|
|
break;
|
|
|
|
case ICMP_UNREACH_NET_UNKNOWN:
|
|
|
|
(void)printf("Unreachable unknown net");
|
|
|
|
break;
|
|
|
|
case ICMP_UNREACH_HOST_UNKNOWN:
|
|
|
|
(void)printf("Unreachable unknown host");
|
|
|
|
break;
|
|
|
|
case ICMP_UNREACH_ISOLATED:
|
|
|
|
(void)printf("Unreachable host isolated");
|
|
|
|
break;
|
|
|
|
case ICMP_UNREACH_NET_PROHIB:
|
|
|
|
(void)printf("Net prohibited access");
|
|
|
|
break;
|
|
|
|
case ICMP_UNREACH_HOST_PROHIB:
|
|
|
|
(void)printf("Host prohibited access");
|
|
|
|
break;
|
|
|
|
case ICMP_UNREACH_TOSNET:
|
|
|
|
(void)printf("Bad TOS for net");
|
|
|
|
break;
|
|
|
|
case ICMP_UNREACH_TOSHOST:
|
|
|
|
(void)printf("Bad TOS for host");
|
|
|
|
break;
|
|
|
|
case 13:
|
|
|
|
(void)printf("Communication prohibited");
|
|
|
|
break;
|
|
|
|
case 14:
|
|
|
|
(void)printf("Host precedence violation");
|
|
|
|
break;
|
|
|
|
case 15:
|
|
|
|
(void)printf("Precedence cutoff");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
default:
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf("Bad Destination Unreachable Code: %d",
|
1997-03-12 00:22:49 +03:00
|
|
|
icp->icmp_code);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Print returned IP header information */
|
1997-03-12 00:22:49 +03:00
|
|
|
pr_retip(icp, cc);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
case ICMP_SOURCEQUENCH:
|
1997-03-12 00:22:49 +03:00
|
|
|
if (!ck_pr_icmph(icp, from, cc, 1))
|
|
|
|
return 0;
|
|
|
|
(void)printf("Source Quench");
|
|
|
|
pr_retip(icp, cc);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
case ICMP_REDIRECT:
|
1997-03-12 00:22:49 +03:00
|
|
|
if (!ck_pr_icmph(icp, from, cc, 1))
|
|
|
|
return 0;
|
|
|
|
switch (icp->icmp_code) {
|
1993-03-21 12:45:37 +03:00
|
|
|
case ICMP_REDIRECT_NET:
|
1997-03-12 00:22:49 +03:00
|
|
|
(void)printf("Redirect: Network");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case ICMP_REDIRECT_HOST:
|
1997-03-12 00:22:49 +03:00
|
|
|
(void)printf("Redirect: Host");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case ICMP_REDIRECT_TOSNET:
|
1997-03-12 00:22:49 +03:00
|
|
|
(void)printf("Redirect: Type of Service and Network");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case ICMP_REDIRECT_TOSHOST:
|
1997-03-12 00:22:49 +03:00
|
|
|
(void)printf("Redirect: Type of Service and Host");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
default:
|
1997-03-12 00:22:49 +03:00
|
|
|
(void)printf("Redirect: Bad Code: %d", icp->icmp_code);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf(" New addr: %s",
|
1997-03-12 00:22:49 +03:00
|
|
|
pr_addr(&icp->icmp_hun.ih_gwaddr));
|
|
|
|
pr_retip(icp, cc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_ECHO:
|
|
|
|
if (!ck_pr_icmph(icp, from, cc, 0))
|
|
|
|
return 0;
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf("Echo Request: ID=%d seq=%d",
|
1997-03-12 00:22:49 +03:00
|
|
|
icp->icmp_id, icp->icmp_seq);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMP_ECHOREPLY:
|
|
|
|
/* displaying other's pings is too noisey */
|
|
|
|
#if 0
|
|
|
|
if (!ck_pr_icmph(icp, from, cc, 0))
|
|
|
|
return 0;
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf("Echo Reply: ID=%d seq=%d",
|
1997-03-12 00:22:49 +03:00
|
|
|
icp->icmp_id, icp->icmp_seq);
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
#else
|
1997-03-12 00:22:49 +03:00
|
|
|
return 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
case ICMP_ROUTERADVERT:
|
|
|
|
if (!ck_pr_icmph(icp, from, cc, 0))
|
|
|
|
return 0;
|
|
|
|
(void)printf("Router Discovery Advert");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
case ICMP_ROUTERSOLICIT:
|
|
|
|
if (!ck_pr_icmph(icp, from, cc, 0))
|
|
|
|
return 0;
|
|
|
|
(void)printf("Router Discovery Solicit");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
case ICMP_TIMXCEED:
|
1997-03-12 00:22:49 +03:00
|
|
|
if (!ck_pr_icmph(icp, from, cc, 1))
|
|
|
|
return 0;
|
|
|
|
switch (icp->icmp_code ) {
|
1993-03-21 12:45:37 +03:00
|
|
|
case ICMP_TIMXCEED_INTRANS:
|
1997-03-12 00:22:49 +03:00
|
|
|
(void)printf("Time To Live exceeded");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case ICMP_TIMXCEED_REASS:
|
1997-03-12 00:22:49 +03:00
|
|
|
(void)printf("Frag reassembly time exceeded");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
default:
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf("Time exceeded, Bad Code: %d",
|
1997-03-12 00:22:49 +03:00
|
|
|
icp->icmp_code);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
1997-03-12 00:22:49 +03:00
|
|
|
pr_retip(icp, cc);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
case ICMP_PARAMPROB:
|
1997-03-12 00:22:49 +03:00
|
|
|
if (!ck_pr_icmph(icp, from, cc, 1))
|
|
|
|
return 0;
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf("Parameter problem: pointer = 0x%02x",
|
1997-03-12 00:22:49 +03:00
|
|
|
icp->icmp_hun.ih_pptr);
|
|
|
|
pr_retip(icp, cc);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
case ICMP_TSTAMP:
|
1997-03-12 00:22:49 +03:00
|
|
|
if (!ck_pr_icmph(icp, from, cc, 0))
|
|
|
|
return 0;
|
|
|
|
(void)printf("Timestamp");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
case ICMP_TSTAMPREPLY:
|
1997-03-12 00:22:49 +03:00
|
|
|
if (!ck_pr_icmph(icp, from, cc, 0))
|
|
|
|
return 0;
|
|
|
|
(void)printf("Timestamp Reply");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
case ICMP_IREQ:
|
1997-03-12 00:22:49 +03:00
|
|
|
if (!ck_pr_icmph(icp, from, cc, 0))
|
|
|
|
return 0;
|
|
|
|
(void)printf("Information Request");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
case ICMP_IREQREPLY:
|
1997-03-12 00:22:49 +03:00
|
|
|
if (!ck_pr_icmph(icp, from, cc, 0))
|
|
|
|
return 0;
|
|
|
|
(void)printf("Information Reply");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
case ICMP_MASKREQ:
|
1997-03-12 00:22:49 +03:00
|
|
|
if (!ck_pr_icmph(icp, from, cc, 0))
|
|
|
|
return 0;
|
|
|
|
(void)printf("Address Mask Request");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
case ICMP_MASKREPLY:
|
1997-03-12 00:22:49 +03:00
|
|
|
if (!ck_pr_icmph(icp, from, cc, 0))
|
|
|
|
return 0;
|
|
|
|
(void)printf("Address Mask Reply");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
default:
|
1997-03-12 00:22:49 +03:00
|
|
|
if (!ck_pr_icmph(icp, from, cc, 0))
|
|
|
|
return 0;
|
|
|
|
(void)printf("Bad ICMP type: %d", icp->icmp_type);
|
|
|
|
if (pingflags & F_VERBOSE)
|
|
|
|
pr_iph(icp, cc);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
return 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1997-03-12 00:22:49 +03:00
|
|
|
* Print an IP header with options.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1997-03-12 00:22:49 +03:00
|
|
|
static void
|
1997-06-01 23:34:46 +04:00
|
|
|
pr_iph(struct icmp *icp,
|
1997-03-12 00:22:49 +03:00
|
|
|
int cc)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-03-12 00:22:49 +03:00
|
|
|
int hlen;
|
|
|
|
u_char *cp;
|
1997-03-19 15:36:03 +03:00
|
|
|
struct ip ip;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-03-19 15:36:03 +03:00
|
|
|
(void) memcpy(&ip, icp->icmp_data, sizeof(ip));
|
|
|
|
|
|
|
|
hlen = ip.ip_hl << 2;
|
1997-06-01 23:34:46 +04:00
|
|
|
cp = (u_char *) &icp->icmp_data[20]; /* point to options */
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
(void)printf("\n Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n");
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf(" %1x %1x %02x %04x %04x",
|
1997-03-19 15:36:03 +03:00
|
|
|
ip.ip_v, ip.ip_hl, ip.ip_tos, ip.ip_len, ip.ip_id);
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf(" %1x %04x",
|
1997-03-19 15:36:03 +03:00
|
|
|
((ip.ip_off)&0xe000)>>13, (ip.ip_off)&0x1fff);
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf(" %02x %02x %04x",
|
1997-03-19 15:36:03 +03:00
|
|
|
ip.ip_ttl, ip.ip_p, ip.ip_sum);
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf(" %15s ",
|
1997-03-19 15:36:03 +03:00
|
|
|
inet_ntoa(*(struct in_addr *)&ip.ip_src.s_addr));
|
|
|
|
(void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip.ip_dst.s_addr));
|
1997-03-12 00:22:49 +03:00
|
|
|
/* dump any option bytes */
|
|
|
|
while (hlen-- > 20 && cp < (u_char*)icp+cc) {
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)printf("%02x", *cp++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1997-03-12 00:22:49 +03:00
|
|
|
* Print an ASCII host address starting from a string of bytes.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1997-03-12 00:22:49 +03:00
|
|
|
static void
|
1997-06-01 23:34:46 +04:00
|
|
|
pr_saddr(char *pat,
|
1997-03-12 00:22:49 +03:00
|
|
|
u_char *cp)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-09-15 11:21:09 +04:00
|
|
|
n_long l;
|
1997-03-12 00:22:49 +03:00
|
|
|
struct in_addr addr;
|
|
|
|
|
|
|
|
l = (u_char)*++cp;
|
|
|
|
l = (l<<8) + (u_char)*++cp;
|
|
|
|
l = (l<<8) + (u_char)*++cp;
|
|
|
|
l = (l<<8) + (u_char)*++cp;
|
|
|
|
addr.s_addr = htonl(l);
|
|
|
|
(void)printf(pat, (l == 0) ? "0.0.0.0" : pr_addr(&addr));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1997-03-12 00:22:49 +03:00
|
|
|
* Return an ASCII host address
|
|
|
|
* as a dotted quad and optionally with a hostname
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1997-03-12 00:22:49 +03:00
|
|
|
static char *
|
|
|
|
pr_addr(struct in_addr *addr) /* in network order */
|
|
|
|
{
|
|
|
|
struct hostent *hp;
|
|
|
|
static char buf[MAXHOSTNAMELEN+4+16+1];
|
|
|
|
|
|
|
|
if ((pingflags & F_NUMERIC)
|
|
|
|
|| !(hp = gethostbyaddr((char *)addr, sizeof(*addr), AF_INET))) {
|
|
|
|
(void)sprintf(buf, "%s", inet_ntoa(*addr));
|
|
|
|
} else {
|
|
|
|
(void)sprintf(buf, "%s (%s)", hp->h_name, inet_ntoa(*addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dump some info on a returned (via ICMP) IP packet.
|
|
|
|
*/
|
|
|
|
static void
|
1997-06-01 23:34:46 +04:00
|
|
|
pr_retip(struct icmp *icp,
|
1997-03-12 00:22:49 +03:00
|
|
|
int cc)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-03-12 00:22:49 +03:00
|
|
|
int hlen;
|
1997-06-01 23:34:46 +04:00
|
|
|
u_char *cp;
|
1997-03-19 15:36:03 +03:00
|
|
|
struct ip ip;
|
|
|
|
|
|
|
|
(void) memcpy(&ip, icp->icmp_data, sizeof(ip));
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
if (pingflags & F_VERBOSE)
|
|
|
|
pr_iph(icp, cc);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-03-19 15:36:03 +03:00
|
|
|
hlen = ip.ip_hl << 2;
|
1997-06-01 23:34:46 +04:00
|
|
|
cp = (u_char *) &icp->icmp_data[hlen];
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1997-03-19 15:36:03 +03:00
|
|
|
if (ip.ip_p == IPPROTO_TCP) {
|
1997-03-12 00:22:49 +03:00
|
|
|
if (pingflags & F_VERBOSE)
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf("\n TCP: from port %u, to port %u",
|
1997-03-12 00:22:49 +03:00
|
|
|
(*cp*256+*(cp+1)), (*(cp+2)*256+*(cp+3)));
|
1997-03-19 15:36:03 +03:00
|
|
|
} else if (ip.ip_p == IPPROTO_UDP) {
|
1997-03-12 00:22:49 +03:00
|
|
|
if (pingflags & F_VERBOSE)
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf("\n UDP: from port %u, to port %u",
|
1997-03-12 00:22:49 +03:00
|
|
|
(*cp*256+*(cp+1)), (*(cp+2)*256+*(cp+3)));
|
1997-03-19 15:36:03 +03:00
|
|
|
} else if (ip.ip_p == IPPROTO_ICMP) {
|
|
|
|
struct icmp icp2;
|
|
|
|
(void) memcpy(&icp2, cp, sizeof(icp2));
|
|
|
|
if (icp2.icmp_type == ICMP_ECHO) {
|
|
|
|
if (pingflags & F_VERBOSE)
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf("\n ID=%u icmp_seq=%u",
|
1997-07-17 10:39:46 +04:00
|
|
|
ntohs((u_short)icp2.icmp_id),
|
|
|
|
ntohs((u_short)icp2.icmp_seq));
|
1997-03-19 15:36:03 +03:00
|
|
|
else
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf(" for icmp_seq=%u",
|
1997-07-17 10:39:46 +04:00
|
|
|
ntohs((u_short)icp2.icmp_seq));
|
1997-03-19 15:36:03 +03:00
|
|
|
}
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
static void
|
|
|
|
fill(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-09-15 11:21:09 +04:00
|
|
|
int i, j, k;
|
1993-03-21 12:45:37 +03:00
|
|
|
char *cp;
|
1997-03-12 00:22:49 +03:00
|
|
|
int pat[16];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
for (cp = fill_pat; *cp != '\0'; cp++) {
|
1994-09-23 05:38:45 +04:00
|
|
|
if (!isxdigit(*cp))
|
1997-03-12 00:22:49 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cp == fill_pat || *cp != '\0' || (cp-fill_pat) > 16*2) {
|
|
|
|
(void)fflush(stdout);
|
1997-06-01 23:34:46 +04:00
|
|
|
errx(1, "\"-p %s\": patterns must be specified with"
|
|
|
|
" 1-32 hex digits\n",
|
|
|
|
fill_pat);
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
|
|
|
|
1997-06-01 23:34:46 +04:00
|
|
|
i = sscanf(fill_pat,
|
|
|
|
"%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
|
|
|
|
&pat[0], &pat[1], &pat[2], &pat[3],
|
|
|
|
&pat[4], &pat[5], &pat[6], &pat[7],
|
|
|
|
&pat[8], &pat[9], &pat[10], &pat[11],
|
1997-03-12 00:22:49 +03:00
|
|
|
&pat[12], &pat[13], &pat[14], &pat[15]);
|
|
|
|
|
|
|
|
for (k=PHDR_LEN, j = 0; k <= datalen; k++) {
|
|
|
|
opack_icmp.icmp_data[k] = pat[j];
|
|
|
|
if (++j >= i)
|
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(pingflags & F_QUIET)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)printf("PATTERN: 0x");
|
1997-03-12 00:22:49 +03:00
|
|
|
for (j=0; j<i; j++)
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)printf("%02x",
|
1997-03-12 00:22:49 +03:00
|
|
|
(u_char)opack_icmp.icmp_data[PHDR_LEN+j]);
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)printf("\n");
|
|
|
|
}
|
1997-03-12 00:22:49 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
rnd_fill(void)
|
|
|
|
{
|
|
|
|
static u_int rnd;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = PHDR_LEN; i < datalen; i++) {
|
|
|
|
rnd = (314157*rnd + 66329) & 0xffff;
|
|
|
|
opack_icmp.icmp_data[i] = rnd>>8;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1997-03-12 00:22:49 +03:00
|
|
|
static void
|
1997-06-01 23:34:46 +04:00
|
|
|
gethost(const char *arg,
|
|
|
|
const char *name,
|
|
|
|
struct sockaddr_in *sa,
|
|
|
|
char *realname,
|
|
|
|
int realname_len)
|
1997-03-12 00:22:49 +03:00
|
|
|
{
|
1997-06-01 23:34:46 +04:00
|
|
|
struct hostent *hp;
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1997-11-06 00:29:29 +03:00
|
|
|
(void)memset(sa, 0, sizeof(*sa));
|
1997-06-01 23:34:46 +04:00
|
|
|
sa->sin_family = AF_INET;
|
1997-03-12 00:22:49 +03:00
|
|
|
|
1997-06-01 23:34:46 +04:00
|
|
|
/* If it is an IP address, try to convert it to a name to
|
|
|
|
* have something nice to display.
|
|
|
|
*/
|
|
|
|
if (inet_aton(name, &sa->sin_addr) != 0) {
|
|
|
|
if (realname) {
|
|
|
|
if (pingflags & F_NUMERIC)
|
|
|
|
hp = 0;
|
|
|
|
else
|
|
|
|
hp = gethostbyaddr((char *)&sa->sin_addr,
|
|
|
|
sizeof(sa->sin_addr),
|
|
|
|
AF_INET);
|
|
|
|
(void)strncpy(realname, hp ? hp->h_name : name,
|
|
|
|
realname_len);
|
|
|
|
realname[realname_len-1] = '\0';
|
|
|
|
}
|
1997-03-12 00:22:49 +03:00
|
|
|
return;
|
1997-06-01 23:34:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
hp = gethostbyname(name);
|
|
|
|
if (!hp)
|
|
|
|
errx(1, "Cannot resolve \"%s\" (%s)",name,hstrerror(h_errno));
|
|
|
|
|
|
|
|
if (hp->h_addrtype != AF_INET)
|
|
|
|
errx(1, "%s only supported with IP", arg);
|
|
|
|
|
1997-11-06 00:29:29 +03:00
|
|
|
(void)memmove(&sa->sin_addr, hp->h_addr, sizeof(sa->sin_addr));
|
1997-06-01 23:34:46 +04:00
|
|
|
|
|
|
|
if (realname) {
|
|
|
|
(void)strncpy(realname, hp->h_name, realname_len);
|
|
|
|
realname[realname_len-1] = '\0';
|
|
|
|
}
|
1997-03-12 00:22:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-06-01 23:34:46 +04:00
|
|
|
(void)fprintf(stderr, "Usage: \n"
|
1997-12-01 11:41:48 +03:00
|
|
|
"%s [-dDfnoqrvRLP] [-c count] [-s size] [-l preload]"
|
1997-06-01 23:34:46 +04:00
|
|
|
" [-p pattern]\n"
|
|
|
|
" [-i interval] [-i maxwait] [-t tos] [-T ttl]"
|
|
|
|
" [-I addr] [-g gateway] host\n",
|
|
|
|
__progname);
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|