2009/10/4 Alfred M. Szmidt wrote: > Regarding option name BSD one has --deadline option for this > (timeout before ping exits) and timeout for 'time to wait for a > response'. So I guess it would nice to keep the options same. > > BSD doesn't use long option names, it uses -w and -W[1]. We can use > --timeout=N and --timeout-response=N for the long options, since that > is exactly what they do. >
Yep, agreed .. yes was confused. Thanks for clearing it. I have changed deadline to ping_timeout rather then timeout because it would have created name confusion with private timeout variable from ping_run. Added comment to libping.c call for gettimeofday as ping_timeout is not accessible there, so did not put an if condition. May you suggest something better then TIMEOUT_UNSPECIFIED ? I cannot think of anything more suitable then this. Review ? Thanks, -- Rakesh Pandit https://fedoraproject.org/ freedom, friends, features, first >From 8dd0e607b8547f4d2aca961516480ed78f71d5ff Mon Sep 17 00:00:00 2001 From: Rakesh Pandit <[email protected]> Date: Mon, 5 Oct 2009 11:49:01 +0530 Subject: [PATCH] Added new option --timeout to ping/ping6 for setting timeout before ping exits. --- ChangeLog | 17 +++++++++++++++++ NEWS | 5 ++++- doc/inetutils.texi | 6 ++++++ ping/libping.c | 2 ++ ping/ping.c | 22 ++++++++++++++++++---- ping/ping6.c | 23 +++++++++++++++++++---- ping/ping_common.c | 18 ++++++++++++++++++ ping/ping_common.h | 3 +++ 8 files changed, 87 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index a7ec4c7..f659668 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2009-10-04 Rakesh Pandit <[email protected]> + + * ping/ping_common.c (ping_check_timeout): New function. + + * ping/ping_common.h (ping_data): Added ping_start_time member. + + * ping/libping.c (ping_init): Set ping_data member ping_start_time + with gettimeofday. + * ping/ping6.c (ping_init): Likewise. + + * ping/ping.c: New option: --timoue, to set timeout before ping + exits. + (parse_opt): Added new entry for 'w' and validity check for + timeout. + (ping_init): Calling ping_check_timeout after each packet sent. + * ping/ping6.c (parse_opt): Likewise. + 2009-10-01 Rakesh Pandit <[email protected]> * talkd/talkd.c [HAVE_CONFIG_H]: Include config.h. diff --git a/NEWS b/NEWS index f256ee3..441c3c0 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,4 @@ -GNU inetutils NEWS -- history of user-visible changes. 2009-06-28 +GNU inetutils NEWS -- history of user-visible changes. 2009-10-05 Copyright (C) 2002, 2004, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. @@ -10,6 +10,9 @@ Please send inetutils bug reports to <[email protected]>. Unreleased Version 1.7: +* New option --timeput/-w for ping and ping6. It allows to set timeout + before ping can exit. + * New logger implementation The `logger' utility has been rewritten from scratch. The new diff --git a/doc/inetutils.texi b/doc/inetutils.texi index ec011ee..ac9b9dc 100644 --- a/doc/inetutils.texi +++ b/doc/inetutils.texi @@ -468,6 +468,12 @@ routes. Many hosts ignore or discard this option. Specifies the number of data bytes to be sent. The default is 56, which translates into 64 @acronym{ICMP} data bytes when combined with the 8 bytes of ICMP header data. + +...@item -w @var{n} +...@itemx --timeo...@var{n} +...@opindex -w +...@opindex --timeout +Specifies the number of seconds before which ping exits. @end table @section Using ping for network fault isolation diff --git a/ping/libping.c b/ping/libping.c index c0d1d65..a1bad35 100644 --- a/ping/libping.c +++ b/ping/libping.c @@ -89,6 +89,8 @@ ping_init (int type, int ident) /* Make sure we use only 16 bits in this field, id for icmp is a u_short. */ p->ping_ident = ident & 0xFFFF; p->ping_cktab_size = PING_CKTABSIZE; + /* Notes down start time, required for timeout. */ + gettimeofday (&p->ping_start_time, NULL); return p; } diff --git a/ping/ping.c b/ping/ping.c index b83332f..e2eef29 100644 --- a/ping/ping.c +++ b/ping/ping.c @@ -67,6 +67,7 @@ size_t interval; size_t data_length = PING_DATALEN; unsigned options; unsigned long preload = 0; +int ping_timeout = TIMEOUT_UNSPECIFIED; int (*ping_type) (char *hostname) = ping_echo; int (*decode_type (const char *arg)) (char *hostname); @@ -113,6 +114,7 @@ static struct argp_option argp_options[] = { {"ignore-routing", 'r', NULL, 0, "send directly to a host on an attached " "network", GRP+1}, {"verbose", 'v', NULL, 0, "verbose output", GRP+1}, + {"timeout", 'w', "N", 0, "stop after N seconds", GRP+1}, #undef GRP #define GRP 20 {NULL, 0, NULL, 0, "Options valid for --echo requests:", GRP}, @@ -176,6 +178,12 @@ parse_opt (int key, char *arg, struct argp_state *state) options |= OPT_QUIET; break; + case 'w': + ping_timeout = ping_cvt_number (arg, 0, 0); + if (ping_timeout > INT_MAX) + error (EXIT_FAILURE, 0, "invalid timeout value (%s)", arg); + break; + case 'R': options |= OPT_RROUTE; break; @@ -370,11 +378,14 @@ ping_run (PING * ping, int (*finish) ()) { if (ping_recv (ping) == 0) nresp++; + + gettimeofday (&now, NULL); if (t == 0) - { - gettimeofday (&now, NULL); - t = &now; - } + t = &now; + + if (ping_check_timeout(&ping->ping_start_time, ping_timeout)) + break; + if (ping->ping_count && nresp >= ping->ping_count) break; } @@ -385,6 +396,9 @@ ping_run (PING * ping, int (*finish) ()) send_echo (ping); if (!(options & OPT_QUIET) && options & OPT_FLOOD) putchar ('.'); + + if (ping_check_timeout(&ping->ping_start_time, ping_timeout)) + break; } else if (finishing) break; diff --git a/ping/ping6.c b/ping/ping6.c index 6a7fbff..cc7bdd3 100644 --- a/ping/ping6.c +++ b/ping/ping6.c @@ -54,6 +54,7 @@ size_t data_length = PING_DATALEN; size_t count = DEFAULT_PING_COUNT; size_t interval; int socket_type; +int ping_timeout = TIMEOUT_UNSPECIFIED; static unsigned int options; static unsigned long preload = 0; @@ -80,6 +81,7 @@ static struct argp_option argp_options[] = { {"numeric", 'n', NULL, 0, "so not resolve host addresses", GRP+1}, {"ignore-routing", 'r', NULL, 0, "send directly to a host on an attached " "network", GRP+1}, + {"timeout", 'w', "N", 0, "stop after N seconds", GRP+1}, #undef GRP #define GRP 10 {NULL, 0, NULL, 0, "Options valid for --echo requests:", GRP}, @@ -151,6 +153,12 @@ parse_opt (int key, char *arg, struct argp_state *state) socket_type = SO_DEBUG; break; + case 'w': + ping_timeout = ping_cvt_number (arg, 0, 0); + if (ping_timeout > INT_MAX) + error (EXIT_FAILURE, 0, "invalid timeout value (%s)", arg); + break; + case 's': data_length = ping_cvt_number (arg, PING_MAX_DATALEN, 1); break; @@ -326,11 +334,14 @@ ping_run (PING * ping, int (*finish) ()) { if (ping_recv (ping) == 0) nresp++; + + gettimeofday (&now, NULL); if (t == 0) - { - gettimeofday (&now, NULL); - t = &now; - } + t = &now; + + if (ping_check_timeout(&ping->ping_start_time, ping_timeout)) + break; + if (ping->ping_count && nresp >= ping->ping_count) break; } @@ -343,6 +354,9 @@ ping_run (PING * ping, int (*finish) ()) { putchar ('.'); } + + if (ping_check_timeout(&ping->ping_start_time, ping_timeout)) + break; } else if (finishing) break; @@ -741,6 +755,7 @@ ping_init (int type, int ident) /* Make sure we use only 16 bits in this field, id for icmp is a u_short. */ p->ping_ident = ident & 0xFFFF; p->ping_cktab_size = PING_CKTABSIZE; + gettimeofday (&p->ping_start_time, NULL); return p; } diff --git a/ping/ping_common.c b/ping/ping_common.c index 67c4cad..56d363c 100644 --- a/ping/ping_common.c +++ b/ping/ping_common.c @@ -216,3 +216,21 @@ ping_unset_data (PING * p) _ping_freebuf (p); } +/* + * ping_check_timeout -- + * Check whether timeout has expired already. + */ +int +ping_check_timeout (struct timeval *ptr_start_time, int ping_timeout) +{ + struct timeval now; + gettimeofday (&now, NULL); + if (ping_timeout != TIMEOUT_UNSPECIFIED) + { + tvsub(&now, ptr_start_time); + /* now here has difference */ + if (now.tv_sec >= ping_timeout) + return 1; + } + return 0; +} diff --git a/ping/ping_common.h b/ping/ping_common.h index 62cc77b..8404efa 100644 --- a/ping/ping_common.h +++ b/ping/ping_common.h @@ -53,6 +53,7 @@ struct ping_stat /* The rationale for not exiting after a sending N packets is that we want to follow the traditional behaviour of ping. */ #define DEFAULT_PING_COUNT 0 +#define TIMEOUT_UNSPECIFIED -1 #define PING_TIMING(s) (s >= PING_HEADER_LEN) #define PING_HEADER_LEN sizeof (struct timeval) @@ -97,6 +98,7 @@ struct ping_data int ping_fd; /* Raw socket descriptor */ int ping_type; /* Type of packets to send */ size_t ping_count; /* Number of packets to send */ + struct timeval ping_start_time; /* Start time */ size_t ping_interval; /* Number of seconds to wait between sending pkts */ union ping_address ping_dest;/* whom to ping */ char *ping_hostname; /* Printable hostname */ @@ -156,4 +158,5 @@ void ping_set_count (PING * ping, size_t count); void ping_set_sockopt (PING * ping, int opt, void *val, int valsize); void ping_set_interval (PING * ping, size_t interval); void ping_unset_data (PING * p); +int ping_check_timeout(struct timeval *ptr_start_time, int ping_timeout); -- 1.6.4.4
