Add -blocknet to prevent connections to a given network

This commit is contained in:
Pieter Wuille 2012-05-04 16:46:22 +02:00
parent c5b3ffd8d5
commit 457754d2c2
3 changed files with 36 additions and 4 deletions

View file

@ -189,6 +189,7 @@ bool AppInit2(int argc, char* argv[])
" -connect=<ip> \t\t " + _("Connect only to the specified node") + "\n" + " -connect=<ip> \t\t " + _("Connect only to the specified node") + "\n" +
" -seednode=<ip> \t\t " + _("Connect to a node to retrieve peer addresses, and disconnect") + "\n" + " -seednode=<ip> \t\t " + _("Connect to a node to retrieve peer addresses, and disconnect") + "\n" +
" -externalip=<ip> \t " + _("Specify your own public address") + "\n" + " -externalip=<ip> \t " + _("Specify your own public address") + "\n" +
" -blocknet=<net> \t " + _("Do not connect to addresses in network net (ipv4, ipv6)") + "\n" +
" -discover \t " + _("Try to discover public IP address (default: 1)") + "\n" + " -discover \t " + _("Try to discover public IP address (default: 1)") + "\n" +
" -irc \t " + _("Find peers using internet relay chat (default: 0)") + "\n" + " -irc \t " + _("Find peers using internet relay chat (default: 0)") + "\n" +
" -listen \t " + _("Accept connections from outside (default: 1)") + "\n" + " -listen \t " + _("Accept connections from outside (default: 1)") + "\n" +
@ -560,6 +561,17 @@ bool AppInit2(int argc, char* argv[])
SoftSetBoolArg("-discover", false); SoftSetBoolArg("-discover", false);
} }
if (mapArgs.count("-blocknet")) {
BOOST_FOREACH(std::string snet, mapMultiArgs["-blocknet"]) {
enum Network net = ParseNetwork(snet);
if (net == NET_UNROUTABLE) {
ThreadSafeMessageBox(_("Unknown network specified in -blocknet"), _("Bitcoin"), wxOK | wxMODAL);
return false;
}
SetLimited(net);
}
}
fNameLookup = GetBoolArg("-dns"); fNameLookup = GetBoolArg("-dns");
fProxyNameLookup = GetBoolArg("-proxydns"); fProxyNameLookup = GetBoolArg("-proxydns");
if (fProxyNameLookup) if (fProxyNameLookup)

View file

@ -48,6 +48,7 @@ uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK);
static CCriticalSection cs_mapLocalHost; static CCriticalSection cs_mapLocalHost;
static map<CNetAddr, int> mapLocalHost; static map<CNetAddr, int> mapLocalHost;
static bool vfReachable[NET_MAX] = {}; static bool vfReachable[NET_MAX] = {};
static bool vfLimited[NET_MAX] = {};
static CNode* pnodeLocalHost = NULL; static CNode* pnodeLocalHost = NULL;
uint64 nLocalHostNonce = 0; uint64 nLocalHostNonce = 0;
array<int, THREAD_MAX> vnThreadsRunning; array<int, THREAD_MAX> vnThreadsRunning;
@ -225,7 +226,20 @@ bool AddLocal(const CNetAddr& addr, int nScore)
return true; return true;
} }
// vote for a local address /** Make a particular network entirely off-limits (no automatic connects to it) */
void SetLimited(enum Network net, bool fLimited)
{
LOCK(cs_mapLocalHost);
vfLimited[net] = fLimited;
}
bool IsLimited(const CNetAddr& addr)
{
LOCK(cs_mapLocalHost);
return vfLimited[addr.GetNetwork()];
}
/** vote for a local address */
bool SeenLocal(const CNetAddr& addr) bool SeenLocal(const CNetAddr& addr)
{ {
{ {
@ -240,18 +254,19 @@ bool SeenLocal(const CNetAddr& addr)
return true; return true;
} }
// check whether a given address is potentially local /** check whether a given address is potentially local */
bool IsLocal(const CNetAddr& addr) bool IsLocal(const CNetAddr& addr)
{ {
LOCK(cs_mapLocalHost); LOCK(cs_mapLocalHost);
return mapLocalHost.count(addr) > 0; return mapLocalHost.count(addr) > 0;
} }
// check whether a given address is in a network we can probably connect to /** check whether a given address is in a network we can probably connect to */
bool IsReachable(const CNetAddr& addr) bool IsReachable(const CNetAddr& addr)
{ {
LOCK(cs_mapLocalHost); LOCK(cs_mapLocalHost);
return vfReachable[addr.GetNetwork()]; enum Network net = addr.GetNetwork();
return vfReachable[net] && !vfLimited[net];
} }
bool GetMyExternalIP2(const CService& addrConnect, const char* pszGet, const char* pszKeyword, CNetAddr& ipRet) bool GetMyExternalIP2(const CService& addrConnect, const char* pszGet, const char* pszKeyword, CNetAddr& ipRet)
@ -1409,6 +1424,9 @@ void ThreadOpenConnections2(void* parg)
nTries++; nTries++;
if (IsLimited(addr))
continue;
// only consider very recently tried nodes after 30 failed attempts // only consider very recently tried nodes after 30 failed attempts
if (nANow - addr.nLastTry < 600 && nTries < 30) if (nANow - addr.nLastTry < 600 && nTries < 30)
continue; continue;

View file

@ -54,6 +54,8 @@ enum
LOCAL_MAX LOCAL_MAX
}; };
void SetLimited(enum Network net, bool fLimited = true);
bool IsLimited(const CNetAddr& addr);
bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE); bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE);
bool SeenLocal(const CNetAddr& addr); bool SeenLocal(const CNetAddr& addr);
bool IsLocal(const CNetAddr& addr); bool IsLocal(const CNetAddr& addr);