diff --git a/src/oping.c b/src/oping.c
index eb97e5d28d65957230d71ecfdb9cfbeb628b6fcd..3201e85dc0f8808268f467d4337ae9a137d7ee64 100644 (file)
--- a/src/oping.c
+++ b/src/oping.c
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * the Free Software Foundation; only version 2 of the License is
+ * applicable.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
double latency_total_square;
} ping_context_t;
-static double opt_interval = 1.0;
-static int opt_addrfamily = PING_DEF_AF;
-static int opt_count = -1;
+static double opt_interval = 1.0;
+static int opt_addrfamily = PING_DEF_AF;
+static char *opt_srcaddr = NULL;
+static int opt_count = -1;
void sigint_handler (int signal)
{
while (1)
{
- optchar = getopt (argc, argv, "46c:hi:");
+ optchar = getopt (argc, argv, "46c:hi:I:");
if (optchar == -1)
break;
opt_interval = new_interval;
}
break;
+ case 'I':
+ {
+ if (opt_srcaddr != NULL)
+ free (opt_srcaddr);
+ opt_srcaddr = strdup (optarg);
+ }
+ break;
case 'h':
default:
if (opt_addrfamily != PING_DEF_AF)
ping_setopt (ping, PING_OPT_AF, (void *) &opt_addrfamily);
+ if (opt_srcaddr != NULL)
+ {
+ if (ping_setopt (ping, PING_OPT_SOURCE, (void *) opt_srcaddr) != 0)
+ {
+ fprintf (stderr, "Setting source address failed: %s\n",
+ ping_get_error (ping));
+ }
+ }
+
for (i = optind; i < argc; i++)
{
- if (ping_host_add (ping, argv[i]) > 0)
+ if (ping_host_add (ping, argv[i]) < 0)
{
- fprintf (stderr, "ping_host_add (%s) failed\n", argv[i]);
+ const char *errmsg = ping_get_error (ping);
+
+ fprintf (stderr, "Adding host `%s' failed: %s\n", argv[i], errmsg);
continue;
}
}
+ /* Drop root privileges if we're setuid-root. */
+ setuid (getuid ());
+
+ i = 0;
for (iter = ping_iterator_get (ping);
iter != NULL;
iter = ping_iterator_next (iter))
buffer_size = sizeof (context->addr);
ping_iterator_get_info (iter, PING_INFO_ADDRESS, context->addr, &buffer_size);
+ buffer_size = 0;
+ ping_iterator_get_info (iter, PING_INFO_DATA, NULL, &buffer_size);
+
+ printf ("PING %s (%s) %u bytes of data.\n",
+ context->host, context->addr, (unsigned int) buffer_size);
+
ping_iterator_set_context (iter, (void *) context);
+
+ i++;
}
+ if (i == 0)
+ return (1);
+
memset (&sigint_action, '\0', sizeof (sigint_action));
sigint_action.sa_handler = sigint_handler;
if (sigaction (SIGINT, &sigint_action, NULL) < 0)
if (ping_send (ping) < 0)
{
- fprintf (stderr, "ping_send failed\n");
+ fprintf (stderr, "ping_send failed: %s\n",
+ ping_get_error (ping));
return (1);
}