From 2fea73808f31f9a5a81d6b014e091f12ff637552 Mon Sep 17 00:00:00 2001 From: Kevin Easton Date: Sat, 17 Aug 2013 15:10:17 +0000 Subject: [PATCH] Improve the initial seeding of the randm() RNG. This is the RNG selected for $rand() when RANDOM_SOURCE is 1. git-svn-id: svn://svn.code.sf.net/p/bitchx/code/trunk@359 13b04d17-f746-0410-82c6-800466cd88b0 --- Changelog | 2 ++ source/ircaux.c | 28 ++++++++++++++++------------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Changelog b/Changelog index cd88955..7fa1821 100644 --- a/Changelog +++ b/Changelog @@ -1,5 +1,7 @@ [Changes 1.2c01] +* Improve the initial seeding of the randm() fallback RNG. (caf) + * Strengthen the cookie generation algorithm for /detach. (caf) * RANDOM_SOURCE now only affects the rand() scripting function. (caf) diff --git a/source/ircaux.c b/source/ircaux.c index 3a1c742..71569d0 100644 --- a/source/ircaux.c +++ b/source/ircaux.c @@ -3135,19 +3135,26 @@ char * get_userhost (void) unsigned long randm(unsigned long l) { /* patch from Sarayan to make $rand() better */ -static const long RAND_A = 16807L; -static const long RAND_M = 2147483647L; -static const long RAND_Q = 127773L; -static const int RAND_R = 2836L; -static u_long z = 0; - long t; + static const long RAND_A = 16807L; + static const long RAND_M = 2147483647L; + static const long RAND_Q = 127773L; + static const int RAND_R = 2836L; + static unsigned long z = 0; if (z == 0) - z = (u_long) getuid(); + { + struct timeval tv; + + get_time(&tv); + z = tv.tv_usec << 12; + z ^= (unsigned long)tv.tv_sec; + z ^= (unsigned long)getuid() << 16; + z ^= (unsigned long)getpid(); + } if (l == 0) { - t = RAND_A * (z % RAND_Q) - RAND_R * (z / RAND_Q); + long t = RAND_A * (z % RAND_Q) - RAND_R * (z / RAND_Q); if (t > 0) z = t; else @@ -3156,10 +3163,7 @@ static u_long z = 0; } else { - if (l < 0) - z = (u_long) getuid(); - else - z = l; + z = l; return 0; } }