Bitcoin Core  27.99.0
P2P Digital Currency
Enumerations | Functions | Variables
netbase.cpp File Reference
#include <netbase.h>
#include <compat/compat.h>
#include <logging.h>
#include <sync.h>
#include <tinyformat.h>
#include <util/sock.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/time.h>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <functional>
#include <limits>
#include <memory>
Include dependency graph for netbase.cpp:

Go to the source code of this file.

Enumerations

enum  SOCKSVersion : uint8_t { SOCKS4 = 0x04 , SOCKS5 = 0x05 }
 SOCKS version. More...
 
enum  SOCKS5Method : uint8_t { NOAUTH = 0x00 , GSSAPI = 0x01 , USER_PASS = 0x02 , NO_ACCEPTABLE = 0xff }
 Values defined for METHOD in RFC1928. More...
 
enum  SOCKS5Command : uint8_t { CONNECT = 0x01 , BIND = 0x02 , UDP_ASSOCIATE = 0x03 }
 Values defined for CMD in RFC1928. More...
 
enum  SOCKS5Reply : uint8_t {
  SUCCEEDED = 0x00 , GENFAILURE = 0x01 , NOTALLOWED = 0x02 , NETUNREACHABLE = 0x03 ,
  HOSTUNREACHABLE = 0x04 , CONNREFUSED = 0x05 , TTLEXPIRED = 0x06 , CMDUNSUPPORTED = 0x07 ,
  ATYPEUNSUPPORTED = 0x08
}
 Values defined for REP in RFC1928. More...
 
enum  SOCKS5Atyp : uint8_t { IPV4 = 0x01 , DOMAINNAME = 0x03 , IPV6 = 0x04 }
 Values defined for ATYPE in RFC1928. More...
 
enum class  IntrRecvError {
  OK , Timeout , Disconnected , NetworkError ,
  Interrupted
}
 Status codes that can be returned by InterruptibleRecv. More...
 

Functions

static Proxy proxyInfo[NET_MAXGUARDED_BY (g_proxyinfo_mutex)
 
std::vector< CNetAddrWrappedGetAddrInfo (const std::string &name, bool allow_lookup)
 Wrapper for getaddrinfo(3). More...
 
enum Network ParseNetwork (const std::string &net_in)
 
std::string GetNetworkName (enum Network net)
 
std::vector< std::string > GetNetworkNames (bool append_unroutable)
 Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE. More...
 
static std::vector< CNetAddrLookupIntern (const std::string &name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
 
std::vector< CNetAddrLookupHost (const std::string &name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
 Resolve a host string to its corresponding network addresses. More...
 
std::optional< CNetAddrLookupHost (const std::string &name, bool fAllowLookup, DNSLookupFn dns_lookup_function)
 Resolve a host string to its first corresponding network address. More...
 
std::vector< CServiceLookup (const std::string &name, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function)
 Resolve a service string to its corresponding service. More...
 
std::optional< CServiceLookup (const std::string &name, uint16_t portDefault, bool fAllowLookup, DNSLookupFn dns_lookup_function)
 Resolve a service string to its first corresponding service. More...
 
CService LookupNumeric (const std::string &name, uint16_t portDefault, DNSLookupFn dns_lookup_function)
 Resolve a service string with a numeric IP to its first corresponding service. More...
 
bool IsUnixSocketPath (const std::string &name)
 Check if a string is a valid UNIX domain socket path. More...
 
static IntrRecvError InterruptibleRecv (uint8_t *data, size_t len, std::chrono::milliseconds timeout, const Sock &sock)
 Try to read a specified number of bytes from a socket. More...
 
static std::string Socks5ErrorString (uint8_t err)
 Convert SOCKS5 reply to an error message. More...
 
bool Socks5 (const std::string &strDest, uint16_t port, const ProxyCredentials *auth, const Sock &sock)
 Connect to a specified destination service through an already connected SOCKS5 proxy. More...
 
std::unique_ptr< SockCreateSockOS (sa_family_t address_family)
 Create a TCP or UNIX socket in the given address family. More...
 
template<typename... Args>
static void LogConnectFailure (bool manual_connection, const char *fmt, const Args &... args)
 
static bool ConnectToSocket (const Sock &sock, struct sockaddr *sockaddr, socklen_t len, const std::string &dest_str, bool manual_connection)
 
std::unique_ptr< SockConnectDirectly (const CService &dest, bool manual_connection)
 Create a socket and try to connect to the specified service. More...
 
bool SetProxy (enum Network net, const Proxy &addrProxy)
 
bool GetProxy (enum Network net, Proxy &proxyInfoOut)
 
bool SetNameProxy (const Proxy &addrProxy)
 Set the name proxy to use for all connections to nodes specified by a hostname. More...
 
bool GetNameProxy (Proxy &nameProxyOut)
 
bool HaveNameProxy ()
 
bool IsProxy (const CNetAddr &addr)
 
std::unique_ptr< SockConnectThroughProxy (const Proxy &proxy, const std::string &dest, uint16_t port, bool &proxy_connection_failed)
 Connect to a specified destination service through a SOCKS5 proxy by first connecting to the SOCKS5 proxy. More...
 
CSubNet LookupSubNet (const std::string &subnet_str)
 Parse and resolve a specified subnet string into the appropriate internal representation. More...
 
bool IsBadPort (uint16_t port)
 Determine if a port is "bad" from the perspective of attempting to connect to a node on that port. More...
 
CService MaybeFlipIPv6toCJDNS (const CService &service)
 If an IPv6 address belongs to the address range used by the CJDNS network and the CJDNS network is reachable (-cjdnsreachable config is set), then change the type from NET_IPV6 to NET_CJDNS. More...
 

Variables

static GlobalMutex g_proxyinfo_mutex
 
int nConnectTimeout = DEFAULT_CONNECT_TIMEOUT
 
bool fNameLookup = DEFAULT_NAME_LOOKUP
 
std::chrono::milliseconds g_socks5_recv_timeout = 20s
 
CThreadInterrupt g_socks5_interrupt
 Interrupt SOCKS5 reads or writes. More...
 
ReachableNets g_reachable_nets
 
DNSLookupFn g_dns_lookup {WrappedGetAddrInfo}
 
std::function< std::unique_ptr< Sock >const sa_family_t &)> CreateSock = CreateSockOS
 Socket factory. More...
 

Enumeration Type Documentation

◆ IntrRecvError

enum IntrRecvError
strong

Status codes that can be returned by InterruptibleRecv.

Enumerator
OK 
Timeout 
Disconnected 
NetworkError 
Interrupted 

Definition at line 279 of file netbase.cpp.

◆ SOCKS5Atyp

enum SOCKS5Atyp : uint8_t

Values defined for ATYPE in RFC1928.

Enumerator
IPV4 
DOMAINNAME 
IPV6 

Definition at line 272 of file netbase.cpp.

◆ SOCKS5Command

enum SOCKS5Command : uint8_t

Values defined for CMD in RFC1928.

Enumerator
CONNECT 
BIND 
UDP_ASSOCIATE 

Definition at line 252 of file netbase.cpp.

◆ SOCKS5Method

enum SOCKS5Method : uint8_t

Values defined for METHOD in RFC1928.

Enumerator
NOAUTH 

No authentication required.

GSSAPI 

GSSAPI.

USER_PASS 

Username/password.

NO_ACCEPTABLE 

No acceptable methods.

Definition at line 244 of file netbase.cpp.

◆ SOCKS5Reply

enum SOCKS5Reply : uint8_t

Values defined for REP in RFC1928.

Enumerator
SUCCEEDED 

Succeeded.

GENFAILURE 

General failure.

NOTALLOWED 

Connection not allowed by ruleset.

NETUNREACHABLE 

Network unreachable.

HOSTUNREACHABLE 

Network unreachable.

CONNREFUSED 

Connection refused.

TTLEXPIRED 

TTL expired.

CMDUNSUPPORTED 

Command not supported.

ATYPEUNSUPPORTED 

Address type not supported.

Definition at line 259 of file netbase.cpp.

◆ SOCKSVersion

enum SOCKSVersion : uint8_t

SOCKS version.

Enumerator
SOCKS4 
SOCKS5 

Definition at line 238 of file netbase.cpp.

Function Documentation

◆ ConnectDirectly()

std::unique_ptr<Sock> ConnectDirectly ( const CService dest,
bool  manual_connection 
)

Create a socket and try to connect to the specified service.

Parameters
[in]destThe service to which to connect.
[in]manual_connectionWhether or not the connection was manually requested (e.g. through the addnode RPC)
Returns
the connected socket if the operation succeeded, empty unique_ptr otherwise

Definition at line 610 of file netbase.cpp.

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

◆ ConnectThroughProxy()

std::unique_ptr<Sock> ConnectThroughProxy ( const Proxy proxy,
const std::string &  dest,
uint16_t  port,
bool &  proxy_connection_failed 
)

Connect to a specified destination service through a SOCKS5 proxy by first connecting to the SOCKS5 proxy.

Parameters
[in]proxyThe SOCKS5 proxy.
[in]destThe destination service to which to connect.
[in]portThe destination port.
[out]proxy_connection_failedWhether or not the connection to the SOCKS5 proxy failed.
Returns
the connected socket if the operation succeeded. Otherwise an empty unique_ptr.

Definition at line 717 of file netbase.cpp.

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

◆ ConnectToSocket()

static bool ConnectToSocket ( const Sock sock,
struct sockaddr *  sockaddr,
socklen_t  len,
const std::string &  dest_str,
bool  manual_connection 
)
static

Definition at line 555 of file netbase.cpp.

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

◆ CreateSockOS()

std::unique_ptr<Sock> CreateSockOS ( sa_family_t  address_family)

Create a TCP or UNIX socket in the given address family.

Parameters
[in]address_familyto use for the socket.
Returns
pointer to the created Sock object or unique_ptr that owns nothing in case of failure

Definition at line 490 of file netbase.cpp.

Here is the call graph for this function:

◆ GetNameProxy()

bool GetNameProxy ( Proxy nameProxyOut)

Definition at line 695 of file netbase.cpp.

Here is the caller graph for this function:

◆ GetNetworkName()

std::string GetNetworkName ( enum Network  net)

Definition at line 107 of file netbase.cpp.

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

◆ GetNetworkNames()

std::vector<std::string> GetNetworkNames ( bool  append_unroutable)

Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE.

Definition at line 123 of file netbase.cpp.

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

◆ GetProxy()

bool GetProxy ( enum Network  net,
Proxy proxyInfoOut 
)

Definition at line 678 of file netbase.cpp.

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

◆ GUARDED_BY()

static Proxy nameProxy GUARDED_BY ( g_proxyinfo_mutex  )
static

◆ HaveNameProxy()

bool HaveNameProxy ( )

Definition at line 703 of file netbase.cpp.

Here is the caller graph for this function:

◆ InterruptibleRecv()

static IntrRecvError InterruptibleRecv ( uint8_t *  data,
size_t  len,
std::chrono::milliseconds  timeout,
const Sock sock 
)
static

Try to read a specified number of bytes from a socket.

Please read the "see also" section for more detail.

Parameters
dataThe buffer where the read bytes should be stored.
lenThe number of bytes to read into the specified buffer.
timeoutThe total timeout for this read.
sockThe socket (has to be in non-blocking mode) from which to read bytes.
Returns
An IntrRecvError indicating the resulting status of this read. IntrRecvError::OK only if all of the specified number of bytes were read.
See also
This function can be interrupted by calling g_socks5_interrupt(). Sockets can be made non-blocking with Sock::SetNonBlocking().

Definition at line 303 of file netbase.cpp.

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

◆ IsBadPort()

bool IsBadPort ( uint16_t  port)

Determine if a port is "bad" from the perspective of attempting to connect to a node on that port.

See also
doc/p2p-bad-ports.md
Parameters
[in]portPort to check.
Returns
whether the port is bad

Definition at line 781 of file netbase.cpp.

Here is the caller graph for this function:

◆ IsProxy()

bool IsProxy ( const CNetAddr addr)

Definition at line 708 of file netbase.cpp.

◆ IsUnixSocketPath()

bool IsUnixSocketPath ( const std::string &  name)

Check if a string is a valid UNIX domain socket path.

Parameters
nameThe string provided by the user representing a local path
Returns
Whether the string has proper format, length, and points to an existing file path

Definition at line 219 of file netbase.cpp.

Here is the caller graph for this function:

◆ LogConnectFailure()

template<typename... Args>
static void LogConnectFailure ( bool  manual_connection,
const char *  fmt,
const Args &...  args 
)
static

Definition at line 546 of file netbase.cpp.

Here is the call graph for this function:

◆ Lookup() [1/2]

std::optional<CService> Lookup ( const std::string &  name,
uint16_t  portDefault,
bool  fAllowLookup,
DNSLookupFn  dns_lookup_function = g_dns_lookup 
)

Resolve a service string to its first corresponding service.

See also
Lookup(const std::string&, uint16_t, bool, unsigned int, DNSLookupFn) for additional parameter descriptions.

Definition at line 202 of file netbase.cpp.

Here is the call graph for this function:

◆ Lookup() [2/2]

std::vector<CService> Lookup ( const std::string &  name,
uint16_t  portDefault,
bool  fAllowLookup,
unsigned int  nMaxSolutions,
DNSLookupFn  dns_lookup_function = g_dns_lookup 
)

Resolve a service string to its corresponding service.

Parameters
nameThe string representing a service. Could be a name or a numerical IP address (IPv6 addresses should be in their disambiguated bracketed form), optionally followed by a uint16_t port number. (e.g. example.com:8333 or
portDefaultThe default port for resulting services if not specified by the service string.
fAllowLookupWhether or not hostname lookups are permitted. If yes, external queries may be performed.
nMaxSolutionsThe maximum number of results we want, specifying 0 means "as many solutions as we get."
Returns
The resulting services to which the specified service string resolved.

Definition at line 184 of file netbase.cpp.

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

◆ LookupHost() [1/2]

std::optional<CNetAddr> LookupHost ( const std::string &  name,
bool  fAllowLookup,
DNSLookupFn  dns_lookup_function = g_dns_lookup 
)

Resolve a host string to its first corresponding network address.

Returns
The resulting network address to which the specified host string resolved or std::nullopt if host does not resolve to an address.
See also
LookupHost(const std::string&, unsigned int, bool, DNSLookupFn) for additional parameter descriptions.

Definition at line 178 of file netbase.cpp.

Here is the call graph for this function:

◆ LookupHost() [2/2]

std::vector<CNetAddr> LookupHost ( const std::string &  name,
unsigned int  nMaxSolutions,
bool  fAllowLookup,
DNSLookupFn  dns_lookup_function = g_dns_lookup 
)

Resolve a host string to its corresponding network addresses.

Parameters
nameThe string representing a host. Could be a name or a numerical IP address (IPv6 addresses in their bracketed form are allowed).
Returns
The resulting network addresses to which the specified host string resolved.
See also
Lookup(const std::string&, uint16_t, bool, unsigned int, DNSLookupFn) for additional parameter descriptions.

Definition at line 166 of file netbase.cpp.

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

◆ LookupIntern()

static std::vector<CNetAddr> LookupIntern ( const std::string &  name,
unsigned int  nMaxSolutions,
bool  fAllowLookup,
DNSLookupFn  dns_lookup_function 
)
static

Definition at line 137 of file netbase.cpp.

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

◆ LookupNumeric()

CService LookupNumeric ( const std::string &  name,
uint16_t  portDefault = 0,
DNSLookupFn  dns_lookup_function = g_dns_lookup 
)

Resolve a service string with a numeric IP to its first corresponding service.

Returns
The resulting CService if the resolution was successful, [::]:0 otherwise.
See also
Lookup(const std::string&, uint16_t, bool, unsigned int, DNSLookupFn) for additional parameter descriptions.

Definition at line 209 of file netbase.cpp.

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

◆ LookupSubNet()

CSubNet LookupSubNet ( const std::string &  subnet_str)

Parse and resolve a specified subnet string into the appropriate internal representation.

Parameters
[in]subnet_strA string representation of a subnet of the form network address [ "/", ( CIDR-style suffix | netmask ) ] e.g. "2001:db8::/32", "192.0.2.0/255.255.255.0" or "8.8.8.8".
Returns
a CSubNet object (that may or may not be valid).

Definition at line 745 of file netbase.cpp.

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

◆ MaybeFlipIPv6toCJDNS()

CService MaybeFlipIPv6toCJDNS ( const CService service)

If an IPv6 address belongs to the address range used by the CJDNS network and the CJDNS network is reachable (-cjdnsreachable config is set), then change the type from NET_IPV6 to NET_CJDNS.

Parameters
[in]serviceAddress to potentially convert.
Returns
a copy of service either unmodified or changed to CJDNS.

Definition at line 871 of file netbase.cpp.

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

◆ ParseNetwork()

enum Network ParseNetwork ( const std::string &  net_in)

Definition at line 87 of file netbase.cpp.

Here is the caller graph for this function:

◆ SetNameProxy()

bool SetNameProxy ( const Proxy addrProxy)

Set the name proxy to use for all connections to nodes specified by a hostname.

After setting this proxy, connecting to a node specified by a hostname won't result in a local lookup of said hostname, rather, connect to the node by asking the name proxy for a proxy connection to the hostname, effectively delegating the hostname lookup to the specified proxy.

This delegation increases privacy for those who set the name proxy as they no longer leak their external hostname queries to their DNS servers.

Returns
Whether or not the operation succeeded.
Note
SOCKS5's support for UDP-over-SOCKS5 has been considered, but no SOCK5 server in common use (most notably Tor) actually implements UDP support, and a DNS resolver is beyond the scope of this project.

Definition at line 687 of file netbase.cpp.

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

◆ SetProxy()

bool SetProxy ( enum Network  net,
const Proxy addrProxy 
)

Definition at line 669 of file netbase.cpp.

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

◆ Socks5()

bool Socks5 ( const std::string &  strDest,
uint16_t  port,
const ProxyCredentials auth,
const Sock socket 
)

Connect to a specified destination service through an already connected SOCKS5 proxy.

Parameters
strDestThe destination fully-qualified domain name.
portThe destination port.
authThe credentials with which to authenticate with the specified SOCKS5 proxy.
socketThe SOCKS5 proxy socket.
Returns
Whether or not the operation succeeded.
Note
The specified SOCKS5 proxy socket must already be connected to the SOCKS5 proxy.
See also
RFC1928: SOCKS Protocol Version 5

Definition at line 361 of file netbase.cpp.

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

◆ Socks5ErrorString()

static std::string Socks5ErrorString ( uint8_t  err)
static

Convert SOCKS5 reply to an error message.

Definition at line 337 of file netbase.cpp.

Here is the caller graph for this function:

◆ WrappedGetAddrInfo()

std::vector<CNetAddr> WrappedGetAddrInfo ( const std::string &  name,
bool  allow_lookup 
)

Wrapper for getaddrinfo(3).

Do not use directly: call Lookup/LookupHost/LookupNumeric/LookupSubNet.

Definition at line 45 of file netbase.cpp.

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

Variable Documentation

◆ CreateSock

std::function<std::unique_ptr<Sock>const sa_family_t&)> CreateSock = CreateSockOS

Socket factory.

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

Definition at line 543 of file netbase.cpp.

◆ fNameLookup

bool fNameLookup = DEFAULT_NAME_LOOKUP

Definition at line 37 of file netbase.cpp.

◆ g_dns_lookup

Definition at line 87 of file netbase.cpp.

◆ g_proxyinfo_mutex

GlobalMutex g_proxyinfo_mutex
static

Definition at line 33 of file netbase.cpp.

◆ g_reachable_nets

ReachableNets g_reachable_nets

Definition at line 43 of file netbase.cpp.

◆ g_socks5_interrupt

CThreadInterrupt g_socks5_interrupt

Interrupt SOCKS5 reads or writes.

Definition at line 41 of file netbase.cpp.

◆ g_socks5_recv_timeout

std::chrono::milliseconds g_socks5_recv_timeout = 20s

Definition at line 40 of file netbase.cpp.

◆ nConnectTimeout

int nConnectTimeout = DEFAULT_CONNECT_TIMEOUT

Definition at line 36 of file netbase.cpp.