init protocolgame

This commit is contained in:
Henrique
2011-08-01 01:28:41 -03:00
parent 586bcde915
commit a38bd18280
15 changed files with 1283 additions and 85 deletions

View File

@@ -36,7 +36,7 @@ private:
asio::ip::tcp::resolver m_resolver;
asio::ip::tcp::socket m_socket;
uint8 m_recvBuffer[65536];
uint8 m_recvBuffer[65538];
uint16 m_recvSize;
RecvCallback m_recvCallback;
};

View File

@@ -11,33 +11,47 @@ void InputMessage::reset()
m_messageSize = 2;
}
uint8 InputMessage::getU8()
uint8 InputMessage::getU8(bool blockReadPos)
{
assert(canRead(1));
return m_buffer[m_readPos++];
uint8 v = m_buffer[m_readPos];
if(!blockReadPos)
m_readPos += 1;
return v;
}
uint16 InputMessage::getU16()
uint16 InputMessage::getU16(bool blockReadPos)
{
assert(canRead(2));
uint16 v = *(uint16_t*)(m_buffer + m_readPos);
m_readPos += 2;
if(!blockReadPos)
m_readPos += 2;
return v;
}
uint32 InputMessage::getU32()
uint32 InputMessage::getU32(bool blockReadPos)
{
assert(canRead(4));
uint32 v = *(uint32*)(m_buffer + m_readPos);
m_readPos += 4;
if(!blockReadPos)
m_readPos += 4;
return v;
}
uint64 InputMessage::getU64()
uint64 InputMessage::getU64(bool blockReadPos)
{
assert(canRead(8));
uint64 v = *(uint64*)(m_buffer + m_readPos);
m_readPos += 8;
if(!blockReadPos)
m_readPos += 8;
return v;
}

View File

@@ -3,6 +3,9 @@
#include "netdeclarations.h"
// TODO remove this
#include <../position.h>
class InputMessage
{
public:
@@ -20,12 +23,13 @@ public:
void reset();
uint8 getU8();
uint16 getU16();
uint32 getU32();
uint64 getU64();
uint8 getU8(bool blockReadPos = false);
uint16 getU16(bool blockReadPos = false);
uint32 getU32(bool blockReadPos = false);
uint64 getU64(bool blockReadPos = false);
std::string getString();
void skipBytes(uint16 bytes) { m_readPos += bytes; }
uint8* getBuffer() { return m_buffer; }
uint16 getMessageSize() { return m_messageSize; }
void setMessageSize(uint16 messageSize) { m_messageSize = messageSize; }

View File

@@ -20,12 +20,12 @@ typedef std::shared_ptr<Protocol> ProtocolPtr;
"2907336840325241747827401343576296990629870233111328210165697754" \
"88792221429527047321331896351555606801473202394175817"
/*
#define RSA "1091201329673994292788609605089955415282375029027981291234687579" \
#define OTSERV_PUBLIC_RSA "1091201329673994292788609605089955415282375029027981291234687579" \
"3726629149257644633073969600111060390723088861007265581882535850" \
"3429057592827629436413108566029093628212635953836686562675849720" \
"6207862794310902180176810615217550567108238764764442605581471797" \
"07119674283982419152118103759076030616683978566631413"
*/
#endif

View File

@@ -6,6 +6,7 @@ Protocol::Protocol() :
{
m_connection->setErrorCallback(std::bind(&Protocol::onError, this, std::placeholders::_1));
m_xteaEncryptionEnabled = false;
m_checksumEnabled = true;
}
void Protocol::connect(const std::string& host, uint16 port)
@@ -13,24 +14,24 @@ void Protocol::connect(const std::string& host, uint16 port)
m_connection->connect(host, port, std::bind(&Protocol::onConnect, asProtocol()));
}
void Protocol::send(OutputMessage* outputMessage)
void Protocol::send(OutputMessage& outputMessage)
{
// 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);
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);
uint16 messageSize = outputMessage.getMessageSize();
outputMessage.setWritePos(OutputMessage::HEADER_POS);
outputMessage.addU16(messageSize);
// Send
m_connection->send(outputMessage->getBuffer(), outputMessage->getMessageSize());
m_connection->send(outputMessage.getBuffer(), outputMessage.getMessageSize());
}
void Protocol::recv()
@@ -54,17 +55,19 @@ void Protocol::internalRecvData(uint8* buffer, uint16 size)
{
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) {
// error
logError("Checksum is invalid.");
return;
if(m_checksumEnabled) {
uint32 checksum = getAdlerChecksum(m_inputMessage.getBuffer() + InputMessage::DATA_POS, m_inputMessage.getMessageSize() - InputMessage::CHECKSUM_LENGTH);
if(m_inputMessage.getU32() != checksum) {
// error
logError("Checksum is invalid.");
return;
}
}
if(m_xteaEncryptionEnabled)
xteaDecrypt(&m_inputMessage);
xteaDecrypt(m_inputMessage);
onRecv(&m_inputMessage);
onRecv(m_inputMessage);
}
void Protocol::onError(const boost::system::error_code& err)
@@ -82,16 +85,16 @@ void Protocol::onError(const boost::system::error_code& err)
callField("onError", message.str());
}
bool Protocol::xteaDecrypt(InputMessage* inputMessage)
bool Protocol::xteaDecrypt(InputMessage& inputMessage)
{
// FIXME: this function has not been tested yet
uint16 messageSize = inputMessage->getMessageSize() - InputMessage::CHECKSUM_LENGTH;
uint16 messageSize = inputMessage.getMessageSize() - InputMessage::CHECKSUM_LENGTH;
if(messageSize % 8 != 0) {
//LOG_TRACE_DEBUG("not valid encrypted message size")
logDebug("not valid encrypted message size");
return false;
}
uint32 *buffer = (uint32*)(inputMessage->getBuffer() + InputMessage::DATA_POS);
uint32 *buffer = (uint32*)(inputMessage.getBuffer() + InputMessage::DATA_POS);
int readPos = 0;
while(readPos < messageSize/4) {
@@ -108,29 +111,29 @@ bool Protocol::xteaDecrypt(InputMessage* inputMessage)
readPos = readPos + 2;
}
int tmp = inputMessage->getU16();
if(tmp > inputMessage->getMessageSize() - 4) {
//LOG_TRACE_DEBUG("not valid unencrypted message size")
int tmp = inputMessage.getU16();
if(tmp > inputMessage.getMessageSize() - 4) {
logDebug("not valid unencrypted message size");
return false;
}
inputMessage->setMessageSize(tmp + InputMessage::UNENCRYPTED_DATA_POS);
inputMessage.setMessageSize(tmp + InputMessage::UNENCRYPTED_DATA_POS);
return true;
}
void Protocol::xteaEncrypt(OutputMessage* outputMessage)
void Protocol::xteaEncrypt(OutputMessage& outputMessage)
{
uint16 messageLength = outputMessage->getMessageSize();
uint16 messageLength = outputMessage.getMessageSize();
//add bytes until reach 8 multiple
if((messageLength % 8) != 0) {
uint16 n = 8 - (messageLength % 8);
outputMessage->addPaddingBytes(n);
outputMessage.addPaddingBytes(n);
messageLength += n;
}
int readPos = 0;
uint32 *buffer = (uint32*)outputMessage->getBuffer() + OutputMessage::DATA_POS;
uint32 *buffer = (uint32*)outputMessage.getBuffer() + OutputMessage::DATA_POS;
while(readPos < messageLength / 4) {
uint32 v0 = buffer[readPos], v1 = buffer[readPos + 1];
uint32 delta = 0x61C88647;

View File

@@ -13,13 +13,13 @@ public:
Protocol();
void connect(const std::string& host, uint16 port);
void send(OutputMessage* outputMessage);
void send(OutputMessage& outputMessage);
void recv();
void internalRecvHeader(uint8* buffer, uint16 size);
void internalRecvData(uint8* buffer, uint16 size);
virtual void onConnect() = 0;
virtual void onRecv(InputMessage* inputMessage) = 0;
virtual void onRecv(InputMessage& inputMessage) = 0;
virtual void onError(const boost::system::error_code& err);
ProtocolPtr asProtocol() { return std::static_pointer_cast<Protocol>(shared_from_this()); }
@@ -28,12 +28,12 @@ public:
protected:
uint32 m_xteaKey[4];
bool m_xteaEncryptionEnabled;
bool m_checksumEnabled, m_xteaEncryptionEnabled;
InputMessage m_inputMessage;
private:
bool xteaDecrypt(InputMessage* inputMessage);
void xteaEncrypt(OutputMessage* outputMessage);
bool xteaDecrypt(InputMessage& inputMessage);
void xteaEncrypt(OutputMessage& outputMessage);
uint32 getAdlerChecksum(uint8* buffer, uint16 size);
ConnectionPtr m_connection;