about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRichard Nyberg <rnyberg@murmeldjur.se>2007-06-20 15:53:44 +0000
committerRichard Nyberg <rnyberg@murmeldjur.se>2007-06-20 15:53:44 +0000
commit9a08fda26edd57b9441c1df5d75423dc3b14417c (patch)
treefd4d897e66b2ab29a0dafb53008537b9ea970c6c
parent46fb0c2419ec863a6749553a4166116ef95fdf21 (diff)
downloadbtpd-9a08fda26edd57b9441c1df5d75423dc3b14417c.tar.gz
btpd-9a08fda26edd57b9441c1df5d75423dc3b14417c.zip
Create the peer id and random seed based on the host name, port and
the time of the initialization in seconds and microseconds.

-rw-r--r--btpd/btpd.c23
1 files changed, 18 insertions, 5 deletions
diff --git a/btpd/btpd.c b/btpd/btpd.c
index 67419ed..b68439c 100644
--- a/btpd/btpd.c
+++ b/btpd/btpd.c
@@ -1,7 +1,7 @@
 #include "btpd.h"
 
+#include <openssl/sha.h>
 #include <signal.h>
-#include <time.h>
 
 static uint8_t m_peer_id[20];
 static struct event m_sigint;
@@ -80,12 +80,25 @@ void ipc_init(void);
 void
 btpd_init(void)
 {
-    srandom(time(NULL));
-
+    unsigned long seed;
+    uint8_t idcon[1024];
+    struct timeval now;
+    int n;
+
+    gettimeofday(&now, NULL);
+    n = snprintf(idcon, sizeof(idcon), "%ld%ld%d", now.tv_sec, now.tv_usec,
+        net_port);
+    if (n < sizeof(idcon))
+        gethostname(idcon + n, sizeof(idcon) - n);
+    idcon[sizeof(idcon) - 1] = '\0';
+    n = strlen(idcon);
+
+    SHA1(idcon, n, m_peer_id);
+    bcopy(m_peer_id, &seed, sizeof(seed));
     bcopy(BTPD_VERSION, m_peer_id, sizeof(BTPD_VERSION) - 1);
     m_peer_id[sizeof(BTPD_VERSION) - 1] = '|';
-    for (int i = sizeof(BTPD_VERSION); i < 20; i++)
-        m_peer_id[i] = rand_between(0, 255);
+
+    srandom(seed);
 
     net_init();
     ipc_init();