being defined.
Published in <sxsheqqq6xb.fsf@florida.arsdigita.de>.
+2001-12-17 Hrvoje Niksic <hniksic@arsdigita.com>
+
+ * gen_sslfunc.c (ssl_init_prng): Use random_number to get a byte
+ of "randomness" at a time.
+ (ssl_init_prng): Don't seed the PRNG; random_number will do that.
+
+ * retr.c (sleep_between_retrievals): Use it. Make sure that the
+ random amount averages in opt.wait.
+ (sleep_between_retrievals): Don't seed the PRNG; random_number
+ will do that.
+
+ * utils.c (random_number): New function.
+
2001-12-14 Hrvoje Niksic <hniksic@arsdigita.com>
* url.c (path_simplify): Move here from utils.c, and make static.
#include <openssl/rand.h>
#include "wget.h"
+#include "utils.h"
#include "connect.h"
#include "url.h"
security will use /dev/random or their own source of randomness
anyway. */
- srand (time (NULL));
while (RAND_status () == 0 && maxrand-- > 0)
{
- int rnd = rand ();
- RAND_seed ((unsigned char *)&rnd, sizeof (rnd));
+ unsigned char rnd = random_number (256);
+ RAND_seed (&rnd, sizeof (rnd));
}
if (RAND_status () == 0)
{
static int first_retrieval = 1;
- if (first_retrieval && opt.random_wait)
- /* --random-wait uses the RNG, so seed it. */
- srand (time (NULL));
-
if (!first_retrieval && (opt.wait || opt.waitretry))
{
if (opt.waitretry && count > 1)
sleep (opt.wait);
else
{
- int waitmax = 2 * opt.wait;
- /* This is equivalent to rand() % waitmax, but uses the
- high-order bits for better randomness. */
- int waitsecs = (double)waitmax * rand () / (RAND_MAX + 1.0);
+ /* Sleep a random amount of time averaging in opt.wait
+ seconds. The sleeping amount ranges from 0 to
+ opt.wait*2, inclusive. */
+ int waitsecs = random_number (opt.wait * 2 + 1);
DEBUGP (("sleep_between_retrievals: norm=%ld,fuzz=%ld,sleep=%d\n",
opt.wait, waitsecs - opt.wait, waitsecs));
#endif /* TIOCGWINSZ */
}
+/* Return a random number between 0 and MAX-1, inclusive.
+
+ If MAX is greater than the value of RAND_MAX+1 on the system, the
+ returned value will be in the range [0, RAND_MAX]. This may be
+ fixed in a future release.
+
+ The random number generator is seeded automatically the first time
+ it is called.
+
+ This uses rand() for portability. It has been suggested that
+ random() offers better randomness, but this is not required for
+ Wget, so I chose to go for simplicity and use rand
+ unconditionally. */
+
+int
+random_number (int max)
+{
+ static int seeded;
+ double bounded;
+ int rnd;
+
+ if (!seeded)
+ {
+ srand (time (NULL));
+ seeded = 1;
+ }
+ rnd = rand ();
+
+ /* On systems that don't define RAND_MAX, assume it to be 2**15 - 1,
+ and enforce that assumption by masking other bits. */
+#ifndef RAND_MAX
+# define RAND_MAX 32767
+ rnd &= RAND_MAX;
+#endif
+
+ /* This is equivalent to rand() % max, but uses the high-order bits
+ for better randomness on architecture where rand() is implemented
+ using a simple congruential generator. */
+
+ bounded = (double)max * rnd / (RAND_MAX + 1.0);
+ return (int)bounded;
+}
+
#if 0
/* A debugging function for checking whether an MD5 library works. */
char *html_quote_string PARAMS ((const char *));
int determine_screen_width PARAMS ((void));
+int random_number PARAMS ((int));
#endif /* UTILS_H */