Browse code

- added a random number init. in main.c (tries to seed the prng with a random value, uses /dev/random if present). TODO: try to use egd if no /dev/random.

Andrei Pelinescu-Onciul authored on 21/11/2002 13:36:23
Showing 2 changed files
... ...
@@ -881,6 +881,8 @@ int main(int argc, char** argv)
881 881
 	int ret;
882 882
 	struct passwd *pw_entry;
883 883
 	struct group  *gr_entry;
884
+	unsigned int seed;
885
+	int rfd;
884 886
 
885 887
 	/*init*/
886 888
 	port_no_str_len=0;
... ...
@@ -1079,6 +1081,27 @@ int main(int argc, char** argv)
1079 1079
 		goto error;
1080 1080
 	}
1081 1081
 
1082
+	/* seed the prng */
1083
+	/* try to use /dev/random if possible */
1084
+	seed=0;
1085
+	if ((rfd=open("/dev/random", O_RDONLY))!=-1){
1086
+try_again:
1087
+		if (read(rfd, (void*)&seed, sizeof(seed))==-1){
1088
+			if (errno==EINTR) goto try_again; /* interrupted by signal */
1089
+			LOG(L_WARN, "WARNING: could not read from /dev/random (%d)\n",
1090
+						errno);
1091
+		}
1092
+		DBG("read %u from /dev/random\n", seed);
1093
+			close(rfd);
1094
+	}else{
1095
+		LOG(L_WARN, "WARNING: could not open /dev/random (%d)\n", errno);
1096
+	}
1097
+	seed+=getpid()+time(0);
1098
+	DBG("seeding PRNG with %u\n", seed);
1099
+	srand(seed);
1100
+	DBG("test random number %u\n", rand());
1101
+	
1102
+	
1082 1103
 	/* init hash fucntion */
1083 1104
 	if (init_hash()<0) {
1084 1105
 		LOG(L_ERR, "ERROR: init_hash failed\n");
... ...
@@ -6,12 +6,12 @@
6 6
 
7 7
 
8 8
 debug=9          # debug level (cmd line: -dddddddddd)
9
-fork=yes          # (cmd. line: -D)
10
-log_stderror=yes # (cmd line: -E)
9
+#fork=yes          # (cmd. line: -D)
10
+#log_stderror=yes # (cmd line: -E)
11 11
 #log_stderror=no	# (cmd line: -E)
12 12
 
13 13
 
14
-children=2
14
+#children=2
15 15
 check_via=no     # (cmd. line: -v)
16 16
 dns=off           # (cmd. line: -r)
17 17
 rev_dns=off      # (cmd. line: -R)