diff --git a/src/oping.c b/src/oping.c
index cc115f27c4c07437595084773b65d9689eeca983..d4469a8f94f4744a4f5e77c8e40f62c44354b39a 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_min;
double latency_max;
double latency_total;
+ 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)
{
ret->latency_min = -1.0;
ret->latency_max = -1.0;
ret->latency_total = 0.0;
+ ret->latency_total_square = 0.0;
return (ret);
}
while (1)
{
- optchar = getopt (argc, argv, "46c:i:");
+ 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:
usage_exit (argv[0]);
}
void print_host (pingobj_iter_t *iter)
{
- double latency;
- uint16_t sequence;
- size_t buffer_len;
+ double latency;
+ unsigned int sequence;
+ size_t buffer_len;
+ size_t data_len;
ping_context_t *context;
buffer_len = sizeof (latency);
ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
&sequence, &buffer_len);
+ data_len = 0;
+ ping_iterator_get_info (iter, PING_INFO_DATA,
+ NULL, &data_len);
+
context = (ping_context_t *) ping_iterator_get_context (iter);
context->req_sent++;
{
context->req_rcvd++;
context->latency_total += latency;
+ context->latency_total_square += (latency * latency);
if ((context->latency_max < 0.0) || (context->latency_max < latency))
context->latency_max = latency;
if ((context->latency_min < 0.0) || (context->latency_min > latency))
context->latency_min = latency;
- printf ("echo reply from %s (%s): icmp_seq=%u time=%.1f ms\n",
+ printf ("%u bytes from %s (%s): icmp_seq=%u time=%.2f ms\n",
+ (unsigned int) data_len,
context->host, context->addr,
(unsigned int) sequence, latency);
}
int optind;
int i;
+ optind = read_options (argc, argv);
+
+ if (optind >= argc)
+ usage_exit (argv[0]);
+
if (geteuid () != 0)
{
fprintf (stderr, "Need superuser privileges to open a RAW socket. Sorry.\n");
return (1);
}
- optind = read_options (argc, argv);
-
- if (optind >= argc)
- usage_exit (argv[0]);
-
if ((ping = ping_construct ()) == NULL)
{
fprintf (stderr, "ping_construct failed\n");
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;
}
}
+ 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);
}
context = ping_iterator_get_context (iter);
printf ("\n--- %s ping statistics ---\n"
- "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n"
- "rtt min/avg/max/mdev = %.3f/%.3f/%.3f/%.3f ms\n",
+ "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n",
context->host, context->req_sent, context->req_rcvd,
100.0 * (context->req_sent - context->req_rcvd) / ((double) context->req_sent),
- context->latency_total,
- context->latency_min,
- context->latency_total / ((double) context->req_rcvd),
- context->latency_max,
- 0.00);
+ context->latency_total);
+
+ if (context->req_rcvd != 0)
+ {
+ double num_total;
+ double average;
+ double deviation;
+
+ num_total = (double) context->req_rcvd;
+
+ average = context->latency_total / num_total;
+ deviation = sqrt (((num_total * context->latency_total_square) - (context->latency_total * context->latency_total))
+ / (num_total * (num_total - 1.0)));
+
+ printf ("rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms\n",
+ context->latency_min,
+ average,
+ context->latency_max,
+ deviation);
+ }
ping_iterator_set_context (iter, NULL);
free (context);