protocol

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2026 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FlagSYN uint8 = 0x1
	FlagACK uint8 = 0x2
	FlagFIN uint8 = 0x4
	FlagRST uint8 = 0x8
)

Flags (4 bits, stored in lower nibble of first byte alongside version)

View Source
const (
	ProtoStream   uint8 = 0x01 // Reliable, ordered (TCP-like)
	ProtoDatagram uint8 = 0x02 // Unreliable, unordered (UDP-like)
	ProtoControl  uint8 = 0x03 // Internal control
)

Protocol types

View Source
const (
	PortPing         uint16 = 0
	PortControl      uint16 = 1
	PortEcho         uint16 = 7
	PortNameserver   uint16 = 53
	PortHTTP         uint16 = 80
	PortSecure       uint16 = 443
	PortStdIO        uint16 = 1000
	PortDataExchange uint16 = 1001
	PortEventStream  uint16 = 1002
)

Well-known ports

View Source
const (
	PortReservedMax   uint16 = 1023
	PortRegisteredMax uint16 = 49151
	PortEphemeralMin  uint16 = 49152
	PortEphemeralMax  uint16 = 65535
)

Port ranges

View Source
const (
	BeaconMsgDiscover      byte = 0x01
	BeaconMsgDiscoverReply byte = 0x02
	BeaconMsgPunchRequest  byte = 0x03
	BeaconMsgPunchCommand  byte = 0x04
	BeaconMsgRelay         byte = 0x05
	BeaconMsgRelayDeliver  byte = 0x06
	BeaconMsgSync          byte = 0x07 // gossip: beacon-to-beacon node list exchange
)

Beacon message types (single-byte codes, all < 0x10 to avoid collision with tunnel magic)

View Source
const AddrSize = 6 // 48 bits: 2 bytes network + 4 bytes node
View Source
const PortHandshake uint16 = 444

Well-known port for handshake requests

View Source
const Version uint8 = 1

Protocol version

Variables

View Source
var (
	AddrZero       = Addr{0, 0}
	AddrRegistry   = Addr{0, 1}
	AddrBeacon     = Addr{0, 2}
	AddrNameserver = Addr{0, 3}
)
View Source
var (
	ErrNodeNotFound     = errors.New("node not found")
	ErrNetworkNotFound  = errors.New("network not found")
	ErrConnClosed       = errors.New("connection closed")
	ErrConnRefused      = errors.New("connection refused")
	ErrDialTimeout      = errors.New("dial timeout")
	ErrChecksumMismatch = errors.New("checksum mismatch")
)

Sentinel errors shared across packages.

View Source
var TunnelMagic = [4]byte{0x50, 0x49, 0x4C, 0x54}

Tunnel magic bytes: "PILT" (0x50494C54)

View Source
var TunnelMagicAuthEx = [4]byte{0x50, 0x49, 0x4C, 0x41}

Tunnel magic bytes for authenticated key exchange: "PILA" (0x50494C41)

View Source
var TunnelMagicKeyEx = [4]byte{0x50, 0x49, 0x4C, 0x4B}

Tunnel magic bytes for key exchange: "PILK" (0x50494C4B)

View Source
var TunnelMagicPunch = [4]byte{0x50, 0x49, 0x4C, 0x50}

Tunnel magic bytes for NAT punch packet: "PILP" (0x50494C50)

View Source
var TunnelMagicSecure = [4]byte{0x50, 0x49, 0x4C, 0x53}

Tunnel magic bytes for encrypted packets: "PILS" (0x50494C53)

Functions

func Checksum

func Checksum(data []byte) uint32

Checksum computes CRC32 (IEEE) over the given data.

func PacketHeaderSize

func PacketHeaderSize() int

Types

type Addr

type Addr struct {
	Network uint16
	Node    uint32
}

Addr is a 48-bit Pilot Protocol virtual address. Layout: [16-bit Network ID][32-bit Node ID] Text format: N:NNNN.HHHH.LLLL

N    = network ID in decimal
NNNN = network ID in hex (redundant, for readability)
HHHH = node ID high 16 bits in hex
LLLL = node ID low 16 bits in hex

func BroadcastAddr

func BroadcastAddr(network uint16) Addr

BroadcastAddr returns the broadcast address for a given network.

func ParseAddr

func ParseAddr(s string) (Addr, error)

ParseAddr parses "0:0000.0000.0001" or "1:00A3.F291.0004" into an Addr.

func UnmarshalAddr

func UnmarshalAddr(buf []byte) Addr

UnmarshalAddr reads a 6-byte address from buf.

func (Addr) IsBroadcast

func (a Addr) IsBroadcast() bool

func (Addr) IsZero

func (a Addr) IsZero() bool

func (Addr) Marshal

func (a Addr) Marshal() []byte

Marshal writes the address as 6 bytes (big-endian).

func (Addr) MarshalTo

func (a Addr) MarshalTo(buf []byte, offset int)

MarshalTo writes the address into buf at the given offset.

func (Addr) String

func (a Addr) String() string

String returns the text representation: N:NNNN.HHHH.LLLL

type Packet

type Packet struct {
	Version  uint8
	Flags    uint8
	Protocol uint8

	Src     Addr
	Dst     Addr
	SrcPort uint16
	DstPort uint16

	Seq    uint32
	Ack    uint32
	Window uint16 // advertised receive window (in segments; 0 = no limit)

	Payload []byte
}

func Unmarshal

func Unmarshal(data []byte) (*Packet, error)

Unmarshal deserializes a packet from wire bytes.

func (*Packet) ClearFlag

func (p *Packet) ClearFlag(f uint8)

func (*Packet) HasFlag

func (p *Packet) HasFlag(f uint8) bool

func (*Packet) Marshal

func (p *Packet) Marshal() ([]byte, error)

Marshal serializes the packet to wire format with checksum.

func (*Packet) SetFlag

func (p *Packet) SetFlag(f uint8)

type SocketAddr

type SocketAddr struct {
	Addr Addr
	Port uint16
}

SocketAddr is a full endpoint: virtual address + port.

func ParseSocketAddr

func ParseSocketAddr(s string) (SocketAddr, error)

ParseSocketAddr parses "N:XXXX.YYYY.YYYY:PORT".

func (SocketAddr) String

func (sa SocketAddr) String() string

Jump to

Keyboard shortcuts

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