From 44028cbbb144e97315f8653323b81efbe0947934 Mon Sep 17 00:00:00 2001 From: Sepalani Date: Thu, 6 Jun 2024 21:40:05 +0400 Subject: [PATCH] Socket: Fix connect issues when using ReShade --- Source/Core/Common/SocketContext.cpp | 21 ++++++++++++++++++++- Source/Core/Core/IOS/Network/Socket.cpp | 13 ++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Source/Core/Common/SocketContext.cpp b/Source/Core/Common/SocketContext.cpp index 15f9fd9010..f9c630c434 100644 --- a/Source/Core/Common/SocketContext.cpp +++ b/Source/Core/Common/SocketContext.cpp @@ -3,6 +3,9 @@ #include "Common/SocketContext.h" +#include "Common/Logging/Log.h" +#include "Common/Network.h" + namespace Common { #ifdef _WIN32 @@ -11,7 +14,23 @@ SocketContext::SocketContext() std::lock_guard g(s_lock); if (s_num_objects == 0) { - static_cast(WSAStartup(MAKEWORD(2, 2), &s_data)); + const int ret = WSAStartup(MAKEWORD(2, 2), &s_data); + if (ret == 0) + { + INFO_LOG_FMT(COMMON, "WSAStartup succeeded, wVersion={}.{}, wHighVersion={}.{}", + int(LOBYTE(s_data.wVersion)), int(HIBYTE(s_data.wVersion)), + int(LOBYTE(s_data.wHighVersion)), int(HIBYTE(s_data.wHighVersion))); + } + else + { + // The WSAStartup function directly returns the extended error code in the return value. + // A call to the WSAGetLastError function is not needed and should not be used. + // + // Source: + // https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsastartup + ERROR_LOG_FMT(COMMON, "WSAStartup failed with error {}: {}", ret, + Common::DecodeNetworkError(ret)); + } } s_num_objects++; } diff --git a/Source/Core/Core/IOS/Network/Socket.cpp b/Source/Core/Core/IOS/Network/Socket.cpp index 4b7c2e2c8a..59f8e31814 100644 --- a/Source/Core/Core/IOS/Network/Socket.cpp +++ b/Source/Core/Core/IOS/Network/Socket.cpp @@ -97,6 +97,17 @@ s32 WiiSockMan::GetNetErrorCode(s32 ret, std::string_view caller, bool is_rw) { #ifdef _WIN32 s32 error_code = WSAGetLastError(); + // Some programs might hijack WinSock2 (e.g. ReShade) and alter the expected return value. + if (error_code == WSAEINVAL && caller == "SO_CONNECT") + { + // Note: + // In order to preserve backward compatibility, this error is reported as WSAEINVAL to Windows + // Sockets 1.1 applications that link to either Winsock.dll or Wsock32.dll. + // + // Source: + // https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect + error_code = WSAEALREADY; + } #else s32 error_code = errno; #endif @@ -292,7 +303,7 @@ void WiiSocket::Update(bool read, bool write, bool except) memory.CopyFromEmu(&addr, ioctl.buffer_in + 8, sizeof(WiiSockAddrIn)); sockaddr_in local_name = WiiSockMan::ToNativeAddrIn(addr); - int ret = connect(fd, (sockaddr*)&local_name, sizeof(local_name)); + const int ret = connect(fd, (sockaddr*)&local_name, sizeof(local_name)); ReturnValue = m_socket_manager.GetNetErrorCode(ret, "SO_CONNECT", false); UpdateConnectingState(ReturnValue);