From 0acbb70d10eaeb1ce2c5767bf66edab364ad2a58 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Sat, 3 Jul 2021 01:43:29 +0200 Subject: [PATCH] add ip to identifiers, changed value format --- include/Client.h | 12 +++++++++--- include/TNetwork.h | 6 ++++-- src/TLuaFile.cpp | 7 +++---- src/TNetwork.cpp | 38 +++++++++++++++++++++----------------- 4 files changed, 37 insertions(+), 26 deletions(-) diff --git a/include/Client.h b/include/Client.h index 385d2ec..8ac7035 100644 --- a/include/Client.h +++ b/include/Client.h @@ -12,6 +12,12 @@ class TServer; +struct TConnection final { + SOCKET Socket; + struct sockaddr SockAddr; + socklen_t SockAddrLen; +}; + class TClient final { public: using TSetOfVehicleData = std::vector; @@ -30,7 +36,7 @@ public: TVehicleDataLockPair GetAllCars(); void SetName(const std::string& Name) { mName = Name; } void SetRoles(const std::string& Role) { mRole = Role; } - void AddIdentifier(const std::string& ID) { mIdentifiers.insert(ID); }; + void SetIdentifier(const std::string& key, const std::string& value) { mIdentifiers[key] = value; } std::string GetCarData(int Ident); void SetUDPAddr(sockaddr_in Addr) { mUDPAddress = Addr; } void SetDownSock(SOCKET CSock) { mSocket[1] = CSock; } @@ -38,7 +44,7 @@ public: void SetStatus(int Status) { mStatus = Status; } // locks void DeleteCar(int Ident); - [[nodiscard]] std::set GetIdentifiers() const { return mIdentifiers; } + [[nodiscard]] const std::unordered_map& GetIdentifiers() const { return mIdentifiers; } [[nodiscard]] sockaddr_in GetUDPAddr() const { return mUDPAddress; } [[nodiscard]] SOCKET GetDownSock() const { return mSocket[1]; } [[nodiscard]] SOCKET GetTCPSock() const { return mSocket[0]; } @@ -78,7 +84,7 @@ private: bool mIsSyncing = false; mutable std::mutex mMissedPacketsMutex; std::queue mPacketsSync; - std::set mIdentifiers; + std::unordered_map mIdentifiers; bool mIsGuest = false; std::mutex mVehicleDataMutex; TSetOfVehicleData mVehicleData; diff --git a/include/TNetwork.h b/include/TNetwork.h index a4d28c5..528aef4 100644 --- a/include/TNetwork.h +++ b/include/TNetwork.h @@ -4,6 +4,8 @@ #include "TResourceManager.h" #include "TServer.h" +struct TConnection; + class TNetwork { public: TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& ResourceManager); @@ -15,8 +17,8 @@ public: std::string TCPRcv(TClient& c); void ClientKick(TClient& c, const std::string& R); [[nodiscard]] bool SyncClient(const std::weak_ptr& c); - void Identify(SOCKET TCPSock); - void Authentication(SOCKET TCPSock); + void Identify(const TConnection& client); + void Authentication(const TConnection& ClientConnection); [[nodiscard]] bool CheckBytes(TClient& c, int32_t BytesRcv); void SyncResources(TClient& c); [[nodiscard]] bool UDPSend(TClient& Client, std::string Data) const; diff --git a/src/TLuaFile.cpp b/src/TLuaFile.cpp index 17f4ffe..a9af4c2 100644 --- a/src/TLuaFile.cpp +++ b/src/TLuaFile.cpp @@ -354,12 +354,11 @@ int lua_GetIdentifiers(lua_State* L) { if (IDs.empty()) return 0; LuaTable::Begin(L); - for (const std::string& ID : IDs) { - LuaTable::BeginEntry(L, ID.substr(0, ID.find(':')).c_str()); - lua_pushstring(L, ID.c_str()); + for (const auto& Pair : IDs) { + LuaTable::BeginEntry(L, Pair.first); + lua_pushstring(L, Pair.second.c_str()); LuaTable::EndEntry(L); } - // LuaTable::End(L, ""); } else return 0; } else { diff --git a/src/TNetwork.cpp b/src/TNetwork.cpp index 279a236..a5d4624 100644 --- a/src/TNetwork.cpp +++ b/src/TNetwork.cpp @@ -163,7 +163,7 @@ void TNetwork::TCPServerMain() { #else // unix // wondering why we need slightly different implementations of this? // ask ms. - SOCKET client = -1; + TConnection client {}; SOCKET Listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int optval = 1; setsockopt(Listener, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval)); @@ -193,8 +193,9 @@ void TNetwork::TCPServerMain() { debug("shutdown during TCP wait for accept loop"); break; } - client = accept(Listener, nullptr, nullptr); - if (client == -1) { + client.SockAddrLen = sizeof(client.SockAddr); + client.Socket = accept(Listener, &client.SockAddr, &client.SockAddrLen); + if (client.Socket == -1) { warn(("Got an invalid client socket on connect! Skipping...")); continue; } @@ -203,11 +204,11 @@ void TNetwork::TCPServerMain() { } catch (const std::exception& e) { error(("fatal: ") + std::string(e.what())); } - } while (client); + } while (client.Socket); debug("all ok, arrived at " + std::string(__func__) + ":" + std::to_string(__LINE__)); - CloseSocketProper(client); + CloseSocketProper(client.Socket); #endif } @@ -216,19 +217,19 @@ void TNetwork::TCPServerMain() { #include "Json.h" namespace json = rapidjson; -void TNetwork::Identify(SOCKET TCPSock) { +void TNetwork::Identify(const TConnection& client) { RegisterThreadAuto(); char Code; - if (recv(TCPSock, &Code, 1, 0) != 1) { - CloseSocketProper(TCPSock); + if (recv(client.Socket, &Code, 1, 0) != 1) { + CloseSocketProper(client.Socket); return; } if (Code == 'C') { - Authentication(TCPSock); + Authentication(client); } else if (Code == 'D') { - HandleDownload(TCPSock); + HandleDownload(client.Socket); } else { - CloseSocketProper(TCPSock); + CloseSocketProper(client.Socket); } } @@ -251,11 +252,12 @@ void TNetwork::HandleDownload(SOCKET TCPSock) { }); } -void TNetwork::Authentication(SOCKET TCPSock) { - auto Client = CreateClient(TCPSock); +void TNetwork::Authentication(const TConnection& ClientConnection) { + auto Client = CreateClient(ClientConnection.Socket); + Client->SetIdentifier("ip", inet_ntoa(reinterpret_cast(&ClientConnection.SockAddr)->sin_addr)); std::string Rc; - info("Identifying new client..."); + info("Identifying new ClientConnection..."); Rc = TCPRcv(*Client); @@ -304,7 +306,9 @@ void TNetwork::Authentication(SOCKET TCPSock) { Client->SetRoles(AuthResponse["roles"].GetString()); Client->SetIsGuest(AuthResponse["guest"].GetBool()); for (const auto& ID : AuthResponse["identifiers"].GetArray()) { - Client->AddIdentifier(ID.GetString()); + auto Raw = std::string(ID.GetString()); + auto SepIndex = Raw.find(':'); + Client->SetIdentifier(Raw.substr(0, SepIndex), Raw.substr(SepIndex + 1)); } } else { ClientKick(*Client, "Invalid authentication data!"); @@ -324,8 +328,8 @@ void TNetwork::Authentication(SOCKET TCPSock) { } info("Client Iteration: Name -> " + Cl->GetName() + ", Guest -> " + std::to_string(Cl->IsGuest()) + ", Roles -> " + Cl->GetRoles()); if (Cl->GetName() == Client->GetName() && Cl->IsGuest() == Client->IsGuest()) { - info("New client matched with current iteration"); - info("Old client (" + Cl->GetName() + ") kicked: Reconnecting"); + info("New ClientConnection matched with current iteration"); + info("Old ClientConnection (" + Cl->GetName() + ") kicked: Reconnecting"); CloseSocketProper(Cl->GetTCPSock()); Cl->SetStatus(-2); return false;