Bitcoin Core  24.99.0
P2P Digital Currency
netbase.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2022 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/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 Proxy
49 {
50 public:
52  explicit Proxy(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 Proxy &addrProxy);
77 bool GetProxy(enum Network net, Proxy &proxyInfoOut);
78 bool IsProxy(const CNetAddr &addr);
95 bool SetNameProxy(const Proxy &addrProxy);
96 bool HaveNameProxy();
97 bool GetNameProxy(Proxy &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 
179 bool LookupSubNet(const std::string& subnet_str, CSubNet& subnet_out);
180 
186 std::unique_ptr<Sock> CreateSockTCP(const CService& address_family);
187 
191 extern std::function<std::unique_ptr<Sock>(const CService&)> CreateSock;
192 
205 bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nTimeout, bool manual_connection);
206 
222 bool ConnectThroughProxy(const Proxy& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed);
223 
224 void InterruptSocks5(bool interrupt);
225 
244 bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* auth, const Sock& socket);
245 
253 bool IsBadPort(uint16_t port);
254 
255 #endif // BITCOIN_NETBASE_H
Network address.
Definition: netaddress.h:120
bool IsValid() const
Definition: netaddress.cpp:445
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:520
Definition: netbase.h:49
Proxy(const CService &_proxy, bool _randomize_credentials=false)
Definition: netbase.h:52
Proxy()
Definition: netbase.h:51
bool IsValid() const
Definition: netbase.h:54
bool randomize_credentials
Definition: netbase.h:57
CService proxy
Definition: netbase.h:56
RAII helper class that manages a socket.
Definition: sock.h:28
Network
A network type.
Definition: netaddress.h:44
ConnectionDirection
Definition: netbase.h:32
std::unique_ptr< Sock > CreateSockTCP(const CService &address_family)
Create a TCP socket in the given address family.
Definition: netbase.cpp:485
static const int DEFAULT_NAME_LOOKUP
-dns default
Definition: netbase.h:30
std::string GetNetworkName(enum Network net)
Definition: netbase.cpp:105
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:170
bool HaveNameProxy()
Definition: netbase.cpp:647
bool SetNameProxy(const Proxy &addrProxy)
Set the name proxy to use for all connections to nodes specified by a hostname.
Definition: netbase.cpp:631
std::vector< CNetAddr > WrappedGetAddrInfo(const std::string &name, bool allow_lookup)
Wrapper for getaddrinfo(3).
Definition: netbase.cpp:43
bool LookupSubNet(const std::string &subnet_str, CSubNet &subnet_out)
Parse and resolve a specified subnet string into the appropriate internal representation.
Definition: netbase.cpp:684
void InterruptSocks5(bool interrupt)
Definition: netbase.cpp:719
bool SetProxy(enum Network net, const Proxy &addrProxy)
Definition: netbase.cpp:613
std::function< std::unique_ptr< Sock >const CService &)> CreateSock
Socket factory.
Definition: netbase.cpp:534
std::vector< std::string > GetNetworkNames(bool append_unroutable=false)
Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE.
Definition: netbase.cpp:121
bool ConnectThroughProxy(const Proxy &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:661
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:546
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:230
std::function< std::vector< CNetAddr >(const std::string &, bool)> DNSLookupFn
Definition: netbase.h:99
static ConnectionDirection & operator|=(ConnectionDirection &a, ConnectionDirection b)
Definition: netbase.h:38
static bool operator&(ConnectionDirection a, ConnectionDirection b)
Definition: netbase.h:43
bool fNameLookup
Definition: netbase.cpp:37
bool GetProxy(enum Network net, Proxy &proxyInfoOut)
Definition: netbase.cpp:622
int nConnectTimeout
Definition: netbase.cpp:36
enum Network ParseNetwork(const std::string &net)
Definition: netbase.cpp:87
bool GetNameProxy(Proxy &nameProxyOut)
Definition: netbase.cpp:639
bool IsProxy(const CNetAddr &addr)
Definition: netbase.cpp:652
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:198
bool IsBadPort(uint16_t port)
Determine if a port is "bad" from the perspective of attempting to connect to a node on that port.
Definition: netbase.cpp:724
DNSLookupFn g_dns_lookup
Definition: netbase.cpp:85
static const int DEFAULT_CONNECT_TIMEOUT
-timeout default
Definition: netbase.h:28
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:366
const char * name
Definition: rest.cpp:46
Credentials for proxy authentication.
Definition: netbase.h:62
std::string username
Definition: netbase.h:63
std::string password
Definition: netbase.h:64