Merge branch 'ab/prettyping'
[liboping.git] / src / oping.c
1 /**
2  * Object oriented C module to send ICMP and ICMPv6 `echo's.
3  * Copyright (C) 2006-2011  Florian octo Forster <ff at octo.it>
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  */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #if STDC_HEADERS
25 # include <stdlib.h>
26 # include <stdio.h>
27 # include <string.h>
28 # include <stdint.h>
29 # include <inttypes.h>
30 # include <errno.h>
31 # include <assert.h>
32 #else
33 # error "You don't have the standard C99 header files installed"
34 #endif /* STDC_HEADERS */
35
36 #if HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39
40 #if HAVE_MATH_H
41 # include <math.h>
42 #endif
43
44 #if TIME_WITH_SYS_TIME
45 # include <sys/time.h>
46 # include <time.h>
47 #else
48 # if HAVE_SYS_TIME_H
49 #  include <sys/time.h>
50 # else
51 #  include <time.h>
52 # endif
53 #endif
54
55 #if HAVE_SYS_SOCKET_H
56 # include <sys/socket.h>
57 #endif
58 #if HAVE_NETINET_IN_H
59 # include <netinet/in.h>
60 #endif
61 #if HAVE_NETINET_IP_H
62 # include <netinet/ip.h>
63 #endif
64
65 #if HAVE_NETDB_H
66 # include <netdb.h> /* NI_MAXHOST */
67 #endif
68
69 #if HAVE_SIGNAL_H
70 # include <signal.h>
71 #endif
72
73 #if HAVE_SYS_TYPES_H
74 #include <sys/types.h>
75 #endif
76
77 #include <locale.h>
78 #include <langinfo.h>
79
80 #if USE_NCURSES
81 # define NCURSES_OPAQUE 1
82 /* http://newsgroups.derkeiler.com/Archive/Rec/rec.games.roguelike.development/2010-09/msg00050.html */
83 # define _X_OPEN_SOURCE_EXTENDED
84
85 # if HAVE_NCURSESW_NCURSES_H
86 #  include <ncursesw/ncurses.h>
87 # elif HAVE_NCURSES_H
88 #  include <ncurses.h>
89 # endif
90
91 # define OPING_GREEN 1
92 # define OPING_YELLOW 2
93 # define OPING_RED 3
94 # define OPING_GREEN_HIST 4
95 # define OPING_YELLOW_HIST 5
96 # define OPING_RED_HIST 6
97
98 static char const * const hist_symbols_utf8[] = {
99         "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" };
100 static size_t const hist_symbols_utf8_num = sizeof (hist_symbols_utf8)
101         / sizeof (hist_symbols_utf8[0]);
102
103 /* scancodes for 6 levels of horizontal bars, ncurses-specific */
104 /* those are not the usual constants because those are not constant */
105 static int const hist_symbols_acs[] = {
106         115, /* ACS_S9 "⎽" */
107         114, /* ACS_S7 "⎼" */
108         113, /* ACS_S5 "─" */
109         112, /* ACS_S3 "⎻" */
110         111  /* ACS_S1 "⎺" */
111 };
112 static size_t const hist_symbols_acs_num = sizeof (hist_symbols_acs)
113         / sizeof (hist_symbols_acs[0]);
114
115 /* use different colors without a background for scancodes */
116 static int const hist_colors_utf8[] = {
117         OPING_GREEN_HIST, OPING_YELLOW_HIST, OPING_RED_HIST };
118 static int const hist_colors_acs[] = {
119         OPING_GREEN, OPING_YELLOW, OPING_RED };
120 /* assuming that both arrays are the same size */
121 static size_t const hist_colors_num = sizeof (hist_colors_utf8)
122         / sizeof (hist_colors_utf8[0]);
123 #endif
124
125 #include "oping.h"
126
127 #ifndef _POSIX_SAVED_IDS
128 # define _POSIX_SAVED_IDS 0
129 #endif
130
131 #ifndef IPTOS_MINCOST
132 # define IPTOS_MINCOST 0x02
133 #endif
134
135 /* Remove GNU specific __attribute__ settings when using another compiler */
136 #if !__GNUC__
137 # define __attribute__(x) /**/
138 #endif
139
140 typedef struct ping_context
141 {
142         char host[NI_MAXHOST];
143         char addr[NI_MAXHOST];
144
145         int index;
146         int req_sent;
147         int req_rcvd;
148
149         double latency_min;
150         double latency_max;
151         double latency_total;
152         double latency_total_square;
153
154 #if USE_NCURSES
155         WINDOW *window;
156 #endif
157 } ping_context_t;
158
159 static double  opt_interval   = 1.0;
160 static int     opt_addrfamily = PING_DEF_AF;
161 static char   *opt_srcaddr    = NULL;
162 static char   *opt_device     = NULL;
163 static char   *opt_filename   = NULL;
164 static int     opt_count      = -1;
165 static int     opt_send_ttl   = 64;
166 static uint8_t opt_send_qos   = 0;
167 #if USE_NCURSES
168 static int     opt_utf8       = 0;
169 #endif
170
171 static int host_num = 0;
172
173 #if USE_NCURSES
174 static WINDOW *main_win = NULL;
175 #endif
176
177 static void sigint_handler (int signal) /* {{{ */
178 {
179         /* Make compiler happy */
180         signal = 0;
181         /* Exit the loop */
182         opt_count = 0;
183 } /* }}} void sigint_handler */
184
185 static ping_context_t *context_create (void) /* {{{ */
186 {
187         ping_context_t *ret;
188
189         if ((ret = malloc (sizeof (ping_context_t))) == NULL)
190                 return (NULL);
191
192         memset (ret, '\0', sizeof (ping_context_t));
193
194         ret->latency_min   = -1.0;
195         ret->latency_max   = -1.0;
196         ret->latency_total = 0.0;
197         ret->latency_total_square = 0.0;
198
199 #if USE_NCURSES
200         ret->window = NULL;
201 #endif
202
203         return (ret);
204 } /* }}} ping_context_t *context_create */
205
206 static void context_destroy (ping_context_t *context) /* {{{ */
207 {
208         if (context == NULL)
209                 return;
210
211 #if USE_NCURSES
212         if (context->window != NULL)
213         {
214                 delwin (context->window);
215                 context->window = NULL;
216         }
217 #endif
218
219         free (context);
220 } /* }}} void context_destroy */
221
222 static double context_get_average (ping_context_t *ctx) /* {{{ */
223 {
224         double num_total;
225
226         if (ctx == NULL)
227                 return (-1.0);
228
229         if (ctx->req_rcvd < 1)
230                 return (-0.0);
231
232         num_total = (double) ctx->req_rcvd;
233         return (ctx->latency_total / num_total);
234 } /* }}} double context_get_average */
235
236 static double context_get_stddev (ping_context_t *ctx) /* {{{ */
237 {
238         double num_total;
239
240         if (ctx == NULL)
241                 return (-1.0);
242
243         if (ctx->req_rcvd < 1)
244                 return (-0.0);
245         else if (ctx->req_rcvd < 2)
246                 return (0.0);
247
248         num_total = (double) ctx->req_rcvd;
249         return (sqrt (((num_total * ctx->latency_total_square)
250                                         - (ctx->latency_total * ctx->latency_total))
251                                 / (num_total * (num_total - 1.0))));
252 } /* }}} double context_get_stddev */
253
254 static double context_get_packet_loss (const ping_context_t *ctx) /* {{{ */
255 {
256         if (ctx == NULL)
257                 return (-1.0);
258
259         if (ctx->req_sent < 1)
260                 return (0.0);
261
262         return (100.0 * (ctx->req_sent - ctx->req_rcvd)
263                         / ((double) ctx->req_sent));
264 } /* }}} double context_get_packet_loss */
265
266 static int ping_initialize_contexts (pingobj_t *ping) /* {{{ */
267 {
268         pingobj_iter_t *iter;
269         int index;
270
271         if (ping == NULL)
272                 return (EINVAL);
273
274         index = 0;
275         for (iter = ping_iterator_get (ping);
276                         iter != NULL;
277                         iter = ping_iterator_next (iter))
278         {
279                 ping_context_t *context;
280                 size_t buffer_size;
281
282                 context = context_create ();
283                 context->index = index;
284
285                 buffer_size = sizeof (context->host);
286                 ping_iterator_get_info (iter, PING_INFO_HOSTNAME, context->host, &buffer_size);
287
288                 buffer_size = sizeof (context->addr);
289                 ping_iterator_get_info (iter, PING_INFO_ADDRESS, context->addr, &buffer_size);
290
291                 ping_iterator_set_context (iter, (void *) context);
292
293                 index++;
294         }
295
296         return (0);
297 } /* }}} int ping_initialize_contexts */
298
299 static void usage_exit (const char *name, int status) /* {{{ */
300 {
301         fprintf (stderr, "Usage: %s [OPTIONS] "
302                                 "-f filename | host [host [host ...]]\n"
303
304                         "\nAvailable options:\n"
305                         "  -4|-6        force the use of IPv4 or IPv6\n"
306                         "  -c count     number of ICMP packets to send\n"
307                         "  -i interval  interval with which to send ICMP packets\n"
308                         "  -t ttl       time to live for each ICMP packet\n"
309                         "  -Q qos       Quality of Service (QoS) of outgoing packets\n"
310                         "               Use \"-Q help\" for a list of valid options.\n"
311                         "  -I srcaddr   source address\n"
312                         "  -D device    outgoing interface name\n"
313                         "  -f filename  filename to read hosts from\n"
314 #if USE_NCURSES
315                         "  -u / -U      force / disable UTF-8 output\n"
316 #endif
317
318                         "\noping "PACKAGE_VERSION", http://verplant.org/liboping/\n"
319                         "by Florian octo Forster <octo@verplant.org>\n"
320                         "for contributions see `AUTHORS'\n",
321                         name);
322         exit (status);
323 } /* }}} void usage_exit */
324
325 __attribute__((noreturn))
326 static void usage_qos_exit (const char *arg, int status) /* {{{ */
327 {
328         if (arg != 0)
329                 fprintf (stderr, "Invalid QoS argument: \"%s\"\n\n", arg);
330
331         fprintf (stderr, "Valid QoS arguments (option \"-Q\") are:\n"
332                         "\n"
333                         "  Differentiated Services (IPv4 and IPv6, RFC 2474)\n"
334                         "\n"
335                         "    be                     Best Effort (BE, default PHB).\n"
336                         "    ef                     Expedited Forwarding (EF) PHB group (RFC 3246).\n"
337                         "                           (low delay, low loss, low jitter)\n"
338                         "    va                     Voice Admit (VA) DSCP (RFC 5865).\n"
339                         "                           (capacity-admitted traffic)\n"
340                         "    af[1-4][1-3]           Assured Forwarding (AF) PHB group (RFC 2597).\n"
341                         "                           For example: \"af12\" (class 1, precedence 2)\n"
342                         "    cs[0-7]                Class Selector (CS) PHB group (RFC 2474).\n"
343                         "                           For example: \"cs1\" (priority traffic)\n"
344                         "\n"
345                         "  Type of Service (IPv4, RFC 1349, obsolete)\n"
346                         "\n"
347                         "    lowdelay     (%#04x)    minimize delay\n"
348                         "    throughput   (%#04x)    maximize throughput\n"
349                         "    reliability  (%#04x)    maximize reliability\n"
350                         "    mincost      (%#04x)    minimize monetary cost\n"
351                         "\n"
352                         "  Specify manually\n"
353                         "\n"
354                         "    0x00 - 0xff            Hexadecimal numeric specification.\n"
355                         "       0 -  255            Decimal numeric specification.\n"
356                         "\n",
357                         (unsigned int) IPTOS_LOWDELAY,
358                         (unsigned int) IPTOS_THROUGHPUT,
359                         (unsigned int) IPTOS_RELIABILITY,
360                         (unsigned int) IPTOS_MINCOST);
361
362         exit (status);
363 } /* }}} void usage_qos_exit */
364
365 static int set_opt_send_qos (const char *opt) /* {{{ */
366 {
367         if (opt == NULL)
368                 return (EINVAL);
369
370         if (strcasecmp ("help", opt) == 0)
371                 usage_qos_exit (/* arg = */ NULL, /* status = */ EXIT_SUCCESS);
372         /* DiffServ (RFC 2474): */
373         /* - Best effort (BE) */
374         else if (strcasecmp ("be", opt) == 0)
375                 opt_send_qos = 0;
376         /* - Expedited Forwarding (EF, RFC 3246) */
377         else if (strcasecmp ("ef", opt) == 0)
378                 opt_send_qos = 0xB8; /* == 0x2E << 2 */
379         /* - Voice Admit (VA, RFC 5865) */
380         else if (strcasecmp ("va", opt) == 0)
381                 opt_send_qos = 0xB0; /* == 0x2D << 2 */
382         /* - Assured Forwarding (AF, RFC 2597) */
383         else if ((strncasecmp ("af", opt, strlen ("af")) == 0)
384                         && (strlen (opt) == 4))
385         {
386                 uint8_t dscp;
387                 uint8_t class = 0;
388                 uint8_t prec = 0;
389
390                 /* There are four classes, AF1x, AF2x, AF3x, and AF4x. */
391                 if (opt[2] == '1')
392                         class = 1;
393                 else if (opt[2] == '2')
394                         class = 2;
395                 else if (opt[2] == '3')
396                         class = 3;
397                 else if (opt[2] == '4')
398                         class = 4;
399                 else
400                         usage_qos_exit (/* arg = */ opt, /* status = */ EXIT_SUCCESS);
401
402                 /* In each class, there are three precedences, AFx1, AFx2, and AFx3 */
403                 if (opt[3] == '1')
404                         prec = 1;
405                 else if (opt[3] == '2')
406                         prec = 2;
407                 else if (opt[3] == '3')
408                         prec = 3;
409                 else
410                         usage_qos_exit (/* arg = */ opt, /* status = */ EXIT_SUCCESS);
411
412                 dscp = (8 * class) + (2 * prec);
413                 /* The lower two bits are used for Explicit Congestion Notification (ECN) */
414                 opt_send_qos = dscp << 2;
415         }
416         /* - Class Selector (CS) */
417         else if ((strncasecmp ("cs", opt, strlen ("cs")) == 0)
418                         && (strlen (opt) == 3))
419         {
420                 uint8_t class;
421
422                 if ((opt[2] < '0') || (opt[2] > '7'))
423                         usage_qos_exit (/* arg = */ opt, /* status = */ EXIT_FAILURE);
424
425                 /* Not exactly legal by the C standard, but I don't know of any
426                  * system not supporting this hack. */
427                 class = ((uint8_t) opt[2]) - ((uint8_t) '0');
428                 opt_send_qos = class << 5;
429         }
430         /* Type of Service (RFC 1349) */
431         else if (strcasecmp ("lowdelay", opt) == 0)
432                 opt_send_qos = IPTOS_LOWDELAY;
433         else if (strcasecmp ("throughput", opt) == 0)
434                 opt_send_qos = IPTOS_THROUGHPUT;
435         else if (strcasecmp ("reliability", opt) == 0)
436                 opt_send_qos = IPTOS_RELIABILITY;
437         else if (strcasecmp ("mincost", opt) == 0)
438                 opt_send_qos = IPTOS_MINCOST;
439         /* Numeric value */
440         else
441         {
442                 unsigned long value;
443                 char *endptr;
444
445                 errno = 0;
446                 endptr = NULL;
447                 value = strtoul (opt, &endptr, /* base = */ 0);
448                 if ((errno != 0) || (endptr == opt)
449                                 || (endptr == NULL) || (*endptr != 0)
450                                 || (value > 0xff))
451                         usage_qos_exit (/* arg = */ opt, /* status = */ EXIT_FAILURE);
452                 
453                 opt_send_qos = (uint8_t) value;
454         }
455
456         return (0);
457 } /* }}} int set_opt_send_qos */
458
459 static char *format_qos (uint8_t qos, char *buffer, size_t buffer_size) /* {{{ */
460 {
461         uint8_t dscp;
462         uint8_t ecn;
463         char *dscp_str;
464         char *ecn_str;
465
466         dscp = qos >> 2;
467         ecn = qos & 0x03;
468
469         switch (dscp)
470         {
471                 case 0x00: dscp_str = "be";  break;
472                 case 0x2e: dscp_str = "ef";  break;
473                 case 0x2d: dscp_str = "va";  break;
474                 case 0x0a: dscp_str = "af11"; break;
475                 case 0x0c: dscp_str = "af12"; break;
476                 case 0x0e: dscp_str = "af13"; break;
477                 case 0x12: dscp_str = "af21"; break;
478                 case 0x14: dscp_str = "af22"; break;
479                 case 0x16: dscp_str = "af23"; break;
480                 case 0x1a: dscp_str = "af31"; break;
481                 case 0x1c: dscp_str = "af32"; break;
482                 case 0x1e: dscp_str = "af33"; break;
483                 case 0x22: dscp_str = "af41"; break;
484                 case 0x24: dscp_str = "af42"; break;
485                 case 0x26: dscp_str = "af43"; break;
486                 case 0x08: dscp_str = "cs1";  break;
487                 case 0x10: dscp_str = "cs2";  break;
488                 case 0x18: dscp_str = "cs3";  break;
489                 case 0x20: dscp_str = "cs4";  break;
490                 case 0x28: dscp_str = "cs5";  break;
491                 case 0x30: dscp_str = "cs6";  break;
492                 case 0x38: dscp_str = "cs7";  break;
493                 default:   dscp_str = NULL;
494         }
495
496         switch (ecn)
497         {
498                 case 0x01: ecn_str = ",ecn(1)"; break;
499                 case 0x02: ecn_str = ",ecn(0)"; break;
500                 case 0x03: ecn_str = ",ce"; break;
501                 default:   ecn_str = "";
502         }
503
504         if (dscp_str == NULL)
505                 snprintf (buffer, buffer_size, "0x%02x%s", dscp, ecn_str);
506         else
507                 snprintf (buffer, buffer_size, "%s%s", dscp_str, ecn_str);
508         buffer[buffer_size - 1] = 0;
509
510         return (buffer);
511 } /* }}} char *format_qos */
512
513 static int read_options (int argc, char **argv) /* {{{ */
514 {
515         int optchar;
516
517         while (1)
518         {
519                 optchar = getopt (argc, argv, "46c:hi:I:t:Q:f:D:"
520 #if USE_NCURSES
521                                 "uU"
522 #endif
523                                 );
524
525                 if (optchar == -1)
526                         break;
527
528                 switch (optchar)
529                 {
530                         case '4':
531                         case '6':
532                                 opt_addrfamily = (optchar == '4') ? AF_INET : AF_INET6;
533                                 break;
534
535                         case 'c':
536                                 {
537                                         int new_count;
538                                         new_count = atoi (optarg);
539                                         if (new_count > 0)
540                                                 opt_count = new_count;
541                                         else
542                                                 fprintf(stderr, "Ignoring invalid count: %s\n",
543                                                                 optarg);
544                                 }
545                                 break;
546
547                         case 'f':
548                                 {
549                                         if (opt_filename != NULL)
550                                                 free (opt_filename);
551                                         opt_filename = strdup (optarg);
552                                 }
553                                 break;
554
555                         case 'i':
556                                 {
557                                         double new_interval;
558                                         new_interval = atof (optarg);
559                                         if (new_interval < 0.001)
560                                                 fprintf (stderr, "Ignoring invalid interval: %s\n",
561                                                                 optarg);
562                                         else
563                                                 opt_interval = new_interval;
564                                 }
565                                 break;
566                         case 'I':
567                                 {
568                                         if (opt_srcaddr != NULL)
569                                                 free (opt_srcaddr);
570                                         opt_srcaddr = strdup (optarg);
571                                 }
572                                 break;
573
574                         case 'D':
575                                 opt_device = optarg;
576                                 break;
577
578                         case 't':
579                         {
580                                 int new_send_ttl;
581                                 new_send_ttl = atoi (optarg);
582                                 if ((new_send_ttl > 0) && (new_send_ttl < 256))
583                                         opt_send_ttl = new_send_ttl;
584                                 else
585                                         fprintf (stderr, "Ignoring invalid TTL argument: %s\n",
586                                                         optarg);
587                                 break;
588                         }
589
590                         case 'Q':
591                                 set_opt_send_qos (optarg);
592                                 break;
593
594 #if USE_NCURSES
595                         case 'u':
596                                 opt_utf8 = 2;
597                                 break;
598                         case 'U':
599                                 opt_utf8 = 1;
600                                 break;
601 #endif
602
603                         case 'h':
604                                 usage_exit (argv[0], 0);
605                                 break;
606                         default:
607                                 usage_exit (argv[0], 1);
608                 }
609         }
610
611         return (optind);
612 } /* }}} read_options */
613
614 static void time_normalize (struct timespec *ts) /* {{{ */
615 {
616         while (ts->tv_nsec < 0)
617         {
618                 if (ts->tv_sec == 0)
619                 {
620                         ts->tv_nsec = 0;
621                         return;
622                 }
623
624                 ts->tv_sec  -= 1;
625                 ts->tv_nsec += 1000000000;
626         }
627
628         while (ts->tv_nsec >= 1000000000)
629         {
630                 ts->tv_sec  += 1;
631                 ts->tv_nsec -= 1000000000;
632         }
633 } /* }}} void time_normalize */
634
635 static void time_calc (struct timespec *ts_dest, /* {{{ */
636                 const struct timespec *ts_int,
637                 const struct timeval  *tv_begin,
638                 const struct timeval  *tv_end)
639 {
640         ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
641         ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
642         time_normalize (ts_dest);
643
644         /* Assure that `(begin + interval) > end'.
645          * This may seem overly complicated, but `tv_sec' is of type `time_t'
646          * which may be `unsigned. *sigh* */
647         if ((tv_end->tv_sec > ts_dest->tv_sec)
648                         || ((tv_end->tv_sec == ts_dest->tv_sec)
649                                 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
650         {
651                 ts_dest->tv_sec  = 0;
652                 ts_dest->tv_nsec = 0;
653                 return;
654         }
655
656         ts_dest->tv_sec = ts_dest->tv_sec - tv_end->tv_sec;
657         ts_dest->tv_nsec = ts_dest->tv_nsec - (tv_end->tv_usec * 1000);
658         time_normalize (ts_dest);
659 } /* }}} void time_calc */
660
661 #if USE_NCURSES
662 static _Bool has_utf8() /* {{{ */
663 {
664 # if HAVE_NCURSESW_NCURSES_H
665         if (!opt_utf8)
666         {
667                 /* Automatically determine */
668                 if (strcasecmp ("UTF-8", nl_langinfo (CODESET)) == 0)
669                         opt_utf8 = 2;
670                 else
671                         opt_utf8 = 1;
672         }
673         return ((_Bool) (opt_utf8 - 1));
674 # else
675         return (0);
676 # endif
677 } /* }}} _Bool has_utf8 */
678
679 static int update_prettyping_graph (ping_context_t *ctx, /* {{{ */
680                 double latency, unsigned int sequence)
681 {
682         int color = OPING_RED;
683         char const *symbol = "!";
684         int symbolc = '!';
685
686         int x_max;
687         int x_pos;
688
689         x_max = getmaxx (ctx->window);
690         x_pos = ((sequence - 1) % (x_max - 4)) + 2;
691
692         if (latency >= 0.0)
693         {
694                 double ratio;
695
696                 size_t symbols_num = hist_symbols_acs_num;
697                 size_t colors_num = 1;
698
699                 size_t index_symbols;
700                 size_t index_colors;
701                 size_t intensity;
702
703                 /* latency is in milliseconds, opt_interval is in seconds. */
704                 ratio = (latency * 0.001) / opt_interval;
705                 if (ratio > 1) {
706                         ratio = 1.0;
707                 }
708
709                 if (has_utf8 ())
710                         symbols_num = hist_symbols_utf8_num;
711
712                 if (has_colors () == TRUE)
713                         colors_num = hist_colors_num;
714
715                 intensity = (size_t) (ratio * ((double) (symbols_num * colors_num)));
716                 if (intensity >= (symbols_num * colors_num))
717                         intensity = (symbols_num * colors_num) - 1;
718
719                 index_symbols = intensity % symbols_num;
720                 assert (index_symbols < symbols_num);
721
722                 index_colors = intensity / symbols_num;
723                 assert (index_colors < colors_num);
724
725                 if (has_utf8())
726                 {
727                         color = hist_colors_utf8[index_colors];
728                         symbol = hist_symbols_utf8[index_symbols];
729                 }
730                 else
731                 {
732                         color = hist_colors_acs[index_colors];
733                         symbolc = hist_symbols_acs[index_symbols] | A_ALTCHARSET;
734                 }
735         }
736         else /* if (!(latency >= 0.0)) */
737                 wattron (ctx->window, A_BOLD);
738
739         if (has_colors () == TRUE)
740                 wattron (ctx->window, COLOR_PAIR(color));
741
742         if (has_utf8())
743                 mvwprintw (ctx->window, /* y = */ 3, /* x = */ x_pos, symbol);
744         else
745                 mvwaddch (ctx->window, /* y = */ 3, /* x = */ x_pos, symbolc);
746
747         if (has_colors () == TRUE)
748                 wattroff (ctx->window, COLOR_PAIR(color));
749
750         /* Use negation here to handle NaN correctly. */
751         if (!(latency >= 0.0))
752                 wattroff (ctx->window, A_BOLD);
753
754         wprintw (ctx->window, " ");
755         return (0);
756 } /* }}} int update_prettyping_graph */
757
758 static int update_stats_from_context (ping_context_t *ctx, pingobj_iter_t *iter) /* {{{ */
759 {
760         double latency = -1.0;
761         size_t buffer_len = sizeof (latency);
762
763         ping_iterator_get_info (iter, PING_INFO_LATENCY,
764                         &latency, &buffer_len);
765
766         unsigned int sequence = 0;
767         buffer_len = sizeof (sequence);
768         ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
769                         &sequence, &buffer_len);
770
771
772         if ((ctx == NULL) || (ctx->window == NULL))
773                 return (EINVAL);
774
775         /* werase (ctx->window); */
776
777         box (ctx->window, 0, 0);
778         wattron (ctx->window, A_BOLD);
779         mvwprintw (ctx->window, /* y = */ 0, /* x = */ 5,
780                         " %s ", ctx->host);
781         wattroff (ctx->window, A_BOLD);
782         wprintw (ctx->window, "ping statistics ");
783         mvwprintw (ctx->window, /* y = */ 1, /* x = */ 2,
784                         "%i packets transmitted, %i received, %.2f%% packet "
785                         "loss, time %.1fms",
786                         ctx->req_sent, ctx->req_rcvd,
787                         context_get_packet_loss (ctx),
788                         ctx->latency_total);
789         if (ctx->req_rcvd != 0)
790         {
791                 double average;
792                 double deviation;
793
794                 average = context_get_average (ctx);
795                 deviation = context_get_stddev (ctx);
796                         
797                 mvwprintw (ctx->window, /* y = */ 2, /* x = */ 2,
798                                 "rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms",
799                                 ctx->latency_min,
800                                 average,
801                                 ctx->latency_max,
802                                 deviation);
803         }
804
805         update_prettyping_graph (ctx, latency, sequence);
806
807         wrefresh (ctx->window);
808
809         return (0);
810 } /* }}} int update_stats_from_context */
811
812 static int on_resize (pingobj_t *ping) /* {{{ */
813 {
814         pingobj_iter_t *iter;
815         int width = 0;
816         int height = 0;
817         int main_win_height;
818
819         getmaxyx (stdscr, height, width);
820         if ((height < 1) || (width < 1))
821                 return (EINVAL);
822
823         main_win_height = height - (5 * host_num);
824         wresize (main_win, main_win_height, /* width = */ width);
825         /* Allow scrolling */
826         scrollok (main_win, TRUE);
827         /* wsetscrreg (main_win, 0, main_win_height - 1); */
828         /* Allow hardware accelerated scrolling. */
829         idlok (main_win, TRUE);
830         wrefresh (main_win);
831
832         for (iter = ping_iterator_get (ping);
833                         iter != NULL;
834                         iter = ping_iterator_next (iter))
835         {
836                 ping_context_t *context;
837
838                 context = ping_iterator_get_context (iter);
839                 if (context == NULL)
840                         continue;
841
842                 if (context->window != NULL)
843                 {
844                         delwin (context->window);
845                         context->window = NULL;
846                 }
847                 context->window = newwin (/* height = */ 5,
848                                 /* width = */ width,
849                                 /* y = */ main_win_height + (5 * context->index),
850                                 /* x = */ 0);
851         }
852
853         return (0);
854 } /* }}} */
855
856 static int check_resize (pingobj_t *ping) /* {{{ */
857 {
858         int need_resize = 0;
859
860         while (42)
861         {
862                 int key = wgetch (stdscr);
863                 if (key == ERR)
864                         break;
865                 else if (key == KEY_RESIZE)
866                         need_resize = 1;
867         }
868
869         if (need_resize)
870                 return (on_resize (ping));
871         else
872                 return (0);
873 } /* }}} int check_resize */
874
875 static int pre_loop_hook (pingobj_t *ping) /* {{{ */
876 {
877         pingobj_iter_t *iter;
878         int width = 0;
879         int height = 0;
880         int main_win_height;
881
882         initscr ();
883         cbreak ();
884         noecho ();
885         nodelay (stdscr, TRUE);
886
887         getmaxyx (stdscr, height, width);
888         if ((height < 1) || (width < 1))
889                 return (EINVAL);
890
891         if (has_colors () == TRUE)
892         {
893                 start_color ();
894                 init_pair (OPING_GREEN,  COLOR_GREEN,  /* default = */ 0);
895                 init_pair (OPING_YELLOW, COLOR_YELLOW, /* default = */ 0);
896                 init_pair (OPING_RED,    COLOR_RED,    /* default = */ 0);
897                 init_pair (OPING_GREEN_HIST,  COLOR_GREEN,  COLOR_BLACK);
898                 init_pair (OPING_YELLOW_HIST, COLOR_YELLOW, COLOR_GREEN);
899                 init_pair (OPING_RED_HIST,    COLOR_RED,    COLOR_YELLOW);
900         }
901
902         main_win_height = height - (5 * host_num);
903         main_win = newwin (/* height = */ main_win_height,
904                         /* width = */ width,
905                         /* y = */ 0, /* x = */ 0);
906         /* Allow scrolling */
907         scrollok (main_win, TRUE);
908         /* wsetscrreg (main_win, 0, main_win_height - 1); */
909         /* Allow hardware accelerated scrolling. */
910         idlok (main_win, TRUE);
911         wmove (main_win, /* y = */ main_win_height - 1, /* x = */ 0);
912         wrefresh (main_win);
913
914         for (iter = ping_iterator_get (ping);
915                         iter != NULL;
916                         iter = ping_iterator_next (iter))
917         {
918                 ping_context_t *context;
919
920                 context = ping_iterator_get_context (iter);
921                 if (context == NULL)
922                         continue;
923
924                 if (context->window != NULL)
925                 {
926                         delwin (context->window);
927                         context->window = NULL;
928                 }
929                 context->window = newwin (/* height = */ 5,
930                                 /* width = */ width,
931                                 /* y = */ main_win_height + (5 * context->index),
932                                 /* x = */ 0);
933         }
934
935
936         /* Don't know what good this does exactly, but without this code
937          * "check_resize" will be called right after startup and *somehow*
938          * this leads to display errors. If we purge all initial characters
939          * here, the problem goes away. "wgetch" is non-blocking due to
940          * "nodelay" (see above). */
941         while (wgetch (stdscr) != ERR)
942         {
943                 /* eat up characters */;
944         }
945
946         return (0);
947 } /* }}} int pre_loop_hook */
948
949 static int pre_sleep_hook (pingobj_t *ping) /* {{{ */
950 {
951         return (check_resize (ping));
952 } /* }}} int pre_sleep_hook */
953
954 static int post_sleep_hook (pingobj_t *ping) /* {{{ */
955 {
956         return (check_resize (ping));
957 } /* }}} int pre_sleep_hook */
958 #else /* if !USE_NCURSES */
959 static int pre_loop_hook (pingobj_t *ping) /* {{{ */
960 {
961         pingobj_iter_t *iter;
962
963         for (iter = ping_iterator_get (ping);
964                         iter != NULL;
965                         iter = ping_iterator_next (iter))
966         {
967                 ping_context_t *ctx;
968                 size_t buffer_size;
969
970                 ctx = ping_iterator_get_context (iter);
971                 if (ctx == NULL)
972                         continue;
973
974                 buffer_size = 0;
975                 ping_iterator_get_info (iter, PING_INFO_DATA, NULL, &buffer_size);
976
977                 printf ("PING %s (%s) %zu bytes of data.\n",
978                                 ctx->host, ctx->addr, buffer_size);
979         }
980
981         return (0);
982 } /* }}} int pre_loop_hook */
983
984 static int pre_sleep_hook (__attribute__((unused)) pingobj_t *ping) /* {{{ */
985 {
986         fflush (stdout);
987
988         return (0);
989 } /* }}} int pre_sleep_hook */
990
991 static int post_sleep_hook (__attribute__((unused)) pingobj_t *ping) /* {{{ */
992 {
993         return (0);
994 } /* }}} int post_sleep_hook */
995 #endif
996
997 static void update_host_hook (pingobj_iter_t *iter, /* {{{ */
998                 __attribute__((unused)) int index)
999 {
1000         double          latency;
1001         unsigned int    sequence;
1002         int             recv_ttl;
1003         uint8_t         recv_qos;
1004         char            recv_qos_str[16];
1005         size_t          buffer_len;
1006         size_t          data_len;
1007         ping_context_t *context;
1008
1009         latency = -1.0;
1010         buffer_len = sizeof (latency);
1011         ping_iterator_get_info (iter, PING_INFO_LATENCY,
1012                         &latency, &buffer_len);
1013
1014         sequence = 0;
1015         buffer_len = sizeof (sequence);
1016         ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
1017                         &sequence, &buffer_len);
1018
1019         recv_ttl = -1;
1020         buffer_len = sizeof (recv_ttl);
1021         ping_iterator_get_info (iter, PING_INFO_RECV_TTL,
1022                         &recv_ttl, &buffer_len);
1023
1024         recv_qos = 0;
1025         buffer_len = sizeof (recv_qos);
1026         ping_iterator_get_info (iter, PING_INFO_RECV_QOS,
1027                         &recv_qos, &buffer_len);
1028
1029         data_len = 0;
1030         ping_iterator_get_info (iter, PING_INFO_DATA,
1031                         NULL, &data_len);
1032
1033         context = (ping_context_t *) ping_iterator_get_context (iter);
1034
1035 #if USE_NCURSES
1036 # define HOST_PRINTF(...) wprintw(main_win, __VA_ARGS__)
1037 #else
1038 # define HOST_PRINTF(...) printf(__VA_ARGS__)
1039 #endif
1040
1041         context->req_sent++;
1042         if (latency > 0.0)
1043         {
1044                 context->req_rcvd++;
1045                 context->latency_total += latency;
1046                 context->latency_total_square += (latency * latency);
1047
1048                 if ((context->latency_max < 0.0) || (context->latency_max < latency))
1049                         context->latency_max = latency;
1050                 if ((context->latency_min < 0.0) || (context->latency_min > latency))
1051                         context->latency_min = latency;
1052
1053 #if USE_NCURSES
1054                 if (has_colors () == TRUE)
1055                 {
1056                         int color = OPING_GREEN;
1057                         double average = context_get_average (context);
1058                         double stddev = context_get_stddev (context);
1059
1060                         if ((latency < (average - (2 * stddev)))
1061                                         || (latency > (average + (2 * stddev))))
1062                                 color = OPING_RED;
1063                         else if ((latency < (average - stddev))
1064                                         || (latency > (average + stddev)))
1065                                 color = OPING_YELLOW;
1066
1067                         HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i ",
1068                                         data_len, context->host, context->addr,
1069                                         sequence, recv_ttl,
1070                                         format_qos (recv_qos, recv_qos_str, sizeof (recv_qos_str)));
1071                         if ((recv_qos != 0) || (opt_send_qos != 0))
1072                         {
1073                                 HOST_PRINTF ("qos=%s ",
1074                                                 format_qos (recv_qos, recv_qos_str, sizeof (recv_qos_str)));
1075                         }
1076                         HOST_PRINTF ("time=");
1077                         wattron (main_win, COLOR_PAIR(color));
1078                         HOST_PRINTF ("%.2f", latency);
1079                         wattroff (main_win, COLOR_PAIR(color));
1080                         HOST_PRINTF (" ms\n");
1081                 }
1082                 else
1083                 {
1084 #endif
1085                 HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i ",
1086                                 data_len,
1087                                 context->host, context->addr,
1088                                 sequence, recv_ttl);
1089                 if ((recv_qos != 0) || (opt_send_qos != 0))
1090                 {
1091                         HOST_PRINTF ("qos=%s ",
1092                                         format_qos (recv_qos, recv_qos_str, sizeof (recv_qos_str)));
1093                 }
1094                 HOST_PRINTF ("time=%.2f ms\n", latency);
1095 #if USE_NCURSES
1096                 }
1097 #endif
1098         }
1099         else
1100         {
1101 #if USE_NCURSES
1102                 if (has_colors () == TRUE)
1103                 {
1104                         HOST_PRINTF ("echo reply from %s (%s): icmp_seq=%u ",
1105                                         context->host, context->addr,
1106                                         sequence);
1107                         wattron (main_win, COLOR_PAIR(OPING_RED) | A_BOLD);
1108                         HOST_PRINTF ("timeout");
1109                         wattroff (main_win, COLOR_PAIR(OPING_RED) | A_BOLD);
1110                         HOST_PRINTF ("\n");
1111                 }
1112                 else
1113                 {
1114 #endif
1115                 HOST_PRINTF ("echo reply from %s (%s): icmp_seq=%u timeout\n",
1116                                 context->host, context->addr,
1117                                 sequence);
1118 #if USE_NCURSES
1119                 }
1120 #endif
1121         }
1122
1123 #if USE_NCURSES
1124         update_stats_from_context (context, iter);
1125         wrefresh (main_win);
1126 #endif
1127 } /* }}} void update_host_hook */
1128
1129 static int post_loop_hook (pingobj_t *ping) /* {{{ */
1130 {
1131         pingobj_iter_t *iter;
1132
1133 #if USE_NCURSES
1134         endwin ();
1135 #endif
1136
1137         for (iter = ping_iterator_get (ping);
1138                         iter != NULL;
1139                         iter = ping_iterator_next (iter))
1140         {
1141                 ping_context_t *context;
1142
1143                 context = ping_iterator_get_context (iter);
1144
1145                 printf ("\n--- %s ping statistics ---\n"
1146                                 "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n",
1147                                 context->host, context->req_sent, context->req_rcvd,
1148                                 context_get_packet_loss (context),
1149                                 context->latency_total);
1150
1151                 if (context->req_rcvd != 0)
1152                 {
1153                         double average;
1154                         double deviation;
1155
1156                         average = context_get_average (context);
1157                         deviation = context_get_stddev (context);
1158
1159                         printf ("rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms\n",
1160                                         context->latency_min,
1161                                         average,
1162                                         context->latency_max,
1163                                         deviation);
1164                 }
1165
1166                 ping_iterator_set_context (iter, NULL);
1167                 context_destroy (context);
1168         }
1169
1170         return (0);
1171 } /* }}} int post_loop_hook */
1172
1173 int main (int argc, char **argv) /* {{{ */
1174 {
1175         pingobj_t      *ping;
1176         pingobj_iter_t *iter;
1177
1178         struct sigaction sigint_action;
1179
1180         struct timeval  tv_begin;
1181         struct timeval  tv_end;
1182         struct timespec ts_wait;
1183         struct timespec ts_int;
1184
1185         int optind;
1186         int i;
1187         int status;
1188 #if _POSIX_SAVED_IDS
1189         uid_t saved_set_uid;
1190
1191         /* Save the old effective user id */
1192         saved_set_uid = geteuid ();
1193         /* Set the effective user ID to the real user ID without changing the
1194          * saved set-user ID */
1195         status = seteuid (getuid ());
1196         if (status != 0)
1197         {
1198                 fprintf (stderr, "Temporarily dropping privileges "
1199                                 "failed: %s\n", strerror (errno));
1200                 exit (EXIT_FAILURE);
1201         }
1202 #endif
1203
1204         setlocale(LC_ALL, "");
1205         optind = read_options (argc, argv);
1206
1207 #if !_POSIX_SAVED_IDS
1208         /* Cannot temporarily drop privileges -> reject every file but "-". */
1209         if ((opt_filename != NULL)
1210                         && (strcmp ("-", opt_filename) != 0)
1211                         && (getuid () != geteuid ()))
1212         {
1213                 fprintf (stderr, "Your real and effective user IDs don't "
1214                                 "match. Reading from a file (option '-f')\n"
1215                                 "is therefore too risky. You can still read "
1216                                 "from STDIN using '-f -' if you like.\n"
1217                                 "Sorry.\n");
1218                 exit (EXIT_FAILURE);
1219         }
1220 #endif
1221
1222         if ((optind >= argc) && (opt_filename == NULL)) {
1223                 usage_exit (argv[0], 1);
1224         }
1225
1226         if ((ping = ping_construct ()) == NULL)
1227         {
1228                 fprintf (stderr, "ping_construct failed\n");
1229                 return (1);
1230         }
1231
1232         if (ping_setopt (ping, PING_OPT_TTL, &opt_send_ttl) != 0)
1233         {
1234                 fprintf (stderr, "Setting TTL to %i failed: %s\n",
1235                                 opt_send_ttl, ping_get_error (ping));
1236         }
1237
1238         if (ping_setopt (ping, PING_OPT_QOS, &opt_send_qos) != 0)
1239         {
1240                 fprintf (stderr, "Setting TOS to %i failed: %s\n",
1241                                 opt_send_qos, ping_get_error (ping));
1242         }
1243
1244         {
1245                 double temp_sec;
1246                 double temp_nsec;
1247
1248                 temp_nsec = modf (opt_interval, &temp_sec);
1249                 ts_int.tv_sec  = (time_t) temp_sec;
1250                 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
1251
1252                 /* printf ("ts_int = %i.%09li\n", (int) ts_int.tv_sec, ts_int.tv_nsec); */
1253         }
1254
1255         if (opt_addrfamily != PING_DEF_AF)
1256                 ping_setopt (ping, PING_OPT_AF, (void *) &opt_addrfamily);
1257
1258         if (opt_srcaddr != NULL)
1259         {
1260                 if (ping_setopt (ping, PING_OPT_SOURCE, (void *) opt_srcaddr) != 0)
1261                 {
1262                         fprintf (stderr, "Setting source address failed: %s\n",
1263                                         ping_get_error (ping));
1264                 }
1265         }
1266
1267         if (opt_device != NULL)
1268         {
1269                 if (ping_setopt (ping, PING_OPT_DEVICE, (void *) opt_device) != 0)
1270                 {
1271                         fprintf (stderr, "Setting device failed: %s\n",
1272                                         ping_get_error (ping));
1273                 }
1274         }
1275
1276         if (opt_filename != NULL)
1277         {
1278                 FILE *infile;
1279                 char line[256];
1280                 char host[256];
1281
1282                 if (strcmp (opt_filename, "-") == 0)
1283                         /* Open STDIN */
1284                         infile = fdopen(0, "r");
1285                 else
1286                         infile = fopen(opt_filename, "r");
1287
1288                 if (infile == NULL)
1289                 {
1290                         fprintf (stderr, "Opening %s failed: %s\n",
1291                                         (strcmp (opt_filename, "-") == 0)
1292                                         ? "STDIN" : opt_filename,
1293                                         strerror(errno));
1294                         return (1);
1295                 }
1296
1297 #if _POSIX_SAVED_IDS
1298                 /* Regain privileges */
1299                 status = seteuid (saved_set_uid);
1300                 if (status != 0)
1301                 {
1302                         fprintf (stderr, "Temporarily re-gaining privileges "
1303                                         "failed: %s\n", strerror (errno));
1304                         exit (EXIT_FAILURE);
1305                 }
1306 #endif
1307
1308                 while (fgets(line, sizeof(line), infile))
1309                 {
1310                         /* Strip whitespace */
1311                         if (sscanf(line, "%s", host) != 1)
1312                                 continue;
1313
1314                         if ((host[0] == 0) || (host[0] == '#'))
1315                                 continue;
1316
1317                         if (ping_host_add(ping, host) < 0)
1318                         {
1319                                 const char *errmsg = ping_get_error (ping);
1320
1321                                 fprintf (stderr, "Adding host `%s' failed: %s\n", host, errmsg);
1322                                 continue;
1323                         }
1324                         else
1325                         {
1326                                 host_num++;
1327                         }
1328                 }
1329
1330 #if _POSIX_SAVED_IDS
1331                 /* Drop privileges */
1332                 status = seteuid (getuid ());
1333                 if (status != 0)
1334                 {
1335                         fprintf (stderr, "Temporarily dropping privileges "
1336                                         "failed: %s\n", strerror (errno));
1337                         exit (EXIT_FAILURE);
1338                 }
1339 #endif
1340
1341                 fclose(infile);
1342         }
1343
1344 #if _POSIX_SAVED_IDS
1345         /* Regain privileges */
1346         status = seteuid (saved_set_uid);
1347         if (status != 0)
1348         {
1349                 fprintf (stderr, "Temporarily re-gaining privileges "
1350                                 "failed: %s\n", strerror (errno));
1351                 exit (EXIT_FAILURE);
1352         }
1353 #endif
1354
1355         for (i = optind; i < argc; i++)
1356         {
1357                 if (ping_host_add (ping, argv[i]) < 0)
1358                 {
1359                         const char *errmsg = ping_get_error (ping);
1360
1361                         fprintf (stderr, "Adding host `%s' failed: %s\n", argv[i], errmsg);
1362                         continue;
1363                 }
1364                 else
1365                 {
1366                         host_num++;
1367                 }
1368         }
1369
1370         /* Permanently drop root privileges if we're setuid-root. */
1371         status = setuid (getuid ());
1372         if (status != 0)
1373         {
1374                 fprintf (stderr, "Dropping privileges failed: %s\n",
1375                                 strerror (errno));
1376                 exit (EXIT_FAILURE);
1377         }
1378
1379 #if _POSIX_SAVED_IDS
1380         saved_set_uid = (uid_t) -1;
1381 #endif
1382
1383         ping_initialize_contexts (ping);
1384
1385         if (i == 0)
1386                 return (1);
1387
1388         memset (&sigint_action, '\0', sizeof (sigint_action));
1389         sigint_action.sa_handler = sigint_handler;
1390         if (sigaction (SIGINT, &sigint_action, NULL) < 0)
1391         {
1392                 perror ("sigaction");
1393                 return (1);
1394         }
1395
1396         pre_loop_hook (ping);
1397
1398         while (opt_count != 0)
1399         {
1400                 int index;
1401                 int status;
1402
1403                 if (gettimeofday (&tv_begin, NULL) < 0)
1404                 {
1405                         perror ("gettimeofday");
1406                         return (1);
1407                 }
1408
1409                 status = ping_send (ping);
1410                 if (status == -EINTR)
1411                 {
1412                         continue;
1413                 }
1414                 else if (status < 0)
1415                 {
1416                         fprintf (stderr, "ping_send failed: %s\n",
1417                                         ping_get_error (ping));
1418                         return (1);
1419                 }
1420
1421                 index = 0;
1422                 for (iter = ping_iterator_get (ping);
1423                                 iter != NULL;
1424                                 iter = ping_iterator_next (iter))
1425                 {
1426                         update_host_hook (iter, index);
1427                         index++;
1428                 }
1429
1430                 pre_sleep_hook (ping);
1431
1432                 /* Don't sleep in the last iteration */
1433                 if (opt_count == 1)
1434                         break;
1435
1436                 if (gettimeofday (&tv_end, NULL) < 0)
1437                 {
1438                         perror ("gettimeofday");
1439                         return (1);
1440                 }
1441
1442                 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
1443
1444                 /* printf ("Sleeping for %i.%09li seconds\n", (int) ts_wait.tv_sec, ts_wait.tv_nsec); */
1445                 while ((status = nanosleep (&ts_wait, &ts_wait)) != 0)
1446                 {
1447                         if (errno == EINTR)
1448                         {
1449                                 continue;
1450                         }
1451                         else
1452                         {
1453                                 perror ("nanosleep");
1454                                 break;
1455                         }
1456                 }
1457
1458                 post_sleep_hook (ping);
1459
1460                 if (opt_count > 0)
1461                         opt_count--;
1462         } /* while (opt_count != 0) */
1463
1464         post_loop_hook (ping);
1465
1466         ping_destroy (ping);
1467
1468         return (0);
1469 } /* }}} int main */
1470
1471 /* vim: set fdm=marker : */