2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2012-01-03 23:33:31 +01:00
|
|
|
#ifndef BITCOIN_NETBASE_H
|
|
|
|
#define BITCOIN_NETBASE_H
|
|
|
|
|
2013-05-28 01:55:01 +02:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
2022-08-02 18:34:58 +02:00
|
|
|
#include <config/bitcoin-config.h>
|
2013-05-28 01:55:01 +02:00
|
|
|
#endif
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <compat.h>
|
|
|
|
#include <netaddress.h>
|
|
|
|
#include <serialize.h>
|
2022-10-26 13:25:11 +02:00
|
|
|
#include <util/sock.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2022-10-26 13:25:11 +02:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <stdint.h>
|
2012-01-03 23:33:31 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
extern int nConnectTimeout;
|
2014-05-31 12:04:34 +02:00
|
|
|
extern bool fNameLookup;
|
2012-01-03 23:33:31 +01:00
|
|
|
|
2015-11-09 19:16:38 +01:00
|
|
|
//! -timeout default
|
2014-09-25 09:01:54 +02:00
|
|
|
static const int DEFAULT_CONNECT_TIMEOUT = 5000;
|
2015-11-09 19:16:38 +01:00
|
|
|
//! -dns default
|
|
|
|
static const int DEFAULT_NAME_LOOKUP = true;
|
2017-12-20 12:45:01 +01:00
|
|
|
static const bool DEFAULT_ALLOWPRIVATENET = false;
|
2012-01-03 23:33:31 +01:00
|
|
|
|
2015-03-16 16:30:49 +01:00
|
|
|
class proxyType
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
proxyType(): randomize_credentials(false) {}
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit proxyType(const CService &_proxy, bool _randomize_credentials=false): proxy(_proxy), randomize_credentials(_randomize_credentials) {}
|
2015-03-16 16:30:49 +01:00
|
|
|
|
|
|
|
bool IsValid() const { return proxy.IsValid(); }
|
|
|
|
|
|
|
|
CService proxy;
|
|
|
|
bool randomize_credentials;
|
|
|
|
};
|
2012-09-23 12:55:05 +02:00
|
|
|
|
2020-06-07 18:59:46 +02:00
|
|
|
/** Credentials for proxy authentication */
|
|
|
|
struct ProxyCredentials
|
|
|
|
{
|
|
|
|
std::string username;
|
|
|
|
std::string password;
|
|
|
|
};
|
|
|
|
|
2020-06-29 11:44:12 +02:00
|
|
|
/**
|
|
|
|
* Wrapper for getaddrinfo(3). Do not use directly: call Lookup/LookupHost/LookupNumeric/LookupSubNet.
|
|
|
|
*/
|
|
|
|
std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup);
|
|
|
|
|
2019-08-07 06:42:54 +02:00
|
|
|
enum Network ParseNetwork(const std::string& net);
|
2014-07-30 15:32:36 +02:00
|
|
|
std::string GetNetworkName(enum Network net);
|
2021-01-19 15:35:56 +01:00
|
|
|
/** Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE. */
|
|
|
|
std::vector<std::string> GetNetworkNames(bool append_unroutable = false);
|
2015-03-16 16:30:49 +01:00
|
|
|
bool SetProxy(enum Network net, const proxyType &addrProxy);
|
2012-09-23 12:55:05 +02:00
|
|
|
bool GetProxy(enum Network net, proxyType &proxyInfoOut);
|
2012-05-24 19:02:21 +02:00
|
|
|
bool IsProxy(const CNetAddr &addr);
|
2021-03-15 16:24:10 +01:00
|
|
|
/**
|
|
|
|
* Set the name proxy to use for all connections to nodes specified by a
|
|
|
|
* hostname. After setting this proxy, connecting to a node specified by a
|
|
|
|
* hostname won't result in a local lookup of said hostname, rather, connect to
|
|
|
|
* the node by asking the name proxy for a proxy connection to the hostname,
|
|
|
|
* effectively delegating the hostname lookup to the specified proxy.
|
|
|
|
*
|
|
|
|
* This delegation increases privacy for those who set the name proxy as they no
|
|
|
|
* longer leak their external hostname queries to their DNS servers.
|
|
|
|
*
|
|
|
|
* @returns Whether or not the operation succeeded.
|
|
|
|
*
|
|
|
|
* @note SOCKS5's support for UDP-over-SOCKS5 has been considered, but no SOCK5
|
|
|
|
* server in common use (most notably Tor) actually implements UDP
|
|
|
|
* support, and a DNS resolver is beyond the scope of this project.
|
|
|
|
*/
|
2015-03-16 16:30:49 +01:00
|
|
|
bool SetNameProxy(const proxyType &addrProxy);
|
2012-09-23 12:55:05 +02:00
|
|
|
bool HaveNameProxy();
|
2017-09-28 17:02:53 +02:00
|
|
|
bool GetNameProxy(proxyType &nameProxyOut);
|
2020-06-29 11:44:12 +02:00
|
|
|
|
|
|
|
using DNSLookupFn = std::function<std::vector<CNetAddr>(const std::string&, bool)>;
|
|
|
|
extern DNSLookupFn g_dns_lookup;
|
|
|
|
|
2021-03-15 16:24:10 +01:00
|
|
|
/**
|
|
|
|
* Resolve a host string to its corresponding network addresses.
|
|
|
|
*
|
|
|
|
* @param name The string representing a host. Could be a name or a numerical
|
|
|
|
* IP address (IPv6 addresses in their bracketed form are
|
|
|
|
* allowed).
|
|
|
|
* @param[out] vIP The resulting network addresses to which the specified host
|
|
|
|
* string resolved.
|
|
|
|
*
|
|
|
|
* @returns Whether or not the specified host string successfully resolved to
|
|
|
|
* any resulting network addresses.
|
|
|
|
*
|
2021-03-01 21:35:28 +01:00
|
|
|
* @see Lookup(const std::string&, std::vector<CService>&, uint16_t, bool, unsigned int, DNSLookupFn)
|
2021-03-15 16:24:10 +01:00
|
|
|
* for additional parameter descriptions.
|
|
|
|
*/
|
2020-06-29 11:44:12 +02:00
|
|
|
bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
|
2021-03-15 16:24:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve a host string to its first corresponding network address.
|
|
|
|
*
|
2021-03-01 21:35:28 +01:00
|
|
|
* @see LookupHost(const std::string&, std::vector<CNetAddr>&, uint16_t, bool, DNSLookupFn)
|
2021-03-15 16:24:10 +01:00
|
|
|
* for additional parameter descriptions.
|
|
|
|
*/
|
2020-06-29 11:44:12 +02:00
|
|
|
bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
|
2021-03-15 16:24:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve a service string to its corresponding service.
|
|
|
|
*
|
|
|
|
* @param name The string representing a service. Could be a name or a
|
|
|
|
* numerical IP address (IPv6 addresses should be in their
|
2021-03-01 21:35:28 +01:00
|
|
|
* disambiguated bracketed form), optionally followed by a uint16_t port
|
2021-03-15 16:24:10 +01:00
|
|
|
* number. (e.g. example.com:8333 or
|
|
|
|
* [2001:db8:85a3:8d3:1319:8a2e:370:7348]:420)
|
|
|
|
* @param[out] vAddr The resulting services to which the specified service string
|
|
|
|
* resolved.
|
|
|
|
* @param portDefault The default port for resulting services if not specified
|
|
|
|
* by the service string.
|
|
|
|
* @param fAllowLookup Whether or not hostname lookups are permitted. If yes,
|
|
|
|
* external queries may be performed.
|
|
|
|
* @param nMaxSolutions The maximum number of results we want, specifying 0
|
|
|
|
* means "as many solutions as we get."
|
|
|
|
*
|
|
|
|
* @returns Whether or not the service string successfully resolved to any
|
|
|
|
* resulting services.
|
|
|
|
*/
|
2021-03-01 21:35:28 +01:00
|
|
|
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);
|
2021-03-15 16:24:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve a service string to its first corresponding service.
|
|
|
|
*
|
2021-03-01 21:35:28 +01:00
|
|
|
* @see Lookup(const std::string&, std::vector<CService>&, uint16_t, bool, unsigned int, DNSLookupFn)
|
2021-03-15 16:24:10 +01:00
|
|
|
* for additional parameter descriptions.
|
|
|
|
*/
|
2021-03-01 21:35:28 +01:00
|
|
|
bool Lookup(const std::string& name, CService& addr, uint16_t portDefault, bool fAllowLookup, DNSLookupFn dns_lookup_function = g_dns_lookup);
|
2021-03-15 16:24:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve a service string with a numeric IP to its first corresponding
|
|
|
|
* service.
|
|
|
|
*
|
|
|
|
* @returns The resulting CService if the resolution was successful, [::]:0 otherwise.
|
|
|
|
*
|
2021-03-01 21:35:28 +01:00
|
|
|
* @see Lookup(const std::string&, std::vector<CService>&, uint16_t, bool, unsigned int, DNSLookupFn)
|
2021-03-15 16:24:10 +01:00
|
|
|
* for additional parameter descriptions.
|
|
|
|
*/
|
2021-03-01 21:35:28 +01:00
|
|
|
CService LookupNumeric(const std::string& name, uint16_t portDefault = 0, DNSLookupFn dns_lookup_function = g_dns_lookup);
|
2021-03-15 16:24:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse and resolve a specified subnet string into the appropriate internal
|
|
|
|
* representation.
|
|
|
|
*
|
|
|
|
* @param strSubnet A string representation of a subnet of the form `network
|
|
|
|
* address [ "/", ( CIDR-style suffix | netmask ) ]`(e.g.
|
|
|
|
* `2001:db8::/32`, `192.0.2.0/255.255.255.0`, or `8.8.8.8`).
|
|
|
|
* @param ret The resulting internal representation of a subnet.
|
|
|
|
*
|
|
|
|
* @returns Whether the operation succeeded or not.
|
|
|
|
*/
|
2020-06-29 11:44:12 +02:00
|
|
|
bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet, DNSLookupFn dns_lookup_function = g_dns_lookup);
|
2022-10-26 13:25:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a TCP socket in the given address family.
|
|
|
|
* @param[in] address_family The socket is created in the same address family as this address.
|
|
|
|
* @return pointer to the created Sock object or unique_ptr that owns nothing in case of failure
|
|
|
|
*/
|
|
|
|
std::unique_ptr<Sock> CreateSockTCP(const CService& address_family);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Socket factory. Defaults to `CreateSockTCP()`, but can be overridden by unit tests.
|
|
|
|
*/
|
|
|
|
extern std::function<std::unique_ptr<Sock>(const CService&)> CreateSock;
|
|
|
|
|
2021-03-15 16:24:10 +01:00
|
|
|
/**
|
|
|
|
* Try to connect to the specified service on the specified socket.
|
|
|
|
*
|
|
|
|
* @param addrConnect The service to which to connect.
|
2023-07-14 17:11:45 +02:00
|
|
|
* @param sock The socket on which to connect.
|
2021-03-15 16:24:10 +01:00
|
|
|
* @param nTimeout Wait this many milliseconds for the connection to be
|
|
|
|
* established.
|
|
|
|
* @param manual_connection Whether or not the connection was manually requested
|
|
|
|
* (e.g. through the addnode RPC)
|
|
|
|
*
|
|
|
|
* @returns Whether or not a connection was successfully made.
|
|
|
|
*/
|
2023-07-14 17:11:45 +02:00
|
|
|
bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nTimeout, bool manual_connection);
|
2021-03-15 16:24:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to a specified destination service through a SOCKS5 proxy by first
|
|
|
|
* connecting to the SOCKS5 proxy.
|
|
|
|
*
|
|
|
|
* @param proxy The SOCKS5 proxy.
|
|
|
|
* @param strDest The destination service to which to connect.
|
|
|
|
* @param port The destination port.
|
|
|
|
* @param sock The socket on which to connect to the SOCKS5 proxy.
|
|
|
|
* @param nTimeout Wait this many milliseconds for the connection to the SOCKS5
|
|
|
|
* proxy to be established.
|
|
|
|
* @param[out] outProxyConnectionFailed Whether or not the connection to the
|
|
|
|
* SOCKS5 proxy failed.
|
|
|
|
*
|
|
|
|
* @returns Whether or not the operation succeeded.
|
|
|
|
*/
|
2021-03-01 21:35:28 +01:00
|
|
|
bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed);
|
2021-03-15 16:24:10 +01:00
|
|
|
|
2014-07-09 11:00:00 +02:00
|
|
|
/** Disable or enable blocking-mode for a socket */
|
2017-07-24 14:58:25 +02:00
|
|
|
bool SetSocketNonBlocking(const SOCKET& hSocket, bool fNonBlocking);
|
2017-05-18 02:26:54 +02:00
|
|
|
/** Set the TCP_NODELAY flag on a socket */
|
2017-07-24 14:58:25 +02:00
|
|
|
bool SetSocketNoDelay(const SOCKET& hSocket);
|
2017-08-09 18:06:31 +02:00
|
|
|
void InterruptSocks5(bool interrupt);
|
2012-01-03 23:33:31 +01:00
|
|
|
|
2021-03-15 16:24:10 +01:00
|
|
|
/**
|
|
|
|
* Connect to a specified destination service through an already connected
|
|
|
|
* SOCKS5 proxy.
|
|
|
|
*
|
|
|
|
* @param strDest The destination fully-qualified domain name.
|
|
|
|
* @param port The destination port.
|
|
|
|
* @param auth The credentials with which to authenticate with the specified
|
|
|
|
* SOCKS5 proxy.
|
|
|
|
* @param sock The SOCKS5 proxy socket.
|
|
|
|
*
|
|
|
|
* @returns Whether or not the operation succeeded.
|
|
|
|
*
|
|
|
|
* @note The specified SOCKS5 proxy socket must already be connected to the
|
|
|
|
* SOCKS5 proxy.
|
|
|
|
*
|
|
|
|
* @see <a href="https://www.ietf.org/rfc/rfc1928.txt">RFC1928: SOCKS Protocol
|
|
|
|
* Version 5</a>
|
|
|
|
*/
|
2021-03-01 21:35:28 +01:00
|
|
|
bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* auth, const Sock& socket);
|
2020-06-07 18:59:46 +02:00
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_NETBASE_H
|