34 #include <condition_variable> 37 #include <arpa/inet.h> 114 std::vector<unsigned char>
data;
201 int nMaxConnections = 0;
202 int m_max_outbound_full_relay = 0;
203 int m_max_outbound_block_relay = 0;
210 unsigned int nSendBufferMaxSize = 0;
211 unsigned int nReceiveFloodSize = 0;
212 uint64_t nMaxOutboundTimeframe = 0;
213 uint64_t nMaxOutboundLimit = 0;
220 bool m_use_addrman_outgoing =
true;
234 m_max_outbound = m_max_outbound_full_relay + m_max_outbound_block_relay + nMaxFeeler;
243 LOCK(cs_totalBytesSent);
249 LOCK(cs_vAddedNodes);
255 CConnman(uint64_t seed0, uint64_t seed1,
bool network_active =
true);
270 void SetNetworkActive(
bool active);
272 bool CheckIncomingNonce(uint64_t nonce);
274 bool ForNode(
NodeId id, std::function<
bool(
CNode* pnode)> func);
278 using NodeFn = std::function<void(CNode*)>;
282 for (
auto&& node : vNodes) {
283 if (NodeFullyConnected(node))
291 for (
auto&& node : vNodes) {
292 if (NodeFullyConnected(node))
297 template<
typename Callable,
typename CallableAfter>
301 for (
auto&& node : vNodes) {
302 if (NodeFullyConnected(node))
308 template<
typename Callable,
typename CallableAfter>
312 for (
auto&& node : vNodes) {
313 if (NodeFullyConnected(node))
321 void MarkAddressGood(
const CAddress& addr);
322 bool AddNewAddresses(
const std::vector<CAddress>& vAddr,
const CAddress& addrFrom, int64_t nTimePenalty = 0);
323 std::vector<CAddress> GetAddresses(
size_t max_addresses,
size_t max_pct);
330 std::vector<CAddress> GetAddresses(
CNode& requestor,
size_t max_addresses,
size_t max_pct);
334 void SetTryNewOutboundPeer(
bool flag);
335 bool GetTryNewOutboundPeer();
343 int GetExtraOutboundCount();
345 bool AddNode(
const std::string& node);
346 bool RemoveAddedNode(
const std::string& node);
347 std::vector<AddedNodeInfo> GetAddedNodeInfo();
350 void GetNodeStats(std::vector<CNodeStats>& vstats);
351 bool DisconnectNode(
const std::string& node);
352 bool DisconnectNode(
const CSubNet& subnet);
353 bool DisconnectNode(
const CNetAddr& addr);
354 bool DisconnectNode(
NodeId id);
365 void SetMaxOutboundTarget(uint64_t limit);
366 uint64_t GetMaxOutboundTarget();
369 void SetMaxOutboundTimeframe(uint64_t timeframe);
370 uint64_t GetMaxOutboundTimeframe();
375 bool OutboundTargetReached(
bool historicalBlockServingLimit);
379 uint64_t GetOutboundTargetBytesLeft();
383 uint64_t GetMaxOutboundTimeLeftInCycle();
385 uint64_t GetTotalBytesRecv();
386 uint64_t GetTotalBytesSent();
388 void SetBestHeight(
int height);
389 int GetBestHeight()
const;
392 CSipHasher GetDeterministicRandomizer(uint64_t
id)
const;
394 unsigned int GetReceiveFloodSize()
const;
396 void WakeMessageHandler();
402 int64_t PoissonNextSendInbound(int64_t
now,
int average_interval_seconds);
404 void SetAsmap(std::vector<bool> asmap) { addrman.m_asmap = std::move(asmap); }
419 const std::vector<CService>& binds,
420 const std::vector<NetWhitebindPermissions>& whiteBinds,
421 const std::vector<CService>& onion_binds);
423 void ThreadOpenAddedConnections();
424 void AddAddrFetch(
const std::string& strDest);
425 void ProcessAddrFetch();
426 void ThreadOpenConnections(std::vector<std::string> connect);
427 void ThreadMessageHandler();
428 void AcceptConnection(
const ListenSocket& hListenSocket);
429 void DisconnectNodes();
431 void InactivityCheck(
CNode *pnode);
432 bool GenerateSelectSet(std::set<SOCKET> &recv_set, std::set<SOCKET> &send_set, std::set<SOCKET> &error_set);
433 void SocketEvents(std::set<SOCKET> &recv_set, std::set<SOCKET> &send_set, std::set<SOCKET> &error_set);
434 void SocketHandler();
435 void ThreadSocketHandler();
436 void ThreadDNSAddressSeed();
438 uint64_t CalculateKeyedNetGroup(
const CAddress& ad)
const;
442 CNode* FindNode(
const std::string& addrName);
449 bool AlreadyConnectedToAddress(
const CAddress& addr);
451 bool AttemptToEvictConnection();
455 void DeleteNode(
CNode* pnode);
459 size_t SocketSendData(
CNode *pnode)
const;
460 void DumpAddresses();
463 void RecordBytesRecv(uint64_t bytes);
464 void RecordBytesSent(uint64_t bytes);
469 std::vector<CAddress> GetCurrentBlockRelayOnlyConns()
const;
472 static bool NodeFullyConnected(
const CNode* pnode);
481 uint64_t nMaxOutboundTotalBytesSentInCycle
GUARDED_BY(cs_totalBytesSent);
482 uint64_t nMaxOutboundCycleStartTime
GUARDED_BY(cs_totalBytesSent);
483 uint64_t nMaxOutboundLimit
GUARDED_BY(cs_totalBytesSent);
484 uint64_t nMaxOutboundTimeframe
GUARDED_BY(cs_totalBytesSent);
493 unsigned int nSendBufferMaxSize{0};
494 unsigned int nReceiveFloodSize{0};
497 std::atomic<bool> fNetworkActive{
true};
498 bool fAddressesInitialized{
false};
500 std::deque<std::string> m_addr_fetches
GUARDED_BY(m_addr_fetches_mutex);
502 std::vector<std::string> vAddedNodes
GUARDED_BY(cs_vAddedNodes);
504 std::vector<CNode*> vNodes
GUARDED_BY(cs_vNodes);
507 std::atomic<NodeId> nLastNodeId{0};
508 unsigned int nPrevNodeCount{0};
518 std::chrono::microseconds m_cache_entry_expiration{0};
586 std::atomic<bool> flagInterruptMsgProc{
false};
601 std::atomic<int64_t> m_next_send_inv_to_incoming{0};
609 friend struct CConnmanTest;
610 friend struct ConnmanTestMsg;
624 virtual bool ProcessMessages(
CNode* pnode, std::atomic<bool>& interrupt) = 0;
625 virtual bool SendMessages(
CNode* pnode) = 0;
626 virtual void InitializeNode(
CNode* pnode) = 0;
627 virtual void FinalizeNode(
const CNode& node,
bool& update_connection_time) = 0;
683 extern std::map<CNetAddr, LocalServiceInfo> mapLocalHost
GUARDED_BY(cs_mapLocalHost);
737 std::chrono::microseconds m_time{0};
738 uint32_t m_message_size{0};
739 uint32_t m_raw_message_size{0};
757 virtual bool Complete()
const = 0;
759 virtual void SetVersion(
int version) = 0;
761 virtual int Read(
const char *data,
unsigned int bytes) = 0;
781 const uint256& GetMessageHash()
const;
782 int readHeader(
const char *pch,
unsigned int nBytes);
783 int readData(
const char *pch,
unsigned int nBytes);
798 : m_chain_params(chain_params),
800 hdrbuf(nTypeIn, nVersionIn),
801 vRecv(nTypeIn, nVersionIn)
817 int Read(
const char *pch,
unsigned int nBytes)
override {
818 int ret = in_data ? readData(pch, nBytes) : readHeader(pch, nBytes);
819 if (ret < 0) Reset();
822 Optional<CNetMessage> GetMessage(std::chrono::microseconds time, uint32_t& out_err_raw_size)
override;
830 virtual void prepareForTransport(
CSerializedNetMsg& msg, std::vector<unsigned char>& header) = 0;
836 void prepareForTransport(
CSerializedNetMsg& msg, std::vector<unsigned char>& header)
override;
843 friend struct ConnmanTestMsg;
853 size_t nSendOffset{0};
855 std::deque<std::vector<unsigned char>> vSendMsg
GUARDED_BY(cs_vSend);
861 std::list<CNetMessage> vProcessMsg
GUARDED_BY(cs_vProcessMsg);
862 size_t nProcessQueueSize{0};
868 std::atomic<int64_t> nLastSend{0};
869 std::atomic<int64_t> nLastRecv{0};
871 std::atomic<int64_t> nTimeOffset{0};
876 std::atomic<int> nVersion{0};
883 bool m_prefer_evict{
false};
888 bool m_legacyWhitelisted{
false};
890 bool m_limited_node{
false};
895 std::atomic_bool m_wants_addrv2{
false};
896 std::atomic_bool fSuccessfullyConnected{
false};
899 std::atomic_bool fDisconnect{
false};
900 bool fSentAddr{
false};
902 std::atomic<int> nRefCount{0};
905 std::atomic_bool fPauseRecv{
false};
906 std::atomic_bool fPauseSend{
false};
909 switch (m_conn_type) {
954 switch (m_conn_type) {
978 Network ConnectedThroughNetwork()
const;
982 mapMsgCmdSize mapRecvBytesPerMsgCmd
GUARDED_BY(cs_vRecv);
986 std::atomic<int> nStartingHeight{-1};
990 std::unique_ptr<CRollingBloomFilter> m_addr_known{
nullptr};
991 bool fGetAddr{
false};
992 std::chrono::microseconds m_next_addr_send
GUARDED_BY(cs_sendProcessing){0};
993 std::chrono::microseconds m_next_local_addr_send
GUARDED_BY(cs_sendProcessing){0};
998 std::vector<uint256> vInventoryBlockToSend
GUARDED_BY(cs_inventory);
1008 std::unique_ptr<CBloomFilter> pfilter
PT_GUARDED_BY(cs_filter) GUARDED_BY(cs_filter){
nullptr};
1014 std::set<uint256> setInventoryTxToSend;
1018 std::atomic<std::chrono::seconds> m_last_mempool_req{std::chrono::seconds{0}};
1019 std::chrono::microseconds nNextInvSend{0};
1025 int64_t nextSendTimeFeeFilter{0};
1032 std::vector<uint256> vBlockHashesToAnnounce
GUARDED_BY(cs_inventory);
1039 std::atomic<int64_t> nLastBlockTime{0};
1045 std::atomic<int64_t> nLastTXTime{0};
1049 std::atomic<uint64_t> nPingNonceSent{0};
1051 std::atomic<std::chrono::microseconds> m_ping_start{std::chrono::microseconds{0}};
1053 std::atomic<int64_t> nPingUsecTime{0};
1055 std::atomic<int64_t> nMinPingUsecTime{std::numeric_limits<int64_t>::max()};
1057 std::atomic<bool> fPingQueued{
false};
1059 CNode(
NodeId id,
ServiceFlags nLocalServicesIn,
int nMyStartingHeightIn,
SOCKET hSocketIn,
const CAddress &addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn,
const CAddress &addrBindIn,
const std::string &addrNameIn,
ConnectionType conn_type_in,
bool inbound_onion =
false);
1092 std::string addrName
GUARDED_BY(cs_addrName);
1099 const bool m_inbound_onion{
false};
1108 return nLocalHostNonce;
1112 return nMyStartingHeight;
1117 assert(nRefCount >= 0);
1121 bool ReceiveMsgBytes(
const char *pch,
unsigned int nBytes,
bool& complete);
1125 m_greatest_common_version = greatest_common_version;
1129 return m_greatest_common_version;
1134 void SetAddrLocal(
const CService& addrLocalIn);
1151 assert(m_addr_known);
1152 m_addr_known->insert(_addr.
GetKey());
1165 assert(m_addr_known);
1166 if (_addr.
IsValid() && !m_addr_known->contains(_addr.
GetKey()) && addr_format_supported) {
1168 vAddrToSend[insecure_rand.
randrange(vAddrToSend.size())] = _addr;
1170 vAddrToSend.push_back(_addr);
1178 if (m_tx_relay !=
nullptr) {
1179 LOCK(m_tx_relay->cs_tx_inventory);
1180 m_tx_relay->filterInventoryKnown.insert(hash);
1186 if (m_tx_relay ==
nullptr)
return;
1187 LOCK(m_tx_relay->cs_tx_inventory);
1188 if (!m_tx_relay->filterInventoryKnown.contains(hash)) {
1189 m_tx_relay->setInventoryTxToSend.insert(hash);
1193 void CloseSocketDisconnect();
1195 void copyStats(
CNodeStats &stats,
const std::vector<bool> &m_asmap);
1199 return nLocalServices;
1202 std::string GetAddrName()
const;
1204 void MaybeSetAddrName(
const std::string& addrNameIn);
1206 std::string ConnectionTypeAsString()
const;
1213 inline std::chrono::microseconds
PoissonNextSend(std::chrono::microseconds now, std::chrono::seconds average_interval)
1215 return std::chrono::microseconds{
PoissonNextSend(now.count(), average_interval.count())};
1218 #endif // BITCOIN_NET_H std::vector< CService > vBinds
int m_max_outbound_full_relay
std::vector< CAddress > m_addrs_response_cache
static const int MAX_BLOCK_RELAY_ONLY_CONNECTIONS
Maximum number of block-relay-only outgoing connections.
uint64_t nTotalBytesSent GUARDED_BY(cs_totalBytesSent)
bool HasPermission(NetPermissionFlags permission) const
std::vector< bool > m_asmap
bool IsReachable(enum Network net)
static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH
Maximum length of incoming protocol messages (no message over 4 MB is currently acceptable).
int GetCommonVersion() const
mapMsgCmdSize mapRecvBytesPerMsgCmd
const std::chrono::seconds now
uint64_t GetLocalNonce() const
static void AddFlag(NetPermissionFlags &flags, NetPermissionFlags f)
ServiceFlags
nServices flags
static const uint64_t MAX_UPLOAD_TIMEFRAME
The default timeframe for -maxuploadtarget.
std::unique_ptr< TransportSerializer > m_serializer
std::string cleanSubVer GUARDED_BY(cs_SubVer)
cleanSubVer is a sanitized string of the user agent byte array we read from the wire.
bool Complete() const override
int m_max_outbound_block_relay
We open manual connections to addresses that users explicitly inputted via the addnode RPC...
std::atomic< int > nBestHeight
bool fRelayTxes GUARDED_BY(cs_filter)
const std::string NET_MESSAGE_COMMAND_OTHER
std::map< CNetAddr, LocalServiceInfo > mapLocalHost GUARDED_BY(cs_mapLocalHost)
void SetVersion(int nVersionIn) override
void PushTxInventory(const uint256 &hash)
static const bool DEFAULT_FORCEDNSSEED
const uint64_t nKeyedNetGroup
bool SeenLocal(const CService &addr)
vote for a local address
mapMsgCmdSize mapSendBytesPerMsgCmd
std::vector< unsigned char > data
const ConnectionType m_conn_type
std::vector< NetWhitelistPermissions > vWhitelistedRange
CAmount minFeeFilter GUARDED_BY(cs_feeFilter)
unsigned int nReceiveFloodSize
int m_max_outbound_full_relay
mapMsgCmdSize mapSendBytesPerMsgCmd
CClientUIInterface * uiInterface
void resize(size_type n, value_type c=0)
std::vector< CService > m_onion_binds
A vector of -bind=<address>:<port>=onion arguments each of which is an address and port that are desi...
void SetAsmap(std::vector< bool > asmap)
void ForEachNodeThen(Callable &&pre, CallableAfter &&post) const
CDataStream m_recv
received message data
static const bool DEFAULT_LISTEN
-listen default
CNetMessage(CDataStream &&recv_in)
RAII-style semaphore lock.
void SetCommonVersion(int greatest_common_version)
static const int MAX_ADDNODE_CONNECTIONS
Maximum number of addnode outgoing nodes.
RecursiveMutex cs_vProcessMsg
Interface for message handling.
void SetVersion(int nVersionIn)
RecursiveMutex cs_addrName
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set. ...
NetEventsInterface * m_msgproc
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
void AdvertiseLocal(CNode *pnode)
int m_max_outbound_block_relay
Double ended buffer combining vector and stream-like interfaces.
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
std::unique_ptr< TransportDeserializer > m_deserializer
std::vector< CAddress > vAddrToSend
bool IsBlockOnlyConn() const
bool IsFeelerConn() const
void PushAddress(const CAddress &_addr, FastRandomContext &insecure_rand)
static const int FEELER_INTERVAL
Run the feeler connection loop once every 2 minutes or 120 seconds.
uint64_t nSendBytes GUARDED_BY(cs_vSend)
static const int TIMEOUT_INTERVAL
Time after which to disconnect, after waiting for a ping response (or inactivity).
Signals for UI communication.
std::list< CNetMessage > vRecvMsg
std::map< uint64_t, CachedAddrResponse > m_addr_response_caches
Addr responses stored in different caches per (network, local socket) prevent cross-network node iden...
static const int MAX_FEELER_CONNECTIONS
Maximum number of feeler connections.
virtual ~TransportDeserializer()
RecursiveMutex cs_sendProcessing
ServiceFlags nLocalServices
Services this instance offers.
bool RelayAddrsWithConn() const
Stochastical (IP) address manager.
int64_t CAmount
Amount in satoshis (Can be negative)
static const size_t DEFAULT_MAXRECEIVEBUFFER
bool IsAddrFetchConn() const
std::chrono::microseconds m_next_local_addr_send GUARDED_BY(cs_sendProcessing)
bool GetUseAddrmanOutgoing() const
bool IsInboundConn() const
bool GetLocal(CService &addr, const CNetAddr *paddrPeer=nullptr)
std::condition_variable condMsgProc
bool IsFullOutboundConn() const
RecursiveMutex cs_vAddedNodes
void Interrupt(NodeContext &node)
Interrupt threads.
ListenSocket(SOCKET socket_, NetPermissionFlags permissions_)
std::thread threadOpenAddedConnections
RecursiveMutex m_addr_fetches_mutex
bool IsPeerAddrLocalGood(CNode *pnode)
std::function< void(CNode *)> NodeFn
NetEventsInterface * m_msgproc
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
A combination of a network address (CNetAddr) and a (TCP) port.
Transport protocol agnostic message container.
std::vector< std::string > vSeedNodes
std::vector< std::string > m_specified_outgoing
int64_t m_peer_connect_timeout
std::unique_ptr< CSemaphore > semOutbound
std::thread threadMessageHandler
static bool HasFlag(const NetPermissionFlags &flags, NetPermissionFlags f)
void ForEachNodeThen(Callable &&pre, CallableAfter &&post)
A CService with information about it as peer.
int Read(const char *pch, unsigned int nBytes) override
RecursiveMutex cs_mapLocalHost
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 t...
static const unsigned int DEFAULT_MAX_PEER_CONNECTIONS
The maximum number of peer connections to maintain.
CClientUIInterface * clientInterface
int GetMyStartingHeight() const
The TransportSerializer prepares messages for the network transport.
ServiceFlags GetLocalServices() const
uint64_t nRecvBytes GUARDED_BY(cs_vRecv)
bool GetNetworkActive() const
Inbound connections are those initiated by a peer.
NetPermissionFlags m_permissions
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
const int64_t nTimeConnected
virtual ~TransportSerializer()
uint64_t nMaxOutboundTimeframe
RecursiveMutex cs_feeFilter
std::list< CNode * > vNodesDisconnected
void RemoveLocal(const CService &addr)
std::vector< NetWhitelistPermissions > vWhitelistedRange
std::vector< CService > onion_binds
bool ExpectServicesFromConn() const
static const bool DEFAULT_WHITELISTFORCERELAY
Default for -whitelistforcerelay.
void AddKnownTx(const uint256 &hash)
BanMan * m_banman
Pointer to this node's banman.
static const size_t DEFAULT_MAXSENDBUFFER
bool fSendMempool GUARDED_BY(cs_tx_inventory)
std::thread threadOpenConnections
static const uint64_t DEFAULT_MAX_UPLOAD_TARGET
The default for -maxuploadtarget.
ConnectionType
Different types of connections to a peer.
bool IsOutboundOrBlockRelayConn() const
CSemaphoreGrant grantOutbound
The TransportDeserializer takes care of holding and deserializing the network receive buffer...
const CChainParams & m_chain_params
std::map< std::string, uint64_t > mapMsgCmdSize
Feeler connections are short-lived connections made to check that a node is alive.
static constexpr size_t MAX_ADDR_TO_SEND
The maximum number of addresses from our addrman to return in response to a getaddr message...
std::thread threadDNSAddressSeed
ServiceFlags nLocalServices
uint64_t nMaxOutboundLimit
std::vector< std::string > m_added_nodes
int64_t PoissonNextSend(int64_t now, int average_interval_seconds)
Return a timestamp in the future (in microseconds) for exponentially distributed events.
std::vector< ListenSocket > vhListenSocket
std::string m_conn_type_string
These are the default connections that we use to connect with the network.
static const unsigned int MAX_SUBVERSION_LENGTH
Maximum length of the user agent string in version message.
uint64_t nTotalBytesRecv GUARDED_BY(cs_totalBytesRecv)
static const bool DEFAULT_UPNP
-upnp default
static const int64_t DEFAULT_PEER_CONNECT_TIMEOUT
-peertimeout default
static const bool DEFAULT_BLOCKSONLY
Default for blocks only.
const uint64_t nLocalHostNonce
unsigned int nSendBufferMaxSize
void ForEachNode(const NodeFn &func) const
const ServiceFlags nLocalServices
Services offered to this peer.
static const bool DEFAULT_WHITELISTRELAY
Default for -whitelistrelay.
static const int MAX_OUTBOUND_FULL_RELAY_CONNECTIONS
Maximum number of automatic outgoing nodes over which we'll relay everything (blocks, tx, addrs, etc)
std::string strSubVersion
Subversion as sent to the P2P network in version messages.
CClientUIInterface uiInterface
boost::optional< T > Optional
Substitute for C++17 std::optional.
Information about a peer.
void ForEachNode(const NodeFn &func)
std::thread threadSocketHandler
Simple class for background tasks that should be run periodically or once "after a while"...
We use block-relay-only connections to help prevent against partition attacks.
RecursiveMutex cs_totalBytesRecv
void SetReachable(enum Network net, bool reachable)
Mark a network as reachable or unreachable (no automatic connects to it)
NetPermissionFlags m_permissionFlags
V1TransportDeserializer(const CChainParams &chain_params, const NodeId node_id, int nTypeIn, int nVersionIn)
RecursiveMutex cs_addrLocal
bool IsManualConn() const
std::vector< CAddress > m_anchors
Addresses that were saved during the previous clean shutdown.
AddrFetch connections are short lived connections used to solicit addresses from peers.
void AddAddressKnown(const CAddress &_addr)
std::unique_ptr< CSemaphore > semAddnode
void Init(const Options &connOptions)
CThreadInterrupt interruptNet
static void NotifyNumConnectionsChanged(ClientModel *clientmodel, int newNumConnections)
CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
bool AddLocal(const CService &addr, int nScore=LOCAL_NONE)
bool m_use_addrman_outgoing
std::vector< NetWhitebindPermissions > vWhiteBinds
bool IsLocal(const CService &addr)
check whether a given address is potentially local
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
void AddSocketPermissionFlags(NetPermissionFlags &flags) const
RecursiveMutex cs_hSocket
const int nMyStartingHeight
bool m_use_addrman_outgoing
Cache responses to addr requests to minimize privacy leak.
RecursiveMutex cs_totalBytesSent
std::chrono::microseconds m_next_addr_send GUARDED_BY(cs_sendProcessing)
std::vector< unsigned char > GetKey() const
std::unique_ptr< TxRelay > m_tx_relay
int64_t m_peer_connect_timeout
std::unique_ptr< CBloomFilter > pfilter PT_GUARDED_BY(cs_filter) GUARDED_BY(cs_filter)