ping_set_tos: Improve error handling.
[liboping.git] / src / oping.c
index 4f9c5f3..313b83d 100644 (file)
@@ -63,6 +63,7 @@
 #endif
 
 #if USE_NCURSES
+# define NCURSES_OPAQUE 1
 # include <ncurses.h>
 
 # define OPING_GREEN 1
@@ -102,6 +103,7 @@ static char   *opt_device     = NULL;
 static char   *opt_filename   = NULL;
 static int     opt_count      = -1;
 static int     opt_send_ttl   = 64;
+static unsigned opt_send_tos  = 0;
 
 static int host_num = 0;
 
@@ -245,6 +247,7 @@ static void usage_exit (const char *name, int status) /* {{{ */
                        "  -c count     number of ICMP packets to send\n"
                        "  -i interval  interval with which to send ICMP packets\n"
                        "  -t ttl       time to live for each ICMP packet\n"
+                       "  -z tos       Type-of-service/class-of-service for each ICMP packet\n"
                        "  -I srcaddr   source address\n"
                        "  -D device    outgoing interface name\n"
                        "  -f filename  filename to read hosts from\n"
@@ -262,7 +265,7 @@ static int read_options (int argc, char **argv) /* {{{ */
 
        while (1)
        {
-               optchar = getopt (argc, argv, "46c:hi:I:t:f:D:");
+               optchar = getopt (argc, argv, "46c:hi:I:t:z:f:D:");
 
                if (optchar == -1)
                        break;
@@ -329,6 +332,18 @@ static int read_options (int argc, char **argv) /* {{{ */
                                break;
                        }
 
+                       case 'z':
+                       {
+                               int new_send_tos;
+                               new_send_tos = atoi (optarg);
+                               if ((new_send_tos > 0) && (new_send_tos < 256))
+                                       opt_send_tos = new_send_tos;
+                               else
+                                       fprintf (stderr, "Ignoring invalid TOS argument: %s\n",
+                                                       optarg);
+                               break;
+                       }
+
                        case 'h':
                                usage_exit (argv[0], 0);
                                break;
@@ -440,14 +455,7 @@ static int on_resize (pingobj_t *ping) /* {{{ */
                return (EINVAL);
 
        main_win_height = height - (4 * host_num);
-#if 1
-       wresize (main_win, main_win_height, /* width = */ 0);
-#else
-       delwin (main_win);
-       main_win = newwin (/* height = */ main_win_height,
-                       /* width = */ 0,
-                       /* y = */ 0, /* x = */ 0);
-#endif
+       wresize (main_win, main_win_height, /* width = */ width);
        /* Allow scrolling */
        scrollok (main_win, TRUE);
        /* wsetscrreg (main_win, 0, main_win_height - 1); */
@@ -623,6 +631,7 @@ static void update_host_hook (pingobj_iter_t *iter, /* {{{ */
        double          latency;
        unsigned int    sequence;
        int             recv_ttl;
+       unsigned        recv_tos;
        size_t          buffer_len;
        size_t          data_len;
        ping_context_t *context;
@@ -642,6 +651,11 @@ static void update_host_hook (pingobj_iter_t *iter, /* {{{ */
        ping_iterator_get_info (iter, PING_INFO_RECV_TTL,
                        &recv_ttl, &buffer_len);
 
+       recv_tos = 0;
+       buffer_len = sizeof (recv_tos);
+       ping_iterator_get_info (iter, PING_INFO_RECV_TOS,
+                       &recv_tos, &buffer_len);
+
        data_len = 0;
        ping_iterator_get_info (iter, PING_INFO_DATA,
                        NULL, &data_len);
@@ -680,10 +694,10 @@ static void update_host_hook (pingobj_iter_t *iter, /* {{{ */
                                        || (latency > (average + stddev)))
                                color = OPING_YELLOW;
 
-                       HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i "
+                       HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i tos=%u "
                                        "time=",
                                        data_len, context->host, context->addr,
-                                       sequence, recv_ttl);
+                                       sequence, recv_ttl, recv_tos);
                        wattron (main_win, COLOR_PAIR(color));
                        HOST_PRINTF ("%.2f", latency);
                        wattroff (main_win, COLOR_PAIR(color));
@@ -692,11 +706,11 @@ static void update_host_hook (pingobj_iter_t *iter, /* {{{ */
                else
                {
 #endif
-               HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i "
+               HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i tos=%u "
                                "time=%.2f ms\n",
                                data_len,
                                context->host, context->addr,
-                               sequence, recv_ttl, latency);
+                               sequence, recv_ttl, recv_tos, latency);
 #if USE_NCURSES
                }
 #endif
@@ -839,6 +853,12 @@ int main (int argc, char **argv) /* {{{ */
                                opt_send_ttl, ping_get_error (ping));
        }
 
+       if (ping_setopt (ping, PING_OPT_TOS, &opt_send_tos) != 0)
+       {
+               fprintf (stderr, "Setting TOS to %i failed: %s\n",
+                               opt_send_tos, ping_get_error (ping));
+       }
+
        {
                double temp_sec;
                double temp_nsec;