Skip to content

Commit 80ba294

Browse files
committed
p2p: allow CConnman::GetAddresses() by network, add doxygen
1 parent a49f3dd commit 80ba294

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

src/net.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <crypto/sha256.h>
1717
#include <i2p.h>
1818
#include <net_permissions.h>
19+
#include <netaddress.h>
1920
#include <netbase.h>
2021
#include <node/ui_interface.h>
2122
#include <protocol.h>
@@ -2669,9 +2670,9 @@ CConnman::~CConnman()
26692670
Stop();
26702671
}
26712672

2672-
std::vector<CAddress> CConnman::GetAddresses(size_t max_addresses, size_t max_pct) const
2673+
std::vector<CAddress> CConnman::GetAddresses(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
26732674
{
2674-
std::vector<CAddress> addresses = addrman.GetAddr(max_addresses, max_pct, /* network */ std::nullopt);
2675+
std::vector<CAddress> addresses = addrman.GetAddr(max_addresses, max_pct, network);
26752676
if (m_banman) {
26762677
addresses.erase(std::remove_if(addresses.begin(), addresses.end(),
26772678
[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
26912692
auto r = m_addr_response_caches.emplace(cache_id, CachedAddrResponse{});
26922693
CachedAddrResponse& cache_entry = r.first->second;
26932694
if (cache_entry.m_cache_entry_expiration < current_time) { // If emplace() added new one it has expiration 0.
2694-
cache_entry.m_addrs_response_cache = GetAddresses(max_addresses, max_pct);
2695+
cache_entry.m_addrs_response_cache = GetAddresses(max_addresses, max_pct, /* network */ std::nullopt);
26952696
// Choosing a proper cache lifetime is a trade-off between the privacy leak minimization
26962697
// and the usefulness of ADDR responses to honest users.
26972698
//

src/net.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,14 @@ class CConnman
923923
};
924924

925925
// Addrman functions
926-
std::vector<CAddress> GetAddresses(size_t max_addresses, size_t max_pct) const;
926+
/**
927+
* Return all or many randomly selected addresses, optionally by network.
928+
*
929+
* @param[in] max_addresses Maximum number of addresses to return (0 = all).
930+
* @param[in] max_pct Maximum percentage of addresses to return (0 = all).
931+
* @param[in] network Select only addresses of this network (nullopt = all).
932+
*/
933+
std::vector<CAddress> GetAddresses(size_t max_addresses, size_t max_pct, std::optional<Network> network) const;
927934
/**
928935
* Cache is used to minimize topology leaks, so it should
929936
* be used for all non-trusted calls, for example, p2p.

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3586,7 +3586,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
35863586
pfrom.vAddrToSend.clear();
35873587
std::vector<CAddress> vAddr;
35883588
if (pfrom.HasPermission(NetPermissionFlags::Addr)) {
3589-
vAddr = m_connman.GetAddresses(MAX_ADDR_TO_SEND, MAX_PCT_ADDR_TO_SEND);
3589+
vAddr = m_connman.GetAddresses(MAX_ADDR_TO_SEND, MAX_PCT_ADDR_TO_SEND, /* network */ std::nullopt);
35903590
} else {
35913591
vAddr = m_connman.GetAddresses(pfrom, MAX_ADDR_TO_SEND, MAX_PCT_ADDR_TO_SEND);
35923592
}

src/rpc/net.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <version.h>
2929
#include <warnings.h>
3030

31+
#include <optional>
32+
3133
#include <univalue.h>
3234

3335
const std::vector<std::string> CONNECTION_TYPE_DOC{
@@ -878,7 +880,7 @@ static RPCHelpMan getnodeaddresses()
878880
if (count < 0) throw JSONRPCError(RPC_INVALID_PARAMETER, "Address count out of range");
879881

880882
// returns a shuffled list of CAddress
881-
const std::vector<CAddress> vAddr{connman.GetAddresses(count, /* max_pct */ 0)};
883+
const std::vector<CAddress> vAddr{connman.GetAddresses(count, /* max_pct */ 0, /* network */ std::nullopt)};
882884
UniValue ret(UniValue::VARR);
883885

884886
for (const CAddress& addr : vAddr) {

src/test/fuzz/connman.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,16 @@ FUZZ_TARGET_INIT(connman, initialize_connman)
7171
(void)connman.ForNode(fuzzed_data_provider.ConsumeIntegral<NodeId>(), [&](auto) { return fuzzed_data_provider.ConsumeBool(); });
7272
},
7373
[&] {
74-
(void)connman.GetAddresses(fuzzed_data_provider.ConsumeIntegral<size_t>(), fuzzed_data_provider.ConsumeIntegral<size_t>());
74+
(void)connman.GetAddresses(
75+
/* max_addresses */ fuzzed_data_provider.ConsumeIntegral<size_t>(),
76+
/* max_pct */ fuzzed_data_provider.ConsumeIntegral<size_t>(),
77+
/* network */ std::nullopt);
7578
},
7679
[&] {
77-
(void)connman.GetAddresses(random_node, fuzzed_data_provider.ConsumeIntegral<size_t>(), fuzzed_data_provider.ConsumeIntegral<size_t>());
80+
(void)connman.GetAddresses(
81+
/* requestor */ random_node,
82+
/* max_addresses */ fuzzed_data_provider.ConsumeIntegral<size_t>(),
83+
/* max_pct */ fuzzed_data_provider.ConsumeIntegral<size_t>());
7884
},
7985
[&] {
8086
(void)connman.GetDeterministicRandomizer(fuzzed_data_provider.ConsumeIntegral<uint64_t>());

0 commit comments

Comments
 (0)