diff options
author | peterbell10 <peterbell10@live.co.uk> | 2017-07-17 01:19:09 +0200 |
---|---|---|
committer | Tiger Wang <ziwei.tiger@outlook.com> | 2017-07-26 12:04:08 +0200 |
commit | e0a44fb3bc250197e30c5023fa53fc5c27a5cdcf (patch) | |
tree | bb7cd67afa2d70a0511f005c31560b591de06851 /src/Protocol/ProtocolRecognizer.cpp | |
parent | Updated armor cover calculation. (#3858) (diff) | |
download | cuberite-e0a44fb3bc250197e30c5023fa53fc5c27a5cdcf.tar cuberite-e0a44fb3bc250197e30c5023fa53fc5c27a5cdcf.tar.gz cuberite-e0a44fb3bc250197e30c5023fa53fc5c27a5cdcf.tar.bz2 cuberite-e0a44fb3bc250197e30c5023fa53fc5c27a5cdcf.tar.lz cuberite-e0a44fb3bc250197e30c5023fa53fc5c27a5cdcf.tar.xz cuberite-e0a44fb3bc250197e30c5023fa53fc5c27a5cdcf.tar.zst cuberite-e0a44fb3bc250197e30c5023fa53fc5c27a5cdcf.zip |
Diffstat (limited to 'src/Protocol/ProtocolRecognizer.cpp')
-rw-r--r-- | src/Protocol/ProtocolRecognizer.cpp | 97 |
1 files changed, 55 insertions, 42 deletions
diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp index 812d81721..187a73a6e 100644 --- a/src/Protocol/ProtocolRecognizer.cpp +++ b/src/Protocol/ProtocolRecognizer.cpp @@ -83,46 +83,58 @@ void cProtocolRecognizer::DataReceived(const char * a_Data, size_t a_Size) return; } - if (m_InPingForUnrecognizedVersion) + if (!m_InPingForUnrecognizedVersion) { - // We already know the verison; handle it here. - UInt32 PacketLen; - UInt32 PacketID; - if (!m_Buffer.ReadVarInt32(PacketLen)) + if (TryRecognizeProtocol()) { + // 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()); return; } - if (!m_Buffer.ReadVarInt32(PacketID)) + else { - return; + m_Buffer.ResetRead(); } - if ((PacketID != 0x01) || (PacketLen != 9)) + } + + if (!m_InPingForUnrecognizedVersion) + { + return; + } + + // Handle server list ping packets + for (;;) + { + UInt32 PacketLen; + UInt32 PacketID; + if ( + !m_Buffer.ReadVarInt32(PacketLen) || + !m_Buffer.CanReadBytes(PacketLen) || + !m_Buffer.ReadVarInt32(PacketID) + ) { - // Not a Ping packet - return; + // Not enough data + m_Buffer.ResetRead(); + break; } - Int64 Data; - if (!m_Buffer.ReadBEInt64(Data)) + if ((PacketID == 0x00) && (PacketLen == 1)) // Request packet { + HandlePacketStatusRequest(); + } + else if ((PacketID == 0x01) && (PacketLen == 9)) // Ping packet + { + HandlePacketStatusPing(); + } + else + { + m_Client->Kick("Server list ping failed, unrecognized packet"); return; } - - cPacketizer Pkt(*this, 0x01); // Pong packet - Pkt.WriteBEInt64(Data); - return; - } - - if (!TryRecognizeProtocol()) - { - 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()); } @@ -1108,20 +1120,6 @@ bool cProtocolRecognizer::TryRecognizeLengthedProtocol(UInt32 a_PacketLengthRema else { m_InPingForUnrecognizedVersion = true; - - UInt32 PacketLen; - UInt32 PacketID; - if (!m_Buffer.ReadVarInt32(PacketLen)) - { - return false; - } - if (!m_Buffer.ReadVarInt32(PacketID)) - { - return false; - } - ASSERT(PacketID == 0x00); // Request packet - ASSERT(PacketLen == 1); // No payload except for packet ID - SendPingStatusResponse(); } return false; } @@ -1155,7 +1153,7 @@ void cProtocolRecognizer::SendPacket(cPacketizer & a_Pkt) -void cProtocolRecognizer::SendPingStatusResponse(void) +void cProtocolRecognizer::HandlePacketStatusRequest(void) { cServer * Server = cRoot::Get()->GetServer(); AString ServerDescription = Server->GetDescription(); @@ -1200,3 +1198,18 @@ void cProtocolRecognizer::SendPingStatusResponse(void) +void cProtocolRecognizer::HandlePacketStatusPing() +{ + Int64 Timestamp; + if (!m_Buffer.ReadBEInt64(Timestamp)) + { + return; + } + + cPacketizer Pkt(*this, 0x01); // Pong packet + Pkt.WriteBEInt64(Timestamp); +} + + + + |