Skip to content

Commit 090e5b4

Browse files
committed
Limited relaying/storing of foreign addresses
Introduce a boolean variable for each "network" (ipv4, ipv6, tor, i2p), and track whether we are likely to able to connect to it. Addresses in "addr" messages outside of our network get limited relaying and are not stored in addrman.
1 parent d321485 commit 090e5b4

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed

src/main.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,6 +2418,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
24182418
}
24192419

24202420
// Store the new addresses
2421+
vector<CAddress> vAddrOk;
24212422
int64 nNow = GetAdjustedTime();
24222423
int64 nSince = nNow - 10 * 60;
24232424
BOOST_FOREACH(CAddress& addr, vAddr)
@@ -2427,6 +2428,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
24272428
if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
24282429
addr.nTime = nNow - 5 * 24 * 60 * 60;
24292430
pfrom->AddAddressKnown(addr);
2431+
bool fReachable = IsReachable(addr);
24302432
if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
24312433
{
24322434
// Relay to a limited number of other nodes
@@ -2451,13 +2453,16 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
24512453
hashKey = Hash(BEGIN(hashKey), END(hashKey));
24522454
mapMix.insert(make_pair(hashKey, pnode));
24532455
}
2454-
int nRelayNodes = 2;
2456+
int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
24552457
for (multimap<uint256, CNode*>::iterator mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
24562458
((*mi).second)->PushAddress(addr);
24572459
}
24582460
}
2461+
// Do not store addresses outside our network
2462+
if (fReachable)
2463+
vAddrOk.push_back(addr);
24592464
}
2460-
addrman.Add(vAddr, pfrom->addr, 2 * 60 * 60);
2465+
addrman.Add(vAddrOk, pfrom->addr, 2 * 60 * 60);
24612466
if (vAddr.size() < 1000)
24622467
pfrom->fGetAddr = false;
24632468
if (pfrom->fOneShot)

src/net.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ bool OpenNetworkConnection(const CAddress& addrConnect, const char *strDest = NU
4545
bool fClient = false;
4646
static bool fUseUPnP = false;
4747
uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK);
48-
CCriticalSection cs_mapLocalHost;
49-
map<CNetAddr, int> mapLocalHost;
48+
static CCriticalSection cs_mapLocalHost;
49+
static map<CNetAddr, int> mapLocalHost;
50+
static bool vfReachable[NET_MAX] = {};
5051
static CNode* pnodeLocalHost = NULL;
5152
uint64 nLocalHostNonce = 0;
5253
array<int, THREAD_MAX> vnThreadsRunning;
@@ -214,6 +215,9 @@ bool AddLocal(const CNetAddr& addr, int nScore)
214215
{
215216
LOCK(cs_mapLocalHost);
216217
mapLocalHost[addr] = std::max(nScore, mapLocalHost[addr]) + (mapLocalHost.count(addr) ? 1 : 0);
218+
enum Network net = addr.GetNetwork();
219+
vfReachable[net] = true;
220+
if (net == NET_IPV6) vfReachable[NET_IPV4] = true;
217221
}
218222

219223
AdvertizeLocal();
@@ -243,6 +247,12 @@ bool IsLocal(const CNetAddr& addr)
243247
return mapLocalHost.count(addr) > 0;
244248
}
245249

250+
// check whether a given address is in a network we can probably connect to
251+
bool IsReachable(const CNetAddr& addr)
252+
{
253+
LOCK(cs_mapLocalHost);
254+
return vfReachable[addr.GetNetwork()];
255+
}
246256

247257
bool GetMyExternalIP2(const CService& addrConnect, const char* pszGet, const char* pszKeyword, CNetAddr& ipRet)
248258
{

src/net.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE);
5858
bool SeenLocal(const CNetAddr& addr);
5959
bool IsLocal(const CNetAddr& addr);
6060
bool GetLocal(CNetAddr &addr, const CNetAddr *paddrPeer = NULL);
61+
bool IsReachable(const CNetAddr &addr);
6162
CAddress GetLocalAddress(const CNetAddr *paddrPeer = NULL);
6263

64+
6365
enum
6466
{
6567
MSG_TX = 1,

src/netbase.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ int nConnectTimeout = 5000;
2525

2626
static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
2727

28+
enum Network ParseNetwork(std::string net) {
29+
if (net == "ipv4") return NET_IPV4;
30+
if (net == "ipv6") return NET_IPV6;
31+
if (net == "tor") return NET_TOR;
32+
if (net == "i2p") return NET_I2P;
33+
return NET_UNROUTABLE;
34+
}
35+
2836
bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
2937
{
3038
vIP.clear();
@@ -688,6 +696,23 @@ bool CNetAddr::IsRoutable() const
688696
return IsValid() && !(IsRFC1918() || IsRFC3927() || IsRFC4862() || (IsRFC4193() && !IsOnionCat() && !IsGarliCat()) || IsRFC4843() || IsLocal());
689697
}
690698

699+
enum Network CNetAddr::GetNetwork() const
700+
{
701+
if (!IsRoutable())
702+
return NET_UNROUTABLE;
703+
704+
if (IsIPv4())
705+
return NET_IPV4;
706+
707+
if (IsOnionCat())
708+
return NET_TOR;
709+
710+
if (IsGarliCat())
711+
return NET_I2P;
712+
713+
return NET_IPV6;
714+
}
715+
691716
std::string CNetAddr::ToStringIP() const
692717
{
693718
if (IsIPv4())

src/netbase.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ extern int nConnectTimeout;
1717
#undef SetPort
1818
#endif
1919

20+
enum Network
21+
{
22+
NET_UNROUTABLE,
23+
NET_IPV4,
24+
NET_IPV6,
25+
NET_TOR,
26+
NET_I2P,
27+
28+
NET_MAX
29+
};
30+
31+
enum Network ParseNetwork(std::string net);
32+
2033
/** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
2134
class CNetAddr
2235
{
@@ -48,6 +61,7 @@ class CNetAddr
4861
bool IsRoutable() const;
4962
bool IsValid() const;
5063
bool IsMulticast() const;
64+
enum Network GetNetwork() const;
5165
std::string ToString() const;
5266
std::string ToStringIP() const;
5367
int GetByte(int n) const;

0 commit comments

Comments
 (0)