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 .\" @(#)ping.8 8.2 (Berkeley) 12/11/93
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.Dd August 15, 2005 .Dd April 4, 2006
.Dt PING 8 .Dt PING 8
.Os .Os
.Sh NAME .Sh NAME
@ -52,6 +52,7 @@ packets to network hosts
.Op Fl S Ar src_addr .Op Fl S Ar src_addr
.Op Fl s Ar packetsize .Op Fl s Ar packetsize
.Op Fl t Ar timeout .Op Fl t Ar timeout
.Op Fl W Ar waittime
.Op Fl z Ar tos .Op Fl z Ar tos
.Ar host .Ar host
.Nm .Nm
@ -68,6 +69,7 @@ packets to network hosts
.Op Fl s Ar packetsize .Op Fl s Ar packetsize
.Op Fl T Ar ttl .Op Fl T Ar ttl
.Op Fl t Ar timeout .Op Fl t Ar timeout
.Op Fl W Ar waittime
.Op Fl z Ar tos .Op Fl z Ar tos
.Ar mcast-group .Ar mcast-group
.Sh DESCRIPTION .Sh DESCRIPTION
@ -306,6 +308,10 @@ Verbose output.
packets other than packets other than
.Tn ECHO_RESPONSE .Tn ECHO_RESPONSE
that are received are listed. 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 .It Fl z Ar tos
Use the specified type of service. Use the specified type of service.
.El .El

View File

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