From: Michael Tremer Date: Thu, 28 Jan 2021 11:13:18 +0000 (+0000) Subject: client: Add some example for gathering random data X-Git-Tag: 0.1.0~46 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=43021b31005abe90cf69cf5b3ed90053bf08adf2;p=fireperf.git client: Add some example for gathering random data Signed-off-by: Michael Tremer --- diff --git a/src/client.c b/src/client.c index f1bc97f..cc9fd44 100644 --- a/src/client.c +++ b/src/client.c @@ -20,8 +20,9 @@ #include #include -#include #include +#include +#include #include #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 {