protocol

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: BSD-3-Clause Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package protocol defines neutral coordination messages and their validation. It uses only the standard library and performs no network or environment reads.

Index

Examples

Constants

View Source
const ConnectionWindowMillis = 90000

ConnectionWindowMillis is the length, in milliseconds, of the window in which CheckFrame counts a connection's bytes against BudgetLimits.ConnectionBytes.

View Source
const NetmapRequestTimeout = 35 * time.Second

NetmapRequestTimeout bounds a client's netmap request; server holds stay below it.

View Source
const ProtocolRevision = "1"

ProtocolRevision names the revision of the coordination contract. Its value is opaque: a consumer compares it for exact equality with a value it knows and reads no order or meaning into it. A change to the wire shapes, limits or decoder rules in this package, to signaling or discovery behavior, or to the N4 transport (QUIC v1, HTTP/3 with "h3", TLS 1.3 pinned by tls_pin), needs a new value.

Variables

View Source
var ErrInvalid = errors.New("invalid coordination message")

ErrInvalid identifies malformed JSON, keys or invalid coordination fields.

View Source
var ErrTooLarge = errors.New("document too large")

ErrTooLarge identifies a document exceeding its encoded byte limit.

Functions

func NetmapVersion

func NetmapVersion(m Netmap) string

NetmapVersion returns raw base64url SHA-256 of canonical JSON with Version empty. Hosts, regions and endpoints are sorted on copies; m is never mutated.

Types

type Budget

type Budget struct {
	// Limits sets the numbers for the frame being checked. CheckFrame reads it
	// from the Budget it receives, never from the storage Pair or Connection
	// selects, so every Budget passed for one traffic stream carries the same value.
	Limits BudgetLimits
	// Pair selects shared pair state, including across connections; nil uses b itself.
	// Only the pointed-to storage is used, not that value's own Pair selector.
	Pair *Budget
	// Connection selects shared connection state across pairs; nil uses b itself.
	// Only the pointed-to storage is used, not its own Connection selector.
	Connection *Budget
	// ConnectionLimit, when non-nil, returns a byte limit for the connection's
	// window that applies when it is above Limits.ConnectionBytes. CheckFrame
	// calls it only once the window's bytes pass Limits.ConnectionBytes, so a
	// limit that costs work to compute is not paid on every frame. Like Limits, it
	// is read from the Budget CheckFrame receives.
	ConnectionLimit func() int
	// contains filtered or unexported fields
}

Budget holds the pair token buckets and connection byte window for CheckFrame. Its zero value is ready for use; callers serialize access and supply milliseconds from one consistent clock. Retain connection state for one connection's lifetime and pair state for the normalized pair's lifetime, including both directions.

type BudgetLimits

type BudgetLimits struct {
	// PairFrameBurst is the pair frame bucket's capacity, in frames. Default 20.
	PairFrameBurst float64
	// PairFrameRate is the pair frame bucket's refill, in frames per second.
	// Default 5.
	PairFrameRate float64
	// PairByteBurst is the pair byte bucket's capacity, in bytes. It must be at
	// least the largest budgeted frame, 1,317 bytes (a 1,280-byte discovery body,
	// its 32-byte destination key and the 5-byte frame header), or that frame is
	// always refused. Default 25,600.
	PairByteBurst float64
	// PairByteRate is the pair byte bucket's refill, in bytes per second.
	// Default 6,400.
	PairByteRate float64
	// ConnectionBytes is the most bytes one connection may send in a 90-second
	// window, counting every observed frame including refused ones. Default
	// 1,048,576.
	ConnectionBytes int
}

BudgetLimits sets the numbers CheckFrame enforces. A field that is zero or negative uses its default, so the zero value selects every default.

type Confirmation

type Confirmation struct {
	// Peer is the node whose direct pong was received.
	Peer NodeKey `json:"peer"`
	// DiscoKey is that node's discovery key at the pong.
	DiscoKey DiscoKey `json:"disco_key"`
	// Endpoint is the unicast UDP source of the pong, with a nonzero port.
	Endpoint netip.AddrPort `json:"endpoint"`
}

Confirmation binds an observed direct UDP pong to the peer's discovery identity.

func (Confirmation) Validate

func (c Confirmation) Validate() error

Validate returns an error wrapping ErrInvalid for zero peer or discovery keys or an invalid unicast endpoint. It never mutates c.

type DiscoKey

type DiscoKey [32]byte

DiscoKey is a public discovery identity with text "discokey:" and 64 lowercase hex digits. The zero value represents a missing identity and is invalid in messages.

func (DiscoKey) IsZero

func (k DiscoKey) IsZero() bool

IsZero reports whether k is the missing discovery identity.

func (DiscoKey) MarshalText

func (k DiscoKey) MarshalText() ([]byte, error)

MarshalText returns k's canonical text and a nil error.

func (DiscoKey) String

func (k DiscoKey) String() string

String returns the canonical text form of k, including a zero key.

func (*DiscoKey) UnmarshalText

func (k *DiscoKey) UnmarshalText(text []byte) error

UnmarshalText decodes a canonical discovery key. ErrInvalid leaves k unchanged.

type Host

type Host struct {
	// Label is a unique 1-64 byte name using lowercase letters, digits or ._:-.
	Label string `json:"label"`
	// NodeKey is the host's unique, nonzero node identity.
	NodeKey NodeKey `json:"node_key"`
	// DiscoKey is the host's nonzero discovery identity.
	DiscoKey DiscoKey `json:"disco_key"`
	// Endpoints holds at most 64 unicast UDP endpoints.
	Endpoints []netip.AddrPort `json:"endpoints"`
	// HomeRegion selects a listed region, or 0 when Regions is empty.
	HomeRegion int `json:"home_region"`
	// Origin is opaque deployment data; absence means no origin is supplied.
	Origin string `json:"origin,omitempty"`
	// Confirmation is this host's observation of the requesting device, if any.
	Confirmation *Confirmation `json:"confirmation,omitempty"`
}

Host publishes a host's identity and endpoints visible to the requesting device.

type Netmap

type Netmap struct {
	// Version is the NetmapVersion hash of all other fields.
	Version string `json:"version"`
	// LeaseSeconds is in [10,600]; deployment default is 90.
	LeaseSeconds int `json:"lease_seconds"`
	// RefreshSeconds is in [1,LeaseSeconds/3]; deployment default is 30.
	RefreshSeconds int `json:"refresh_seconds"`
	// Regions is optional; none selects the client's BaseURL plus "/signal".
	Regions []Region `json:"regions,omitempty"`
	// Hosts holds at most 256 unique host identities.
	Hosts []Host `json:"hosts"`
}

Netmap grants a bounded lease on a device's visible hosts and signaling regions. Validate checks the wire invariants; the consumer checks its own identity.

func DecodeNetmap

func DecodeNetmap(r io.Reader) (Netmap, error)

DecodeNetmap reads one netmap object, capped at 1 MiB, then validates it. It refuses unknown fields and trailing data. Failures wrap ErrTooLarge, ErrInvalid or an underlying reader error.

func (Netmap) Validate

func (m Netmap) Validate() error

Validate returns ErrInvalid for a malformed netmap; it never mutates m.

type NodeKey

type NodeKey [32]byte

NodeKey is a public node identity. Its text is "nodekey:" and 64 lowercase hex digits. The zero value represents a missing identity and is invalid in messages.

Example

ExampleNodeKey shows the canonical public node-key encoding.

var key NodeKey
key[31] = 1
fmt.Println(key.String())
Output:
nodekey:0000000000000000000000000000000000000000000000000000000000000001

func (NodeKey) IsZero

func (k NodeKey) IsZero() bool

IsZero reports whether k is the missing node identity.

func (NodeKey) MarshalText

func (k NodeKey) MarshalText() ([]byte, error)

MarshalText returns k's canonical text and a nil error.

func (NodeKey) String

func (k NodeKey) String() string

String returns the canonical text form of k, including a zero key.

func (*NodeKey) UnmarshalText

func (k *NodeKey) UnmarshalText(text []byte) error

UnmarshalText decodes a canonical node key. ErrInvalid leaves k unchanged.

type Region

type Region struct {
	// ID is the identifier referenced by a host's HomeRegion.
	ID int `json:"id"`
	// Code is the region's short name.
	Code string `json:"code"`
	// URL is the absolute HTTPS URL of the signaling operation.
	URL string `json:"url"`
}

Region identifies a signaling operation served over HTTPS.

type Registration

type Registration struct {
	// NodeKey identifies the registered node.
	NodeKey NodeKey `json:"node_key"`
	// DiscoKey identifies its discovery key.
	DiscoKey DiscoKey `json:"disco_key"`
	// Endpoints holds at most 64 unicast UDP endpoints.
	Endpoints []netip.AddrPort `json:"endpoints"`
	// Confirmations holds at most 64 distinct peer observations; absent means none.
	Confirmations []Confirmation `json:"confirmations,omitempty"`
}

Registration publishes one node's discovery identity, endpoints and confirmations. Validate checks nonzero keys, endpoint bounds and unique confirmation peers.

func DecodeRegistration

func DecodeRegistration(r io.Reader) (Registration, error)

DecodeRegistration reads one registration object, capped at 64 KiB. It refuses unknown fields and trailing data, then validates the fields. Failures wrap ErrTooLarge, ErrInvalid or an underlying reader error.

func (Registration) Validate

func (r Registration) Validate() error

Validate returns ErrInvalid for a malformed registration; it never mutates r.

type Verdict

type Verdict struct {
	// Action is "accept", "drop" or "close".
	Action string
	// Reason names the rejected rule; it is empty on acceptance.
	Reason string
}

Verdict is the envelope action and its reason; accept has an empty reason.

func CheckFrame

func CheckFrame(frameType byte, body []byte, senderDisco []byte, first bool, clientInfo string, pairLive bool, b *Budget, nowMillis int64) Verdict

CheckFrame checks a decoded client frame and updates b's traffic budgets. senderDisco is the authenticated sender's 32-byte key, first marks the initial frame, clientInfo is decrypted JSON, and pairLive states current authorization. b must be nonnil. nowMillis uses a consistent clock. Malformed frames close; unauthorized pairs and sender mismatches drop. It performs no I/O.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL