diff options
author | Diadlo <polsha3@gmail.com> | 2017-06-05 00:10:27 +0300 |
---|---|---|
committer | Diadlo <polsha3@gmail.com> | 2017-06-05 02:02:31 +0300 |
commit | 0eaa44650fcea3864ff045a197dbf580b31390c8 (patch) | |
tree | ad0fdab356b4f4a69097052e0d916d04959db66a /toxcore/DHT.c | |
parent | 71260e38e8d12547b0e55916daf6cadd72f52e19 (diff) |
Extract 'update_client' function
To make 'client_or_ip_port_in_list' code easy to read
Diffstat (limited to 'toxcore/DHT.c')
-rw-r--r-- | toxcore/DHT.c | 78 |
1 files changed, 42 insertions, 36 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index d8035ab1..6e425939 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -564,6 +564,47 @@ static uint32_t index_of_client_ip_port(const Client_data *array, uint32_t size, | |||
564 | return UINT32_MAX; | 564 | return UINT32_MAX; |
565 | } | 565 | } |
566 | 566 | ||
567 | /* Update ip_port of client if it's needed. | ||
568 | */ | ||
569 | static void update_client(Logger *log, int index, Client_data *client, IP_Port ip_port) | ||
570 | { | ||
571 | uint64_t temp_time = unix_time(); | ||
572 | if (ip_port.ip.family == AF_INET) { | ||
573 | if (!ipport_equal(&client[index].assoc4.ip_port, &ip_port)) { | ||
574 | char ip_str[IP_NTOA_LEN]; | ||
575 | LOGGER_TRACE(log, "coipil[%u]: switching ipv4 from %s:%u to %s:%u", index, | ||
576 | ip_ntoa(&client[index].assoc4.ip_port.ip, ip_str, sizeof(ip_str)), | ||
577 | net_ntohs(client[index].assoc4.ip_port.port), | ||
578 | ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), | ||
579 | net_ntohs(ip_port.port)); | ||
580 | } | ||
581 | |||
582 | if (LAN_ip(client[index].assoc4.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) { | ||
583 | return; | ||
584 | } | ||
585 | |||
586 | client[index].assoc4.ip_port = ip_port; | ||
587 | client[index].assoc4.timestamp = temp_time; | ||
588 | } else if (ip_port.ip.family == AF_INET6) { | ||
589 | |||
590 | if (!ipport_equal(&client[index].assoc6.ip_port, &ip_port)) { | ||
591 | char ip_str[IP_NTOA_LEN]; | ||
592 | LOGGER_TRACE(log, "coipil[%u]: switching ipv6 from %s:%u to %s:%u", index, | ||
593 | ip_ntoa(&client[index].assoc6.ip_port.ip, ip_str, sizeof(ip_str)), | ||
594 | net_ntohs(client[index].assoc6.ip_port.port), | ||
595 | ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), | ||
596 | net_ntohs(ip_port.port)); | ||
597 | } | ||
598 | |||
599 | if (LAN_ip(client[index].assoc6.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) { | ||
600 | return; | ||
601 | } | ||
602 | |||
603 | client[index].assoc6.ip_port = ip_port; | ||
604 | client[index].assoc6.timestamp = temp_time; | ||
605 | } | ||
606 | } | ||
607 | |||
567 | /* Check if client with public_key is already in list of length length. | 608 | /* Check if client with public_key is already in list of length length. |
568 | * If it is then set its corresponding timestamp to current time. | 609 | * If it is then set its corresponding timestamp to current time. |
569 | * If the id is already in the list with a different ip_port, update it. | 610 | * If the id is already in the list with a different ip_port, update it. |
@@ -579,42 +620,7 @@ static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t le | |||
579 | 620 | ||
580 | /* if public_key is in list, find it and maybe overwrite ip_port */ | 621 | /* if public_key is in list, find it and maybe overwrite ip_port */ |
581 | if (index != UINT32_MAX) { | 622 | if (index != UINT32_MAX) { |
582 | /* Refresh the client timestamp. */ | 623 | update_client(log, index, &list[index], ip_port); |
583 | if (ip_port.ip.family == AF_INET) { | ||
584 | if (!ipport_equal(&list[index].assoc4.ip_port, &ip_port)) { | ||
585 | char ip_str[IP_NTOA_LEN]; | ||
586 | LOGGER_TRACE(log, "coipil[%u]: switching ipv4 from %s:%u to %s:%u", index, | ||
587 | ip_ntoa(&list[index].assoc4.ip_port.ip, ip_str, sizeof(ip_str)), | ||
588 | net_ntohs(list[index].assoc4.ip_port.port), | ||
589 | ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), | ||
590 | net_ntohs(ip_port.port)); | ||
591 | } | ||
592 | |||
593 | if (LAN_ip(list[index].assoc4.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) { | ||
594 | return 1; | ||
595 | } | ||
596 | |||
597 | list[index].assoc4.ip_port = ip_port; | ||
598 | list[index].assoc4.timestamp = temp_time; | ||
599 | } else if (ip_port.ip.family == AF_INET6) { | ||
600 | |||
601 | if (!ipport_equal(&list[index].assoc6.ip_port, &ip_port)) { | ||
602 | char ip_str[IP_NTOA_LEN]; | ||
603 | LOGGER_TRACE(log, "coipil[%u]: switching ipv6 from %s:%u to %s:%u", index, | ||
604 | ip_ntoa(&list[index].assoc6.ip_port.ip, ip_str, sizeof(ip_str)), | ||
605 | net_ntohs(list[index].assoc6.ip_port.port), | ||
606 | ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), | ||
607 | net_ntohs(ip_port.port)); | ||
608 | } | ||
609 | |||
610 | if (LAN_ip(list[index].assoc6.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) { | ||
611 | return 1; | ||
612 | } | ||
613 | |||
614 | list[index].assoc6.ip_port = ip_port; | ||
615 | list[index].assoc6.timestamp = temp_time; | ||
616 | } | ||
617 | |||
618 | return 1; | 624 | return 1; |
619 | } | 625 | } |
620 | 626 | ||