Skip to content

Commit

Permalink
Check port and timeout recv
Browse files Browse the repository at this point in the history
  • Loading branch information
WiserTixx committed Jan 11, 2025
1 parent d14b64c commit 333a952
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Network/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,17 @@ void GetServerInfo(std::string Data) {
return;
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(std::stoi(Data.substr(Data.find(':') + 1)));

int port = std::stoi(Data.substr(Data.find(':') + 1));

if (port < 1 || port > 65535) {
debug("Invalid port number: " + std::to_string(port));
KillSocket(ISock);
CoreSend("I" + Data + ";");
return;
}

ServerAddr.sin_port = htons(port);
inet_pton(AF_INET, IP.c_str(), &ServerAddr.sin_addr);
if (connect(ISock, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr)) != 0) {
debug("Connection to server failed with error: " + std::to_string(WSAGetLastError()));
Expand All @@ -129,6 +139,12 @@ void GetServerInfo(std::string Data) {

std::string buffer;
buffer.resize(1024);

struct timeval timeout;
timeout.tv_sec = 10;
timeout.tv_usec = 0;
setsockopt(ISock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout));

int bytesReceived = recv(ISock, &buffer[0], buffer.size() - 1, 0);

if (bytesReceived > 0) {
Expand Down

0 comments on commit 333a952

Please sign in to comment.