Skip to content

Commit

Permalink
Move CNode::addrLocal access behind locked accessors
Browse files Browse the repository at this point in the history
zcash: cherry picked from commit db2dc7a
zcash: bitcoin/bitcoin#9708
  • Loading branch information
TheBlueMatt authored and str4d committed Apr 1, 2021
1 parent 116deee commit c9e2172
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5776,7 +5776,7 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
return true;
}

pfrom->addrLocal = addrMe;
pfrom->SetAddrLocal(addrMe);
if (pfrom->fInbound && addrMe.IsRoutable())
{
SeenLocal(addrMe);
Expand Down Expand Up @@ -5815,7 +5815,7 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
LogPrintf("ProcessMessages: advertizing address %s\n", addr.ToString());
pfrom->PushAddress(addr, insecure_rand);
} else if (IsPeerAddrLocalGood(pfrom)) {
addr.SetIP(pfrom->addrLocal);
addr.SetIP(addrMe);
LogPrintf("ProcessMessages: advertizing address %s\n", addr.ToString());
pfrom->PushAddress(addr, insecure_rand);
}
Expand Down
24 changes: 20 additions & 4 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ int GetnScore(const CService& addr)
// Is our peer's addrLocal potentially useful as an external IP source?
bool IsPeerAddrLocalGood(CNode *pnode)
{
return fDiscover && pnode->addr.IsRoutable() && pnode->addrLocal.IsRoutable() &&
!IsLimited(pnode->addrLocal.GetNetwork());
CService addrLocal = pnode->GetAddrLocal();
return fDiscover && pnode->addr.IsRoutable() && addrLocal.IsRoutable() &&
!IsLimited(addrLocal.GetNetwork());
}

// pushes our own address to a peer
Expand All @@ -208,7 +209,7 @@ void AdvertizeLocal(CNode *pnode)
if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
GetRand((GetnScore(addrLocal) > LOCAL_MANUAL) ? 8:2) == 0))
{
addrLocal.SetIP(pnode->addrLocal);
addrLocal.SetIP(pnode->GetAddrLocal());
}
if (addrLocal.IsRoutable())
{
Expand Down Expand Up @@ -652,6 +653,20 @@ void CNode::MaybeSetAddrName(const std::string& addrNameIn) {
}
}

CService CNode::GetAddrLocal() const {
LOCK(cs_addrLocal);
return addrLocal;
}

void CNode::SetAddrLocal(const CService& addrLocalIn) {
LOCK(cs_addrLocal);
if (addrLocal.IsValid()) {
error("Addr local already set for node: %i. Refusing to change from %s to %s", id, addrLocal.ToString(), addrLocalIn.ToString());
} else {
addrLocal = addrLocalIn;
}
}

void CNode::copyStats(CNodeStats &stats)
{
stats.nodeid = this->GetId();
Expand Down Expand Up @@ -698,7 +713,8 @@ void CNode::copyStats(CNodeStats &stats)
stats.dPingWait = (((double)nPingUsecWait) / 1e6);

// Leave string empty if addrLocal invalid (not filled in yet)
stats.addrLocal = addrLocal.IsValid() ? addrLocal.ToString() : "";
CService addrLocalUnlocked = GetAddrLocal();
stats.addrLocal = addrLocalUnlocked.IsValid() ? addrLocalUnlocked.ToString() : "";
}

// requires LOCK(cs_vRecvMsg)
Expand Down
8 changes: 7 additions & 1 deletion src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ class CNode
const int64_t nTimeConnected;
std::atomic<int64_t> nTimeOffset;
const CAddress addr;
CService addrLocal;
int nVersion;
// strSubVer is whatever byte array we read from the wire. However, this field is intended
// to be printed out, displayed to humans in various forms and so on. So we sanitize it and
Expand Down Expand Up @@ -381,6 +380,9 @@ class CNode

mutable CCriticalSection cs_addrName;
std::string addrName;

CService addrLocal;
mutable CCriticalSection cs_addrLocal;
public:

// Regenerate the span for this CNode. This re-queries the log filter to see
Expand Down Expand Up @@ -417,6 +419,10 @@ class CNode
msg.SetVersion(nVersionIn);
}

CService GetAddrLocal() const;
//! May not be called more than once
void SetAddrLocal(const CService& addrLocalIn);

CNode* AddRef()
{
nRefCount++;
Expand Down

0 comments on commit c9e2172

Please sign in to comment.