]> git.ipfire.org Git - fireperf.git/commitdiff
random: Move pool into extra file
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 18 Feb 2021 15:39:14 +0000 (15:39 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 18 Feb 2021 15:39:42 +0000 (15:39 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/client.c
src/random.c [new file with mode: 0644]
src/random.h [new file with mode: 0644]

index 9503f4ec5b8bbce8dfabf92384ea5cf93afbaa29..8d605c49c74674619e71d5b08a2a9659c6abf876 100644 (file)
@@ -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 \
index 53c93daff8df0055293b2bb278d4b23e29be6a62..f848f35a74fe7b0bcc34657f7615ad0d8ee0bd98 100644 (file)
 #include <signal.h>
 #include <string.h>
 #include <sys/epoll.h>
-#include <sys/random.h>
 #include <unistd.h>
 
 #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 (file)
index 0000000..b039414
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <stdlib.h>
+#include <sys/random.h>
+
+#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 (file)
index 0000000..4e83ff9
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#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 */