@@ -164,8 +164,9 @@ int GetnScore(const CService& addr)
164
164
// Is our peer's addrLocal potentially useful as an external IP source?
165
165
bool IsPeerAddrLocalGood (CNode *pnode)
166
166
{
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 ());
169
170
}
170
171
171
172
// pushes our own address to a peer
@@ -180,7 +181,7 @@ void AdvertiseLocal(CNode *pnode)
180
181
if (IsPeerAddrLocalGood (pnode) && (!addrLocal.IsRoutable () ||
181
182
GetRand ((GetnScore (addrLocal) > LOCAL_MANUAL) ? 8 :2 ) == 0 ))
182
183
{
183
- addrLocal.SetIP (pnode->addrLocal );
184
+ addrLocal.SetIP (pnode->GetAddrLocal () );
184
185
}
185
186
if (addrLocal.IsRoutable ())
186
187
{
@@ -307,9 +308,11 @@ CNode* CConnman::FindNode(const CSubNet& subNet)
307
308
CNode* CConnman::FindNode (const std::string& addrName)
308
309
{
309
310
LOCK (cs_vNodes);
310
- BOOST_FOREACH (CNode* pnode, vNodes)
311
- if (pnode->addrName == addrName)
311
+ BOOST_FOREACH (CNode* pnode, vNodes) {
312
+ if (pnode->GetAddrName () == addrName) {
312
313
return (pnode);
314
+ }
315
+ }
313
316
return NULL ;
314
317
}
315
318
@@ -373,9 +376,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
373
376
CNode* pnode = FindNode ((CService)addrConnect);
374
377
if (pnode)
375
378
{
376
- if (pnode->addrName .empty ()) {
377
- pnode->addrName = std::string (pszDest);
378
- }
379
+ pnode->MaybeSetAddrName (std::string (pszDest));
379
380
CloseSocket (hSocket);
380
381
LogPrintf (" Failed to open new connection, already connected\n " );
381
382
return NULL ;
@@ -389,7 +390,6 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
389
390
uint64_t nonce = GetDeterministicRandomizer (RANDOMIZER_ID_LOCALHOSTNONCE).Write (id).Finalize ();
390
391
CNode* pnode = new CNode (id, nLocalServices, GetBestHeight (), hSocket, addrConnect, CalculateKeyedNetGroup (addrConnect), nonce, pszDest ? pszDest : " " , false );
391
392
pnode->nServicesExpected = ServiceFlags (addrConnect.nServices & nRelevantServices);
392
- pnode->nTimeConnected = GetSystemTimeInSeconds ();
393
393
pnode->AddRef ();
394
394
395
395
return pnode;
@@ -594,28 +594,67 @@ void CConnman::AddWhitelistedRange(const CSubNet &subnet) {
594
594
vWhitelistedRange.push_back (subnet);
595
595
}
596
596
597
+
598
+ std::string CNode::GetAddrName () const {
599
+ LOCK (cs_addrName);
600
+ return addrName;
601
+ }
602
+
603
+ void CNode::MaybeSetAddrName (const std::string& addrNameIn) {
604
+ LOCK (cs_addrName);
605
+ if (addrName.empty ()) {
606
+ addrName = addrNameIn;
607
+ }
608
+ }
609
+
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
+
597
624
#undef X
598
625
#define X (name ) stats.name = name
599
626
void CNode::copyStats (CNodeStats &stats)
600
627
{
601
628
stats.nodeid = this ->GetId ();
602
629
X (nServices);
603
630
X (addr);
604
- X (fRelayTxes );
631
+ {
632
+ LOCK (cs_filter);
633
+ X (fRelayTxes );
634
+ }
605
635
X (nLastSend);
606
636
X (nLastRecv);
607
637
X (nTimeConnected);
608
638
X (nTimeOffset);
609
- X ( addrName);
639
+ stats. addrName = GetAddrName ( );
610
640
X (nVersion);
611
- X (cleanSubVer);
641
+ {
642
+ LOCK (cs_SubVer);
643
+ X (cleanSubVer);
644
+ }
612
645
X (fInbound );
613
646
X (fAddnode );
614
647
X (nStartingHeight);
615
- X (nSendBytes);
616
- X (mapSendBytesPerMsgCmd);
617
- X (nRecvBytes);
618
- X (mapRecvBytesPerMsgCmd);
648
+ {
649
+ LOCK (cs_vSend);
650
+ X (mapSendBytesPerMsgCmd);
651
+ X (nSendBytes);
652
+ }
653
+ {
654
+ LOCK (cs_vRecv);
655
+ X (mapRecvBytesPerMsgCmd);
656
+ X (nRecvBytes);
657
+ }
619
658
X (fWhitelisted );
620
659
621
660
// It is common for nodes with good ping times to suddenly become lagged,
@@ -635,14 +674,16 @@ void CNode::copyStats(CNodeStats &stats)
635
674
stats.dPingWait = (((double )nPingUsecWait) / 1e6 );
636
675
637
676
// Leave string empty if addrLocal invalid (not filled in yet)
638
- stats.addrLocal = addrLocal.IsValid () ? addrLocal.ToString () : " " ;
677
+ CService addrLocalUnlocked = GetAddrLocal ();
678
+ stats.addrLocal = addrLocalUnlocked.IsValid () ? addrLocalUnlocked.ToString () : " " ;
639
679
}
640
680
#undef X
641
681
642
682
bool CNode::ReceiveMsgBytes (const char *pch, unsigned int nBytes, bool & complete)
643
683
{
644
684
complete = false ;
645
685
int64_t nTimeMicros = GetTimeMicros ();
686
+ LOCK (cs_vRecv);
646
687
nLastRecv = nTimeMicros / 1000000 ;
647
688
nRecvBytes += nBytes;
648
689
while (nBytes > 0 ) {
@@ -1786,8 +1827,9 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
1786
1827
if (pnode->addr .IsValid ()) {
1787
1828
mapConnected[pnode->addr ] = pnode->fInbound ;
1788
1829
}
1789
- if (!pnode->addrName .empty ()) {
1790
- mapConnectedByName[pnode->addrName ] = std::make_pair (pnode->fInbound , static_cast <const CService&>(pnode->addr ));
1830
+ std::string addrName = pnode->GetAddrName ();
1831
+ if (!addrName.empty ()) {
1832
+ mapConnectedByName[std::move (addrName)] = std::make_pair (pnode->fInbound , static_cast <const CService&>(pnode->addr ));
1791
1833
}
1792
1834
}
1793
1835
}
@@ -2414,9 +2456,8 @@ void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats)
2414
2456
vstats.reserve (vNodes.size ());
2415
2457
for (std::vector<CNode*>::iterator it = vNodes.begin (); it != vNodes.end (); ++it) {
2416
2458
CNode* pnode = *it;
2417
- CNodeStats stats;
2418
- pnode->copyStats (stats);
2419
- vstats.push_back (stats);
2459
+ vstats.emplace_back ();
2460
+ pnode->copyStats (vstats.back ());
2420
2461
}
2421
2462
}
2422
2463
@@ -2568,6 +2609,7 @@ unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
2568
2609
unsigned int CConnman::GetSendBufferSize () const { return nSendBufferMaxSize; }
2569
2610
2570
2611
CNode::CNode (NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const std::string& addrNameIn, bool fInboundIn ) :
2612
+ nTimeConnected(GetSystemTimeInSeconds()),
2571
2613
addr(addrIn),
2572
2614
fInbound(fInboundIn ),
2573
2615
id(idIn),
@@ -2587,7 +2629,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
2587
2629
nLastRecv = 0 ;
2588
2630
nSendBytes = 0 ;
2589
2631
nRecvBytes = 0 ;
2590
- nTimeConnected = GetSystemTimeInSeconds ();
2591
2632
nTimeOffset = 0 ;
2592
2633
addrName = addrNameIn == " " ? addr.ToStringIPPort () : addrNameIn;
2593
2634
nVersion = 0 ;
0 commit comments