[Olsr-dev] [PATCH v1 1/1] main: improve random number generator seed

Ferry Huberts (spam-protected)
Sat Nov 10 11:50:55 CET 2012


From: Ferry Huberts <(spam-protected)>

Make it much more random when /dev/random or /dev/urandom is
available.

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 src/main.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/src/main.c b/src/main.c
index ae38439..382c5dc 100644
--- a/src/main.c
+++ b/src/main.c
@@ -230,6 +230,28 @@ olsrmain_load_config(char *file) {
   return 0;
 }
 
+static void initRandom(void) {
+  unsigned int seed = (unsigned int)time(NULL);
+
+#ifndef _WIN32
+  int randomFile;
+
+  randomFile = open("/dev/random", O_RDONLY);
+  if (randomFile == -1) {
+    randomFile = open("/dev/urandom", O_RDONLY);
+  }
+
+  if (randomFile != -1) {
+    if (read(randomFile, &seed, sizeof(seed)) != sizeof(seed)) {
+      ; /* to fix an 'unused result' compiler warning */
+    }
+    close(randomFile);
+  }
+#endif /* _WIN32 */
+
+  srandom(seed);
+}
+
 /**
  * Main entrypoint
  */
@@ -297,7 +319,7 @@ int main(int argc, char *argv[]) {
   olsr_openlog("olsrd");
 
   /* setup random seed */
-  srandom(time(NULL));
+  initRandom();
 
   /* Init widely used statics */
   memset(&all_zero, 0, sizeof(union olsr_ip_addr));
-- 
1.7.11.7





More information about the Olsr-dev mailing list