Bitcoin Core  27.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 #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 
30 enum class ConnectionDirection {
31  None = 0,
32  In = (1U << 0),
33  Out = (1U << 1),
34  Both = (In | Out),
35 };
37  using underlying = typename std::underlying_type<ConnectionDirection>::type;
38  a = ConnectionDirection(underlying(a) | underlying(b));
39  return a;
40 }
42  using underlying = typename std::underlying_type<ConnectionDirection>::type;
43  return (underlying(a) & underlying(b));
44 }
45 
46 class Proxy
47 {
48 public:
50  explicit Proxy(const CService &_proxy, bool _randomize_credentials=false): proxy(_proxy), randomize_credentials(_randomize_credentials) {}
51 
52  bool IsValid() const { return proxy.IsValid(); }
53 
56 };
57 
60 {
61  std::string username;
62  std::string password;
63 };
64 
69 public:
71  {
73  LOCK(m_mutex);
74  m_reachable.insert(net);
75  }
76 
78  {
80  LOCK(m_mutex);
81  m_reachable.erase(net);
82  }
83 
85  {
87  LOCK(m_mutex);
88  m_reachable.clear();
89  }
90 
91  [[nodiscard]] bool Contains(Network net) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
92  {
94  LOCK(m_mutex);
95  return m_reachable.count(net) > 0;
96  }
97 
98  [[nodiscard]] bool Contains(const CNetAddr& addr) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
99  {
101  return Contains(addr.GetNetwork());
102  }
103 
104 private:
105  mutable Mutex m_mutex;
106 
107  std::unordered_set<Network> m_reachable GUARDED_BY(m_mutex){
109  NET_IPV4,
110  NET_IPV6,
111  NET_ONION,
112  NET_I2P,
113  NET_CJDNS,
115  };
116 };
117 
119 
123 std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup);
124 
125 enum Network ParseNetwork(const std::string& net);
126 std::string GetNetworkName(enum Network net);
128 std::vector<std::string> GetNetworkNames(bool append_unroutable = false);
129 bool SetProxy(enum Network net, const Proxy &addrProxy);
130 bool GetProxy(enum Network net, Proxy &proxyInfoOut);
131 bool IsProxy(const CNetAddr &addr);
148 bool SetNameProxy(const Proxy &addrProxy);
149 bool HaveNameProxy();
150 bool GetNameProxy(Proxy &nameProxyOut);
151 
152 using DNSLookupFn = std::function<std::vector<CNetAddr>(const std::string&, bool)>;
154 
168 std::vector<CNetAddr> LookupHost(const std::string& name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
169 
179 std::optional<CNetAddr> LookupHost(const std::string& name, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
180 
199 std::vector<CService> Lookup(const std::string& name, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function = g_dns_lookup);
200 
207 std::optional<CService> Lookup(const std::string& name, uint16_t portDefault, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
208 
218 CService LookupNumeric(const std::string& name, uint16_t portDefault = 0, DNSLookupFn dns_lookup_function = g_dns_lookup);
219 
229 CSubNet LookupSubNet(const std::string& subnet_str);
230 
236 std::unique_ptr<Sock> CreateSockTCP(const CService& address_family);
237 
241 extern std::function<std::unique_ptr<Sock>(const CService&)> CreateSock;
242 
255 bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nTimeout, bool manual_connection);
256 
272 bool ConnectThroughProxy(const Proxy& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed);
273 
278 
297 bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* auth, const Sock& socket);
298 
306 bool IsBadPort(uint16_t port);
307 
315 CService MaybeFlipIPv6toCJDNS(const CService& service);
316 
317 #endif // BITCOIN_NETBASE_H
Proxy()
Definition: netbase.h:49
std::unique_ptr< Sock > CreateSockTCP(const CService &address_family)
Create a TCP socket in the given address family.
Definition: netbase.cpp:447
void Remove(Network net) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:77
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:508
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:53
bool randomize_credentials
Definition: netbase.h:55
CThreadInterrupt g_socks5_interrupt
Interrupt SOCKS5 reads or writes.
Definition: netbase.cpp:33
bool HaveNameProxy()
Definition: netbase.cpp:605
IPv4.
Definition: netaddress.h:37
void Add(Network net) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:70
bool Contains(Network net) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:91
int nConnectTimeout
Definition: netbase.cpp:28
bool GetNameProxy(Proxy &nameProxyOut)
Definition: netbase.cpp:597
List of reachable networks.
Definition: netbase.h:68
I2P.
Definition: netaddress.h:46
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:768
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:619
enum Network ParseNetwork(const std::string &net)
Definition: netbase.cpp:81
bool IsProxy(const CNetAddr &addr)
Definition: netbase.cpp:610
static ConnectionDirection & operator|=(ConnectionDirection &a, ConnectionDirection b)
Definition: netbase.h:36
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:54
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:335
static bool operator &(ConnectionDirection a, ConnectionDirection b)
Definition: netbase.h:41
#define LOCK(cs)
Definition: sync.h:257
const char * name
Definition: rest.cpp:49
ReachableNets g_reachable_nets
Definition: netbase.cpp:35
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:530
bool SetProxy(enum Network net, const Proxy &addrProxy)
Definition: netbase.cpp:571
Credentials for proxy authentication.
Definition: netbase.h:59
Network
A network type.
Definition: netaddress.h:32
bool IsValid() const
Definition: netbase.h:52
bool fNameLookup
Definition: netbase.cpp:29
static const int DEFAULT_NAME_LOOKUP
-dns default
Definition: netbase.h:28
A helper class for interruptible sleeps.
bool SetNameProxy(const Proxy &addrProxy)
Set the name proxy to use for all connections to nodes specified by a hostname.
Definition: netbase.cpp:589
Definition: netbase.h:46
Network address.
Definition: netaddress.h:111
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
std::function< std::vector< CNetAddr >(const std::string &, bool)> DNSLookupFn
Definition: netbase.h:152
bool GetProxy(enum Network net, Proxy &proxyInfoOut)
Definition: netbase.cpp:580
std::function< std::unique_ptr< Sock >const CService &)> CreateSock
Socket factory.
Definition: netbase.cpp:496
Mutex m_mutex
Definition: netbase.h:105
static const int DEFAULT_CONNECT_TIMEOUT
-timeout default
Definition: netbase.h:26
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:40
std::string GetNetworkName(enum Network net)
Definition: netbase.cpp:99
void RemoveAll() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:84
TOR (v2 or v3)
Definition: netaddress.h:43
#define AssertLockNotHeld(cs)
Definition: sync.h:147
bool Contains(const CNetAddr &addr) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: netbase.h:98
CSubNet LookupSubNet(const std::string &subnet_str)
Parse and resolve a specified subnet string into the appropriate internal representation.
Definition: netbase.cpp:642
std::string password
Definition: netbase.h:62
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:678
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:107
std::string username
Definition: netbase.h:61
Proxy(const CService &_proxy, bool _randomize_credentials=false)
Definition: netbase.h:50
ConnectionDirection
Definition: netbase.h:30
CJDNS.
Definition: netaddress.h:49
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:34