Bitcoin ABC 0.30.5
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 <type_traits>
22#include <vector>
23
24extern int nConnectTimeout;
25extern bool fNameLookup;
26
28static const int DEFAULT_CONNECT_TIMEOUT = 5000;
30static const int DEFAULT_NAME_LOOKUP = true;
31
33 None = 0,
34 In = (1U << 0),
35 Out = (1U << 1),
36 Both = (In | Out),
37};
38
41 using underlying = typename std::underlying_type<ConnectionDirection>::type;
42 a = ConnectionDirection(underlying(a) | underlying(b));
43 return a;
44}
45
47 using underlying = typename std::underlying_type<ConnectionDirection>::type;
48 return (underlying(a) & underlying(b));
49}
50
51class proxyType {
52public:
54 explicit proxyType(const CService &_proxy,
55 bool _randomize_credentials = false)
56 : proxy(_proxy), randomize_credentials(_randomize_credentials) {}
57
58 bool IsValid() const { return proxy.IsValid(); }
59
62};
63
66 std::string username;
67 std::string password;
68};
69
74std::vector<CNetAddr> WrappedGetAddrInfo(const std::string &name,
75 bool allow_lookup);
76
77enum Network ParseNetwork(const std::string &net);
78std::string GetNetworkName(enum Network net);
83std::vector<std::string> GetNetworkNames(bool append_unroutable = false);
84bool SetProxy(enum Network net, const proxyType &addrProxy);
85bool GetProxy(enum Network net, proxyType &proxyInfoOut);
86bool IsProxy(const CNetAddr &addr);
87
104bool SetNameProxy(const proxyType &addrProxy);
105bool HaveNameProxy();
106bool GetNameProxy(proxyType &nameProxyOut);
107
109 std::function<std::vector<CNetAddr>(const std::string &, bool)>;
111
127bool LookupHost(const std::string &name, std::vector<CNetAddr> &vIP,
128 unsigned int nMaxSolutions, bool fAllowLookup,
129 DNSLookupFn dns_lookup_function = g_dns_lookup);
130
137bool LookupHost(const std::string &name, CNetAddr &addr, bool fAllowLookup,
138 DNSLookupFn dns_lookup_function = g_dns_lookup);
139
160bool Lookup(const std::string &name, std::vector<CService> &vAddr,
161 uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions,
162 DNSLookupFn dns_lookup_function = g_dns_lookup);
163
170bool Lookup(const std::string &name, CService &addr, uint16_t portDefault,
171 bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
172
183CService LookupNumeric(const std::string &name, uint16_t portDefault = 0,
184 DNSLookupFn dns_lookup_function = g_dns_lookup);
185
197bool LookupSubNet(const std::string &strSubnet, CSubNet &subnet,
198 DNSLookupFn dns_lookup_function = g_dns_lookup);
199
207std::unique_ptr<Sock> CreateSockTCP(const CService &address_family);
208
213extern std::function<std::unique_ptr<Sock>(const CService &)> CreateSock;
214
227bool ConnectSocketDirectly(const CService &addrConnect, const Sock &sock,
228 int nTimeout, bool manual_connection);
229
245bool ConnectThroughProxy(const proxyType &proxy, const std::string &strDest,
246 uint16_t port, const Sock &sock, int nTimeout,
247 bool &outProxyConnectionFailed);
248
250bool SetSocketNonBlocking(const SOCKET &hSocket, bool fNonBlocking);
252bool SetSocketNoDelay(const SOCKET &hSocket);
253void InterruptSocks5(bool interrupt);
254
255bool Socks5(const std::string &strDest, uint16_t port,
256 const ProxyCredentials *auth, const Sock &socket);
257
265bool IsBadPort(uint16_t port);
266
267#endif // BITCOIN_NETBASE_H
Network address.
Definition: netaddress.h:121
bool IsValid() const
Definition: netaddress.cpp:477
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:28
bool IsValid() const
Definition: netbase.h:58
CService proxy
Definition: netbase.h:60
proxyType()
Definition: netbase.h:53
bool randomize_credentials
Definition: netbase.h:61
proxyType(const CService &_proxy, bool _randomize_credentials=false)
Definition: netbase.h:54
unsigned int SOCKET
Definition: compat.h:40
Network
A network type.
Definition: netaddress.h:44
ConnectionDirection
Definition: netbase.h:32
bool GetNameProxy(proxyType &nameProxyOut)
Definition: netbase.cpp:734
static const int DEFAULT_NAME_LOOKUP
-dns default
Definition: netbase.h:30
std::string GetNetworkName(enum Network net)
Definition: netbase.cpp:114
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:191
bool HaveNameProxy()
Definition: netbase.cpp:743
bool GetProxy(enum Network net, proxyType &proxyInfoOut)
Definition: netbase.cpp:715
bool SetSocketNoDelay(const SOCKET &hSocket)
Set the TCP_NODELAY flag on a socket.
Definition: netbase.cpp:843
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:758
std::vector< std::string > GetNetworkNames(bool append_unroutable=false)
Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE.
Definition: netbase.cpp:137
void InterruptSocks5(bool interrupt)
Definition: netbase.cpp:850
std::unique_ptr< Sock > CreateSockTCP(const CService &address_family)
Create a TCP socket in the given address family.
Definition: netbase.cpp:567
std::function< std::unique_ptr< Sock >(const CService &)> CreateSock
Socket factory.
Definition: netbase.cpp:615
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:629
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:261
static bool operator&(ConnectionDirection a, ConnectionDirection b)
Definition: netbase.h:46
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:781
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:725
static ConnectionDirection & operator|=(ConnectionDirection &a, ConnectionDirection b)
Definition: netbase.h:39
enum Network ParseNetwork(const std::string &net)
Definition: netbase.cpp:92
bool SetSocketNonBlocking(const SOCKET &hSocket, bool fNonBlocking)
Disable or enable blocking-mode for a socket.
Definition: netbase.cpp:817
bool IsProxy(const CNetAddr &addr)
Definition: netbase.cpp:748
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:223
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:854
std::vector< CNetAddr > WrappedGetAddrInfo(const std::string &name, bool allow_lookup)
Wrapper for getaddrinfo(3).
Definition: netbase.cpp:46
DNSLookupFn g_dns_lookup
Definition: netbase.cpp:90
std::function< std::vector< CNetAddr >(const std::string &, bool)> DNSLookupFn
Definition: netbase.h:109
static const int DEFAULT_CONNECT_TIMEOUT
-timeout default
Definition: netbase.h:28
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:421
bool SetProxy(enum Network net, const proxyType &addrProxy)
Definition: netbase.cpp:705
const char * name
Definition: rest.cpp:47
Credentials for proxy authentication.
Definition: netbase.h:65
std::string username
Definition: netbase.h:66
std::string password
Definition: netbase.h:67