Skip to content

Commit 457754d

Browse files
committed
Add -blocknet to prevent connections to a given network
1 parent c5b3ffd commit 457754d

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

src/init.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ bool AppInit2(int argc, char* argv[])
189189
" -connect=<ip> \t\t " + _("Connect only to the specified node") + "\n" +
190190
" -seednode=<ip> \t\t " + _("Connect to a node to retrieve peer addresses, and disconnect") + "\n" +
191191
" -externalip=<ip> \t " + _("Specify your own public address") + "\n" +
192+
" -blocknet=<net> \t " + _("Do not connect to addresses in network net (ipv4, ipv6)") + "\n" +
192193
" -discover \t " + _("Try to discover public IP address (default: 1)") + "\n" +
193194
" -irc \t " + _("Find peers using internet relay chat (default: 0)") + "\n" +
194195
" -listen \t " + _("Accept connections from outside (default: 1)") + "\n" +
@@ -560,6 +561,17 @@ bool AppInit2(int argc, char* argv[])
560561
SoftSetBoolArg("-discover", false);
561562
}
562563

564+
if (mapArgs.count("-blocknet")) {
565+
BOOST_FOREACH(std::string snet, mapMultiArgs["-blocknet"]) {
566+
enum Network net = ParseNetwork(snet);
567+
if (net == NET_UNROUTABLE) {
568+
ThreadSafeMessageBox(_("Unknown network specified in -blocknet"), _("Bitcoin"), wxOK | wxMODAL);
569+
return false;
570+
}
571+
SetLimited(net);
572+
}
573+
}
574+
563575
fNameLookup = GetBoolArg("-dns");
564576
fProxyNameLookup = GetBoolArg("-proxydns");
565577
if (fProxyNameLookup)

src/net.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK);
4848
static CCriticalSection cs_mapLocalHost;
4949
static map<CNetAddr, int> mapLocalHost;
5050
static bool vfReachable[NET_MAX] = {};
51+
static bool vfLimited[NET_MAX] = {};
5152
static CNode* pnodeLocalHost = NULL;
5253
uint64 nLocalHostNonce = 0;
5354
array<int, THREAD_MAX> vnThreadsRunning;
@@ -225,7 +226,20 @@ bool AddLocal(const CNetAddr& addr, int nScore)
225226
return true;
226227
}
227228

228-
// vote for a local address
229+
/** Make a particular network entirely off-limits (no automatic connects to it) */
230+
void SetLimited(enum Network net, bool fLimited)
231+
{
232+
LOCK(cs_mapLocalHost);
233+
vfLimited[net] = fLimited;
234+
}
235+
236+
bool IsLimited(const CNetAddr& addr)
237+
{
238+
LOCK(cs_mapLocalHost);
239+
return vfLimited[addr.GetNetwork()];
240+
}
241+
242+
/** vote for a local address */
229243
bool SeenLocal(const CNetAddr& addr)
230244
{
231245
{
@@ -240,18 +254,19 @@ bool SeenLocal(const CNetAddr& addr)
240254
return true;
241255
}
242256

243-
// check whether a given address is potentially local
257+
/** check whether a given address is potentially local */
244258
bool IsLocal(const CNetAddr& addr)
245259
{
246260
LOCK(cs_mapLocalHost);
247261
return mapLocalHost.count(addr) > 0;
248262
}
249263

250-
// check whether a given address is in a network we can probably connect to
264+
/** check whether a given address is in a network we can probably connect to */
251265
bool IsReachable(const CNetAddr& addr)
252266
{
253267
LOCK(cs_mapLocalHost);
254-
return vfReachable[addr.GetNetwork()];
268+
enum Network net = addr.GetNetwork();
269+
return vfReachable[net] && !vfLimited[net];
255270
}
256271

257272
bool GetMyExternalIP2(const CService& addrConnect, const char* pszGet, const char* pszKeyword, CNetAddr& ipRet)
@@ -1409,6 +1424,9 @@ void ThreadOpenConnections2(void* parg)
14091424

14101425
nTries++;
14111426

1427+
if (IsLimited(addr))
1428+
continue;
1429+
14121430
// only consider very recently tried nodes after 30 failed attempts
14131431
if (nANow - addr.nLastTry < 600 && nTries < 30)
14141432
continue;

src/net.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ enum
5454
LOCAL_MAX
5555
};
5656

57+
void SetLimited(enum Network net, bool fLimited = true);
58+
bool IsLimited(const CNetAddr& addr);
5759
bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE);
5860
bool SeenLocal(const CNetAddr& addr);
5961
bool IsLocal(const CNetAddr& addr);

0 commit comments

Comments
 (0)