[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-inetutils] ping
From: |
Debarshi Ray |
Subject: |
Re: [bug-inetutils] ping |
Date: |
Thu, 5 Jun 2008 17:04:07 +0530 |
I have updated the patch
(http://rishi.fedorapeople.org/gnu/ping-multiple.diff and below) to
include multiple host support for ping6 as well.
diff -urNp inetutils/ping/ping.c inetutils-build/ping/ping.c
--- inetutils/ping/ping.c 2007-12-10 21:17:19.000000000 +0530
+++ inetutils-build/ping/ping.c 2008-06-05 15:32:16.000000000 +0530
@@ -54,10 +54,10 @@
#include "ping_impl.h"
#include "libinetutils.h"
-extern int ping_echo (int argc, char **argv);
-extern int ping_timestamp (int argc, char **argv);
-extern int ping_address (int argc, char **argv);
-extern int ping_router (int argc, char **argv);
+extern int ping_echo (char *hostname);
+extern int ping_timestamp (char *hostname);
+extern int ping_address (char *hostname);
+extern int ping_router (char *hostname);
PING *ping;
bool is_root = false;
@@ -70,9 +70,9 @@ size_t interval;
size_t data_length = PING_DATALEN;
unsigned options;
unsigned long preload = 0;
-int (*ping_type) (int argc, char **argv) = ping_echo;
+int (*ping_type) (char *hostname) = ping_echo;
-int (*decode_type (const char *arg)) (int argc, char **argv);
+int (*decode_type (const char *arg)) (char *hostname);
static int send_echo (PING * ping);
#define MIN_USER_INTERVAL (200000/PING_PRECISION)
@@ -232,7 +232,7 @@ main (int argc, char **argv)
{
int index;
int one = 1;
- int status;
+ int status = 0;
if (getuid () == 0)
is_root = true;
@@ -264,15 +264,20 @@ main (int argc, char **argv)
init_data_buffer (patptr, pattern_len);
- status = (*(ping_type)) (argc, argv);
+ while (argc--)
+ {
+ status |= (*(ping_type)) (*argv++);
+ ping_reset (ping);
+ }
+
free (ping);
free (data_buffer);
return status;
}
-int (*decode_type (const char *arg)) (int argc, char **argv)
+int (*decode_type (const char *arg)) (char *hostname)
{
- int (*ping_type) (int argc, char **argv);
+ int (*ping_type) (char *hostname);
if (strcasecmp (arg, "echo") == 0)
ping_type = ping_echo;
@@ -308,12 +313,13 @@ ping_run (PING * ping, int (*finish) ())
struct timeval *t = NULL;
int finishing = 0;
int nresp = 0;
+ int i;
signal (SIGINT, sig_int);
fdmax = ping->ping_fd + 1;
- while (preload--)
+ for (i = 0; i < preload; i++)
send_echo (ping);
if (options & OPT_FLOOD)
@@ -390,8 +396,7 @@ ping_run (PING * ping, int (*finish) ())
}
}
- free (ping->ping_buffer);
- free (ping->ping_cktab);
+ ping_unset_data (ping);
if (finish)
return (*finish) ();
diff -urNp inetutils/ping/ping_address.c inetutils-build/ping/ping_address.c
--- inetutils/ping/ping_address.c 2007-12-10 21:17:19.000000000 +0530
+++ inetutils-build/ping/ping_address.c 2008-06-05 01:27:45.000000000 +0530
@@ -63,14 +63,14 @@ static void print_address (int dupflag,
static int address_finish ();
int
-ping_address (int argc, char **argv)
+ping_address (char *hostname)
{
ping_set_type (ping, ICMP_ADDRESS);
ping_set_event_handler (ping, recv_address, NULL);
ping_set_packetsize (ping, 12); /* FIXME: constant */
ping_set_count (ping, 1);
- if (ping_set_dest (ping, *argv))
+ if (ping_set_dest (ping, hostname))
error (EXIT_FAILURE, 0, "unknown host");
printf ("PING %s (%s): sending address mask request\n",
diff -urNp inetutils/ping/ping_echo.c inetutils-build/ping/ping_echo.c
--- inetutils/ping/ping_echo.c 2007-12-10 21:17:19.000000000 +0530
+++ inetutils-build/ping/ping_echo.c 2008-06-05 01:27:07.000000000 +0530
@@ -70,7 +70,7 @@ void print_icmp_header (struct sockaddr_
static void print_ip_opt (struct ip *ip, int hlen);
int
-ping_echo (int argc, char **argv)
+ping_echo (char *hostname)
{
#ifdef IP_OPTIONS
char rspace[3 + 4 * NROUTES + 1]; /* record route space */
@@ -88,7 +88,7 @@ ping_echo (int argc, char **argv)
ping_set_packetsize (ping, data_length);
ping_set_event_handler (ping, handler, &ping_stat);
- if (ping_set_dest (ping, *argv))
+ if (ping_set_dest (ping, hostname))
error (EXIT_FAILURE, 0, "unknown host");
if (options & OPT_RROUTE)
diff -urNp inetutils/ping/ping_router.c inetutils-build/ping/ping_router.c
--- inetutils/ping/ping_router.c 2007-12-10 21:17:19.000000000 +0530
+++ inetutils-build/ping/ping_router.c 2008-06-05 01:28:22.000000000 +0530
@@ -49,7 +49,7 @@
#include <ping_impl.h>
int
-ping_router (int argc, char **argv)
+ping_router (char *hostname)
{
return 0;
}
diff -urNp inetutils/ping/ping_timestamp.c inetutils-build/ping/ping_timestamp.c
--- inetutils/ping/ping_timestamp.c 2007-12-10 21:17:19.000000000 +0530
+++ inetutils-build/ping/ping_timestamp.c 2008-06-05 01:28:11.000000000
+0530
@@ -60,13 +60,13 @@ static void print_timestamp (int dupflag
static int timestamp_finish ();
int
-ping_timestamp (int argc, char **argv)
+ping_timestamp (char *hostname)
{
ping_set_type (ping, ICMP_TIMESTAMP);
ping_set_event_handler (ping, recv_timestamp, NULL);
ping_set_packetsize (ping, 20);
- if (ping_set_dest (ping, *argv))
+ if (ping_set_dest (ping, hostname))
error (EXIT_FAILURE, 0, "unknown host");
printf ("PING %s (%s): sending timestamp requests\n",
diff -urNp inetutils/ping/ping6.c inetutils-build/ping/ping6.c
--- inetutils/ping/ping6.c 2008-06-03 23:52:39.000000000 +0530
+++ inetutils-build/ping/ping6.c 2008-06-05 16:19:51.000000000 +0530
@@ -56,8 +56,8 @@ size_t data_length = PING_DATALEN;
static unsigned int options;
static unsigned long preload = 0;
-static int ping_echo (int argc, char **argv);
-
+static int ping_echo (char *hostname);
+static void ping_reset (PING * p);
static int send_echo (PING * ping);
ARGP_PROGRAM_DATA ("ping6", "2007", "Jeroen Dekkers");
@@ -167,6 +167,7 @@ int
main (int argc, char **argv)
{
int index;
+ int status = 0;
if (getuid () == 0)
is_root = true;
@@ -189,7 +190,13 @@ main (int argc, char **argv)
init_data_buffer (patptr, pattern_len);
- return ping_echo (argc, argv);
+ while (argc--)
+ {
+ status |= ping_echo (*argv++);
+ ping_reset (ping);
+ }
+
+ return status;
}
static char *
@@ -248,12 +255,13 @@ ping_run (PING * ping, int (*finish) ())
struct timeval *t = NULL;
int finishing = 0;
int nresp = 0;
+ int i;
signal (SIGINT, sig_int);
fdmax = ping->ping_fd + 1;
- while (preload--)
+ for (i = 0; i < preload; i++)
send_echo (ping);
if (options & OPT_FLOOD)
@@ -390,11 +398,12 @@ static void print_icmp_error (struct soc
static int echo_finish (void);
static int
-ping_echo (int argc, char **argv)
+ping_echo (char *hostname)
{
int err;
char buffer[256];
struct ping_stat ping_stat;
+ int status;
if (options & OPT_FLOOD && options & OPT_INTERVAL)
error (EXIT_FAILURE, 0, "-f and -i incompatible options");
@@ -405,8 +414,8 @@ ping_echo (int argc, char **argv)
ping->ping_datalen = data_length;
ping->ping_closure = &ping_stat;
- if (ping_set_dest (ping, *argv))
- error (EXIT_FAILURE, 0, "unknown host %s", *argv);
+ if (ping_set_dest (ping, hostname))
+ error (EXIT_FAILURE, 0, "unknown host %s", hostname);
err = getnameinfo ((struct sockaddr *) &ping->ping_dest,
sizeof (ping->ping_dest), buffer,
@@ -426,7 +435,17 @@ ping_echo (int argc, char **argv)
printf ("PING %s (%s): %d data bytes\n",
ping->ping_hostname, buffer, data_length);
- return ping_run (ping, echo_finish);
+ status = ping_run (ping, echo_finish);
+ free (ping->ping_hostname);
+ return status;
+}
+
+static void
+ping_reset (PING * p)
+{
+ p->ping_num_xmit = 0;
+ p->ping_num_recv = 0;
+ p->ping_num_rept = 0;
}
static int
@@ -645,7 +664,7 @@ echo_finish ()
printf ("round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n",
ping_stat->tmin, avg, ping_stat->tmax, nsqrt (vari, 0.0005));
}
- exit (ping->ping_num_recv == 0);
+ return (ping->ping_num_recv == 0);
}
static PING *
diff -urNp inetutils/libicmp/libping.c inetutils-build/libicmp/libping.c
--- inetutils/libicmp/libping.c 2007-12-10 21:17:18.000000000 +0530
+++ inetutils-build/libicmp/libping.c 2008-06-05 00:10:27.000000000 +0530
@@ -99,6 +99,14 @@ ping_init (int type, int ident)
}
void
+ping_reset (PING * p)
+{
+ p->ping_num_xmit = 0;
+ p->ping_num_recv = 0;
+ p->ping_num_rept = 0;
+}
+
+void
ping_set_type (PING * p, int type)
{
p->ping_type = type;
@@ -119,6 +127,11 @@ _ping_freebuf (PING * p)
free (p->ping_buffer);
p->ping_buffer = NULL;
}
+ if (p->ping_cktab)
+ {
+ free (p->ping_cktab);
+ p->ping_cktab = NULL;
+ }
}
int
@@ -154,6 +167,12 @@ ping_set_data (PING * p, void *data, siz
return 0;
}
+void
+ping_unset_data (PING * p)
+{
+ _ping_freebuf (p);
+}
+
int
ping_xmit (PING * p)
{
diff -urNp inetutils/libicmp/ping.h inetutils-build/libicmp/ping.h
--- inetutils/libicmp/ping.h 2007-12-10 21:17:18.000000000 +0530
+++ inetutils-build/libicmp/ping.h 2008-06-05 00:10:27.000000000 +0530
@@ -72,6 +72,7 @@ struct ping_data
#define _PING_TST(p,bit) (_C_BIT (p,bit) & _C_MASK (bit))
PING *ping_init (int type, int ident);
+void ping_reset (PING * p);
void ping_set_type (PING * p, int type);
void ping_set_count (PING * ping, size_t count);
void ping_set_sockopt (PING * ping, int opt, void *val, int valsize);
@@ -82,5 +83,6 @@ int ping_set_pattern (PING * p, int len,
void ping_set_event_handler (PING * ping, ping_efp fp, void *closure);
int ping_set_data (PING * p, void *data, size_t off, size_t len);
void ping_set_datalen (PING * p, size_t len);
+void ping_unset_data (PING * p);
int ping_recv (PING * p);
int ping_xmit (PING * p);
diff -urNp inetutils/ChangeLog inetutils-build/ChangeLog
--- inetutils/ChangeLog 2008-06-03 23:52:38.000000000 +0530
+++ inetutils-build/ChangeLog 2008-06-05 16:52:45.000000000 +0530
@@ -1,3 +1,39 @@
+2008-06-05 Debarshi Ray <address@hidden>
+
+ * libicmp/libping.c (ping_reset): New function.
+ (_ping_freebuf): Free PING->ping_cktab.
+ (ping_unset_data): New function.
+
+ * libicmp/ping.h (ping_reset): New function.
+ (ping_unset_data): New function.
+
+ * ping/ping.c (ping_type): Changed type to `int (*ping_type) (char
+ *hostname)'. All users changed accordingly.
+ (decode_type): Likewise.
+ (main): Multiple host support implemented.
+ (ping_run): Added new variable I. Replaced `while' with `for' to
+ avoid decrementing PRELOAD. Replaced free with ping_unset_data.
+
+ * ping/ping6.c (main): Added new variable STATUS. Multiple host
+ support implemented.
+ (ping_run): Added new variable I. Replaced `while' with `for' to
+ avoid decrementing PRELOAD.
+ (ping_echo): Changed type to `static int ping_echo (char *hostname)`.
+ All callers and declarations changed accordingly. Added new variable
+ STATUS. Free PING->ping_hostname.
+ (ping_reset): New function.
+ (echo_finish): Call return instead of exit.
+
+ * ping/ping_address.c (ping_address): Changed type to
+ `int ping_address (char *hostname)'. All callers and extern
+ declarations changed accordingly.
+
+ * ping/ping_echo.c (ping_echo): Likewise.
+
+ * ping/ping_router.c (ping_router): Likewise.
+
+ * ping/ping_timestamp.c (ping_timestamp): Likewise.
+
2008-05-20 Debarshi Ray <address@hidden>
* ftp/cmds.c, ftp/ftp.c, ftp/main.c, ftpd/ftpd.c, ftpd/popen.c,
However, ping6 suffers from segmentations faults when run with -c, -i,
and -r. This is due to the use of ping before it is initialized by
ping_init. And while fixing this, I would also like to factor out the
code common to both ping and ping6. What do you think?
Happy hacking,
Debarshi