Code

Created standard subversion directories.
[liboping.git] / src / oping.c
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; either version 2 of the License, or
8  * (at your option) any later version.
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;
66         
67         double latency_min;
68         double latency_max;
69         double latency_total;
70 } ping_context_t;
72 static double opt_interval   = 1.0;
73 static int    opt_addrfamily = PING_DEF_AF;
74 static int    opt_count      = -1;
76 void sigint_handler (int signal)
77 {
78         /* Exit the loop */
79         opt_count = 0;
80 }
82 ping_context_t *context_create (void)
83 {
84         ping_context_t *ret;
86         if ((ret = malloc (sizeof (ping_context_t))) == NULL)
87                 return (NULL);
89         memset (ret, '\0', sizeof (ping_context_t));
91         ret->latency_min   = -1.0;
92         ret->latency_max   = -1.0;
93         ret->latency_total = 0.0;
95         return (ret);
96 }
98 void context_destroy (ping_context_t *context)
99 {
100         free (context);
103 void usage_exit (const char *name)
105         fprintf (stderr, "Usage: %s [-46] [-c count] [-i interval] host [host [host ...]]\n",
106                         name);
107         exit (1);
110 int read_options (int argc, char **argv)
112         int optchar;
114         while (1)
115         {
116                 optchar = getopt (argc, argv, "46c:i:");
118                 if (optchar == -1)
119                         break;
121                 switch (optchar)
122                 {
123                         case '4':
124                         case '6':
125                                 opt_addrfamily = (optchar == '4') ? AF_INET : AF_INET6;
126                                 break;
128                         case 'c':
129                                 {
130                                         int new_count;
131                                         new_count = atoi (optarg);
132                                         if (new_count > 0)
133                                                 opt_count = new_count;
134                                 }
135                                 break;
137                         case 'i':
138                                 {
139                                         double new_interval;
140                                         new_interval = atof (optarg);
141                                         if (new_interval >= 0.2)
142                                                 opt_interval = new_interval;
143                                 }
144                                 break;
146                         default:
147                                 usage_exit (argv[0]);
148                 }
149         }
151         return (optind);
154 void print_host (pingobj_iter_t *iter)
156         double   latency;
157         uint16_t sequence;
158         size_t   buffer_len;
159         ping_context_t *context;
160         
161         buffer_len = sizeof (latency);
162         ping_iterator_get_info (iter, PING_INFO_LATENCY,
163                         &latency, &buffer_len);
165         buffer_len = sizeof (sequence);
166         ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
167                         &sequence, &buffer_len);
169         context = (ping_context_t *) ping_iterator_get_context (iter);
171         context->req_sent++;
172         if (latency > 0.0)
173         {
174                 context->req_rcvd++;
175                 context->latency_total += latency;
177                 if ((context->latency_max < 0.0) || (context->latency_max < latency))
178                         context->latency_max = latency;
179                 if ((context->latency_min < 0.0) || (context->latency_min > latency))
180                         context->latency_min = latency;
182                 printf ("echo reply from %s (%s): icmp_seq=%u time=%.1f ms\n",
183                                 context->host, context->addr,
184                                 (unsigned int) sequence, latency);
185         }
186         else
187         {
188                 printf ("echo reply from %s (%s): icmp_seq=%u timeout\n",
189                                 context->host, context->addr,
190                                 (unsigned int) sequence);
191         }
194 void time_normalize (struct timespec *ts)
196         while (ts->tv_nsec < 0)
197         {
198                 if (ts->tv_sec == 0)
199                 {
200                         ts->tv_nsec = 0;
201                         return;
202                 }
204                 ts->tv_sec  -= 1;
205                 ts->tv_nsec += 1000000000;
206         }
208         while (ts->tv_nsec >= 1000000000)
209         {
210                 ts->tv_sec  += 1;
211                 ts->tv_nsec -= 1000000000;
212         }
215 void time_calc (struct timespec *ts_dest,
216                 const struct timespec *ts_int,
217                 const struct timeval  *tv_begin,
218                 const struct timeval  *tv_end)
220         ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
221         ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
222         time_normalize (ts_dest);
224         /* Assure that `(begin + interval) > end'.
225          * This may seem overly complicated, but `tv_sec' is of type `time_t'
226          * which may be `unsigned. *sigh* */
227         if ((tv_end->tv_sec > ts_dest->tv_sec)
228                         || ((tv_end->tv_sec == ts_dest->tv_sec)
229                                 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
230         {
231                 ts_dest->tv_sec  = 0;
232                 ts_dest->tv_nsec = 0;
233                 return;
234         }
236         ts_dest->tv_sec = ts_dest->tv_sec - tv_end->tv_sec;
237         ts_dest->tv_nsec = ts_dest->tv_nsec - (tv_end->tv_usec * 1000);
238         time_normalize (ts_dest);
241 int main (int argc, char **argv)
243         pingobj_t      *ping;
244         pingobj_iter_t *iter;
246         struct sigaction sigint_action;
248         struct timeval  tv_begin;
249         struct timeval  tv_end;
250         struct timespec ts_wait;
251         struct timespec ts_int;
253         int optind;
254         int i;
256         if (geteuid () != 0)
257         {
258                 fprintf (stderr, "Need superuser privileges to open a RAW socket. Sorry.\n");
259                 return (1);
260         }
262         optind = read_options (argc, argv);
264         if (optind >= argc)
265                 usage_exit (argv[0]);
267         if ((ping = ping_construct ()) == NULL)
268         {
269                 fprintf (stderr, "ping_construct failed\n");
270                 return (1);
271         }
273         {
274                 double temp_sec;
275                 double temp_nsec;
277                 temp_nsec = modf (opt_interval, &temp_sec);
278                 ts_int.tv_sec  = (time_t) temp_sec;
279                 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
281                 /* printf ("ts_int = %i.%09li\n", (int) ts_int.tv_sec, ts_int.tv_nsec); */
282         }
284         if (opt_addrfamily != PING_DEF_AF)
285                 ping_setopt (ping, PING_OPT_AF, (void *) &opt_addrfamily);
287         for (i = optind; i < argc; i++)
288         {
289                 if (ping_host_add (ping, argv[i]) > 0)
290                 {
291                         fprintf (stderr, "ping_host_add (%s) failed\n", argv[i]);
292                         continue;
293                 }
294         }
296         for (iter = ping_iterator_get (ping);
297                         iter != NULL;
298                         iter = ping_iterator_next (iter))
299         {
300                 ping_context_t *context;
301                 size_t buffer_size;
303                 context = context_create ();
305                 buffer_size = sizeof (context->host);
306                 ping_iterator_get_info (iter, PING_INFO_HOSTNAME, context->host, &buffer_size);
308                 buffer_size = sizeof (context->addr);
309                 ping_iterator_get_info (iter, PING_INFO_ADDRESS, context->addr, &buffer_size);
311                 ping_iterator_set_context (iter, (void *) context);
312         }
314         memset (&sigint_action, '\0', sizeof (sigint_action));
315         sigint_action.sa_handler = sigint_handler;
316         if (sigaction (SIGINT, &sigint_action, NULL) < 0)
317         {
318                 perror ("sigaction");
319                 return (1);
320         }
322         while (opt_count != 0)
323         {
324                 int status;
326                 if (gettimeofday (&tv_begin, NULL) < 0)
327                 {
328                         perror ("gettimeofday");
329                         return (1);
330                 }
332                 if (ping_send (ping) < 0)
333                 {
334                         fprintf (stderr, "ping_send failed\n");
335                         return (1);
336                 }
338                 for (iter = ping_iterator_get (ping);
339                                 iter != NULL;
340                                 iter = ping_iterator_next (iter))
341                 {
342                         print_host (iter);
343                 }
344                 fflush (stdout);
346                 /* Don't sleep in the last iteration */
347                 if (opt_count == 1)
348                         break;
350                 if (gettimeofday (&tv_end, NULL) < 0)
351                 {
352                         perror ("gettimeofday");
353                         return (1);
354                 }
356                 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
358                 /* printf ("Sleeping for %i.%09li seconds\n", (int) ts_wait.tv_sec, ts_wait.tv_nsec); */
359                 while ((status = nanosleep (&ts_wait, &ts_wait)) != 0)
360                 {
361                         if (errno != EINTR)
362                         {
363                                 perror ("nanosleep");
364                                 break;
365                         }
366                         else if (opt_count == 0)
367                         {
368                                 /* sigint */
369                                 break;
370                         }
371                 }
373                 if (opt_count > 0)
374                         opt_count--;
375         } /* while (opt_count != 0) */
377         for (iter = ping_iterator_get (ping);
378                         iter != NULL;
379                         iter = ping_iterator_next (iter))
380         {
381                 ping_context_t *context;
383                 context = ping_iterator_get_context (iter);
385                 printf ("\n--- %s ping statistics ---\n"
386                                 "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n"
387                                 "rtt min/avg/max/mdev = %.3f/%.3f/%.3f/%.3f ms\n",
388                                 context->host, context->req_sent, context->req_rcvd,
389                                 100.0 * (context->req_sent - context->req_rcvd) / ((double) context->req_sent),
390                                 context->latency_total,
391                                 context->latency_min,
392                                 context->latency_total / ((double) context->req_rcvd),
393                                 context->latency_max,
394                                 0.00);
396                 ping_iterator_set_context (iter, NULL);
397                 free (context);
398         }
400         ping_destroy (ping);
402         return (0);