From ffdf1fb1deb0d1712a6bdfbfd4638c0a731c3626 Mon Sep 17 00:00:00 2001 From: moneromooo Date: Tue, 27 Oct 2020 22:02:41 +0000 Subject: [PATCH 1/3] p2p: rewrite boost's make_address_v4 to cater for < 1.66 --- src/p2p/net_node.inl | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index 2f47de0f0..c3d594c99 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -71,6 +71,17 @@ #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 @@ -1484,36 +1495,32 @@ namespace nodetool const uint32_t actual_ip = na.as().ip(); classB.insert(actual_ip & 0x0000ffff); } -#if BOOST_VERSION > 106600 else if (cntxt.m_remote_address.get_type_id() == epee::net_utils::ipv6_network_address::get_type_id()) { const epee::net_utils::network_address na = cntxt.m_remote_address; 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(boost::asio::ip::v4_mapped, actual_ip); + boost::asio::ip::address_v4 v4ip = make_address_v4_from_v6(actual_ip); uint32_t actual_ipv4; memcpy(&actual_ipv4, v4ip.to_bytes().data(), sizeof(actual_ipv4)); classB.insert(actual_ipv4 & ntohl(0xffff0000)); } } -#endif return true; }); } auto get_host_string = [](const epee::net_utils::network_address &address) { -#if BOOST_VERSION > 106600 if (address.get_type_id() == epee::net_utils::ipv6_network_address::get_type_id()) { 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(boost::asio::ip::v4_mapped, actual_ip); + boost::asio::ip::address_v4 v4ip = make_address_v4_from_v6(actual_ip); return epee::net_utils::ipv4_network_address(v4ip.to_uint(), 0).host_str(); } } -#endif return address.host_str(); }; std::unordered_set hosts_added; @@ -1533,20 +1540,18 @@ namespace nodetool uint32_t actual_ip = na.as().ip(); skip = classB.find(actual_ip & 0x0000ffff) != classB.end(); } -#if BOOST_VERSION > 106600 else if (skip_duplicate_class_B && pe.adr.get_type_id() == epee::net_utils::ipv6_network_address::get_type_id()) { const epee::net_utils::network_address na = pe.adr; 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(boost::asio::ip::v4_mapped, actual_ip); + boost::asio::ip::address_v4 v4ip = make_address_v4_from_v6(actual_ip); uint32_t actual_ipv4; memcpy(&actual_ipv4, v4ip.to_bytes().data(), sizeof(actual_ipv4)); skip = classB.find(actual_ipv4 & ntohl(0xffff0000)) != classB.end(); } } -#endif // consider each host once, to avoid giving undue inflence to hosts running several nodes if (!skip) From 67f2bec2dfa463aa3b4fbd115132b6cd4ca495e5 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Wed, 4 Nov 2020 23:01:23 +0000 Subject: [PATCH 2/3] p2p: make this work with boost <= 1.65 (pffff) --- src/p2p/net_node.inl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index c3d594c99..7976b6fa0 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -1518,7 +1518,11 @@ namespace nodetool if (actual_ip.is_v4_mapped()) { boost::asio::ip::address_v4 v4ip = make_address_v4_from_v6(actual_ip); +#if BOOST_VERSION >= 106600 return epee::net_utils::ipv4_network_address(v4ip.to_uint(), 0).host_str(); +#else + return epee::net_utils::ipv4_network_address(v4ip.to_ulong(), 0).host_str(); +#endif } } return address.host_str(); From c64b94bd14f1f04aa1f8d3069dbf9ea7bb5cfcac Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Thu, 5 Nov 2020 16:05:54 +0000 Subject: [PATCH 3/3] p2p: fix endianness when checking IPv6 addresses mapping to IPv4 --- src/p2p/net_node.inl | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index 7976b6fa0..41377775a 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -1518,11 +1518,9 @@ namespace nodetool if (actual_ip.is_v4_mapped()) { boost::asio::ip::address_v4 v4ip = make_address_v4_from_v6(actual_ip); -#if BOOST_VERSION >= 106600 - return epee::net_utils::ipv4_network_address(v4ip.to_uint(), 0).host_str(); -#else - return epee::net_utils::ipv4_network_address(v4ip.to_ulong(), 0).host_str(); -#endif + 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(); } } return address.host_str();