mirror of
https://github.com/monero-project/monero.git
synced 2026-07-28 14:47:15 -07:00
Merge pull request #10788
6da0999p2p: replace make_address_v4_from_v6 with boost function (jpk68)ff1f97bdns_utils: replace address-to-string functions (jpk68) ACKs: selsta, tobtoht
This commit is contained in:
+15
-36
@@ -34,6 +34,7 @@
|
||||
#include <deque>
|
||||
#include <set>
|
||||
#include <stdlib.h>
|
||||
#include <cstring>
|
||||
#include "include_base_utils.h"
|
||||
#include "common/threadpool.h"
|
||||
#include "crypto/crypto.h"
|
||||
@@ -41,6 +42,7 @@
|
||||
#include <boost/algorithm/string/join.hpp>
|
||||
#include <boost/optional.hpp>
|
||||
#include <boost/utility/string_ref.hpp>
|
||||
#include <boost/asio/ip/address.hpp>
|
||||
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<std::string> ipv4_to_string(const char* src, size_t len)
|
||||
static boost::optional<std::string> 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<std::string> ipv6_to_string(const char* src, size_t len)
|
||||
static boost::optional<std::string> 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<std::string> txt_to_string(const char* src, size_t len)
|
||||
|
||||
+3
-14
@@ -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<class t_payload_net_handler>
|
||||
@@ -1566,7 +1555,7 @@ namespace nodetool
|
||||
const boost::asio::ip::address_v6 actual_ip = address.as<const epee::net_utils::ipv6_network_address>().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<const epee::net_utils::ipv6_network_address>().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<const epee::net_utils::ipv6_network_address>().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;
|
||||
|
||||
Reference in New Issue
Block a user