From e4b60b6a26505a7ee35d009e3fd540a5facc87d2 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 16 Jun 2017 14:03:13 +0200 Subject: Protocol: Do not assume anything about unknown packets. (#3647) --- src/Protocol/ProtocolRecognizer.cpp | 73 +++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 35 deletions(-) (limited to 'src/Protocol') diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp index 4487cdf6b..812d81721 100644 --- a/src/Protocol/ProtocolRecognizer.cpp +++ b/src/Protocol/ProtocolRecognizer.cpp @@ -70,56 +70,59 @@ AString cProtocolRecognizer::GetVersionTextFromInt(int a_ProtocolVersion) void cProtocolRecognizer::DataReceived(const char * a_Data, size_t a_Size) { - if (m_Protocol == nullptr) + if (m_Protocol != nullptr) { - if (!m_Buffer.Write(a_Data, a_Size)) + // Protocol was already recognized, send to the handler: + m_Protocol->DataReceived(a_Data, a_Size); + return; + } + + if (!m_Buffer.Write(a_Data, a_Size)) + { + m_Client->Kick("Unsupported protocol version"); + return; + } + + if (m_InPingForUnrecognizedVersion) + { + // We already know the verison; handle it here. + UInt32 PacketLen; + UInt32 PacketID; + if (!m_Buffer.ReadVarInt32(PacketLen)) { - m_Client->Kick("Unsupported protocol version"); return; } - - if (m_InPingForUnrecognizedVersion) + if (!m_Buffer.ReadVarInt32(PacketID)) { - // We already know the verison; handle it here. - UInt32 PacketLen; - UInt32 PacketID; - if (!m_Buffer.ReadVarInt32(PacketLen)) - { - return; - } - if (!m_Buffer.ReadVarInt32(PacketID)) - { - return; - } - ASSERT(PacketID == 0x01); // Ping packet - ASSERT(PacketLen == 9); // Payload of the packet ID and a UInt64 - - Int64 Data; - if (!m_Buffer.ReadBEInt64(Data)) - { - return; - } - - cPacketizer Pkt(*this, 0x01); // Pong packet - Pkt.WriteBEInt64(Data); + return; + } + if ((PacketID != 0x01) || (PacketLen != 9)) + { + // Not a Ping packet return; } - if (!TryRecognizeProtocol()) + Int64 Data; + if (!m_Buffer.ReadBEInt64(Data)) { return; } - // The protocol has just been recognized, dump the whole m_Buffer contents into it for parsing: - AString Dump; - m_Buffer.ResetRead(); - m_Buffer.ReadAll(Dump); - m_Protocol->DataReceived(Dump.data(), Dump.size()); + cPacketizer Pkt(*this, 0x01); // Pong packet + Pkt.WriteBEInt64(Data); + return; } - else + + if (!TryRecognizeProtocol()) { - m_Protocol->DataReceived(a_Data, a_Size); + return; } + + // The protocol has just been recognized, dump the whole m_Buffer contents into it for parsing: + AString Dump; + m_Buffer.ResetRead(); + m_Buffer.ReadAll(Dump); + m_Protocol->DataReceived(Dump.data(), Dump.size()); } -- cgit v1.2.3