mxoemu/Proxy/RemoteSocket.cpp

121 lines
3.3 KiB
C++
Raw Normal View History

2009-08-11 12:17:38 +00:00
// *************************************************************************************************
// --------------------------------------
// Copyright (C) 2006-2010 Rajko Stojadinovic
//
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// *************************************************************************************************
#include "RemoteSocket.h"
#include "LocalSocket.h"
#include "Util.h"
#include <iostream>
2009-08-12 19:55:32 +00:00
#include "SequencedPacket.h"
2009-08-11 12:17:38 +00:00
#include "Logging.h"
#include "EncryptedPacket.h"
extern LocalSocket *worldLocal;
extern u_short GlobalPort;
extern std::string GlobalIP;
RemoteSocket *globalRemoteSocket = NULL;
extern LocalSocket *globalLocalSocket;
2009-08-12 19:55:32 +00:00
extern uint16 WTC_localSeq;
extern uint16 WTC_remoteSeq;
2009-08-11 12:17:38 +00:00
extern uint8 WTC_flags;
RemoteSocket::RemoteSocket(ISocketHandler& h)
:UdpSocket(h)
{
2009-08-12 19:55:32 +00:00
WTC_localSeq = 0;
WTC_remoteSeq = 0;
2009-08-11 12:17:38 +00:00
WTC_flags = 0;
InitializeCriticalSection(&CriticalSection);
}
RemoteSocket::~RemoteSocket()
{
2009-08-12 19:55:32 +00:00
WTC_localSeq = 0;
WTC_remoteSeq = 0;
2009-08-11 12:17:38 +00:00
WTC_flags = 0;
DeleteCriticalSection(&CriticalSection);
}
extern CryptoPP::CBC_Mode<CryptoPP::Twofish>::Decryption *TFDecryptWTC;
extern CryptoPP::CBC_Mode<CryptoPP::Twofish>::Encryption *TFEncryptWTC;
void PerformReplay( const char*p, size_t l, bool server)
{
if (server == false)
{
if (globalRemoteSocket == NULL)
return;
globalRemoteSocket->Replay(p,l);
}
else
{
if (globalLocalSocket == NULL)
return;
globalLocalSocket->Replay(p,l);
}
}
void RemoteSocket::Replay( const char *p,size_t l)
{
if (p==NULL || l < 1)
return;
if (TFEncryptWTC == NULL)
return;
EnterCriticalSection(&CriticalSection);
2009-08-12 19:55:32 +00:00
WTC_localSeq++;
2009-08-11 12:17:38 +00:00
2009-08-12 19:55:32 +00:00
if (WTC_localSeq >= 4096)
WTC_localSeq=0;
2009-08-11 12:17:38 +00:00
2009-08-12 19:55:32 +00:00
SequencedPacket replayMePacket(WTC_localSeq,WTC_remoteSeq,WTC_flags,string(p,l));
2009-08-11 12:17:38 +00:00
ByteBuffer replayMePlain = replayMePacket.getDataWithHeader();
EncryptedPacket replayMeCryptor;
replayMeCryptor.append((const byte*)replayMePlain.contents(),replayMePlain.size());
ByteBuffer SendMe = replayMeCryptor.toCipherText(TFEncryptWTC);
LeaveCriticalSection(&CriticalSection);
//output to OnRawData for retesting and decomposition
OnRawData(SendMe.contents(),SendMe.size(),NULL,97);
}
void RemoteSocket::OnRawData(const char *p,size_t l,struct sockaddr *sa_from,socklen_t sa_len)
{
if (TFDecryptWTC!=NULL && p[0]==0x01)
{
globalRemoteSocket = this;
}
EnterCriticalSection(&CriticalSection);
string outputMe = WorldToClient(p,l);
worldLocal->SendToBuf(GlobalIP,GlobalPort,outputMe.data(),outputMe.size());
LeaveCriticalSection(&CriticalSection);
}