/contrib/bind9/lib/isc/random.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 113 lines · 66 code · 19 blank · 28 comment · 8 complexity · e2c1837368d0d4d17dadc67e47e51630 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or 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. /* $Id: random.c,v 1.28 2009/07/16 05:52:46 marka Exp $ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <stdlib.h>
  21. #include <time.h> /* Required for time(). */
  22. #ifdef HAVE_SYS_TYPES_H
  23. #include <sys/types.h>
  24. #endif
  25. #ifdef HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28. #include <isc/mutex.h>
  29. #include <isc/once.h>
  30. #include <isc/random.h>
  31. #include <isc/string.h>
  32. #include <isc/util.h>
  33. static isc_once_t once = ISC_ONCE_INIT;
  34. static void
  35. initialize_rand(void)
  36. {
  37. #ifndef HAVE_ARC4RANDOM
  38. unsigned int pid = getpid();
  39. /*
  40. * The low bits of pid generally change faster.
  41. * Xor them with the high bits of time which change slowly.
  42. */
  43. pid = ((pid << 16) & 0xffff0000) | ((pid >> 16) & 0xffff);
  44. srand(time(NULL) ^ pid);
  45. #endif
  46. }
  47. static void
  48. initialize(void)
  49. {
  50. RUNTIME_CHECK(isc_once_do(&once, initialize_rand) == ISC_R_SUCCESS);
  51. }
  52. void
  53. isc_random_seed(isc_uint32_t seed)
  54. {
  55. initialize();
  56. #ifndef HAVE_ARC4RANDOM
  57. srand(seed);
  58. #else
  59. arc4random_addrandom((u_char *) &seed, sizeof(isc_uint32_t));
  60. #endif
  61. }
  62. void
  63. isc_random_get(isc_uint32_t *val)
  64. {
  65. REQUIRE(val != NULL);
  66. initialize();
  67. #ifndef HAVE_ARC4RANDOM
  68. /*
  69. * rand()'s lower bits are not random.
  70. * rand()'s upper bit is zero.
  71. */
  72. #if RAND_MAX >= 0xfffff
  73. /* We have at least 20 bits. Use lower 16 excluding lower most 4 */
  74. *val = ((rand() >> 4) & 0xffff) | ((rand() << 12) & 0xffff0000);
  75. #elif RAND_MAX >= 0x7fff
  76. /* We have at least 15 bits. Use lower 10/11 excluding lower most 4 */
  77. *val = ((rand() >> 4) & 0x000007ff) | ((rand() << 7) & 0x003ff800) |
  78. ((rand() << 18) & 0xffc00000);
  79. #else
  80. #error RAND_MAX is too small
  81. #endif
  82. #else
  83. *val = arc4random();
  84. #endif
  85. }
  86. isc_uint32_t
  87. isc_random_jitter(isc_uint32_t max, isc_uint32_t jitter) {
  88. isc_uint32_t rnd;
  89. REQUIRE(jitter < max || (jitter == 0 && max == 0));
  90. if (jitter == 0)
  91. return (max);
  92. isc_random_get(&rnd);
  93. return (max - rnd % jitter);
  94. }