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...

Defines

#define SEED_SIZE   20
 Size of key to use.
#define REKEY_BYTES   (1 << 24)
 Number of bytes to reseed after.

Functions

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.
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.


Function Documentation

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(), VERB_OPS, and verbose().

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

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 ub_randstate::rc4, ub_randstate::rc4_ready, and ub_arc4random_stir().

Referenced by daemon_get_shufport(), iter_ns_probability(), iter_server_selection(), pending_tcp_query(), rnd_test(), select_id(), select_ifport(), serviced_perturb_qname(), and ub_arc4random_stir().

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().


Generated on Sun Sep 21 16:23:30 2008 for unbound by  doxygen 1.5.6