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
32 # error "You don't have the standard C99 header files installed"
33 #endif /* STDC_HEADERS */
39 #if TIME_WITH_SYS_TIME
40 # include <sys/time.h>
44 # include <sys/time.h>
51 # include <netdb.h> /* NI_MAXHOST */
60 typedef struct ping_context
62 char host[NI_MAXHOST];
63 char addr[NI_MAXHOST];
71 double latency_total_square;
74 static double opt_interval = 1.0;
75 static int opt_addrfamily = PING_DEF_AF;
76 static char *opt_srcaddr = NULL;
77 static char *opt_device = NULL;
78 static char *opt_filename = NULL;
79 static int opt_count = -1;
80 static int opt_send_ttl = 64;
82 static void sigint_handler (int signal)
84 /* Make compiler happy */
90 static ping_context_t *context_create (void)
94 if ((ret = malloc (sizeof (ping_context_t))) == NULL)
97 memset (ret, '\0', sizeof (ping_context_t));
99 ret->latency_min = -1.0;
100 ret->latency_max = -1.0;
101 ret->latency_total = 0.0;
102 ret->latency_total_square = 0.0;
107 static void context_destroy (ping_context_t *context)
112 static void usage_exit (const char *name, int status)
116 name_length = (int) strlen (name);
118 fprintf (stderr, "Usage: %s [OPTIONS] "
119 "-f filename | host [host [host ...]]\n"
121 "\nAvailable options:\n"
122 " -4|-6 force the use of IPv4 or IPv6\n"
123 " -c count number of ICMP packets to send\n"
124 " -i interval interval with which to send ICMP packets\n"
125 " -t ttl time to live for each ICMP packet\n"
126 " -I srcaddr source address\n"
127 " -D device outgoing interface name\n"
128 " -f filename filename to read hosts from\n"
130 "\noping "PACKAGE_VERSION", http://verplant.org/liboping/\n"
131 "by Florian octo Forster <octo@verplant.org>\n"
132 "for contributions see `AUTHORS'\n",
137 static _Bool is_setuid (void)
139 return (getuid () != geteuid ());
142 static int read_options (int argc, char **argv)
148 optchar = getopt (argc, argv, "46c:hi:I:t:f:D:");
157 opt_addrfamily = (optchar == '4') ? AF_INET : AF_INET6;
163 new_count = atoi (optarg);
165 opt_count = new_count;
167 fprintf(stderr, "Ignoring invalid count: %s\n",
175 fprintf (stderr, "For security reasons the `-f' option "
176 "is disabled if real and effective "
177 "user IDs don't match. Sorry.\n");
181 if (opt_filename != NULL)
183 opt_filename = strdup (optarg);
190 new_interval = atof (optarg);
191 if (new_interval < 0.001)
192 fprintf (stderr, "Ignoring invalid interval: %s\n",
195 opt_interval = new_interval;
200 if (opt_srcaddr != NULL)
202 opt_srcaddr = strdup (optarg);
213 new_send_ttl = atoi (optarg);
214 if ((new_send_ttl > 0) && (new_send_ttl < 256))
215 opt_send_ttl = new_send_ttl;
217 fprintf (stderr, "Ignoring invalid TTL argument: %s\n",
223 usage_exit (argv[0], 0);
226 usage_exit (argv[0], 1);
233 static void print_host (pingobj_iter_t *iter)
236 unsigned int sequence;
240 ping_context_t *context;
243 buffer_len = sizeof (latency);
244 ping_iterator_get_info (iter, PING_INFO_LATENCY,
245 &latency, &buffer_len);
248 buffer_len = sizeof (sequence);
249 ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
250 &sequence, &buffer_len);
253 buffer_len = sizeof (recv_ttl);
254 ping_iterator_get_info (iter, PING_INFO_RECV_TTL,
255 &recv_ttl, &buffer_len);
258 ping_iterator_get_info (iter, PING_INFO_DATA,
261 context = (ping_context_t *) ping_iterator_get_context (iter);
267 context->latency_total += latency;
268 context->latency_total_square += (latency * latency);
270 if ((context->latency_max < 0.0) || (context->latency_max < latency))
271 context->latency_max = latency;
272 if ((context->latency_min < 0.0) || (context->latency_min > latency))
273 context->latency_min = latency;
275 printf ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i time=%.2f ms\n",
277 context->host, context->addr,
278 sequence, recv_ttl, latency);
282 printf ("echo reply from %s (%s): icmp_seq=%u timeout\n",
283 context->host, context->addr,
288 static void time_normalize (struct timespec *ts)
290 while (ts->tv_nsec < 0)
299 ts->tv_nsec += 1000000000;
302 while (ts->tv_nsec >= 1000000000)
305 ts->tv_nsec -= 1000000000;
309 static void time_calc (struct timespec *ts_dest,
310 const struct timespec *ts_int,
311 const struct timeval *tv_begin,
312 const struct timeval *tv_end)
314 ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
315 ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
316 time_normalize (ts_dest);
318 /* Assure that `(begin + interval) > end'.
319 * This may seem overly complicated, but `tv_sec' is of type `time_t'
320 * which may be `unsigned. *sigh* */
321 if ((tv_end->tv_sec > ts_dest->tv_sec)
322 || ((tv_end->tv_sec == ts_dest->tv_sec)
323 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
326 ts_dest->tv_nsec = 0;
330 ts_dest->tv_sec = ts_dest->tv_sec - tv_end->tv_sec;
331 ts_dest->tv_nsec = ts_dest->tv_nsec - (tv_end->tv_usec * 1000);
332 time_normalize (ts_dest);
335 int main (int argc, char **argv)
338 pingobj_iter_t *iter;
340 struct sigaction sigint_action;
342 struct timeval tv_begin;
343 struct timeval tv_end;
344 struct timespec ts_wait;
345 struct timespec ts_int;
350 optind = read_options (argc, argv);
352 if ((optind >= argc) && (opt_filename == NULL)) {
353 usage_exit (argv[0], 1);
358 fprintf (stderr, "Need superuser privileges to open a RAW socket. Sorry.\n");
362 if ((ping = ping_construct ()) == NULL)
364 fprintf (stderr, "ping_construct failed\n");
368 if (ping_setopt (ping, PING_OPT_TTL, &opt_send_ttl) != 0)
370 fprintf (stderr, "Setting TTL to %i failed: %s\n",
371 opt_send_ttl, ping_get_error (ping));
378 temp_nsec = modf (opt_interval, &temp_sec);
379 ts_int.tv_sec = (time_t) temp_sec;
380 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
382 /* printf ("ts_int = %i.%09li\n", (int) ts_int.tv_sec, ts_int.tv_nsec); */
385 if (opt_addrfamily != PING_DEF_AF)
386 ping_setopt (ping, PING_OPT_AF, (void *) &opt_addrfamily);
388 if (opt_srcaddr != NULL)
390 if (ping_setopt (ping, PING_OPT_SOURCE, (void *) opt_srcaddr) != 0)
392 fprintf (stderr, "Setting source address failed: %s\n",
393 ping_get_error (ping));
397 if (opt_device != NULL)
399 if (ping_setopt (ping, PING_OPT_DEVICE, (void *) opt_device) != 0)
401 fprintf (stderr, "Setting device failed: %s\n",
402 ping_get_error (ping));
406 if (opt_filename != NULL)
412 if (strcmp (opt_filename, "-") == 0)
414 infile = fdopen(0, "r");
416 infile = fopen(opt_filename, "r");
420 fprintf (stderr, "Opening %s failed: %s\n",
421 (strcmp (opt_filename, "-") == 0)
422 ? "STDIN" : opt_filename,
427 while (fgets(line, sizeof(line), infile))
429 /* Strip whitespace */
430 if (sscanf(line, "%s", host) != 1)
433 if ((host[0] == 0) || (host[0] == '#'))
436 if (ping_host_add(ping, host) < 0)
438 const char *errmsg = ping_get_error (ping);
440 fprintf (stderr, "Adding host `%s' failed: %s\n", host, errmsg);
448 for (i = optind; i < argc; i++)
450 if (ping_host_add (ping, argv[i]) < 0)
452 const char *errmsg = ping_get_error (ping);
454 fprintf (stderr, "Adding host `%s' failed: %s\n", argv[i], errmsg);
459 /* Drop root privileges if we're setuid-root. */
463 for (iter = ping_iterator_get (ping);
465 iter = ping_iterator_next (iter))
467 ping_context_t *context;
470 context = context_create ();
472 buffer_size = sizeof (context->host);
473 ping_iterator_get_info (iter, PING_INFO_HOSTNAME, context->host, &buffer_size);
475 buffer_size = sizeof (context->addr);
476 ping_iterator_get_info (iter, PING_INFO_ADDRESS, context->addr, &buffer_size);
479 ping_iterator_get_info (iter, PING_INFO_DATA, NULL, &buffer_size);
481 printf ("PING %s (%s) %zu bytes of data.\n",
482 context->host, context->addr, buffer_size);
484 ping_iterator_set_context (iter, (void *) context);
492 memset (&sigint_action, '\0', sizeof (sigint_action));
493 sigint_action.sa_handler = sigint_handler;
494 if (sigaction (SIGINT, &sigint_action, NULL) < 0)
496 perror ("sigaction");
500 while (opt_count != 0)
504 if (gettimeofday (&tv_begin, NULL) < 0)
506 perror ("gettimeofday");
510 if (ping_send (ping) < 0)
512 fprintf (stderr, "ping_send failed: %s\n",
513 ping_get_error (ping));
517 for (iter = ping_iterator_get (ping);
519 iter = ping_iterator_next (iter))
525 /* Don't sleep in the last iteration */
529 if (gettimeofday (&tv_end, NULL) < 0)
531 perror ("gettimeofday");
535 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
537 /* printf ("Sleeping for %i.%09li seconds\n", (int) ts_wait.tv_sec, ts_wait.tv_nsec); */
538 while ((status = nanosleep (&ts_wait, &ts_wait)) != 0)
542 perror ("nanosleep");
545 else if (opt_count == 0)
554 } /* while (opt_count != 0) */
556 for (iter = ping_iterator_get (ping);
558 iter = ping_iterator_next (iter))
560 ping_context_t *context;
562 context = ping_iterator_get_context (iter);
564 printf ("\n--- %s ping statistics ---\n"
565 "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n",
566 context->host, context->req_sent, context->req_rcvd,
567 100.0 * (context->req_sent - context->req_rcvd) / ((double) context->req_sent),
568 context->latency_total);
570 if (context->req_rcvd != 0)
576 num_total = (double) context->req_rcvd;
578 average = context->latency_total / num_total;
579 deviation = sqrt (((num_total * context->latency_total_square) - (context->latency_total * context->latency_total))
580 / (num_total * (num_total - 1.0)));
582 printf ("rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms\n",
583 context->latency_min,
585 context->latency_max,
589 ping_iterator_set_context (iter, NULL);
590 context_destroy (context);