Skip to content

Commit 036073b

Browse files
committed
Move CNode::addrName accesses behind locked accessors
1 parent d8f2b8a commit 036073b

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

src/net.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,11 @@ CNode* CConnman::FindNode(const CSubNet& subNet)
307307
CNode* CConnman::FindNode(const std::string& addrName)
308308
{
309309
LOCK(cs_vNodes);
310-
BOOST_FOREACH(CNode* pnode, vNodes)
311-
if (pnode->addrName == addrName)
310+
BOOST_FOREACH(CNode* pnode, vNodes) {
311+
if (pnode->GetAddrName() == addrName) {
312312
return (pnode);
313+
}
314+
}
313315
return NULL;
314316
}
315317

@@ -373,9 +375,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
373375
CNode* pnode = FindNode((CService)addrConnect);
374376
if (pnode)
375377
{
376-
if (pnode->addrName.empty()) {
377-
pnode->addrName = std::string(pszDest);
378-
}
378+
pnode->MaybeSetAddrName(std::string(pszDest));
379379
CloseSocket(hSocket);
380380
LogPrintf("Failed to open new connection, already connected\n");
381381
return NULL;
@@ -593,6 +593,19 @@ void CConnman::AddWhitelistedRange(const CSubNet &subnet) {
593593
vWhitelistedRange.push_back(subnet);
594594
}
595595

596+
597+
std::string CNode::GetAddrName() const {
598+
LOCK(cs_addrName);
599+
return addrName;
600+
}
601+
602+
void CNode::MaybeSetAddrName(const std::string& addrNameIn) {
603+
LOCK(cs_addrName);
604+
if (addrName.empty()) {
605+
addrName = addrNameIn;
606+
}
607+
}
608+
596609
#undef X
597610
#define X(name) stats.name = name
598611
void CNode::copyStats(CNodeStats &stats)
@@ -608,7 +621,7 @@ void CNode::copyStats(CNodeStats &stats)
608621
X(nLastRecv);
609622
X(nTimeConnected);
610623
X(nTimeOffset);
611-
X(addrName);
624+
stats.addrName = GetAddrName();
612625
X(nVersion);
613626
{
614627
LOCK(cs_SubVer);
@@ -1798,8 +1811,9 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
17981811
if (pnode->addr.IsValid()) {
17991812
mapConnected[pnode->addr] = pnode->fInbound;
18001813
}
1801-
if (!pnode->addrName.empty()) {
1802-
mapConnectedByName[pnode->addrName] = std::make_pair(pnode->fInbound, static_cast<const CService&>(pnode->addr));
1814+
std::string addrName = pnode->GetAddrName();
1815+
if (!addrName.empty()) {
1816+
mapConnectedByName[std::move(addrName)] = std::make_pair(pnode->fInbound, static_cast<const CService&>(pnode->addr));
18031817
}
18041818
}
18051819
}

src/net.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,6 @@ class CNode
590590
const int64_t nTimeConnected;
591591
std::atomic<int64_t> nTimeOffset;
592592
const CAddress addr;
593-
std::string addrName;
594593
CService addrLocal;
595594
std::atomic<int> nVersion;
596595
// strSubVer is whatever byte array we read from the wire. However, this field is intended
@@ -696,6 +695,9 @@ class CNode
696695
const int nMyStartingHeight;
697696
int nSendVersion;
698697
std::list<CNetMessage> vRecvMsg; // Used only by SocketHandler thread
698+
699+
mutable CCriticalSection cs_addrName;
700+
std::string addrName;
699701
public:
700702

701703
NodeId GetId() const {
@@ -798,6 +800,10 @@ class CNode
798800
{
799801
return nLocalServices;
800802
}
803+
804+
std::string GetAddrName() const;
805+
//! Sets the addrName only if it was not previously set
806+
void MaybeSetAddrName(const std::string& addrNameIn);
801807
};
802808

803809

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ void PushNodeVersion(CNode *pnode, CConnman& connman, int64_t nTime)
264264

265265
void InitializeNode(CNode *pnode, CConnman& connman) {
266266
CAddress addr = pnode->addr;
267-
std::string addrName = pnode->addrName;
267+
std::string addrName = pnode->GetAddrName();
268268
NodeId nodeid = pnode->GetId();
269269
{
270270
LOCK(cs_main);

0 commit comments

Comments
 (0)