Bitcoin Core 22.99.0
P2P Digital Currency
netbase.h
Go to the documentation of this file.
1// Copyright (c) 2009-2019 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.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 <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};
39 using underlying = typename std::underlying_type<ConnectionDirection>::type;
40 a = ConnectionDirection(underlying(a) | underlying(b));
41 return a;
42}
44 using underlying = typename std::underlying_type<ConnectionDirection>::type;
45 return (underlying(a) & underlying(b));
46}
47
49{
50public:
52 explicit proxyType(const CService &_proxy, bool _randomize_credentials=false): proxy(_proxy), randomize_credentials(_randomize_credentials) {}
53
54 bool IsValid() const { return proxy.IsValid(); }
55
58};
59
62{
63 std::string username;
64 std::string password;
65};
66
70std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup);
71
72enum Network ParseNetwork(const std::string& net);
73std::string GetNetworkName(enum Network net);
75std::vector<std::string> GetNetworkNames(bool append_unroutable = false);
76bool SetProxy(enum Network net, const proxyType &addrProxy);
77bool GetProxy(enum Network net, proxyType &proxyInfoOut);
78bool IsProxy(const CNetAddr &addr);
95bool SetNameProxy(const proxyType &addrProxy);
96bool HaveNameProxy();
97bool GetNameProxy(proxyType &nameProxyOut);
98
99using DNSLookupFn = std::function<std::vector<CNetAddr>(const std::string&, bool)>;
101
117bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
118
125bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
126
147bool Lookup(const std::string& name, std::vector<CService>& vAddr, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function = g_dns_lookup);
148
155bool Lookup(const std::string& name, CService& addr, uint16_t portDefault, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
156
166CService LookupNumeric(const std::string& name, uint16_t portDefault = 0, DNSLookupFn dns_lookup_function = g_dns_lookup);
167
178bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet, DNSLookupFn dns_lookup_function = g_dns_lookup);
179
185std::unique_ptr<Sock> CreateSockTCP(const CService& address_family);
186
190extern std::function<std::unique_ptr<Sock>(const CService&)> CreateSock;
191
204bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nTimeout, bool manual_connection);
205
221bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed);
222
224bool SetSocketNonBlocking(const SOCKET& hSocket, bool fNonBlocking);
226bool SetSocketNoDelay(const SOCKET& hSocket);
227void InterruptSocks5(bool interrupt);
228
247bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* auth, const Sock& socket);
248
249#endif // BITCOIN_NETBASE_H
Network address.
Definition: netaddress.h:119
bool IsValid() const
Definition: netaddress.cpp:451
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:523
RAII helper class that manages a socket.
Definition: sock.h:26
bool IsValid() const
Definition: netbase.h:54
CService proxy
Definition: netbase.h:56
proxyType()
Definition: netbase.h:51
bool randomize_credentials
Definition: netbase.h:57
proxyType(const CService &_proxy, bool _randomize_credentials=false)
Definition: netbase.h:52
unsigned int SOCKET
Definition: compat.h:41
Network
A network type.
Definition: netaddress.h:45
ConnectionDirection
Definition: netbase.h:32
bool GetNameProxy(proxyType &nameProxyOut)
Definition: netbase.cpp:634
static const int DEFAULT_NAME_LOOKUP
-dns default
Definition: netbase.h:30
std::string GetNetworkName(enum Network net)
Definition: netbase.cpp:105
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:170
bool HaveNameProxy()
Definition: netbase.cpp:642
bool GetProxy(enum Network net, proxyType &proxyInfoOut)
Definition: netbase.cpp:617
bool SetSocketNoDelay(const SOCKET &hSocket)
Set the TCP_NODELAY flag on a socket.
Definition: netbase.cpp:747
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:656
std::function< std::unique_ptr< Sock >(const CService &)> CreateSock
Socket factory.
Definition: netbase.cpp:529
std::vector< std::string > GetNetworkNames(bool append_unroutable=false)
Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE.
Definition: netbase.cpp:121
void InterruptSocks5(bool interrupt)
Definition: netbase.cpp:754
std::unique_ptr< Sock > CreateSockTCP(const CService &address_family)
Create a TCP socket in the given address family.
Definition: netbase.cpp:486
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:541
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:230
std::function< std::vector< CNetAddr >(const std::string &, bool)> DNSLookupFn
Definition: netbase.h:99
static bool operator&(ConnectionDirection a, ConnectionDirection b)
Definition: netbase.h:43
bool fNameLookup
Definition: netbase.cpp:37
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:679
int nConnectTimeout
Definition: netbase.cpp:36
bool SetNameProxy(const proxyType &addrProxy)
Set the name proxy to use for all connections to nodes specified by a hostname.
Definition: netbase.cpp:626
static ConnectionDirection & operator|=(ConnectionDirection &a, ConnectionDirection b)
Definition: netbase.h:38
enum Network ParseNetwork(const std::string &net)
Definition: netbase.cpp:87
bool SetSocketNonBlocking(const SOCKET &hSocket, bool fNonBlocking)
Disable or enable blocking-mode for a socket.
Definition: netbase.cpp:720
bool IsProxy(const CNetAddr &addr)
Definition: netbase.cpp:647
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:198
std::vector< CNetAddr > WrappedGetAddrInfo(const std::string &name, bool allow_lookup)
Wrapper for getaddrinfo(3).
Definition: netbase.cpp:43
DNSLookupFn g_dns_lookup
Definition: netbase.cpp:85
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:367
bool SetProxy(enum Network net, const proxyType &addrProxy)
Definition: netbase.cpp:608
const char * name
Definition: rest.cpp:43
Credentials for proxy authentication.
Definition: netbase.h:62
std::string username
Definition: netbase.h:63
std::string password
Definition: netbase.h:64