![]() |
Bitcoin Core
22.99.0
P2P Digital Currency
|
#include <net.h>
Classes | |
struct | CachedAddrResponse |
Cache responses to addr requests to minimize privacy leak. More... | |
struct | ListenSocket |
class | NodesSnapshot |
RAII helper to atomically create a copy of m_nodes and add a reference to each of the nodes. More... | |
struct | Options |
Public Types | |
using | NodeFn = std::function< void(CNode *)> |
Public Member Functions | |
void | Init (const Options &connOptions) |
CConnman (uint64_t seed0, uint64_t seed1, AddrMan &addrman, bool network_active=true) | |
~CConnman () | |
bool | Start (CScheduler &scheduler, const Options &options) |
void | StopThreads () |
void | StopNodes () |
void | Stop () |
void | Interrupt () |
bool | GetNetworkActive () const |
bool | GetUseAddrmanOutgoing () const |
void | SetNetworkActive (bool active) |
void | OpenNetworkConnection (const CAddress &addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *strDest, ConnectionType conn_type) |
bool | CheckIncomingNonce (uint64_t nonce) |
bool | ForNode (NodeId id, std::function< bool(CNode *pnode)> func) |
void | PushMessage (CNode *pnode, CSerializedNetMsg &&msg) |
void | ForEachNode (const NodeFn &func) |
void | ForEachNode (const NodeFn &func) const |
std::vector< CAddress > | GetAddresses (size_t max_addresses, size_t max_pct, std::optional< Network > network) const |
Return all or many randomly selected addresses, optionally by network. More... | |
std::vector< CAddress > | GetAddresses (CNode &requestor, size_t max_addresses, size_t max_pct) |
Cache is used to minimize topology leaks, so it should be used for all non-trusted calls, for example, p2p. More... | |
void | SetTryNewOutboundPeer (bool flag) |
bool | GetTryNewOutboundPeer () const |
void | StartExtraBlockRelayPeers () |
int | GetExtraFullOutboundCount () const |
int | GetExtraBlockRelayCount () const |
bool | AddNode (const std::string &node) |
bool | RemoveAddedNode (const std::string &node) |
std::vector< AddedNodeInfo > | GetAddedNodeInfo () const |
bool | AddConnection (const std::string &address, ConnectionType conn_type) |
Attempts to open a connection. More... | |
size_t | GetNodeCount (ConnectionDirection) const |
void | GetNodeStats (std::vector< CNodeStats > &vstats) const |
bool | DisconnectNode (const std::string &node) |
bool | DisconnectNode (const CSubNet &subnet) |
bool | DisconnectNode (const CNetAddr &addr) |
bool | DisconnectNode (NodeId id) |
ServiceFlags | GetLocalServices () const |
Used to convey which local services we are offering peers during node connection. More... | |
uint64_t | GetMaxOutboundTarget () const |
std::chrono::seconds | GetMaxOutboundTimeframe () const |
bool | OutboundTargetReached (bool historicalBlockServingLimit) const |
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 More... | |
uint64_t | GetOutboundTargetBytesLeft () const |
response the bytes left in the current max outbound cycle in case of no limit, it will always response 0 More... | |
std::chrono::seconds | GetMaxOutboundTimeLeftInCycle () const |
returns the time left in the current max outbound cycle in case of no limit, it will always return 0 More... | |
uint64_t | GetTotalBytesRecv () const |
uint64_t | GetTotalBytesSent () const |
CSipHasher | GetDeterministicRandomizer (uint64_t id) const |
Get a unique deterministic randomizer. More... | |
unsigned int | GetReceiveFloodSize () const |
void | WakeMessageHandler () |
bool | ShouldRunInactivityChecks (const CNode &node, std::chrono::seconds now) const |
Return true if we should disconnect the peer for failing an inactivity check. More... | |
Private Member Functions | |
bool | BindListenPort (const CService &bindAddr, bilingual_str &strError, NetPermissionFlags permissions) |
bool | Bind (const CService &addr, unsigned int flags, NetPermissionFlags permissions) |
bool | InitBinds (const Options &options) |
void | ThreadOpenAddedConnections () |
void | AddAddrFetch (const std::string &strDest) |
void | ProcessAddrFetch () |
void | ThreadOpenConnections (std::vector< std::string > connect) |
void | ThreadMessageHandler () |
void | ThreadI2PAcceptIncoming () |
void | AcceptConnection (const ListenSocket &hListenSocket) |
void | CreateNodeFromAcceptedSocket (std::unique_ptr< Sock > &&sock, NetPermissionFlags permissionFlags, const CAddress &addr_bind, const CAddress &addr) |
Create a CNode object from a socket that has just been accepted and add the node to the m_nodes member. More... | |
void | DisconnectNodes () |
void | NotifyNumConnectionsChanged () |
bool | InactivityCheck (const CNode &node) const |
Return true if the peer is inactive and should be disconnected. More... | |
bool | GenerateSelectSet (const std::vector< CNode * > &nodes, std::set< SOCKET > &recv_set, std::set< SOCKET > &send_set, std::set< SOCKET > &error_set) |
Generate a collection of sockets to check for IO readiness. More... | |
void | SocketEvents (const std::vector< CNode * > &nodes, std::set< SOCKET > &recv_set, std::set< SOCKET > &send_set, std::set< SOCKET > &error_set) |
Check which sockets are ready for IO. More... | |
void | SocketHandler () |
Check connected and listening sockets for IO readiness and process them accordingly. More... | |
void | SocketHandlerConnected (const std::vector< CNode * > &nodes, const std::set< SOCKET > &recv_set, const std::set< SOCKET > &send_set, const std::set< SOCKET > &error_set) |
Do the read/write for connected sockets that are ready for IO. More... | |
void | SocketHandlerListening (const std::set< SOCKET > &recv_set) |
Accept incoming connections, one from each read-ready listening socket. More... | |
void | ThreadSocketHandler () |
void | ThreadDNSAddressSeed () |
uint64_t | CalculateKeyedNetGroup (const CAddress &ad) const |
CNode * | FindNode (const CNetAddr &ip) |
CNode * | FindNode (const CSubNet &subNet) |
CNode * | FindNode (const std::string &addrName) |
CNode * | FindNode (const CService &addr) |
bool | AlreadyConnectedToAddress (const CAddress &addr) |
Determine whether we're already connected to a given address, in order to avoid initiating duplicate connections. More... | |
bool | AttemptToEvictConnection () |
Try to find a connection to evict when the node is full. More... | |
CNode * | ConnectNode (CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type) |
void | AddWhitelistPermissionFlags (NetPermissionFlags &flags, const CNetAddr &addr) const |
void | DeleteNode (CNode *pnode) |
NodeId | GetNewNodeId () |
size_t | SocketSendData (CNode &node) const EXCLUSIVE_LOCKS_REQUIRED(node.cs_vSend) |
void | DumpAddresses () |
void | RecordBytesRecv (uint64_t bytes) |
void | RecordBytesSent (uint64_t bytes) |
std::vector< CAddress > | GetCurrentBlockRelayOnlyConns () const |
Return vector of current BLOCK_RELAY peers. More... | |
uint64_t nTotalBytesSent | GUARDED_BY (cs_totalBytesSent) |
uint64_t nMaxOutboundTotalBytesSentInCycle | GUARDED_BY (cs_totalBytesSent) |
std::chrono::seconds nMaxOutboundCycleStartTime | GUARDED_BY (cs_totalBytesSent) |
uint64_t nMaxOutboundLimit | GUARDED_BY (cs_totalBytesSent) |
std::deque< std::string > m_addr_fetches | GUARDED_BY (m_addr_fetches_mutex) |
std::vector< std::string > m_added_nodes | GUARDED_BY (m_added_nodes_mutex) |
std::vector< CNode * > m_nodes | GUARDED_BY (m_nodes_mutex) |
bool fMsgProcWake | GUARDED_BY (mutexMsgProc) |
flag for waking the message processor. More... | |
Static Private Member Functions | |
static bool | NodeFullyConnected (const CNode *pnode) |
Private Attributes | |
RecursiveMutex | cs_totalBytesSent |
std::atomic< uint64_t > | nTotalBytesRecv {0} |
std::chrono::seconds | m_peer_connect_timeout |
std::vector< NetWhitelistPermissions > | vWhitelistedRange |
unsigned int | nSendBufferMaxSize {0} |
unsigned int | nReceiveFloodSize {0} |
std::vector< ListenSocket > | vhListenSocket |
std::atomic< bool > | fNetworkActive {true} |
bool | fAddressesInitialized {false} |
AddrMan & | addrman |
Mutex | m_addr_fetches_mutex |
Mutex | m_added_nodes_mutex |
std::list< CNode * > | m_nodes_disconnected |
RecursiveMutex | m_nodes_mutex |
std::atomic< NodeId > | nLastNodeId {0} |
unsigned int | nPrevNodeCount {0} |
std::map< uint64_t, CachedAddrResponse > | m_addr_response_caches |
Addr responses stored in different caches per (network, local socket) prevent cross-network node identification. More... | |
ServiceFlags | nLocalServices |
Services this instance offers. More... | |
std::unique_ptr< CSemaphore > | semOutbound |
std::unique_ptr< CSemaphore > | semAddnode |
int | nMaxConnections |
int | m_max_outbound_full_relay |
int | m_max_outbound_block_relay |
int | nMaxAddnode |
int | nMaxFeeler |
int | m_max_outbound |
bool | m_use_addrman_outgoing |
CClientUIInterface * | m_client_interface |
NetEventsInterface * | m_msgproc |
BanMan * | m_banman |
Pointer to this node's banman. More... | |
std::vector< CAddress > | m_anchors |
Addresses that were saved during the previous clean shutdown. More... | |
const uint64_t | nSeed0 |
SipHasher seeds for deterministic randomness. More... | |
const uint64_t | nSeed1 |
std::condition_variable | condMsgProc |
Mutex | mutexMsgProc |
std::atomic< bool > | flagInterruptMsgProc {false} |
CThreadInterrupt | interruptNet |
This is signaled when network activity should cease. More... | |
std::unique_ptr< i2p::sam::Session > | m_i2p_sam_session |
I2P SAM session. More... | |
std::thread | threadDNSAddressSeed |
std::thread | threadSocketHandler |
std::thread | threadOpenAddedConnections |
std::thread | threadOpenConnections |
std::thread | threadMessageHandler |
std::thread | threadI2PAcceptIncoming |
std::atomic_bool | m_try_another_outbound_peer |
flag for deciding to connect to an extra outbound peer, in excess of m_max_outbound_full_relay This takes the place of a feeler connection More... | |
std::atomic_bool | m_start_extra_block_relay_peers {false} |
flag for initiating extra block-relay-only peer connections. More... | |
std::vector< CService > | m_onion_binds |
A vector of -bind=<address>:<port>=onion arguments each of which is an address and port that are designated for incoming Tor connections. More... | |
Friends | |
struct | CConnmanTest |
struct | ConnmanTestMsg |
using CConnman::NodeFn = std::function<void(CNode*)> |
CConnman::CConnman | ( | uint64_t | seed0, |
uint64_t | seed1, | ||
AddrMan & | addrman, | ||
bool | network_active = true |
||
) |
CConnman::~CConnman | ( | ) |
|
private |
|
private |
bool CConnman::AddConnection | ( | const std::string & | address, |
ConnectionType | conn_type | ||
) |
Attempts to open a connection.
Currently only used from tests.
[in] | address | Address of node to try connecting to |
[in] | conn_type | ConnectionType::OUTBOUND, ConnectionType::BLOCK_RELAY, ConnectionType::ADDR_FETCH or ConnectionType::FEELER |
Definition at line 1238 of file net.cpp.
bool CConnman::AddNode | ( | const std::string & | node | ) |
|
private |
|
private |
|
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.
Definition at line 1067 of file net.cpp.
|
private |
|
private |
|
private |
|
private |
|
private |
Create a CNode
object from a socket that has just been accepted and add the node to the m_nodes
member.
[in] | sock | Connected socket to communicate with the peer. |
[in] | permissionFlags | The peer's permissions. |
[in] | addr_bind | The address and port at our side of the connection. |
[in] | addr | The address and port at the peer's side of the connection. |
Definition at line 1139 of file net.cpp.
|
private |
bool CConnman::DisconnectNode | ( | const CNetAddr & | addr | ) |
bool CConnman::DisconnectNode | ( | const CSubNet & | subnet | ) |
bool CConnman::DisconnectNode | ( | const std::string & | node | ) |
|
private |
|
private |
|
private |
|
inline |
|
inline |
|
private |
Generate a collection of sockets to check for IO readiness.
[in] | nodes | Select from these nodes' sockets. |
[out] | recv_set | Sockets to check for read readiness. |
[out] | send_set | Sockets to check for write readiness. |
[out] | error_set | Sockets to check for errors. |
Definition at line 1377 of file net.cpp.
std::vector< AddedNodeInfo > CConnman::GetAddedNodeInfo | ( | ) | const |
std::vector< CAddress > CConnman::GetAddresses | ( | CNode & | requestor, |
size_t | max_addresses, | ||
size_t | max_pct | ||
) |
Cache is used to minimize topology leaks, so it should be used for all non-trusted calls, for example, p2p.
A non-malicious call (from RPC or a peer with addr permission) should call the function without a parameter to avoid using the cache.
Definition at line 2761 of file net.cpp.
std::vector< CAddress > CConnman::GetAddresses | ( | size_t | max_addresses, |
size_t | max_pct, | ||
std::optional< Network > | network | ||
) | const |
Return all or many randomly selected addresses, optionally by network.
[in] | max_addresses | Maximum number of addresses to return (0 = all). |
[in] | max_pct | Maximum percentage of addresses to return (0 = all). |
[in] | network | Select only addresses of this network (nullopt = all). |
Definition at line 2750 of file net.cpp.
|
private |
CSipHasher CConnman::GetDeterministicRandomizer | ( | uint64_t | id | ) | const |
ServiceFlags CConnman::GetLocalServices | ( | ) | const |
Used to convey which local services we are offering peers during node connection.
The data returned by this is used in CNode construction, which is used to advertise which services we are offering that peer during net_processing.cpp:PushNodeVersion()
.
uint64_t CConnman::GetMaxOutboundTarget | ( | ) | const |
std::chrono::seconds CConnman::GetMaxOutboundTimeframe | ( | ) | const |
std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle | ( | ) | const |
|
inline |
|
private |
size_t CConnman::GetNodeCount | ( | ConnectionDirection | flags | ) | const |
void CConnman::GetNodeStats | ( | std::vector< CNodeStats > & | vstats | ) | const |
uint64_t CConnman::GetOutboundTargetBytesLeft | ( | ) | const |
uint64_t CConnman::GetTotalBytesRecv | ( | ) | const |
uint64_t CConnman::GetTotalBytesSent | ( | ) | const |
bool CConnman::GetTryNewOutboundPeer | ( | ) | const |
|
inlineprivate |
|
inlineprivate |
|
inlineprivate |
|
private |
|
private |
|
private |
|
private |
|
private |
flag for waking the message processor.
|
private |
|
inline |
|
private |
void CConnman::Interrupt | ( | ) |
|
staticprivate |
|
private |
void CConnman::OpenNetworkConnection | ( | const CAddress & | addrConnect, |
bool | fCountFailure, | ||
CSemaphoreGrant * | grantOutbound, | ||
const char * | strDest, | ||
ConnectionType | conn_type | ||
) |
bool CConnman::OutboundTargetReached | ( | bool | historicalBlockServingLimit | ) | const |
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
Definition at line 2942 of file net.cpp.
|
private |
void CConnman::PushMessage | ( | CNode * | pnode, |
CSerializedNetMsg && | msg | ||
) |
|
private |
|
private |
bool CConnman::RemoveAddedNode | ( | const std::string & | node | ) |
void CConnman::SetNetworkActive | ( | bool | active | ) |
void CConnman::SetTryNewOutboundPeer | ( | bool | flag | ) |
bool CConnman::ShouldRunInactivityChecks | ( | const CNode & | node, |
std::chrono::seconds | now | ||
) | const |
|
private |
Check which sockets are ready for IO.
[in] | nodes | Select from these nodes' sockets. |
[out] | recv_set | Sockets which are ready for read. |
[out] | send_set | Sockets which are ready for write. |
[out] | error_set | Sockets which have errors. This calls GenerateSelectSet() to gather a list of sockets to check. |
Definition at line 1469 of file net.cpp.
|
private |
|
private |
Do the read/write for connected sockets that are ready for IO.
[in] | nodes | Nodes to process. The socket of each node is checked against recv_set , send_set and error_set . |
[in] | recv_set | Sockets that are ready for read. |
[in] | send_set | Sockets that are ready for send. |
[in] | error_set | Sockets that have an exceptional condition (error). |
Definition at line 1570 of file net.cpp.
|
private |
|
private |
bool CConnman::Start | ( | CScheduler & | scheduler, |
const Options & | options | ||
) |
|
inline |
void CConnman::StopNodes | ( | ) |
void CConnman::StopThreads | ( | ) |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
void CConnman::WakeMessageHandler | ( | ) |
|
friend |
|
mutableprivate |
|
private |
|
private |
|
private |
Addr responses stored in different caches per (network, local socket) prevent cross-network node identification.
If a node for example is multi-homed under Tor and IPv6, a single cache (or no cache at all) would let an attacker to easily detect that it is the same node by comparing responses. Indexing by local socket prevents leakage when a node has multiple listening addresses on the same network.
The used memory equals to 1000 CAddress records (or around 40 bytes) per distinct Network (up to 5) we have/had an inbound peer from, resulting in at most ~196 KB. Every separate local socket may add up to ~196 KB extra.
|
private |
|
private |
|
private |
|
private |
|
private |
|
mutableprivate |
|
private |
|
private |
|
private |
|
private |
|
private |
Services this instance offers.
This data is replicated in each CNode instance we create during peer connection (in ConnectNode()) under a member also called nLocalServices.
This data is not marked const, but after being set it should not change. See the note in CNode::nLocalServices documentation.
|
private |
|
private |
|
private |
|
private |
|
private |