Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
net.cpp File Reference
#include <bitcoin-build-config.h>
#include <net.h>
#include <addrdb.h>
#include <addrman.h>
#include <banman.h>
#include <clientversion.h>
#include <common/args.h>
#include <common/netif.h>
#include <compat/compat.h>
#include <consensus/consensus.h>
#include <crypto/sha256.h>
#include <i2p.h>
#include <key.h>
#include <logging.h>
#include <memusage.h>
#include <net_permissions.h>
#include <netaddress.h>
#include <netbase.h>
#include <node/eviction.h>
#include <node/interface_ui.h>
#include <protocol.h>
#include <random.h>
#include <scheduler.h>
#include <util/fs.h>
#include <util/sock.h>
#include <util/strencodings.h>
#include <util/thread.h>
#include <util/threadinterrupt.h>
#include <util/trace.h>
#include <util/translation.h>
#include <util/vector.h>
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <functional>
#include <optional>
#include <string_view>
#include <unordered_map>
Include dependency graph for net.cpp:

Go to the source code of this file.

Classes

class  CNetCleanup

Macros

#define X(name)

Enumerations

enum  BindFlags { BF_NONE = 0 , BF_REPORT_ERROR = (1U << 0) , BF_DONT_ADVERTISE = (1U << 1) }
 Used to pass flags to the Bind() function. More...

Functions

 TRACEPOINT_SEMAPHORE (net, closed_connection)
 TRACEPOINT_SEMAPHORE (net, evicted_inbound_connection)
 TRACEPOINT_SEMAPHORE (net, inbound_connection)
 TRACEPOINT_SEMAPHORE (net, outbound_connection)
 TRACEPOINT_SEMAPHORE (net, outbound_message)
std::map< CNetAddr, LocalServiceInfo > mapLocalHost GUARDED_BY (g_maplocalhost_mutex)
uint16_t GetListenPort ()
static std::optional< CServiceGetLocal (const CNode &peer)
static std::vector< CAddressConvertSeeds (const std::vector< uint8_t > &vSeedsIn)
 Convert the serialized seeds into usable address objects.
CService GetLocalAddress (const CNode &peer)
static int GetnScore (const CService &addr)
static bool IsPeerAddrLocalGood (CNode *pnode)
std::optional< CServiceGetLocalAddrForPeer (CNode &node)
 Returns a local address that we should advertise to this peer.
void ClearLocal ()
bool AddLocal (const CService &addr_, int nScore)
bool AddLocal (const CNetAddr &addr, int nScore)
void RemoveLocal (const CService &addr)
bool SeenLocal (const CService &addr)
 vote for a local address
bool IsLocal (const CService &addr)
 check whether a given address is potentially local
void Discover ()
 Look up IP addresses from all interfaces on the machine and add them to the list of local addresses to self-advertise.
static std::unique_ptr< TransportMakeTransport (NodeId id, bool use_v2transport, bool inbound) noexcept
static bool IsOutboundMessageAllowedInPrivateBroadcast (std::string_view type) noexcept
 Private broadcast connections only need to send certain message types.
static void CaptureMessageToFile (const CAddress &addr, const std::string &msg_type, std::span< const unsigned char > data, bool is_incoming)

Variables

static constexpr size_t MAX_BLOCK_RELAY_ONLY_ANCHORS = 2
 Maximum number of block-relay-only anchor connections.
const char *const ANCHORS_DATABASE_FILENAME = "anchors.dat"
 Anchor IP address database file name.
static constexpr std::chrono::minutes DUMP_PEERS_INTERVAL {15}
static constexpr int DNSSEEDS_TO_QUERY_AT_ONCE = 3
 Number of DNS seeds to query when the number of connections is low.
static constexpr int SEED_OUTBOUND_CONNECTION_THRESHOLD = 2
 Minimum number of outbound connections under which we will keep fetching our address seeds.
static constexpr std::chrono::seconds DNSSEEDS_DELAY_FEW_PEERS {11}
 How long to delay before querying DNS seeds.
static constexpr std::chrono::minutes DNSSEEDS_DELAY_MANY_PEERS {5}
static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000
static constexpr std::chrono::seconds MAX_UPLOAD_TIMEFRAME {60 * 60 * 24}
 The default timeframe for -maxuploadtarget.
static constexpr auto FEELER_SLEEP_WINDOW {1s}
static constexpr auto EXTRA_NETWORK_PEER_INTERVAL {5min}
 Frequency to attempt extra connections to reachable networks we're not connected to yet.
static const uint64_t SELECT_TIMEOUT_MILLISECONDS = 50
const std::string NET_MESSAGE_TYPE_OTHER = "*other*"
static const uint64_t RANDOMIZER_ID_NETGROUP = 0x6c0edd8036ef4036ULL
static const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE = 0xd93e69e2bbfa5735ULL
static const uint64_t RANDOMIZER_ID_NETWORKKEY = 0x0e8a2b136c592a7dULL
bool fDiscover = true
bool fListen = true
GlobalMutex g_maplocalhost_mutex
std::string strSubVersion
 Subversion as sent to the P2P network in version messages.
static CNetCleanup instance_of_cnetcleanup
std::function< void(const CAddress &addr, const std::string &msg_type, std::span< const unsigned char > data, bool is_incoming)> CaptureMessage = CaptureMessageToFile
 Defaults to CaptureMessageToFile(), but can be overridden by unit tests.

Macro Definition Documentation

◆ X

#define X ( name)
Value:
stats.name = name
const char * name
Definition rest.cpp:48

Definition at line 613 of file net.cpp.

Enumeration Type Documentation

◆ BindFlags

enum BindFlags

Used to pass flags to the Bind() function.

Enumerator
BF_NONE 
BF_REPORT_ERROR 
BF_DONT_ADVERTISE 

Do not call AddLocal() for our special addresses, e.g., for incoming Tor connections, to prevent gossiping them over the network.

Definition at line 94 of file net.cpp.

Function Documentation

◆ AddLocal() [1/2]

bool AddLocal ( const CNetAddr & addr,
int nScore )

Definition at line 305 of file net.cpp.

Here is the call graph for this function:

◆ AddLocal() [2/2]

bool AddLocal ( const CService & addr_,
int nScore )

Definition at line 277 of file net.cpp.

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

◆ CaptureMessageToFile()

void CaptureMessageToFile ( const CAddress & addr,
const std::string & msg_type,
std::span< const unsigned char > data,
bool is_incoming )
static

Definition at line 4184 of file net.cpp.

Here is the call graph for this function:

◆ ClearLocal()

void ClearLocal ( )

Definition at line 270 of file net.cpp.

Here is the caller graph for this function:

◆ ConvertSeeds()

std::vector< CAddress > ConvertSeeds ( const std::vector< uint8_t > & vSeedsIn)
static

Convert the serialized seeds into usable address objects.

Definition at line 195 of file net.cpp.

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

◆ Discover()

void Discover ( )

Look up IP addresses from all interfaces on the machine and add them to the list of local addresses to self-advertise.

The loopback interface is skipped.

Definition at line 3343 of file net.cpp.

Here is the call graph for this function:

◆ GetListenPort()

uint16_t GetListenPort ( )

Definition at line 138 of file net.cpp.

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

◆ GetLocal()

std::optional< CService > GetLocal ( const CNode & peer)
staticnodiscard

Definition at line 165 of file net.cpp.

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

◆ GetLocalAddress()

CService GetLocalAddress ( const CNode & peer)

Definition at line 220 of file net.cpp.

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

◆ GetLocalAddrForPeer()

std::optional< CService > GetLocalAddrForPeer ( CNode & node)

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

Definition at line 240 of file net.cpp.

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

◆ GetnScore()

int GetnScore ( const CService & addr)
static

Definition at line 225 of file net.cpp.

Here is the caller graph for this function:

◆ GUARDED_BY()

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

◆ IsLocal()

bool IsLocal ( const CService & addr)

check whether a given address is potentially local

Definition at line 329 of file net.cpp.

Here is the caller graph for this function:

◆ IsOutboundMessageAllowedInPrivateBroadcast()

bool IsOutboundMessageAllowedInPrivateBroadcast ( std::string_view type)
staticnoexcept

Private broadcast connections only need to send certain message types.

Other messages are not needed and may degrade privacy.

Definition at line 4049 of file net.cpp.

Here is the caller graph for this function:

◆ IsPeerAddrLocalGood()

bool IsPeerAddrLocalGood ( CNode * pnode)
staticnodiscard

Definition at line 233 of file net.cpp.

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

◆ MakeTransport()

std::unique_ptr< Transport > MakeTransport ( NodeId id,
bool use_v2transport,
bool inbound )
staticnoexcept

Definition at line 3959 of file net.cpp.

Here is the caller graph for this function:

◆ RemoveLocal()

void RemoveLocal ( const CService & addr)

Definition at line 310 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 318 of file net.cpp.

Here is the caller graph for this function:

◆ TRACEPOINT_SEMAPHORE() [1/5]

TRACEPOINT_SEMAPHORE ( net ,
closed_connection  )

◆ TRACEPOINT_SEMAPHORE() [2/5]

TRACEPOINT_SEMAPHORE ( net ,
evicted_inbound_connection  )

◆ TRACEPOINT_SEMAPHORE() [3/5]

TRACEPOINT_SEMAPHORE ( net ,
inbound_connection  )

◆ TRACEPOINT_SEMAPHORE() [4/5]

TRACEPOINT_SEMAPHORE ( net ,
outbound_connection  )

◆ TRACEPOINT_SEMAPHORE() [5/5]

TRACEPOINT_SEMAPHORE ( net ,
outbound_message  )

Variable Documentation

◆ ANCHORS_DATABASE_FILENAME

const char* const ANCHORS_DATABASE_FILENAME = "anchors.dat"

Anchor IP address database file name.

Definition at line 60 of file net.cpp.

◆ CaptureMessage

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

Defaults to CaptureMessageToFile(), but can be overridden by unit tests.

Definition at line 4224 of file net.cpp.

◆ DNSSEEDS_DELAY_FEW_PEERS

std::chrono::seconds DNSSEEDS_DELAY_FEW_PEERS {11}
staticconstexpr

How long to delay before querying DNS seeds.

If we have more than THRESHOLD entries in addrman, then it's likely that we got those addresses from having previously connected to the P2P network, and that we'll be able to successfully reconnect to the P2P network via contacting one of them. So if that's the case, spend a little longer trying to connect to known peers before querying the DNS seeds.

Definition at line 80 of file net.cpp.

◆ DNSSEEDS_DELAY_MANY_PEERS

std::chrono::minutes DNSSEEDS_DELAY_MANY_PEERS {5}
staticconstexpr

Definition at line 81 of file net.cpp.

◆ DNSSEEDS_DELAY_PEER_THRESHOLD

int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000
staticconstexpr

Definition at line 82 of file net.cpp.

◆ DNSSEEDS_TO_QUERY_AT_ONCE

int DNSSEEDS_TO_QUERY_AT_ONCE = 3
staticconstexpr

Number of DNS seeds to query when the number of connections is low.

Definition at line 66 of file net.cpp.

◆ DUMP_PEERS_INTERVAL

std::chrono::minutes DUMP_PEERS_INTERVAL {15}
staticconstexpr

Definition at line 63 of file net.cpp.

◆ EXTRA_NETWORK_PEER_INTERVAL

auto EXTRA_NETWORK_PEER_INTERVAL {5min}
staticconstexpr

Frequency to attempt extra connections to reachable networks we're not connected to yet.

Definition at line 91 of file net.cpp.

◆ fDiscover

bool fDiscover = true

Definition at line 116 of file net.cpp.

◆ FEELER_SLEEP_WINDOW

auto FEELER_SLEEP_WINDOW {1s}
staticconstexpr

Definition at line 88 of file net.cpp.

◆ fListen

bool fListen = true

Definition at line 117 of file net.cpp.

◆ g_maplocalhost_mutex

GlobalMutex g_maplocalhost_mutex

Definition at line 118 of file net.cpp.

◆ instance_of_cnetcleanup

CNetCleanup instance_of_cnetcleanup
static

Definition at line 3584 of file net.cpp.

◆ MAX_BLOCK_RELAY_ONLY_ANCHORS

size_t MAX_BLOCK_RELAY_ONLY_ANCHORS = 2
staticconstexpr

Maximum number of block-relay-only anchor connections.

Definition at line 57 of file net.cpp.

◆ MAX_UPLOAD_TIMEFRAME

std::chrono::seconds MAX_UPLOAD_TIMEFRAME {60 * 60 * 24}
staticconstexpr

The default timeframe for -maxuploadtarget.

1 day.

Definition at line 85 of file net.cpp.

◆ NET_MESSAGE_TYPE_OTHER

const std::string NET_MESSAGE_TYPE_OTHER = "*other*"

Definition at line 108 of file net.cpp.

◆ RANDOMIZER_ID_LOCALHOSTNONCE

const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE = 0xd93e69e2bbfa5735ULL
static

Definition at line 111 of file net.cpp.

◆ RANDOMIZER_ID_NETGROUP

const uint64_t RANDOMIZER_ID_NETGROUP = 0x6c0edd8036ef4036ULL
static

Definition at line 110 of file net.cpp.

◆ RANDOMIZER_ID_NETWORKKEY

const uint64_t RANDOMIZER_ID_NETWORKKEY = 0x0e8a2b136c592a7dULL
static

Definition at line 112 of file net.cpp.

◆ SEED_OUTBOUND_CONNECTION_THRESHOLD

int SEED_OUTBOUND_CONNECTION_THRESHOLD = 2
staticconstexpr

Minimum number of outbound connections under which we will keep fetching our address seeds.

Definition at line 69 of file net.cpp.

◆ SELECT_TIMEOUT_MILLISECONDS

const uint64_t SELECT_TIMEOUT_MILLISECONDS = 50
static

Definition at line 106 of file net.cpp.

◆ strSubVersion

std::string strSubVersion

Subversion as sent to the P2P network in version messages.

Definition at line 120 of file net.cpp.