Skip to main content

Wire Protocol

The cairn wire protocol uses CBOR (Concise Binary Object Representation) for all message framing. CBOR provides compact binary encoding with a self-describing structure, making it efficient for constrained peer-to-peer connections while remaining easy to parse across all supported languages.

Frame Format

Every message on the wire follows the same envelope structure:

FieldSizeDescription
Version1 byteProtocol version (currently 0x01)
Message Type2 bytesUnsigned 16-bit type code (big-endian)
Session ID16 bytesUUID identifying the session
Sequence Number8 bytesUnsigned 64-bit monotonic counter (big-endian)
Payload Length4 bytesUnsigned 32-bit length of the payload (big-endian)
PayloadvariableType-specific CBOR-encoded body

The envelope is binary, not CBOR-encoded itself, to keep framing overhead minimal. The payload within the envelope is CBOR-encoded and type-specific.

Message Types

Handshake Messages (0x01xx)

Used during the Noise XX handshake to establish a secure session:

Type CodeNameDirectionDescription
0x0100HandshakeInitInitiator -> ResponderFirst Noise XX message (-> e)
0x0101HandshakeResponseResponder -> InitiatorSecond Noise XX message (<- e, ee, s, es)
0x0102HandshakeFinalInitiator -> ResponderThird Noise XX message (-> s, se)

Data Messages (0x03xx)

Carry encrypted application data after the handshake completes:

Type CodeNameDescription
0x0300DataEncrypted application payload on a named channel
0x0303ChannelInitOpens a new named channel on the session

Data messages are encrypted using the session keys derived from the Noise handshake, then further protected by the Double Ratchet.

Control Messages (0x02xx)

Manage session lifecycle:

Type CodeNameDescription
0x0200KeepaliveHeartbeat to detect connection liveness
0x0201CloseGraceful session teardown
0x0202ReconnectResume a suspended session

Signaling Messages (0x04xx)

Used for peer discovery and relay coordination:

Type CodeNameDescription
0x0400PeerAnnounceAnnounce presence to signaling server
0x0401RelayRequestRequest a TURN relay allocation
0x0402RelayResponseRelay allocation result

Custom Messages (0xF000--0xFFFF)

The application range 0xF000--0xFFFF is reserved for user-defined message types. Applications can register handlers for these type codes at the node or session level.

Versioning

Protocol versioning is negotiated during the handshake:

  1. The initiator sends a HandshakeInit with its supported protocol version in the envelope header.
  2. The responder checks compatibility and responds with the agreed version.
  3. If versions are incompatible, the responder closes the connection with an error.

Compatibility guarantees:

  • Minor version bumps are backwards-compatible (new optional fields, new message types).
  • Major version bumps may break compatibility and require both peers to upgrade.
  • Unknown message types are silently ignored, allowing gradual rollout of new features.

Reference

The full protocol specification, including detailed CBOR schemas, encryption layer integration, and edge-case handling, is documented in the internal design documents for contributors. See docs/technical-specification.md in the repository root.