Skip to content

Commit

Permalink
Fix reading bnet packets.
Browse files Browse the repository at this point in the history
  • Loading branch information
ratkosrb committed Oct 29, 2023
1 parent 9f37c2e commit fa9ad07
Show file tree
Hide file tree
Showing 13 changed files with 352 additions and 586 deletions.
56 changes: 50 additions & 6 deletions src/server/bnetserver/AuthSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,34 @@ void AuthSocket::OnAccept()
// Read the packet from the client
void AuthSocket::OnRead()
{
size_t recvLen = recv_len();
std::vector<uint8> buf;
buf.resize(recv_len());
recv((char*)&buf[0], recv_len());
buf.resize(recvLen);
recv((char*)&buf[0], recvLen);
sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, "Received %u bytes", recvLen);

ASSERT(buf.size() > sizeof(uint16));
uint16 headerSize = *((uint16*)buf.data());
EndianConvertReverse(headerSize);
sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, "Header size is %u", headerSize);

Header header;
ASSERT(header.ParseFromArray(buf.data(), buf.size()));
ASSERT(header.ParseFromArray(buf.data() + sizeof(uint16), headerSize));
sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, "Service id is %u, service hash is %u", header.service_id(), header.service_hash());

MessageBuffer msgBuffer;
msgBuffer.Write(buf.data() + sizeof(uint16) + header.size(), recvLen - (sizeof(uint16) + header.size()));

sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, "Recieved request for service id %u", header.service_id());
if (header.service_id() != 0xFE)
{
sServiceDispatcher.Dispatch(this, header.service_hash(), header.token(), header.method_id(), std::move(msgBuffer));
}
}

void AuthSocket::SendResponse(uint32 token, pb::Message const* response)
{
sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, "SendResponse: token %u", token);

Header header;
header.set_token(token);
header.set_service_id(0xFE);
Expand All @@ -135,11 +151,13 @@ void AuthSocket::SendResponse(uint32 token, pb::Message const* response)
packet.WriteCompleted(response->ByteSize());
response->SerializeToArray(ptr, response->ByteSize());

//AsyncWrite(&packet);
send((char*)packet.GetBasePointer(), packet.GetBufferSize());
}

void AuthSocket::SendResponse(uint32 token, uint32 status)
{
sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, "SendResponse: token %u status %u", token, status);

Header header;
header.set_token(token);
header.set_status(status);
Expand All @@ -154,7 +172,33 @@ void AuthSocket::SendResponse(uint32 token, uint32 status)
packet.WriteCompleted(header.ByteSize());
header.SerializeToArray(ptr, header.ByteSize());

//AsyncWrite(&packet);
send((char*)packet.GetBasePointer(), packet.GetBufferSize());
}

void AuthSocket::SendRequest(uint32 serviceHash, uint32 methodId, pb::Message const* request)
{
sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, "SendResponse: serviceHash %u methodId %u", serviceHash, methodId);

Header header;
header.set_service_id(0);
header.set_service_hash(serviceHash);
header.set_method_id(methodId);
header.set_size(request->ByteSize());
header.set_token(_requestToken++);

uint16 headerSize = header.ByteSize();
EndianConvertReverse(headerSize);

MessageBuffer packet;
packet.Write(&headerSize, sizeof(headerSize));
uint8* ptr = packet.GetWritePointer();
packet.WriteCompleted(header.ByteSize());
header.SerializeToArray(ptr, header.ByteSize());
ptr = packet.GetWritePointer();
packet.WriteCompleted(request->ByteSize());
request->SerializeToArray(ptr, request->ByteSize());

send((char*)packet.GetBasePointer(), packet.GetBufferSize());
}

void AuthSocket::LoadRealmlist(ByteBuffer &pkt)
Expand Down
10 changes: 10 additions & 0 deletions src/server/bnetserver/AuthSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "Auth/Sha1.h"
#include "SRP6/SRP6.h"
#include "ByteBuffer.h"
#include "Utilities/MessageBuffer.h"
#include "BufferedSocket.h"
#include <google/protobuf/message.h>

Expand Down Expand Up @@ -108,6 +109,12 @@ class AuthSocket: public BufferedSocket
static void InitTcpSSL();
void SendResponse(uint32 token, pb::Message const* response);
void SendResponse(uint32 token, uint32 status);
void SendRequest(uint32 serviceHash, uint32 methodId, pb::Message const* request, std::function<void(MessageBuffer)> callback)
{
_responseCallbacks[_requestToken] = std::move(callback);
SendRequest(serviceHash, methodId, request);
}
void SendRequest(uint32 serviceHash, uint32 methodId, pb::Message const* request);

void OnAccept();
void OnRead();
Expand Down Expand Up @@ -170,6 +177,9 @@ class AuthSocket: public BufferedSocket
AccountTypes m_accountDefaultSecurityLevel = SEC_PLAYER;
typedef std::map<uint32, AccountTypes> AccountSecurityMap;
AccountSecurityMap m_accountSecurityOnRealm;

std::unordered_map<uint32, std::function<void(MessageBuffer)>> _responseCallbacks;
uint32 _requestToken = 0;
};
#endif
// @}
18 changes: 9 additions & 9 deletions src/server/bnetserver/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@

#ifdef WIN32
#include "ServiceWin32.h"
char serviceName[] = "realmd";
char serviceLongName[] = "MaNGOS realmd service";
char serviceName[] = "bnetserver";
char serviceLongName[] = "MaNGOS bnetserver service";
char serviceDescription[] = "Massive Network Game Object Server";
/*
* -1 - not in service mode
Expand Down Expand Up @@ -94,7 +94,7 @@ void usage(const char *prog)
,prog);
}

char const* g_mainLogFileName = "Realmd.log";
char const* g_mainLogFileName = "bnetserver.log";

// Launch the realm server
extern int main(int argc, char **argv)
Expand Down Expand Up @@ -207,7 +207,7 @@ extern int main(int argc, char **argv)
if (confVersion < _REALMDCONFVERSION)
{
sLog.Out(LOG_BASIC, LOG_LVL_ERROR, "*****************************************************************************");
sLog.Out(LOG_BASIC, LOG_LVL_ERROR, " WARNING: Your realmd.conf version indicates your conf file is out of date!");
sLog.Out(LOG_BASIC, LOG_LVL_ERROR, " WARNING: Your bnetserver.conf version indicates your conf file is out of date!");
sLog.Out(LOG_BASIC, LOG_LVL_ERROR, " Please check for updates, as your current default values may cause");
sLog.Out(LOG_BASIC, LOG_LVL_ERROR, " strange behavior.");
sLog.Out(LOG_BASIC, LOG_LVL_ERROR, "*****************************************************************************");
Expand Down Expand Up @@ -239,7 +239,7 @@ extern int main(int argc, char **argv)

sLog.Out(LOG_BASIC, LOG_LVL_BASIC, "Max allowed open files is %d", ACE::max_handles());

// realmd PID file creation
// bnetserver PID file creation
std::string pidfile = sConfig.GetStringDefault("PidFile", "");
if(!pidfile.empty())
{
Expand Down Expand Up @@ -307,7 +307,7 @@ extern int main(int argc, char **argv)

if(acceptor.open(bind_addr, ACE_Reactor::instance(), ACE_NONBLOCK) == -1)
{
sLog.Out(LOG_BASIC, LOG_LVL_ERROR, "MaNGOS realmd can not bind to %s:%d", bind_ip.c_str(), rmport);
sLog.Out(LOG_BASIC, LOG_LVL_ERROR, "MaNGOS bnetserver can not bind to %s:%d", bind_ip.c_str(), rmport);
Log::WaitBeforeContinueIfNeed();
return 1;
}
Expand All @@ -332,7 +332,7 @@ extern int main(int argc, char **argv)

if(!curAff )
{
sLog.Out(LOG_BASIC, LOG_LVL_ERROR, "Processors marked in UseProcessors bitmask (hex) %x not accessible for realmd. Accessible processors bitmask (hex): %x",Aff,appAff);
sLog.Out(LOG_BASIC, LOG_LVL_ERROR, "Processors marked in UseProcessors bitmask (hex) %x not accessible for bnetserver. Accessible processors bitmask (hex): %x",Aff,appAff);
}
else
{
Expand All @@ -350,9 +350,9 @@ extern int main(int argc, char **argv)
if(Prio)
{
if(SetPriorityClass(hProcess,HIGH_PRIORITY_CLASS))
sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, "realmd process priority class set to HIGH");
sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, "bnetserver process priority class set to HIGH");
else
sLog.Out(LOG_BASIC, LOG_LVL_ERROR, "Can't set realmd process priority class.");
sLog.Out(LOG_BASIC, LOG_LVL_ERROR, "Can't set bnetserver process priority class.");
sLog.Out(LOG_BASIC, LOG_LVL_MINIMAL, "");
}
}
Expand Down
57 changes: 57 additions & 0 deletions src/server/bnetserver/Services/ConnectionService.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2008-2019 TrinityCore <https://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/>.
*/

#include "ConnectionService.h"
#include "Duration.h"
#include "Log.h"
#include "AuthSocket.h"
#include "Util.h"
#include "BattlenetRpcErrorCodes.h"

Battlenet::Services::Connection::Connection(AuthSocket* session) : ConnectionService(session)
{
}

uint32 Battlenet::Services::Connection::HandleConnect(connection::v1::ConnectRequest const* request, connection::v1::ConnectResponse* response, std::function<void(ServiceBase*, uint32, ::google::protobuf::Message const*)>& /*continuation*/)
{
if (request->has_client_id())
response->mutable_client_id()->CopyFrom(request->client_id());

std::chrono::system_clock::duration now = std::chrono::system_clock::now().time_since_epoch();

response->mutable_server_id()->set_label(GetPID());
response->mutable_server_id()->set_epoch(std::chrono::duration_cast<Seconds>(now).count());
response->set_server_time(std::chrono::duration_cast<Milliseconds>(now).count());

response->set_use_bindless_rpc(request->use_bindless_rpc());
return ERROR_OK;
}

uint32 Battlenet::Services::Connection::HandleKeepAlive(NoData const* /*request*/)
{
return ERROR_OK;
}

uint32 Battlenet::Services::Connection::HandleRequestDisconnect(connection::v1::DisconnectRequest const* request)
{
connection::v1::DisconnectNotification disconnectNotification;
disconnectNotification.set_error_code(request->error_code());
ForceDisconnect(&disconnectNotification);

_session->close_connection();
return ERROR_OK;
}
46 changes: 46 additions & 0 deletions src/server/bnetserver/Services/ConnectionService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2008-2019 TrinityCore <https://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 ConnectionService_h__
#define ConnectionService_h__

#include "Common.h"
#include "Service.h"
#include "connection_service.pb.h"

class AuthSocket;

namespace Battlenet
{
namespace Services
{
class Connection : public Service<connection::v1::ConnectionService>
{
typedef Service<connection::v1::ConnectionService> ConnectionService;

public:
Connection(AuthSocket* session);

uint32 HandleConnect(connection::v1::ConnectRequest const* request, connection::v1::ConnectResponse* response, std::function<void(ServiceBase*, uint32, ::google::protobuf::Message const*)>& continuation) override;
uint32 HandleKeepAlive(NoData const* request) override;
uint32 HandleRequestDisconnect(connection::v1::DisconnectRequest const* request) override;

};
}
}

#endif // ConnectionService_h__
10 changes: 5 additions & 5 deletions src/server/bnetserver/Services/ServiceDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@

#include "ServiceDispatcher.h"
#include "AuthSocket.h"
#include "ConnectionService.h"

Battlenet::ServiceDispatcher::ServiceDispatcher()
{
/*
AddService<Services::Account>();
AddService<Services::Authentication>();
//AddService<Services::Account>();
//AddService<Services::Authentication>();
AddService<Service<club::v1::membership::ClubMembershipService>>();
AddService<Services::Connection>();
AddService<Service<friends::v1::FriendsService>>();
AddService<Services::GameUtilities>();
//AddService<Services::GameUtilities>();
AddService<Service<presence::v1::PresenceService>>();
AddService<Service<report::v1::ReportService>>();
AddService<Service<report::v2::ReportService>>();
AddService<Service<resources::v1::ResourcesService>>();
AddService<Service<user_manager::v1::UserManagerService>>();
*/
}

void Battlenet::ServiceDispatcher::Dispatch(AuthSocket* session, uint32 serviceHash, uint32 token, uint32 methodId, MessageBuffer buffer)
Expand Down
2 changes: 1 addition & 1 deletion src/server/bnetserver/Services/ServiceDispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ namespace Battlenet
};
}

#define sServiceDispatcher ServiceDispatcher::Instance()
#define sServiceDispatcher Battlenet::ServiceDispatcher::Instance()

#endif // ServiceDispatcher_h__
Loading

0 comments on commit fa9ad07

Please sign in to comment.