aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVasil Dimov <vd@FreeBSD.org>2023-02-07 13:30:37 +0100
committerVasil Dimov <vd@FreeBSD.org>2023-10-05 15:10:34 +0200
commit6e308651c441cbf8763c67cc099c538c333c2872 (patch)
tree7d4ddc4737c3a19fcb560fc33fe9bfbea56b7f35 /src
parentc42ded3d9bda8b273780a4a81490bbf1b9e9c261 (diff)
net: move IsReachable() code to netbase and encapsulate it
`vfLimited`, `IsReachable()`, `SetReachable()` need not be in the `net` module. Move them to `netbase` because they will be needed in `LookupSubNet()` to possibly flip the result to CJDNS (if that network is reachable). In the process, encapsulate them in a class. `NET_UNROUTABLE` and `NET_INTERNAL` are no longer ignored when adding or removing reachable networks. This was unnecessary.
Diffstat (limited to 'src')
-rw-r--r--src/init.cpp26
-rw-r--r--src/net.cpp35
-rw-r--r--src/net.h10
-rw-r--r--src/net_processing.cpp7
-rw-r--r--src/netbase.cpp2
-rw-r--r--src/netbase.h56
-rw-r--r--src/rpc/net.cpp4
-rw-r--r--src/test/net_tests.cpp96
-rw-r--r--src/torcontrol.cpp2
9 files changed, 135 insertions, 103 deletions
diff --git a/src/init.cpp b/src/init.cpp
index a0b4425898..b289851e5d 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -1305,30 +1305,24 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
}
if (args.IsArgSet("-onlynet")) {
- std::set<enum Network> nets;
+ g_reachable_nets.RemoveAll();
for (const std::string& snet : args.GetArgs("-onlynet")) {
enum Network net = ParseNetwork(snet);
if (net == NET_UNROUTABLE)
return InitError(strprintf(_("Unknown network specified in -onlynet: '%s'"), snet));
- nets.insert(net);
- }
- for (int n = 0; n < NET_MAX; n++) {
- enum Network net = (enum Network)n;
- assert(IsReachable(net));
- if (!nets.count(net))
- SetReachable(net, false);
+ g_reachable_nets.Add(net);
}
}
if (!args.IsArgSet("-cjdnsreachable")) {
- if (args.IsArgSet("-onlynet") && IsReachable(NET_CJDNS)) {
+ if (args.IsArgSet("-onlynet") && g_reachable_nets.Contains(NET_CJDNS)) {
return InitError(
_("Outbound connections restricted to CJDNS (-onlynet=cjdns) but "
"-cjdnsreachable is not provided"));
}
- SetReachable(NET_CJDNS, false);
+ g_reachable_nets.Remove(NET_CJDNS);
}
- // Now IsReachable(NET_CJDNS) is true if:
+ // Now g_reachable_nets.Contains(NET_CJDNS) is true if:
// 1. -cjdnsreachable is given and
// 2.1. -onlynet is not given or
// 2.2. -onlynet=cjdns is given
@@ -1336,7 +1330,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
// Requesting DNS seeds entails connecting to IPv4/IPv6, which -onlynet options may prohibit:
// If -dnsseed=1 is explicitly specified, abort. If it's left unspecified by the user, we skip
// the DNS seeds by adjusting -dnsseed in InitParameterInteraction.
- if (args.GetBoolArg("-dnsseed") == true && !IsReachable(NET_IPV4) && !IsReachable(NET_IPV6)) {
+ if (args.GetBoolArg("-dnsseed") == true && !g_reachable_nets.Contains(NET_IPV4) && !g_reachable_nets.Contains(NET_IPV6)) {
return InitError(strprintf(_("Incompatible options: -dnsseed=1 was explicitly specified, but -onlynet forbids connections to IPv4/IPv6")));
};
@@ -1366,7 +1360,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
onion_proxy = addrProxy;
}
- const bool onlynet_used_with_onion{args.IsArgSet("-onlynet") && IsReachable(NET_ONION)};
+ const bool onlynet_used_with_onion{args.IsArgSet("-onlynet") && g_reachable_nets.Contains(NET_ONION)};
// -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses
// -noonion (or -onion=0) disables connecting to .onion entirely
@@ -1401,7 +1395,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
"reaching the Tor network is not provided: none of -proxy, -onion or "
"-listenonion is given"));
}
- SetReachable(NET_ONION, false);
+ g_reachable_nets.Remove(NET_ONION);
}
for (const std::string& strAddr : args.GetArgs("-externalip")) {
@@ -1876,12 +1870,12 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
}
SetProxy(NET_I2P, Proxy{addr.value()});
} else {
- if (args.IsArgSet("-onlynet") && IsReachable(NET_I2P)) {
+ if (args.IsArgSet("-onlynet") && g_reachable_nets.Contains(NET_I2P)) {
return InitError(
_("Outbound connections restricted to i2p (-onlynet=i2p) but "
"-i2psam is not provided"));
}
- SetReachable(NET_I2P, false);
+ g_reachable_nets.Remove(NET_I2P);
}
connOptions.m_i2p_accept_incoming = args.GetBoolArg("-i2pacceptincoming", DEFAULT_I2P_ACCEPT_INCOMING);
diff --git a/src/net.cpp b/src/net.cpp
index 994abd986d..d20d185e27 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -115,7 +115,6 @@ bool fDiscover = true;
bool fListen = true;
GlobalMutex g_maplocalhost_mutex;
std::map<CNetAddr, LocalServiceInfo> mapLocalHost GUARDED_BY(g_maplocalhost_mutex);
-static bool vfLimited[NET_MAX] GUARDED_BY(g_maplocalhost_mutex) = {};
std::string strSubVersion;
size_t CSerializedNetMsg::GetMemoryUsage() const noexcept
@@ -232,7 +231,7 @@ static int GetnScore(const CService& addr)
{
CService addrLocal = pnode->GetAddrLocal();
return fDiscover && pnode->addr.IsRoutable() && addrLocal.IsRoutable() &&
- IsReachable(addrLocal.GetNetwork());
+ g_reachable_nets.Contains(addrLocal);
}
std::optional<CService> GetLocalAddrForPeer(CNode& node)
@@ -280,7 +279,7 @@ std::optional<CService> GetLocalAddrForPeer(CNode& node)
CService MaybeFlipIPv6toCJDNS(const CService& service)
{
CService ret{service};
- if (ret.IsIPv6() && ret.HasCJDNSPrefix() && IsReachable(NET_CJDNS)) {
+ if (ret.IsIPv6() && ret.HasCJDNSPrefix() && g_reachable_nets.Contains(NET_CJDNS)) {
ret.m_net = NET_CJDNS;
}
return ret;
@@ -297,7 +296,7 @@ bool AddLocal(const CService& addr_, int nScore)
if (!fDiscover && nScore < LOCAL_MANUAL)
return false;
- if (!IsReachable(addr))
+ if (!g_reachable_nets.Contains(addr))
return false;
LogPrintf("AddLocal(%s,%i)\n", addr.ToStringAddrPort(), nScore);
@@ -327,25 +326,6 @@ void RemoveLocal(const CService& addr)
mapLocalHost.erase(addr);
}
-void SetReachable(enum Network net, bool reachable)
-{
- if (net == NET_UNROUTABLE || net == NET_INTERNAL)
- return;
- LOCK(g_maplocalhost_mutex);
- vfLimited[net] = !reachable;
-}
-
-bool IsReachable(enum Network net)
-{
- LOCK(g_maplocalhost_mutex);
- return !vfLimited[net];
-}
-
-bool IsReachable(const CNetAddr &addr)
-{
- return IsReachable(addr.GetNetwork());
-}
-
/** vote for a local address */
bool SeenLocal(const CService& addr)
{
@@ -2433,7 +2413,7 @@ std::unordered_set<Network> CConnman::GetReachableEmptyNetworks() const
for (int n = 0; n < NET_MAX; n++) {
enum Network net = (enum Network)n;
if (net == NET_UNROUTABLE || net == NET_INTERNAL) continue;
- if (IsReachable(net) && addrman.Size(net, std::nullopt) == 0) {
+ if (g_reachable_nets.Contains(net) && addrman.Size(net, std::nullopt) == 0) {
networks.insert(net);
}
}
@@ -2453,7 +2433,7 @@ bool CConnman::MaybePickPreferredNetwork(std::optional<Network>& network)
LOCK(m_nodes_mutex);
for (const auto net : nets) {
- if (IsReachable(net) && m_network_conn_counts[net] == 0 && addrman.Size(net) != 0) {
+ if (g_reachable_nets.Contains(net) && m_network_conn_counts[net] == 0 && addrman.Size(net) != 0) {
network = net;
return true;
}
@@ -2683,7 +2663,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
if (anchor && !m_anchors.empty()) {
const CAddress addr = m_anchors.back();
m_anchors.pop_back();
- if (!addr.IsValid() || IsLocal(addr) || !IsReachable(addr) ||
+ if (!addr.IsValid() || IsLocal(addr) || !g_reachable_nets.Contains(addr) ||
!HasAllDesirableServiceFlags(addr.nServices) ||
outbound_ipv46_peer_netgroups.count(m_netgroupman.GetGroup(addr))) continue;
addrConnect = addr;
@@ -2738,8 +2718,9 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
break;
}
- if (!IsReachable(addr))
+ if (!g_reachable_nets.Contains(addr)) {
continue;
+ }
// only consider very recently tried nodes after 30 failed attempts
if (current_time - addr_last_try < 10min && nTries < 30) {
diff --git a/src/net.h b/src/net.h
index e8e31f72e4..9a517ac332 100644
--- a/src/net.h
+++ b/src/net.h
@@ -160,16 +160,6 @@ enum
/** Returns a local address that we should advertise to this peer. */
std::optional<CService> GetLocalAddrForPeer(CNode& node);
-/**
- * Mark a network as reachable or unreachable (no automatic connects to it)
- * @note Networks are reachable by default
- */
-void SetReachable(enum Network net, bool reachable);
-/** @returns true if the network is reachable, false otherwise */
-bool IsReachable(enum Network net);
-/** @returns true if the address is in a reachable network, false otherwise */
-bool IsReachable(const CNetAddr& addr);
-
bool AddLocal(const CService& addr, int nScore = LOCAL_NONE);
bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE);
void RemoveLocal(const CService& addr);
diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index 06086d6804..298531f66f 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -3823,14 +3823,15 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
continue;
}
++num_proc;
- bool fReachable = IsReachable(addr);
+ const bool reachable{g_reachable_nets.Contains(addr)};
if (addr.nTime > current_a_time - 10min && !peer->m_getaddr_sent && vAddr.size() <= 10 && addr.IsRoutable()) {
// Relay to a limited number of other nodes
- RelayAddress(pfrom.GetId(), addr, fReachable);
+ RelayAddress(pfrom.GetId(), addr, reachable);
}
// Do not store addresses outside our network
- if (fReachable)
+ if (reachable) {
vAddrOk.push_back(addr);
+ }
}
peer->m_addr_processed += num_proc;
peer->m_addr_rate_limited += num_rate_limit;
diff --git a/src/netbase.cpp b/src/netbase.cpp
index ca1a80d72f..16b5e1c70d 100644
--- a/src/netbase.cpp
+++ b/src/netbase.cpp
@@ -32,6 +32,8 @@ bool fNameLookup = DEFAULT_NAME_LOOKUP;
std::chrono::milliseconds g_socks5_recv_timeout = 20s;
static std::atomic<bool> interruptSocks5Recv(false);
+ReachableNets g_reachable_nets;
+
std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup)
{
addrinfo ai_hint{};
diff --git a/src/netbase.h b/src/netbase.h
index 1da4f5c51d..aaa5229b82 100644
--- a/src/netbase.h
+++ b/src/netbase.h
@@ -19,6 +19,7 @@
#include <stdint.h>
#include <string>
#include <type_traits>
+#include <unordered_set>
#include <vector>
extern int nConnectTimeout;
@@ -65,6 +66,61 @@ struct ProxyCredentials
};
/**
+ * List of reachable networks. Everything is reachable by default.
+ */
+class ReachableNets {
+public:
+ void Add(Network net) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
+ {
+ AssertLockNotHeld(m_mutex);
+ LOCK(m_mutex);
+ m_reachable.insert(net);
+ }
+
+ void Remove(Network net) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
+ {
+ AssertLockNotHeld(m_mutex);
+ LOCK(m_mutex);
+ m_reachable.erase(net);
+ }
+
+ void RemoveAll() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
+ {
+ AssertLockNotHeld(m_mutex);
+ LOCK(m_mutex);
+ m_reachable.clear();
+ }
+
+ [[nodiscard]] bool Contains(Network net) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
+ {
+ AssertLockNotHeld(m_mutex);
+ LOCK(m_mutex);
+ return m_reachable.count(net) > 0;
+ }
+
+ [[nodiscard]] bool Contains(const CNetAddr& addr) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
+ {
+ AssertLockNotHeld(m_mutex);
+ return Contains(addr.GetNetwork());
+ }
+
+private:
+ mutable Mutex m_mutex;
+
+ std::unordered_set<Network> m_reachable GUARDED_BY(m_mutex){
+ NET_UNROUTABLE,
+ NET_IPV4,
+ NET_IPV6,
+ NET_ONION,
+ NET_I2P,
+ NET_CJDNS,
+ NET_INTERNAL
+ };
+};
+
+extern ReachableNets g_reachable_nets;
+
+/**
* Wrapper for getaddrinfo(3). Do not use directly: call Lookup/LookupHost/LookupNumeric/LookupSubNet.
*/
std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup);
diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp
index 96d06b6b9f..07f61a084d 100644
--- a/src/rpc/net.cpp
+++ b/src/rpc/net.cpp
@@ -585,8 +585,8 @@ static UniValue GetNetworksInfo()
UniValue obj(UniValue::VOBJ);
GetProxy(network, proxy);
obj.pushKV("name", GetNetworkName(network));
- obj.pushKV("limited", !IsReachable(network));
- obj.pushKV("reachable", IsReachable(network));
+ obj.pushKV("limited", !g_reachable_nets.Contains(network));
+ obj.pushKV("reachable", g_reachable_nets.Contains(network));
obj.pushKV("proxy", proxy.IsValid() ? proxy.proxy.ToStringAddrPort() : std::string());
obj.pushKV("proxy_randomize_credentials", proxy.randomize_credentials);
networks.push_back(obj);
diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp
index 5976aa3713..81b1283663 100644
--- a/src/test/net_tests.cpp
+++ b/src/test/net_tests.cpp
@@ -718,47 +718,55 @@ BOOST_AUTO_TEST_CASE(get_local_addr_for_peer_port)
BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network)
{
- BOOST_CHECK(IsReachable(NET_IPV4));
- BOOST_CHECK(IsReachable(NET_IPV6));
- BOOST_CHECK(IsReachable(NET_ONION));
- BOOST_CHECK(IsReachable(NET_I2P));
- BOOST_CHECK(IsReachable(NET_CJDNS));
-
- SetReachable(NET_IPV4, false);
- SetReachable(NET_IPV6, false);
- SetReachable(NET_ONION, false);
- SetReachable(NET_I2P, false);
- SetReachable(NET_CJDNS, false);
-
- BOOST_CHECK(!IsReachable(NET_IPV4));
- BOOST_CHECK(!IsReachable(NET_IPV6));
- BOOST_CHECK(!IsReachable(NET_ONION));
- BOOST_CHECK(!IsReachable(NET_I2P));
- BOOST_CHECK(!IsReachable(NET_CJDNS));
-
- SetReachable(NET_IPV4, true);
- SetReachable(NET_IPV6, true);
- SetReachable(NET_ONION, true);
- SetReachable(NET_I2P, true);
- SetReachable(NET_CJDNS, true);
-
- BOOST_CHECK(IsReachable(NET_IPV4));
- BOOST_CHECK(IsReachable(NET_IPV6));
- BOOST_CHECK(IsReachable(NET_ONION));
- BOOST_CHECK(IsReachable(NET_I2P));
- BOOST_CHECK(IsReachable(NET_CJDNS));
+ BOOST_CHECK(g_reachable_nets.Contains(NET_IPV4));
+ BOOST_CHECK(g_reachable_nets.Contains(NET_IPV6));
+ BOOST_CHECK(g_reachable_nets.Contains(NET_ONION));
+ BOOST_CHECK(g_reachable_nets.Contains(NET_I2P));
+ BOOST_CHECK(g_reachable_nets.Contains(NET_CJDNS));
+
+ g_reachable_nets.Remove(NET_IPV4);
+ g_reachable_nets.Remove(NET_IPV6);
+ g_reachable_nets.Remove(NET_ONION);
+ g_reachable_nets.Remove(NET_I2P);
+ g_reachable_nets.Remove(NET_CJDNS);
+
+ BOOST_CHECK(!g_reachable_nets.Contains(NET_IPV4));
+ BOOST_CHECK(!g_reachable_nets.Contains(NET_IPV6));
+ BOOST_CHECK(!g_reachable_nets.Contains(NET_ONION));
+ BOOST_CHECK(!g_reachable_nets.Contains(NET_I2P));
+ BOOST_CHECK(!g_reachable_nets.Contains(NET_CJDNS));
+
+ g_reachable_nets.Add(NET_IPV4);
+ g_reachable_nets.Add(NET_IPV6);
+ g_reachable_nets.Add(NET_ONION);
+ g_reachable_nets.Add(NET_I2P);
+ g_reachable_nets.Add(NET_CJDNS);
+
+ BOOST_CHECK(g_reachable_nets.Contains(NET_IPV4));
+ BOOST_CHECK(g_reachable_nets.Contains(NET_IPV6));
+ BOOST_CHECK(g_reachable_nets.Contains(NET_ONION));
+ BOOST_CHECK(g_reachable_nets.Contains(NET_I2P));
+ BOOST_CHECK(g_reachable_nets.Contains(NET_CJDNS));
}
BOOST_AUTO_TEST_CASE(LimitedAndReachable_NetworkCaseUnroutableAndInternal)
{
- BOOST_CHECK(IsReachable(NET_UNROUTABLE));
- BOOST_CHECK(IsReachable(NET_INTERNAL));
-
- SetReachable(NET_UNROUTABLE, false);
- SetReachable(NET_INTERNAL, false);
-
- BOOST_CHECK(IsReachable(NET_UNROUTABLE)); // Ignored for both networks
- BOOST_CHECK(IsReachable(NET_INTERNAL));
+ // Should be reachable by default.
+ BOOST_CHECK(g_reachable_nets.Contains(NET_UNROUTABLE));
+ BOOST_CHECK(g_reachable_nets.Contains(NET_INTERNAL));
+
+ g_reachable_nets.RemoveAll();
+
+ BOOST_CHECK(!g_reachable_nets.Contains(NET_UNROUTABLE));
+ BOOST_CHECK(!g_reachable_nets.Contains(NET_INTERNAL));
+
+ g_reachable_nets.Add(NET_IPV4);
+ g_reachable_nets.Add(NET_IPV6);
+ g_reachable_nets.Add(NET_ONION);
+ g_reachable_nets.Add(NET_I2P);
+ g_reachable_nets.Add(NET_CJDNS);
+ g_reachable_nets.Add(NET_UNROUTABLE);
+ g_reachable_nets.Add(NET_INTERNAL);
}
CNetAddr UtilBuildAddress(unsigned char p1, unsigned char p2, unsigned char p3, unsigned char p4)
@@ -776,13 +784,13 @@ BOOST_AUTO_TEST_CASE(LimitedAndReachable_CNetAddr)
{
CNetAddr addr = UtilBuildAddress(0x001, 0x001, 0x001, 0x001); // 1.1.1.1
- SetReachable(NET_IPV4, true);
- BOOST_CHECK(IsReachable(addr));
+ g_reachable_nets.Add(NET_IPV4);
+ BOOST_CHECK(g_reachable_nets.Contains(addr));
- SetReachable(NET_IPV4, false);
- BOOST_CHECK(!IsReachable(addr));
+ g_reachable_nets.Remove(NET_IPV4);
+ BOOST_CHECK(!g_reachable_nets.Contains(addr));
- SetReachable(NET_IPV4, true); // have to reset this, because this is stateful.
+ g_reachable_nets.Add(NET_IPV4); // have to reset this, because this is stateful.
}
@@ -790,7 +798,7 @@ BOOST_AUTO_TEST_CASE(LocalAddress_BasicLifecycle)
{
CService addr = CService(UtilBuildAddress(0x002, 0x001, 0x001, 0x001), 1000); // 2.1.1.1:1000
- SetReachable(NET_IPV4, true);
+ g_reachable_nets.Add(NET_IPV4);
BOOST_CHECK(!IsLocal(addr));
BOOST_CHECK(AddLocal(addr, 1000));
@@ -915,7 +923,7 @@ BOOST_AUTO_TEST_CASE(advertise_local_address)
ConnectionType::OUTBOUND_FULL_RELAY,
/*inbound_onion=*/false);
};
- SetReachable(NET_CJDNS, true);
+ g_reachable_nets.Add(NET_CJDNS);
CAddress addr_ipv4{Lookup("1.2.3.4", 8333, false).value(), NODE_NONE};
BOOST_REQUIRE(addr_ipv4.IsValid());
diff --git a/src/torcontrol.cpp b/src/torcontrol.cpp
index 4c99aa5746..60cf31a964 100644
--- a/src/torcontrol.cpp
+++ b/src/torcontrol.cpp
@@ -409,7 +409,7 @@ void TorController::get_socks_cb(TorControlConnection& _conn, const TorControlRe
//
// If NET_ONION is not reachable, then none of -proxy or -onion was given.
// Since we are here, then -torcontrol and -torpassword were given.
- SetReachable(NET_ONION, true);
+ g_reachable_nets.Add(NET_ONION);
}
}