Bitcoin Core  22.99.0
P2P Digital Currency
netbase.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2019 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_NETBASE_H
6 #define BITCOIN_NETBASE_H
7 
8 #if defined(HAVE_CONFIG_H)
10 #endif
11 
12 #include <compat.h>
13 #include <netaddress.h>
14 #include <serialize.h>
15 #include <util/sock.h>
16 
17 #include <functional>
18 #include <memory>
19 #include <stdint.h>
20 #include <string>
21 #include <type_traits>
22 #include <vector>
23 
24 extern int nConnectTimeout;
25 extern bool fNameLookup;
26 
28 static const int DEFAULT_CONNECT_TIMEOUT = 5000;
30 static const int DEFAULT_NAME_LOOKUP = true;
31 
32 enum class ConnectionDirection {
33  None = 0,
34  In = (1U << 0),
35  Out = (1U << 1),
36  Both = (In | Out),
37 };
39  using underlying = typename std::underlying_type<ConnectionDirection>::type;
40  a = ConnectionDirection(underlying(a) | underlying(b));
41  return a;
42 }
44  using underlying = typename std::underlying_type<ConnectionDirection>::type;
45  return (underlying(a) & underlying(b));
46 }
47 
48 class proxyType
49 {
50 public:
52  explicit proxyType(const CService &_proxy, bool _randomize_credentials=false): proxy(_proxy), randomize_credentials(_randomize_credentials) {}
53 
54  bool IsValid() const { return proxy.IsValid(); }
55 
58 };
59 
62 {
63  std::string username;
64  std::string password;
65 };
66 
70 std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup);
71 
72 enum Network ParseNetwork(const std::string& net);
73 std::string GetNetworkName(enum Network net);
75 std::vector<std::string> GetNetworkNames(bool append_unroutable = false);
76 bool SetProxy(enum Network net, const proxyType &addrProxy);
77 bool GetProxy(enum Network net, proxyType &proxyInfoOut);
78 bool IsProxy(const CNetAddr &addr);
95 bool SetNameProxy(const proxyType &addrProxy);
96 bool HaveNameProxy();
97 bool GetNameProxy(proxyType &nameProxyOut);
98 
99 using DNSLookupFn = std::function<std::vector<CNetAddr>(const std::string&, bool)>;
101 
117 bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
118 
125 bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
126 
147 bool Lookup(const std::string& name, std::vector<CService>& vAddr, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function = g_dns_lookup);
148 
155 bool Lookup(const std::string& name, CService& addr, uint16_t portDefault, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
156 
166 CService LookupNumeric(const std::string& name, uint16_t portDefault = 0, DNSLookupFn dns_lookup_function = g_dns_lookup);
167 
178 bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet, DNSLookupFn dns_lookup_function = g_dns_lookup);
179 
185 std::unique_ptr<Sock> CreateSockTCP(const CService& address_family);
186 
190 extern std::function<std::unique_ptr<Sock>(const CService&)> CreateSock;
191 
204 bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nTimeout, bool manual_connection);
205 
221 bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed);
222 
224 bool SetSocketNonBlocking(const SOCKET& hSocket, bool fNonBlocking);
226 bool SetSocketNoDelay(const SOCKET& hSocket);
227 void InterruptSocks5(bool interrupt);
228 
247 bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* auth, const Sock& socket);
248 
249 #endif // BITCOIN_NETBASE_H
GetNameProxy
bool GetNameProxy(proxyType &nameProxyOut)
Definition: netbase.cpp:631
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:539
ConnectSocketDirectly
bool ConnectSocketDirectly(const CService &addrConnect, const Sock &sock, int nTimeout, bool manual_connection)
Try to connect to the specified service on the specified socket.
Definition: netbase.cpp:538
proxyType::IsValid
bool IsValid() const
Definition: netbase.h:54
InterruptSocks5
void InterruptSocks5(bool interrupt)
Definition: netbase.cpp:751
ConnectionDirection::Out
@ Out
SetSocketNonBlocking
bool SetSocketNonBlocking(const SOCKET &hSocket, bool fNonBlocking)
Disable or enable blocking-mode for a socket.
Definition: netbase.cpp:717
CNetAddr
Network address.
Definition: netaddress.h:118
SetSocketNoDelay
bool SetSocketNoDelay(const SOCKET &hSocket)
Set the TCP_NODELAY flag on a socket.
Definition: netbase.cpp:744
ConnectionDirection::Both
@ Both
operator|=
static ConnectionDirection & operator|=(ConnectionDirection &a, ConnectionDirection b)
Definition: netbase.h:38
bitcoin-config.h
Network
Network
A network type.
Definition: netaddress.h:45
IsProxy
bool IsProxy(const CNetAddr &addr)
Definition: netbase.cpp:644
HaveNameProxy
bool HaveNameProxy()
Definition: netbase.cpp:639
operator&
static bool operator&(ConnectionDirection a, ConnectionDirection b)
Definition: netbase.h:43
proxyType
Definition: netbase.h:48
Sock
RAII helper class that manages a socket.
Definition: sock.h:25
GetNetworkNames
std::vector< std::string > GetNetworkNames(bool append_unroutable=false)
Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE.
Definition: netbase.cpp:118
ConnectionDirection::In
@ In
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.
Definition: netbase.cpp:364
nConnectTimeout
int nConnectTimeout
Definition: netbase.cpp:36
compat.h
fNameLookup
bool fNameLookup
Definition: netbase.cpp:37
netaddress.h
ParseNetwork
enum Network ParseNetwork(const std::string &net)
Definition: netbase.cpp:87
ProxyCredentials::password
std::string password
Definition: netbase.h:64
WrappedGetAddrInfo
std::vector< CNetAddr > WrappedGetAddrInfo(const std::string &name, bool allow_lookup)
Wrapper for getaddrinfo(3).
Definition: netbase.cpp:43
CNetAddr::IsValid
bool IsValid() const
Definition: netaddress.cpp:451
CSubNet
Definition: netaddress.h:486
Lookup
bool Lookup(const std::string &name, std::vector< CService > &vAddr, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function=g_dns_lookup)
Resolve a service string to its corresponding service.
Definition: netbase.cpp:195
proxyType::proxyType
proxyType()
Definition: netbase.h:51
proxyType::proxyType
proxyType(const CService &_proxy, bool _randomize_credentials=false)
Definition: netbase.h:52
CreateSock
std::function< std::unique_ptr< Sock >const CService &)> CreateSock
Socket factory.
Definition: netbase.cpp:526
DEFAULT_CONNECT_TIMEOUT
static const int DEFAULT_CONNECT_TIMEOUT
-timeout default
Definition: netbase.h:28
ProxyCredentials
Credentials for proxy authentication.
Definition: netbase.h:61
SetNameProxy
bool SetNameProxy(const proxyType &addrProxy)
Set the name proxy to use for all connections to nodes specified by a hostname.
Definition: netbase.cpp:623
name
const char * name
Definition: rest.cpp:43
DEFAULT_NAME_LOOKUP
static const int DEFAULT_NAME_LOOKUP
-dns default
Definition: netbase.h:30
LookupHost
bool LookupHost(const std::string &name, std::vector< CNetAddr > &vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function=g_dns_lookup)
Resolve a host string to its corresponding network addresses.
Definition: netbase.cpp:167
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.
Definition: netbase.cpp:676
GetProxy
bool GetProxy(enum Network net, proxyType &proxyInfoOut)
Definition: netbase.cpp:614
proxyType::randomize_credentials
bool randomize_credentials
Definition: netbase.h:57
ConnectThroughProxy
bool ConnectThroughProxy(const proxyType &proxy, const std::string &strDest, uint16_t port, const Sock &sock, int nTimeout, bool &outProxyConnectionFailed)
Connect to a specified destination service through a SOCKS5 proxy by first connecting to the SOCKS5 p...
Definition: netbase.cpp:653
serialize.h
DNSLookupFn
std::function< std::vector< CNetAddr >(const std::string &, bool)> DNSLookupFn
Definition: netbase.h:99
ConnectionDirection::None
@ None
ConnectionDirection
ConnectionDirection
Definition: netbase.h:32
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.
Definition: netbase.cpp:227
SetProxy
bool SetProxy(enum Network net, const proxyType &addrProxy)
Definition: netbase.cpp:605
g_dns_lookup
DNSLookupFn g_dns_lookup
Definition: netbase.cpp:85
sock.h
GetNetworkName
std::string GetNetworkName(enum Network net)
Definition: netbase.cpp:102
ProxyCredentials::username
std::string username
Definition: netbase.h:63
proxyType::proxy
CService proxy
Definition: netbase.h:56
CreateSockTCP
std::unique_ptr< Sock > CreateSockTCP(const CService &address_family)
Create a TCP socket in the given address family.
Definition: netbase.cpp:483
SOCKET
unsigned int SOCKET
Definition: compat.h:41