Core/Authserver: Refactored handling battle.net client packets

This commit is contained in:
Shauren
2014-10-05 16:21:27 +02:00
parent 2494346288
commit dd26efa40b
10 changed files with 246 additions and 88 deletions

View File

@@ -0,0 +1,75 @@
/*
* Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 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 General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BattlenetPacketFactory_h__
#define BattlenetPacketFactory_h__
#include "BattlenetPackets.h"
#include <map>
namespace Battlenet
{
class PacketFactory
{
typedef ClientPacket*(*PacketCreateFn)(PacketHeader const& header, BitStream& stream);
public:
ClientPacket* Create(PacketHeader const& header, BitStream& stream)
{
auto creator = _creators.find(header);
if (creator == _creators.end())
return nullptr;
ClientPacket* packet = creator->second(header, stream);
packet->Read();
return packet;
}
static PacketFactory& Instance()
{
static PacketFactory instance;
return instance;
}
private:
PacketFactory()
{
_creators[PacketHeader(Authentication::CMSG_LOGON_REQUEST, AUTHENTICATION)] = &New<Authentication::LogonRequest>;
_creators[PacketHeader(Authentication::CMSG_RESUME_REQUEST, AUTHENTICATION)] = &New<Authentication::ResumeRequest>;
_creators[PacketHeader(Authentication::CMSG_PROOF_RESPONSE, AUTHENTICATION)] = &New<Authentication::ProofResponse>;
_creators[PacketHeader(Connection::CMSG_PING, CONNECTION)] = &New<Connection::Ping>;
_creators[PacketHeader(Connection::CMSG_ENABLE_ENCRYPTION, CONNECTION)] = &New<Connection::EnableEncryption>;
_creators[PacketHeader(Connection::CMSG_LOGOUT_REQUEST, CONNECTION)] = &New<Connection::LogoutRequest>;
_creators[PacketHeader(WoWRealm::CMSG_LIST_SUBSCRIBE_REQUEST, WOWREALM)] = &New<WoWRealm::ListSubscribeRequest>;
_creators[PacketHeader(WoWRealm::CMSG_JOIN_REQUEST_V2, WOWREALM)] = &New<WoWRealm::JoinRequestV2>;
}
template<class PacketType>
static ClientPacket* New(PacketHeader const& header, BitStream& stream)
{
return new PacketType(header, stream);
}
std::map<PacketHeader, PacketCreateFn> _creators;
};
}
#define sBattlenetPacketFactory Battlenet::PacketFactory::Instance()
#endif // BattlenetPacketFactory_h__

View File

@@ -50,6 +50,11 @@ std::string Battlenet::Authentication::LogonRequest::ToString() const
return stream.str();
}
void Battlenet::Authentication::LogonRequest::CallHandler(Session* session) const
{
session->HandleLogonRequest(*this);
}
void Battlenet::Authentication::ResumeRequest::Read()
{
Program = _stream.ReadFourCC();
@@ -77,13 +82,18 @@ std::string Battlenet::Authentication::ResumeRequest::ToString() const
for (Component const& component : Components)
stream << std::endl << "Battlenet::Component Program: " << component.Program << ", Platform: " << component.Platform << ", Build: " << component.Build;
stream << std::endl << "Battlenet::Authentication::ResumeRequest Login: " << Login;
stream << std::endl << "Battlenet::Authentication::ResumeRequest Region: " << uint32(Region);
stream << std::endl << "Battlenet::Authentication::ResumeRequest GameAccountName: " << GameAccountName;
stream << std::endl << "Login: " << Login;
stream << std::endl << "Region: " << uint32(Region);
stream << std::endl << "GameAccountName: " << GameAccountName;
return stream.str();
}
void Battlenet::Authentication::ResumeRequest::CallHandler(Session* session) const
{
session->HandleResumeRequest(*this);
}
Battlenet::Authentication::ProofRequest::~ProofRequest()
{
for (size_t i = 0; i < Modules.size(); ++i)
@@ -143,6 +153,11 @@ std::string Battlenet::Authentication::ProofResponse::ToString() const
return stream.str();
}
void Battlenet::Authentication::ProofResponse::CallHandler(Session* session) const
{
session->HandleProofResponse(*this);
}
Battlenet::Authentication::LogonResponse::~LogonResponse()
{
for (ModuleInfo* m : Modules)

View File

@@ -47,6 +47,7 @@ namespace Battlenet
void Read() override;
std::string ToString() const override;
void CallHandler(Session* session) const override;
std::string Program;
std::string Platform;
@@ -65,6 +66,7 @@ namespace Battlenet
void Read() override;
std::string ToString() const override;
void CallHandler(Session* session) const override;
std::string Program;
std::string Platform;
@@ -87,6 +89,7 @@ namespace Battlenet
void Read() override;
std::string ToString() const override;
void CallHandler(Session* session) const override;
std::vector<BitStream*> Modules;
};

View File

@@ -31,6 +31,7 @@ using boost::asio::ip::tcp;
namespace Battlenet
{
class BitStream;
class Session;
enum Channel
{
@@ -100,6 +101,7 @@ namespace Battlenet
ClientPacket(PacketHeader const& header, BitStream& stream) : Packet(header, stream) { }
void Write() override final { ASSERT(!"Write not implemented for this packet."); }
virtual void CallHandler(Session* session) const = 0;
};
class ServerPacket : public Packet

View File

@@ -15,8 +15,39 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "BattlenetSession.h"
#include "ConnectionPackets.h"
std::string Battlenet::Connection::Ping::ToString() const
{
return "Battlenet::Connection::Ping";
}
void Battlenet::Connection::Ping::CallHandler(Session* session) const
{
session->HandlePing(*this);
}
std::string Battlenet::Connection::EnableEncryption::ToString() const
{
return "Battlenet::Connection::EnableEncryption";
}
void Battlenet::Connection::EnableEncryption::CallHandler(Session* session) const
{
session->HandleEnableEncryption(*this);
}
std::string Battlenet::Connection::LogoutRequest::ToString() const
{
return "Battlenet::Connection::LogoutRequest";
}
void Battlenet::Connection::LogoutRequest::CallHandler(Session* session) const
{
session->HandleLogoutRequest(*this);
}
std::string Battlenet::Connection::Pong::ToString() const
{
return "Battlenet::Connection::Pong";

View File

@@ -39,6 +39,45 @@ namespace Battlenet
SMSG_STUN_SERVERS = 0x4 // Not implemented
};
class Ping final : public ClientPacket
{
public:
Ping(PacketHeader const& header, BitStream& stream) : ClientPacket(header, stream)
{
ASSERT(header == PacketHeader(CMSG_PING, CONNECTION) && "Invalid packet header for Ping");
}
void Read() override { }
std::string ToString() const override;
void CallHandler(Session* session) const override;
};
class EnableEncryption final : public ClientPacket
{
public:
EnableEncryption(PacketHeader const& header, BitStream& stream) : ClientPacket(header, stream)
{
ASSERT(header == PacketHeader(CMSG_ENABLE_ENCRYPTION, CONNECTION) && "Invalid packet header for EnableEncryption");
}
void Read() override { }
std::string ToString() const override;
void CallHandler(Session* session) const override;
};
class LogoutRequest final : public ClientPacket
{
public:
LogoutRequest(PacketHeader const& header, BitStream& stream) : ClientPacket(header, stream)
{
ASSERT(header == PacketHeader(CMSG_LOGOUT_REQUEST, CONNECTION) && "Invalid packet header for LogoutRequest");
}
void Read() override { }
std::string ToString() const override;
void CallHandler(Session* session) const override;
};
class Pong final : public ServerPacket
{
public:

View File

@@ -16,9 +16,20 @@
*/
#include "WoWRealmPackets.h"
#include "BattlenetSession.h"
#include <boost/lexical_cast.hpp>
#include <boost/asio/ip/address.hpp>
std::string Battlenet::WoWRealm::ListSubscribeRequest::ToString() const
{
return "Battlenet::WoWRealm::ListSubscribeRequest";
}
void Battlenet::WoWRealm::ListSubscribeRequest::CallHandler(Session* session) const
{
session->HandleListSubscribeRequest(*this);
}
Battlenet::WoWRealm::ListSubscribeResponse::~ListSubscribeResponse()
{
for (ServerPacket* realmData : RealmData)
@@ -131,6 +142,11 @@ std::string Battlenet::WoWRealm::JoinRequestV2::ToString() const
return stream.str().c_str();
}
void Battlenet::WoWRealm::JoinRequestV2::CallHandler(Session* session) const
{
session->HandleJoinRequestV2(*this);
}
void Battlenet::WoWRealm::JoinResponseV2::Write()
{
_stream.Write(0, 27);

View File

@@ -39,6 +39,19 @@ namespace Battlenet
SMSG_JOIN_RESPONSE_V2 = 0x8
};
class ListSubscribeRequest final : public ClientPacket
{
public:
ListSubscribeRequest(PacketHeader const& header, BitStream& stream) : ClientPacket(header, stream)
{
ASSERT(header == PacketHeader(CMSG_LIST_SUBSCRIBE_REQUEST, WOWREALM) && "Invalid packet header for ListSubscribeRequest");
}
void Read() override { }
std::string ToString() const override;
void CallHandler(Session* session) const override;
};
class JoinRequestV2 final : public ClientPacket
{
public:
@@ -49,6 +62,7 @@ namespace Battlenet
void Read() override;
std::string ToString() const override;
void CallHandler(Session* session) const override;
uint32 ClientSeed;
RealmId Realm;

View File

@@ -17,6 +17,7 @@
#include "AuthCodes.h"
#include "BattlenetBitStream.h"
#include "BattlenetPacketFactory.h"
#include "BattlenetSessionManager.h"
#include "Database/DatabaseEnv.h"
#include "HmacHash.h"
@@ -24,27 +25,6 @@
#include "RealmList.h"
#include "SHA256.h"
#include <map>
#include <boost/asio/write.hpp>
std::map<Battlenet::PacketHeader, Battlenet::Session::PacketHandler> InitHandlers()
{
std::map<Battlenet::PacketHeader, Battlenet::Session::PacketHandler> handlers;
handlers[Battlenet::PacketHeader(Battlenet::Authentication::CMSG_LOGON_REQUEST, Battlenet::AUTHENTICATION)] = &Battlenet::Session::HandleLogonRequest;
handlers[Battlenet::PacketHeader(Battlenet::Authentication::CMSG_RESUME_REQUEST, Battlenet::AUTHENTICATION)] = &Battlenet::Session::HandleResumeRequest;
handlers[Battlenet::PacketHeader(Battlenet::Authentication::CMSG_PROOF_RESPONSE, Battlenet::AUTHENTICATION)] = &Battlenet::Session::HandleProofResponse;
handlers[Battlenet::PacketHeader(Battlenet::Connection::CMSG_PING, Battlenet::CONNECTION)] = &Battlenet::Session::HandlePing;
handlers[Battlenet::PacketHeader(Battlenet::Connection::CMSG_ENABLE_ENCRYPTION, Battlenet::CONNECTION)] = &Battlenet::Session::HandleEnableEncryption;
handlers[Battlenet::PacketHeader(Battlenet::Connection::CMSG_LOGOUT_REQUEST, Battlenet::CONNECTION)] = &Battlenet::Session::HandleLogoutRequest;
handlers[Battlenet::PacketHeader(Battlenet::WoWRealm::CMSG_LIST_SUBSCRIBE_REQUEST, Battlenet::WOWREALM)] = &Battlenet::Session::HandleListSubscribeRequest;
handlers[Battlenet::PacketHeader(Battlenet::WoWRealm::CMSG_JOIN_REQUEST_V2, Battlenet::WOWREALM)] = &Battlenet::Session::HandleJoinRequestV2;
return handlers;
}
std::map<Battlenet::PacketHeader, Battlenet::Session::PacketHandler> Handlers = InitHandlers();
Battlenet::Session::ModuleHandler const Battlenet::Session::ModuleHandlers[MODULE_COUNT] =
{
@@ -109,7 +89,12 @@ void Battlenet::Session::_SetVSFields(std::string const& pstr)
LoginDatabase.Execute(stmt);
}
bool Battlenet::Session::HandleLogonRequest(PacketHeader& header, BitStream& packet)
void Battlenet::Session::LogUnhandledPacket(ClientPacket const& packet)
{
TC_LOG_DEBUG("server.battlenet", "Battlenet::Session::LogUnhandledPacket %s", packet.ToString().c_str());
}
void Battlenet::Session::HandleLogonRequest(Authentication::LogonRequest const& info)
{
// Verify that this IP is not in the ip_banned table
LoginDatabase.Execute(LoginDatabase.GetPreparedStatement(LOGIN_DEL_EXPIRED_IP_BANS));
@@ -123,18 +108,15 @@ bool Battlenet::Session::HandleLogonRequest(PacketHeader& header, BitStream& pac
complete->SetAuthResult(LOGIN_BANNED);
AsyncWrite(complete);
TC_LOG_DEBUG("server.battlenet", "[Battlenet::AuthChallenge] Banned ip '%s:%d' tries to login!", ip_address.c_str(), GetRemotePort());
return true;
return;
}
Authentication::LogonRequest info(header, packet);
info.Read();
if (info.Program != "WoW")
{
Authentication::LogonResponse* complete = new Authentication::LogonResponse();
complete->SetAuthResult(AUTH_INVALID_PROGRAM);
AsyncWrite(complete);
return true;
return;
}
if (!sBattlenetMgr->HasPlatform(info.Platform))
@@ -142,7 +124,7 @@ bool Battlenet::Session::HandleLogonRequest(PacketHeader& header, BitStream& pac
Authentication::LogonResponse* complete = new Authentication::LogonResponse();
complete->SetAuthResult(AUTH_INVALID_OS);
AsyncWrite(complete);
return true;
return;
}
if (!sBattlenetMgr->HasPlatform(info.Locale))
@@ -150,7 +132,7 @@ bool Battlenet::Session::HandleLogonRequest(PacketHeader& header, BitStream& pac
Authentication::LogonResponse* complete = new Authentication::LogonResponse();
complete->SetAuthResult(AUTH_UNSUPPORTED_LANGUAGE);
AsyncWrite(complete);
return true;
return;
}
for (Component const& component : info.Components)
@@ -171,7 +153,7 @@ bool Battlenet::Session::HandleLogonRequest(PacketHeader& header, BitStream& pac
}
AsyncWrite(complete);
return true;
return;
}
if (component.Platform == "base")
@@ -192,7 +174,7 @@ bool Battlenet::Session::HandleLogonRequest(PacketHeader& header, BitStream& pac
Authentication::LogonResponse* complete = new Authentication::LogonResponse();
complete->SetAuthResult(AUTH_UNKNOWN_ACCOUNT);
AsyncWrite(complete);
return true;
return;
}
Field* fields = result->Fetch();
@@ -209,7 +191,7 @@ bool Battlenet::Session::HandleLogonRequest(PacketHeader& header, BitStream& pac
Authentication::LogonResponse* complete = new Authentication::LogonResponse();
complete->SetAuthResult(AUTH_ACCOUNT_LOCKED);
AsyncWrite(complete);
return true;
return;
}
}
else
@@ -234,7 +216,7 @@ bool Battlenet::Session::HandleLogonRequest(PacketHeader& header, BitStream& pac
Authentication::LogonResponse* complete = new Authentication::LogonResponse();
complete->SetAuthResult(AUTH_ACCOUNT_LOCKED);
AsyncWrite(complete);
return true;
return;
}
}
}
@@ -256,7 +238,7 @@ bool Battlenet::Session::HandleLogonRequest(PacketHeader& header, BitStream& pac
complete->SetAuthResult(LOGIN_BANNED);
AsyncWrite(complete);
TC_LOG_DEBUG("server.battlenet", "'%s:%d' [Battlenet::AuthChallenge] Banned account %s tried to login!", ip_address.c_str(), GetRemotePort(), _accountName.c_str());
return true;
return;
}
else
{
@@ -264,7 +246,7 @@ bool Battlenet::Session::HandleLogonRequest(PacketHeader& header, BitStream& pac
complete->SetAuthResult(LOGIN_SUSPENDED);
AsyncWrite(complete);
TC_LOG_DEBUG("server.battlenet", "'%s:%d' [Battlenet::AuthChallenge] Temporarily banned account %s tried to login!", ip_address.c_str(), GetRemotePort(), _accountName.c_str());
return true;
return;
}
}
@@ -314,16 +296,10 @@ bool Battlenet::Session::HandleLogonRequest(PacketHeader& header, BitStream& pac
// if has authenticator, send Token module
request->Modules.push_back(thumbprint);
AsyncWrite(request);
return true;
}
bool Battlenet::Session::HandleResumeRequest(PacketHeader& header, BitStream& packet)
void Battlenet::Session::HandleResumeRequest(Authentication::ResumeRequest const& reconnect)
{
Authentication::ResumeRequest reconnect(header, packet);
reconnect.Read();
TC_LOG_DEBUG("server.battlenet", "%s", reconnect.ToString().c_str());
_accountName = reconnect.Login;
_locale = reconnect.Locale;
_os = reconnect.Platform;
@@ -341,7 +317,7 @@ bool Battlenet::Session::HandleResumeRequest(PacketHeader& header, BitStream& pa
Authentication::ResumeResponse* resume = new Authentication::ResumeResponse();
resume->SetAuthResult(AUTH_UNKNOWN_ACCOUNT);
AsyncWrite(resume);
return false;
return;
}
Field* fields = result->Fetch();
@@ -370,20 +346,16 @@ bool Battlenet::Session::HandleResumeRequest(PacketHeader& header, BitStream& pa
request->Modules.push_back(thumbprint);
request->Modules.push_back(resume);
AsyncWrite(request);
return true;
}
bool Battlenet::Session::HandleProofResponse(PacketHeader& header, BitStream& packet)
void Battlenet::Session::HandleProofResponse(Authentication::ProofResponse const& proof)
{
Authentication::ProofResponse proof(header, packet);
proof.Read();
if (_modulesWaitingForData.size() < proof.Modules.size())
{
Authentication::LogonResponse* complete = new Authentication::LogonResponse();
complete->SetAuthResult(AUTH_CORRUPTED_MODULE);
AsyncWrite(complete);
return true;
return;
}
ServerPacket* response = nullptr;
@@ -402,32 +374,29 @@ bool Battlenet::Session::HandleProofResponse(PacketHeader& header, BitStream& pa
}
AsyncWrite(response);
return true;
return;
}
bool Battlenet::Session::HandlePing(PacketHeader& /*header*/, BitStream& /*packet*/)
void Battlenet::Session::HandlePing(Connection::Ping const& /*ping*/)
{
AsyncWrite(new Connection::Pong());
return true;
}
bool Battlenet::Session::HandleEnableEncryption(PacketHeader& /*header*/, BitStream& /*packet*/)
void Battlenet::Session::HandleEnableEncryption(Connection::EnableEncryption const& /*enableEncryption*/)
{
_crypt.Init(&K);
return true;
}
bool Battlenet::Session::HandleLogoutRequest(PacketHeader& /*header*/, BitStream& /*packet*/)
void Battlenet::Session::HandleLogoutRequest(Connection::LogoutRequest const& /*logoutRequest*/)
{
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_BNET_SESSION_KEY);
stmt->setString(0, "");
stmt->setBool(1, false);
stmt->setUInt32(2, _accountId);
LoginDatabase.Execute(stmt);
return true;
}
bool Battlenet::Session::HandleListSubscribeRequest(PacketHeader& /*header*/, BitStream& /*packet*/)
void Battlenet::Session::HandleListSubscribeRequest(WoWRealm::ListSubscribeRequest const& /*listSubscribeRequest*/)
{
sRealmList->UpdateIfNeed();
@@ -491,21 +460,17 @@ bool Battlenet::Session::HandleListSubscribeRequest(PacketHeader& /*header*/, Bi
counts->RealmData.push_back(new WoWRealm::ListComplete());
AsyncWrite(counts);
return true;
}
bool Battlenet::Session::HandleJoinRequestV2(PacketHeader& header, BitStream& packet)
void Battlenet::Session::HandleJoinRequestV2(WoWRealm::JoinRequestV2 const& join)
{
WoWRealm::JoinRequestV2 join(header, packet);
join.Read();
WoWRealm::JoinResponseV2* result = new WoWRealm::JoinResponseV2();
Realm const* realm = sRealmList->GetRealm(join.Realm);
if (!realm || realm->flag & (REALM_FLAG_INVALID | REALM_FLAG_OFFLINE))
{
result->Response = WoWRealm::JoinResponseV2::FAILURE;
AsyncWrite(result);
return true;
return;
}
result->ServerSeed = uint32(rand32());
@@ -535,22 +500,21 @@ bool Battlenet::Session::HandleJoinRequestV2(PacketHeader& header, BitStream& pa
result->IPv4.emplace_back(realm->LocalAddress, realm->port);
AsyncWrite(result);
return true;
}
void Battlenet::Session::ReadHandler()
{
BitStream packet(std::move(GetReadBuffer()));
_crypt.DecryptRecv(packet.GetBuffer(), packet.GetSize());
BitStream stream(std::move(GetReadBuffer()));
_crypt.DecryptRecv(stream.GetBuffer(), stream.GetSize());
while (!packet.IsRead())
while (!stream.IsRead())
{
try
{
PacketHeader header;
header.Opcode = packet.Read<uint32>(6);
if (packet.Read<bool>(1))
header.Channel = packet.Read<int32>(4);
header.Opcode = stream.Read<uint32>(6);
if (stream.Read<bool>(1))
header.Channel = stream.Read<int32>(4);
if (header.Channel != AUTHENTICATION && !_authed)
{
@@ -559,12 +523,11 @@ void Battlenet::Session::ReadHandler()
return;
}
TC_LOG_TRACE("server.battlenet", "Battlenet::Session::ReadDataHandler %s", header.ToString().c_str());
std::map<PacketHeader, PacketHandler>::const_iterator itr = Handlers.find(header);
if (itr != Handlers.end())
if (ClientPacket* packet = sBattlenetPacketFactory.Create(header, stream))
{
if ((this->*(itr->second))(header, packet))
break;
TC_LOG_TRACE("server.battlenet", "Battlenet::Session::ReadDataHandler %s", packet->ToString().c_str());
packet->CallHandler(this);
delete packet;
}
else
{
@@ -572,7 +535,7 @@ void Battlenet::Session::ReadHandler()
break;
}
packet.AlignToNextByte();
stream.AlignToNextByte();
}
catch (BitStreamPositionException const& e)
{

View File

@@ -59,21 +59,21 @@ namespace Battlenet
explicit Session(tcp::socket&& socket);
~Session();
typedef bool(Session::*PacketHandler)(PacketHeader& socket, BitStream& packet);
void LogUnhandledPacket(ClientPacket const& packet);
// Authentication
bool HandleLogonRequest(PacketHeader& header, BitStream& packet);
bool HandleResumeRequest(PacketHeader& header, BitStream& packet);
bool HandleProofResponse(PacketHeader& header, BitStream& packet);
void HandleLogonRequest(Authentication::LogonRequest const& logonRequest);
void HandleResumeRequest(Authentication::ResumeRequest const& resumeRequest);
void HandleProofResponse(Authentication::ProofResponse const& proofResponse);
// Connection
bool HandlePing(PacketHeader& header, BitStream& packet);
bool HandleEnableEncryption(PacketHeader& header, BitStream& packet);
bool HandleLogoutRequest(PacketHeader& header, BitStream& packet);
void HandlePing(Connection::Ping const& ping);
void HandleEnableEncryption(Connection::EnableEncryption const& enableEncryption);
void HandleLogoutRequest(Connection::LogoutRequest const& logoutRequest);
// WoWRealm
bool HandleListSubscribeRequest(PacketHeader& header, BitStream& packet);
bool HandleJoinRequestV2(PacketHeader& header, BitStream& packet);
void HandleListSubscribeRequest(WoWRealm::ListSubscribeRequest const& listSubscribeRequest);
void HandleJoinRequestV2(WoWRealm::JoinRequestV2 const& joinRequest);
void Start() override;