2 * Object oriented C module to send ICMP and ICMPv6 `echo's.
3 * Copyright (C) 2006-2016 Florian octo Forster <ff at octo.it>
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
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.
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
29 # include <inttypes.h>
33 # error "You don't have the standard C99 header files installed"
34 #endif /* STDC_HEADERS */
44 #if TIME_WITH_SYS_TIME
45 # include <sys/time.h>
49 # include <sys/time.h>
56 # include <sys/socket.h>
59 # include <netinet/in.h>
62 # include <netinet/ip.h>
66 # include <netdb.h> /* NI_MAXHOST */
74 #include <sys/types.h>
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
85 # if HAVE_NCURSESW_NCURSES_H
86 # include <ncursesw/ncurses.h>
91 # define OPING_GREEN 1
92 # define OPING_YELLOW 2
94 # define OPING_GREEN_HIST 4
95 # define OPING_YELLOW_HIST 5
96 # define OPING_RED_HIST 6
98 double const threshold_green = 0.8;
99 double const threshold_yellow = 0.95;
101 static char const * const hist_symbols_utf8[] = {
102 "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" };
103 static size_t const hist_symbols_utf8_num = sizeof (hist_symbols_utf8)
104 / sizeof (hist_symbols_utf8[0]);
106 /* scancodes for 6 levels of horizontal bars, ncurses-specific */
107 /* those are not the usual constants because those are not constant */
108 static int const hist_symbols_acs[] = {
109 115, /* ACS_S9 "⎽" */
110 114, /* ACS_S7 "⎼" */
111 113, /* ACS_S5 "─" */
112 112, /* ACS_S3 "⎻" */
115 static size_t const hist_symbols_acs_num = sizeof (hist_symbols_acs)
116 / sizeof (hist_symbols_acs[0]);
118 /* use different colors without a background for scancodes */
119 static int const hist_colors_utf8[] = {
120 OPING_GREEN_HIST, OPING_YELLOW_HIST, OPING_RED_HIST };
121 static int const hist_colors_acs[] = {
122 OPING_GREEN, OPING_YELLOW, OPING_RED };
123 /* assuming that both arrays are the same size */
124 static size_t const hist_colors_num = sizeof (hist_colors_utf8)
125 / sizeof (hist_colors_utf8[0]);
129 #define BOXPLOT_WHISKER_BAR (113 | A_ALTCHARSET)
131 #define BOXPLOT_WHISKER_LEFT_END (116 | A_ALTCHARSET)
133 #define BOXPLOT_WHISKER_RIGHT_END (117 | A_ALTCHARSET)
135 #define BOXPLOT_BOX ' '
137 #define BOXPLOT_MEDIAN (120 | A_ALTCHARSET)
141 #ifndef _POSIX_SAVED_IDS
142 # define _POSIX_SAVED_IDS 0
145 #ifndef IPTOS_MINCOST
146 # define IPTOS_MINCOST 0x02
149 /* Remove GNU specific __attribute__ settings when using another compiler */
151 # define __attribute__(x) /**/
154 typedef struct ping_context
156 char host[NI_MAXHOST];
157 char addr[NI_MAXHOST];
163 double latency_total;
165 #ifndef HISTORY_SIZE_MAX
166 # define HISTORY_SIZE_MAX 900
168 /* The last n RTTs in the order they were sent. */
169 double history_by_time[HISTORY_SIZE_MAX];
171 /* Current number of entries in the history. This is a value between 0
172 * and HISTORY_SIZE_MAX. */
175 /* Number "received" entries in the history, i.e. non-NAN entries. */
176 size_t history_received;
178 /* Index of the next RTT to be written to history_by_time. This wraps
179 * around to 0 once the histroty has grown to HISTORY_SIZE_MAX. */
180 size_t history_index;
182 /* The last history_size RTTs sorted by value. timed out packets (NAN
183 * entries) are sorted to the back. */
184 double history_by_value[HISTORY_SIZE_MAX];
186 /* If set to true, history_by_value has to be re-calculated. */
194 static double opt_interval = 1.0;
195 static double opt_timeout = PING_DEF_TIMEOUT;
196 static int opt_addrfamily = PING_DEF_AF;
197 static char *opt_srcaddr = NULL;
198 static char *opt_device = NULL;
199 static char *opt_mark = NULL;
200 static char *opt_filename = NULL;
201 static int opt_count = -1;
202 static int opt_send_ttl = 64;
203 static uint8_t opt_send_qos = 0;
204 #define OPING_DEFAULT_PERCENTILE 95.0
205 static double opt_percentile = -1.0;
206 static double opt_exit_status_threshold = 1.0;
208 static int opt_show_graph = 1;
209 static int opt_utf8 = 0;
211 static char *opt_outfile = NULL;
213 static int host_num = 0;
214 static FILE *outfile = NULL;
217 static WINDOW *main_win = NULL;
220 static void sigint_handler (int signal) /* {{{ */
222 /* Make compiler happy */
226 } /* }}} void sigint_handler */
228 static ping_context_t *context_create (void) /* {{{ */
232 if ((ret = malloc (sizeof (ping_context_t))) == NULL)
235 memset (ret, '\0', sizeof (ping_context_t));
237 ret->latency_total = 0.0;
244 } /* }}} ping_context_t *context_create */
246 static void context_destroy (ping_context_t *context) /* {{{ */
252 if (context->window != NULL)
254 delwin (context->window);
255 context->window = NULL;
260 } /* }}} void context_destroy */
262 static int compare_double (void const *arg0, void const *arg1) /* {{{ */
264 double dbl0 = *((double *) arg0);
265 double dbl1 = *((double *) arg1);
274 else if (isnan (dbl1))
276 else if (dbl0 < dbl1)
278 else if (dbl0 > dbl1)
282 } /* }}} int compare_double */
284 static void clean_history (ping_context_t *ctx) /* {{{ */
288 if (!ctx->history_dirty)
291 /* Copy all values from by_time to by_value. */
292 memcpy (ctx->history_by_value, ctx->history_by_time,
293 sizeof (ctx->history_by_time));
296 qsort (ctx->history_by_value, ctx->history_size, sizeof
297 (ctx->history_by_value[0]), compare_double);
299 /* Update the number of received RTTs. */
300 ctx->history_received = 0;
301 for (i = 0; i < ctx->history_size; i++)
302 if (!isnan (ctx->history_by_value[i]))
303 ctx->history_received++;
306 ctx->history_dirty = 0;
307 } /* }}} void clean_history */
309 static double percentile_to_latency (ping_context_t *ctx, /* {{{ */
316 /* Not a single packet was received successfully. */
317 if (ctx->history_received == 0)
320 if (percentile <= 0.0)
322 else if (percentile >= 100.0)
323 index = ctx->history_received - 1;
326 index = (size_t) ceil ((percentile / 100.0) * ((double) ctx->history_received));
331 return (ctx->history_by_value[index]);
332 } /* }}} double percentile_to_latency */
335 static double latency_to_ratio (ping_context_t *ctx, /* {{{ */
344 /* Not a single packet was received successfully. */
345 if (ctx->history_received == 0)
349 high = ctx->history_received - 1;
351 if (latency < ctx->history_by_value[low])
353 else if (latency >= ctx->history_by_value[high])
356 /* Do a binary search for the latency. This will work even when the
357 * exact latency is not in the array. If the latency is in the array
358 * multiple times, "low" will be set to the index of the last
359 * occurrence. The value at index "high" will be larger than the
360 * searched for latency (assured by the above "if" block. */
361 while ((high - low) > 1)
363 index = (high + low) / 2;
365 if (ctx->history_by_value[index] > latency)
371 assert (ctx->history_by_value[high] > latency);
372 assert (ctx->history_by_value[low] <= latency);
374 if (ctx->history_by_value[low] == latency)
379 return (((double) (index + 1)) / ((double) ctx->history_received));
380 } /* }}} double latency_to_ratio */
383 static double context_get_packet_loss (const ping_context_t *ctx) /* {{{ */
388 if (ctx->req_sent < 1)
391 return (100.0 * (ctx->req_sent - ctx->req_rcvd)
392 / ((double) ctx->req_sent));
393 } /* }}} double context_get_packet_loss */
395 static int ping_initialize_contexts (pingobj_t *ping) /* {{{ */
397 pingobj_iter_t *iter;
404 for (iter = ping_iterator_get (ping);
406 iter = ping_iterator_next (iter))
408 ping_context_t *context;
411 context = context_create ();
412 context->index = index;
414 buffer_size = sizeof (context->host);
415 ping_iterator_get_info (iter, PING_INFO_HOSTNAME, context->host, &buffer_size);
417 buffer_size = sizeof (context->addr);
418 ping_iterator_get_info (iter, PING_INFO_ADDRESS, context->addr, &buffer_size);
420 ping_iterator_set_context (iter, (void *) context);
426 } /* }}} int ping_initialize_contexts */
428 static void usage_exit (const char *name, int status) /* {{{ */
430 fprintf (stderr, "Usage: %s [OPTIONS] "
431 "-f filename | host [host [host ...]]\n"
433 "\nAvailable options:\n"
434 " -4|-6 force the use of IPv4 or IPv6\n"
435 " -c count number of ICMP packets to send\n"
436 " -i interval interval with which to send ICMP packets\n"
437 " -w timeout time to wait for replies, in seconds\n"
438 " -t ttl time to live for each ICMP packet\n"
439 " -Q qos Quality of Service (QoS) of outgoing packets\n"
440 " Use \"-Q help\" for a list of valid options.\n"
441 " -I srcaddr source address\n"
442 " -D device outgoing interface name\n"
443 " -m mark mark to set on outgoing packets\n"
444 " -f filename read hosts from <filename>\n"
445 " -O filename write RTT measurements to <filename>\n"
447 " -u / -U force / disable UTF-8 output\n"
448 " -g graph graph type to draw\n"
450 " -P percent Report the n'th percentile of latency\n"
451 " -Z percent Exit with non-zero exit status if more than this percentage of\n"
452 " probes timed out. (default: never)\n"
454 "\noping "PACKAGE_VERSION", http://noping.cc/\n"
455 "by Florian octo Forster <ff@octo.it>\n"
456 "for contributions see `AUTHORS'\n",
459 } /* }}} void usage_exit */
461 __attribute__((noreturn))
462 static void usage_qos_exit (const char *arg, int status) /* {{{ */
465 fprintf (stderr, "Invalid QoS argument: \"%s\"\n\n", arg);
467 fprintf (stderr, "Valid QoS arguments (option \"-Q\") are:\n"
469 " Differentiated Services (IPv4 and IPv6, RFC 2474)\n"
471 " be Best Effort (BE, default PHB).\n"
472 " ef Expedited Forwarding (EF) PHB group (RFC 3246).\n"
473 " (low delay, low loss, low jitter)\n"
474 " va Voice Admit (VA) DSCP (RFC 5865).\n"
475 " (capacity-admitted traffic)\n"
476 " af[1-4][1-3] Assured Forwarding (AF) PHB group (RFC 2597).\n"
477 " For example: \"af12\" (class 1, precedence 2)\n"
478 " cs[0-7] Class Selector (CS) PHB group (RFC 2474).\n"
479 " For example: \"cs1\" (priority traffic)\n"
481 " Type of Service (IPv4, RFC 1349, obsolete)\n"
483 " lowdelay (%#04x) minimize delay\n"
484 " throughput (%#04x) maximize throughput\n"
485 " reliability (%#04x) maximize reliability\n"
486 " mincost (%#04x) minimize monetary cost\n"
488 " Specify manually\n"
490 " 0x00 - 0xff Hexadecimal numeric specification.\n"
491 " 0 - 255 Decimal numeric specification.\n"
493 (unsigned int) IPTOS_LOWDELAY,
494 (unsigned int) IPTOS_THROUGHPUT,
495 (unsigned int) IPTOS_RELIABILITY,
496 (unsigned int) IPTOS_MINCOST);
499 } /* }}} void usage_qos_exit */
501 static int set_opt_send_qos (const char *opt) /* {{{ */
506 if (strcasecmp ("help", opt) == 0)
507 usage_qos_exit (/* arg = */ NULL, /* status = */ EXIT_SUCCESS);
508 /* DiffServ (RFC 2474): */
509 /* - Best effort (BE) */
510 else if (strcasecmp ("be", opt) == 0)
512 /* - Expedited Forwarding (EF, RFC 3246) */
513 else if (strcasecmp ("ef", opt) == 0)
514 opt_send_qos = 0xB8; /* == 0x2E << 2 */
515 /* - Voice Admit (VA, RFC 5865) */
516 else if (strcasecmp ("va", opt) == 0)
517 opt_send_qos = 0xB0; /* == 0x2D << 2 */
518 /* - Assured Forwarding (AF, RFC 2597) */
519 else if ((strncasecmp ("af", opt, strlen ("af")) == 0)
520 && (strlen (opt) == 4))
526 /* There are four classes, AF1x, AF2x, AF3x, and AF4x. */
529 else if (opt[2] == '2')
531 else if (opt[2] == '3')
533 else if (opt[2] == '4')
536 usage_qos_exit (/* arg = */ opt, /* status = */ EXIT_SUCCESS);
538 /* In each class, there are three precedences, AFx1, AFx2, and AFx3 */
541 else if (opt[3] == '2')
543 else if (opt[3] == '3')
546 usage_qos_exit (/* arg = */ opt, /* status = */ EXIT_SUCCESS);
548 dscp = (8 * class) + (2 * prec);
549 /* The lower two bits are used for Explicit Congestion Notification (ECN) */
550 opt_send_qos = dscp << 2;
552 /* - Class Selector (CS) */
553 else if ((strncasecmp ("cs", opt, strlen ("cs")) == 0)
554 && (strlen (opt) == 3))
558 if ((opt[2] < '0') || (opt[2] > '7'))
559 usage_qos_exit (/* arg = */ opt, /* status = */ EXIT_FAILURE);
561 /* Not exactly legal by the C standard, but I don't know of any
562 * system not supporting this hack. */
563 class = ((uint8_t) opt[2]) - ((uint8_t) '0');
564 opt_send_qos = class << 5;
566 /* Type of Service (RFC 1349) */
567 else if (strcasecmp ("lowdelay", opt) == 0)
568 opt_send_qos = IPTOS_LOWDELAY;
569 else if (strcasecmp ("throughput", opt) == 0)
570 opt_send_qos = IPTOS_THROUGHPUT;
571 else if (strcasecmp ("reliability", opt) == 0)
572 opt_send_qos = IPTOS_RELIABILITY;
573 else if (strcasecmp ("mincost", opt) == 0)
574 opt_send_qos = IPTOS_MINCOST;
583 value = strtoul (opt, &endptr, /* base = */ 0);
584 if ((errno != 0) || (endptr == opt)
585 || (endptr == NULL) || (*endptr != 0)
587 usage_qos_exit (/* arg = */ opt, /* status = */ EXIT_FAILURE);
589 opt_send_qos = (uint8_t) value;
593 } /* }}} int set_opt_send_qos */
595 static char *format_qos (uint8_t qos, char *buffer, size_t buffer_size) /* {{{ */
607 case 0x00: dscp_str = "be"; break;
608 case 0x2e: dscp_str = "ef"; break;
609 case 0x2d: dscp_str = "va"; break;
610 case 0x0a: dscp_str = "af11"; break;
611 case 0x0c: dscp_str = "af12"; break;
612 case 0x0e: dscp_str = "af13"; break;
613 case 0x12: dscp_str = "af21"; break;
614 case 0x14: dscp_str = "af22"; break;
615 case 0x16: dscp_str = "af23"; break;
616 case 0x1a: dscp_str = "af31"; break;
617 case 0x1c: dscp_str = "af32"; break;
618 case 0x1e: dscp_str = "af33"; break;
619 case 0x22: dscp_str = "af41"; break;
620 case 0x24: dscp_str = "af42"; break;
621 case 0x26: dscp_str = "af43"; break;
622 case 0x08: dscp_str = "cs1"; break;
623 case 0x10: dscp_str = "cs2"; break;
624 case 0x18: dscp_str = "cs3"; break;
625 case 0x20: dscp_str = "cs4"; break;
626 case 0x28: dscp_str = "cs5"; break;
627 case 0x30: dscp_str = "cs6"; break;
628 case 0x38: dscp_str = "cs7"; break;
629 default: dscp_str = NULL;
634 case 0x01: ecn_str = ",ecn(1)"; break;
635 case 0x02: ecn_str = ",ecn(0)"; break;
636 case 0x03: ecn_str = ",ce"; break;
637 default: ecn_str = "";
640 if (dscp_str == NULL)
641 snprintf (buffer, buffer_size, "0x%02x%s", dscp, ecn_str);
643 snprintf (buffer, buffer_size, "%s%s", dscp_str, ecn_str);
644 buffer[buffer_size - 1] = 0;
647 } /* }}} char *format_qos */
649 static int read_options (int argc, char **argv) /* {{{ */
655 optchar = getopt (argc, argv, "46c:hi:I:t:Q:f:D:Z:O:P:m:w:"
668 opt_addrfamily = (optchar == '4') ? AF_INET : AF_INET6;
674 new_count = atoi (optarg);
677 opt_count = new_count;
679 if ((opt_percentile < 0.0) && (opt_count < 20))
680 opt_percentile = 100.0 * (opt_count - 1) / opt_count;
683 fprintf(stderr, "Ignoring invalid count: %s\n",
690 if (opt_filename != NULL)
692 opt_filename = strdup (optarg);
699 new_interval = atof (optarg);
700 if (new_interval < 0.001)
701 fprintf (stderr, "Ignoring invalid interval: %s\n",
704 opt_interval = new_interval;
711 double t = strtod (optarg, &endp);
712 if ((optarg[0] != 0) && (endp != NULL) && (*endp == 0))
715 fprintf (stderr, "Ignoring invalid timeout: %s\n",
722 if (opt_srcaddr != NULL)
724 opt_srcaddr = strdup (optarg);
739 new_send_ttl = atoi (optarg);
740 if ((new_send_ttl > 0) && (new_send_ttl < 256))
741 opt_send_ttl = new_send_ttl;
743 fprintf (stderr, "Ignoring invalid TTL argument: %s\n",
749 set_opt_send_qos (optarg);
755 opt_outfile = strdup (optarg);
761 double new_percentile;
762 new_percentile = atof (optarg);
763 if (isnan (new_percentile)
764 || (new_percentile < 0.1)
765 || (new_percentile > 100.0))
766 fprintf (stderr, "Ignoring invalid percentile: %s\n",
769 opt_percentile = new_percentile;
775 if (strcasecmp ("none", optarg) == 0)
777 else if (strcasecmp ("prettyping", optarg) == 0)
779 else if (strcasecmp ("histogram", optarg) == 0)
781 else if (strcasecmp ("boxplot", optarg) == 0)
784 fprintf (stderr, "Unknown graph option: %s\n", optarg);
801 tmp = strtod (optarg, &endptr);
802 if ((errno != 0) || (endptr == NULL) || (*endptr != 0) || (tmp < 0.0) || (tmp > 100.0))
804 fprintf (stderr, "Ignoring invalid -Z argument: %s\n", optarg);
805 fprintf (stderr, "The \"-Z\" option requires a numeric argument between 0 and 100.\n");
808 opt_exit_status_threshold = tmp / 100.0;
814 usage_exit (argv[0], 0);
818 usage_exit (argv[0], 1);
822 if (opt_percentile <= 0.0)
823 opt_percentile = OPING_DEFAULT_PERCENTILE;
826 } /* }}} read_options */
828 static void time_normalize (struct timespec *ts) /* {{{ */
830 while (ts->tv_nsec < 0)
839 ts->tv_nsec += 1000000000;
842 while (ts->tv_nsec >= 1000000000)
845 ts->tv_nsec -= 1000000000;
847 } /* }}} void time_normalize */
849 static void time_calc (struct timespec *ts_dest, /* {{{ */
850 const struct timespec *ts_int,
851 const struct timeval *tv_begin,
852 const struct timeval *tv_end)
854 ts_dest->tv_sec = tv_begin->tv_sec + ts_int->tv_sec;
855 ts_dest->tv_nsec = (tv_begin->tv_usec * 1000) + ts_int->tv_nsec;
856 time_normalize (ts_dest);
858 /* Assure that `(begin + interval) > end'.
859 * This may seem overly complicated, but `tv_sec' is of type `time_t'
860 * which may be `unsigned. *sigh* */
861 if ((tv_end->tv_sec > ts_dest->tv_sec)
862 || ((tv_end->tv_sec == ts_dest->tv_sec)
863 && ((tv_end->tv_usec * 1000) > ts_dest->tv_nsec)))
866 ts_dest->tv_nsec = 0;
870 ts_dest->tv_sec = ts_dest->tv_sec - tv_end->tv_sec;
871 ts_dest->tv_nsec = ts_dest->tv_nsec - (tv_end->tv_usec * 1000);
872 time_normalize (ts_dest);
873 } /* }}} void time_calc */
876 static _Bool has_utf8() /* {{{ */
878 # if HAVE_NCURSESW_NCURSES_H
881 /* Automatically determine */
882 if (strcasecmp ("UTF-8", nl_langinfo (CODESET)) == 0)
887 return ((_Bool) (opt_utf8 - 1));
891 } /* }}} _Bool has_utf8 */
893 static int update_graph_boxplot (ping_context_t *ctx) /* {{{ */
903 if (ctx->history_received == 0)
906 x_max = (size_t) getmaxx (ctx->window);
911 counters = calloc (x_max, sizeof (*counters));
912 ratios = calloc (x_max, sizeof (*ratios));
915 for (i = 0; i < ctx->history_received; i++)
917 double latency = ctx->history_by_value[i] / 1000.0;
918 size_t index = (size_t) (((double) x_max) * latency / opt_interval);
926 /* Sum and calc ratios */
927 ratios[0] = ((double) counters[0]) / ((double) ctx->history_received);
928 for (x = 1; x < x_max; x++)
930 counters[x] += counters[x - 1];
931 ratios[x] = ((double) counters[x]) / ((double) ctx->history_received);
934 for (x = 0; x < x_max; x++)
941 if (ratios[x] >= 0.5)
943 symbol = BOXPLOT_MEDIAN;
946 else if (ratios[x] > 0.25)
948 symbol = BOXPLOT_BOX;
951 else if (ratios[x] > 0.025)
952 symbol = BOXPLOT_WHISKER_BAR;
954 symbol = ' '; /* NOP */
958 if ((ratios[x - 1] < 0.5) && (ratios[x] >= 0.5))
960 symbol = BOXPLOT_MEDIAN;
963 else if (((ratios[x] >= 0.25) && (ratios[x] <= 0.75))
964 || ((ratios[x - 1] < 0.75) && (ratios[x] > 0.75)))
966 symbol = BOXPLOT_BOX;
969 else if ((ratios[x] < 0.5) && (ratios[x] >= 0.025))
971 if (ratios[x - 1] < 0.025)
972 symbol = BOXPLOT_WHISKER_LEFT_END;
974 symbol = BOXPLOT_WHISKER_BAR;
976 else if ((ratios[x] > .5) && (ratios[x] < 0.975))
978 symbol = BOXPLOT_WHISKER_BAR;
980 else if ((ratios[x] >= 0.975) && (ratios[x - 1] < 0.975))
981 symbol = BOXPLOT_WHISKER_RIGHT_END;
985 wattron (ctx->window, A_REVERSE);
986 mvwaddch (ctx->window, /* y = */ 3, /* x = */ (int) (x + 2), symbol);
987 // mvwprintw (ctx->window, /* y = */ 3, /* x = */ (int) (x + 2), symbol);
989 wattroff (ctx->window, A_REVERSE);
995 } /* }}} int update_graph_boxplot */
997 static int update_graph_prettyping (ping_context_t *ctx, /* {{{ */
998 double latency, unsigned int sequence)
1002 size_t history_offset;
1004 x_max = (size_t) getmaxx (ctx->window);
1009 /* Determine the first index in the history we need to draw
1012 if (((size_t) x_max) < ctx->history_size) /* window is smaller than history */
1014 if (ctx->history_index > x_max)
1015 history_offset = ctx->history_index - x_max;
1016 else /* wrap around */
1017 history_offset = ctx->history_index + ctx->history_size - x_max;
1019 else /* window is larger than history */
1021 if (ctx->history_index != ctx->history_size) /* no longer growing. */
1022 history_offset = ctx->history_index;
1027 for (x = 0; x < x_max; x++)
1032 int color = OPING_RED;
1033 char const *symbol = "!";
1036 if (x >= ctx->history_size)
1038 mvwaddch (ctx->window, /* y = */ 3, /* x = */ x + 2, ' ');
1042 index = (history_offset + x) % ctx->history_size;
1043 latency = ctx->history_by_time[index];
1049 size_t symbols_num = hist_symbols_acs_num;
1050 size_t colors_num = 1;
1052 size_t index_symbols;
1053 size_t index_colors;
1056 /* latency is in milliseconds, opt_interval is in seconds. */
1057 ratio = (latency * 0.001) / opt_interval;
1063 symbols_num = hist_symbols_utf8_num;
1065 if (has_colors () == TRUE)
1066 colors_num = hist_colors_num;
1068 intensity = (size_t) (ratio * ((double) (symbols_num * colors_num)));
1069 if (intensity >= (symbols_num * colors_num))
1070 intensity = (symbols_num * colors_num) - 1;
1072 index_symbols = intensity % symbols_num;
1073 assert (index_symbols < symbols_num);
1075 index_colors = intensity / symbols_num;
1076 assert (index_colors < colors_num);
1080 color = hist_colors_utf8[index_colors];
1081 symbol = hist_symbols_utf8[index_symbols];
1085 color = hist_colors_acs[index_colors];
1086 symbolc = hist_symbols_acs[index_symbols] | A_ALTCHARSET;
1089 else /* if (!(latency >= 0.0)) */
1090 wattron (ctx->window, A_BOLD);
1092 if (has_colors () == TRUE)
1093 wattron (ctx->window, COLOR_PAIR(color));
1096 mvwprintw (ctx->window, /* y = */ 3, /* x = */ x + 2, symbol);
1098 mvwaddch (ctx->window, /* y = */ 3, /* x = */ x + 2, symbolc);
1100 if (has_colors () == TRUE)
1101 wattroff (ctx->window, COLOR_PAIR(color));
1103 /* Use negation here to handle NaN correctly. */
1104 if (!(latency >= 0.0))
1105 wattroff (ctx->window, A_BOLD);
1109 } /* }}} int update_graph_prettyping */
1111 static int update_graph_histogram (ping_context_t *ctx) /* {{{ */
1114 uint32_t *accumulated;
1120 size_t symbols_num = hist_symbols_acs_num;
1122 clean_history (ctx);
1124 if (ctx->history_received == 0)
1128 symbols_num = hist_symbols_utf8_num;
1130 x_max = (size_t) getmaxx (ctx->window);
1135 counters = calloc (x_max, sizeof (*counters));
1136 accumulated = calloc (x_max, sizeof (*accumulated));
1140 for (i = 0; i < ctx->history_received; i++)
1142 double latency = ctx->history_by_value[i] / 1000.0;
1143 size_t index = (size_t) (((double) x_max) * latency / opt_interval);
1149 if (max < counters[index])
1150 max = counters[index];
1154 accumulated[0] = counters[0];
1155 for (x = 1; x < x_max; x++)
1156 accumulated[x] = counters[x] + accumulated[x - 1];
1158 /* Calculate ratios */
1159 for (x = 0; x < x_max; x++)
1161 double height = ((double) counters[x]) / ((double) max);
1162 double ratio_this = ((double) accumulated[x]) / ((double) ctx->history_received);
1163 double ratio_prev = 0.0;
1167 index = (size_t) (height * ((double) symbols_num));
1168 if (index >= symbols_num)
1169 index = symbols_num - 1;
1172 ratio_prev = ((double) accumulated[x - 1]) / ((double) ctx->history_received);
1174 if (has_colors () == TRUE)
1176 if ((ratio_this <= threshold_green)
1177 || ((ratio_prev < threshold_green)
1178 && (ratio_this > threshold_green)))
1179 color = OPING_GREEN;
1180 else if ((ratio_this <= threshold_yellow)
1181 || ((ratio_prev < threshold_yellow)
1182 && (ratio_this > threshold_yellow)))
1183 color = OPING_YELLOW;
1187 wattron (ctx->window, COLOR_PAIR(color));
1190 if (counters[x] == 0)
1191 mvwaddch (ctx->window, /* y = */ 3, /* x = */ x + 2, ' ');
1192 else if (has_utf8 ())
1193 mvwprintw (ctx->window, /* y = */ 3, /* x = */ x + 2,
1194 hist_symbols_utf8[index]);
1196 mvwaddch (ctx->window, /* y = */ 3, /* x = */ x + 2,
1197 hist_symbols_acs[index] | A_ALTCHARSET);
1199 if (has_colors () == TRUE)
1200 wattroff (ctx->window, COLOR_PAIR(color));
1206 } /* }}} int update_graph_histogram */
1208 static int update_stats_from_context (ping_context_t *ctx, pingobj_iter_t *iter) /* {{{ */
1210 double latency = -1.0;
1211 size_t buffer_len = sizeof (latency);
1213 ping_iterator_get_info (iter, PING_INFO_LATENCY,
1214 &latency, &buffer_len);
1216 unsigned int sequence = 0;
1217 buffer_len = sizeof (sequence);
1218 ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
1219 &sequence, &buffer_len);
1222 if ((ctx == NULL) || (ctx->window == NULL))
1225 /* werase (ctx->window); */
1227 box (ctx->window, 0, 0);
1228 wattron (ctx->window, A_BOLD);
1229 mvwprintw (ctx->window, /* y = */ 0, /* x = */ 5,
1231 wattroff (ctx->window, A_BOLD);
1232 wprintw (ctx->window, "ping statistics ");
1233 mvwprintw (ctx->window, /* y = */ 1, /* x = */ 2,
1234 "%i packets transmitted, %i received, %.2f%% packet "
1235 "loss, time %.1fms",
1236 ctx->req_sent, ctx->req_rcvd,
1237 context_get_packet_loss (ctx),
1238 ctx->latency_total);
1239 if (ctx->req_rcvd != 0)
1246 min = percentile_to_latency (ctx, 0.0);
1247 median = percentile_to_latency (ctx, 50.0);
1248 max = percentile_to_latency (ctx, 100.0);
1249 percentile = percentile_to_latency (ctx, opt_percentile);
1251 mvwprintw (ctx->window, /* y = */ 2, /* x = */ 2,
1252 "RTT[ms]: min = %.0f, median = %.0f, p(%.0f) = %.0f, max = %.0f ",
1253 min, median, opt_percentile, percentile, max);
1256 if (opt_show_graph == 1)
1257 update_graph_prettyping (ctx, latency, sequence);
1258 else if (opt_show_graph == 2)
1259 update_graph_histogram (ctx);
1260 else if (opt_show_graph == 3)
1261 update_graph_boxplot (ctx);
1263 wrefresh (ctx->window);
1266 } /* }}} int update_stats_from_context */
1268 static int on_resize (pingobj_t *ping) /* {{{ */
1270 pingobj_iter_t *iter;
1273 int main_win_height;
1274 int box_height = (opt_show_graph == 0) ? 4 : 5;
1276 getmaxyx (stdscr, height, width);
1277 if ((height < 1) || (width < 1))
1280 main_win_height = height - (box_height * host_num);
1281 wresize (main_win, main_win_height, /* width = */ width);
1282 /* Allow scrolling */
1283 scrollok (main_win, TRUE);
1284 /* wsetscrreg (main_win, 0, main_win_height - 1); */
1285 /* Allow hardware accelerated scrolling. */
1286 idlok (main_win, TRUE);
1287 wrefresh (main_win);
1289 for (iter = ping_iterator_get (ping);
1291 iter = ping_iterator_next (iter))
1293 ping_context_t *context;
1295 context = ping_iterator_get_context (iter);
1296 if (context == NULL)
1299 if (context->window != NULL)
1301 delwin (context->window);
1302 context->window = NULL;
1304 context->window = newwin (/* height = */ box_height,
1305 /* width = */ width,
1306 /* y = */ main_win_height + (box_height * context->index),
1313 static int check_resize (pingobj_t *ping) /* {{{ */
1315 int need_resize = 0;
1319 int key = wgetch (stdscr);
1322 else if (key == KEY_RESIZE)
1324 else if (key == 'g')
1326 if (opt_show_graph == 3)
1328 else if (opt_show_graph > 0)
1334 return (on_resize (ping));
1337 } /* }}} int check_resize */
1339 static int pre_loop_hook (pingobj_t *ping) /* {{{ */
1341 pingobj_iter_t *iter;
1344 int main_win_height;
1345 int box_height = (opt_show_graph == 0) ? 4 : 5;
1350 nodelay (stdscr, TRUE);
1352 getmaxyx (stdscr, height, width);
1353 if ((height < 1) || (width < 1))
1356 if (has_colors () == TRUE)
1359 init_pair (OPING_GREEN, COLOR_GREEN, /* default = */ 0);
1360 init_pair (OPING_YELLOW, COLOR_YELLOW, /* default = */ 0);
1361 init_pair (OPING_RED, COLOR_RED, /* default = */ 0);
1362 init_pair (OPING_GREEN_HIST, COLOR_GREEN, COLOR_BLACK);
1363 init_pair (OPING_YELLOW_HIST, COLOR_YELLOW, COLOR_GREEN);
1364 init_pair (OPING_RED_HIST, COLOR_RED, COLOR_YELLOW);
1367 main_win_height = height - (box_height * host_num);
1368 main_win = newwin (/* height = */ main_win_height,
1369 /* width = */ width,
1370 /* y = */ 0, /* x = */ 0);
1371 /* Allow scrolling */
1372 scrollok (main_win, TRUE);
1373 /* wsetscrreg (main_win, 0, main_win_height - 1); */
1374 /* Allow hardware accelerated scrolling. */
1375 idlok (main_win, TRUE);
1376 wmove (main_win, /* y = */ main_win_height - 1, /* x = */ 0);
1377 wrefresh (main_win);
1379 for (iter = ping_iterator_get (ping);
1381 iter = ping_iterator_next (iter))
1383 ping_context_t *context;
1385 context = ping_iterator_get_context (iter);
1386 if (context == NULL)
1389 if (context->window != NULL)
1391 delwin (context->window);
1392 context->window = NULL;
1394 context->window = newwin (/* height = */ box_height,
1395 /* width = */ width,
1396 /* y = */ main_win_height + (box_height * context->index),
1401 /* Don't know what good this does exactly, but without this code
1402 * "check_resize" will be called right after startup and *somehow*
1403 * this leads to display errors. If we purge all initial characters
1404 * here, the problem goes away. "wgetch" is non-blocking due to
1405 * "nodelay" (see above). */
1406 while (wgetch (stdscr) != ERR)
1408 /* eat up characters */;
1412 } /* }}} int pre_loop_hook */
1414 static int pre_sleep_hook (pingobj_t *ping) /* {{{ */
1416 return (check_resize (ping));
1417 } /* }}} int pre_sleep_hook */
1419 static int post_sleep_hook (pingobj_t *ping) /* {{{ */
1421 return (check_resize (ping));
1422 } /* }}} int pre_sleep_hook */
1423 #else /* if !USE_NCURSES */
1424 static int pre_loop_hook (pingobj_t *ping) /* {{{ */
1426 pingobj_iter_t *iter;
1428 for (iter = ping_iterator_get (ping);
1430 iter = ping_iterator_next (iter))
1432 ping_context_t *ctx;
1435 ctx = ping_iterator_get_context (iter);
1440 ping_iterator_get_info (iter, PING_INFO_DATA, NULL, &buffer_size);
1442 printf ("PING %s (%s) %zu bytes of data.\n",
1443 ctx->host, ctx->addr, buffer_size);
1447 } /* }}} int pre_loop_hook */
1449 static int pre_sleep_hook (__attribute__((unused)) pingobj_t *ping) /* {{{ */
1454 } /* }}} int pre_sleep_hook */
1456 static int post_sleep_hook (__attribute__((unused)) pingobj_t *ping) /* {{{ */
1459 } /* }}} int post_sleep_hook */
1462 static void update_context (ping_context_t *ctx, double latency) /* {{{ */
1469 ctx->latency_total += latency;
1476 ctx->history_by_time[ctx->history_index] = latency;
1478 ctx->history_dirty = 1;
1480 /* Update index and size. */
1481 ctx->history_index = (ctx->history_index + 1) % HISTORY_SIZE_MAX;
1482 if (ctx->history_size < HISTORY_SIZE_MAX)
1483 ctx->history_size++;
1484 } /* }}} void update_context */
1486 static void update_host_hook (pingobj_iter_t *iter, /* {{{ */
1487 __attribute__((unused)) int index)
1490 unsigned int sequence;
1493 char recv_qos_str[16];
1496 ping_context_t *context;
1499 buffer_len = sizeof (latency);
1500 ping_iterator_get_info (iter, PING_INFO_LATENCY,
1501 &latency, &buffer_len);
1504 buffer_len = sizeof (sequence);
1505 ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
1506 &sequence, &buffer_len);
1509 buffer_len = sizeof (recv_ttl);
1510 ping_iterator_get_info (iter, PING_INFO_RECV_TTL,
1511 &recv_ttl, &buffer_len);
1514 buffer_len = sizeof (recv_qos);
1515 ping_iterator_get_info (iter, PING_INFO_RECV_QOS,
1516 &recv_qos, &buffer_len);
1519 ping_iterator_get_info (iter, PING_INFO_DATA,
1522 context = (ping_context_t *) ping_iterator_get_context (iter);
1525 # define HOST_PRINTF(...) wprintw(main_win, __VA_ARGS__)
1527 # define HOST_PRINTF(...) printf(__VA_ARGS__)
1530 update_context (context, latency);
1535 if (has_colors () == TRUE)
1538 int color = OPING_GREEN;
1540 ratio = latency_to_ratio (context, latency);
1541 if (ratio < threshold_green)
1542 color = OPING_GREEN;
1543 else if (ratio < threshold_yellow)
1544 color = OPING_YELLOW;
1548 HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i ",
1549 data_len, context->host, context->addr,
1551 format_qos (recv_qos, recv_qos_str, sizeof (recv_qos_str)));
1552 if ((recv_qos != 0) || (opt_send_qos != 0))
1554 HOST_PRINTF ("qos=%s ",
1555 format_qos (recv_qos, recv_qos_str, sizeof (recv_qos_str)));
1557 HOST_PRINTF ("time=");
1558 wattron (main_win, COLOR_PAIR(color));
1559 HOST_PRINTF ("%.2f", latency);
1560 wattroff (main_win, COLOR_PAIR(color));
1561 HOST_PRINTF (" ms\n");
1566 HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i ",
1568 context->host, context->addr,
1569 sequence, recv_ttl);
1570 if ((recv_qos != 0) || (opt_send_qos != 0))
1572 HOST_PRINTF ("qos=%s ",
1573 format_qos (recv_qos, recv_qos_str, sizeof (recv_qos_str)));
1575 HOST_PRINTF ("time=%.2f ms\n", latency);
1580 else /* if (!(latency > 0.0)) */
1583 if (has_colors () == TRUE)
1585 HOST_PRINTF ("echo reply from %s (%s): icmp_seq=%u ",
1586 context->host, context->addr,
1588 wattron (main_win, COLOR_PAIR(OPING_RED) | A_BOLD);
1589 HOST_PRINTF ("timeout");
1590 wattroff (main_win, COLOR_PAIR(OPING_RED) | A_BOLD);
1596 HOST_PRINTF ("echo reply from %s (%s): icmp_seq=%u timeout\n",
1597 context->host, context->addr,
1604 if (outfile != NULL)
1606 struct timespec ts = { 0, 0 };
1608 if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
1610 double t = ((double) ts.tv_sec) + (((double) ts.tv_nsec) / 1000000000.0);
1612 if ((sequence % 32) == 0)
1613 fprintf (outfile, "#time,host,latency[ms]\n");
1615 fprintf (outfile, "%.3f,\"%s\",%.2f\n", t, context->host, latency);
1620 update_stats_from_context (context, iter);
1621 wrefresh (main_win);
1623 } /* }}} void update_host_hook */
1625 /* Prints statistics for each host, cleans up the contexts and returns the
1626 * number of hosts which failed to return more than the fraction
1627 * opt_exit_status_threshold of pings. */
1628 static int post_loop_hook (pingobj_t *ping) /* {{{ */
1630 pingobj_iter_t *iter;
1631 int failure_count = 0;
1637 for (iter = ping_iterator_get (ping);
1639 iter = ping_iterator_next (iter))
1641 ping_context_t *context;
1643 context = ping_iterator_get_context (iter);
1645 printf ("\n--- %s ping statistics ---\n"
1646 "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n",
1647 context->host, context->req_sent, context->req_rcvd,
1648 context_get_packet_loss (context),
1649 context->latency_total);
1652 double pct_failed = 1.0 - (((double) context->req_rcvd)
1653 / ((double) context->req_sent));
1654 if (pct_failed > opt_exit_status_threshold)
1658 if (context->req_rcvd != 0)
1665 min = percentile_to_latency (context, 0.0);
1666 median = percentile_to_latency (context, 50.0);
1667 max = percentile_to_latency (context, 100.0);
1668 percentile = percentile_to_latency (context, opt_percentile);
1670 printf ("RTT[ms]: min = %.0f, median = %.0f, p(%.0f) = %.0f, max = %.0f\n",
1671 min, median, opt_percentile, percentile, max);
1674 ping_iterator_set_context (iter, NULL);
1675 context_destroy (context);
1678 return (failure_count);
1679 } /* }}} int post_loop_hook */
1681 int main (int argc, char **argv) /* {{{ */
1684 pingobj_iter_t *iter;
1686 struct sigaction sigint_action;
1688 struct timeval tv_begin;
1689 struct timeval tv_end;
1690 struct timespec ts_wait;
1691 struct timespec ts_int;
1696 #if _POSIX_SAVED_IDS
1697 uid_t saved_set_uid;
1699 /* Save the old effective user id */
1700 saved_set_uid = geteuid ();
1701 /* Set the effective user ID to the real user ID without changing the
1702 * saved set-user ID */
1703 status = seteuid (getuid ());
1706 fprintf (stderr, "Temporarily dropping privileges "
1707 "failed: %s\n", strerror (errno));
1708 exit (EXIT_FAILURE);
1712 setlocale(LC_ALL, "");
1713 optind = read_options (argc, argv);
1715 #if !_POSIX_SAVED_IDS
1716 /* Cannot temporarily drop privileges -> reject every file but "-". */
1717 if ((opt_filename != NULL)
1718 && (strcmp ("-", opt_filename) != 0)
1719 && (getuid () != geteuid ()))
1721 fprintf (stderr, "Your real and effective user IDs don't "
1722 "match. Reading from a file (option '-f')\n"
1723 "is therefore too risky. You can still read "
1724 "from STDIN using '-f -' if you like.\n"
1726 exit (EXIT_FAILURE);
1730 if ((optind >= argc) && (opt_filename == NULL)) {
1731 usage_exit (argv[0], 1);
1734 if ((ping = ping_construct ()) == NULL)
1736 fprintf (stderr, "ping_construct failed\n");
1740 if (ping_setopt (ping, PING_OPT_TTL, &opt_send_ttl) != 0)
1742 fprintf (stderr, "Setting TTL to %i failed: %s\n",
1743 opt_send_ttl, ping_get_error (ping));
1746 if (ping_setopt (ping, PING_OPT_QOS, &opt_send_qos) != 0)
1748 fprintf (stderr, "Setting TOS to %i failed: %s\n",
1749 opt_send_qos, ping_get_error (ping));
1756 temp_nsec = modf (opt_interval, &temp_sec);
1757 ts_int.tv_sec = (time_t) temp_sec;
1758 ts_int.tv_nsec = (long) (temp_nsec * 1000000000L);
1760 /* printf ("ts_int = %i.%09li\n", (int) ts_int.tv_sec, ts_int.tv_nsec); */
1763 if (ping_setopt (ping, PING_OPT_TIMEOUT, (void*)(&opt_timeout)) != 0)
1765 fprintf (stderr, "Setting timeout failed: %s\n",
1766 ping_get_error (ping));
1769 if (opt_addrfamily != PING_DEF_AF)
1770 ping_setopt (ping, PING_OPT_AF, (void *) &opt_addrfamily);
1772 if (opt_srcaddr != NULL)
1774 if (ping_setopt (ping, PING_OPT_SOURCE, (void *) opt_srcaddr) != 0)
1776 fprintf (stderr, "Setting source address failed: %s\n",
1777 ping_get_error (ping));
1781 if (opt_device != NULL)
1783 if (ping_setopt (ping, PING_OPT_DEVICE, (void *) opt_device) != 0)
1785 fprintf (stderr, "Setting device failed: %s\n",
1786 ping_get_error (ping));
1790 if (opt_mark != NULL)
1793 int mark = (int) strtol (opt_mark, &endp, /* base = */ 0);
1794 if ((opt_mark[0] != 0) && (endp != NULL) && (*endp == 0))
1796 if (ping_setopt(ping, PING_OPT_MARK, (void*)(&mark)) != 0)
1798 fprintf (stderr, "Setting mark failed: %s\n",
1799 ping_get_error (ping));
1804 fprintf(stderr, "Ignoring invalid mark: %s\n", optarg);
1808 if (opt_filename != NULL)
1814 if (strcmp (opt_filename, "-") == 0)
1816 infile = fdopen(0, "r");
1818 infile = fopen(opt_filename, "r");
1822 fprintf (stderr, "Opening %s failed: %s\n",
1823 (strcmp (opt_filename, "-") == 0)
1824 ? "STDIN" : opt_filename,
1829 #if _POSIX_SAVED_IDS
1830 /* Regain privileges */
1831 status = seteuid (saved_set_uid);
1834 fprintf (stderr, "Temporarily re-gaining privileges "
1835 "failed: %s\n", strerror (errno));
1836 exit (EXIT_FAILURE);
1840 while (fgets(line, sizeof(line), infile))
1842 /* Strip whitespace */
1843 if (sscanf(line, "%s", host) != 1)
1846 if ((host[0] == 0) || (host[0] == '#'))
1849 if (ping_host_add(ping, host) < 0)
1851 const char *errmsg = ping_get_error (ping);
1853 fprintf (stderr, "Adding host `%s' failed: %s\n", host, errmsg);
1862 #if _POSIX_SAVED_IDS
1863 /* Drop privileges */
1864 status = seteuid (getuid ());
1867 fprintf (stderr, "Temporarily dropping privileges "
1868 "failed: %s\n", strerror (errno));
1869 exit (EXIT_FAILURE);
1876 #if _POSIX_SAVED_IDS
1877 /* Regain privileges */
1878 status = seteuid (saved_set_uid);
1881 fprintf (stderr, "Temporarily re-gaining privileges "
1882 "failed: %s\n", strerror (errno));
1883 exit (EXIT_FAILURE);
1887 for (i = optind; i < argc; i++)
1889 if (ping_host_add (ping, argv[i]) < 0)
1891 const char *errmsg = ping_get_error (ping);
1893 fprintf (stderr, "Adding host `%s' failed: %s\n", argv[i], errmsg);
1902 /* Permanently drop root privileges if we're setuid-root. */
1903 status = setuid (getuid ());
1906 fprintf (stderr, "Dropping privileges failed: %s\n",
1908 exit (EXIT_FAILURE);
1912 exit (EXIT_FAILURE);
1914 #if _POSIX_SAVED_IDS
1915 saved_set_uid = (uid_t) -1;
1918 if (opt_outfile != NULL)
1920 outfile = fopen (opt_outfile, "a");
1921 if (outfile == NULL)
1923 fprintf (stderr, "opening \"%s\" failed: %s\n",
1924 opt_outfile, strerror (errno));
1925 exit (EXIT_FAILURE);
1929 ping_initialize_contexts (ping);
1934 memset (&sigint_action, '\0', sizeof (sigint_action));
1935 sigint_action.sa_handler = sigint_handler;
1936 if (sigaction (SIGINT, &sigint_action, NULL) < 0)
1938 perror ("sigaction");
1942 pre_loop_hook (ping);
1944 while (opt_count != 0)
1949 if (gettimeofday (&tv_begin, NULL) < 0)
1951 perror ("gettimeofday");
1955 status = ping_send (ping);
1956 if (status == -EINTR)
1960 else if (status < 0)
1962 fprintf (stderr, "ping_send failed: %s\n",
1963 ping_get_error (ping));
1968 for (iter = ping_iterator_get (ping);
1970 iter = ping_iterator_next (iter))
1972 update_host_hook (iter, index);
1976 pre_sleep_hook (ping);
1978 /* Don't sleep in the last iteration */
1982 if (gettimeofday (&tv_end, NULL) < 0)
1984 perror ("gettimeofday");
1988 time_calc (&ts_wait, &ts_int, &tv_begin, &tv_end);
1990 /* printf ("Sleeping for %i.%09li seconds\n", (int) ts_wait.tv_sec, ts_wait.tv_nsec); */
1991 while ((status = nanosleep (&ts_wait, &ts_wait)) != 0)
1999 perror ("nanosleep");
2004 post_sleep_hook (ping);
2008 } /* while (opt_count != 0) */
2010 /* Returns the number of failed hosts according to -Z. */
2011 status = post_loop_hook (ping);
2013 ping_destroy (ping);
2015 if (outfile != NULL)
2022 exit (EXIT_SUCCESS);
2029 } /* }}} int main */
2031 /* vim: set fdm=marker : */