Skip to content

Commit

Permalink
Drop IsLimited in favor of IsReachable
Browse files Browse the repository at this point in the history
These two methods have had the same meaning, but inverted, since
110b62f. Having one name for a single
concept simplifies the code.
  • Loading branch information
Empact authored and furszy committed Aug 10, 2021
1 parent a40711b commit 6b607ef
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 65 deletions.
12 changes: 6 additions & 6 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ static void registerSignalHandler(int signal, void(*handler)(int))

bool static Bind(CConnman& connman, const CService& addr, unsigned int flags)
{
if (!(flags & BF_EXPLICIT) && IsLimited(addr))
if (!(flags & BF_EXPLICIT) && !IsReachable(addr))
return false;
std::string strError;
if (!connman.BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) {
Expand Down Expand Up @@ -1384,7 +1384,7 @@ bool AppInitMain()
for (int n = 0; n < NET_MAX; n++) {
enum Network net = (enum Network)n;
if (!nets.count(net))
SetLimited(net);
SetReachable(net, false);
}
}

Expand All @@ -1403,7 +1403,7 @@ bool AppInitMain()
// -proxy sets a proxy for all outgoing network traffic
// -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.empty() && proxyArg != "0") {
CService proxyAddr;
if (!Lookup(proxyArg, proxyAddr, 9050, fNameLookup)) {
Expand All @@ -1418,7 +1418,7 @@ bool AppInitMain()
SetProxy(NET_IPV6, addrProxy);
SetProxy(NET_ONION, addrProxy);
SetNameProxy(addrProxy);
SetLimited(NET_ONION, false); // by default, -proxy sets onion as reachable, unless -noonion later
SetReachable(NET_ONION, true); // by default, -proxy sets onion as reachable, unless -noonion later
}

// -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses
Expand All @@ -1427,7 +1427,7 @@ bool AppInitMain()
std::string onionArg = gArgs.GetArg("-onion", "");
if (!onionArg.empty()) {
if (onionArg == "0") { // Handle -noonion/-onion=0
SetLimited(NET_ONION); // set onions as unreachable
SetReachable(NET_ONION, false);
} else {
CService onionProxy;
if (!Lookup(onionArg, onionProxy, 9050, fNameLookup)) {
Expand All @@ -1437,7 +1437,7 @@ bool AppInitMain()
if (!addrOnion.IsValid())
return UIError(strprintf(_("%s Invalid %s address or hostname: '%s'"), "isValid():", "-onion", onionArg));
SetProxy(NET_ONION, addrOnion);
SetLimited(NET_ONION, false);
SetReachable(NET_ONION, true);
}
}

Expand Down
32 changes: 9 additions & 23 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,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
Expand Down Expand Up @@ -189,7 +189,7 @@ bool AddLocal(const CService& addr, int nScore)
if (!fDiscover && nScore < LOCAL_MANUAL)
return false;

if (IsLimited(addr))
if (!IsReachable(addr))
return false;

LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore);
Expand Down Expand Up @@ -220,24 +220,23 @@ bool RemoveLocal(const CService& addr)
return true;
}

/** 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)
bool IsReachable(const CNetAddr& addr)
{
return IsLimited(addr.GetNetwork());
return IsReachable(addr.GetNetwork());
}

/** vote for a local address */
Expand All @@ -260,19 +259,6 @@ bool IsLocal(const CService& addr)
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);
Expand Down Expand Up @@ -1577,7 +1563,7 @@ void CConnman::ThreadOpenConnections()
if (nTries > 100)
break;

if (IsLimited(addr))
if (!IsReachable(addr))
continue;

// only connect to full nodes
Expand Down
16 changes: 11 additions & 5 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,17 +416,23 @@ enum {

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);
bool RemoveLocal(const CService& addr);
bool SeenLocal(const CService& addr);
bool IsLocal(const CService& addr);
bool GetLocal(CService& addr, const CNetAddr* paddrPeer = NULL);
bool IsReachable(enum Network net);
bool IsReachable(const CNetAddr& addr);
CAddress GetLocalAddress(const CNetAddr* paddrPeer, ServiceFlags nLocalServices);

bool validateMasternodeIP(const std::string& addrStr); // valid, reachable and routable address
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ static UniValue GetNetworksInfo()
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());
obj.pushKV("proxy_randomize_credentials", proxy.randomize_credentials);
Expand Down
45 changes: 16 additions & 29 deletions src/test/net_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,26 +233,21 @@ BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)

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);
Expand All @@ -261,19 +256,13 @@ BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network)

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);

BOOST_CHECK_EQUAL(IsLimited(NET_UNROUTABLE), false); // Ignored for both networks
BOOST_CHECK_EQUAL(IsLimited(NET_INTERNAL), false);

BOOST_CHECK_EQUAL(IsReachable(NET_UNROUTABLE), true);
BOOST_CHECK_EQUAL(IsReachable(NET_UNROUTABLE), true); // Ignored for both networks
BOOST_CHECK_EQUAL(IsReachable(NET_INTERNAL), true);
}

Expand All @@ -292,23 +281,21 @@ BOOST_AUTO_TEST_CASE(LimitedAndReachable_CNetAddr)
{
CNetAddr addr = UtilBuildAddress(0x001, 0x001, 0x001, 0x001); // 1.1.1.1

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);

SetLimited(NET_IPV4, false); // have to reset this, because this is stateful.
SetReachable(NET_IPV4, true); // have to reset this, because this is stateful.
}


BOOST_AUTO_TEST_CASE(LocalAddress_BasicLifecycle)
{
CService addr = CService(UtilBuildAddress(0x002, 0x001, 0x001, 0x001), 1000); // 2.1.1.1:1000

SetLimited(NET_IPV4, false);
SetReachable(NET_IPV4, true);

BOOST_CHECK_EQUAL(IsLocal(addr), false);
BOOST_CHECK_EQUAL(AddLocal(addr, 1000), true);
Expand Down
2 changes: 1 addition & 1 deletion src/torcontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ void TorController::auth_cb(TorControlConnection& _conn, const TorControlReply&
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
Expand Down

0 comments on commit 6b607ef

Please sign in to comment.