summaryrefslogtreecommitdiffstats
path: root/Tools/ProtoProxy/Connection.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Tools/ProtoProxy/Connection.cpp')
-rw-r--r--Tools/ProtoProxy/Connection.cpp134
1 files changed, 68 insertions, 66 deletions
diff --git a/Tools/ProtoProxy/Connection.cpp b/Tools/ProtoProxy/Connection.cpp
index 4c72c7097..f9b732142 100644
--- a/Tools/ProtoProxy/Connection.cpp
+++ b/Tools/ProtoProxy/Connection.cpp
@@ -12,6 +12,8 @@
#ifdef _WIN32
#include <direct.h> // For _mkdir()
+#else
+ #include <sys/stat.h> // for mkdir
#endif
@@ -59,25 +61,25 @@
#define COPY_TO_SERVER() \
do { \
- AString ToServer; \
+ ContiguousByteBuffer ToServer; \
m_ClientBuffer.ReadAgain(ToServer); \
switch (m_ServerState) \
{ \
case csUnencrypted: \
{ \
- SERVERSEND(ToServer.data(), ToServer.size()); \
+ SERVERSEND(ToServer); \
break; \
} \
case csEncryptedUnderstood: \
case csEncryptedUnknown: \
{ \
- SERVERENCRYPTSEND(ToServer.data(), ToServer.size()); \
+ SERVERENCRYPTSEND(ToServer); \
break; \
} \
case csWaitingForEncryption: \
{ \
Log("Waiting for server encryption, queued %u bytes", ToServer.size()); \
- m_ServerEncryptionBuffer.append(ToServer.data(), ToServer.size()); \
+ m_ServerEncryptionBuffer += ToServer; \
break; \
} \
} \
@@ -86,19 +88,19 @@
#define COPY_TO_CLIENT() \
do { \
- AString ToClient; \
+ ContiguousByteBuffer ToClient; \
m_ServerBuffer.ReadAgain(ToClient); \
switch (m_ClientState) \
{ \
case csUnencrypted: \
{ \
- CLIENTSEND(ToClient.data(), ToClient.size()); \
+ CLIENTSEND(ToClient); \
break; \
} \
case csEncryptedUnderstood: \
case csEncryptedUnknown: \
{ \
- CLIENTENCRYPTSEND(ToClient.data(), ToClient.size()); \
+ CLIENTENCRYPTSEND(ToClient); \
break; \
} \
case csWaitingForEncryption: \
@@ -114,7 +116,7 @@
do { \
if (!Proc) \
{ \
- AString Leftover; \
+ ContiguousByteBuffer Leftover; \
m_ClientBuffer.ReadAgain(Leftover); \
DataLog(Leftover.data(), Leftover.size(), "Leftover data after client packet parsing, %d bytes:", Leftover.size()); \
m_ClientBuffer.ResetRead(); \
@@ -374,15 +376,15 @@ bool cConnection::RelayFromServer(void)
}
case csEncryptedUnderstood:
{
- m_ServerDecryptor.ProcessData(reinterpret_cast<Byte *>(Buffer), reinterpret_cast<Byte *>(Buffer), static_cast<size_t>(res));
+ m_ServerDecryptor.ProcessData(reinterpret_cast<std::byte *>(Buffer), reinterpret_cast<const Byte *>(Buffer), static_cast<size_t>(res));
DataLog(Buffer, static_cast<size_t>(res), "Decrypted %d bytes from the SERVER", res);
return DecodeServersPackets(Buffer, res);
}
case csEncryptedUnknown:
{
- m_ServerDecryptor.ProcessData(reinterpret_cast<Byte *>(Buffer), reinterpret_cast<Byte *>(Buffer), static_cast<size_t>(res));
+ m_ServerDecryptor.ProcessData(reinterpret_cast<std::byte *>(Buffer), reinterpret_cast<const Byte *>(Buffer), static_cast<size_t>(res));
DataLog(Buffer, static_cast<size_t>(res), "Decrypted %d bytes from the SERVER", res);
- return CLIENTSEND(Buffer, static_cast<size_t>(res));
+ return CLIENTSEND({ reinterpret_cast<const std::byte *>(Buffer), static_cast<size_t>(res) });
}
}
ASSERT(!"Unhandled server state while relaying from server");
@@ -419,8 +421,8 @@ bool cConnection::RelayFromClient(void)
case csEncryptedUnknown:
{
DataLog(Buffer, static_cast<size_t>(res), "Decrypted %d bytes from the CLIENT", res);
- m_ServerEncryptor.ProcessData(reinterpret_cast<Byte *>(Buffer), reinterpret_cast<Byte *>(Buffer), static_cast<size_t>(res));
- return SERVERSEND(Buffer, static_cast<size_t>(res));
+ m_ServerEncryptor.ProcessData(reinterpret_cast<std::byte *>(Buffer), reinterpret_cast<const std::byte *>(Buffer), static_cast<size_t>(res));
+ return SERVERSEND({ reinterpret_cast<const std::byte *>(Buffer), static_cast<size_t>(res) });
}
}
ASSERT(!"Unhandled server state while relaying from client");
@@ -441,11 +443,11 @@ double cConnection::GetRelativeTime(void)
-bool cConnection::SendData(SOCKET a_Socket, const char * a_Data, size_t a_Size, const char * a_Peer)
+bool cConnection::SendData(SOCKET a_Socket, const ContiguousByteBufferView a_Data, const char * a_Peer)
{
- DataLog(a_Data, a_Size, "Sending data to %s, %u bytes", a_Peer, static_cast<unsigned>(a_Size));
+ DataLog(a_Data.data(), a_Data.size(), "Sending data to %s, %zu bytes", a_Peer, a_Data.size());
- int res = static_cast<int>(send(a_Socket, a_Data, a_Size, 0)); // Windows uses int for a_Size, Linux uses size_t; but Windows doesn't complain. Return type is int on Windows and ssize_t on Linux
+ int res = static_cast<int>(send(a_Socket, reinterpret_cast<const char *>(a_Data.data()), a_Data.size(), 0)); // Windows uses int for a_Size, Linux uses size_t; but Windows doesn't complain. Return type is int on Windows and ssize_t on Linux
if (res <= 0)
{
Log("%s closed the socket: %d, %d; aborting connection", a_Peer, res, SocketError);
@@ -460,32 +462,30 @@ bool cConnection::SendData(SOCKET a_Socket, const char * a_Data, size_t a_Size,
bool cConnection::SendData(SOCKET a_Socket, cByteBuffer & a_Data, const char * a_Peer)
{
- AString All;
+ ContiguousByteBuffer All;
a_Data.ReadAll(All);
a_Data.CommitRead();
- return SendData(a_Socket, All.data(), All.size(), a_Peer);
+ return SendData(a_Socket, All, a_Peer);
}
-bool cConnection::SendEncryptedData(SOCKET a_Socket, cAesCfb128Encryptor & a_Encryptor, const char * a_Data, size_t a_Size, const char * a_Peer)
+bool cConnection::SendEncryptedData(SOCKET a_Socket, cAesCfb128Encryptor & a_Encryptor, ContiguousByteBufferView a_Data, const char * a_Peer)
{
- DataLog(a_Data, a_Size, "Encrypting %d bytes to %s", a_Size, a_Peer);
- const Byte * Data = reinterpret_cast<const Byte *>(a_Data);
- while (a_Size > 0)
+ DataLog(a_Data.data(), a_Data.size(), "Encrypting %zu bytes to %s", a_Data.size(), a_Peer);
+ while (a_Data.size() > 0)
{
- Byte Buffer[64 KiB];
- size_t NumBytes = (a_Size > sizeof(Buffer)) ? sizeof(Buffer) : a_Size;
- a_Encryptor.ProcessData(Buffer, Data, NumBytes);
- bool res = SendData(a_Socket, reinterpret_cast<const char *>(Buffer), NumBytes, a_Peer);
+ std::byte Buffer[64 KiB];
+ size_t NumBytes = (a_Data.size() > sizeof(Buffer)) ? sizeof(Buffer) : a_Data.size();
+ a_Encryptor.ProcessData(Buffer, a_Data.data(), NumBytes);
+ bool res = SendData(a_Socket, { Buffer, NumBytes }, a_Peer);
if (!res)
{
return false;
}
- Data += NumBytes;
- a_Size -= NumBytes;
+ a_Data = a_Data.substr(NumBytes);
}
return true;
}
@@ -496,10 +496,10 @@ bool cConnection::SendEncryptedData(SOCKET a_Socket, cAesCfb128Encryptor & a_Enc
bool cConnection::SendEncryptedData(SOCKET a_Socket, cAesCfb128Encryptor & a_Encryptor, cByteBuffer & a_Data, const char * a_Peer)
{
- AString All;
+ ContiguousByteBuffer All;
a_Data.ReadAll(All);
a_Data.CommitRead();
- return SendEncryptedData(a_Socket, a_Encryptor, All.data(), All.size(), a_Peer);
+ return SendEncryptedData(a_Socket, a_Encryptor, All, a_Peer);
}
@@ -647,7 +647,7 @@ bool cConnection::DecodeServersPackets(const char * a_Data, int a_Size)
if (PacketLen == 0)
{
m_ServerBuffer.ResetRead();
- AString All;
+ ContiguousByteBuffer All;
m_ServerBuffer.ReadAll(All);
DataLog(All.data(), All.size(), "====== Received a bad packet length? Inspect the contents below ======");
m_ServerBuffer.CommitRead(); // Try to recover by marking everything as read
@@ -798,10 +798,11 @@ bool cConnection::HandleClientHandshake(void)
Packet.WriteVarUTF8String(ServerHost);
Packet.WriteBEUInt16(m_Server.GetConnectPort());
Packet.WriteVarInt32(NextState);
- AString Pkt;
+ ContiguousByteBuffer Pkt;
Packet.ReadAll(Pkt);
cByteBuffer ToServer(512);
- ToServer.WriteVarUTF8String(Pkt);
+ ToServer.WriteVarInt32(static_cast<UInt32>(Pkt.size()));
+ ToServer.Write(Pkt.data(), Pkt.size());
SERVERSEND(ToServer);
m_ClientProtocolState = static_cast<int>(NextState);
@@ -1111,8 +1112,8 @@ bool cConnection::HandleClientPluginMessage(void)
{
HANDLE_CLIENT_PACKET_READ(ReadVarUTF8String, AString, ChannelName);
HANDLE_CLIENT_PACKET_READ(ReadBEUInt16, UInt16, Length);
- AString Data;
- if (!m_ClientBuffer.ReadString(Data, Length))
+ ContiguousByteBuffer Data;
+ if (!m_ClientBuffer.ReadSome(Data, Length))
{
return false;
}
@@ -1253,8 +1254,8 @@ bool cConnection::HandleClientWindowClose(void)
bool cConnection::HandleClientUnknownPacket(UInt32 a_PacketType, UInt32 a_PacketLen, UInt32 a_PacketReadSoFar)
{
- AString Data;
- if (!m_ClientBuffer.ReadString(Data, a_PacketLen - a_PacketReadSoFar))
+ ContiguousByteBuffer Data;
+ if (!m_ClientBuffer.ReadSome(Data, a_PacketLen - a_PacketReadSoFar))
{
return false;
}
@@ -1288,14 +1289,14 @@ bool cConnection::HandleServerLoginEncryptionKeyRequest(void)
// Read the packet from the server:
HANDLE_SERVER_PACKET_READ(ReadVarUTF8String, AString, ServerID);
HANDLE_SERVER_PACKET_READ(ReadBEUInt16, UInt16, PublicKeyLength);
- AString PublicKey;
- if (!m_ServerBuffer.ReadString(PublicKey, PublicKeyLength))
+ ContiguousByteBuffer PublicKey;
+ if (!m_ServerBuffer.ReadSome(PublicKey, PublicKeyLength))
{
return false;
}
HANDLE_SERVER_PACKET_READ(ReadBEUInt16, UInt16, NonceLength);
- AString Nonce;
- if (!m_ServerBuffer.ReadString(Nonce, NonceLength))
+ ContiguousByteBuffer Nonce;
+ if (!m_ServerBuffer.ReadSome(Nonce, NonceLength))
{
return false;
}
@@ -1304,7 +1305,7 @@ bool cConnection::HandleServerLoginEncryptionKeyRequest(void)
DataLog(PublicKey.data(), PublicKey.size(), " Public key (%u bytes)", static_cast<unsigned>(PublicKey.size()));
// Reply to the server:
- SendEncryptionKeyResponse(PublicKey, Nonce);
+ SendEncryptionKeyResponse({ reinterpret_cast<const char *>(PublicKey.data()), PublicKey.size() }, { reinterpret_cast<const char *>(Nonce.data()), Nonce.size() });
// Do not send to client - we want the client connection open
return true;
@@ -1330,7 +1331,7 @@ bool cConnection::HandleServerLoginSuccess(void)
Log("Server communication is now encrypted");
m_ServerState = csEncryptedUnderstood;
DataLog(m_ServerEncryptionBuffer.data(), m_ServerEncryptionBuffer.size(), "Sending the queued data to server (%u bytes):", m_ServerEncryptionBuffer.size());
- SERVERENCRYPTSEND(m_ServerEncryptionBuffer.data(), m_ServerEncryptionBuffer.size());
+ SERVERENCRYPTSEND(m_ServerEncryptionBuffer);
m_ServerEncryptionBuffer.clear();
}
COPY_TO_CLIENT();
@@ -1827,8 +1828,8 @@ bool cConnection::HandleServerKick(void)
AString PacketStart("\xff");
PacketStart.push_back(static_cast<char>(ReasonBE16.size() / 256));
PacketStart.push_back(static_cast<char>(ReasonBE16.size() % 256));
- CLIENTSEND(PacketStart.data(), PacketStart.size());
- CLIENTSEND(reinterpret_cast<const char *>(ReasonBE16.data()), ReasonBE16.size() * sizeof(char16_t));
+ CLIENTSEND({ reinterpret_cast<const std::byte *>(PacketStart.data()), PacketStart.size() });
+ CLIENTSEND({ reinterpret_cast<const std::byte *>(ReasonBE16.data()), ReasonBE16.size() * sizeof(char16_t) });
return true;
}
else
@@ -1856,8 +1857,8 @@ bool cConnection::HandleServerMapChunk(void)
HANDLE_SERVER_PACKET_READ(ReadBEUInt16, UInt16, PrimaryBitmap);
HANDLE_SERVER_PACKET_READ(ReadBEUInt16, UInt16, AdditionalBitmap);
HANDLE_SERVER_PACKET_READ(ReadBEUInt32, UInt32, CompressedSize);
- AString CompressedData;
- if (!m_ServerBuffer.ReadString(CompressedData, CompressedSize))
+ ContiguousByteBuffer CompressedData;
+ if (!m_ServerBuffer.ReadSome(CompressedData, CompressedSize))
{
return false;
}
@@ -1880,8 +1881,8 @@ bool cConnection::HandleServerMapChunkBulk(void)
HANDLE_SERVER_PACKET_READ(ReadBEUInt16, UInt16, ChunkCount);
HANDLE_SERVER_PACKET_READ(ReadBEUInt32, UInt32, CompressedSize);
HANDLE_SERVER_PACKET_READ(ReadBool, bool, IsSkyLightSent);
- AString CompressedData;
- if (!m_ServerBuffer.ReadString(CompressedData, CompressedSize))
+ ContiguousByteBuffer CompressedData;
+ if (!m_ServerBuffer.ReadSome(CompressedData, CompressedSize))
{
return false;
}
@@ -1930,8 +1931,8 @@ bool cConnection::HandleServerMultiBlockChange(void)
HANDLE_SERVER_PACKET_READ(ReadBEInt32, Int32, ChunkZ);
HANDLE_SERVER_PACKET_READ(ReadBEUInt16, UInt16, NumBlocks);
HANDLE_SERVER_PACKET_READ(ReadBEUInt32, UInt32, DataSize);
- AString BlockChangeData;
- if (!m_ServerBuffer.ReadString(BlockChangeData, DataSize))
+ ContiguousByteBuffer BlockChangeData;
+ if (!m_ServerBuffer.ReadSome(BlockChangeData, DataSize))
{
return false;
}
@@ -2039,8 +2040,8 @@ bool cConnection::HandleServerPluginMessage(void)
{
HANDLE_SERVER_PACKET_READ(ReadVarUTF8String, AString, ChannelName);
HANDLE_SERVER_PACKET_READ(ReadBEUInt16, UInt16, Length);
- AString Data;
- if (!m_ServerBuffer.ReadString(Data, Length))
+ ContiguousByteBuffer Data;
+ if (!m_ServerBuffer.ReadSome(Data, Length))
{
return false;
}
@@ -2278,7 +2279,7 @@ bool cConnection::HandleServerSpawnObjectVehicle(void)
#ifdef _DEBUG
// DEBUG:
// This packet is still troublesome when DataIndicator != 0
- AString Buffer;
+ ContiguousByteBuffer Buffer;
m_ServerBuffer.ResetRead();
m_ServerBuffer.ReadAll(Buffer);
m_ServerBuffer.ResetRead();
@@ -2328,7 +2329,7 @@ bool cConnection::HandleServerSpawnObjectVehicle(void)
}
// TODO: Splash potions
}
- if ((ExtraLen > 0) && !m_ServerBuffer.ReadString(ExtraData, ExtraLen))
+ if ((ExtraLen > 0) && !m_ServerBuffer.ReadSome(ExtraData, ExtraLen))
{
return false;
}
@@ -2449,10 +2450,11 @@ bool cConnection::HandleServerStatusResponse(void)
cByteBuffer Packet(Response.size() + 50);
Packet.WriteVarInt32(0); // Packet type - status response
Packet.WriteVarUTF8String(Response);
- AString Pkt;
+ ContiguousByteBuffer Pkt;
Packet.ReadAll(Pkt);
cByteBuffer ToClient(Response.size() + 50);
- ToClient.WriteVarUTF8String(Pkt);
+ ToClient.WriteVarInt32(static_cast<UInt32>(Pkt.size()));
+ ToClient.Write(Pkt.data(), Pkt.size());
CLIENTSEND(ToClient);
return true;
}
@@ -2545,8 +2547,8 @@ bool cConnection::HandleServerUpdateTileEntity(void)
HANDLE_SERVER_PACKET_READ(ReadBEUInt8, UInt8, Action);
HANDLE_SERVER_PACKET_READ(ReadBEUInt16, UInt16, DataLength);
- AString Data;
- if ((DataLength > 0) && !m_ServerBuffer.ReadString(Data, DataLength))
+ ContiguousByteBuffer Data;
+ if ((DataLength > 0) && !m_ServerBuffer.ReadSome(Data, DataLength))
{
return false;
}
@@ -2662,9 +2664,9 @@ bool cConnection::HandleServerWindowOpen(void)
bool cConnection::HandleServerUnknownPacket(UInt32 a_PacketType, UInt32 a_PacketLen, UInt32 a_PacketReadSoFar)
{
- AString Data;
+ ContiguousByteBuffer Data;
ASSERT(a_PacketLen >= a_PacketReadSoFar);
- if (!m_ServerBuffer.ReadString(Data, a_PacketLen - a_PacketReadSoFar))
+ if (!m_ServerBuffer.ReadSome(Data, a_PacketLen - a_PacketReadSoFar))
{
return false;
}
@@ -2764,9 +2766,9 @@ bool cConnection::ParseMetadata(cByteBuffer & a_Buffer, AString & a_Metadata)
rs = rs - static_cast<int>(a_Buffer.GetReadableSpace());
cByteBuffer LenBuf(8);
LenBuf.WriteVarInt32(Len);
- AString VarLen;
+ ContiguousByteBuffer VarLen;
LenBuf.ReadAll(VarLen);
- a_Metadata.append(VarLen);
+ a_Metadata += { reinterpret_cast<const char *>(VarLen.data()), VarLen.size() };
Length = Len;
break;
}
@@ -2794,12 +2796,12 @@ bool cConnection::ParseMetadata(cByteBuffer & a_Buffer, AString & a_Metadata)
} // switch (Type)
// Read the data in this item:
- AString data;
- if (!a_Buffer.ReadString(data, Length))
+ ContiguousByteBuffer data;
+ if (!a_Buffer.ReadSome(data, Length))
{
return false;
}
- a_Metadata.append(data);
+ a_Metadata += { reinterpret_cast<const char *>(data.data()), data.size() };
if (!a_Buffer.ReadBEUInt8(x))
{
return false;