Thread safe random functions. More...
Functions | |
| void | ub_systemseed (unsigned int seed) |
| Initialize the system randomness. | |
| struct ub_randstate * | ub_initstate (unsigned int seed, struct ub_randstate *from) |
| Initialize a random generator state for use. | |
| long int | ub_random (struct ub_randstate *state) |
| 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 *state) |
| Delete the random state. | |
Thread safe random functions.
Similar to arc4random() with an explicit initialisation routine.
| 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.
| seed,: | seed value to create state (if no good entropy is found). |
References log_err(), verbose(), and VERB_OPS.
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.
| 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. |
References log_err(), ub_systemseed(), and ub_arc4random_stir().
Referenced by daemon_create_workers(), worker_create(), ub_ctx_create(), libworker_setup(), and rnd_test().
| long int ub_random | ( | struct ub_randstate * | state | ) |
Generate next random number from the state passed along.
Thread safe, so random numbers are repeatable.
| state,: | must have been initialised with ub_initstate. |
References ub_randstate::rc4_ready, ub_arc4random_stir(), ub_randstate::rc4, and MAX_VALUE.
Referenced by daemon_create_workers(), libworker_setup(), select_id(), pending_tcp_query(), serviced_perturb_qname(), rnd_test(), 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.
| state,: | must have been initialised with ub_initstate. |
| x,: | an upper limit. not (negative or zero). must be smaller than 2**31. |
References MAX_VALUE, and ub_random().
Referenced by daemon_get_shufport(), iter_server_selection(), iter_ns_probability(), pick_outgoing_tcp(), select_ifport(), rnd_test(), and calc_next_probe().
| void ub_randfree | ( | struct ub_randstate * | state | ) |
Delete the random state.
| state,: | to delete. |
Referenced by daemon_delete(), worker_delete(), ub_ctx_create(), ub_ctx_delete(), libworker_delete(), and rnd_test().