Bitcoin ABC 0.32.12
P2P Digital Currency
netbase.h
Go to the documentation of this file.
1// Copyright (c) 2009-2016 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)
9#include <config/bitcoin-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 <cstdint>
18#include <functional>
19#include <memory>
20#include <string>
21#include <type_traits>
22#include <vector>
23
24extern int nConnectTimeout;
25extern bool fNameLookup;
26
28static const int DEFAULT_CONNECT_TIMEOUT = 5000;
30static const int DEFAULT_NAME_LOOKUP = true;
31
35const std::string ADDR_PREFIX_UNIX = "unix:";
36
38 None = 0,
39 In = (1U << 0),
40 Out = (1U << 1),
41 Both = (In | Out),
42};
43
46 using underlying = typename std::underlying_type<ConnectionDirection>::type;
47 a = ConnectionDirection(underlying(a) | underlying(b));
48 return a;
49}
50
52 using underlying = typename std::underlying_type<ConnectionDirection>::type;
53 return (underlying(a) & underlying(b));
54}
55
65bool IsUnixSocketPath(const std::string &name);
66
67class Proxy {
68public:
70 explicit Proxy(const CService &_proxy, bool randomize_credentials = false)
71 : proxy(_proxy), m_is_unix_socket(false),
72 m_randomize_credentials(randomize_credentials) {}
73 explicit Proxy(const std::string path, bool randomize_credentials = false)
75 m_randomize_credentials(randomize_credentials) {}
76
78 std::string m_unix_socket_path;
81
82 bool IsValid() const {
83 if (m_is_unix_socket) {
85 }
86 return proxy.IsValid();
87 }
88
89 sa_family_t GetFamily() const {
90 if (m_is_unix_socket) {
91 return AF_UNIX;
92 }
93 return proxy.GetSAFamily();
94 }
95
96 std::string ToString() const {
97 if (m_is_unix_socket) {
98 return m_unix_socket_path;
99 }
100 return proxy.ToStringAddrPort();
101 }
102
103 std::unique_ptr<Sock> Connect() const;
104};
105
108 std::string username;
109 std::string password;
110};
111
116std::vector<CNetAddr> WrappedGetAddrInfo(const std::string &name,
117 bool allow_lookup);
118
119enum Network ParseNetwork(const std::string &net);
120std::string GetNetworkName(enum Network net);
125std::vector<std::string> GetNetworkNames(bool append_unroutable = false);
126bool SetProxy(enum Network net, const Proxy &addrProxy);
127bool GetProxy(enum Network net, Proxy &proxyInfoOut);
128bool IsProxy(const CNetAddr &addr);
129
146bool SetNameProxy(const Proxy &addrProxy);
147bool HaveNameProxy();
148bool GetNameProxy(Proxy &nameProxyOut);
149
151 std::function<std::vector<CNetAddr>(const std::string &, bool)>;
153
167std::vector<CNetAddr>
168LookupHost(const std::string &name, unsigned int nMaxSolutions,
169 bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
170
181std::optional<CNetAddr>
182LookupHost(const std::string &name, bool fAllowLookup,
183 DNSLookupFn dns_lookup_function = g_dns_lookup);
184
203std::vector<CService> Lookup(const std::string &name, uint16_t portDefault,
204 bool fAllowLookup, unsigned int nMaxSolutions,
205 DNSLookupFn dns_lookup_function = g_dns_lookup);
206
213std::optional<CService> Lookup(const std::string &name, uint16_t portDefault,
214 bool fAllowLookup,
215 DNSLookupFn dns_lookup_function = g_dns_lookup);
216
227CService LookupNumeric(const std::string &name, uint16_t portDefault = 0,
228 DNSLookupFn dns_lookup_function = g_dns_lookup);
229
241bool LookupSubNet(const std::string &strSubnet, CSubNet &subnet,
242 DNSLookupFn dns_lookup_function = g_dns_lookup);
243
255std::unique_ptr<Sock> CreateSockOS(int domain, int type, int protocol);
256
261extern std::function<std::unique_ptr<Sock>(int, int, int)> CreateSock;
262
273std::unique_ptr<Sock> ConnectDirectly(const CService &dest,
274 bool manual_connection);
275
289std::unique_ptr<Sock> ConnectThroughProxy(const Proxy &proxy,
290 const std::string &dest,
291 uint16_t port,
292 bool &proxy_connection_failed);
293
294void InterruptSocks5(bool interrupt);
295
296bool Socks5(const std::string &strDest, uint16_t port,
297 const ProxyCredentials *auth, const Sock &socket);
298
306bool IsBadPort(uint16_t port);
307
308#endif // BITCOIN_NETBASE_H
Network address.
Definition: netaddress.h:114
bool IsValid() const
Definition: netaddress.cpp:477
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:573
sa_family_t GetSAFamily() const
Get the address family.
std::string ToStringAddrPort() const
Definition: netbase.h:67
std::string ToString() const
Definition: netbase.h:96
bool m_randomize_credentials
Definition: netbase.h:80
Proxy()
Definition: netbase.h:69
std::unique_ptr< Sock > Connect() const
Definition: netbase.cpp:760
bool IsValid() const
Definition: netbase.h:82
sa_family_t GetFamily() const
Definition: netbase.h:89
Proxy(const CService &_proxy, bool randomize_credentials=false)
Definition: netbase.h:70
bool m_is_unix_socket
Definition: netbase.h:79
Proxy(const std::string path, bool randomize_credentials=false)
Definition: netbase.h:73
CService proxy
Definition: netbase.h:77
std::string m_unix_socket_path
Definition: netbase.h:78
RAII helper class that manages a socket and closes it automatically when it goes out of scope.
Definition: sock.h:27
Network
A network type.
Definition: netaddress.h:37
ConnectionDirection
Definition: netbase.h:37
std::unique_ptr< Sock > ConnectDirectly(const CService &dest, bool manual_connection)
Create a socket and try to connect to the specified service.
Definition: netbase.cpp:732
static const int DEFAULT_NAME_LOOKUP
-dns default
Definition: netbase.h:30
std::string GetNetworkName(enum Network net)
Definition: netbase.cpp:122
bool HaveNameProxy()
Definition: netbase.cpp:840
bool SetNameProxy(const Proxy &addrProxy)
Set the name proxy to use for all connections to nodes specified by a hostname.
Definition: netbase.cpp:822
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.
Definition: netbase.cpp:224
std::vector< std::string > GetNetworkNames(bool append_unroutable=false)
Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE.
Definition: netbase.cpp:145
const std::string ADDR_PREFIX_UNIX
Prefix for unix domain socket addresses (which are local filesystem paths)
Definition: netbase.h:35
void InterruptSocks5(bool interrupt)
Definition: netbase.cpp:918
bool SetProxy(enum Network net, const Proxy &addrProxy)
Definition: netbase.cpp:802
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:257
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.
Definition: netbase.cpp:198
std::unique_ptr< Sock > CreateSockOS(int domain, int type, int protocol)
Create a real socket from the operating system.
Definition: netbase.cpp:594
static bool operator&(ConnectionDirection a, ConnectionDirection b)
Definition: netbase.h:51
bool fNameLookup
Definition: netbase.cpp:48
bool GetProxy(enum Network net, Proxy &proxyInfoOut)
Definition: netbase.cpp:812
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:881
int nConnectTimeout
Definition: netbase.cpp:47
static ConnectionDirection & operator|=(ConnectionDirection &a, ConnectionDirection b)
Definition: netbase.h:44
enum Network ParseNetwork(const std::string &net)
Definition: netbase.cpp:100
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 p...
Definition: netbase.cpp:855
std::function< std::unique_ptr< Sock >(int, int, int)> CreateSock
Socket factory.
Definition: netbase.cpp:660
bool IsUnixSocketPath(const std::string &name)
Check if a string is a valid UNIX domain socket path.
Definition: netbase.cpp:269
bool GetNameProxy(Proxy &nameProxyOut)
Definition: netbase.cpp:831
bool IsProxy(const CNetAddr &addr)
Definition: netbase.cpp:845
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:922
std::vector< CNetAddr > WrappedGetAddrInfo(const std::string &name, bool allow_lookup)
Wrapper for getaddrinfo(3).
Definition: netbase.cpp:54
DNSLookupFn g_dns_lookup
Definition: netbase.cpp:98
std::function< std::vector< CNetAddr >(const std::string &, bool)> DNSLookupFn
Definition: netbase.h:151
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:432
const char * name
Definition: rest.cpp:47
Credentials for proxy authentication.
Definition: netbase.h:107
std::string username
Definition: netbase.h:108
std::string password
Definition: netbase.h:109