开源工具iperf代码C/C++
时间: 2023-12-20 15:04:28 浏览: 447
以下是iperf3的C代码,可以在GitHub上找到完整的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "iperf.h"
#include "iperf_api.h"
#include "iperf_locale.h"
#include "units.h"
#include "net.h"
#include "timer.h"
#include "tcp_window_size.h"
#include "cjson.h"
#ifdef HAVE_SSL
#include <openssl/ssl.h>
#include <openssl/err.h>
#endif
/* Forwards. */
static int iperf_run(struct iperf_test *test);
/**************************************************************************/
int
main(int argc, char **argv)
{
int i;
struct iperf_test *test;
int result;
struct sigaction sa;
char client_version[10] = IPERF_VERSION;
setlocale(LC_ALL, "");
bindtextdomain("iperf3", LOCALEDIR);
textdomain("iperf3");
/* Initialize settings. */
test = iperf_new_test();
/* Register a few signal handlers to make clean exit more likely. */
memset(&sa, 0, sizeof(sa));
sa.sa_handler = &iperf_got_sigint;
sigaction(SIGINT, &sa, NULL);
sa.sa_handler = &iperf_got_sigpipe;
sigaction(SIGPIPE, &sa, NULL);
/* Parse command line options, handling some options immediately. */
i = iperf_parse_arguments(test, argc, argv);
if (i < 0) {
fprintf(stderr, "%s", iperf_strerror(i));
exit(1);
}
if (test->version) {
printf("%s\n", client_version);
exit(0);
} else if (test->help) {
iperf_printf(test, "\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n",
"Usage: iperf3 [-options] [-s|-c host] [options]\n",
"Client/Server:",
" -V, --version show version information and quit.",
" -h, --help show this message and quit.",
" -i, --interval n seconds between periodic bandwidth reports.",
" -f, --format [kmKM] format to report: Kbits, Mbits, KBytes, MBytes",
" -P, --parallel n number of parallel client streams to run.");
iperf_printf(test, "\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n",
"Client specific:",
" -c, --client <host> run in client mode, connecting to <host>.",
" -u, --udp use UDP rather than TCP.",
" -b, --bitrate #[KMG][/#] target bitrate in bits/sec (0 for unlimited)",
" (default 1 Mbit/sec for UDP, unlimited for TCP)",
" -t, --time n time in seconds to transmit for (default 10 secs)",
" -n, --bytes n number of bytes to transmit (instead of -t)",
" -k, --blockcount #[KMG] number of blocks (packets) to transmit (instead of -t or -n)",
" -l, --length #[KMG] length of buffer to read or write",
" (default 128 KB for TCP, dynamic or 8 KB for UDP)",
" -R, --reverse reverse the direction of a test (client sends, server receives).");
iperf_printf(test, "\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n",
"Server specific:",
" -s, --server run in server mode.",
" -D, --daemon run the server as a daemon",
" -I, --pidfile file write PID file (default /var/run/iperf3.pid)",
" -1, --one-off handle one client connection then exit.",
" -B, --bind <host> bind to a specific interface, e.g. eth0");
iperf_printf(test, "\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n",
"JSON options:",
" -J, --json output in JSON format",
" --logfile f send output to a log file",
"",
"For more information and tuning options, see iperf3's man page.",
"",
"Please report bugs to https://github.com/esnet/iperf",
"");
exit(0);
}
/* Check for version number consistency. */
if (test->protocol->id == Ptcp && test->version == 3 && test->tcp.omit_version) {
i = 2;
} else {
i = 3;
}
if (strncmp(client_version, test->version_string, i) != 0) {
iperf_printf(test, "warning: version number mismatch (client %s, server %s)\n",
client_version, test->version_string);
}
/* Initialize IP protocol */
if (test->cookiefile) {
test->cookie = iperf_load_cookie(test->cookiefile);
if (test->cookie == NULL) {
iperf_errexit(test, "unable to load cookie from file '%s'", test->cookiefile);
}
}
if (test->reverse)
test->settings->reverse = 1;
if (test->protocol->id == Pudp) {
if (test->settings->mss || test->settings->socket_bufsize) {
iperf_printf(test, "warning: MSS and socket buffer size settings are not used in UDP mode.\n");
}
if (test->settings->no_delay) {
iperf_printf(test, "warning: the TCP_NODELAY option is not used in UDP mode.\n");
}
if (test->settings->pmtu != -1) {
iperf_printf(test, "warning: the PMTU option is not used in UDP mode.\n");
}
}
#ifdef HAVE_SSL
/* Initialize SSL library */
if (test->protocol->id == Ptcp && test->settings->ssl) {
SSL_load_error_strings();
SSL_library_init();
test->sslctx = SSL_CTX_new(TLS_client_method());
if (test->sslctx == NULL) {
iperf_errexit(test, "failed to create SSL context\n");
}
if (test->settings->ssl_cafile) {
if (SSL_CTX_load_verify_locations(test->sslctx, test->settings->ssl_cafile, NULL) != 1) {
SSL_CTX_free(test->sslctx);
iperf_errexit(test, "failed to load CA certificates from %s\n", test->settings->ssl_cafile);
}
}
if (test->settings->ssl_cert) {
if (SSL_CTX_use_certificate_chain_file(test->sslctx, test->settings->ssl_cert) != 1) {
SSL_CTX_free(test->sslctx);
iperf_errexit(test, "failed to load SSL certificate from %s\n", test->settings->ssl_cert);
}
}
if (test->settings->ssl_key) {
if (SSL_CTX_use_PrivateKey_file(test->sslctx, test->settings->ssl_key, SSL_FILETYPE_PEM) != 1) {
SSL_CTX_free(test->sslctx);
iperf_errexit(test, "failed to load SSL key from %s\n", test->settings->ssl_key);
}
}
SSL_CTX_set_verify(test->sslctx, SSL_VERIFY_PEER, NULL);
}
#endif
/* Daemon mode. */
if (test->daemon) {
if (daemon(0, 0) != 0) {
iperf_errexit(test, "error - failed to become a daemon: %s\n", strerror(errno));
}
if (test->pidfile) {
FILE *f;
f = fopen(test->pidfile, "w");
if (f == NULL) {
iperf_errexit(test, "error - unable to write PID file '%s': %s\n", test->pidfile, strerror(errno));
}
fprintf(f, "%d\n", getpid());
fclose(f);
}
}
/* Ignore SIGPIPE to simplify error handling */
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, NULL);
/* If we are doing a single client (-1) and it is a daemon, don't do -D again */
if (test->daemon && test->num_ostreams == 1)
test->daemon = 0;
/* Defer daemon mode until after the above check for single client + daemon */
if (test->daemon) {
if (daemon(0, 0) != 0) {
iperf_errexit(test, "error - failed to become a daemon: %s\n", strerror(errno));
}
if (test->pidfile) {
FILE *f;
f = fopen(test->pidfile, "w");
if (f == NULL) {
iperf_errexit(test, "error - unable to write PID file '%s': %s\n", test->pidfile, strerror(errno));
}
fprintf(f, "%d\n", getpid());
fclose(f);
}
}
/* Ignore SIGPIPE to simplify error handling */
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, NULL);
/* Set up the output stream */
if (test->json_output) {
test->outfile = json_get_output_stream(test->json_output_file);
} else if (test->logfile) {
test->outfile = fopen(test->logfile, "w");
if (test->outfile == NULL) {
iperf_errexit(test, "error - unable to write log to file '%s': %s\n", test->logfile, strerror(errno));
}
} else {
test->outfile = stdout;
}
/* Start the client or server */
if (test->server) {
if (test->daemon) {
iperf_printf(test, "Server listening on port %d\n", test->server_port);
}
else {
iperf_printf(test, "-----------------------------------------------------------\n");
iperf_printf(test, "Server listening on %s port %d\n", test->settings->domain == AF_INET6 ? "[::]" : "0.0.0.0", test->server_port);
iperf_printf(test, "-----------------------------------------------------------\n");
}
result = iperf_run_server(test);
} else {
if (test->daemon) {
iperf_printf(test, "Client connecting to %s, TCP port %d\n", test->server_hostname, test->server_port);
}
else {
if (test->reverse)
iperf_printf(test, "-----------------------------------------------------------\n");
iperf_printf(test, "Client connecting to %s, %s port %d\n", test->server_hostname, test->protocol->name, test->server_port);
if (test->reverse)
iperf_printf(test, "-----------------------------------------------------------\n");
}
result = iperf_run_client(test);
}
if (test->cookie)
iperf_delete_cookie(test->cookie);
#ifdef HAVE_SSL
if (test->protocol->id == Ptcp && test->settings->ssl) {
SSL_CTX_free(test->sslctx);
}
ERR_free_strings();
#endif
iperf_free_test(test);
return result;
}
static int
iperf_run(struct iperf_test *test)
{
test->start_time = milliseconds();
test->next_time = test->start_time;
test->bytes_sent = 0;
test->blocks_sent = 0;
test->retransmits = 0;
if (test->server_hostname) {
test->server_hostname_len = strlen(test->server_hostname);
}
if (test->bind_address) {
test->bind_address_len = strlen(test->bind_address);
}
if (test->json_output) {
cJSON *json_output = cJSON_CreateObject();
if (json_output == NULL) {
iperf_errexit(test, "error - cJSON_CreateObject failed: %s\n", strerror(errno));
}
cJSON_AddItemToObject(json_output, "start", cJSON_CreateNumber((double) test->start_time / 1000));
if (test->verbose) {
cJSON_AddItemToObject(json_output, "verbose", cJSON_CreateNumber(1));
}
cJSON_AddItemToObject(json_output, "system_info", cJSON_CreateObject());
iperf_json_printf(json_output, "version", "%s", test->version);
iperf_json_printf(json_output, "system_info", "%s", get_system_info());
if (test->title) {
iperf_json_printf(json_output, "title", "%s", test->title);
}
if (test->extra_data) {
cJSON_AddItemToObject(json_output, "extra_data", cJSON_Parse(test->extra_data));
}
test->json_start_time = milliseconds();
test->json_output_string = cJSON_Print(json_output);
cJSON_Delete(json_output);
if (test->json_output_string == NULL) {
iperf_errexit(test, "error - cJSON_Print failed: %s\n", strerror(errno));
}
}
if (test->protocol->id == Ptcp) {
if (test->settings->socket_bufsize) {
if (iperf_set_tcp_windowsize(test) != 0) {
iperf_errexit(test, "error - %s\n", strerror(errno));
}
}
}
if (test->reverse) {
if (test->protocol->id == Ptcp) {
if (iperf_create_streams(test, test->reverse) < 0) {
iperf_errexit(test, "error - %s\n", strerror(errno));
}
}
if (iperf_connect(test, test->reverse) < 0) {
iperf_errexit(test, "error - %s\n", strerror(errno));
}
} else {
if (iperf_create_streams(test, test->reverse) < 0) {
iperf_errexit(test, "error - %s\n", strerror(errno));
}
if (iperf_connect(test, test->reverse) < 0) {
iperf_errexit(test, "error - %s\n", strerror(errno));
}
}
if (test->json_output) {
cJSON *json_output = cJSON_CreateObject();
if (json_output == NULL) {
iperf_errexit(test, "error - cJSON_CreateObject failed: %s\n", strerror(errno));
}
cJSON_AddItemToObject(json_output, "start", cJSON_CreateNumber((double) test->start_time / 1000));
if (test->verbose) {
cJSON_AddItemToObject(json_output, "verbose", cJSON_CreateNumber(1));
}
iperf_json_printf(json_output, "interval", "%d", test->settings->stats_interval);
if (test->title) {
iperf_json_printf(json_output, "title", "%s", test->title);
}
if (test->extra_data) {
cJSON_AddItemToObject(json_output, "extra_data", cJSON_Parse(test->extra_data));
}
iperf_json_printf(json_output, "start_connected", "%d", test->connected);
cJSON_AddItemToObject(json_output, "intervals", cJSON_CreateArray());
test->json_output_string = cJSON_Print(json_output);
cJSON_Delete(json_output);
if (test->json_output_string == NULL) {
iperf_errexit(test, "error - cJSON_Print failed: %s\n", strerror(errno));
}
}
if (test->protocol->id == Pudp) {
if (iperf_udp_connect(test) < 0) {
iperf_errexit(test, "error - %s\n", strerror(errno));
}
}
if (test->reverse) {
if (test->protocol->id == Ptcp) {
if (iperf_listen(test) < 0) {
iperf_errexit(test, "error - %s\n", strerror(errno));
}
}
if (test->protocol->id == Pudp || test->protocol->id == Psctp) {
if (iperf_run_server_udp(test) < 0) {
iperf_errexit(test, "error - %s\n", strerror(errno));
}
} else {
if (iperf_run_server_tcp(test) < 0) {
iperf_errexit(test, "error - %s\n", strerror(errno));
}
}
} else {
if (test->protocol->id == Ptcp) {
if (iperf_run_client_tcp(test) < 0) {
iperf_errexit(test, "error - %s\n", strerror(errno));
}
} else {
if (iperf_run_client_udp(test) < 0) {
iperf_errexit(test, "error - %s\n", strerror(errno));
}
}
}
if (test->json_output) {
cJSON *json_output = cJSON_CreateObject();
if (json_output == NULL) {
iperf_errexit(test, "error - cJSON_CreateObject failed: %s\n", strerror(errno));
}
cJSON_AddItemToObject(json_output, "start", cJSON_CreateNumber((double) test->start_time / 1000));
if (test->verbose) {
cJSON_AddItemToObject(json_output, "verbose", cJSON_CreateNumber(1));
}
iperf_json_printf(json_output, "interval", "%d", test->settings->stats_interval);
if (test->title) {
iperf_json_printf(json_output, "title", "%s", test->title);
}
if (test
阅读全文
相关推荐


















