From 30639abb887241d391c6ba865f5495e3eeb23960 Mon Sep 17 00:00:00 2001 From: Katharine Chui Date: Mon, 11 May 2026 11:08:15 +0200 Subject: [PATCH] enforce expected recv size during event and server info tcp recv, remove unused instance of CheckBytes function --- include/Network/network.hpp | 2 +- src/Network/Core.cpp | 13 ++++++------- src/Network/GlobalHandler.cpp | 11 ----------- src/Network/VehicleEvent.cpp | 13 +++++++++---- 4 files changed, 16 insertions(+), 23 deletions(-) diff --git a/include/Network/network.hpp b/include/Network/network.hpp index 94e4fb5..092853e 100644 --- a/include/Network/network.hpp +++ b/include/Network/network.hpp @@ -42,7 +42,7 @@ extern std::string magic; int KillSocket(uint64_t Dead); void UUl(const std::string& R); void UDPSend(std::string Data); -bool CheckBytes(int32_t Bytes); +bool CheckBytes(int32_t Bytes, int32_t Expected = -1); void GameSend(std::string_view Data); void SendLarge(std::string Data); std::string TCPRcv(uint64_t Sock); diff --git a/src/Network/Core.cpp b/src/Network/Core.cpp index 9dec167..8dddfdd 100644 --- a/src/Network/Core.cpp +++ b/src/Network/Core.cpp @@ -145,27 +145,26 @@ void GetServerInfo(std::string Data) { std::vector data(sizeof(Header)); int Temp = RecvWaitAll(ISock, data.data(), sizeof(Header)); - auto checkBytes = ([&](const int32_t bytes) -> bool { + auto checkBytes = ([&](const int32_t bytes, const int32_t expected = -1) -> bool { if (bytes == 0) { return false; } else if (bytes < 0) { return false; } + if (expected != -1 && bytes != expected) { + return false; + } return true; }); - if (!checkBytes(Temp)) { + if (!checkBytes(Temp, sizeof(Header))) { return ""; } memcpy(&Header, data.data(), sizeof(Header)); - if (!checkBytes(Temp)) { - return ""; - } - data.resize(Header, 0); Temp = RecvWaitAll(ISock, data.data(), Header); - if (!checkBytes(Temp)) { + if (!checkBytes(Temp, Header)) { return ""; } return std::string(data.data(), Header); diff --git a/src/Network/GlobalHandler.cpp b/src/Network/GlobalHandler.cpp index da4b6f2..c2290df 100644 --- a/src/Network/GlobalHandler.cpp +++ b/src/Network/GlobalHandler.cpp @@ -50,17 +50,6 @@ int KillSocket(uint64_t Dead) { return a; } -bool CheckBytes(uint32_t Bytes) { - if (Bytes == 0) { - debug("(Proxy) Connection closing"); - return false; - } else if (Bytes < 0) { - debug("(Proxy) send failed with error: " + std::to_string(WSAGetLastError())); - return false; - } - return true; -} - void GameSend(std::string_view Data) { static std::mutex Lock; std::scoped_lock Guard(Lock); diff --git a/src/Network/VehicleEvent.cpp b/src/Network/VehicleEvent.cpp index 60a4bf5..cecacea 100644 --- a/src/Network/VehicleEvent.cpp +++ b/src/Network/VehicleEvent.cpp @@ -28,9 +28,9 @@ int LastPort; std::string LastIP; SOCKET TCPSock = -1; -bool CheckBytes(int32_t Bytes) { +bool CheckBytes(int32_t Bytes, int32_t Expected) { if (Bytes == 0) { - debug("(TCP) Connection closing... CheckBytes(16)"); + debug("(TCP) Connection closing..."); Terminate = true; return false; } else if (Bytes < 0) { @@ -39,6 +39,11 @@ bool CheckBytes(int32_t Bytes) { Terminate = true; return false; } + if (Expected != -1 && Bytes != Expected) { + debug(std::format("(TCP) Short recv detected, expected {} bytes, got {} bytes", Expected, Bytes)); + Terminate = true; + return false; + } return true; } void UUl(const std::string& R) { @@ -103,7 +108,7 @@ std::string TCPRcv(SOCKET Sock) { int Temp; std::vector Data(sizeof(Header)); Temp = RecvWaitAll(Sock, Data.data(), sizeof(Header)); - if (!CheckBytes(Temp)) { + if (!CheckBytes(Temp, sizeof(Header))) { UUl("Socket Closed Code 3"); return ""; } @@ -116,7 +121,7 @@ std::string TCPRcv(SOCKET Sock) { Data.resize(Header, 0); Temp = RecvWaitAll(Sock, Data.data(), Header); - if (!CheckBytes(Temp)) { + if (!CheckBytes(Temp, Header)) { UUl("Socket Closed Code 5"); return ""; }