From 1d3778fc53b51c5aa06b1bd1fd9c968debbdde13 Mon Sep 17 00:00:00 2001 From: jfreegman Date: Thu, 3 Feb 2022 12:32:30 -0500 Subject: [PATCH] Move duplicate code for converting IPv4-in-6 to IPv4 to a function --- .../docker/tox-bootstrapd.sha256 | 2 +- toxcore/DHT.c | 32 +++++++++++-------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index 8a7162e4..28ea6f35 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -6fbe15237f53f8cae5f633c317b41982eb50c52b6209e14548daae7f1f03955b /usr/local/bin/tox-bootstrapd +87ab42da03d54cf33815b364a974d1548a2a673415bd82e86ffbd6511483eb81 /usr/local/bin/tox-bootstrapd diff --git a/toxcore/DHT.c b/toxcore/DHT.c index e7341f95..b214e98c 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -173,6 +173,22 @@ static bool assoc_timeout(uint64_t cur_time, const IPPTsPng *assoc) return (assoc->timestamp + BAD_NODE_TIMEOUT) <= cur_time; } +/** Converts an IPv4-in-IPv6 to IPv4 and returns the new IP_Port. + * + * If the ip_port is already IPv4 this function returns a copy of the original ip_port. + */ +static IP_Port ip_port_normalize(const IP_Port *ip_port) +{ + IP_Port res = *ip_port; + + if (net_family_is_ipv6(res.ip.family) && ipv6_ipv4_in_v6(&res.ip.ip.v6)) { + res.ip.family = net_family_ipv4; + res.ip.ip.v4.uint32 = res.ip.ip.v6.uint32[3]; + } + + return res; +} + /** Compares pk1 and pk2 with pk. * * return 0 if both are same distance. @@ -1152,16 +1168,10 @@ static bool ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, cons */ uint32_t addto_lists(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key) { - IP_Port ipp_copy = *ip_port; + IP_Port ipp_copy = ip_port_normalize(ip_port); uint32_t used = 0; - /* convert IPv4-in-IPv6 to IPv4 */ - if (net_family_is_ipv6(ipp_copy.ip.family) && ipv6_ipv4_in_v6(&ipp_copy.ip.ip.v6)) { - ipp_copy.ip.family = net_family_ipv4; - ipp_copy.ip.ip.v4.uint32 = ipp_copy.ip.ip.v6.uint32[3]; - } - /* NOTE: Current behavior if there are two clients with the same id is * to replace the first ip by the second. */ @@ -1240,13 +1250,7 @@ static bool update_client_data(const Mono_Time *mono_time, Client_data *array, s */ static void returnedip_ports(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *nodepublic_key) { - IP_Port ipp_copy = *ip_port; - - /* convert IPv4-in-IPv6 to IPv4 */ - if (net_family_is_ipv6(ipp_copy.ip.family) && ipv6_ipv4_in_v6(&ipp_copy.ip.ip.v6)) { - ipp_copy.ip.family = net_family_ipv4; - ipp_copy.ip.ip.v4.uint32 = ipp_copy.ip.ip.v6.uint32[3]; - } + IP_Port ipp_copy = ip_port_normalize(ip_port); if (id_equal(public_key, dht->self_public_key)) { update_client_data(dht->mono_time, dht->close_clientlist, LCLIENT_LIST, &ipp_copy, nodepublic_key, true);