From 01b28caf75cfbf0b936af44b1c7c8abbbe4e9a8b Mon Sep 17 00:00:00 2001 From: Dario Lombardo Date: Thu, 7 Jul 2016 23:01:06 +0200 Subject: randpkt_core: change rand() to g_rand_int (CID 1355360). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I48560cbb6007eb16aa545a3e8e6a0e8e3b206930 Reviewed-on: https://code.wireshark.org/review/16326 Petri-Dish: João Valverde Tested-by: Petri Dish Buildbot Petri-Dish: Dario Lombardo Reviewed-by: Alexis La Goutte --- randpkt_core/randpkt_core.c | 85 +++++++++++---------------------------------- randpkt_core/randpkt_core.h | 3 -- 2 files changed, 20 insertions(+), 68 deletions(-) (limited to 'randpkt_core') diff --git a/randpkt_core/randpkt_core.c b/randpkt_core/randpkt_core.c index 95c2ccf83e..b29b18bccc 100644 --- a/randpkt_core/randpkt_core.c +++ b/randpkt_core/randpkt_core.c @@ -32,12 +32,10 @@ #include "wsutil/file_util.h" #include -#ifdef _WIN32 -#include -#endif /* _WIN32 */ - #define array_length(x) (sizeof x / sizeof x[0]) +GRand *pkt_rand = NULL; + /* Types of produceable packets */ enum { PKT_ARP, @@ -572,7 +570,7 @@ void randpkt_loop(randpkt_example* example, guint64 produce_count) /* Produce random packets */ for (i = 0; i < produce_count; i++) { if (example->produce_max_bytes > 0) { - len_random = (rand() % example->produce_max_bytes + 1); + len_random = g_rand_int_range(pkt_rand, 0, example->produce_max_bytes + 1); } else { len_random = 0; @@ -585,16 +583,16 @@ void randpkt_loop(randpkt_example* example, guint64 produce_count) pkthdr->ts.secs = i; /* just for variety */ for (j = example->pseudo_length; j < (int) sizeof(*ps_header); j++) { - ((guint8*)ps_header)[j] = (rand() % 0x100); + ((guint8*)ps_header)[j] = g_rand_int_range(pkt_rand, 0, 0x100); } for (j = example->sample_length; j < len_this_pkt; j++) { /* Add format strings here and there */ - if ((int) (100.0*rand()/(RAND_MAX+1.0)) < 3 && j < (len_random - 3)) { + if ((int) (100.0*g_rand_double(pkt_rand)) < 3 && j < (len_random - 3)) { memcpy(&buffer[j], "%s", 3); j += 2; } else { - buffer[j] = (rand() % 0x100); + buffer[j] = g_rand_int_range(pkt_rand, 0, 0x100); } } @@ -665,19 +663,30 @@ void randpkt_loop(randpkt_example* example, guint64 produce_count) gboolean randpkt_example_close(randpkt_example* example) { int err; + gboolean ok = TRUE; if (!wtap_dump_close(example->dump, &err)) { fprintf(stderr, "Error writing to %s: %s\n", example->filename, wtap_strerror(err)); - return FALSE; + ok = FALSE; } - return TRUE; + + if (pkt_rand != NULL) { + g_rand_free(pkt_rand); + pkt_rand = NULL; + } + + return ok; } void randpkt_example_init(randpkt_example* example, char* produce_filename, int produce_max_bytes) { int err; + if (pkt_rand == NULL) { + pkt_rand = g_rand_new(); + } + wtap_opttypes_initialize(); if (strcmp(produce_filename, "-") == 0) { @@ -706,60 +715,6 @@ void randpkt_example_init(randpkt_example* example, char* produce_filename, int } } -/* Seed the random-number generator */ -void -randpkt_seed(void) -{ - unsigned int randomness; - time_t now; -#ifndef _WIN32 - int fd; - ssize_t ret; - -#define RANDOM_DEV "/dev/urandom" - - /* - * Assume it's at least worth trying /dev/urandom on UN*X. - * If it doesn't exist, fall back on time(). - * - * XXX - Use CryptGenRandom on Windows? - */ - fd = ws_open(RANDOM_DEV, O_RDONLY); - if (fd == -1) { - if (errno != ENOENT) { - fprintf(stderr, - "randpkt: Could not open " RANDOM_DEV " for reading: %s\n", - g_strerror(errno)); - exit(2); - } - goto fallback; - } - - ret = ws_read(fd, &randomness, sizeof randomness); - if (ret == -1) { - fprintf(stderr, - "randpkt: Could not read from " RANDOM_DEV ": %s\n", - g_strerror(errno)); - exit(2); - } - if ((size_t)ret != sizeof randomness) { - fprintf(stderr, - "randpkt: Tried to read %lu bytes from " RANDOM_DEV ", got %ld\n", - (unsigned long)sizeof randomness, (long)ret); - exit(2); - } - srand(randomness); - ws_close(fd); - return; - -fallback: -#endif - now = time(NULL); - randomness = (unsigned int) now; - - srand(randomness); -} - /* Parse command-line option "type" and return enum type */ int randpkt_parse_type(char *string) { @@ -768,7 +723,7 @@ int randpkt_parse_type(char *string) /* Called with NULL, choose a random packet */ if (!string) { - return examples[rand() % num_entries].produceable_type; + return examples[g_rand_int_range(pkt_rand, 0, num_entries)].produceable_type; } for (i = 0; i < num_entries; i++) { diff --git a/randpkt_core/randpkt_core.h b/randpkt_core/randpkt_core.h index b45554f5c1..11bc58a0ce 100644 --- a/randpkt_core/randpkt_core.h +++ b/randpkt_core/randpkt_core.h @@ -50,9 +50,6 @@ guint randpkt_example_count(void); /* Return the list of the active examples */ void randpkt_example_list(char*** abbrev_list, char*** longname_list); -/* Seed the random-number generator */ -void randpkt_seed(void); - /* Parse command-line option "type" and return enum type */ int randpkt_parse_type(char *string); -- cgit v1.2.3