Bitcoin Core  27.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 #include <compat/compat.h>
9 #include <netaddress.h>
10 #include <serialize.h>
11 #include <util/sock.h>
12 #include <util/threadinterrupt.h>
13 
14 #include <functional>
15 #include <memory>
16 #include <stdint.h>
17 #include <string>
18 #include <type_traits>
19 #include <unordered_set>
20 #include <vector>
21 
22 extern int nConnectTimeout;
23 extern bool fNameLookup;
24 
26 static const int DEFAULT_CONNECT_TIMEOUT = 5000;
28 static const int DEFAULT_NAME_LOOKUP = true;
29 
31 const std::string ADDR_PREFIX_UNIX = "unix:";
32 
33 enum class ConnectionDirection {
34  None = 0,
35  In = (1U << 0),
36  Out = (1U << 1),
37  Both = (In | Out),
38 };
40  using underlying = typename std::underlying_type<ConnectionDirection>::type;
41  a = ConnectionDirection(underlying(a) | underlying(b));
42  return a;
43 }
45  using underlying = typename std::underlying_type<ConnectionDirection>::type;
46  return (underlying(a) & underlying(b));
47 }
48 
56 bool IsUnixSocketPath(const std::string& name);
57 
58 class Proxy
59 {
60 public:
62  explicit Proxy(const CService& _proxy, bool _randomize_credentials = false) : proxy(_proxy), m_is_unix_socket(false), m_randomize_credentials(_randomize_credentials) {}
63  explicit Proxy(const std::string path, bool _randomize_credentials = false) : m_unix_socket_path(path), m_is_unix_socket(true), m_randomize_credentials(_randomize_credentials) {}
64 
66  std::string m_unix_socket_path;
69 
70  bool IsValid() const
71  {
73  return proxy.IsValid();
74  }
75 
76  sa_family_t GetFamily() const
77  {
78  if (m_is_unix_socket) return AF_UNIX;
79  return proxy.GetSAFamily();
80  }
81 
82  std::string ToString() const
83  {
85  return proxy.ToStringAddrPort();
86  }
87 
88  std::unique_ptr<Sock> Connect() const;
89 };
90 
93 {
94  std::string username;
95  std::string password;
96 };
97 
102 public:
104  {
106  LOCK(m_mutex);
107  m_reachable.insert(net);
108  }
109 
111  {
113  LOCK(m_mutex);
114  m_reachable.erase(net);
115  }
116 
118  {
120  LOCK(m_mutex);
121  m_reachable.clear();
122  }
123 
124  [[nodiscard]] bool Contains(Network net) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
125  {
127  LOCK(m_mutex);
128  return m_reachable.count(net) > 0;
129  }
130 
131  [[nodiscard]] bool Contains(const CNetAddr& addr) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
132  {
134  return Contains(addr.GetNetwork());
135  }
136 
137 private:
138  mutable Mutex m_mutex;
139 
140  std::unordered_set<Network> m_reachable GUARDED_BY(m_mutex){
142  NET_IPV4,
143  NET_IPV6,
144  NET_ONION,
145  NET_I2P,
146  NET_CJDNS,
148  };
149 };
150 
152 
156 std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup);
157 
158 enum Network ParseNetwork(const std::string& net);
159 std::string GetNetworkName(enum Network net);
161 std::vector<std::string> GetNetworkNames(bool append_unroutable = false);
162 bool SetProxy(enum Network net, const Proxy &addrProxy);
163 bool GetProxy(enum Network net, Proxy &proxyInfoOut);
164 bool IsProxy(const CNetAddr &addr);
181 bool SetNameProxy(const Proxy &addrProxy);
182 bool HaveNameProxy();
183 bool GetNameProxy(Proxy &nameProxyOut);
184 
185 using DNSLookupFn = std::function<std::vector<CNetAddr>(const std::string&, bool)>;
187 
201 std::vector<CNetAddr> LookupHost(const std::string& name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
202 
212 std::optional<CNetAddr> LookupHost(const std::string& name, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
213 
232 std::vector<CService> Lookup(const std::string& name, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function = g_dns_lookup);
233 
240 std::optional<CService> Lookup(const std::string& name, uint16_t portDefault, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
241 
251 CService LookupNumeric(const std::string& name, uint16_t portDefault = 0, DNSLookupFn dns_lookup_function = g_dns_lookup);
252 
262 CSubNet LookupSubNet(const std::string& subnet_str);
263 
269 std::unique_ptr<Sock> CreateSockOS(sa_family_t address_family);
270 
274 extern std::function<std::unique_ptr<Sock>(const sa_family_t&)> CreateSock;
275 
284 std::unique_ptr<Sock> ConnectDirectly(const CService& dest, bool manual_connection);
285 
297 std::unique_ptr<Sock> ConnectThroughProxy(const Proxy& proxy,
298  const std::string& dest,
299  uint16_t port,
300  bool& proxy_connection_failed);
301 
306 
325 bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* auth, const Sock& socket);
326 
334 bool IsBadPort(uint16_t port);
335 
343 CService MaybeFlipIPv6toCJDNS(const CService& service);
344 
345 #endif // BITCOIN_NETBASE_H
Network address.
Definition: netaddress.h:112
bool IsValid() const
Definition: netaddress.cpp:425
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:531
sa_family_t GetSAFamily() const
Get the address family.
Definition: netaddress.cpp:821
std::string ToStringAddrPort() const
Definition: netaddress.cpp:902
A helper class for interruptible sleeps.
Definition: netbase.h:59
Proxy(const CService &_proxy, bool _randomize_credentials=false)
Definition: netbase.h:62
std::string ToString() const
Definition: netbase.h:82
bool m_randomize_credentials
Definition: netbase.h:68
Proxy()
Definition: netbase.h:61
std::unique_ptr< Sock > Connect() const
Definition: netbase.cpp:631
bool IsValid() const
Definition: netbase.h:70
sa_family_t GetFamily() const
Definition: netbase.h:76
bool m_is_unix_socket
Definition: netbase.h:67
CService proxy
Definition: netbase.h:65
Proxy(const std::string path, bool _randomize_credentials=false)
Definition: netbase.h:63
std::string m_unix_socket_path
Definition: netbase.h:66
List of reachable networks.
Definition: netbase.h:101
std::unordered_set< Network > m_reachable GUARDED_BY(m_mutex)
Definition: netbase.h:140
void Add(Network net) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:103
bool Contains(Network net) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:124
void Remove(Network net) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:110
void RemoveAll() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:117
Mutex m_mutex
Definition: netbase.h:138
bool Contains(const CNetAddr &addr) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:131
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:32
@ NET_I2P
I2P.
Definition: netaddress.h:46
@ NET_CJDNS
CJDNS.
Definition: netaddress.h:49
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:43
@ NET_IPV6
IPv6.
Definition: netaddress.h:40
@ NET_IPV4
IPv4.
Definition: netaddress.h:37
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:34
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:53
CSubNet LookupSubNet(const std::string &subnet_str)
Parse and resolve a specified subnet string into the appropriate internal representation.
Definition: netbase.cpp:739
ConnectionDirection
Definition: netbase.h:33
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:182
static const int DEFAULT_NAME_LOOKUP
-dns default
Definition: netbase.h:28
std::string GetNetworkName(enum Network net)
Definition: netbase.cpp:105
CThreadInterrupt g_socks5_interrupt
Interrupt SOCKS5 reads or writes.
Definition: netbase.cpp:39
std::unique_ptr< Sock > CreateSockOS(sa_family_t address_family)
Create a TCP or UNIX socket in the given address family.
Definition: netbase.cpp:488
bool HaveNameProxy()
Definition: netbase.cpp:697
bool SetNameProxy(const Proxy &addrProxy)
Set the name proxy to use for all connections to nodes specified by a hostname.
Definition: netbase.cpp:681
std::vector< CNetAddr > WrappedGetAddrInfo(const std::string &name, bool allow_lookup)
Wrapper for getaddrinfo(3).
Definition: netbase.cpp:43
const std::string ADDR_PREFIX_UNIX
Prefix for unix domain socket addresses (which are local filesystem paths)
Definition: netbase.h:31
bool SetProxy(enum Network net, const Proxy &addrProxy)
Definition: netbase.cpp:663
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
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:711
CService MaybeFlipIPv6toCJDNS(const CService &service)
If an IPv6 address belongs to the address range used by the CJDNS network and the CJDNS network is re...
Definition: netbase.cpp:865
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:207
std::function< std::vector< CNetAddr >(const std::string &, bool)> DNSLookupFn
Definition: netbase.h:185
ReachableNets g_reachable_nets
Definition: netbase.cpp:41
static ConnectionDirection & operator|=(ConnectionDirection &a, ConnectionDirection b)
Definition: netbase.h:39
static bool operator&(ConnectionDirection a, ConnectionDirection b)
Definition: netbase.h:44
bool fNameLookup
Definition: netbase.cpp:35
bool GetProxy(enum Network net, Proxy &proxyInfoOut)
Definition: netbase.cpp:672
int nConnectTimeout
Definition: netbase.cpp:34
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:608
enum Network ParseNetwork(const std::string &net)
Definition: netbase.cpp:87
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:164
bool IsUnixSocketPath(const std::string &name)
Check if a string is a valid UNIX domain socket path.
Definition: netbase.cpp:217
bool GetNameProxy(Proxy &nameProxyOut)
Definition: netbase.cpp:689
bool IsProxy(const CNetAddr &addr)
Definition: netbase.cpp:702
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:775
DNSLookupFn g_dns_lookup
Definition: netbase.cpp:85
static const int DEFAULT_CONNECT_TIMEOUT
-timeout default
Definition: netbase.h:26
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:359
std::function< std::unique_ptr< Sock >const sa_family_t &)> CreateSock
Socket factory.
Definition: netbase.cpp:541
const char * name
Definition: rest.cpp:48
Credentials for proxy authentication.
Definition: netbase.h:93
std::string username
Definition: netbase.h:94
std::string password
Definition: netbase.h:95
#define AssertLockNotHeld(cs)
Definition: sync.h:147
#define LOCK(cs)
Definition: sync.h:257
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49