enforce expected recv size during event and server info tcp recv, remove unused instance of CheckBytes function

This commit is contained in:
Katharine Chui
2026-05-11 11:08:15 +02:00
parent 23760da53b
commit 30639abb88
4 changed files with 16 additions and 23 deletions
+1 -1
View File
@@ -42,7 +42,7 @@ extern std::string magic;
int KillSocket(uint64_t Dead); int KillSocket(uint64_t Dead);
void UUl(const std::string& R); void UUl(const std::string& R);
void UDPSend(std::string Data); 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 GameSend(std::string_view Data);
void SendLarge(std::string Data); void SendLarge(std::string Data);
std::string TCPRcv(uint64_t Sock); std::string TCPRcv(uint64_t Sock);
+6 -7
View File
@@ -145,27 +145,26 @@ void GetServerInfo(std::string Data) {
std::vector<char> data(sizeof(Header)); std::vector<char> data(sizeof(Header));
int Temp = RecvWaitAll(ISock, data.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) { if (bytes == 0) {
return false; return false;
} else if (bytes < 0) { } else if (bytes < 0) {
return false; return false;
} }
if (expected != -1 && bytes != expected) {
return false;
}
return true; return true;
}); });
if (!checkBytes(Temp)) { if (!checkBytes(Temp, sizeof(Header))) {
return ""; return "";
} }
memcpy(&Header, data.data(), sizeof(Header)); memcpy(&Header, data.data(), sizeof(Header));
if (!checkBytes(Temp)) {
return "";
}
data.resize(Header, 0); data.resize(Header, 0);
Temp = RecvWaitAll(ISock, data.data(), Header); Temp = RecvWaitAll(ISock, data.data(), Header);
if (!checkBytes(Temp)) { if (!checkBytes(Temp, Header)) {
return ""; return "";
} }
return std::string(data.data(), Header); return std::string(data.data(), Header);
-11
View File
@@ -50,17 +50,6 @@ int KillSocket(uint64_t Dead) {
return a; 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) { void GameSend(std::string_view Data) {
static std::mutex Lock; static std::mutex Lock;
std::scoped_lock Guard(Lock); std::scoped_lock Guard(Lock);
+9 -4
View File
@@ -28,9 +28,9 @@ int LastPort;
std::string LastIP; std::string LastIP;
SOCKET TCPSock = -1; SOCKET TCPSock = -1;
bool CheckBytes(int32_t Bytes) { bool CheckBytes(int32_t Bytes, int32_t Expected) {
if (Bytes == 0) { if (Bytes == 0) {
debug("(TCP) Connection closing... CheckBytes(16)"); debug("(TCP) Connection closing...");
Terminate = true; Terminate = true;
return false; return false;
} else if (Bytes < 0) { } else if (Bytes < 0) {
@@ -39,6 +39,11 @@ bool CheckBytes(int32_t Bytes) {
Terminate = true; Terminate = true;
return false; 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; return true;
} }
void UUl(const std::string& R) { void UUl(const std::string& R) {
@@ -103,7 +108,7 @@ std::string TCPRcv(SOCKET Sock) {
int Temp; int Temp;
std::vector<char> Data(sizeof(Header)); std::vector<char> Data(sizeof(Header));
Temp = RecvWaitAll(Sock, Data.data(), sizeof(Header)); Temp = RecvWaitAll(Sock, Data.data(), sizeof(Header));
if (!CheckBytes(Temp)) { if (!CheckBytes(Temp, sizeof(Header))) {
UUl("Socket Closed Code 3"); UUl("Socket Closed Code 3");
return ""; return "";
} }
@@ -116,7 +121,7 @@ std::string TCPRcv(SOCKET Sock) {
Data.resize(Header, 0); Data.resize(Header, 0);
Temp = RecvWaitAll(Sock, Data.data(), Header); Temp = RecvWaitAll(Sock, Data.data(), Header);
if (!CheckBytes(Temp)) { if (!CheckBytes(Temp, Header)) {
UUl("Socket Closed Code 5"); UUl("Socket Closed Code 5");
return ""; return "";
} }