From 96668add6e1e319330f0e7587944ecf826b3d85c Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Sun, 8 Nov 2020 02:29:06 +0100 Subject: [PATCH] Safety improvements --- src/Network/Auth.cpp | 23 +++++++++---- src/Network/VehicleData.cpp | 64 +++++++++++++++++++++---------------- src/main.cpp | 5 +++ 3 files changed, 58 insertions(+), 34 deletions(-) diff --git a/src/Network/Auth.cpp b/src/Network/Auth.cpp index 77af733..cdcb7da 100644 --- a/src/Network/Auth.cpp +++ b/src/Network/Auth.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "UnixCompat.h" @@ -117,6 +118,7 @@ std::string GenerateM(RSA*key){ } void Identification(SOCKET TCPSock,Hold*S,RSA*Skey){ + DebugPrintTID(); Assert(S); Assert(Skey); S->TCPSock = TCPSock; @@ -171,6 +173,7 @@ void Identification(SOCKET TCPSock,Hold*S,RSA*Skey){ closesocket(TCPSock); return; } + DebugPrintTIDInternal(std::string("Client(") + Name + ")"); debug(Sec("Name -> ") + Name + Sec(", Role -> ") + Role + Sec(", ID -> ") + DID); for(Client*c: CI->Clients){ if(c != nullptr){ @@ -242,6 +245,7 @@ void TCPServerMain(){ } info(Sec("Vehicle event network online")); do{ + try { client = accept(Listener, nullptr, nullptr); if(client == -1){ warn(Sec("Got an invalid client socket on connect! Skipping...")); @@ -249,6 +253,9 @@ void TCPServerMain(){ } std::thread ID(Identify,client); ID.detach(); + } catch (const std::exception& e) { + error(Sec("fatal: ") + std::string(e)); + } }while(client); closesocket(client); @@ -276,13 +283,17 @@ void TCPServerMain(){ } info(Sec("Vehicle event network online")); do{ - client = accept(Listener, nullptr, nullptr); - if(client == -1){ - warn(Sec("Got an invalid client socket on connect! Skipping...")); - continue; + try { + client = accept(Listener, nullptr, nullptr); + if(client == -1){ + warn(Sec("Got an invalid client socket on connect! Skipping...")); + continue; + } + std::thread ID(Identify,client); + ID.detach(); + } catch (const std::exception& e) { + error(Sec("fatal: ") + std::string(e.what())); } - std::thread ID(Identify,client); - ID.detach(); }while(client); debug("all ok, arrived at " + std::string(__func__) + ":" + std::to_string(__LINE__)); diff --git a/src/Network/VehicleData.cpp b/src/Network/VehicleData.cpp index 703e7f7..1662024 100644 --- a/src/Network/VehicleData.cpp +++ b/src/Network/VehicleData.cpp @@ -301,21 +301,25 @@ void LOOP() { info(Sec("Vehicle data network online on port ") + std::to_string(Port) + Sec(" with a Max of ") + std::to_string(MaxPlayers) + Sec(" Clients")); while (true) { - sockaddr_in client {}; - std::string Data = UDPRcvFromClient(client); //Receives any data from Socket - auto Pos = Data.find(':'); - if (Data.empty() || Pos < 0 || Pos > 2) - continue; - /*char clientIp[256]; - ZeroMemory(clientIp, 256); ///Code to get IP we don't need that yet - inet_ntop(AF_INET, &client.sin_addr, clientIp, 256);*/ - uint8_t ID = Data.at(0) - 1; - for (Client* c : CI->Clients) { - if (c != nullptr && c->GetID() == ID) { - c->SetUDPAddr(client); - c->isConnected = true; - UDPParser(c, Data.substr(2)); + try { + sockaddr_in client {}; + std::string Data = UDPRcvFromClient(client); //Receives any data from Socket + auto Pos = Data.find(':'); + if (Data.empty() || Pos < 0 || Pos > 2) + continue; + /*char clientIp[256]; + ZeroMemory(clientIp, 256); ///Code to get IP we don't need that yet + inet_ntop(AF_INET, &client.sin_addr, clientIp, 256);*/ + uint8_t ID = Data.at(0) - 1; + for (Client* c : CI->Clients) { + if (c != nullptr && c->GetID() == ID) { + c->SetUDPAddr(client); + c->isConnected = true; + UDPParser(c, Data.substr(2)); + } } + } catch (const std::exception& e) { + error(Sec("fatal: ") + std::string(e.what())); } } /*closesocket(UDPSock); @@ -343,21 +347,25 @@ void LOOP() { info(Sec("Vehicle data network online on port ") + std::to_string(Port) + Sec(" with a Max of ") + std::to_string(MaxPlayers) + Sec(" Clients")); while (true) { - sockaddr_in client {}; - std::string Data = UDPRcvFromClient(client); //Receives any data from Socket - size_t Pos = Data.find(':'); - if (Data.empty() || Pos > 2) - continue; - /*char clientIp[256]; - ZeroMemory(clientIp, 256); ///Code to get IP we don't need that yet - inet_ntop(AF_INET, &client.sin_addr, clientIp, 256);*/ - uint8_t ID = uint8_t(Data.at(0)) - 1; - for (Client* c : CI->Clients) { - if (c != nullptr && c->GetID() == ID) { - c->SetUDPAddr(client); - c->isConnected = true; - UDPParser(c, Data.substr(2)); + try { + sockaddr_in client {}; + std::string Data = UDPRcvFromClient(client); //Receives any data from Socket + size_t Pos = Data.find(':'); + if (Data.empty() || Pos > 2) + continue; + /*char clientIp[256]; + ZeroMemory(clientIp, 256); ///Code to get IP we don't need that yet + inet_ntop(AF_INET, &client.sin_addr, clientIp, 256);*/ + uint8_t ID = uint8_t(Data.at(0)) - 1; + for (Client* c : CI->Clients) { + if (c != nullptr && c->GetID() == ID) { + c->SetUDPAddr(client); + c->isConnected = true; + UDPParser(c, Data.substr(2)); + } } + } catch (const std::exception& e) { + error(Sec("fatal: ") + std::string(e.what())); } } /*closesocket(UDPSock); // TODO: Why not this? We did this in TCPServerMain? diff --git a/src/main.cpp b/src/main.cpp index ee1243f..dd0ae9e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,6 +14,7 @@ } int main(int argc, char* argv[]) { + try { DebugPrintTID(); // curl needs to be initialized to properly deallocate its resources later Assert(curl_global_init(CURL_GLOBAL_DEFAULT) == CURLE_OK); @@ -31,5 +32,9 @@ int main(int argc, char* argv[]) { NetMain(); // clean up curl at the end to be sure curl_global_cleanup(); + } catch (const std::exception& e) { + error(std::string(e.what())); + throw; + } return 0; }