File: randomhelper.cc

package info (click to toggle)
pdns-recursor 4.0.4-1%2Bdeb9u3~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 5,484 kB
  • sloc: cpp: 36,380; sh: 11,771; makefile: 305; xml: 37
file content (40 lines) | stat: -rw-r--r-- 1,007 bytes parent folder | download | duplicates (5)
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
32
33
34
35
36
37
38
39
40
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "misc.hh"
#include "logger.hh"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "dns_random.hh"                     

void seedRandom(const string& source)
{
  L<<Logger::Warning<<"Reading random entropy from '"<<source<<"'"<<endl;

  int fd=open(source.c_str(), O_RDONLY);
  if(fd < 0) {
    L<<Logger::Error<<"Unable to open source of random '"<<source<<"': "<<stringerror()<<endl;
    exit(EXIT_FAILURE);
  }
  char seed[16];
  int ret;
  int pos=0;
  while(pos!=sizeof(seed)) {
    ret = read(fd, seed+pos, sizeof(seed)-pos);
    if(ret < 0) { 
      L<<Logger::Error<<"Unable to read random seed from "<<source<<": "<<stringerror()<<endl;
      close(fd);
      exit(EXIT_FAILURE);
    }
    if(!ret) {
      L<<Logger::Error<<"Unable to read random seed from "<<source<<": end of file"<<endl;
      close(fd);
      exit(EXIT_FAILURE);
    }
    pos+=ret;
  }
  close(fd);
  dns_random_init(seed);
}