From ae70feb29b4130bf61473655ad99291fa4ccef3b Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Thu, 7 Dec 2023 11:13:32 +0000 Subject: [PATCH] [*] Removed static IAT lookups to WS2_32.dll --- Source/AuProcAddresses.NT.cpp | 19 +- Source/AuProcAddresses.NT.hpp | 206 ++++++++++++++---- Source/IO/Net/AuIPAddress.cpp | 15 +- Source/IO/Net/AuNetError.cpp | 5 +- Source/IO/Net/AuNetSocket.NT.cpp | 68 ++++-- Source/IO/Net/AuNetSocketServer.NT.cpp | 20 +- Source/IO/Net/AuNetSocketServer.cpp | 7 +- ...uNetSocketServerAcceptReadOperation.NT.cpp | 58 +++-- Source/IO/Net/AuNetStream.NT.cpp | 78 ++++--- Source/IO/Net/AuNetStream.NT.hpp | 2 +- Source/IO/Net/Net.cpp | 3 +- 11 files changed, 368 insertions(+), 113 deletions(-) diff --git a/Source/AuProcAddresses.NT.cpp b/Source/AuProcAddresses.NT.cpp index c9783db4..d0af3ae7 100644 --- a/Source/AuProcAddresses.NT.cpp +++ b/Source/AuProcAddresses.NT.cpp @@ -231,7 +231,24 @@ namespace Aurora ADD_GET_PROC(WS2, FreeAddrInfoExW) ADD_GET_PROC(WS2, getaddrinfo) ADD_GET_PROC(WS2, freeaddrinfo) - + ADD_GET_PROC(WS2, listen) + ADD_GET_PROC(WS2, bind) + ADD_GET_PROC(WS2, closesocket) + ADD_GET_PROC(WS2, shutdown) + ADD_GET_PROC(WS2, getpeername) + ADD_GET_PROC(WS2, getsockname) + ADD_GET_PROC(WS2, ioctlsocket) + ADD_GET_PROC(WS2, setsockopt) + ADD_GET_PROC(WS2, WSAStartup) + ADD_GET_PROC(WS2, WSAIoctl) + ADD_GET_PROC(WS2, WSAGetLastError) + ADD_GET_PROC(WS2, WSASocketW) + ADD_GET_PROC(WS2, WSARecvFrom) + ADD_GET_PROC(WS2, WSARecv) + ADD_GET_PROC(WS2, WSASendTo) + ADD_GET_PROC(WS2, WSASend) + ADD_GET_PROC(WS2, WSAStringToAddressA) + ADD_GET_PROC(WinTrust, WinVerifyTrust) ADD_GET_PROC(IPHelper, GetAdaptersAddresses) diff --git a/Source/AuProcAddresses.NT.hpp b/Source/AuProcAddresses.NT.hpp index ace61314..f540042b 100644 --- a/Source/AuProcAddresses.NT.hpp +++ b/Source/AuProcAddresses.NT.hpp @@ -268,45 +268,6 @@ namespace Aurora inline PVOID(__stdcall *pAddDllDirectory)( PCWSTR NewDirectory ); - - inline INT(__stdcall *pGetAddrInfoExCancel)( - LPHANDLE lpHandle - ); - - using LPLOOKUPSERVICE_COMPLETION_ROUTINE = void(__stdcall *)( - DWORD dwError, - DWORD dwBytes, - LPWSAOVERLAPPED lpOverlapped - ); - - inline INT(__stdcall *pGetAddrInfoExW)( - PCWSTR pName, - PCWSTR pServiceName, - DWORD dwNameSpace, - LPGUID lpNspId, - const ADDRINFOEXW * hints, - PADDRINFOEXW * ppResult, - struct timeval * timeout, - LPOVERLAPPED lpOverlapped, - LPLOOKUPSERVICE_COMPLETION_ROUTINE lpCompletionRoutine, - LPHANDLE lpHandle - ); - - inline void(__stdcall *pFreeAddrInfoExW)( - PADDRINFOEXW pAddrInfoEx - ); - - inline INT(__stdcall *pgetaddrinfo)( - PCSTR pNodeName, - PCSTR pServiceName, - const ADDRINFOA * pHints, - PADDRINFOA * ppResult - ); - - inline void(__stdcall *pfreeaddrinfo)( - PADDRINFOA pAddrInfo - ); - inline BOOL(__stdcall *pPrefetchVirtualMemory)( HANDLE hProcess, ULONG_PTR NumberOfEntries, @@ -809,6 +770,7 @@ namespace Aurora ); // MPR.dll + inline DWORD(__stdcall *pWNetCloseEnum)( HANDLE hEnum ); @@ -835,6 +797,172 @@ namespace Aurora LPDWORD lpBufferSize ); + // Winsock2 + + inline INT(__stdcall *pGetAddrInfoExCancel)( + LPHANDLE lpHandle + ); + + using LPLOOKUPSERVICE_COMPLETION_ROUTINE = void(__stdcall *)( + DWORD dwError, + DWORD dwBytes, + LPWSAOVERLAPPED lpOverlapped + ); + + inline INT(__stdcall *pGetAddrInfoExW)( + PCWSTR pName, + PCWSTR pServiceName, + DWORD dwNameSpace, + LPGUID lpNspId, + const ADDRINFOEXW * hints, + PADDRINFOEXW * ppResult, + struct timeval * timeout, + LPOVERLAPPED lpOverlapped, + LPLOOKUPSERVICE_COMPLETION_ROUTINE lpCompletionRoutine, + LPHANDLE lpHandle + ); + + inline void(__stdcall *pFreeAddrInfoExW)( + PADDRINFOEXW pAddrInfoEx + ); + + inline INT(__stdcall *pgetaddrinfo)( + PCSTR pNodeName, + PCSTR pServiceName, + const ADDRINFOA * pHints, + PADDRINFOA * ppResult + ); + + inline void(__stdcall *pfreeaddrinfo)( + PADDRINFOA pAddrInfo + ); + + inline int(__stdcall *plisten)( + SOCKET s, + int backlog + ); + + inline int(__stdcall *pbind)( + SOCKET s, + const sockaddr *name, + int namelen + ); + + inline int(__stdcall *pclosesocket)( + SOCKET s + ); + + inline int(__stdcall *pWSAStartup)( + WORD wVersionRequested, + LPWSADATA lpWSAData + ); + + inline int(__stdcall *pshutdown)( + SOCKET s, + int flags + ); + + inline int(__stdcall *pgetpeername)( + SOCKET s, + sockaddr *name, + int *namelen + ); + + inline int(__stdcall *pgetsockname)( + SOCKET s, + sockaddr *name, + int *namelen + ); + + inline int(__stdcall *pWSAIoctl)( + SOCKET s, + DWORD dwIoControlCode, + LPVOID lpvInBuffer, + DWORD cbInBuffer, + LPVOID lpvOutBuffer, + DWORD cbOutBuffer, + LPDWORD lpcbBytesReturned, + LPWSAOVERLAPPED lpOverlapped, + LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine + ); + + inline int(__stdcall *pioctlsocket)( + SOCKET s, + long cmd, + u_long *argp + ); + + inline int(__stdcall *psetsockopt)( + SOCKET s, + int level, + int optname, + const char * optval, + int optlen + ); + + inline int(__stdcall *pWSAGetLastError)(); + + inline SOCKET(__stdcall *pWSASocketW)( + int af, + int type, + int protocol, + LPWSAPROTOCOL_INFOW lpProtocolInfo, + GROUP g, + DWORD dwFlags + ); + + inline int(__stdcall *pWSARecvFrom)( + SOCKET s, + LPWSABUF lpBuffers, + DWORD dwBufferCount, + LPDWORD lpNumberOfBytesRecvd, + LPDWORD lpFlags, + sockaddr * lpFrom, + LPINT lpFromlen, + LPWSAOVERLAPPED lpOverlapped, + LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine + ); + + inline int(__stdcall *pWSARecv)( + SOCKET s, + LPWSABUF lpBuffers, + DWORD dwBufferCount, + LPDWORD lpNumberOfBytesRecvd, + LPDWORD lpFlags, + LPWSAOVERLAPPED lpOverlapped, + LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine + ); + + inline int(__stdcall *pWSASendTo)( + SOCKET s, + LPWSABUF lpBuffers, + DWORD dwBufferCount, + LPDWORD lpNumberOfBytesSent, + DWORD dwFlags, + const sockaddr * lpTo, + int iTolen, + LPWSAOVERLAPPED lpOverlapped, + LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine + ); + + inline int(__stdcall *pWSASend)( + SOCKET s, + LPWSABUF lpBuffers, + DWORD dwBufferCount, + LPDWORD lpNumberOfBytesSent, + DWORD dwFlags, + LPWSAOVERLAPPED lpOverlapped, + LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine + ); + + inline INT(__stdcall *pWSAStringToAddressA)( + LPSTR AddressString, + INT AddressFamily, + LPWSAPROTOCOL_INFOA lpProtocolInfo, + LPSOCKADDR lpAddress, + LPINT lpAddressLength + ); + inline bool gUseNativeWaitMutex {}; inline bool gUseNativeWaitCondvar {}; inline bool gUseNativeWaitSemapahore {}; diff --git a/Source/IO/Net/AuIPAddress.cpp b/Source/IO/Net/AuIPAddress.cpp index e922ddda..5f217983 100644 --- a/Source/IO/Net/AuIPAddress.cpp +++ b/Source/IO/Net/AuIPAddress.cpp @@ -29,11 +29,16 @@ namespace Aurora::IO::Net int size = sizeof(sastor); - if (::WSAStringToAddressA(AuString(src).data(), - af, - NULL, - (struct sockaddr *)&sastor, - &size)) + if (!pWSAStringToAddressA) + { + return 0; + } + + if (pWSAStringToAddressA(AuString(src).data(), + af, + NULL, + (struct sockaddr *)&sastor, + &size)) { return 0; } diff --git a/Source/IO/Net/AuNetError.cpp b/Source/IO/Net/AuNetError.cpp index 4e7275ce..27914b68 100644 --- a/Source/IO/Net/AuNetError.cpp +++ b/Source/IO/Net/AuNetError.cpp @@ -28,7 +28,10 @@ namespace Aurora::IO::Net void NetError_SetCurrent(NetError &error) { #if defined(AURORA_IS_MODERNNT_DERIVED) - NetError_SetOsError(error, ::WSAGetLastError()); + if (pWSAGetLastError) + { + NetError_SetOsError(error, pWSAGetLastError()); + } #elif defined(AURORA_IS_POSIX_DERIVED) NetError_SetOsError(error, errno); #endif diff --git a/Source/IO/Net/AuNetSocket.NT.cpp b/Source/IO/Net/AuNetSocket.NT.cpp index 33091a15..66857a30 100644 --- a/Source/IO/Net/AuNetSocket.NT.cpp +++ b/Source/IO/Net/AuNetSocket.NT.cpp @@ -70,7 +70,10 @@ namespace Aurora::IO::Net if (this->osHandle_ && this->osHandle_ != -1) { - ::closesocket(this->osHandle_); + if (pclosesocket) + { + pclosesocket(this->osHandle_); + } this->osHandle_ = 0; } } @@ -100,11 +103,19 @@ namespace Aurora::IO::Net if (this->osHandle_ && this->osHandle_ != -1) { - ::closesocket(this->osHandle_); + if (pclosesocket) + { + pclosesocket(this->osHandle_); + } this->osHandle_ = 0; } - this->osHandle_ = ::WSASocketW( + if (!pWSASocketW) + { + return; + } + + this->osHandle_ = pWSASocketW( IPToDomain(this->remoteEndpoint_), TransportToPlatformType(this->remoteEndpoint_), IPPROTO_IP, @@ -156,11 +167,15 @@ namespace Aurora::IO::Net bool Socket::PrepareConnectOperations() { - ::setsockopt(this->osHandle_, - SOL_SOCKET, - SO_UPDATE_CONNECT_CONTEXT, - nullptr, - 0); + if (psetsockopt) + { + // we'll soon find out if this truly failed later on... + (void)psetsockopt(this->osHandle_, + SOL_SOCKET, + SO_UPDATE_CONNECT_CONTEXT, + nullptr, + 0); + } return true; } @@ -174,7 +189,12 @@ namespace Aurora::IO::Net { int iLen { (int)this->endpointSize_ }; - if (::getsockname(this->osHandle_, (sockaddr *)this->localEndpoint_.hint, &iLen) == -1) + if (!pgetsockname) + { + return false; + } + + if (pgetsockname(this->osHandle_, (sockaddr *)this->localEndpoint_.hint, &iLen) == -1) { SysPushErrorIO(); return false; @@ -189,7 +209,12 @@ namespace Aurora::IO::Net { int iLen { (int)this->endpointSize_ }; - if (::getpeername(this->osHandle_, (sockaddr *)this->remoteEndpoint_.hint, &iLen) == -1) + if (!pgetpeername) + { + return false; + } + + if (pgetpeername(this->osHandle_, (sockaddr *)this->remoteEndpoint_.hint, &iLen) == -1) { SysPushErrorIO(); return false; @@ -208,8 +233,12 @@ namespace Aurora::IO::Net AF_UNSPEC; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = 0; - return ::bind(this->osHandle_, - (SOCKADDR *)&addr, sizeof(addr)) == 0; + if (!pbind) + { + return false; + } + return pbind(this->osHandle_, + (SOCKADDR *)&addr, sizeof(addr)) == 0; } bool Socket::ConnectOverlapped() @@ -217,8 +246,13 @@ namespace Aurora::IO::Net DWORD dwNumBytes {}; GUID guid = WSAID_CONNECTEX; LPFN_CONNECTEX lpConnectEx {}; + + if (!pWSAIoctl) + { + return false; + } - if (::WSAIoctl(this->osHandle_, + if (pWSAIoctl(this->osHandle_, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof(guid), @@ -264,7 +298,8 @@ namespace Aurora::IO::Net bool Socket::MakeNonblocking() { u_long iMode { 1 }; - return ::ioctlsocket(this->osHandle_, FIONBIO, &iMode) == 0; + return pioctlsocket && + pioctlsocket(this->osHandle_, FIONBIO, &iMode) == 0; } void Socket::Shutdown(bool bNow) @@ -272,7 +307,10 @@ namespace Aurora::IO::Net if (bNow) { this->SendEnd(); - ::shutdown(this->osHandle_, SD_BOTH); + if (pshutdown) + { + pshutdown(this->osHandle_, SD_BOTH); + } } else { diff --git a/Source/IO/Net/AuNetSocketServer.NT.cpp b/Source/IO/Net/AuNetSocketServer.NT.cpp index abbe72de..4e5bbef1 100644 --- a/Source/IO/Net/AuNetSocketServer.NT.cpp +++ b/Source/IO/Net/AuNetSocketServer.NT.cpp @@ -49,13 +49,18 @@ namespace Aurora::IO::Net this->pSocketDriver_ = AuDynamicCast(this->SharedFromThis()); this->localEndpoint_ = localAddress; + if (!pWSASocketW) + { + return {}; + } + if (!this->SendPreestablish()) { SysPushErrorIO("Preestablish drop"); return false; } - this->osHandle_ = ::WSASocketW( + this->osHandle_ = pWSASocketW( IPToDomain(localAddress), TransportToPlatformType(localAddress), IPPROTO_IP, @@ -77,7 +82,11 @@ namespace Aurora::IO::Net bool SocketServerImpl::ImplBind() { - if (::bind(this->osHandle_, + if (!pbind) + { + return false; + } + if (pbind(this->osHandle_, (SOCKADDR *)this->localEndpoint_.hint, this->endpointSize_) != 0) { NetError error; @@ -104,7 +113,12 @@ namespace Aurora::IO::Net We need a sane number between the two. 512 seems reasonable */ - if (::listen(this->osHandle_, this->uBacklog) != 0) + if (!plisten) + { + return false; + } + + if (plisten(this->osHandle_, this->uBacklog) != 0) { NetError error; NetError_SetCurrent(error); diff --git a/Source/IO/Net/AuNetSocketServer.cpp b/Source/IO/Net/AuNetSocketServer.cpp index 44c3e586..d6910260 100644 --- a/Source/IO/Net/AuNetSocketServer.cpp +++ b/Source/IO/Net/AuNetSocketServer.cpp @@ -268,7 +268,12 @@ namespace Aurora::IO::Net { socklen_t iLen { (int)this->endpointSize_ }; - if (::getsockname(this->osHandle_, (sockaddr *)this->localEndpoint_.hint, &iLen) == -1) + if (!pgetsockname) + { + return false; + } + + if (pgetsockname(this->osHandle_, (sockaddr *)this->localEndpoint_.hint, &iLen) == -1) { SysPushErrorIO(); return false; diff --git a/Source/IO/Net/AuNetSocketServerAcceptReadOperation.NT.cpp b/Source/IO/Net/AuNetSocketServerAcceptReadOperation.NT.cpp index b6435b72..d8416212 100644 --- a/Source/IO/Net/AuNetSocketServerAcceptReadOperation.NT.cpp +++ b/Source/IO/Net/AuNetSocketServerAcceptReadOperation.NT.cpp @@ -40,13 +40,19 @@ namespace Aurora::IO::Net return; } + if (!psetsockopt) + { + this->nextSocketPtr->SendErrorNoStream({}); + return; + } + SOCKET hListenHandle = (SOCKET)this->pParent_->ToPlatformHandle(); - int ret = ::setsockopt(this->nextSocket, - SOL_SOCKET, - SO_UPDATE_ACCEPT_CONTEXT, - (char *)&hListenHandle, - sizeof(SOCKET)); - auto dwError = WSAGetLastError(); + int ret = psetsockopt(this->nextSocket, + SOL_SOCKET, + SO_UPDATE_ACCEPT_CONTEXT, + (char *)&hListenHandle, + sizeof(SOCKET)); + auto dwError = pWSAGetLastError(); if (ret == -1) { SysPushErrorNet("Couldn't enable socket after overlapped accept"); @@ -149,23 +155,33 @@ namespace Aurora::IO::Net return; } + if (!pWSAGetLastError) + { + return; + } + + if (!pWSAIoctl) + { + return; + } + GUID GuidAcceptEx = WSAID_ACCEPTEX; OVERLAPPED a {}; DWORD dwBytes; a.hEvent = CreateEventA(NULL, true, 0, NULL); - if ((WSAIoctl(this->pParent_->ToPlatformHandle(), - SIO_GET_EXTENSION_FUNCTION_POINTER, - &GuidAcceptEx, - sizeof(GuidAcceptEx), - &lpfnAcceptEx, - sizeof(lpfnAcceptEx), - &dwBytes, - &a, - NULL) != 0) && - (::WSAGetLastError() != ERROR_IO_PENDING)) + if ((pWSAIoctl(this->pParent_->ToPlatformHandle(), + SIO_GET_EXTENSION_FUNCTION_POINTER, + &GuidAcceptEx, + sizeof(GuidAcceptEx), + &lpfnAcceptEx, + sizeof(lpfnAcceptEx), + &dwBytes, + &a, + NULL) != 0) && + (pWSAGetLastError() != ERROR_IO_PENDING)) { - int error = ::WSAGetLastError(); + int error = pWSAGetLastError(); SysPushErrorIO(); ::CloseHandle(a.hEvent); return; @@ -178,7 +194,13 @@ namespace Aurora::IO::Net bool SocketServerAcceptReadOperation::Pretick() { auto &localAddress = this->pParent_->GetLocalEndpoint(); - nextSocket = ::WSASocketW( + + if (!pWSASocketW) + { + return {}; + } + + nextSocket = pWSASocketW( IPToDomain(localAddress), TransportToPlatformType(localAddress), IPPROTO_IP, diff --git a/Source/IO/Net/AuNetStream.NT.cpp b/Source/IO/Net/AuNetStream.NT.cpp index 9788b0b7..95eefc3a 100644 --- a/Source/IO/Net/AuNetStream.NT.cpp +++ b/Source/IO/Net/AuNetStream.NT.cpp @@ -121,26 +121,36 @@ namespace Aurora::IO::Net if (!this->bDatagramMode) { - ret = ::WSARecv(this->GetSocket(), - bufferArray, - 1, - NULL, - &todo, - &this->overlap, - WsaOverlappedCompletionRoutine); + if (!pWSARecv) + { + return this->TranslateLastError(false, true); + } + + ret = pWSARecv(this->GetSocket(), + bufferArray, + 1, + NULL, + &todo, + &this->overlap, + WsaOverlappedCompletionRoutine); } else { + if (!pWSARecvFrom) + { + return this->TranslateLastError(false, true); + } + this->iSocketLength = this->pSocket->endpointSize_; - ret = ::WSARecvFrom(this->GetSocket(), - bufferArray, - 1, - NULL, - &todo, - (sockaddr *)netEndpoint.hint, - &this->iSocketLength, - &this->overlap, - WsaOverlappedCompletionRoutine); + ret = pWSARecvFrom(this->GetSocket(), + bufferArray, + 1, + NULL, + &todo, + (sockaddr *)netEndpoint.hint, + &this->iSocketLength, + &this->overlap, + WsaOverlappedCompletionRoutine); } return this->TranslateLastError(ret != -1); @@ -213,7 +223,12 @@ namespace Aurora::IO::Net int ret; if (!this->bDatagramMode) { - ret = ::WSASend(this->GetSocket(), + if (!pWSASend) + { + return this->TranslateLastError(false, true); + } + + ret = pWSASend(this->GetSocket(), bufferArray, 1, NULL, @@ -223,24 +238,31 @@ namespace Aurora::IO::Net } else { + if (!pWSASendTo) + { + return this->TranslateLastError(false, true); + } + this->iSocketLength = this->pSocket->endpointSize_; - ret = ::WSASendTo(this->GetSocket(), - bufferArray, - 1, - NULL, - this->dwRecvFlags, - (sockaddr *)netEndpoint.hint, - EndpointToLength(netEndpoint), - &this->overlap, - WsaOverlappedCompletionRoutine); + ret = pWSASendTo(this->GetSocket(), + bufferArray, + 1, + NULL, + this->dwRecvFlags, + (sockaddr *)netEndpoint.hint, + EndpointToLength(netEndpoint), + &this->overlap, + WsaOverlappedCompletionRoutine); } return this->TranslateLastError(ret != -1); } - bool NtAsyncNetworkTransaction::TranslateLastError(bool bReturnValue) + bool NtAsyncNetworkTransaction::TranslateLastError(bool bReturnValue, bool bForceFail) { - auto er = WSAGetLastError(); + auto er = bForceFail ? + ERROR_IO_PREEMPTED : + pWSAGetLastError(); if (bReturnValue) { return true; diff --git a/Source/IO/Net/AuNetStream.NT.hpp b/Source/IO/Net/AuNetStream.NT.hpp index 1b3b5269..1a3187a1 100644 --- a/Source/IO/Net/AuNetStream.NT.hpp +++ b/Source/IO/Net/AuNetStream.NT.hpp @@ -55,7 +55,7 @@ namespace Aurora::IO::Net SOCKET GetSocket(); HANDLE GetAlertable(); - bool TranslateLastError(bool bReturnValue); + bool TranslateLastError(bool bReturnValue, bool bForceFail = false); SocketBase * pSocket; OVERLAPPED overlap {}; diff --git a/Source/IO/Net/Net.cpp b/Source/IO/Net/Net.cpp index 39266065..794c1dc2 100644 --- a/Source/IO/Net/Net.cpp +++ b/Source/IO/Net/Net.cpp @@ -23,7 +23,8 @@ namespace Aurora::IO::Net WSADATA wsaData; wVersionRequested = MAKEWORD(2, 2); - gWin32NetReady = !WSAStartup(wVersionRequested, &wsaData); + gWin32NetReady = pWSAStartup && + !pWSAStartup(wVersionRequested, &wsaData); #endif