From: Michael Tremer Date: Thu, 18 Feb 2021 15:39:14 +0000 (+0000) Subject: random: Move pool into extra file X-Git-Tag: 0.2.0~18 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=eb47fe084de098adfc5b44e62930aa0d764a92a9;p=fireperf.git random: Move pool into extra file Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 9503f4e..8d605c4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -66,6 +66,8 @@ fireperf_SOURCES = \ src/logging.h \ src/main.c \ src/main.h \ + src/random.c \ + src/random.h \ src/server.c \ src/server.h \ src/util.c \ diff --git a/src/client.c b/src/client.c index 53c93da..f848f35 100644 --- a/src/client.c +++ b/src/client.c @@ -24,12 +24,12 @@ #include #include #include -#include #include #include "client.h" #include "logging.h" #include "main.h" +#include "random.h" #include "util.h" // Set to one when the timeout has expired @@ -44,58 +44,6 @@ static void handle_SIGALRM(int signal) { } } -const char ZERO[SOCKET_SEND_BUFFER_SIZE] = { 0 }; - -struct fireperf_random_pool { - char* data; - size_t size; -}; - -static void fireperf_random_pool_free(struct fireperf_random_pool* pool) { - if (pool->data) - free(pool->data); - - free(pool); -} - -static struct fireperf_random_pool* fireperf_random_pool_create(struct fireperf_config* conf, size_t size) { - struct fireperf_random_pool* pool = calloc(1, sizeof(*pool)); - if (!pool) - return NULL; - - pool->size = size; - - // Allocate the data array - pool->data = malloc(pool->size); - if (!pool->data) - goto ERROR; - - size_t offset = 0; - while (offset < pool->size) { - offset += getrandom(pool->data + offset, pool->size - offset, 0); - } - - DEBUG(conf, "Allocated random pool of %zu bytes(s)\n", pool->size); - - return pool; - -ERROR: - fireperf_random_pool_free(pool); - - return NULL; -} - -static const char* fireperf_random_pool_get_slice(struct fireperf_random_pool* pool, size_t size) { - if (size > pool->size) - return NULL; - - // Find a random value between the start and end of - // the data region that is at least size bytes long. - off_t offset = random() % (pool->size - size); - - return pool->data + offset; -} - static int open_connection(struct fireperf_config* conf) { // Open a new socket int fd = socket(AF_INET6, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0); diff --git a/src/random.c b/src/random.c new file mode 100644 index 0000000..b039414 --- /dev/null +++ b/src/random.c @@ -0,0 +1,73 @@ +/*############################################################################# +# # +# fireperf - A network benchmarking tool # +# Copyright (C) 2021 IPFire Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include +#include + +#include "logging.h" +#include "main.h" +#include "random.h" + +const char ZERO[SOCKET_SEND_BUFFER_SIZE] = { 0 }; + +struct fireperf_random_pool* fireperf_random_pool_create(struct fireperf_config* conf, size_t size) { + struct fireperf_random_pool* pool = calloc(1, sizeof(*pool)); + if (!pool) + return NULL; + + pool->size = size; + + // Allocate the data array + pool->data = malloc(pool->size); + if (!pool->data) + goto ERROR; + + size_t offset = 0; + while (offset < pool->size) { + offset += getrandom(pool->data + offset, pool->size - offset, 0); + } + + DEBUG(conf, "Allocated random pool of %zu bytes(s)\n", pool->size); + + return pool; + +ERROR: + fireperf_random_pool_free(pool); + + return NULL; +} + +void fireperf_random_pool_free(struct fireperf_random_pool* pool) { + if (pool->data) + free(pool->data); + + free(pool); +} + +const char* fireperf_random_pool_get_slice(struct fireperf_random_pool* pool, size_t size) { + if (size > pool->size) + return NULL; + + // Find a random value between the start and end of + // the data region that is at least size bytes long. + off_t offset = random() % (pool->size - size); + + return pool->data + offset; +} diff --git a/src/random.h b/src/random.h new file mode 100644 index 0000000..4e83ff9 --- /dev/null +++ b/src/random.h @@ -0,0 +1,37 @@ +/*############################################################################# +# # +# fireperf - A network benchmarking tool # +# Copyright (C) 2021 IPFire Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#ifndef FIREPERF_RANDOM_H +#define FIREPERF_RANDOM_H + +struct fireperf_random_pool { + char* data; + size_t size; +}; + +const char ZERO[SOCKET_SEND_BUFFER_SIZE]; + +struct fireperf_random_pool* fireperf_random_pool_create( + struct fireperf_config* conf, size_t size); +void fireperf_random_pool_free(struct fireperf_random_pool* pool); + +const char* fireperf_random_pool_get_slice(struct fireperf_random_pool* pool, size_t size); + +#endif /* FIREPERF_RANDOM_H */