From b580c66b6143d22a7437c930327917dd0ced8d26 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Tue, 27 May 2025 11:55:18 +0200 Subject: [PATCH] Restrict minimum state for ClientInterface::sendToAll --- src/server.cpp | 7 +++---- src/server/clientiface.cpp | 32 +++++++++++--------------------- src/server/clientiface.h | 2 +- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/src/server.cpp b/src/server.cpp index 008b91022..efe74119c 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1572,7 +1572,8 @@ void Server::SendChatMessage(session_t peer_id, const ChatMessage &message) if (peer_id != PEER_ID_INEXISTENT) { Send(&pkt); } else { - m_clients.sendToAll(&pkt); + // If a client has completed auth but is still joining, still send chat + m_clients.sendToAll(&pkt, CS_InitDone); } } @@ -3183,9 +3184,7 @@ std::wstring Server::handleChat(const std::string &name, ChatMessage chatmsg(line); - std::vector clients = m_clients.getClientIDs(); - for (u16 cid : clients) - SendChatMessage(cid, chatmsg); + SendChatMessage(PEER_ID_INEXISTENT, chatmsg); return L""; } diff --git a/src/server/clientiface.cpp b/src/server/clientiface.cpp index 6b5f66456..2f6abbf85 100644 --- a/src/server/clientiface.cpp +++ b/src/server/clientiface.cpp @@ -767,33 +767,22 @@ void ClientInterface::sendCustom(session_t peer_id, u8 channel, NetworkPacket *p m_con->Send(peer_id, channel, pkt, reliable); } -void ClientInterface::sendToAll(NetworkPacket *pkt) +void ClientInterface::sendToAll(NetworkPacket *pkt, ClientState state_min) { + auto &ccf = clientCommandFactoryTable[pkt->getCommand()]; + FATAL_ERROR_IF(!ccf.name, "packet type missing in table"); RecursiveMutexAutoLock clientslock(m_clients_mutex); - for (auto &client_it : m_clients) { - RemoteClient *client = client_it.second; - - if (client->net_proto_version != 0) { - auto &ccf = clientCommandFactoryTable[pkt->getCommand()]; - FATAL_ERROR_IF(!ccf.name, "packet type missing in table"); - m_con->Send(client->peer_id, ccf.channel, pkt, ccf.reliable); - } + for (auto &[peer_id, client] : m_clients) { + if (client->getState() >= state_min) + m_con->Send(peer_id, ccf.channel, pkt, ccf.reliable); } } RemoteClient* ClientInterface::getClientNoEx(session_t peer_id, ClientState state_min) { RecursiveMutexAutoLock clientslock(m_clients_mutex); - RemoteClientMap::const_iterator n = m_clients.find(peer_id); - // The client may not exist; clients are immediately removed if their - // access is denied, and this event occurs later then. - if (n == m_clients.end()) - return NULL; - - if (n->second->getState() >= state_min) - return n->second; - - return NULL; + RemoteClient *client = lockedGetClientNoEx(peer_id, state_min); + return client; } RemoteClient* ClientInterface::lockedGetClientNoEx(session_t peer_id, ClientState state_min) @@ -802,12 +791,13 @@ RemoteClient* ClientInterface::lockedGetClientNoEx(session_t peer_id, ClientStat // The client may not exist; clients are immediately removed if their // access is denied, and this event occurs later then. if (n == m_clients.end()) - return NULL; + return nullptr; + assert(n->second->peer_id == peer_id); if (n->second->getState() >= state_min) return n->second; - return NULL; + return nullptr; } ClientState ClientInterface::getClientState(session_t peer_id) diff --git a/src/server/clientiface.h b/src/server/clientiface.h index 30b0454ef..d2194774c 100644 --- a/src/server/clientiface.h +++ b/src/server/clientiface.h @@ -458,7 +458,7 @@ public: void sendCustom(session_t peer_id, u8 channel, NetworkPacket *pkt, bool reliable); /* send to all clients */ - void sendToAll(NetworkPacket *pkt); + void sendToAll(NetworkPacket *pkt, ClientState state_min = CS_Active); /* delete a client */ void DeleteClient(session_t peer_id);