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
This commit is contained in:
Kevin Easton
2013-08-17 14:47:04 +00:00
parent d194069da9
commit 5582171286
4 changed files with 38 additions and 23 deletions

View File

@@ -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)

View File

@@ -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 *);

View File

@@ -1348,12 +1348,30 @@ BUILT_IN_FUNCTION(function_mid, word)
BUILT_IN_FUNCTION(function_rand, word)
{
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;
}

View File

@@ -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)
{
switch (get_int_var(RANDOM_SOURCE_VAR))
{
case 0:
default:
/* Always use the strongest random source for internal client use. */
return randd(l);
case 1:
return randm(l);
case 2:
return randt(l);
}
}
/*