Bitcoin ABC  0.26.3
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.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 <vector>
22 
23 extern int nConnectTimeout;
24 extern bool fNameLookup;
25 
27 static const int DEFAULT_CONNECT_TIMEOUT = 5000;
29 static const int DEFAULT_NAME_LOOKUP = true;
30 
31 class proxyType {
32 public:
34  explicit proxyType(const CService &_proxy,
35  bool _randomize_credentials = false)
36  : proxy(_proxy), randomize_credentials(_randomize_credentials) {}
37 
38  bool IsValid() const { return proxy.IsValid(); }
39 
42 };
43 
48 std::vector<CNetAddr> WrappedGetAddrInfo(const std::string &name,
49  bool allow_lookup);
50 
51 enum Network ParseNetwork(const std::string &net);
52 std::string GetNetworkName(enum Network net);
57 std::vector<std::string> GetNetworkNames(bool append_unroutable = false);
58 bool SetProxy(enum Network net, const proxyType &addrProxy);
59 bool GetProxy(enum Network net, proxyType &proxyInfoOut);
60 bool IsProxy(const CNetAddr &addr);
61 
78 bool SetNameProxy(const proxyType &addrProxy);
79 bool HaveNameProxy();
80 bool GetNameProxy(proxyType &nameProxyOut);
81 
82 using DNSLookupFn =
83  std::function<std::vector<CNetAddr>(const std::string &, bool)>;
85 
101 bool LookupHost(const std::string &name, std::vector<CNetAddr> &vIP,
102  unsigned int nMaxSolutions, bool fAllowLookup,
103  DNSLookupFn dns_lookup_function = g_dns_lookup);
104 
111 bool LookupHost(const std::string &name, CNetAddr &addr, bool fAllowLookup,
112  DNSLookupFn dns_lookup_function = g_dns_lookup);
113 
134 bool Lookup(const std::string &name, std::vector<CService> &vAddr,
135  uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions,
136  DNSLookupFn dns_lookup_function = g_dns_lookup);
137 
144 bool Lookup(const std::string &name, CService &addr, uint16_t portDefault,
145  bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
146 
157 CService LookupNumeric(const std::string &name, uint16_t portDefault = 0,
158  DNSLookupFn dns_lookup_function = g_dns_lookup);
159 
171 bool LookupSubNet(const std::string &strSubnet, CSubNet &subnet,
172  DNSLookupFn dns_lookup_function = g_dns_lookup);
173 
181 std::unique_ptr<Sock> CreateSockTCP(const CService &address_family);
182 
187 extern std::function<std::unique_ptr<Sock>(const CService &)> CreateSock;
188 
201 bool ConnectSocketDirectly(const CService &addrConnect, const Sock &sock,
202  int nTimeout, bool manual_connection);
203 
219 bool ConnectThroughProxy(const proxyType &proxy, const std::string &strDest,
220  uint16_t port, const Sock &sock, int nTimeout,
221  bool &outProxyConnectionFailed);
222 
224 bool SetSocketNonBlocking(const SOCKET &hSocket, bool fNonBlocking);
226 bool SetSocketNoDelay(const SOCKET &hSocket);
227 void InterruptSocks5(bool interrupt);
228 
236 bool IsBadPort(uint16_t port);
237 
238 #endif // BITCOIN_NETBASE_H
Network address.
Definition: netaddress.h:122
bool IsValid() const
Definition: netaddress.cpp:479
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:545
RAII helper class that manages a socket.
Definition: sock.h:26
bool IsValid() const
Definition: netbase.h:38
CService proxy
Definition: netbase.h:40
proxyType()
Definition: netbase.h:33
bool randomize_credentials
Definition: netbase.h:41
proxyType(const CService &_proxy, bool _randomize_credentials=false)
Definition: netbase.h:34
unsigned int SOCKET
Definition: compat.h:40
Network
A network type.
Definition: netaddress.h:45
bool GetNameProxy(proxyType &nameProxyOut)
Definition: netbase.cpp:739
std::unique_ptr< Sock > CreateSockTCP(const CService &address_family)
Create a TCP socket in the given address family.
Definition: netbase.cpp:572
static const int DEFAULT_NAME_LOOKUP
-dns default
Definition: netbase.h:29
std::string GetNetworkName(enum Network net)
Definition: netbase.cpp:113
bool LookupHost(const std::string &name, std::vector< CNetAddr > &vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function=g_dns_lookup)
Resolve a host string to its corresponding network addresses.
Definition: netbase.cpp:190
bool HaveNameProxy()
Definition: netbase.cpp:748
bool GetProxy(enum Network net, proxyType &proxyInfoOut)
Definition: netbase.cpp:720
bool SetSocketNoDelay(const SOCKET &hSocket)
Set the TCP_NODELAY flag on a socket.
Definition: netbase.cpp:848
bool ConnectThroughProxy(const proxyType &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:763
std::vector< CNetAddr > WrappedGetAddrInfo(const std::string &name, bool allow_lookup)
Wrapper for getaddrinfo(3).
Definition: netbase.cpp:45
void InterruptSocks5(bool interrupt)
Definition: netbase.cpp:855
std::vector< std::string > GetNetworkNames(bool append_unroutable=false)
Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE.
Definition: netbase.cpp:136
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:634
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:260
std::function< std::unique_ptr< Sock >const CService &)> CreateSock
Socket factory.
Definition: netbase.cpp:620
bool fNameLookup
Definition: netbase.cpp:38
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:786
int nConnectTimeout
Definition: netbase.cpp:37
bool SetNameProxy(const proxyType &addrProxy)
Set the name proxy to use for all connections to nodes specified by a hostname.
Definition: netbase.cpp:730
enum Network ParseNetwork(const std::string &net)
Definition: netbase.cpp:91
bool SetSocketNonBlocking(const SOCKET &hSocket, bool fNonBlocking)
Disable or enable blocking-mode for a socket.
Definition: netbase.cpp:822
bool IsProxy(const CNetAddr &addr)
Definition: netbase.cpp:753
bool Lookup(const std::string &name, std::vector< CService > &vAddr, 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:222
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:859
DNSLookupFn g_dns_lookup
Definition: netbase.cpp:89
std::function< std::vector< CNetAddr >(const std::string &, bool)> DNSLookupFn
Definition: netbase.h:83
static const int DEFAULT_CONNECT_TIMEOUT
-timeout default
Definition: netbase.h:27
bool SetProxy(enum Network net, const proxyType &addrProxy)
Definition: netbase.cpp:710
const char * name
Definition: rest.cpp:49