Bitcoin Core 31.99.0
P2P Digital Currency
netbase.h
Go to the documentation of this file.
1// Copyright (c) 2009-present 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 <cstdint>
15#include <functional>
16#include <memory>
17#include <optional>
18#include <string>
19#include <type_traits>
20#include <unordered_set>
21#include <vector>
22
23extern int nConnectTimeout;
24extern bool fNameLookup;
25
27static const int DEFAULT_CONNECT_TIMEOUT = 5000;
29static const int DEFAULT_NAME_LOOKUP = true;
30
32const std::string ADDR_PREFIX_UNIX = "unix:";
33
35 None = 0,
36 In = (1U << 0),
37 Out = (1U << 1),
38 Both = (In | Out),
39};
41 using underlying = std::underlying_type_t<ConnectionDirection>;
42 a = ConnectionDirection(underlying(a) | underlying(b));
43 return a;
44}
46 using underlying = std::underlying_type_t<ConnectionDirection>;
47 return (underlying(a) & underlying(b));
48}
49
57bool IsUnixSocketPath(const std::string& name);
58
59class Proxy
60{
61public:
63 explicit Proxy(const CService& _proxy, bool tor_stream_isolation = false) : proxy(_proxy), m_is_unix_socket(false), m_tor_stream_isolation(tor_stream_isolation) {}
64 explicit Proxy(std::string path, bool tor_stream_isolation = false)
65 : m_unix_socket_path(std::move(path)), m_is_unix_socket(true), m_tor_stream_isolation(tor_stream_isolation) {}
66
68 std::string m_unix_socket_path;
71
72 bool IsValid() const
73 {
75 return proxy.IsValid();
76 }
77
78 sa_family_t GetFamily() const
79 {
80 if (m_is_unix_socket) return AF_UNIX;
81 return proxy.GetSAFamily();
82 }
83
84 std::string ToString() const
85 {
87 return proxy.ToStringAddrPort();
88 }
89
90 std::unique_ptr<Sock> Connect() const;
91};
92
95{
96 std::string username;
97 std::string password;
98};
99
104public:
106 {
108 LOCK(m_mutex);
109 m_reachable.insert(net);
110 }
111
113 {
115 LOCK(m_mutex);
116 m_reachable.erase(net);
117 }
118
120 {
122 LOCK(m_mutex);
123 m_reachable.clear();
124 }
125
127 {
129 LOCK(m_mutex);
130 m_reachable = DefaultNets();
131 }
132
133 [[nodiscard]] bool Contains(Network net) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
134 {
136 LOCK(m_mutex);
137 return m_reachable.contains(net);
138 }
139
140 [[nodiscard]] bool Contains(const CNetAddr& addr) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
141 {
143 return Contains(addr.GetNetwork());
144 }
145
146 [[nodiscard]] std::unordered_set<Network> All() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
147 {
149 LOCK(m_mutex);
150 return m_reachable;
151 }
152
153private:
154 static std::unordered_set<Network> DefaultNets()
155 {
156 return {
158 NET_IPV4,
159 NET_IPV6,
160 NET_ONION,
161 NET_I2P,
162 NET_CJDNS,
164 };
165 };
166
167 mutable Mutex m_mutex;
168 std::unordered_set<Network> m_reachable GUARDED_BY(m_mutex){DefaultNets()};
169};
170
172
176std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup);
177
178enum Network ParseNetwork(const std::string& net);
179std::string GetNetworkName(enum Network net);
181std::vector<std::string> GetNetworkNames(bool append_unroutable = false);
182bool SetProxy(enum Network net, const Proxy &addrProxy);
183std::optional<Proxy> GetProxy(enum Network net);
184bool IsProxy(const CNetAddr &addr);
201bool SetNameProxy(const Proxy &addrProxy);
202bool HaveNameProxy();
203std::optional<Proxy> GetNameProxy();
204
205using DNSLookupFn = std::function<std::vector<CNetAddr>(const std::string&, bool)>;
207
221std::vector<CNetAddr> LookupHost(const std::string& name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
222
232std::optional<CNetAddr> LookupHost(const std::string& name, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
233
252std::vector<CService> Lookup(const std::string& name, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function = g_dns_lookup);
253
260std::optional<CService> Lookup(const std::string& name, uint16_t portDefault, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
261
271CService LookupNumeric(const std::string& name, uint16_t portDefault = 0, DNSLookupFn dns_lookup_function = g_dns_lookup);
272
282CSubNet LookupSubNet(const std::string& subnet_str);
283
291std::unique_ptr<Sock> CreateSockOS(int domain, int type, int protocol);
292
296extern std::function<std::unique_ptr<Sock>(int, int, int)> CreateSock;
297
306std::unique_ptr<Sock> ConnectDirectly(const CService& dest, bool manual_connection);
307
319std::unique_ptr<Sock> ConnectThroughProxy(const Proxy& proxy,
320 const std::string& dest,
321 uint16_t port,
322 bool& proxy_connection_failed);
323
328
347bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* auth, const Sock& socket);
348
356bool IsBadPort(uint16_t port);
357
366
368CService GetBindAddress(const Sock& sock);
369
370#endif // BITCOIN_NETBASE_H
Network address.
Definition: netaddress.h:113
bool IsValid() const
Definition: netaddress.cpp:424
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:530
sa_family_t GetSAFamily() const
Get the address family.
Definition: netaddress.cpp:822
std::string ToStringAddrPort() const
Definition: netaddress.cpp:903
A helper class for interruptible sleeps.
Definition: netbase.h:60
std::string ToString() const
Definition: netbase.h:84
Proxy()
Definition: netbase.h:62
Proxy(std::string path, bool tor_stream_isolation=false)
Definition: netbase.h:64
std::unique_ptr< Sock > Connect() const
Definition: netbase.cpp:668
bool IsValid() const
Definition: netbase.h:72
sa_family_t GetFamily() const
Definition: netbase.h:78
bool m_tor_stream_isolation
Definition: netbase.h:70
bool m_is_unix_socket
Definition: netbase.h:69
Proxy(const CService &_proxy, bool tor_stream_isolation=false)
Definition: netbase.h:63
CService proxy
Definition: netbase.h:67
std::string m_unix_socket_path
Definition: netbase.h:68
List of reachable networks.
Definition: netbase.h:103
std::unordered_set< Network > All() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:146
void Add(Network net) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:105
bool Contains(Network net) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:133
static std::unordered_set< Network > DefaultNets()
Definition: netbase.h:154
void Remove(Network net) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:112
void RemoveAll() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:119
std::unordered_set< Network > m_reachable GUARDED_BY(m_mutex)
Definition: netbase.h:168
Mutex m_mutex
Definition: netbase.h:167
bool Contains(const CNetAddr &addr) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:140
void Reset() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:126
RAII helper class that manages a socket and closes it automatically when it goes out of scope.
Definition: sock.h:28
Definition: common.h:29
Network
A network type.
Definition: netaddress.h:33
@ NET_I2P
I2P.
Definition: netaddress.h:47
@ NET_CJDNS
CJDNS.
Definition: netaddress.h:50
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:44
@ NET_IPV6
IPv6.
Definition: netaddress.h:41
@ NET_IPV4
IPv4.
Definition: netaddress.h:38
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:35
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:54
CSubNet LookupSubNet(const std::string &subnet_str)
Parse and resolve a specified subnet string into the appropriate internal representation.
Definition: netbase.cpp:814
std::optional< Proxy > GetNameProxy()
Definition: netbase.cpp:727
ConnectionDirection
Definition: netbase.h: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:645
static const int DEFAULT_NAME_LOOKUP
-dns default
Definition: netbase.h:29
std::string GetNetworkName(enum Network net)
Definition: netbase.cpp:114
CThreadInterrupt g_socks5_interrupt
Interrupt SOCKS5 reads or writes.
Definition: netbase.cpp:41
bool HaveNameProxy()
Definition: netbase.cpp:736
CService GetBindAddress(const Sock &sock)
Get the bind address for a socket as CService.
Definition: netbase.cpp:953
bool SetNameProxy(const Proxy &addrProxy)
Set the name proxy to use for all connections to nodes specified by a hostname.
Definition: netbase.cpp:719
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:191
std::vector< std::string > GetNetworkNames(bool append_unroutable=false)
Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE.
Definition: netbase.cpp:130
const std::string ADDR_PREFIX_UNIX
Prefix for unix domain socket addresses (which are local filesystem paths)
Definition: netbase.h:32
bool SetProxy(enum Network net, const Proxy &addrProxy)
Definition: netbase.cpp:700
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:944
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:216
std::function< std::vector< CNetAddr >(const std::string &, bool)> DNSLookupFn
Definition: netbase.h:205
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:173
ReachableNets g_reachable_nets
Definition: netbase.cpp:43
std::unique_ptr< Sock > CreateSockOS(int domain, int type, int protocol)
Create a real socket from the operating system.
Definition: netbase.cpp:522
static bool operator&(ConnectionDirection a, ConnectionDirection b)
Definition: netbase.h:45
bool fNameLookup
Definition: netbase.cpp:37
int nConnectTimeout
Definition: netbase.cpp:36
static ConnectionDirection & operator|=(ConnectionDirection &a, ConnectionDirection b)
Definition: netbase.h:40
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:787
std::function< std::unique_ptr< Sock >(int, int, int)> CreateSock
Socket factory.
Definition: netbase.cpp:577
bool IsUnixSocketPath(const std::string &name)
Check if a string is a valid UNIX domain socket path.
Definition: netbase.cpp:226
bool IsProxy(const CNetAddr &addr)
Definition: netbase.cpp:741
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:849
std::vector< CNetAddr > WrappedGetAddrInfo(const std::string &name, bool allow_lookup)
Wrapper for getaddrinfo(3).
Definition: netbase.cpp:45
DNSLookupFn g_dns_lookup
Definition: netbase.cpp:98
static const int DEFAULT_CONNECT_TIMEOUT
-timeout default
Definition: netbase.h:27
std::optional< Proxy > GetProxy(enum Network net)
Definition: netbase.cpp:709
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:392
const char * name
Definition: rest.cpp:49
Credentials for proxy authentication.
Definition: netbase.h:95
std::string username
Definition: netbase.h:96
std::string password
Definition: netbase.h:97
#define AssertLockNotHeld(cs)
Definition: sync.h:149
#define LOCK(cs)
Definition: sync.h:268
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49