random.c revision ca41b452ede6feaa9d8739ec3cae19389a7b0d03
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews/*
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * Copyright (C) 1999, 2000 Internet Software Consortium.
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews *
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * Permission to use, copy, modify, and distribute this software for any
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * purpose with or without fee is hereby granted, provided that the above
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * copyright notice and this permission notice appear in all copies.
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews *
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews * SOFTWARE.
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews */
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <config.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <stdlib.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <string.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <isc/assertions.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <isc/mutex.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <isc/once.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <isc/random.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#include <isc/util.h>
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrewsstatic isc_once_t once = ISC_ONCE_INIT;
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrewsstatic isc_mutex_t rand_lock;
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrewsstatic void
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrewsinitialize_rand(void)
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews{
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews RUNTIME_CHECK(isc_mutex_init(&rand_lock) == ISC_R_SUCCESS);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews}
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrewsstatic void
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrewsinitialize(void)
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews{
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews RUNTIME_CHECK(isc_once_do(&once, initialize_rand) == ISC_R_SUCCESS);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews}
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrewsisc_result_t
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrewsisc_random_init(isc_random_t *r)
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews{
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews REQUIRE(r != NULL);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews r->magic = ISC_RANDOM_MAGIC;
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#if 0
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews return (isc_mutex_init(&r->lock));
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#else
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews return (ISC_R_SUCCESS);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#endif
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews}
cc51cd2d2076e33117c60c9effcb8caccde4983bWitold Krecicki
30370d905e9be3be7d9b947fd432bacecbb13bb9Evan Huntisc_result_t
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrewsisc_random_invalidate(isc_random_t *r)
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews{
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews isc_result_t result;
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews REQUIRE(ISC_RANDOM_VALID(r));
4df43743ab1ef4bad2b96840a7b2b9c77bc82bc2Mark Andrews
1bce43adcc11d21afaf645cbe466553aabc725b2Mark Andrews#if 0
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews result = isc_mutex_destroy(&r->lock);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#else
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews result = ISC_R_SUCCESS;
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#endif
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews memset(r, 0, sizeof(isc_random_t));
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews return (result);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews}
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrewsvoid
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrewsisc_random_seed(isc_random_t *r, isc_uint32_t seed)
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews{
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews REQUIRE(ISC_RANDOM_VALID(r));
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews initialize();
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
cc51cd2d2076e33117c60c9effcb8caccde4983bWitold Krecicki#if 0
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews LOCK(&r->lock);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#endif
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews LOCK(&rand_lock);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews srand(seed);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews UNLOCK(&rand_lock);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#if 0
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews UNLOCK(&r->lock);
4df43743ab1ef4bad2b96840a7b2b9c77bc82bc2Mark Andrews#endif
1bce43adcc11d21afaf645cbe466553aabc725b2Mark Andrews}
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrewsvoid
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrewsisc_random_get(isc_random_t *r, isc_uint32_t *val)
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews{
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews REQUIRE(ISC_RANDOM_VALID(r));
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews REQUIRE(val != NULL);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews initialize();
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#if 0
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews LOCK(&r->lock);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#endif
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews LOCK(&rand_lock);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews *val = rand();
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews UNLOCK(&rand_lock);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#if 0
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews UNLOCK(&r->lock);
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews#endif
3b83676e079a799f97ad8b76c057e6ecb0426b1dMark Andrews}