2 * Object oriented C module to send ICMP and ICMPv6 `echo's.
3 * Copyright (C) 2006 Florian octo Forster <octo at verplant.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 # error "You don't have the standard C99 header files installed"
32 #endif /* STDC_HEADERS */
38 #if TIME_WITH_SYS_TIME
39 # include <sys/time.h>
43 # include <sys/time.h>
50 # include <netdb.h> /* NI_MAXHOST */
59 typedef struct ping_context
61 char host[NI_MAXHOST];
62 char addr[NI_MAXHOST];
70 double latency_total_square;
73 static double opt_interval = 1.0;
74 static int opt_addrfamily = PING_DEF_AF;
75 static char *opt_srcaddr = NULL;
76 static int opt_count = -1;
78 void sigint_handler (int signal)
84 ping_context_t *context_create (void)
88 if ((ret = malloc (sizeof (ping_context_t))) == NULL)
91 memset (ret, '\0', sizeof (ping_context_t));
93 ret->latency_min = -1.0;
94 ret->latency_max = -1.0;
95 ret->latency_total = 0.0;
96 ret->latency_total_square = 0.0;
101 void context_destroy (ping_context_t *context)
106 void usage_exit (const char *name)
108 fprintf (stderr, "Usage: %s [-46] [-c count] [-i interval] host [host [host ...]]\n",
113 int read_options (int argc, char **argv)
119 optchar = getopt (argc, argv, "46c:hi:I:");
128 opt_addrfamily = (optchar == '4') ? AF_INET : AF_INET6;
134 new_count = atoi (optarg);
136 opt_count = new_count;
143 new_interval = atof (optarg);
144 if (new_interval >= 0.2)
145 opt_interval = new_interval;
150 if (opt_srcaddr != NULL)
152 opt_srcaddr = strdup (optarg);
158 usage_exit (argv[0]);
165 void print_host (pingobj_iter_t *iter)
168 unsigned int sequence;
171 ping_context_t *context;
173 buffer_len = sizeof (latency);
174 ping_iterator_get_info (iter, PING_INFO_LATENCY,
175 &latency, &buffer_len);
177 buffer_len = sizeof (sequence);
178 ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
179 &sequence, &buffer_len);
182 ping_iterator_get_info (iter, PING_INFO_DATA,
185 context = (ping_context_t *) ping_iterator_get_context (iter);
191 context->latency_total += latency;
192 context->latency_total_square += (latency * latency);
194 if ((context->latency_max < 0.0) || (context->latency_max < latency))
195 context->latency_max = latency;
196 if ((context->latency_min < 0.0) || (context->latency_min > latency))
197 context->latency_min = latency;
199 printf ("%u bytes from %s (%s): icmp_seq=%u time=%.2f ms\n",
200 (unsigned int) data_len,
201 context->host, context->addr,
202 (unsigned int) sequence, latency);
206 printf ("echo reply from %s (%s): icmp_seq=%u timeout\n",
207 context->host, context->addr,
208 (unsigned int) sequence);
212 void time_normalize (struct timespec *ts)
214 while (ts->tv_nsec < 0)
223 ts->tv_nsec += 1000000000;
226 while (ts->tv_nsec >= 1000000000)
229 ts->tv_nsec -= 1000000000;
233 void time_calc (struct timespec *ts_dest,
234 const struct timespec *ts_int,
235 const struct timeval *tv_begin,
236 const struct timeval *tv_end)
238 ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
239 ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
240 time_normalize (ts_dest);
242 /* Assure that `(begin + interval) > end'.
243 * This may seem overly complicated, but `tv_sec' is of type `time_t'
244 * which may be `unsigned. *sigh* */
245 if ((tv_end->tv_sec > ts_dest->tv_sec)
246 || ((tv_end->tv_sec == ts_dest->tv_sec)
247 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
250 ts_dest->tv_nsec = 0;
254 ts_dest->tv_sec = ts_dest->tv_sec - tv_end->tv_sec;
255 ts_dest->tv_nsec = ts_dest->tv_nsec - (tv_end->tv_usec * 1000);
256 time_normalize (ts_dest);
259 int main (int argc, char **argv)
262 pingobj_iter_t *iter;
264 struct sigaction sigint_action;
266 struct timeval tv_begin;
267 struct timeval tv_end;
268 struct timespec ts_wait;
269 struct timespec ts_int;
274 optind = read_options (argc, argv);
277 usage_exit (argv[0]);
281 fprintf (stderr, "Need superuser privileges to open a RAW socket. Sorry.\n");
285 if ((ping = ping_construct ()) == NULL)
287 fprintf (stderr, "ping_construct failed\n");
295 temp_nsec = modf (opt_interval, &temp_sec);
296 ts_int.tv_sec = (time_t) temp_sec;
297 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
299 /* printf ("ts_int = %i.%09li\n", (int) ts_int.tv_sec, ts_int.tv_nsec); */
302 if (opt_addrfamily != PING_DEF_AF)
303 ping_setopt (ping, PING_OPT_AF, (void *) &opt_addrfamily);
305 if (opt_srcaddr != NULL)
307 if (ping_setopt (ping, PING_OPT_SOURCE, (void *) opt_srcaddr) != 0)
309 fprintf (stderr, "Setting source address failed: %s\n",
310 ping_get_error (ping));
314 for (i = optind; i < argc; i++)
316 if (ping_host_add (ping, argv[i]) < 0)
318 const char *errmsg = ping_get_error (ping);
320 fprintf (stderr, "Adding host `%s' failed: %s\n", argv[i], errmsg);
326 for (iter = ping_iterator_get (ping);
328 iter = ping_iterator_next (iter))
330 ping_context_t *context;
333 context = context_create ();
335 buffer_size = sizeof (context->host);
336 ping_iterator_get_info (iter, PING_INFO_HOSTNAME, context->host, &buffer_size);
338 buffer_size = sizeof (context->addr);
339 ping_iterator_get_info (iter, PING_INFO_ADDRESS, context->addr, &buffer_size);
342 ping_iterator_get_info (iter, PING_INFO_DATA, NULL, &buffer_size);
344 printf ("PING %s (%s) %u bytes of data.\n",
345 context->host, context->addr, (unsigned int) buffer_size);
347 ping_iterator_set_context (iter, (void *) context);
355 memset (&sigint_action, '\0', sizeof (sigint_action));
356 sigint_action.sa_handler = sigint_handler;
357 if (sigaction (SIGINT, &sigint_action, NULL) < 0)
359 perror ("sigaction");
363 while (opt_count != 0)
367 if (gettimeofday (&tv_begin, NULL) < 0)
369 perror ("gettimeofday");
373 if (ping_send (ping) < 0)
375 fprintf (stderr, "ping_send failed\n");
379 for (iter = ping_iterator_get (ping);
381 iter = ping_iterator_next (iter))
387 /* Don't sleep in the last iteration */
391 if (gettimeofday (&tv_end, NULL) < 0)
393 perror ("gettimeofday");
397 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
399 /* printf ("Sleeping for %i.%09li seconds\n", (int) ts_wait.tv_sec, ts_wait.tv_nsec); */
400 while ((status = nanosleep (&ts_wait, &ts_wait)) != 0)
404 perror ("nanosleep");
407 else if (opt_count == 0)
416 } /* while (opt_count != 0) */
418 for (iter = ping_iterator_get (ping);
420 iter = ping_iterator_next (iter))
422 ping_context_t *context;
424 context = ping_iterator_get_context (iter);
426 printf ("\n--- %s ping statistics ---\n"
427 "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n",
428 context->host, context->req_sent, context->req_rcvd,
429 100.0 * (context->req_sent - context->req_rcvd) / ((double) context->req_sent),
430 context->latency_total);
432 if (context->req_rcvd != 0)
438 num_total = (double) context->req_rcvd;
440 average = context->latency_total / num_total;
441 deviation = sqrt (((num_total * context->latency_total_square) - (context->latency_total * context->latency_total))
442 / (num_total * (num_total - 1.0)));
444 printf ("rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms\n",
445 context->latency_min,
447 context->latency_max,
451 ping_iterator_set_context (iter, NULL);