]> git.ipfire.org Git - fireperf.git/commitdiff
client: Add some example for gathering random data
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 28 Jan 2021 11:13:18 +0000 (11:13 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 28 Jan 2021 11:13:18 +0000 (11:13 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/client.c

index f1bc97fe840d5959b774547baefdcb4a14ab734c..cc9fd4470a29cfe730d2274925c1bdda2c154435 100644 (file)
@@ -20,8 +20,9 @@
 
 #include <errno.h>
 #include <signal.h>
-#include <sys/epoll.h>
 #include <string.h>
+#include <sys/epoll.h>
+#include <sys/random.h>
 #include <unistd.h>
 
 #include "client.h"
@@ -60,8 +61,22 @@ static int connect_socket(struct fireperf_config* conf, int fd) {
        return 0;
 }
 
-static void randomize_buffer(char* buffer, size_t s) {
-       return; // TODO
+/*
+       This is just an example implementation because getrandom() is too slow for
+       this purpose. I would need a really fast pseudo-RNG.
+*/
+static int randomize_buffer(char* buffer, size_t s) {
+       ssize_t r = getrandom(buffer, s, 0);
+
+       // Error
+       if (r < 0)
+               return 1;
+
+       // We received less data than we wanted
+       else if (r > 0 && (size_t)r < s)
+               return 1;
+
+       return 0;
 }
 
 static int send_data_to_server(struct fireperf_config* conf, int fd) {
@@ -72,7 +87,12 @@ static int send_data_to_server(struct fireperf_config* conf, int fd) {
 
        // Randomize the buffer if requested, otherwise just send an empty buffer
        if (!conf->zero) {
-               randomize_buffer(buffer, sizeof(buffer));
+               int r = randomize_buffer(buffer, sizeof(buffer));
+               if (r) {
+                       ERROR(conf, "Could not generate a random block of data: %s\n",
+                               strerror(errno));
+                       return 1;
+               }
        }
 
        do {