30 m_xteaEncryptionEnabled =
false;
31 m_checksumEnabled =
false;
53 m_connection->
close();
75 if(m_xteaEncryptionEnabled)
76 xteaEncrypt(outputMessage);
80 outputMessage->writeChecksum();
83 outputMessage->writeMessageSize();
87 m_connection->
write(outputMessage->getHeaderBuffer(), outputMessage->getMessageSize());
90 outputMessage->
reset();
95 m_inputMessage->
reset();
101 if(m_xteaEncryptionEnabled)
107 m_connection->
read(2, std::bind(&Protocol::internalRecvHeader,
asProtocol(), std::placeholders::_1, std::placeholders::_2));
110 void Protocol::internalRecvHeader(
uint8* buffer,
uint16 size)
118 m_connection->
read(remainingSize, std::bind(&Protocol::internalRecvData,
asProtocol(), std::placeholders::_1, std::placeholders::_2));
121 void Protocol::internalRecvData(
uint8* buffer,
uint16 size)
125 g_logger.traceError(
"received data while disconnected");
131 if(m_checksumEnabled && !m_inputMessage->
readChecksum()) {
132 g_logger.traceError(
"got a network message with invalid checksum");
136 if(m_xteaEncryptionEnabled) {
137 if(!xteaDecrypt(m_inputMessage)) {
138 g_logger.traceError(
"failed to decrypt message");
148 std::uniform_int_distribution<uint32> unif(0, 0xFFFFFFFF);
165 std::vector<uint32> xteaKey;
167 for(
int i = 0; i < 4; ++i)
175 if(encryptedSize % 8 != 0) {
176 g_logger.traceError(
"invalid encrypted network message");
183 while(readPos < encryptedSize/4) {
184 uint32 v0 = buffer[readPos], v1 = buffer[readPos + 1];
185 uint32 delta = 0x61C88647;
188 for(
int32 i = 0; i < 32; i++) {
189 v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum +
m_xteaKey[sum>>11 & 3]);
191 v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum +
m_xteaKey[sum & 3]);
193 buffer[readPos] = v0; buffer[readPos + 1] = v1;
194 readPos = readPos + 2;
198 int sizeDelta = decryptedSize - encryptedSize;
199 if(sizeDelta > 0 || -sizeDelta > encryptedSize) {
200 g_logger.traceError(
"invalid decrypted network message");
210 outputMessage->writeMessageSize();
211 uint16 encryptedSize = outputMessage->getMessageSize();
214 if((encryptedSize % 8) != 0) {
215 uint16 n = 8 - (encryptedSize % 8);
216 outputMessage->addPaddingBytes(n);
221 uint32 *buffer = (
uint32*)(outputMessage->getDataBuffer() - 2);
222 while(readPos < encryptedSize / 4) {
223 uint32 v0 = buffer[readPos], v1 = buffer[readPos + 1];
224 uint32 delta = 0x61C88647;
227 for(
int32 i = 0; i < 32; i++) {
228 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum +
m_xteaKey[sum & 3]);
230 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum +
m_xteaKey[sum>>11 & 3]);
232 buffer[readPos] = v0; buffer[readPos + 1] = v1;
233 readPos = readPos + 2;