2009/10/5 Rakesh Pandit wrote: > 2009/10/5 Alfred M. Szmidt wrote: >> May you suggest something better then TIMEOUT_UNSPECIFIED ? I cannot >> think of anything more suitable then this. >> >> I don't know why you need that macro, just define the variables to -1. >> Please call the variable timeout, not ping_timeout. > > As I mentioned it confuses with private variable timeout in ping_run. > Do you consider it a good option to change that private variable? >
If you can suggest something better here I will change the name accordingly. But as there is a conflict I have kept it ping_timeout, which seemed apt to me. -- Rakesh Pandit https://fedoraproject.org/ freedom, friends, features, first >From bb9f8f33dca576e1c812f7293b29b511d4c0007c Mon Sep 17 00:00:00 2001 From: Rakesh Pandit <[email protected]> Date: Tue, 6 Oct 2009 11:23:25 +0530 Subject: [PATCH] Added new option --timeout to ping/ping6 for setting timeout before ping exits. --- ChangeLog | 23 +++++++++++++++++++++++ NEWS | 5 ++++- doc/inetutils.texi | 6 ++++++ ping/libping.c | 2 ++ ping/ping.c | 15 +++++++++++++++ ping/ping6.c | 17 +++++++++++++++++ ping/ping_common.c | 18 ++++++++++++++++++ ping/ping_common.h | 2 ++ 8 files changed, 87 insertions(+), 1 deletions(-) diff --git a/ChangeLog b/ChangeLog index a7ec4c7..31aa687 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,26 @@ +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: --timeout, to set timeout before ping + exits. + (parse_opt): Added new entry for 'w' and validity check for + timeout. + * ping/ping6.c (parse_opt): Likewise. + + * ping/ping.c (ping_init): Calling ping_check_timeout after each + packet sent. + * ping/ping6.c (parse_init): Likewise. + + * doc/inetutils.texi: Added timeout option details. + * NEWS: Updated about new timeout option. + 2009-10-01 Rakesh Pandit <[email protected]> * talkd/talkd.c [HAVE_CONFIG_H]: Include config.h. diff --git a/NEWS b/NEWS index f256ee3..03cb28b 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-06 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..f93d78c 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 = -1; 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; @@ -375,6 +383,10 @@ ping_run (PING * ping, int (*finish) ()) gettimeofday (&now, NULL); t = &now; } + + if (ping_timeout_p(&ping->ping_start_time, ping_timeout)) + break; + if (ping->ping_count && nresp >= ping->ping_count) break; } @@ -385,6 +397,9 @@ ping_run (PING * ping, int (*finish) ()) send_echo (ping); if (!(options & OPT_QUIET) && options & OPT_FLOOD) putchar ('.'); + + if (ping_timeout_p(&ping->ping_start_time, ping_timeout)) + break; } else if (finishing) break; diff --git a/ping/ping6.c b/ping/ping6.c index 6a7fbff..0668042 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 = -1; 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; @@ -331,6 +339,10 @@ ping_run (PING * ping, int (*finish) ()) gettimeofday (&now, NULL); t = &now; } + + if (ping_timeout_p(&ping->ping_start_time, ping_timeout)) + break; + if (ping->ping_count && nresp >= ping->ping_count) break; } @@ -343,6 +355,9 @@ ping_run (PING * ping, int (*finish) ()) { putchar ('.'); } + + if (ping_timeout_p(&ping->ping_start_time, ping_timeout)) + break; } else if (finishing) break; @@ -741,6 +756,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_common.c b/ping/ping_common.c index 67c4cad..a942fec 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_timeout_p (struct timeval *ptr_start_time, int ping_timeout) +{ + struct timeval now; + gettimeofday (&now, NULL); + if (ping_timeout != -1) + { + 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..3da294d 100644 --- a/ping/ping_common.h +++ b/ping/ping_common.h @@ -97,6 +97,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 +157,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_timeout_p (struct timeval *ptr_start_time, int ping_timeout); -- 1.6.4.4
