Skip to main content

Network Stack

Discord-PXE owns the networking path needed by the Discord runtime instead of delegating to an operating-system socket API.

The singleton implementation remains the reference path while the sharded transport moves connection-specific state into explicit contexts.

Primary sources:

Layer model​

NIC / Ethernet
│
▼
ARP / IPv4
│
▼
TCP
│
▼
TLS
│
▼
HTTP / WebSocket
│
▼
Discord Gateway

The project is intentionally explicit about these boundaries because each one has different state and failure behavior.

Shared lower layer, private connection layer​

The sharded TCP source describes the intended split directly:

each active shard gets its own local port and sequence space while sharing the machine's IPv4/ARP layer and gateway IP.

That distinction is important.

The physical machine has one interface and one IP identity, but multiple outbound TCP connections still require independent:

  • local source ports
  • local sequence values
  • remote sequence values
  • retry state
  • callback state
  • receive/transmit counters
  • reset/failure state

Per-shard TCP context​

The TCP workspace stores fields such as:

state
local sequence
remote sequence
retry timer
retry count
local port
remote IP
receive callback
receive callback argument
RX/TX counters
bad packet counter
RST counter
destination MAC

The shard descriptor points to this workspace through SHARD_OFF_TCP_CTX.

Local port derivation​

Each shard derives a source port from its shard ID:

base 50000 + (shard_id & 0x0FFF)

This gives active shards distinct connection identities without requiring a full operating-system ephemeral-port allocator.

Port derivation is a design constraint

Mask-based derivation is simple and deterministic, but it is not equivalent to a general-purpose ephemeral-port allocator. Scaling assumptions still need to respect collision behavior and the local shard model.

Remote endpoint​

The sharded Discord TCP client targets port 443.

The IP layer and route resolution remain machine-level concerns, while TCP connection state remains shard-level.

TCP checksum​

The shard TCP implementation constructs the pseudo-header checksum from:

  • local IPv4 address
  • remote IPv4 address
  • protocol number
  • TCP length
  • TCP header/payload

The checksum routine handles odd payload lengths and folds the accumulator into the final one's-complement result.

This code runs without a kernel networking stack beneath it.

Packet construction​

A send operation constructs:

source port
destination port
local sequence
remote sequence / ACK
header length
flags
window
checksum
urgent pointer
payload

The implementation bounds payload size with SHARD_TCP_MAX_PAYLOAD before copying data into the transmit buffer.

Code example of building a TCP header
; RDI = output TCP header, RBX = per-shard TCP context
mov ax, [rbx + STCP_LOCAL_PORT]
xchg al, ah
mov [rdi + 0], ax

mov ax, SHARD_TCP_REMOTE_PORT
xchg al, ah
mov [rdi + 2], ax

mov eax, [rbx + STCP_LOCAL_SEQ]
bswap eax
mov [rdi + 4], eax

mov eax, [rbx + STCP_REMOTE_SEQ]
bswap eax
mov [rdi + 8], eax

mov byte [rdi + 12], 0x50 ; 20-byte header
mov byte [rdi + 13], dl ; caller-supplied flags
mov word [rdi + 16], 0 ; checksum filled later

This is a compact teaching equivalent of the packet-building path; the real routine also fills window/checksum/payload-related fields.

That protects the fixed packet-building path from oversized caller requests.

Callbacks bind TCP upward​

The TCP workspace stores an RX callback and argument.

Higher layers can therefore bind a specific shard's TCP input to that shard's TLS parser without global dispatch guessing which connection received the bytes.

This is one of the critical conversions away from singleton state.

Connection states​

The sharded TCP state machine includes:

IDLE
WAIT_ARP
SYN_SENT
UP
FAILED
CLOSED

Lifecycle code can inspect the TCP state directly when deciding whether a shard needs backoff/reset.

Why keep the singleton implementation?​

The singleton transport remains valuable because it answers whether a failure is:

network/protocol regression
or
context/refactor regression

Replacing every layer at once would remove that comparison point.

Relationship to reliable transport​

Basic TCP state and reliable retransmission storage are separated.

Connection reset preserves the pointer to the reliable queue allocation, zeros it, and reattaches it to the reset TCP workspace rather than leaking pages on every reconnect.

See TCP Reliability & Flow.

Diagnostics​

Counters and state are intentionally kept in memory structures that can be printed by shard diagnostics.

A useful transport dump can include:

  • connection state
  • source port
  • bytes sent/received
  • retry count
  • RST count
  • last shard error

This is especially important on real hardware where there is no external kernel TCP tooling to inspect the socket for you.

Per-shard TCP source-port derivation​

Context before the block
SymbolMeaning
RBXPointer to the owning shard context.
RAXPointer to the allocated TCP context.
SHARD_OFF_IDOffset of the global shard ID inside the shard descriptor.
SHARD_TCP_PORT_BASEBase source port, currently 50000.
SHARD_TCP_PORT_MASK0x0FFF, keeping the low 12 shard-ID bits.
mov edx, [rbx + SHARD_OFF_ID] ; EDX = this shard's global Discord shard ID.
and edx, SHARD_TCP_PORT_MASK ; Keep only the low 12 bits used by local port derivation.
add edx, SHARD_TCP_PORT_BASE ; Translate masked shard ID into the 50000+ source-port range.
mov [rax + STCP_LOCAL_PORT], edx; Store the resulting source port in this shard's TCP context.
Success condition

After this block, the TCP context owns a deterministic source port derived from the shard ID. Two correctly isolated low-numbered local shards should therefore not report the same source port.

State after the block​

STCP_LOCAL_PORT is initialized; no packet has been sent yet, and TCP state is still independent of TLS/WebSocket/Gateway state.

TCP checksum finalization​

The TCP checksum path folds a wider accumulator into a 16-bit one's-complement value, then converts it to wire byte order.

Register context
RegisterMeaning at entry to the fold
EBXRunning checksum accumulator after pseudo-header and TCP bytes have been added.
EAXScratch register used to carry the high 16 bits during each fold.
AXFinal 16-bit checksum returned in network byte order.
.fold:
mov eax, ebx ; Copy the 32-bit accumulator so its high half can be extracted.
shr eax, 16 ; EAX = high 16 bits from the first accumulation pass.
and ebx, 0xFFFF ; Keep only the low 16 bits in EBX.
add ebx, eax ; Fold high 16 bits into low 16 bits.

mov eax, ebx ; Copy again because the prior add may itself have produced a carry.
shr eax, 16 ; Extract that carry/high half.
and ebx, 0xFFFF ; Retain the low 16 bits again.
add ebx, eax ; Fold the final carry back into the checksum.

mov eax, ebx ; EAX = folded 16-bit sum.
not eax ; One's-complement the folded sum.
and eax, 0xFFFF ; Discard complemented bits above the 16-bit checksum.
xchg al, ah ; Swap bytes so AX is in TCP/network byte order.
Debugging implication

A bug here can masquerade as a completely different subsystem failure: remote hosts may silently discard malformed TCP segments, making TLS appear to stall even though TLS never received a valid stream.

Success condition

The returned AX is ready to be written directly into the TCP checksum field.

Source​