X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Foping.c;h=78f053f31e4f3e8568ff32e18c22caa3731cce62;hb=21a27ab171f36612cb5249d5aa04584ce12a2624;hp=313b83db4cd7b9849fb9dfd2bd2e7e0e5b560cef;hpb=08991474f5e5c8208287f13351b7c25ff73acfb3;p=liboping.git diff --git a/src/oping.c b/src/oping.c index 313b83d..78f053f 100644 --- a/src/oping.c +++ b/src/oping.c @@ -25,6 +25,8 @@ # include # include # include +# include +# include # include # include #else @@ -50,6 +52,16 @@ # endif #endif +#if HAVE_SYS_SOCKET_H +# include +#endif +#if HAVE_NETINET_IN_H +# include +#endif +#if HAVE_NETINET_IP_H +# include +#endif + #if HAVE_NETDB_H # include /* NI_MAXHOST */ #endif @@ -103,7 +115,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 uint8_t opt_send_tos = 0; static int host_num = 0; @@ -259,6 +271,61 @@ static void usage_exit (const char *name, int status) /* {{{ */ exit (status); } /* }}} void usage_exit */ +static void usage_tos_exit (const char *arg, int status) /* {{{ */ +{ + if (arg != 0) + fprintf (stderr, "Invalid ToS argument: \"%s\"\n\n", arg); + + fprintf (stderr, "Valid ToS arguments (option \"-z\") are:\n" + "\n" + " lowdelay (%#04x) minimize delays\n" + " throughput (%#04x) optimize throughput\n" + " reliability (%#04x) optimize reliability\n" + " mincost (%#04x) minimize cost\n" + " 0x00 - 0xff specify manually\n" + "\n", + (unsigned int) IPTOS_LOWDELAY, + (unsigned int) IPTOS_THROUGHPUT, + (unsigned int) IPTOS_RELIABILITY, + (unsigned int) IPTOS_MINCOST); + + exit (status); +} /* }}} void usage_tos_exit */ + +static int set_opt_send_tos (const char *opt) /* {{{ */ +{ + if (opt == NULL) + return (EINVAL); + + if (strcasecmp ("lowdelay", opt) == 0) + opt_send_tos = IPTOS_LOWDELAY; + else if (strcasecmp ("throughput", opt) == 0) + opt_send_tos = IPTOS_THROUGHPUT; + else if (strcasecmp ("reliability", opt) == 0) + opt_send_tos = IPTOS_RELIABILITY; + else if (strcasecmp ("mincost", opt) == 0) + opt_send_tos = IPTOS_MINCOST; + else if (strcasecmp ("help", opt) == 0) + usage_tos_exit (/* arg = */ NULL, /* status = */ EXIT_SUCCESS); + else + { + unsigned long value; + char *endptr; + + errno = 0; + endptr = NULL; + value = strtoul (opt, &endptr, /* base = */ 0); + if ((errno != 0) || (endptr == opt) + || (endptr == NULL) || (*endptr != 0) + || (value >= 0xff)) + usage_tos_exit (/* arg = */ opt, /* status = */ EXIT_FAILURE); + + opt_send_tos = (uint8_t) value; + } + + return (0); +} /* }}} int set_opt_send_tos */ + static int read_options (int argc, char **argv) /* {{{ */ { int optchar; @@ -333,16 +400,8 @@ static int read_options (int argc, char **argv) /* {{{ */ } 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); + set_opt_send_tos (optarg); break; - } case 'h': usage_exit (argv[0], 0); @@ -631,7 +690,7 @@ static void update_host_hook (pingobj_iter_t *iter, /* {{{ */ double latency; unsigned int sequence; int recv_ttl; - unsigned recv_tos; + uint8_t recv_tos; size_t buffer_len; size_t data_len; ping_context_t *context; @@ -694,8 +753,8 @@ 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 tos=%u " - "time=", + HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i tos=0x%02"PRIx8 + " time=", data_len, context->host, context->addr, sequence, recv_ttl, recv_tos); wattron (main_win, COLOR_PAIR(color)); @@ -706,8 +765,8 @@ static void update_host_hook (pingobj_iter_t *iter, /* {{{ */ else { #endif - HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i tos=%u " - "time=%.2f ms\n", + HOST_PRINTF ("%zu bytes from %s (%s): icmp_seq=%u ttl=%i tos=0x%02"PRIx8 + " time=%.2f ms\n", data_len, context->host, context->addr, sequence, recv_ttl, recv_tos, latency);