Skip to content

Commit d6b076c

Browse files
committed
Drop IsLimited in favor of IsReachable
These two methods have had the same meaning, but inverted, since 110b62f. Having one name for a single concept simplifies the code.
1 parent 0ed279c commit d6b076c

File tree

6 files changed

+44
-65
lines changed

6 files changed

+44
-65
lines changed

src/init.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ bool AppInitMain(InitInterfaces& interfaces)
13211321
for (int n = 0; n < NET_MAX; n++) {
13221322
enum Network net = (enum Network)n;
13231323
if (!nets.count(net))
1324-
SetLimited(net);
1324+
SetReachable(net, false);
13251325
}
13261326
}
13271327

@@ -1332,7 +1332,7 @@ bool AppInitMain(InitInterfaces& interfaces)
13321332
// -proxy sets a proxy for all outgoing network traffic
13331333
// -noproxy (or -proxy=0) as well as the empty string can be used to not set a proxy, this is the default
13341334
std::string proxyArg = gArgs.GetArg("-proxy", "");
1335-
SetLimited(NET_ONION);
1335+
SetReachable(NET_ONION, false);
13361336
if (proxyArg != "" && proxyArg != "0") {
13371337
CService proxyAddr;
13381338
if (!Lookup(proxyArg.c_str(), proxyAddr, 9050, fNameLookup)) {
@@ -1347,7 +1347,7 @@ bool AppInitMain(InitInterfaces& interfaces)
13471347
SetProxy(NET_IPV6, addrProxy);
13481348
SetProxy(NET_ONION, addrProxy);
13491349
SetNameProxy(addrProxy);
1350-
SetLimited(NET_ONION, false); // by default, -proxy sets onion as reachable, unless -noonion later
1350+
SetReachable(NET_ONION, true); // by default, -proxy sets onion as reachable, unless -noonion later
13511351
}
13521352

13531353
// -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses
@@ -1356,7 +1356,7 @@ bool AppInitMain(InitInterfaces& interfaces)
13561356
std::string onionArg = gArgs.GetArg("-onion", "");
13571357
if (onionArg != "") {
13581358
if (onionArg == "0") { // Handle -noonion/-onion=0
1359-
SetLimited(NET_ONION); // set onions as unreachable
1359+
SetReachable(NET_ONION, false);
13601360
} else {
13611361
CService onionProxy;
13621362
if (!Lookup(onionArg.c_str(), onionProxy, 9050, fNameLookup)) {
@@ -1366,7 +1366,7 @@ bool AppInitMain(InitInterfaces& interfaces)
13661366
if (!addrOnion.IsValid())
13671367
return InitError(strprintf(_("Invalid -onion address or hostname: '%s'"), onionArg));
13681368
SetProxy(NET_ONION, addrOnion);
1369-
SetLimited(NET_ONION, false);
1369+
SetReachable(NET_ONION, true);
13701370
}
13711371
}
13721372

src/net.cpp

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ bool IsPeerAddrLocalGood(CNode *pnode)
183183
{
184184
CService addrLocal = pnode->GetAddrLocal();
185185
return fDiscover && pnode->addr.IsRoutable() && addrLocal.IsRoutable() &&
186-
!IsLimited(addrLocal.GetNetwork());
186+
IsReachable(addrLocal.GetNetwork());
187187
}
188188

189189
// pushes our own address to a peer
@@ -222,7 +222,7 @@ bool AddLocal(const CService& addr, int nScore)
222222
if (!fDiscover && nScore < LOCAL_MANUAL)
223223
return false;
224224

225-
if (IsLimited(addr))
225+
if (!IsReachable(addr))
226226
return false;
227227

228228
LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore);
@@ -252,24 +252,23 @@ void RemoveLocal(const CService& addr)
252252
mapLocalHost.erase(addr);
253253
}
254254

255-
/** Make a particular network entirely off-limits (no automatic connects to it) */
256-
void SetLimited(enum Network net, bool fLimited)
255+
void SetReachable(enum Network net, bool reachable)
257256
{
258257
if (net == NET_UNROUTABLE || net == NET_INTERNAL)
259258
return;
260259
LOCK(cs_mapLocalHost);
261-
vfLimited[net] = fLimited;
260+
vfLimited[net] = !reachable;
262261
}
263262

264-
bool IsLimited(enum Network net)
263+
bool IsReachable(enum Network net)
265264
{
266265
LOCK(cs_mapLocalHost);
267-
return vfLimited[net];
266+
return !vfLimited[net];
268267
}
269268

270-
bool IsLimited(const CNetAddr &addr)
269+
bool IsReachable(const CNetAddr &addr)
271270
{
272-
return IsLimited(addr.GetNetwork());
271+
return IsReachable(addr.GetNetwork());
273272
}
274273

275274
/** vote for a local address */
@@ -292,19 +291,6 @@ bool IsLocal(const CService& addr)
292291
return mapLocalHost.count(addr) > 0;
293292
}
294293

295-
/** check whether a given network is one we can probably connect to */
296-
bool IsReachable(enum Network net)
297-
{
298-
return !IsLimited(net);
299-
}
300-
301-
/** check whether a given address is in a network we can probably connect to */
302-
bool IsReachable(const CNetAddr& addr)
303-
{
304-
return IsReachable(addr.GetNetwork());
305-
}
306-
307-
308294
CNode* CConnman::FindNode(const CNetAddr& ip)
309295
{
310296
LOCK(cs_vNodes);
@@ -1965,7 +1951,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
19651951
if (nTries > 100)
19661952
break;
19671953

1968-
if (IsLimited(addr))
1954+
if (!IsReachable(addr))
19691955
continue;
19701956

19711957
// only consider very recently tried nodes after 30 failed attempts
@@ -2327,7 +2313,7 @@ NodeId CConnman::GetNewNodeId()
23272313

23282314

23292315
bool CConnman::Bind(const CService &addr, unsigned int flags) {
2330-
if (!(flags & BF_EXPLICIT) && IsLimited(addr))
2316+
if (!(flags & BF_EXPLICIT) && !IsReachable(addr))
23312317
return false;
23322318
std::string strError;
23332319
if (!BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) {

src/net.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -520,17 +520,23 @@ enum
520520

521521
bool IsPeerAddrLocalGood(CNode *pnode);
522522
void AdvertiseLocal(CNode *pnode);
523-
void SetLimited(enum Network net, bool fLimited = true);
524-
bool IsLimited(enum Network net);
525-
bool IsLimited(const CNetAddr& addr);
523+
524+
/**
525+
* Mark a network as reachable or unreachable (no automatic connects to it)
526+
* @note Networks are reachable by default
527+
*/
528+
void SetReachable(enum Network net, bool reachable);
529+
/** @returns true if the network is reachable, false otherwise */
530+
bool IsReachable(enum Network net);
531+
/** @returns true if the address is in a reachable network, false otherwise */
532+
bool IsReachable(const CNetAddr& addr);
533+
526534
bool AddLocal(const CService& addr, int nScore = LOCAL_NONE);
527535
bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE);
528536
void RemoveLocal(const CService& addr);
529537
bool SeenLocal(const CService& addr);
530538
bool IsLocal(const CService& addr);
531539
bool GetLocal(CService &addr, const CNetAddr *paddrPeer = nullptr);
532-
bool IsReachable(enum Network net);
533-
bool IsReachable(const CNetAddr &addr);
534540
CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices);
535541

536542

src/rpc/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ static UniValue GetNetworksInfo()
423423
UniValue obj(UniValue::VOBJ);
424424
GetProxy(network, proxy);
425425
obj.pushKV("name", GetNetworkName(network));
426-
obj.pushKV("limited", IsLimited(network));
426+
obj.pushKV("limited", !IsReachable(network));
427427
obj.pushKV("reachable", IsReachable(network));
428428
obj.pushKV("proxy", proxy.IsValid() ? proxy.proxy.ToStringIPPort() : std::string());
429429
obj.pushKV("proxy_randomize_credentials", proxy.randomize_credentials);

src/test/net_tests.cpp

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -230,26 +230,21 @@ BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)
230230

231231
BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network)
232232
{
233-
SetLimited(NET_IPV4, true);
234-
SetLimited(NET_IPV6, true);
235-
SetLimited(NET_ONION, true);
233+
BOOST_CHECK_EQUAL(IsReachable(NET_IPV4), true);
234+
BOOST_CHECK_EQUAL(IsReachable(NET_IPV6), true);
235+
BOOST_CHECK_EQUAL(IsReachable(NET_ONION), true);
236236

237-
BOOST_CHECK_EQUAL(IsLimited(NET_IPV4), true);
238-
BOOST_CHECK_EQUAL(IsLimited(NET_IPV6), true);
239-
BOOST_CHECK_EQUAL(IsLimited(NET_ONION), true);
237+
SetReachable(NET_IPV4, false);
238+
SetReachable(NET_IPV6, false);
239+
SetReachable(NET_ONION, false);
240240

241241
BOOST_CHECK_EQUAL(IsReachable(NET_IPV4), false);
242242
BOOST_CHECK_EQUAL(IsReachable(NET_IPV6), false);
243243
BOOST_CHECK_EQUAL(IsReachable(NET_ONION), false);
244244

245-
246-
SetLimited(NET_IPV4, false);
247-
SetLimited(NET_IPV6, false);
248-
SetLimited(NET_ONION, false);
249-
250-
BOOST_CHECK_EQUAL(IsLimited(NET_IPV4), false);
251-
BOOST_CHECK_EQUAL(IsLimited(NET_IPV6), false);
252-
BOOST_CHECK_EQUAL(IsLimited(NET_ONION), false);
245+
SetReachable(NET_IPV4, true);
246+
SetReachable(NET_IPV6, true);
247+
SetReachable(NET_ONION, true);
253248

254249
BOOST_CHECK_EQUAL(IsReachable(NET_IPV4), true);
255250
BOOST_CHECK_EQUAL(IsReachable(NET_IPV6), true);
@@ -258,19 +253,13 @@ BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network)
258253

259254
BOOST_AUTO_TEST_CASE(LimitedAndReachable_NetworkCaseUnroutableAndInternal)
260255
{
261-
BOOST_CHECK_EQUAL(IsLimited(NET_UNROUTABLE), false);
262-
BOOST_CHECK_EQUAL(IsLimited(NET_INTERNAL), false);
263-
264256
BOOST_CHECK_EQUAL(IsReachable(NET_UNROUTABLE), true);
265257
BOOST_CHECK_EQUAL(IsReachable(NET_INTERNAL), true);
266258

267-
SetLimited(NET_UNROUTABLE, true);
268-
SetLimited(NET_INTERNAL, true);
259+
SetReachable(NET_UNROUTABLE, false);
260+
SetReachable(NET_INTERNAL, false);
269261

270-
BOOST_CHECK_EQUAL(IsLimited(NET_UNROUTABLE), false); // Ignored for both networks
271-
BOOST_CHECK_EQUAL(IsLimited(NET_INTERNAL), false);
272-
273-
BOOST_CHECK_EQUAL(IsReachable(NET_UNROUTABLE), true);
262+
BOOST_CHECK_EQUAL(IsReachable(NET_UNROUTABLE), true); // Ignored for both networks
274263
BOOST_CHECK_EQUAL(IsReachable(NET_INTERNAL), true);
275264
}
276265

@@ -289,23 +278,21 @@ BOOST_AUTO_TEST_CASE(LimitedAndReachable_CNetAddr)
289278
{
290279
CNetAddr addr = UtilBuildAddress(0x001, 0x001, 0x001, 0x001); // 1.1.1.1
291280

292-
SetLimited(NET_IPV4, false);
293-
BOOST_CHECK_EQUAL(IsLimited(addr), false);
281+
SetReachable(NET_IPV4, true);
294282
BOOST_CHECK_EQUAL(IsReachable(addr), true);
295283

296-
SetLimited(NET_IPV4, true);
297-
BOOST_CHECK_EQUAL(IsLimited(addr), true);
284+
SetReachable(NET_IPV4, false);
298285
BOOST_CHECK_EQUAL(IsReachable(addr), false);
299286

300-
SetLimited(NET_IPV4, false); // have to reset this, because this is stateful.
287+
SetReachable(NET_IPV4, true); // have to reset this, because this is stateful.
301288
}
302289

303290

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

308-
SetLimited(NET_IPV4, false);
295+
SetReachable(NET_IPV4, true);
309296

310297
BOOST_CHECK_EQUAL(IsLocal(addr), false);
311298
BOOST_CHECK_EQUAL(AddLocal(addr, 1000), true);

src/torcontrol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ void TorController::auth_cb(TorControlConnection& _conn, const TorControlReply&
527527
CService resolved(LookupNumeric("127.0.0.1", 9050));
528528
proxyType addrOnion = proxyType(resolved, true);
529529
SetProxy(NET_ONION, addrOnion);
530-
SetLimited(NET_ONION, false);
530+
SetReachable(NET_ONION, true);
531531
}
532532

533533
// Finally - now create the service

0 commit comments

Comments
 (0)