1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert * Copyright (C) 1999-2003 Internet Software Consortium.
4*a466cc55SCy Schubert *
5*a466cc55SCy Schubert * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert *
9*a466cc55SCy Schubert * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert */
17*a466cc55SCy Schubert
18*a466cc55SCy Schubert /* $Id: random.c,v 1.28 2009/07/16 05:52:46 marka Exp $ */
19*a466cc55SCy Schubert
20*a466cc55SCy Schubert /*! \file */
21*a466cc55SCy Schubert
22*a466cc55SCy Schubert #include <config.h>
23*a466cc55SCy Schubert
24*a466cc55SCy Schubert #include <stdlib.h>
25*a466cc55SCy Schubert #include <time.h> /* Required for time(). */
26*a466cc55SCy Schubert #ifdef HAVE_SYS_TYPES_H
27*a466cc55SCy Schubert #include <sys/types.h>
28*a466cc55SCy Schubert #endif
29*a466cc55SCy Schubert #ifdef HAVE_UNISTD_H
30*a466cc55SCy Schubert #include <unistd.h>
31*a466cc55SCy Schubert #endif
32*a466cc55SCy Schubert
33*a466cc55SCy Schubert #include <isc/mutex.h>
34*a466cc55SCy Schubert #include <isc/once.h>
35*a466cc55SCy Schubert #include <isc/random.h>
36*a466cc55SCy Schubert #include <isc/string.h>
37*a466cc55SCy Schubert #include <isc/util.h>
38*a466cc55SCy Schubert
39*a466cc55SCy Schubert static isc_once_t once = ISC_ONCE_INIT;
40*a466cc55SCy Schubert
41*a466cc55SCy Schubert static void
initialize_rand(void)42*a466cc55SCy Schubert initialize_rand(void)
43*a466cc55SCy Schubert {
44*a466cc55SCy Schubert #ifndef HAVE_ARC4RANDOM
45*a466cc55SCy Schubert unsigned int pid = getpid();
46*a466cc55SCy Schubert
47*a466cc55SCy Schubert /*
48*a466cc55SCy Schubert * The low bits of pid generally change faster.
49*a466cc55SCy Schubert * Xor them with the high bits of time which change slowly.
50*a466cc55SCy Schubert */
51*a466cc55SCy Schubert pid = ((pid << 16) & 0xffff0000) | ((pid >> 16) & 0xffff);
52*a466cc55SCy Schubert
53*a466cc55SCy Schubert srand(time(NULL) ^ pid);
54*a466cc55SCy Schubert #endif
55*a466cc55SCy Schubert }
56*a466cc55SCy Schubert
57*a466cc55SCy Schubert static void
initialize(void)58*a466cc55SCy Schubert initialize(void)
59*a466cc55SCy Schubert {
60*a466cc55SCy Schubert RUNTIME_CHECK(isc_once_do(&once, initialize_rand) == ISC_R_SUCCESS);
61*a466cc55SCy Schubert }
62*a466cc55SCy Schubert
63*a466cc55SCy Schubert void
isc_random_seed(isc_uint32_t seed)64*a466cc55SCy Schubert isc_random_seed(isc_uint32_t seed)
65*a466cc55SCy Schubert {
66*a466cc55SCy Schubert initialize();
67*a466cc55SCy Schubert
68*a466cc55SCy Schubert #ifndef HAVE_ARC4RANDOM
69*a466cc55SCy Schubert srand(seed);
70*a466cc55SCy Schubert #endif
71*a466cc55SCy Schubert }
72*a466cc55SCy Schubert
73*a466cc55SCy Schubert void
isc_random_get(isc_uint32_t * val)74*a466cc55SCy Schubert isc_random_get(isc_uint32_t *val)
75*a466cc55SCy Schubert {
76*a466cc55SCy Schubert REQUIRE(val != NULL);
77*a466cc55SCy Schubert
78*a466cc55SCy Schubert initialize();
79*a466cc55SCy Schubert
80*a466cc55SCy Schubert #ifndef HAVE_ARC4RANDOM
81*a466cc55SCy Schubert /*
82*a466cc55SCy Schubert * rand()'s lower bits are not random.
83*a466cc55SCy Schubert * rand()'s upper bit is zero.
84*a466cc55SCy Schubert */
85*a466cc55SCy Schubert #if RAND_MAX >= 0xfffff
86*a466cc55SCy Schubert /* We have at least 20 bits. Use lower 16 excluding lower most 4 */
87*a466cc55SCy Schubert *val = ((rand() >> 4) & 0xffff) | ((rand() << 12) & 0xffff0000);
88*a466cc55SCy Schubert #elif RAND_MAX >= 0x7fff
89*a466cc55SCy Schubert /* We have at least 15 bits. Use lower 10/11 excluding lower most 4 */
90*a466cc55SCy Schubert *val = ((rand() >> 4) & 0x000007ff) | ((rand() << 7) & 0x003ff800) |
91*a466cc55SCy Schubert ((rand() << 18) & 0xffc00000);
92*a466cc55SCy Schubert #else
93*a466cc55SCy Schubert #error RAND_MAX is too small
94*a466cc55SCy Schubert #endif
95*a466cc55SCy Schubert #else
96*a466cc55SCy Schubert *val = arc4random();
97*a466cc55SCy Schubert #endif
98*a466cc55SCy Schubert }
99*a466cc55SCy Schubert
100*a466cc55SCy Schubert isc_uint32_t
isc_random_jitter(isc_uint32_t max,isc_uint32_t jitter)101*a466cc55SCy Schubert isc_random_jitter(isc_uint32_t max, isc_uint32_t jitter) {
102*a466cc55SCy Schubert isc_uint32_t rnd;
103*a466cc55SCy Schubert
104*a466cc55SCy Schubert REQUIRE(jitter < max || (jitter == 0 && max == 0));
105*a466cc55SCy Schubert
106*a466cc55SCy Schubert if (jitter == 0)
107*a466cc55SCy Schubert return (max);
108*a466cc55SCy Schubert
109*a466cc55SCy Schubert isc_random_get(&rnd);
110*a466cc55SCy Schubert return (max - rnd % jitter);
111*a466cc55SCy Schubert }
112