Bitcoin Core  26.1.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 <unordered_set>
23 #include <vector>
24 
25 extern int nConnectTimeout;
26 extern bool fNameLookup;
27 
29 static const int DEFAULT_CONNECT_TIMEOUT = 5000;
31 static const int DEFAULT_NAME_LOOKUP = true;
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 
49 class Proxy
50 {
51 public:
53  explicit Proxy(const CService &_proxy, bool _randomize_credentials=false): proxy(_proxy), randomize_credentials(_randomize_credentials) {}
54 
55  bool IsValid() const { return proxy.IsValid(); }
56 
59 };
60 
63 {
64  std::string username;
65  std::string password;
66 };
67 
72 public:
74  {
76  LOCK(m_mutex);
77  m_reachable.insert(net);
78  }
79 
81  {
83  LOCK(m_mutex);
84  m_reachable.erase(net);
85  }
86 
88  {
90  LOCK(m_mutex);
91  m_reachable.clear();
92  }
93 
94  [[nodiscard]] bool Contains(Network net) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
95  {
97  LOCK(m_mutex);
98  return m_reachable.count(net) > 0;
99  }
100 
101  [[nodiscard]] bool Contains(const CNetAddr& addr) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
102  {
104  return Contains(addr.GetNetwork());
105  }
106 
107 private:
108  mutable Mutex m_mutex;
109 
110  std::unordered_set<Network> m_reachable GUARDED_BY(m_mutex){
112  NET_IPV4,
113  NET_IPV6,
114  NET_ONION,
115  NET_I2P,
116  NET_CJDNS,
118  };
119 };
120 
122 
126 std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup);
127 
128 enum Network ParseNetwork(const std::string& net);
129 std::string GetNetworkName(enum Network net);
131 std::vector<std::string> GetNetworkNames(bool append_unroutable = false);
132 bool SetProxy(enum Network net, const Proxy &addrProxy);
133 bool GetProxy(enum Network net, Proxy &proxyInfoOut);
134 bool IsProxy(const CNetAddr &addr);
151 bool SetNameProxy(const Proxy &addrProxy);
152 bool HaveNameProxy();
153 bool GetNameProxy(Proxy &nameProxyOut);
154 
155 using DNSLookupFn = std::function<std::vector<CNetAddr>(const std::string&, bool)>;
157 
171 std::vector<CNetAddr> LookupHost(const std::string& name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
172 
182 std::optional<CNetAddr> LookupHost(const std::string& name, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
183 
202 std::vector<CService> Lookup(const std::string& name, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function = g_dns_lookup);
203 
210 std::optional<CService> Lookup(const std::string& name, uint16_t portDefault, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
211 
221 CService LookupNumeric(const std::string& name, uint16_t portDefault = 0, DNSLookupFn dns_lookup_function = g_dns_lookup);
222 
234 bool LookupSubNet(const std::string& subnet_str, CSubNet& subnet_out);
235 
241 std::unique_ptr<Sock> CreateSockTCP(const CService& address_family);
242 
246 extern std::function<std::unique_ptr<Sock>(const CService&)> CreateSock;
247 
260 bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nTimeout, bool manual_connection);
261 
277 bool ConnectThroughProxy(const Proxy& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed);
278 
279 void InterruptSocks5(bool interrupt);
280 
299 bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* auth, const Sock& socket);
300 
308 bool IsBadPort(uint16_t port);
309 
317 CService MaybeFlipIPv6toCJDNS(const CService& service);
318 
319 #endif // BITCOIN_NETBASE_H
Proxy()
Definition: netbase.h:52
std::unique_ptr< Sock > CreateSockTCP(const CService &address_family)
Create a TCP socket in the given address family.
Definition: netbase.cpp:453
void Remove(Network net) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:80
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:514
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:57
bool randomize_credentials
Definition: netbase.h:58
bool HaveNameProxy()
Definition: netbase.cpp:611
IPv4.
Definition: netaddress.h:41
void Add(Network net) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:73
void InterruptSocks5(bool interrupt)
Definition: netbase.cpp:684
bool Contains(Network net) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:94
int nConnectTimeout
Definition: netbase.cpp:28
bool GetNameProxy(Proxy &nameProxyOut)
Definition: netbase.cpp:603
List of reachable networks.
Definition: netbase.h:71
I2P.
Definition: netaddress.h:50
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:779
std::vector< std::string > GetNetworkNames(bool append_unroutable=false)
Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE.
Definition: netbase.cpp:115
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:625
enum Network ParseNetwork(const std::string &net)
Definition: netbase.cpp:81
bool IsProxy(const CNetAddr &addr)
Definition: netbase.cpp:616
static ConnectionDirection & operator|=(ConnectionDirection &a, ConnectionDirection b)
Definition: netbase.h:39
bool IsValid() const
Definition: netaddress.cpp:425
std::vector< CNetAddr > WrappedGetAddrInfo(const std::string &name, bool allow_lookup)
Wrapper for getaddrinfo(3).
Definition: netbase.cpp:37
CService proxy
Definition: netbase.h:57
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:334
static bool operator &(ConnectionDirection a, ConnectionDirection b)
Definition: netbase.h:44
#define LOCK(cs)
Definition: sync.h:258
const char * name
Definition: rest.cpp:45
ReachableNets g_reachable_nets
Definition: netbase.cpp:35
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:534
bool SetProxy(enum Network net, const Proxy &addrProxy)
Definition: netbase.cpp:577
Credentials for proxy authentication.
Definition: netbase.h:62
Network
A network type.
Definition: netaddress.h:36
bool IsValid() const
Definition: netbase.h:55
bool fNameLookup
Definition: netbase.cpp:29
static const int DEFAULT_NAME_LOOKUP
-dns default
Definition: netbase.h:31
bool SetNameProxy(const Proxy &addrProxy)
Set the name proxy to use for all connections to nodes specified by a hostname.
Definition: netbase.cpp:595
Definition: netbase.h:49
Network address.
Definition: netaddress.h:115
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
std::function< std::vector< CNetAddr >(const std::string &, bool)> DNSLookupFn
Definition: netbase.h:155
bool GetProxy(enum Network net, Proxy &proxyInfoOut)
Definition: netbase.cpp:586
std::function< std::unique_ptr< Sock >const CService &)> CreateSock
Socket factory.
Definition: netbase.cpp:502
Mutex m_mutex
Definition: netbase.h:108
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:648
static const int DEFAULT_CONNECT_TIMEOUT
-timeout default
Definition: netbase.h:29
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:176
IPv6.
Definition: netaddress.h:44
std::string GetNetworkName(enum Network net)
Definition: netbase.cpp:99
void RemoveAll() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:87
TOR (v2 or v3)
Definition: netaddress.h:47
#define AssertLockNotHeld(cs)
Definition: sync.h:148
bool Contains(const CNetAddr &addr) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:101
std::string password
Definition: netbase.h:65
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:689
RAII helper class that manages a socket and closes it automatically when it goes out of scope...
Definition: sock.h:26
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:201
std::unordered_set< Network > m_reachable GUARDED_BY(m_mutex)
Definition: netbase.h:110
std::string username
Definition: netbase.h:64
Proxy(const CService &_proxy, bool _randomize_credentials=false)
Definition: netbase.h:53
ConnectionDirection
Definition: netbase.h:33
CJDNS.
Definition: netaddress.h:53
DNSLookupFn g_dns_lookup
Definition: netbase.cpp:79
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:158
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:38