diff --git a/src/game/WorldSocket.cpp b/src/game/WorldSocket.cpp index 8cd7c423c..8c246cf9c 100644 --- a/src/game/WorldSocket.cpp +++ b/src/game/WorldSocket.cpp @@ -52,8 +52,48 @@ struct ServerPktHeader { - uint16 size; - uint16 cmd; + /** + * size is the length of the payload _plus_ the length of the opcode + */ + ServerPktHeader(uint32 size, uint16 cmd) : size(size) + { + uint8 headerIndex=0; + if(isLargePacket()) + { + sLog.outDebug("initializing large server to client packet. Size: %u, cmd: %u", size, cmd); + header= new uint8[5]; + header[headerIndex++] = 0x80|(0xFF &(size>>16)); + } + else + { + header= new uint8[4]; + } + + header[headerIndex++] = 0xFF &(size>>8); + header[headerIndex++] = 0xFF &size; + + header[headerIndex++] = 0xFF & cmd; + header[headerIndex++] = 0xFF & (cmd>>8); + } + + ~ServerPktHeader() + { + delete[] header; + } + + uint8 getHeaderLength() + { + // cmd = 2 bytes, size= 2||3bytes + return 2+(isLargePacket()?3:2); + } + + bool isLargePacket() + { + return size > 0x7FFF; + } + + const uint32 size; + uint8 *header; }; struct ClientPktHeader @@ -961,23 +1001,17 @@ int WorldSocket::HandlePing (WorldPacket& recvPacket) int WorldSocket::iSendPacket (const WorldPacket& pct) { - if (m_OutBuffer->space () < pct.size () + sizeof (ServerPktHeader)) + ServerPktHeader header(pct.size()+2, pct.GetOpcode()); + if (m_OutBuffer->space () < pct.size () + header.getHeaderLength()) { errno = ENOBUFS; return -1; } - ServerPktHeader header; - header.cmd = pct.GetOpcode (); - EndianConvert(header.cmd); + m_Crypt.EncryptSend ( header.header, header.getHeaderLength()); - header.size = (uint16) pct.size () + 2; - EndianConvertReverse(header.size); - - m_Crypt.EncryptSend ((uint8*) & header, sizeof (header)); - - if (m_OutBuffer->copy ((char*) & header, sizeof (header)) == -1) + if (m_OutBuffer->copy ((char*) header.header, header.getHeaderLength()) == -1) ACE_ASSERT (false); if (!pct.empty ()) diff --git a/src/shared/Auth/AuthCrypt.cpp b/src/shared/Auth/AuthCrypt.cpp index c6aae7026..27dce1d6d 100644 --- a/src/shared/Auth/AuthCrypt.cpp +++ b/src/shared/Auth/AuthCrypt.cpp @@ -48,9 +48,8 @@ void AuthCrypt::DecryptRecv(uint8 *data, size_t len) void AuthCrypt::EncryptSend(uint8 *data, size_t len) { if (!_initialized) return; - if (len < CRYPTED_SEND_LEN) return; - for (size_t t = 0; t < CRYPTED_SEND_LEN; t++) + for (size_t t = 0; t < len; t++) { _send_i %= _key.size(); uint8 x = (data[t] ^ _key[_send_i]) + _send_j; diff --git a/src/shared/Auth/AuthCrypt.h b/src/shared/Auth/AuthCrypt.h index 60f91b052..a14e4d900 100644 --- a/src/shared/Auth/AuthCrypt.h +++ b/src/shared/Auth/AuthCrypt.h @@ -30,7 +30,6 @@ class AuthCrypt AuthCrypt(); ~AuthCrypt(); - const static size_t CRYPTED_SEND_LEN = 4; const static size_t CRYPTED_RECV_LEN = 6; void Init();