File: dns_random.c

package info (click to toggle)
bglibs 2.04%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,468 kB
  • sloc: ansic: 15,821; perl: 674; sh: 63; makefile: 29
file content (31 lines) | stat: -rw-r--r-- 669 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <unistd.h>
#include "dns.h"
#include "uint32.h"
#include "surfrand.h"

static struct surfrand state;
static int inited = 0;

/** Initialize the DNS library random state. */
void dns_random_init(const char data[SURF_SEED])
{
  struct timeval tv;
  uint32 altdata[SURF_SEED_U32];

  surfrand_init(&state, data ? (const uint32*)data : altdata, SURF_SEED_U32);

  gettimeofday(&tv,0);
  state.counter[8] = tv.tv_sec;
  state.counter[9] = tv.tv_usec;

  state.counter[10] = getpid();
  state.counter[11] = getppid();

  inited = 1;
}

/** Generate a random number less than \c n. */
unsigned int dns_random(unsigned int n)
{
  return surfrand_uniform(&state, n);
}