diff options
-rw-r--r-- | core/network.c | 14 | ||||
-rw-r--r-- | core/network.h | 3 | ||||
-rw-r--r-- | testing/DHT_test.c | 8 |
3 files changed, 21 insertions, 4 deletions
diff --git a/core/network.c b/core/network.c index 81da3f1b..a20b02f6 100644 --- a/core/network.c +++ b/core/network.c | |||
@@ -40,6 +40,16 @@ uint64_t current_time() | |||
40 | 40 | ||
41 | } | 41 | } |
42 | 42 | ||
43 | int random_int() | ||
44 | { | ||
45 | #ifdef WIN32 | ||
46 | //TODO replace rand with a more random windows function | ||
47 | return rand(); | ||
48 | #else | ||
49 | return random(); | ||
50 | #endif | ||
51 | } | ||
52 | |||
43 | //our UDP socket, a global variable. | 53 | //our UDP socket, a global variable. |
44 | static int sock; | 54 | static int sock; |
45 | 55 | ||
@@ -87,7 +97,11 @@ int init_networking(IP ip ,uint16_t port) | |||
87 | { | 97 | { |
88 | return -1; | 98 | return -1; |
89 | } | 99 | } |
100 | |||
101 | #else | ||
102 | srandom((uint32_t)current_time()); | ||
90 | #endif | 103 | #endif |
104 | srand((uint32_t)current_time()); | ||
91 | 105 | ||
92 | //initialize our socket | 106 | //initialize our socket |
93 | sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | 107 | sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
diff --git a/core/network.h b/core/network.h index 24f7281d..da92837d 100644 --- a/core/network.h +++ b/core/network.h | |||
@@ -79,6 +79,9 @@ typedef struct | |||
79 | //returns current time in milleseconds since the epoch. | 79 | //returns current time in milleseconds since the epoch. |
80 | uint64_t current_time(); | 80 | uint64_t current_time(); |
81 | 81 | ||
82 | //return a random number | ||
83 | int random_int(); | ||
84 | |||
82 | //Basic network functions: | 85 | //Basic network functions: |
83 | 86 | ||
84 | //Function to send packet(data) of length length to ip_port | 87 | //Function to send packet(data) of length length to ip_port |
diff --git a/testing/DHT_test.c b/testing/DHT_test.c index 799d31ad..4c1a9cd8 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c | |||
@@ -96,9 +96,6 @@ void printpacket(char * data, uint32_t length, IP_Port ip_port) | |||
96 | 96 | ||
97 | int main(int argc, char *argv[]) | 97 | int main(int argc, char *argv[]) |
98 | { | 98 | { |
99 | srand(time(NULL)); | ||
100 | int randdomnum = rand(); | ||
101 | memcpy(self_client_id, &randdomnum, 4); | ||
102 | //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); | 99 | //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); |
103 | 100 | ||
104 | if (argc < 4) { | 101 | if (argc < 4) { |
@@ -107,12 +104,15 @@ int main(int argc, char *argv[]) | |||
107 | } | 104 | } |
108 | addfriend(argv[3]); | 105 | addfriend(argv[3]); |
109 | 106 | ||
110 | |||
111 | //initialize networking | 107 | //initialize networking |
112 | //bind to ip 0.0.0.0:PORT | 108 | //bind to ip 0.0.0.0:PORT |
113 | IP ip; | 109 | IP ip; |
114 | ip.i = 0; | 110 | ip.i = 0; |
115 | init_networking(ip, PORT); | 111 | init_networking(ip, PORT); |
112 | |||
113 | int randdomnum = random_int(); | ||
114 | memcpy(self_client_id, &randdomnum, 4); | ||
115 | |||
116 | 116 | ||
117 | perror("Initialization"); | 117 | perror("Initialization"); |
118 | IP_Port bootstrap_ip_port; | 118 | IP_Port bootstrap_ip_port; |