Code

noping: Use colors to highlight "unusual" response times.
[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>
68 # define OPING_GREEN 1
69 # define OPING_YELLOW 2
70 # define OPING_RED 3
71 #endif
73 #include "oping.h"
75 #ifndef _POSIX_SAVED_IDS
76 # define _POSIX_SAVED_IDS 0
77 #endif
79 typedef struct ping_context
80 {
81         char host[NI_MAXHOST];
82         char addr[NI_MAXHOST];
84         int req_sent;
85         int req_rcvd;
87         double latency_min;
88         double latency_max;
89         double latency_total;
90         double latency_total_square;
92 #if USE_NCURSES
93         WINDOW *window;
94 #endif
95 } ping_context_t;
97 static double  opt_interval   = 1.0;
98 static int     opt_addrfamily = PING_DEF_AF;
99 static char   *opt_srcaddr    = NULL;
100 static char   *opt_device     = NULL;
101 static char   *opt_filename   = NULL;
102 static int     opt_count      = -1;
103 static int     opt_send_ttl   = 64;
105 #if USE_NCURSES
106 static WINDOW *main_win = NULL;
107 #endif
109 static void sigint_handler (int signal) /* {{{ */
111         /* Make compiler happy */
112         signal = 0;
113         /* Exit the loop */
114         opt_count = 0;
115 } /* }}} void sigint_handler */
117 static ping_context_t *context_create (void)
119         ping_context_t *ret;
121         if ((ret = malloc (sizeof (ping_context_t))) == NULL)
122                 return (NULL);
124         memset (ret, '\0', sizeof (ping_context_t));
126         ret->latency_min   = -1.0;
127         ret->latency_max   = -1.0;
128         ret->latency_total = 0.0;
129         ret->latency_total_square = 0.0;
131 #if USE_NCURSES
132         ret->window = NULL;
133 #endif
135         return (ret);
138 static void context_destroy (ping_context_t *context)
140         if (context == NULL)
141                 return;
143 #if USE_NCURSES
144         if (context->window != NULL)
145         {
146                 delwin (context->window);
147                 context->window = NULL;
148         }
149 #endif
151         free (context);
154 static double context_get_average (ping_context_t *ctx) /* {{{ */
156         double num_total;
158         if (ctx == NULL)
159                 return (-1.0);
161         if (ctx->req_rcvd < 1)
162                 return (-0.0);
164         num_total = (double) ctx->req_rcvd;
165         return (ctx->latency_total / num_total);
166 } /* }}} double context_get_average */
168 static double context_get_stddev (ping_context_t *ctx) /* {{{ */
170         double num_total;
172         if (ctx == NULL)
173                 return (-1.0);
175         if (ctx->req_rcvd < 1)
176                 return (-0.0);
177         else if (ctx->req_rcvd < 2)
178                 return (0.0);
180         num_total = (double) ctx->req_rcvd;
181         return (sqrt (((num_total * ctx->latency_total_square)
182                                         - (ctx->latency_total * ctx->latency_total))
183                                 / (num_total * (num_total - 1.0))));
184 } /* }}} double context_get_stddev */
186 static double context_get_packet_loss (const ping_context_t *ctx) /* {{{ */
188         if (ctx == NULL)
189                 return (-1.0);
191         if (ctx->req_sent < 1)
192                 return (0.0);
194         return (100.0 * (ctx->req_sent - ctx->req_rcvd)
195                         / ((double) ctx->req_sent));
196 } /* }}} double context_get_packet_loss */
198 static void usage_exit (const char *name, int status)
200         int name_length;
202         name_length = (int) strlen (name);
204         fprintf (stderr, "Usage: %s [OPTIONS] "
205                                 "-f filename | host [host [host ...]]\n"
207                         "\nAvailable options:\n"
208                         "  -4|-6        force the use of IPv4 or IPv6\n"
209                         "  -c count     number of ICMP packets to send\n"
210                         "  -i interval  interval with which to send ICMP packets\n"
211                         "  -t ttl       time to live for each ICMP packet\n"
212                         "  -I srcaddr   source address\n"
213                         "  -D device    outgoing interface name\n"
214                         "  -f filename  filename to read hosts from\n"
216                         "\noping "PACKAGE_VERSION", http://verplant.org/liboping/\n"
217                         "by Florian octo Forster <octo@verplant.org>\n"
218                         "for contributions see `AUTHORS'\n",
219                         name);
220         exit (status);
223 static int read_options (int argc, char **argv) /* {{{ */
225         int optchar;
227         while (1)
228         {
229                 optchar = getopt (argc, argv, "46c:hi:I:t:f:D:");
231                 if (optchar == -1)
232                         break;
234                 switch (optchar)
235                 {
236                         case '4':
237                         case '6':
238                                 opt_addrfamily = (optchar == '4') ? AF_INET : AF_INET6;
239                                 break;
241                         case 'c':
242                                 {
243                                         int new_count;
244                                         new_count = atoi (optarg);
245                                         if (new_count > 0)
246                                                 opt_count = new_count;
247                                         else
248                                                 fprintf(stderr, "Ignoring invalid count: %s\n",
249                                                                 optarg);
250                                 }
251                                 break;
253                         case 'f':
254                                 {
255                                         if (opt_filename != NULL)
256                                                 free (opt_filename);
257                                         opt_filename = strdup (optarg);
258                                 }
259                                 break;
261                         case 'i':
262                                 {
263                                         double new_interval;
264                                         new_interval = atof (optarg);
265                                         if (new_interval < 0.001)
266                                                 fprintf (stderr, "Ignoring invalid interval: %s\n",
267                                                                 optarg);
268                                         else
269                                                 opt_interval = new_interval;
270                                 }
271                                 break;
272                         case 'I':
273                                 {
274                                         if (opt_srcaddr != NULL)
275                                                 free (opt_srcaddr);
276                                         opt_srcaddr = strdup (optarg);
277                                 }
278                                 break;
280                         case 'D':
281                                 opt_device = optarg;
282                                 break;
284                         case 't':
285                         {
286                                 int new_send_ttl;
287                                 new_send_ttl = atoi (optarg);
288                                 if ((new_send_ttl > 0) && (new_send_ttl < 256))
289                                         opt_send_ttl = new_send_ttl;
290                                 else
291                                         fprintf (stderr, "Ignoring invalid TTL argument: %s\n",
292                                                         optarg);
293                                 break;
294                         }
296                         case 'h':
297                                 usage_exit (argv[0], 0);
298                                 break;
299                         default:
300                                 usage_exit (argv[0], 1);
301                 }
302         }
304         return (optind);
305 } /* }}} read_options */
307 static void time_normalize (struct timespec *ts) /* {{{ */
309         while (ts->tv_nsec < 0)
310         {
311                 if (ts->tv_sec == 0)
312                 {
313                         ts->tv_nsec = 0;
314                         return;
315                 }
317                 ts->tv_sec  -= 1;
318                 ts->tv_nsec += 1000000000;
319         }
321         while (ts->tv_nsec >= 1000000000)
322         {
323                 ts->tv_sec  += 1;
324                 ts->tv_nsec -= 1000000000;
325         }
326 } /* }}} void time_normalize */
328 static void time_calc (struct timespec *ts_dest, /* {{{ */
329                 const struct timespec *ts_int,
330                 const struct timeval  *tv_begin,
331                 const struct timeval  *tv_end)
333         ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
334         ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
335         time_normalize (ts_dest);
337         /* Assure that `(begin + interval) > end'.
338          * This may seem overly complicated, but `tv_sec' is of type `time_t'
339          * which may be `unsigned. *sigh* */
340         if ((tv_end->tv_sec > ts_dest->tv_sec)
341                         || ((tv_end->tv_sec == ts_dest->tv_sec)
342                                 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
343         {
344                 ts_dest->tv_sec  = 0;
345                 ts_dest->tv_nsec = 0;
346                 return;
347         }
349         ts_dest->tv_sec = ts_dest->tv_sec - tv_end->tv_sec;
350         ts_dest->tv_nsec = ts_dest->tv_nsec - (tv_end->tv_usec * 1000);
351         time_normalize (ts_dest);
352 } /* }}} void time_calc */
354 #if USE_NCURSES
355 static int context_window_repaint (ping_context_t *ctx, /* {{{ */
356                 int index)
358         if (ctx == NULL)
359                 return (EINVAL);
361         if (ctx->window == NULL)
362         {
363                 ctx->window = newwin (/* height = */ 4, /* width = */ 0,
364                                 /* start y = */ 4 * index, /* start x = */ 0);
365         }
366         else /* if (ctx->window != NULL) */
367         {
368                 werase (ctx->window);
369         }
371         box (ctx->window, 0, 0);
372         wattron (ctx->window, A_BOLD);
373         mvwprintw (ctx->window, /* y = */ 0, /* x = */ 5,
374                         " %s ", ctx->host);
375         wattroff (ctx->window, A_BOLD);
376         wprintw (ctx->window, "ping statistics ");
377         mvwprintw (ctx->window, /* y = */ 1, /* x = */ 2,
378                         "%i packets transmitted, %i received, %.2f%% packet "
379                         "loss, time %.1fms",
380                         ctx->req_sent, ctx->req_rcvd,
381                         context_get_packet_loss (ctx),
382                         ctx->latency_total);
383         if (ctx->req_rcvd != 0)
384         {
385                 double average;
386                 double deviation;
388                 average = context_get_average (ctx);
389                 deviation = context_get_stddev (ctx);
390                         
391                 mvwprintw (ctx->window, /* y = */ 2, /* x = */ 2,
392                                 "rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms",
393                                 ctx->latency_min,
394                                 average,
395                                 ctx->latency_max,
396                                 deviation);
397         }
399         wrefresh (ctx->window);
401         return (0);
402 } /* }}} int context_window_repaint */
404 static int resize_windows (pingobj_t *ping) /* {{{ */
406         int index;
407         pingobj_iter_t *iter;
408         int width = 0;
409         int height = 0;
410         int need_resize = 0;
412         while (42)
413         {
414                 int key = wgetch (stdscr);
415                 if (key == ERR)
416                         break;
417                 else if (key == KEY_RESIZE)
418                         need_resize = 1;
419         }
421         if (!need_resize)
422                 return (0);
424         getmaxyx (stdscr, height, width);
425         if ((height < 1) || (width < 1))
426                 return (EINVAL);
428         index = 0;
429         for (iter = ping_iterator_get (ping);
430                         iter != NULL;
431                         iter = ping_iterator_next (iter))
432         {
433                 ping_context_t *ctx = ping_iterator_get_context (iter);
435                 if (ctx->window == NULL)
436                 {
437                         index++;
438                         continue;
439                 }
441                 wresize (ctx->window, 4, width);
442                 context_window_repaint (ctx, index);
444                 index++;
445         }
447         if (main_win != NULL)
448         {
449                 wresize (main_win, height - (4 * index), width);
450                 /* touchwin (main_win); */
451                 /* wrefresh (main_win); */
452                 clearok (main_win, TRUE);
453         }
455         return (0);
456 } /* }}} int resize_windows */
457 #endif
459 static void print_host (pingobj_iter_t *iter, /* {{{ */
460                 int index)
462         double          latency;
463         unsigned int    sequence;
464         int             recv_ttl;
465         size_t          buffer_len;
466         size_t          data_len;
467         ping_context_t *context;
469         latency = -1.0;
470         buffer_len = sizeof (latency);
471         ping_iterator_get_info (iter, PING_INFO_LATENCY,
472                         &latency, &buffer_len);
474         sequence = 0;
475         buffer_len = sizeof (sequence);
476         ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
477                         &sequence, &buffer_len);
479         recv_ttl = -1;
480         buffer_len = sizeof (recv_ttl);
481         ping_iterator_get_info (iter, PING_INFO_RECV_TTL,
482                         &recv_ttl, &buffer_len);
484         data_len = 0;
485         ping_iterator_get_info (iter, PING_INFO_DATA,
486                         NULL, &data_len);
488         context = (ping_context_t *) ping_iterator_get_context (iter);
490 #if USE_NCURSES
491 # define HOST_PRINTF(...) wprintw(main_win, __VA_ARGS__)
492 #else
493 # define HOST_PRINTF(...) printf(__VA_ARGS__)
494 #endif
496         context->req_sent++;
497         if (latency > 0.0)
498         {
499                 context->req_rcvd++;
500                 context->latency_total += latency;
501                 context->latency_total_square += (latency * latency);
503                 if ((context->latency_max < 0.0) || (context->latency_max < latency))
504                         context->latency_max = latency;
505                 if ((context->latency_min < 0.0) || (context->latency_min > latency))
506                         context->latency_min = latency;
508 #if USE_NCURSES
509                 if (has_colors () == TRUE)
510                 {
511                         int color = OPING_GREEN;
512                         double average = context_get_average (context);
513                         double stddev = context_get_stddev (context);
515                         if ((latency < (average - (2 * stddev)))
516                                         || (latency > (average + (2 * stddev))))
517                                 color = OPING_RED;
518                         else if ((latency < (average - stddev))
519                                         || (latency > (average + stddev)))
520                                 color = OPING_YELLOW;
522                         HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i "
523                                         "time=",
524                                         data_len, context->host, context->addr,
525                                         sequence, recv_ttl);
526                         wattron (main_win, COLOR_PAIR(color));
527                         HOST_PRINTF ("%.2f", latency);
528                         wattroff (main_win, COLOR_PAIR(color));
529                         HOST_PRINTF (" ms\n");
530                 }
531                 else
532                 {
533 #endif
534                 HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i "
535                                 "time=%.2f ms\n",
536                                 data_len,
537                                 context->host, context->addr,
538                                 sequence, recv_ttl, latency);
539 #if USE_NCURSES
540                 }
541 #endif
542         }
543         else
544         {
545 #if USE_NCURSES
546                 if (has_colors () == TRUE)
547                 {
548                         HOST_PRINTF ("echo reply from %s (%s): icmp_seq=%u ",
549                                         context->host, context->addr,
550                                         sequence);
551                         wattron (main_win, COLOR_PAIR(OPING_RED) | A_BOLD);
552                         HOST_PRINTF ("timeout");
553                         wattroff (main_win, COLOR_PAIR(OPING_RED) | A_BOLD);
554                         HOST_PRINTF ("\n");
555                 }
556                 else
557                 {
558 #endif
559                 HOST_PRINTF ("echo reply from %s (%s): icmp_seq=%u timeout\n",
560                                 context->host, context->addr,
561                                 sequence);
562 #if USE_NCURSES
563                 }
564 #endif
565         }
567 #if USE_NCURSES
568         context_window_repaint (context, index);
569         wrefresh (main_win);
570 #endif
571 } /* }}} void print_host */
573 static int print_header (pingobj_t *ping) /* {{{ */
575         pingobj_iter_t *iter;
576         int index;
578 #if USE_NCURSES
579         initscr ();
580         cbreak ();
581         noecho ();
582         nodelay (stdscr, TRUE);
584         if (has_colors () == TRUE)
585         {
586                 start_color ();
587                 init_pair (OPING_GREEN,  COLOR_GREEN,  /* default = */ 0);
588                 init_pair (OPING_YELLOW, COLOR_YELLOW, /* default = */ 0);
589                 init_pair (OPING_RED,    COLOR_RED,    /* default = */ 0);
590         }
591 #endif
593         index = 0;
594         for (iter = ping_iterator_get (ping);
595                         iter != NULL;
596                         iter = ping_iterator_next (iter))
597         {
598                 ping_context_t *context;
599                 size_t buffer_size;
601                 context = context_create ();
603                 buffer_size = sizeof (context->host);
604                 ping_iterator_get_info (iter, PING_INFO_HOSTNAME, context->host, &buffer_size);
606                 buffer_size = sizeof (context->addr);
607                 ping_iterator_get_info (iter, PING_INFO_ADDRESS, context->addr, &buffer_size);
609                 buffer_size = 0;
610                 ping_iterator_get_info (iter, PING_INFO_DATA, NULL, &buffer_size);
612 #if USE_NCURSES
613                 context_window_repaint (context, index);
614 #else /* !USE_NCURSES */
615                 printf ("PING %s (%s) %zu bytes of data.\n",
616                                 context->host, context->addr, buffer_size);
617 #endif
619                 ping_iterator_set_context (iter, (void *) context);
621                 index++;
622         }
624 #if USE_NCURSES
625         main_win = newwin (/* height = */ 0, /* width = */ 0,
626                         /* y = */ 4 * index, /* x = */ 0);
627         /* Allow scrolling */
628         scrollok (main_win, TRUE);
629         /* Allow hardware accelerated scrolling. */
630         idlok (main_win, TRUE);
632         /* Don't know what good this does exactly, but without this code
633          * "resize_windows" will be called right after startup and *somehow*
634          * this leads to display errors. If we purge all initial characters
635          * here, the problem goes away. "wgetch" is non-blocking due to
636          * "nodelay" (see above). */
637         while (wgetch (stdscr) != ERR)
638         {
639                 /* eat up characters */;
640         }
641 #endif
643         return (0);
644 } /* }}} int print_header */
646 static int print_footer (pingobj_t *ping) /* {{{ */
648         pingobj_iter_t *iter;
650 #if USE_NCURSES
651         endwin ();
652 #endif
654         for (iter = ping_iterator_get (ping);
655                         iter != NULL;
656                         iter = ping_iterator_next (iter))
657         {
658                 ping_context_t *context;
660                 context = ping_iterator_get_context (iter);
662                 printf ("\n--- %s ping statistics ---\n"
663                                 "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n",
664                                 context->host, context->req_sent, context->req_rcvd,
665                                 context_get_packet_loss (context),
666                                 context->latency_total);
668                 if (context->req_rcvd != 0)
669                 {
670                         double average;
671                         double deviation;
673                         average = context_get_average (context);
674                         deviation = context_get_stddev (context);
676                         printf ("rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms\n",
677                                         context->latency_min,
678                                         average,
679                                         context->latency_max,
680                                         deviation);
681                 }
683                 ping_iterator_set_context (iter, NULL);
684                 context_destroy (context);
685         }
687         return (0);
688 } /* }}} int print_footer */
690 int main (int argc, char **argv) /* {{{ */
692         pingobj_t      *ping;
693         pingobj_iter_t *iter;
695         struct sigaction sigint_action;
697         struct timeval  tv_begin;
698         struct timeval  tv_end;
699         struct timespec ts_wait;
700         struct timespec ts_int;
702         int optind;
703         int i;
704         int status;
705 #if _POSIX_SAVED_IDS
706         uid_t saved_set_uid;
708         /* Save the old effective user id */
709         saved_set_uid = geteuid ();
710         /* Set the effective user ID to the real user ID without changing the
711          * saved set-user ID */
712         status = seteuid (getuid ());
713         if (status != 0)
714         {
715                 fprintf (stderr, "Temporarily dropping privileges "
716                                 "failed: %s\n", strerror (errno));
717                 exit (EXIT_FAILURE);
718         }
719 #endif
721         optind = read_options (argc, argv);
723 #if !_POSIX_SAVED_IDS
724         /* Cannot temporarily drop privileges -> reject every file but "-". */
725         if ((opt_filename != NULL)
726                         && (strcmp ("-", opt_filename) != 0)
727                         && (getuid () != geteuid ()))
728         {
729                 fprintf (stderr, "Your real and effective user IDs don't "
730                                 "match. Reading from a file (option '-f')\n"
731                                 "is therefore too risky. You can still read "
732                                 "from STDIN using '-f -' if you like.\n"
733                                 "Sorry.\n");
734                 exit (EXIT_FAILURE);
735         }
736 #endif
738         if ((optind >= argc) && (opt_filename == NULL)) {
739                 usage_exit (argv[0], 1);
740         }
742         if ((ping = ping_construct ()) == NULL)
743         {
744                 fprintf (stderr, "ping_construct failed\n");
745                 return (1);
746         }
748         if (ping_setopt (ping, PING_OPT_TTL, &opt_send_ttl) != 0)
749         {
750                 fprintf (stderr, "Setting TTL to %i failed: %s\n",
751                                 opt_send_ttl, ping_get_error (ping));
752         }
754         {
755                 double temp_sec;
756                 double temp_nsec;
758                 temp_nsec = modf (opt_interval, &temp_sec);
759                 ts_int.tv_sec  = (time_t) temp_sec;
760                 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
762                 /* printf ("ts_int = %i.%09li\n", (int) ts_int.tv_sec, ts_int.tv_nsec); */
763         }
765         if (opt_addrfamily != PING_DEF_AF)
766                 ping_setopt (ping, PING_OPT_AF, (void *) &opt_addrfamily);
768         if (opt_srcaddr != NULL)
769         {
770                 if (ping_setopt (ping, PING_OPT_SOURCE, (void *) opt_srcaddr) != 0)
771                 {
772                         fprintf (stderr, "Setting source address failed: %s\n",
773                                         ping_get_error (ping));
774                 }
775         }
777         if (opt_device != NULL)
778         {
779                 if (ping_setopt (ping, PING_OPT_DEVICE, (void *) opt_device) != 0)
780                 {
781                         fprintf (stderr, "Setting device failed: %s\n",
782                                         ping_get_error (ping));
783                 }
784         }
786         if (opt_filename != NULL)
787         {
788                 FILE *infile;
789                 char line[256];
790                 char host[256];
792                 if (strcmp (opt_filename, "-") == 0)
793                         /* Open STDIN */
794                         infile = fdopen(0, "r");
795                 else
796                         infile = fopen(opt_filename, "r");
798                 if (infile == NULL)
799                 {
800                         fprintf (stderr, "Opening %s failed: %s\n",
801                                         (strcmp (opt_filename, "-") == 0)
802                                         ? "STDIN" : opt_filename,
803                                         strerror(errno));
804                         return (1);
805                 }
807 #if _POSIX_SAVED_IDS
808                 /* Regain privileges */
809                 status = seteuid (saved_set_uid);
810                 if (status != 0)
811                 {
812                         fprintf (stderr, "Temporarily re-gaining privileges "
813                                         "failed: %s\n", strerror (errno));
814                         exit (EXIT_FAILURE);
815                 }
816 #endif
818                 while (fgets(line, sizeof(line), infile))
819                 {
820                         /* Strip whitespace */
821                         if (sscanf(line, "%s", host) != 1)
822                                 continue;
824                         if ((host[0] == 0) || (host[0] == '#'))
825                                 continue;
827                         if (ping_host_add(ping, host) < 0)
828                         {
829                                 const char *errmsg = ping_get_error (ping);
831                                 fprintf (stderr, "Adding host `%s' failed: %s\n", host, errmsg);
832                                 continue;
833                         }
834                 }
836 #if _POSIX_SAVED_IDS
837                 /* Drop privileges */
838                 status = seteuid (getuid ());
839                 if (status != 0)
840                 {
841                         fprintf (stderr, "Temporarily dropping privileges "
842                                         "failed: %s\n", strerror (errno));
843                         exit (EXIT_FAILURE);
844                 }
845 #endif
847                 fclose(infile);
848         }
850 #if _POSIX_SAVED_IDS
851         /* Regain privileges */
852         status = seteuid (saved_set_uid);
853         if (status != 0)
854         {
855                 fprintf (stderr, "Temporarily re-gaining privileges "
856                                 "failed: %s\n", strerror (errno));
857                 exit (EXIT_FAILURE);
858         }
859 #endif
861         for (i = optind; i < argc; i++)
862         {
863                 if (ping_host_add (ping, argv[i]) < 0)
864                 {
865                         const char *errmsg = ping_get_error (ping);
867                         fprintf (stderr, "Adding host `%s' failed: %s\n", argv[i], errmsg);
868                         continue;
869                 }
870         }
872         /* Permanently drop root privileges if we're setuid-root. */
873         status = setuid (getuid ());
874         if (status != 0)
875         {
876                 fprintf (stderr, "Dropping privileges failed: %s\n",
877                                 strerror (errno));
878                 exit (EXIT_FAILURE);
879         }
881 #if _POSIX_SAVED_IDS
882         saved_set_uid = (uid_t) -1;
883 #endif
885         print_header (ping);
887         if (i == 0)
888                 return (1);
890         memset (&sigint_action, '\0', sizeof (sigint_action));
891         sigint_action.sa_handler = sigint_handler;
892         if (sigaction (SIGINT, &sigint_action, NULL) < 0)
893         {
894                 perror ("sigaction");
895                 return (1);
896         }
898         while (opt_count != 0)
899         {
900                 int index;
901                 int status;
903                 if (gettimeofday (&tv_begin, NULL) < 0)
904                 {
905                         perror ("gettimeofday");
906                         return (1);
907                 }
909                 if (ping_send (ping) < 0)
910                 {
911                         fprintf (stderr, "ping_send failed: %s\n",
912                                         ping_get_error (ping));
913                         return (1);
914                 }
916                 index = 0;
917                 for (iter = ping_iterator_get (ping);
918                                 iter != NULL;
919                                 iter = ping_iterator_next (iter))
920                 {
921                         print_host (iter, index);
922                         index++;
923                 }
924 #if !USE_NCURSES
925                 fflush (stdout);
926 #endif
928                 /* Don't sleep in the last iteration */
929                 if (opt_count == 1)
930                         break;
932                 if (gettimeofday (&tv_end, NULL) < 0)
933                 {
934                         perror ("gettimeofday");
935                         return (1);
936                 }
938                 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
940                 /* printf ("Sleeping for %i.%09li seconds\n", (int) ts_wait.tv_sec, ts_wait.tv_nsec); */
941                 while ((status = nanosleep (&ts_wait, &ts_wait)) != 0)
942                 {
943                         if (errno != EINTR)
944                         {
945                                 perror ("nanosleep");
946                                 break;
947                         }
948                         else if (opt_count == 0)
949                         {
950                                 /* sigint */
951                                 break;
952                         }
954 #if USE_NCURSES
955                         resize_windows (ping);
956 #endif
957                 }
959 #if USE_NCURSES
960                 resize_windows (ping);
961 #endif
963                 if (opt_count > 0)
964                         opt_count--;
965         } /* while (opt_count != 0) */
967         print_footer (ping);
969         ping_destroy (ping);
971         return (0);
972 } /* }}} int main */
974 /* vim: set fdm=marker : */