From 80ba294854e5025bcada58f1403858e6ea1d4380 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 2 May 2021 19:05:42 +0200 Subject: [PATCH] p2p: allow CConnman::GetAddresses() by network, add doxygen --- src/net.cpp | 7 ++++--- src/net.h | 9 ++++++++- src/net_processing.cpp | 2 +- src/rpc/net.cpp | 4 +++- src/test/fuzz/connman.cpp | 10 ++++++++-- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index dfdf9eb8fa..1322c971fb 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -2669,9 +2670,9 @@ CConnman::~CConnman() Stop(); } -std::vector CConnman::GetAddresses(size_t max_addresses, size_t max_pct) const +std::vector CConnman::GetAddresses(size_t max_addresses, size_t max_pct, std::optional network) const { - std::vector addresses = addrman.GetAddr(max_addresses, max_pct, /* network */ std::nullopt); + std::vector addresses = addrman.GetAddr(max_addresses, max_pct, network); if (m_banman) { addresses.erase(std::remove_if(addresses.begin(), addresses.end(), [this](const CAddress& addr){return m_banman->IsDiscouraged(addr) || m_banman->IsBanned(addr);}), @@ -2691,7 +2692,7 @@ std::vector CConnman::GetAddresses(CNode& requestor, size_t max_addres auto r = m_addr_response_caches.emplace(cache_id, CachedAddrResponse{}); CachedAddrResponse& cache_entry = r.first->second; if (cache_entry.m_cache_entry_expiration < current_time) { // If emplace() added new one it has expiration 0. - cache_entry.m_addrs_response_cache = GetAddresses(max_addresses, max_pct); + cache_entry.m_addrs_response_cache = GetAddresses(max_addresses, max_pct, /* network */ std::nullopt); // Choosing a proper cache lifetime is a trade-off between the privacy leak minimization // and the usefulness of ADDR responses to honest users. // diff --git a/src/net.h b/src/net.h index 65b262e573..28f2d9ace6 100644 --- a/src/net.h +++ b/src/net.h @@ -923,7 +923,14 @@ public: }; // Addrman functions - std::vector GetAddresses(size_t max_addresses, size_t max_pct) const; + /** + * Return all or many randomly selected addresses, optionally by network. + * + * @param[in] max_addresses Maximum number of addresses to return (0 = all). + * @param[in] max_pct Maximum percentage of addresses to return (0 = all). + * @param[in] network Select only addresses of this network (nullopt = all). + */ + std::vector GetAddresses(size_t max_addresses, size_t max_pct, std::optional network) const; /** * Cache is used to minimize topology leaks, so it should * be used for all non-trusted calls, for example, p2p. diff --git a/src/net_processing.cpp b/src/net_processing.cpp index fdd36835c2..cee2419610 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -3586,7 +3586,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, pfrom.vAddrToSend.clear(); std::vector vAddr; if (pfrom.HasPermission(NetPermissionFlags::Addr)) { - vAddr = m_connman.GetAddresses(MAX_ADDR_TO_SEND, MAX_PCT_ADDR_TO_SEND); + vAddr = m_connman.GetAddresses(MAX_ADDR_TO_SEND, MAX_PCT_ADDR_TO_SEND, /* network */ std::nullopt); } else { vAddr = m_connman.GetAddresses(pfrom, MAX_ADDR_TO_SEND, MAX_PCT_ADDR_TO_SEND); } diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 1f6b6e8d7e..18e4355e88 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -28,6 +28,8 @@ #include #include +#include + #include const std::vector CONNECTION_TYPE_DOC{ @@ -878,7 +880,7 @@ static RPCHelpMan getnodeaddresses() if (count < 0) throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range"); // returns a shuffled list of CAddress - const std::vector vAddr{connman.GetAddresses(count, /* max_pct */ 0)}; + const std::vector vAddr{connman.GetAddresses(count, /* max_pct */ 0, /* network */ std::nullopt)}; UniValue ret(UniValue::VARR); for (const CAddress& addr : vAddr) { diff --git a/src/test/fuzz/connman.cpp b/src/test/fuzz/connman.cpp index e07f25dedf..3e9998af30 100644 --- a/src/test/fuzz/connman.cpp +++ b/src/test/fuzz/connman.cpp @@ -71,10 +71,16 @@ FUZZ_TARGET_INIT(connman, initialize_connman) (void)connman.ForNode(fuzzed_data_provider.ConsumeIntegral(), [&](auto) { return fuzzed_data_provider.ConsumeBool(); }); }, [&] { - (void)connman.GetAddresses(fuzzed_data_provider.ConsumeIntegral(), fuzzed_data_provider.ConsumeIntegral()); + (void)connman.GetAddresses( + /* max_addresses */ fuzzed_data_provider.ConsumeIntegral(), + /* max_pct */ fuzzed_data_provider.ConsumeIntegral(), + /* network */ std::nullopt); }, [&] { - (void)connman.GetAddresses(random_node, fuzzed_data_provider.ConsumeIntegral(), fuzzed_data_provider.ConsumeIntegral()); + (void)connman.GetAddresses( + /* requestor */ random_node, + /* max_addresses */ fuzzed_data_provider.ConsumeIntegral(), + /* max_pct */ fuzzed_data_provider.ConsumeIntegral()); }, [&] { (void)connman.GetDeterministicRandomizer(fuzzed_data_provider.ConsumeIntegral());