ETL-RUDP, reliable delivery over UDP, protocol version 1.
Written from scratch. The techniques are public and documented for decades, but no line comes from SLikeNet, RakNet or any other code base: both are licensed with attribution clauses, and this library is sold.
This block is the specification, and it comes before the code on purpose. Once two programs speak a wire format, it cannot change without breaking one of them. It includes fields the implementation does not use yet, reserved now so that using them later is not a break.
1. Every datagram starts with the same 8 byte header: 0..1 signature, 0xE7 0x4C 2 protocol version (1) 3 datagram type 4..7 connection id, 32 bits little-endian
The signature exists to discard cheaply. An open UDP port gets scans, stray packets of other protocols and garbage. Checking two bytes first avoids spending work on them.
The connection id is drawn by the server in the handshake and is not predictable. It does two jobs: it tells a reconnection from the same address and port apart from an old connection that has not died yet, and it forces anyone who wants to inject a packet into someone else's conversation to guess 32 bits.
2. Datagram types: 0x01 REQUEST client -> server 0x02 CHALLENGE server -> client 0x03 RESPONSE client -> server 0x04 ACCEPTED server -> client 0x05 REJECTED server -> client 0x06 DISCONNECT either direction 0x07 DATA either direction 0x08 PING either direction 0x09 PONG reply to a ping
3. The handshake has four steps: client server | --- REQUEST (version, nonce) ---> | stores nothing | <-- CHALLENGE (cookie) ---------- | cookie = hash(secret, ip, port, epoch) | --- RESPONSE (cookie back) -----> | checks the cookie, only then allocates | <-- ACCEPTED (connection id) ---- |
Three steps would be enough to connect. The fourth exists for one reason: the server cannot allocate state because of a single packet. The UDP source address is not verified, so an attacker forges thousands of addresses, sends one request from each, and the server ends up with thousands of half open connections from machines that do not exist.
The cookie is derived from the source address and from a secret only the server knows. Whoever is not really at that address does not receive the challenge and cannot send it back. The server stores nothing between challenge and response: the cookie itself is the memory. Same reasoning as the TCP SYN cookies.
The epoch in the cookie is a counter that moves from time to time. Cookies of the current and of the previous epoch are accepted, older ones are not. That gives the challenge a validity without storing when it was issued.
4. Data datagram: 8..9 sequence of this datagram, 16 bits 10..11 last sequence received from the other side, 16 bits 12..15 bit map of the 32 sequences before that one 16.. one or more message blocks, up to the end
The sequence belongs to the datagram, not to the message, and is only used for acknowledgement.
The acknowledgement is a bit map, not a cumulative number. In UDP packets are lost out of order: receiving 5 and 7 without 6 is routine. A cumulative number could only say "received up to 5" and 7 would be lost from view. The map acknowledges 33 datagrams at once, and since every new datagram repeats the map, losing an acknowledgement packet costs no retransmission: the next one carries the same information.
Bit N of the map is sequence (last - 1 - N).
The sequence wraps at 65536, and comparing with '<' is wrong: right after the wrap, 0 is newer than 65535. The correct comparison uses the signed difference, in SequenceGreaterThan.
5. Message block: 0 flags bits 0..2 delivery mode (EDelivery) bit 3 fragmented message bits 4..7 reserved, must be zero 1 channel 2..3 message id, 16 bits [if fragmented] 4..5 fragment group id, 16 bits 6..7 index of this fragment, 16 bits 8..9 total fragments, 16 bits n..n+1 payload length, 16 bits ... payload
The message id is present in every mode, including the unreliable one, where it is not used. It costs 2 bytes per message. A variable size header per mode would save those bytes and complicate the parser on the hottest path of the protocol. The choice is simplicity, and it is written here so it can be reviewed with numbers in hand.
Several messages fit in one datagram. That makes twenty small updates per frame cheap: one syscall instead of twenty, and one 16 byte header instead of twenty.
6. Delivery modes: 0 UNRELIABLE send and forget. Can vanish, arrive out of order or twice. 1 UNRELIABLE SEQUENCED send and forget, but the receiver drops anything older than what it already delivered. For state that is replaced: position, health, score. 2 RELIABLE retransmits until acknowledged. Arrives, order not guaranteed. 3 RELIABLE ORDERED retransmits and delivers in order. What arrives early waits for what is missing. 4 RELIABLE SEQUENCED retransmits and delivers only the newest. An old value that finally arrived no longer matters.
Order is per channel. Without that, an ordered file transfer holds the whole conversation behind it, the classic head of line blocking. Separate channels are separate queues.
7. Fragmentation: a message larger than the useful payload of a datagram is split into fragments, which are always treated as reliable. With one missing the whole message does not exist, and retransmitting what is missing is cheaper than sending everything again.
The receiver keeps the fragments of a group until it is complete. There is a ceiling of open groups and a deadline for each one, otherwise a malicious peer sends fragment 1 of 65535 different groups and the receiver keeps them all.
8. Retransmission: a reliable datagram is kept until its acknowledgement arrives. If the deadline passes first, the reliable content it carried is sent again in a new datagram, with a new sequence. The sequence identifies the datagram, and reusing it would confuse the acknowledgement.
The deadline follows RFC 6298: a mean of the round trip time and its variation are kept, and the deadline is 'mean + 4 x variation', clamped to a minimum and a maximum. A fixed deadline does not work: 100 ms is too short on a satellite link and too long on a local network, and being wrong either way is expensive.
The retransmission uses a new sequence, and that removes the need for Karn's algorithm. Karn exists for protocols that reuse the number on retransmission: there the acknowledgement can answer the original or the copy, and the way out is to discard the sample. Here the two datagrams have different numbers, so the acknowledgement names one of them without ambiguity and the sample is always valid.
On each retransmission of the same content the deadline doubles, up to the ceiling. Retransmitting at a fixed interval during congestion is exactly what prolongs it.
9. Not implemented yet, and reserved in the format:
- Real congestion control. What exists is a ceiling of unacknowledged datagrams. A window that grows and shrinks with loss is the next step and does not change the format.
- MTU discovery. The useful size is configured, with a default that crosses any path.
- NAT traversal.
- Encryption. The hook is the ITransform of TCP, applied to the datagram payload after the 8 byte header, which must stay in clear so the receiver knows which connection it is before decrypting.
Nothing here is guaranteed to be thread safe. See CRudpPeer.