1 /**
2 * Object oriented C module to send ICMP and ICMPv6 `echo's.
3 * Copyright (C) 2006 Florian octo Forster <octo at verplant.org>
4 *
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
8 * applicable.
9 *
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.
14 *
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
18 */
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #if STDC_HEADERS
25 # include <stdlib.h>
26 # include <stdio.h>
27 # include <string.h>
28 # include <errno.h>
29 # include <assert.h>
30 #else
31 # error "You don't have the standard C99 header files installed"
32 #endif /* STDC_HEADERS */
34 #if HAVE_MATH_H
35 # include <math.h>
36 #endif
38 #if TIME_WITH_SYS_TIME
39 # include <sys/time.h>
40 # include <time.h>
41 #else
42 # if HAVE_SYS_TIME_H
43 # include <sys/time.h>
44 # else
45 # include <time.h>
46 # endif
47 #endif
49 #if HAVE_NETDB_H
50 # include <netdb.h> /* NI_MAXHOST */
51 #endif
53 #if HAVE_SIGNAL_H
54 # include <signal.h>
55 #endif
57 #include "oping.h"
59 typedef struct ping_context
60 {
61 char host[NI_MAXHOST];
62 char addr[NI_MAXHOST];
64 int req_sent;
65 int req_rcvd;
67 double latency_min;
68 double latency_max;
69 double latency_total;
70 double latency_total_square;
71 } ping_context_t;
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 void sigint_handler (int signal)
79 {
80 /* Exit the loop */
81 opt_count = 0;
82 }
84 ping_context_t *context_create (void)
85 {
86 ping_context_t *ret;
88 if ((ret = malloc (sizeof (ping_context_t))) == NULL)
89 return (NULL);
91 memset (ret, '\0', sizeof (ping_context_t));
93 ret->latency_min = -1.0;
94 ret->latency_max = -1.0;
95 ret->latency_total = 0.0;
96 ret->latency_total_square = 0.0;
98 return (ret);
99 }
101 void context_destroy (ping_context_t *context)
102 {
103 free (context);
104 }
106 void usage_exit (const char *name)
107 {
108 fprintf (stderr, "Usage: %s [-46] [-c count] [-i interval] host [host [host ...]]\n",
109 name);
110 exit (1);
111 }
113 int read_options (int argc, char **argv)
114 {
115 int optchar;
117 while (1)
118 {
119 optchar = getopt (argc, argv, "46c:hi:I:");
121 if (optchar == -1)
122 break;
124 switch (optchar)
125 {
126 case '4':
127 case '6':
128 opt_addrfamily = (optchar == '4') ? AF_INET : AF_INET6;
129 break;
131 case 'c':
132 {
133 int new_count;
134 new_count = atoi (optarg);
135 if (new_count > 0)
136 opt_count = new_count;
137 }
138 break;
140 case 'i':
141 {
142 double new_interval;
143 new_interval = atof (optarg);
144 if (new_interval >= 0.2)
145 opt_interval = new_interval;
146 }
147 break;
148 case 'I':
149 {
150 if (opt_srcaddr != NULL)
151 free (opt_srcaddr);
152 opt_srcaddr = strdup (optarg);
153 }
154 break;
156 case 'h':
157 default:
158 usage_exit (argv[0]);
159 }
160 }
162 return (optind);
163 }
165 void print_host (pingobj_iter_t *iter)
166 {
167 double latency;
168 unsigned int sequence;
169 size_t buffer_len;
170 size_t data_len;
171 ping_context_t *context;
173 buffer_len = sizeof (latency);
174 ping_iterator_get_info (iter, PING_INFO_LATENCY,
175 &latency, &buffer_len);
177 buffer_len = sizeof (sequence);
178 ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
179 &sequence, &buffer_len);
181 data_len = 0;
182 ping_iterator_get_info (iter, PING_INFO_DATA,
183 NULL, &data_len);
185 context = (ping_context_t *) ping_iterator_get_context (iter);
187 context->req_sent++;
188 if (latency > 0.0)
189 {
190 context->req_rcvd++;
191 context->latency_total += latency;
192 context->latency_total_square += (latency * latency);
194 if ((context->latency_max < 0.0) || (context->latency_max < latency))
195 context->latency_max = latency;
196 if ((context->latency_min < 0.0) || (context->latency_min > latency))
197 context->latency_min = latency;
199 printf ("%u bytes from %s (%s): icmp_seq=%u time=%.2f ms\n",
200 (unsigned int) data_len,
201 context->host, context->addr,
202 (unsigned int) sequence, latency);
203 }
204 else
205 {
206 printf ("echo reply from %s (%s): icmp_seq=%u timeout\n",
207 context->host, context->addr,
208 (unsigned int) sequence);
209 }
210 }
212 void time_normalize (struct timespec *ts)
213 {
214 while (ts->tv_nsec < 0)
215 {
216 if (ts->tv_sec == 0)
217 {
218 ts->tv_nsec = 0;
219 return;
220 }
222 ts->tv_sec -= 1;
223 ts->tv_nsec += 1000000000;
224 }
226 while (ts->tv_nsec >= 1000000000)
227 {
228 ts->tv_sec += 1;
229 ts->tv_nsec -= 1000000000;
230 }
231 }
233 void time_calc (struct timespec *ts_dest,
234 const struct timespec *ts_int,
235 const struct timeval *tv_begin,
236 const struct timeval *tv_end)
237 {
238 ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
239 ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
240 time_normalize (ts_dest);
242 /* Assure that `(begin + interval) > end'.
243 * This may seem overly complicated, but `tv_sec' is of type `time_t'
244 * which may be `unsigned. *sigh* */
245 if ((tv_end->tv_sec > ts_dest->tv_sec)
246 || ((tv_end->tv_sec == ts_dest->tv_sec)
247 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
248 {
249 ts_dest->tv_sec = 0;
250 ts_dest->tv_nsec = 0;
251 return;
252 }
254 ts_dest->tv_sec = ts_dest->tv_sec - tv_end->tv_sec;
255 ts_dest->tv_nsec = ts_dest->tv_nsec - (tv_end->tv_usec * 1000);
256 time_normalize (ts_dest);
257 }
259 int main (int argc, char **argv)
260 {
261 pingobj_t *ping;
262 pingobj_iter_t *iter;
264 struct sigaction sigint_action;
266 struct timeval tv_begin;
267 struct timeval tv_end;
268 struct timespec ts_wait;
269 struct timespec ts_int;
271 int optind;
272 int i;
274 optind = read_options (argc, argv);
276 if (optind >= argc)
277 usage_exit (argv[0]);
279 if (geteuid () != 0)
280 {
281 fprintf (stderr, "Need superuser privileges to open a RAW socket. Sorry.\n");
282 return (1);
283 }
285 if ((ping = ping_construct ()) == NULL)
286 {
287 fprintf (stderr, "ping_construct failed\n");
288 return (1);
289 }
291 {
292 double temp_sec;
293 double temp_nsec;
295 temp_nsec = modf (opt_interval, &temp_sec);
296 ts_int.tv_sec = (time_t) temp_sec;
297 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
299 /* printf ("ts_int = %i.%09li\n", (int) ts_int.tv_sec, ts_int.tv_nsec); */
300 }
302 if (opt_addrfamily != PING_DEF_AF)
303 ping_setopt (ping, PING_OPT_AF, (void *) &opt_addrfamily);
305 if (opt_srcaddr != NULL)
306 {
307 if (ping_setopt (ping, PING_OPT_SOURCE, (void *) opt_srcaddr) != 0)
308 {
309 fprintf (stderr, "Setting source address failed: %s\n",
310 ping_get_error (ping));
311 }
312 }
314 for (i = optind; i < argc; i++)
315 {
316 if (ping_host_add (ping, argv[i]) < 0)
317 {
318 const char *errmsg = ping_get_error (ping);
320 fprintf (stderr, "Adding host `%s' failed: %s\n", argv[i], errmsg);
321 continue;
322 }
323 }
325 i = 0;
326 for (iter = ping_iterator_get (ping);
327 iter != NULL;
328 iter = ping_iterator_next (iter))
329 {
330 ping_context_t *context;
331 size_t buffer_size;
333 context = context_create ();
335 buffer_size = sizeof (context->host);
336 ping_iterator_get_info (iter, PING_INFO_HOSTNAME, context->host, &buffer_size);
338 buffer_size = sizeof (context->addr);
339 ping_iterator_get_info (iter, PING_INFO_ADDRESS, context->addr, &buffer_size);
341 buffer_size = 0;
342 ping_iterator_get_info (iter, PING_INFO_DATA, NULL, &buffer_size);
344 printf ("PING %s (%s) %u bytes of data.\n",
345 context->host, context->addr, (unsigned int) buffer_size);
347 ping_iterator_set_context (iter, (void *) context);
349 i++;
350 }
352 if (i == 0)
353 return (1);
355 memset (&sigint_action, '\0', sizeof (sigint_action));
356 sigint_action.sa_handler = sigint_handler;
357 if (sigaction (SIGINT, &sigint_action, NULL) < 0)
358 {
359 perror ("sigaction");
360 return (1);
361 }
363 while (opt_count != 0)
364 {
365 int status;
367 if (gettimeofday (&tv_begin, NULL) < 0)
368 {
369 perror ("gettimeofday");
370 return (1);
371 }
373 if (ping_send (ping) < 0)
374 {
375 fprintf (stderr, "ping_send failed: %s\n",
376 ping_get_error (ping));
377 return (1);
378 }
380 for (iter = ping_iterator_get (ping);
381 iter != NULL;
382 iter = ping_iterator_next (iter))
383 {
384 print_host (iter);
385 }
386 fflush (stdout);
388 /* Don't sleep in the last iteration */
389 if (opt_count == 1)
390 break;
392 if (gettimeofday (&tv_end, NULL) < 0)
393 {
394 perror ("gettimeofday");
395 return (1);
396 }
398 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
400 /* printf ("Sleeping for %i.%09li seconds\n", (int) ts_wait.tv_sec, ts_wait.tv_nsec); */
401 while ((status = nanosleep (&ts_wait, &ts_wait)) != 0)
402 {
403 if (errno != EINTR)
404 {
405 perror ("nanosleep");
406 break;
407 }
408 else if (opt_count == 0)
409 {
410 /* sigint */
411 break;
412 }
413 }
415 if (opt_count > 0)
416 opt_count--;
417 } /* while (opt_count != 0) */
419 for (iter = ping_iterator_get (ping);
420 iter != NULL;
421 iter = ping_iterator_next (iter))
422 {
423 ping_context_t *context;
425 context = ping_iterator_get_context (iter);
427 printf ("\n--- %s ping statistics ---\n"
428 "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n",
429 context->host, context->req_sent, context->req_rcvd,
430 100.0 * (context->req_sent - context->req_rcvd) / ((double) context->req_sent),
431 context->latency_total);
433 if (context->req_rcvd != 0)
434 {
435 double num_total;
436 double average;
437 double deviation;
439 num_total = (double) context->req_rcvd;
441 average = context->latency_total / num_total;
442 deviation = sqrt (((num_total * context->latency_total_square) - (context->latency_total * context->latency_total))
443 / (num_total * (num_total - 1.0)));
445 printf ("rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms\n",
446 context->latency_min,
447 average,
448 context->latency_max,
449 deviation);
450 }
452 ping_iterator_set_context (iter, NULL);
453 free (context);
454 }
456 ping_destroy (ping);
458 return (0);
459 }