Files
DarkflameServer/dNet/CommonPackets.cpp
Aaron Kimbrell 70f1c2b19e refactor: common and auth packets as structs
Adds CommonPackets (ServiceType::COMMON): ClientVersionConfirm (the
client's handshake, with the handler that logs it and answers),
ServerVersionConfirm, DisconnectNotify and GeneralNotify (layout from the
1.10.64 client's PacketHandler_MSG_SERVER_GENERAL_NOTIFY; DLU does not send
it yet). AuthPackets::LoginRequest reads the login and keeps the old login
logic in its Handle; ClientPackets::LoginResponse (with the Stamp list)
replaces the hand written response, and AuthPackets::SendLoginResponse
fills it from the settings as before. dServer::Disconnect writes a
DisconnectNotify. AuthServer's if-chain and WorldServer's COMMON case
become CommonPackets::Handle / AuthPackets::Handle, dispatch maps that
read the struct, drop and log it if it does not deserialize, then Handle.
HandleHandshake and SendHandshake are gone.

No wire change. Verified against a frozen verbatim copy of the old
functions (tests/dGameTests/dNetTests/Legacy): the old handshake and login
handlers and the new dispatchers are fed the same packets and must send
identical bytes to the same address (same RNG seed for the session key),
over grids of versions, service types, ports, response codes, error
messages, IPs and stamp lists; plus hand computed golden bytes, round
trips, truncation checks, and a deliberate field width mutation that made
the tests fail. The only behaviour difference: truncated handshake and
login packets are now dropped instead of handled with whatever was read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 22:30:48 -05:00

112 lines
3.6 KiB
C++

#include "CommonPackets.h"
#include "dConfig.h"
#include "dServer.h"
#include "eServerDisconnectIdentifiers.h"
#include "Game.h"
#include "GeneralUtils.h"
#include "Logger.h"
#include "ServiceType.h"
#include "StringifiedEnum.h"
#include <functional>
#include <map>
#include <memory>
namespace CommonPackets {
void ClientVersionConfirm::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(netVersion);
bitStream.Write(unknown);
bitStream.Write(serviceType);
bitStream.Write(padding);
bitStream.Write(processID);
bitStream.Write(port);
bitStream.Write(unknown2);
}
bool ClientVersionConfirm::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(netVersion));
VALIDATE_READ(bitStream.Read(unknown));
VALIDATE_READ(bitStream.Read(serviceType));
VALIDATE_READ(bitStream.Read(padding));
VALIDATE_READ(bitStream.Read(processID));
VALIDATE_READ(bitStream.Read(port));
VALIDATE_READ(bitStream.Read(unknown2));
return true;
}
void ClientVersionConfirm::Handle() {
if (serviceType != ServiceType::CLIENT) LOG("WARNING: Service is not a Client!");
if (port != sysAddr.port) LOG("WARNING: Port written in packet does not match the port the client is connecting over!");
LOG_DEBUG("Client Data [Version: %i, Service: %s, Process: %u, Port: %u, Sysaddr Port: %u]", netVersion, StringifiedEnum::ToString(serviceType).data(), processID, port, sysAddr.port);
ServerVersionConfirm response;
const auto& clientNetVersionString = Game::config->GetValue("client_net_version");
response.netVersion = GeneralUtils::TryParse<uint32_t>(clientNetVersionString).value_or(ServerVersionConfirm::DEFAULT_NET_VERSION);
response.serviceType = static_cast<uint32_t>(Game::server->GetServerType());
response.Send(sysAddr);
}
void ServerVersionConfirm::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(netVersion);
bitStream.Write(unknown);
bitStream.Write(serviceType);
bitStream.Write(trailing);
}
bool ServerVersionConfirm::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(netVersion));
VALIDATE_READ(bitStream.Read(unknown));
VALIDATE_READ(bitStream.Read(serviceType));
VALIDATE_READ(bitStream.Read(trailing));
return true;
}
void DisconnectNotify::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(disconnectID);
}
bool DisconnectNotify::Deserialize(RakNet::BitStream& bitStream) {
VALIDATE_READ(bitStream.Read(disconnectID));
return true;
}
void GeneralNotify::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write(notifyType);
bitStream.Write<uint8_t>(showMessageBox);
}
bool GeneralNotify::Deserialize(RakNet::BitStream& bitStream) {
uint8_t show{};
VALIDATE_READ(bitStream.Read(notifyType));
VALIDATE_READ(bitStream.Read(show));
showMessageBox = show != 0;
return true;
}
namespace {
// Only what a client sends; the other COMMON packets go from server to client.
const std::map<MessageType::Server, std::function<std::unique_ptr<ClientVersionConfirm>()>> g_Handlers = {
{ MessageType::Server::VERSION_CONFIRM, []() { return std::make_unique<ClientVersionConfirm>(); } },
};
}
void Handle(RakNet::BitStream& inStream, const SystemAddress& sysAddr, const uint32_t packetID) {
const auto messageID = static_cast<MessageType::Server>(packetID);
const auto it = g_Handlers.find(messageID);
if (it == g_Handlers.end()) {
LOG_DEBUG("Unhandled common packet %i", packetID);
return;
}
auto request = it->second();
request->sysAddr = sysAddr;
if (!request->Deserialize(inStream)) {
LOG("Failed to read common packet %s", StringifiedEnum::ToString(messageID).data());
return;
}
request->Handle();
}
}