many changes and refactoring

This commit is contained in:
Eduardo Bart
2011-07-13 18:12:36 -03:00
parent 6c05ee0e82
commit 8ef1b28546
120 changed files with 1545 additions and 1273 deletions

View File

@@ -40,12 +40,12 @@ void Connection::poll()
ioService.reset();
}
void Connection::connect(const std::string& host, uint16 port, const SimpleCallback& connectCallback)
void Connection::connect(const std::string& host, uint16 port, const boost::function<void()>& connectCallback)
{
m_connectCallback = connectCallback;
m_connectionState = CONNECTION_STATE_RESOLVING;
boost::asio::ip::tcp::resolver::query query(host, convert_cast<std::string>(port));
boost::asio::ip::tcp::resolver::query query(host, convert<std::string>(port));
m_resolver.async_resolve(query, boost::bind(&Connection::onResolve, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::iterator));
m_timer.expires_from_now(boost::posix_time::seconds(2));
@@ -70,7 +70,7 @@ void Connection::onTimeout(const boost::system::error_code& error)
void Connection::onResolve(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIterator)
{
logTrace();
trace();
m_timer.cancel();
@@ -88,7 +88,7 @@ void Connection::onResolve(const boost::system::error_code& error, boost::asio::
void Connection::onConnect(const boost::system::error_code& error)
{
logTrace();
trace();
m_timer.cancel();
@@ -108,7 +108,7 @@ void Connection::onConnect(const boost::system::error_code& error)
void Connection::onSend(const boost::system::error_code& error, size_t)
{
logTrace();
trace();
m_timer.cancel();
@@ -121,7 +121,7 @@ void Connection::onSend(const boost::system::error_code& error, size_t)
void Connection::onRecvHeader(const boost::system::error_code& error)
{
logTrace();
trace();
if(error) {
if(m_errorCallback)
@@ -139,7 +139,7 @@ void Connection::onRecvHeader(const boost::system::error_code& error)
void Connection::onRecvData(const boost::system::error_code& error)
{
logTrace();
trace();
if(error) {
if(m_errorCallback)

View File

@@ -25,10 +25,13 @@
#ifndef CONNECTION_H
#define CONNECTION_H
#include <global.h>
#include <net/inputmessage.h>
#include <net/outputmessage.h>
#include <prerequisites.h>
#include <boost/asio.hpp>
#include <boost/function.hpp>
typedef boost::function<void(boost::system::error_code&)> ErrorCallback;
typedef boost::function<void(InputMessage*)> RecvCallback;
@@ -40,7 +43,7 @@ public:
static void poll();
void connect(const std::string& host, uint16 port, const SimpleCallback& connectCallback);
void connect(const std::string& host, uint16 port, const boost::function<void()>& connectCallback);
void send(OutputMessage *outputMessage);
void setErrorCallback(const ErrorCallback& errorCallback) { m_errorCallback = errorCallback; }
@@ -63,7 +66,7 @@ public:
private:
ErrorCallback m_errorCallback;
RecvCallback m_recvCallback;
SimpleCallback m_connectCallback;
boost::function<void()> m_connectCallback;
ConnectionState_t m_connectionState;
boost::asio::deadline_timer m_timer;

View File

@@ -87,6 +87,6 @@ std::string InputMessage::getString()
bool InputMessage::canRead(int bytes)
{
if((m_readPos + bytes > m_messageSize) || (m_readPos + bytes > BUFFER_MAXSIZE))
logFatal("[InputMessage::canRead()]: Cant read. Message is finished or read position has reached buffer's maximum size.");
fatal("[InputMessage::canRead()]: Cant read. Message is finished or read position has reached buffer's maximum size.");
return true;
}

View File

@@ -25,7 +25,7 @@
#ifndef INPUTMESSAGE_H
#define INPUTMESSAGE_H
#include <prerequisites.h>
#include <global.h>
class InputMessage
{

View File

@@ -105,6 +105,6 @@ void OutputMessage::addPaddingBytes(int bytes, uint8 byte)
bool OutputMessage::canWrite(int bytes)
{
if(m_writePos + bytes > BUFFER_MAXSIZE)
logFatal("[OutputMessage::canWrite()]: Can't write. Write position has reached buffer's maxium size.");
fatal("[OutputMessage::canWrite()]: Can't write. Write position has reached buffer's maxium size.");
return true;
}

View File

@@ -25,7 +25,7 @@
#ifndef OUTPUTMESSAGE_H
#define OUTPUTMESSAGE_H
#include <prerequisites.h>
#include <global.h>
class OutputMessage
{

View File

@@ -24,6 +24,8 @@
#include <net/protocol.h>
#include <boost/bind.hpp>
Protocol::Protocol() :
m_connection(new Connection)
{
@@ -32,7 +34,7 @@ Protocol::Protocol() :
m_xteaEncryptionEnabled = false;
}
void Protocol::connect(const std::string& host, uint16 port, const SimpleCallback& callback)
void Protocol::connect(const std::string& host, uint16 port, const boost::function<void()>& callback)
{
m_connection->connect(host, port, callback);
}
@@ -62,7 +64,7 @@ void Protocol::onRecv(InputMessage *inputMessage)
uint32 checksum = getAdlerChecksum(inputMessage->getBuffer() + InputMessage::DATA_POS, inputMessage->getMessageSize() - InputMessage::CHECKSUM_LENGTH);
if(inputMessage->getU32() != checksum) {
// error
logError("Checksum is invalid.");
error("Checksum is invalid.");
return;
}
@@ -70,9 +72,9 @@ void Protocol::onRecv(InputMessage *inputMessage)
xteaDecrypt(inputMessage);
}
void Protocol::onError(const boost::system::error_code& error)
void Protocol::onError(const boost::system::error_code& err)
{
flogError("PROTOCOL ERROR: %s", error.message());
error("PROTOCOL ERROR: ", err.message());
// invalid hostname
// connection timeouted

View File

@@ -43,11 +43,11 @@ class Protocol : public Scriptable
public:
Protocol();
void connect(const std::string& host, uint16 port, const SimpleCallback& callback);
void connect(const std::string& host, uint16 port, const boost::function<void()>& callback);
void send(OutputMessage *outputMessage);
virtual void onRecv(InputMessage *inputMessage);
virtual void onError(const boost::system::error_code& error);
virtual void onError(const boost::system::error_code& err);
virtual const char *getScriptableName() const { return "Protocol"; }

156
src/framework/net/rsa.cpp Normal file
View File

@@ -0,0 +1,156 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* 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.
*/
#include <global.h>
#include "rsa.h"
Rsa::Rsa()
{
m_keySet = false;
mpz_init2(m_p, 1024);
mpz_init2(m_q, 1024);
mpz_init2(m_d, 1024);
mpz_init2(m_u, 1024);
mpz_init2(m_dp, 1024);
mpz_init2(m_dq, 1024);
mpz_init2(m_mod, 1024);
}
Rsa::~Rsa()
{
mpz_clear(m_p);
mpz_clear(m_q);
mpz_clear(m_d);
mpz_clear(m_u);
mpz_clear(m_dp);
mpz_clear(m_dq);
mpz_clear(m_mod);
}
bool Rsa::setKey(const std::string& file)
{
//loads p,q and d from a file
FILE* f = fopen(file.c_str(), "r");
if(!f){
return false;
}
char p[512];
char q[512];
char d[512];
delete fgets(p, 512, f);
delete fgets(q, 512, f);
delete fgets(d, 512, f);
setKey(p, q, d);
return true;
}
void Rsa::setKey(const char* p, const char* q, const char* d)
{
mpz_set_str(m_p, p, 10);
mpz_set_str(m_q, q, 10);
mpz_set_str(m_d, d, 10);
mpz_t pm1,qm1;
mpz_init2(pm1,520);
mpz_init2(qm1,520);
mpz_sub_ui(pm1, m_p, 1);
mpz_sub_ui(qm1, m_q, 1);
mpz_invert(m_u, m_p, m_q);
mpz_mod(m_dp, m_d, pm1);
mpz_mod(m_dq, m_d, qm1);
mpz_mul(m_mod, m_p, m_q);
mpz_clear(pm1);
mpz_clear(qm1);
}
bool Rsa::encrypt(char* msg, int32_t size, const char* key)
{
mpz_t plain, c;
mpz_init2(plain, 1024);
mpz_init2(c, 1024);
mpz_t e;
mpz_init(e);
mpz_set_ui(e,65537);
mpz_t mod;
mpz_init2(mod, 1024);
mpz_set_str(mod, key, 10);
mpz_import(plain, 128, 1, 1, 0, 0, msg);
mpz_powm(c, plain, e, mod);
size_t count = (mpz_sizeinbase(c, 2) + 7)/8;
memset(msg, 0, 128 - count);
mpz_export(&msg[128 - count], NULL, 1, 1, 0, 0, c);
mpz_clear(c);
mpz_clear(plain);
mpz_clear(e);
mpz_clear(mod);
return true;
}
bool Rsa::decrypt(char* msg, int32_t size)
{
mpz_t c,v1,v2,u2,tmp;
mpz_init2(c, 1024);
mpz_init2(v1, 1024);
mpz_init2(v2, 1024);
mpz_init2(u2, 1024);
mpz_init2(tmp, 1024);
mpz_import(c, 128, 1, 1, 0, 0, msg);
mpz_mod(tmp, c, m_p);
mpz_powm(v1, tmp, m_dp, m_p);
mpz_mod(tmp, c, m_q);
mpz_powm(v2, tmp, m_dq, m_q);
mpz_sub(u2, v2, v1);
mpz_mul(tmp, u2, m_u);
mpz_mod(u2, tmp, m_q);
if(mpz_cmp_si(u2, 0) < 0){
mpz_add(tmp, u2, m_q);
mpz_set(u2, tmp);
}
mpz_mul(tmp, u2, m_p);
mpz_set_ui(c, 0);
mpz_add(c, v1, tmp);
size_t count = (mpz_sizeinbase(c, 2) + 7)/8;
memset(msg, 0, 128 - count);
mpz_export(&msg[128 - count], NULL, 1, 1, 0, 0, c);
mpz_clear(c);
mpz_clear(v1);
mpz_clear(v2);
mpz_clear(u2);
mpz_clear(tmp);
return true;
}

49
src/framework/net/rsa.h Normal file
View File

@@ -0,0 +1,49 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* 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.
*/
#ifndef RSA_H
#define RSA_H
#include <global.h>
#include <gmp.h>
class Rsa
{
public:
Rsa();
~Rsa();
void setKey(const char* p, const char* q, const char* d);
bool setKey(const std::string& file);
bool decrypt(char* msg, int32_t size);
static bool encrypt(char* msg, int32_t size, const char* key);
protected:
bool m_keySet;
mpz_t m_p, m_q, m_u, m_d, m_dp, m_dq, m_mod;
};
typedef boost::shared_ptr<Rsa> RsaPtr;
#endif //RSA_H