Files

45 lines
2.2 KiB
C++

#pragma once
#include "gateway/legacy_stream.hpp"
#include <algorithm>
#include <string_view>
namespace gateway::realm_query {
inline constexpr uint32_t Request = 0x4400b5;
inline constexpr uint32_t Response = 0x4b0005;
// Bound to the same address and Source realm name advertised by AuthResponse.
// This transport-owned context deliberately does not depend on world entry.
class Context {
uint32_t address_ = 0;
std::string name_, normalized_;
public:
void bind(uint32_t address, std::string_view name) {
if (!address || name.empty() || name.size() > 127 ||
std::any_of(name.begin(), name.end(), [](unsigned char c){ return c < 32; }))
throw Error("invalid authenticated realm name");
std::string normalized(name);
normalized.erase(std::remove(normalized.begin(), normalized.end(), ' '), normalized.end());
if (normalized.empty()) throw Error("empty normalized realm name");
if (address_ && (address_ != address || name_ != name))
throw Error("authenticated realm context changed");
name_ = name; normalized_ = std::move(normalized); address_ = address;
}
LegacyPacket reply(std::span<const uint8_t> request) const {
if (!address_ || request.size() != 4) throw Error("invalid realm query");
const uint32_t key = uint32_t(request[0]) | (uint32_t(request[1]) << 8) |
(uint32_t(request[2]) << 16) | (uint32_t(request[3]) << 24);
LegacyPacket packet{Response, Bytes(request.begin(), request.end())};
packet.body.push_back(key == address_ ? 0 : 1); // RESPONSE_SUCCESS / FAILURE
if (key != address_) return packet;
// MSB-first IsLocal(1), IsInternalRealm(1), actual length(8), normalized length(8).
const uint32_t bits = 0x800000u | (uint32_t(name_.size()) << 14) |
(uint32_t(normalized_.size()) << 6);
packet.body.push_back(uint8_t(bits >> 16));
packet.body.push_back(uint8_t(bits >> 8));
packet.body.push_back(uint8_t(bits));
packet.body.insert(packet.body.end(), name_.begin(), name_.end());
packet.body.insert(packet.body.end(), normalized_.begin(), normalized_.end());
return packet;
}
};
}