diff options
Diffstat (limited to 'core/DHT.c')
-rw-r--r-- | core/DHT.c | 559 |
1 files changed, 182 insertions, 377 deletions
@@ -23,7 +23,6 @@ | |||
23 | 23 | ||
24 | #include "DHT.h" | 24 | #include "DHT.h" |
25 | 25 | ||
26 | |||
27 | typedef struct | 26 | typedef struct |
28 | { | 27 | { |
29 | uint8_t client_id[CLIENT_ID_SIZE]; | 28 | uint8_t client_id[CLIENT_ID_SIZE]; |
@@ -34,8 +33,10 @@ typedef struct | |||
34 | (for nodes in friends_list) or us (for nodes in close_clientlist) */ | 33 | (for nodes in friends_list) or us (for nodes in close_clientlist) */ |
35 | uint32_t ret_timestamp; | 34 | uint32_t ret_timestamp; |
36 | }Client_data; | 35 | }Client_data; |
36 | |||
37 | /* maximum number of clients stored per friend. */ | 37 | /* maximum number of clients stored per friend. */ |
38 | #define MAX_FRIEND_CLIENTS 8 | 38 | #define MAX_FRIEND_CLIENTS 8 |
39 | |||
39 | typedef struct | 40 | typedef struct |
40 | { | 41 | { |
41 | uint8_t client_id[CLIENT_ID_SIZE]; | 42 | uint8_t client_id[CLIENT_ID_SIZE]; |
@@ -50,7 +51,6 @@ typedef struct | |||
50 | uint32_t NATping_timestamp; | 51 | uint32_t NATping_timestamp; |
51 | }Friend; | 52 | }Friend; |
52 | 53 | ||
53 | |||
54 | typedef struct | 54 | typedef struct |
55 | { | 55 | { |
56 | uint8_t client_id[CLIENT_ID_SIZE]; | 56 | uint8_t client_id[CLIENT_ID_SIZE]; |
@@ -74,8 +74,6 @@ uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | |||
74 | #define LCLIENT_LIST 32 | 74 | #define LCLIENT_LIST 32 |
75 | static Client_data close_clientlist[LCLIENT_LIST]; | 75 | static Client_data close_clientlist[LCLIENT_LIST]; |
76 | 76 | ||
77 | |||
78 | |||
79 | static Friend * friends_list; | 77 | static Friend * friends_list; |
80 | static uint16_t num_friends; | 78 | static uint16_t num_friends; |
81 | 79 | ||
@@ -88,7 +86,6 @@ static Pinged pings[LPING_ARRAY]; | |||
88 | 86 | ||
89 | static Pinged send_nodes[LSEND_NODES_ARRAY]; | 87 | static Pinged send_nodes[LSEND_NODES_ARRAY]; |
90 | 88 | ||
91 | |||
92 | /* Compares client_id1 and client_id2 with client_id | 89 | /* Compares client_id1 and client_id2 with client_id |
93 | return 0 if both are same distance | 90 | return 0 if both are same distance |
94 | return 1 if client_id1 is closer | 91 | return 1 if client_id1 is closer |
@@ -96,17 +93,12 @@ static Pinged send_nodes[LSEND_NODES_ARRAY]; | |||
96 | int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2) /* tested */ | 93 | int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2) /* tested */ |
97 | { | 94 | { |
98 | uint32_t i; | 95 | uint32_t i; |
99 | for(i = 0; i < CLIENT_ID_SIZE; ++i) | 96 | for(i = 0; i < CLIENT_ID_SIZE; ++i) { |
100 | { | 97 | if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i])) { |
101 | if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i])) | ||
102 | { | ||
103 | return 1; | 98 | return 1; |
104 | } | 99 | } else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i])) { |
105 | else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i])) | ||
106 | { | ||
107 | return 2; | 100 | return 2; |
108 | } | 101 | } |
109 | |||
110 | } | 102 | } |
111 | 103 | ||
112 | return 0; | 104 | return 0; |
@@ -123,17 +115,14 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_ | |||
123 | uint32_t i; | 115 | uint32_t i; |
124 | uint32_t temp_time = unix_time(); | 116 | uint32_t temp_time = unix_time(); |
125 | 117 | ||
126 | for(i = 0; i < length; ++i) | 118 | for(i = 0; i < length; ++i) { |
127 | { | ||
128 | /*If ip_port is assigned to a different client_id replace it*/ | 119 | /*If ip_port is assigned to a different client_id replace it*/ |
129 | if(list[i].ip_port.ip.i == ip_port.ip.i && | 120 | if(list[i].ip_port.ip.i == ip_port.ip.i && |
130 | list[i].ip_port.port == ip_port.port) | 121 | list[i].ip_port.port == ip_port.port) { |
131 | { | ||
132 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 122 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
133 | } | 123 | } |
134 | 124 | ||
135 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) | 125 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { |
136 | { | ||
137 | /* Refresh the client timestamp. */ | 126 | /* Refresh the client timestamp. */ |
138 | list[i].timestamp = temp_time; | 127 | list[i].timestamp = temp_time; |
139 | list[i].ip_port.ip.i = ip_port.ip.i; | 128 | list[i].ip_port.ip.i = ip_port.ip.i; |
@@ -150,10 +139,8 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_ | |||
150 | int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) | 139 | int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) |
151 | { | 140 | { |
152 | uint32_t i; | 141 | uint32_t i; |
153 | for(i = 0; i < length; ++i) | 142 | for(i = 0; i < length; ++i) { |
154 | { | 143 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { |
155 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) | ||
156 | { | ||
157 | 144 | ||
158 | return 1; | 145 | return 1; |
159 | } | 146 | } |
@@ -167,8 +154,7 @@ int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) | |||
167 | static int friend_number(uint8_t * client_id) | 154 | static int friend_number(uint8_t * client_id) |
168 | { | 155 | { |
169 | uint32_t i; | 156 | uint32_t i; |
170 | for(i = 0; i < num_friends; ++i) | 157 | for(i = 0; i < num_friends; ++i) { |
171 | { | ||
172 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ | 158 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ |
173 | { | 159 | { |
174 | return i; | 160 | return i; |
@@ -177,13 +163,11 @@ static int friend_number(uint8_t * client_id) | |||
177 | return -1; | 163 | return -1; |
178 | } | 164 | } |
179 | 165 | ||
180 | |||
181 | /* the number of seconds for a non responsive node to become bad. */ | 166 | /* the number of seconds for a non responsive node to become bad. */ |
182 | #define BAD_NODE_TIMEOUT 70 | 167 | #define BAD_NODE_TIMEOUT 70 |
183 | /* the max number of nodes to send with send nodes. */ | 168 | /* the max number of nodes to send with send nodes. */ |
184 | #define MAX_SENT_NODES 8 | 169 | #define MAX_SENT_NODES 8 |
185 | 170 | ||
186 | |||
187 | /* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request: | 171 | /* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request: |
188 | put them in the nodes_list and return how many were found. | 172 | put them in the nodes_list and return how many were found. |
189 | TODO: Make this function much more efficient. */ | 173 | TODO: Make this function much more efficient. */ |
@@ -192,48 +176,35 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | |||
192 | uint32_t i, j, k; | 176 | uint32_t i, j, k; |
193 | int num_nodes=0; | 177 | int num_nodes=0; |
194 | uint32_t temp_time = unix_time(); | 178 | uint32_t temp_time = unix_time(); |
195 | for(i = 0; i < LCLIENT_LIST; ++i) | 179 | for(i = 0; i < LCLIENT_LIST; ++i) { |
196 | { | ||
197 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time && | 180 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time && |
198 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) | 181 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) { |
199 | /* if node is good and not already in list. */ | 182 | /* if node is good and not already in list. */ |
200 | { | 183 | if(num_nodes < MAX_SENT_NODES) { |
201 | if(num_nodes < MAX_SENT_NODES) | ||
202 | { | ||
203 | memcpy(nodes_list[num_nodes].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); | 184 | memcpy(nodes_list[num_nodes].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); |
204 | nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port; | 185 | nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port; |
205 | num_nodes++; | 186 | num_nodes++; |
206 | } | 187 | } |
207 | else for(j = 0; j < MAX_SENT_NODES; ++j) | 188 | else for(j = 0; j < MAX_SENT_NODES; ++j) { |
208 | { | 189 | if(id_closest(client_id, nodes_list[j].client_id, close_clientlist[i].client_id) == 2) { |
209 | if(id_closest(client_id, nodes_list[j].client_id, close_clientlist[i].client_id) == 2) | ||
210 | { | ||
211 | memcpy(nodes_list[j].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); | 190 | memcpy(nodes_list[j].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); |
212 | nodes_list[j].ip_port = close_clientlist[i].ip_port; | 191 | nodes_list[j].ip_port = close_clientlist[i].ip_port; |
213 | break; | 192 | break; |
214 | } | 193 | } |
215 | } | 194 | } |
216 | } | 195 | } |
217 | |||
218 | } | 196 | } |
219 | for(i = 0; i < num_friends; ++i) | 197 | for(i = 0; i < num_friends; ++i) { |
220 | { | 198 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
221 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) | ||
222 | { | ||
223 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time && | 199 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time && |
224 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) | 200 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) { |
225 | /* if node is good and not already in list. */ | 201 | /* if node is good and not already in list. */ |
226 | { | 202 | if(num_nodes < MAX_SENT_NODES) { |
227 | if(num_nodes < MAX_SENT_NODES) | ||
228 | { | ||
229 | memcpy(nodes_list[num_nodes].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); | 203 | memcpy(nodes_list[num_nodes].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); |
230 | nodes_list[num_nodes].ip_port = friends_list[i].client_list[j].ip_port; | 204 | nodes_list[num_nodes].ip_port = friends_list[i].client_list[j].ip_port; |
231 | num_nodes++; | 205 | num_nodes++; |
232 | } | 206 | } else for(k = 0; k < MAX_SENT_NODES; ++k) { |
233 | else for(k = 0; k < MAX_SENT_NODES; ++k) | 207 | if(id_closest(client_id, nodes_list[k].client_id, friends_list[i].client_list[j].client_id) == 2) { |
234 | { | ||
235 | if(id_closest(client_id, nodes_list[k].client_id, friends_list[i].client_list[j].client_id) == 2) | ||
236 | { | ||
237 | memcpy(nodes_list[k].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); | 208 | memcpy(nodes_list[k].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); |
238 | nodes_list[k].ip_port = friends_list[i].client_list[j].ip_port; | 209 | nodes_list[k].ip_port = friends_list[i].client_list[j].ip_port; |
239 | break; | 210 | break; |
@@ -243,12 +214,9 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | |||
243 | } | 214 | } |
244 | } | 215 | } |
245 | 216 | ||
246 | return num_nodes; | 217 | return num_nodes; |
247 | |||
248 | } | 218 | } |
249 | 219 | ||
250 | |||
251 | |||
252 | /* replace first bad (or empty) node with this one | 220 | /* replace first bad (or empty) node with this one |
253 | return 0 if successful | 221 | return 0 if successful |
254 | return 1 if not (list contains no bad nodes) */ | 222 | return 1 if not (list contains no bad nodes) */ |
@@ -256,10 +224,8 @@ int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Por | |||
256 | { | 224 | { |
257 | uint32_t i; | 225 | uint32_t i; |
258 | uint32_t temp_time = unix_time(); | 226 | uint32_t temp_time = unix_time(); |
259 | for(i = 0; i < length; ++i) | 227 | for(i = 0; i < length; ++i) { |
260 | { | 228 | if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ { |
261 | if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ | ||
262 | { | ||
263 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 229 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
264 | list[i].ip_port = ip_port; | 230 | list[i].ip_port = ip_port; |
265 | list[i].timestamp = temp_time; | 231 | list[i].timestamp = temp_time; |
@@ -279,10 +245,8 @@ int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Po | |||
279 | uint32_t i; | 245 | uint32_t i; |
280 | uint32_t temp_time = unix_time(); | 246 | uint32_t temp_time = unix_time(); |
281 | 247 | ||
282 | for(i = 0; i < length; ++i) | 248 | for(i = 0; i < length; ++i) { |
283 | { | 249 | if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) { |
284 | if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) | ||
285 | { | ||
286 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 250 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
287 | list[i].ip_port = ip_port; | 251 | list[i].ip_port = ip_port; |
288 | list[i].timestamp = temp_time; | 252 | list[i].timestamp = temp_time; |
@@ -302,23 +266,16 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id) | |||
302 | uint32_t i; | 266 | uint32_t i; |
303 | 267 | ||
304 | /* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */ | 268 | /* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */ |
305 | if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) | 269 | if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) { |
306 | { | ||
307 | 270 | ||
308 | if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) | 271 | if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) { |
309 | { | ||
310 | /* if we can't replace bad nodes we try replacing good ones */ | 272 | /* if we can't replace bad nodes we try replacing good ones */ |
311 | replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key); | 273 | replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key); |
312 | } | 274 | } |
313 | |||
314 | } | 275 | } |
315 | for(i = 0; i < num_friends; ++i) | 276 | for(i = 0; i < num_friends; ++i) { |
316 | { | 277 | if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) { |
317 | if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) | 278 | if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) { |
318 | { | ||
319 | |||
320 | if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) | ||
321 | { | ||
322 | /* if we can't replace bad nodes we try replacing good ones. */ | 279 | /* if we can't replace bad nodes we try replacing good ones. */ |
323 | replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, friends_list[i].client_id); | 280 | replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, friends_list[i].client_id); |
324 | } | 281 | } |
@@ -332,12 +289,9 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient | |||
332 | { | 289 | { |
333 | uint32_t i, j; | 290 | uint32_t i, j; |
334 | uint32_t temp_time = unix_time(); | 291 | uint32_t temp_time = unix_time(); |
335 | if(memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) | 292 | if(memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) { |
336 | { | 293 | for(i = 0; i < LCLIENT_LIST; ++i) { |
337 | for(i = 0; i < LCLIENT_LIST; ++i) | 294 | if(memcmp(nodeclient_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) { |
338 | { | ||
339 | if(memcmp(nodeclient_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) | ||
340 | { | ||
341 | close_clientlist[i].ret_ip_port = ip_port; | 295 | close_clientlist[i].ret_ip_port = ip_port; |
342 | close_clientlist[i].ret_timestamp = temp_time; | 296 | close_clientlist[i].ret_timestamp = temp_time; |
343 | return; | 297 | return; |
@@ -345,14 +299,10 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient | |||
345 | } | 299 | } |
346 | } | 300 | } |
347 | else | 301 | else |
348 | for(i = 0; i < num_friends; ++i) | 302 | for(i = 0; i < num_friends; ++i) { |
349 | { | 303 | if(memcmp(client_id, friends_list[i].client_id, CLIENT_ID_SIZE) == 0) { |
350 | if(memcmp(client_id, friends_list[i].client_id, CLIENT_ID_SIZE) == 0) | 304 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
351 | { | 305 | if(memcmp(nodeclient_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE) == 0) { |
352 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) | ||
353 | { | ||
354 | if(memcmp(nodeclient_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE) == 0) | ||
355 | { | ||
356 | friends_list[i].client_list[j].ret_ip_port = ip_port; | 306 | friends_list[i].client_list[j].ret_ip_port = ip_port; |
357 | friends_list[i].client_list[j].ret_timestamp = temp_time; | 307 | friends_list[i].client_list[j].ret_timestamp = temp_time; |
358 | return; | 308 | return; |
@@ -364,6 +314,7 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient | |||
364 | 314 | ||
365 | /* ping timeout in seconds */ | 315 | /* ping timeout in seconds */ |
366 | #define PING_TIMEOUT 5 | 316 | #define PING_TIMEOUT 5 |
317 | |||
367 | /* check if we are currently pinging an ip_port and/or a ping_id | 318 | /* check if we are currently pinging an ip_port and/or a ping_id |
368 | variables with values of zero will not be checked. | 319 | variables with values of zero will not be checked. |
369 | if we are already, return 1 | 320 | if we are already, return 1 |
@@ -375,28 +326,22 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id) | |||
375 | uint8_t pinging; | 326 | uint8_t pinging; |
376 | uint32_t temp_time = unix_time(); | 327 | uint32_t temp_time = unix_time(); |
377 | 328 | ||
378 | for(i = 0; i < LPING_ARRAY; ++i ) | 329 | for(i = 0; i < LPING_ARRAY; ++i ) { |
379 | { | 330 | if((pings[i].timestamp + PING_TIMEOUT) > temp_time) { |
380 | if((pings[i].timestamp + PING_TIMEOUT) > temp_time) | ||
381 | { | ||
382 | pinging = 0; | 331 | pinging = 0; |
383 | if(ip_port.ip.i != 0) | 332 | if(ip_port.ip.i != 0) { |
384 | { | ||
385 | if(pings[i].ip_port.ip.i == ip_port.ip.i && | 333 | if(pings[i].ip_port.ip.i == ip_port.ip.i && |
386 | pings[i].ip_port.port == ip_port.port) | 334 | pings[i].ip_port.port == ip_port.port) { |
387 | { | ||
388 | ++pinging; | 335 | ++pinging; |
389 | } | 336 | } |
390 | } | 337 | } |
391 | if(ping_id != 0) | 338 | if(ping_id != 0) { |
392 | { | ||
393 | if(pings[i].ping_id == ping_id) | 339 | if(pings[i].ping_id == ping_id) |
394 | { | 340 | { |
395 | ++pinging; | 341 | ++pinging; |
396 | } | 342 | } |
397 | } | 343 | } |
398 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) | 344 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) { |
399 | { | ||
400 | return 1; | 345 | return 1; |
401 | } | 346 | } |
402 | 347 | ||
@@ -407,7 +352,6 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id) | |||
407 | 352 | ||
408 | } | 353 | } |
409 | 354 | ||
410 | |||
411 | /* Same as last function but for get_node requests. */ | 355 | /* Same as last function but for get_node requests. */ |
412 | int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) | 356 | int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) |
413 | { | 357 | { |
@@ -415,28 +359,21 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) | |||
415 | uint8_t pinging; | 359 | uint8_t pinging; |
416 | uint32_t temp_time = unix_time(); | 360 | uint32_t temp_time = unix_time(); |
417 | 361 | ||
418 | for(i = 0; i < LSEND_NODES_ARRAY; ++i ) | 362 | for(i = 0; i < LSEND_NODES_ARRAY; ++i ) { |
419 | { | 363 | if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) { |
420 | if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) | ||
421 | { | ||
422 | pinging = 0; | 364 | pinging = 0; |
423 | if(ip_port.ip.i != 0) | 365 | if(ip_port.ip.i != 0) { |
424 | { | ||
425 | if(send_nodes[i].ip_port.ip.i == ip_port.ip.i && | 366 | if(send_nodes[i].ip_port.ip.i == ip_port.ip.i && |
426 | send_nodes[i].ip_port.port == ip_port.port) | 367 | send_nodes[i].ip_port.port == ip_port.port) { |
427 | { | ||
428 | ++pinging; | 368 | ++pinging; |
429 | } | 369 | } |
430 | } | 370 | } |
431 | if(ping_id != 0) | 371 | if(ping_id != 0) { |
432 | { | 372 | if(send_nodes[i].ping_id == ping_id) { |
433 | if(send_nodes[i].ping_id == ping_id) | ||
434 | { | ||
435 | ++pinging; | 373 | ++pinging; |
436 | } | 374 | } |
437 | } | 375 | } |
438 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) | 376 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) { |
439 | { | ||
440 | return 1; | 377 | return 1; |
441 | } | 378 | } |
442 | 379 | ||
@@ -447,7 +384,6 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) | |||
447 | 384 | ||
448 | } | 385 | } |
449 | 386 | ||
450 | |||
451 | /* Add a new ping request to the list of ping requests | 387 | /* Add a new ping request to the list of ping requests |
452 | returns the ping_id to put in the ping request | 388 | returns the ping_id to put in the ping request |
453 | returns 0 if problem. | 389 | returns 0 if problem. |
@@ -458,12 +394,9 @@ uint64_t add_pinging(IP_Port ip_port) | |||
458 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); | 394 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); |
459 | uint32_t temp_time = unix_time(); | 395 | uint32_t temp_time = unix_time(); |
460 | 396 | ||
461 | for(i = 0; i < PING_TIMEOUT; ++i ) | 397 | for(i = 0; i < PING_TIMEOUT; ++i ) { |
462 | { | 398 | for(j = 0; j < LPING_ARRAY; ++j ) { |
463 | for(j = 0; j < LPING_ARRAY; ++j ) | 399 | if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) { |
464 | { | ||
465 | if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) | ||
466 | { | ||
467 | pings[j].timestamp = temp_time; | 400 | pings[j].timestamp = temp_time; |
468 | pings[j].ip_port = ip_port; | 401 | pings[j].ip_port = ip_port; |
469 | pings[j].ping_id = ping_id; | 402 | pings[j].ping_id = ping_id; |
@@ -482,12 +415,9 @@ uint64_t add_gettingnodes(IP_Port ip_port) | |||
482 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); | 415 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); |
483 | uint32_t temp_time = unix_time(); | 416 | uint32_t temp_time = unix_time(); |
484 | 417 | ||
485 | for(i = 0; i < PING_TIMEOUT; ++i ) | 418 | for(i = 0; i < PING_TIMEOUT; ++i ) { |
486 | { | 419 | for(j = 0; j < LSEND_NODES_ARRAY; ++j ) { |
487 | for(j = 0; j < LSEND_NODES_ARRAY; ++j ) | 420 | if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) { |
488 | { | ||
489 | if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) | ||
490 | { | ||
491 | send_nodes[j].timestamp = temp_time; | 421 | send_nodes[j].timestamp = temp_time; |
492 | send_nodes[j].ip_port = ip_port; | 422 | send_nodes[j].ip_port = ip_port; |
493 | send_nodes[j].ping_id = ping_id; | 423 | send_nodes[j].ping_id = ping_id; |
@@ -499,25 +429,20 @@ uint64_t add_gettingnodes(IP_Port ip_port) | |||
499 | 429 | ||
500 | } | 430 | } |
501 | 431 | ||
502 | |||
503 | |||
504 | /* send a ping request | 432 | /* send a ping request |
505 | Ping request only works if none has been sent to that ip/port in the last 5 seconds. */ | 433 | Ping request only works if none has been sent to that ip/port in the last 5 seconds. */ |
506 | static int pingreq(IP_Port ip_port, uint8_t * public_key) | 434 | static int pingreq(IP_Port ip_port, uint8_t * public_key) |
507 | { | 435 | { |
508 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ | 436 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ { |
509 | { | ||
510 | return 1; | 437 | return 1; |
511 | } | 438 | } |
512 | 439 | ||
513 | if(is_pinging(ip_port, 0)) | 440 | if(is_pinging(ip_port, 0)) { |
514 | { | ||
515 | return 1; | 441 | return 1; |
516 | } | 442 | } |
517 | 443 | ||
518 | uint64_t ping_id = add_pinging(ip_port); | 444 | uint64_t ping_id = add_pinging(ip_port); |
519 | if(ping_id == 0) | 445 | if(ping_id == 0) { |
520 | { | ||
521 | return 1; | 446 | return 1; |
522 | } | 447 | } |
523 | 448 | ||
@@ -527,8 +452,7 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key) | |||
527 | random_nonce(nonce); | 452 | random_nonce(nonce); |
528 | 453 | ||
529 | int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); | 454 | int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); |
530 | if(len != sizeof(ping_id) + ENCRYPTION_PADDING) | 455 | if(len != sizeof(ping_id) + ENCRYPTION_PADDING) { |
531 | { | ||
532 | return -1; | 456 | return -1; |
533 | } | 457 | } |
534 | data[0] = 0; | 458 | data[0] = 0; |
@@ -540,12 +464,11 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key) | |||
540 | 464 | ||
541 | } | 465 | } |
542 | 466 | ||
543 | |||
544 | /* send a ping response */ | 467 | /* send a ping response */ |
545 | static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) | 468 | static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) |
546 | { | 469 | { |
547 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ | 470 | /* check if packet is gonna be sent to ourself */ |
548 | { | 471 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) { |
549 | return 1; | 472 | return 1; |
550 | } | 473 | } |
551 | 474 | ||
@@ -555,8 +478,7 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) | |||
555 | random_nonce(nonce); | 478 | random_nonce(nonce); |
556 | 479 | ||
557 | int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); | 480 | int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); |
558 | if(len != sizeof(ping_id) + ENCRYPTION_PADDING) | 481 | if(len != sizeof(ping_id) + ENCRYPTION_PADDING) { |
559 | { | ||
560 | return -1; | 482 | return -1; |
561 | } | 483 | } |
562 | data[0] = 1; | 484 | data[0] = 1; |
@@ -571,20 +493,18 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) | |||
571 | /* send a getnodes request */ | 493 | /* send a getnodes request */ |
572 | static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) | 494 | static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) |
573 | { | 495 | { |
574 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ | 496 | /* check if packet is gonna be sent to ourself */ |
575 | { | 497 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) { |
576 | return 1; | 498 | return 1; |
577 | } | 499 | } |
578 | 500 | ||
579 | if(is_gettingnodes(ip_port, 0)) | 501 | if(is_gettingnodes(ip_port, 0)) { |
580 | { | ||
581 | return 1; | 502 | return 1; |
582 | } | 503 | } |
583 | 504 | ||
584 | uint64_t ping_id = add_gettingnodes(ip_port); | 505 | uint64_t ping_id = add_gettingnodes(ip_port); |
585 | 506 | ||
586 | if(ping_id == 0) | 507 | if(ping_id == 0) { |
587 | { | ||
588 | return 1; | 508 | return 1; |
589 | } | 509 | } |
590 | 510 | ||
@@ -599,8 +519,7 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) | |||
599 | 519 | ||
600 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, sizeof(ping_id) + CLIENT_ID_SIZE, encrypt); | 520 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, sizeof(ping_id) + CLIENT_ID_SIZE, encrypt); |
601 | 521 | ||
602 | if(len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) | 522 | if(len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) { |
603 | { | ||
604 | return -1; | 523 | return -1; |
605 | } | 524 | } |
606 | data[0] = 2; | 525 | data[0] = 2; |
@@ -611,12 +530,10 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) | |||
611 | 530 | ||
612 | } | 531 | } |
613 | 532 | ||
614 | |||
615 | /* send a send nodes response */ | 533 | /* send a send nodes response */ |
616 | static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id) | 534 | static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id) |
617 | { | 535 | { |
618 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ | 536 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ { |
619 | { | ||
620 | return 1; | 537 | return 1; |
621 | } | 538 | } |
622 | 539 | ||
@@ -626,8 +543,7 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, | |||
626 | Node_format nodes_list[MAX_SENT_NODES]; | 543 | Node_format nodes_list[MAX_SENT_NODES]; |
627 | int num_nodes = get_close_nodes(client_id, nodes_list); | 544 | int num_nodes = get_close_nodes(client_id, nodes_list); |
628 | 545 | ||
629 | if(num_nodes == 0) | 546 | if(num_nodes == 0) { |
630 | { | ||
631 | return 0; | 547 | return 0; |
632 | } | 548 | } |
633 | 549 | ||
@@ -642,8 +558,7 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, | |||
642 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, | 558 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, |
643 | sizeof(ping_id) + num_nodes * sizeof(Node_format), encrypt); | 559 | sizeof(ping_id) + num_nodes * sizeof(Node_format), encrypt); |
644 | 560 | ||
645 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) | 561 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) { |
646 | { | ||
647 | return -1; | 562 | return -1; |
648 | } | 563 | } |
649 | 564 | ||
@@ -656,20 +571,17 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, | |||
656 | 571 | ||
657 | } | 572 | } |
658 | 573 | ||
659 | |||
660 | |||
661 | /* Packet handling functions | 574 | /* Packet handling functions |
662 | One to handle each types of packets we receive | 575 | One to handle each types of packets we receive |
663 | return 0 if handled correctly, 1 if packet is bad. */ | 576 | return 0 if handled correctly, 1 if packet is bad. */ |
664 | int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) | 577 | int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) |
665 | { | 578 | { |
666 | uint64_t ping_id; | 579 | uint64_t ping_id; |
667 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) | 580 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) { |
668 | { | ||
669 | return 1; | 581 | return 1; |
670 | } | 582 | } |
671 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ | 583 | /* check if packet is from ourself. */ |
672 | { | 584 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) { |
673 | return 1; | 585 | return 1; |
674 | } | 586 | } |
675 | 587 | ||
@@ -678,8 +590,7 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) | |||
678 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, | 590 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, |
679 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 591 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
680 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); | 592 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); |
681 | if(len != sizeof(ping_id)) | 593 | if(len != sizeof(ping_id)) { |
682 | { | ||
683 | return 1; | 594 | return 1; |
684 | } | 595 | } |
685 | 596 | ||
@@ -695,12 +606,10 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) | |||
695 | int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) | 606 | int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) |
696 | { | 607 | { |
697 | uint64_t ping_id; | 608 | uint64_t ping_id; |
698 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) | 609 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) { |
699 | { | ||
700 | return 1; | 610 | return 1; |
701 | } | 611 | } |
702 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ | 612 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ { |
703 | { | ||
704 | return 1; | 613 | return 1; |
705 | } | 614 | } |
706 | 615 | ||
@@ -709,13 +618,11 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) | |||
709 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, | 618 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, |
710 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 619 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
711 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); | 620 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); |
712 | if(len != sizeof(ping_id)) | 621 | if(len != sizeof(ping_id)) { |
713 | { | ||
714 | return 1; | 622 | return 1; |
715 | } | 623 | } |
716 | 624 | ||
717 | if(is_pinging(source, ping_id)) | 625 | if(is_pinging(source, ping_id)) { |
718 | { | ||
719 | addto_lists(source, packet + 1); | 626 | addto_lists(source, packet + 1); |
720 | return 0; | 627 | return 0; |
721 | } | 628 | } |
@@ -726,12 +633,11 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) | |||
726 | int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) | 633 | int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) |
727 | { | 634 | { |
728 | uint64_t ping_id; | 635 | uint64_t ping_id; |
729 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) | 636 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) { |
730 | { | ||
731 | return 1; | 637 | return 1; |
732 | } | 638 | } |
733 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ | 639 | /* check if packet is from ourself. */ |
734 | { | 640 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) { |
735 | return 1; | 641 | return 1; |
736 | } | 642 | } |
737 | 643 | ||
@@ -741,8 +647,7 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
741 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 647 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
742 | sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, plain); | 648 | sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, plain); |
743 | 649 | ||
744 | if(len != sizeof(ping_id) + CLIENT_ID_SIZE) | 650 | if(len != sizeof(ping_id) + CLIENT_ID_SIZE) { |
745 | { | ||
746 | return 1; | 651 | return 1; |
747 | } | 652 | } |
748 | 653 | ||
@@ -764,8 +669,7 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
764 | (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 669 | (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) |
765 | + ENCRYPTION_PADDING)) % (sizeof(Node_format)) != 0 || | 670 | + ENCRYPTION_PADDING)) % (sizeof(Node_format)) != 0 || |
766 | length < 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 671 | length < 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) |
767 | + sizeof(Node_format) + ENCRYPTION_PADDING) | 672 | + sizeof(Node_format) + ENCRYPTION_PADDING) { |
768 | { | ||
769 | return 1; | 673 | return 1; |
770 | } | 674 | } |
771 | uint32_t num_nodes = (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES | 675 | uint32_t num_nodes = (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES |
@@ -778,14 +682,12 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
778 | sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain); | 682 | sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain); |
779 | 683 | ||
780 | 684 | ||
781 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) | 685 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) { |
782 | { | ||
783 | return 1; | 686 | return 1; |
784 | } | 687 | } |
785 | 688 | ||
786 | memcpy(&ping_id, plain, sizeof(ping_id)); | 689 | memcpy(&ping_id, plain, sizeof(ping_id)); |
787 | if(!is_gettingnodes(source, ping_id)) | 690 | if(!is_gettingnodes(source, ping_id)) { |
788 | { | ||
789 | return 1; | 691 | return 1; |
790 | } | 692 | } |
791 | 693 | ||
@@ -795,8 +697,7 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
795 | addto_lists(source, packet + 1); | 697 | addto_lists(source, packet + 1); |
796 | 698 | ||
797 | uint32_t i; | 699 | uint32_t i; |
798 | for(i = 0; i < num_nodes; ++i) | 700 | for(i = 0; i < num_nodes; ++i) { |
799 | { | ||
800 | pingreq(nodes_list[i].ip_port, nodes_list[i].client_id); | 701 | pingreq(nodes_list[i].ip_port, nodes_list[i].client_id); |
801 | returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); | 702 | returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); |
802 | } | 703 | } |
@@ -809,16 +710,12 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
809 | int DHT_addfriend(uint8_t * client_id) | 710 | int DHT_addfriend(uint8_t * client_id) |
810 | { | 711 | { |
811 | Friend * temp; | 712 | Friend * temp; |
812 | if(num_friends == 0) | 713 | if(num_friends == 0) { |
813 | { | ||
814 | temp = malloc(sizeof(Friend)); | 714 | temp = malloc(sizeof(Friend)); |
815 | } | 715 | } else { |
816 | else | ||
817 | { | ||
818 | temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1)); | 716 | temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1)); |
819 | } | 717 | } |
820 | if(temp == NULL) | 718 | if(temp == NULL) { |
821 | { | ||
822 | return 1; | 719 | return 1; |
823 | } | 720 | } |
824 | 721 | ||
@@ -830,24 +727,19 @@ int DHT_addfriend(uint8_t * client_id) | |||
830 | return 0; | 727 | return 0; |
831 | } | 728 | } |
832 | 729 | ||
833 | |||
834 | |||
835 | int DHT_delfriend(uint8_t * client_id) | 730 | int DHT_delfriend(uint8_t * client_id) |
836 | { | 731 | { |
837 | uint32_t i; | 732 | uint32_t i; |
838 | Friend * temp; | 733 | Friend * temp; |
839 | for(i = 0; i < num_friends; ++i) | 734 | for(i = 0; i < num_friends; ++i) { |
840 | { | 735 | /* Equal */ |
841 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ | 736 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0){ |
842 | { | ||
843 | --num_friends; | 737 | --num_friends; |
844 | if(num_friends != i) | 738 | if(num_friends != i) { |
845 | { | ||
846 | memcpy(friends_list[i].client_id, friends_list[num_friends].client_id, CLIENT_ID_SIZE); | 739 | memcpy(friends_list[i].client_id, friends_list[num_friends].client_id, CLIENT_ID_SIZE); |
847 | } | 740 | } |
848 | temp = realloc(friends_list, sizeof(Friend) * (num_friends)); | 741 | temp = realloc(friends_list, sizeof(Friend) * (num_friends)); |
849 | if(temp != NULL) | 742 | if(temp != NULL) { |
850 | { | ||
851 | friends_list = temp; | 743 | friends_list = temp; |
852 | } | 744 | } |
853 | return 0; | 745 | return 0; |
@@ -856,38 +748,28 @@ int DHT_delfriend(uint8_t * client_id) | |||
856 | return 1; | 748 | return 1; |
857 | } | 749 | } |
858 | 750 | ||
859 | |||
860 | |||
861 | |||
862 | /* TODO: Optimize this. */ | 751 | /* TODO: Optimize this. */ |
863 | IP_Port DHT_getfriendip(uint8_t * client_id) | 752 | IP_Port DHT_getfriendip(uint8_t * client_id) |
864 | { | 753 | { |
865 | uint32_t i, j; | 754 | uint32_t i, j; |
866 | IP_Port empty = {{{0}}, 0}; | 755 | IP_Port empty = {{{0}}, 0}; |
867 | uint32_t temp_time = unix_time(); | 756 | uint32_t temp_time = unix_time(); |
868 | for(i = 0; i < num_friends; ++i) | 757 | for(i = 0; i < num_friends; ++i) { |
869 | { | 758 | /* Equal */ |
870 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ | 759 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)1 { |
871 | { | 760 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
872 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) | ||
873 | { | ||
874 | if(memcmp(friends_list[i].client_list[j].client_id, client_id, CLIENT_ID_SIZE) == 0 && | 761 | if(memcmp(friends_list[i].client_list[j].client_id, client_id, CLIENT_ID_SIZE) == 0 && |
875 | friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) | 762 | friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) { |
876 | { | ||
877 | return friends_list[i].client_list[j].ip_port; | 763 | return friends_list[i].client_list[j].ip_port; |
878 | } | 764 | } |
879 | 765 | } | |
880 | } | ||
881 | |||
882 | return empty; | 766 | return empty; |
883 | } | 767 | } |
884 | } | 768 | } |
885 | empty.ip.i = 1; | 769 | empty.ip.i = 1; |
886 | return empty; | 770 | return empty; |
887 | 771 | ||
888 | } | 772 | } |
889 | |||
890 | |||
891 | 773 | ||
892 | /* The timeout after which a node is discarded completely. */ | 774 | /* The timeout after which a node is discarded completely. */ |
893 | #define Kill_NODE_TIMEOUT 300 | 775 | #define Kill_NODE_TIMEOUT 300 |
@@ -900,8 +782,6 @@ IP_Port DHT_getfriendip(uint8_t * client_id) | |||
900 | 782 | ||
901 | /* Ping each client in the "friends" list every 60 seconds. | 783 | /* Ping each client in the "friends" list every 60 seconds. |
902 | Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. */ | 784 | Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. */ |
903 | |||
904 | |||
905 | void doDHTFriends() | 785 | void doDHTFriends() |
906 | { | 786 | { |
907 | uint32_t i, j; | 787 | uint32_t i, j; |
@@ -909,27 +789,21 @@ void doDHTFriends() | |||
909 | uint32_t rand_node; | 789 | uint32_t rand_node; |
910 | uint32_t index[MAX_FRIEND_CLIENTS]; | 790 | uint32_t index[MAX_FRIEND_CLIENTS]; |
911 | 791 | ||
912 | for(i = 0; i < num_friends; ++i) | 792 | for(i = 0; i < num_friends; ++i) { |
913 | { | ||
914 | uint32_t num_nodes = 0; | 793 | uint32_t num_nodes = 0; |
915 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) | 794 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
916 | { | 795 | if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ { |
917 | if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ | 796 | if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { |
918 | { | ||
919 | if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) | ||
920 | { | ||
921 | pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id); | 797 | pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id); |
922 | friends_list[i].client_list[j].last_pinged = temp_time; | 798 | friends_list[i].client_list[j].last_pinged = temp_time; |
923 | } | 799 | } |
924 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ | 800 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ { |
925 | { | ||
926 | index[num_nodes] = j; | 801 | index[num_nodes] = j; |
927 | ++num_nodes; | 802 | ++num_nodes; |
928 | } | 803 | } |
929 | } | 804 | } |
930 | } | 805 | } |
931 | if(friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) | 806 | if(friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { |
932 | { | ||
933 | rand_node = rand() % num_nodes; | 807 | rand_node = rand() % num_nodes; |
934 | getnodes(friends_list[i].client_list[index[rand_node]].ip_port, | 808 | getnodes(friends_list[i].client_list[index[rand_node]].ip_port, |
935 | friends_list[i].client_list[index[rand_node]].client_id, | 809 | friends_list[i].client_list[index[rand_node]].client_id, |
@@ -951,62 +825,49 @@ void doClose() /* tested */ | |||
951 | uint32_t rand_node; | 825 | uint32_t rand_node; |
952 | uint32_t index[LCLIENT_LIST]; | 826 | uint32_t index[LCLIENT_LIST]; |
953 | 827 | ||
954 | for(i = 0; i < LCLIENT_LIST; ++i) | 828 | for(i = 0; i < LCLIENT_LIST; ++i) { |
955 | { | 829 | /* if node is not dead. */ |
956 | if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ | 830 | if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) { |
957 | { | ||
958 | if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) | 831 | if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) |
959 | { | 832 | { |
960 | pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id); | 833 | pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id); |
961 | close_clientlist[i].last_pinged = temp_time; | 834 | close_clientlist[i].last_pinged = temp_time; |
962 | } | 835 | } |
963 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ | 836 | /* if node is good. */ |
964 | { | 837 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) { |
965 | index[num_nodes] = i; | 838 | index[num_nodes] = i; |
966 | ++num_nodes; | 839 | ++num_nodes; |
967 | } | 840 | } |
968 | } | 841 | } |
969 | } | 842 | } |
970 | 843 | ||
971 | if(close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) | 844 | if(close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { |
972 | { | ||
973 | rand_node = rand() % num_nodes; | 845 | rand_node = rand() % num_nodes; |
974 | getnodes(close_clientlist[index[rand_node]].ip_port, | 846 | getnodes(close_clientlist[index[rand_node]].ip_port, |
975 | close_clientlist[index[rand_node]].client_id, | 847 | close_clientlist[index[rand_node]].client_id, |
976 | self_public_key); | 848 | self_public_key); |
977 | close_lastgetnodes = temp_time; | 849 | close_lastgetnodes = temp_time; |
978 | } | 850 | } |
979 | |||
980 | } | 851 | } |
981 | 852 | ||
982 | |||
983 | |||
984 | |||
985 | |||
986 | |||
987 | void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key) | 853 | void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key) |
988 | { | 854 | { |
989 | getnodes(ip_port, public_key, self_public_key); | 855 | getnodes(ip_port, public_key, self_public_key); |
990 | } | 856 | } |
991 | 857 | ||
992 | |||
993 | |||
994 | /* send the given packet to node with client_id | 858 | /* send the given packet to node with client_id |
995 | returns -1 if failure */ | 859 | returns -1 if failure */ |
996 | int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length) | 860 | int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length) |
997 | { | 861 | { |
998 | uint32_t i; | 862 | uint32_t i; |
999 | for(i = 0; i < LCLIENT_LIST; ++i) | 863 | for(i = 0; i < LCLIENT_LIST; ++i) { |
1000 | { | 864 | if(memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) { |
1001 | if(memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) | ||
1002 | { | ||
1003 | return sendpacket(close_clientlist[i].ip_port, packet, length); | 865 | return sendpacket(close_clientlist[i].ip_port, packet, length); |
1004 | } | 866 | } |
1005 | } | 867 | } |
1006 | return -1; | 868 | return -1; |
1007 | } | 869 | } |
1008 | 870 | ||
1009 | |||
1010 | /* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist | 871 | /* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist |
1011 | ip_portlist must be at least MAX_FRIEND_CLIENTS big | 872 | ip_portlist must be at least MAX_FRIEND_CLIENTS big |
1012 | returns the number of ips returned | 873 | returns the number of ips returned |
@@ -1017,18 +878,15 @@ static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num) | |||
1017 | int num_ips = 0; | 878 | int num_ips = 0; |
1018 | uint32_t i; | 879 | uint32_t i; |
1019 | uint32_t temp_time = unix_time(); | 880 | uint32_t temp_time = unix_time(); |
1020 | if(friend_num >= num_friends) | 881 | if(friend_num >= num_friends) { |
1021 | { | ||
1022 | return -1; | 882 | return -1; |
1023 | } | 883 | } |
1024 | for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) | 884 | for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) |
1025 | { | 885 | { |
886 | /*If ip is not zero and node is good */ | ||
1026 | if(friends_list[friend_num].client_list[i].ret_ip_port.ip.i != 0 && | 887 | if(friends_list[friend_num].client_list[i].ret_ip_port.ip.i != 0 && |
1027 | friends_list[friend_num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) | 888 | friends_list[friend_num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { |
1028 | /*If ip is not zero and node is good */ | 889 | if(memcmp(friends_list[friend_num].client_list[i].client_id, friends_list[friend_num].client_id, CLIENT_ID_SIZE) == 0 ) { |
1029 | { | ||
1030 | if(memcmp(friends_list[friend_num].client_list[i].client_id, friends_list[friend_num].client_id, CLIENT_ID_SIZE) == 0 ) | ||
1031 | { | ||
1032 | return 0; | 890 | return 0; |
1033 | } | 891 | } |
1034 | ip_portlist[num_ips] = friends_list[friend_num].client_list[i].ret_ip_port; | 892 | ip_portlist[num_ips] = friends_list[friend_num].client_list[i].ret_ip_port; |
@@ -1038,7 +896,6 @@ static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num) | |||
1038 | return num_ips; | 896 | return num_ips; |
1039 | } | 897 | } |
1040 | 898 | ||
1041 | |||
1042 | /* Send the following packet to everyone who tells us they are connected to friend_id | 899 | /* Send the following packet to everyone who tells us they are connected to friend_id |
1043 | returns the number of nodes it sent the packet to */ | 900 | returns the number of nodes it sent the packet to */ |
1044 | int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) | 901 | int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) |
@@ -1046,18 +903,14 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) | |||
1046 | uint32_t i, j; | 903 | uint32_t i, j; |
1047 | uint32_t sent = 0; | 904 | uint32_t sent = 0; |
1048 | uint32_t temp_time = unix_time(); | 905 | uint32_t temp_time = unix_time(); |
1049 | for(i = 0; i < num_friends; ++i) | 906 | for(i = 0; i < num_friends; ++i) { |
1050 | { | 907 | /* Equal */ |
1051 | if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) /* Equal */ | 908 | if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) { |
1052 | { | 909 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
1053 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) | 910 | /*If ip is not zero and node is good */ |
1054 | { | ||
1055 | if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 && | 911 | if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 && |
1056 | friends_list[i].client_list[j].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) | 912 | friends_list[i].client_list[j].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { |
1057 | /*If ip is not zero and node is good */ | 913 | if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length) { |
1058 | { | ||
1059 | if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length) | ||
1060 | { | ||
1061 | ++sent; | 914 | ++sent; |
1062 | } | 915 | } |
1063 | } | 916 | } |
@@ -1073,8 +926,7 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) | |||
1073 | int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) | 926 | int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) |
1074 | { | 927 | { |
1075 | int num = friend_number(friend_id); | 928 | int num = friend_number(friend_id); |
1076 | if(num == -1) | 929 | if(num == -1) { |
1077 | { | ||
1078 | return 0; | 930 | return 0; |
1079 | } | 931 | } |
1080 | 932 | ||
@@ -1082,22 +934,18 @@ int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) | |||
1082 | int n = 0; | 934 | int n = 0; |
1083 | uint32_t i; | 935 | uint32_t i; |
1084 | uint32_t temp_time = unix_time(); | 936 | uint32_t temp_time = unix_time(); |
1085 | for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) | 937 | for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
1086 | { | 938 | /*If ip is not zero and node is good */ |
1087 | if(friends_list[num].client_list[i].ret_ip_port.ip.i != 0 && | 939 | if(friends_list[num].client_list[i].ret_ip_port.ip.i != 0 && |
1088 | friends_list[num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) | 940 | friends_list[num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { |
1089 | /*If ip is not zero and node is good */ | ||
1090 | { | ||
1091 | ip_list[n] = friends_list[num].client_list[i].ip_port; | 941 | ip_list[n] = friends_list[num].client_list[i].ip_port; |
1092 | ++n; | 942 | ++n; |
1093 | } | 943 | } |
1094 | } | 944 | } |
1095 | if(n < 1) | 945 | if(n < 1) { |
1096 | { | ||
1097 | return 0; | 946 | return 0; |
1098 | } | 947 | } |
1099 | if(sendpacket(ip_list[rand() % n], packet, length) == length) | 948 | if(sendpacket(ip_list[rand() % n], packet, length) == length) { |
1100 | { | ||
1101 | return 1; | 949 | return 1; |
1102 | } | 950 | } |
1103 | return 0; | 951 | return 0; |
@@ -1112,10 +960,9 @@ int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id) | |||
1112 | { | 960 | { |
1113 | 961 | ||
1114 | uint32_t i; | 962 | uint32_t i; |
1115 | for(i = 0; i < num_friends; ++i) | 963 | for(i = 0; i < num_friends; ++i) { |
1116 | { | 964 | /* Equal */ |
1117 | if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) /* Equal */ | 965 | if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) { |
1118 | { | ||
1119 | return friend_iplist(ip_portlist, i); | 966 | return friend_iplist(ip_portlist, i); |
1120 | } | 967 | } |
1121 | } | 968 | } |
@@ -1132,60 +979,49 @@ int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type) | |||
1132 | 979 | ||
1133 | uint8_t packet[MAX_DATA_SIZE]; | 980 | uint8_t packet[MAX_DATA_SIZE]; |
1134 | int len = create_request(packet, public_key, data, sizeof(uint64_t) + 1, 254); /* 254 is NAT ping request packet id */ | 981 | int len = create_request(packet, public_key, data, sizeof(uint64_t) + 1, 254); /* 254 is NAT ping request packet id */ |
1135 | if(len == -1) | 982 | if(len == -1) { |
1136 | { | ||
1137 | return -1; | 983 | return -1; |
1138 | } | 984 | } |
1139 | int num = 0; | 985 | int num = 0; |
1140 | if(type == 0) | 986 | if(type == 0) { |
1141 | { | ||
1142 | num = route_tofriend(public_key, packet, len);/*If packet is request use many people to route it*/ | 987 | num = route_tofriend(public_key, packet, len);/*If packet is request use many people to route it*/ |
1143 | } | 988 | } |
1144 | else if(type == 1) | 989 | else if(type == 1) { |
1145 | { | ||
1146 | num = routeone_tofriend(public_key, packet, len);/*If packet is response use only one person to route it*/ | 990 | num = routeone_tofriend(public_key, packet, len);/*If packet is response use only one person to route it*/ |
1147 | } | 991 | } |
1148 | if(num == 0) | 992 | if(num == 0) { |
1149 | { | ||
1150 | return -1; | 993 | return -1; |
1151 | } | 994 | } |
1152 | return num; | 995 | return num; |
1153 | } | 996 | } |
1154 | 997 | ||
1155 | |||
1156 | /* Handle a recieved ping request for */ | 998 | /* Handle a recieved ping request for */ |
1157 | int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source) | 999 | int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source) |
1158 | { | 1000 | { |
1159 | if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && | 1001 | if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && |
1160 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) | 1002 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) { |
1161 | { | ||
1162 | return 1; | 1003 | return 1; |
1163 | } | 1004 | } |
1164 | if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)//check if request is for us. | 1005 | /* check if request is for us. */ |
1165 | { | 1006 | if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { |
1166 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 1007 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
1167 | uint8_t data[MAX_DATA_SIZE]; | 1008 | uint8_t data[MAX_DATA_SIZE]; |
1168 | int len = handle_request(public_key, data, packet, length); | 1009 | int len = handle_request(public_key, data, packet, length); |
1169 | if(len != sizeof(uint64_t) + 1) | 1010 | if(len != sizeof(uint64_t) + 1) { |
1170 | { | ||
1171 | return 1; | 1011 | return 1; |
1172 | } | 1012 | } |
1173 | uint64_t ping_id; | 1013 | uint64_t ping_id; |
1174 | memcpy(&ping_id, data + 1, sizeof(uint64_t)); | 1014 | memcpy(&ping_id, data + 1, sizeof(uint64_t)); |
1175 | 1015 | ||
1176 | int friendnumber = friend_number(public_key); | 1016 | int friendnumber = friend_number(public_key); |
1177 | if(friendnumber == -1) | 1017 | if(friendnumber == -1) { |
1178 | { | ||
1179 | return 1; | 1018 | return 1; |
1180 | } | 1019 | } |
1181 | 1020 | ||
1182 | if(data[0] == 0) | 1021 | if(data[0] == 0) { |
1183 | { | ||
1184 | send_NATping(public_key, ping_id, 1);/*1 is reply*/ | 1022 | send_NATping(public_key, ping_id, 1);/*1 is reply*/ |
1185 | return 0; | 1023 | return 0; |
1186 | } | 1024 | } else if (data[0] == 1) { |
1187 | else if (data[0] == 1) | ||
1188 | { | ||
1189 | if(friends_list[friendnumber].NATping_id == ping_id) | 1025 | if(friends_list[friendnumber].NATping_id == ping_id) |
1190 | { | 1026 | { |
1191 | friends_list[friendnumber].NATping_id = ((uint64_t)random_int() << 32) + random_int(); | 1027 | friends_list[friendnumber].NATping_id = ((uint64_t)random_int() << 32) + random_int(); |
@@ -1195,10 +1031,9 @@ int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source) | |||
1195 | } | 1031 | } |
1196 | return 1; | 1032 | return 1; |
1197 | } | 1033 | } |
1198 | else//if request is not for us, try routing it. | 1034 | /* if request is not for us, try routing it. */ |
1199 | { | 1035 | else { |
1200 | if(route_packet(packet + 1, packet, length) == length) | 1036 | if(route_packet(packet + 1, packet, length) == length) { |
1201 | { | ||
1202 | return 0; | 1037 | return 0; |
1203 | } | 1038 | } |
1204 | } | 1039 | } |
@@ -1212,24 +1047,19 @@ int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source) | |||
1212 | static IP NAT_commonip(IP_Port * ip_portlist, uint16_t len, uint16_t min_num) | 1047 | static IP NAT_commonip(IP_Port * ip_portlist, uint16_t len, uint16_t min_num) |
1213 | { | 1048 | { |
1214 | IP zero = {{0}}; | 1049 | IP zero = {{0}}; |
1215 | if(len > MAX_FRIEND_CLIENTS) | 1050 | if(len > MAX_FRIEND_CLIENTS) { |
1216 | { | ||
1217 | return zero; | 1051 | return zero; |
1218 | } | 1052 | } |
1219 | 1053 | ||
1220 | uint32_t i, j; | 1054 | uint32_t i, j; |
1221 | uint16_t numbers[MAX_FRIEND_CLIENTS] = {0}; | 1055 | uint16_t numbers[MAX_FRIEND_CLIENTS] = {0}; |
1222 | for(i = 0; i < len; ++i) | 1056 | for(i = 0; i < len; ++i) { |
1223 | { | 1057 | for(j = 0; j < len; ++j) { |
1224 | for(j = 0; j < len; ++j) | 1058 | if(ip_portlist[i].ip.i == ip_portlist[j].ip.i) { |
1225 | { | ||
1226 | if(ip_portlist[i].ip.i == ip_portlist[j].ip.i) | ||
1227 | { | ||
1228 | ++numbers[i]; | 1059 | ++numbers[i]; |
1229 | } | 1060 | } |
1230 | } | 1061 | } |
1231 | if(numbers[i] >= min_num) | 1062 | if(numbers[i] >= min_num) { |
1232 | { | ||
1233 | return ip_portlist[i].ip; | 1063 | return ip_portlist[i].ip; |
1234 | } | 1064 | } |
1235 | } | 1065 | } |
@@ -1244,10 +1074,8 @@ static uint16_t NAT_getports(uint16_t * portlist, IP_Port * ip_portlist, uint16_ | |||
1244 | { | 1074 | { |
1245 | uint32_t i; | 1075 | uint32_t i; |
1246 | uint16_t num = 0; | 1076 | uint16_t num = 0; |
1247 | for(i = 0; i < len; ++i) | 1077 | for(i = 0; i < len; ++i) { |
1248 | { | 1078 | if(ip_portlist[i].ip.i == ip.i) { |
1249 | if(ip_portlist[i].ip.i == ip.i) | ||
1250 | { | ||
1251 | portlist[num] = ntohs(ip_portlist[i].port); | 1079 | portlist[num] = ntohs(ip_portlist[i].port); |
1252 | ++num; | 1080 | ++num; |
1253 | } | 1081 | } |
@@ -1259,14 +1087,12 @@ static uint16_t NAT_getports(uint16_t * portlist, IP_Port * ip_portlist, uint16_ | |||
1259 | 1087 | ||
1260 | static void punch_holes(IP ip, uint16_t * port_list, uint16_t numports, uint16_t friend_num) | 1088 | static void punch_holes(IP ip, uint16_t * port_list, uint16_t numports, uint16_t friend_num) |
1261 | { | 1089 | { |
1262 | if(numports > MAX_FRIEND_CLIENTS || numports == 0) | 1090 | if(numports > MAX_FRIEND_CLIENTS || numports == 0) { |
1263 | { | ||
1264 | return; | 1091 | return; |
1265 | } | 1092 | } |
1266 | uint32_t i; | 1093 | uint32_t i; |
1267 | uint32_t top = friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS; | 1094 | uint32_t top = friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS; |
1268 | for(i = friends_list[friend_num].punching_index; i != top; i++) | 1095 | for(i = friends_list[friend_num].punching_index; i != top; i++) { |
1269 | { | ||
1270 | /*TODO: improve port guessing algorithm*/ | 1096 | /*TODO: improve port guessing algorithm*/ |
1271 | uint16_t port = port_list[(i/2) % numports] + (i/(2*numports))*((i % 2) ? -1 : 1); | 1097 | uint16_t port = port_list[(i/2) % numports] + (i/(2*numports))*((i % 2) ? -1 : 1); |
1272 | IP_Port pinging = {ip, htons(port)}; | 1098 | IP_Port pinging = {ip, htons(port)}; |
@@ -1282,28 +1108,22 @@ static void doNAT() | |||
1282 | { | 1108 | { |
1283 | uint32_t i; | 1109 | uint32_t i; |
1284 | uint32_t temp_time = unix_time(); | 1110 | uint32_t temp_time = unix_time(); |
1285 | for(i = 0; i < num_friends; ++i) | 1111 | for(i = 0; i < num_friends; ++i) { |
1286 | { | ||
1287 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; | 1112 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; |
1288 | int num = friend_iplist(ip_list, i); | 1113 | int num = friend_iplist(ip_list, i); |
1289 | /*If we are connected to friend or if friend is not online don't try to hole punch with him*/ | 1114 | /*If we are connected to friend or if friend is not online don't try to hole punch with him*/ |
1290 | if(num < MAX_FRIEND_CLIENTS/2) | 1115 | if(num < MAX_FRIEND_CLIENTS/2) { |
1291 | { | ||
1292 | continue; | 1116 | continue; |
1293 | } | 1117 | } |
1294 | if(friends_list[i].hole_punching != 1) | 1118 | if(friends_list[i].hole_punching != 1) { |
1295 | { | 1119 | if(friends_list[i].NATping_timestamp + PUNCH_INTERVAL < temp_time) { |
1296 | if(friends_list[i].NATping_timestamp + PUNCH_INTERVAL < temp_time) | ||
1297 | { | ||
1298 | send_NATping(friends_list[i].client_id, friends_list[i].NATping_id, 0); /*0 is request*/ | 1120 | send_NATping(friends_list[i].client_id, friends_list[i].NATping_id, 0); /*0 is request*/ |
1299 | friends_list[i].NATping_timestamp = temp_time; | 1121 | friends_list[i].NATping_timestamp = temp_time; |
1300 | } | 1122 | } |
1301 | } | 1123 | } |
1302 | else if(friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time) | 1124 | else if(friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time) { |
1303 | { | ||
1304 | IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS/2); | 1125 | IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS/2); |
1305 | if(ip.i == 0) | 1126 | if(ip.i == 0) { |
1306 | { | ||
1307 | continue; | 1127 | continue; |
1308 | } | 1128 | } |
1309 | uint16_t port_list[MAX_FRIEND_CLIENTS]; | 1129 | uint16_t port_list[MAX_FRIEND_CLIENTS]; |
@@ -1318,7 +1138,6 @@ static void doNAT() | |||
1318 | 1138 | ||
1319 | /*END OF NAT PUNCHING FUNCTIONS*/ | 1139 | /*END OF NAT PUNCHING FUNCTIONS*/ |
1320 | 1140 | ||
1321 | |||
1322 | int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | 1141 | int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) |
1323 | { | 1142 | { |
1324 | switch (packet[0]) { | 1143 | switch (packet[0]) { |
@@ -1346,7 +1165,6 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | |||
1346 | 1165 | ||
1347 | } | 1166 | } |
1348 | 1167 | ||
1349 | |||
1350 | void doDHT() | 1168 | void doDHT() |
1351 | { | 1169 | { |
1352 | doClose(); | 1170 | doClose(); |
@@ -1354,8 +1172,6 @@ void doDHT() | |||
1354 | doNAT(); | 1172 | doNAT(); |
1355 | } | 1173 | } |
1356 | 1174 | ||
1357 | |||
1358 | |||
1359 | /* get the size of the DHT (for saving) */ | 1175 | /* get the size of the DHT (for saving) */ |
1360 | uint32_t DHT_size() | 1176 | uint32_t DHT_size() |
1361 | { | 1177 | { |
@@ -1374,12 +1190,10 @@ void DHT_save(uint8_t * data) | |||
1374 | return 0 if success */ | 1190 | return 0 if success */ |
1375 | int DHT_load(uint8_t * data, uint32_t size) | 1191 | int DHT_load(uint8_t * data, uint32_t size) |
1376 | { | 1192 | { |
1377 | if(size < sizeof(close_clientlist)) | 1193 | if(size < sizeof(close_clientlist)) { |
1378 | { | ||
1379 | return -1; | 1194 | return -1; |
1380 | } | 1195 | } |
1381 | if((size - sizeof(close_clientlist)) % sizeof(Friend) != 0) | 1196 | if((size - sizeof(close_clientlist)) % sizeof(Friend) != 0) { |
1382 | { | ||
1383 | return -1; | 1197 | return -1; |
1384 | } | 1198 | } |
1385 | uint32_t i, j; | 1199 | uint32_t i, j; |
@@ -1388,17 +1202,13 @@ int DHT_load(uint8_t * data, uint32_t size) | |||
1388 | 1202 | ||
1389 | temp = (size - sizeof(close_clientlist))/sizeof(Friend); | 1203 | temp = (size - sizeof(close_clientlist))/sizeof(Friend); |
1390 | 1204 | ||
1391 | if(temp != 0) | 1205 | if(temp != 0) { |
1392 | { | ||
1393 | Friend * tempfriends_list = (Friend *)(data + sizeof(close_clientlist)); | 1206 | Friend * tempfriends_list = (Friend *)(data + sizeof(close_clientlist)); |
1394 | 1207 | ||
1395 | for(i = 0; i < temp; ++i) | 1208 | for(i = 0; i < temp; ++i) { |
1396 | { | ||
1397 | DHT_addfriend(tempfriends_list[i].client_id); | 1209 | DHT_addfriend(tempfriends_list[i].client_id); |
1398 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) | 1210 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
1399 | { | 1211 | if(tempfriends_list[i].client_list[j].timestamp != 0) { |
1400 | if(tempfriends_list[i].client_list[j].timestamp != 0) | ||
1401 | { | ||
1402 | getnodes(tempfriends_list[i].client_list[j].ip_port, | 1212 | getnodes(tempfriends_list[i].client_list[j].ip_port, |
1403 | tempfriends_list[i].client_list[j].client_id, tempfriends_list[i].client_id); | 1213 | tempfriends_list[i].client_list[j].client_id, tempfriends_list[i].client_id); |
1404 | } | 1214 | } |
@@ -1407,10 +1217,8 @@ int DHT_load(uint8_t * data, uint32_t size) | |||
1407 | } | 1217 | } |
1408 | Client_data * tempclose_clientlist = (Client_data *)data; | 1218 | Client_data * tempclose_clientlist = (Client_data *)data; |
1409 | 1219 | ||
1410 | for(i = 0; i < LCLIENT_LIST; ++i) | 1220 | for(i = 0; i < LCLIENT_LIST; ++i) { |
1411 | { | 1221 | if(tempclose_clientlist[i].timestamp != 0) { |
1412 | if(tempclose_clientlist[i].timestamp != 0) | ||
1413 | { | ||
1414 | DHT_bootstrap(tempclose_clientlist[i].ip_port, tempclose_clientlist[i].client_id); | 1222 | DHT_bootstrap(tempclose_clientlist[i].ip_port, tempclose_clientlist[i].client_id); |
1415 | } | 1223 | } |
1416 | } | 1224 | } |
@@ -1423,13 +1231,10 @@ int DHT_isconnected() | |||
1423 | { | 1231 | { |
1424 | uint32_t i; | 1232 | uint32_t i; |
1425 | uint32_t temp_time = unix_time(); | 1233 | uint32_t temp_time = unix_time(); |
1426 | for(i = 0; i < LCLIENT_LIST; ++i) | 1234 | for(i = 0; i < LCLIENT_LIST; ++i) { |
1427 | { | 1235 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) { |
1428 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) | ||
1429 | { | ||
1430 | return 1; | 1236 | return 1; |
1431 | } | 1237 | } |
1432 | } | 1238 | } |
1433 | return 0; | 1239 | return 0; |
1434 | } | 1240 | } \ No newline at end of file |
1435 | |||