Add a new feature to ping(8) - possibility to specify maximum

wait time for a packet. This allows to:

 - Count number of packets received before and after specified
   time.
 - Shorten time of execution of 'ping -c 1' scripts.

Submitted by:	Lytochkin Boris <lytboris gmail.com>
This commit is contained in:
Gleb Smirnoff 2006-04-05 12:30:42 +00:00
parent a460ae4b4c
commit d6cd14974c
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=157535
2 changed files with 35 additions and 8 deletions

View File

@ -28,7 +28,7 @@
.\" @(#)ping.8 8.2 (Berkeley) 12/11/93
.\" $FreeBSD$
.\"
.Dd August 15, 2005
.Dd April 4, 2006
.Dt PING 8
.Os
.Sh NAME
@ -52,6 +52,7 @@ packets to network hosts
.Op Fl S Ar src_addr
.Op Fl s Ar packetsize
.Op Fl t Ar timeout
.Op Fl W Ar waittime
.Op Fl z Ar tos
.Ar host
.Nm
@ -68,6 +69,7 @@ packets to network hosts
.Op Fl s Ar packetsize
.Op Fl T Ar ttl
.Op Fl t Ar timeout
.Op Fl W Ar waittime
.Op Fl z Ar tos
.Ar mcast-group
.Sh DESCRIPTION
@ -306,6 +308,10 @@ Verbose output.
packets other than
.Tn ECHO_RESPONSE
that are received are listed.
.It Fl W Ar waittime
Time in milliseconds to wait for a reply for each packet sent.
If a reply arrives later, the packet is not printed as replied, but
considered as replied when calculating statistics.
.It Fl z Ar tos
Use the specified type of service.
.El

View File

@ -100,7 +100,7 @@ __FBSDID("$FreeBSD$");
/* runs out of buffer space */
#define MAXIPLEN (sizeof(struct ip) + MAX_IPOPTLEN)
#define MAXICMPLEN (ICMP_ADVLENMIN + MAX_IPOPTLEN)
#define MAXWAIT 10 /* max seconds to wait for response */
#define MAXWAIT 10000 /* max ms to wait for response */
#define MAXALARM (60 * 60) /* max seconds for alarm timeout */
#define MAXTOS 255
@ -143,6 +143,7 @@ int options;
#define F_MASK 0x80000
#define F_TIME 0x100000
#define F_SWEEP 0x200000
#define F_WAITTIME 0x400000
/*
* MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
@ -183,6 +184,8 @@ int sweepmax; /* max value of payload in sweep */
int sweepmin = 0; /* start value of payload in sweep */
int sweepincr = 1; /* payload increment in sweep */
int interval = 1000; /* interval between packets, ms */
int waittime = MAXWAIT; /* timeout for each packet */
long nrcvtimeout = 0; /* # of packets we got back after waittime */
/* timing */
int timing; /* flag to do timing */
@ -261,7 +264,7 @@ main(argc, argv)
outpack = outpackhdr + sizeof(struct ip);
while ((ch = getopt(argc, argv,
"Aac:DdfG:g:h:I:i:Ll:M:m:nop:QqRrS:s:T:t:vz:"
"Aac:DdfG:g:h:I:i:Ll:M:m:nop:QqRrS:s:T:t:vW:z:"
#ifdef IPSEC
#ifdef IPSEC_POLICY_IPSEC
"P:"
@ -469,6 +472,14 @@ main(argc, argv)
case 'v':
options |= F_VERBOSE;
break;
case 'W': /* wait ms for answer */
t = strtod(optarg, &ep);
if (*ep || ep == optarg || t > (double)INT_MAX)
errx(EX_USAGE, "invalid timing interval: `%s'",
optarg);
options |= F_WAITTIME;
waittime = (int)t;
break;
case 'z':
options |= F_HDRINCL;
ultmp = strtoul(optarg, &ep, 0);
@ -880,8 +891,10 @@ main(argc, argv)
intvl.tv_sec = 2 * tmax / 1000;
if (!intvl.tv_sec)
intvl.tv_sec = 1;
} else
intvl.tv_sec = MAXWAIT;
} else {
intvl.tv_sec = waittime / 1000;
intvl.tv_usec = waittime % 1000 * 1000;
}
}
(void)gettimeofday(&last, NULL);
if (ntransmitted - nreceived - 1 > nmissedmax) {
@ -1075,6 +1088,11 @@ pr_pack(buf, cc, from, tv)
if (options & F_QUIET)
return;
if (options & F_WAITTIME && triptime > waittime) {
++nrcvtimeout;
return;
}
if (options & F_FLOOD)
(void)write(STDOUT_FILENO, &BSPACE, 1);
@ -1373,6 +1391,8 @@ finish()
(int)(((ntransmitted - nreceived) * 100) /
ntransmitted));
}
if (nrcvtimeout)
(void)printf(", %ld packets out of wait time", nrcvtimeout);
(void)putchar('\n');
if (nreceived && timing) {
double n = nreceived + nrepeats;
@ -1686,13 +1706,14 @@ static void
usage()
{
(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
"usage: ping [-AaDdfnoQqRrv] [-c count] [-G sweepmaxsize] [-g sweepminsize]",
" [-h sweepincrsize] [-i wait] [-l preload] [-M mask | time] [-m ttl]",
" " SECOPT " [-p pattern] [-S src_addr] [-s packetsize] [-t timeout]",
" [-z tos] host",
" [-W waittime] [-z tos] host",
" ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload]",
" [-M mask | time] [-m ttl]" SECOPT " [-p pattern] [-S src_addr]",
" [-s packetsize] [-T ttl] [-t timeout] [-z tos] mcast-group");
" [-s packetsize] [-T ttl] [-t timeout] [-W waittime]",
" [-z tos] mcast-group");
exit(EX_USAGE);
}