Bitcoin ABC 0.32.12
P2P Digital Currency
Classes | Typedefs | Enumerations | Functions | Variables
netbase.h File Reference
#include <compat/compat.h>
#include <netaddress.h>
#include <serialize.h>
#include <util/sock.h>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <type_traits>
#include <vector>
Include dependency graph for netbase.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Proxy
 
struct  ProxyCredentials
 Credentials for proxy authentication. More...
 

Typedefs

using DNSLookupFn = std::function< std::vector< CNetAddr >(const std::string &, bool)>
 

Enumerations

enum class  ConnectionDirection { None = 0 , In = (1U << 0) , Out = (1U << 1) , Both = (In | Out) }
 

Functions

static ConnectionDirectionoperator|= (ConnectionDirection &a, ConnectionDirection b)
 
static bool operator& (ConnectionDirection a, ConnectionDirection b)
 
bool IsUnixSocketPath (const std::string &name)
 Check if a string is a valid UNIX domain socket path. More...
 
std::vector< CNetAddrWrappedGetAddrInfo (const std::string &name, bool allow_lookup)
 Wrapper for getaddrinfo(3). More...
 
enum Network ParseNetwork (const std::string &net)
 
std::string GetNetworkName (enum Network net)
 
std::vector< std::string > GetNetworkNames (bool append_unroutable=false)
 Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE. More...
 
bool SetProxy (enum Network net, const Proxy &addrProxy)
 
bool GetProxy (enum Network net, Proxy &proxyInfoOut)
 
bool IsProxy (const CNetAddr &addr)
 
bool SetNameProxy (const Proxy &addrProxy)
 Set the name proxy to use for all connections to nodes specified by a hostname. More...
 
bool HaveNameProxy ()
 
bool GetNameProxy (Proxy &nameProxyOut)
 
std::vector< CNetAddrLookupHost (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. More...
 
std::optional< CNetAddrLookupHost (const std::string &name, bool fAllowLookup, DNSLookupFn dns_lookup_function=g_dns_lookup)
 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=g_dns_lookup)
 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=g_dns_lookup)
 Resolve a service string to its first corresponding service. More...
 
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. More...
 
bool LookupSubNet (const std::string &strSubnet, CSubNet &subnet, DNSLookupFn dns_lookup_function=g_dns_lookup)
 Parse and resolve a specified subnet string into the appropriate internal representation. More...
 
std::unique_ptr< SockCreateSockOS (int domain, int type, int protocol)
 Create a real socket from the operating system. More...
 
std::unique_ptr< SockConnectDirectly (const CService &dest, bool manual_connection)
 Create a socket and try to connect to the specified service. More...
 
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...
 
void InterruptSocks5 (bool interrupt)
 
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. 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...
 

Variables

int nConnectTimeout
 
bool fNameLookup
 
static const int DEFAULT_CONNECT_TIMEOUT = 5000
 -timeout default More...
 
static const int DEFAULT_NAME_LOOKUP = true
 -dns default More...
 
const std::string ADDR_PREFIX_UNIX = "unix:"
 Prefix for unix domain socket addresses (which are local filesystem paths) More...
 
DNSLookupFn g_dns_lookup
 
std::function< std::unique_ptr< Sock >(int, int, int)> CreateSock
 Socket factory. More...
 

Typedef Documentation

◆ DNSLookupFn

using DNSLookupFn = std::function<std::vector<CNetAddr>(const std::string &, bool)>

Definition at line 150 of file netbase.h.

Enumeration Type Documentation

◆ ConnectionDirection

enum class ConnectionDirection
strong
Enumerator
None 
In 
Out 
Both 

Definition at line 37 of file netbase.h.

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 732 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 855 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 ( int  domain,
int  type,
int  protocol 
)

Create a real socket from the operating system.

Parameters
[in]domainCommunications domain, first argument to the socket(2) syscall.
[in]typeType of the socket, second argument to the socket(2) syscall.
[in]protocolThe particular protocol to be used with the socket, third argument to the socket(2) syscall.
Returns
pointer to the created Sock object or unique_ptr that owns nothing in case of failure

Definition at line 594 of file netbase.cpp.

Here is the call graph for this function:

◆ GetNameProxy()

bool GetNameProxy ( Proxy nameProxyOut)

Definition at line 831 of file netbase.cpp.

Here is the caller graph for this function:

◆ GetNetworkName()

std::string GetNetworkName ( enum Network  net)

Definition at line 122 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 = false)

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

Definition at line 145 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 812 of file netbase.cpp.

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

◆ HaveNameProxy()

bool HaveNameProxy ( )

Definition at line 840 of file netbase.cpp.

Here is the caller graph for this function:

◆ InterruptSocks5()

void InterruptSocks5 ( bool  interrupt)

Definition at line 918 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 922 of file netbase.cpp.

Here is the caller graph for this function:

◆ IsProxy()

bool IsProxy ( const CNetAddr addr)

Definition at line 845 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 269 of file netbase.cpp.

Here is the caller 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 247 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 224 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 216 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 198 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 257 of file netbase.cpp.

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

◆ LookupSubNet()

bool LookupSubNet ( const std::string &  strSubnet,
CSubNet subnet,
DNSLookupFn  dns_lookup_function = g_dns_lookup 
)

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

Parameters
strSubnetA 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).
subnetThe resulting internal representation of a subnet.
Returns
Whether the operation succeeded or not.

Definition at line 881 of file netbase.cpp.

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

◆ operator&()

static bool operator& ( ConnectionDirection  a,
ConnectionDirection  b 
)
inlinestatic

Definition at line 51 of file netbase.h.

◆ operator|=()

static ConnectionDirection & operator|= ( ConnectionDirection a,
ConnectionDirection  b 
)
inlinestatic

Definition at line 44 of file netbase.h.

◆ ParseNetwork()

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

Definition at line 100 of file netbase.cpp.

Here is the call graph for this function:
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 822 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 802 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 sock 
)

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.
sockThe 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

If a timeout happens here, this effectively means we timed out while connecting to the remote node. This is very common for Tor, so do not print an error message.

Definition at line 432 of file netbase.cpp.

Here is the call graph for this function:
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 54 of file netbase.cpp.

Here is the call graph for this function:

Variable Documentation

◆ ADDR_PREFIX_UNIX

const std::string ADDR_PREFIX_UNIX = "unix:"

Prefix for unix domain socket addresses (which are local filesystem paths)

Definition at line 35 of file netbase.h.

◆ CreateSock

std::function<std::unique_ptr<Sock>(int, int, int)> CreateSock
extern

Socket factory.

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

Definition at line 660 of file netbase.cpp.

◆ DEFAULT_CONNECT_TIMEOUT

const int DEFAULT_CONNECT_TIMEOUT = 5000
static

-timeout default

Definition at line 28 of file netbase.h.

◆ DEFAULT_NAME_LOOKUP

const int DEFAULT_NAME_LOOKUP = true
static

-dns default

Definition at line 30 of file netbase.h.

◆ fNameLookup

bool fNameLookup
extern

Definition at line 48 of file netbase.cpp.

◆ g_dns_lookup

DNSLookupFn g_dns_lookup
extern

Definition at line 98 of file netbase.cpp.

◆ nConnectTimeout

int nConnectTimeout
extern

Definition at line 47 of file netbase.cpp.