2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
// Protocol132.cpp
|
|
|
|
|
|
|
|
// Implements the cProtocol132 class representing the release 1.3.2 protocol (#39)
|
|
|
|
|
|
|
|
#include "Globals.h"
|
2013-11-24 13:44:22 -05:00
|
|
|
#include "ChunkDataSerializer.h"
|
2013-07-29 07:13:03 -04:00
|
|
|
#include "Protocol132.h"
|
|
|
|
#include "../Root.h"
|
|
|
|
#include "../Server.h"
|
2013-08-16 04:48:19 -04:00
|
|
|
#include "../World.h"
|
2013-07-29 07:13:03 -04:00
|
|
|
#include "../ClientHandle.h"
|
|
|
|
#include "../Item.h"
|
2013-08-19 05:39:13 -04:00
|
|
|
#include "../Entities/Player.h"
|
2013-07-29 07:13:03 -04:00
|
|
|
#include "../Mobs/Monster.h"
|
|
|
|
#include "../UI/Window.h"
|
2013-08-19 05:39:13 -04:00
|
|
|
#include "../Entities/Pickup.h"
|
2013-07-29 07:13:03 -04:00
|
|
|
#include "../WorldStorage/FastNBT.h"
|
2014-01-19 10:38:59 -05:00
|
|
|
#include "../WorldStorage/EnchantmentSerializer.h"
|
2013-07-29 07:13:03 -04:00
|
|
|
#include "../StringCompression.h"
|
2014-04-29 11:37:15 -04:00
|
|
|
#include "PolarSSL++/Sha1Checksum.h"
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
|
2014-01-23 17:35:23 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
#define HANDLE_PACKET_READ(Proc, Type, Var) \
|
|
|
|
Type Var; \
|
|
|
|
{ \
|
|
|
|
if (!m_ReceivedData.Proc(Var)) \
|
|
|
|
{ \
|
|
|
|
m_ReceivedData.CheckValid(); \
|
|
|
|
return PARSE_INCOMPLETE; \
|
|
|
|
} \
|
|
|
|
m_ReceivedData.CheckValid(); \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const int MAX_ENC_LEN = 512; // Maximum size of the encrypted message; should be 128, but who knows...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PACKET_KEEP_ALIVE = 0x00,
|
|
|
|
PACKET_LOGIN = 0x01,
|
|
|
|
PACKET_ENTITY_EQUIPMENT = 0x05,
|
|
|
|
PACKET_COMPASS = 0x06,
|
|
|
|
PACKET_PLAYER_SPAWN = 0x14,
|
|
|
|
PACKET_COLLECT_PICKUP = 0x16,
|
|
|
|
PACKET_SPAWN_MOB = 0x18,
|
|
|
|
PACKET_DESTROY_ENTITIES = 0x1d,
|
|
|
|
PACKET_CHUNK_DATA = 0x33,
|
|
|
|
PACKET_BLOCK_CHANGE = 0x35,
|
|
|
|
PACKET_BLOCK_ACTION = 0x36,
|
|
|
|
PACKET_BLOCK_BREAK_ANIM = 0x37,
|
|
|
|
PACKET_SOUND_EFFECT = 0x3e,
|
|
|
|
PACKET_SOUND_PARTICLE_EFFECT = 0x3d,
|
2013-07-30 16:48:59 -04:00
|
|
|
PACKET_TAB_COMPLETION = 0xcb,
|
2013-07-29 07:13:03 -04:00
|
|
|
PACKET_LOCALE_VIEW_DISTANCE = 0xcc,
|
|
|
|
PACKET_CLIENT_STATUSES = 0xcd,
|
|
|
|
PACKET_ENCRYPTION_KEY_RESP = 0xfc,
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-07-29 07:13:03 -04:00
|
|
|
// cProtocol132:
|
|
|
|
|
|
|
|
cProtocol132::cProtocol132(cClientHandle * a_Client) :
|
|
|
|
super(a_Client),
|
|
|
|
m_IsEncrypted(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cProtocol132::~cProtocol132()
|
|
|
|
{
|
|
|
|
if (!m_DataToSend.empty())
|
|
|
|
{
|
2014-03-12 13:34:50 -04:00
|
|
|
LOGD("There are " SIZE_T_FMT " unsent bytes while deleting cProtocol132", m_DataToSend.size());
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-03-07 14:04:25 -05:00
|
|
|
void cProtocol132::DataReceived(const char * a_Data, size_t a_Size)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
if (m_IsEncrypted)
|
|
|
|
{
|
2014-01-23 17:35:23 -05:00
|
|
|
Byte Decrypted[512];
|
2013-07-29 07:13:03 -04:00
|
|
|
while (a_Size > 0)
|
|
|
|
{
|
2014-04-04 04:31:50 -04:00
|
|
|
size_t NumBytes = (a_Size > sizeof(Decrypted)) ? sizeof(Decrypted) : a_Size;
|
2014-01-23 17:35:23 -05:00
|
|
|
m_Decryptor.ProcessData(Decrypted, (Byte *)a_Data, NumBytes);
|
2013-07-29 07:13:03 -04:00
|
|
|
super::DataReceived((const char *)Decrypted, NumBytes);
|
|
|
|
a_Size -= NumBytes;
|
|
|
|
a_Data += NumBytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
super::DataReceived(a_Data, a_Size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cProtocol132::SendBlockAction(int a_BlockX, int a_BlockY, int a_BlockZ, char a_Byte1, char a_Byte2, BLOCKTYPE a_BlockType)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSPacket);
|
|
|
|
WriteByte (PACKET_BLOCK_ACTION);
|
|
|
|
WriteInt (a_BlockX);
|
|
|
|
WriteShort((short)a_BlockY);
|
|
|
|
WriteInt (a_BlockZ);
|
2014-04-04 04:31:50 -04:00
|
|
|
WriteChar (a_Byte1);
|
|
|
|
WriteChar (a_Byte2);
|
2013-07-29 07:13:03 -04:00
|
|
|
WriteShort(a_BlockType);
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cProtocol132::SendBlockBreakAnim(int a_entityID, int a_BlockX, int a_BlockY, int a_BlockZ, char stage)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSPacket);
|
|
|
|
WriteByte (PACKET_BLOCK_BREAK_ANIM);
|
|
|
|
WriteInt (a_entityID);
|
|
|
|
WriteInt (a_BlockX);
|
|
|
|
WriteInt (a_BlockY);
|
|
|
|
WriteInt (a_BlockZ);
|
2014-04-04 04:31:50 -04:00
|
|
|
WriteChar (stage);
|
2013-07-29 07:13:03 -04:00
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cProtocol132::SendBlockChange(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSPacket);
|
|
|
|
WriteByte (PACKET_BLOCK_CHANGE);
|
|
|
|
WriteInt (a_BlockX);
|
|
|
|
WriteByte ((unsigned char)a_BlockY);
|
|
|
|
WriteInt (a_BlockZ);
|
|
|
|
WriteShort(a_BlockType);
|
|
|
|
WriteByte (a_BlockMeta);
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cProtocol132::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerializer & a_Serializer)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSPacket);
|
|
|
|
|
|
|
|
// Pre-chunk not used in 1.3.2. Finally.
|
|
|
|
|
|
|
|
// Send the chunk data:
|
2014-09-07 18:36:30 -04:00
|
|
|
AString Serialized = a_Serializer.Serialize(cChunkDataSerializer::RELEASE_1_3_2, a_ChunkX, a_ChunkZ);
|
2013-07-29 07:13:03 -04:00
|
|
|
WriteByte(PACKET_CHUNK_DATA);
|
|
|
|
WriteInt (a_ChunkX);
|
|
|
|
WriteInt (a_ChunkZ);
|
|
|
|
SendData(Serialized.data(), Serialized.size());
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-27 14:56:29 -04:00
|
|
|
void cProtocol132::SendCollectEntity(const cEntity & a_Entity, const cPlayer & a_Player)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSPacket);
|
|
|
|
WriteByte(PACKET_COLLECT_PICKUP);
|
2014-06-27 14:56:29 -04:00
|
|
|
WriteInt (a_Entity.GetUniqueID());
|
2013-07-29 07:13:03 -04:00
|
|
|
WriteInt (a_Player.GetUniqueID());
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cProtocol132::SendDestroyEntity(const cEntity & a_Entity)
|
|
|
|
{
|
|
|
|
if (a_Entity.GetUniqueID() == m_Client->GetPlayer()->GetUniqueID())
|
|
|
|
{
|
|
|
|
// Do not send "destroy self" to the client, the client would crash (FS #254)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cCSLock Lock(m_CSPacket);
|
|
|
|
WriteByte(PACKET_DESTROY_ENTITIES);
|
|
|
|
WriteByte(1); // entity count
|
|
|
|
WriteInt (a_Entity.GetUniqueID());
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cProtocol132::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum, const cItem & a_Item)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSPacket);
|
|
|
|
WriteByte (PACKET_ENTITY_EQUIPMENT);
|
|
|
|
WriteInt (a_Entity.GetUniqueID());
|
|
|
|
WriteShort(a_SlotNum);
|
|
|
|
WriteItem (a_Item);
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cProtocol132::SendLogin(const cPlayer & a_Player, const cWorld & a_World)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSPacket);
|
|
|
|
WriteByte (PACKET_LOGIN);
|
|
|
|
WriteInt (a_Player.GetUniqueID()); // EntityID of the player
|
|
|
|
WriteString("default"); // Level type
|
2014-04-04 04:31:50 -04:00
|
|
|
WriteByte ((Byte)a_Player.GetGameMode());
|
2013-07-29 07:13:03 -04:00
|
|
|
WriteByte ((Byte)(a_World.GetDimension()));
|
|
|
|
WriteByte (2); // TODO: Difficulty
|
|
|
|
WriteByte (0); // Unused, used to be world height
|
|
|
|
WriteByte (8); // Client list width or something
|
|
|
|
Flush();
|
2014-06-08 15:58:08 -04:00
|
|
|
m_LastSentDimension = a_World.GetDimension();
|
2013-07-29 07:13:03 -04:00
|
|
|
SendCompass(a_World);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cProtocol132::SendPlayerSpawn(const cPlayer & a_Player)
|
|
|
|
{
|
|
|
|
const cItem & HeldItem = a_Player.GetEquippedItem();
|
|
|
|
cCSLock Lock(m_CSPacket);
|
|
|
|
WriteByte (PACKET_PLAYER_SPAWN);
|
|
|
|
WriteInt (a_Player.GetUniqueID());
|
|
|
|
WriteString(a_Player.GetName());
|
|
|
|
WriteInt ((int)(a_Player.GetPosX() * 32));
|
|
|
|
WriteInt ((int)(a_Player.GetPosY() * 32));
|
|
|
|
WriteInt ((int)(a_Player.GetPosZ() * 32));
|
2014-04-04 04:31:50 -04:00
|
|
|
WriteChar ((char)((a_Player.GetYaw() / 360.f) * 256));
|
|
|
|
WriteChar ((char)((a_Player.GetPitch() / 360.f) * 256));
|
2013-07-29 07:13:03 -04:00
|
|
|
WriteShort (HeldItem.IsEmpty() ? 0 : HeldItem.m_ItemType);
|
|
|
|
// Player metadata: just use a default metadata value, since the client doesn't like starting without any metadata:
|
|
|
|
WriteByte (0); // Index 0, byte (flags)
|
|
|
|
WriteByte (0); // Flags, empty
|
|
|
|
WriteByte (0x7f); // End of metadata
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-12 20:08:02 -04:00
|
|
|
void cProtocol132::SendSoundEffect(const AString & a_SoundName, double a_X, double a_Y, double a_Z, float a_Volume, float a_Pitch)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSPacket);
|
|
|
|
WriteByte (PACKET_SOUND_EFFECT);
|
|
|
|
WriteString (a_SoundName);
|
2014-07-13 07:31:09 -04:00
|
|
|
WriteInt ((int)(a_X * 8.0));
|
|
|
|
WriteInt ((int)(a_Y * 8.0));
|
|
|
|
WriteInt ((int)(a_Z * 8.0));
|
2013-07-29 07:13:03 -04:00
|
|
|
WriteFloat (a_Volume);
|
2014-04-04 04:31:50 -04:00
|
|
|
WriteChar ((char)(a_Pitch * 63.0f));
|
2013-07-29 07:13:03 -04:00
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cProtocol132::SendSoundParticleEffect(int a_EffectID, int a_SrcX, int a_SrcY, int a_SrcZ, int a_Data)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSPacket);
|
|
|
|
WriteByte(PACKET_SOUND_PARTICLE_EFFECT);
|
|
|
|
WriteInt (a_EffectID);
|
2013-11-12 16:43:20 -05:00
|
|
|
WriteInt (a_SrcX);
|
2014-04-04 04:31:50 -04:00
|
|
|
WriteByte((Byte)a_SrcY);
|
2013-11-12 16:43:20 -05:00
|
|
|
WriteInt (a_SrcZ);
|
2013-07-29 07:13:03 -04:00
|
|
|
WriteInt (a_Data);
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cProtocol132::SendSpawnMob(const cMonster & a_Mob)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSPacket);
|
|
|
|
WriteByte (PACKET_SPAWN_MOB);
|
|
|
|
WriteInt (a_Mob.GetUniqueID());
|
2014-04-04 04:31:50 -04:00
|
|
|
WriteByte ((Byte)a_Mob.GetMobType());
|
2013-07-29 07:13:03 -04:00
|
|
|
WriteVectorI((Vector3i)(a_Mob.GetPosition() * 32));
|
2014-01-17 05:11:17 -05:00
|
|
|
WriteByte ((Byte)((a_Mob.GetYaw() / 360.f) * 256));
|
|
|
|
WriteByte ((Byte)((a_Mob.GetPitch() / 360.f) * 256));
|
2013-07-29 07:13:03 -04:00
|
|
|
WriteByte ((Byte)((a_Mob.GetHeadYaw() / 360.f) * 256));
|
|
|
|
WriteShort ((short)(a_Mob.GetSpeedX() * 400));
|
|
|
|
WriteShort ((short)(a_Mob.GetSpeedY() * 400));
|
|
|
|
WriteShort ((short)(a_Mob.GetSpeedZ() * 400));
|
2013-10-10 19:41:54 -04:00
|
|
|
|
|
|
|
WriteCommonMetadata(a_Mob);
|
|
|
|
WriteMobMetadata(a_Mob);
|
|
|
|
WriteByte(0x7f);
|
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-30 16:48:59 -04:00
|
|
|
void cProtocol132::SendTabCompletionResults(const AStringVector & a_Results)
|
|
|
|
{
|
|
|
|
if (a_Results.empty())
|
|
|
|
{
|
|
|
|
// No results to send
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AString Serialized(a_Results[0]);
|
|
|
|
for (AStringVector::const_iterator itr = a_Results.begin() + 1, end = a_Results.end(); itr != end; ++itr)
|
|
|
|
{
|
|
|
|
Serialized.push_back(0);
|
|
|
|
Serialized.append(*itr);
|
|
|
|
} // for itr - a_Results[]
|
|
|
|
|
|
|
|
cCSLock Lock(m_CSPacket);
|
|
|
|
WriteByte(PACKET_TAB_COMPLETION);
|
|
|
|
WriteString(Serialized);
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
void cProtocol132::SendUnloadChunk(int a_ChunkX, int a_ChunkZ)
|
|
|
|
{
|
2013-09-08 11:57:50 -04:00
|
|
|
// Unloading the chunk is done by sending a "map chunk" packet
|
|
|
|
// with IncludeInitialize set to true and primary bitmap set to 0:
|
|
|
|
cCSLock Lock(m_CSPacket);
|
|
|
|
WriteByte(PACKET_CHUNK_DATA);
|
|
|
|
WriteInt (a_ChunkX);
|
|
|
|
WriteInt (a_ChunkZ);
|
|
|
|
WriteBool(true); // IncludeInitialize
|
|
|
|
WriteShort(0); // Primary bitmap
|
|
|
|
WriteShort(0); // Add bitmap
|
|
|
|
WriteInt(0);
|
|
|
|
Flush();
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cProtocol132::SendWholeInventory(const cWindow & a_Window)
|
|
|
|
{
|
2014-07-17 16:50:58 -04:00
|
|
|
// 1.3.2 requires player inventory slots to be sent as SetSlot packets,
|
2013-07-29 07:13:03 -04:00
|
|
|
// otherwise it sometimes fails to update the window
|
|
|
|
|
|
|
|
// Send the entire window:
|
|
|
|
super::SendWholeInventory(a_Window);
|
|
|
|
|
|
|
|
// Send the player inventory and hotbar:
|
2014-04-18 15:09:44 -04:00
|
|
|
cPlayer * Player = m_Client->GetPlayer();
|
|
|
|
const cInventory & Inventory = Player->GetInventory();
|
2013-07-29 07:13:03 -04:00
|
|
|
int BaseOffset = a_Window.GetNumSlots() - (cInventory::invNumSlots - cInventory::invInventoryOffset); // Number of non-inventory slots
|
|
|
|
char WindowID = a_Window.GetWindowID();
|
2014-04-04 04:31:50 -04:00
|
|
|
for (short i = 0; i < cInventory::invInventoryCount; i++)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
SendInventorySlot(WindowID, BaseOffset + i, Inventory.GetInventorySlot(i));
|
|
|
|
} // for i - Inventory[]
|
|
|
|
BaseOffset += cInventory::invInventoryCount;
|
2014-04-04 04:31:50 -04:00
|
|
|
for (short i = 0; i < cInventory::invHotbarCount; i++)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
SendInventorySlot(WindowID, BaseOffset + i, Inventory.GetHotbarSlot(i));
|
|
|
|
} // for i - Hotbar[]
|
|
|
|
|
|
|
|
// Send even the item being dragged:
|
2014-04-18 15:09:44 -04:00
|
|
|
SendInventorySlot(-1, -1, Player->GetDraggingItem());
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AString cProtocol132::GetAuthServerID(void)
|
|
|
|
{
|
|
|
|
// http://wiki.vg/wiki/index.php?title=Session&oldid=2615
|
|
|
|
// Server uses SHA1 to mix ServerID, Client secret and server public key together
|
|
|
|
// The mixing is done in StartEncryption, the result is in m_AuthServerID
|
|
|
|
|
|
|
|
return m_AuthServerID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cProtocol132::ParsePacket(unsigned char a_PacketType)
|
|
|
|
{
|
|
|
|
switch (a_PacketType)
|
|
|
|
{
|
|
|
|
default: return super::ParsePacket(a_PacketType); // off-load previously known packets into cProtocol125
|
|
|
|
case PACKET_CLIENT_STATUSES: return ParseClientStatuses();
|
|
|
|
case PACKET_ENCRYPTION_KEY_RESP: return ParseEncryptionKeyResponse();
|
2013-07-30 16:48:59 -04:00
|
|
|
case PACKET_LOCALE_VIEW_DISTANCE: return ParseLocaleViewDistance();
|
|
|
|
case PACKET_TAB_COMPLETION: return ParseTabCompletion();
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cProtocol132::ParseBlockPlace(void)
|
|
|
|
{
|
|
|
|
HANDLE_PACKET_READ(ReadBEInt, int, PosX);
|
|
|
|
HANDLE_PACKET_READ(ReadByte, Byte, PosY);
|
|
|
|
HANDLE_PACKET_READ(ReadBEInt, int, PosZ);
|
|
|
|
HANDLE_PACKET_READ(ReadChar, char, BlockFace);
|
|
|
|
|
|
|
|
cItem HeldItem;
|
|
|
|
int res = ParseItem(HeldItem);
|
|
|
|
if (res < 0)
|
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE_PACKET_READ(ReadChar, char, CursorX);
|
|
|
|
HANDLE_PACKET_READ(ReadChar, char, CursorY);
|
|
|
|
HANDLE_PACKET_READ(ReadChar, char, CursorZ);
|
|
|
|
|
2014-02-04 13:59:05 -05:00
|
|
|
m_Client->HandleRightClick(PosX, PosY, PosZ, static_cast<eBlockFace>(BlockFace), CursorX, CursorY, CursorZ, HeldItem);
|
2013-07-29 07:13:03 -04:00
|
|
|
return PARSE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cProtocol132::ParseHandshake(void)
|
|
|
|
{
|
|
|
|
HANDLE_PACKET_READ(ReadByte, Byte, ProtocolVersion);
|
|
|
|
HANDLE_PACKET_READ(ReadBEUTF16String16, AString, Username);
|
|
|
|
HANDLE_PACKET_READ(ReadBEUTF16String16, AString, ServerHost);
|
|
|
|
HANDLE_PACKET_READ(ReadBEInt, int, ServerPort);
|
|
|
|
m_Username = Username;
|
|
|
|
|
2014-07-21 09:19:48 -04:00
|
|
|
if (!m_Client->HandleHandshake( m_Username))
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2014-07-17 16:15:34 -04:00
|
|
|
return PARSE_OK; // Player is not allowed into the server
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
2014-01-23 17:35:23 -05:00
|
|
|
// Send a 0xfd Encryption Key Request http://wiki.vg/Protocol#0xFD
|
2013-07-29 07:13:03 -04:00
|
|
|
SendEncryptionKeyRequest();
|
|
|
|
|
|
|
|
return PARSE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cProtocol132::ParseClientStatuses(void)
|
|
|
|
{
|
2014-01-23 17:35:23 -05:00
|
|
|
HANDLE_PACKET_READ(ReadByte, Byte, Status);
|
2013-07-29 07:13:03 -04:00
|
|
|
if ((Status & 1) == 0)
|
|
|
|
{
|
|
|
|
m_Client->HandleLogin(39, m_Username);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_Client->HandleRespawn();
|
|
|
|
}
|
|
|
|
return PARSE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cProtocol132::ParseEncryptionKeyResponse(void)
|
|
|
|
{
|
2014-04-04 04:31:50 -04:00
|
|
|
// Read the encryption key:
|
2013-07-29 07:13:03 -04:00
|
|
|
HANDLE_PACKET_READ(ReadBEShort, short, EncKeyLength);
|
2014-04-04 04:31:50 -04:00
|
|
|
if (EncKeyLength > MAX_ENC_LEN)
|
|
|
|
{
|
|
|
|
LOGD("Too long encryption key");
|
|
|
|
m_Client->Kick("Hacked client");
|
|
|
|
return PARSE_OK;
|
|
|
|
}
|
2013-07-29 07:13:03 -04:00
|
|
|
AString EncKey;
|
2014-04-04 04:31:50 -04:00
|
|
|
if (!m_ReceivedData.ReadString(EncKey, (size_t)EncKeyLength))
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
return PARSE_INCOMPLETE;
|
|
|
|
}
|
2014-04-04 04:31:50 -04:00
|
|
|
|
|
|
|
// Read the encryption nonce:
|
2013-07-29 07:13:03 -04:00
|
|
|
HANDLE_PACKET_READ(ReadBEShort, short, EncNonceLength);
|
|
|
|
AString EncNonce;
|
2014-04-04 04:31:50 -04:00
|
|
|
if (!m_ReceivedData.ReadString(EncNonce, (size_t)EncNonceLength))
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
return PARSE_INCOMPLETE;
|
|
|
|
}
|
2014-04-04 04:31:50 -04:00
|
|
|
if (EncNonceLength > MAX_ENC_LEN)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2014-04-04 04:31:50 -04:00
|
|
|
LOGD("Too long encryption nonce");
|
2013-07-29 07:13:03 -04:00
|
|
|
m_Client->Kick("Hacked client");
|
|
|
|
return PARSE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HandleEncryptionKeyResponse(EncKey, EncNonce);
|
|
|
|
return PARSE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cProtocol132::ParseLocaleViewDistance(void)
|
|
|
|
{
|
|
|
|
HANDLE_PACKET_READ(ReadBEUTF16String16, AString, Locale);
|
|
|
|
HANDLE_PACKET_READ(ReadChar, char, ViewDistance);
|
|
|
|
HANDLE_PACKET_READ(ReadChar, char, ChatFlags);
|
|
|
|
HANDLE_PACKET_READ(ReadChar, char, ClientDifficulty);
|
2014-02-16 07:26:07 -05:00
|
|
|
m_Client->SetLocale(Locale);
|
2013-07-29 07:13:03 -04:00
|
|
|
// TODO: m_Client->HandleViewDistance(ViewDistance);
|
|
|
|
// TODO: m_Client->HandleChatFlags(ChatFlags);
|
|
|
|
// Ignoring client difficulty
|
|
|
|
return PARSE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cProtocol132::ParseLogin(void)
|
|
|
|
{
|
|
|
|
// Login packet not used in 1.3.2
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cProtocol132::ParsePlayerAbilities(void)
|
|
|
|
{
|
|
|
|
HANDLE_PACKET_READ(ReadBool, bool, Flags);
|
|
|
|
HANDLE_PACKET_READ(ReadChar, char, FlyingSpeed);
|
|
|
|
HANDLE_PACKET_READ(ReadChar, char, WalkingSpeed);
|
|
|
|
// TODO: m_Client->HandlePlayerAbilities(...);
|
|
|
|
return PARSE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-30 16:48:59 -04:00
|
|
|
int cProtocol132::ParseTabCompletion(void)
|
|
|
|
{
|
|
|
|
HANDLE_PACKET_READ(ReadBEUTF16String16, AString, Text);
|
|
|
|
m_Client->HandleTabCompletion(Text);
|
|
|
|
return PARSE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-04-04 04:13:25 -04:00
|
|
|
void cProtocol132::SendData(const char * a_Data, size_t a_Size)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
m_DataToSend.append(a_Data, a_Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cProtocol132::Flush(void)
|
|
|
|
{
|
|
|
|
ASSERT(m_CSPacket.IsLockedByCurrentThread()); // Did all packets lock the CS properly?
|
|
|
|
|
|
|
|
if (m_DataToSend.empty())
|
|
|
|
{
|
|
|
|
LOGD("Flushing empty");
|
|
|
|
return;
|
|
|
|
}
|
2014-04-04 04:31:50 -04:00
|
|
|
const char * Data = m_DataToSend.data();
|
|
|
|
size_t Size = m_DataToSend.size();
|
2013-07-29 07:13:03 -04:00
|
|
|
if (m_IsEncrypted)
|
|
|
|
{
|
2014-01-23 17:35:23 -05:00
|
|
|
Byte Encrypted[8192]; // Larger buffer, we may be sending lots of data (chunks)
|
2014-04-04 04:31:50 -04:00
|
|
|
while (Size > 0)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2014-04-04 04:31:50 -04:00
|
|
|
size_t NumBytes = (Size > sizeof(Encrypted)) ? sizeof(Encrypted) : Size;
|
|
|
|
m_Encryptor.ProcessData(Encrypted, (Byte *)Data, NumBytes);
|
2013-07-29 07:13:03 -04:00
|
|
|
super::SendData((const char *)Encrypted, NumBytes);
|
2014-04-04 04:31:50 -04:00
|
|
|
Size -= NumBytes;
|
|
|
|
Data += NumBytes;
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-04 04:31:50 -04:00
|
|
|
super::SendData(Data, Size);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
m_DataToSend.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cProtocol132::WriteItem(const cItem & a_Item)
|
|
|
|
{
|
|
|
|
short ItemType = a_Item.m_ItemType;
|
|
|
|
ASSERT(ItemType >= -1); // Check validity of packets in debug runtime
|
|
|
|
if (ItemType <= 0)
|
|
|
|
{
|
|
|
|
// Fix, to make sure no invalid values are sent.
|
|
|
|
ItemType = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a_Item.IsEmpty())
|
|
|
|
{
|
|
|
|
WriteShort(-1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteShort(ItemType);
|
2014-04-04 04:31:50 -04:00
|
|
|
WriteChar (a_Item.m_ItemCount);
|
2013-07-29 07:13:03 -04:00
|
|
|
WriteShort(a_Item.m_ItemDamage);
|
|
|
|
|
|
|
|
if (a_Item.m_Enchantments.IsEmpty())
|
|
|
|
{
|
|
|
|
WriteShort(-1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the enchantments:
|
|
|
|
cFastNBTWriter Writer;
|
|
|
|
const char * TagName = (a_Item.m_ItemType == E_ITEM_BOOK) ? "StoredEnchantments" : "ench";
|
2014-01-19 11:52:45 -05:00
|
|
|
EnchantmentSerializer::WriteToNBTCompound(a_Item.m_Enchantments, Writer, TagName);
|
2013-07-29 07:13:03 -04:00
|
|
|
Writer.Finish();
|
|
|
|
AString Compressed;
|
|
|
|
CompressStringGZIP(Writer.GetResult().data(), Writer.GetResult().size(), Compressed);
|
2014-04-04 04:31:50 -04:00
|
|
|
WriteShort((short)Compressed.size());
|
2013-07-29 07:13:03 -04:00
|
|
|
SendData(Compressed.data(), Compressed.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cProtocol132::ParseItem(cItem & a_Item)
|
|
|
|
{
|
|
|
|
HANDLE_PACKET_READ(ReadBEShort, short, ItemType);
|
|
|
|
|
|
|
|
if (ItemType <= -1)
|
|
|
|
{
|
|
|
|
a_Item.Empty();
|
|
|
|
return PARSE_OK;
|
|
|
|
}
|
|
|
|
a_Item.m_ItemType = ItemType;
|
|
|
|
|
|
|
|
HANDLE_PACKET_READ(ReadChar, char, ItemCount);
|
|
|
|
HANDLE_PACKET_READ(ReadBEShort, short, ItemDamage);
|
|
|
|
a_Item.m_ItemCount = ItemCount;
|
|
|
|
a_Item.m_ItemDamage = ItemDamage;
|
|
|
|
if (ItemCount <= 0)
|
|
|
|
{
|
|
|
|
a_Item.Empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE_PACKET_READ(ReadBEShort, short, MetadataLength);
|
|
|
|
if (MetadataLength <= 0)
|
|
|
|
{
|
|
|
|
return PARSE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the metadata
|
|
|
|
AString Metadata;
|
2014-04-04 04:31:50 -04:00
|
|
|
Metadata.resize((size_t)MetadataLength);
|
|
|
|
if (!m_ReceivedData.ReadBuf((void *)Metadata.data(), (size_t)MetadataLength))
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
return PARSE_INCOMPLETE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ParseItemMetadata(a_Item, Metadata);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cProtocol132::ParseItemMetadata(cItem & a_Item, const AString & a_Metadata)
|
|
|
|
{
|
|
|
|
// Uncompress the GZIPped data:
|
|
|
|
AString Uncompressed;
|
|
|
|
if (UncompressStringGZIP(a_Metadata.data(), a_Metadata.size(), Uncompressed) != Z_OK)
|
|
|
|
{
|
|
|
|
AString HexDump;
|
|
|
|
CreateHexDump(HexDump, a_Metadata.data(), a_Metadata.size(), 16);
|
|
|
|
LOG("Cannot unGZIP item metadata:\n%s", HexDump.c_str());
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse into NBT:
|
|
|
|
cParsedNBT NBT(Uncompressed.data(), Uncompressed.size());
|
|
|
|
if (!NBT.IsValid())
|
|
|
|
{
|
|
|
|
AString HexDump;
|
|
|
|
CreateHexDump(HexDump, Uncompressed.data(), Uncompressed.size(), 16);
|
|
|
|
LOG("Cannot parse NBT item metadata:\n%s", HexDump.c_str());
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load enchantments from the NBT:
|
|
|
|
for (int tag = NBT.GetFirstChild(NBT.GetRoot()); tag >= 0; tag = NBT.GetNextSibling(tag))
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
(NBT.GetType(tag) == TAG_List) &&
|
|
|
|
(
|
|
|
|
(NBT.GetName(tag) == "ench") ||
|
|
|
|
(NBT.GetName(tag) == "StoredEnchantments")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
{
|
2014-01-19 11:52:45 -05:00
|
|
|
EnchantmentSerializer::ParseFromNBT(a_Item.m_Enchantments, NBT, tag);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PARSE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cProtocol132::SendCompass(const cWorld & a_World)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSPacket);
|
|
|
|
WriteByte(PACKET_COMPASS);
|
|
|
|
WriteInt((int)(a_World.GetSpawnX()));
|
|
|
|
WriteInt((int)(a_World.GetSpawnY()));
|
|
|
|
WriteInt((int)(a_World.GetSpawnZ()));
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cProtocol132::SendEncryptionKeyRequest(void)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CSPacket);
|
2014-04-18 15:09:44 -04:00
|
|
|
cServer * Server = cRoot::Get()->GetServer();
|
2014-01-06 16:22:33 -05:00
|
|
|
WriteByte(0xfd);
|
2014-04-18 15:09:44 -04:00
|
|
|
WriteString(Server->GetServerID());
|
|
|
|
const AString & PublicKeyDER = Server->GetPublicKeyDER();
|
2014-04-18 16:20:24 -04:00
|
|
|
WriteShort((short)(PublicKeyDER.size()));
|
2014-04-18 15:09:44 -04:00
|
|
|
SendData(PublicKeyDER.data(), PublicKeyDER.size());
|
2013-07-29 07:13:03 -04:00
|
|
|
WriteShort(4);
|
|
|
|
WriteInt((int)(intptr_t)this); // Using 'this' as the cryptographic nonce, so that we don't have to generate one each time :)
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cProtocol132::HandleEncryptionKeyResponse(const AString & a_EncKey, const AString & a_EncNonce)
|
|
|
|
{
|
|
|
|
// Decrypt EncNonce using privkey
|
2014-04-29 05:04:54 -04:00
|
|
|
cRsaPrivateKey & rsaDecryptor = cRoot::Get()->GetServer()->GetPrivateKey();
|
2014-01-23 17:35:23 -05:00
|
|
|
|
2014-01-02 12:08:38 -05:00
|
|
|
Int32 DecryptedNonce[MAX_ENC_LEN / sizeof(Int32)];
|
2014-01-23 17:35:23 -05:00
|
|
|
int res = rsaDecryptor.Decrypt((const Byte *)a_EncNonce.data(), a_EncNonce.size(), (Byte *)DecryptedNonce, sizeof(DecryptedNonce));
|
|
|
|
if (res != 4)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
LOGD("Bad nonce length");
|
|
|
|
m_Client->Kick("Hacked client");
|
|
|
|
return;
|
|
|
|
}
|
2014-01-02 12:08:38 -05:00
|
|
|
if (ntohl(DecryptedNonce[0]) != (unsigned)(uintptr_t)this)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
LOGD("Bad nonce value");
|
|
|
|
m_Client->Kick("Hacked client");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt the symmetric encryption key using privkey:
|
2014-01-23 17:35:23 -05:00
|
|
|
Byte DecryptedKey[MAX_ENC_LEN];
|
|
|
|
res = rsaDecryptor.Decrypt((const Byte *)a_EncKey.data(), a_EncKey.size(), DecryptedKey, sizeof(DecryptedKey));
|
|
|
|
if (res != 16)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
LOGD("Bad key length");
|
|
|
|
m_Client->Kick("Hacked client");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Send encryption key response:
|
|
|
|
cCSLock Lock(m_CSPacket);
|
2014-01-06 16:22:33 -05:00
|
|
|
WriteByte(0xfc);
|
2013-07-29 07:13:03 -04:00
|
|
|
WriteShort(0);
|
|
|
|
WriteShort(0);
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
2014-01-23 17:35:23 -05:00
|
|
|
#ifdef _DEBUG
|
|
|
|
AString DecryptedKeyHex;
|
|
|
|
CreateHexDump(DecryptedKeyHex, DecryptedKey, res, 16);
|
|
|
|
LOGD("Received encryption key, %d bytes:\n%s", res, DecryptedKeyHex.c_str());
|
|
|
|
#endif
|
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
StartEncryption(DecryptedKey);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-23 17:35:23 -05:00
|
|
|
void cProtocol132::StartEncryption(const Byte * a_Key)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2014-01-23 17:35:23 -05:00
|
|
|
m_Encryptor.Init(a_Key, a_Key);
|
|
|
|
m_Decryptor.Init(a_Key, a_Key);
|
2013-07-29 07:13:03 -04:00
|
|
|
m_IsEncrypted = true;
|
|
|
|
|
|
|
|
// Prepare the m_AuthServerID:
|
2014-04-29 11:37:15 -04:00
|
|
|
cSha1Checksum Checksum;
|
2014-04-18 15:09:44 -04:00
|
|
|
cServer * Server = cRoot::Get()->GetServer();
|
|
|
|
AString ServerID = Server->GetServerID();
|
2014-01-23 17:35:23 -05:00
|
|
|
Checksum.Update((const Byte *)ServerID.c_str(), ServerID.length());
|
2013-07-29 07:13:03 -04:00
|
|
|
Checksum.Update(a_Key, 16);
|
2014-04-18 16:20:24 -04:00
|
|
|
Checksum.Update((const Byte *)Server->GetPublicKeyDER().data(), Server->GetPublicKeyDER().size());
|
2014-01-23 17:35:23 -05:00
|
|
|
Byte Digest[20];
|
|
|
|
Checksum.Finalize(Digest);
|
2014-04-29 11:37:15 -04:00
|
|
|
cSha1Checksum::DigestToJava(Digest, m_AuthServerID);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|