tibia-client/src/framework/net/protocol.cpp

251 lines
7.2 KiB
C++
Raw Normal View History

2011-08-28 15:17:58 +02:00
/*
2012-01-02 17:58:37 +01:00
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
2011-08-28 15:17:58 +02:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2011-07-28 01:01:33 +02:00
#include "protocol.h"
#include "connection.h"
#include <framework/core/application.h>
2011-07-13 23:12:36 +02:00
2011-08-16 05:27:46 +02:00
Protocol::Protocol()
2011-04-20 08:40:31 +02:00
{
2011-05-30 05:11:12 +02:00
m_xteaEncryptionEnabled = false;
2011-08-16 05:27:46 +02:00
m_checksumEnabled = false;
m_inputMessage = InputMessagePtr(new InputMessage);
2011-04-20 08:40:31 +02:00
}
2012-01-13 16:41:57 +01:00
Protocol::~Protocol()
{
#ifndef NDEBUG
assert(!g_app.isTerminated());
#endif
2012-01-13 16:41:57 +01:00
disconnect();
}
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-08-16 05:27:46 +02:00
m_connection = ConnectionPtr(new Connection);
2012-04-14 02:14:25 +02:00
m_connection->setErrorCallback(std::bind(&Protocol::onError, asProtocol(), std::placeholders::_1));
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-08-16 02:30:31 +02:00
void Protocol::disconnect()
{
2011-08-21 23:49:31 +02:00
if(m_connection) {
m_connection->close();
m_connection.reset();
}
2011-08-16 02:30:31 +02:00
}
2011-08-29 20:38:01 +02:00
bool Protocol::isConnected()
{
if(m_connection && m_connection->isConnected())
return true;
return false;
}
bool Protocol::isConnecting()
{
if(m_connection && m_connection->isConnecting())
return true;
return false;
}
void Protocol::send(const OutputMessagePtr& outputMessage)
2011-05-30 05:11:12 +02:00
{
2011-08-16 02:30:31 +02:00
// encrypt
2011-05-30 05:11:12 +02:00
if(m_xteaEncryptionEnabled)
xteaEncrypt(outputMessage);
// write checksum
if(m_checksumEnabled)
outputMessage->writeChecksum();
2011-05-30 05:11:12 +02:00
// wirte message size
outputMessage->writeMessageSize();
2011-05-30 05:11:12 +02:00
2011-08-16 02:30:31 +02:00
// send
2011-08-29 04:08:02 +02:00
if(m_connection)
m_connection->write(outputMessage->getHeaderBuffer(), outputMessage->getMessageSize());
2012-06-22 18:02:16 +02:00
// reset message to allow reuse
outputMessage->reset();
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
{
m_inputMessage->reset();
2011-07-27 20:10:49 +02:00
// first update message header size
int headerSize = 2; // 2 bytes for message size
if(m_checksumEnabled)
headerSize += 4; // 4 bytes for checksum
if(m_xteaEncryptionEnabled)
headerSize += 2; // 2 bytes for XTEA encrypted message size
m_inputMessage->setHeaderSize(headerSize);
// read the first 2 bytes which contain the message size
2011-08-29 04:08:02 +02:00
if(m_connection)
m_connection->read(2, std::bind(&Protocol::internalRecvHeader, asProtocol(), std::placeholders::_1, std::placeholders::_2));
2011-07-27 20:10:49 +02:00
}
2011-07-28 01:01:33 +02:00
void Protocol::internalRecvHeader(uint8* buffer, uint16 size)
2011-07-27 20:10:49 +02:00
{
2011-08-16 02:30:31 +02:00
// read message size
m_inputMessage->fillBuffer(buffer, size);
uint16 remainingSize = m_inputMessage->readSize();
2011-07-27 20:10:49 +02:00
// read remaining message data
2011-08-29 04:08:02 +02:00
if(m_connection)
m_connection->read(remainingSize, std::bind(&Protocol::internalRecvData, asProtocol(), std::placeholders::_1, std::placeholders::_2));
2011-07-27 20:10:49 +02:00
}
2011-07-28 01:01:33 +02:00
void Protocol::internalRecvData(uint8* buffer, uint16 size)
2011-07-27 20:10:49 +02:00
{
2012-04-03 16:15:11 +02:00
// process data only if really connected
if(!isConnected()) {
2012-06-01 22:39:23 +02:00
g_logger.traceError("received data while disconnected");
2012-04-03 16:15:11 +02:00
return;
}
m_inputMessage->fillBuffer(buffer, size);
2011-07-27 20:10:49 +02:00
if(m_checksumEnabled && !m_inputMessage->readChecksum()) {
2012-06-01 22:39:23 +02:00
g_logger.traceError("got a network message with invalid checksum");
return;
2011-05-30 05:11:12 +02:00
}
if(m_xteaEncryptionEnabled) {
if(!xteaDecrypt(m_inputMessage)) {
2012-06-01 22:39:23 +02:00
g_logger.traceError("failed to decrypt message");
return;
}
}
2011-08-01 06:28:41 +02:00
onRecv(m_inputMessage);
2011-05-30 05:11:12 +02:00
}
2011-08-16 05:27:46 +02:00
void Protocol::generateXteaKey()
{
std::mt19937 eng(std::time(NULL));
std::uniform_int_distribution<uint32> unif(0, 0xFFFFFFFF);
m_xteaKey[0] = unif(eng);
m_xteaKey[1] = unif(eng);
m_xteaKey[2] = unif(eng);
m_xteaKey[3] = unif(eng);
}
2012-08-02 02:24:54 +02:00
void Protocol::setXteaKey(uint32 a, uint32 b, uint32 c, uint32 d)
{
m_xteaKey[0] = a;
m_xteaKey[1] = b;
m_xteaKey[2] = c;
m_xteaKey[3] = d;
}
2012-06-05 21:16:57 +02:00
std::vector<int> Protocol::getXteaKey()
{
std::vector<int> xteaKey;
xteaKey.resize(4);
for(int i = 0; i < 4; ++i)
xteaKey[i] = m_xteaKey[i];
return xteaKey;
}
bool Protocol::xteaDecrypt(const InputMessagePtr& inputMessage)
2011-05-30 05:11:12 +02:00
{
uint16 encryptedSize = inputMessage->getUnreadSize();
if(encryptedSize % 8 != 0) {
2012-06-01 22:39:23 +02:00
g_logger.traceError("invalid encrypted network message");
2011-05-30 05:11:12 +02:00
return false;
}
uint32 *buffer = (uint32*)(inputMessage->getReadBuffer());
2011-05-30 05:11:12 +02:00
int readPos = 0;
while(readPos < encryptedSize/4) {
2011-05-30 05:11:12 +02:00
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;
}
uint16 decryptedSize = inputMessage->getU16() + 2;
int sizeDelta = decryptedSize - encryptedSize;
if(sizeDelta > 0 || -sizeDelta > encryptedSize) {
g_logger.traceError("invalid decrypted network message");
2011-05-30 05:11:12 +02:00
return false;
}
inputMessage->setMessageSize(inputMessage->getMessageSize() + sizeDelta);
2011-05-30 05:11:12 +02:00
return true;
}
void Protocol::xteaEncrypt(const OutputMessagePtr& outputMessage)
2011-05-30 05:11:12 +02:00
{
outputMessage->writeMessageSize();
uint16 encryptedSize = outputMessage->getMessageSize();
2011-08-12 02:06:01 +02:00
2011-05-30 05:11:12 +02:00
//add bytes until reach 8 multiple
if((encryptedSize % 8) != 0) {
uint16 n = 8 - (encryptedSize % 8);
outputMessage->addPaddingBytes(n);
encryptedSize += n;
2011-05-30 05:11:12 +02:00
}
int readPos = 0;
uint32 *buffer = (uint32*)(outputMessage->getDataBuffer() - 2);
while(readPos < encryptedSize / 4) {
2011-05-30 05:11:12 +02:00
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;
}
}
2012-06-05 21:16:57 +02:00
void Protocol::onConnect()
{
callLuaField("onConnect");
}
2012-06-05 23:27:37 +02:00
void Protocol::onRecv(const InputMessagePtr& inputMessage)
{
callLuaField("onRecv", inputMessage);
}
void Protocol::onError(const boost::system::error_code& err)
{
callLuaField("onError", err.message(), err.value());
2012-06-05 23:27:37 +02:00
disconnect();
}