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

141 lines
6.2 KiB
C

#include <windows.h>
#include <stdint.h>
#include <string.h>
#include "wire_profile5_auto_realm.private.h"
/* Profile 5 admits the pinned local singleton realm. Profile 2 (public
* Source) admits the realm the user chose in the realm screen; its endpoint
* public endpoint names a host; the resolved IPv4 stays private to the account
* worker, whose prepared transport (transport_prepared==1) already used it. */
#if defined(GW_SOURCE_ONLY_PROFILE) && GW_SOURCE_ONLY_PROFILE == 2
#define GW_AUTO_REALM_PROFILE 2
#else
#define GW_AUTO_REALM_PROFILE 5
#ifndef GW_SOURCE_LOCAL_AUTH_IPV4
#error Profile5 auto realm admission requires a pinned Source authentication IPv4
#endif
_Static_assert(sizeof(GW_SOURCE_LOCAL_AUTH_IPV4) <= 16,
"Pinned Source IPv4 does not fit the Wire endpoint ABI");
#endif
/* Diagnostic only: records why a next-generation (account-switch) realm
* admission was denied, with a snapshot of the deciding retirement/owner state.
* The admission DECISION is byte-for-byte identical to the prior single-return
* guard: every clause below is one disjunct of that guard, in the same order,
* all equally denying. `reason` is 0 only when the function returns 1.
* Codes: 1 args, 2 preopen, 3 auth, 4 endpoint, 5 credentials, 6 retirement ABI,
* 7 generation coupling, 8 generation-1 terminal state, 90/91/92/93 the
* generation>1 terminal check (state!=2 / owner mismatch / expected / completed;
* 91 is the "retained primary owner" case). */
struct gw_wire_profile5_admit_diag {
uint32_t abi, size;
volatile int32_t reason;
uint32_t generation;
int32_t retire_state;
uint32_t expected, completed, next;
uint64_t retire_owner, call_owner;
};
_Static_assert(sizeof(struct gw_wire_profile5_admit_diag) == 48,
"Profile5 admit diagnostic ABI differs");
__declspec(dllexport) struct gw_wire_profile5_admit_diag
gw_wire_profile5_admit_diag_data = {.abi = 1, .size = 48};
static int gw_p5_admit_deny(int32_t code)
{
gw_wire_profile5_admit_diag_data.reason = code;
return 0;
}
int gw_wire_profile5_auto_realm_admit(
const struct gw_wire_auth_control *auth,
const struct gw_wire_auth_credentials *credentials,
const struct gw_wire_endpoint_profile *endpoint,
const struct gw_session_retirement_control *retirement,
const struct GwWirePreOpenHook *preopen,
void *owner, void *owner_slot_value, DWORD thread, LONG profile_kind)
{
#if GW_AUTO_REALM_PROFILE == 5
static const char pinned_auth_ipv4[16] = GW_SOURCE_LOCAL_AUTH_IPV4;
#endif
struct gw_wire_profile5_admit_diag *diag = &gw_wire_profile5_admit_diag_data;
uint32_t generation;
diag->call_owner = (uint64_t)(uintptr_t)owner;
diag->generation = 0;
if (retirement && retirement->abi == 1 &&
retirement->size == sizeof(*retirement)) {
diag->retire_state = retirement->state;
diag->retire_owner = retirement->owner;
diag->expected = retirement->expected_credentials_generation;
diag->completed = retirement->completed_credentials_generation;
diag->next = retirement->next_credentials_generation;
}
/* 1: basic arguments / owner-slot / thread / profile */
if (!auth || !credentials || !endpoint || !retirement || !preopen ||
!owner || owner_slot_value != owner || !thread ||
profile_kind != GW_AUTO_REALM_PROFILE)
return gw_p5_admit_deny(1);
/* 2: pre-open hook readiness */
if (preopen->abi != 1 || preopen->size != sizeof(*preopen) ||
preopen->ready != 1 || preopen->error || preopen->owner != owner ||
preopen->primary_thread != thread || !preopen->bridge_module ||
!preopen->callback)
return gw_p5_admit_deny(2);
/* 3: authentication terminal state + prepared transport */
if (auth->abi != 1 || auth->size != sizeof(*auth) ||
auth->state != 2 || auth->error || auth->cancel ||
auth->auth_result || auth->discovery_result ||
#if GW_AUTO_REALM_PROFILE == 5
auth->realm_count != 1 || auth->selected_index != 0 ||
#else
auth->realm_count < 1 || auth->realm_count > 64 ||
auth->selected_index < 0 || auth->selected_index >= auth->realm_count ||
#endif
auth->transport_result || auth->server_proof_verified != 1 ||
auth->transport_prepared != 1)
return gw_p5_admit_deny(3);
/* 4: owned endpoint identity */
if (endpoint->abi != 1 || endpoint->size != sizeof(*endpoint) ||
endpoint->kind != 1 || endpoint->reserved
#if GW_AUTO_REALM_PROFILE == 5
|| memcmp(endpoint->auth_ipv4, pinned_auth_ipv4,
sizeof(pinned_auth_ipv4)) != 0
#endif
)
return gw_p5_admit_deny(4);
/* 5: credentials consumed and wiped */
if (credentials->abi != 1 || credentials->size != sizeof(*credentials) ||
credentials->state != 3 || credentials->reserved ||
credentials->username_bytes || credentials->password_bytes)
return gw_p5_admit_deny(5);
/* 6: retirement control ABI */
if (retirement->abi != 1 || retirement->size != sizeof(*retirement))
return gw_p5_admit_deny(6);
/* 7: generation coupling (consumed == submitted == next-expected) */
generation = credentials->generation;
diag->generation = generation;
if (!generation || generation > GW_SESSION_EPOCH_LIMIT ||
credentials->consumed_generation != generation ||
retirement->next_credentials_generation != generation)
return gw_p5_admit_deny(7);
/* 8: first login — retirement untouched */
if (generation == 1) {
if (retirement->state == 0 &&
!retirement->expected_credentials_generation &&
!retirement->completed_credentials_generation) {
diag->reason = 0;
return 1;
}
return gw_p5_admit_deny(8);
}
/* 9x: account switch — prior generation retired terminally by this owner */
if (retirement->state != 2)
return gw_p5_admit_deny(90);
if (retirement->owner != (uint64_t)(uintptr_t)owner)
return gw_p5_admit_deny(91);
if (retirement->expected_credentials_generation != generation - 1)
return gw_p5_admit_deny(92);
if (retirement->completed_credentials_generation != generation - 1)
return gw_p5_admit_deny(93);
diag->reason = 0;
return 1;
}