Bitcoin Core  21.99.0
P2P Digital Currency
Classes | Typedefs | Enumerations | Functions | Variables
net.h File Reference
#include <addrdb.h>
#include <addrman.h>
#include <amount.h>
#include <bloom.h>
#include <chainparams.h>
#include <compat.h>
#include <crypto/siphash.h>
#include <hash.h>
#include <i2p.h>
#include <net_permissions.h>
#include <netaddress.h>
#include <netbase.h>
#include <policy/feerate.h>
#include <protocol.h>
#include <random.h>
#include <span.h>
#include <streams.h>
#include <sync.h>
#include <threadinterrupt.h>
#include <uint256.h>
#include <util/check.h>
#include <atomic>
#include <condition_variable>
#include <cstdint>
#include <deque>
#include <map>
#include <memory>
#include <optional>
#include <thread>
#include <vector>
Include dependency graph for net.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  AddedNodeInfo
 
struct  CSerializedNetMsg
 
struct  LocalServiceInfo
 
class  CNodeStats
 
class  CNetMessage
 Transport protocol agnostic message container. More...
 
class  TransportDeserializer
 The TransportDeserializer takes care of holding and deserializing the network receive buffer. More...
 
class  V1TransportDeserializer
 
class  TransportSerializer
 The TransportSerializer prepares messages for the network transport. More...
 
class  V1TransportSerializer
 
class  CNode
 Information about a peer. More...
 
struct  CNode::TxRelay
 
class  NetEventsInterface
 Interface for message handling. More...
 
class  CConnman
 
struct  CConnman::Options
 
struct  CConnman::ListenSocket
 
struct  CConnman::CachedAddrResponse
 Cache responses to addr requests to minimize privacy leak. More...
 
struct  NodeEvictionCandidate
 

Typedefs

typedef int64_t NodeId
 
typedef std::map< std::string, uint64_t > mapMsgCmdSize
 

Enumerations

enum  ConnectionType {
  ConnectionType::INBOUND, ConnectionType::OUTBOUND_FULL_RELAY, ConnectionType::MANUAL, ConnectionType::FEELER,
  ConnectionType::BLOCK_RELAY, ConnectionType::ADDR_FETCH
}
 Different types of connections to a peer. More...
 
enum  {
  LOCAL_NONE, LOCAL_IF, LOCAL_BIND, LOCAL_MAPPED,
  LOCAL_MANUAL, LOCAL_MAX
}
 

Functions

std::string ConnectionTypeAsString (ConnectionType conn_type)
 Convert ConnectionType enum to a string value. More...
 
void Discover ()
 
uint16_t GetListenPort ()
 
bool IsPeerAddrLocalGood (CNode *pnode)
 
std::optional< CAddressGetLocalAddrForPeer (CNode *pnode)
 Returns a local address that we should advertise to this peer. More...
 
void SetReachable (enum Network net, bool reachable)
 Mark a network as reachable or unreachable (no automatic connects to it) More...
 
bool IsReachable (enum Network net)
 
bool IsReachable (const CNetAddr &addr)
 
bool AddLocal (const CService &addr, int nScore=LOCAL_NONE)
 
bool AddLocal (const CNetAddr &addr, int nScore=LOCAL_NONE)
 
void RemoveLocal (const CService &addr)
 
bool SeenLocal (const CService &addr)
 vote for a local address More...
 
bool IsLocal (const CService &addr)
 check whether a given address is potentially local More...
 
bool GetLocal (CService &addr, const CNetAddr *paddrPeer=nullptr)
 
CAddress GetLocalAddress (const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
 
std::map< CNetAddr, LocalServiceInfo > mapLocalHost GUARDED_BY (cs_mapLocalHost)
 
std::chrono::microseconds PoissonNextSend (std::chrono::microseconds now, std::chrono::seconds average_interval)
 Return a timestamp in the future (in microseconds) for exponentially distributed events. More...
 
void CaptureMessage (const CAddress &addr, const std::string &msg_type, const Span< const unsigned char > &data, bool is_incoming)
 Dump binary message to file, with timestamp. More...
 
std::optional< NodeIdSelectNodeToEvict (std::vector< NodeEvictionCandidate > &&vEvictionCandidates)
 Select an inbound peer to evict after filtering out (protecting) peers having distinct, difficult-to-forge characteristics. More...
 
void ProtectEvictionCandidatesByRatio (std::vector< NodeEvictionCandidate > &vEvictionCandidates)
 Protect desirable or disadvantaged inbound peers from eviction by ratio. More...
 

Variables

static const bool DEFAULT_WHITELISTRELAY = true
 Default for -whitelistrelay. More...
 
static const bool DEFAULT_WHITELISTFORCERELAY = false
 Default for -whitelistforcerelay. More...
 
static const int TIMEOUT_INTERVAL = 20 * 60
 Time after which to disconnect, after waiting for a ping response (or inactivity). More...
 
static constexpr auto FEELER_INTERVAL = 2min
 Run the feeler connection loop once every 2 minutes. More...
 
static constexpr auto EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL = 5min
 Run the extra block-relay-only connection loop once every 5 minutes. More...
 
static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH = 4 * 1000 * 1000
 Maximum length of incoming protocol messages (no message over 4 MB is currently acceptable). More...
 
static const unsigned int MAX_SUBVERSION_LENGTH = 256
 Maximum length of the user agent string in version message. More...
 
static const int MAX_OUTBOUND_FULL_RELAY_CONNECTIONS = 8
 Maximum number of automatic outgoing nodes over which we'll relay everything (blocks, tx, addrs, etc) More...
 
static const int MAX_ADDNODE_CONNECTIONS = 8
 Maximum number of addnode outgoing nodes. More...
 
static const int MAX_BLOCK_RELAY_ONLY_CONNECTIONS = 2
 Maximum number of block-relay-only outgoing connections. More...
 
static const int MAX_FEELER_CONNECTIONS = 1
 Maximum number of feeler connections. More...
 
static const bool DEFAULT_LISTEN = true
 -listen default More...
 
static const unsigned int DEFAULT_MAX_PEER_CONNECTIONS = 125
 The maximum number of peer connections to maintain. More...
 
static constexpr uint64_t DEFAULT_MAX_UPLOAD_TARGET = 0
 The default for -maxuploadtarget. More...
 
static const bool DEFAULT_BLOCKSONLY = false
 Default for blocks only. More...
 
static const int64_t DEFAULT_PEER_CONNECT_TIMEOUT = 60
 -peertimeout default More...
 
static const int NUM_FDS_MESSAGE_CAPTURE = 1
 Number of file descriptors required for message capture. More...
 
static const bool DEFAULT_FORCEDNSSEED = false
 
static const bool DEFAULT_DNSSEED = true
 
static const bool DEFAULT_FIXEDSEEDS = true
 
static const size_t DEFAULT_MAXRECEIVEBUFFER = 5 * 1000
 
static const size_t DEFAULT_MAXSENDBUFFER = 1 * 1000
 
bool fDiscover
 
bool fListen
 
std::string strSubVersion
 Subversion as sent to the P2P network in version messages. More...
 
RecursiveMutex cs_mapLocalHost
 
const std::string NET_MESSAGE_COMMAND_OTHER
 

Typedef Documentation

◆ mapMsgCmdSize

typedef std::map<std::string, uint64_t> mapMsgCmdSize

Definition at line 237 of file net.h.

◆ NodeId

typedef int64_t NodeId

Definition at line 88 of file net.h.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
LOCAL_NONE 
LOCAL_IF 
LOCAL_BIND 
LOCAL_MAPPED 
LOCAL_MANUAL 
LOCAL_MAX 

Definition at line 188 of file net.h.

◆ ConnectionType

enum ConnectionType
strong

Different types of connections to a peer.

This enum encapsulates the information we have available at the time of opening or accepting the connection. Aside from INBOUND, all types are initiated by us.

If adding or removing types, please update CONNECTION_TYPE_DOC in src/rpc/net.cpp and src/qt/rpcconsole.cpp, as well as the descriptions in src/qt/guiutil.cpp and src/bitcoin-cli.cppNetinfoRequestHandler.

Enumerator
INBOUND 

Inbound connections are those initiated by a peer.

This is the only property we know at the time of connection, until P2P messages are exchanged.

OUTBOUND_FULL_RELAY 

These are the default connections that we use to connect with the network.

There is no restriction on what is relayed; by default we relay blocks, addresses & transactions. We automatically attempt to open MAX_OUTBOUND_FULL_RELAY_CONNECTIONS using addresses from our AddrMan.

MANUAL 

We open manual connections to addresses that users explicitly requested via the addnode RPC or the -addnode/-connect configuration options.

Even if a manual connection is misbehaving, we do not automatically disconnect or add it to our discouragement filter.

FEELER 

Feeler connections are short-lived connections made to check that a node is alive.

They can be useful for:

  • test-before-evict: if one of the peers is considered for eviction from our AddrMan because another peer is mapped to the same slot in the tried table, evict only if this longer-known peer is offline.
  • move node addresses from New to Tried table, so that we have more connectable addresses in our AddrMan. Note that in the literature ("Eclipse Attacks on Bitcoin’s Peer-to-Peer Network") only the latter feature is referred to as "feeler connections", although in our codebase feeler connections encompass test-before-evict as well. We make these connections approximately every FEELER_INTERVAL: first we resolve previously found collisions if they exist (test-before-evict), otherwise we connect to a node from the new table.
BLOCK_RELAY 

We use block-relay-only connections to help prevent against partition attacks.

By not relaying transactions or addresses, these connections are harder to detect by a third party, thus helping obfuscate the network topology. We automatically attempt to open MAX_BLOCK_RELAY_ONLY_ANCHORS using addresses from our anchors.dat. Then addresses from our AddrMan if MAX_BLOCK_RELAY_ONLY_CONNECTIONS isn't reached yet.

ADDR_FETCH 

AddrFetch connections are short lived connections used to solicit addresses from peers.

These are initiated to addresses submitted via the -seednode command line argument, or under certain conditions when the AddrMan is empty.

Definition at line 121 of file net.h.

Function Documentation

◆ AddLocal() [1/2]

bool AddLocal ( const CNetAddr addr,
int  nScore = LOCAL_NONE 
)

Definition at line 255 of file net.cpp.

Here is the call graph for this function:

◆ AddLocal() [2/2]

bool AddLocal ( const CService addr,
int  nScore = LOCAL_NONE 
)

Definition at line 229 of file net.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CaptureMessage()

void CaptureMessage ( const CAddress addr,
const std::string &  msg_type,
const Span< const unsigned char > &  data,
bool  is_incoming 
)

Dump binary message to file, with timestamp.

Definition at line 3030 of file net.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ConnectionTypeAsString()

std::string ConnectionTypeAsString ( ConnectionType  conn_type)

Convert ConnectionType enum to a string value.

Definition at line 514 of file net.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Discover()

void Discover ( )

Definition at line 2323 of file net.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetListenPort()

uint16_t GetListenPort ( )

Definition at line 120 of file net.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetLocal()

bool GetLocal ( CService addr,
const CNetAddr paddrPeer = nullptr 
)

Definition at line 126 of file net.cpp.

Here is the caller graph for this function:

◆ GetLocalAddress()

CAddress GetLocalAddress ( const CNetAddr paddrPeer,
ServiceFlags  nLocalServices 
)

Definition at line 176 of file net.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetLocalAddrForPeer()

std::optional<CAddress> GetLocalAddrForPeer ( CNode pnode)

Returns a local address that we should advertise to this peer.

Definition at line 203 of file net.cpp.

Here is the call graph for this function:

◆ GUARDED_BY()

std::map<CNetAddr, LocalServiceInfo> mapLocalHost GUARDED_BY ( cs_mapLocalHost  )

◆ IsLocal()

bool IsLocal ( const CService addr)

check whether a given address is potentially local

Definition at line 300 of file net.cpp.

Here is the caller graph for this function:

◆ IsPeerAddrLocalGood()

bool IsPeerAddrLocalGood ( CNode pnode)

Definition at line 196 of file net.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsReachable() [1/2]

bool IsReachable ( const CNetAddr addr)
Returns
true if the address is in a reachable network, false otherwise

Definition at line 281 of file net.cpp.

Here is the call graph for this function:

◆ IsReachable() [2/2]

bool IsReachable ( enum Network  net)
Returns
true if the network is reachable, false otherwise

Definition at line 275 of file net.cpp.

Here is the caller graph for this function:

◆ PoissonNextSend()

std::chrono::microseconds PoissonNextSend ( std::chrono::microseconds  now,
std::chrono::seconds  average_interval 
)

Return a timestamp in the future (in microseconds) for exponentially distributed events.

Definition at line 3012 of file net.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ProtectEvictionCandidatesByRatio()

void ProtectEvictionCandidatesByRatio ( std::vector< NodeEvictionCandidate > &  vEvictionCandidates)

Protect desirable or disadvantaged inbound peers from eviction by ratio.

This function protects half of the peers which have been connected the longest, to replicate the non-eviction implicit behavior and preclude attacks that start later.

Half of these protected spots (1/4 of the total) are reserved for onion peers connected via our tor control service, if any, sorted by longest uptime, even if they're not longest uptime overall. Any remaining slots of the 1/4 are then allocated to protect localhost peers, if any (or up to 2 localhost peers if no slots remain and 2 or more onion peers were protected), sorted by longest uptime, as manually configured hidden services not using -bind=addr[:port]=onion will not be detected as inbound onion connections.

This helps protect onion peers, which tend to be otherwise disadvantaged under our eviction criteria for their higher min ping times relative to IPv4 and IPv6 peers, and favorise the diversity of peer connections.

This function was extracted from SelectNodeToEvict() to be able to test the ratio-based protection logic deterministically.

Definition at line 897 of file net.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RemoveLocal()

void RemoveLocal ( const CService addr)

Definition at line 260 of file net.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SeenLocal()

bool SeenLocal ( const CService addr)

vote for a local address

Definition at line 287 of file net.cpp.

◆ SelectNodeToEvict()

std::optional<NodeId> SelectNodeToEvict ( std::vector< NodeEvictionCandidate > &&  vEvictionCandidates)

Select an inbound peer to evict after filtering out (protecting) peers having distinct, difficult-to-forge characteristics.

The protection logic picks out fixed numbers of desirable peers per various criteria, followed by (mostly) ratios of desirable or disadvantaged peers. If any eviction candidates remain, the selection logic chooses a peer to evict.

Definition at line 933 of file net.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetReachable()

void SetReachable ( enum Network  net,
bool  reachable 
)

Mark a network as reachable or unreachable (no automatic connects to it)

Note
Networks are reachable by default

Definition at line 267 of file net.cpp.

Here is the caller graph for this function:

Variable Documentation

◆ cs_mapLocalHost

RecursiveMutex cs_mapLocalHost

Definition at line 109 of file net.cpp.

◆ DEFAULT_BLOCKSONLY

const bool DEFAULT_BLOCKSONLY = false
static

Default for blocks only.

Definition at line 76 of file net.h.

◆ DEFAULT_DNSSEED

const bool DEFAULT_DNSSEED = true
static

Definition at line 83 of file net.h.

◆ DEFAULT_FIXEDSEEDS

const bool DEFAULT_FIXEDSEEDS = true
static

Definition at line 84 of file net.h.

◆ DEFAULT_FORCEDNSSEED

const bool DEFAULT_FORCEDNSSEED = false
static

Definition at line 82 of file net.h.

◆ DEFAULT_LISTEN

const bool DEFAULT_LISTEN = true
static

-listen default

Definition at line 70 of file net.h.

◆ DEFAULT_MAX_PEER_CONNECTIONS

const unsigned int DEFAULT_MAX_PEER_CONNECTIONS = 125
static

The maximum number of peer connections to maintain.

Definition at line 72 of file net.h.

◆ DEFAULT_MAX_UPLOAD_TARGET

constexpr uint64_t DEFAULT_MAX_UPLOAD_TARGET = 0
staticconstexpr

The default for -maxuploadtarget.

0 = Unlimited

Definition at line 74 of file net.h.

◆ DEFAULT_MAXRECEIVEBUFFER

const size_t DEFAULT_MAXRECEIVEBUFFER = 5 * 1000
static

Definition at line 85 of file net.h.

◆ DEFAULT_MAXSENDBUFFER

const size_t DEFAULT_MAXSENDBUFFER = 1 * 1000
static

Definition at line 86 of file net.h.

◆ DEFAULT_PEER_CONNECT_TIMEOUT

const int64_t DEFAULT_PEER_CONNECT_TIMEOUT = 60
static

-peertimeout default

Definition at line 78 of file net.h.

◆ DEFAULT_WHITELISTFORCERELAY

const bool DEFAULT_WHITELISTFORCERELAY = false
static

Default for -whitelistforcerelay.

Definition at line 49 of file net.h.

◆ DEFAULT_WHITELISTRELAY

const bool DEFAULT_WHITELISTRELAY = true
static

Default for -whitelistrelay.

Definition at line 47 of file net.h.

◆ EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL

constexpr auto EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL = 5min
staticconstexpr

Run the extra block-relay-only connection loop once every 5 minutes.

Definition at line 56 of file net.h.

◆ fDiscover

bool fDiscover

Definition at line 107 of file net.cpp.

◆ FEELER_INTERVAL

constexpr auto FEELER_INTERVAL = 2min
staticconstexpr

Run the feeler connection loop once every 2 minutes.

Definition at line 54 of file net.h.

◆ fListen

bool fListen

Definition at line 108 of file net.cpp.

◆ MAX_ADDNODE_CONNECTIONS

const int MAX_ADDNODE_CONNECTIONS = 8
static

Maximum number of addnode outgoing nodes.

Definition at line 64 of file net.h.

◆ MAX_BLOCK_RELAY_ONLY_CONNECTIONS

const int MAX_BLOCK_RELAY_ONLY_CONNECTIONS = 2
static

Maximum number of block-relay-only outgoing connections.

Definition at line 66 of file net.h.

◆ MAX_FEELER_CONNECTIONS

const int MAX_FEELER_CONNECTIONS = 1
static

Maximum number of feeler connections.

Definition at line 68 of file net.h.

◆ MAX_OUTBOUND_FULL_RELAY_CONNECTIONS

const int MAX_OUTBOUND_FULL_RELAY_CONNECTIONS = 8
static

Maximum number of automatic outgoing nodes over which we'll relay everything (blocks, tx, addrs, etc)

Definition at line 62 of file net.h.

◆ MAX_PROTOCOL_MESSAGE_LENGTH

const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH = 4 * 1000 * 1000
static

Maximum length of incoming protocol messages (no message over 4 MB is currently acceptable).

Definition at line 58 of file net.h.

◆ MAX_SUBVERSION_LENGTH

const unsigned int MAX_SUBVERSION_LENGTH = 256
static

Maximum length of the user agent string in version message.

Definition at line 60 of file net.h.

◆ NET_MESSAGE_COMMAND_OTHER

const std::string NET_MESSAGE_COMMAND_OTHER

Definition at line 99 of file net.cpp.

◆ NUM_FDS_MESSAGE_CAPTURE

const int NUM_FDS_MESSAGE_CAPTURE = 1
static

Number of file descriptors required for message capture.

Definition at line 80 of file net.h.

◆ strSubVersion

std::string strSubVersion

Subversion as sent to the P2P network in version messages.

Definition at line 112 of file net.cpp.

◆ TIMEOUT_INTERVAL

const int TIMEOUT_INTERVAL = 20 * 60
static

Time after which to disconnect, after waiting for a ping response (or inactivity).

Definition at line 52 of file net.h.