Skip to content

Commit 139d2f7

Browse files
committed
Keep local service information per address
Keep local service information per CNetAddr instead of per CService, but move the port into the information kept on it.
1 parent 89b5616 commit 139d2f7

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/net.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ void ThreadDNSAddressSeed2(void* parg);
3838
bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false);
3939

4040

41+
struct LocalServiceInfo {
42+
int nScore;
43+
int nPort;
44+
};
4145

4246
//
4347
// Global state variables
@@ -46,7 +50,7 @@ bool fClient = false;
4650
static bool fUseUPnP = false;
4751
uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK);
4852
static CCriticalSection cs_mapLocalHost;
49-
static map<CService, int> mapLocalHost;
53+
static map<CNetAddr, LocalServiceInfo> mapLocalHost;
5054
static bool vfReachable[NET_MAX] = {};
5155
static bool vfLimited[NET_MAX] = {};
5256
static CNode* pnodeLocalHost = NULL;
@@ -98,23 +102,23 @@ bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
98102
if (fUseProxy || mapArgs.count("-connect") || fNoListen)
99103
return false;
100104

101-
int nBestCount = -1;
105+
int nBestScore = -1;
102106
int nBestReachability = -1;
103107
{
104108
LOCK(cs_mapLocalHost);
105-
for (map<CService, int>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
109+
for (map<CNetAddr, LocalServiceInfo>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
106110
{
107-
int nCount = (*it).second;
111+
int nScore = (*it).second.nScore;
108112
int nReachability = (*it).first.GetReachabilityFrom(paddrPeer);
109-
if (nReachability > nBestReachability || (nReachability == nBestReachability && nCount > nBestCount))
113+
if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
110114
{
111-
addr = (*it).first;
115+
addr = CService((*it).first, (*it).second.nPort);
112116
nBestReachability = nReachability;
113-
nBestCount = nCount;
117+
nBestScore = nScore;
114118
}
115119
}
116120
}
117-
return nBestCount >= 0;
121+
return nBestScore >= 0;
118122
}
119123

120124
// get best local address for a particular peer as a CAddress
@@ -211,7 +215,12 @@ bool AddLocal(const CService& addr, int nScore)
211215

212216
{
213217
LOCK(cs_mapLocalHost);
214-
mapLocalHost[addr] = std::max(nScore, mapLocalHost[addr]) + (mapLocalHost.count(addr) ? 1 : 0);
218+
bool fAlready = mapLocalHost.count(addr) > 0;
219+
LocalServiceInfo &info = mapLocalHost[addr];
220+
if (!fAlready || nScore >= info.nScore) {
221+
info.nScore = nScore;
222+
info.nPort = addr.GetPort() + (fAlready ? 1 : 0);
223+
}
215224
enum Network net = addr.GetNetwork();
216225
vfReachable[net] = true;
217226
if (net == NET_IPV6) vfReachable[NET_IPV4] = true;
@@ -249,7 +258,7 @@ bool SeenLocal(const CService& addr)
249258
LOCK(cs_mapLocalHost);
250259
if (mapLocalHost.count(addr) == 0)
251260
return false;
252-
mapLocalHost[addr]++;
261+
mapLocalHost[addr].nScore++;
253262
}
254263

255264
AdvertizeLocal();

0 commit comments

Comments
 (0)