2012-01-03 23:33:31 +01:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
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.
|
2012-01-03 23:33:31 +01:00
|
|
|
|
2014-06-26 20:55:39 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2015-04-03 00:51:08 +02:00
|
|
|
#include "config/dash-config.h"
|
2014-06-26 20:55:39 +02:00
|
|
|
#endif
|
|
|
|
|
2012-01-03 23:33:31 +01:00
|
|
|
#include "netbase.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2012-12-18 20:56:21 +01:00
|
|
|
#include "hash.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "sync.h"
|
|
|
|
#include "uint256.h"
|
2015-03-16 16:30:49 +01:00
|
|
|
#include "random.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "util.h"
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
#include "utilstrencodings.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2017-08-09 18:06:31 +02:00
|
|
|
#include <atomic>
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2012-01-03 23:33:31 +01:00
|
|
|
#ifndef WIN32
|
2013-07-17 10:51:40 +02:00
|
|
|
#include <fcntl.h>
|
2012-01-03 23:33:31 +01:00
|
|
|
#endif
|
|
|
|
|
2012-05-13 00:40:30 +02:00
|
|
|
#include <boost/algorithm/string/case_conv.hpp> // for to_lower()
|
2012-10-02 21:36:39 +02:00
|
|
|
#include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
|
2012-01-03 23:33:31 +01:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL)
|
2013-05-28 01:55:01 +02:00
|
|
|
#define MSG_NOSIGNAL 0
|
|
|
|
#endif
|
|
|
|
|
2012-01-03 23:33:31 +01:00
|
|
|
// Settings
|
2012-05-24 19:02:21 +02:00
|
|
|
static proxyType proxyInfo[NET_MAX];
|
2015-03-16 16:30:49 +01:00
|
|
|
static proxyType nameProxy;
|
2012-09-23 12:55:05 +02:00
|
|
|
static CCriticalSection cs_proxyInfos;
|
2014-09-25 09:01:54 +02:00
|
|
|
int nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
|
2015-11-09 19:16:38 +01:00
|
|
|
bool fNameLookup = DEFAULT_NAME_LOOKUP;
|
2012-01-03 23:33:31 +01:00
|
|
|
|
2014-09-08 13:49:56 +02:00
|
|
|
// Need ample time for negotiation for very slow proxies such as Tor (milliseconds)
|
|
|
|
static const int SOCKS5_RECV_TIMEOUT = 20 * 1000;
|
2017-08-09 18:06:31 +02:00
|
|
|
static std::atomic<bool> interruptSocks5Recv(false);
|
2014-09-08 13:49:56 +02:00
|
|
|
|
2012-04-10 20:22:04 +02:00
|
|
|
enum Network ParseNetwork(std::string net) {
|
2012-05-13 00:40:30 +02:00
|
|
|
boost::to_lower(net);
|
2012-04-10 20:22:04 +02:00
|
|
|
if (net == "ipv4") return NET_IPV4;
|
|
|
|
if (net == "ipv6") return NET_IPV6;
|
2014-07-30 15:27:03 +02:00
|
|
|
if (net == "tor" || net == "onion") return NET_TOR;
|
2012-04-10 20:22:04 +02:00
|
|
|
return NET_UNROUTABLE;
|
|
|
|
}
|
|
|
|
|
2014-07-30 15:32:36 +02:00
|
|
|
std::string GetNetworkName(enum Network net) {
|
|
|
|
switch(net)
|
|
|
|
{
|
|
|
|
case NET_IPV4: return "ipv4";
|
|
|
|
case NET_IPV6: return "ipv6";
|
|
|
|
case NET_TOR: return "onion";
|
|
|
|
default: return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-30 22:44:23 +02:00
|
|
|
void SplitHostPort(std::string in, int &portOut, std::string &hostOut) {
|
|
|
|
size_t colon = in.find_last_of(':');
|
|
|
|
// if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
|
|
|
|
bool fHaveColon = colon != in.npos;
|
|
|
|
bool fBracketed = fHaveColon && (in[0]=='[' && in[colon-1]==']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
|
|
|
|
bool fMultiColon = fHaveColon && (in.find_last_of(':',colon-1) != in.npos);
|
|
|
|
if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
|
2014-05-03 10:25:58 +02:00
|
|
|
int32_t n;
|
|
|
|
if (ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
|
2012-05-30 22:44:23 +02:00
|
|
|
in = in.substr(0, colon);
|
2014-05-03 10:25:58 +02:00
|
|
|
portOut = n;
|
2012-05-30 22:44:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (in.size()>0 && in[0] == '[' && in[in.size()-1] == ']')
|
|
|
|
hostOut = in.substr(1, in.size()-2);
|
|
|
|
else
|
|
|
|
hostOut = in;
|
|
|
|
}
|
|
|
|
|
2012-04-15 22:58:32 +02:00
|
|
|
bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
|
2012-01-03 23:33:31 +01:00
|
|
|
{
|
|
|
|
vIP.clear();
|
2012-04-29 02:11:56 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
CNetAddr addr;
|
|
|
|
if (addr.SetSpecial(std::string(pszName))) {
|
|
|
|
vIP.push_back(addr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 20:55:39 +02:00
|
|
|
#ifdef HAVE_GETADDRINFO_A
|
|
|
|
struct in_addr ipv4_addr;
|
|
|
|
#ifdef HAVE_INET_PTON
|
|
|
|
if (inet_pton(AF_INET, pszName, &ipv4_addr) > 0) {
|
|
|
|
vIP.push_back(CNetAddr(ipv4_addr));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct in6_addr ipv6_addr;
|
|
|
|
if (inet_pton(AF_INET6, pszName, &ipv6_addr) > 0) {
|
|
|
|
vIP.push_back(CNetAddr(ipv6_addr));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ipv4_addr.s_addr = inet_addr(pszName);
|
|
|
|
if (ipv4_addr.s_addr != INADDR_NONE) {
|
|
|
|
vIP.push_back(CNetAddr(ipv4_addr));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-01-13 02:02:47 +01:00
|
|
|
struct addrinfo aiHint;
|
|
|
|
memset(&aiHint, 0, sizeof(struct addrinfo));
|
2012-01-03 23:33:31 +01:00
|
|
|
aiHint.ai_socktype = SOCK_STREAM;
|
|
|
|
aiHint.ai_protocol = IPPROTO_TCP;
|
|
|
|
aiHint.ai_family = AF_UNSPEC;
|
2013-04-27 14:27:05 +02:00
|
|
|
#ifdef WIN32
|
2012-06-19 02:22:09 +02:00
|
|
|
aiHint.ai_flags = fAllowLookup ? 0 : AI_NUMERICHOST;
|
2012-01-03 23:33:31 +01:00
|
|
|
#else
|
2012-06-19 02:22:09 +02:00
|
|
|
aiHint.ai_flags = fAllowLookup ? AI_ADDRCONFIG : AI_NUMERICHOST;
|
2012-01-03 23:33:31 +01:00
|
|
|
#endif
|
2014-06-26 20:55:39 +02:00
|
|
|
|
2012-01-03 23:33:31 +01:00
|
|
|
struct addrinfo *aiRes = NULL;
|
2014-06-26 20:55:39 +02:00
|
|
|
#ifdef HAVE_GETADDRINFO_A
|
|
|
|
struct gaicb gcb, *query = &gcb;
|
|
|
|
memset(query, 0, sizeof(struct gaicb));
|
|
|
|
gcb.ar_name = pszName;
|
|
|
|
gcb.ar_request = &aiHint;
|
|
|
|
int nErr = getaddrinfo_a(GAI_NOWAIT, &query, 1, NULL);
|
|
|
|
if (nErr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
do {
|
2017-09-07 17:59:00 +02:00
|
|
|
// Should set the timeout limit to a reasonable value to avoid
|
2014-06-26 20:55:39 +02:00
|
|
|
// generating unnecessary checking call during the polling loop,
|
|
|
|
// while it can still response to stop request quick enough.
|
|
|
|
// 2 seconds looks fine in our situation.
|
|
|
|
struct timespec ts = { 2, 0 };
|
|
|
|
gai_suspend(&query, 1, &ts);
|
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
|
|
|
|
nErr = gai_error(query);
|
|
|
|
if (0 == nErr)
|
|
|
|
aiRes = query->ar_result;
|
|
|
|
} while (nErr == EAI_INPROGRESS);
|
|
|
|
#else
|
2012-01-03 23:33:31 +01:00
|
|
|
int nErr = getaddrinfo(pszName, NULL, &aiHint, &aiRes);
|
2014-06-26 20:55:39 +02:00
|
|
|
#endif
|
2012-01-03 23:33:31 +01:00
|
|
|
if (nErr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
struct addrinfo *aiTrav = aiRes;
|
|
|
|
while (aiTrav != NULL && (nMaxSolutions == 0 || vIP.size() < nMaxSolutions))
|
|
|
|
{
|
|
|
|
if (aiTrav->ai_family == AF_INET)
|
|
|
|
{
|
|
|
|
assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in));
|
|
|
|
vIP.push_back(CNetAddr(((struct sockaddr_in*)(aiTrav->ai_addr))->sin_addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aiTrav->ai_family == AF_INET6)
|
|
|
|
{
|
|
|
|
assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in6));
|
2016-04-08 14:11:33 +02:00
|
|
|
struct sockaddr_in6* s6 = (struct sockaddr_in6*) aiTrav->ai_addr;
|
|
|
|
vIP.push_back(CNetAddr(s6->sin6_addr, s6->sin6_scope_id));
|
2012-01-03 23:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
aiTrav = aiTrav->ai_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(aiRes);
|
|
|
|
|
|
|
|
return (vIP.size() > 0);
|
|
|
|
}
|
|
|
|
|
2012-04-15 22:58:32 +02:00
|
|
|
bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
|
2012-01-03 23:33:31 +01:00
|
|
|
{
|
2013-04-27 14:27:05 +02:00
|
|
|
std::string strHost(pszName);
|
|
|
|
if (strHost.empty())
|
2012-01-03 23:33:31 +01:00
|
|
|
return false;
|
2013-04-27 14:27:05 +02:00
|
|
|
if (boost::algorithm::starts_with(strHost, "[") && boost::algorithm::ends_with(strHost, "]"))
|
2012-01-03 23:33:31 +01:00
|
|
|
{
|
2013-04-27 14:27:05 +02:00
|
|
|
strHost = strHost.substr(1, strHost.size() - 2);
|
2012-01-03 23:33:31 +01:00
|
|
|
}
|
|
|
|
|
2012-10-02 21:36:39 +02:00
|
|
|
return LookupIntern(strHost.c_str(), vIP, nMaxSolutions, fAllowLookup);
|
2012-01-03 23:33:31 +01:00
|
|
|
}
|
|
|
|
|
2017-09-03 15:29:10 +02:00
|
|
|
bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup)
|
|
|
|
{
|
|
|
|
std::vector<CNetAddr> vIP;
|
|
|
|
LookupHost(pszName, vIP, 1, fAllowLookup);
|
|
|
|
if(vIP.empty())
|
|
|
|
return false;
|
|
|
|
addr = vIP.front();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-15 22:58:32 +02:00
|
|
|
bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions)
|
2012-01-03 23:33:31 +01:00
|
|
|
{
|
|
|
|
if (pszName[0] == 0)
|
|
|
|
return false;
|
|
|
|
int port = portDefault;
|
2012-05-30 22:44:23 +02:00
|
|
|
std::string hostname = "";
|
|
|
|
SplitHostPort(std::string(pszName), port, hostname);
|
2012-01-03 23:33:31 +01:00
|
|
|
|
|
|
|
std::vector<CNetAddr> vIP;
|
2012-05-30 22:44:23 +02:00
|
|
|
bool fRet = LookupIntern(hostname.c_str(), vIP, nMaxSolutions, fAllowLookup);
|
2011-12-17 01:48:03 +01:00
|
|
|
if (!fRet)
|
|
|
|
return false;
|
|
|
|
vAddr.resize(vIP.size());
|
2012-04-15 22:52:09 +02:00
|
|
|
for (unsigned int i = 0; i < vIP.size(); i++)
|
2011-12-17 01:48:03 +01:00
|
|
|
vAddr[i] = CService(vIP[i], port);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup)
|
|
|
|
{
|
|
|
|
std::vector<CService> vService;
|
|
|
|
bool fRet = Lookup(pszName, vService, portDefault, fAllowLookup, 1);
|
2012-01-03 23:33:31 +01:00
|
|
|
if (!fRet)
|
|
|
|
return false;
|
2011-12-17 01:48:03 +01:00
|
|
|
addr = vService[0];
|
2012-01-03 23:33:31 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-03 15:29:10 +02:00
|
|
|
CService LookupNumeric(const char *pszName, int portDefault)
|
2012-01-03 23:33:31 +01:00
|
|
|
{
|
2017-09-03 15:29:10 +02:00
|
|
|
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(pszName, addr, portDefault, false))
|
|
|
|
addr = CService();
|
|
|
|
return addr;
|
2012-01-03 23:33:31 +01:00
|
|
|
}
|
|
|
|
|
2015-08-25 20:12:08 +02:00
|
|
|
struct timeval MillisToTimeval(int64_t nTimeout)
|
2014-09-08 13:49:56 +02:00
|
|
|
{
|
|
|
|
struct timeval timeout;
|
|
|
|
timeout.tv_sec = nTimeout / 1000;
|
|
|
|
timeout.tv_usec = (nTimeout % 1000) * 1000;
|
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read bytes from socket. This will either read the full number of bytes requested
|
|
|
|
* or return False on error or timeout.
|
2017-08-09 18:06:31 +02:00
|
|
|
* This function can be interrupted by calling InterruptSocks5()
|
2014-09-08 13:49:56 +02:00
|
|
|
*
|
|
|
|
* @param data Buffer to receive into
|
|
|
|
* @param len Length of data to receive
|
|
|
|
* @param timeout Timeout in milliseconds for receive operation
|
|
|
|
*
|
|
|
|
* @note This function requires that hSocket is in non-blocking mode.
|
|
|
|
*/
|
|
|
|
bool static InterruptibleRecv(char* data, size_t len, int timeout, SOCKET& hSocket)
|
|
|
|
{
|
|
|
|
int64_t curTime = GetTimeMillis();
|
|
|
|
int64_t endTime = curTime + timeout;
|
|
|
|
// Maximum time to wait in one select call. 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 = recv(hSocket, data, len, 0); // Optimistically try the recv first
|
|
|
|
if (ret > 0) {
|
|
|
|
len -= ret;
|
|
|
|
data += ret;
|
|
|
|
} else if (ret == 0) { // Unexpected disconnection
|
|
|
|
return false;
|
|
|
|
} else { // Other error or blocking
|
|
|
|
int nErr = WSAGetLastError();
|
|
|
|
if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL) {
|
2015-07-10 00:23:27 +02:00
|
|
|
if (!IsSelectableSocket(hSocket)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-08 13:49:56 +02:00
|
|
|
struct timeval tval = MillisToTimeval(std::min(endTime - curTime, maxWait));
|
|
|
|
fd_set fdset;
|
|
|
|
FD_ZERO(&fdset);
|
|
|
|
FD_SET(hSocket, &fdset);
|
|
|
|
int nRet = select(hSocket + 1, &fdset, NULL, NULL, &tval);
|
|
|
|
if (nRet == SOCKET_ERROR) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-08-09 18:06:31 +02:00
|
|
|
if (interruptSocks5Recv)
|
|
|
|
return false;
|
2014-09-08 13:49:56 +02:00
|
|
|
curTime = GetTimeMillis();
|
2012-04-01 20:25:48 +02:00
|
|
|
}
|
2014-09-08 13:49:56 +02:00
|
|
|
return len == 0;
|
2012-04-01 20:25:48 +02:00
|
|
|
}
|
|
|
|
|
2015-03-16 16:30:49 +01:00
|
|
|
struct ProxyCredentials
|
|
|
|
{
|
|
|
|
std::string username;
|
|
|
|
std::string password;
|
|
|
|
};
|
|
|
|
|
2016-05-19 08:45:27 +02:00
|
|
|
std::string Socks5ErrorString(int err)
|
|
|
|
{
|
|
|
|
switch(err) {
|
|
|
|
case 0x01: return "general failure";
|
|
|
|
case 0x02: return "connection not allowed";
|
|
|
|
case 0x03: return "network unreachable";
|
|
|
|
case 0x04: return "host unreachable";
|
|
|
|
case 0x05: return "connection refused";
|
|
|
|
case 0x06: return "TTL expired";
|
|
|
|
case 0x07: return "protocol error";
|
|
|
|
case 0x08: return "address type not supported";
|
|
|
|
default: return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 16:30:49 +01:00
|
|
|
/** Connect using SOCKS5 (as described in RFC1928) */
|
2015-04-23 15:06:11 +02:00
|
|
|
static bool Socks5(const std::string& strDest, int port, const ProxyCredentials *auth, SOCKET& hSocket)
|
2012-04-01 20:25:48 +02:00
|
|
|
{
|
2016-05-19 08:45:27 +02:00
|
|
|
LogPrint("net", "SOCKS5 connecting %s\n", strDest);
|
2015-03-16 16:30:49 +01:00
|
|
|
if (strDest.size() > 255) {
|
2014-07-10 12:13:03 +02:00
|
|
|
CloseSocket(hSocket);
|
2012-04-19 17:02:21 +02:00
|
|
|
return error("Hostname too long");
|
|
|
|
}
|
2015-03-16 16:30:49 +01:00
|
|
|
// Accepted authentication methods
|
|
|
|
std::vector<uint8_t> vSocks5Init;
|
|
|
|
vSocks5Init.push_back(0x05);
|
|
|
|
if (auth) {
|
|
|
|
vSocks5Init.push_back(0x02); // # METHODS
|
|
|
|
vSocks5Init.push_back(0x00); // X'00' NO AUTHENTICATION REQUIRED
|
|
|
|
vSocks5Init.push_back(0x02); // X'02' USERNAME/PASSWORD (RFC1929)
|
|
|
|
} else {
|
|
|
|
vSocks5Init.push_back(0x01); // # METHODS
|
|
|
|
vSocks5Init.push_back(0x00); // X'00' NO AUTHENTICATION REQUIRED
|
|
|
|
}
|
|
|
|
ssize_t ret = send(hSocket, (const char*)begin_ptr(vSocks5Init), vSocks5Init.size(), MSG_NOSIGNAL);
|
|
|
|
if (ret != (ssize_t)vSocks5Init.size()) {
|
2014-07-10 12:13:03 +02:00
|
|
|
CloseSocket(hSocket);
|
2012-04-01 20:25:48 +02:00
|
|
|
return error("Error sending to proxy");
|
|
|
|
}
|
|
|
|
char pchRet1[2];
|
2015-03-16 16:30:49 +01:00
|
|
|
if (!InterruptibleRecv(pchRet1, 2, SOCKS5_RECV_TIMEOUT, hSocket)) {
|
2014-07-10 12:13:03 +02:00
|
|
|
CloseSocket(hSocket);
|
2016-05-19 08:45:27 +02:00
|
|
|
LogPrintf("Socks5() connect to %s:%d failed: InterruptibleRecv() timeout or other failure\n", strDest, port);
|
|
|
|
return false;
|
2012-04-01 20:25:48 +02:00
|
|
|
}
|
2015-03-16 16:30:49 +01:00
|
|
|
if (pchRet1[0] != 0x05) {
|
2014-07-10 12:13:03 +02:00
|
|
|
CloseSocket(hSocket);
|
2012-04-01 20:25:48 +02:00
|
|
|
return error("Proxy failed to initialize");
|
|
|
|
}
|
2015-03-16 16:30:49 +01:00
|
|
|
if (pchRet1[1] == 0x02 && auth) {
|
|
|
|
// Perform username/password authentication (as described in RFC1929)
|
|
|
|
std::vector<uint8_t> vAuth;
|
|
|
|
vAuth.push_back(0x01);
|
|
|
|
if (auth->username.size() > 255 || auth->password.size() > 255)
|
|
|
|
return error("Proxy username or password too long");
|
|
|
|
vAuth.push_back(auth->username.size());
|
|
|
|
vAuth.insert(vAuth.end(), auth->username.begin(), auth->username.end());
|
|
|
|
vAuth.push_back(auth->password.size());
|
|
|
|
vAuth.insert(vAuth.end(), auth->password.begin(), auth->password.end());
|
|
|
|
ret = send(hSocket, (const char*)begin_ptr(vAuth), vAuth.size(), MSG_NOSIGNAL);
|
|
|
|
if (ret != (ssize_t)vAuth.size()) {
|
|
|
|
CloseSocket(hSocket);
|
|
|
|
return error("Error sending authentication to proxy");
|
|
|
|
}
|
|
|
|
LogPrint("proxy", "SOCKS5 sending proxy authentication %s:%s\n", auth->username, auth->password);
|
|
|
|
char pchRetA[2];
|
|
|
|
if (!InterruptibleRecv(pchRetA, 2, SOCKS5_RECV_TIMEOUT, hSocket)) {
|
|
|
|
CloseSocket(hSocket);
|
|
|
|
return error("Error reading proxy authentication response");
|
|
|
|
}
|
|
|
|
if (pchRetA[0] != 0x01 || pchRetA[1] != 0x00) {
|
|
|
|
CloseSocket(hSocket);
|
2015-08-09 01:17:27 +02:00
|
|
|
return error("Proxy authentication unsuccessful");
|
2015-03-16 16:30:49 +01:00
|
|
|
}
|
|
|
|
} else if (pchRet1[1] == 0x00) {
|
|
|
|
// Perform no authentication
|
|
|
|
} else {
|
|
|
|
CloseSocket(hSocket);
|
|
|
|
return error("Proxy requested wrong authentication method %02x", pchRet1[1]);
|
|
|
|
}
|
|
|
|
std::vector<uint8_t> vSocks5;
|
|
|
|
vSocks5.push_back(0x05); // VER protocol version
|
|
|
|
vSocks5.push_back(0x01); // CMD CONNECT
|
|
|
|
vSocks5.push_back(0x00); // RSV Reserved
|
|
|
|
vSocks5.push_back(0x03); // ATYP DOMAINNAME
|
|
|
|
vSocks5.push_back(strDest.size()); // Length<=255 is checked at beginning of function
|
|
|
|
vSocks5.insert(vSocks5.end(), strDest.begin(), strDest.end());
|
|
|
|
vSocks5.push_back((port >> 8) & 0xFF);
|
|
|
|
vSocks5.push_back((port >> 0) & 0xFF);
|
|
|
|
ret = send(hSocket, (const char*)begin_ptr(vSocks5), vSocks5.size(), MSG_NOSIGNAL);
|
|
|
|
if (ret != (ssize_t)vSocks5.size()) {
|
2014-07-10 12:13:03 +02:00
|
|
|
CloseSocket(hSocket);
|
2012-04-01 20:25:48 +02:00
|
|
|
return error("Error sending to proxy");
|
|
|
|
}
|
|
|
|
char pchRet2[4];
|
2015-03-16 16:30:49 +01:00
|
|
|
if (!InterruptibleRecv(pchRet2, 4, SOCKS5_RECV_TIMEOUT, hSocket)) {
|
2014-07-10 12:13:03 +02:00
|
|
|
CloseSocket(hSocket);
|
2012-04-01 20:25:48 +02:00
|
|
|
return error("Error reading proxy response");
|
|
|
|
}
|
2015-03-16 16:30:49 +01:00
|
|
|
if (pchRet2[0] != 0x05) {
|
2014-07-10 12:13:03 +02:00
|
|
|
CloseSocket(hSocket);
|
2012-04-01 20:25:48 +02:00
|
|
|
return error("Proxy failed to accept request");
|
|
|
|
}
|
2015-03-16 16:30:49 +01:00
|
|
|
if (pchRet2[1] != 0x00) {
|
2016-05-19 08:45:27 +02:00
|
|
|
// Failures to connect to a peer that are not proxy errors
|
2014-07-10 12:13:03 +02:00
|
|
|
CloseSocket(hSocket);
|
2016-05-19 08:45:27 +02:00
|
|
|
LogPrintf("Socks5() connect to %s:%d failed: %s\n", strDest, port, Socks5ErrorString(pchRet2[1]));
|
|
|
|
return false;
|
2012-04-01 20:25:48 +02:00
|
|
|
}
|
2015-03-16 16:30:49 +01:00
|
|
|
if (pchRet2[2] != 0x00) {
|
2014-07-10 12:13:03 +02:00
|
|
|
CloseSocket(hSocket);
|
2012-04-01 20:25:48 +02:00
|
|
|
return error("Error: malformed proxy response");
|
|
|
|
}
|
|
|
|
char pchRet3[256];
|
|
|
|
switch (pchRet2[3])
|
|
|
|
{
|
2014-09-08 13:49:56 +02:00
|
|
|
case 0x01: ret = InterruptibleRecv(pchRet3, 4, SOCKS5_RECV_TIMEOUT, hSocket); break;
|
|
|
|
case 0x04: ret = InterruptibleRecv(pchRet3, 16, SOCKS5_RECV_TIMEOUT, hSocket); break;
|
2012-04-01 20:25:48 +02:00
|
|
|
case 0x03:
|
|
|
|
{
|
2014-09-08 13:49:56 +02:00
|
|
|
ret = InterruptibleRecv(pchRet3, 1, SOCKS5_RECV_TIMEOUT, hSocket);
|
|
|
|
if (!ret) {
|
2014-07-10 12:13:03 +02:00
|
|
|
CloseSocket(hSocket);
|
2012-04-01 20:25:48 +02:00
|
|
|
return error("Error reading from proxy");
|
2014-04-10 02:09:17 +02:00
|
|
|
}
|
2012-04-01 20:25:48 +02:00
|
|
|
int nRecv = pchRet3[0];
|
2014-09-08 13:49:56 +02:00
|
|
|
ret = InterruptibleRecv(pchRet3, nRecv, SOCKS5_RECV_TIMEOUT, hSocket);
|
2012-04-01 20:25:48 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-07-10 12:13:03 +02:00
|
|
|
default: CloseSocket(hSocket); return error("Error: malformed proxy response");
|
2012-04-01 20:25:48 +02:00
|
|
|
}
|
2015-03-16 16:30:49 +01:00
|
|
|
if (!ret) {
|
2014-07-10 12:13:03 +02:00
|
|
|
CloseSocket(hSocket);
|
2012-04-01 20:25:48 +02:00
|
|
|
return error("Error reading from proxy");
|
|
|
|
}
|
2015-03-16 16:30:49 +01:00
|
|
|
if (!InterruptibleRecv(pchRet3, 2, SOCKS5_RECV_TIMEOUT, hSocket)) {
|
2014-07-10 12:13:03 +02:00
|
|
|
CloseSocket(hSocket);
|
2012-04-01 20:25:48 +02:00
|
|
|
return error("Error reading from proxy");
|
|
|
|
}
|
2016-05-19 08:45:27 +02:00
|
|
|
LogPrint("net", "SOCKS5 connected %s\n", strDest);
|
2012-04-01 20:25:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-19 17:02:21 +02:00
|
|
|
bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRet, int nTimeout)
|
2012-01-03 23:33:31 +01:00
|
|
|
{
|
|
|
|
hSocketRet = INVALID_SOCKET;
|
|
|
|
|
2012-05-11 15:28:59 +02:00
|
|
|
struct sockaddr_storage sockaddr;
|
|
|
|
socklen_t len = sizeof(sockaddr);
|
|
|
|
if (!addrConnect.GetSockAddr((struct sockaddr*)&sockaddr, &len)) {
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrintf("Cannot connect to %s: unsupported network\n", addrConnect.ToString());
|
2012-03-31 17:58:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-11 15:28:59 +02:00
|
|
|
SOCKET hSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
|
2012-01-03 23:33:31 +01:00
|
|
|
if (hSocket == INVALID_SOCKET)
|
|
|
|
return false;
|
2014-07-09 11:00:00 +02:00
|
|
|
|
2012-01-03 23:33:31 +01:00
|
|
|
int set = 1;
|
2015-10-22 01:52:29 +02:00
|
|
|
#ifdef SO_NOSIGPIPE
|
2014-06-16 15:55:39 +02:00
|
|
|
// Different way of disabling SIGPIPE on BSD
|
2012-01-03 23:33:31 +01:00
|
|
|
setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
|
|
|
|
#endif
|
|
|
|
|
2015-10-22 01:52:29 +02:00
|
|
|
//Disable Nagle's algorithm
|
|
|
|
#ifdef WIN32
|
|
|
|
setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int));
|
|
|
|
#else
|
|
|
|
setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&set, sizeof(int));
|
|
|
|
#endif
|
|
|
|
|
2014-07-09 11:00:00 +02:00
|
|
|
// Set to non-blocking
|
|
|
|
if (!SetSocketNonBlocking(hSocket, true))
|
|
|
|
return error("ConnectSocketDirectly: Setting socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
|
2012-01-03 23:33:31 +01:00
|
|
|
|
2012-05-11 15:28:59 +02:00
|
|
|
if (connect(hSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
|
2012-01-03 23:33:31 +01:00
|
|
|
{
|
2014-06-22 20:17:15 +02:00
|
|
|
int nErr = WSAGetLastError();
|
2012-01-03 23:33:31 +01:00
|
|
|
// WSAEINVAL is here because some legacy version of winsock uses it
|
2014-06-22 20:17:15 +02:00
|
|
|
if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL)
|
2012-01-03 23:33:31 +01:00
|
|
|
{
|
2014-09-08 13:49:56 +02:00
|
|
|
struct timeval timeout = MillisToTimeval(nTimeout);
|
2012-01-03 23:33:31 +01:00
|
|
|
fd_set fdset;
|
|
|
|
FD_ZERO(&fdset);
|
|
|
|
FD_SET(hSocket, &fdset);
|
|
|
|
int nRet = select(hSocket + 1, NULL, &fdset, NULL, &timeout);
|
|
|
|
if (nRet == 0)
|
|
|
|
{
|
2014-01-16 16:15:27 +01:00
|
|
|
LogPrint("net", "connection to %s timeout\n", addrConnect.ToString());
|
2014-07-10 12:13:03 +02:00
|
|
|
CloseSocket(hSocket);
|
2012-01-03 23:33:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (nRet == SOCKET_ERROR)
|
|
|
|
{
|
2014-05-08 14:15:19 +02:00
|
|
|
LogPrintf("select() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
|
2014-07-10 12:13:03 +02:00
|
|
|
CloseSocket(hSocket);
|
2012-01-03 23:33:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
socklen_t nRetSize = sizeof(nRet);
|
|
|
|
#ifdef WIN32
|
|
|
|
if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (char*)(&nRet), &nRetSize) == SOCKET_ERROR)
|
|
|
|
#else
|
|
|
|
if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, &nRet, &nRetSize) == SOCKET_ERROR)
|
|
|
|
#endif
|
|
|
|
{
|
2014-05-08 14:15:19 +02:00
|
|
|
LogPrintf("getsockopt() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
|
2014-07-10 12:13:03 +02:00
|
|
|
CloseSocket(hSocket);
|
2012-01-03 23:33:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (nRet != 0)
|
|
|
|
{
|
2014-05-08 14:15:19 +02:00
|
|
|
LogPrintf("connect() to %s failed after select(): %s\n", addrConnect.ToString(), NetworkErrorString(nRet));
|
2014-07-10 12:13:03 +02:00
|
|
|
CloseSocket(hSocket);
|
2012-01-03 23:33:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef WIN32
|
|
|
|
else if (WSAGetLastError() != WSAEISCONN)
|
|
|
|
#else
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2014-05-08 14:15:19 +02:00
|
|
|
LogPrintf("connect() to %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
|
2014-07-10 12:13:03 +02:00
|
|
|
CloseSocket(hSocket);
|
2012-01-03 23:33:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-19 17:02:21 +02:00
|
|
|
hSocketRet = hSocket;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-16 16:30:49 +01:00
|
|
|
bool SetProxy(enum Network net, const proxyType &addrProxy) {
|
2012-05-24 19:02:21 +02:00
|
|
|
assert(net >= 0 && net < NET_MAX);
|
2014-06-11 13:20:59 +02:00
|
|
|
if (!addrProxy.IsValid())
|
2012-05-24 19:02:21 +02:00
|
|
|
return false;
|
2012-09-23 12:55:05 +02:00
|
|
|
LOCK(cs_proxyInfos);
|
2014-06-11 13:20:59 +02:00
|
|
|
proxyInfo[net] = addrProxy;
|
2012-05-24 19:02:21 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-23 12:55:05 +02:00
|
|
|
bool GetProxy(enum Network net, proxyType &proxyInfoOut) {
|
2012-05-24 19:02:21 +02:00
|
|
|
assert(net >= 0 && net < NET_MAX);
|
2012-09-23 12:55:05 +02:00
|
|
|
LOCK(cs_proxyInfos);
|
2014-06-11 13:20:59 +02:00
|
|
|
if (!proxyInfo[net].IsValid())
|
2012-05-24 19:02:21 +02:00
|
|
|
return false;
|
2012-09-23 12:55:05 +02:00
|
|
|
proxyInfoOut = proxyInfo[net];
|
2012-05-24 19:02:21 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-16 16:30:49 +01:00
|
|
|
bool SetNameProxy(const proxyType &addrProxy) {
|
2014-06-11 13:20:59 +02:00
|
|
|
if (!addrProxy.IsValid())
|
2012-05-24 19:02:21 +02:00
|
|
|
return false;
|
2012-09-23 12:55:05 +02:00
|
|
|
LOCK(cs_proxyInfos);
|
2014-06-11 13:20:59 +02:00
|
|
|
nameProxy = addrProxy;
|
2012-05-24 19:02:21 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-16 16:30:49 +01:00
|
|
|
bool GetNameProxy(proxyType &nameProxyOut) {
|
2012-09-23 12:55:05 +02:00
|
|
|
LOCK(cs_proxyInfos);
|
2014-06-11 13:20:59 +02:00
|
|
|
if(!nameProxy.IsValid())
|
2012-09-23 12:55:05 +02:00
|
|
|
return false;
|
2014-06-11 13:20:59 +02:00
|
|
|
nameProxyOut = nameProxy;
|
2012-09-23 12:55:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HaveNameProxy() {
|
|
|
|
LOCK(cs_proxyInfos);
|
2014-06-11 13:20:59 +02:00
|
|
|
return nameProxy.IsValid();
|
2012-05-24 19:02:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsProxy(const CNetAddr &addr) {
|
2012-09-23 12:55:05 +02:00
|
|
|
LOCK(cs_proxyInfos);
|
|
|
|
for (int i = 0; i < NET_MAX; i++) {
|
2015-03-16 16:30:49 +01:00
|
|
|
if (addr == (CNetAddr)proxyInfo[i].proxy)
|
2012-05-24 19:02:21 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-23 15:06:11 +02:00
|
|
|
static bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed)
|
2012-04-19 17:02:21 +02:00
|
|
|
{
|
|
|
|
SOCKET hSocket = INVALID_SOCKET;
|
2012-05-24 19:02:21 +02:00
|
|
|
// first connect to proxy server
|
2015-03-16 16:30:49 +01:00
|
|
|
if (!ConnectSocketDirectly(proxy.proxy, hSocket, nTimeout)) {
|
2014-12-02 17:43:42 +01:00
|
|
|
if (outProxyConnectionFailed)
|
|
|
|
*outProxyConnectionFailed = true;
|
2012-05-24 19:02:21 +02:00
|
|
|
return false;
|
2014-12-02 17:43:42 +01:00
|
|
|
}
|
2012-05-24 19:02:21 +02:00
|
|
|
// do socks negotiation
|
2015-03-16 16:30:49 +01:00
|
|
|
if (proxy.randomize_credentials) {
|
|
|
|
ProxyCredentials random_auth;
|
2016-10-18 15:38:44 +02:00
|
|
|
static std::atomic_int counter;
|
|
|
|
random_auth.username = random_auth.password = strprintf("%i", counter++);
|
2015-03-16 16:30:49 +01:00
|
|
|
if (!Socks5(strDest, (unsigned short)port, &random_auth, hSocket))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (!Socks5(strDest, (unsigned short)port, 0, hSocket))
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-03 23:33:31 +01:00
|
|
|
|
|
|
|
hSocketRet = hSocket;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-16 16:30:49 +01:00
|
|
|
bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed)
|
|
|
|
{
|
|
|
|
proxyType proxy;
|
|
|
|
if (outProxyConnectionFailed)
|
|
|
|
*outProxyConnectionFailed = false;
|
|
|
|
|
|
|
|
if (GetProxy(addrDest.GetNetwork(), proxy))
|
|
|
|
return ConnectThroughProxy(proxy, addrDest.ToStringIP(), addrDest.GetPort(), hSocketRet, nTimeout, outProxyConnectionFailed);
|
|
|
|
else // no proxy needed (none set for target network)
|
|
|
|
return ConnectSocketDirectly(addrDest, hSocketRet, nTimeout);
|
|
|
|
}
|
|
|
|
|
2014-12-02 17:43:42 +01:00
|
|
|
bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout, bool *outProxyConnectionFailed)
|
2012-04-19 17:38:03 +02:00
|
|
|
{
|
2015-05-31 15:48:23 +02:00
|
|
|
std::string strDest;
|
2012-04-19 17:38:03 +02:00
|
|
|
int port = portDefault;
|
2014-12-02 17:43:42 +01:00
|
|
|
|
|
|
|
if (outProxyConnectionFailed)
|
|
|
|
*outProxyConnectionFailed = false;
|
|
|
|
|
2015-05-31 15:48:23 +02:00
|
|
|
SplitHostPort(std::string(pszDest), port, strDest);
|
2012-09-23 12:55:05 +02:00
|
|
|
|
2016-09-02 12:32:49 +02:00
|
|
|
proxyType proxy;
|
|
|
|
GetNameProxy(proxy);
|
2012-09-23 12:55:05 +02:00
|
|
|
|
2017-07-14 18:58:57 +02:00
|
|
|
std::vector<CService> addrResolved;
|
|
|
|
if (Lookup(strDest.c_str(), addrResolved, port, fNameLookup && !HaveNameProxy(), 256)) {
|
|
|
|
if (addrResolved.size() > 0) {
|
|
|
|
addr = addrResolved[GetRand(addrResolved.size())];
|
|
|
|
return ConnectSocket(addr, hSocketRet, nTimeout);
|
|
|
|
}
|
2012-04-19 17:38:03 +02:00
|
|
|
}
|
2014-06-11 13:20:59 +02:00
|
|
|
|
2017-09-03 15:29:10 +02:00
|
|
|
addr = CService();
|
2014-06-11 13:20:59 +02:00
|
|
|
|
|
|
|
if (!HaveNameProxy())
|
2012-04-19 17:38:03 +02:00
|
|
|
return false;
|
2016-09-02 12:32:49 +02:00
|
|
|
return ConnectThroughProxy(proxy, strDest, port, hSocketRet, nTimeout, outProxyConnectionFailed);
|
2012-04-19 17:38:03 +02:00
|
|
|
}
|
|
|
|
|
2017-09-03 15:29:10 +02:00
|
|
|
bool LookupSubNet(const char* pszName, CSubNet& ret)
|
2014-04-28 11:08:57 +02:00
|
|
|
{
|
2017-09-03 15:29:10 +02:00
|
|
|
std::string strSubnet(pszName);
|
2014-04-28 11:08:57 +02:00
|
|
|
size_t slash = strSubnet.find_last_of('/');
|
|
|
|
std::vector<CNetAddr> vIP;
|
|
|
|
|
|
|
|
std::string strAddress = strSubnet.substr(0, slash);
|
2017-09-02 22:07:11 +02:00
|
|
|
if (LookupHost(strAddress.c_str(), vIP, 1, false))
|
2014-04-28 11:08:57 +02:00
|
|
|
{
|
2017-09-03 15:29:10 +02:00
|
|
|
CNetAddr network = vIP[0];
|
2014-04-28 11:08:57 +02:00
|
|
|
if (slash != strSubnet.npos)
|
|
|
|
{
|
|
|
|
std::string strNetmask = strSubnet.substr(slash + 1);
|
|
|
|
int32_t n;
|
|
|
|
// IPv4 addresses start at offset 12, and first 12 bytes must match, so just offset n
|
2017-09-03 15:29:10 +02:00
|
|
|
if (ParseInt32(strNetmask, &n)) { // If valid number, assume /24 syntax
|
|
|
|
ret = CSubNet(network, n);
|
|
|
|
return ret.IsValid();
|
2014-04-28 11:08:57 +02:00
|
|
|
}
|
|
|
|
else // If not a valid number, try full netmask syntax
|
|
|
|
{
|
2017-09-03 15:29:10 +02:00
|
|
|
// Never allow lookup for netmask
|
|
|
|
if (LookupHost(strNetmask.c_str(), vIP, 1, false)) {
|
|
|
|
ret = CSubNet(network, vIP[0]);
|
|
|
|
return ret.IsValid();
|
2014-04-28 11:08:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-25 10:47:34 +02:00
|
|
|
else
|
2017-09-03 15:29:10 +02:00
|
|
|
{
|
|
|
|
ret = CSubNet(network);
|
|
|
|
return ret.IsValid();
|
|
|
|
}
|
2015-06-25 10:47:34 +02:00
|
|
|
}
|
2017-09-03 15:29:10 +02:00
|
|
|
return false;
|
2015-05-25 20:03:51 +02:00
|
|
|
}
|
|
|
|
|
2014-05-08 14:15:19 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
std::string NetworkErrorString(int err)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
buf[0] = 0;
|
|
|
|
if(FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
|
|
|
|
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
buf, sizeof(buf), NULL))
|
|
|
|
{
|
|
|
|
return strprintf("%s (%d)", buf, err);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return strprintf("Unknown error (%d)", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
std::string NetworkErrorString(int err)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
const char *s = buf;
|
|
|
|
buf[0] = 0;
|
|
|
|
/* Too bad there are two incompatible implementations of the
|
|
|
|
* thread-safe strerror. */
|
|
|
|
#ifdef STRERROR_R_CHAR_P /* GNU variant can return a pointer outside the passed buffer */
|
|
|
|
s = strerror_r(err, buf, sizeof(buf));
|
|
|
|
#else /* POSIX variant always returns message in buffer */
|
2014-07-07 17:34:00 +02:00
|
|
|
if (strerror_r(err, buf, sizeof(buf)))
|
|
|
|
buf[0] = 0;
|
2014-05-08 14:15:19 +02:00
|
|
|
#endif
|
|
|
|
return strprintf("%s (%d)", s, err);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-07-10 12:13:03 +02:00
|
|
|
bool CloseSocket(SOCKET& hSocket)
|
|
|
|
{
|
|
|
|
if (hSocket == INVALID_SOCKET)
|
|
|
|
return false;
|
|
|
|
#ifdef WIN32
|
|
|
|
int ret = closesocket(hSocket);
|
|
|
|
#else
|
|
|
|
int ret = close(hSocket);
|
|
|
|
#endif
|
|
|
|
hSocket = INVALID_SOCKET;
|
|
|
|
return ret != SOCKET_ERROR;
|
|
|
|
}
|
2014-07-09 11:00:00 +02:00
|
|
|
|
|
|
|
bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking)
|
|
|
|
{
|
|
|
|
if (fNonBlocking) {
|
|
|
|
#ifdef WIN32
|
|
|
|
u_long nOne = 1;
|
|
|
|
if (ioctlsocket(hSocket, FIONBIO, &nOne) == SOCKET_ERROR) {
|
|
|
|
#else
|
|
|
|
int fFlags = fcntl(hSocket, F_GETFL, 0);
|
|
|
|
if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == SOCKET_ERROR) {
|
|
|
|
#endif
|
|
|
|
CloseSocket(hSocket);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#ifdef WIN32
|
|
|
|
u_long nZero = 0;
|
|
|
|
if (ioctlsocket(hSocket, FIONBIO, &nZero) == SOCKET_ERROR) {
|
|
|
|
#else
|
|
|
|
int fFlags = fcntl(hSocket, F_GETFL, 0);
|
|
|
|
if (fcntl(hSocket, F_SETFL, fFlags & ~O_NONBLOCK) == SOCKET_ERROR) {
|
|
|
|
#endif
|
|
|
|
CloseSocket(hSocket);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-08-09 18:06:31 +02:00
|
|
|
|
|
|
|
void InterruptSocks5(bool interrupt)
|
|
|
|
{
|
|
|
|
interruptSocks5Recv = interrupt;
|
|
|
|
}
|