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 index;
85 int req_sent;
86 int req_rcvd;
88 double latency_min;
89 double latency_max;
90 double latency_total;
91 double latency_total_square;
93 #if USE_NCURSES
94 WINDOW *window;
95 #endif
96 } ping_context_t;
98 static double opt_interval = 1.0;
99 static int opt_addrfamily = PING_DEF_AF;
100 static char *opt_srcaddr = NULL;
101 static char *opt_device = NULL;
102 static char *opt_filename = NULL;
103 static int opt_count = -1;
104 static int opt_send_ttl = 64;
106 static int host_num = 0;
108 #if USE_NCURSES
109 static WINDOW *main_win = NULL;
110 #endif
112 static void sigint_handler (int signal) /* {{{ */
113 {
114 /* Make compiler happy */
115 signal = 0;
116 /* Exit the loop */
117 opt_count = 0;
118 } /* }}} void sigint_handler */
120 static ping_context_t *context_create (void) /* {{{ */
121 {
122 ping_context_t *ret;
124 if ((ret = malloc (sizeof (ping_context_t))) == NULL)
125 return (NULL);
127 memset (ret, '\0', sizeof (ping_context_t));
129 ret->latency_min = -1.0;
130 ret->latency_max = -1.0;
131 ret->latency_total = 0.0;
132 ret->latency_total_square = 0.0;
134 #if USE_NCURSES
135 ret->window = NULL;
136 #endif
138 return (ret);
139 } /* }}} ping_context_t *context_create */
141 static void context_destroy (ping_context_t *context) /* {{{ */
142 {
143 if (context == NULL)
144 return;
146 #if USE_NCURSES
147 if (context->window != NULL)
148 {
149 delwin (context->window);
150 context->window = NULL;
151 }
152 #endif
154 free (context);
155 } /* }}} void context_destroy */
157 static double context_get_average (ping_context_t *ctx) /* {{{ */
158 {
159 double num_total;
161 if (ctx == NULL)
162 return (-1.0);
164 if (ctx->req_rcvd < 1)
165 return (-0.0);
167 num_total = (double) ctx->req_rcvd;
168 return (ctx->latency_total / num_total);
169 } /* }}} double context_get_average */
171 static double context_get_stddev (ping_context_t *ctx) /* {{{ */
172 {
173 double num_total;
175 if (ctx == NULL)
176 return (-1.0);
178 if (ctx->req_rcvd < 1)
179 return (-0.0);
180 else if (ctx->req_rcvd < 2)
181 return (0.0);
183 num_total = (double) ctx->req_rcvd;
184 return (sqrt (((num_total * ctx->latency_total_square)
185 - (ctx->latency_total * ctx->latency_total))
186 / (num_total * (num_total - 1.0))));
187 } /* }}} double context_get_stddev */
189 static double context_get_packet_loss (const ping_context_t *ctx) /* {{{ */
190 {
191 if (ctx == NULL)
192 return (-1.0);
194 if (ctx->req_sent < 1)
195 return (0.0);
197 return (100.0 * (ctx->req_sent - ctx->req_rcvd)
198 / ((double) ctx->req_sent));
199 } /* }}} double context_get_packet_loss */
201 static int ping_initialize_contexts (pingobj_t *ping) /* {{{ */
202 {
203 pingobj_iter_t *iter;
204 int index;
206 if (ping == NULL)
207 return (EINVAL);
209 index = 0;
210 for (iter = ping_iterator_get (ping);
211 iter != NULL;
212 iter = ping_iterator_next (iter))
213 {
214 ping_context_t *context;
215 size_t buffer_size;
217 context = context_create ();
218 context->index = index;
220 buffer_size = sizeof (context->host);
221 ping_iterator_get_info (iter, PING_INFO_HOSTNAME, context->host, &buffer_size);
223 buffer_size = sizeof (context->addr);
224 ping_iterator_get_info (iter, PING_INFO_ADDRESS, context->addr, &buffer_size);
226 ping_iterator_set_context (iter, (void *) context);
228 index++;
229 }
231 return (0);
232 } /* }}} int ping_initialize_contexts */
234 static void usage_exit (const char *name, int status) /* {{{ */
235 {
236 int name_length;
238 name_length = (int) strlen (name);
240 fprintf (stderr, "Usage: %s [OPTIONS] "
241 "-f filename | host [host [host ...]]\n"
243 "\nAvailable options:\n"
244 " -4|-6 force the use of IPv4 or IPv6\n"
245 " -c count number of ICMP packets to send\n"
246 " -i interval interval with which to send ICMP packets\n"
247 " -t ttl time to live for each ICMP packet\n"
248 " -I srcaddr source address\n"
249 " -D device outgoing interface name\n"
250 " -f filename filename to read hosts from\n"
252 "\noping "PACKAGE_VERSION", http://verplant.org/liboping/\n"
253 "by Florian octo Forster <octo@verplant.org>\n"
254 "for contributions see `AUTHORS'\n",
255 name);
256 exit (status);
257 } /* }}} void usage_exit */
259 static int read_options (int argc, char **argv) /* {{{ */
260 {
261 int optchar;
263 while (1)
264 {
265 optchar = getopt (argc, argv, "46c:hi:I:t:f:D:");
267 if (optchar == -1)
268 break;
270 switch (optchar)
271 {
272 case '4':
273 case '6':
274 opt_addrfamily = (optchar == '4') ? AF_INET : AF_INET6;
275 break;
277 case 'c':
278 {
279 int new_count;
280 new_count = atoi (optarg);
281 if (new_count > 0)
282 opt_count = new_count;
283 else
284 fprintf(stderr, "Ignoring invalid count: %s\n",
285 optarg);
286 }
287 break;
289 case 'f':
290 {
291 if (opt_filename != NULL)
292 free (opt_filename);
293 opt_filename = strdup (optarg);
294 }
295 break;
297 case 'i':
298 {
299 double new_interval;
300 new_interval = atof (optarg);
301 if (new_interval < 0.001)
302 fprintf (stderr, "Ignoring invalid interval: %s\n",
303 optarg);
304 else
305 opt_interval = new_interval;
306 }
307 break;
308 case 'I':
309 {
310 if (opt_srcaddr != NULL)
311 free (opt_srcaddr);
312 opt_srcaddr = strdup (optarg);
313 }
314 break;
316 case 'D':
317 opt_device = optarg;
318 break;
320 case 't':
321 {
322 int new_send_ttl;
323 new_send_ttl = atoi (optarg);
324 if ((new_send_ttl > 0) && (new_send_ttl < 256))
325 opt_send_ttl = new_send_ttl;
326 else
327 fprintf (stderr, "Ignoring invalid TTL argument: %s\n",
328 optarg);
329 break;
330 }
332 case 'h':
333 usage_exit (argv[0], 0);
334 break;
335 default:
336 usage_exit (argv[0], 1);
337 }
338 }
340 return (optind);
341 } /* }}} read_options */
343 static void time_normalize (struct timespec *ts) /* {{{ */
344 {
345 while (ts->tv_nsec < 0)
346 {
347 if (ts->tv_sec == 0)
348 {
349 ts->tv_nsec = 0;
350 return;
351 }
353 ts->tv_sec -= 1;
354 ts->tv_nsec += 1000000000;
355 }
357 while (ts->tv_nsec >= 1000000000)
358 {
359 ts->tv_sec += 1;
360 ts->tv_nsec -= 1000000000;
361 }
362 } /* }}} void time_normalize */
364 static void time_calc (struct timespec *ts_dest, /* {{{ */
365 const struct timespec *ts_int,
366 const struct timeval *tv_begin,
367 const struct timeval *tv_end)
368 {
369 ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
370 ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
371 time_normalize (ts_dest);
373 /* Assure that `(begin + interval) > end'.
374 * This may seem overly complicated, but `tv_sec' is of type `time_t'
375 * which may be `unsigned. *sigh* */
376 if ((tv_end->tv_sec > ts_dest->tv_sec)
377 || ((tv_end->tv_sec == ts_dest->tv_sec)
378 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
379 {
380 ts_dest->tv_sec = 0;
381 ts_dest->tv_nsec = 0;
382 return;
383 }
385 ts_dest->tv_sec = ts_dest->tv_sec - tv_end->tv_sec;
386 ts_dest->tv_nsec = ts_dest->tv_nsec - (tv_end->tv_usec * 1000);
387 time_normalize (ts_dest);
388 } /* }}} void time_calc */
390 #if USE_NCURSES
391 static int update_stats_from_context (ping_context_t *ctx) /* {{{ */
392 {
393 if ((ctx == NULL) || (ctx->window == NULL))
394 return (EINVAL);
396 werase (ctx->window);
398 box (ctx->window, 0, 0);
399 wattron (ctx->window, A_BOLD);
400 mvwprintw (ctx->window, /* y = */ 0, /* x = */ 5,
401 " %s ", ctx->host);
402 wattroff (ctx->window, A_BOLD);
403 wprintw (ctx->window, "ping statistics ");
404 mvwprintw (ctx->window, /* y = */ 1, /* x = */ 2,
405 "%i packets transmitted, %i received, %.2f%% packet "
406 "loss, time %.1fms",
407 ctx->req_sent, ctx->req_rcvd,
408 context_get_packet_loss (ctx),
409 ctx->latency_total);
410 if (ctx->req_rcvd != 0)
411 {
412 double average;
413 double deviation;
415 average = context_get_average (ctx);
416 deviation = context_get_stddev (ctx);
418 mvwprintw (ctx->window, /* y = */ 2, /* x = */ 2,
419 "rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms",
420 ctx->latency_min,
421 average,
422 ctx->latency_max,
423 deviation);
424 }
426 wrefresh (ctx->window);
428 return (0);
429 } /* }}} int update_stats_from_context */
431 static int on_resize (pingobj_t *ping) /* {{{ */
432 {
433 pingobj_iter_t *iter;
434 int width = 0;
435 int height = 0;
436 int main_win_height;
438 getmaxyx (stdscr, height, width);
439 if ((height < 1) || (width < 1))
440 return (EINVAL);
442 main_win_height = height - (4 * host_num);
443 #if 1
444 wresize (main_win, main_win_height, /* width = */ 0);
445 #else
446 delwin (main_win);
447 main_win = newwin (/* height = */ main_win_height,
448 /* width = */ 0,
449 /* y = */ 0, /* x = */ 0);
450 #endif
451 /* Allow scrolling */
452 scrollok (main_win, TRUE);
453 /* wsetscrreg (main_win, 0, main_win_height - 1); */
454 /* Allow hardware accelerated scrolling. */
455 idlok (main_win, TRUE);
456 wrefresh (main_win);
458 for (iter = ping_iterator_get (ping);
459 iter != NULL;
460 iter = ping_iterator_next (iter))
461 {
462 ping_context_t *context;
464 context = ping_iterator_get_context (iter);
465 if (context == NULL)
466 continue;
468 if (context->window != NULL)
469 {
470 delwin (context->window);
471 context->window = NULL;
472 }
473 context->window = newwin (/* height = */ 4,
474 /* width = */ 0,
475 /* y = */ main_win_height + (4 * context->index),
476 /* x = */ 0);
477 }
479 return (0);
480 } /* }}} */
482 static int check_resize (pingobj_t *ping) /* {{{ */
483 {
484 int need_resize = 0;
486 while (42)
487 {
488 int key = wgetch (stdscr);
489 if (key == ERR)
490 break;
491 else if (key == KEY_RESIZE)
492 need_resize = 1;
493 }
495 if (need_resize)
496 return (on_resize (ping));
497 else
498 return (0);
499 } /* }}} int check_resize */
501 static int pre_loop_hook (pingobj_t *ping) /* {{{ */
502 {
503 pingobj_iter_t *iter;
504 int width = 0;
505 int height = 0;
506 int main_win_height;
508 initscr ();
509 cbreak ();
510 noecho ();
511 nodelay (stdscr, TRUE);
513 getmaxyx (stdscr, height, width);
514 if ((height < 1) || (width < 1))
515 return (EINVAL);
517 if (has_colors () == TRUE)
518 {
519 start_color ();
520 init_pair (OPING_GREEN, COLOR_GREEN, /* default = */ 0);
521 init_pair (OPING_YELLOW, COLOR_YELLOW, /* default = */ 0);
522 init_pair (OPING_RED, COLOR_RED, /* default = */ 0);
523 }
525 main_win_height = height - (4 * host_num);
526 main_win = newwin (/* height = */ main_win_height,
527 /* width = */ 0,
528 /* y = */ 0, /* x = */ 0);
529 /* Allow scrolling */
530 scrollok (main_win, TRUE);
531 /* wsetscrreg (main_win, 0, main_win_height - 1); */
532 /* Allow hardware accelerated scrolling. */
533 idlok (main_win, TRUE);
534 wmove (main_win, /* y = */ main_win_height - 1, /* x = */ 0);
535 wrefresh (main_win);
537 for (iter = ping_iterator_get (ping);
538 iter != NULL;
539 iter = ping_iterator_next (iter))
540 {
541 ping_context_t *context;
543 context = ping_iterator_get_context (iter);
544 if (context == NULL)
545 continue;
547 if (context->window != NULL)
548 {
549 delwin (context->window);
550 context->window = NULL;
551 }
552 context->window = newwin (/* height = */ 4,
553 /* width = */ 0,
554 /* y = */ main_win_height + (4 * context->index),
555 /* x = */ 0);
556 }
559 /* Don't know what good this does exactly, but without this code
560 * "check_resize" will be called right after startup and *somehow*
561 * this leads to display errors. If we purge all initial characters
562 * here, the problem goes away. "wgetch" is non-blocking due to
563 * "nodelay" (see above). */
564 while (wgetch (stdscr) != ERR)
565 {
566 /* eat up characters */;
567 }
569 return (0);
570 } /* }}} int pre_loop_hook */
572 static int pre_sleep_hook (pingobj_t *ping) /* {{{ */
573 {
574 return (check_resize (ping));
575 } /* }}} int pre_sleep_hook */
577 static int post_sleep_hook (pingobj_t *ping) /* {{{ */
578 {
579 return (check_resize (ping));
580 } /* }}} int pre_sleep_hook */
581 #else /* if !USE_NCURSES */
582 static int pre_loop_hook (pingobj_t *ping) /* {{{ */
583 {
584 pingobj_iter_t *iter;
586 for (iter = ping_iterator_get (ping);
587 iter != NULL;
588 iter = ping_iterator_next (iter))
589 {
590 ping_context_t *ctx;
591 size_t buffer_size;
593 ctx = ping_iterator_get_context (iter);
594 if (ctx == NULL)
595 continue;
597 buffer_size = 0;
598 ping_iterator_get_info (iter, PING_INFO_DATA, NULL, &buffer_size);
600 printf ("PING %s (%s) %zu bytes of data.\n",
601 ctx->host, ctx->addr, buffer_size);
602 }
604 return (0);
605 } /* }}} int pre_loop_hook */
607 static int pre_sleep_hook (__attribute__((unused)) pingobj_t *ping) /* {{{ */
608 {
609 fflush (stdout);
611 return (0);
612 } /* }}} int pre_sleep_hook */
614 static int post_sleep_hook (__attribute__((unused)) pingobj_t *ping) /* {{{ */
615 {
616 return (0);
617 } /* }}} int post_sleep_hook */
618 #endif
620 static void update_host_hook (pingobj_iter_t *iter, /* {{{ */
621 int index)
622 {
623 double latency;
624 unsigned int sequence;
625 int recv_ttl;
626 size_t buffer_len;
627 size_t data_len;
628 ping_context_t *context;
630 latency = -1.0;
631 buffer_len = sizeof (latency);
632 ping_iterator_get_info (iter, PING_INFO_LATENCY,
633 &latency, &buffer_len);
635 sequence = 0;
636 buffer_len = sizeof (sequence);
637 ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
638 &sequence, &buffer_len);
640 recv_ttl = -1;
641 buffer_len = sizeof (recv_ttl);
642 ping_iterator_get_info (iter, PING_INFO_RECV_TTL,
643 &recv_ttl, &buffer_len);
645 data_len = 0;
646 ping_iterator_get_info (iter, PING_INFO_DATA,
647 NULL, &data_len);
649 context = (ping_context_t *) ping_iterator_get_context (iter);
651 #if USE_NCURSES
652 # define HOST_PRINTF(...) wprintw(main_win, __VA_ARGS__)
653 #else
654 # define HOST_PRINTF(...) printf(__VA_ARGS__)
655 #endif
657 context->req_sent++;
658 if (latency > 0.0)
659 {
660 context->req_rcvd++;
661 context->latency_total += latency;
662 context->latency_total_square += (latency * latency);
664 if ((context->latency_max < 0.0) || (context->latency_max < latency))
665 context->latency_max = latency;
666 if ((context->latency_min < 0.0) || (context->latency_min > latency))
667 context->latency_min = latency;
669 #if USE_NCURSES
670 if (has_colors () == TRUE)
671 {
672 int color = OPING_GREEN;
673 double average = context_get_average (context);
674 double stddev = context_get_stddev (context);
676 if ((latency < (average - (2 * stddev)))
677 || (latency > (average + (2 * stddev))))
678 color = OPING_RED;
679 else if ((latency < (average - stddev))
680 || (latency > (average + stddev)))
681 color = OPING_YELLOW;
683 HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i "
684 "time=",
685 data_len, context->host, context->addr,
686 sequence, recv_ttl);
687 wattron (main_win, COLOR_PAIR(color));
688 HOST_PRINTF ("%.2f", latency);
689 wattroff (main_win, COLOR_PAIR(color));
690 HOST_PRINTF (" ms\n");
691 }
692 else
693 {
694 #endif
695 HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i "
696 "time=%.2f ms\n",
697 data_len,
698 context->host, context->addr,
699 sequence, recv_ttl, latency);
700 #if USE_NCURSES
701 }
702 #endif
703 }
704 else
705 {
706 #if USE_NCURSES
707 if (has_colors () == TRUE)
708 {
709 HOST_PRINTF ("echo reply from %s (%s): icmp_seq=%u ",
710 context->host, context->addr,
711 sequence);
712 wattron (main_win, COLOR_PAIR(OPING_RED) | A_BOLD);
713 HOST_PRINTF ("timeout");
714 wattroff (main_win, COLOR_PAIR(OPING_RED) | A_BOLD);
715 HOST_PRINTF ("\n");
716 }
717 else
718 {
719 #endif
720 HOST_PRINTF ("echo reply from %s (%s): icmp_seq=%u timeout\n",
721 context->host, context->addr,
722 sequence);
723 #if USE_NCURSES
724 }
725 #endif
726 }
728 #if USE_NCURSES
729 update_stats_from_context (context);
730 wrefresh (main_win);
731 #endif
732 } /* }}} void update_host_hook */
734 static int post_loop_hook (pingobj_t *ping) /* {{{ */
735 {
736 pingobj_iter_t *iter;
738 #if USE_NCURSES
739 endwin ();
740 #endif
742 for (iter = ping_iterator_get (ping);
743 iter != NULL;
744 iter = ping_iterator_next (iter))
745 {
746 ping_context_t *context;
748 context = ping_iterator_get_context (iter);
750 printf ("\n--- %s ping statistics ---\n"
751 "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n",
752 context->host, context->req_sent, context->req_rcvd,
753 context_get_packet_loss (context),
754 context->latency_total);
756 if (context->req_rcvd != 0)
757 {
758 double average;
759 double deviation;
761 average = context_get_average (context);
762 deviation = context_get_stddev (context);
764 printf ("rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms\n",
765 context->latency_min,
766 average,
767 context->latency_max,
768 deviation);
769 }
771 ping_iterator_set_context (iter, NULL);
772 context_destroy (context);
773 }
775 return (0);
776 } /* }}} int post_loop_hook */
778 int main (int argc, char **argv) /* {{{ */
779 {
780 pingobj_t *ping;
781 pingobj_iter_t *iter;
783 struct sigaction sigint_action;
785 struct timeval tv_begin;
786 struct timeval tv_end;
787 struct timespec ts_wait;
788 struct timespec ts_int;
790 int optind;
791 int i;
792 int status;
793 #if _POSIX_SAVED_IDS
794 uid_t saved_set_uid;
796 /* Save the old effective user id */
797 saved_set_uid = geteuid ();
798 /* Set the effective user ID to the real user ID without changing the
799 * saved set-user ID */
800 status = seteuid (getuid ());
801 if (status != 0)
802 {
803 fprintf (stderr, "Temporarily dropping privileges "
804 "failed: %s\n", strerror (errno));
805 exit (EXIT_FAILURE);
806 }
807 #endif
809 optind = read_options (argc, argv);
811 #if !_POSIX_SAVED_IDS
812 /* Cannot temporarily drop privileges -> reject every file but "-". */
813 if ((opt_filename != NULL)
814 && (strcmp ("-", opt_filename) != 0)
815 && (getuid () != geteuid ()))
816 {
817 fprintf (stderr, "Your real and effective user IDs don't "
818 "match. Reading from a file (option '-f')\n"
819 "is therefore too risky. You can still read "
820 "from STDIN using '-f -' if you like.\n"
821 "Sorry.\n");
822 exit (EXIT_FAILURE);
823 }
824 #endif
826 if ((optind >= argc) && (opt_filename == NULL)) {
827 usage_exit (argv[0], 1);
828 }
830 if ((ping = ping_construct ()) == NULL)
831 {
832 fprintf (stderr, "ping_construct failed\n");
833 return (1);
834 }
836 if (ping_setopt (ping, PING_OPT_TTL, &opt_send_ttl) != 0)
837 {
838 fprintf (stderr, "Setting TTL to %i failed: %s\n",
839 opt_send_ttl, ping_get_error (ping));
840 }
842 {
843 double temp_sec;
844 double temp_nsec;
846 temp_nsec = modf (opt_interval, &temp_sec);
847 ts_int.tv_sec = (time_t) temp_sec;
848 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
850 /* printf ("ts_int = %i.%09li\n", (int) ts_int.tv_sec, ts_int.tv_nsec); */
851 }
853 if (opt_addrfamily != PING_DEF_AF)
854 ping_setopt (ping, PING_OPT_AF, (void *) &opt_addrfamily);
856 if (opt_srcaddr != NULL)
857 {
858 if (ping_setopt (ping, PING_OPT_SOURCE, (void *) opt_srcaddr) != 0)
859 {
860 fprintf (stderr, "Setting source address failed: %s\n",
861 ping_get_error (ping));
862 }
863 }
865 if (opt_device != NULL)
866 {
867 if (ping_setopt (ping, PING_OPT_DEVICE, (void *) opt_device) != 0)
868 {
869 fprintf (stderr, "Setting device failed: %s\n",
870 ping_get_error (ping));
871 }
872 }
874 if (opt_filename != NULL)
875 {
876 FILE *infile;
877 char line[256];
878 char host[256];
880 if (strcmp (opt_filename, "-") == 0)
881 /* Open STDIN */
882 infile = fdopen(0, "r");
883 else
884 infile = fopen(opt_filename, "r");
886 if (infile == NULL)
887 {
888 fprintf (stderr, "Opening %s failed: %s\n",
889 (strcmp (opt_filename, "-") == 0)
890 ? "STDIN" : opt_filename,
891 strerror(errno));
892 return (1);
893 }
895 #if _POSIX_SAVED_IDS
896 /* Regain privileges */
897 status = seteuid (saved_set_uid);
898 if (status != 0)
899 {
900 fprintf (stderr, "Temporarily re-gaining privileges "
901 "failed: %s\n", strerror (errno));
902 exit (EXIT_FAILURE);
903 }
904 #endif
906 while (fgets(line, sizeof(line), infile))
907 {
908 /* Strip whitespace */
909 if (sscanf(line, "%s", host) != 1)
910 continue;
912 if ((host[0] == 0) || (host[0] == '#'))
913 continue;
915 if (ping_host_add(ping, host) < 0)
916 {
917 const char *errmsg = ping_get_error (ping);
919 fprintf (stderr, "Adding host `%s' failed: %s\n", host, errmsg);
920 continue;
921 }
922 else
923 {
924 host_num++;
925 }
926 }
928 #if _POSIX_SAVED_IDS
929 /* Drop privileges */
930 status = seteuid (getuid ());
931 if (status != 0)
932 {
933 fprintf (stderr, "Temporarily dropping privileges "
934 "failed: %s\n", strerror (errno));
935 exit (EXIT_FAILURE);
936 }
937 #endif
939 fclose(infile);
940 }
942 #if _POSIX_SAVED_IDS
943 /* Regain privileges */
944 status = seteuid (saved_set_uid);
945 if (status != 0)
946 {
947 fprintf (stderr, "Temporarily re-gaining privileges "
948 "failed: %s\n", strerror (errno));
949 exit (EXIT_FAILURE);
950 }
951 #endif
953 for (i = optind; i < argc; i++)
954 {
955 if (ping_host_add (ping, argv[i]) < 0)
956 {
957 const char *errmsg = ping_get_error (ping);
959 fprintf (stderr, "Adding host `%s' failed: %s\n", argv[i], errmsg);
960 continue;
961 }
962 else
963 {
964 host_num++;
965 }
966 }
968 /* Permanently drop root privileges if we're setuid-root. */
969 status = setuid (getuid ());
970 if (status != 0)
971 {
972 fprintf (stderr, "Dropping privileges failed: %s\n",
973 strerror (errno));
974 exit (EXIT_FAILURE);
975 }
977 #if _POSIX_SAVED_IDS
978 saved_set_uid = (uid_t) -1;
979 #endif
981 ping_initialize_contexts (ping);
983 if (i == 0)
984 return (1);
986 memset (&sigint_action, '\0', sizeof (sigint_action));
987 sigint_action.sa_handler = sigint_handler;
988 if (sigaction (SIGINT, &sigint_action, NULL) < 0)
989 {
990 perror ("sigaction");
991 return (1);
992 }
994 pre_loop_hook (ping);
996 while (opt_count != 0)
997 {
998 int index;
999 int status;
1001 if (gettimeofday (&tv_begin, NULL) < 0)
1002 {
1003 perror ("gettimeofday");
1004 return (1);
1005 }
1007 if (ping_send (ping) < 0)
1008 {
1009 fprintf (stderr, "ping_send failed: %s\n",
1010 ping_get_error (ping));
1011 return (1);
1012 }
1014 index = 0;
1015 for (iter = ping_iterator_get (ping);
1016 iter != NULL;
1017 iter = ping_iterator_next (iter))
1018 {
1019 update_host_hook (iter, index);
1020 index++;
1021 }
1023 pre_sleep_hook (ping);
1025 /* Don't sleep in the last iteration */
1026 if (opt_count == 1)
1027 break;
1029 if (gettimeofday (&tv_end, NULL) < 0)
1030 {
1031 perror ("gettimeofday");
1032 return (1);
1033 }
1035 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
1037 /* printf ("Sleeping for %i.%09li seconds\n", (int) ts_wait.tv_sec, ts_wait.tv_nsec); */
1038 while ((status = nanosleep (&ts_wait, &ts_wait)) != 0)
1039 {
1040 if (errno != EINTR)
1041 {
1042 perror ("nanosleep");
1043 break;
1044 }
1045 else if (opt_count == 0)
1046 {
1047 /* sigint */
1048 break;
1049 }
1050 }
1052 post_sleep_hook (ping);
1054 if (opt_count > 0)
1055 opt_count--;
1056 } /* while (opt_count != 0) */
1058 post_loop_hook (ping);
1060 ping_destroy (ping);
1062 return (0);
1063 } /* }}} int main */
1065 /* vim: set fdm=marker : */