mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 22:50:59 +01:00
Merge #21387: p2p: Refactor sock to add I2P fuzz and unit tests
40316a37cbtest: add I2P test for a runaway SAM proxy (Vasil Dimov)2d8ac77970fuzz: add tests for the I2P Session public interface (Vasil Dimov)9947e44de0i2p: use pointers to Sock to accommodate mocking (Vasil Dimov)82d360b5a8net: change ConnectSocketDirectly() to take a Sock argument (Vasil Dimov)b5861100f8net: add connect() and getsockopt() wrappers to Sock (Vasil Dimov)5a887d49b2fuzz: avoid FuzzedSock::Recv() repeated errors with EAGAIN (Vasil Dimov)3088f83d01fuzz: extend FuzzedSock::Recv() to support MSG_PEEK (Vasil Dimov)9b05c49adefuzz: implement unimplemented FuzzedSock methods (Vasil Dimov) Pull request description: Change the networking code and the I2P code to be fully mockable and use `FuzzedSocket` to fuzz the I2P methods `Listen()`, `Accept()` and `Connect()`. Add a mocked `Sock` implementation that returns a predefined data on reads and use it for a regression unit test for the bug fixed in https://github.com/bitcoin/bitcoin/pull/21407. ACKs for top commit: practicalswift: Tested ACK40316a37cbMarcoFalke: Concept ACK40316a37cbjonatack: re-ACK40316a37cbreviewed `git range-diff01bb3afb23c861d 40316a3` and the new unit test commit, debug built, ran unit tests, ran bitcoind with an I2P service and network operation with seven I2P peers (2 in, 5 out) is looking nominal laanwj: Code review ACK40316a37cbTree-SHA512: 7fc4f129849e16e0c7e16662d9f4d35dfcc369bb31450ee369a2b97bdca95285533bee7787983e881e5a3d248f912afb42b4a2299d5860ace7129b0b19623cc8
This commit is contained in:
@@ -537,12 +537,12 @@ static void LogConnectFailure(bool manual_connection, const char* fmt, const Arg
|
||||
}
|
||||
}
|
||||
|
||||
bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocket, int nTimeout, bool manual_connection)
|
||||
bool ConnectSocketDirectly(const CService &addrConnect, const Sock& sock, int nTimeout, bool manual_connection)
|
||||
{
|
||||
// Create a sockaddr from the specified service.
|
||||
struct sockaddr_storage sockaddr;
|
||||
socklen_t len = sizeof(sockaddr);
|
||||
if (hSocket == INVALID_SOCKET) {
|
||||
if (sock.Get() == INVALID_SOCKET) {
|
||||
LogPrintf("Cannot connect to %s: invalid socket\n", addrConnect.ToString());
|
||||
return false;
|
||||
}
|
||||
@@ -552,8 +552,7 @@ bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocket, i
|
||||
}
|
||||
|
||||
// Connect to the addrConnect service on the hSocket socket.
|
||||
if (connect(hSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
|
||||
{
|
||||
if (sock.Connect(reinterpret_cast<struct sockaddr*>(&sockaddr), len) == SOCKET_ERROR) {
|
||||
int nErr = WSAGetLastError();
|
||||
// WSAEINVAL is here because some legacy version of winsock uses it
|
||||
if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL)
|
||||
@@ -561,46 +560,34 @@ bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocket, i
|
||||
// Connection didn't actually fail, but is being established
|
||||
// asynchronously. Thus, use async I/O api (select/poll)
|
||||
// synchronously to check for successful connection with a timeout.
|
||||
#ifdef USE_POLL
|
||||
struct pollfd pollfd = {};
|
||||
pollfd.fd = hSocket;
|
||||
pollfd.events = POLLIN | POLLOUT;
|
||||
int nRet = poll(&pollfd, 1, nTimeout);
|
||||
#else
|
||||
struct timeval timeout = MillisToTimeval(nTimeout);
|
||||
fd_set fdset;
|
||||
FD_ZERO(&fdset);
|
||||
FD_SET(hSocket, &fdset);
|
||||
int nRet = select(hSocket + 1, nullptr, &fdset, nullptr, &timeout);
|
||||
#endif
|
||||
// Upon successful completion, both select and poll return the total
|
||||
// number of file descriptors that have been selected. A value of 0
|
||||
// indicates that the call timed out and no file descriptors have
|
||||
// been selected.
|
||||
if (nRet == 0)
|
||||
{
|
||||
LogPrint(BCLog::NET, "connection to %s timeout\n", addrConnect.ToString());
|
||||
const Sock::Event requested = Sock::RECV | Sock::SEND;
|
||||
Sock::Event occurred;
|
||||
if (!sock.Wait(std::chrono::milliseconds{nTimeout}, requested, &occurred)) {
|
||||
LogPrintf("wait for connect to %s failed: %s\n",
|
||||
addrConnect.ToString(),
|
||||
NetworkErrorString(WSAGetLastError()));
|
||||
return false;
|
||||
}
|
||||
if (nRet == SOCKET_ERROR)
|
||||
{
|
||||
LogPrintf("select() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
|
||||
} else if (occurred == 0) {
|
||||
LogPrint(BCLog::NET, "connection attempt to %s timed out\n", addrConnect.ToString());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Even if the select/poll was successful, the connect might not
|
||||
// Even if the wait was successful, the connect might not
|
||||
// have been successful. The reason for this failure is hidden away
|
||||
// in the SO_ERROR for the socket in modern systems. We read it into
|
||||
// nRet here.
|
||||
socklen_t nRetSize = sizeof(nRet);
|
||||
if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (sockopt_arg_type)&nRet, &nRetSize) == SOCKET_ERROR)
|
||||
{
|
||||
// sockerr here.
|
||||
int sockerr;
|
||||
socklen_t sockerr_len = sizeof(sockerr);
|
||||
if (sock.GetSockOpt(SOL_SOCKET, SO_ERROR, (sockopt_arg_type)&sockerr, &sockerr_len) ==
|
||||
SOCKET_ERROR) {
|
||||
LogPrintf("getsockopt() for %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
|
||||
return false;
|
||||
}
|
||||
if (nRet != 0)
|
||||
{
|
||||
LogConnectFailure(manual_connection, "connect() to %s failed after select(): %s", addrConnect.ToString(), NetworkErrorString(nRet));
|
||||
if (sockerr != 0) {
|
||||
LogConnectFailure(manual_connection,
|
||||
"connect() to %s failed after wait: %s",
|
||||
addrConnect.ToString(),
|
||||
NetworkErrorString(sockerr));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -668,7 +655,7 @@ bool IsProxy(const CNetAddr &addr) {
|
||||
bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, uint16_t port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed)
|
||||
{
|
||||
// first connect to proxy server
|
||||
if (!ConnectSocketDirectly(proxy.proxy, sock.Get(), nTimeout, true)) {
|
||||
if (!ConnectSocketDirectly(proxy.proxy, sock, nTimeout, true)) {
|
||||
outProxyConnectionFailed = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user