Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
Classes | Macros | Enumerations | Functions | Variables
net.cpp File Reference
#include <net.h>
#include <addrdb.h>
#include <addrman.h>
#include <avalanche/avalanche.h>
#include <banman.h>
#include <clientversion.h>
#include <common/args.h>
#include <compat.h>
#include <config.h>
#include <consensus/consensus.h>
#include <crypto/sha256.h>
#include <dnsseeds.h>
#include <i2p.h>
#include <logging.h>
#include <netaddress.h>
#include <netbase.h>
#include <node/ui_interface.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/trace.h>
#include <util/translation.h>
#include <fcntl.h>
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <functional>
#include <limits>
#include <optional>
#include <unordered_map>
Include dependency graph for net.cpp:

Go to the source code of this file.

Classes

struct  CompareNodeNetworkTime
 Sort eviction candidates by network/localhost and connection uptime. More...
 
class  CNetCleanup
 

Macros

#define FEELER_SLEEP_WINDOW   1
 

Enumerations

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

Functions

std::map< CNetAddr, LocalServiceInfo > mapLocalHost GUARDED_BY (g_maplocalhost_mutex)
 
uint16_t GetListenPort ()
 
bool GetLocal (CService &addr, const CNetAddr *paddrPeer)
 
static std::vector< CAddressconvertSeed6 (const std::vector< SeedSpec6 > &vSeedsIn)
 Convert the pnSeed6 array into usable address objects.
 
CService GetLocalAddress (const CNetAddr &addrPeer)
 
static int GetnScore (const CService &addr)
 
bool IsPeerAddrLocalGood (CNode *pnode)
 
std::optional< CServiceGetLocalAddrForPeer (CNode &node)
 Returns a local address that we should advertise to this peer.
 
bool AddLocal (const CService &addr, int nScore)
 
bool AddLocal (const CNetAddr &addr, int nScore)
 
void RemoveLocal (const CService &addr)
 
void SetReachable (enum Network net, bool reachable)
 Mark a network as reachable or unreachable (no automatic connects to it)
 
bool IsReachable (enum Network net)
 
bool IsReachable (const CNetAddr &addr)
 
bool SeenLocal (const CService &addr)
 vote for a local address
 
bool IsLocal (const CService &addr)
 check whether a given address is potentially local
 
static CAddress GetBindAddress (SOCKET sock)
 Get the bind address for a socket as CAddress.
 
std::string ConnectionTypeAsString (ConnectionType conn_type)
 Convert ConnectionType enum to a string value.
 
static bool ReverseCompareNodeMinPingTime (const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
 
static bool ReverseCompareNodeTimeConnected (const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
 
static bool CompareNetGroupKeyed (const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
 
static bool CompareNodeBlockTime (const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
 
static bool CompareNodeTXTime (const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
 
static bool CompareNodeProofTime (const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
 
static bool CompareNodeBlockRelayOnlyTime (const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
 
static bool CompareNodeAvailabilityScore (const NodeEvictionCandidate &a, const NodeEvictionCandidate &b)
 
template<typename T , typename Comparator >
static void EraseLastKElements (std::vector< T > &elements, Comparator comparator, size_t k, std::function< bool(const NodeEvictionCandidate &)> predicate=[](const NodeEvictionCandidate &n) { return true;})
 Sort an array by the specified comparator, then erase the last K elements where predicate is true.
 
void ProtectEvictionCandidatesByRatio (std::vector< NodeEvictionCandidate > &eviction_candidates)
 Protect desirable or disadvantaged inbound peers from eviction by ratio.
 
std::optional< NodeIdSelectNodeToEvict (std::vector< NodeEvictionCandidate > &&vEvictionCandidates)
 Select an inbound peer to evict after filtering out (protecting) peers having distinct, difficult-to-forge characteristics.
 
void Discover ()
 Look up IP addresses from all interfaces on the machine and add them to the list of local addresses to self-advertise.
 
std::string getSubVersionEB (uint64_t MaxBlockSize)
 This function convert MaxBlockSize from byte to MB with a decimal precision one digit rounded down E.g.
 
std::string userAgent (const Config &config)
 
void CaptureMessageToFile (const CAddress &addr, const std::string &msg_type, Span< const uint8_t > data, bool is_incoming)
 Dump binary message to file, with timestamp.
 

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 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 const uint64_t SELECT_TIMEOUT_MILLISECONDS = 50
 
const std::string NET_MESSAGE_COMMAND_OTHER = "*other*"
 
static const uint64_t RANDOMIZER_ID_NETGROUP = 0x6c0edd8036ef4036ULL
 
static const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE = 0xd93e69e2bbfa5735ULL
 
static const uint64_t RANDOMIZER_ID_EXTRAENTROPY = 0x94b05d41679a4ff7ULL
 
static const uint64_t RANDOMIZER_ID_ADDRCACHE = 0x1cf2e4ddd306dda9ULL
 
bool fDiscover = true
 
bool fListen = true
 
GlobalMutex g_maplocalhost_mutex
 
static CNetCleanup instance_of_cnetcleanup
 
std::function< void(const CAddress &addr, const std::string &msg_type, Span< const uint8_t > data, bool is_incoming)> CaptureMessage = CaptureMessageToFile
 Defaults to CaptureMessageToFile(), but can be overridden by unit tests.
 

Macro Definition Documentation

◆ FEELER_SLEEP_WINDOW

#define FEELER_SLEEP_WINDOW   1

Definition at line 94 of file net.cpp.

Enumeration Type Documentation

◆ BindFlags

Used to pass flags to the Bind() function.

Enumerator
BF_NONE 
BF_EXPLICIT 
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 97 of file net.cpp.

Function Documentation

◆ AddLocal() [1/2]

bool AddLocal ( const CNetAddr addr,
int  nScore 
)

Definition at line 307 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 278 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,
Span< const uint8_t data,
bool  is_incoming 
)

Dump binary message to file, with timestamp.

Definition at line 3443 of file net.cpp.

Here is the call graph for this function:

◆ CompareNetGroupKeyed()

static bool CompareNetGroupKeyed ( const NodeEvictionCandidate a,
const NodeEvictionCandidate b 
)
static

Definition at line 918 of file net.cpp.

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

◆ CompareNodeAvailabilityScore()

static bool CompareNodeAvailabilityScore ( const NodeEvictionCandidate a,
const NodeEvictionCandidate b 
)
static

Definition at line 988 of file net.cpp.

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

◆ CompareNodeBlockRelayOnlyTime()

static bool CompareNodeBlockRelayOnlyTime ( const NodeEvictionCandidate a,
const NodeEvictionCandidate b 
)
static

Definition at line 971 of file net.cpp.

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

◆ CompareNodeBlockTime()

static bool CompareNodeBlockTime ( const NodeEvictionCandidate a,
const NodeEvictionCandidate b 
)
static

Definition at line 923 of file net.cpp.

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

◆ CompareNodeProofTime()

static bool CompareNodeProofTime ( const NodeEvictionCandidate a,
const NodeEvictionCandidate b 
)
static

Definition at line 957 of file net.cpp.

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

◆ CompareNodeTXTime()

static bool CompareNodeTXTime ( const NodeEvictionCandidate a,
const NodeEvictionCandidate b 
)
static

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

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

◆ convertSeed6()

static std::vector< CAddress > convertSeed6 ( const std::vector< SeedSpec6 > &  vSeedsIn)
static

Convert the pnSeed6 array into usable address objects.

Definition at line 198 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 and only the first address from each interface is used.

Definition at line 2634 of file net.cpp.

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

◆ EraseLastKElements()

template<typename T , typename Comparator >
static void EraseLastKElements ( std::vector< T > &  elements,
Comparator  comparator,
size_t  k,
std::function< bool(const NodeEvictionCandidate &)>  predicate = [](const NodeEvictionCandidate &n) { return true; } 
)
static

Sort an array by the specified comparator, then erase the last K elements where predicate is true.

Definition at line 1029 of file net.cpp.

Here is the caller graph for this function:

◆ GetBindAddress()

static CAddress GetBindAddress ( SOCKET  sock)
static

Get the bind address for a socket as CAddress.

Definition at line 408 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 137 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 
)

Definition at line 173 of file net.cpp.

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

◆ GetLocalAddress()

CService GetLocalAddress ( const CNetAddr addrPeer)

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

Here is the call graph for this function:

◆ GetnScore()

static int GetnScore ( const CService addr)
static

Definition at line 230 of file net.cpp.

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

◆ getSubVersionEB()

std::string getSubVersionEB ( uint64_t  MaxBlockSize)

This function convert MaxBlockSize from byte to MB with a decimal precision one digit rounded down E.g.

1660000 -> 1.6 2010000 -> 2.0 1000000 -> 1.0 230000 -> 0.2 50000 -> 0.0

NB behavior for EB<1MB not standardized yet still the function applies the same algo used for EB greater or equal to 1MB

Definition at line 3409 of file net.cpp.

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

◆ GUARDED_BY()

◆ IsLocal()

bool IsLocal ( const CService addr)

check whether a given address is potentially local

Definition at line 346 of file net.cpp.

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

◆ IsPeerAddrLocalGood()

bool IsPeerAddrLocalGood ( CNode pnode)

Definition at line 237 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 330 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 325 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 the following categories of peers, sorted by longest uptime, even if they're not longest uptime overall:

  • onion peers connected via our tor control service
  • localhost peers, as manually configured hidden services not using -bind=addr[:port]=onion will not be detected as inbound onion connections
  • I2P peers

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

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

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

◆ ReverseCompareNodeMinPingTime()

static bool ReverseCompareNodeMinPingTime ( const NodeEvictionCandidate a,
const NodeEvictionCandidate b 
)
static

Definition at line 908 of file net.cpp.

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

◆ ReverseCompareNodeTimeConnected()

static bool ReverseCompareNodeTimeConnected ( const NodeEvictionCandidate a,
const NodeEvictionCandidate b 
)
static

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

Here is the call graph for this function:

◆ 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 1135 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 317 of file net.cpp.

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

◆ userAgent()

std::string userAgent ( const Config config)

Definition at line 3423 of file net.cpp.

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

Variable Documentation

◆ ANCHORS_DATABASE_FILENAME

const char* const ANCHORS_DATABASE_FILENAME = "anchors.dat"

Anchor IP address database file name.

Definition at line 64 of file net.cpp.

◆ CaptureMessage

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

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

Definition at line 3474 of file net.cpp.

◆ DNSSEEDS_DELAY_FEW_PEERS

constexpr 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 84 of file net.cpp.

◆ DNSSEEDS_DELAY_MANY_PEERS

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

Definition at line 85 of file net.cpp.

◆ DNSSEEDS_DELAY_PEER_THRESHOLD

constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000
staticconstexpr

Definition at line 87 of file net.cpp.

◆ DNSSEEDS_TO_QUERY_AT_ONCE

constexpr int DNSSEEDS_TO_QUERY_AT_ONCE = 3
staticconstexpr

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

Definition at line 72 of file net.cpp.

◆ DUMP_PEERS_INTERVAL

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

Definition at line 67 of file net.cpp.

◆ fDiscover

bool fDiscover = true

Definition at line 125 of file net.cpp.

◆ fListen

bool fListen = true

Definition at line 126 of file net.cpp.

◆ g_maplocalhost_mutex

GlobalMutex g_maplocalhost_mutex

Definition at line 127 of file net.cpp.

◆ instance_of_cnetcleanup

CNetCleanup instance_of_cnetcleanup
static

Definition at line 2899 of file net.cpp.

◆ MAX_BLOCK_RELAY_ONLY_ANCHORS

constexpr size_t MAX_BLOCK_RELAY_ONLY_ANCHORS = 2
staticconstexpr

Maximum number of block-relay-only anchor connections.

Definition at line 58 of file net.cpp.

◆ MAX_UPLOAD_TIMEFRAME

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

The default timeframe for -maxuploadtarget.

1 day.

Definition at line 90 of file net.cpp.

◆ NET_MESSAGE_COMMAND_OTHER

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

Definition at line 112 of file net.cpp.

◆ RANDOMIZER_ID_ADDRCACHE

const uint64_t RANDOMIZER_ID_ADDRCACHE = 0x1cf2e4ddd306dda9ULL
static

Definition at line 121 of file net.cpp.

◆ RANDOMIZER_ID_EXTRAENTROPY

const uint64_t RANDOMIZER_ID_EXTRAENTROPY = 0x94b05d41679a4ff7ULL
static

Definition at line 119 of file net.cpp.

◆ RANDOMIZER_ID_LOCALHOSTNONCE

const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE = 0xd93e69e2bbfa5735ULL
static

Definition at line 117 of file net.cpp.

◆ RANDOMIZER_ID_NETGROUP

const uint64_t RANDOMIZER_ID_NETGROUP = 0x6c0edd8036ef4036ULL
static

Definition at line 115 of file net.cpp.

◆ SELECT_TIMEOUT_MILLISECONDS

const uint64_t SELECT_TIMEOUT_MILLISECONDS = 50
static

Definition at line 110 of file net.cpp.