Receiving & Parsing
Receiving & Parsing
Header parsing and payload decryption
Parsing KSP streams requires a stateful reader that validates sizes prior to allocating memory, protecting against Denial of Service (DoS) attacks.
State Machine
The packet parser maintains a buffer and processes bytes incrementally over the TCP stream:
[ Read Stream ] ─► (Wait for 48 Bytes) ─► [ Parse Header ]
│
┌────────────────────────────────────────┘
▼
[ Validate Length ] ─► (Payload > Max? -> ProtocolError)
▼
[ Allocate Buffer ] ─► (Read Payload + 16-byte Tag)
▼
[ Decrypt & Verify Tag ] ─► (Fail? -> Silent Discard / Terminate)
▼
[ Check Sequence ] ─► (Duplicate? -> Discard) ──► [ Deliver to Stream Buffer ]Defensive Memory Allocation
A malicious client could send a forged header with Payload Length set to 16 MB, causing the server to pre-allocate massive buffers before receiving data. Implementations MUST limit maximum frame size buffers (e.g. 64 KB limit for standard messages) and stream data directly to temporary files or circular queues if processing files.