Skip to content

Commit db2dc7a

Browse files
committed
Move CNode::addrLocal access behind locked accessors
1 parent 036073b commit db2dc7a

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/net.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ int GetnScore(const CService& addr)
164164
// Is our peer's addrLocal potentially useful as an external IP source?
165165
bool IsPeerAddrLocalGood(CNode *pnode)
166166
{
167-
return fDiscover && pnode->addr.IsRoutable() && pnode->addrLocal.IsRoutable() &&
168-
!IsLimited(pnode->addrLocal.GetNetwork());
167+
CService addrLocal = pnode->GetAddrLocal();
168+
return fDiscover && pnode->addr.IsRoutable() && addrLocal.IsRoutable() &&
169+
!IsLimited(addrLocal.GetNetwork());
169170
}
170171

171172
// pushes our own address to a peer
@@ -180,7 +181,7 @@ void AdvertiseLocal(CNode *pnode)
180181
if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
181182
GetRand((GetnScore(addrLocal) > LOCAL_MANUAL) ? 8:2) == 0))
182183
{
183-
addrLocal.SetIP(pnode->addrLocal);
184+
addrLocal.SetIP(pnode->GetAddrLocal());
184185
}
185186
if (addrLocal.IsRoutable())
186187
{
@@ -606,6 +607,20 @@ void CNode::MaybeSetAddrName(const std::string& addrNameIn) {
606607
}
607608
}
608609

610+
CService CNode::GetAddrLocal() const {
611+
LOCK(cs_addrLocal);
612+
return addrLocal;
613+
}
614+
615+
void CNode::SetAddrLocal(const CService& addrLocalIn) {
616+
LOCK(cs_addrLocal);
617+
if (addrLocal.IsValid()) {
618+
error("Addr local already set for node: %i. Refusing to change from %s to %s", id, addrLocal.ToString(), addrLocalIn.ToString());
619+
} else {
620+
addrLocal = addrLocalIn;
621+
}
622+
}
623+
609624
#undef X
610625
#define X(name) stats.name = name
611626
void CNode::copyStats(CNodeStats &stats)
@@ -659,7 +674,8 @@ void CNode::copyStats(CNodeStats &stats)
659674
stats.dPingWait = (((double)nPingUsecWait) / 1e6);
660675

661676
// Leave string empty if addrLocal invalid (not filled in yet)
662-
stats.addrLocal = addrLocal.IsValid() ? addrLocal.ToString() : "";
677+
CService addrLocalUnlocked = GetAddrLocal();
678+
stats.addrLocal = addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToString() : "";
663679
}
664680
#undef X
665681

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-
CService addrLocal;
594593
std::atomic<int> nVersion;
595594
// strSubVer is whatever byte array we read from the wire. However, this field is intended
596595
// to be printed out, displayed to humans in various forms and so on. So we sanitize it and
@@ -698,6 +697,9 @@ class CNode
698697

699698
mutable CCriticalSection cs_addrName;
700699
std::string addrName;
700+
701+
CService addrLocal;
702+
mutable CCriticalSection cs_addrLocal;
701703
public:
702704

703705
NodeId GetId() const {
@@ -731,6 +733,10 @@ class CNode
731733
void SetSendVersion(int nVersionIn);
732734
int GetSendVersion() const;
733735

736+
CService GetAddrLocal() const;
737+
//! May not be called more than once
738+
void SetAddrLocal(const CService& addrLocalIn);
739+
734740
CNode* AddRef()
735741
{
736742
nRefCount++;

src/net_processing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
12741274
connman.PushMessage(pfrom, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERACK));
12751275

12761276
pfrom->nServices = nServices;
1277-
pfrom->addrLocal = addrMe;
1277+
pfrom->SetAddrLocal(addrMe);
12781278
{
12791279
LOCK(pfrom->cs_SubVer);
12801280
pfrom->strSubVer = strSubVer;
@@ -1315,7 +1315,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
13151315
LogPrint("net", "ProcessMessages: advertising address %s\n", addr.ToString());
13161316
pfrom->PushAddress(addr, insecure_rand);
13171317
} else if (IsPeerAddrLocalGood(pfrom)) {
1318-
addr.SetIP(pfrom->addrLocal);
1318+
addr.SetIP(addrMe);
13191319
LogPrint("net", "ProcessMessages: advertising address %s\n", addr.ToString());
13201320
pfrom->PushAddress(addr, insecure_rand);
13211321
}

0 commit comments

Comments
 (0)