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; only version 2 of the License is
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 char *opt_device = NULL;
77 static char *opt_filename = NULL;
78 static int opt_count = -1;
79 static int opt_send_ttl = 64;
81 static void sigint_handler (int signal)
83 /* Make compiler happy */
89 static ping_context_t *context_create (void)
93 if ((ret = malloc (sizeof (ping_context_t))) == NULL)
96 memset (ret, '\0', sizeof (ping_context_t));
98 ret->latency_min = -1.0;
99 ret->latency_max = -1.0;
100 ret->latency_total = 0.0;
101 ret->latency_total_square = 0.0;
106 static void context_destroy (ping_context_t *context)
111 static void usage_exit (const char *name, int status)
115 name_length = (int) strlen (name);
117 fprintf (stderr, "Usage: %s [OPTIONS] "
118 "-f filename | host [host [host ...]]\n"
120 "\nAvailable options:\n"
121 " -4|-6 force the use of IPv4 or IPv6\n"
122 " -c count number of ICMP packets to send\n"
123 " -i interval interval with which to send ICMP packets\n"
124 " -t ttl time to live for each ICMP packet\n"
125 " -I srcaddr source address\n"
126 " -D device outgoing interface name\n"
127 " -f filename filename to read hosts from\n"
129 "\noping "PACKAGE_VERSION", http://verplant.org/liboping/\n"
130 "by Florian octo Forster <octo@verplant.org>\n"
131 "for contributions see `AUTHORS'\n",
136 static int read_options (int argc, char **argv)
142 optchar = getopt (argc, argv, "46c:hi:I:t:f:D:");
151 opt_addrfamily = (optchar == '4') ? AF_INET : AF_INET6;
157 new_count = atoi (optarg);
159 opt_count = new_count;
161 fprintf(stderr, "Ignoring invalid count: %s\n",
168 if (opt_filename != NULL)
170 opt_filename = strdup (optarg);
177 new_interval = atof (optarg);
178 if (new_interval < 0.001)
179 fprintf (stderr, "Ignoring invalid interval: %s\n",
182 opt_interval = new_interval;
187 if (opt_srcaddr != NULL)
189 opt_srcaddr = strdup (optarg);
200 new_send_ttl = atoi (optarg);
201 if ((new_send_ttl > 0) && (new_send_ttl < 256))
202 opt_send_ttl = new_send_ttl;
204 fprintf (stderr, "Ignoring invalid TTL argument: %s\n",
210 usage_exit (argv[0], 0);
213 usage_exit (argv[0], 1);
220 static void print_host (pingobj_iter_t *iter)
223 unsigned int sequence;
227 ping_context_t *context;
230 buffer_len = sizeof (latency);
231 ping_iterator_get_info (iter, PING_INFO_LATENCY,
232 &latency, &buffer_len);
235 buffer_len = sizeof (sequence);
236 ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
237 &sequence, &buffer_len);
240 buffer_len = sizeof (recv_ttl);
241 ping_iterator_get_info (iter, PING_INFO_RECV_TTL,
242 &recv_ttl, &buffer_len);
245 ping_iterator_get_info (iter, PING_INFO_DATA,
248 context = (ping_context_t *) ping_iterator_get_context (iter);
254 context->latency_total += latency;
255 context->latency_total_square += (latency * latency);
257 if ((context->latency_max < 0.0) || (context->latency_max < latency))
258 context->latency_max = latency;
259 if ((context->latency_min < 0.0) || (context->latency_min > latency))
260 context->latency_min = latency;
262 printf ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i time=%.2f ms\n",
264 context->host, context->addr,
265 sequence, recv_ttl, latency);
269 printf ("echo reply from %s (%s): icmp_seq=%u timeout\n",
270 context->host, context->addr,
275 static void time_normalize (struct timespec *ts)
277 while (ts->tv_nsec < 0)
286 ts->tv_nsec += 1000000000;
289 while (ts->tv_nsec >= 1000000000)
292 ts->tv_nsec -= 1000000000;
296 static void time_calc (struct timespec *ts_dest,
297 const struct timespec *ts_int,
298 const struct timeval *tv_begin,
299 const struct timeval *tv_end)
301 ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
302 ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
303 time_normalize (ts_dest);
305 /* Assure that `(begin + interval) > end'.
306 * This may seem overly complicated, but `tv_sec' is of type `time_t'
307 * which may be `unsigned. *sigh* */
308 if ((tv_end->tv_sec > ts_dest->tv_sec)
309 || ((tv_end->tv_sec == ts_dest->tv_sec)
310 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
313 ts_dest->tv_nsec = 0;
317 ts_dest->tv_sec = ts_dest->tv_sec - tv_end->tv_sec;
318 ts_dest->tv_nsec = ts_dest->tv_nsec - (tv_end->tv_usec * 1000);
319 time_normalize (ts_dest);
322 int main (int argc, char **argv)
325 pingobj_iter_t *iter;
327 struct sigaction sigint_action;
329 struct timeval tv_begin;
330 struct timeval tv_end;
331 struct timespec ts_wait;
332 struct timespec ts_int;
337 optind = read_options (argc, argv);
339 if ((optind >= argc) && (opt_filename == NULL)) {
340 usage_exit (argv[0], 1);
345 fprintf (stderr, "Need superuser privileges to open a RAW socket. Sorry.\n");
349 if ((ping = ping_construct ()) == NULL)
351 fprintf (stderr, "ping_construct failed\n");
355 if (ping_setopt (ping, PING_OPT_TTL, &opt_send_ttl) != 0)
357 fprintf (stderr, "Setting TTL to %i failed: %s\n",
358 opt_send_ttl, ping_get_error (ping));
365 temp_nsec = modf (opt_interval, &temp_sec);
366 ts_int.tv_sec = (time_t) temp_sec;
367 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
369 /* printf ("ts_int = %i.%09li\n", (int) ts_int.tv_sec, ts_int.tv_nsec); */
372 if (opt_addrfamily != PING_DEF_AF)
373 ping_setopt (ping, PING_OPT_AF, (void *) &opt_addrfamily);
375 if (opt_srcaddr != NULL)
377 if (ping_setopt (ping, PING_OPT_SOURCE, (void *) opt_srcaddr) != 0)
379 fprintf (stderr, "Setting source address failed: %s\n",
380 ping_get_error (ping));
384 if (opt_device != NULL)
386 if (ping_setopt (ping, PING_OPT_DEVICE, (void *) opt_device) != 0)
388 fprintf (stderr, "Setting device failed: %s\n",
389 ping_get_error (ping));
393 if (opt_filename != NULL)
399 if (strcmp (opt_filename, "-") == 0)
401 infile = fdopen(0, "r");
403 infile = fopen(opt_filename, "r");
407 fprintf (stderr, "Opening %s failed: %s\n",
408 (strcmp (opt_filename, "-") == 0)
409 ? "STDIN" : opt_filename,
414 while (fgets(line, sizeof(line), infile))
416 /* Strip whitespace */
417 if (sscanf(line, "%s", host) != 1)
420 if ((host[0] == 0) || (host[0] == '#'))
423 if (ping_host_add(ping, host) < 0)
425 const char *errmsg = ping_get_error (ping);
427 fprintf (stderr, "Adding host `%s' failed: %s\n", host, errmsg);
435 for (i = optind; i < argc; i++)
437 if (ping_host_add (ping, argv[i]) < 0)
439 const char *errmsg = ping_get_error (ping);
441 fprintf (stderr, "Adding host `%s' failed: %s\n", argv[i], errmsg);
446 /* Drop root privileges if we're setuid-root. */
450 for (iter = ping_iterator_get (ping);
452 iter = ping_iterator_next (iter))
454 ping_context_t *context;
457 context = context_create ();
459 buffer_size = sizeof (context->host);
460 ping_iterator_get_info (iter, PING_INFO_HOSTNAME, context->host, &buffer_size);
462 buffer_size = sizeof (context->addr);
463 ping_iterator_get_info (iter, PING_INFO_ADDRESS, context->addr, &buffer_size);
466 ping_iterator_get_info (iter, PING_INFO_DATA, NULL, &buffer_size);
468 printf ("PING %s (%s) %zu bytes of data.\n",
469 context->host, context->addr, buffer_size);
471 ping_iterator_set_context (iter, (void *) context);
479 memset (&sigint_action, '\0', sizeof (sigint_action));
480 sigint_action.sa_handler = sigint_handler;
481 if (sigaction (SIGINT, &sigint_action, NULL) < 0)
483 perror ("sigaction");
487 while (opt_count != 0)
491 if (gettimeofday (&tv_begin, NULL) < 0)
493 perror ("gettimeofday");
497 if (ping_send (ping) < 0)
499 fprintf (stderr, "ping_send failed: %s\n",
500 ping_get_error (ping));
504 for (iter = ping_iterator_get (ping);
506 iter = ping_iterator_next (iter))
512 /* Don't sleep in the last iteration */
516 if (gettimeofday (&tv_end, NULL) < 0)
518 perror ("gettimeofday");
522 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
524 /* printf ("Sleeping for %i.%09li seconds\n", (int) ts_wait.tv_sec, ts_wait.tv_nsec); */
525 while ((status = nanosleep (&ts_wait, &ts_wait)) != 0)
529 perror ("nanosleep");
532 else if (opt_count == 0)
541 } /* while (opt_count != 0) */
543 for (iter = ping_iterator_get (ping);
545 iter = ping_iterator_next (iter))
547 ping_context_t *context;
549 context = ping_iterator_get_context (iter);
551 printf ("\n--- %s ping statistics ---\n"
552 "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n",
553 context->host, context->req_sent, context->req_rcvd,
554 100.0 * (context->req_sent - context->req_rcvd) / ((double) context->req_sent),
555 context->latency_total);
557 if (context->req_rcvd != 0)
563 num_total = (double) context->req_rcvd;
565 average = context->latency_total / num_total;
566 deviation = sqrt (((num_total * context->latency_total_square) - (context->latency_total * context->latency_total))
567 / (num_total * (num_total - 1.0)));
569 printf ("rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms\n",
570 context->latency_min,
572 context->latency_max,
576 ping_iterator_set_context (iter, NULL);
577 context_destroy (context);