unbound  0.1
Data Structures | Macros | Functions
random.c File Reference

Thread safe random functions. More...

#include "config.h"
#include "util/random.h"
#include "util/log.h"
#include <openssl/rand.h>
#include <openssl/rc4.h>
#include <openssl/err.h>

Data Structures

struct  ub_randstate
 Struct with per-thread random state. More...

Macros

#define SEED_SIZE   24
 Size of key to use (must be multiple of 8)
#define MAX_VALUE   0x7fffffff
 Max random value.
#define REKEY_BYTES   (1 << 24)
 Number of bytes to reseed after.

Functions

void ub_systemseed (unsigned int seed)
 Initialize the system randomness.
static void ub_arc4random_stir (struct ub_randstate *s, struct ub_randstate *from)
 reseed random generator
struct ub_randstateub_initstate (unsigned int seed, struct ub_randstate *from)
 Initialize a random generator state for use.
long int ub_random (struct ub_randstate *s)
 Generate next random number from the state passed along.
long int ub_random_max (struct ub_randstate *state, long int x)
 Generate random number between 0 and x-1.
void ub_randfree (struct ub_randstate *s)
 Delete the random state.

Detailed Description

Thread safe random functions.

Similar to arc4random() with an explicit initialisation routine.

The code in this file is based on arc4random from openssh-4.0p1/openbsd-compat/bsd-arc4random.c That code is also BSD licensed. Here is their statement:

Copyright (c) 1996, David Mazieres dm@uu.nosp@m.n.or.nosp@m.g Copyright (c) 2008, Damien Miller djm@o.nosp@m.penb.nosp@m.sd.or.nosp@m.g

Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Macro Definition Documentation

#define MAX_VALUE   0x7fffffff

Max random value.

Similar to RAND_MAX, but more portable (mingw uses only 15 bits random).

Referenced by ub_random(), and ub_random_max().

Function Documentation

void ub_systemseed ( unsigned int  seed)

Initialize the system randomness.

Obtains entropy from the system before a chroot or privilege makes it unavailable. You do not have to call this, otherwise ub_initstate does so.

Parameters
seed,:seed value to create state (if no good entropy is found).

References log_err(), VERB_OPS, and verbose().

Referenced by daemon_init(), ub_arc4random_stir(), and ub_initstate().

struct ub_randstate* ub_initstate ( unsigned int  seed,
struct ub_randstate from 
)
read

Initialize a random generator state for use.

Parameters
seed,:seed value to create state contents. (ignored for arc4random).
from,:if not NULL, the seed is taken from this random structure. can be used to seed random states via a parent-random-state that is itself seeded with entropy.
Returns
new state or NULL alloc failure.

References log_err(), ub_arc4random_stir(), and ub_systemseed().

Referenced by daemon_create_workers(), libworker_setup(), rnd_test(), ub_ctx_create(), and worker_create().

long int ub_random ( struct ub_randstate state)

Generate next random number from the state passed along.

Thread safe, so random numbers are repeatable.

Parameters
state,:must have been initialised with ub_initstate.
Returns
: random 31 bit value.

References MAX_VALUE, ub_randstate::rc4, ub_randstate::rc4_ready, and ub_arc4random_stir().

Referenced by daemon_create_workers(), libworker_setup(), pending_tcp_query(), rnd_test(), select_id(), serviced_perturb_qname(), ub_arc4random_stir(), and ub_random_max().

long int ub_random_max ( struct ub_randstate state,
long int  x 
)

Generate random number between 0 and x-1.

No modulo bias.

Parameters
state,:must have been initialised with ub_initstate.
x,:an upper limit. not (negative or zero). must be smaller than 2**31.
Returns
: random value between 0..x-1. Possibly more than one random number is picked from the random stream to satisfy this.

References MAX_VALUE, and ub_random().

Referenced by calc_next_probe(), daemon_get_shufport(), iter_ns_probability(), iter_server_selection(), pick_outgoing_tcp(), rnd_test(), and select_ifport().

void ub_randfree ( struct ub_randstate state)

Delete the random state.

Parameters
state,:to delete.

Referenced by daemon_delete(), libworker_delete(), rnd_test(), ub_ctx_create(), ub_ctx_delete(), and worker_delete().