Code

noping: Print host name in bold.
[liboping.git] / src / oping.c
1 /**
2  * Object oriented C module to send ICMP and ICMPv6 `echo's.
3  * Copyright (C) 2006-2010  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_UNISTD_H
35 # include <unistd.h>
36 #endif
38 #if HAVE_MATH_H
39 # include <math.h>
40 #endif
42 #if TIME_WITH_SYS_TIME
43 # include <sys/time.h>
44 # include <time.h>
45 #else
46 # if HAVE_SYS_TIME_H
47 #  include <sys/time.h>
48 # else
49 #  include <time.h>
50 # endif
51 #endif
53 #if HAVE_NETDB_H
54 # include <netdb.h> /* NI_MAXHOST */
55 #endif
57 #if HAVE_SIGNAL_H
58 # include <signal.h>
59 #endif
61 #if HAVE_SYS_TYPES_H
62 #include <sys/types.h>
63 #endif
65 #if USE_NCURSES
66 # include <ncurses.h>
67 #endif
69 #include "oping.h"
71 #ifndef _POSIX_SAVED_IDS
72 # define _POSIX_SAVED_IDS 0
73 #endif
75 typedef struct ping_context
76 {
77         char host[NI_MAXHOST];
78         char addr[NI_MAXHOST];
80         int req_sent;
81         int req_rcvd;
83         double latency_min;
84         double latency_max;
85         double latency_total;
86         double latency_total_square;
88 #if USE_NCURSES
89         WINDOW *window;
90 #endif
91 } ping_context_t;
93 static double  opt_interval   = 1.0;
94 static int     opt_addrfamily = PING_DEF_AF;
95 static char   *opt_srcaddr    = NULL;
96 static char   *opt_device     = NULL;
97 static char   *opt_filename   = NULL;
98 static int     opt_count      = -1;
99 static int     opt_send_ttl   = 64;
101 #if USE_NCURSES
102 static WINDOW *main_win = NULL;
103 #endif
105 static void sigint_handler (int signal) /* {{{ */
107         /* Make compiler happy */
108         signal = 0;
109         /* Exit the loop */
110         opt_count = 0;
111 } /* }}} void sigint_handler */
113 static ping_context_t *context_create (void)
115         ping_context_t *ret;
117         if ((ret = malloc (sizeof (ping_context_t))) == NULL)
118                 return (NULL);
120         memset (ret, '\0', sizeof (ping_context_t));
122         ret->latency_min   = -1.0;
123         ret->latency_max   = -1.0;
124         ret->latency_total = 0.0;
125         ret->latency_total_square = 0.0;
127 #if USE_NCURSES
128         ret->window = NULL;
129 #endif
131         return (ret);
134 static void context_destroy (ping_context_t *context)
136         if (context == NULL)
137                 return;
139 #if USE_NCURSES
140         if (context->window != NULL)
141         {
142                 delwin (context->window);
143                 context->window = NULL;
144         }
145 #endif
147         free (context);
150 static double context_get_average (ping_context_t *ctx) /* {{{ */
152         double num_total;
154         if (ctx == NULL)
155                 return (-1.0);
157         if (ctx->req_rcvd < 1)
158                 return (-0.0);
160         num_total = (double) ctx->req_rcvd;
161         return (ctx->latency_total / num_total);
162 } /* }}} double context_get_average */
164 static double context_get_stddev (ping_context_t *ctx) /* {{{ */
166         double num_total;
168         if (ctx == NULL)
169                 return (-1.0);
171         if (ctx->req_rcvd < 1)
172                 return (-0.0);
173         else if (ctx->req_rcvd < 2)
174                 return (0.0);
176         num_total = (double) ctx->req_rcvd;
177         return (sqrt (((num_total * ctx->latency_total_square)
178                                         - (ctx->latency_total * ctx->latency_total))
179                                 / (num_total * (num_total - 1.0))));
180 } /* }}} double context_get_stddev */
182 static double context_get_packet_loss (const ping_context_t *ctx) /* {{{ */
184         if (ctx == NULL)
185                 return (-1.0);
187         if (ctx->req_sent < 1)
188                 return (0.0);
190         return (100.0 * (ctx->req_sent - ctx->req_rcvd)
191                         / ((double) ctx->req_sent));
192 } /* }}} double context_get_packet_loss */
194 static void usage_exit (const char *name, int status)
196         int name_length;
198         name_length = (int) strlen (name);
200         fprintf (stderr, "Usage: %s [OPTIONS] "
201                                 "-f filename | host [host [host ...]]\n"
203                         "\nAvailable options:\n"
204                         "  -4|-6        force the use of IPv4 or IPv6\n"
205                         "  -c count     number of ICMP packets to send\n"
206                         "  -i interval  interval with which to send ICMP packets\n"
207                         "  -t ttl       time to live for each ICMP packet\n"
208                         "  -I srcaddr   source address\n"
209                         "  -D device    outgoing interface name\n"
210                         "  -f filename  filename to read hosts from\n"
212                         "\noping "PACKAGE_VERSION", http://verplant.org/liboping/\n"
213                         "by Florian octo Forster <octo@verplant.org>\n"
214                         "for contributions see `AUTHORS'\n",
215                         name);
216         exit (status);
219 static int read_options (int argc, char **argv) /* {{{ */
221         int optchar;
223         while (1)
224         {
225                 optchar = getopt (argc, argv, "46c:hi:I:t:f:D:");
227                 if (optchar == -1)
228                         break;
230                 switch (optchar)
231                 {
232                         case '4':
233                         case '6':
234                                 opt_addrfamily = (optchar == '4') ? AF_INET : AF_INET6;
235                                 break;
237                         case 'c':
238                                 {
239                                         int new_count;
240                                         new_count = atoi (optarg);
241                                         if (new_count > 0)
242                                                 opt_count = new_count;
243                                         else
244                                                 fprintf(stderr, "Ignoring invalid count: %s\n",
245                                                                 optarg);
246                                 }
247                                 break;
249                         case 'f':
250                                 {
251                                         if (opt_filename != NULL)
252                                                 free (opt_filename);
253                                         opt_filename = strdup (optarg);
254                                 }
255                                 break;
257                         case 'i':
258                                 {
259                                         double new_interval;
260                                         new_interval = atof (optarg);
261                                         if (new_interval < 0.001)
262                                                 fprintf (stderr, "Ignoring invalid interval: %s\n",
263                                                                 optarg);
264                                         else
265                                                 opt_interval = new_interval;
266                                 }
267                                 break;
268                         case 'I':
269                                 {
270                                         if (opt_srcaddr != NULL)
271                                                 free (opt_srcaddr);
272                                         opt_srcaddr = strdup (optarg);
273                                 }
274                                 break;
276                         case 'D':
277                                 opt_device = optarg;
278                                 break;
280                         case 't':
281                         {
282                                 int new_send_ttl;
283                                 new_send_ttl = atoi (optarg);
284                                 if ((new_send_ttl > 0) && (new_send_ttl < 256))
285                                         opt_send_ttl = new_send_ttl;
286                                 else
287                                         fprintf (stderr, "Ignoring invalid TTL argument: %s\n",
288                                                         optarg);
289                                 break;
290                         }
292                         case 'h':
293                                 usage_exit (argv[0], 0);
294                                 break;
295                         default:
296                                 usage_exit (argv[0], 1);
297                 }
298         }
300         return (optind);
301 } /* }}} read_options */
303 static void time_normalize (struct timespec *ts) /* {{{ */
305         while (ts->tv_nsec < 0)
306         {
307                 if (ts->tv_sec == 0)
308                 {
309                         ts->tv_nsec = 0;
310                         return;
311                 }
313                 ts->tv_sec  -= 1;
314                 ts->tv_nsec += 1000000000;
315         }
317         while (ts->tv_nsec >= 1000000000)
318         {
319                 ts->tv_sec  += 1;
320                 ts->tv_nsec -= 1000000000;
321         }
322 } /* }}} void time_normalize */
324 static void time_calc (struct timespec *ts_dest, /* {{{ */
325                 const struct timespec *ts_int,
326                 const struct timeval  *tv_begin,
327                 const struct timeval  *tv_end)
329         ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
330         ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
331         time_normalize (ts_dest);
333         /* Assure that `(begin + interval) > end'.
334          * This may seem overly complicated, but `tv_sec' is of type `time_t'
335          * which may be `unsigned. *sigh* */
336         if ((tv_end->tv_sec > ts_dest->tv_sec)
337                         || ((tv_end->tv_sec == ts_dest->tv_sec)
338                                 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
339         {
340                 ts_dest->tv_sec  = 0;
341                 ts_dest->tv_nsec = 0;
342                 return;
343         }
345         ts_dest->tv_sec = ts_dest->tv_sec - tv_end->tv_sec;
346         ts_dest->tv_nsec = ts_dest->tv_nsec - (tv_end->tv_usec * 1000);
347         time_normalize (ts_dest);
348 } /* }}} void time_calc */
350 #if USE_NCURSES
351 static int context_window_repaint (ping_context_t *ctx, /* {{{ */
352                 int index)
354         if (ctx == NULL)
355                 return (EINVAL);
357         if (ctx->window == NULL)
358         {
359                 ctx->window = newwin (/* height = */ 4, /* width = */ 0,
360                                 /* start y = */ 4 * index, /* start x = */ 0);
361         }
362         else /* if (ctx->window != NULL) */
363         {
364                 werase (ctx->window);
365         }
367         box (ctx->window, 0, 0);
368         wattron (ctx->window, A_BOLD);
369         mvwprintw (ctx->window, /* y = */ 0, /* x = */ 5,
370                         " %s ", ctx->host);
371         wattroff (ctx->window, A_BOLD);
372         wprintw (ctx->window, "ping statistics ");
373         mvwprintw (ctx->window, /* y = */ 1, /* x = */ 2,
374                         "%i packets transmitted, %i received, %.2f%% packet "
375                         "loss, time %.1fms",
376                         ctx->req_sent, ctx->req_rcvd,
377                         context_get_packet_loss (ctx),
378                         ctx->latency_total);
379         if (ctx->req_rcvd != 0)
380         {
381                 double average;
382                 double deviation;
384                 average = context_get_average (ctx);
385                 deviation = context_get_stddev (ctx);
386                         
387                 mvwprintw (ctx->window, /* y = */ 2, /* x = */ 2,
388                                 "rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms",
389                                 ctx->latency_min,
390                                 average,
391                                 ctx->latency_max,
392                                 deviation);
393         }
395         wrefresh (ctx->window);
397         return (0);
398 } /* }}} int context_window_repaint */
400 static int resize_windows (pingobj_t *ping) /* {{{ */
402         int index;
403         pingobj_iter_t *iter;
404         int width = 0;
405         int height = 0;
406         int need_resize = 0;
408         while (42)
409         {
410                 int key = wgetch (stdscr);
411                 if (key == ERR)
412                         break;
413                 else if (key == KEY_RESIZE)
414                         need_resize = 1;
415         }
417         if (!need_resize)
418                 return (0);
420         getmaxyx (stdscr, height, width);
421         if ((height < 1) || (width < 1))
422                 return (EINVAL);
424         index = 0;
425         for (iter = ping_iterator_get (ping);
426                         iter != NULL;
427                         iter = ping_iterator_next (iter))
428         {
429                 ping_context_t *ctx = ping_iterator_get_context (iter);
431                 if (ctx->window == NULL)
432                 {
433                         index++;
434                         continue;
435                 }
437                 wresize (ctx->window, 4, width);
438                 context_window_repaint (ctx, index);
440                 index++;
441         }
443         if (main_win != NULL)
444         {
445                 wresize (main_win, height - (4 * index), width);
446                 /* touchwin (main_win); */
447                 /* wrefresh (main_win); */
448                 clearok (main_win, TRUE);
449         }
451         return (0);
452 } /* }}} int resize_windows */
453 #endif
455 static void print_host (pingobj_iter_t *iter, /* {{{ */
456                 int index)
458         double          latency;
459         unsigned int    sequence;
460         int             recv_ttl;
461         size_t          buffer_len;
462         size_t          data_len;
463         ping_context_t *context;
465         latency = -1.0;
466         buffer_len = sizeof (latency);
467         ping_iterator_get_info (iter, PING_INFO_LATENCY,
468                         &latency, &buffer_len);
470         sequence = 0;
471         buffer_len = sizeof (sequence);
472         ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
473                         &sequence, &buffer_len);
475         recv_ttl = -1;
476         buffer_len = sizeof (recv_ttl);
477         ping_iterator_get_info (iter, PING_INFO_RECV_TTL,
478                         &recv_ttl, &buffer_len);
480         data_len = 0;
481         ping_iterator_get_info (iter, PING_INFO_DATA,
482                         NULL, &data_len);
484         context = (ping_context_t *) ping_iterator_get_context (iter);
486 #if USE_NCURSES
487 # define HOST_PRINTF(...) wprintw(main_win, __VA_ARGS__)
488 #else
489 # define HOST_PRINTF(...) printf(__VA_ARGS__)
490 #endif
492         context->req_sent++;
493         if (latency > 0.0)
494         {
495                 context->req_rcvd++;
496                 context->latency_total += latency;
497                 context->latency_total_square += (latency * latency);
499                 if ((context->latency_max < 0.0) || (context->latency_max < latency))
500                         context->latency_max = latency;
501                 if ((context->latency_min < 0.0) || (context->latency_min > latency))
502                         context->latency_min = latency;
504                 HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i "
505                                 "time=%.2f ms\n",
506                                 data_len,
507                                 context->host, context->addr,
508                                 sequence, recv_ttl, latency);
509         }
510         else
511         {
512                 HOST_PRINTF ("echo reply from %s (%s): icmp_seq=%u timeout\n",
513                                 context->host, context->addr,
514                                 sequence);
515         }
517 #if USE_NCURSES
518         context_window_repaint (context, index);
519         wrefresh (main_win);
520 #endif
521 } /* }}} void print_host */
523 static int print_header (pingobj_t *ping) /* {{{ */
525         pingobj_iter_t *iter;
526         int index;
528 #if USE_NCURSES
529         initscr ();
530         cbreak ();
531         noecho ();
532         nodelay (stdscr, TRUE);
533 #endif
535         index = 0;
536         for (iter = ping_iterator_get (ping);
537                         iter != NULL;
538                         iter = ping_iterator_next (iter))
539         {
540                 ping_context_t *context;
541                 size_t buffer_size;
543                 context = context_create ();
545                 buffer_size = sizeof (context->host);
546                 ping_iterator_get_info (iter, PING_INFO_HOSTNAME, context->host, &buffer_size);
548                 buffer_size = sizeof (context->addr);
549                 ping_iterator_get_info (iter, PING_INFO_ADDRESS, context->addr, &buffer_size);
551                 buffer_size = 0;
552                 ping_iterator_get_info (iter, PING_INFO_DATA, NULL, &buffer_size);
554 #if USE_NCURSES
555                 context_window_repaint (context, index);
556 #else /* !USE_NCURSES */
557                 printf ("PING %s (%s) %zu bytes of data.\n",
558                                 context->host, context->addr, buffer_size);
559 #endif
561                 ping_iterator_set_context (iter, (void *) context);
563                 index++;
564         }
566 #if USE_NCURSES
567         main_win = newwin (/* height = */ 0, /* width = */ 0,
568                         /* y = */ 4 * index, /* x = */ 0);
569         /* Allow scrolling */
570         scrollok (main_win, TRUE);
571         /* Allow hardware accelerated scrolling. */
572         idlok (main_win, TRUE);
574         /* Don't know what good this does exactly, but without this code
575          * "resize_windows" will be called right after startup and *somehow*
576          * this leads to display errors. If we purge all initial characters
577          * here, the problem goes away. "wgetch" is non-blocking due to
578          * "nodelay" (see above). */
579         while (wgetch (stdscr) != ERR)
580         {
581                 /* eat up characters */;
582         }
583 #endif
585         return (0);
586 } /* }}} int print_header */
588 static int print_footer (pingobj_t *ping) /* {{{ */
590         pingobj_iter_t *iter;
592 #if USE_NCURSES
593         endwin ();
594 #endif
596         for (iter = ping_iterator_get (ping);
597                         iter != NULL;
598                         iter = ping_iterator_next (iter))
599         {
600                 ping_context_t *context;
602                 context = ping_iterator_get_context (iter);
604                 printf ("\n--- %s ping statistics ---\n"
605                                 "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n",
606                                 context->host, context->req_sent, context->req_rcvd,
607                                 context_get_packet_loss (context),
608                                 context->latency_total);
610                 if (context->req_rcvd != 0)
611                 {
612                         double average;
613                         double deviation;
615                         average = context_get_average (context);
616                         deviation = context_get_stddev (context);
618                         printf ("rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms\n",
619                                         context->latency_min,
620                                         average,
621                                         context->latency_max,
622                                         deviation);
623                 }
625                 ping_iterator_set_context (iter, NULL);
626                 context_destroy (context);
627         }
629         return (0);
630 } /* }}} int print_footer */
632 int main (int argc, char **argv) /* {{{ */
634         pingobj_t      *ping;
635         pingobj_iter_t *iter;
637         struct sigaction sigint_action;
639         struct timeval  tv_begin;
640         struct timeval  tv_end;
641         struct timespec ts_wait;
642         struct timespec ts_int;
644         int optind;
645         int i;
646         int status;
647 #if _POSIX_SAVED_IDS
648         uid_t saved_set_uid;
650         /* Save the old effective user id */
651         saved_set_uid = geteuid ();
652         /* Set the effective user ID to the real user ID without changing the
653          * saved set-user ID */
654         status = seteuid (getuid ());
655         if (status != 0)
656         {
657                 fprintf (stderr, "Temporarily dropping privileges "
658                                 "failed: %s\n", strerror (errno));
659                 exit (EXIT_FAILURE);
660         }
661 #endif
663         optind = read_options (argc, argv);
665 #if !_POSIX_SAVED_IDS
666         /* Cannot temporarily drop privileges -> reject every file but "-". */
667         if ((opt_filename != NULL)
668                         && (strcmp ("-", opt_filename) != 0)
669                         && (getuid () != geteuid ()))
670         {
671                 fprintf (stderr, "Your real and effective user IDs don't "
672                                 "match. Reading from a file (option '-f')\n"
673                                 "is therefore too risky. You can still read "
674                                 "from STDIN using '-f -' if you like.\n"
675                                 "Sorry.\n");
676                 exit (EXIT_FAILURE);
677         }
678 #endif
680         if ((optind >= argc) && (opt_filename == NULL)) {
681                 usage_exit (argv[0], 1);
682         }
684         if ((ping = ping_construct ()) == NULL)
685         {
686                 fprintf (stderr, "ping_construct failed\n");
687                 return (1);
688         }
690         if (ping_setopt (ping, PING_OPT_TTL, &opt_send_ttl) != 0)
691         {
692                 fprintf (stderr, "Setting TTL to %i failed: %s\n",
693                                 opt_send_ttl, ping_get_error (ping));
694         }
696         {
697                 double temp_sec;
698                 double temp_nsec;
700                 temp_nsec = modf (opt_interval, &temp_sec);
701                 ts_int.tv_sec  = (time_t) temp_sec;
702                 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
704                 /* printf ("ts_int = %i.%09li\n", (int) ts_int.tv_sec, ts_int.tv_nsec); */
705         }
707         if (opt_addrfamily != PING_DEF_AF)
708                 ping_setopt (ping, PING_OPT_AF, (void *) &opt_addrfamily);
710         if (opt_srcaddr != NULL)
711         {
712                 if (ping_setopt (ping, PING_OPT_SOURCE, (void *) opt_srcaddr) != 0)
713                 {
714                         fprintf (stderr, "Setting source address failed: %s\n",
715                                         ping_get_error (ping));
716                 }
717         }
719         if (opt_device != NULL)
720         {
721                 if (ping_setopt (ping, PING_OPT_DEVICE, (void *) opt_device) != 0)
722                 {
723                         fprintf (stderr, "Setting device failed: %s\n",
724                                         ping_get_error (ping));
725                 }
726         }
728         if (opt_filename != NULL)
729         {
730                 FILE *infile;
731                 char line[256];
732                 char host[256];
734                 if (strcmp (opt_filename, "-") == 0)
735                         /* Open STDIN */
736                         infile = fdopen(0, "r");
737                 else
738                         infile = fopen(opt_filename, "r");
740                 if (infile == NULL)
741                 {
742                         fprintf (stderr, "Opening %s failed: %s\n",
743                                         (strcmp (opt_filename, "-") == 0)
744                                         ? "STDIN" : opt_filename,
745                                         strerror(errno));
746                         return (1);
747                 }
749 #if _POSIX_SAVED_IDS
750                 /* Regain privileges */
751                 status = seteuid (saved_set_uid);
752                 if (status != 0)
753                 {
754                         fprintf (stderr, "Temporarily re-gaining privileges "
755                                         "failed: %s\n", strerror (errno));
756                         exit (EXIT_FAILURE);
757                 }
758 #endif
760                 while (fgets(line, sizeof(line), infile))
761                 {
762                         /* Strip whitespace */
763                         if (sscanf(line, "%s", host) != 1)
764                                 continue;
766                         if ((host[0] == 0) || (host[0] == '#'))
767                                 continue;
769                         if (ping_host_add(ping, host) < 0)
770                         {
771                                 const char *errmsg = ping_get_error (ping);
773                                 fprintf (stderr, "Adding host `%s' failed: %s\n", host, errmsg);
774                                 continue;
775                         }
776                 }
778 #if _POSIX_SAVED_IDS
779                 /* Drop privileges */
780                 status = seteuid (getuid ());
781                 if (status != 0)
782                 {
783                         fprintf (stderr, "Temporarily dropping privileges "
784                                         "failed: %s\n", strerror (errno));
785                         exit (EXIT_FAILURE);
786                 }
787 #endif
789                 fclose(infile);
790         }
792 #if _POSIX_SAVED_IDS
793         /* Regain privileges */
794         status = seteuid (saved_set_uid);
795         if (status != 0)
796         {
797                 fprintf (stderr, "Temporarily re-gaining privileges "
798                                 "failed: %s\n", strerror (errno));
799                 exit (EXIT_FAILURE);
800         }
801 #endif
803         for (i = optind; i < argc; i++)
804         {
805                 if (ping_host_add (ping, argv[i]) < 0)
806                 {
807                         const char *errmsg = ping_get_error (ping);
809                         fprintf (stderr, "Adding host `%s' failed: %s\n", argv[i], errmsg);
810                         continue;
811                 }
812         }
814         /* Permanently drop root privileges if we're setuid-root. */
815         status = setuid (getuid ());
816         if (status != 0)
817         {
818                 fprintf (stderr, "Dropping privileges failed: %s\n",
819                                 strerror (errno));
820                 exit (EXIT_FAILURE);
821         }
823 #if _POSIX_SAVED_IDS
824         saved_set_uid = (uid_t) -1;
825 #endif
827         print_header (ping);
829         if (i == 0)
830                 return (1);
832         memset (&sigint_action, '\0', sizeof (sigint_action));
833         sigint_action.sa_handler = sigint_handler;
834         if (sigaction (SIGINT, &sigint_action, NULL) < 0)
835         {
836                 perror ("sigaction");
837                 return (1);
838         }
840         while (opt_count != 0)
841         {
842                 int index;
843                 int status;
845                 if (gettimeofday (&tv_begin, NULL) < 0)
846                 {
847                         perror ("gettimeofday");
848                         return (1);
849                 }
851                 if (ping_send (ping) < 0)
852                 {
853                         fprintf (stderr, "ping_send failed: %s\n",
854                                         ping_get_error (ping));
855                         return (1);
856                 }
858                 index = 0;
859                 for (iter = ping_iterator_get (ping);
860                                 iter != NULL;
861                                 iter = ping_iterator_next (iter))
862                 {
863                         print_host (iter, index);
864                         index++;
865                 }
866 #if !USE_NCURSES
867                 fflush (stdout);
868 #endif
870                 /* Don't sleep in the last iteration */
871                 if (opt_count == 1)
872                         break;
874                 if (gettimeofday (&tv_end, NULL) < 0)
875                 {
876                         perror ("gettimeofday");
877                         return (1);
878                 }
880                 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
882                 /* printf ("Sleeping for %i.%09li seconds\n", (int) ts_wait.tv_sec, ts_wait.tv_nsec); */
883                 while ((status = nanosleep (&ts_wait, &ts_wait)) != 0)
884                 {
885                         if (errno != EINTR)
886                         {
887                                 perror ("nanosleep");
888                                 break;
889                         }
890                         else if (opt_count == 0)
891                         {
892                                 /* sigint */
893                                 break;
894                         }
896 #if USE_NCURSES
897                         resize_windows (ping);
898 #endif
899                 }
901 #if USE_NCURSES
902                 resize_windows (ping);
903 #endif
905                 if (opt_count > 0)
906                         opt_count--;
907         } /* while (opt_count != 0) */
909         print_footer (ping);
911         ping_destroy (ping);
913         return (0);
914 } /* }}} int main */
916 /* vim: set fdm=marker : */