mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-09 10:43:19 -05:00
net: Use steady clock in InterruptibleRecv
This commit is contained in:
parent
5150e28010
commit
fa454dcb20
2 changed files with 12 additions and 12 deletions
|
@ -36,8 +36,8 @@ static Proxy nameProxy GUARDED_BY(g_proxyinfo_mutex);
|
||||||
int nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
|
int nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
|
||||||
bool fNameLookup = DEFAULT_NAME_LOOKUP;
|
bool fNameLookup = DEFAULT_NAME_LOOKUP;
|
||||||
|
|
||||||
// Need ample time for negotiation for very slow proxies such as Tor (milliseconds)
|
// Need ample time for negotiation for very slow proxies such as Tor
|
||||||
int g_socks5_recv_timeout = 20 * 1000;
|
std::chrono::milliseconds g_socks5_recv_timeout = 20s;
|
||||||
static std::atomic<bool> interruptSocks5Recv(false);
|
static std::atomic<bool> interruptSocks5Recv(false);
|
||||||
|
|
||||||
std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup)
|
std::vector<CNetAddr> WrappedGetAddrInfo(const std::string& name, bool allow_lookup)
|
||||||
|
@ -296,7 +296,7 @@ enum class IntrRecvError {
|
||||||
*
|
*
|
||||||
* @param data The buffer where the read bytes should be stored.
|
* @param data The buffer where the read bytes should be stored.
|
||||||
* @param len The number of bytes to read into the specified buffer.
|
* @param len The number of bytes to read into the specified buffer.
|
||||||
* @param timeout The total timeout in milliseconds for this read.
|
* @param timeout The total timeout for this read.
|
||||||
* @param sock The socket (has to be in non-blocking mode) from which to read bytes.
|
* @param sock The socket (has to be in non-blocking mode) from which to read bytes.
|
||||||
*
|
*
|
||||||
* @returns An IntrRecvError indicating the resulting status of this read.
|
* @returns An IntrRecvError indicating the resulting status of this read.
|
||||||
|
@ -306,10 +306,10 @@ enum class IntrRecvError {
|
||||||
* @see This function can be interrupted by calling InterruptSocks5(bool).
|
* @see This function can be interrupted by calling InterruptSocks5(bool).
|
||||||
* Sockets can be made non-blocking with Sock::SetNonBlocking().
|
* Sockets can be made non-blocking with Sock::SetNonBlocking().
|
||||||
*/
|
*/
|
||||||
static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, const Sock& sock)
|
static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, std::chrono::milliseconds timeout, const Sock& sock)
|
||||||
{
|
{
|
||||||
int64_t curTime = GetTimeMillis();
|
auto curTime{Now<SteadyMilliseconds>()};
|
||||||
int64_t endTime = curTime + timeout;
|
const auto endTime{curTime + timeout};
|
||||||
while (len > 0 && curTime < endTime) {
|
while (len > 0 && curTime < endTime) {
|
||||||
ssize_t ret = sock.Recv(data, len, 0); // Optimistically try the recv first
|
ssize_t ret = sock.Recv(data, len, 0); // Optimistically try the recv first
|
||||||
if (ret > 0) {
|
if (ret > 0) {
|
||||||
|
@ -333,7 +333,7 @@ static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, c
|
||||||
}
|
}
|
||||||
if (interruptSocks5Recv)
|
if (interruptSocks5Recv)
|
||||||
return IntrRecvError::Interrupted;
|
return IntrRecvError::Interrupted;
|
||||||
curTime = GetTimeMillis();
|
curTime = Now<SteadyMilliseconds>();
|
||||||
}
|
}
|
||||||
return len == 0 ? IntrRecvError::OK : IntrRecvError::Timeout;
|
return len == 0 ? IntrRecvError::OK : IntrRecvError::Timeout;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,11 +14,11 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace {
|
extern std::chrono::milliseconds g_socks5_recv_timeout;
|
||||||
int default_socks5_recv_timeout;
|
|
||||||
};
|
|
||||||
|
|
||||||
extern int g_socks5_recv_timeout;
|
namespace {
|
||||||
|
decltype(g_socks5_recv_timeout) default_socks5_recv_timeout;
|
||||||
|
};
|
||||||
|
|
||||||
void initialize_socks5()
|
void initialize_socks5()
|
||||||
{
|
{
|
||||||
|
@ -35,7 +35,7 @@ FUZZ_TARGET_INIT(socks5, initialize_socks5)
|
||||||
InterruptSocks5(fuzzed_data_provider.ConsumeBool());
|
InterruptSocks5(fuzzed_data_provider.ConsumeBool());
|
||||||
// Set FUZZED_SOCKET_FAKE_LATENCY=1 to exercise recv timeout code paths. This
|
// Set FUZZED_SOCKET_FAKE_LATENCY=1 to exercise recv timeout code paths. This
|
||||||
// will slow down fuzzing.
|
// will slow down fuzzing.
|
||||||
g_socks5_recv_timeout = (fuzzed_data_provider.ConsumeBool() && std::getenv("FUZZED_SOCKET_FAKE_LATENCY") != nullptr) ? 1 : default_socks5_recv_timeout;
|
g_socks5_recv_timeout = (fuzzed_data_provider.ConsumeBool() && std::getenv("FUZZED_SOCKET_FAKE_LATENCY") != nullptr) ? 1ms : default_socks5_recv_timeout;
|
||||||
FuzzedSock fuzzed_sock = ConsumeSock(fuzzed_data_provider);
|
FuzzedSock fuzzed_sock = ConsumeSock(fuzzed_data_provider);
|
||||||
// This Socks5(...) fuzzing harness would have caught CVE-2017-18350 within
|
// This Socks5(...) fuzzing harness would have caught CVE-2017-18350 within
|
||||||
// a few seconds of fuzzing.
|
// a few seconds of fuzzing.
|
||||||
|
|
Loading…
Add table
Reference in a new issue