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_filename = NULL;
77 static int opt_count = -1;
78 static int opt_send_ttl = 64;
80 static void sigint_handler (int signal)
82 /* Make compiler happy */
88 static ping_context_t *context_create (void)
92 if ((ret = malloc (sizeof (ping_context_t))) == NULL)
95 memset (ret, '\0', sizeof (ping_context_t));
97 ret->latency_min = -1.0;
98 ret->latency_max = -1.0;
99 ret->latency_total = 0.0;
100 ret->latency_total_square = 0.0;
105 static void context_destroy (ping_context_t *context)
110 static void usage_exit (const char *name)
114 name_length = (int) strlen (name);
116 fprintf (stderr, "Usage: %s [OPTIONS] "
117 "-f filename | host [host [host ...]]\n"
119 "\nAvailable options:\n"
120 " -4|-6 force the use of IPv4 or IPv6\n"
121 " -c count number of ICMP packets to send\n"
122 " -i interval interval with which to send ICMP packets\n"
123 " -t ttl time to live for each ICMP packet\n"
124 " -I srcaddr source address\n"
125 " -f filename filename to read hosts from\n"
127 "\noping "PACKAGE_VERSION", http://verplant.org/liboping/\n"
128 "by Florian octo Forster <octo@verplant.org>\n"
129 "for contributions see `AUTHORS'\n",
134 static int read_options (int argc, char **argv)
140 optchar = getopt (argc, argv, "46c:hi:I:t:f:");
149 opt_addrfamily = (optchar == '4') ? AF_INET : AF_INET6;
155 new_count = atoi (optarg);
157 opt_count = new_count;
159 fprintf(stderr, "Ignoring invalid count: %s\n",
166 if (opt_filename != NULL)
168 opt_filename = strdup (optarg);
175 new_interval = atof (optarg);
176 if (new_interval < 0.001)
177 fprintf (stderr, "Ignoring invalid interval: %s\n",
180 opt_interval = new_interval;
185 if (opt_srcaddr != NULL)
187 opt_srcaddr = strdup (optarg);
194 new_send_ttl = atoi (optarg);
195 if ((new_send_ttl > 0) && (new_send_ttl < 256))
196 opt_send_ttl = new_send_ttl;
198 fprintf (stderr, "Ignoring invalid TTL argument: %s\n",
205 usage_exit (argv[0]);
212 static void print_host (pingobj_iter_t *iter)
215 unsigned int sequence;
219 ping_context_t *context;
222 buffer_len = sizeof (latency);
223 ping_iterator_get_info (iter, PING_INFO_LATENCY,
224 &latency, &buffer_len);
227 buffer_len = sizeof (sequence);
228 ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
229 &sequence, &buffer_len);
232 buffer_len = sizeof (recv_ttl);
233 ping_iterator_get_info (iter, PING_INFO_RECV_TTL,
234 &recv_ttl, &buffer_len);
237 ping_iterator_get_info (iter, PING_INFO_DATA,
240 context = (ping_context_t *) ping_iterator_get_context (iter);
246 context->latency_total += latency;
247 context->latency_total_square += (latency * latency);
249 if ((context->latency_max < 0.0) || (context->latency_max < latency))
250 context->latency_max = latency;
251 if ((context->latency_min < 0.0) || (context->latency_min > latency))
252 context->latency_min = latency;
254 printf ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i time=%.2f ms\n",
256 context->host, context->addr,
257 sequence, recv_ttl, latency);
261 printf ("echo reply from %s (%s): icmp_seq=%u timeout\n",
262 context->host, context->addr,
267 static void time_normalize (struct timespec *ts)
269 while (ts->tv_nsec < 0)
278 ts->tv_nsec += 1000000000;
281 while (ts->tv_nsec >= 1000000000)
284 ts->tv_nsec -= 1000000000;
288 static void time_calc (struct timespec *ts_dest,
289 const struct timespec *ts_int,
290 const struct timeval *tv_begin,
291 const struct timeval *tv_end)
293 ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
294 ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
295 time_normalize (ts_dest);
297 /* Assure that `(begin + interval) > end'.
298 * This may seem overly complicated, but `tv_sec' is of type `time_t'
299 * which may be `unsigned. *sigh* */
300 if ((tv_end->tv_sec > ts_dest->tv_sec)
301 || ((tv_end->tv_sec == ts_dest->tv_sec)
302 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
305 ts_dest->tv_nsec = 0;
309 ts_dest->tv_sec = ts_dest->tv_sec - tv_end->tv_sec;
310 ts_dest->tv_nsec = ts_dest->tv_nsec - (tv_end->tv_usec * 1000);
311 time_normalize (ts_dest);
314 int main (int argc, char **argv)
317 pingobj_iter_t *iter;
319 struct sigaction sigint_action;
321 struct timeval tv_begin;
322 struct timeval tv_end;
323 struct timespec ts_wait;
324 struct timespec ts_int;
329 optind = read_options (argc, argv);
331 if ((optind >= argc) && (opt_filename == NULL)) {
332 usage_exit (argv[0]);
337 fprintf (stderr, "Need superuser privileges to open a RAW socket. Sorry.\n");
341 if ((ping = ping_construct ()) == NULL)
343 fprintf (stderr, "ping_construct failed\n");
347 if (ping_setopt (ping, PING_OPT_TTL, &opt_send_ttl) != 0)
349 fprintf (stderr, "Setting TTL to %i failed: %s\n",
350 opt_send_ttl, ping_get_error (ping));
357 temp_nsec = modf (opt_interval, &temp_sec);
358 ts_int.tv_sec = (time_t) temp_sec;
359 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
361 /* printf ("ts_int = %i.%09li\n", (int) ts_int.tv_sec, ts_int.tv_nsec); */
364 if (opt_addrfamily != PING_DEF_AF)
365 ping_setopt (ping, PING_OPT_AF, (void *) &opt_addrfamily);
367 if (opt_srcaddr != NULL)
369 if (ping_setopt (ping, PING_OPT_SOURCE, (void *) opt_srcaddr) != 0)
371 fprintf (stderr, "Setting source address failed: %s\n",
372 ping_get_error (ping));
376 if (opt_filename != NULL)
382 if (strcmp (opt_filename, "-") == 0)
384 infile = fdopen(0, "r");
386 infile = fopen(opt_filename, "r");
390 fprintf (stderr, "Opening %s failed: %s\n",
391 (strcmp (opt_filename, "-") == 0)
392 ? "STDIN" : opt_filename,
397 while (fgets(line, sizeof(line), infile))
399 /* Strip whitespace */
400 if (sscanf(line, "%s", host) != 1)
403 if ((host[0] == 0) || (host[0] == '#'))
406 if (ping_host_add(ping, host) < 0)
408 const char *errmsg = ping_get_error (ping);
410 fprintf (stderr, "Adding host `%s' failed: %s\n", host, errmsg);
418 for (i = optind; i < argc; i++)
420 if (ping_host_add (ping, argv[i]) < 0)
422 const char *errmsg = ping_get_error (ping);
424 fprintf (stderr, "Adding host `%s' failed: %s\n", argv[i], errmsg);
429 /* Drop root privileges if we're setuid-root. */
433 for (iter = ping_iterator_get (ping);
435 iter = ping_iterator_next (iter))
437 ping_context_t *context;
440 context = context_create ();
442 buffer_size = sizeof (context->host);
443 ping_iterator_get_info (iter, PING_INFO_HOSTNAME, context->host, &buffer_size);
445 buffer_size = sizeof (context->addr);
446 ping_iterator_get_info (iter, PING_INFO_ADDRESS, context->addr, &buffer_size);
449 ping_iterator_get_info (iter, PING_INFO_DATA, NULL, &buffer_size);
451 printf ("PING %s (%s) %zu bytes of data.\n",
452 context->host, context->addr, buffer_size);
454 ping_iterator_set_context (iter, (void *) context);
462 memset (&sigint_action, '\0', sizeof (sigint_action));
463 sigint_action.sa_handler = sigint_handler;
464 if (sigaction (SIGINT, &sigint_action, NULL) < 0)
466 perror ("sigaction");
470 while (opt_count != 0)
474 if (gettimeofday (&tv_begin, NULL) < 0)
476 perror ("gettimeofday");
480 if (ping_send (ping) < 0)
482 fprintf (stderr, "ping_send failed: %s\n",
483 ping_get_error (ping));
487 for (iter = ping_iterator_get (ping);
489 iter = ping_iterator_next (iter))
495 /* Don't sleep in the last iteration */
499 if (gettimeofday (&tv_end, NULL) < 0)
501 perror ("gettimeofday");
505 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
507 /* printf ("Sleeping for %i.%09li seconds\n", (int) ts_wait.tv_sec, ts_wait.tv_nsec); */
508 while ((status = nanosleep (&ts_wait, &ts_wait)) != 0)
512 perror ("nanosleep");
515 else if (opt_count == 0)
524 } /* while (opt_count != 0) */
526 for (iter = ping_iterator_get (ping);
528 iter = ping_iterator_next (iter))
530 ping_context_t *context;
532 context = ping_iterator_get_context (iter);
534 printf ("\n--- %s ping statistics ---\n"
535 "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n",
536 context->host, context->req_sent, context->req_rcvd,
537 100.0 * (context->req_sent - context->req_rcvd) / ((double) context->req_sent),
538 context->latency_total);
540 if (context->req_rcvd != 0)
546 num_total = (double) context->req_rcvd;
548 average = context->latency_total / num_total;
549 deviation = sqrt (((num_total * context->latency_total_square) - (context->latency_total * context->latency_total))
550 / (num_total * (num_total - 1.0)));
552 printf ("rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms\n",
553 context->latency_min,
555 context->latency_max,
559 ping_iterator_set_context (iter, NULL);
560 context_destroy (context);