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