Bitcoin
Classes | Public Types | Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
CConnman Class Reference

#include <net.h>

Classes

struct  ListenSocket
 
struct  Options
 

Public Types

enum  NumConnections { CONNECTIONS_NONE = 0, CONNECTIONS_IN = (1U << 0), CONNECTIONS_OUT = (1U << 1), CONNECTIONS_ALL = (CONNECTIONS_IN | CONNECTIONS_OUT) }
 

Public Member Functions

void Init (const Options &connOptions)
 
 CConnman (uint64_t seed0, uint64_t seed1)
 
 ~CConnman ()
 
bool Start (CScheduler &scheduler, const Options &options)
 
void Stop () NO_THREAD_SAFETY_ANALYSIS
 
void Interrupt ()
 
bool GetNetworkActive () const
 
bool GetUseAddrmanOutgoing () const
 
void SetNetworkActive (bool active)
 
void OpenNetworkConnection (const CAddress &addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound=nullptr, const char *strDest=nullptr, bool fOneShot=false, bool fFeeler=false, bool manual_connection=false)
 
bool CheckIncomingNonce (uint64_t nonce)
 
bool ForNode (NodeId id, std::function< bool(CNode *pnode)> func)
 
void PushMessage (CNode *pnode, CSerializedNetMsg &&msg)
 
template<typename Callable >
void ForEachNode (Callable &&func)
 
template<typename Callable >
void ForEachNode (Callable &&func) const
 
template<typename Callable , typename CallableAfter >
void ForEachNodeThen (Callable &&pre, CallableAfter &&post)
 
template<typename Callable , typename CallableAfter >
void ForEachNodeThen (Callable &&pre, CallableAfter &&post) const
 
size_t GetAddressCount () const
 
void SetServices (const CService &addr, ServiceFlags nServices)
 
void MarkAddressGood (const CAddress &addr)
 
void AddNewAddresses (const std::vector< CAddress > &vAddr, const CAddress &addrFrom, int64_t nTimePenalty=0)
 
std::vector< CAddressGetAddresses ()
 
void SetTryNewOutboundPeer (bool flag)
 
bool GetTryNewOutboundPeer ()
 
int GetExtraOutboundCount ()
 
bool AddNode (const std::string &node)
 
bool RemoveAddedNode (const std::string &node)
 
std::vector< AddedNodeInfoGetAddedNodeInfo ()
 
size_t GetNodeCount (NumConnections num)
 
void GetNodeStats (std::vector< CNodeStats > &vstats)
 
bool DisconnectNode (const std::string &node)
 
bool DisconnectNode (const CSubNet &subnet)
 
bool DisconnectNode (const CNetAddr &addr)
 
bool DisconnectNode (NodeId id)
 
ServiceFlags GetLocalServices () const
 
void SetMaxOutboundTarget (uint64_t limit)
 set the max outbound target in bytes More...
 
uint64_t GetMaxOutboundTarget ()
 
void SetMaxOutboundTimeframe (uint64_t timeframe)
 set the timeframe for the max outbound target More...
 
uint64_t GetMaxOutboundTimeframe ()
 
bool OutboundTargetReached (bool historicalBlockServingLimit)
 
uint64_t GetOutboundTargetBytesLeft ()
 
uint64_t GetMaxOutboundTimeLeftInCycle ()
 
uint64_t GetTotalBytesRecv ()
 
uint64_t GetTotalBytesSent ()
 
void SetBestHeight (int height)
 
int GetBestHeight () const
 
CSipHasher GetDeterministicRandomizer (uint64_t id) const
 
unsigned int GetReceiveFloodSize () const
 
void WakeMessageHandler ()
 
int64_t PoissonNextSendInbound (int64_t now, int average_interval_seconds)
 

Private Member Functions

bool BindListenPort (const CService &bindAddr, std::string &strError, bool fWhitelisted=false)
 
bool Bind (const CService &addr, unsigned int flags)
 
bool InitBinds (const std::vector< CService > &binds, const std::vector< CService > &whiteBinds)
 
void ThreadOpenAddedConnections ()
 
void AddOneShot (const std::string &strDest)
 
void ProcessOneShot ()
 
void ThreadOpenConnections (std::vector< std::string > connect)
 
void ThreadMessageHandler ()
 
void AcceptConnection (const ListenSocket &hListenSocket)
 
void DisconnectNodes ()
 
void NotifyNumConnectionsChanged ()
 
void InactivityCheck (CNode *pnode)
 
bool GenerateSelectSet (std::set< SOCKET > &recv_set, std::set< SOCKET > &send_set, std::set< SOCKET > &error_set)
 
void SocketEvents (std::set< SOCKET > &recv_set, std::set< SOCKET > &send_set, std::set< SOCKET > &error_set)
 
void SocketHandler ()
 
void ThreadSocketHandler ()
 
void ThreadDNSAddressSeed ()
 
uint64_t CalculateKeyedNetGroup (const CAddress &ad) const
 
CNodeFindNode (const CNetAddr &ip)
 
CNodeFindNode (const CSubNet &subNet)
 
CNodeFindNode (const std::string &addrName)
 
CNodeFindNode (const CService &addr)
 
bool AttemptToEvictConnection ()
 
CNodeConnectNode (CAddress addrConnect, const char *pszDest, bool fCountFailure, bool manual_connection)
 
bool IsWhitelistedRange (const CNetAddr &addr)
 
void DeleteNode (CNode *pnode)
 
NodeId GetNewNodeId ()
 
size_t SocketSendData (CNode *pnode) const
 
void DumpAddresses ()
 
void RecordBytesRecv (uint64_t bytes)
 
void RecordBytesSent (uint64_t bytes)
 
uint64_t nTotalBytesRecv GUARDED_BY (cs_totalBytesRecv)
 
uint64_t nTotalBytesSent GUARDED_BY (cs_totalBytesSent)
 
uint64_t nMaxOutboundTotalBytesSentInCycle GUARDED_BY (cs_totalBytesSent)
 
uint64_t nMaxOutboundCycleStartTime GUARDED_BY (cs_totalBytesSent)
 
uint64_t nMaxOutboundLimit GUARDED_BY (cs_totalBytesSent)
 
uint64_t nMaxOutboundTimeframe GUARDED_BY (cs_totalBytesSent)
 
std::deque< std::string > vOneShots GUARDED_BY (cs_vOneShots)
 
std::vector< std::string > vAddedNodes GUARDED_BY (cs_vAddedNodes)
 
std::vector< CNode * > vNodes GUARDED_BY (cs_vNodes)
 

Static Private Member Functions

static bool NodeFullyConnected (const CNode *pnode)
 

Private Attributes

CCriticalSection cs_totalBytesRecv
 
CCriticalSection cs_totalBytesSent
 
int64_t m_peer_connect_timeout
 
std::vector< CSubNetvWhitelistedRange
 
unsigned int nSendBufferMaxSize {0}
 
unsigned int nReceiveFloodSize {0}
 
std::vector< ListenSocketvhListenSocket
 
std::atomic< bool > fNetworkActive {true}
 
bool fAddressesInitialized {false}
 
CAddrMan addrman
 
CCriticalSection cs_vOneShots
 
CCriticalSection cs_vAddedNodes
 
std::list< CNode * > vNodesDisconnected
 
CCriticalSection cs_vNodes
 
std::atomic< NodeIdnLastNodeId {0}
 
unsigned int nPrevNodeCount {0}
 
ServiceFlags nLocalServices
 
std::unique_ptr< CSemaphoresemOutbound
 
std::unique_ptr< CSemaphoresemAddnode
 
int nMaxConnections
 
int nMaxOutbound
 
int nMaxAddnode
 
int nMaxFeeler
 
bool m_use_addrman_outgoing
 
std::atomic< int > nBestHeight
 
CClientUIInterfaceclientInterface
 
NetEventsInterfacem_msgproc
 
BanManm_banman
 
const uint64_t nSeed0
 
const uint64_t nSeed1
 
bool fMsgProcWake
 
std::condition_variable condMsgProc
 
Mutex mutexMsgProc
 
std::atomic< bool > flagInterruptMsgProc {false}
 
CThreadInterrupt interruptNet
 
std::thread threadDNSAddressSeed
 
std::thread threadSocketHandler
 
std::thread threadOpenAddedConnections
 
std::thread threadOpenConnections
 
std::thread threadMessageHandler
 
std::atomic_bool m_try_another_outbound_peer
 
std::atomic< int64_tm_next_send_inv_to_incoming {0}
 

Friends

struct CConnmanTest
 

Member Enumeration Documentation

◆ NumConnections

Enumerator
CONNECTIONS_NONE 
CONNECTIONS_IN 
CONNECTIONS_OUT 
CONNECTIONS_ALL 

Constructor & Destructor Documentation

◆ CConnman()

CConnman::CConnman ( uint64_t  seed0,
uint64_t  seed1 
)

◆ ~CConnman()

CConnman::~CConnman ( )

Member Function Documentation

◆ AcceptConnection()

void CConnman::AcceptConnection ( const ListenSocket hListenSocket)
private

◆ AddNewAddresses()

void CConnman::AddNewAddresses ( const std::vector< CAddress > &  vAddr,
const CAddress addrFrom,
int64_t  nTimePenalty = 0 
)

◆ AddNode()

bool CConnman::AddNode ( const std::string &  node)

◆ AddOneShot()

void CConnman::AddOneShot ( const std::string &  strDest)
private

◆ AttemptToEvictConnection()

bool CConnman::AttemptToEvictConnection ( )
private

Try to find a connection to evict when the node is full. Extreme care must be taken to avoid opening the node to attacker triggered network partitioning. The strategy used here is to protect a small number of peers for each of several distinct characteristics which are difficult to forge. In order to partition a node the attacker must be simultaneously better at all of them than honest peers.

◆ Bind()

bool CConnman::Bind ( const CService addr,
unsigned int  flags 
)
private

◆ BindListenPort()

bool CConnman::BindListenPort ( const CService bindAddr,
std::string &  strError,
bool  fWhitelisted = false 
)
private

◆ CalculateKeyedNetGroup()

uint64_t CConnman::CalculateKeyedNetGroup ( const CAddress ad) const
private

◆ CheckIncomingNonce()

bool CConnman::CheckIncomingNonce ( uint64_t  nonce)

◆ ConnectNode()

CNode * CConnman::ConnectNode ( CAddress  addrConnect,
const char *  pszDest,
bool  fCountFailure,
bool  manual_connection 
)
private

debug print

◆ DeleteNode()

void CConnman::DeleteNode ( CNode pnode)
private

◆ DisconnectNode() [1/4]

bool CConnman::DisconnectNode ( const std::string &  node)

◆ DisconnectNode() [2/4]

bool CConnman::DisconnectNode ( const CSubNet subnet)

◆ DisconnectNode() [3/4]

bool CConnman::DisconnectNode ( const CNetAddr addr)

◆ DisconnectNode() [4/4]

bool CConnman::DisconnectNode ( NodeId  id)

◆ DisconnectNodes()

void CConnman::DisconnectNodes ( )
private

◆ DumpAddresses()

void CConnman::DumpAddresses ( )
private

◆ FindNode() [1/4]

CNode * CConnman::FindNode ( const CNetAddr ip)
private

◆ FindNode() [2/4]

CNode * CConnman::FindNode ( const CSubNet subNet)
private

◆ FindNode() [3/4]

CNode * CConnman::FindNode ( const std::string &  addrName)
private

◆ FindNode() [4/4]

CNode * CConnman::FindNode ( const CService addr)
private

◆ ForEachNode() [1/2]

template<typename Callable >
void CConnman::ForEachNode ( Callable &&  func)
inline

◆ ForEachNode() [2/2]

template<typename Callable >
void CConnman::ForEachNode ( Callable &&  func) const
inline

◆ ForEachNodeThen() [1/2]

template<typename Callable , typename CallableAfter >
void CConnman::ForEachNodeThen ( Callable &&  pre,
CallableAfter &&  post 
)
inline

◆ ForEachNodeThen() [2/2]

template<typename Callable , typename CallableAfter >
void CConnman::ForEachNodeThen ( Callable &&  pre,
CallableAfter &&  post 
) const
inline

◆ ForNode()

bool CConnman::ForNode ( NodeId  id,
std::function< bool(CNode *pnode)>  func 
)

◆ GenerateSelectSet()

bool CConnman::GenerateSelectSet ( std::set< SOCKET > &  recv_set,
std::set< SOCKET > &  send_set,
std::set< SOCKET > &  error_set 
)
private

◆ GetAddedNodeInfo()

std::vector< AddedNodeInfo > CConnman::GetAddedNodeInfo ( )

◆ GetAddressCount()

size_t CConnman::GetAddressCount ( ) const

◆ GetAddresses()

std::vector< CAddress > CConnman::GetAddresses ( )

◆ GetBestHeight()

int CConnman::GetBestHeight ( ) const

◆ GetDeterministicRandomizer()

CSipHasher CConnman::GetDeterministicRandomizer ( uint64_t  id) const

Get a unique deterministic randomizer.

◆ GetExtraOutboundCount()

int CConnman::GetExtraOutboundCount ( )

◆ GetLocalServices()

ServiceFlags CConnman::GetLocalServices ( ) const

◆ GetMaxOutboundTarget()

uint64_t CConnman::GetMaxOutboundTarget ( )

◆ GetMaxOutboundTimeframe()

uint64_t CConnman::GetMaxOutboundTimeframe ( )

◆ GetMaxOutboundTimeLeftInCycle()

uint64_t CConnman::GetMaxOutboundTimeLeftInCycle ( )

response the time in second left in the current max outbound cycle in case of no limit, it will always response 0

◆ GetNetworkActive()

bool CConnman::GetNetworkActive ( ) const
inline

◆ GetNewNodeId()

NodeId CConnman::GetNewNodeId ( )
private

◆ GetNodeCount()

size_t CConnman::GetNodeCount ( NumConnections  num)

◆ GetNodeStats()

void CConnman::GetNodeStats ( std::vector< CNodeStats > &  vstats)

◆ GetOutboundTargetBytesLeft()

uint64_t CConnman::GetOutboundTargetBytesLeft ( )

response the bytes left in the current max outbound cycle in case of no limit, it will always response 0

◆ GetReceiveFloodSize()

unsigned int CConnman::GetReceiveFloodSize ( ) const

◆ GetTotalBytesRecv()

uint64_t CConnman::GetTotalBytesRecv ( )

◆ GetTotalBytesSent()

uint64_t CConnman::GetTotalBytesSent ( )

◆ GetTryNewOutboundPeer()

bool CConnman::GetTryNewOutboundPeer ( )

◆ GetUseAddrmanOutgoing()

bool CConnman::GetUseAddrmanOutgoing ( ) const
inline

◆ GUARDED_BY() [1/9]

uint64_t nTotalBytesRecv CConnman::GUARDED_BY ( cs_totalBytesRecv  )
private

◆ GUARDED_BY() [2/9]

uint64_t nTotalBytesSent CConnman::GUARDED_BY ( cs_totalBytesSent  )
private

◆ GUARDED_BY() [3/9]

uint64_t nMaxOutboundTotalBytesSentInCycle CConnman::GUARDED_BY ( cs_totalBytesSent  )
private

◆ GUARDED_BY() [4/9]

uint64_t nMaxOutboundCycleStartTime CConnman::GUARDED_BY ( cs_totalBytesSent  )
private

◆ GUARDED_BY() [5/9]

uint64_t nMaxOutboundLimit CConnman::GUARDED_BY ( cs_totalBytesSent  )
private

◆ GUARDED_BY() [6/9]

uint64_t nMaxOutboundTimeframe CConnman::GUARDED_BY ( cs_totalBytesSent  )
private

◆ GUARDED_BY() [7/9]

std::deque<std::string> vOneShots CConnman::GUARDED_BY ( cs_vOneShots  )
private

◆ GUARDED_BY() [8/9]

std::vector<std::string> vAddedNodes CConnman::GUARDED_BY ( cs_vAddedNodes  )
private

◆ GUARDED_BY() [9/9]

std::vector<CNode*> vNodes CConnman::GUARDED_BY ( cs_vNodes  )
private

◆ InactivityCheck()

void CConnman::InactivityCheck ( CNode pnode)
private

◆ Init()

void CConnman::Init ( const Options connOptions)
inline

◆ InitBinds()

bool CConnman::InitBinds ( const std::vector< CService > &  binds,
const std::vector< CService > &  whiteBinds 
)
private

◆ Interrupt()

void CConnman::Interrupt ( )

◆ IsWhitelistedRange()

bool CConnman::IsWhitelistedRange ( const CNetAddr addr)
private

◆ MarkAddressGood()

void CConnman::MarkAddressGood ( const CAddress addr)

◆ NodeFullyConnected()

bool CConnman::NodeFullyConnected ( const CNode pnode)
staticprivate

◆ NotifyNumConnectionsChanged()

void CConnman::NotifyNumConnectionsChanged ( )
private

◆ OpenNetworkConnection()

void CConnman::OpenNetworkConnection ( const CAddress addrConnect,
bool  fCountFailure,
CSemaphoreGrant grantOutbound = nullptr,
const char *  strDest = nullptr,
bool  fOneShot = false,
bool  fFeeler = false,
bool  manual_connection = false 
)

◆ OutboundTargetReached()

bool CConnman::OutboundTargetReached ( bool  historicalBlockServingLimit)

check if the outbound target is reached if param historicalBlockServingLimit is set true, the function will response true if the limit for serving historical blocks has been reached

◆ PoissonNextSendInbound()

int64_t CConnman::PoissonNextSendInbound ( int64_t  now,
int  average_interval_seconds 
)

Attempts to obfuscate tx time through exponentially distributed emitting. Works assuming that a single interval is used. Variable intervals will result in privacy decrease.

◆ ProcessOneShot()

void CConnman::ProcessOneShot ( )
private

◆ PushMessage()

void CConnman::PushMessage ( CNode pnode,
CSerializedNetMsg &&  msg 
)

◆ RecordBytesRecv()

void CConnman::RecordBytesRecv ( uint64_t  bytes)
private

◆ RecordBytesSent()

void CConnman::RecordBytesSent ( uint64_t  bytes)
private

◆ RemoveAddedNode()

bool CConnman::RemoveAddedNode ( const std::string &  node)

◆ SetBestHeight()

void CConnman::SetBestHeight ( int  height)

◆ SetMaxOutboundTarget()

void CConnman::SetMaxOutboundTarget ( uint64_t  limit)

set the max outbound target in bytes

◆ SetMaxOutboundTimeframe()

void CConnman::SetMaxOutboundTimeframe ( uint64_t  timeframe)

set the timeframe for the max outbound target

◆ SetNetworkActive()

void CConnman::SetNetworkActive ( bool  active)

◆ SetServices()

void CConnman::SetServices ( const CService addr,
ServiceFlags  nServices 
)

◆ SetTryNewOutboundPeer()

void CConnman::SetTryNewOutboundPeer ( bool  flag)

◆ SocketEvents()

void CConnman::SocketEvents ( std::set< SOCKET > &  recv_set,
std::set< SOCKET > &  send_set,
std::set< SOCKET > &  error_set 
)
private

◆ SocketHandler()

void CConnman::SocketHandler ( )
private

◆ SocketSendData()

size_t CConnman::SocketSendData ( CNode pnode) const
private

◆ Start()

bool CConnman::Start ( CScheduler scheduler,
const Options options 
)

◆ Stop()

void CConnman::Stop ( )

◆ ThreadDNSAddressSeed()

void CConnman::ThreadDNSAddressSeed ( )
private

◆ ThreadMessageHandler()

void CConnman::ThreadMessageHandler ( )
private

◆ ThreadOpenAddedConnections()

void CConnman::ThreadOpenAddedConnections ( )
private

◆ ThreadOpenConnections()

void CConnman::ThreadOpenConnections ( std::vector< std::string >  connect)
private

◆ ThreadSocketHandler()

void CConnman::ThreadSocketHandler ( )
private

◆ WakeMessageHandler()

void CConnman::WakeMessageHandler ( )

Friends And Related Function Documentation

◆ CConnmanTest

friend struct CConnmanTest
friend

Member Data Documentation

◆ addrman

CAddrMan CConnman::addrman
private

◆ clientInterface

CClientUIInterface* CConnman::clientInterface
private

◆ condMsgProc

std::condition_variable CConnman::condMsgProc
private

◆ cs_totalBytesRecv

CCriticalSection CConnman::cs_totalBytesRecv
private

◆ cs_totalBytesSent

CCriticalSection CConnman::cs_totalBytesSent
private

◆ cs_vAddedNodes

CCriticalSection CConnman::cs_vAddedNodes
private

◆ cs_vNodes

CCriticalSection CConnman::cs_vNodes
mutableprivate

◆ cs_vOneShots

CCriticalSection CConnman::cs_vOneShots
private

◆ fAddressesInitialized

bool CConnman::fAddressesInitialized {false}
private

◆ flagInterruptMsgProc

std::atomic<bool> CConnman::flagInterruptMsgProc {false}
private

◆ fMsgProcWake

bool CConnman::fMsgProcWake
private

flag for waking the message processor.

◆ fNetworkActive

std::atomic<bool> CConnman::fNetworkActive {true}
private

◆ interruptNet

CThreadInterrupt CConnman::interruptNet
private

◆ m_banman

BanMan* CConnman::m_banman
private

◆ m_msgproc

NetEventsInterface* CConnman::m_msgproc
private

◆ m_next_send_inv_to_incoming

std::atomic<int64_t> CConnman::m_next_send_inv_to_incoming {0}
private

◆ m_peer_connect_timeout

int64_t CConnman::m_peer_connect_timeout
private

◆ m_try_another_outbound_peer

std::atomic_bool CConnman::m_try_another_outbound_peer
private

flag for deciding to connect to an extra outbound peer, in excess of nMaxOutbound This takes the place of a feeler connection

◆ m_use_addrman_outgoing

bool CConnman::m_use_addrman_outgoing
private

◆ mutexMsgProc

Mutex CConnman::mutexMsgProc
private

◆ nBestHeight

std::atomic<int> CConnman::nBestHeight
private

◆ nLastNodeId

std::atomic<NodeId> CConnman::nLastNodeId {0}
private

◆ nLocalServices

ServiceFlags CConnman::nLocalServices
private

Services this instance offers

◆ nMaxAddnode

int CConnman::nMaxAddnode
private

◆ nMaxConnections

int CConnman::nMaxConnections
private

◆ nMaxFeeler

int CConnman::nMaxFeeler
private

◆ nMaxOutbound

int CConnman::nMaxOutbound
private

◆ nPrevNodeCount

unsigned int CConnman::nPrevNodeCount {0}
private

◆ nReceiveFloodSize

unsigned int CConnman::nReceiveFloodSize {0}
private

◆ nSeed0

const uint64_t CConnman::nSeed0
private

SipHasher seeds for deterministic randomness

◆ nSeed1

const uint64_t CConnman::nSeed1
private

◆ nSendBufferMaxSize

unsigned int CConnman::nSendBufferMaxSize {0}
private

◆ semAddnode

std::unique_ptr<CSemaphore> CConnman::semAddnode
private

◆ semOutbound

std::unique_ptr<CSemaphore> CConnman::semOutbound
private

◆ threadDNSAddressSeed

std::thread CConnman::threadDNSAddressSeed
private

◆ threadMessageHandler

std::thread CConnman::threadMessageHandler
private

◆ threadOpenAddedConnections

std::thread CConnman::threadOpenAddedConnections
private

◆ threadOpenConnections

std::thread CConnman::threadOpenConnections
private

◆ threadSocketHandler

std::thread CConnman::threadSocketHandler
private

◆ vhListenSocket

std::vector<ListenSocket> CConnman::vhListenSocket
private

◆ vNodesDisconnected

std::list<CNode*> CConnman::vNodesDisconnected
private

◆ vWhitelistedRange

std::vector<CSubNet> CConnman::vWhitelistedRange
private

The documentation for this class was generated from the following files: