From 74c00388bb0ae97402ecb53842a3378246975025 Mon Sep 17 00:00:00 2001
From: Rakesh Pandit <rakesh@fedoraproject.org>
Date: Sun, 4 Oct 2009 03:07:14 +0530
Subject: [PATCH 2/2] Added new option --deadline to set timeout before ping exits.

---
 ChangeLog          |   18 ++++++++++++++++++
 ping/libping.c     |    1 +
 ping/ping.c        |   21 +++++++++++++++++++--
 ping/ping6.c       |   22 ++++++++++++++++++++--
 ping/ping_common.h |    2 ++
 5 files changed, 60 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index a7ec4c7..ae9cbc0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2009-10-04  Rakesh Pandit <rakesh@fedoraproject.org>
+
+	* 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: --deadline, to set timeout before ping
+	exits.
+	(parse_opt): Added new entry for 'w' and validity check for
+	deadline.
+	* ping/ping6.c (parse_opt): Likewise.
+
+	* ping/ping.c (ping_run): Check if difference between current time
+	and start time has crossed deadline.
+	* ping/ping6.c (ping_run): Likewise.
+	
 2009-10-01  Rakesh Pandit <rakesh@fedoraproject.org>
 
 	* talkd/talkd.c [HAVE_CONFIG_H]: Include config.h.
diff --git a/ping/libping.c b/ping/libping.c
index c0d1d65..7e56d3d 100644
--- a/ping/libping.c
+++ b/ping/libping.c
@@ -89,6 +89,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.c b/ping/ping.c
index b83332f..5ef8c4d 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 deadline = DEFAULT_DEADLINE;
 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},
+  {"deadline", 'w', "NUMBER", 0, "stop after NUMBER 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':
+      deadline = ping_cvt_number (arg, 0, 0);
+      if (deadline > INT_MAX)
+        error (EXIT_FAILURE, 0, "invalid deadline value (%s)", arg);
+      break;
+
     case 'R':
       options |= OPT_RROUTE;
       break;
@@ -338,6 +346,7 @@ ping_run (PING * ping, int (*finish) ())
   while (!stop)
     {
       int n;
+      double interval = 0.0;
 
       FD_ZERO (&fdset);
       FD_SET (ping->ping_fd, &fdset);
@@ -370,11 +379,19 @@ ping_run (PING * ping, int (*finish) ())
 	{
 	  if (ping_recv (ping) == 0)
 	    nresp++;
+
+	  gettimeofday (&now, NULL);
 	  if (t == 0)
+	    t = &now;
+
+	  if (deadline != DEFAULT_DEADLINE)
 	    {
-	      gettimeofday (&now, NULL);
-	      t = &now;
+	      tvsub(&now, &ping->ping_start_time);
+	      /* now here has difference*/
+	      if (now.tv_sec >= deadline)
+		break;
 	    }
+
 	  if (ping->ping_count && nresp >= ping->ping_count)
 	    break;
 	}
diff --git a/ping/ping6.c b/ping/ping6.c
index 6a7fbff..f15190e 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 deadline = DEFAULT_DEADLINE;
 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},
+  {"deadline", 'w', "NUMBER", 0, "stop after NUMBER 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':
+      deadline = ping_cvt_number (arg, 0, 0);
+      if (deadline > INT_MAX)
+        error (EXIT_FAILURE, 0, "invalid deadline value (%s)", arg);
+      break;
+
     case 's':
       data_length = ping_cvt_number (arg, PING_MAX_DATALEN, 1);
       break;
@@ -294,6 +302,7 @@ ping_run (PING * ping, int (*finish) ())
   while (!stop)
     {
       int n;
+      double interval = 0.0;
 
       FD_ZERO (&fdset);
       FD_SET (ping->ping_fd, &fdset);
@@ -326,11 +335,19 @@ ping_run (PING * ping, int (*finish) ())
 	{
 	  if (ping_recv (ping) == 0)
 	    nresp++;
+
+	  gettimeofday (&now, NULL);
 	  if (t == 0)
+	    t = &now;
+
+	  if (deadline != DEFAULT_DEADLINE)
 	    {
-	      gettimeofday (&now, NULL);
-	      t = &now;
+	      tvsub(&now, &ping->ping_start_time);
+	      /* now here has difference*/
+	      if (now.tv_sec >= deadline)
+		break;
 	    }
+
 	  if (ping->ping_count && nresp >= ping->ping_count)
 	    break;
 	}
@@ -741,6 +758,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.h b/ping/ping_common.h
index 62cc77b..e1a3c3e 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 DEFAULT_DEADLINE -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 */
-- 
1.6.4.4

