diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -1978,7 +1978,9 @@ } for (int n = 0; n < NET_MAX; n++) { enum Network net = (enum Network)n; - if (!nets.count(net)) SetLimited(net); + if (!nets.count(net)) { + SetReachable(net, false); + } } } @@ -1992,7 +1994,7 @@ // -noproxy (or -proxy=0) as well as the empty string can be used to not set // a proxy, this is the default std::string proxyArg = gArgs.GetArg("-proxy", ""); - SetLimited(NET_ONION); + SetReachable(NET_ONION, false); if (proxyArg != "" && proxyArg != "0") { CService proxyAddr; if (!Lookup(proxyArg.c_str(), proxyAddr, 9050, fNameLookup)) { @@ -2011,7 +2013,7 @@ SetProxy(NET_ONION, addrProxy); SetNameProxy(addrProxy); // by default, -proxy sets onion as reachable, unless -noonion later - SetLimited(NET_ONION, false); + SetReachable(NET_ONION, true); } // -onion can be used to set only a proxy for .onion, or override normal @@ -2021,8 +2023,9 @@ // to -proxy set above, or none) std::string onionArg = gArgs.GetArg("-onion", ""); if (onionArg != "") { - if (onionArg == "0") { // Handle -noonion/-onion=0 - SetLimited(NET_ONION); // set onions as unreachable + if (onionArg == "0") { + // Handle -noonion/-onion=0 + SetReachable(NET_ONION, false); } else { CService onionProxy; if (!Lookup(onionArg.c_str(), onionProxy, 9050, fNameLookup)) { @@ -2035,7 +2038,7 @@ _("Invalid -onion address or hostname: '%s'"), onionArg)); } SetProxy(NET_ONION, addrOnion); - SetLimited(NET_ONION, false); + SetReachable(NET_ONION, true); } } diff --git a/src/net.h b/src/net.h --- a/src/net.h +++ b/src/net.h @@ -480,17 +480,23 @@ bool IsPeerAddrLocalGood(CNode *pnode); void AdvertiseLocal(CNode *pnode); -void SetLimited(enum Network net, bool fLimited = true); -bool IsLimited(enum Network net); -bool IsLimited(const CNetAddr &addr); + +/** + * 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); bool SeenLocal(const CService &addr); bool IsLocal(const CService &addr); bool GetLocal(CService &addr, const CNetAddr *paddrPeer = nullptr); -bool IsReachable(enum Network net); -bool IsReachable(const CNetAddr &addr); CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices); diff --git a/src/net.cpp b/src/net.cpp --- a/src/net.cpp +++ b/src/net.cpp @@ -175,7 +175,7 @@ bool IsPeerAddrLocalGood(CNode *pnode) { CService addrLocal = pnode->GetAddrLocal(); return fDiscover && pnode->addr.IsRoutable() && addrLocal.IsRoutable() && - !IsLimited(addrLocal.GetNetwork()); + IsReachable(addrLocal.GetNetwork()); } // Pushes our own address to a peer. @@ -217,7 +217,7 @@ return false; } - if (IsLimited(addr)) { + if (!IsReachable(addr)) { return false; } @@ -246,24 +246,21 @@ mapLocalHost.erase(addr); } -/** - * Make a particular network entirely off-limits (no automatic connects to it). - */ -void SetLimited(enum Network net, bool fLimited) { +void SetReachable(enum Network net, bool reachable) { if (net == NET_UNROUTABLE || net == NET_INTERNAL) { return; } LOCK(cs_mapLocalHost); - vfLimited[net] = fLimited; + vfLimited[net] = !reachable; } -bool IsLimited(enum Network net) { +bool IsReachable(enum Network net) { LOCK(cs_mapLocalHost); - return vfLimited[net]; + return !vfLimited[net]; } -bool IsLimited(const CNetAddr &addr) { - return IsLimited(addr.GetNetwork()); +bool IsReachable(const CNetAddr &addr) { + return IsReachable(addr.GetNetwork()); } /** vote for a local address */ @@ -282,16 +279,6 @@ return mapLocalHost.count(addr) > 0; } -/** check whether a given network is one we can probably connect to */ -bool IsReachable(enum Network net) { - return !IsLimited(net); -} - -/** check whether a given address is in a network we can probably connect to */ -bool IsReachable(const CNetAddr &addr) { - return IsReachable(addr.GetNetwork()); -} - CNode *CConnman::FindNode(const CNetAddr &ip) { LOCK(cs_vNodes); for (CNode *pnode : vNodes) { @@ -1770,7 +1757,7 @@ break; } - if (IsLimited(addr)) { + if (!IsReachable(addr)) { continue; } @@ -2176,7 +2163,7 @@ } bool CConnman::Bind(const CService &addr, unsigned int flags) { - if (!(flags & BF_EXPLICIT) && IsLimited(addr)) { + if (!(flags & BF_EXPLICIT) && !IsReachable(addr)) { return false; } std::string strError; diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -497,7 +497,7 @@ UniValue obj(UniValue::VOBJ); GetProxy(network, proxy); obj.pushKV("name", GetNetworkName(network)); - obj.pushKV("limited", IsLimited(network)); + obj.pushKV("limited", !IsReachable(network)); obj.pushKV("reachable", IsReachable(network)); obj.pushKV("proxy", proxy.IsValid() ? proxy.proxy.ToStringIPPort() : std::string()); diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -224,25 +224,21 @@ } BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network) { - SetLimited(NET_IPV4, true); - SetLimited(NET_IPV6, true); - SetLimited(NET_ONION, true); + BOOST_CHECK_EQUAL(IsReachable(NET_IPV4), true); + BOOST_CHECK_EQUAL(IsReachable(NET_IPV6), true); + BOOST_CHECK_EQUAL(IsReachable(NET_ONION), true); - BOOST_CHECK_EQUAL(IsLimited(NET_IPV4), true); - BOOST_CHECK_EQUAL(IsLimited(NET_IPV6), true); - BOOST_CHECK_EQUAL(IsLimited(NET_ONION), true); + SetReachable(NET_IPV4, false); + SetReachable(NET_IPV6, false); + SetReachable(NET_ONION, false); BOOST_CHECK_EQUAL(IsReachable(NET_IPV4), false); BOOST_CHECK_EQUAL(IsReachable(NET_IPV6), false); BOOST_CHECK_EQUAL(IsReachable(NET_ONION), false); - SetLimited(NET_IPV4, false); - SetLimited(NET_IPV6, false); - SetLimited(NET_ONION, false); - - BOOST_CHECK_EQUAL(IsLimited(NET_IPV4), false); - BOOST_CHECK_EQUAL(IsLimited(NET_IPV6), false); - BOOST_CHECK_EQUAL(IsLimited(NET_ONION), false); + SetReachable(NET_IPV4, true); + SetReachable(NET_IPV6, true); + SetReachable(NET_ONION, true); BOOST_CHECK_EQUAL(IsReachable(NET_IPV4), true); BOOST_CHECK_EQUAL(IsReachable(NET_IPV6), true); @@ -250,19 +246,13 @@ } BOOST_AUTO_TEST_CASE(LimitedAndReachable_NetworkCaseUnroutableAndInternal) { - BOOST_CHECK_EQUAL(IsLimited(NET_UNROUTABLE), false); - BOOST_CHECK_EQUAL(IsLimited(NET_INTERNAL), false); - BOOST_CHECK_EQUAL(IsReachable(NET_UNROUTABLE), true); BOOST_CHECK_EQUAL(IsReachable(NET_INTERNAL), true); - SetLimited(NET_UNROUTABLE, true); - SetLimited(NET_INTERNAL, true); + SetReachable(NET_UNROUTABLE, false); + SetReachable(NET_INTERNAL, false); // Ignored for both networks - BOOST_CHECK_EQUAL(IsLimited(NET_UNROUTABLE), false); - BOOST_CHECK_EQUAL(IsLimited(NET_INTERNAL), false); - BOOST_CHECK_EQUAL(IsReachable(NET_UNROUTABLE), true); BOOST_CHECK_EQUAL(IsReachable(NET_INTERNAL), true); } @@ -281,16 +271,14 @@ // 1.1.1.1 CNetAddr addr = UtilBuildAddress(0x001, 0x001, 0x001, 0x001); - SetLimited(NET_IPV4, false); - BOOST_CHECK_EQUAL(IsLimited(addr), false); + SetReachable(NET_IPV4, true); BOOST_CHECK_EQUAL(IsReachable(addr), true); - SetLimited(NET_IPV4, true); - BOOST_CHECK_EQUAL(IsLimited(addr), true); + SetReachable(NET_IPV4, false); BOOST_CHECK_EQUAL(IsReachable(addr), false); // have to reset this, because this is stateful. - SetLimited(NET_IPV4, false); + SetReachable(NET_IPV4, true); } BOOST_AUTO_TEST_CASE(LocalAddress_BasicLifecycle) { @@ -298,7 +286,7 @@ CService addr = CService(UtilBuildAddress(0x002, 0x001, 0x001, 0x001), 1000); - SetLimited(NET_IPV4, false); + SetReachable(NET_IPV4, true); BOOST_CHECK_EQUAL(IsLocal(addr), false); BOOST_CHECK_EQUAL(AddLocal(addr, 1000), true); diff --git a/src/torcontrol.cpp b/src/torcontrol.cpp --- a/src/torcontrol.cpp +++ b/src/torcontrol.cpp @@ -594,7 +594,7 @@ CService resolved(LookupNumeric("127.0.0.1", 9050)); proxyType addrOnion = proxyType(resolved, true); SetProxy(NET_ONION, addrOnion); - SetLimited(NET_ONION, false); + SetReachable(NET_ONION, true); } // Finally - now create the service