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
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <netbase.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <sync.h>
|
2018-04-13 19:15:35 +02:00
|
|
|
#include <tinyformat.h>
|
2022-10-26 13:25:11 +02:00
|
|
|
#include <util/sock.h>
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/strencodings.h>
|
Merge #17754: net: Don't allow resolving of std::string with embedded NUL characters. Add tests.
7a046cdc1423963bdcbcf9bb98560af61fa90b37 tests: Avoid using C-style NUL-terminated strings as arguments (practicalswift)
fefb9165f23fe9d10ad092ec31715f906e0d2ee7 tests: Add tests to make sure lookup methods fail on std::string parameters with embedded NUL characters (practicalswift)
9574de86ad703ad942cdd0eca79f48c0d42b102b net: Avoid using C-style NUL-terminated strings as arguments in the netbase interface (practicalswift)
Pull request description:
Don't allow resolving of `std::string`:s with embedded `NUL` characters.
Avoid using C-style `NUL`-terminated strings as arguments in the `netbase` interface
Add tests.
The only place in where C-style `NUL`-terminated strings are actually needed is here:
```diff
+ if (!ValidAsCString(name)) {
+ return false;
+ }
...
- int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes);
+ int nErr = getaddrinfo(name.c_str(), nullptr, &aiHint, &aiRes);
if (nErr)
return false;
```
Interface changes:
```diff
-bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
+bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
-bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup);
+bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup);
-bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
+bool Lookup(const std::string& name, CService& addr, int portDefault, bool fAllowLookup);
-bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
+bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
-bool LookupSubNet(const char *pszName, CSubNet& subnet);
+bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet);
-CService LookupNumeric(const char *pszName, int portDefault = 0);
+CService LookupNumeric(const std::string& name, int portDefault = 0);
-bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed);
+bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool& outProxyConnectionFailed);
```
It should be noted that the `ConnectThroughProxy` change (from `bool *outProxyConnectionFailed` to `bool& outProxyConnectionFailed`) has nothing to do with `NUL` handling but I thought it was worth doing when touching this file :)
ACKs for top commit:
EthanHeilman:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
laanwj:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
Tree-SHA512: 66556e290db996917b54091acd591df221f72230f6b9f6b167b9195ee870ebef6e26f4cda2f6f54d00e1c362e1743bf56785d0de7cae854e6bf7d26f6caccaba
2020-01-22 20:14:12 +01:00
|
|
|
#include <util/string.h>
|
|
|
|
#include <util/system.h>
|
2022-10-26 13:25:11 +02:00
|
|
|
#include <util/time.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2017-08-09 18:06:31 +02:00
|
|
|
#include <atomic>
|
2020-08-24 21:03:31 +02:00
|
|
|
#include <cstdint>
|
2022-10-26 13:25:11 +02:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
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>
|
2018-09-13 12:10:17 +02:00
|
|
|
#else
|
|
|
|
#include <codecvt>
|
2012-01-03 23:33:31 +01:00
|
|
|
#endif
|
|
|
|
|
2018-09-27 03:54:52 +02:00
|
|
|
#ifdef USE_POLL
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
2012-01-03 23:33:31 +01:00
|
|
|
|
2018-03-19 16:04:36 +01:00
|
|
|
#if !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-09-23 12:55:05 +02:00
|
|
|
static CCriticalSection cs_proxyInfos;
|
2018-11-28 17:07:52 +01:00
|
|
|
static proxyType proxyInfo[NET_MAX] GUARDED_BY(cs_proxyInfos);
|
|
|
|
static proxyType nameProxy GUARDED_BY(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)
|
2020-06-07 18:59:46 +02:00
|
|
|
int g_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
|
|
|
|
2020-06-29 11:44:12 +02:00
|
|
|
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};
|
|
|
|
|
2019-08-07 06:42:54 +02:00
|
|
|
enum Network ParseNetwork(const std::string& net_in) {
|
|
|
|
std::string net = ToLower(net_in);
|
2012-04-10 20:22:04 +02:00
|
|
|
if (net == "ipv4") return NET_IPV4;
|
|
|
|
if (net == "ipv6") return NET_IPV6;
|
2020-04-16 07:21:49 +02:00
|
|
|
if (net == "onion") return NET_ONION;
|
2018-06-27 07:42:30 +02:00
|
|
|
if (net == "tor") {
|
|
|
|
LogPrintf("Warning: net name 'tor' is deprecated and will be removed in the future. You should use 'onion' instead.\n");
|
2020-04-16 07:21:49 +02:00
|
|
|
return NET_ONION;
|
2018-06-27 07:42:30 +02:00
|
|
|
}
|
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";
|
2020-04-16 07:21:49 +02:00
|
|
|
case NET_ONION: return "onion";
|
2014-07-30 15:32:36 +02:00
|
|
|
default: return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 11:44:12 +02:00
|
|
|
static bool LookupIntern(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
|
2012-01-03 23:33:31 +01:00
|
|
|
{
|
|
|
|
vIP.clear();
|
2012-04-29 02:11:56 +02:00
|
|
|
|
Merge #17754: net: Don't allow resolving of std::string with embedded NUL characters. Add tests.
7a046cdc1423963bdcbcf9bb98560af61fa90b37 tests: Avoid using C-style NUL-terminated strings as arguments (practicalswift)
fefb9165f23fe9d10ad092ec31715f906e0d2ee7 tests: Add tests to make sure lookup methods fail on std::string parameters with embedded NUL characters (practicalswift)
9574de86ad703ad942cdd0eca79f48c0d42b102b net: Avoid using C-style NUL-terminated strings as arguments in the netbase interface (practicalswift)
Pull request description:
Don't allow resolving of `std::string`:s with embedded `NUL` characters.
Avoid using C-style `NUL`-terminated strings as arguments in the `netbase` interface
Add tests.
The only place in where C-style `NUL`-terminated strings are actually needed is here:
```diff
+ if (!ValidAsCString(name)) {
+ return false;
+ }
...
- int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes);
+ int nErr = getaddrinfo(name.c_str(), nullptr, &aiHint, &aiRes);
if (nErr)
return false;
```
Interface changes:
```diff
-bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
+bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
-bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup);
+bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup);
-bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
+bool Lookup(const std::string& name, CService& addr, int portDefault, bool fAllowLookup);
-bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
+bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
-bool LookupSubNet(const char *pszName, CSubNet& subnet);
+bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet);
-CService LookupNumeric(const char *pszName, int portDefault = 0);
+CService LookupNumeric(const std::string& name, int portDefault = 0);
-bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed);
+bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool& outProxyConnectionFailed);
```
It should be noted that the `ConnectThroughProxy` change (from `bool *outProxyConnectionFailed` to `bool& outProxyConnectionFailed`) has nothing to do with `NUL` handling but I thought it was worth doing when touching this file :)
ACKs for top commit:
EthanHeilman:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
laanwj:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
Tree-SHA512: 66556e290db996917b54091acd591df221f72230f6b9f6b167b9195ee870ebef6e26f4cda2f6f54d00e1c362e1743bf56785d0de7cae854e6bf7d26f6caccaba
2020-01-22 20:14:12 +01:00
|
|
|
if (!ValidAsCString(name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-29 02:11:56 +02:00
|
|
|
{
|
|
|
|
CNetAddr addr;
|
2019-07-15 21:21:10 +02:00
|
|
|
// From our perspective, onion addresses are not hostnames but rather
|
|
|
|
// direct encodings of CNetAddr much like IPv4 dotted-decimal notation
|
|
|
|
// or IPv6 colon-separated hextet notation. Since we can't use
|
|
|
|
// getaddrinfo to decode them and it wouldn't make sense to resolve
|
|
|
|
// them, we return a network address representing it instead. See
|
|
|
|
// CNetAddr::SetSpecial(const std::string&) for more details.
|
Merge #17754: net: Don't allow resolving of std::string with embedded NUL characters. Add tests.
7a046cdc1423963bdcbcf9bb98560af61fa90b37 tests: Avoid using C-style NUL-terminated strings as arguments (practicalswift)
fefb9165f23fe9d10ad092ec31715f906e0d2ee7 tests: Add tests to make sure lookup methods fail on std::string parameters with embedded NUL characters (practicalswift)
9574de86ad703ad942cdd0eca79f48c0d42b102b net: Avoid using C-style NUL-terminated strings as arguments in the netbase interface (practicalswift)
Pull request description:
Don't allow resolving of `std::string`:s with embedded `NUL` characters.
Avoid using C-style `NUL`-terminated strings as arguments in the `netbase` interface
Add tests.
The only place in where C-style `NUL`-terminated strings are actually needed is here:
```diff
+ if (!ValidAsCString(name)) {
+ return false;
+ }
...
- int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes);
+ int nErr = getaddrinfo(name.c_str(), nullptr, &aiHint, &aiRes);
if (nErr)
return false;
```
Interface changes:
```diff
-bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
+bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
-bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup);
+bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup);
-bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
+bool Lookup(const std::string& name, CService& addr, int portDefault, bool fAllowLookup);
-bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
+bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
-bool LookupSubNet(const char *pszName, CSubNet& subnet);
+bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet);
-CService LookupNumeric(const char *pszName, int portDefault = 0);
+CService LookupNumeric(const std::string& name, int portDefault = 0);
-bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed);
+bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool& outProxyConnectionFailed);
```
It should be noted that the `ConnectThroughProxy` change (from `bool *outProxyConnectionFailed` to `bool& outProxyConnectionFailed`) has nothing to do with `NUL` handling but I thought it was worth doing when touching this file :)
ACKs for top commit:
EthanHeilman:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
laanwj:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
Tree-SHA512: 66556e290db996917b54091acd591df221f72230f6b9f6b167b9195ee870ebef6e26f4cda2f6f54d00e1c362e1743bf56785d0de7cae854e6bf7d26f6caccaba
2020-01-22 20:14:12 +01:00
|
|
|
if (addr.SetSpecial(name)) {
|
2012-04-29 02:11:56 +02:00
|
|
|
vIP.push_back(addr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 11:44:12 +02:00
|
|
|
for (const CNetAddr& resolved : dns_lookup_function(name, fAllowLookup)) {
|
|
|
|
if (nMaxSolutions > 0 && vIP.size() >= nMaxSolutions) {
|
|
|
|
break;
|
2017-06-24 12:16:41 +02:00
|
|
|
}
|
|
|
|
/* Never allow resolving to an internal address. Consider any such result invalid */
|
|
|
|
if (!resolved.IsInternal()) {
|
|
|
|
vIP.push_back(resolved);
|
2012-01-03 23:33:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (vIP.size() > 0);
|
|
|
|
}
|
|
|
|
|
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)
|
2012-01-03 23:33:31 +01:00
|
|
|
{
|
Merge #17754: net: Don't allow resolving of std::string with embedded NUL characters. Add tests.
7a046cdc1423963bdcbcf9bb98560af61fa90b37 tests: Avoid using C-style NUL-terminated strings as arguments (practicalswift)
fefb9165f23fe9d10ad092ec31715f906e0d2ee7 tests: Add tests to make sure lookup methods fail on std::string parameters with embedded NUL characters (practicalswift)
9574de86ad703ad942cdd0eca79f48c0d42b102b net: Avoid using C-style NUL-terminated strings as arguments in the netbase interface (practicalswift)
Pull request description:
Don't allow resolving of `std::string`:s with embedded `NUL` characters.
Avoid using C-style `NUL`-terminated strings as arguments in the `netbase` interface
Add tests.
The only place in where C-style `NUL`-terminated strings are actually needed is here:
```diff
+ if (!ValidAsCString(name)) {
+ return false;
+ }
...
- int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes);
+ int nErr = getaddrinfo(name.c_str(), nullptr, &aiHint, &aiRes);
if (nErr)
return false;
```
Interface changes:
```diff
-bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
+bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
-bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup);
+bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup);
-bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
+bool Lookup(const std::string& name, CService& addr, int portDefault, bool fAllowLookup);
-bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
+bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
-bool LookupSubNet(const char *pszName, CSubNet& subnet);
+bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet);
-CService LookupNumeric(const char *pszName, int portDefault = 0);
+CService LookupNumeric(const std::string& name, int portDefault = 0);
-bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed);
+bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool& outProxyConnectionFailed);
```
It should be noted that the `ConnectThroughProxy` change (from `bool *outProxyConnectionFailed` to `bool& outProxyConnectionFailed`) has nothing to do with `NUL` handling but I thought it was worth doing when touching this file :)
ACKs for top commit:
EthanHeilman:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
laanwj:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
Tree-SHA512: 66556e290db996917b54091acd591df221f72230f6b9f6b167b9195ee870ebef6e26f4cda2f6f54d00e1c362e1743bf56785d0de7cae854e6bf7d26f6caccaba
2020-01-22 20:14:12 +01:00
|
|
|
if (!ValidAsCString(name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string strHost = name;
|
2013-04-27 14:27:05 +02:00
|
|
|
if (strHost.empty())
|
2012-01-03 23:33:31 +01:00
|
|
|
return false;
|
2018-07-24 20:49:54 +02:00
|
|
|
if (strHost.front() == '[' && strHost.back() == ']') {
|
2013-04-27 14:27:05 +02:00
|
|
|
strHost = strHost.substr(1, strHost.size() - 2);
|
2012-01-03 23:33:31 +01:00
|
|
|
}
|
|
|
|
|
2020-06-29 11:44:12 +02:00
|
|
|
return LookupIntern(strHost, vIP, nMaxSolutions, fAllowLookup, dns_lookup_function);
|
2012-01-03 23:33:31 +01:00
|
|
|
}
|
|
|
|
|
2020-06-29 11:44:12 +02:00
|
|
|
bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup, DNSLookupFn dns_lookup_function)
|
2017-09-03 15:29:10 +02:00
|
|
|
{
|
Merge #17754: net: Don't allow resolving of std::string with embedded NUL characters. Add tests.
7a046cdc1423963bdcbcf9bb98560af61fa90b37 tests: Avoid using C-style NUL-terminated strings as arguments (practicalswift)
fefb9165f23fe9d10ad092ec31715f906e0d2ee7 tests: Add tests to make sure lookup methods fail on std::string parameters with embedded NUL characters (practicalswift)
9574de86ad703ad942cdd0eca79f48c0d42b102b net: Avoid using C-style NUL-terminated strings as arguments in the netbase interface (practicalswift)
Pull request description:
Don't allow resolving of `std::string`:s with embedded `NUL` characters.
Avoid using C-style `NUL`-terminated strings as arguments in the `netbase` interface
Add tests.
The only place in where C-style `NUL`-terminated strings are actually needed is here:
```diff
+ if (!ValidAsCString(name)) {
+ return false;
+ }
...
- int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes);
+ int nErr = getaddrinfo(name.c_str(), nullptr, &aiHint, &aiRes);
if (nErr)
return false;
```
Interface changes:
```diff
-bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
+bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
-bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup);
+bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup);
-bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
+bool Lookup(const std::string& name, CService& addr, int portDefault, bool fAllowLookup);
-bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
+bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
-bool LookupSubNet(const char *pszName, CSubNet& subnet);
+bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet);
-CService LookupNumeric(const char *pszName, int portDefault = 0);
+CService LookupNumeric(const std::string& name, int portDefault = 0);
-bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed);
+bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool& outProxyConnectionFailed);
```
It should be noted that the `ConnectThroughProxy` change (from `bool *outProxyConnectionFailed` to `bool& outProxyConnectionFailed`) has nothing to do with `NUL` handling but I thought it was worth doing when touching this file :)
ACKs for top commit:
EthanHeilman:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
laanwj:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
Tree-SHA512: 66556e290db996917b54091acd591df221f72230f6b9f6b167b9195ee870ebef6e26f4cda2f6f54d00e1c362e1743bf56785d0de7cae854e6bf7d26f6caccaba
2020-01-22 20:14:12 +01:00
|
|
|
if (!ValidAsCString(name)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-03 15:29:10 +02:00
|
|
|
std::vector<CNetAddr> vIP;
|
2020-06-29 11:44:12 +02:00
|
|
|
LookupHost(name, vIP, 1, fAllowLookup, dns_lookup_function);
|
2017-09-03 15:29:10 +02:00
|
|
|
if(vIP.empty())
|
|
|
|
return false;
|
|
|
|
addr = vIP.front();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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)
|
2012-01-03 23:33:31 +01:00
|
|
|
{
|
Merge #17754: net: Don't allow resolving of std::string with embedded NUL characters. Add tests.
7a046cdc1423963bdcbcf9bb98560af61fa90b37 tests: Avoid using C-style NUL-terminated strings as arguments (practicalswift)
fefb9165f23fe9d10ad092ec31715f906e0d2ee7 tests: Add tests to make sure lookup methods fail on std::string parameters with embedded NUL characters (practicalswift)
9574de86ad703ad942cdd0eca79f48c0d42b102b net: Avoid using C-style NUL-terminated strings as arguments in the netbase interface (practicalswift)
Pull request description:
Don't allow resolving of `std::string`:s with embedded `NUL` characters.
Avoid using C-style `NUL`-terminated strings as arguments in the `netbase` interface
Add tests.
The only place in where C-style `NUL`-terminated strings are actually needed is here:
```diff
+ if (!ValidAsCString(name)) {
+ return false;
+ }
...
- int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes);
+ int nErr = getaddrinfo(name.c_str(), nullptr, &aiHint, &aiRes);
if (nErr)
return false;
```
Interface changes:
```diff
-bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
+bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
-bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup);
+bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup);
-bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
+bool Lookup(const std::string& name, CService& addr, int portDefault, bool fAllowLookup);
-bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
+bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
-bool LookupSubNet(const char *pszName, CSubNet& subnet);
+bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet);
-CService LookupNumeric(const char *pszName, int portDefault = 0);
+CService LookupNumeric(const std::string& name, int portDefault = 0);
-bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed);
+bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool& outProxyConnectionFailed);
```
It should be noted that the `ConnectThroughProxy` change (from `bool *outProxyConnectionFailed` to `bool& outProxyConnectionFailed`) has nothing to do with `NUL` handling but I thought it was worth doing when touching this file :)
ACKs for top commit:
EthanHeilman:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
laanwj:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
Tree-SHA512: 66556e290db996917b54091acd591df221f72230f6b9f6b167b9195ee870ebef6e26f4cda2f6f54d00e1c362e1743bf56785d0de7cae854e6bf7d26f6caccaba
2020-01-22 20:14:12 +01:00
|
|
|
if (name.empty() || !ValidAsCString(name)) {
|
2012-01-03 23:33:31 +01:00
|
|
|
return false;
|
Merge #17754: net: Don't allow resolving of std::string with embedded NUL characters. Add tests.
7a046cdc1423963bdcbcf9bb98560af61fa90b37 tests: Avoid using C-style NUL-terminated strings as arguments (practicalswift)
fefb9165f23fe9d10ad092ec31715f906e0d2ee7 tests: Add tests to make sure lookup methods fail on std::string parameters with embedded NUL characters (practicalswift)
9574de86ad703ad942cdd0eca79f48c0d42b102b net: Avoid using C-style NUL-terminated strings as arguments in the netbase interface (practicalswift)
Pull request description:
Don't allow resolving of `std::string`:s with embedded `NUL` characters.
Avoid using C-style `NUL`-terminated strings as arguments in the `netbase` interface
Add tests.
The only place in where C-style `NUL`-terminated strings are actually needed is here:
```diff
+ if (!ValidAsCString(name)) {
+ return false;
+ }
...
- int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes);
+ int nErr = getaddrinfo(name.c_str(), nullptr, &aiHint, &aiRes);
if (nErr)
return false;
```
Interface changes:
```diff
-bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
+bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
-bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup);
+bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup);
-bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
+bool Lookup(const std::string& name, CService& addr, int portDefault, bool fAllowLookup);
-bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
+bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
-bool LookupSubNet(const char *pszName, CSubNet& subnet);
+bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet);
-CService LookupNumeric(const char *pszName, int portDefault = 0);
+CService LookupNumeric(const std::string& name, int portDefault = 0);
-bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed);
+bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool& outProxyConnectionFailed);
```
It should be noted that the `ConnectThroughProxy` change (from `bool *outProxyConnectionFailed` to `bool& outProxyConnectionFailed`) has nothing to do with `NUL` handling but I thought it was worth doing when touching this file :)
ACKs for top commit:
EthanHeilman:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
laanwj:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
Tree-SHA512: 66556e290db996917b54091acd591df221f72230f6b9f6b167b9195ee870ebef6e26f4cda2f6f54d00e1c362e1743bf56785d0de7cae854e6bf7d26f6caccaba
2020-01-22 20:14:12 +01:00
|
|
|
}
|
2021-03-01 21:35:28 +01:00
|
|
|
uint16_t port{portDefault};
|
2018-03-01 20:50:33 +01:00
|
|
|
std::string hostname;
|
Merge #17754: net: Don't allow resolving of std::string with embedded NUL characters. Add tests.
7a046cdc1423963bdcbcf9bb98560af61fa90b37 tests: Avoid using C-style NUL-terminated strings as arguments (practicalswift)
fefb9165f23fe9d10ad092ec31715f906e0d2ee7 tests: Add tests to make sure lookup methods fail on std::string parameters with embedded NUL characters (practicalswift)
9574de86ad703ad942cdd0eca79f48c0d42b102b net: Avoid using C-style NUL-terminated strings as arguments in the netbase interface (practicalswift)
Pull request description:
Don't allow resolving of `std::string`:s with embedded `NUL` characters.
Avoid using C-style `NUL`-terminated strings as arguments in the `netbase` interface
Add tests.
The only place in where C-style `NUL`-terminated strings are actually needed is here:
```diff
+ if (!ValidAsCString(name)) {
+ return false;
+ }
...
- int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes);
+ int nErr = getaddrinfo(name.c_str(), nullptr, &aiHint, &aiRes);
if (nErr)
return false;
```
Interface changes:
```diff
-bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
+bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
-bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup);
+bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup);
-bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
+bool Lookup(const std::string& name, CService& addr, int portDefault, bool fAllowLookup);
-bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
+bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
-bool LookupSubNet(const char *pszName, CSubNet& subnet);
+bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet);
-CService LookupNumeric(const char *pszName, int portDefault = 0);
+CService LookupNumeric(const std::string& name, int portDefault = 0);
-bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed);
+bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool& outProxyConnectionFailed);
```
It should be noted that the `ConnectThroughProxy` change (from `bool *outProxyConnectionFailed` to `bool& outProxyConnectionFailed`) has nothing to do with `NUL` handling but I thought it was worth doing when touching this file :)
ACKs for top commit:
EthanHeilman:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
laanwj:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
Tree-SHA512: 66556e290db996917b54091acd591df221f72230f6b9f6b167b9195ee870ebef6e26f4cda2f6f54d00e1c362e1743bf56785d0de7cae854e6bf7d26f6caccaba
2020-01-22 20:14:12 +01:00
|
|
|
SplitHostPort(name, port, hostname);
|
2012-01-03 23:33:31 +01:00
|
|
|
|
|
|
|
std::vector<CNetAddr> vIP;
|
2020-06-29 11:44:12 +02:00
|
|
|
bool fRet = LookupIntern(hostname, vIP, nMaxSolutions, fAllowLookup, dns_lookup_function);
|
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;
|
|
|
|
}
|
|
|
|
|
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)
|
2011-12-17 01:48:03 +01:00
|
|
|
{
|
Merge #17754: net: Don't allow resolving of std::string with embedded NUL characters. Add tests.
7a046cdc1423963bdcbcf9bb98560af61fa90b37 tests: Avoid using C-style NUL-terminated strings as arguments (practicalswift)
fefb9165f23fe9d10ad092ec31715f906e0d2ee7 tests: Add tests to make sure lookup methods fail on std::string parameters with embedded NUL characters (practicalswift)
9574de86ad703ad942cdd0eca79f48c0d42b102b net: Avoid using C-style NUL-terminated strings as arguments in the netbase interface (practicalswift)
Pull request description:
Don't allow resolving of `std::string`:s with embedded `NUL` characters.
Avoid using C-style `NUL`-terminated strings as arguments in the `netbase` interface
Add tests.
The only place in where C-style `NUL`-terminated strings are actually needed is here:
```diff
+ if (!ValidAsCString(name)) {
+ return false;
+ }
...
- int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes);
+ int nErr = getaddrinfo(name.c_str(), nullptr, &aiHint, &aiRes);
if (nErr)
return false;
```
Interface changes:
```diff
-bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
+bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
-bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup);
+bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup);
-bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
+bool Lookup(const std::string& name, CService& addr, int portDefault, bool fAllowLookup);
-bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
+bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
-bool LookupSubNet(const char *pszName, CSubNet& subnet);
+bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet);
-CService LookupNumeric(const char *pszName, int portDefault = 0);
+CService LookupNumeric(const std::string& name, int portDefault = 0);
-bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed);
+bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool& outProxyConnectionFailed);
```
It should be noted that the `ConnectThroughProxy` change (from `bool *outProxyConnectionFailed` to `bool& outProxyConnectionFailed`) has nothing to do with `NUL` handling but I thought it was worth doing when touching this file :)
ACKs for top commit:
EthanHeilman:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
laanwj:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
Tree-SHA512: 66556e290db996917b54091acd591df221f72230f6b9f6b167b9195ee870ebef6e26f4cda2f6f54d00e1c362e1743bf56785d0de7cae854e6bf7d26f6caccaba
2020-01-22 20:14:12 +01:00
|
|
|
if (!ValidAsCString(name)) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-12-17 01:48:03 +01:00
|
|
|
std::vector<CService> vService;
|
2020-06-29 11:44:12 +02:00
|
|
|
bool fRet = Lookup(name, vService, portDefault, fAllowLookup, 1, dns_lookup_function);
|
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;
|
|
|
|
}
|
|
|
|
|
2021-03-01 21:35:28 +01:00
|
|
|
CService LookupNumeric(const std::string& name, uint16_t portDefault, DNSLookupFn dns_lookup_function)
|
2012-01-03 23:33:31 +01:00
|
|
|
{
|
Merge #17754: net: Don't allow resolving of std::string with embedded NUL characters. Add tests.
7a046cdc1423963bdcbcf9bb98560af61fa90b37 tests: Avoid using C-style NUL-terminated strings as arguments (practicalswift)
fefb9165f23fe9d10ad092ec31715f906e0d2ee7 tests: Add tests to make sure lookup methods fail on std::string parameters with embedded NUL characters (practicalswift)
9574de86ad703ad942cdd0eca79f48c0d42b102b net: Avoid using C-style NUL-terminated strings as arguments in the netbase interface (practicalswift)
Pull request description:
Don't allow resolving of `std::string`:s with embedded `NUL` characters.
Avoid using C-style `NUL`-terminated strings as arguments in the `netbase` interface
Add tests.
The only place in where C-style `NUL`-terminated strings are actually needed is here:
```diff
+ if (!ValidAsCString(name)) {
+ return false;
+ }
...
- int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes);
+ int nErr = getaddrinfo(name.c_str(), nullptr, &aiHint, &aiRes);
if (nErr)
return false;
```
Interface changes:
```diff
-bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
+bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
-bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup);
+bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup);
-bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
+bool Lookup(const std::string& name, CService& addr, int portDefault, bool fAllowLookup);
-bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
+bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
-bool LookupSubNet(const char *pszName, CSubNet& subnet);
+bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet);
-CService LookupNumeric(const char *pszName, int portDefault = 0);
+CService LookupNumeric(const std::string& name, int portDefault = 0);
-bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed);
+bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool& outProxyConnectionFailed);
```
It should be noted that the `ConnectThroughProxy` change (from `bool *outProxyConnectionFailed` to `bool& outProxyConnectionFailed`) has nothing to do with `NUL` handling but I thought it was worth doing when touching this file :)
ACKs for top commit:
EthanHeilman:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
laanwj:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
Tree-SHA512: 66556e290db996917b54091acd591df221f72230f6b9f6b167b9195ee870ebef6e26f4cda2f6f54d00e1c362e1743bf56785d0de7cae854e6bf7d26f6caccaba
2020-01-22 20:14:12 +01:00
|
|
|
if (!ValidAsCString(name)) {
|
|
|
|
return {};
|
|
|
|
}
|
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.
|
2020-06-29 11:44:12 +02:00
|
|
|
if(!Lookup(name, addr, portDefault, false, dns_lookup_function))
|
2017-09-03 15:29:10 +02:00
|
|
|
addr = CService();
|
|
|
|
return addr;
|
2012-01-03 23:33:31 +01:00
|
|
|
}
|
|
|
|
|
2017-09-28 05:09:49 +02:00
|
|
|
/** SOCKS version */
|
|
|
|
enum SOCKSVersion: uint8_t {
|
|
|
|
SOCKS4 = 0x04,
|
|
|
|
SOCKS5 = 0x05
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Values defined for METHOD in RFC1928 */
|
|
|
|
enum SOCKS5Method: uint8_t {
|
2018-10-01 03:13:42 +02:00
|
|
|
NOAUTH = 0x00, //!< No authentication required
|
|
|
|
GSSAPI = 0x01, //!< GSSAPI
|
|
|
|
USER_PASS = 0x02, //!< Username/password
|
|
|
|
NO_ACCEPTABLE = 0xff, //!< No acceptable methods
|
2017-09-28 05:09:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Values defined for CMD in RFC1928 */
|
|
|
|
enum SOCKS5Command: uint8_t {
|
|
|
|
CONNECT = 0x01,
|
|
|
|
BIND = 0x02,
|
|
|
|
UDP_ASSOCIATE = 0x03
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Values defined for REP in RFC1928 */
|
|
|
|
enum SOCKS5Reply: uint8_t {
|
2018-10-01 03:13:42 +02:00
|
|
|
SUCCEEDED = 0x00, //!< Succeeded
|
|
|
|
GENFAILURE = 0x01, //!< General failure
|
|
|
|
NOTALLOWED = 0x02, //!< Connection not allowed by ruleset
|
|
|
|
NETUNREACHABLE = 0x03, //!< Network unreachable
|
|
|
|
HOSTUNREACHABLE = 0x04, //!< Network unreachable
|
|
|
|
CONNREFUSED = 0x05, //!< Connection refused
|
|
|
|
TTLEXPIRED = 0x06, //!< TTL expired
|
|
|
|
CMDUNSUPPORTED = 0x07, //!< Command not supported
|
|
|
|
ATYPEUNSUPPORTED = 0x08, //!< Address type not supported
|
2017-09-28 05:09:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Values defined for ATYPE in RFC1928 */
|
|
|
|
enum SOCKS5Atyp: uint8_t {
|
|
|
|
IPV4 = 0x01,
|
|
|
|
DOMAINNAME = 0x03,
|
|
|
|
IPV6 = 0x04,
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Status codes that can be returned by InterruptibleRecv */
|
2019-01-03 10:18:47 +01:00
|
|
|
enum class IntrRecvError {
|
|
|
|
OK,
|
|
|
|
Timeout,
|
|
|
|
Disconnected,
|
|
|
|
NetworkError,
|
|
|
|
Interrupted
|
|
|
|
};
|
|
|
|
|
2014-09-08 13:49:56 +02:00
|
|
|
/**
|
2019-07-15 21:21:10 +02:00
|
|
|
* Try to read a specified number of bytes from a socket. Please read the "see
|
|
|
|
* also" section for more detail.
|
2014-09-08 13:49:56 +02:00
|
|
|
*
|
2019-07-15 21:21:10 +02:00
|
|
|
* @param data The buffer where the read bytes should be stored.
|
|
|
|
* @param len The number of bytes to read into the specified buffer.
|
|
|
|
* @param timeout The total timeout in milliseconds for this read.
|
2022-10-26 13:25:11 +02:00
|
|
|
* @param sock The socket (has to be in non-blocking mode) from which to read bytes.
|
2014-09-08 13:49:56 +02:00
|
|
|
*
|
2019-07-15 21:21:10 +02:00
|
|
|
* @returns An IntrRecvError indicating the resulting status of this read.
|
|
|
|
* IntrRecvError::OK only if all of the specified number of bytes were
|
|
|
|
* read.
|
|
|
|
*
|
|
|
|
* @see This function can be interrupted by calling InterruptSocks5(bool).
|
|
|
|
* Sockets can be made non-blocking with SetSocketNonBlocking(const
|
|
|
|
* SOCKET&, bool).
|
2014-09-08 13:49:56 +02:00
|
|
|
*/
|
2022-10-26 13:25:11 +02:00
|
|
|
static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, const Sock& sock)
|
2014-09-08 13:49:56 +02:00
|
|
|
{
|
|
|
|
int64_t curTime = GetTimeMillis();
|
|
|
|
int64_t endTime = curTime + timeout;
|
2019-07-15 21:21:10 +02:00
|
|
|
// 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.
|
2014-09-08 13:49:56 +02:00
|
|
|
const int64_t maxWait = 1000;
|
|
|
|
while (len > 0 && curTime < endTime) {
|
2022-10-26 13:25:11 +02:00
|
|
|
ssize_t ret = sock.Recv(data, len, 0); // Optimistically try the recv first
|
2014-09-08 13:49:56 +02:00
|
|
|
if (ret > 0) {
|
|
|
|
len -= ret;
|
|
|
|
data += ret;
|
|
|
|
} else if (ret == 0) { // Unexpected disconnection
|
2019-01-03 10:18:47 +01:00
|
|
|
return IntrRecvError::Disconnected;
|
2014-09-08 13:49:56 +02:00
|
|
|
} else { // Other error or blocking
|
|
|
|
int nErr = WSAGetLastError();
|
|
|
|
if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL) {
|
2019-07-15 21:21:10 +02:00
|
|
|
// Only wait at most maxWait milliseconds at a time, unless
|
|
|
|
// we're approaching the end of the specified total timeout
|
2018-09-27 03:54:52 +02:00
|
|
|
int timeout_ms = std::min(endTime - curTime, maxWait);
|
2022-10-26 13:25:11 +02:00
|
|
|
if (!sock.Wait(std::chrono::milliseconds{timeout_ms}, Sock::RECV)) {
|
2019-01-03 10:18:47 +01:00
|
|
|
return IntrRecvError::NetworkError;
|
2014-09-08 13:49:56 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-03 10:18:47 +01:00
|
|
|
return IntrRecvError::NetworkError;
|
2014-09-08 13:49:56 +02:00
|
|
|
}
|
|
|
|
}
|
2017-08-09 18:06:31 +02:00
|
|
|
if (interruptSocks5Recv)
|
2019-01-03 10:18:47 +01:00
|
|
|
return IntrRecvError::Interrupted;
|
2014-09-08 13:49:56 +02:00
|
|
|
curTime = GetTimeMillis();
|
2012-04-01 20:25:48 +02:00
|
|
|
}
|
2019-01-03 10:18:47 +01:00
|
|
|
return len == 0 ? IntrRecvError::OK : IntrRecvError::Timeout;
|
2012-04-01 20:25:48 +02:00
|
|
|
}
|
|
|
|
|
2017-10-18 17:01:17 +02:00
|
|
|
/** Convert SOCKS5 reply to an error message */
|
2018-05-04 22:42:39 +02:00
|
|
|
static std::string Socks5ErrorString(uint8_t err)
|
2016-05-19 08:45:27 +02:00
|
|
|
{
|
|
|
|
switch(err) {
|
2017-09-28 05:09:49 +02:00
|
|
|
case SOCKS5Reply::GENFAILURE:
|
|
|
|
return "general failure";
|
|
|
|
case SOCKS5Reply::NOTALLOWED:
|
|
|
|
return "connection not allowed";
|
|
|
|
case SOCKS5Reply::NETUNREACHABLE:
|
|
|
|
return "network unreachable";
|
|
|
|
case SOCKS5Reply::HOSTUNREACHABLE:
|
|
|
|
return "host unreachable";
|
|
|
|
case SOCKS5Reply::CONNREFUSED:
|
|
|
|
return "connection refused";
|
|
|
|
case SOCKS5Reply::TTLEXPIRED:
|
|
|
|
return "TTL expired";
|
|
|
|
case SOCKS5Reply::CMDUNSUPPORTED:
|
|
|
|
return "protocol error";
|
|
|
|
case SOCKS5Reply::ATYPEUNSUPPORTED:
|
|
|
|
return "address type not supported";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
2016-05-19 08:45:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 21:35:28 +01:00
|
|
|
bool Socks5(const std::string& strDest, uint16_t port, const ProxyCredentials* auth, const Sock& sock)
|
2012-04-01 20:25:48 +02:00
|
|
|
{
|
2019-01-03 10:18:47 +01:00
|
|
|
IntrRecvError recvr;
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::NET, "SOCKS5 connecting %s\n", strDest);
|
2015-03-16 16:30:49 +01:00
|
|
|
if (strDest.size() > 255) {
|
2012-04-19 17:02:21 +02:00
|
|
|
return error("Hostname too long");
|
|
|
|
}
|
2019-07-15 21:21:10 +02:00
|
|
|
// Construct the version identifier/method selection message
|
2015-03-16 16:30:49 +01:00
|
|
|
std::vector<uint8_t> vSocks5Init;
|
2019-07-15 21:21:10 +02:00
|
|
|
vSocks5Init.push_back(SOCKSVersion::SOCKS5); // We want the SOCK5 protocol
|
2015-03-16 16:30:49 +01:00
|
|
|
if (auth) {
|
2019-07-15 21:21:10 +02:00
|
|
|
vSocks5Init.push_back(0x02); // 2 method identifiers follow...
|
2017-09-28 05:09:49 +02:00
|
|
|
vSocks5Init.push_back(SOCKS5Method::NOAUTH);
|
|
|
|
vSocks5Init.push_back(SOCKS5Method::USER_PASS);
|
2015-03-16 16:30:49 +01:00
|
|
|
} else {
|
2019-07-15 21:21:10 +02:00
|
|
|
vSocks5Init.push_back(0x01); // 1 method identifier follows...
|
2017-09-28 05:09:49 +02:00
|
|
|
vSocks5Init.push_back(SOCKS5Method::NOAUTH);
|
2015-03-16 16:30:49 +01:00
|
|
|
}
|
2022-10-26 13:25:11 +02:00
|
|
|
ssize_t ret = sock.Send(vSocks5Init.data(), vSocks5Init.size(), MSG_NOSIGNAL);
|
2015-03-16 16:30:49 +01:00
|
|
|
if (ret != (ssize_t)vSocks5Init.size()) {
|
2012-04-01 20:25:48 +02:00
|
|
|
return error("Error sending to proxy");
|
|
|
|
}
|
2017-09-28 05:09:49 +02:00
|
|
|
uint8_t pchRet1[2];
|
2020-06-07 18:59:46 +02:00
|
|
|
if ((recvr = InterruptibleRecv(pchRet1, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
|
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
|
|
|
}
|
2017-09-28 05:09:49 +02:00
|
|
|
if (pchRet1[0] != SOCKSVersion::SOCKS5) {
|
2012-04-01 20:25:48 +02:00
|
|
|
return error("Proxy failed to initialize");
|
|
|
|
}
|
2017-09-28 05:09:49 +02:00
|
|
|
if (pchRet1[1] == SOCKS5Method::USER_PASS && auth) {
|
2015-03-16 16:30:49 +01:00
|
|
|
// Perform username/password authentication (as described in RFC1929)
|
|
|
|
std::vector<uint8_t> vAuth;
|
2017-09-28 05:09:49 +02:00
|
|
|
vAuth.push_back(0x01); // Current (and only) version of user/pass subnegotiation
|
2015-03-16 16:30:49 +01:00
|
|
|
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());
|
2022-10-26 13:25:11 +02:00
|
|
|
ret = sock.Send(vAuth.data(), vAuth.size(), MSG_NOSIGNAL);
|
2015-03-16 16:30:49 +01:00
|
|
|
if (ret != (ssize_t)vAuth.size()) {
|
|
|
|
return error("Error sending authentication to proxy");
|
|
|
|
}
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::PROXY, "SOCKS5 sending proxy authentication %s:%s\n", auth->username, auth->password);
|
2017-09-28 05:09:49 +02:00
|
|
|
uint8_t pchRetA[2];
|
2020-06-07 18:59:46 +02:00
|
|
|
if ((recvr = InterruptibleRecv(pchRetA, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
|
2015-03-16 16:30:49 +01:00
|
|
|
return error("Error reading proxy authentication response");
|
|
|
|
}
|
|
|
|
if (pchRetA[0] != 0x01 || pchRetA[1] != 0x00) {
|
2015-08-09 01:17:27 +02:00
|
|
|
return error("Proxy authentication unsuccessful");
|
2015-03-16 16:30:49 +01:00
|
|
|
}
|
2017-09-28 05:09:49 +02:00
|
|
|
} else if (pchRet1[1] == SOCKS5Method::NOAUTH) {
|
2015-03-16 16:30:49 +01:00
|
|
|
// Perform no authentication
|
|
|
|
} else {
|
|
|
|
return error("Proxy requested wrong authentication method %02x", pchRet1[1]);
|
|
|
|
}
|
|
|
|
std::vector<uint8_t> vSocks5;
|
2017-09-28 05:09:49 +02:00
|
|
|
vSocks5.push_back(SOCKSVersion::SOCKS5); // VER protocol version
|
|
|
|
vSocks5.push_back(SOCKS5Command::CONNECT); // CMD CONNECT
|
|
|
|
vSocks5.push_back(0x00); // RSV Reserved must be 0
|
|
|
|
vSocks5.push_back(SOCKS5Atyp::DOMAINNAME); // ATYP DOMAINNAME
|
2015-03-16 16:30:49 +01:00
|
|
|
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);
|
2022-10-26 13:25:11 +02:00
|
|
|
ret = sock.Send(vSocks5.data(), vSocks5.size(), MSG_NOSIGNAL);
|
2015-03-16 16:30:49 +01:00
|
|
|
if (ret != (ssize_t)vSocks5.size()) {
|
2012-04-01 20:25:48 +02:00
|
|
|
return error("Error sending to proxy");
|
|
|
|
}
|
2017-09-28 05:09:49 +02:00
|
|
|
uint8_t pchRet2[4];
|
2020-06-07 18:59:46 +02:00
|
|
|
if ((recvr = InterruptibleRecv(pchRet2, 4, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
|
2019-01-03 10:18:47 +01:00
|
|
|
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
|
|
|
|
* error message. */
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return error("Error while reading proxy response");
|
|
|
|
}
|
2012-04-01 20:25:48 +02:00
|
|
|
}
|
2017-09-28 05:09:49 +02:00
|
|
|
if (pchRet2[0] != SOCKSVersion::SOCKS5) {
|
2012-04-01 20:25:48 +02:00
|
|
|
return error("Proxy failed to accept request");
|
|
|
|
}
|
2017-09-28 05:09:49 +02:00
|
|
|
if (pchRet2[1] != SOCKS5Reply::SUCCEEDED) {
|
2016-05-19 08:45:27 +02:00
|
|
|
// Failures to connect to a peer that are not proxy errors
|
|
|
|
LogPrintf("Socks5() connect to %s:%d failed: %s\n", strDest, port, Socks5ErrorString(pchRet2[1]));
|
|
|
|
return false;
|
2012-04-01 20:25:48 +02:00
|
|
|
}
|
2017-09-28 05:09:49 +02:00
|
|
|
if (pchRet2[2] != 0x00) { // Reserved field must be 0
|
2012-04-01 20:25:48 +02:00
|
|
|
return error("Error: malformed proxy response");
|
|
|
|
}
|
2017-09-28 05:09:49 +02:00
|
|
|
uint8_t pchRet3[256];
|
2012-04-01 20:25:48 +02:00
|
|
|
switch (pchRet2[3])
|
|
|
|
{
|
2020-06-07 18:59:46 +02:00
|
|
|
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;
|
2017-09-28 05:09:49 +02:00
|
|
|
case SOCKS5Atyp::DOMAINNAME:
|
2012-04-01 20:25:48 +02:00
|
|
|
{
|
2020-06-07 18:59:46 +02:00
|
|
|
recvr = InterruptibleRecv(pchRet3, 1, g_socks5_recv_timeout, sock);
|
2019-01-03 10:18:47 +01:00
|
|
|
if (recvr != IntrRecvError::OK) {
|
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];
|
2020-06-07 18:59:46 +02:00
|
|
|
recvr = InterruptibleRecv(pchRet3, nRecv, g_socks5_recv_timeout, sock);
|
2012-04-01 20:25:48 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-12-13 05:09:57 +01:00
|
|
|
default: return error("Error: malformed proxy response");
|
2012-04-01 20:25:48 +02:00
|
|
|
}
|
2019-01-03 10:18:47 +01:00
|
|
|
if (recvr != IntrRecvError::OK) {
|
2012-04-01 20:25:48 +02:00
|
|
|
return error("Error reading from proxy");
|
|
|
|
}
|
2020-06-07 18:59:46 +02:00
|
|
|
if ((recvr = InterruptibleRecv(pchRet3, 2, g_socks5_recv_timeout, sock)) != IntrRecvError::OK) {
|
2012-04-01 20:25:48 +02:00
|
|
|
return error("Error reading from proxy");
|
|
|
|
}
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::NET, "SOCKS5 connected %s\n", strDest);
|
2012-04-01 20:25:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-26 13:25:11 +02:00
|
|
|
std::unique_ptr<Sock> CreateSockTCP(const CService& address_family)
|
2012-01-03 23:33:31 +01:00
|
|
|
{
|
2019-07-15 21:21:10 +02:00
|
|
|
// Create a sockaddr from the specified service.
|
2012-05-11 15:28:59 +02:00
|
|
|
struct sockaddr_storage sockaddr;
|
|
|
|
socklen_t len = sizeof(sockaddr);
|
2022-10-26 13:25:11 +02:00
|
|
|
if (!address_family.GetSockAddr((struct sockaddr*)&sockaddr, &len)) {
|
|
|
|
LogPrintf("Cannot create socket for %s: unsupported network\n", address_family.ToString());
|
|
|
|
return nullptr;
|
2012-03-31 17:58:25 +02:00
|
|
|
}
|
|
|
|
|
2019-07-15 21:21:10 +02:00
|
|
|
// Create a TCP socket in the address family of the specified service.
|
2012-05-11 15:28:59 +02:00
|
|
|
SOCKET hSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
|
2022-10-26 13:25:11 +02:00
|
|
|
if (hSocket == INVALID_SOCKET) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-12-13 05:09:57 +01:00
|
|
|
|
2019-07-15 21:21:10 +02:00
|
|
|
// Ensure that waiting for I/O on this socket won't result in undefined
|
|
|
|
// behavior.
|
2017-12-13 05:09:57 +01:00
|
|
|
if (!IsSelectableSocket(hSocket)) {
|
|
|
|
CloseSocket(hSocket);
|
|
|
|
LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
|
2022-10-26 13:25:11 +02:00
|
|
|
return nullptr;
|
2017-12-13 05:09:57 +01:00
|
|
|
}
|
2014-07-09 11:00:00 +02:00
|
|
|
|
2015-10-22 01:52:29 +02:00
|
|
|
#ifdef SO_NOSIGPIPE
|
2017-05-18 02:26:54 +02:00
|
|
|
int set = 1;
|
2019-07-15 21:21:10 +02:00
|
|
|
// Set the no-sigpipe option on the socket for BSD systems, other UNIXes
|
|
|
|
// should use the MSG_NOSIGNAL flag for every send.
|
2012-01-03 23:33:31 +01:00
|
|
|
setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
|
|
|
|
#endif
|
|
|
|
|
2019-07-15 21:21:10 +02:00
|
|
|
// Set the no-delay option (disable Nagle's algorithm) on the TCP socket.
|
2017-05-18 02:26:54 +02:00
|
|
|
SetSocketNoDelay(hSocket);
|
2015-10-22 01:52:29 +02:00
|
|
|
|
2019-07-15 21:21:10 +02:00
|
|
|
// Set the non-blocking option on the socket.
|
2017-07-24 14:58:25 +02:00
|
|
|
if (!SetSocketNonBlocking(hSocket, true)) {
|
|
|
|
CloseSocket(hSocket);
|
2022-10-26 13:25:11 +02:00
|
|
|
LogPrintf("Error setting socket to non-blocking: %s\n", NetworkErrorString(WSAGetLastError()));
|
|
|
|
return nullptr;
|
2017-07-24 14:58:25 +02:00
|
|
|
}
|
2022-10-26 13:25:11 +02:00
|
|
|
return std::make_unique<Sock>(hSocket);
|
2017-12-13 05:09:57 +01:00
|
|
|
}
|
2012-01-03 23:33:31 +01:00
|
|
|
|
2022-10-26 13:25:11 +02:00
|
|
|
std::function<std::unique_ptr<Sock>(const CService&)> CreateSock = CreateSockTCP;
|
|
|
|
|
2018-04-13 19:15:35 +02:00
|
|
|
template<typename... Args>
|
|
|
|
static void LogConnectFailure(bool manual_connection, const char* fmt, const Args&... args) {
|
|
|
|
std::string error_message = tfm::format(fmt, args...);
|
|
|
|
if (manual_connection) {
|
|
|
|
LogPrintf("%s\n", error_message);
|
|
|
|
} else {
|
|
|
|
LogPrint(BCLog::NET, "%s\n", error_message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocket, int nTimeout, bool manual_connection)
|
2017-12-13 05:09:57 +01:00
|
|
|
{
|
2019-07-15 21:21:10 +02:00
|
|
|
// Create a sockaddr from the specified service.
|
2017-12-13 05:09:57 +01:00
|
|
|
struct sockaddr_storage sockaddr;
|
|
|
|
socklen_t len = sizeof(sockaddr);
|
|
|
|
if (hSocket == INVALID_SOCKET) {
|
|
|
|
LogPrintf("Cannot connect to %s: invalid socket\n", addrConnect.ToString());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!addrConnect.GetSockAddr((struct sockaddr*)&sockaddr, &len)) {
|
|
|
|
LogPrintf("Cannot connect to %s: unsupported network\n", addrConnect.ToString());
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-15 21:21:10 +02:00
|
|
|
|
|
|
|
// Connect to the addrConnect service on the hSocket socket.
|
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
|
|
|
{
|
2019-07-15 21:21:10 +02:00
|
|
|
// 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.
|
2018-09-27 03:54:52 +02:00
|
|
|
#ifdef USE_POLL
|
|
|
|
struct pollfd pollfd = {};
|
|
|
|
pollfd.fd = hSocket;
|
|
|
|
pollfd.events = POLLIN | POLLOUT;
|
|
|
|
int nRet = poll(&pollfd, 1, nTimeout);
|
|
|
|
#else
|
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);
|
2019-08-06 05:08:33 +02:00
|
|
|
int nRet = select(hSocket + 1, nullptr, &fdset, nullptr, &timeout);
|
2018-09-27 03:54:52 +02:00
|
|
|
#endif
|
2019-07-15 21:21:10 +02:00
|
|
|
// 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.
|
2012-01-03 23:33:31 +01:00
|
|
|
if (nRet == 0)
|
|
|
|
{
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::NET, "connection to %s timeout\n", addrConnect.ToString());
|
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()));
|
2012-01-03 23:33:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-07-15 21:21:10 +02:00
|
|
|
|
|
|
|
// Even if the select/poll 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.
|
2012-01-03 23:33:31 +01:00
|
|
|
socklen_t nRetSize = sizeof(nRet);
|
2018-04-19 14:24:26 +02:00
|
|
|
if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (sockopt_arg_type)&nRet, &nRetSize) == SOCKET_ERROR)
|
2012-01-03 23:33:31 +01:00
|
|
|
{
|
2014-05-08 14:15:19 +02:00
|
|
|
LogPrintf("getsockopt() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
|
2012-01-03 23:33:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (nRet != 0)
|
|
|
|
{
|
2018-04-13 19:15:35 +02:00
|
|
|
LogConnectFailure(manual_connection, "connect() to %s failed after select(): %s", addrConnect.ToString(), NetworkErrorString(nRet));
|
2012-01-03 23:33:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef WIN32
|
|
|
|
else if (WSAGetLastError() != WSAEISCONN)
|
|
|
|
#else
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2018-04-13 19:15:35 +02:00
|
|
|
LogConnectFailure(manual_connection, "connect() to %s failed: %s", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
|
2012-01-03 23:33:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 17:02:21 +02:00
|
|
|
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++) {
|
2018-02-07 22:15:16 +01:00
|
|
|
if (addr == static_cast<CNetAddr>(proxyInfo[i].proxy))
|
2012-05-24 19:02:21 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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)
|
2012-04-19 17:02:21 +02:00
|
|
|
{
|
2012-05-24 19:02:21 +02:00
|
|
|
// first connect to proxy server
|
2022-10-26 13:25:11 +02:00
|
|
|
if (!ConnectSocketDirectly(proxy.proxy, sock.Get(), nTimeout, true)) {
|
Merge #17754: net: Don't allow resolving of std::string with embedded NUL characters. Add tests.
7a046cdc1423963bdcbcf9bb98560af61fa90b37 tests: Avoid using C-style NUL-terminated strings as arguments (practicalswift)
fefb9165f23fe9d10ad092ec31715f906e0d2ee7 tests: Add tests to make sure lookup methods fail on std::string parameters with embedded NUL characters (practicalswift)
9574de86ad703ad942cdd0eca79f48c0d42b102b net: Avoid using C-style NUL-terminated strings as arguments in the netbase interface (practicalswift)
Pull request description:
Don't allow resolving of `std::string`:s with embedded `NUL` characters.
Avoid using C-style `NUL`-terminated strings as arguments in the `netbase` interface
Add tests.
The only place in where C-style `NUL`-terminated strings are actually needed is here:
```diff
+ if (!ValidAsCString(name)) {
+ return false;
+ }
...
- int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes);
+ int nErr = getaddrinfo(name.c_str(), nullptr, &aiHint, &aiRes);
if (nErr)
return false;
```
Interface changes:
```diff
-bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
+bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
-bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup);
+bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup);
-bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
+bool Lookup(const std::string& name, CService& addr, int portDefault, bool fAllowLookup);
-bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
+bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
-bool LookupSubNet(const char *pszName, CSubNet& subnet);
+bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet);
-CService LookupNumeric(const char *pszName, int portDefault = 0);
+CService LookupNumeric(const std::string& name, int portDefault = 0);
-bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed);
+bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool& outProxyConnectionFailed);
```
It should be noted that the `ConnectThroughProxy` change (from `bool *outProxyConnectionFailed` to `bool& outProxyConnectionFailed`) has nothing to do with `NUL` handling but I thought it was worth doing when touching this file :)
ACKs for top commit:
EthanHeilman:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
laanwj:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
Tree-SHA512: 66556e290db996917b54091acd591df221f72230f6b9f6b167b9195ee870ebef6e26f4cda2f6f54d00e1c362e1743bf56785d0de7cae854e6bf7d26f6caccaba
2020-01-22 20:14:12 +01:00
|
|
|
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;
|
2017-07-14 23:33:10 +02:00
|
|
|
static std::atomic_int counter(0);
|
2016-10-18 15:38:44 +02:00
|
|
|
random_auth.username = random_auth.password = strprintf("%i", counter++);
|
2021-03-01 21:35:28 +01:00
|
|
|
if (!Socks5(strDest, port, &random_auth, sock)) {
|
2015-03-16 16:30:49 +01:00
|
|
|
return false;
|
2017-12-13 05:09:57 +01:00
|
|
|
}
|
2015-03-16 16:30:49 +01:00
|
|
|
} else {
|
2021-03-01 21:35:28 +01:00
|
|
|
if (!Socks5(strDest, port, 0, sock)) {
|
2015-03-16 16:30:49 +01:00
|
|
|
return false;
|
2017-12-13 05:09:57 +01:00
|
|
|
}
|
2015-03-16 16:30:49 +01:00
|
|
|
}
|
2012-01-03 23:33:31 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-04-13 19:15:35 +02:00
|
|
|
|
2020-06-29 11:44:12 +02:00
|
|
|
bool LookupSubNet(const std::string& strSubnet, CSubNet& ret, DNSLookupFn dns_lookup_function)
|
2014-04-28 11:08:57 +02:00
|
|
|
{
|
Merge #17754: net: Don't allow resolving of std::string with embedded NUL characters. Add tests.
7a046cdc1423963bdcbcf9bb98560af61fa90b37 tests: Avoid using C-style NUL-terminated strings as arguments (practicalswift)
fefb9165f23fe9d10ad092ec31715f906e0d2ee7 tests: Add tests to make sure lookup methods fail on std::string parameters with embedded NUL characters (practicalswift)
9574de86ad703ad942cdd0eca79f48c0d42b102b net: Avoid using C-style NUL-terminated strings as arguments in the netbase interface (practicalswift)
Pull request description:
Don't allow resolving of `std::string`:s with embedded `NUL` characters.
Avoid using C-style `NUL`-terminated strings as arguments in the `netbase` interface
Add tests.
The only place in where C-style `NUL`-terminated strings are actually needed is here:
```diff
+ if (!ValidAsCString(name)) {
+ return false;
+ }
...
- int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes);
+ int nErr = getaddrinfo(name.c_str(), nullptr, &aiHint, &aiRes);
if (nErr)
return false;
```
Interface changes:
```diff
-bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
+bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
-bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup);
+bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup);
-bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
+bool Lookup(const std::string& name, CService& addr, int portDefault, bool fAllowLookup);
-bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
+bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
-bool LookupSubNet(const char *pszName, CSubNet& subnet);
+bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet);
-CService LookupNumeric(const char *pszName, int portDefault = 0);
+CService LookupNumeric(const std::string& name, int portDefault = 0);
-bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed);
+bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool& outProxyConnectionFailed);
```
It should be noted that the `ConnectThroughProxy` change (from `bool *outProxyConnectionFailed` to `bool& outProxyConnectionFailed`) has nothing to do with `NUL` handling but I thought it was worth doing when touching this file :)
ACKs for top commit:
EthanHeilman:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
laanwj:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
Tree-SHA512: 66556e290db996917b54091acd591df221f72230f6b9f6b167b9195ee870ebef6e26f4cda2f6f54d00e1c362e1743bf56785d0de7cae854e6bf7d26f6caccaba
2020-01-22 20:14:12 +01:00
|
|
|
if (!ValidAsCString(strSubnet)) {
|
|
|
|
return false;
|
|
|
|
}
|
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);
|
Merge #17754: net: Don't allow resolving of std::string with embedded NUL characters. Add tests.
7a046cdc1423963bdcbcf9bb98560af61fa90b37 tests: Avoid using C-style NUL-terminated strings as arguments (practicalswift)
fefb9165f23fe9d10ad092ec31715f906e0d2ee7 tests: Add tests to make sure lookup methods fail on std::string parameters with embedded NUL characters (practicalswift)
9574de86ad703ad942cdd0eca79f48c0d42b102b net: Avoid using C-style NUL-terminated strings as arguments in the netbase interface (practicalswift)
Pull request description:
Don't allow resolving of `std::string`:s with embedded `NUL` characters.
Avoid using C-style `NUL`-terminated strings as arguments in the `netbase` interface
Add tests.
The only place in where C-style `NUL`-terminated strings are actually needed is here:
```diff
+ if (!ValidAsCString(name)) {
+ return false;
+ }
...
- int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes);
+ int nErr = getaddrinfo(name.c_str(), nullptr, &aiHint, &aiRes);
if (nErr)
return false;
```
Interface changes:
```diff
-bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
+bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
-bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup);
+bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup);
-bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
+bool Lookup(const std::string& name, CService& addr, int portDefault, bool fAllowLookup);
-bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
+bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
-bool LookupSubNet(const char *pszName, CSubNet& subnet);
+bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet);
-CService LookupNumeric(const char *pszName, int portDefault = 0);
+CService LookupNumeric(const std::string& name, int portDefault = 0);
-bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed);
+bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool& outProxyConnectionFailed);
```
It should be noted that the `ConnectThroughProxy` change (from `bool *outProxyConnectionFailed` to `bool& outProxyConnectionFailed`) has nothing to do with `NUL` handling but I thought it was worth doing when touching this file :)
ACKs for top commit:
EthanHeilman:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
laanwj:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
Tree-SHA512: 66556e290db996917b54091acd591df221f72230f6b9f6b167b9195ee870ebef6e26f4cda2f6f54d00e1c362e1743bf56785d0de7cae854e6bf7d26f6caccaba
2020-01-22 20:14:12 +01:00
|
|
|
// TODO: Use LookupHost(const std::string&, CNetAddr&, bool) instead to just get
|
2019-07-15 21:21:10 +02:00
|
|
|
// one CNetAddr.
|
2020-06-29 11:44:12 +02:00
|
|
|
if (LookupHost(strAddress, vIP, 1, false, dns_lookup_function))
|
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);
|
2020-08-24 21:03:31 +02:00
|
|
|
uint8_t n;
|
|
|
|
if (ParseUInt8(strNetmask, &n)) {
|
|
|
|
// If valid number, assume CIDR variable-length subnet masking
|
2017-09-03 15:29:10 +02:00
|
|
|
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
|
2020-06-29 11:44:12 +02:00
|
|
|
if (LookupHost(strNetmask, vIP, 1, false, dns_lookup_function)) {
|
2017-09-03 15:29:10 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-24 14:58:25 +02:00
|
|
|
bool SetSocketNonBlocking(const SOCKET& hSocket, bool fNonBlocking)
|
2014-07-09 11:00:00 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
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
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-08-09 18:06:31 +02:00
|
|
|
|
2017-07-24 14:58:25 +02:00
|
|
|
bool SetSocketNoDelay(const SOCKET& hSocket)
|
2017-05-18 02:26:54 +02:00
|
|
|
{
|
|
|
|
int set = 1;
|
|
|
|
int rc = setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int));
|
|
|
|
return rc == 0;
|
|
|
|
}
|
|
|
|
|
2017-08-09 18:06:31 +02:00
|
|
|
void InterruptSocks5(bool interrupt)
|
|
|
|
{
|
|
|
|
interruptSocks5Recv = interrupt;
|
|
|
|
}
|