From 5582171286dc69642203307d068e51d45be0834c Mon Sep 17 00:00:00 2001 From: Kevin Easton Date: Sat, 17 Aug 2013 14:47:04 +0000 Subject: [PATCH] Switch random_number() to always use the best entropy source for internal client purposes. The RANDOM_SOURCE setting now only affects the $rand() scripting function. git-svn-id: svn://svn.code.sf.net/p/bitchx/code/trunk@357 13b04d17-f746-0410-82c6-800466cd88b0 --- Changelog | 2 ++ include/ircaux.h | 6 ++++-- source/functions.c | 32 ++++++++++++++++++++++++++------ source/ircaux.c | 21 ++++++--------------- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/Changelog b/Changelog index 15f235e..b09cc03 100644 --- a/Changelog +++ b/Changelog @@ -1,5 +1,7 @@ [Changes 1.2c01] +* RANDOM_SOURCE now only affects the rand() scripting function. (caf) + * Make RANDOM_LOCAL_PORTS actually random. (caf) * Show same message on local terminal as used in emergency exit QUIT. (caf) diff --git a/include/ircaux.h b/include/ircaux.h index 0ce0665..e887577 100644 --- a/include/ircaux.h +++ b/include/ircaux.h @@ -128,8 +128,10 @@ char * BX_strmpcat (char *, size_t, const char *, ...); char * chomp (char *); size_t BX_ccspan (const char *, int); u_char *BX_strcpy_nocolorcodes (u_char *, const u_char *); - -u_long BX_random_number (u_long); +unsigned long randm(unsigned long); +unsigned long randt(unsigned long); +unsigned long randd(unsigned long); +unsigned long BX_random_number(unsigned long); char * get_userhost (void); char * urlencode (const char *); diff --git a/source/functions.c b/source/functions.c index 7cd9703..c7e199a 100644 --- a/source/functions.c +++ b/source/functions.c @@ -1347,13 +1347,31 @@ BUILT_IN_FUNCTION(function_mid, word) */ BUILT_IN_FUNCTION(function_rand, word) { - long tempin; - int result; + long tempin; + unsigned long rand_n; + int result; GET_INT_ARG(tempin, word); - if (tempin == 0) - tempin = (unsigned long) -1; /* This is cheating. :P */ - result = random_number(0L) % tempin; + + switch (get_int_var(RANDOM_SOURCE_VAR)) + { + case 0: + default: + rand_n = randd(0); + break; + case 1: + rand_n = randm(0); + break; + case 2: + rand_n = randt(0); + break; + } + + if (tempin) + result = rand_n % tempin; + else + result = rand_n; + RETURN_INT(result); } @@ -1365,7 +1383,9 @@ BUILT_IN_FUNCTION(function_rand, word) */ BUILT_IN_FUNCTION(function_srand, word) { - random_number((long) now); + /* randd() and randt() do not accept seeding */ + randm((long)now); + RETURN_EMPTY; } diff --git a/source/ircaux.c b/source/ircaux.c index 8056958..3a1c742 100644 --- a/source/ircaux.c +++ b/source/ircaux.c @@ -3132,7 +3132,7 @@ char * get_userhost (void) * to call it once to set the seed. Subsequent calls should use 'l' * as 0, and it will return a value. */ -static unsigned long randm (unsigned long l) +unsigned long randm(unsigned long l) { /* patch from Sarayan to make $rand() better */ static const long RAND_A = 16807L; @@ -3178,7 +3178,7 @@ static unsigned long randt_2 (void) return (unsigned long) tp1.tv_usec; } -static unsigned long randt (unsigned long l) +unsigned long randt(unsigned long l) { #ifdef HAVE_GETTIMEOFDAY unsigned long t1, t2, t; @@ -3203,7 +3203,7 @@ static unsigned long randt (unsigned long l) * substantial unpredictable numbers. At worst, it is mathematical psuedo- * random sequence (which randm() is). */ -static unsigned long randd (unsigned long l) +unsigned long randd(unsigned long l) { unsigned long value; static int random_fd = -1; @@ -3227,19 +3227,10 @@ static int random_fd = -1; return value; } - -unsigned long BX_random_number (unsigned long l) +unsigned long BX_random_number(unsigned long l) { - switch (get_int_var(RANDOM_SOURCE_VAR)) - { - case 0: - default: - return randd(l); - case 1: - return randm(l); - case 2: - return randt(l); - } + /* Always use the strongest random source for internal client use. */ + return randd(l); } /*