From 956d115851755390bb59ad93a6d501be444b9f73 Mon Sep 17 00:00:00 2001 From: "Bruce A. Mah" Date: Wed, 18 Nov 2015 09:40:24 -0800 Subject: [PATCH] Improve command error handling. Exit with non-zero exit code if server mode has too many errors. Properly detect complain about non-numeric arguments to -A, -L, and -S. Implement range checks for argument to -S. Fixes #316. --- src/iperf_api.c | 19 ++++++++++++++----- src/iperf_api.h | 1 + src/iperf_error.c | 5 ++++- src/main.c | 5 ++--- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/iperf_api.c b/src/iperf_api.c index f200752..4813c20 100755 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -662,6 +662,7 @@ iperf_parse_arguments(struct iperf_test *test, int argc, char **argv) int flag; int blksize; int server_flag, client_flag, rate_flag, duration_flag; + char *endptr; #if defined(HAVE_CPU_AFFINITY) char* comma; #endif /* HAVE_CPU_AFFINITY */ @@ -825,13 +826,20 @@ iperf_parse_arguments(struct iperf_test *test, int argc, char **argv) test->settings->domain = AF_INET6; break; case 'S': - test->settings->tos = strtol(optarg, NULL, 0); + test->settings->tos = strtol(optarg, &endptr, 0); + if (endptr == optarg || + test->settings->tos < 0 || + test->settings->tos > 255) { + i_errno = IEBADTOS; + return -1; + } client_flag = 1; break; case 'L': #if defined(HAVE_FLOWLABEL) - test->settings->flowlabel = strtol(optarg, NULL, 0); - if (test->settings->flowlabel < 1 || test->settings->flowlabel > 0xfffff) { + test->settings->flowlabel = strtol(optarg, &endptr, 0); + if (endptr == optarg || + test->settings->flowlabel < 1 || test->settings->flowlabel > 0xfffff) { i_errno = IESETFLOW; return -1; } @@ -876,8 +884,9 @@ iperf_parse_arguments(struct iperf_test *test, int argc, char **argv) break; case 'A': #if defined(HAVE_CPU_AFFINITY) - test->affinity = atoi(optarg); - if (test->affinity < 0 || test->affinity > 1024) { + test->affinity = strtol(optarg, &endptr, 0); + if (endptr == optarg || + test->affinity < 0 || test->affinity > 1024) { i_errno = IEAFFINITY; return -1; } diff --git a/src/iperf_api.h b/src/iperf_api.h index 525f862..d16cb8a 100755 --- a/src/iperf_api.h +++ b/src/iperf_api.h @@ -289,6 +289,7 @@ enum { IENOSCTP = 18, // No SCTP support available IEBIND = 19, // Local port specified with no local bind option IEUDPBLOCKSIZE = 20, // Block size too large. Maximum value = %dMAX_UDP_BLOCKSIZE + IEBADTOS = 21, // Bad TOS value /* Test errors */ IENEWTEST = 100, // Unable to create a new test (check perror) IEINITTEST = 101, // Test initialization failed (check perror) diff --git a/src/iperf_error.c b/src/iperf_error.c index 367ff3e..9201cb7 100644 --- a/src/iperf_error.c +++ b/src/iperf_error.c @@ -1,5 +1,5 @@ /* - * iperf, Copyright (c) 2014, The Regents of the University of + * iperf, Copyright (c) 2014, 2015, The Regents of the University of * California, through Lawrence Berkeley National Laboratory (subject * to receipt of any required approvals from the U.S. Dept. of * Energy). All rights reserved. @@ -127,6 +127,9 @@ iperf_strerror(int i_errno) case IEUDPBLOCKSIZE: snprintf(errstr, len, "block size too large (maximum = %d bytes)", MAX_UDP_BLOCKSIZE); break; + case IEBADTOS: + snprintf(errstr, len, "bad TOS value (must be between 0 and 255 inclusive)"); + break; case IEMSS: snprintf(errstr, len, "TCP MSS too large (maximum = %d bytes)", MAX_MSS); break; diff --git a/src/main.c b/src/main.c index 1870f92..4bd5a32 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,5 @@ /* - * iperf, Copyright (c) 2014, The Regents of the University of + * iperf, Copyright (c) 2014, 2015, The Regents of the University of * California, through Lawrence Berkeley National Laboratory (subject * to receipt of any required approvals from the U.S. Dept. of * Energy). All rights reserved. @@ -153,10 +153,9 @@ run(struct iperf_test *test) for (;;) { if (iperf_run_server(test) < 0) { iperf_err(test, "error - %s", iperf_strerror(i_errno)); - fprintf(stderr, "\n"); ++consecutive_errors; if (consecutive_errors >= 5) { - fprintf(stderr, "too many errors, exiting\n"); + iperf_errexit(test, "too many errors, exiting"); break; } } else