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