diff options
Diffstat (limited to 'src/iperf_api.c')
-rw-r--r--[-rwxr-xr-x] | src/iperf_api.c | 107 |
1 files changed, 77 insertions, 30 deletions
diff --git a/src/iperf_api.c b/src/iperf_api.c index e1bbfa5..ed643b7 100755..100644 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -1,5 +1,5 @@ /* - * iperf, Copyright (c) 2014-2019, The Regents of the University of + * iperf, Copyright (c) 2014-2020, 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. @@ -63,6 +63,10 @@ #include <sys/cpuset.h> #endif /* HAVE_CPUSET_SETAFFINITY */ +#if defined(__CYGWIN__) || defined(_WIN32) || defined(_WIN64) || defined(__WINDOWS__) +#define CPU_SETSIZE __CPU_SETSIZE +#endif /* __CYGWIN__, _WIN32, _WIN64, __WINDOWS__ */ + #if defined(HAVE_SETPROCESSAFFINITYMASK) #include <Windows.h> #endif /* HAVE_SETPROCESSAFFINITYMASK */ @@ -340,6 +344,12 @@ iperf_get_test_no_delay(struct iperf_test *ipt) return ipt->no_delay; } +int +iperf_get_test_connect_timeout(struct iperf_test *ipt) +{ + return ipt->settings->connect_timeout; +} + /************** Setter routines for some fields inside iperf_test *************/ void @@ -470,7 +480,9 @@ iperf_set_test_role(struct iperf_test *ipt, char role) { ipt->role = role; if (!ipt->reverse) { - if (role == 'c') + if (ipt->bidirectional) + ipt->mode = BIDIRECTIONAL; + else if (role == 'c') ipt->mode = SENDER; else if (role == 's') ipt->mode = RECEIVER; @@ -547,13 +559,13 @@ iperf_set_test_unit_format(struct iperf_test *ipt, char unit_format) void iperf_set_test_client_username(struct iperf_test *ipt, char *client_username) { - ipt->settings->client_username = client_username; + ipt->settings->client_username = strdup(client_username); } void iperf_set_test_client_password(struct iperf_test *ipt, char *client_password) { - ipt->settings->client_password = client_password; + ipt->settings->client_password = strdup(client_password); } void @@ -561,6 +573,18 @@ iperf_set_test_client_rsa_pubkey(struct iperf_test *ipt, char *client_rsa_pubkey { ipt->settings->client_rsa_pubkey = load_pubkey_from_base64(client_rsa_pubkey_base64); } + +void +iperf_set_test_server_authorized_users(struct iperf_test *ipt, char *server_authorized_users) +{ + ipt->server_authorized_users = strdup(server_authorized_users); +} + +void +iperf_set_test_server_rsa_privkey(struct iperf_test *ipt, char *server_rsa_privkey_base64) +{ + ipt->server_rsa_private_key = load_privkey_from_base64(server_rsa_privkey_base64); +} #endif // HAVE_SSL void @@ -590,7 +614,7 @@ iperf_set_test_tos(struct iperf_test *ipt, int tos) void iperf_set_test_extra_data(struct iperf_test *ipt, char *dat) { - ipt->extra_data = dat; + ipt->extra_data = strdup(dat); } void @@ -609,6 +633,13 @@ iperf_set_test_no_delay(struct iperf_test* ipt, int no_delay) ipt->no_delay = no_delay; } +void +iperf_set_test_connect_timeout(struct iperf_test* ipt, int ct) +{ + ipt->settings->connect_timeout = ct; +} + + /********************** Get/set test protocol structure ***********************/ struct protocol * @@ -755,7 +786,7 @@ iperf_on_connect(struct iperf_test *test) } } if (test->settings->rate) - iperf_printf(test, " Target Bitrate: %llu\n", test->settings->rate); + iperf_printf(test, " Target Bitrate: %"PRIu64"\n", test->settings->rate); } } @@ -1263,12 +1294,6 @@ iperf_parse_arguments(struct iperf_test *test, int argc, char **argv) else if (iperf_getpass(&client_password, &s, stdin) < 0){ return -1; } - - if (strlen(client_username) > 20 || strlen(client_password) > 20){ - i_errno = IESETCLIENTAUTH; - return -1; - } - if (test_load_pubkey_from_file(client_rsa_public_key) < 0){ i_errno = IESETCLIENTAUTH; return -1; @@ -1397,7 +1422,7 @@ iperf_check_throttle(struct iperf_stream *sp, struct iperf_time *nowP) double seconds; uint64_t bits_per_second; - if (sp->test->done) + if (sp->test->done || sp->test->settings->rate == 0 || sp->test->settings->burst != 0) return; iperf_time_diff(&sp->result->start_time_fixed, nowP, &temp_time); seconds = iperf_time_in_secs(&temp_time); @@ -1442,8 +1467,7 @@ iperf_send(struct iperf_test *test, fd_set *write_setP) streams_active = 1; test->bytes_sent += r; ++test->blocks_sent; - if (test->settings->rate != 0 && test->settings->burst == 0) - iperf_check_throttle(sp, &now); + iperf_check_throttle(sp, &now); if (multisend > 1 && test->settings->bytes != 0 && test->bytes_sent >= test->settings->bytes) break; if (multisend > 1 && test->settings->blocks != 0 && test->blocks_sent >= test->settings->blocks) @@ -1559,15 +1583,18 @@ int test_is_authorized(struct iperf_test *test){ if (test->settings->authtoken){ char *username = NULL, *password = NULL; time_t ts; - decode_auth_setting(test->debug, test->settings->authtoken, test->server_rsa_private_key, &username, &password, &ts); + int rc = decode_auth_setting(test->debug, test->settings->authtoken, test->server_rsa_private_key, &username, &password, &ts); + if (rc) { + return -1; + } int ret = check_authentication(username, password, ts, test->server_authorized_users); if (ret == 0){ - iperf_printf(test, report_authetication_successed, username, ts); + iperf_printf(test, report_authentication_succeeded, username, ts); free(username); free(password); return 0; } else { - iperf_printf(test, report_authetication_failed, username, ts); + iperf_printf(test, report_authentication_failed, username, ts); free(username); free(password); return -1; @@ -1730,15 +1757,25 @@ send_parameters(struct iperf_test *test) if (test->repeating_payload) cJSON_AddNumberToObject(j, "repeating_payload", test->repeating_payload); #if defined(HAVE_SSL) - if (test->settings->client_username && test->settings->client_password && test->settings->client_rsa_pubkey){ - encode_auth_setting(test->settings->client_username, test->settings->client_password, test->settings->client_rsa_pubkey, &test->settings->authtoken); - cJSON_AddStringToObject(j, "authtoken", test->settings->authtoken); - } + /* Send authentication parameters */ + if (test->settings->client_username && test->settings->client_password && test->settings->client_rsa_pubkey){ + int rc = encode_auth_setting(test->settings->client_username, test->settings->client_password, test->settings->client_rsa_pubkey, &test->settings->authtoken); + + if (rc) { + cJSON_Delete(j); + i_errno = IESENDPARAMS; + return -1; + } + + cJSON_AddStringToObject(j, "authtoken", test->settings->authtoken); + } #endif // HAVE_SSL cJSON_AddStringToObject(j, "client_version", IPERF_VERSION); if (test->debug) { - printf("send_parameters:\n%s\n", cJSON_Print(j)); + char *str = cJSON_Print(j); + printf("send_parameters:\n%s\n", str); + cJSON_free(str); } if (JSON_write(test->ctrl_sck, j) < 0) { @@ -1768,7 +1805,7 @@ get_parameters(struct iperf_test *test) char *str; str = cJSON_Print(j); printf("get_parameters:\n%s\n", str ); - free(str); + cJSON_free(str); } if ((j_p = cJSON_GetObjectItem(j, "tcp")) != NULL) @@ -1833,8 +1870,8 @@ get_parameters(struct iperf_test *test) #endif //HAVE_SSL if (test->mode && test->protocol->id == Ptcp && has_tcpinfo_retransmits()) test->sender_has_retransmits = 1; - if (test->settings->rate) - cJSON_AddNumberToObject(test->json_start, "target_bitrate", test->settings->rate); + if (test->settings->rate) + cJSON_AddNumberToObject(test->json_start, "target_bitrate", test->settings->rate); cJSON_Delete(j); } return r; @@ -1935,7 +1972,7 @@ send_results(struct iperf_test *test) if (r == 0 && test->debug) { char *str = cJSON_Print(j); printf("send_results\n%s\n", str); - free(str); + cJSON_free(str); } if (r == 0 && JSON_write(test->ctrl_sck, j) < 0) { i_errno = IESENDRESULTS; @@ -1993,7 +2030,7 @@ get_results(struct iperf_test *test) if (test->debug) { char *str = cJSON_Print(j); printf("get_results\n%s\n", str); - free(str); + cJSON_free(str); } test->remote_cpu_util[0] = j_cpu_util_total->valuedouble; @@ -2130,7 +2167,7 @@ JSON_write(int fd, cJSON *json) if (Nwrite(fd, str, hsize, Ptcp) < 0) r = -1; } - free(str); + cJSON_free(str); } return r; } @@ -3417,7 +3454,9 @@ iperf_print_results(struct iperf_test *test) /* Print server output if we're on the client and it was requested/provided */ if (test->role == 'c' && iperf_get_test_get_server_output(test) && !test->json_output) { if (test->json_server_output) { - iperf_printf(test, "\nServer JSON output:\n%s\n", cJSON_Print(test->json_server_output)); + char *str = cJSON_Print(test->json_server_output); + iperf_printf(test, "\nServer JSON output:\n%s\n", str); + cJSON_free(str); cJSON_Delete(test->json_server_output); test->json_server_output = NULL; } @@ -3849,9 +3888,15 @@ diskfile_recv(struct iperf_stream *sp) void iperf_catch_sigend(void (*handler)(int)) { +#ifdef SIGINT signal(SIGINT, handler); +#endif +#ifdef SIGTERM signal(SIGTERM, handler); +#endif +#ifdef SIGHUP signal(SIGHUP, handler); +#endif } /** @@ -3995,6 +4040,8 @@ iperf_json_finish(struct iperf_test *test) return -1; fprintf(test->outfile, "%s\n", test->json_output_string); iflush(test); + cJSON_free(test->json_output_string); + test->json_output_string = NULL; cJSON_Delete(test->json_top); test->json_top = test->json_start = test->json_connected = test->json_intervals = test->json_server_output = test->json_end = NULL; return 0; |