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 int opt_count = -1;
78 static void sigint_handler (int signal)
80 /* Make compiler happy */
86 static ping_context_t *context_create (void)
90 if ((ret = malloc (sizeof (ping_context_t))) == NULL)
93 memset (ret, '\0', sizeof (ping_context_t));
95 ret->latency_min = -1.0;
96 ret->latency_max = -1.0;
97 ret->latency_total = 0.0;
98 ret->latency_total_square = 0.0;
103 static void context_destroy (ping_context_t *context)
108 static void usage_exit (const char *name)
110 fprintf (stderr, "Usage: %s [-46] [-c count] [-i interval] host [host [host ...]]\n",
115 static int read_options (int argc, char **argv)
121 optchar = getopt (argc, argv, "46c:hi:I:");
130 opt_addrfamily = (optchar == '4') ? AF_INET : AF_INET6;
136 new_count = atoi (optarg);
138 opt_count = new_count;
145 new_interval = atof (optarg);
146 if (new_interval >= 0.2)
147 opt_interval = new_interval;
152 if (opt_srcaddr != NULL)
154 opt_srcaddr = strdup (optarg);
160 usage_exit (argv[0]);
167 static void print_host (pingobj_iter_t *iter)
170 unsigned int sequence;
173 ping_context_t *context;
175 buffer_len = sizeof (latency);
176 ping_iterator_get_info (iter, PING_INFO_LATENCY,
177 &latency, &buffer_len);
179 buffer_len = sizeof (sequence);
180 ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
181 &sequence, &buffer_len);
184 ping_iterator_get_info (iter, PING_INFO_DATA,
187 context = (ping_context_t *) ping_iterator_get_context (iter);
193 context->latency_total += latency;
194 context->latency_total_square += (latency * latency);
196 if ((context->latency_max < 0.0) || (context->latency_max < latency))
197 context->latency_max = latency;
198 if ((context->latency_min < 0.0) || (context->latency_min > latency))
199 context->latency_min = latency;
201 printf ("%zu bytes from %s (%s): icmp_seq=%u time=%.2f ms\n",
203 context->host, context->addr,
208 printf ("echo reply from %s (%s): icmp_seq=%u timeout\n",
209 context->host, context->addr,
214 static void time_normalize (struct timespec *ts)
216 while (ts->tv_nsec < 0)
225 ts->tv_nsec += 1000000000;
228 while (ts->tv_nsec >= 1000000000)
231 ts->tv_nsec -= 1000000000;
235 static void time_calc (struct timespec *ts_dest,
236 const struct timespec *ts_int,
237 const struct timeval *tv_begin,
238 const struct timeval *tv_end)
240 ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
241 ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
242 time_normalize (ts_dest);
244 /* Assure that `(begin + interval) > end'.
245 * This may seem overly complicated, but `tv_sec' is of type `time_t'
246 * which may be `unsigned. *sigh* */
247 if ((tv_end->tv_sec > ts_dest->tv_sec)
248 || ((tv_end->tv_sec == ts_dest->tv_sec)
249 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
252 ts_dest->tv_nsec = 0;
256 ts_dest->tv_sec = ts_dest->tv_sec - tv_end->tv_sec;
257 ts_dest->tv_nsec = ts_dest->tv_nsec - (tv_end->tv_usec * 1000);
258 time_normalize (ts_dest);
261 int main (int argc, char **argv)
264 pingobj_iter_t *iter;
266 struct sigaction sigint_action;
268 struct timeval tv_begin;
269 struct timeval tv_end;
270 struct timespec ts_wait;
271 struct timespec ts_int;
276 optind = read_options (argc, argv);
279 usage_exit (argv[0]);
283 fprintf (stderr, "Need superuser privileges to open a RAW socket. Sorry.\n");
287 if ((ping = ping_construct ()) == NULL)
289 fprintf (stderr, "ping_construct failed\n");
297 temp_nsec = modf (opt_interval, &temp_sec);
298 ts_int.tv_sec = (time_t) temp_sec;
299 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
301 /* printf ("ts_int = %i.%09li\n", (int) ts_int.tv_sec, ts_int.tv_nsec); */
304 if (opt_addrfamily != PING_DEF_AF)
305 ping_setopt (ping, PING_OPT_AF, (void *) &opt_addrfamily);
307 if (opt_srcaddr != NULL)
309 if (ping_setopt (ping, PING_OPT_SOURCE, (void *) opt_srcaddr) != 0)
311 fprintf (stderr, "Setting source address failed: %s\n",
312 ping_get_error (ping));
316 for (i = optind; i < argc; i++)
318 if (ping_host_add (ping, argv[i]) < 0)
320 const char *errmsg = ping_get_error (ping);
322 fprintf (stderr, "Adding host `%s' failed: %s\n", argv[i], errmsg);
327 /* Drop root privileges if we're setuid-root. */
331 for (iter = ping_iterator_get (ping);
333 iter = ping_iterator_next (iter))
335 ping_context_t *context;
338 context = context_create ();
340 buffer_size = sizeof (context->host);
341 ping_iterator_get_info (iter, PING_INFO_HOSTNAME, context->host, &buffer_size);
343 buffer_size = sizeof (context->addr);
344 ping_iterator_get_info (iter, PING_INFO_ADDRESS, context->addr, &buffer_size);
347 ping_iterator_get_info (iter, PING_INFO_DATA, NULL, &buffer_size);
349 printf ("PING %s (%s) %zu bytes of data.\n",
350 context->host, context->addr, buffer_size);
352 ping_iterator_set_context (iter, (void *) context);
360 memset (&sigint_action, '\0', sizeof (sigint_action));
361 sigint_action.sa_handler = sigint_handler;
362 if (sigaction (SIGINT, &sigint_action, NULL) < 0)
364 perror ("sigaction");
368 while (opt_count != 0)
372 if (gettimeofday (&tv_begin, NULL) < 0)
374 perror ("gettimeofday");
378 if (ping_send (ping) < 0)
380 fprintf (stderr, "ping_send failed: %s\n",
381 ping_get_error (ping));
385 for (iter = ping_iterator_get (ping);
387 iter = ping_iterator_next (iter))
393 /* Don't sleep in the last iteration */
397 if (gettimeofday (&tv_end, NULL) < 0)
399 perror ("gettimeofday");
403 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
405 /* printf ("Sleeping for %i.%09li seconds\n", (int) ts_wait.tv_sec, ts_wait.tv_nsec); */
406 while ((status = nanosleep (&ts_wait, &ts_wait)) != 0)
410 perror ("nanosleep");
413 else if (opt_count == 0)
422 } /* while (opt_count != 0) */
424 for (iter = ping_iterator_get (ping);
426 iter = ping_iterator_next (iter))
428 ping_context_t *context;
430 context = ping_iterator_get_context (iter);
432 printf ("\n--- %s ping statistics ---\n"
433 "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n",
434 context->host, context->req_sent, context->req_rcvd,
435 100.0 * (context->req_sent - context->req_rcvd) / ((double) context->req_sent),
436 context->latency_total);
438 if (context->req_rcvd != 0)
444 num_total = (double) context->req_rcvd;
446 average = context->latency_total / num_total;
447 deviation = sqrt (((num_total * context->latency_total_square) - (context->latency_total * context->latency_total))
448 / (num_total * (num_total - 1.0)));
450 printf ("rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms\n",
451 context->latency_min,
453 context->latency_max,
457 ping_iterator_set_context (iter, NULL);
458 context_destroy (context);