486 lines
16 KiB
C++
486 lines
16 KiB
C++
#pragma once
|
|
// Private binary transport for the existing backend ABI, never a game packet.
|
|
// No native engine pointers or raw gw_wire_call memory cross this boundary.
|
|
#include "gateway/protocol.hpp"
|
|
#include "wine_wire_abi.h"
|
|
#include <algorithm>
|
|
#include <bit>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
namespace gateway::backend_ipc {
|
|
// Keep this private field codec self-contained. The in-client frontend must
|
|
// not acquire the backend/codec or crypto-library imports just to frame bytes.
|
|
inline void le(Bytes &out, uint64_t value, size_t count) {
|
|
if (count > 8)
|
|
throw Error("backend IPC integer width");
|
|
for (size_t i = 0; i < count; ++i)
|
|
out.push_back(uint8_t(value >> (8 * i)));
|
|
}
|
|
inline void append(Bytes &out, std::span<const uint8_t> value) {
|
|
out.insert(out.end(), value.begin(), value.end());
|
|
}
|
|
class Reader {
|
|
std::span<const uint8_t> bytes;
|
|
size_t position = 0;
|
|
|
|
public:
|
|
explicit Reader(std::span<const uint8_t> value) : bytes(value) {}
|
|
uint64_t le(size_t count) {
|
|
if (count > 8 || count > bytes.size() - position)
|
|
throw Error("backend IPC truncated integer");
|
|
uint64_t value = 0;
|
|
for (size_t i = 0; i < count; ++i)
|
|
value |= uint64_t(bytes[position++]) << (8 * i);
|
|
return value;
|
|
}
|
|
Bytes take(size_t count) {
|
|
if (count > bytes.size() - position)
|
|
throw Error("backend IPC truncated bytes");
|
|
Bytes value(bytes.begin() + position, bytes.begin() + position + count);
|
|
position += count;
|
|
return value;
|
|
}
|
|
void end() const {
|
|
if (position != bytes.size())
|
|
throw Error("backend IPC trailing bytes");
|
|
}
|
|
};
|
|
constexpr uint32_t Magic = 0x42495747, Version = 1, Request = 1, Response = 2;
|
|
constexpr size_t HeaderBytes = 32, MaxPayload = GW_CODEC_BOUND,
|
|
MaxFrame = GW_CODEC_BOUND + 65536, MaxString = 4096;
|
|
struct Context {
|
|
uint64_t generation = 0, sequence = 0;
|
|
};
|
|
inline void wipe(void *data, size_t size) noexcept {
|
|
auto p = static_cast<volatile uint8_t *>(data);
|
|
while (size--)
|
|
*p++ = 0;
|
|
}
|
|
struct WipeBytes {
|
|
Bytes &bytes;
|
|
~WipeBytes() {
|
|
if (!bytes.empty())
|
|
wipe(bytes.data(), bytes.size());
|
|
}
|
|
};
|
|
inline bool auth(uint32_t op) {
|
|
return op == GW_WIRE_AUTHENTICATE || op == GW_WIRE_AUTHENTICATE_MEMORY ||
|
|
op == GW_WIRE_AUTHENTICATE_MEMORY_IPV4;
|
|
}
|
|
inline bool path(uint32_t op) {
|
|
return op == GW_WIRE_TRANSPORT_OPEN || op == GW_WIRE_TRANSPORT_OPEN_IPV4;
|
|
}
|
|
inline void operation(uint32_t op) {
|
|
if (op > GW_WIRE_PRIVATE_GAMEPLAY_CAPTURE && op != GW_WIRE_ACCOUNT_NAME)
|
|
throw Error("backend IPC operation bound");
|
|
}
|
|
inline void string(Bytes &out, const char *p, size_t bound = MaxString) {
|
|
if (!p) {
|
|
le(out, UINT32_MAX, 4);
|
|
return;
|
|
}
|
|
size_t length = 0;
|
|
while (length < bound && p[length])
|
|
++length;
|
|
if (length == bound)
|
|
throw Error("backend IPC string bound");
|
|
le(out, length, 4);
|
|
append(out, std::span(reinterpret_cast<const uint8_t *>(p), length));
|
|
}
|
|
struct Text {
|
|
bool present = false;
|
|
std::string bytes;
|
|
Text() = default;
|
|
Text(const Text &) = delete;
|
|
Text &operator=(const Text &) = delete;
|
|
Text(Text &&other) : present(other.present), bytes(other.bytes) {
|
|
other.clear();
|
|
other.bytes.clear();
|
|
other.present = false;
|
|
}
|
|
Text &operator=(Text &&other) {
|
|
if (this != &other) {
|
|
clear();
|
|
present = other.present;
|
|
bytes = other.bytes;
|
|
other.clear();
|
|
other.bytes.clear();
|
|
other.present = false;
|
|
}
|
|
return *this;
|
|
}
|
|
~Text() { clear(); }
|
|
const char *pointer() const { return present ? bytes.c_str() : nullptr; }
|
|
void clear() noexcept {
|
|
if (!bytes.empty())
|
|
wipe(bytes.data(), bytes.size());
|
|
}
|
|
};
|
|
inline Text text(Reader &r, size_t bound = MaxString) {
|
|
auto size = r.le(4);
|
|
if (size == UINT32_MAX)
|
|
return {};
|
|
if (size >= bound)
|
|
throw Error("backend IPC string bound");
|
|
auto bytes = r.take(size);
|
|
WipeBytes clean{bytes};
|
|
if (std::find(bytes.begin(), bytes.end(), 0) != bytes.end())
|
|
throw Error("backend IPC embedded string terminator");
|
|
Text out;
|
|
out.present = true;
|
|
out.bytes.assign(bytes.begin(), bytes.end());
|
|
return out;
|
|
}
|
|
inline Bytes frame(uint32_t kind, Context context, const Bytes &body) {
|
|
if (!context.generation || !context.sequence ||
|
|
body.size() > MaxFrame - HeaderBytes)
|
|
throw Error("backend IPC frame bound");
|
|
Bytes out;
|
|
out.reserve(HeaderBytes + body.size());
|
|
le(out, Magic, 4);
|
|
le(out, Version, 2);
|
|
le(out, kind, 2);
|
|
le(out, context.generation, 8);
|
|
le(out, context.sequence, 8);
|
|
le(out, body.size(), 4);
|
|
le(out, 0, 4);
|
|
append(out, body);
|
|
return out;
|
|
}
|
|
inline uint32_t frame_size(std::span<const uint8_t> header) {
|
|
Reader r(header);
|
|
if (r.le(4) != Magic || r.le(2) != Version)
|
|
throw Error("backend IPC header identity");
|
|
auto kind = r.le(2);
|
|
if (kind != Request && kind != Response)
|
|
throw Error("backend IPC frame kind");
|
|
if (!r.le(8) || !r.le(8))
|
|
throw Error("backend IPC zero context");
|
|
auto size = r.le(4);
|
|
if (size > MaxFrame - HeaderBytes || r.le(4))
|
|
throw Error("backend IPC frame bound");
|
|
r.end();
|
|
return uint32_t(size);
|
|
}
|
|
inline Bytes body(std::span<const uint8_t> input, uint32_t kind,
|
|
Context context) {
|
|
if (input.size() < HeaderBytes || input.size() > MaxFrame)
|
|
throw Error("backend IPC incomplete frame");
|
|
auto size = frame_size(input.first(HeaderBytes));
|
|
Reader r(input.first(HeaderBytes));
|
|
r.le(6);
|
|
if (r.le(2) != kind || r.le(8) != context.generation ||
|
|
r.le(8) != context.sequence)
|
|
throw Error("backend IPC stale context");
|
|
if (input.size() != HeaderBytes + size)
|
|
throw Error("backend IPC frame length");
|
|
return Bytes(input.begin() + HeaderBytes, input.end());
|
|
}
|
|
inline void blob(Bytes &out, const uint8_t *data, uint32_t bytes,
|
|
size_t bound = MaxPayload) {
|
|
if (bytes > bound || (!data && bytes))
|
|
throw Error("backend IPC buffer bound");
|
|
le(out, data ? 1 : 0, 1);
|
|
le(out, bytes, 4);
|
|
if (bytes)
|
|
append(out, std::span(data, bytes));
|
|
}
|
|
struct Buffer {
|
|
bool present = false;
|
|
Bytes bytes;
|
|
uint8_t zero = 0;
|
|
Buffer() = default;
|
|
Buffer(bool value, Bytes data, uint8_t initial)
|
|
: present(value), bytes(std::move(data)), zero(initial) {}
|
|
Buffer(const Buffer &) = delete;
|
|
Buffer &operator=(const Buffer &) = delete;
|
|
Buffer(Buffer &&other) noexcept
|
|
: present(other.present), bytes(std::move(other.bytes)),
|
|
zero(other.zero) {
|
|
other.present = false;
|
|
other.zero = 0;
|
|
}
|
|
Buffer &operator=(Buffer &&other) noexcept {
|
|
if (this != &other) {
|
|
clear();
|
|
present = other.present;
|
|
bytes = std::move(other.bytes);
|
|
zero = other.zero;
|
|
other.present = false;
|
|
other.zero = 0;
|
|
}
|
|
return *this;
|
|
}
|
|
~Buffer() { clear(); }
|
|
const uint8_t *pointer() const {
|
|
return present ? (bytes.empty() ? &zero : bytes.data()) : nullptr;
|
|
}
|
|
void clear() noexcept {
|
|
if (!bytes.empty())
|
|
wipe(bytes.data(), bytes.size());
|
|
zero = 0;
|
|
}
|
|
};
|
|
inline Buffer buffer(Reader &r, size_t bound = MaxPayload) {
|
|
auto present = r.le(1), size = r.le(4);
|
|
if (present > 1 || size > bound || (!present && size))
|
|
throw Error("backend IPC buffer bound");
|
|
return {present != 0, r.take(size), 0};
|
|
}
|
|
struct OwnedCall {
|
|
gw_wire_call call{};
|
|
gw_native_auth_memory_ipv4_config ipv4{};
|
|
Text username, password_path, version_path, addon, ipv4_address,
|
|
ipv4_username, ipv4_version_path;
|
|
Buffer input, password;
|
|
Bytes output;
|
|
uint8_t output_zero = 0;
|
|
OwnedCall() {
|
|
call.abi = GW_WINE_WIRE_ABI;
|
|
call.size = sizeof(call);
|
|
}
|
|
OwnedCall(const OwnedCall &) = delete;
|
|
OwnedCall &operator=(const OwnedCall &) = delete;
|
|
void clear() noexcept {
|
|
username.clear();
|
|
password_path.clear();
|
|
version_path.clear();
|
|
addon.clear();
|
|
ipv4_address.clear();
|
|
ipv4_username.clear();
|
|
ipv4_version_path.clear();
|
|
input.clear();
|
|
password.clear();
|
|
if (!output.empty())
|
|
wipe(output.data(), output.size());
|
|
output_zero = 0;
|
|
wipe(&call, sizeof(call));
|
|
wipe(&ipv4, sizeof(ipv4));
|
|
}
|
|
~OwnedCall() { clear(); }
|
|
};
|
|
inline Bytes encode_request(const gw_wire_call &c, Context context) {
|
|
if (c.abi != GW_WINE_WIRE_ABI || c.size != sizeof(c) ||
|
|
c.capacity > MaxPayload || c.input_bytes > MaxPayload)
|
|
throw Error("backend IPC local call bound");
|
|
operation(c.operation);
|
|
Bytes b;
|
|
b.reserve(MaxFrame);
|
|
WipeBytes clean{b};
|
|
le(b, c.abi, 4);
|
|
le(b, c.size, 4);
|
|
le(b, c.operation, 4);
|
|
le(b, c.login, 8);
|
|
le(b, c.transport, 8);
|
|
for (auto value :
|
|
{c.index, c.build, c.opcode, c.input_bytes, c.capacity, c.realm_id,
|
|
c.virtual_address, c.super_district, c.reserved})
|
|
le(b, value, 4);
|
|
le(b, c.output ? 1 : 0, 1);
|
|
if (auth(c.operation)) {
|
|
le(b, c.auth.abi, 4);
|
|
le(b, c.auth.auth_port, 2);
|
|
string(b, c.auth.username);
|
|
string(b, c.auth.password_path);
|
|
string(b, c.auth.version_hash_path);
|
|
}
|
|
if (path(c.operation))
|
|
string(b, c.addon_path);
|
|
if (c.operation == GW_WIRE_AUTHENTICATE_MEMORY_IPV4) {
|
|
if (!c.input || c.input_bytes != sizeof(gw_native_auth_memory_ipv4_config))
|
|
throw Error("backend IPC IPv4 config shape");
|
|
gw_native_auth_memory_ipv4_config cfg{};
|
|
std::copy_n(c.input, sizeof(cfg), reinterpret_cast<uint8_t *>(&cfg));
|
|
le(b, cfg.abi, 4);
|
|
le(b, cfg.size, 4);
|
|
le(b, cfg.auth_port, 2);
|
|
le(b, cfg.reserved, 2);
|
|
string(b, cfg.auth_ipv4);
|
|
string(b, cfg.username);
|
|
string(b, cfg.version_hash_path);
|
|
blob(b, cfg.password, cfg.password_bytes, 64);
|
|
} else if (c.operation != GW_WIRE_AUTHENTICATE) {
|
|
blob(b, c.input, c.input_bytes,
|
|
c.operation == GW_WIRE_AUTHENTICATE_MEMORY ? 64 : MaxPayload);
|
|
}
|
|
auto out = frame(Request, context, b);
|
|
wipe(b.data(), b.size());
|
|
return out;
|
|
}
|
|
inline std::unique_ptr<OwnedCall> decode_request(std::span<const uint8_t> input,
|
|
Context context) {
|
|
auto b = body(input, Request, context);
|
|
WipeBytes clean{b};
|
|
Reader r(b);
|
|
auto p = std::make_unique<OwnedCall>();
|
|
auto &c = p->call;
|
|
c.abi = uint32_t(r.le(4));
|
|
c.size = uint32_t(r.le(4));
|
|
c.operation = uint32_t(r.le(4));
|
|
operation(c.operation);
|
|
if (c.abi != GW_WINE_WIRE_ABI || c.size != sizeof(c))
|
|
throw Error("backend IPC ABI mismatch");
|
|
c.login = r.le(8);
|
|
c.transport = r.le(8);
|
|
c.index = uint32_t(r.le(4));
|
|
c.build = uint32_t(r.le(4));
|
|
c.opcode = uint32_t(r.le(4));
|
|
c.input_bytes = uint32_t(r.le(4));
|
|
c.capacity = uint32_t(r.le(4));
|
|
c.realm_id = uint32_t(r.le(4));
|
|
c.virtual_address = uint32_t(r.le(4));
|
|
c.super_district = uint32_t(r.le(4));
|
|
c.reserved = uint32_t(r.le(4));
|
|
const auto output_present = r.le(1);
|
|
if (output_present > 1)
|
|
throw Error("backend IPC output presence");
|
|
if (c.input_bytes > MaxPayload || c.capacity > MaxPayload)
|
|
throw Error("backend IPC request capacity bound");
|
|
if (auth(c.operation)) {
|
|
c.auth.abi = uint32_t(r.le(4));
|
|
c.auth.auth_port = uint16_t(r.le(2));
|
|
p->username = text(r);
|
|
p->password_path = text(r);
|
|
p->version_path = text(r);
|
|
c.auth.username = p->username.pointer();
|
|
c.auth.password_path = p->password_path.pointer();
|
|
c.auth.version_hash_path = p->version_path.pointer();
|
|
}
|
|
if (path(c.operation)) {
|
|
p->addon = text(r);
|
|
c.addon_path = p->addon.pointer();
|
|
}
|
|
if (c.operation == GW_WIRE_AUTHENTICATE_MEMORY_IPV4) {
|
|
if (c.input_bytes != sizeof(p->ipv4))
|
|
throw Error("backend IPC IPv4 config shape");
|
|
auto &cfg = p->ipv4;
|
|
cfg.abi = uint32_t(r.le(4));
|
|
cfg.size = uint32_t(r.le(4));
|
|
cfg.auth_port = uint16_t(r.le(2));
|
|
cfg.reserved = uint16_t(r.le(2));
|
|
p->ipv4_address = text(r);
|
|
p->ipv4_username = text(r);
|
|
p->ipv4_version_path = text(r);
|
|
p->password = buffer(r, 64);
|
|
cfg.auth_ipv4 = p->ipv4_address.pointer();
|
|
cfg.username = p->ipv4_username.pointer();
|
|
cfg.version_hash_path = p->ipv4_version_path.pointer();
|
|
cfg.password = p->password.pointer();
|
|
cfg.password_bytes = uint32_t(p->password.bytes.size());
|
|
// auth fields must be empty for this ABI; do not silently replace the
|
|
// caller's malformed outer pointers with the nested fields.
|
|
if (c.auth.abi || c.auth.auth_port || c.auth.username ||
|
|
c.auth.password_path || c.auth.version_hash_path)
|
|
throw Error("backend IPC IPv4 outer auth fields");
|
|
c.input = reinterpret_cast<const uint8_t *>(&cfg);
|
|
} else if (c.operation != GW_WIRE_AUTHENTICATE) {
|
|
p->input =
|
|
buffer(r, c.operation == GW_WIRE_AUTHENTICATE_MEMORY ? 64 : MaxPayload);
|
|
if (c.input_bytes != p->input.bytes.size())
|
|
throw Error("backend IPC input length");
|
|
c.input = p->input.pointer();
|
|
}
|
|
r.end();
|
|
p->output.resize(c.capacity);
|
|
c.output = output_present
|
|
? (p->output.empty() ? &p->output_zero : p->output.data())
|
|
: nullptr;
|
|
return p;
|
|
}
|
|
inline void fixed_string(Reader &r, char *output, size_t bound) {
|
|
auto value = text(r, bound);
|
|
if (!value.present)
|
|
throw Error("backend IPC missing fixed string");
|
|
std::fill_n(output, bound, 0);
|
|
std::copy(value.bytes.begin(), value.bytes.end(), output);
|
|
}
|
|
inline Bytes encode_response(const gw_wire_call &c, int32_t status,
|
|
Context context) {
|
|
if (c.required > MaxPayload || c.capacity > MaxPayload ||
|
|
(!c.result && c.required > c.capacity))
|
|
throw Error("backend IPC response bound");
|
|
Bytes b;
|
|
b.reserve(MaxFrame);
|
|
WipeBytes clean{b};
|
|
le(b, c.operation, 4);
|
|
le(b, uint32_t(status), 4);
|
|
le(b, uint32_t(c.result), 4);
|
|
le(b, c.login, 8);
|
|
le(b, c.transport, 8);
|
|
for (auto value : {c.opcode, c.required, c.consumed, c.value, c.reserved,
|
|
c.packet.opcode, c.packet.payload_bytes})
|
|
le(b, value, 4);
|
|
le(b, c.character.guid, 8);
|
|
le(b, c.character.level, 4);
|
|
le(b, c.character.race, 4);
|
|
le(b, c.character.class_id, 4);
|
|
string(b, c.character.name, sizeof(c.character.name));
|
|
for (auto value : {c.realm.type, c.realm.flags, c.realm.characters,
|
|
c.realm.category, c.realm.wire_id})
|
|
le(b, value, 4);
|
|
le(b, std::bit_cast<uint32_t>(c.realm.population), 4);
|
|
string(b, c.realm.name, sizeof(c.realm.name));
|
|
string(b, c.realm.address, sizeof(c.realm.address));
|
|
blob(b, c.result == 0 ? c.output : nullptr, c.result == 0 ? c.required : 0);
|
|
auto out = frame(Response, context, b);
|
|
wipe(b.data(), b.size());
|
|
return out;
|
|
}
|
|
inline int32_t apply_response(std::span<const uint8_t> input, gw_wire_call &c,
|
|
Context context) {
|
|
auto b = body(input, Response, context);
|
|
WipeBytes clean{b};
|
|
Reader r(b);
|
|
if (r.le(4) != c.operation)
|
|
throw Error("backend IPC wrong operation reply");
|
|
auto status = std::bit_cast<int32_t>(uint32_t(r.le(4))),
|
|
result = std::bit_cast<int32_t>(uint32_t(r.le(4)));
|
|
auto login = r.le(8), transport = r.le(8);
|
|
auto opcode = uint32_t(r.le(4)), required = uint32_t(r.le(4)),
|
|
consumed = uint32_t(r.le(4)), value = uint32_t(r.le(4));
|
|
const auto reserved = uint32_t(r.le(4));
|
|
gw_native_packet packet{uint32_t(r.le(4)), uint32_t(r.le(4))};
|
|
gw_native_character character{};
|
|
character.guid = r.le(8);
|
|
character.level = uint32_t(r.le(4));
|
|
character.race = uint32_t(r.le(4));
|
|
character.class_id = uint32_t(r.le(4));
|
|
fixed_string(r, character.name, sizeof(character.name));
|
|
gw_native_realm realm{};
|
|
realm.type = uint32_t(r.le(4));
|
|
realm.flags = uint32_t(r.le(4));
|
|
realm.characters = uint32_t(r.le(4));
|
|
realm.category = uint32_t(r.le(4));
|
|
realm.wire_id = uint32_t(r.le(4));
|
|
realm.population = std::bit_cast<float>(uint32_t(r.le(4)));
|
|
fixed_string(r, realm.name, sizeof(realm.name));
|
|
fixed_string(r, realm.address, sizeof(realm.address));
|
|
auto output = buffer(r);
|
|
r.end();
|
|
if (required > MaxPayload || output.bytes.size() > c.capacity ||
|
|
(!c.output && !output.bytes.empty()) ||
|
|
(result == 0 ? output.bytes.size() != required : !output.bytes.empty()))
|
|
throw Error("backend IPC caller output bound");
|
|
c.result = result;
|
|
c.login = login;
|
|
c.transport = transport;
|
|
c.opcode = opcode;
|
|
c.required = required;
|
|
c.consumed = consumed;
|
|
c.value = value;
|
|
c.reserved = reserved;
|
|
c.packet = packet;
|
|
c.character = character;
|
|
c.realm = realm;
|
|
if (!output.bytes.empty())
|
|
std::copy(output.bytes.begin(), output.bytes.end(), c.output);
|
|
output.clear();
|
|
wipe(b.data(), b.size());
|
|
return status;
|
|
}
|
|
} // namespace gateway::backend_ipc
|