summaryrefslogtreecommitdiff
path: root/toxcore/DHT.c
diff options
context:
space:
mode:
authorDiadlo <polsha3@gmail.com>2017-03-06 09:21:36 +0300
committerDiadlo <polsha3@gmail.com>2017-06-05 02:05:34 +0300
commitf9607bced5cc7c9ccf6788c588c650e1f078b38f (patch)
treee487bbfc64c0bebc2750648ffe6fb6823f8813d8 /toxcore/DHT.c
parent91e3eb0159d7303644f317b61b67e51b93c2f568 (diff)
Make work with assoc more generic
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r--toxcore/DHT.c98
1 files changed, 43 insertions, 55 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index e3b50569..43230853 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -562,41 +562,35 @@ static uint32_t index_of_client_ip_port(const Client_data *array, uint32_t size,
562 */ 562 */
563static void update_client(Logger *log, int index, Client_data *client, IP_Port ip_port) 563static void update_client(Logger *log, int index, Client_data *client, IP_Port ip_port)
564{ 564{
565 uint64_t temp_time = unix_time(); 565 IPPTsPng *assoc;
566 int ip_version;
567
566 if (ip_port.ip.family == AF_INET) { 568 if (ip_port.ip.family == AF_INET) {
567 if (!ipport_equal(&client[index].assoc4.ip_port, &ip_port)) { 569 assoc = &client->assoc4;
568 char ip_str[IP_NTOA_LEN]; 570 ip_version = 4;
569 LOGGER_TRACE(log, "coipil[%u]: switching ipv4 from %s:%u to %s:%u", index,
570 ip_ntoa(&client[index].assoc4.ip_port.ip, ip_str, sizeof(ip_str)),
571 net_ntohs(client[index].assoc4.ip_port.port),
572 ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)),
573 net_ntohs(ip_port.port));
574 }
575
576 if (LAN_ip(client[index].assoc4.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) {
577 return;
578 }
579
580 client[index].assoc4.ip_port = ip_port;
581 client[index].assoc4.timestamp = temp_time;
582 } else if (ip_port.ip.family == AF_INET6) { 571 } else if (ip_port.ip.family == AF_INET6) {
572 assoc = &client->assoc6;
573 ip_version = 6;
574 } else {
575 return;
576 }
583 577
584 if (!ipport_equal(&client[index].assoc6.ip_port, &ip_port)) { 578 if (!ipport_equal(&assoc->ip_port, &ip_port)) {
585 char ip_str[IP_NTOA_LEN]; 579 char ip_str[IP_NTOA_LEN];
586 LOGGER_TRACE(log, "coipil[%u]: switching ipv6 from %s:%u to %s:%u", index, 580 LOGGER_TRACE(log, "coipil[%u]: switching ipv%d from %s:%u to %s:%u",
587 ip_ntoa(&client[index].assoc6.ip_port.ip, ip_str, sizeof(ip_str)), 581 index, ip_version,
588 net_ntohs(client[index].assoc6.ip_port.port), 582 ip_ntoa(&assoc->ip_port.ip, ip_str, sizeof(ip_str)),
589 ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), 583 net_ntohs(assoc->ip_port.port),
590 net_ntohs(ip_port.port)); 584 ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)),
591 } 585 net_ntohs(ip_port.port));
592 586 }
593 if (LAN_ip(client[index].assoc6.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) {
594 return;
595 }
596 587
597 client[index].assoc6.ip_port = ip_port; 588 if (LAN_ip(assoc->ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) {
598 client[index].assoc6.timestamp = temp_time; 589 return;
599 } 590 }
591
592 assoc->ip_port = ip_port;
593 assoc->timestamp = unix_time();
600} 594}
601 595
602/* Check if client with public_key is already in list of length length. 596/* Check if client with public_key is already in list of length length.
@@ -629,31 +623,26 @@ static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t le
629 return 0; 623 return 0;
630 } 624 }
631 625
632 if (ip_port.ip.family == AF_INET) { 626 IPPTsPng *assoc;
633 /* Initialize client timestamp. */ 627 int ip_version;
634 list[index].assoc4.timestamp = temp_time;
635 memcpy(list[index].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
636
637 LOGGER_DEBUG(log, "coipil[%u]: switching public_key (ipv4)", index);
638 628
639 /* kill the other address, if it was set */ 629 if (ip_port.ip.family == AF_INET) {
640 memset(&list[index].assoc6, 0, sizeof(list[index].assoc6)); 630 assoc = &list[index].assoc4;
641 return 1; 631 ip_version = 4;
632 } else {
633 assoc = &list[index].assoc6;
634 ip_version = 6;
642 } 635 }
643 636
644 if (ip_port.ip.family == AF_INET6) { 637 /* Initialize client timestamp. */
645 /* Initialize client timestamp. */ 638 assoc->timestamp = temp_time;
646 list[index].assoc6.timestamp = temp_time; 639 memcpy(list[index].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
647 memcpy(list[index].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
648
649 LOGGER_DEBUG(log, "coipil[%u]: switching public_key (ipv6)", index);
650 640
651 /* kill the other address, if it was set */ 641 LOGGER_DEBUG(log, "coipil[%u]: switching public_key (ipv%d)", index, ip_version);
652 memset(&list[index].assoc4, 0, sizeof(list[index].assoc4));
653 return 1;
654 }
655 642
656 return 0; 643 /* kill the other address, if it was set */
644 memset(assoc, 0, sizeof(IPPTsPng));
645 return 1;
657} 646}
658 647
659/* Add node to the node list making sure only the nodes closest to cmp_pk are in the list. 648/* Add node to the node list making sure only the nodes closest to cmp_pk are in the list.
@@ -997,12 +986,11 @@ static bool is_pk_in_client_list(Client_data *list, unsigned int client_list_len
997 return 0; 986 return 0;
998 } 987 }
999 988
1000 if ((ip_port.ip.family == AF_INET && !is_timeout(list[index].assoc4.timestamp, BAD_NODE_TIMEOUT)) 989 const IPPTsPng *assoc = ip_port.ip.family == AF_INET ?
1001 || (ip_port.ip.family == AF_INET6 && !is_timeout(list[index].assoc6.timestamp, BAD_NODE_TIMEOUT))) { 990 &list[index].assoc4 :
1002 return 1; 991 &list[index].assoc6;
1003 }
1004 992
1005 return 0; 993 return !is_timeout(assoc->timestamp, BAD_NODE_TIMEOUT);
1006} 994}
1007 995
1008static bool is_pk_in_close_list(DHT *dht, const uint8_t *public_key, IP_Port ip_port) 996static bool is_pk_in_close_list(DHT *dht, const uint8_t *public_key, IP_Port ip_port)