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 int opt_count = -1;
77 void sigint_handler (int signal)
83 ping_context_t *context_create (void)
87 if ((ret = malloc (sizeof (ping_context_t))) == NULL)
90 memset (ret, '\0', sizeof (ping_context_t));
92 ret->latency_min = -1.0;
93 ret->latency_max = -1.0;
94 ret->latency_total = 0.0;
95 ret->latency_total_square = 0.0;
100 void context_destroy (ping_context_t *context)
105 void usage_exit (const char *name)
107 fprintf (stderr, "Usage: %s [-46] [-c count] [-i interval] host [host [host ...]]\n",
112 int read_options (int argc, char **argv)
118 optchar = getopt (argc, argv, "46c:i:");
127 opt_addrfamily = (optchar == '4') ? AF_INET : AF_INET6;
133 new_count = atoi (optarg);
135 opt_count = new_count;
142 new_interval = atof (optarg);
143 if (new_interval >= 0.2)
144 opt_interval = new_interval;
149 usage_exit (argv[0]);
156 void print_host (pingobj_iter_t *iter)
161 ping_context_t *context;
163 buffer_len = sizeof (latency);
164 ping_iterator_get_info (iter, PING_INFO_LATENCY,
165 &latency, &buffer_len);
167 buffer_len = sizeof (sequence);
168 ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
169 &sequence, &buffer_len);
171 context = (ping_context_t *) ping_iterator_get_context (iter);
177 context->latency_total += latency;
178 context->latency_total_square += (latency * latency);
180 if ((context->latency_max < 0.0) || (context->latency_max < latency))
181 context->latency_max = latency;
182 if ((context->latency_min < 0.0) || (context->latency_min > latency))
183 context->latency_min = latency;
185 printf ("echo reply from %s (%s): icmp_seq=%u time=%.1f ms\n",
186 context->host, context->addr,
187 (unsigned int) sequence, latency);
191 printf ("echo reply from %s (%s): icmp_seq=%u timeout\n",
192 context->host, context->addr,
193 (unsigned int) sequence);
197 void time_normalize (struct timespec *ts)
199 while (ts->tv_nsec < 0)
208 ts->tv_nsec += 1000000000;
211 while (ts->tv_nsec >= 1000000000)
214 ts->tv_nsec -= 1000000000;
218 void time_calc (struct timespec *ts_dest,
219 const struct timespec *ts_int,
220 const struct timeval *tv_begin,
221 const struct timeval *tv_end)
223 ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
224 ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
225 time_normalize (ts_dest);
227 /* Assure that `(begin + interval) > end'.
228 * This may seem overly complicated, but `tv_sec' is of type `time_t'
229 * which may be `unsigned. *sigh* */
230 if ((tv_end->tv_sec > ts_dest->tv_sec)
231 || ((tv_end->tv_sec == ts_dest->tv_sec)
232 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
235 ts_dest->tv_nsec = 0;
239 ts_dest->tv_sec = ts_dest->tv_sec - tv_end->tv_sec;
240 ts_dest->tv_nsec = ts_dest->tv_nsec - (tv_end->tv_usec * 1000);
241 time_normalize (ts_dest);
244 int main (int argc, char **argv)
247 pingobj_iter_t *iter;
249 struct sigaction sigint_action;
251 struct timeval tv_begin;
252 struct timeval tv_end;
253 struct timespec ts_wait;
254 struct timespec ts_int;
261 fprintf (stderr, "Need superuser privileges to open a RAW socket. Sorry.\n");
265 optind = read_options (argc, argv);
268 usage_exit (argv[0]);
270 if ((ping = ping_construct ()) == NULL)
272 fprintf (stderr, "ping_construct failed\n");
280 temp_nsec = modf (opt_interval, &temp_sec);
281 ts_int.tv_sec = (time_t) temp_sec;
282 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
284 /* printf ("ts_int = %i.%09li\n", (int) ts_int.tv_sec, ts_int.tv_nsec); */
287 if (opt_addrfamily != PING_DEF_AF)
288 ping_setopt (ping, PING_OPT_AF, (void *) &opt_addrfamily);
290 for (i = optind; i < argc; i++)
292 if (ping_host_add (ping, argv[i]) > 0)
294 fprintf (stderr, "ping_host_add (%s) failed\n", argv[i]);
299 for (iter = ping_iterator_get (ping);
301 iter = ping_iterator_next (iter))
303 ping_context_t *context;
306 context = context_create ();
308 buffer_size = sizeof (context->host);
309 ping_iterator_get_info (iter, PING_INFO_HOSTNAME, context->host, &buffer_size);
311 buffer_size = sizeof (context->addr);
312 ping_iterator_get_info (iter, PING_INFO_ADDRESS, context->addr, &buffer_size);
314 ping_iterator_set_context (iter, (void *) context);
317 memset (&sigint_action, '\0', sizeof (sigint_action));
318 sigint_action.sa_handler = sigint_handler;
319 if (sigaction (SIGINT, &sigint_action, NULL) < 0)
321 perror ("sigaction");
325 while (opt_count != 0)
329 if (gettimeofday (&tv_begin, NULL) < 0)
331 perror ("gettimeofday");
335 if (ping_send (ping) < 0)
337 fprintf (stderr, "ping_send failed\n");
341 for (iter = ping_iterator_get (ping);
343 iter = ping_iterator_next (iter))
349 /* Don't sleep in the last iteration */
353 if (gettimeofday (&tv_end, NULL) < 0)
355 perror ("gettimeofday");
359 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
361 /* printf ("Sleeping for %i.%09li seconds\n", (int) ts_wait.tv_sec, ts_wait.tv_nsec); */
362 while ((status = nanosleep (&ts_wait, &ts_wait)) != 0)
366 perror ("nanosleep");
369 else if (opt_count == 0)
378 } /* while (opt_count != 0) */
380 for (iter = ping_iterator_get (ping);
382 iter = ping_iterator_next (iter))
384 ping_context_t *context;
388 context = ping_iterator_get_context (iter);
390 average = context->latency_total / ((double) context->req_rcvd);
391 deviation = context->latency_total_square / ((double) context->req_rcvd);
392 deviation = sqrt (deviation - (average * average));
394 printf ("\n--- %s ping statistics ---\n"
395 "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n"
396 "rtt min/avg/max/mdev = %.3f/%.3f/%.3f/%.3f ms\n",
397 context->host, context->req_sent, context->req_rcvd,
398 100.0 * (context->req_sent - context->req_rcvd) / ((double) context->req_sent),
399 context->latency_total,
400 context->latency_min,
402 context->latency_max,
405 ping_iterator_set_context (iter, NULL);