+static int pre_sleep_hook (pingobj_t *ping) /* {{{ */
+{
+ return (check_resize (ping));
+} /* }}} int pre_sleep_hook */
+
+static int post_sleep_hook (pingobj_t *ping) /* {{{ */
+{
+ return (check_resize (ping));
+} /* }}} int pre_sleep_hook */
+#else /* if !USE_NCURSES */
+static int pre_loop_hook (pingobj_t *ping) /* {{{ */
+{
+ pingobj_iter_t *iter;
+
+ for (iter = ping_iterator_get (ping);
+ iter != NULL;
+ iter = ping_iterator_next (iter))
+ {
+ ping_context_t *ctx;
+ size_t buffer_size;
+
+ ctx = ping_iterator_get_context (iter);
+ if (ctx == NULL)
+ continue;
+
+ buffer_size = 0;
+ ping_iterator_get_info (iter, PING_INFO_DATA, NULL, &buffer_size);
+
+ printf ("PING %s (%s) %zu bytes of data.\n",
+ ctx->host, ctx->addr, buffer_size);
+ }
+
+ return (0);
+} /* }}} int pre_loop_hook */
+
+static int pre_sleep_hook (__attribute__((unused)) pingobj_t *ping) /* {{{ */
+{
+ fflush (stdout);
+
+ return (0);
+} /* }}} int pre_sleep_hook */
+
+static int post_sleep_hook (__attribute__((unused)) pingobj_t *ping) /* {{{ */
+{
+ return (0);
+} /* }}} int post_sleep_hook */
+#endif
+
+static void update_host_hook (pingobj_iter_t *iter, /* {{{ */
+ int index)
+{
+ double latency;
+ unsigned int sequence;
+ int recv_ttl;
+ uint8_t recv_qos;
+ char recv_qos_str[16];
+ size_t buffer_len;
+ size_t data_len;
+ ping_context_t *context;
+
+ latency = -1.0;
+ buffer_len = sizeof (latency);
+ ping_iterator_get_info (iter, PING_INFO_LATENCY,
+ &latency, &buffer_len);
+
+ sequence = 0;
+ buffer_len = sizeof (sequence);
+ ping_iterator_get_info (iter, PING_INFO_SEQUENCE,
+ &sequence, &buffer_len);
+
+ recv_ttl = -1;
+ buffer_len = sizeof (recv_ttl);
+ ping_iterator_get_info (iter, PING_INFO_RECV_TTL,
+ &recv_ttl, &buffer_len);
+
+ recv_qos = 0;
+ buffer_len = sizeof (recv_qos);
+ ping_iterator_get_info (iter, PING_INFO_RECV_QOS,
+ &recv_qos, &buffer_len);
+
+ data_len = 0;
+ ping_iterator_get_info (iter, PING_INFO_DATA,
+ NULL, &data_len);
+
+ context = (ping_context_t *) ping_iterator_get_context (iter);
+
+#if USE_NCURSES
+# define HOST_PRINTF(...) wprintw(main_win, __VA_ARGS__)
+#else
+# define HOST_PRINTF(...) printf(__VA_ARGS__)
+#endif
+
+ context->req_sent++;
+ if (latency > 0.0)
+ {
+ context->req_rcvd++;
+ context->latency_total += latency;
+ context->latency_total_square += (latency * latency);
+
+ if ((context->latency_max < 0.0) || (context->latency_max < latency))
+ context->latency_max = latency;
+ if ((context->latency_min < 0.0) || (context->latency_min > latency))
+ context->latency_min = latency;
+
+#if USE_NCURSES
+ if (has_colors () == TRUE)
+ {
+ int color = OPING_GREEN;
+ double average = context_get_average (context);
+ double stddev = context_get_stddev (context);
+
+ if ((latency < (average - (2 * stddev)))
+ || (latency > (average + (2 * stddev))))
+ color = OPING_RED;
+ else if ((latency < (average - stddev))
+ || (latency > (average + stddev)))
+ color = OPING_YELLOW;
+
+ HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i ",
+ data_len, context->host, context->addr,
+ sequence, recv_ttl,
+ format_qos (recv_qos, recv_qos_str, sizeof (recv_qos_str)));
+ if ((recv_qos != 0) || (opt_send_qos != 0))
+ {
+ HOST_PRINTF ("qos=%s ",
+ format_qos (recv_qos, recv_qos_str, sizeof (recv_qos_str)));
+ }
+ HOST_PRINTF ("time=");
+ wattron (main_win, COLOR_PAIR(color));
+ HOST_PRINTF ("%.2f", latency);
+ wattroff (main_win, COLOR_PAIR(color));
+ HOST_PRINTF (" ms\n");
+ }
+ else
+ {
+#endif
+ HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i ",
+ data_len,
+ context->host, context->addr,
+ sequence, recv_ttl);
+ if ((recv_qos != 0) || (opt_send_qos != 0))
+ {
+ HOST_PRINTF ("qos=%s ",
+ format_qos (recv_qos, recv_qos_str, sizeof (recv_qos_str)));
+ }
+ HOST_PRINTF ("time=%.2f ms\n", latency);
+#if USE_NCURSES
+ }
+#endif
+ }
+ else
+ {
+#if USE_NCURSES
+ if (has_colors () == TRUE)
+ {
+ HOST_PRINTF ("echo reply from %s (%s): icmp_seq=%u ",
+ context->host, context->addr,
+ sequence);
+ wattron (main_win, COLOR_PAIR(OPING_RED) | A_BOLD);
+ HOST_PRINTF ("timeout");
+ wattroff (main_win, COLOR_PAIR(OPING_RED) | A_BOLD);
+ HOST_PRINTF ("\n");
+ }
+ else
+ {
+#endif
+ HOST_PRINTF ("echo reply from %s (%s): icmp_seq=%u timeout\n",
+ context->host, context->addr,
+ sequence);
+#if USE_NCURSES
+ }
+#endif
+ }
+
+#if USE_NCURSES
+ update_stats_from_context (context);
+ wrefresh (main_win);
+#endif
+} /* }}} void update_host_hook */
+
+static int post_loop_hook (pingobj_t *ping) /* {{{ */
+{
+ pingobj_iter_t *iter;
+
+#if USE_NCURSES
+ endwin ();
+#endif
+
+ for (iter = ping_iterator_get (ping);
+ iter != NULL;
+ iter = ping_iterator_next (iter))
+ {
+ ping_context_t *context;
+
+ context = ping_iterator_get_context (iter);
+
+ printf ("\n--- %s ping statistics ---\n"
+ "%i packets transmitted, %i received, %.2f%% packet loss, time %.1fms\n",
+ context->host, context->req_sent, context->req_rcvd,
+ context_get_packet_loss (context),
+ context->latency_total);
+
+ if (context->req_rcvd != 0)
+ {
+ double average;
+ double deviation;
+
+ average = context_get_average (context);
+ deviation = context_get_stddev (context);
+
+ printf ("rtt min/avg/max/sdev = %.3f/%.3f/%.3f/%.3f ms\n",
+ context->latency_min,
+ average,
+ context->latency_max,
+ deviation);
+ }
+
+ ping_iterator_set_context (iter, NULL);
+ context_destroy (context);
+ }
+
+ return (0);
+} /* }}} int post_loop_hook */
+
+int main (int argc, char **argv) /* {{{ */