2011-07-28 01:01:33 +02:00
|
|
|
#include "protocol.h"
|
|
|
|
#include "connection.h"
|
2011-07-13 23:12:36 +02:00
|
|
|
|
2011-04-20 08:40:31 +02:00
|
|
|
Protocol::Protocol() :
|
|
|
|
m_connection(new Connection)
|
|
|
|
{
|
2011-07-27 01:13:27 +02:00
|
|
|
m_connection->setErrorCallback(std::bind(&Protocol::onError, this, std::placeholders::_1));
|
2011-05-30 05:11:12 +02:00
|
|
|
m_xteaEncryptionEnabled = false;
|
2011-04-20 08:40:31 +02:00
|
|
|
}
|
|
|
|
|
2011-07-27 20:10:49 +02:00
|
|
|
void Protocol::connect(const std::string& host, uint16 port)
|
2011-04-20 08:40:31 +02:00
|
|
|
{
|
2011-07-27 20:10:49 +02:00
|
|
|
m_connection->connect(host, port, std::bind(&Protocol::onConnect, asProtocol()));
|
2011-04-20 08:40:31 +02:00
|
|
|
}
|
|
|
|
|
2011-07-28 01:01:33 +02:00
|
|
|
void Protocol::send(OutputMessage* outputMessage)
|
2011-05-30 05:11:12 +02:00
|
|
|
{
|
|
|
|
// Encrypt
|
|
|
|
if(m_xteaEncryptionEnabled)
|
|
|
|
xteaEncrypt(outputMessage);
|
|
|
|
|
|
|
|
// Set checksum
|
|
|
|
uint32 checksum = getAdlerChecksum(outputMessage->getBuffer() + OutputMessage::DATA_POS, outputMessage->getMessageSize());
|
|
|
|
outputMessage->setWritePos(OutputMessage::CHECKSUM_POS);
|
|
|
|
outputMessage->addU32(checksum);
|
|
|
|
|
|
|
|
// Set size
|
|
|
|
uint16 messageSize = outputMessage->getMessageSize();
|
|
|
|
outputMessage->setWritePos(OutputMessage::HEADER_POS);
|
|
|
|
outputMessage->addU16(messageSize);
|
|
|
|
|
|
|
|
// Send
|
2011-07-27 20:10:49 +02:00
|
|
|
m_connection->send(outputMessage->getBuffer(), outputMessage->getMessageSize());
|
2011-05-30 05:11:12 +02:00
|
|
|
}
|
|
|
|
|
2011-07-27 20:10:49 +02:00
|
|
|
void Protocol::recv()
|
2011-05-30 05:11:12 +02:00
|
|
|
{
|
2011-07-27 20:10:49 +02:00
|
|
|
m_inputMessage.reset();
|
|
|
|
|
|
|
|
m_connection->recv(InputMessage::HEADER_LENGTH, 2, std::bind(&Protocol::internalRecvHeader, asProtocol(), std::placeholders::_1, std::placeholders::_2));
|
|
|
|
}
|
|
|
|
|
2011-07-28 01:01:33 +02:00
|
|
|
void Protocol::internalRecvHeader(uint8* buffer, uint16 size)
|
2011-07-27 20:10:49 +02:00
|
|
|
{
|
|
|
|
memcpy(m_inputMessage.getBuffer() + InputMessage::HEADER_POS, buffer, size);
|
|
|
|
|
|
|
|
uint16 dataSize = m_inputMessage.getU16();
|
|
|
|
m_inputMessage.setMessageSize(dataSize);
|
|
|
|
|
|
|
|
m_connection->recv(dataSize, 5, std::bind(&Protocol::internalRecvData, asProtocol(), std::placeholders::_1, std::placeholders::_2));
|
|
|
|
}
|
|
|
|
|
2011-07-28 01:01:33 +02:00
|
|
|
void Protocol::internalRecvData(uint8* buffer, uint16 size)
|
2011-07-27 20:10:49 +02:00
|
|
|
{
|
|
|
|
memcpy(m_inputMessage.getBuffer() + InputMessage::CHECKSUM_POS, buffer, size);
|
|
|
|
|
|
|
|
uint32 checksum = getAdlerChecksum(m_inputMessage.getBuffer() + InputMessage::DATA_POS, m_inputMessage.getMessageSize() - InputMessage::CHECKSUM_LENGTH);
|
|
|
|
if(m_inputMessage.getU32() != checksum) {
|
2011-05-30 05:11:12 +02:00
|
|
|
// error
|
2011-07-17 13:52:20 +02:00
|
|
|
logError("Checksum is invalid.");
|
2011-05-30 05:11:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(m_xteaEncryptionEnabled)
|
2011-07-27 20:10:49 +02:00
|
|
|
xteaDecrypt(&m_inputMessage);
|
|
|
|
|
|
|
|
onRecv(&m_inputMessage);
|
2011-05-30 05:11:12 +02:00
|
|
|
}
|
|
|
|
|
2011-07-13 23:12:36 +02:00
|
|
|
void Protocol::onError(const boost::system::error_code& err)
|
2011-04-20 08:40:31 +02:00
|
|
|
{
|
2011-07-27 20:10:49 +02:00
|
|
|
std::stringstream message;
|
|
|
|
|
2011-07-17 13:52:20 +02:00
|
|
|
logError("PROTOCOL ERROR: ", err.message());
|
2011-07-27 20:10:49 +02:00
|
|
|
message << "Boost error: " << err.message();
|
2011-04-20 08:40:31 +02:00
|
|
|
|
|
|
|
// invalid hostname
|
|
|
|
// connection timeouted
|
|
|
|
|
|
|
|
// displays a dialog, finish protocol
|
2011-07-27 20:10:49 +02:00
|
|
|
|
|
|
|
callField("onError", message.str());
|
2011-04-20 08:40:31 +02:00
|
|
|
}
|
2011-05-30 05:11:12 +02:00
|
|
|
|
2011-07-28 01:01:33 +02:00
|
|
|
bool Protocol::xteaDecrypt(InputMessage* inputMessage)
|
2011-05-30 05:11:12 +02:00
|
|
|
{
|
|
|
|
// FIXME: this function has not been tested yet
|
|
|
|
uint16 messageSize = inputMessage->getMessageSize() - InputMessage::CHECKSUM_LENGTH;
|
|
|
|
if(messageSize % 8 != 0) {
|
|
|
|
//LOG_TRACE_DEBUG("not valid encrypted message size")
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 *buffer = (uint32*)(inputMessage->getBuffer() + InputMessage::DATA_POS);
|
|
|
|
int readPos = 0;
|
|
|
|
|
|
|
|
while(readPos < messageSize/4) {
|
|
|
|
uint32 v0 = buffer[readPos], v1 = buffer[readPos + 1];
|
|
|
|
uint32 delta = 0x61C88647;
|
|
|
|
uint32 sum = 0xC6EF3720;
|
|
|
|
|
|
|
|
for(int32 i = 0; i < 32; i++) {
|
|
|
|
v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + m_xteaKey[sum>>11 & 3]);
|
|
|
|
sum += delta;
|
|
|
|
v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + m_xteaKey[sum & 3]);
|
|
|
|
}
|
|
|
|
buffer[readPos] = v0; buffer[readPos + 1] = v1;
|
|
|
|
readPos = readPos + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tmp = inputMessage->getU16();
|
|
|
|
if(tmp > inputMessage->getMessageSize() - 4) {
|
|
|
|
//LOG_TRACE_DEBUG("not valid unencrypted message size")
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inputMessage->setMessageSize(tmp + InputMessage::UNENCRYPTED_DATA_POS);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-28 01:01:33 +02:00
|
|
|
void Protocol::xteaEncrypt(OutputMessage* outputMessage)
|
2011-05-30 05:11:12 +02:00
|
|
|
{
|
|
|
|
uint16 messageLength = outputMessage->getMessageSize();
|
|
|
|
|
|
|
|
//add bytes until reach 8 multiple
|
|
|
|
if((messageLength % 8) != 0) {
|
|
|
|
uint16 n = 8 - (messageLength % 8);
|
|
|
|
outputMessage->addPaddingBytes(n);
|
|
|
|
messageLength += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int readPos = 0;
|
|
|
|
uint32 *buffer = (uint32*)outputMessage->getBuffer() + OutputMessage::DATA_POS;
|
|
|
|
while(readPos < messageLength / 4) {
|
|
|
|
uint32 v0 = buffer[readPos], v1 = buffer[readPos + 1];
|
|
|
|
uint32 delta = 0x61C88647;
|
|
|
|
uint32 sum = 0;
|
|
|
|
|
|
|
|
for(int32 i = 0; i < 32; i++) {
|
|
|
|
v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + m_xteaKey[sum & 3]);
|
|
|
|
sum -= delta;
|
|
|
|
v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + m_xteaKey[sum>>11 & 3]);
|
|
|
|
}
|
|
|
|
buffer[readPos] = v0; buffer[readPos + 1] = v1;
|
|
|
|
readPos = readPos + 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-28 01:01:33 +02:00
|
|
|
uint32 Protocol::getAdlerChecksum(uint8* buffer, uint16 size)
|
2011-05-30 05:11:12 +02:00
|
|
|
{
|
|
|
|
uint32 a = 1, b = 0;
|
|
|
|
while (size > 0) {
|
|
|
|
size_t tlen = size > 5552 ? 5552 : size;
|
|
|
|
size -= tlen;
|
|
|
|
do {
|
|
|
|
a += *buffer++;
|
|
|
|
b += a;
|
|
|
|
} while (--tlen);
|
|
|
|
|
|
|
|
a %= 65521;
|
|
|
|
b %= 65521;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (b << 16) | a;
|
|
|
|
}
|