aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Corallo <git@bluematt.me>2017-02-06 12:04:34 -0500
committerMatt Corallo <git@bluematt.me>2017-02-10 11:32:41 -0500
commit036073bf87c07f8d69e39168dd93a52f1aafe85c (patch)
tree95cdeea1af6e92752009b265b4f1b67e1d040f7f
parentd8f2b8a8c032b83a3bd90750e58abaeece7e34e7 (diff)
downloadbitcoin-036073bf87c07f8d69e39168dd93a52f1aafe85c.tar.xz
Move CNode::addrName accesses behind locked accessors
-rw-r--r--src/net.cpp30
-rw-r--r--src/net.h8
-rw-r--r--src/net_processing.cpp2
3 files changed, 30 insertions, 10 deletions
diff --git a/src/net.cpp b/src/net.cpp
index e7521f86d1..8aa1261984 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -307,9 +307,11 @@ CNode* CConnman::FindNode(const CSubNet& subNet)
CNode* CConnman::FindNode(const std::string& addrName)
{
LOCK(cs_vNodes);
- BOOST_FOREACH(CNode* pnode, vNodes)
- if (pnode->addrName == addrName)
+ BOOST_FOREACH(CNode* pnode, vNodes) {
+ if (pnode->GetAddrName() == addrName) {
return (pnode);
+ }
+ }
return NULL;
}
@@ -373,9 +375,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
CNode* pnode = FindNode((CService)addrConnect);
if (pnode)
{
- if (pnode->addrName.empty()) {
- pnode->addrName = std::string(pszDest);
- }
+ pnode->MaybeSetAddrName(std::string(pszDest));
CloseSocket(hSocket);
LogPrintf("Failed to open new connection, already connected\n");
return NULL;
@@ -593,6 +593,19 @@ void CConnman::AddWhitelistedRange(const CSubNet &subnet) {
vWhitelistedRange.push_back(subnet);
}
+
+std::string CNode::GetAddrName() const {
+ LOCK(cs_addrName);
+ return addrName;
+}
+
+void CNode::MaybeSetAddrName(const std::string& addrNameIn) {
+ LOCK(cs_addrName);
+ if (addrName.empty()) {
+ addrName = addrNameIn;
+ }
+}
+
#undef X
#define X(name) stats.name = name
void CNode::copyStats(CNodeStats &stats)
@@ -608,7 +621,7 @@ void CNode::copyStats(CNodeStats &stats)
X(nLastRecv);
X(nTimeConnected);
X(nTimeOffset);
- X(addrName);
+ stats.addrName = GetAddrName();
X(nVersion);
{
LOCK(cs_SubVer);
@@ -1798,8 +1811,9 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
if (pnode->addr.IsValid()) {
mapConnected[pnode->addr] = pnode->fInbound;
}
- if (!pnode->addrName.empty()) {
- mapConnectedByName[pnode->addrName] = std::make_pair(pnode->fInbound, static_cast<const CService&>(pnode->addr));
+ std::string addrName = pnode->GetAddrName();
+ if (!addrName.empty()) {
+ mapConnectedByName[std::move(addrName)] = std::make_pair(pnode->fInbound, static_cast<const CService&>(pnode->addr));
}
}
}
diff --git a/src/net.h b/src/net.h
index ddc050eb1f..2cfc74e3d5 100644
--- a/src/net.h
+++ b/src/net.h
@@ -590,7 +590,6 @@ public:
const int64_t nTimeConnected;
std::atomic<int64_t> nTimeOffset;
const CAddress addr;
- std::string addrName;
CService addrLocal;
std::atomic<int> nVersion;
// strSubVer is whatever byte array we read from the wire. However, this field is intended
@@ -696,6 +695,9 @@ private:
const int nMyStartingHeight;
int nSendVersion;
std::list<CNetMessage> vRecvMsg; // Used only by SocketHandler thread
+
+ mutable CCriticalSection cs_addrName;
+ std::string addrName;
public:
NodeId GetId() const {
@@ -798,6 +800,10 @@ public:
{
return nLocalServices;
}
+
+ std::string GetAddrName() const;
+ //! Sets the addrName only if it was not previously set
+ void MaybeSetAddrName(const std::string& addrNameIn);
};
diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index 62397e68ce..b0c9b3c71b 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -264,7 +264,7 @@ void PushNodeVersion(CNode *pnode, CConnman& connman, int64_t nTime)
void InitializeNode(CNode *pnode, CConnman& connman) {
CAddress addr = pnode->addr;
- std::string addrName = pnode->addrName;
+ std::string addrName = pnode->GetAddrName();
NodeId nodeid = pnode->GetId();
{
LOCK(cs_main);