diff options
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r-- | toxcore/DHT.c | 387 |
1 files changed, 81 insertions, 306 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 470826f0..ee51f16c 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -131,12 +131,9 @@ static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id | |||
131 | uint64_t temp_time = unix_time(); | 131 | uint64_t temp_time = unix_time(); |
132 | 132 | ||
133 | for (i = 0; i < length; i++) | 133 | for (i = 0; i < length; i++) |
134 | #ifdef CLIENT_ONETOONE_IP /* Dead nodes are considered dead (not in the list)*/ | 134 | /* Dead nodes are considered dead (not in the list)*/ |
135 | if (!is_timeout(temp_time, list[i].assoc.timestamp, KILL_NODE_TIMEOUT)) | ||
136 | #else | ||
137 | if (!is_timeout(temp_time, list[i].assoc4.timestamp, KILL_NODE_TIMEOUT) || | 135 | if (!is_timeout(temp_time, list[i].assoc4.timestamp, KILL_NODE_TIMEOUT) || |
138 | !is_timeout(temp_time, list[i].assoc6.timestamp, KILL_NODE_TIMEOUT)) | 136 | !is_timeout(temp_time, list[i].assoc6.timestamp, KILL_NODE_TIMEOUT)) |
139 | #endif | ||
140 | if (id_equal(list[i].client_id, client_id)) | 137 | if (id_equal(list[i].client_id, client_id)) |
141 | return 1; | 138 | return 1; |
142 | 139 | ||
@@ -155,41 +152,12 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t | |||
155 | uint32_t i; | 152 | uint32_t i; |
156 | uint64_t temp_time = unix_time(); | 153 | uint64_t temp_time = unix_time(); |
157 | 154 | ||
158 | #ifdef CLIENT_ONETOONE_IP | ||
159 | uint8_t candropipv4 = 1; | ||
160 | |||
161 | if (ip_port.ip.family == AF_INET6) { | ||
162 | uint8_t ipv6cnt = 0; | ||
163 | |||
164 | /* ipv6: count how many spots are used */ | ||
165 | for (i = 0; i < length; i++) | ||
166 | if (list[i].assoc.ip_port.ip.family == AF_INET6) | ||
167 | ipv6cnt++; | ||
168 | |||
169 | /* more than half the list filled with ipv6: block ipv4->ipv6 overwrite */ | ||
170 | if (ipv6cnt > length / 2) | ||
171 | candropipv4 = 0; | ||
172 | } | ||
173 | |||
174 | #endif | ||
175 | |||
176 | /* if client_id is in list, find it and maybe overwrite ip_port */ | 155 | /* if client_id is in list, find it and maybe overwrite ip_port */ |
177 | for (i = 0; i < length; ++i) | 156 | for (i = 0; i < length; ++i) |
178 | if (id_equal(list[i].client_id, client_id)) { | 157 | if (id_equal(list[i].client_id, client_id)) { |
179 | /* Refresh the client timestamp. */ | 158 | /* Refresh the client timestamp. */ |
180 | #ifdef CLIENT_ONETOONE_IP | ||
181 | /* if we got "too many" ipv6 addresses already, keep the ipv4 address */ | ||
182 | if (!candropipv4 && (list[i].assoc.ip_port.ip.family == AF_INET)) | ||
183 | return 1; | ||
184 | |||
185 | if (LAN_ip(list[i].assoc.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) | ||
186 | return 1; | ||
187 | |||
188 | list[i].assoc.ip_port = ip_port; | ||
189 | list[i].assoc.timestamp = temp_time; | ||
190 | #else | ||
191 | |||
192 | if (ip_port.ip.family == AF_INET) { | 159 | if (ip_port.ip.family == AF_INET) { |
160 | |||
193 | #ifdef LOGGING | 161 | #ifdef LOGGING |
194 | 162 | ||
195 | if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { | 163 | if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { |
@@ -209,6 +177,7 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t | |||
209 | list[i].assoc4.ip_port = ip_port; | 177 | list[i].assoc4.ip_port = ip_port; |
210 | list[i].assoc4.timestamp = temp_time; | 178 | list[i].assoc4.timestamp = temp_time; |
211 | } else if (ip_port.ip.family == AF_INET6) { | 179 | } else if (ip_port.ip.family == AF_INET6) { |
180 | |||
212 | #ifdef LOGGING | 181 | #ifdef LOGGING |
213 | 182 | ||
214 | if (!ipport_equal(&list[i].assoc6.ip_port, &ip_port)) { | 183 | if (!ipport_equal(&list[i].assoc6.ip_port, &ip_port)) { |
@@ -229,7 +198,6 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t | |||
229 | list[i].assoc6.timestamp = temp_time; | 198 | list[i].assoc6.timestamp = temp_time; |
230 | } | 199 | } |
231 | 200 | ||
232 | #endif | ||
233 | return 1; | 201 | return 1; |
234 | } | 202 | } |
235 | 203 | ||
@@ -238,21 +206,6 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t | |||
238 | * TODO: maybe we SHOULDN'T do that if that client_id is in a friend_list | 206 | * TODO: maybe we SHOULDN'T do that if that client_id is in a friend_list |
239 | * and the one who is the actual friend's client_id/address set? */ | 207 | * and the one who is the actual friend's client_id/address set? */ |
240 | for (i = 0; i < length; ++i) { | 208 | for (i = 0; i < length; ++i) { |
241 | #ifdef CLIENT_ONETOONE_IP | ||
242 | |||
243 | if (ipport_equal(&list[i].assoc.ip_port, &ip_port)) { | ||
244 | /* Initialize client timestamp. */ | ||
245 | list[i].assoc.timestamp = temp_time; | ||
246 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | ||
247 | #ifdef LOGGING | ||
248 | sprintf(logbuffer, "coipil[%u]: switching client_id\n", i); | ||
249 | loglog(logbuffer); | ||
250 | #endif | ||
251 | return 1; | ||
252 | } | ||
253 | |||
254 | #else | ||
255 | |||
256 | /* MAYBE: check the other address, if valid, don't nuke? */ | 209 | /* MAYBE: check the other address, if valid, don't nuke? */ |
257 | if ((ip_port.ip.family == AF_INET) && ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { | 210 | if ((ip_port.ip.family == AF_INET) && ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { |
258 | /* Initialize client timestamp. */ | 211 | /* Initialize client timestamp. */ |
@@ -277,8 +230,6 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t | |||
277 | memset(&list[i].assoc4, 0, sizeof(list[i].assoc4)); | 230 | memset(&list[i].assoc4, 0, sizeof(list[i].assoc4)); |
278 | return 1; | 231 | return 1; |
279 | } | 232 | } |
280 | |||
281 | #endif | ||
282 | } | 233 | } |
283 | 234 | ||
284 | return 0; | 235 | return 0; |
@@ -338,22 +289,15 @@ static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nod | |||
338 | continue; | 289 | continue; |
339 | 290 | ||
340 | IPPTsPng *ipptp = NULL; | 291 | IPPTsPng *ipptp = NULL; |
341 | #ifdef CLIENT_ONETOONE_IP | ||
342 | ipptp = &client->assoc; | ||
343 | #else | ||
344 | |||
345 | if (sa_family == AF_INET) | 292 | if (sa_family == AF_INET) |
346 | ipptp = &client->assoc4; | 293 | ipptp = &client->assoc4; |
347 | else | 294 | else |
348 | ipptp = &client->assoc6; | 295 | ipptp = &client->assoc6; |
349 | 296 | ||
350 | #endif | ||
351 | |||
352 | /* node not in a good condition? */ | 297 | /* node not in a good condition? */ |
353 | if (is_timeout(timestamp, ipptp->timestamp, BAD_NODE_TIMEOUT)) | 298 | if (is_timeout(timestamp, ipptp->timestamp, BAD_NODE_TIMEOUT)) |
354 | continue; | 299 | continue; |
355 | 300 | ||
356 | #ifdef TOX_ENABLE_IPV6 | ||
357 | IP *client_ip = &ipptp->ip_port.ip; | 301 | IP *client_ip = &ipptp->ip_port.ip; |
358 | 302 | ||
359 | /* | 303 | /* |
@@ -373,9 +317,6 @@ static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nod | |||
373 | } | 317 | } |
374 | 318 | ||
375 | ipv46x = !(sa_family == ip_treat_as_family); | 319 | ipv46x = !(sa_family == ip_treat_as_family); |
376 | #else | ||
377 | ipv46x = !(sa_family == AF_INET); | ||
378 | #endif | ||
379 | 320 | ||
380 | /* node address of the wrong family? */ | 321 | /* node address of the wrong family? */ |
381 | if (ipv46x) | 322 | if (ipv46x) |
@@ -454,43 +395,17 @@ static int replace_bad( Client_data *list, | |||
454 | uint32_t i; | 395 | uint32_t i; |
455 | uint64_t temp_time = unix_time(); | 396 | uint64_t temp_time = unix_time(); |
456 | 397 | ||
457 | #ifdef CLIENT_ONETOONE_IP | ||
458 | uint8_t candropipv4 = 1; | ||
459 | |||
460 | if (ip_port.ip.family == AF_INET6) { | ||
461 | uint32_t ipv6cnt = 0; | ||
462 | |||
463 | /* ipv6: count how many spots are used */ | ||
464 | for (i = 0; i < length; i++) | ||
465 | if (list[i].assoc.ip_port.ip.family == AF_INET6) | ||
466 | ipv6cnt++; | ||
467 | |||
468 | /* more than half the list filled with ipv6: block ipv4->ipv6 overwrite */ | ||
469 | if (ipv6cnt > length / 2) | ||
470 | candropipv4 = 0; | ||
471 | } | ||
472 | |||
473 | #endif | ||
474 | |||
475 | for (i = 0; i < length; ++i) { | 398 | for (i = 0; i < length; ++i) { |
476 | /* If node is bad */ | 399 | /* If node is bad */ |
477 | Client_data *client = &list[i]; | 400 | Client_data *client = &list[i]; |
478 | IPPTsPng *ipptp = NULL; | 401 | IPPTsPng *ipptp = NULL; |
479 | 402 | ||
480 | #ifdef CLIENT_ONETOONE_IP | ||
481 | ipptp = &client->assoc; | ||
482 | |||
483 | if ((candropipv4 || (ipptp->ip_port.ip.family == AF_INET6)) && | ||
484 | is_timeout(temp_time, ipptp->timestamp, BAD_NODE_TIMEOUT)) { | ||
485 | #else | ||
486 | |||
487 | if (ip_port.ip.family == AF_INET) | 403 | if (ip_port.ip.family == AF_INET) |
488 | ipptp = &client->assoc4; | 404 | ipptp = &client->assoc4; |
489 | else | 405 | else |
490 | ipptp = &client->assoc6; | 406 | ipptp = &client->assoc6; |
491 | 407 | ||
492 | if (is_timeout(temp_time, ipptp->timestamp, BAD_NODE_TIMEOUT)) { | 408 | if (is_timeout(temp_time, ipptp->timestamp, BAD_NODE_TIMEOUT)) { |
493 | #endif | ||
494 | memcpy(client->client_id, client_id, CLIENT_ID_SIZE); | 409 | memcpy(client->client_id, client_id, CLIENT_ID_SIZE); |
495 | ipptp->ip_port = ip_port; | 410 | ipptp->ip_port = ip_port; |
496 | ipptp->timestamp = temp_time; | 411 | ipptp->timestamp = temp_time; |
@@ -542,60 +457,17 @@ static int replace_good( Client_data *list, | |||
542 | 457 | ||
543 | int8_t replace = -1; | 458 | int8_t replace = -1; |
544 | 459 | ||
545 | #ifdef CLIENT_ONETOONE_IP | 460 | /* Because the list is sorted, we can simply check the client_id at the |
546 | uint8_t candropipv4 = 1; | 461 | * border, either it is closer, then every other one is as well, or it is |
547 | 462 | * further, then it gets pushed out in favor of the new address, which | |
548 | if (ip_port.ip.family == AF_INET6) { | 463 | * will with the next sort() move to its "rightful" position |
549 | uint32_t i, ipv6cnt = 0; | 464 | * |
550 | 465 | * CAVEAT: weirdly enough, the list is sorted DESCENDING in distance | |
551 | /* ipv6: count how many spots are used */ | 466 | * so the furthest element is the first, NOT the last (at least that's |
552 | for (i = 0; i < length; i++) | 467 | * what the comment above sort_list() claims) |
553 | if (list[i].assoc.ip_port.ip.family == AF_INET6) | 468 | */ |
554 | ipv6cnt++; | 469 | if (id_closest(comp_client_id, list[0].client_id, client_id) == 2) |
555 | 470 | replace = 0; | |
556 | /* more than half the list filled with ipv6: block ipv4->ipv6 overwrite */ | ||
557 | if (ipv6cnt > length / 2) | ||
558 | candropipv4 = 0; | ||
559 | } | ||
560 | |||
561 | uint32_t i; | ||
562 | |||
563 | if (candropipv4) { | ||
564 | /* either we got an ipv4 address, or we're "allowed" to push out an ipv4 | ||
565 | * address in favor of an ipv6 one | ||
566 | * | ||
567 | * because the list is sorted, we can simply check the client_id at the | ||
568 | * border, either it is closer, then every other one is as well, or it is | ||
569 | * further, then it gets pushed out in favor of the new address, which | ||
570 | * will with the next sort() move to its "rightful" position | ||
571 | * | ||
572 | * CAVEAT: weirdly enough, the list is sorted DESCENDING in distance | ||
573 | * so the furthest element is the first, NOT the last (at least that's | ||
574 | * what the comment above sort_list() claims) | ||
575 | */ | ||
576 | #endif | ||
577 | if (id_closest(comp_client_id, list[0].client_id, client_id) == 2) | ||
578 | replace = 0; | ||
579 | |||
580 | #ifdef CLIENT_ONETOONE_IP | ||
581 | } else { | ||
582 | /* ipv6 case without a right to push out an ipv4: only look for ipv6 | ||
583 | * addresses, the first one we find is either closer (then we can skip | ||
584 | * out like above) or further (then we can replace it, like above) | ||
585 | */ | ||
586 | for (i = 0; i < length; i++) { | ||
587 | Client_data *client = &list[i]; | ||
588 | |||
589 | if (client->assoc.ip_port.ip.family == AF_INET6) { | ||
590 | if (id_closest(comp_client_id, list[i].client_id, client_id) == 2) | ||
591 | replace = i; | ||
592 | |||
593 | break; | ||
594 | } | ||
595 | } | ||
596 | } | ||
597 | |||
598 | #endif | ||
599 | 471 | ||
600 | if (replace != -1) { | 472 | if (replace != -1) { |
601 | #ifdef DEBUG | 473 | #ifdef DEBUG |
@@ -603,16 +475,12 @@ static int replace_good( Client_data *list, | |||
603 | #endif | 475 | #endif |
604 | Client_data *client = &list[replace]; | 476 | Client_data *client = &list[replace]; |
605 | IPPTsPng *ipptp = NULL; | 477 | IPPTsPng *ipptp = NULL; |
606 | #ifdef CLIENT_ONETOONE_IP | ||
607 | ipptp = &client->assoc; | ||
608 | #else | ||
609 | 478 | ||
610 | if (ip_port.ip.family == AF_INET) | 479 | if (ip_port.ip.family == AF_INET) |
611 | ipptp = &client->assoc4; | 480 | ipptp = &client->assoc4; |
612 | else | 481 | else |
613 | ipptp = &client->assoc6; | 482 | ipptp = &client->assoc6; |
614 | 483 | ||
615 | #endif | ||
616 | memcpy(client->client_id, client_id, CLIENT_ID_SIZE); | 484 | memcpy(client->client_id, client_id, CLIENT_ID_SIZE); |
617 | ipptp->ip_port = ip_port; | 485 | ipptp->ip_port = ip_port; |
618 | ipptp->timestamp = unix_time(); | 486 | ipptp->timestamp = unix_time(); |
@@ -681,11 +549,6 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint | |||
681 | if (id_equal(client_id, dht->c->self_public_key)) { | 549 | if (id_equal(client_id, dht->c->self_public_key)) { |
682 | for (i = 0; i < LCLIENT_LIST; ++i) { | 550 | for (i = 0; i < LCLIENT_LIST; ++i) { |
683 | if (id_equal(nodeclient_id, dht->close_clientlist[i].client_id)) { | 551 | if (id_equal(nodeclient_id, dht->close_clientlist[i].client_id)) { |
684 | #ifdef CLIENT_ONETOONE_IP | ||
685 | dht->close_clientlist[i].assoc.ret_ip_port = ip_port; | ||
686 | dht->close_clientlist[i].assoc.ret_timestamp = temp_time; | ||
687 | #else | ||
688 | |||
689 | if (ip_port.ip.family == AF_INET) { | 552 | if (ip_port.ip.family == AF_INET) { |
690 | dht->close_clientlist[i].assoc4.ret_ip_port = ip_port; | 553 | dht->close_clientlist[i].assoc4.ret_ip_port = ip_port; |
691 | dht->close_clientlist[i].assoc4.ret_timestamp = temp_time; | 554 | dht->close_clientlist[i].assoc4.ret_timestamp = temp_time; |
@@ -694,7 +557,6 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint | |||
694 | dht->close_clientlist[i].assoc6.ret_timestamp = temp_time; | 557 | dht->close_clientlist[i].assoc6.ret_timestamp = temp_time; |
695 | } | 558 | } |
696 | 559 | ||
697 | #endif | ||
698 | return; | 560 | return; |
699 | } | 561 | } |
700 | } | 562 | } |
@@ -704,11 +566,6 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint | |||
704 | if (id_equal(client_id, dht->friends_list[i].client_id)) { | 566 | if (id_equal(client_id, dht->friends_list[i].client_id)) { |
705 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 567 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
706 | if (id_equal(nodeclient_id, dht->friends_list[i].client_list[j].client_id)) { | 568 | if (id_equal(nodeclient_id, dht->friends_list[i].client_list[j].client_id)) { |
707 | #ifdef CLIENT_ONETOONE_IP | ||
708 | dht->friends_list[i].client_list[j].assoc.ret_ip_port = ip_port; | ||
709 | dht->friends_list[i].client_list[j].assoc.ret_timestamp = temp_time; | ||
710 | #else | ||
711 | |||
712 | if (ip_port.ip.family == AF_INET) { | 569 | if (ip_port.ip.family == AF_INET) { |
713 | dht->friends_list[i].client_list[j].assoc4.ret_ip_port = ip_port; | 570 | dht->friends_list[i].client_list[j].assoc4.ret_ip_port = ip_port; |
714 | dht->friends_list[i].client_list[j].assoc4.ret_timestamp = temp_time; | 571 | dht->friends_list[i].client_list[j].assoc4.ret_timestamp = temp_time; |
@@ -717,7 +574,6 @@ static void returnedip_ports(DHT *dht, IP_Port ip_port, uint8_t *client_id, uint | |||
717 | dht->friends_list[i].client_list[j].assoc6.ret_timestamp = temp_time; | 574 | dht->friends_list[i].client_list[j].assoc6.ret_timestamp = temp_time; |
718 | } | 575 | } |
719 | 576 | ||
720 | #endif | ||
721 | return; | 577 | return; |
722 | } | 578 | } |
723 | } | 579 | } |
@@ -836,7 +692,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl | |||
836 | new_nonce(nonce); | 692 | new_nonce(nonce); |
837 | 693 | ||
838 | memcpy(plain, &ping_id, sizeof(ping_id)); | 694 | memcpy(plain, &ping_id, sizeof(ping_id)); |
839 | #ifdef TOX_ENABLE_IPV6 | 695 | |
840 | Node4_format *nodes4_list = (Node4_format *)(plain + sizeof(ping_id)); | 696 | Node4_format *nodes4_list = (Node4_format *)(plain + sizeof(ping_id)); |
841 | int i, num_nodes_ok = 0; | 697 | int i, num_nodes_ok = 0; |
842 | 698 | ||
@@ -862,10 +718,6 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl | |||
862 | num_nodes = num_nodes_ok; | 718 | num_nodes = num_nodes_ok; |
863 | } | 719 | } |
864 | 720 | ||
865 | #else | ||
866 | memcpy(plain + sizeof(ping_id), nodes_list, num_nodes * Node4_format_size); | ||
867 | #endif | ||
868 | |||
869 | int len = encrypt_data( public_key, | 721 | int len = encrypt_data( public_key, |
870 | dht->c->self_secret_key, | 722 | dht->c->self_secret_key, |
871 | nonce, | 723 | nonce, |
@@ -887,7 +739,6 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl | |||
887 | return sendpacket(dht->c->lossless_udp->net, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); | 739 | return sendpacket(dht->c->lossless_udp->net, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); |
888 | } | 740 | } |
889 | 741 | ||
890 | #ifdef TOX_ENABLE_IPV6 | ||
891 | /* Send a send nodes response: message for IPv6 nodes */ | 742 | /* Send a send nodes response: message for IPv6 nodes */ |
892 | static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id) | 743 | static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id) |
893 | { | 744 | { |
@@ -933,7 +784,6 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_ | |||
933 | 784 | ||
934 | return sendpacket(dht->c->lossless_udp->net, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); | 785 | return sendpacket(dht->c->lossless_udp->net, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); |
935 | } | 786 | } |
936 | #endif | ||
937 | 787 | ||
938 | static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length) | 788 | static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
939 | { | 789 | { |
@@ -962,10 +812,8 @@ static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32 | |||
962 | 812 | ||
963 | memcpy(&ping_id, plain, sizeof(ping_id)); | 813 | memcpy(&ping_id, plain, sizeof(ping_id)); |
964 | sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); | 814 | sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); |
965 | #ifdef TOX_ENABLE_IPV6 | ||
966 | sendnodes_ipv6(dht, source, packet + 1, plain + sizeof(ping_id), | 815 | sendnodes_ipv6(dht, source, packet + 1, plain + sizeof(ping_id), |
967 | ping_id); /* TODO: prevent possible amplification attacks */ | 816 | ping_id); /* TODO: prevent possible amplification attacks */ |
968 | #endif | ||
969 | 817 | ||
970 | add_toping(dht->ping, packet + 1, source); | 818 | add_toping(dht->ping, packet + 1, source); |
971 | //send_ping_request(dht, source, packet + 1); /* TODO: make this smarter? */ | 819 | //send_ping_request(dht, source, packet + 1); /* TODO: make this smarter? */ |
@@ -1005,14 +853,11 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3 | |||
1005 | if (!is_gettingnodes(dht, source, ping_id)) | 853 | if (!is_gettingnodes(dht, source, ping_id)) |
1006 | return 1; | 854 | return 1; |
1007 | 855 | ||
1008 | uint32_t i; | ||
1009 | Node_format nodes_list[MAX_SENT_NODES]; | ||
1010 | |||
1011 | #ifdef TOX_ENABLE_IPV6 | ||
1012 | Node4_format *nodes4_list = (Node4_format *)(plain + sizeof(ping_id)); | 856 | Node4_format *nodes4_list = (Node4_format *)(plain + sizeof(ping_id)); |
857 | Node_format nodes_list[MAX_SENT_NODES]; | ||
858 | uint32_t i, num_nodes_ok = 0; | ||
1013 | 859 | ||
1014 | uint32_t num_nodes_ok = 0; | 860 | /* blow up from Node4 (IPv4) wire format to Node (IPv4/IPv6) structure */ |
1015 | |||
1016 | for (i = 0; i < num_nodes; i++) | 861 | for (i = 0; i < num_nodes; i++) |
1017 | if ((nodes4_list[i].ip_port.ip.uint32 != 0) && (nodes4_list[i].ip_port.ip.uint32 != (uint32_t)~0)) { | 862 | if ((nodes4_list[i].ip_port.ip.uint32 != 0) && (nodes4_list[i].ip_port.ip.uint32 != (uint32_t)~0)) { |
1018 | memcpy(nodes_list[num_nodes_ok].client_id, nodes4_list[i].client_id, CLIENT_ID_SIZE); | 863 | memcpy(nodes_list[num_nodes_ok].client_id, nodes4_list[i].client_id, CLIENT_ID_SIZE); |
@@ -1028,10 +873,6 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3 | |||
1028 | num_nodes = num_nodes_ok; | 873 | num_nodes = num_nodes_ok; |
1029 | } | 874 | } |
1030 | 875 | ||
1031 | #else | ||
1032 | memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format)); | ||
1033 | #endif | ||
1034 | |||
1035 | addto_lists(dht, source, packet + 1); | 876 | addto_lists(dht, source, packet + 1); |
1036 | 877 | ||
1037 | for (i = 0; i < num_nodes; ++i) { | 878 | for (i = 0; i < num_nodes; ++i) { |
@@ -1042,7 +883,6 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3 | |||
1042 | return 0; | 883 | return 0; |
1043 | } | 884 | } |
1044 | 885 | ||
1045 | #ifdef TOX_ENABLE_IPV6 | ||
1046 | static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, uint32_t length) | 886 | static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, uint32_t length) |
1047 | { | 887 | { |
1048 | DHT *dht = object; | 888 | DHT *dht = object; |
@@ -1088,7 +928,6 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet, | |||
1088 | 928 | ||
1089 | return 0; | 929 | return 0; |
1090 | } | 930 | } |
1091 | #endif | ||
1092 | 931 | ||
1093 | /*----------------------------------------------------------------------------------*/ | 932 | /*----------------------------------------------------------------------------------*/ |
1094 | /*------------------------END of packet handling functions--------------------------*/ | 933 | /*------------------------END of packet handling functions--------------------------*/ |
@@ -1103,21 +942,17 @@ static void get_bunchnodes(DHT *dht, Client_data *list, uint16_t length, uint16_ | |||
1103 | 942 | ||
1104 | for (i = 0; i < length; ++i) { | 943 | for (i = 0; i < length; ++i) { |
1105 | IPPTsPng *assoc; | 944 | IPPTsPng *assoc; |
1106 | #ifdef CLIENT_ONETOONE_IP | ||
1107 | assoc = &list[i].assoc; | ||
1108 | #else | ||
1109 | uint32_t a; | 945 | uint32_t a; |
1110 | 946 | ||
1111 | for (a = 0, assoc = &list[i].assoc6; a < 2; a++, assoc = &list[i].assoc4) | 947 | for (a = 0, assoc = &list[i].assoc6; a < 2; a++, assoc = &list[i].assoc4) |
1112 | #endif | 948 | if (ipport_isset(&(assoc->ip_port)) && |
1113 | if (ipport_isset(&(assoc->ip_port)) && | 949 | !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { |
1114 | !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { | 950 | getnodes(dht, assoc->ip_port, list[i].client_id, client_id); |
1115 | getnodes(dht, assoc->ip_port, list[i].client_id, client_id); | 951 | ++num; |
1116 | ++num; | ||
1117 | 952 | ||
1118 | if (num >= max_num) | 953 | if (num >= max_num) |
1119 | return; | 954 | return; |
1120 | } | 955 | } |
1121 | } | 956 | } |
1122 | } | 957 | } |
1123 | 958 | ||
@@ -1194,17 +1029,13 @@ int DHT_getfriendip(DHT *dht, uint8_t *client_id, IP_Port *ip_port) | |||
1194 | 1029 | ||
1195 | if (id_equal(client->client_id, client_id)) { | 1030 | if (id_equal(client->client_id, client_id)) { |
1196 | IPPTsPng *assoc = NULL; | 1031 | IPPTsPng *assoc = NULL; |
1197 | #ifdef CLIENT_ONETOONE_IP | ||
1198 | assoc = &client->assoc; | ||
1199 | #else | ||
1200 | uint32_t a; | 1032 | uint32_t a; |
1201 | 1033 | ||
1202 | for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) | 1034 | for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) |
1203 | #endif | 1035 | if (!is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) { |
1204 | if (!is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) { | 1036 | *ip_port = assoc->ip_port; |
1205 | *ip_port = assoc->ip_port; | 1037 | return 1; |
1206 | return 1; | 1038 | } |
1207 | } | ||
1208 | } | 1039 | } |
1209 | } | 1040 | } |
1210 | 1041 | ||
@@ -1229,27 +1060,22 @@ static void do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, uint8 | |||
1229 | /* If node is not dead. */ | 1060 | /* If node is not dead. */ |
1230 | Client_data *client = &list[i]; | 1061 | Client_data *client = &list[i]; |
1231 | IPPTsPng *assoc; | 1062 | IPPTsPng *assoc; |
1232 | #ifdef CLIENT_ONETOONE_IP | ||
1233 | assoc = &client->assoc; | ||
1234 | #else | ||
1235 | uint32_t a; | 1063 | uint32_t a; |
1236 | 1064 | ||
1237 | for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) | 1065 | for (a = 0, assoc = &client->assoc6; a < 2; a++, assoc = &client->assoc4) |
1238 | #endif | 1066 | if (!is_timeout(temp_time, assoc->timestamp, KILL_NODE_TIMEOUT)) { |
1239 | 1067 | if (is_timeout(temp_time, assoc->last_pinged, PING_INTERVAL)) { | |
1240 | if (!is_timeout(temp_time, assoc->timestamp, KILL_NODE_TIMEOUT)) { | 1068 | send_ping_request(dht->ping, assoc->ip_port, client->client_id ); |
1241 | if (is_timeout(temp_time, assoc->last_pinged, PING_INTERVAL)) { | 1069 | assoc->last_pinged = temp_time; |
1242 | send_ping_request(dht->ping, assoc->ip_port, client->client_id ); | 1070 | } |
1243 | assoc->last_pinged = temp_time; | ||
1244 | } | ||
1245 | 1071 | ||
1246 | /* If node is good. */ | 1072 | /* If node is good. */ |
1247 | if (!is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) { | 1073 | if (!is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) { |
1248 | client_list[num_nodes] = client; | 1074 | client_list[num_nodes] = client; |
1249 | assoc_list[num_nodes] = assoc; | 1075 | assoc_list[num_nodes] = assoc; |
1250 | ++num_nodes; | 1076 | ++num_nodes; |
1077 | } | ||
1251 | } | 1078 | } |
1252 | } | ||
1253 | } | 1079 | } |
1254 | 1080 | ||
1255 | if ((num_nodes != 0) && | 1081 | if ((num_nodes != 0) && |
@@ -1291,31 +1117,25 @@ int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enable | |||
1291 | { | 1117 | { |
1292 | IP_Port ip_port_v64; | 1118 | IP_Port ip_port_v64; |
1293 | IP *ip_extra = NULL; | 1119 | IP *ip_extra = NULL; |
1294 | #ifdef TOX_ENABLE_IPV6 | ||
1295 | IP_Port ip_port_v4; | 1120 | IP_Port ip_port_v4; |
1296 | ip_init(&ip_port_v64.ip, ipv6enabled); | 1121 | ip_init(&ip_port_v64.ip, ipv6enabled); |
1297 | 1122 | ||
1298 | if (ipv6enabled) { | 1123 | if (ipv6enabled) { |
1124 | /* setup for getting BOTH: an IPv6 AND an IPv4 address */ | ||
1299 | ip_port_v64.ip.family = AF_UNSPEC; | 1125 | ip_port_v64.ip.family = AF_UNSPEC; |
1300 | ip_reset(&ip_port_v4.ip); | 1126 | ip_reset(&ip_port_v4.ip); |
1301 | ip_extra = &ip_port_v4.ip; | 1127 | ip_extra = &ip_port_v4.ip; |
1302 | } | 1128 | } |
1303 | 1129 | ||
1304 | #else | ||
1305 | ip_init(&ip_port_v64.ip, 0); | ||
1306 | #endif | ||
1307 | |||
1308 | if (addr_resolve_or_parse_ip(address, &ip_port_v64.ip, ip_extra)) { | 1130 | if (addr_resolve_or_parse_ip(address, &ip_port_v64.ip, ip_extra)) { |
1309 | ip_port_v64.port = port; | 1131 | ip_port_v64.port = port; |
1310 | DHT_bootstrap(dht, ip_port_v64, public_key); | 1132 | DHT_bootstrap(dht, ip_port_v64, public_key); |
1311 | #ifdef TOX_ENABLE_IPV6 | ||
1312 | 1133 | ||
1313 | if ((ip_extra != NULL) && ip_isset(ip_extra)) { | 1134 | if ((ip_extra != NULL) && ip_isset(ip_extra)) { |
1314 | ip_port_v4.port = port; | 1135 | ip_port_v4.port = port; |
1315 | DHT_bootstrap(dht, ip_port_v4, public_key); | 1136 | DHT_bootstrap(dht, ip_port_v4, public_key); |
1316 | } | 1137 | } |
1317 | 1138 | ||
1318 | #endif | ||
1319 | return 1; | 1139 | return 1; |
1320 | } else | 1140 | } else |
1321 | return 0; | 1141 | return 0; |
@@ -1332,12 +1152,6 @@ int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) | |||
1332 | for (i = 0; i < LCLIENT_LIST; ++i) { | 1152 | for (i = 0; i < LCLIENT_LIST; ++i) { |
1333 | if (id_equal(client_id, dht->close_clientlist[i].client_id)) { | 1153 | if (id_equal(client_id, dht->close_clientlist[i].client_id)) { |
1334 | Client_data *client = &dht->close_clientlist[i]; | 1154 | Client_data *client = &dht->close_clientlist[i]; |
1335 | #ifdef CLIENT_ONETOONE_IP | ||
1336 | |||
1337 | if (ip_isset(&client->assoc.ip_port.ip)) | ||
1338 | return sendpacket(dht->c->lossless_udp->net, dht->close_clientlist[i].assoc.ip_port, packet, length); | ||
1339 | |||
1340 | #else | ||
1341 | 1155 | ||
1342 | if (ip_isset(&client->assoc6.ip_port.ip)) | 1156 | if (ip_isset(&client->assoc6.ip_port.ip)) |
1343 | return sendpacket(dht->c->lossless_udp->net, client->assoc6.ip_port, packet, length); | 1157 | return sendpacket(dht->c->lossless_udp->net, client->assoc6.ip_port, packet, length); |
@@ -1345,8 +1159,6 @@ int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) | |||
1345 | return sendpacket(dht->c->lossless_udp->net, client->assoc4.ip_port, packet, length); | 1159 | return sendpacket(dht->c->lossless_udp->net, client->assoc4.ip_port, packet, length); |
1346 | else | 1160 | else |
1347 | break; | 1161 | break; |
1348 | |||
1349 | #endif | ||
1350 | } | 1162 | } |
1351 | } | 1163 | } |
1352 | 1164 | ||
@@ -1355,7 +1167,6 @@ int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) | |||
1355 | 1167 | ||
1356 | /* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist. | 1168 | /* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist. |
1357 | * ip_portlist must be at least MAX_FRIEND_CLIENTS big. | 1169 | * ip_portlist must be at least MAX_FRIEND_CLIENTS big. |
1358 | * unless CLIENT_ONETOONE_IP is defined, returns an array with ips of the same family. | ||
1359 | * | 1170 | * |
1360 | * return the number of ips returned. | 1171 | * return the number of ips returned. |
1361 | * return 0 if we are connected to friend or if no ips were found. | 1172 | * return 0 if we are connected to friend or if no ips were found. |
@@ -1375,55 +1186,56 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num) | |||
1375 | int num_ipv4s = 0; | 1186 | int num_ipv4s = 0; |
1376 | IP_Port ipv6s[MAX_FRIEND_CLIENTS]; | 1187 | IP_Port ipv6s[MAX_FRIEND_CLIENTS]; |
1377 | int num_ipv6s = 0; | 1188 | int num_ipv6s = 0; |
1189 | uint8_t connected; | ||
1378 | 1190 | ||
1379 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 1191 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
1380 | client = &(friend->client_list[i]); | 1192 | client = &(friend->client_list[i]); |
1381 | 1193 | connected = 0; | |
1382 | IPPTsPng *assoc = NULL; | ||
1383 | #ifdef CLIENT_ONETOONE_IP | ||
1384 | assoc = &client->assoc; | ||
1385 | #else | ||
1386 | assoc = &client->assoc4; | ||
1387 | #endif | ||
1388 | |||
1389 | if (id_equal(client->client_id, friend->client_id) && | ||
1390 | !is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) | ||
1391 | return 0; | ||
1392 | 1194 | ||
1393 | /* If ip is not zero and node is good. */ | 1195 | /* If ip is not zero and node is good. */ |
1394 | if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { | 1196 | if (ip_isset(&client->assoc4.ret_ip_port.ip) && !is_timeout(temp_time, client->assoc4.ret_timestamp, BAD_NODE_TIMEOUT)) { |
1395 | ipv4s[num_ipv4s] = assoc->ret_ip_port; | 1197 | ipv4s[num_ipv4s] = client->assoc4.ret_ip_port; |
1396 | ++num_ipv4s; | 1198 | ++num_ipv4s; |
1199 | |||
1200 | connected = 1; | ||
1397 | } | 1201 | } |
1398 | } | ||
1399 | 1202 | ||
1400 | #ifndef CLIENT_ONETOONE_IP | 1203 | if (ip_isset(&client->assoc6.ret_ip_port.ip) && !is_timeout(temp_time, client->assoc6.ret_timestamp, BAD_NODE_TIMEOUT)) { |
1204 | ipv6s[num_ipv6s] = client->assoc6.ret_ip_port; | ||
1205 | ++num_ipv6s; | ||
1401 | 1206 | ||
1402 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 1207 | connected = 1; |
1403 | client = &(friend->client_list[i]); | 1208 | } |
1404 | 1209 | ||
1405 | IPPTsPng *assoc = NULL; | 1210 | if (connected && id_equal(client->client_id, friend->client_id)) |
1406 | assoc = &client->assoc6; | 1211 | return 0; /* direct connectivity */ |
1212 | } | ||
1407 | 1213 | ||
1408 | if (id_equal(client->client_id, friend->client_id) && | 1214 | #ifdef FRIEND_IPLIST_PAD |
1409 | !is_timeout(temp_time, assoc->timestamp, BAD_NODE_TIMEOUT)) | 1215 | memcpy(ip_portlist, ipv6s, num_ipv6s * sizeof(IP_Port)); |
1410 | return 0; | 1216 | if (num_ipv6s == MAX_FRIEND_CLIENTS) |
1217 | return MAX_FRIEND_CLIENTS; | ||
1411 | 1218 | ||
1412 | /* If ip is not zero and node is good. */ | 1219 | int num_ipv4s_used = MAX_FRIEND_CLIENTS - num_ipv6s; |
1413 | if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { | 1220 | if (num_ipv4s_used > num_ipv4s) |
1414 | ipv6s[num_ipv6s] = assoc->ret_ip_port; | 1221 | num_ipv4s_used = num_ipv4s; |
1415 | ++num_ipv6s; | 1222 | memcpy(&ip_portlist[num_ipv6s], ipv4s, num_ipv4s_used * sizeof(IP_Port)); |
1416 | } | 1223 | return num_ipv6s + num_ipv4s_used; |
1417 | } | 1224 | |
1225 | #else /* !FRIEND_IPLIST_PAD */ | ||
1418 | 1226 | ||
1227 | /* there must be some secret reason why we can't pad the longer list | ||
1228 | * with the shorter one... | ||
1229 | */ | ||
1419 | if (num_ipv6s >= num_ipv4s) { | 1230 | if (num_ipv6s >= num_ipv4s) { |
1420 | memcpy(ip_portlist, ipv6s, num_ipv6s * sizeof(IP_Port)); | 1231 | memcpy(ip_portlist, ipv6s, num_ipv6s * sizeof(IP_Port)); |
1421 | return num_ipv6s; | 1232 | return num_ipv6s; |
1422 | } | 1233 | } |
1423 | 1234 | ||
1424 | #endif | ||
1425 | memcpy(ip_portlist, ipv4s, num_ipv4s * sizeof(IP_Port)); | 1235 | memcpy(ip_portlist, ipv4s, num_ipv4s * sizeof(IP_Port)); |
1426 | return num_ipv4s; | 1236 | return num_ipv4s; |
1237 | |||
1238 | #endif /* !FRIEND_IPLIST_PAD */ | ||
1427 | } | 1239 | } |
1428 | 1240 | ||
1429 | 1241 | ||
@@ -1452,30 +1264,23 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt | |||
1452 | DHT_Friend *friend = &dht->friends_list[num]; | 1264 | DHT_Friend *friend = &dht->friends_list[num]; |
1453 | Client_data *client; | 1265 | Client_data *client; |
1454 | 1266 | ||
1455 | #ifndef CLIENT_ONETOONE_IP | ||
1456 | /* extra legwork, because having the outside allocating the space for us | 1267 | /* extra legwork, because having the outside allocating the space for us |
1457 | * is *usually* good(tm) (bites us in the behind in this case though) */ | 1268 | * is *usually* good(tm) (bites us in the behind in this case though) */ |
1458 | uint32_t a; | 1269 | uint32_t a; |
1459 | 1270 | ||
1460 | for (a = 0; a < 2; a++) | 1271 | for (a = 0; a < 2; a++) |
1461 | #endif | ||
1462 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 1272 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
1463 | if (friend_sent[i])/* Send one packet per client.*/ | 1273 | if (friend_sent[i])/* Send one packet per client.*/ |
1464 | continue; | 1274 | continue; |
1465 | 1275 | ||
1466 | client = &friend->client_list[i]; | 1276 | client = &friend->client_list[i]; |
1467 | IPPTsPng *assoc = NULL; | 1277 | IPPTsPng *assoc = NULL; |
1468 | #ifdef CLIENT_ONETOONE_IP | ||
1469 | assoc = &client->assoc; | ||
1470 | #else | ||
1471 | 1278 | ||
1472 | if (!a) | 1279 | if (!a) |
1473 | assoc = &client->assoc4; | 1280 | assoc = &client->assoc4; |
1474 | else | 1281 | else |
1475 | assoc = &client->assoc6; | 1282 | assoc = &client->assoc6; |
1476 | 1283 | ||
1477 | #endif | ||
1478 | |||
1479 | /* If ip is not zero and node is good. */ | 1284 | /* If ip is not zero and node is good. */ |
1480 | if (ip_isset(&assoc->ret_ip_port.ip) && | 1285 | if (ip_isset(&assoc->ret_ip_port.ip) && |
1481 | !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { | 1286 | !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { |
@@ -1510,27 +1315,20 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint | |||
1510 | uint32_t i; | 1315 | uint32_t i; |
1511 | uint64_t temp_time = unix_time(); | 1316 | uint64_t temp_time = unix_time(); |
1512 | 1317 | ||
1513 | #ifndef CLIENT_ONETOONE_IP | ||
1514 | /* extra legwork, because having the outside allocating the space for us | 1318 | /* extra legwork, because having the outside allocating the space for us |
1515 | * is *usually* good(tm) (bites us in the behind in this case though) */ | 1319 | * is *usually* good(tm) (bites us in the behind in this case though) */ |
1516 | uint32_t a; | 1320 | uint32_t a; |
1517 | 1321 | ||
1518 | for (a = 0; a < 2; a++) | 1322 | for (a = 0; a < 2; a++) |
1519 | #endif | ||
1520 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 1323 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
1521 | client = &friend->client_list[i]; | 1324 | client = &friend->client_list[i]; |
1522 | IPPTsPng *assoc = NULL; | 1325 | IPPTsPng *assoc = NULL; |
1523 | #ifdef CLIENT_ONETOONE_IP | ||
1524 | assoc = &client->assoc; | ||
1525 | #else | ||
1526 | 1326 | ||
1527 | if (!a) | 1327 | if (!a) |
1528 | assoc = &client->assoc4; | 1328 | assoc = &client->assoc4; |
1529 | else | 1329 | else |
1530 | assoc = &client->assoc6; | 1330 | assoc = &client->assoc6; |
1531 | 1331 | ||
1532 | #endif | ||
1533 | |||
1534 | /* If ip is not zero and node is good. */ | 1332 | /* If ip is not zero and node is good. */ |
1535 | if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { | 1333 | if (ip_isset(&assoc->ret_ip_port.ip) && !is_timeout(temp_time, assoc->ret_timestamp, BAD_NODE_TIMEOUT)) { |
1536 | ip_list[n] = assoc->ip_port; | 1334 | ip_list[n] = assoc->ip_port; |
@@ -1776,9 +1574,7 @@ DHT *new_DHT(Net_Crypto *c) | |||
1776 | dht->c = c; | 1574 | dht->c = c; |
1777 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_GET_NODES, &handle_getnodes, dht); | 1575 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_GET_NODES, &handle_getnodes, dht); |
1778 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES, &handle_sendnodes, dht); | 1576 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES, &handle_sendnodes, dht); |
1779 | #ifdef TOX_ENABLE_IPV6 | ||
1780 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES_IPV6, &handle_sendnodes_ipv6, dht); | 1577 | networking_registerhandler(c->lossless_udp->net, NET_PACKET_SEND_NODES_IPV6, &handle_sendnodes_ipv6, dht); |
1781 | #endif | ||
1782 | 1578 | ||
1783 | init_cryptopackets(dht); | 1579 | init_cryptopackets(dht); |
1784 | cryptopacket_registerhandler(c, CRYPTO_PACKET_NAT_PING, &handle_NATping, dht); | 1580 | cryptopacket_registerhandler(c, CRYPTO_PACKET_NAT_PING, &handle_NATping, dht); |
@@ -1885,12 +1681,8 @@ uint32_t DHT_size(DHT *dht) | |||
1885 | uint32_t num = 0, i; | 1681 | uint32_t num = 0, i; |
1886 | 1682 | ||
1887 | for (i = 0; i < LCLIENT_LIST; ++i) | 1683 | for (i = 0; i < LCLIENT_LIST; ++i) |
1888 | #ifdef CLIENT_ONETOONE_IP | ||
1889 | if (dht->close_clientlist[i].assoc.timestamp != 0) | ||
1890 | #else | ||
1891 | if ((dht->close_clientlist[i].assoc4.timestamp != 0) || | 1684 | if ((dht->close_clientlist[i].assoc4.timestamp != 0) || |
1892 | (dht->close_clientlist[i].assoc6.timestamp != 0)) | 1685 | (dht->close_clientlist[i].assoc6.timestamp != 0)) |
1893 | #endif | ||
1894 | num++; | 1686 | num++; |
1895 | 1687 | ||
1896 | uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; | 1688 | uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2; |
@@ -1917,11 +1709,7 @@ void DHT_save(DHT *dht, uint8_t *data) | |||
1917 | data += sizeof(uint32_t); | 1709 | data += sizeof(uint32_t); |
1918 | 1710 | ||
1919 | len = sizeof(DHT_Friend) * dht->num_friends; | 1711 | len = sizeof(DHT_Friend) * dht->num_friends; |
1920 | #ifdef CLIENT_ONETOONE_IP | ||
1921 | type = DHT_STATE_TYPE_FRIENDS; | ||
1922 | #else | ||
1923 | type = DHT_STATE_TYPE_FRIENDS_ASSOC46; | 1712 | type = DHT_STATE_TYPE_FRIENDS_ASSOC46; |
1924 | #endif | ||
1925 | data = z_state_save_subheader(data, len, type); | 1713 | data = z_state_save_subheader(data, len, type); |
1926 | memcpy(data, dht->friends_list, len); | 1714 | memcpy(data, dht->friends_list, len); |
1927 | data += len; | 1715 | data += len; |
@@ -1929,32 +1717,20 @@ void DHT_save(DHT *dht, uint8_t *data) | |||
1929 | uint32_t num = 0, i; | 1717 | uint32_t num = 0, i; |
1930 | 1718 | ||
1931 | for (i = 0; i < LCLIENT_LIST; ++i) | 1719 | for (i = 0; i < LCLIENT_LIST; ++i) |
1932 | #ifdef CLIENT_ONETOONE_IP | ||
1933 | if (dht->close_clientlist[i].assoc.timestamp != 0) | ||
1934 | #else | ||
1935 | if ((dht->close_clientlist[i].assoc4.timestamp != 0) || | 1720 | if ((dht->close_clientlist[i].assoc4.timestamp != 0) || |
1936 | (dht->close_clientlist[i].assoc6.timestamp != 0)) | 1721 | (dht->close_clientlist[i].assoc6.timestamp != 0)) |
1937 | #endif | ||
1938 | num++; | 1722 | num++; |
1939 | 1723 | ||
1940 | len = num * sizeof(Client_data); | 1724 | len = num * sizeof(Client_data); |
1941 | #ifdef CLIENT_ONETOONE_IP | ||
1942 | type = DHT_STATE_TYPE_CLIENTS; | ||
1943 | #else | ||
1944 | type = DHT_STATE_TYPE_CLIENTS_ASSOC46; | 1725 | type = DHT_STATE_TYPE_CLIENTS_ASSOC46; |
1945 | #endif | ||
1946 | data = z_state_save_subheader(data, len, type); | 1726 | data = z_state_save_subheader(data, len, type); |
1947 | 1727 | ||
1948 | if (num) { | 1728 | if (num) { |
1949 | Client_data *clients = (Client_data *)data; | 1729 | Client_data *clients = (Client_data *)data; |
1950 | 1730 | ||
1951 | for (num = 0, i = 0; i < LCLIENT_LIST; ++i) | 1731 | for (num = 0, i = 0; i < LCLIENT_LIST; ++i) |
1952 | #ifdef CLIENT_ONETOONE_IP | ||
1953 | if (dht->close_clientlist[i].assoc.timestamp != 0) | ||
1954 | #else | ||
1955 | if ((dht->close_clientlist[i].assoc4.timestamp != 0) || | 1732 | if ((dht->close_clientlist[i].assoc4.timestamp != 0) || |
1956 | (dht->close_clientlist[i].assoc6.timestamp != 0)) | 1733 | (dht->close_clientlist[i].assoc6.timestamp != 0)) |
1957 | #endif | ||
1958 | memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data)); | 1734 | memcpy(&clients[num++], &dht->close_clientlist[i], sizeof(Client_data)); |
1959 | } | 1735 | } |
1960 | 1736 | ||
@@ -2005,18 +1781,18 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length, | |||
2005 | break; | 1781 | break; |
2006 | 1782 | ||
2007 | case DHT_STATE_TYPE_FRIENDS_ASSOC46: | 1783 | case DHT_STATE_TYPE_FRIENDS_ASSOC46: |
2008 | if (length % sizeof(DHT_Friend_new) != 0) | 1784 | if (length % sizeof(DHT_Friend) != 0) |
2009 | break; | 1785 | break; |
2010 | 1786 | ||
2011 | { /* localize declarations */ | 1787 | { /* localize declarations */ |
2012 | DHT_Friend_new *friend_list = (DHT_Friend_new *)data; | 1788 | DHT_Friend *friend_list = (DHT_Friend *)data; |
2013 | num = length / sizeof(DHT_Friend_new); | 1789 | num = length / sizeof(DHT_Friend); |
2014 | 1790 | ||
2015 | for (i = 0; i < num; ++i) { | 1791 | for (i = 0; i < num; ++i) { |
2016 | DHT_addfriend(dht, friend_list[i].client_id); | 1792 | DHT_addfriend(dht, friend_list[i].client_id); |
2017 | 1793 | ||
2018 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 1794 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
2019 | Client_data_new *client = &friend_list[i].client_list[j]; | 1795 | Client_data *client = &friend_list[i].client_list[j]; |
2020 | 1796 | ||
2021 | if (client->assoc4.timestamp != 0) | 1797 | if (client->assoc4.timestamp != 0) |
2022 | getnodes(dht, client->assoc4.ip_port, client->client_id, friend_list[i].client_id); | 1798 | getnodes(dht, client->assoc4.ip_port, client->client_id, friend_list[i].client_id); |
@@ -2030,12 +1806,12 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length, | |||
2030 | break; | 1806 | break; |
2031 | 1807 | ||
2032 | case DHT_STATE_TYPE_CLIENTS_ASSOC46: | 1808 | case DHT_STATE_TYPE_CLIENTS_ASSOC46: |
2033 | if ((length % sizeof(Client_data_new)) != 0) | 1809 | if ((length % sizeof(Client_data)) != 0) |
2034 | break; | 1810 | break; |
2035 | 1811 | ||
2036 | { /* localize declarations */ | 1812 | { /* localize declarations */ |
2037 | num = length / sizeof(Client_data_new); | 1813 | num = length / sizeof(Client_data); |
2038 | Client_data_new *client_list = (Client_data_new *)data; | 1814 | Client_data *client_list = (Client_data *)data; |
2039 | 1815 | ||
2040 | for (i = 0; i < num; ++i) { | 1816 | for (i = 0; i < num; ++i) { |
2041 | if (client_list[i].assoc4.timestamp != 0) | 1817 | if (client_list[i].assoc4.timestamp != 0) |
@@ -2048,9 +1824,12 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length, | |||
2048 | 1824 | ||
2049 | break; | 1825 | break; |
2050 | 1826 | ||
1827 | #ifdef DEBUG | ||
2051 | default: | 1828 | default: |
2052 | fprintf(stderr, "Load state (DHT): contains unrecognized part (len %u, type %u)\n", | 1829 | fprintf(stderr, "Load state (DHT): contains unrecognized part (len %u, type %u)\n", |
2053 | length, type); | 1830 | length, type); |
1831 | break; | ||
1832 | #endif | ||
2054 | } | 1833 | } |
2055 | 1834 | ||
2056 | return 0; | 1835 | return 0; |
@@ -2085,13 +1864,9 @@ int DHT_isconnected(DHT *dht) | |||
2085 | 1864 | ||
2086 | for (i = 0; i < LCLIENT_LIST; ++i) { | 1865 | for (i = 0; i < LCLIENT_LIST; ++i) { |
2087 | Client_data *client = &dht->close_clientlist[i]; | 1866 | Client_data *client = &dht->close_clientlist[i]; |
2088 | #ifdef CLIENT_ONETOONE_IP | ||
2089 | 1867 | ||
2090 | if (!is_timeout(temp_time, client->assoc.timestamp, BAD_NODE_TIMEOUT)) | ||
2091 | #else | ||
2092 | if (!is_timeout(temp_time, client->assoc4.timestamp, BAD_NODE_TIMEOUT) || | 1868 | if (!is_timeout(temp_time, client->assoc4.timestamp, BAD_NODE_TIMEOUT) || |
2093 | !is_timeout(temp_time, client->assoc6.timestamp, BAD_NODE_TIMEOUT)) | 1869 | !is_timeout(temp_time, client->assoc6.timestamp, BAD_NODE_TIMEOUT)) |
2094 | #endif | ||
2095 | return 1; | 1870 | return 1; |
2096 | } | 1871 | } |
2097 | 1872 | ||