Bitcoin
Classes | Macros | Enumerations | Functions | Variables
netbase.cpp File Reference
#include <netbase.h>
#include <sync.h>
#include <tinyformat.h>
#include <util/system.h>
#include <util/strencodings.h>
#include <atomic>
#include <fcntl.h>

Classes

struct  ProxyCredentials
 

Macros

#define MSG_NOSIGNAL   0
 

Enumerations

enum  SOCKSVersion : uint8_t { SOCKS4 = 0x04, SOCKS5 = 0x05 }
 
enum  SOCKS5Method : uint8_t { NOAUTH = 0x00, GSSAPI = 0x01, USER_PASS = 0x02, NO_ACCEPTABLE = 0xff }
 
enum  SOCKS5Command : uint8_t { CONNECT = 0x01, BIND = 0x02, UDP_ASSOCIATE = 0x03 }
 
enum  SOCKS5Reply : uint8_t {
  SUCCEEDED = 0x00, GENFAILURE = 0x01, NOTALLOWED = 0x02, NETUNREACHABLE = 0x03,
  HOSTUNREACHABLE = 0x04, CONNREFUSED = 0x05, TTLEXPIRED = 0x06, CMDUNSUPPORTED = 0x07,
  ATYPEUNSUPPORTED = 0x08
}
 
enum  SOCKS5Atyp : uint8_t { IPV4 = 0x01, DOMAINNAME = 0x03, IPV6 = 0x04 }
 
enum  IntrRecvError {
  IntrRecvError::OK, IntrRecvError::Timeout, IntrRecvError::Disconnected, IntrRecvError::NetworkError,
  IntrRecvError::Interrupted
}
 

Functions

static proxyType proxyInfo [NET_MAXGUARDED_BY (cs_proxyInfos)
 
static std::atomic< bool > interruptSocks5Recv (false)
 
enum Network ParseNetwork (std::string net)
 
std::string GetNetworkName (enum Network net)
 
static bool LookupIntern (const char *pszName, std::vector< CNetAddr > &vIP, unsigned int nMaxSolutions, bool fAllowLookup)
 
bool LookupHost (const char *pszName, std::vector< CNetAddr > &vIP, unsigned int nMaxSolutions, bool fAllowLookup)
 
bool LookupHost (const char *pszName, CNetAddr &addr, bool fAllowLookup)
 
bool Lookup (const char *pszName, std::vector< CService > &vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions)
 
bool Lookup (const char *pszName, CService &addr, int portDefault, bool fAllowLookup)
 
CService LookupNumeric (const char *pszName, int portDefault)
 
struct timeval MillisToTimeval (int64_t nTimeout)
 
static IntrRecvError InterruptibleRecv (uint8_t *data, size_t len, int timeout, const SOCKET &hSocket)
 
static std::string Socks5ErrorString (uint8_t err)
 
static bool Socks5 (const std::string &strDest, int port, const ProxyCredentials *auth, const SOCKET &hSocket)
 
SOCKET CreateSocket (const CService &addrConnect)
 
template<typename... Args>
static void LogConnectFailure (bool manual_connection, const char *fmt, const Args &... args)
 
bool ConnectSocketDirectly (const CService &addrConnect, const SOCKET &hSocket, int nTimeout, bool manual_connection)
 
bool SetProxy (enum Network net, const proxyType &addrProxy)
 
bool GetProxy (enum Network net, proxyType &proxyInfoOut)
 
bool SetNameProxy (const proxyType &addrProxy)
 
bool GetNameProxy (proxyType &nameProxyOut)
 
bool HaveNameProxy ()
 
bool IsProxy (const CNetAddr &addr)
 
bool ConnectThroughProxy (const proxyType &proxy, const std::string &strDest, int port, const SOCKET &hSocket, int nTimeout, bool *outProxyConnectionFailed)
 
bool LookupSubNet (const char *pszName, CSubNet &ret)
 
std::string NetworkErrorString (int err)
 
bool CloseSocket (SOCKET &hSocket)
 
bool SetSocketNonBlocking (const SOCKET &hSocket, bool fNonBlocking)
 
bool SetSocketNoDelay (const SOCKET &hSocket)
 
void InterruptSocks5 (bool interrupt)
 

Variables

static CCriticalSection cs_proxyInfos
 
int nConnectTimeout = DEFAULT_CONNECT_TIMEOUT
 
bool fNameLookup = DEFAULT_NAME_LOOKUP
 
static const int SOCKS5_RECV_TIMEOUT = 20 * 1000
 

Macro Definition Documentation

◆ MSG_NOSIGNAL

#define MSG_NOSIGNAL   0

Enumeration Type Documentation

◆ IntrRecvError

enum IntrRecvError
strong

Status codes that can be returned by InterruptibleRecv

Enumerator
OK 
Timeout 
Disconnected 
NetworkError 
Interrupted 

◆ SOCKS5Atyp

Values defined for ATYPE in RFC1928

Enumerator
IPV4 
DOMAINNAME 
IPV6 

◆ SOCKS5Command

Values defined for CMD in RFC1928

Enumerator
CONNECT 
BIND 
UDP_ASSOCIATE 

◆ SOCKS5Method

Values defined for METHOD in RFC1928

Enumerator
NOAUTH 

No authentication required.

GSSAPI 

GSSAPI.

USER_PASS 

Username/password.

NO_ACCEPTABLE 

No acceptable methods.

◆ SOCKS5Reply

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.

◆ SOCKSVersion

SOCKS version

Enumerator
SOCKS4 
SOCKS5 

Function Documentation

◆ CloseSocket()

bool CloseSocket ( SOCKET hSocket)

Close socket and set hSocket to INVALID_SOCKET

◆ ConnectSocketDirectly()

bool ConnectSocketDirectly ( const CService addrConnect,
const SOCKET hSocket,
int  nTimeout,
bool  manual_connection 
)

Try to connect to the specified service on the specified socket.

Parameters
addrConnectThe service to which to connect.
hSocketThe socket on which to connect.
nTimeoutWait this many milliseconds for the connection to be established.
manual_connectionWhether or not the connection was manually requested (e.g. thru the addnode RPC)
Returns
Whether or not a connection was successfully made.

◆ ConnectThroughProxy()

bool ConnectThroughProxy ( const proxyType proxy,
const std::string &  strDest,
int  port,
const SOCKET hSocket,
int  nTimeout,
bool *  outProxyConnectionFailed 
)

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

Parameters
proxyThe SOCKS5 proxy.
strDestThe destination service to which to connect.
portThe destination port.
hSocketThe socket on which to connect to the SOCKS5 proxy.
nTimeoutWait this many milliseconds for the connection to the SOCKS5 proxy to be established.
outProxyConnectionFailed[out]Whether or not the connection to the SOCKS5 proxy failed.
Returns
Whether or not the operation succeeded.

◆ CreateSocket()

SOCKET CreateSocket ( const CService addrConnect)

Try to create a socket file descriptor with specific properties in the communications domain (address family) of the specified service.

For details on the desired properties, see the inline comments in the source code.

◆ GetNameProxy()

bool GetNameProxy ( proxyType nameProxyOut)

◆ GetNetworkName()

std::string GetNetworkName ( enum Network  net)

◆ GetProxy()

bool GetProxy ( enum Network  net,
proxyType proxyInfoOut 
)

◆ GUARDED_BY()

static proxyType nameProxy GUARDED_BY ( cs_proxyInfos  )
static

◆ HaveNameProxy()

bool HaveNameProxy ( )

◆ InterruptibleRecv()

static IntrRecvError InterruptibleRecv ( uint8_t data,
size_t  len,
int  timeout,
const SOCKET hSocket 
)
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 in milliseconds for this read.
hSocketThe 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 InterruptSocks5(bool). Sockets can be made non-blocking with SetSocketNonBlocking(const SOCKET&, bool).

◆ InterruptSocks5()

void InterruptSocks5 ( bool  interrupt)

◆ interruptSocks5Recv()

static std::atomic<bool> interruptSocks5Recv ( false  )
static

◆ IsProxy()

bool IsProxy ( const CNetAddr addr)

◆ LogConnectFailure()

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

◆ Lookup() [1/2]

bool Lookup ( const char *  pszName,
std::vector< CService > &  vAddr,
int  portDefault,
bool  fAllowLookup,
unsigned int  nMaxSolutions 
)

Resolve a service string to its corresponding service.

Parameters
pszNameThe 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 port number. (e.g. example.com:8333 or
[out]vAddrThe resulting services to which the specified service string resolved.
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
Whether or not the service string successfully resolved to any resulting services.

◆ Lookup() [2/2]

bool Lookup ( const char *  pszName,
CService addr,
int  portDefault,
bool  fAllowLookup 
)

Resolve a service string to its first corresponding service.

See also
Lookup(const char *, std::vector<CService>&, int, bool, unsigned int) for additional parameter descriptions.

◆ LookupHost() [1/2]

bool LookupHost ( const char *  pszName,
std::vector< CNetAddr > &  vIP,
unsigned int  nMaxSolutions,
bool  fAllowLookup 
)

Resolve a host string to its corresponding network addresses.

Parameters
pszNameThe string representing a host. Could be a name or a numerical IP address (IPv6 addresses in their bracketed form are allowed).
[out]vIPThe resulting network addresses to which the specified host string resolved.
Returns
Whether or not the specified host string successfully resolved to any resulting network addresses.
See also
Lookup(const char *, std::vector<CService>&, int, bool, unsigned int) for additional parameter descriptions.

◆ LookupHost() [2/2]

bool LookupHost ( const char *  pszName,
CNetAddr addr,
bool  fAllowLookup 
)

Resolve a host string to its first corresponding network address.

See also
LookupHost(const char *, std::vector<CNetAddr>&, unsigned int, bool) for additional parameter descriptions.

◆ LookupIntern()

static bool LookupIntern ( const char *  pszName,
std::vector< CNetAddr > &  vIP,
unsigned int  nMaxSolutions,
bool  fAllowLookup 
)
static

◆ LookupNumeric()

CService LookupNumeric ( const char *  pszName,
int  portDefault 
)

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 char *, CService&, int, bool) for additional parameter descriptions.

◆ LookupSubNet()

bool LookupSubNet ( const char *  pszName,
CSubNet ret 
)

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

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

◆ MillisToTimeval()

struct timeval MillisToTimeval ( int64_t  nTimeout)

Convert milliseconds to a struct timeval for e.g. select.

◆ NetworkErrorString()

std::string NetworkErrorString ( int  err)

Return readable error string for a network error code

◆ ParseNetwork()

enum Network ParseNetwork ( std::string  net)

◆ SetNameProxy()

bool SetNameProxy ( const proxyType addrProxy)

Set the name proxy to use for all connections to nodes specified by a hostname. After setting this proxy, connecting to a node sepcified 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.

◆ SetProxy()

bool SetProxy ( enum Network  net,
const proxyType addrProxy 
)

◆ SetSocketNoDelay()

bool SetSocketNoDelay ( const SOCKET hSocket)

Set the TCP_NODELAY flag on a socket

◆ SetSocketNonBlocking()

bool SetSocketNonBlocking ( const SOCKET hSocket,
bool  fNonBlocking 
)

Disable or enable blocking-mode for a socket

◆ Socks5()

static bool Socks5 ( const std::string &  strDest,
int  port,
const ProxyCredentials auth,
const SOCKET hSocket 
)
static

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

◆ Socks5ErrorString()

static std::string Socks5ErrorString ( uint8_t  err)
static

Convert SOCKS5 reply to an error message

Variable Documentation

◆ cs_proxyInfos

CCriticalSection cs_proxyInfos
static

◆ fNameLookup

bool fNameLookup = DEFAULT_NAME_LOOKUP

◆ nConnectTimeout

int nConnectTimeout = DEFAULT_CONNECT_TIMEOUT

◆ SOCKS5_RECV_TIMEOUT

const int SOCKS5_RECV_TIMEOUT = 20 * 1000
static