From ff1f97bf6e9990dbe36ecea6e15f8dfa858d6974 Mon Sep 17 00:00:00 2001 From: jpk68 Date: Wed, 3 Jun 2026 12:04:46 -0400 Subject: [PATCH 1/2] dns_utils: replace address-to-string functions --- src/common/dns_utils.cpp | 51 ++++++++++++---------------------------- 1 file changed, 15 insertions(+), 36 deletions(-) diff --git a/src/common/dns_utils.cpp b/src/common/dns_utils.cpp index 48dab6961..8c20b7887 100644 --- a/src/common/dns_utils.cpp +++ b/src/common/dns_utils.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include "include_base_utils.h" #include "common/threadpool.h" #include "crypto/crypto.h" @@ -41,6 +42,7 @@ #include #include #include +#include using namespace epee; #undef MONERO_DEFAULT_LOG_CATEGORY @@ -133,55 +135,32 @@ static const char *get_record_name(int record_type) } } -// fuck it, I'm tired of dealing with getnameinfo()/inet_ntop/etc -boost::optional ipv4_to_string(const char* src, size_t len) +static boost::optional ipv4_to_string(const char* src, size_t len) { if (len < 4) { - MERROR("Invalid IPv4 address: " << std::string(src, len)); + MERROR("Invalid IPv4 address with length " << len); return boost::none; } - std::stringstream ss; - unsigned int bytes[4]; - for (int i = 0; i < 4; i++) - { - unsigned char a = src[i]; - bytes[i] = a; - } - ss << bytes[0] << "." - << bytes[1] << "." - << bytes[2] << "." - << bytes[3]; - return ss.str(); + boost::asio::ip::address_v4::bytes_type bytes; + std::memcpy(bytes.data(), src, 4); + boost::asio::ip::address_v4 addr(bytes); + return addr.to_string(); } -// this obviously will need to change, but is here to reflect the above -// stop-gap measure and to make the tests pass at least... -boost::optional ipv6_to_string(const char* src, size_t len) +static boost::optional ipv6_to_string(const char* src, size_t len) { - if (len < 8) + if (len < 16) { - MERROR("Invalid IPv4 address: " << std::string(src, len)); + MERROR("Invalid IPv6 address with length " << len); return boost::none; } - std::stringstream ss; - unsigned int bytes[8]; - for (int i = 0; i < 8; i++) - { - unsigned char a = src[i]; - bytes[i] = a; - } - ss << bytes[0] << ":" - << bytes[1] << ":" - << bytes[2] << ":" - << bytes[3] << ":" - << bytes[4] << ":" - << bytes[5] << ":" - << bytes[6] << ":" - << bytes[7]; - return ss.str(); + boost::asio::ip::address_v6::bytes_type bytes; + std::memcpy(bytes.data(), src, 16); + boost::asio::ip::address_v6 addr(bytes); + return addr.to_string(); } boost::optional txt_to_string(const char* src, size_t len) From 6da09994b9737bd81bd1e73f009c80f770b76c40 Mon Sep 17 00:00:00 2001 From: jpk68 Date: Fri, 5 Jun 2026 10:04:17 -0400 Subject: [PATCH 2/2] p2p: replace make_address_v4_from_v6 with boost function --- src/p2p/net_node.inl | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index 93735c42b..ff54d5dce 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -64,17 +64,6 @@ #define MIN_WANTED_SEED_NODES 12 -static inline boost::asio::ip::address_v4 make_address_v4_from_v6(const boost::asio::ip::address_v6& a) -{ - const auto &bytes = a.to_bytes(); - uint32_t v4 = 0; - v4 = (v4 << 8) | bytes[12]; - v4 = (v4 << 8) | bytes[13]; - v4 = (v4 << 8) | bytes[14]; - v4 = (v4 << 8) | bytes[15]; - return boost::asio::ip::address_v4(v4); -} - namespace nodetool { template @@ -1566,7 +1555,7 @@ namespace nodetool const boost::asio::ip::address_v6 actual_ip = address.as().ip(); if (actual_ip.is_v4_mapped()) { - boost::asio::ip::address_v4 v4ip = make_address_v4_from_v6(actual_ip); + auto v4ip = boost::asio::ip::make_address_v4(boost::asio::ip::v4_mapped, actual_ip); uint32_t actual_ipv4; memcpy(&actual_ipv4, v4ip.to_bytes().data(), sizeof(actual_ipv4)); return epee::net_utils::ipv4_network_address(actual_ipv4, 0).host_str(); @@ -1632,7 +1621,7 @@ namespace nodetool const boost::asio::ip::address_v6 &actual_ip = na.as().ip(); if (actual_ip.is_v4_mapped()) { - boost::asio::ip::address_v4 v4ip = make_address_v4_from_v6(actual_ip); + auto v4ip = boost::asio::ip::make_address_v4(boost::asio::ip::v4_mapped, actual_ip); uint32_t actual_ipv4; memcpy(&actual_ipv4, v4ip.to_bytes().data(), sizeof(actual_ipv4)); connected_subnets.insert(actual_ipv4 & subnet_mask); @@ -1688,7 +1677,7 @@ namespace nodetool const boost::asio::ip::address_v6 &actual_ip = na.as().ip(); if (actual_ip.is_v4_mapped()) { - boost::asio::ip::address_v4 v4ip = make_address_v4_from_v6(actual_ip); + auto v4ip = boost::asio::ip::make_address_v4(boost::asio::ip::v4_mapped, actual_ip); uint32_t actual_ipv4; memcpy(&actual_ipv4, v4ip.to_bytes().data(), sizeof(actual_ipv4)); uint32_t subnet = actual_ipv4 & subnet_mask;