@@ -588,9 +588,9 @@ bool CNode::IsBlockRelayOnly() const {
588
588
return (ignores_incoming_txs && !HasPermission (NetPermissionFlags::Relay)) || IsBlockOnlyConn ();
589
589
}
590
590
591
- std::string CNode:: ConnectionTypeAsString () const
591
+ std::string ConnectionTypeAsString (ConnectionType conn_type)
592
592
{
593
- switch (m_conn_type ) {
593
+ switch (conn_type ) {
594
594
case ConnectionType::INBOUND:
595
595
return " inbound" ;
596
596
case ConnectionType::MANUAL:
@@ -700,7 +700,7 @@ void CNode::copyStats(CNodeStats &stats, const std::vector<bool> &m_asmap)
700
700
X (verifiedPubKeyHash);
701
701
}
702
702
X (m_masternode_connection);
703
- stats. m_conn_type_string = ConnectionTypeAsString ( );
703
+ X (m_conn_type );
704
704
}
705
705
#undef X
706
706
@@ -2169,7 +2169,7 @@ void CConnman::ThreadDNSAddressSeed()
2169
2169
{
2170
2170
LOCK (cs_vNodes);
2171
2171
for (const CNode* pnode : vNodes) {
2172
- if (pnode->fSuccessfullyConnected && !pnode->IsOutboundOrBlockRelayConn () && !pnode->m_masternode_probe_connection ) ++nRelevant;
2172
+ if (pnode->fSuccessfullyConnected && !pnode->IsFullOutboundConn () && !pnode->m_masternode_probe_connection ) ++nRelevant;
2173
2173
}
2174
2174
}
2175
2175
if (nRelevant >= 2 ) {
@@ -2256,7 +2256,7 @@ void CConnman::ProcessAddrFetch()
2256
2256
}
2257
2257
}
2258
2258
2259
- bool CConnman::GetTryNewOutboundPeer ()
2259
+ bool CConnman::GetTryNewOutboundPeer () const
2260
2260
{
2261
2261
return m_try_another_outbound_peer;
2262
2262
}
@@ -2273,7 +2273,7 @@ void CConnman::SetTryNewOutboundPeer(bool flag)
2273
2273
// Also exclude peers that haven't finished initial connection handshake yet
2274
2274
// (so that we don't decide we're over our desired connection limit, and then
2275
2275
// evict some peer that has finished the handshake)
2276
- int CConnman::GetExtraFullOutboundCount ()
2276
+ int CConnman::GetExtraFullOutboundCount () const
2277
2277
{
2278
2278
int full_outbound_peers = 0 ;
2279
2279
{
@@ -2291,7 +2291,7 @@ int CConnman::GetExtraFullOutboundCount()
2291
2291
return std::max (full_outbound_peers - m_max_outbound_full_relay, 0 );
2292
2292
}
2293
2293
2294
- int CConnman::GetExtraBlockRelayCount ()
2294
+ int CConnman::GetExtraBlockRelayCount () const
2295
2295
{
2296
2296
int block_relay_peers = 0 ;
2297
2297
{
@@ -2619,7 +2619,7 @@ std::vector<CAddress> CConnman::GetCurrentBlockRelayOnlyConns() const
2619
2619
return ret;
2620
2620
}
2621
2621
2622
- std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo ()
2622
+ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo () const
2623
2623
{
2624
2624
std::vector<AddedNodeInfo> ret;
2625
2625
@@ -2965,6 +2965,7 @@ void CConnman::ThreadMessageHandler()
2965
2965
{
2966
2966
int64_t nLastSendMessagesTimeMasternodes = 0 ;
2967
2967
2968
+ FastRandomContext rng;
2968
2969
while (!flagInterruptMsgProc)
2969
2970
{
2970
2971
std::vector<CNode*> vNodesCopy = CopyNodeVector ();
@@ -2976,6 +2977,10 @@ void CConnman::ThreadMessageHandler()
2976
2977
fSkipSendMessagesForMasternodes = false ;
2977
2978
nLastSendMessagesTimeMasternodes = GetTimeMillis ();
2978
2979
}
2980
+ // Randomize the order in which we process messages from/to our peers.
2981
+ // This prevents attacks in which an attacker exploits having multiple
2982
+ // consecutive connections in the vNodes list.
2983
+ Shuffle (vNodesCopy.begin (), vNodesCopy.end (), rng);
2979
2984
2980
2985
for (CNode* pnode : vNodesCopy)
2981
2986
{
@@ -3616,7 +3621,7 @@ CConnman::~CConnman()
3616
3621
Stop ();
3617
3622
}
3618
3623
3619
- std::vector<CAddress> CConnman::GetAddresses (size_t max_addresses, size_t max_pct, std::optional<Network> network)
3624
+ std::vector<CAddress> CConnman::GetAddresses (size_t max_addresses, size_t max_pct, std::optional<Network> network) const
3620
3625
{
3621
3626
std::vector<CAddress> addresses = addrman.GetAddr (max_addresses, max_pct, network);
3622
3627
if (m_banman) {
@@ -3837,7 +3842,7 @@ void CConnman::AddPendingProbeConnections(const std::set<uint256> &proTxHashes)
3837
3842
masternodePendingProbes.insert (proTxHashes.begin (), proTxHashes.end ());
3838
3843
}
3839
3844
3840
- size_t CConnman::GetNodeCount (ConnectionDirection flags)
3845
+ size_t CConnman::GetNodeCount (ConnectionDirection flags) const
3841
3846
{
3842
3847
LOCK (cs_vNodes);
3843
3848
@@ -3864,7 +3869,7 @@ size_t CConnman::GetMaxOutboundNodeCount()
3864
3869
return m_max_outbound;
3865
3870
}
3866
3871
3867
- void CConnman::GetNodeStats (std::vector<CNodeStats>& vstats)
3872
+ void CConnman::GetNodeStats (std::vector<CNodeStats>& vstats) const
3868
3873
{
3869
3874
vstats.clear ();
3870
3875
LOCK (cs_vNodes);
@@ -4003,18 +4008,18 @@ void CConnman::RecordBytesSent(uint64_t bytes)
4003
4008
nMaxOutboundTotalBytesSentInCycle += bytes;
4004
4009
}
4005
4010
4006
- uint64_t CConnman::GetMaxOutboundTarget ()
4011
+ uint64_t CConnman::GetMaxOutboundTarget () const
4007
4012
{
4008
4013
LOCK (cs_totalBytesSent);
4009
4014
return nMaxOutboundLimit;
4010
4015
}
4011
4016
4012
- std::chrono::seconds CConnman::GetMaxOutboundTimeframe ()
4017
+ std::chrono::seconds CConnman::GetMaxOutboundTimeframe () const
4013
4018
{
4014
4019
return MAX_UPLOAD_TIMEFRAME;
4015
4020
}
4016
4021
4017
- std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle ()
4022
+ std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle () const
4018
4023
{
4019
4024
LOCK (cs_totalBytesSent);
4020
4025
if (nMaxOutboundLimit == 0 )
@@ -4028,7 +4033,7 @@ std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle()
4028
4033
return (cycleEndTime < now) ? 0s : cycleEndTime - now;
4029
4034
}
4030
4035
4031
- bool CConnman::OutboundTargetReached (bool historicalBlockServingLimit)
4036
+ bool CConnman::OutboundTargetReached (bool historicalBlockServingLimit) const
4032
4037
{
4033
4038
LOCK (cs_totalBytesSent);
4034
4039
if (nMaxOutboundLimit == 0 )
@@ -4048,7 +4053,7 @@ bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
4048
4053
return false ;
4049
4054
}
4050
4055
4051
- uint64_t CConnman::GetOutboundTargetBytesLeft ()
4056
+ uint64_t CConnman::GetOutboundTargetBytesLeft () const
4052
4057
{
4053
4058
LOCK (cs_totalBytesSent);
4054
4059
if (nMaxOutboundLimit == 0 )
@@ -4057,13 +4062,13 @@ uint64_t CConnman::GetOutboundTargetBytesLeft()
4057
4062
return (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit) ? 0 : nMaxOutboundLimit - nMaxOutboundTotalBytesSentInCycle;
4058
4063
}
4059
4064
4060
- uint64_t CConnman::GetTotalBytesRecv ()
4065
+ uint64_t CConnman::GetTotalBytesRecv () const
4061
4066
{
4062
4067
LOCK (cs_totalBytesRecv);
4063
4068
return nTotalBytesRecv;
4064
4069
}
4065
4070
4066
- uint64_t CConnman::GetTotalBytesSent ()
4071
+ uint64_t CConnman::GetTotalBytesSent () const
4067
4072
{
4068
4073
LOCK (cs_totalBytesSent);
4069
4074
return nTotalBytesSent;
0 commit comments