Bitcoin ABC  0.22.12
P2P Digital Currency
Classes | Macros | Enumerations | Functions | Variables
net.cpp File Reference
#include <net.h>
#include <banman.h>
#include <clientversion.h>
#include <config.h>
#include <consensus/consensus.h>
#include <crypto/sha256.h>
#include <netbase.h>
#include <node/ui_interface.h>
#include <random.h>
#include <scheduler.h>
#include <util/strencodings.h>
#include <util/translation.h>
#include <fcntl.h>
#include <unordered_map>
#include <cmath>
Include dependency graph for net.cpp:

Go to the source code of this file.

Classes

struct  NodeEvictionCandidate
 
class  CNetCleanup
 

Macros

#define FEELER_SLEEP_WINDOW   1
 
#define MSG_NOSIGNAL   0
 
#define MSG_DONTWAIT   0
 

Enumerations

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

Functions

std::map< CNetAddr, LocalServiceInfo > mapLocalHost GUARDED_BY (cs_mapLocalHost)
 
unsigned short 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. More...
 
CAddress GetLocalAddress (const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
 
static int GetnScore (const CService &addr)
 
bool IsPeerAddrLocalGood (CNode *pnode)
 
void AdvertiseLocal (CNode *pnode)
 
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) More...
 
bool IsReachable (enum Network net)
 
bool IsReachable (const CNetAddr &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...
 
static CAddress GetBindAddress (SOCKET sock)
 Get the bind address for a socket as CAddress. More...
 
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)
 
template<typename T , typename Comparator >
static void EraseLastKElements (std::vector< T > &elements, Comparator comparator, size_t k)
 Sort an array by the specified comparator, then erase the last K elements. More...
 
void StartMapPort ()
 
void InterruptMapPort ()
 
void StopMapPort ()
 
void Discover ()
 
int64_t PoissonNextSend (int64_t now, int average_interval_seconds)
 Return a timestamp in the future (in microseconds) for exponentially distributed events. More...
 
std::string getSubVersionEB (uint64_t MaxBlockSize)
 This function convert MaxBlockSize from byte to MB with a decimal precision one digit rounded down E.g. More...
 
std::string userAgent (const Config &config)
 

Variables

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. More...
 
static constexpr std::chrono::seconds DNSSEEDS_DELAY_FEW_PEERS {11}
 How long to delay before querying DNS seeds. More...
 
static constexpr std::chrono::minutes DNSSEEDS_DELAY_MANY_PEERS {5}
 
static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000
 
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
 
bool fDiscover = true
 
bool fListen = true
 
bool g_relay_txes = !DEFAULT_BLOCKSONLY
 
RecursiveMutex cs_mapLocalHost
 
static CNetCleanup instance_of_cnetcleanup
 

Macro Definition Documentation

◆ FEELER_SLEEP_WINDOW

#define FEELER_SLEEP_WINDOW   1

Definition at line 73 of file net.cpp.

◆ MSG_DONTWAIT

#define MSG_DONTWAIT   0

Definition at line 84 of file net.cpp.

◆ MSG_NOSIGNAL

#define MSG_NOSIGNAL   0

Definition at line 78 of file net.cpp.

Enumeration Type Documentation

◆ BindFlags

enum BindFlags

Used to pass flags to the Bind() function.

Enumerator
BF_NONE 
BF_EXPLICIT 
BF_REPORT_ERROR 

Definition at line 88 of file net.cpp.

Function Documentation

◆ AddLocal() [1/2]

bool AddLocal ( const CService addr,
int  nScore 
)

Definition at line 229 of file net.cpp.

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

◆ AddLocal() [2/2]

bool AddLocal ( const CNetAddr addr,
int  nScore 
)

Definition at line 257 of file net.cpp.

Here is the call graph for this function:

◆ AdvertiseLocal()

void AdvertiseLocal ( CNode pnode)

Definition at line 200 of file net.cpp.

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

◆ CompareNetGroupKeyed()

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

Definition at line 883 of file net.cpp.

Here is the caller graph for this function:

◆ CompareNodeBlockTime()

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

Definition at line 888 of file net.cpp.

Here is the caller graph for this function:

◆ CompareNodeTXTime()

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

Definition at line 903 of file net.cpp.

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 151 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 2437 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 
)
static

Sort an array by the specified comparator, then erase the last K elements.

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

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

◆ GetListenPort()

unsigned short GetListenPort ( )

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

Here is the caller graph for this function:

◆ GetLocalAddress()

CAddress GetLocalAddress ( const CNetAddr paddrPeer,
ServiceFlags  nLocalServices 
)

Definition at line 173 of file net.cpp.

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

◆ GetnScore()

static int GetnScore ( const CService addr)
static

Definition at line 184 of file net.cpp.

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

Here is the caller graph for this function:

◆ GUARDED_BY()

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

◆ InterruptMapPort()

void InterruptMapPort ( )

Definition at line 1735 of file net.cpp.

Here is the caller graph for this function:

◆ IsLocal()

bool IsLocal ( const CService addr)

check whether a given address is potentially local

Definition at line 295 of file net.cpp.

Here is the caller graph for this function:

◆ IsPeerAddrLocalGood()

bool IsPeerAddrLocalGood ( CNode pnode)

Definition at line 193 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 ( 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:

◆ IsReachable() [2/2]

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

Definition at line 280 of file net.cpp.

Here is the call graph for this function:

◆ PoissonNextSend()

int64_t PoissonNextSend ( int64_t  now,
int  average_interval_seconds 
)

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

Definition at line 3117 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 261 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 873 of file net.cpp.

Here is the caller graph for this function:

◆ ReverseCompareNodeTimeConnected()

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

Definition at line 878 of file net.cpp.

Here is the caller graph for this function:

◆ SeenLocal()

bool SeenLocal ( const CService addr)

vote for a local address

Definition at line 285 of file net.cpp.

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:

◆ StartMapPort()

void StartMapPort ( )

Definition at line 1732 of file net.cpp.

Here is the caller graph for this function:

◆ StopMapPort()

void StopMapPort ( )

Definition at line 1738 of file net.cpp.

Here is the caller graph for this function:

◆ userAgent()

std::string userAgent ( const Config config)

Definition at line 3164 of file net.cpp.

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

Variable Documentation

◆ cs_mapLocalHost

RecursiveMutex cs_mapLocalHost

Definition at line 112 of file net.cpp.

◆ DNSSEEDS_DELAY_FEW_PEERS

constexpr std::chrono::seconds DNSSEEDS_DELAY_FEW_PEERS {11}
static

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

◆ DNSSEEDS_DELAY_MANY_PEERS

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

Definition at line 67 of file net.cpp.

◆ DNSSEEDS_DELAY_PEER_THRESHOLD

constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000
static

Definition at line 69 of file net.cpp.

◆ DNSSEEDS_TO_QUERY_AT_ONCE

constexpr int DNSSEEDS_TO_QUERY_AT_ONCE = 3
static

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

Definition at line 54 of file net.cpp.

◆ DUMP_PEERS_INTERVAL

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

Definition at line 49 of file net.cpp.

◆ fDiscover

bool fDiscover = true

Definition at line 109 of file net.cpp.

◆ fListen

bool fListen = true

Definition at line 110 of file net.cpp.

◆ g_relay_txes

bool g_relay_txes = !DEFAULT_BLOCKSONLY

Definition at line 111 of file net.cpp.

◆ instance_of_cnetcleanup

CNetCleanup instance_of_cnetcleanup
static

Definition at line 2694 of file net.cpp.

◆ NET_MESSAGE_COMMAND_OTHER

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

Definition at line 98 of file net.cpp.

◆ RANDOMIZER_ID_EXTRAENTROPY

const uint64_t RANDOMIZER_ID_EXTRAENTROPY = 0x94b05d41679a4ff7ULL
static

Definition at line 105 of file net.cpp.

◆ RANDOMIZER_ID_LOCALHOSTNONCE

const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE = 0xd93e69e2bbfa5735ULL
static

Definition at line 103 of file net.cpp.

◆ RANDOMIZER_ID_NETGROUP

const uint64_t RANDOMIZER_ID_NETGROUP = 0x6c0edd8036ef4036ULL
static

Definition at line 101 of file net.cpp.

◆ SELECT_TIMEOUT_MILLISECONDS

const uint64_t SELECT_TIMEOUT_MILLISECONDS = 50
static

Definition at line 96 of file net.cpp.