From 7f372b12cb01f7df9d5466de11b36494353613a8 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sat, 12 Sep 2020 21:08:23 +0100 Subject: Add State enum to protocol --- src/Protocol/Protocol.h | 8 ++++++++ src/Protocol/ProtocolRecognizer.cpp | 4 ++-- src/Protocol/Protocol_1_12.cpp | 26 ++++++++++---------------- src/Protocol/Protocol_1_14.cpp | 6 +++--- src/Protocol/Protocol_1_8.cpp | 23 +++++++++-------------- src/Protocol/Protocol_1_8.h | 33 ++++++++++++++++----------------- src/Protocol/Protocol_1_9.cpp | 15 ++++++--------- src/Protocol/Protocol_1_9.h | 2 +- 8 files changed, 55 insertions(+), 62 deletions(-) diff --git a/src/Protocol/Protocol.h b/src/Protocol/Protocol.h index 222e6031e..9d3cd77f2 100644 --- a/src/Protocol/Protocol.h +++ b/src/Protocol/Protocol.h @@ -345,6 +345,14 @@ public: v1_14 = 477 }; + enum State + { + Status = 1, + Login = 2, + Game = 3, + Invalid = 255 + }; + /** Called when client sends some data */ virtual void DataReceived(cByteBuffer & a_Buffer, const char * a_Data, size_t a_Size) = 0; diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp index e78ed6d6c..ef273d9e6 100644 --- a/src/Protocol/ProtocolRecognizer.cpp +++ b/src/Protocol/ProtocolRecognizer.cpp @@ -244,7 +244,7 @@ std::unique_ptr cMultiVersionProtocol::TryRecognizeLengthedProtocol(c UInt32 ProtocolVersion; AString ServerAddress; UInt16 ServerPort; - UInt32 NextState; + cProtocol::State NextState; if (!m_Buffer.ReadVarInt(PacketType) || (PacketType != 0x00)) { @@ -308,7 +308,7 @@ std::unique_ptr cMultiVersionProtocol::TryRecognizeLengthedProtocol(c a_Client.GetIPString().c_str(), ProtocolVersion, ProtocolVersion ); - if (NextState != 1) + if (NextState != cProtocol::State::Status) { throw sTriedToJoinWithUnsupportedProtocolException( Printf("Unsupported protocol version %u.\nTry connecting with Minecraft " MCS_CLIENT_VERSIONS, ProtocolVersion) diff --git a/src/Protocol/Protocol_1_12.cpp b/src/Protocol/Protocol_1_12.cpp index dcbb6e17c..8a3076980 100644 --- a/src/Protocol/Protocol_1_12.cpp +++ b/src/Protocol/Protocol_1_12.cpp @@ -1022,9 +1022,8 @@ bool cProtocol_1_12::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTyp { switch (m_State) { - case 1: + case State::Status: { - // Status switch (a_PacketType) { case 0x00: HandlePacketStatusRequest(a_ByteBuffer); return true; @@ -1033,9 +1032,8 @@ bool cProtocol_1_12::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTyp break; } - case 2: + case State::Login: { - // Login switch (a_PacketType) { case 0x00: HandlePacketLoginStart(a_ByteBuffer); return true; @@ -1044,9 +1042,8 @@ bool cProtocol_1_12::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTyp break; } - case 3: + case State::Game: { - // Game switch (a_PacketType) { case 0x00: HandleConfirmTeleport(a_ByteBuffer); return true; @@ -1093,10 +1090,10 @@ bool cProtocol_1_12::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTyp // Cannot kick the client - we don't know this state and thus the packet number for the kick packet // Switch to a state when all further packets are silently ignored: - m_State = 255; + m_State = State::Invalid; return false; } - case 255: + case State::Invalid: { // This is the state used for "not processing packets anymore" when we receive a bad packet from a client. // Do not output anything (the caller will do that for us), just return failure @@ -1172,9 +1169,8 @@ bool cProtocol_1_12_1::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketT { switch (m_State) { - case 1: + case State::Status: { - // Status switch (a_PacketType) { case 0x00: HandlePacketStatusRequest(a_ByteBuffer); return true; @@ -1183,9 +1179,8 @@ bool cProtocol_1_12_1::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketT break; } - case 2: + case State::Login: { - // Login switch (a_PacketType) { case 0x00: HandlePacketLoginStart(a_ByteBuffer); return true; @@ -1194,9 +1189,8 @@ bool cProtocol_1_12_1::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketT break; } - case 3: + case State::Game: { - // Game switch (a_PacketType) { case 0x00: HandleConfirmTeleport(a_ByteBuffer); return true; @@ -1243,10 +1237,10 @@ bool cProtocol_1_12_1::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketT // Cannot kick the client - we don't know this state and thus the packet number for the kick packet // Switch to a state when all further packets are silently ignored: - m_State = 255; + m_State = State::Invalid; return false; } - case 255: + case State::Invalid: { // This is the state used for "not processing packets anymore" when we receive a bad packet from a client. // Do not output anything (the caller will do that for us), just return failure diff --git a/src/Protocol/Protocol_1_14.cpp b/src/Protocol/Protocol_1_14.cpp index c3e4a62d0..335677155 100644 --- a/src/Protocol/Protocol_1_14.cpp +++ b/src/Protocol/Protocol_1_14.cpp @@ -226,7 +226,7 @@ std::pair cProtocol_1_14::GetItemFromProtocolID(UInt32 a_ProtocolI UInt32 cProtocol_1_14::GetProtocolBlockType(BLOCKTYPE a_BlockType, NIBBLETYPE a_Meta) { - return Palette_1_14::From(PaletteUpgrade::FromBlock(a_BlockType, a_Meta)); + return Palette_1_14::FromBlock(PaletteUpgrade::FromBlock(a_BlockType, a_Meta)); } @@ -235,7 +235,7 @@ UInt32 cProtocol_1_14::GetProtocolBlockType(BLOCKTYPE a_BlockType, NIBBLETYPE a_ UInt32 cProtocol_1_14::GetProtocolItemType(short a_ItemID, short a_ItemDamage) { - return Palette_1_14::From(PaletteUpgrade::FromItem(a_ItemID, a_ItemDamage)); + return Palette_1_14::FromItem(PaletteUpgrade::FromItem(a_ItemID, a_ItemDamage)); } @@ -253,7 +253,7 @@ UInt32 cProtocol_1_14::GetProtocolStatisticType(Statistic a_Statistic) bool cProtocol_1_14::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketType) { - if (m_State != 3) + if (m_State != State::Game) { return Super::HandlePacket(a_ByteBuffer, a_PacketType); } diff --git a/src/Protocol/Protocol_1_8.cpp b/src/Protocol/Protocol_1_8.cpp index 1185c264a..1ccc24e90 100644 --- a/src/Protocol/Protocol_1_8.cpp +++ b/src/Protocol/Protocol_1_8.cpp @@ -100,7 +100,7 @@ extern bool g_ShouldLogCommIn, g_ShouldLogCommOut; //////////////////////////////////////////////////////////////////////////////// // cProtocol_1_8_0: -cProtocol_1_8_0::cProtocol_1_8_0(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State) : +cProtocol_1_8_0::cProtocol_1_8_0(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, State a_State) : Super(a_Client), m_ServerAddress(a_ServerAddress), // Note: a_ServerPort is unused @@ -383,16 +383,14 @@ void cProtocol_1_8_0::SendDisconnect(const AString & a_Reason) { switch (m_State) { - case 2: + case State::Login: { - // During login: cPacketizer Pkt(*this, pktDisconnectDuringLogin); Pkt.WriteString(Printf("{\"text\":\"%s\"}", EscapeString(a_Reason).c_str())); break; } - case 3: + case State::Game: { - // In-game: cPacketizer Pkt(*this, pktDisconnectDuringGame); Pkt.WriteString(Printf("{\"text\":\"%s\"}", EscapeString(a_Reason).c_str())); break; @@ -796,7 +794,7 @@ void cProtocol_1_8_0::SendLoginSuccess(void) Pkt.WriteVarInt32(CompressionThreshold); } - m_State = 3; // State = Game + m_State = State::Game; { cPacketizer Pkt(*this, pktLoginSuccess); @@ -2217,9 +2215,8 @@ bool cProtocol_1_8_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTy { switch (m_State) { - case 1: + case State::Status: { - // Status switch (a_PacketType) { case 0x00: HandlePacketStatusRequest(a_ByteBuffer); return true; @@ -2228,9 +2225,8 @@ bool cProtocol_1_8_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTy break; } - case 2: + case State::Login: { - // Login switch (a_PacketType) { case 0x00: HandlePacketLoginStart (a_ByteBuffer); return true; @@ -2239,9 +2235,8 @@ bool cProtocol_1_8_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTy break; } - case 3: + case State::Game: { - // Game switch (a_PacketType) { case 0x00: HandlePacketKeepAlive (a_ByteBuffer); return true; @@ -2281,10 +2276,10 @@ bool cProtocol_1_8_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTy // Cannot kick the client - we don't know this state and thus the packet number for the kick packet // Switch to a state when all further packets are silently ignored: - m_State = 255; + m_State = State::Invalid; return false; } - case 255: + case State::Invalid: { // This is the state used for "not processing packets anymore" when we receive a bad packet from a client. // Do not output anything (the caller will do that for us), just return failure diff --git a/src/Protocol/Protocol_1_8.h b/src/Protocol/Protocol_1_8.h index 92eb4b88a..47e483941 100644 --- a/src/Protocol/Protocol_1_8.h +++ b/src/Protocol/Protocol_1_8.h @@ -31,7 +31,7 @@ class cProtocol_1_8_0: public: - cProtocol_1_8_0(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); + cProtocol_1_8_0(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, State a_State); /** Called when client sends some data: */ virtual void DataReceived(cByteBuffer & a_Buffer, const char * a_Data, size_t a_Size) override; @@ -134,6 +134,9 @@ public: protected: + /** State of the protocol. */ + State m_State; + /** Adds the received (unencrypted) data to m_ReceivedData, parses complete packets */ virtual void AddReceivedData(cByteBuffer & a_Buffer, const char * a_Data, size_t a_Size); @@ -190,7 +193,6 @@ protected: The message payload is still in the bytebuffer, the handler reads it specifically for each handled channel */ virtual void HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const AString & a_Channel); - /** Sends the data to the client, encrypting them if needed. */ virtual void SendData(const char * a_Data, size_t a_Size) override; @@ -229,11 +231,20 @@ protected: /** Writes the block entity data for the specified block entity into the packet. */ virtual void WriteBlockEntity(cPacketizer & a_Pkt, const cBlockEntity & a_BlockEntity); - /** State of the protocol. 1 = status, 2 = login, 3 = game */ - UInt32 m_State; - private: + AString m_ServerAddress; + + AString m_AuthServerID; + + bool m_IsEncrypted; + + cAesCfb128Decryptor m_Decryptor; + cAesCfb128Encryptor m_Encryptor; + + /** The logfile where the comm is logged, when g_ShouldLogComm is true */ + cFile m_CommLogFile; + /** Sends an entity teleport packet. Mitigates a 1.8 bug where the position in the entity spawn packet is ignored, and so entities don't show up until a teleport is sent. */ @@ -247,16 +258,4 @@ private: Protocols <= 1.12 use strings, hence this is a static as the string-mapping was append-only for the versions that used it. Returns an empty string, handled correctly by the client, for newer, unsupported statistics. */ static const char * GetProtocolStatisticName(Statistic a_Statistic); - - AString m_ServerAddress; - - AString m_AuthServerID; - - bool m_IsEncrypted; - - cAesCfb128Decryptor m_Decryptor; - cAesCfb128Encryptor m_Encryptor; - - /** The logfile where the comm is logged, when g_ShouldLogComm is true */ - cFile m_CommLogFile; } ; diff --git a/src/Protocol/Protocol_1_9.cpp b/src/Protocol/Protocol_1_9.cpp index b4fc47d0f..db0d4c17d 100644 --- a/src/Protocol/Protocol_1_9.cpp +++ b/src/Protocol/Protocol_1_9.cpp @@ -79,7 +79,7 @@ static const UInt32 OFF_HAND = 1; //////////////////////////////////////////////////////////////////////////////// // cProtocol_1_9_0: -cProtocol_1_9_0::cProtocol_1_9_0(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State) : +cProtocol_1_9_0::cProtocol_1_9_0(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, State a_State) : Super(a_Client, a_ServerAddress, a_ServerPort, a_State), m_IsTeleportIdConfirmed(true), m_OutstandingTeleportId(0) @@ -575,9 +575,8 @@ bool cProtocol_1_9_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTy { switch (m_State) { - case 1: + case State::Status: { - // Status switch (a_PacketType) { case 0x00: HandlePacketStatusRequest(a_ByteBuffer); return true; @@ -586,9 +585,8 @@ bool cProtocol_1_9_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTy break; } - case 2: + case State::Login: { - // Login switch (a_PacketType) { case 0x00: HandlePacketLoginStart (a_ByteBuffer); return true; @@ -597,9 +595,8 @@ bool cProtocol_1_9_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTy break; } - case 3: + case State::Game: { - // Game switch (a_PacketType) { case 0x00: HandleConfirmTeleport (a_ByteBuffer); return true; @@ -643,10 +640,10 @@ bool cProtocol_1_9_0::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketTy // Cannot kick the client - we don't know this state and thus the packet number for the kick packet // Switch to a state when all further packets are silently ignored: - m_State = 255; + m_State = State::Invalid; return false; } - case 255: + case State::Invalid: { // This is the state used for "not processing packets anymore" when we receive a bad packet from a client. // Do not output anything (the caller will do that for us), just return failure diff --git a/src/Protocol/Protocol_1_9.h b/src/Protocol/Protocol_1_9.h index 84da2d686..a7bd12c0a 100644 --- a/src/Protocol/Protocol_1_9.h +++ b/src/Protocol/Protocol_1_9.h @@ -37,7 +37,7 @@ class cProtocol_1_9_0: public: - cProtocol_1_9_0(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); + cProtocol_1_9_0(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, State a_State); /** Sending stuff to clients (alphabetically sorted): */ virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity & a_Vehicle) override; -- cgit v1.2.3