Files
praxisandClaude Fable 5.2 8e02dccb86 In-client stack: transport, session lifecycle and engine bridge
175 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

232 lines
9.7 KiB
C

#include <windows.h>
#include <stdint.h>
#include <string.h>
#include "direct_profile5_prepare.h"
#include "wine_session_retirement.h"
#include "wine_source_profile.h"
/* GW_DIRECT_PUBLIC_PROFILE=2 binds the same direct login to the public
* Source profile (play.octowow.st:3724). Its endpoint carries no address:
* the Wire account worker resolves the immutable profile host on submission,
* so no expiring DNS token is held while the user types. */
#if defined(GW_DIRECT_PUBLIC_PROFILE)
#if GW_DIRECT_PUBLIC_PROFILE != 2
#error Unsupported direct public Source profile
#endif
#define GW_DIRECT_PROFILE_KIND 2
#define GW_DIRECT_PROFILE_PORT 3724
static const char public_host[] = "play.octowow.st";
static const char pinned_auth_ipv4[16] = {0};
#else
#if !defined(GW_DIRECT_PROFILE5_AUTH_IPV4) || !defined(GW_DIRECT_PROFILE5_AUTH_PORT)
#error Direct profile5 requires a pinned Source authentication endpoint
#endif
_Static_assert(sizeof(GW_DIRECT_PROFILE5_AUTH_IPV4) <= 16,
"Pinned Source IPv4 does not fit the Wire endpoint ABI");
#define GW_DIRECT_PROFILE_KIND 5
#define GW_DIRECT_PROFILE_PORT GW_DIRECT_PROFILE5_AUTH_PORT
static const char public_host[] = GW_DIRECT_PROFILE5_AUTH_IPV4;
static const char pinned_auth_ipv4[16] = GW_DIRECT_PROFILE5_AUTH_IPV4;
#endif
struct GwDirectFrameData {
uint32_t abi, size;
volatile LONG command, completed, calls, thread, error;
volatile LONG initialize_status, call_status, result, echoed, elapsed_ms;
void *host;
WCHAR filename[512];
};
_Static_assert(sizeof(struct GwDirectFrameData) == 1080,
"Existing Wire frame ABI changed");
_Static_assert(sizeof(LONG) == sizeof(int32_t),
"Win32 frame command width changed");
struct GwDirectProfile5State {
void *owner;
void *volatile *owner_slot;
DWORD thread;
uint32_t generation, port;
void *expected_frame_host;
struct gw_wire_auth_control *account;
struct gw_wire_endpoint_profile *endpoint;
struct gw_wire_auth_credentials *mailbox;
struct gw_session_retirement_control *retirement;
struct GwDirectFrameData *frame;
};
static struct GwDirectProfile5State selected;
static volatile LONG selected_state;
static void *owned_export(HMODULE module, const char *name)
{
return (void *)(uintptr_t)GetProcAddress(module, name);
}
static size_t bounded_length(const char *value, size_t limit)
{
size_t i;
if (!value) return limit;
for (i = 0; i < limit; ++i)
if (!value[i]) return i;
return limit;
}
static int retirement_generation(const struct gw_session_retirement_control *r,
void *owner, uint32_t *generation)
{
uint32_t next;
if (!r || r->abi != 1 || r->size != sizeof(*r)) return 0;
next = r->next_credentials_generation;
if (!next || next > GW_SESSION_EPOCH_LIMIT) return 0;
if (next == 1) {
if (r->state != 0 || r->expected_credentials_generation ||
r->completed_credentials_generation) return 0;
} else if (r->state != 2 || r->owner != (uint64_t)(uintptr_t)owner ||
r->expected_credentials_generation != next - 1 ||
r->completed_credentials_generation != next - 1) return 0;
*generation = next;
return 1;
}
int gw_direct_profile5_ready(void *owner, uint32_t generation)
{
const struct GwDirectProfile5State *s = &selected;
if (InterlockedCompareExchange(&selected_state, 1, 1) != 1 ||
!owner || owner != s->owner ||
generation != s->generation ||
GetCurrentThreadId() != s->thread ||
*s->owner_slot != owner ||
s->account->state != 0 || s->account->cancel ||
s->account->auth_port != s->port ||
s->account->password_path[0] ||
!s->account->version_hash_path[0] ||
!s->account->addon_path[0] ||
s->endpoint->kind != 1 ||
memcmp(s->endpoint->auth_ipv4, pinned_auth_ipv4,
sizeof(pinned_auth_ipv4)) != 0 ||
s->frame->abi != 1 || s->frame->size != sizeof(*s->frame) ||
s->frame->host != s->expected_frame_host ||
s->frame->completed != 1 || s->frame->initialize_status != 0 ||
s->frame->error != 0 || s->frame->command != 2 ||
s->mailbox->abi != 1 || s->mailbox->size != sizeof(*s->mailbox) ||
s->mailbox->state != 0 || s->mailbox->generation ||
s->mailbox->username_bytes || s->mailbox->password_bytes ||
s->mailbox->consumed_generation || s->mailbox->reserved)
return 0;
{
uint32_t next = 0;
return retirement_generation(s->retirement, owner, &next) &&
next == generation;
}
}
int gw_direct_profile5_prepare(const struct GwDirectProfile5Input *input,
struct GwDirectLoginBinding *binding)
{
typedef LONG (WINAPI *ReadProfile)(struct gw_source_profile *);
struct gw_source_profile source = {1, sizeof(source), 0, 0, {0}};
struct GwDirectProfile5State next = {0};
ReadProfile read_profile;
size_t version_bytes, addon_bytes;
uint32_t generation = 0;
LONG prior_selected;
if (!input || !binding || input->abi != 1 ||
input->size != sizeof(*input) || !input->wire_module ||
!input->owner || !input->original_owner_slot ||
!input->client_thread || !input->frame_admitted ||
!input->frame_host ||
GetCurrentThreadId() != input->client_thread ||
*input->original_owner_slot != input->owner) return -1;
version_bytes = bounded_length(input->version_hash_path, 512);
addon_bytes = bounded_length(input->addon_tail_path, 512);
if (!version_bytes || version_bytes >= 512 ||
!addon_bytes || addon_bytes >= 512) return -2;
prior_selected = InterlockedCompareExchange(&selected_state, 0, 0);
if (prior_selected == 1) {
if (selected.owner != input->owner ||
selected.owner_slot != input->original_owner_slot ||
selected.thread != input->client_thread ||
selected.expected_frame_host != input->frame_host ||
InterlockedCompareExchange(&selected_state, -2, 1) != 1)
return -3;
} else if (prior_selected != 0 ||
InterlockedCompareExchange(&selected_state, -2, 0) != 0)
return -3;
read_profile = (ReadProfile)(uintptr_t)owned_export(input->wire_module,
"gw_source_profile_read");
next.account = owned_export(input->wire_module, "gw_wire_auth_data");
next.endpoint = owned_export(input->wire_module,
"gw_wire_endpoint_profile_data");
next.mailbox = owned_export(input->wire_module,
"gw_wire_auth_credentials_data");
next.retirement = owned_export(input->wire_module,
"gw_wire_session_retirement_data");
next.frame = owned_export(input->wire_module, "gw_wire_frame_data");
if (!read_profile || !next.account || !next.endpoint || !next.mailbox ||
!next.retirement || !next.frame || read_profile(&source) != GW_DIRECT_PROFILE_KIND ||
source.abi != 1 || source.size != sizeof(source) ||
source.kind != GW_DIRECT_PROFILE_KIND ||
source.auth_port != GW_DIRECT_PROFILE_PORT ||
bounded_length(source.hostname, sizeof(source.hostname)) !=
sizeof(public_host) - 1 ||
memcmp(source.hostname, public_host, sizeof(public_host)) != 0 ||
!retirement_generation(next.retirement, input->owner, &generation) ||
(prior_selected == 1 && generation != selected.generation + 1) ||
(prior_selected == 0 && generation != 1) ||
next.account->abi != 1 ||
next.account->size != sizeof(*next.account) ||
next.account->state || next.account->cancel ||
next.account->username[0] || next.account->password_path[0] ||
next.endpoint->abi != 1 ||
next.endpoint->size != sizeof(*next.endpoint) ||
next.endpoint->kind || next.endpoint->reserved ||
next.endpoint->auth_ipv4[0] ||
next.mailbox->abi != 1 ||
next.mailbox->size != sizeof(*next.mailbox) ||
next.mailbox->state || next.mailbox->generation ||
next.mailbox->username_bytes || next.mailbox->password_bytes ||
next.frame->abi != 1 || next.frame->size != sizeof(*next.frame) ||
next.frame->host != input->frame_host ||
next.frame->completed != 1 || next.frame->initialize_status != 0 ||
next.frame->error != 0 || next.frame->command != 2) {
InterlockedExchange(&selected_state, -1);
return -4;
}
next.owner = input->owner;
next.owner_slot = input->original_owner_slot;
next.thread = input->client_thread;
next.generation = generation;
next.port = source.auth_port;
next.expected_frame_host = input->frame_host;
next.account->auth_port = source.auth_port;
next.account->retention_ms = 120000;
next.account->auth_result = -1;
next.account->discovery_result = -1;
next.account->selected_index = -1;
next.account->transport_result = -1;
memcpy(next.account->version_hash_path,
input->version_hash_path, version_bytes + 1);
memcpy(next.account->addon_path, input->addon_tail_path,
addon_bytes + 1);
next.endpoint->kind = 1;
memcpy(next.endpoint->auth_ipv4, pinned_auth_ipv4,
sizeof(pinned_auth_ipv4));
selected = next;
MemoryBarrier();
InterlockedExchange(&selected_state, 1);
memset(binding, 0, sizeof(*binding));
binding->abi = 1;
binding->size = sizeof(*binding);
binding->owner = input->owner;
binding->original_owner_slot = input->original_owner_slot;
binding->client_thread = input->client_thread;
binding->generation = generation;
binding->mailbox = next.mailbox;
binding->frame_command = (volatile int32_t *)(void *)&next.frame->command;
binding->profile_ready = gw_direct_profile5_ready;
return 0;
}