# 2.6 Message Framing

_Peios Specifications / PGSS / Logon_

> The header both channels share — magic, version, message type and size limit — and how a message body may be extended.

Both of an authority's channels use the framing specified here without
modification. The identity socket's departures are stated in §2.14; they
concern which message types are served where, not the encoding.

## 2.6.1 Header

Every message begins with a 12-byte header:

| Offset | Size | Field | Value |
|---|---|---|---|
| 0 | 4 | `magic` | `PGSL` (`50 47 53 4c`) |
| 4 | 2 | `version` | `5` |
| 6 | 2 | `msg_type` | See §2.A |
| 8 | 4 | `total_len` | Header plus body, in bytes |

`total_len` counts the header. A message is therefore self-delimiting
from its first 12 bytes, and a reader can size its buffer before reading
the body.

## 2.6.2 Magic

The magic MUST be checked on every message and MUST cause an immediate,
whole-connection failure when wrong.

This is not decoration. Peios protocols share this codec and this header
layout, and some of them share message bodies. A socket plugged into the
wrong daemon would otherwise *partially* work, which is far worse than
failing outright — a subtle misbehavior several fields in, rather than
a hard error on byte zero. A protocol reusing this codec MUST take a
distinct magic.

## 2.6.3 Version

A peer receiving a `version` it does not implement MUST refuse the
message. An authority MUST refuse with `AccessDenied` carrying
`UnsupportedVersion` where it can still encode one.

## 2.6.4 Size limit

A message MUST NOT exceed **65536 bytes** in total. A decoder MUST
reject a header declaring more, without reading the body, and MUST
reject one declaring fewer than the header's own length.

## 2.6.5 Message type

The high bit of `msg_type` marks a message sent by the **authority**.
Client messages have it clear. This is a readability property rather
than a security one — a peer MUST validate the message type it received
against what it expected, not merely against the direction bit.

## 2.6.6 Encoding

All multi-byte integers are little-endian.

**Strings** are UTF-8 and are **not NUL-terminated**. A string is
encoded as a `u32` byte count followed by exactly that many bytes. A
decoder MUST reject a string whose bytes are not valid UTF-8.

An empty string and an absent optional string are encoded identically,
as a zero byte count. A field documented as optional is therefore absent
when empty, and an implementation MUST NOT distinguish the two.

**Byte strings** are encoded the same way and carry no encoding
requirement.

**Arrays** are encoded as a `u32` element count followed by that many
length-framed structures. Every array in this chapter has a stated
maximum; an encoder MUST refuse to produce a longer one and a decoder
MUST reject one it receives. The same holds for every stated byte limit.

Two distinct length mechanisms therefore appear, and confusing them is
the most likely implementation error:

- **Byte strings and strings** are prefixed with a `u32` byte count, and
  the bytes follow immediately.
- **Structures and array elements** are prefixed with a `u32` byte count
  of the *whole structure*, and a decoder MUST skip to the structure's
  declared end after reading the fields it knows.

The second is what makes the format extensible.

## 2.6.7 Body extensibility

The body is a single length-framed structure, and **every structure and
every array element within it is length-framed too**.

A decoder reads the fields it knows and then skips to the structure's
declared end. A field appended by a newer peer is therefore stepped over
rather than mistaken for the next field — which is what makes the format
extensible *inside arrays*, where ignoring trailing bytes at the message
level would not help.

A structure that ends before a field an older encoder never wrote is not
an error. A decoder that reaches the end of a structure's body where an
optional trailing field would have been MUST substitute that field's
documented default (§2.7, §2.9) rather than failing.

The rules this keeps working under are binding on anyone revising this
chapter:

- Fields are **appended only**. Never reordered, never removed, never
  changed in meaning.
- A new field MUST be **optional with a safe default**, because older
  peers will not send it and will not read it.
- Adding a value to an enumeration is a **breaking change** and requires
  a version bump, because every peer is required to understand every
  value it is sent.

The last rule is the one that surprises people. An unknown enumeration
value cannot be safely ignored: a client that skipped an unrecognized
credential type would silently fail to collect something the authority
required, and an authority that skipped an unrecognized logon type would
grant the wrong kind of session.

There are exactly four exceptions, and each is stated where it applies:
`LogonStart.supported_credential_types`, which is a statement of
capability rather than an instruction (§2.7); `LogonStart.logon_type`,
which only the authority ever receives (§2.7); the field mask of
`Lookup`, whose reply says which bits it answered (§2.16); and a denial
code that only ever answers a message newer than every peer that does
not know it, so that no such peer is ever sent it (§2.B, §2.22, §2.23).

## 2.6.8 Diagnostic operation identifier

`CredentialRequest`, `AccessGranted`, `AccessDenied`, and `Authenticated` MAY
append an `operation_guid` byte string to their version-6 body fields. Absence
defaults to an empty string. An explicit empty string also means that no
identifier was supplied. A nonempty value MUST contain exactly 16 bytes in
the PCDS GUID byte layout and MUST NOT be the zero GUID.

The authority assigns this identifier to the operation. The identifier MUST
remain the same across that operation's replies. An authority MUST NOT copy
an upstream source's proposed identifier into this field as its own. Fields
added after this extension MUST follow `operation_guid`; they MUST NOT move it.

The identifier provides diagnostic correlation only. It conveys no identity,
credential proof, capability, access grant, or logon session. A client MUST NOT
change an authentication decision because this optional metadata is absent,
malformed, or inconsistent. It treats invalid correlation metadata as
unavailable while retaining the ordinary validation of the reply and its
credential or token result.

An authority MAY omit the field if diagnostic generation or message-size
bounds prevent including it. A client cannot require an identifier before
receiving a reply. Existing decoders skip the appended field through the
body's declared length. This extension introduces no message or enumeration
value and retains public framing version 6.

Authenticated can then append the source-owned credential binding in §2.24.
It follows the optional operation GUID slot, using an explicit empty GUID
field when necessary. These are distinct contracts: correlation is diagnostic
and cannot affect authentication; the credential binding records the specific
proof's reference and revision, without becoming proof or a capability.

## 2.6.9 Initial unpublished layout replacement

The coordinated SSH credential extension replaced the unpublished version-1
layout without retaining older codecs. Current LogonStart capability,
required-method and binding fields, Prompt parameters, and all current
Assertion fields are mandatory, including explicit empty/zero values.
Omitted mandatory fields fail decoding. The compatibility rules above apply
to future extensions of this replacement contract; they do not require
accepting the superseded development layout. See PGSS Logon §2.D.

Version 3 adds the mandatory claim-selection discriminator to AuthenticateStart
and the mandatory coverage discriminator to Authenticated (§2.24). It replaces
the unpublished version-2 public framing contract, without a compatibility
decoder. That development contract is superseded by version 4 below.
The separate principal-source protocol retains its own version.

Version 4 replaces the unpublished version-3 ASCII-only claim-name contract
with the KACS Unicode name rule (§2.24). Message body layouts are unchanged.
The framing version MUST still change: a Unicode-aware consumer MUST NOT
accept an older authority's named absence assertions. No compatibility
decoder is provided for that replacement.

Version 5 adds credential type 3, Mschapv2, and the mandatory length-framed
MSCHAPv2 success-output field in Authenticated (§2.24). The separate
principal-source protocol uses its own version 3 for the corresponding
source assertion output.

Version 6 adds credential type 4, Ntlmv2 (§2.11), and the mandatory
length-framed `ntlmv2_session_key` field in Authenticated (§2.24) and
AccessGranted (§2.9). AccessGranted's `profile` becomes mandatory, since
the new field follows it. Coordinated clients and authority MUST use
version 6. The principal-source protocol uses its own version 4 for the
corresponding source request and assertion fields.
