summaryrefslogtreecommitdiff
path: root/core/DHT.c
diff options
context:
space:
mode:
authorplutooo <tfy12vbr@student.lu.se>2013-08-03 08:29:23 -0700
committerplutooo <tfy12vbr@student.lu.se>2013-08-03 08:32:09 -0700
commite5d2c219af19421c275d9f9f20f1a4a4a994833f (patch)
treec23d93018ce0d4ff4202b54fdbbde87aac039f1f /core/DHT.c
parent7d7c5faa09f490b0c45ece2149207e78a8ba83f1 (diff)
DHT: added id_equal(), ipport_equal(), is_timeout()
Diffstat (limited to 'core/DHT.c')
-rw-r--r--core/DHT.c160
1 files changed, 89 insertions, 71 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 6972fdeb..5d5910e0 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -119,23 +119,39 @@ static Pinged send_nodes[LSEND_NODES_ARRAY];
119 * return 1 if client_id1 is closer 119 * return 1 if client_id1 is closer
120 * return 2 if client_id2 is closer 120 * return 2 if client_id2 is closer
121 */ 121 */
122int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2) 122int id_closest(uint8_t * id, uint8_t * id1, uint8_t * id2)
123{ 123{
124 uint32_t i; 124 size_t i;
125 uint8_t tmp1, tmp2; 125 uint8_t distance1, distance2;
126 126
127 for(i = 0; i < CLIENT_ID_SIZE; ++i) { 127 for(i = 0; i < CLIENT_ID_SIZE; ++i) {
128 tmp1 = abs(client_id[i] ^ client_id1[i]); 128
129 tmp2 = abs(client_id[i] ^ client_id2[i]); 129 distance1 = abs(id[i] ^ id1[i]);
130 distance2 = abs(id[i] ^ id2[i]);
130 131
131 if(tmp1 < tmp2) 132 if(distance1 < distance2)
132 return 1; 133 return 1;
133 else if(tmp1 > tmp2) 134 if(distance1 > distance2)
134 return 2; 135 return 2;
135 } 136 }
136 return 0; 137 return 0;
137} 138}
138 139
140int ipport_equal(IP_Port a, IP_Port b)
141{
142 return (a.ip.i == b.ip.i) && (a.port == b.port);
143}
144
145int id_equal(uint8_t* a, uint8_t* b)
146{
147 return memcmp(a, b, CLIENT_ID_SIZE) == 0;
148}
149
150int is_timeout(uint64_t time_now, uint64_t timestamp, uint64_t timeout)
151{
152 return timestamp + timeout <= time_now;
153}
154
139/* check if client with client_id is already in list of length length. 155/* check if client with client_id is already in list of length length.
140 * if it is then set its corresponding timestamp to current time. 156 * if it is then set its corresponding timestamp to current time.
141 * if the id is already in the list with a different ip_port, update it. 157 * if the id is already in the list with a different ip_port, update it.
@@ -150,12 +166,11 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_
150 166
151 for(i = 0; i < length; ++i) { 167 for(i = 0; i < length; ++i) {
152 /*If ip_port is assigned to a different client_id replace it*/ 168 /*If ip_port is assigned to a different client_id replace it*/
153 if(list[i].ip_port.ip.i == ip_port.ip.i && 169 if(ipport_equal(list[i].ip_port, ip_port)) {
154 list[i].ip_port.port == ip_port.port) {
155 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); 170 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
156 } 171 }
157 172
158 if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { 173 if(id_equal(list[i].client_id, client_id)) {
159 /* Refresh the client timestamp. */ 174 /* Refresh the client timestamp. */
160 list[i].timestamp = temp_time; 175 list[i].timestamp = temp_time;
161 list[i].ip_port.ip.i = ip_port.ip.i; 176 list[i].ip_port.ip.i = ip_port.ip.i;
@@ -172,10 +187,12 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_
172int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) 187int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
173{ 188{
174 uint32_t i; 189 uint32_t i;
190
175 for(i = 0; i < length; ++i) { 191 for(i = 0; i < length; ++i) {
176 if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) 192 if(id_equal(list[i].client_id, client_id))
177 return 1; 193 return 1;
178 } 194 }
195
179 return 0; 196 return 0;
180} 197}
181 198
@@ -184,10 +201,12 @@ int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id)
184static int friend_number(uint8_t * client_id) 201static int friend_number(uint8_t * client_id)
185{ 202{
186 uint32_t i; 203 uint32_t i;
204
187 for(i = 0; i < num_friends; ++i) { 205 for(i = 0; i < num_friends; ++i) {
188 if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) 206 if(id_equal(friends_list[i].client_id, client_id))
189 return i; 207 return i;
190 } 208 }
209
191 return -1; 210 return -1;
192} 211}
193 212
@@ -199,11 +218,11 @@ static int friend_number(uint8_t * client_id)
199int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) 218int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
200{ 219{
201 uint32_t i, j, k; 220 uint32_t i, j, k;
202 uint64_t temp_time = unix_time(); 221 uint64_t temp_time = unix_time();
203 int num_nodes = 0, closest, tout, inlist; 222 int num_nodes = 0, closest, tout, inlist;
204 223
205 for (i = 0; i < LCLIENT_LIST; ++i) { 224 for (i = 0; i < LCLIENT_LIST; ++i) {
206 tout = close_clientlist[i].timestamp <= temp_time - BAD_NODE_TIMEOUT; 225 tout = is_timeout(temp_time, close_clientlist[i].timestamp, BAD_NODE_TIMEOUT);
207 inlist = client_in_nodelist(nodes_list, MAX_SENT_NODES, close_clientlist[i].client_id); 226 inlist = client_in_nodelist(nodes_list, MAX_SENT_NODES, close_clientlist[i].client_id);
208 227
209 /* if node isn't good or is already in list. */ 228 /* if node isn't good or is already in list. */
@@ -218,7 +237,9 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
218 237
219 nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port; 238 nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port;
220 num_nodes++; 239 num_nodes++;
240
221 } else { 241 } else {
242
222 for(j = 0; j < MAX_SENT_NODES; ++j) { 243 for(j = 0; j < MAX_SENT_NODES; ++j) {
223 closest = id_closest( client_id, 244 closest = id_closest( client_id,
224 nodes_list[j].client_id, 245 nodes_list[j].client_id,
@@ -237,7 +258,8 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list)
237 258
238 for(i = 0; i < num_friends; ++i) { 259 for(i = 0; i < num_friends; ++i) {
239 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 260 for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
240 tout = friends_list[i].client_list[j].timestamp <= temp_time - BAD_NODE_TIMEOUT; 261
262 tout = is_timeout(temp_time, friends_list[i].client_list[j].timestamp, BAD_NODE_TIMEOUT);
241 inlist = client_in_nodelist( nodes_list, 263 inlist = client_in_nodelist( nodes_list,
242 MAX_SENT_NODES, 264 MAX_SENT_NODES,
243 friends_list[i].client_list[j].client_id); 265 friends_list[i].client_list[j].client_id);
@@ -288,7 +310,7 @@ int replace_bad( Client_data * list,
288 uint64_t temp_time = unix_time(); 310 uint64_t temp_time = unix_time();
289 for(i = 0; i < length; ++i) { 311 for(i = 0; i < length; ++i) {
290 /* if node is bad */ 312 /* if node is bad */
291 if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) { 313 if(is_timeout(temp_time, list[i].timestamp, BAD_NODE_TIMEOUT)) {
292 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); 314 memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE);
293 list[i].ip_port = ip_port; 315 list[i].ip_port = ip_port;
294 list[i].timestamp = temp_time; 316 list[i].timestamp = temp_time;
@@ -376,27 +398,23 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient
376 uint32_t i, j; 398 uint32_t i, j;
377 uint64_t temp_time = unix_time(); 399 uint64_t temp_time = unix_time();
378 400
379 if (memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) { 401 if (id_equal(client_id, self_public_key)) {
380 for (i = 0; i < LCLIENT_LIST; ++i) {
381 402
382 if (memcmp( nodeclient_id, 403 for (i = 0; i < LCLIENT_LIST; ++i) {
383 close_clientlist[i].client_id, 404 if (id_equal(nodeclient_id, close_clientlist[i].client_id)) {
384 CLIENT_ID_SIZE ) == 0) {
385 close_clientlist[i].ret_ip_port = ip_port; 405 close_clientlist[i].ret_ip_port = ip_port;
386 close_clientlist[i].ret_timestamp = temp_time; 406 close_clientlist[i].ret_timestamp = temp_time;
387 return; 407 return;
388 } 408 }
389 } 409 }
410
390 } else { 411 } else {
412
391 for (i = 0; i < num_friends; ++i) { 413 for (i = 0; i < num_friends; ++i) {
392 if (memcmp( client_id, 414 if (id_equal(client_id, friends_list[i].client_id)) {
393 friends_list[i].client_id,
394 CLIENT_ID_SIZE ) == 0) {
395 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
396 415
397 if (memcmp( nodeclient_id, 416 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
398 friends_list[i].client_list[j].client_id, 417 if (id_equal(nodeclient_id, friends_list[i].client_list[j].client_id)) {
399 CLIENT_ID_SIZE ) == 0) {
400 friends_list[i].client_list[j].ret_ip_port = ip_port; 418 friends_list[i].client_list[j].ret_ip_port = ip_port;
401 friends_list[i].client_list[j].ret_timestamp = temp_time; 419 friends_list[i].client_list[j].ret_timestamp = temp_time;
402 return; 420 return;
@@ -404,6 +422,7 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient
404 } 422 }
405 } 423 }
406 } 424 }
425
407 } 426 }
408} 427}
409 428
@@ -419,14 +438,15 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id)
419 uint64_t temp_time = unix_time(); 438 uint64_t temp_time = unix_time();
420 439
421 for (i = 0; i < LPING_ARRAY; ++i ) { 440 for (i = 0; i < LPING_ARRAY; ++i ) {
422 if ((pings[i].timestamp + PING_TIMEOUT) > temp_time) { 441 if (!is_timeout(temp_time, pings[i].timestamp, PING_TIMEOUT)) {
423 pinging = 0; 442 pinging = 0;
424 if (ip_port.ip.i != 0 && 443
425 pings[i].ip_port.ip.i == ip_port.ip.i && 444 if (ip_port.ip.i != 0 && ipport_equal(pings[i].ip_port, ip_port))
426 pings[i].ip_port.port == ip_port.port)
427 ++pinging; 445 ++pinging;
446
428 if (ping_id != 0 && pings[i].ping_id == ping_id) 447 if (ping_id != 0 && pings[i].ping_id == ping_id)
429 ++pinging; 448 ++pinging;
449
430 if (pinging == ((ping_id != 0) + (ip_port.ip.i != 0))) 450 if (pinging == ((ping_id != 0) + (ip_port.ip.i != 0)))
431 return 1; 451 return 1;
432 } 452 }
@@ -443,14 +463,15 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
443 uint64_t temp_time = unix_time(); 463 uint64_t temp_time = unix_time();
444 464
445 for(i = 0; i < LSEND_NODES_ARRAY; ++i ) { 465 for(i = 0; i < LSEND_NODES_ARRAY; ++i ) {
446 if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) { 466 if(!is_timeout(temp_time, send_nodes[i].timestamp, PING_TIMEOUT)) {
447 pinging = 0; 467 pinging = 0;
448 if(ip_port.ip.i != 0 && 468
449 send_nodes[i].ip_port.ip.i == ip_port.ip.i && 469 if(ip_port.ip.i != 0 && ipport_equal(send_nodes[i].ip_port, ip_port))
450 send_nodes[i].ip_port.port == ip_port.port)
451 ++pinging; 470 ++pinging;
471
452 if(ping_id != 0 && send_nodes[i].ping_id == ping_id) 472 if(ping_id != 0 && send_nodes[i].ping_id == ping_id)
453 ++pinging; 473 ++pinging;
474
454 if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) 475 if(pinging == (ping_id != 0) + (ip_port.ip.i != 0))
455 return 1; 476 return 1;
456 } 477 }
@@ -471,10 +492,9 @@ uint64_t add_pinging(IP_Port ip_port)
471 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); 492 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
472 uint64_t temp_time = unix_time(); 493 uint64_t temp_time = unix_time();
473 494
474
475 for(i = 0; i < PING_TIMEOUT; ++i ) { 495 for(i = 0; i < PING_TIMEOUT; ++i ) {
476 for(j = 0; j < LPING_ARRAY; ++j ) { 496 for(j = 0; j < LPING_ARRAY; ++j ) {
477 if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) { 497 if(is_timeout(temp_time, pings[j].timestamp, PING_TIMEOUT - i)) {
478 pings[j].timestamp = temp_time; 498 pings[j].timestamp = temp_time;
479 pings[j].ip_port = ip_port; 499 pings[j].ip_port = ip_port;
480 pings[j].ping_id = ping_id; 500 pings[j].ping_id = ping_id;
@@ -495,7 +515,7 @@ uint64_t add_gettingnodes(IP_Port ip_port)
495 515
496 for(i = 0; i < PING_TIMEOUT; ++i ) { 516 for(i = 0; i < PING_TIMEOUT; ++i ) {
497 for(j = 0; j < LSEND_NODES_ARRAY; ++j ) { 517 for(j = 0; j < LSEND_NODES_ARRAY; ++j ) {
498 if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) { 518 if(is_timeout(temp_time, send_nodes[j].timestamp, PING_TIMEOUT - i)) {
499 send_nodes[j].timestamp = temp_time; 519 send_nodes[j].timestamp = temp_time;
500 send_nodes[j].ip_port = ip_port; 520 send_nodes[j].ip_port = ip_port;
501 send_nodes[j].ping_id = ping_id; 521 send_nodes[j].ping_id = ping_id;
@@ -513,8 +533,7 @@ uint64_t add_gettingnodes(IP_Port ip_port)
513static int pingreq(IP_Port ip_port, uint8_t * public_key) 533static int pingreq(IP_Port ip_port, uint8_t * public_key)
514{ 534{
515 /* check if packet is gonna be sent to ourself */ 535 /* check if packet is gonna be sent to ourself */
516 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0 536 if(id_equal(public_key, self_public_key) || is_pinging(ip_port, 0))
517 || is_pinging(ip_port, 0))
518 return 1; 537 return 1;
519 538
520 uint64_t ping_id = add_pinging(ip_port); 539 uint64_t ping_id = add_pinging(ip_port);
@@ -548,7 +567,7 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key)
548static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) 567static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
549{ 568{
550 /* check if packet is gonna be sent to ourself */ 569 /* check if packet is gonna be sent to ourself */
551 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) 570 if(id_equal(public_key, self_public_key))
552 return 1; 571 return 1;
553 572
554 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING]; 573 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING];
@@ -577,8 +596,7 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id)
577static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) 596static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
578{ 597{
579 /* check if packet is gonna be sent to ourself */ 598 /* check if packet is gonna be sent to ourself */
580 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0 599 if(id_equal(public_key, self_public_key) || is_gettingnodes(ip_port, 0))
581 || is_gettingnodes(ip_port, 0))
582 return 1; 600 return 1;
583 601
584 uint64_t ping_id = add_gettingnodes(ip_port); 602 uint64_t ping_id = add_gettingnodes(ip_port);
@@ -617,7 +635,7 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id)
617static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id) 635static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id)
618{ 636{
619 /* check if packet is gonna be sent to ourself */ 637 /* check if packet is gonna be sent to ourself */
620 if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) 638 if(id_equal(public_key, self_public_key))
621 return 1; 639 return 1;
622 640
623 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) 641 uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
@@ -665,7 +683,7 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source)
665 return 1; 683 return 1;
666 684
667 /* check if packet is from ourself. */ 685 /* check if packet is from ourself. */
668 if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) 686 if(id_equal(packet + 1, self_public_key))
669 return 1; 687 return 1;
670 688
671 int len = decrypt_data( packet + 1, 689 int len = decrypt_data( packet + 1,
@@ -691,7 +709,7 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source)
691 return 1; 709 return 1;
692 710
693 /* check if packet is from ourself. */ 711 /* check if packet is from ourself. */
694 if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) 712 if(id_equal(packet + 1, self_public_key))
695 return 1; 713 return 1;
696 714
697 int len = decrypt_data( packet + 1, 715 int len = decrypt_data( packet + 1,
@@ -720,7 +738,7 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
720 return 1; 738 return 1;
721 739
722 /* check if packet is from ourself. */ 740 /* check if packet is from ourself. */
723 if (memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) 741 if (id_equal(packet + 1, self_public_key))
724 return 1; 742 return 1;
725 743
726 uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; 744 uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE];
@@ -810,16 +828,20 @@ int DHT_delfriend(uint8_t * client_id)
810 Friend * temp; 828 Friend * temp;
811 for (i = 0; i < num_friends; ++i) { 829 for (i = 0; i < num_friends; ++i) {
812 /* Equal */ 830 /* Equal */
813 if (memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { 831 if (id_equal(friends_list[i].client_id, client_id)) {
814 --num_friends; 832 --num_friends;
833
815 if (num_friends != i) { 834 if (num_friends != i) {
816 memcpy( friends_list[i].client_id, 835 memcpy( friends_list[i].client_id,
817 friends_list[num_friends].client_id, 836 friends_list[num_friends].client_id,
818 CLIENT_ID_SIZE ); 837 CLIENT_ID_SIZE );
819 } 838 }
839
820 temp = realloc(friends_list, sizeof(Friend) * (num_friends)); 840 temp = realloc(friends_list, sizeof(Friend) * (num_friends));
821 if (temp != NULL) 841 if (temp == NULL)
822 friends_list = temp; 842 return 1;
843
844 friends_list = temp;
823 return 0; 845 return 0;
824 } 846 }
825 } 847 }
@@ -836,12 +858,9 @@ IP_Port DHT_getfriendip(uint8_t * client_id)
836 858
837 for (i = 0; i < num_friends; ++i) { 859 for (i = 0; i < num_friends; ++i) {
838 /* Equal */ 860 /* Equal */
839 if (memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { 861 if (id_equal(friends_list[i].client_id, client_id)) {
840 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 862 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
841 if (memcmp( friends_list[i].client_list[j].client_id, 863 if (id_equal(friends_list[i].client_list[j].client_id, client_id) && !is_timeout(temp_time, friends_list[i].client_list[j].timestamp, BAD_NODE_TIMEOUT))
842 client_id,
843 CLIENT_ID_SIZE ) == 0 &&
844 friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time)
845 return friends_list[i].client_list[j].ip_port; 864 return friends_list[i].client_list[j].ip_port;
846 } 865 }
847 return empty; 866 return empty;
@@ -865,14 +884,14 @@ void doDHTFriends()
865 uint32_t num_nodes = 0; 884 uint32_t num_nodes = 0;
866 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { 885 for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) {
867 /* if node is not dead. */ 886 /* if node is not dead. */
868 if (friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) { 887 if (!is_timeout(temp_time, friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) {
869 if ((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { 888 if ((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) {
870 pingreq( friends_list[i].client_list[j].ip_port, 889 pingreq( friends_list[i].client_list[j].ip_port,
871 friends_list[i].client_list[j].client_id ); 890 friends_list[i].client_list[j].client_id );
872 friends_list[i].client_list[j].last_pinged = temp_time; 891 friends_list[i].client_list[j].last_pinged = temp_time;
873 } 892 }
874 /* if node is good. */ 893 /* if node is good. */
875 if (friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) { 894 if (!is_timeout(temp_time, friends_list[i].client_list[j].timestamp, BAD_NODE_TIMEOUT)) {
876 index[num_nodes] = j; 895 index[num_nodes] = j;
877 ++num_nodes; 896 ++num_nodes;
878 } 897 }
@@ -903,14 +922,14 @@ void doClose()
903 922
904 for (i = 0; i < LCLIENT_LIST; ++i) { 923 for (i = 0; i < LCLIENT_LIST; ++i) {
905 /* if node is not dead. */ 924 /* if node is not dead. */
906 if (close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) { 925 if (!is_timeout(temp_time, close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) {
907 if ((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) { 926 if ((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) {
908 pingreq( close_clientlist[i].ip_port, 927 pingreq( close_clientlist[i].ip_port,
909 close_clientlist[i].client_id ); 928 close_clientlist[i].client_id );
910 close_clientlist[i].last_pinged = temp_time; 929 close_clientlist[i].last_pinged = temp_time;
911 } 930 }
912 /* if node is good. */ 931 /* if node is good. */
913 if (close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) { 932 if (!is_timeout(temp_time, close_clientlist[i].timestamp, BAD_NODE_TIMEOUT)) {
914 index[num_nodes] = i; 933 index[num_nodes] = i;
915 ++num_nodes; 934 ++num_nodes;
916 } 935 }
@@ -937,10 +956,12 @@ void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key)
937int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length) 956int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length)
938{ 957{
939 uint32_t i; 958 uint32_t i;
959
940 for (i = 0; i < LCLIENT_LIST; ++i) { 960 for (i = 0; i < LCLIENT_LIST; ++i) {
941 if (memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) 961 if (id_equal(client_id, close_clientlist[i].client_id))
942 return sendpacket(close_clientlist[i].ip_port, packet, length); 962 return sendpacket(close_clientlist[i].ip_port, packet, length);
943 } 963 }
964
944 return -1; 965 return -1;
945} 966}
946 967
@@ -966,10 +987,9 @@ static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num)
966 client = &friend->client_list[i]; 987 client = &friend->client_list[i];
967 988
968 /*If ip is not zero and node is good */ 989 /*If ip is not zero and node is good */
969 if (client->ret_ip_port.ip.i != 0 && 990 if (client->ret_ip_port.ip.i != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) {
970 client->ret_timestamp + BAD_NODE_TIMEOUT > temp_time) {
971 991
972 if (memcmp(client->client_id, friend->client_id, CLIENT_ID_SIZE) == 0) 992 if (id_equal(client->client_id, friend->client_id))
973 return 0; 993 return 0;
974 994
975 ip_portlist[num_ips] = client->ret_ip_port; 995 ip_portlist[num_ips] = client->ret_ip_port;
@@ -997,8 +1017,7 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length)
997 client = &friend->client_list[i]; 1017 client = &friend->client_list[i];
998 1018
999 /*If ip is not zero and node is good */ 1019 /*If ip is not zero and node is good */
1000 if (client->ret_ip_port.ip.i != 0 && 1020 if (client->ret_ip_port.ip.i != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) {
1001 client->ret_timestamp + BAD_NODE_TIMEOUT > temp_time) {
1002 1021
1003 if (sendpacket(client->ip_port, packet, length) == length) 1022 if (sendpacket(client->ip_port, packet, length) == length)
1004 ++sent; 1023 ++sent;
@@ -1028,8 +1047,7 @@ int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length)
1028 client = &friend->client_list[i]; 1047 client = &friend->client_list[i];
1029 1048
1030 /*If ip is not zero and node is good */ 1049 /*If ip is not zero and node is good */
1031 if(client->ret_ip_port.ip.i != 0 && 1050 if(client->ret_ip_port.ip.i != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) {
1032 client->ret_timestamp + BAD_NODE_TIMEOUT > temp_time) {
1033 ip_list[n] = client->ip_port; 1051 ip_list[n] = client->ip_port;
1034 ++n; 1052 ++n;
1035 } 1053 }
@@ -1052,7 +1070,7 @@ int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id)
1052 uint32_t i; 1070 uint32_t i;
1053 for (i = 0; i < num_friends; ++i) { 1071 for (i = 0; i < num_friends; ++i) {
1054 /* Equal */ 1072 /* Equal */
1055 if (memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) 1073 if (id_equal(friends_list[i].client_id, friend_id))
1056 return friend_iplist(ip_portlist, i); 1074 return friend_iplist(ip_portlist, i);
1057 } 1075 }
1058 return -1; 1076 return -1;
@@ -1094,7 +1112,7 @@ int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source)
1094 return 1; 1112 return 1;
1095 1113
1096 /* check if request is for us. */ 1114 /* check if request is for us. */
1097 if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { 1115 if (id_equal(packet + 1, self_public_key)) {
1098 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; 1116 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
1099 uint8_t data[MAX_DATA_SIZE]; 1117 uint8_t data[MAX_DATA_SIZE];
1100 1118
@@ -1329,7 +1347,7 @@ int DHT_isconnected()
1329 uint64_t temp_time = unix_time(); 1347 uint64_t temp_time = unix_time();
1330 1348
1331 for(i = 0; i < LCLIENT_LIST; ++i) { 1349 for(i = 0; i < LCLIENT_LIST; ++i) {
1332 if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) 1350 if(!is_timeout(temp_time, close_clientlist[i].timestamp, BAD_NODE_TIMEOUT))
1333 return 1; 1351 return 1;
1334 } 1352 }
1335 return 0; 1353 return 0;