p2p: allow CConnman::GetAddresses() by network, add doxygen

This commit is contained in:
Jon Atack committed 2021-05-19 13:05:54 +02:00
1 parent a49f3ddbba
commit 80ba294854
5 files changed
+24 -8

No files matched your search

+4 -3
View File
@@ -16,6 +16,7 @@
#include <crypto/sha256.h> #include <crypto/sha256.h>
#include <i2p.h> #include <i2p.h>
#include <net_permissions.h> #include <net_permissions.h>
#include <netaddress.h>
#include <netbase.h> #include <netbase.h>
#include <node/ui_interface.h> #include <node/ui_interface.h>
#include <protocol.h> #include <protocol.h>
@@ -2669,9 +2670,9 @@ CConnman::~CConnman()
Stop(); Stop();
} }
std::vector<CAddress> CConnman::GetAddresses(size_t max_addresses, size_t max_pct) const std::vector<CAddress> CConnman::GetAddresses(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
{ {
std::vector<CAddress> addresses = addrman.GetAddr(max_addresses, max_pct, /* network */ std::nullopt); std::vector<CAddress> addresses = addrman.GetAddr(max_addresses, max_pct, network);
if (m_banman) { if (m_banman) {
addresses.erase(std::remove_if(addresses.begin(), addresses.end(), addresses.erase(std::remove_if(addresses.begin(), addresses.end(),
[this](const CAddress& addr){return m_banman->IsDiscouraged(addr) || m_banman->IsBanned(addr);}), [this](const CAddress& addr){return m_banman->IsDiscouraged(addr) || m_banman->IsBanned(addr);}),
@@ -2691,7 +2692,7 @@ std::vector<CAddress> CConnman::GetAddresses(CNode& requestor, size_t max_addres
auto r = m_addr_response_caches.emplace(cache_id, CachedAddrResponse{}); auto r = m_addr_response_caches.emplace(cache_id, CachedAddrResponse{});
CachedAddrResponse& cache_entry = r.first->second; CachedAddrResponse& cache_entry = r.first->second;
if (cache_entry.m_cache_entry_expiration < current_time) { // If emplace() added new one it has expiration 0. 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 // Choosing a proper cache lifetime is a trade-off between the privacy leak minimization
// and the usefulness of ADDR responses to honest users. // and the usefulness of ADDR responses to honest users.
// //
+8 -1
View File
@@ -923,7 +923,14 @@ public:
}; };
// Addrman functions // Addrman functions
std::vector<CAddress> 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<CAddress> GetAddresses(size_t max_addresses, size_t max_pct, std::optional<Network> network) const;
/** /**
* Cache is used to minimize topology leaks, so it should * Cache is used to minimize topology leaks, so it should
* be used for all non-trusted calls, for example, p2p. * be used for all non-trusted calls, for example, p2p.
+1 -1
View File
@@ -3586,7 +3586,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
pfrom.vAddrToSend.clear(); pfrom.vAddrToSend.clear();
std::vector<CAddress> vAddr; std::vector<CAddress> vAddr;
if (pfrom.HasPermission(NetPermissionFlags::Addr)) { 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 { } else {
vAddr = m_connman.GetAddresses(pfrom, MAX_ADDR_TO_SEND, MAX_PCT_ADDR_TO_SEND); vAddr = m_connman.GetAddresses(pfrom, MAX_ADDR_TO_SEND, MAX_PCT_ADDR_TO_SEND);
} }
+3 -1
View File
@@ -28,6 +28,8 @@
#include <version.h> #include <version.h>
#include <warnings.h> #include <warnings.h>
#include <optional>
#include <univalue.h> #include <univalue.h>
const std::vector<std::string> CONNECTION_TYPE_DOC{ const std::vector<std::string> CONNECTION_TYPE_DOC{
@@ -878,7 +880,7 @@ static RPCHelpMan getnodeaddresses()
if (count < 0) throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range"); if (count < 0) throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range");
// returns a shuffled list of CAddress // returns a shuffled list of CAddress
const std::vector<CAddress> vAddr{connman.GetAddresses(count, /* max_pct */ 0)}; const std::vector<CAddress> vAddr{connman.GetAddresses(count, /* max_pct */ 0, /* network */ std::nullopt)};
UniValue ret(UniValue::VARR); UniValue ret(UniValue::VARR);
for (const CAddress& addr : vAddr) { for (const CAddress& addr : vAddr) {
+8 -2
View File
@@ -71,10 +71,16 @@ FUZZ_TARGET_INIT(connman, initialize_connman)
(void)connman.ForNode(fuzzed_data_provider.ConsumeIntegral<NodeId>(), [&](auto) { return fuzzed_data_provider.ConsumeBool(); }); (void)connman.ForNode(fuzzed_data_provider.ConsumeIntegral<NodeId>(), [&](auto) { return fuzzed_data_provider.ConsumeBool(); });
}, },
[&] { [&] {
(void)connman.GetAddresses(fuzzed_data_provider.ConsumeIntegral<size_t>(), fuzzed_data_provider.ConsumeIntegral<size_t>()); (void)connman.GetAddresses(
/* max_addresses */ fuzzed_data_provider.ConsumeIntegral<size_t>(),
/* max_pct */ fuzzed_data_provider.ConsumeIntegral<size_t>(),
/* network */ std::nullopt);
}, },
[&] { [&] {
(void)connman.GetAddresses(random_node, fuzzed_data_provider.ConsumeIntegral<size_t>(), fuzzed_data_provider.ConsumeIntegral<size_t>()); (void)connman.GetAddresses(
/* requestor */ random_node,
/* max_addresses */ fuzzed_data_provider.ConsumeIntegral<size_t>(),
/* max_pct */ fuzzed_data_provider.ConsumeIntegral<size_t>());
}, },
[&] { [&] {
(void)connman.GetDeterministicRandomizer(fuzzed_data_provider.ConsumeIntegral<uint64_t>()); (void)connman.GetDeterministicRandomizer(fuzzed_data_provider.ConsumeIntegral<uint64_t>());