From 998b12617e7ba30f4cd7baf55ca103721921db74 Mon Sep 17 00:00:00 2001 From: dongresource Date: Sat, 23 Jul 2022 02:00:40 +0200 Subject: [PATCH] Reject packets sent before a connection has been fully established --- src/servers/CNLoginServer.cpp | 11 +++++++++++ src/servers/CNShardServer.cpp | 28 ++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/servers/CNLoginServer.cpp b/src/servers/CNLoginServer.cpp index 6f216e8..52abd0c 100644 --- a/src/servers/CNLoginServer.cpp +++ b/src/servers/CNLoginServer.cpp @@ -19,6 +19,17 @@ CNLoginServer::CNLoginServer(uint16_t p) { void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) { printPacket(data); + if (loginSessions.find(sock) == loginSessions.end() && + data->type != P_CL2LS_REQ_LOGIN && data->type != P_CL2LS_REP_LIVE_CHECK) { + + if (settings::VERBOSITY > 0) { + std::cerr << "OpenFusion: LOGIN PKT OUT-OF-SEQ. PacketType: " << + Packets::p2str(data->type) << " (" << data->type << ")" << std::endl; + } + + return; + } + switch (data->type) { case P_CL2LS_REQ_LOGIN: { login(sock, data); diff --git a/src/servers/CNShardServer.cpp b/src/servers/CNShardServer.cpp index a66fda0..2a5f0a4 100644 --- a/src/servers/CNShardServer.cpp +++ b/src/servers/CNShardServer.cpp @@ -29,11 +29,31 @@ CNShardServer::CNShardServer(uint16_t p) { void CNShardServer::handlePacket(CNSocket* sock, CNPacketData* data) { printPacket(data); - if (ShardPackets.find(data->type) != ShardPackets.end()) - ShardPackets[data->type](sock, data); - else if (settings::VERBOSITY > 0) - std::cerr << "OpenFusion: SHARD UNIMPLM ERR. PacketType: " << Packets::p2str(data->type) << " (" << data->type << ")" << std::endl; + // if it's a valid packet + if (ShardPackets.find(data->type) != ShardPackets.end()) { + // reject gameplay packets if not yet fully connected + if (PlayerManager::players.find(sock) == PlayerManager::players.end() + && data->type != P_CL2FE_REQ_PC_ENTER && data->type != P_CL2FE_REP_LIVE_CHECK) { + + if (settings::VERBOSITY > 0) { + std::cerr << "OpenFusion: SHARD PKT OUT-OF-SEQ. PacketType: " << + Packets::p2str(data->type) << " (" << data->type << ")" << std::endl; + } + + return; + } + + // run the appropriate packet handler + ShardPackets[data->type](sock, data); + } else if (settings::VERBOSITY > 0) { + std::cerr << "OpenFusion: SHARD UNIMPLM ERR. PacketType: " << Packets::p2str(data->type) << " (" << data->type << ")" << std::endl; + } + + /* + * We must re-check if the player is still connected in case + * they were dropped when handling the packet. + */ if (PlayerManager::players.find(sock) != PlayerManager::players.end()) PlayerManager::players[sock]->lastHeartbeat = getTime(); }