summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: a353d0d)
raw | patch | inline | side by side (parent: a353d0d)
author | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Tue, 8 Jun 2010 07:27:48 +0000 (09:27 +0200) | ||
committer | Florian Forster <octo@leeloo.lan.home.verplant.org> | |
Tue, 8 Jun 2010 07:27:48 +0000 (09:27 +0200) |
src/oping.c | patch | blob | history |
diff --git a/src/oping.c b/src/oping.c
index d27f187963dc511395c54bad8566f6f2c9d12562..e4584d71cc12b526fca4d87cc2c01a505ced4997 100644 (file)
--- a/src/oping.c
+++ b/src/oping.c
/**
* Object oriented C module to send ICMP and ICMPv6 `echo's.
- * Copyright (C) 2006 Florian octo Forster <octo at verplant.org>
+ * Copyright (C) 2006-2010 Florian octo Forster <octo at verplant.org>
*
* 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
static void context_destroy (ping_context_t *context)
{
+ if (context == NULL)
+ return;
+
free (context);
}
}
}
-static void time_calc (struct timespec *ts_dest,
+static void time_calc (struct timespec *ts_dest, /* {{{ */
const struct timespec *ts_int,
const struct timeval *tv_begin,
const struct timeval *tv_end)
ts_dest->tv_sec = ts_dest->tv_sec - tv_end->tv_sec;
ts_dest->tv_nsec = ts_dest->tv_nsec - (tv_end->tv_usec * 1000);
time_normalize (ts_dest);
-}
+} /* }}} void time_calc */
+
+static int print_header (pingobj_t *ping) /* {{{ */
+{
+ pingobj_iter_t *iter;
+ int i;
+
+ i = 0;
+ for (iter = ping_iterator_get (ping);
+ iter != NULL;
+ iter = ping_iterator_next (iter))
+ {
+ ping_context_t *context;
+ size_t buffer_size;
+
+ context = context_create ();
+
+ buffer_size = sizeof (context->host);
+ ping_iterator_get_info (iter, PING_INFO_HOSTNAME, context->host, &buffer_size);
+
+ 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) %zu bytes of data.\n",
+ context->host, context->addr, buffer_size);
+
+ ping_iterator_set_context (iter, (void *) context);
+
+ i++;
+ }
+
+ return (0);
+} /* }}} int print_header */
+
+static int print_footer (pingobj_t *ping)
+{
+ pingobj_iter_t *iter;
+
+ for (iter = ping_iterator_get (ping);
+ iter != NULL;
+ iter = ping_iterator_next (iter))
+ {
+ ping_context_t *context;
+
+ context = ping_iterator_get_context (iter);
+
+ printf ("\n--- %s ping statistics ---\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);
+
+ 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);
+ context_destroy (context);
+ }
-int main (int argc, char **argv)
+ return (0);
+} /* }}} int print_footer */
+
+int main (int argc, char **argv) /* {{{ */
{
pingobj_t *ping;
pingobj_iter_t *iter;
saved_set_uid = (uid_t) -1;
#endif
- i = 0;
- for (iter = ping_iterator_get (ping);
- iter != NULL;
- iter = ping_iterator_next (iter))
- {
- ping_context_t *context;
- size_t buffer_size;
-
- context = context_create ();
-
- buffer_size = sizeof (context->host);
- ping_iterator_get_info (iter, PING_INFO_HOSTNAME, context->host, &buffer_size);
-
- 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) %zu bytes of data.\n",
- context->host, context->addr, buffer_size);
-
- ping_iterator_set_context (iter, (void *) context);
-
- i++;
- }
+ print_header (ping);
if (i == 0)
return (1);
opt_count--;
} /* while (opt_count != 0) */
- for (iter = ping_iterator_get (ping);
- iter != NULL;
- iter = ping_iterator_next (iter))
- {
- ping_context_t *context;
-
- context = ping_iterator_get_context (iter);
-
- printf ("\n--- %s ping statistics ---\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);
-
- 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);
- context_destroy (context);
- }
+ print_footer (ping);
ping_destroy (ping);
return (0);
-}
+} /* }}} int main */
+
+/* vim: set fdm=marker : */