]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/randomhelper.cc
8d8b2db57b8d1ea8d980cd38e4d8567861f687cc
[thirdparty/pdns.git] / pdns / randomhelper.cc
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 #include "misc.hh"
5 #include "logger.hh"
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include "dns_random.hh"
11
12 void seedRandom(const string& source)
13 {
14 L<<Logger::Warning<<"Reading random entropy from '"<<source<<"'"<<endl;
15
16 int fd=open(source.c_str(), O_RDONLY);
17 if(fd < 0) {
18 L<<Logger::Error<<"Unable to open source of random '"<<source<<"': "<<stringerror()<<endl;
19 exit(EXIT_FAILURE);
20 }
21 char seed[16];
22 int ret;
23 int pos=0;
24 while(pos!=sizeof(seed)) {
25 ret = read(fd, seed+pos, sizeof(seed)-pos);
26 if(ret < 0) {
27 L<<Logger::Error<<"Unable to read random seed from "<<source<<": "<<stringerror()<<endl;
28 close(fd);
29 exit(EXIT_FAILURE);
30 }
31 if(!ret) {
32 L<<Logger::Error<<"Unable to read random seed from "<<source<<": end of file"<<endl;
33 close(fd);
34 exit(EXIT_FAILURE);
35 }
36 pos+=ret;
37 }
38 close(fd);
39 dns_random_init(seed);
40 }