]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
1405. [func] Use arc4random() if available.
authorMark Andrews <marka@isc.org>
Wed, 4 Dec 2002 01:19:28 +0000 (01:19 +0000)
committerMark Andrews <marka@isc.org>
Wed, 4 Dec 2002 01:19:28 +0000 (01:19 +0000)
from: jakob@crt.se
reviewed: marka

CHANGES
acconfig.h
bin/tests/genrandom.c
configure.in
lib/dns/rdataset.c
lib/isc/random.c

diff --git a/CHANGES b/CHANGES
index e8344729829b385ae1389500d6da2ad12281e1a2..2ae45c1df1e548a9cda1d22dd31154fae9c25290 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,5 @@
+1405.  [func]          Use arc4random() if available.
+
 1404.  [bug]           libbind: ns_name_ntol() could overwite a zero length
                        buffer.
 
index b04dc0db4d06fd5615b7fd7e9c6c933b37cf3e2a..814b9fdd6973acdf7f9235a52a9e073d8a039c1e 100644 (file)
@@ -15,7 +15,7 @@
  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-/* $Id: acconfig.h,v 1.38 2002/10/15 04:32:49 marka Exp $ */
+/* $Id: acconfig.h,v 1.39 2002/12/04 01:19:27 marka Exp $ */
 
 /***
  *** This file is not to be included by any public header files, because
@@ -76,6 +76,9 @@
 /* define if gai_strerror() exists */
 #undef HAVE_GAISTRERROR
 
+/* define if arc4random() exists */
+#undef HAVE_ARC4RANDOM
+
 /* define if pthread_setconcurrency() should be called to tell the
  * OS how many threads we might want to run.
  */
index e90cbacc650c5647d1359d6fae163d02cc52f229..800623ccaf2df70a128c51c6530326e6a0219538 100644 (file)
@@ -15,7 +15,7 @@
  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-/* $Id: genrandom.c,v 1.8 2001/01/09 21:41:04 bwelling Exp $ */
+/* $Id: genrandom.c,v 1.9 2002/12/04 01:19:28 marka Exp $ */
 
 #include <config.h>
 
@@ -47,9 +47,15 @@ main(int argc, char **argv) {
                exit(1);
        }
 
+#ifndef HAVE_ARC4RANDOM
        srand(0x12345678);
+#endif
        while (bytes > 0) {
+#ifndef HAVE_ARC4RANDOM
                unsigned short int x = (rand() & 0xFFFF);
+#else
+               unsigned short int x = (arc4random() & 0xFFFF);
+#endif
                unsigned char c = x & 0xFF;
                if (putc(c, fp) == EOF) {
                        printf("error writing to file\n");
index 917953a4953d1e476585da19eede84ec6ced8ca3..8b99fb23bed1631ca7ca91e8aafc45b90cde0e98 100644 (file)
@@ -18,7 +18,7 @@ AC_DIVERT_PUSH(AC_DIVERSION_NOTICE)dnl
 esyscmd([sed "s/^/# /" COPYRIGHT])dnl
 AC_DIVERT_POP()dnl
 
-AC_REVISION($Revision: 1.333 $)
+AC_REVISION($Revision: 1.334 $)
 
 AC_INIT(lib/dns/name.c)
 AC_PREREQ(2.13)
@@ -521,6 +521,11 @@ case "$use_randomdev" in
                ;;
 esac
 
+#
+# Do we have arc4random() ?
+#
+AC_CHECK_FUNC(arc4random, AC_DEFINE(HAVE_ARC4RANDOM))
+
 #
 # Begin pthreads checking.
 #
index aca841ca2183ed1f3fd24dfe2679f960a03b3e94..2b3c0c22c4798840ac9b87cb45093e7d42cbf6e3 100644 (file)
@@ -15,7 +15,7 @@
  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-/* $Id: rdataset.c,v 1.63 2002/04/02 06:06:29 marka Exp $ */
+/* $Id: rdataset.c,v 1.64 2002/12/04 01:19:28 marka Exp $ */
 
 #include <config.h>
 
@@ -370,7 +370,11 @@ towiresorted(dns_rdataset_t *rdataset, dns_name_t *owner_name,
                         */
                        for (i = 0; i < count; i++) {
                                dns_rdata_t rdata;
+#ifndef HAVE_ARC4RANDOM
                                choice = i + (((u_int)rand()>>3) % (count - i));
+#else
+                               choice = i + (((u_int)arc4random()>>3) % (count - i));
+#endif
                                rdata = shuffled[i];
                                shuffled[i] = shuffled[choice];
                                shuffled[choice] = rdata;
@@ -385,7 +389,11 @@ towiresorted(dns_rdataset_t *rdataset, dns_name_t *owner_name,
                        /*
                         * "Cyclic" order.
                         */
+#ifndef HAVE_ARC4RANDOM
                        unsigned int j = (((unsigned int)rand()) >> 3) % count;
+#else
+                       unsigned int j = (((unsigned int)arc4random()) >> 3) % count;
+#endif
                        for (i = 0; i < count; i++) {
                                if (order != NULL)
                                        sorted[j].key = (*order)(&shuffled[i],
index 4ed13d977d82489536217df20a288c9dd94c2eef..c52b9a9957e4ee118246e82dcede1592b548ba8b 100644 (file)
@@ -15,7 +15,7 @@
  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-/* $Id: random.c,v 1.15 2001/01/09 21:56:22 bwelling Exp $ */
+/* $Id: random.c,v 1.16 2002/12/04 01:19:28 marka Exp $ */
 
 #include <config.h>
 
@@ -33,7 +33,9 @@ static isc_once_t once = ISC_ONCE_INIT;
 static void
 initialize_rand(void)
 {
+#ifndef HAVE_ARC4RANDOM
        srand(time(NULL));
+#endif
 }
 
 static void
@@ -47,7 +49,11 @@ isc_random_seed(isc_uint32_t seed)
 {
        initialize();
 
+#ifndef HAVE_ARC4RANDOM
        srand(seed);
+#else
+       arc4random_addrandom((u_char *) &seed, sizeof(isc_uint32_t));
+#endif
 }
 
 void
@@ -57,7 +63,11 @@ isc_random_get(isc_uint32_t *val)
 
        initialize();
 
+#ifndef HAVE_ARC4RANDOM
        *val = rand();
+#else
+       *val = arc4random();
+#endif
 }
 
 isc_uint32_t
@@ -66,5 +76,9 @@ isc_random_jitter(isc_uint32_t max, isc_uint32_t jitter) {
        if (jitter == 0)
                return (max);
        else
+#ifndef HAVE_ARC4RANDOM
                return (max - rand() % jitter);
+#else
+               return (max - arc4random() % jitter);
+#endif
 }