aboutsummaryrefslogtreecommitdiff
path: root/src/netbase.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/netbase.cpp')
-rw-r--r--src/netbase.cpp360
1 files changed, 108 insertions, 252 deletions
diff --git a/src/netbase.cpp b/src/netbase.cpp
index 0c5b3a220e..2980bdf459 100644
--- a/src/netbase.cpp
+++ b/src/netbase.cpp
@@ -5,6 +5,7 @@
#include <netbase.h>
+#include <compat.h>
#include <sync.h>
#include <tinyformat.h>
#include <util/sock.h>
@@ -14,6 +15,7 @@
#include <util/time.h>
#include <atomic>
+#include <chrono>
#include <cstdint>
#include <functional>
#include <limits>
@@ -29,10 +31,6 @@
#include <poll.h>
#endif
-#if !defined(MSG_NOSIGNAL)
-#define MSG_NOSIGNAL 0
-#endif
-
// Settings
static Mutex g_proxyinfo_mutex;
static proxyType proxyInfo[NET_MAX] GUARDED_BY(g_proxyinfo_mutex);
@@ -41,9 +39,53 @@ int nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
bool fNameLookup = DEFAULT_NAME_LOOKUP;
// Need ample time for negotiation for very slow proxies such as Tor (milliseconds)
-static const int SOCKS5_RECV_TIMEOUT = 20 * 1000;
+int g_socks5_recv_timeout = 20 * 1000;
static std::atomic<bool> interruptSocks5Recv(false);
+std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup)
+{
+ addrinfo ai_hint{};
+ // We want a TCP port, which is a streaming socket type
+ ai_hint.ai_socktype = SOCK_STREAM;
+ ai_hint.ai_protocol = IPPROTO_TCP;
+ // We don't care which address family (IPv4 or IPv6) is returned
+ ai_hint.ai_family = AF_UNSPEC;
+ // If we allow lookups of hostnames, use the AI_ADDRCONFIG flag to only
+ // return addresses whose family we have an address configured for.
+ //
+ // If we don't allow lookups, then use the AI_NUMERICHOST flag for
+ // getaddrinfo to only decode numerical network addresses and suppress
+ // hostname lookups.
+ ai_hint.ai_flags = allow_lookup ? AI_ADDRCONFIG : AI_NUMERICHOST;
+
+ addrinfo* ai_res{nullptr};
+ const int n_err{getaddrinfo(name.c_str(), nullptr, &ai_hint, &ai_res)};
+ if (n_err != 0) {
+ return {};
+ }
+
+ // Traverse the linked list starting with ai_trav.
+ addrinfo* ai_trav{ai_res};
+ std::vector<CNetAddr> resolved_addresses;
+ while (ai_trav != nullptr) {
+ if (ai_trav->ai_family == AF_INET) {
+ assert(ai_trav->ai_addrlen >= sizeof(sockaddr_in));
+ resolved_addresses.emplace_back(reinterpret_cast<sockaddr_in*>(ai_trav->ai_addr)->sin_addr);
+ }
+ if (ai_trav->ai_family == AF_INET6) {
+ assert(ai_trav->ai_addrlen >= sizeof(sockaddr_in6));
+ const sockaddr_in6* s6{reinterpret_cast<sockaddr_in6*>(ai_trav->ai_addr)};
+ resolved_addresses.emplace_back(s6->sin6_addr, s6->sin6_scope_id);
+ }
+ ai_trav = ai_trav->ai_next;
+ }
+ freeaddrinfo(ai_res);
+
+ return resolved_addresses;
+}
+
+DNSLookupFn g_dns_lookup{WrappedGetAddrInfo};
+
enum Network ParseNetwork(const std::string& net_in) {
std::string net = ToLower(net_in);
if (net == "ipv4") return NET_IPV4;
@@ -53,6 +95,9 @@ enum Network ParseNetwork(const std::string& net_in) {
LogPrintf("Warning: net name 'tor' is deprecated and will be removed in the future. You should use 'onion' instead.\n");
return NET_ONION;
}
+ if (net == "i2p") {
+ return NET_I2P;
+ }
return NET_UNROUTABLE;
}
@@ -77,7 +122,7 @@ std::vector<std::string> GetNetworkNames(bool append_unroutable)
std::vector<std::string> names;
for (int n = 0; n < NET_MAX; ++n) {
const enum Network network{static_cast<Network>(n)};
- if (network == NET_UNROUTABLE || network == NET_I2P || network == NET_CJDNS || network == NET_INTERNAL) continue;
+ if (network == NET_UNROUTABLE || network == NET_CJDNS || network == NET_INTERNAL) continue;
names.emplace_back(GetNetworkName(network));
}
if (append_unroutable) {
@@ -86,7 +131,7 @@ std::vector<std::string> GetNetworkNames(bool append_unroutable)
return names;
}
-bool static LookupIntern(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
+static bool LookupIntern(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
{
vIP.clear();
@@ -108,73 +153,20 @@ bool static LookupIntern(const std::string& name, std::vector<CNetAddr>& vIP, un
}
}
- struct addrinfo aiHint;
- memset(&aiHint, 0, sizeof(struct addrinfo));
-
- // We want a TCP port, which is a streaming socket type
- aiHint.ai_socktype = SOCK_STREAM;
- aiHint.ai_protocol = IPPROTO_TCP;
- // We don't care which address family (IPv4 or IPv6) is returned
- aiHint.ai_family = AF_UNSPEC;
- // If we allow lookups of hostnames, use the AI_ADDRCONFIG flag to only
- // return addresses whose family we have an address configured for.
- //
- // If we don't allow lookups, then use the AI_NUMERICHOST flag for
- // getaddrinfo to only decode numerical network addresses and suppress
- // hostname lookups.
- aiHint.ai_flags = fAllowLookup ? AI_ADDRCONFIG : AI_NUMERICHOST;
- struct addrinfo *aiRes = nullptr;
- int nErr = getaddrinfo(name.c_str(), nullptr, &aiHint, &aiRes);
- if (nErr)
- return false;
-
- // Traverse the linked list starting with aiTrav, add all non-internal
- // IPv4,v6 addresses to vIP while respecting nMaxSolutions.
- struct addrinfo *aiTrav = aiRes;
- while (aiTrav != nullptr && (nMaxSolutions == 0 || vIP.size() < nMaxSolutions))
- {
- CNetAddr resolved;
- if (aiTrav->ai_family == AF_INET)
- {
- assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in));
- resolved = CNetAddr(((struct sockaddr_in*)(aiTrav->ai_addr))->sin_addr);
- }
-
- if (aiTrav->ai_family == AF_INET6)
- {
- assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in6));
- struct sockaddr_in6* s6 = (struct sockaddr_in6*) aiTrav->ai_addr;
- resolved = CNetAddr(s6->sin6_addr, s6->sin6_scope_id);
+ for (const CNetAddr& resolved : dns_lookup_function(name, fAllowLookup)) {
+ if (nMaxSolutions > 0 && vIP.size() >= nMaxSolutions) {
+ break;
}
/* Never allow resolving to an internal address. Consider any such result invalid */
if (!resolved.IsInternal()) {
vIP.push_back(resolved);
}
-
- aiTrav = aiTrav->ai_next;
}
- freeaddrinfo(aiRes);
-
return (vIP.size() > 0);
}
-/**
- * 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.
- *
- * @see Lookup(const char *, std::vector<CService>&, int, bool, unsigned int)
- * for additional parameter descriptions.
- */
-bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
+bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
{
if (!ValidAsCString(name)) {
return false;
@@ -186,59 +178,33 @@ bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned in
strHost = strHost.substr(1, strHost.size() - 2);
}
- return LookupIntern(strHost, vIP, nMaxSolutions, fAllowLookup);
+ return LookupIntern(strHost, vIP, nMaxSolutions, fAllowLookup, dns_lookup_function);
}
- /**
- * Resolve a host string to its first corresponding network address.
- *
- * @see LookupHost(const std::string&, std::vector<CNetAddr>&, unsigned int, bool) for
- * additional parameter descriptions.
- */
-bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup)
+bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup, DNSLookupFn dns_lookup_function)
{
if (!ValidAsCString(name)) {
return false;
}
std::vector<CNetAddr> vIP;
- LookupHost(name, vIP, 1, fAllowLookup);
+ LookupHost(name, vIP, 1, fAllowLookup, dns_lookup_function);
if(vIP.empty())
return false;
addr = vIP.front();
return true;
}
-/**
- * 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
- * disambiguated bracketed form), optionally followed by a port
- * 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.
- */
-bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions)
+bool Lookup(const std::string& name, std::vector<CService>& vAddr, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function)
{
if (name.empty() || !ValidAsCString(name)) {
return false;
}
- int port = portDefault;
+ uint16_t port{portDefault};
std::string hostname;
SplitHostPort(name, port, hostname);
std::vector<CNetAddr> vIP;
- bool fRet = LookupIntern(hostname, vIP, nMaxSolutions, fAllowLookup);
+ bool fRet = LookupIntern(hostname, vIP, nMaxSolutions, fAllowLookup, dns_lookup_function);
if (!fRet)
return false;
vAddr.resize(vIP.size());
@@ -247,36 +213,20 @@ bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefau
return true;
}
-/**
- * Resolve a service string to its first corresponding service.
- *
- * @see Lookup(const char *, std::vector<CService>&, int, bool, unsigned int)
- * for additional parameter descriptions.
- */
-bool Lookup(const std::string& name, CService& addr, int portDefault, bool fAllowLookup)
+bool Lookup(const std::string& name, CService& addr, uint16_t portDefault, bool fAllowLookup, DNSLookupFn dns_lookup_function)
{
if (!ValidAsCString(name)) {
return false;
}
std::vector<CService> vService;
- bool fRet = Lookup(name, vService, portDefault, fAllowLookup, 1);
+ bool fRet = Lookup(name, vService, portDefault, fAllowLookup, 1, dns_lookup_function);
if (!fRet)
return false;
addr = vService[0];
return true;
}
-/**
- * Resolve a service string with a numeric IP to its first corresponding
- * service.
- *
- * @returns The resulting CService if the resolution was successful, [::]:0
- * otherwise.
- *
- * @see Lookup(const char *, CService&, int, bool) for additional parameter
- * descriptions.
- */
-CService LookupNumeric(const std::string& name, int portDefault)
+CService LookupNumeric(const std::string& name, uint16_t portDefault, DNSLookupFn dns_lookup_function)
{
if (!ValidAsCString(name)) {
return {};
@@ -284,7 +234,7 @@ CService LookupNumeric(const std::string& name, int portDefault)
CService addr;
// "1.2:345" will fail to resolve the ip, but will still set the port.
// If the ip fails to resolve, re-init the result.
- if(!Lookup(name, addr, portDefault, false))
+ if(!Lookup(name, addr, portDefault, false, dns_lookup_function))
addr = CService();
return addr;
}
@@ -360,9 +310,6 @@ static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, c
{
int64_t curTime = GetTimeMillis();
int64_t endTime = curTime + timeout;
- // Maximum time to wait for I/O readiness. It will take up until this time
- // (in millis) to break off in case of an interruption.
- const int64_t maxWait = 1000;
while (len > 0 && curTime < endTime) {
ssize_t ret = sock.Recv(data, len, 0); // Optimistically try the recv first
if (ret > 0) {
@@ -373,10 +320,11 @@ static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, c
} else { // Other error or blocking
int nErr = WSAGetLastError();
if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL) {
- // Only wait at most maxWait milliseconds at a time, unless
+ // Only wait at most MAX_WAIT_FOR_IO at a time, unless
// we're approaching the end of the specified total timeout
- int timeout_ms = std::min(endTime - curTime, maxWait);
- if (!sock.Wait(std::chrono::milliseconds{timeout_ms}, Sock::RECV)) {
+ const auto remaining = std::chrono::milliseconds{endTime - curTime};
+ const auto timeout = std::min(remaining, std::chrono::milliseconds{MAX_WAIT_FOR_IO});
+ if (!sock.Wait(timeout, Sock::RECV)) {
return IntrRecvError::NetworkError;
}
} else {
@@ -390,13 +338,6 @@ static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, c
return len == 0 ? IntrRecvError::OK : IntrRecvError::Timeout;
}
-/** Credentials for proxy authentication */
-struct ProxyCredentials
-{
- std::string username;
- std::string password;
-};
-
/** Convert SOCKS5 reply to an error message */
static std::string Socks5ErrorString(uint8_t err)
{
@@ -422,25 +363,7 @@ static std::string Socks5ErrorString(uint8_t err)
}
}
-/**
- * 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>
- */
-static bool Socks5(const std::string& strDest, int port, const ProxyCredentials* auth, const Sock& sock)
+bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* auth, const Sock& sock)
{
IntrRecvError recvr;
LogPrint(BCLog::NET, "SOCKS5 connecting %s\n", strDest);
@@ -463,7 +386,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials*
return error("Error sending to proxy");
}
uint8_t pchRet1[2];
- if ((recvr = InterruptibleRecv(pchRet1, 2, SOCKS5_RECV_TIMEOUT, sock)) != IntrRecvError::OK) {
+ if ((recvr = InterruptibleRecv(pchRet1, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
LogPrintf("Socks5() connect to %s:%d failed: InterruptibleRecv() timeout or other failure\n", strDest, port);
return false;
}
@@ -486,7 +409,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials*
}
LogPrint(BCLog::PROXY, "SOCKS5 sending proxy authentication %s:%s\n", auth->username, auth->password);
uint8_t pchRetA[2];
- if ((recvr = InterruptibleRecv(pchRetA, 2, SOCKS5_RECV_TIMEOUT, sock)) != IntrRecvError::OK) {
+ if ((recvr = InterruptibleRecv(pchRetA, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
return error("Error reading proxy authentication response");
}
if (pchRetA[0] != 0x01 || pchRetA[1] != 0x00) {
@@ -511,7 +434,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials*
return error("Error sending to proxy");
}
uint8_t pchRet2[4];
- if ((recvr = InterruptibleRecv(pchRet2, 4, SOCKS5_RECV_TIMEOUT, sock)) != IntrRecvError::OK) {
+ if ((recvr = InterruptibleRecv(pchRet2, 4, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
if (recvr == IntrRecvError::Timeout) {
/* If a timeout happens here, this effectively means we timed out while connecting
* to the remote node. This is very common for Tor, so do not print an
@@ -535,16 +458,16 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials*
uint8_t pchRet3[256];
switch (pchRet2[3])
{
- case SOCKS5Atyp::IPV4: recvr = InterruptibleRecv(pchRet3, 4, SOCKS5_RECV_TIMEOUT, sock); break;
- case SOCKS5Atyp::IPV6: recvr = InterruptibleRecv(pchRet3, 16, SOCKS5_RECV_TIMEOUT, sock); break;
+ case SOCKS5Atyp::IPV4: recvr = InterruptibleRecv(pchRet3, 4, g_socks5_recv_timeout, sock); break;
+ case SOCKS5Atyp::IPV6: recvr = InterruptibleRecv(pchRet3, 16, g_socks5_recv_timeout, sock); break;
case SOCKS5Atyp::DOMAINNAME:
{
- recvr = InterruptibleRecv(pchRet3, 1, SOCKS5_RECV_TIMEOUT, sock);
+ recvr = InterruptibleRecv(pchRet3, 1, g_socks5_recv_timeout, sock);
if (recvr != IntrRecvError::OK) {
return error("Error reading from proxy");
}
int nRecv = pchRet3[0];
- recvr = InterruptibleRecv(pchRet3, nRecv, SOCKS5_RECV_TIMEOUT, sock);
+ recvr = InterruptibleRecv(pchRet3, nRecv, g_socks5_recv_timeout, sock);
break;
}
default: return error("Error: malformed proxy response");
@@ -552,7 +475,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials*
if (recvr != IntrRecvError::OK) {
return error("Error reading from proxy");
}
- if ((recvr = InterruptibleRecv(pchRet3, 2, SOCKS5_RECV_TIMEOUT, sock)) != IntrRecvError::OK) {
+ if ((recvr = InterruptibleRecv(pchRet3, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
return error("Error reading from proxy");
}
LogPrint(BCLog::NET, "SOCKS5 connected %s\n", strDest);
@@ -614,24 +537,12 @@ static void LogConnectFailure(bool manual_connection, const char* fmt, const Arg
}
}
-/**
- * Try to connect to the specified service on the specified socket.
- *
- * @param addrConnect The service to which to connect.
- * @param hSocket The socket on which to connect.
- * @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.
- */
-bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocket, int nTimeout, bool manual_connection)
+bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nTimeout, bool manual_connection)
{
// Create a sockaddr from the specified service.
struct sockaddr_storage sockaddr;
socklen_t len = sizeof(sockaddr);
- if (hSocket == INVALID_SOCKET) {
+ if (sock.Get() == INVALID_SOCKET) {
LogPrintf("Cannot connect to %s: invalid socket\n", addrConnect.ToString());
return false;
}
@@ -641,8 +552,7 @@ bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocket, i
}
// Connect to the addrConnect service on the hSocket socket.
- if (connect(hSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
- {
+ if (sock.Connect(reinterpret_cast<struct sockaddr*>(&sockaddr), len) == SOCKET_ERROR) {
int nErr = WSAGetLastError();
// WSAEINVAL is here because some legacy version of winsock uses it
if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL)
@@ -650,46 +560,34 @@ bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocket, i
// Connection didn't actually fail, but is being established
// asynchronously. Thus, use async I/O api (select/poll)
// synchronously to check for successful connection with a timeout.
-#ifdef USE_POLL
- struct pollfd pollfd = {};
- pollfd.fd = hSocket;
- pollfd.events = POLLIN | POLLOUT;
- int nRet = poll(&pollfd, 1, nTimeout);
-#else
- struct timeval timeout = MillisToTimeval(nTimeout);
- fd_set fdset;
- FD_ZERO(&fdset);
- FD_SET(hSocket, &fdset);
- int nRet = select(hSocket + 1, nullptr, &fdset, nullptr, &timeout);
-#endif
- // Upon successful completion, both select and poll return the total
- // number of file descriptors that have been selected. A value of 0
- // indicates that the call timed out and no file descriptors have
- // been selected.
- if (nRet == 0)
- {
- LogPrint(BCLog::NET, "connection to %s timeout\n", addrConnect.ToString());
+ const Sock::Event requested = Sock::RECV | Sock::SEND;
+ Sock::Event occurred;
+ if (!sock.Wait(std::chrono::milliseconds{nTimeout}, requested, &occurred)) {
+ LogPrintf("wait for connect to %s failed: %s\n",
+ addrConnect.ToString(),
+ NetworkErrorString(WSAGetLastError()));
return false;
- }
- if (nRet == SOCKET_ERROR)
- {
- LogPrintf("select() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
+ } else if (occurred == 0) {
+ LogPrint(BCLog::NET, "connection attempt to %s timed out\n", addrConnect.ToString());
return false;
}
- // Even if the select/poll was successful, the connect might not
+ // Even if the wait was successful, the connect might not
// have been successful. The reason for this failure is hidden away
// in the SO_ERROR for the socket in modern systems. We read it into
- // nRet here.
- socklen_t nRetSize = sizeof(nRet);
- if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (sockopt_arg_type)&nRet, &nRetSize) == SOCKET_ERROR)
- {
+ // sockerr here.
+ int sockerr;
+ socklen_t sockerr_len = sizeof(sockerr);
+ if (sock.GetSockOpt(SOL_SOCKET, SO_ERROR, (sockopt_arg_type)&sockerr, &sockerr_len) ==
+ SOCKET_ERROR) {
LogPrintf("getsockopt() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
return false;
}
- if (nRet != 0)
- {
- LogConnectFailure(manual_connection, "connect() to %s failed after select(): %s", addrConnect.ToString(), NetworkErrorString(nRet));
+ if (sockerr != 0) {
+ LogConnectFailure(manual_connection,
+ "connect() to %s failed after wait: %s",
+ addrConnect.ToString(),
+ NetworkErrorString(sockerr));
return false;
}
}
@@ -724,22 +622,6 @@ bool GetProxy(enum Network net, proxyType &proxyInfoOut) {
return true;
}
-/**
- * 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.
- */
bool SetNameProxy(const proxyType &addrProxy) {
if (!addrProxy.IsValid())
return false;
@@ -770,25 +652,10 @@ bool IsProxy(const CNetAddr &addr) {
return false;
}
-/**
- * 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.
- */
-bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, int port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed)
+bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed)
{
// first connect to proxy server
- if (!ConnectSocketDirectly(proxy.proxy, sock.Get(), nTimeout, true)) {
+ if (!ConnectSocketDirectly(proxy.proxy, sock, nTimeout, true)) {
outProxyConnectionFailed = true;
return false;
}
@@ -797,29 +664,18 @@ bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, int
ProxyCredentials random_auth;
static std::atomic_int counter(0);
random_auth.username = random_auth.password = strprintf("%i", counter++);
- if (!Socks5(strDest, (uint16_t)port, &random_auth, sock)) {
+ if (!Socks5(strDest, port, &random_auth, sock)) {
return false;
}
} else {
- if (!Socks5(strDest, (uint16_t)port, 0, sock)) {
+ if (!Socks5(strDest, port, 0, sock)) {
return false;
}
}
return true;
}
-/**
- * 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.
- */
-bool LookupSubNet(const std::string& strSubnet, CSubNet& ret)
+bool LookupSubNet(const std::string& strSubnet, CSubNet& ret, DNSLookupFn dns_lookup_function)
{
if (!ValidAsCString(strSubnet)) {
return false;
@@ -830,7 +686,7 @@ bool LookupSubNet(const std::string& strSubnet, CSubNet& ret)
std::string strAddress = strSubnet.substr(0, slash);
// TODO: Use LookupHost(const std::string&, CNetAddr&, bool) instead to just get
// one CNetAddr.
- if (LookupHost(strAddress, vIP, 1, false))
+ if (LookupHost(strAddress, vIP, 1, false, dns_lookup_function))
{
CNetAddr network = vIP[0];
if (slash != strSubnet.npos)
@@ -845,7 +701,7 @@ bool LookupSubNet(const std::string& strSubnet, CSubNet& ret)
else // If not a valid number, try full netmask syntax
{
// Never allow lookup for netmask
- if (LookupHost(strNetmask, vIP, 1, false)) {
+ if (LookupHost(strNetmask, vIP, 1, false, dns_lookup_function)) {
ret = CSubNet(network, vIP[0]);
return ret.IsValid();
}