transport

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: MPL-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const MaxPacketSize = 1200

MaxPacketSize is the UDP payload cap (spec §3.7, IPv6-safe 1200).

Variables

View Source
var (
	// ErrPacketTooLarge is returned when Send payload exceeds MaxPacketSize.
	ErrPacketTooLarge = errors.New("a2al/transport: packet exceeds MaxPacketSize")
	// ErrClosed is returned after the transport is closed.
	ErrClosed = errors.New("a2al/transport: closed")
	// ErrUnknownPeer is returned by MemTransport when the destination is not registered.
	ErrUnknownPeer = errors.New("a2al/transport: unknown mem peer")
	// ErrDuplicatePeer is returned when a MemTransport name is already bound.
	ErrDuplicatePeer = errors.New("a2al/transport: duplicate mem peer name")
	// ErrMemBufferFull is returned when the peer inbox is full (backpressure).
	ErrMemBufferFull = errors.New("a2al/transport: mem peer inbox full")
)

Functions

func ListenDualUDP added in v0.2.6

func ListenDualUDP(addr string) (net.PacketConn, error)

ListenDualUDP opens a UDP socket that handles both IPv4 and IPv6 traffic.

On non-Windows platforms (Linux, macOS, Android, iOS), a single socket bound to [::]:port handles both families: the OS delivers IPv4 packets with an IPv4-mapped source address (::ffff:x.x.x.x), which normalizeUDPAddr unwraps at the UDPMux read boundary.

Address resolution rules (see resolveListenAddr):

  • ":port", "0.0.0.0:port" → single dual-stack socket on [::]:port
  • "1.2.3.4:port" → single IPv4-only socket (explicit bind preserved)
  • "[2001:db8::1]:port" → single IPv6-only socket

Types

type Addr

type Addr struct {
	Name string
}

Addr is a logical in-memory address (net.Addr, Network "mem").

func (Addr) Network

func (a Addr) Network() string

Network implements net.Addr.

func (Addr) String

func (a Addr) String() string

String implements net.Addr.

type MemNetwork

type MemNetwork struct {
	// contains filtered or unexported fields
}

MemNetwork links MemTransport instances for process-local testing (spec §3.4).

func NewMemNetwork

func NewMemNetwork() *MemNetwork

NewMemNetwork creates an empty network.

func (*MemNetwork) NewTransport

func (n *MemNetwork) NewTransport(name string) (*MemTransport, error)

NewTransport registers a peer name and returns a transport bound to it.

type MemTransport

type MemTransport struct {
	// contains filtered or unexported fields
}

MemTransport delivers packets via MemNetwork.

func (*MemTransport) Close

func (t *MemTransport) Close() error

func (*MemTransport) LocalAddr

func (t *MemTransport) LocalAddr() net.Addr

func (*MemTransport) Receive

func (t *MemTransport) Receive() ([]byte, net.Addr, error)

func (*MemTransport) Send

func (t *MemTransport) Send(addr net.Addr, data []byte) error

type MuxDHTTransport

type MuxDHTTransport struct {
	// contains filtered or unexported fields
}

func (*MuxDHTTransport) Close

func (t *MuxDHTTransport) Close() error

func (*MuxDHTTransport) LocalAddr

func (t *MuxDHTTransport) LocalAddr() net.Addr

func (*MuxDHTTransport) Receive

func (t *MuxDHTTransport) Receive() ([]byte, net.Addr, error)

func (*MuxDHTTransport) Send

func (t *MuxDHTTransport) Send(addr net.Addr, data []byte) error

type Transport

type Transport interface {
	Send(addr net.Addr, data []byte) error
	Receive() ([]byte, net.Addr, error)
	LocalAddr() net.Addr
	Close() error
}

Transport is the DHT packet plane (spec §3.3).

type UDPMux

type UDPMux struct {
	// contains filtered or unexported fields
}

UDPMux demultiplexes one UDP socket between DHT (CBOR) and QUIC (RFC 9000).

Discrimination: all QUIC packets (long and short header) have the "Fixed Bit" (0x40) set per RFC 9000 §17. A2AL DHT messages are CBOR maps whose first byte falls in 0xa0-0xbf, where bit 6 is always clear. Checking `data[0]&0x40 != 0` is therefore a reliable, stateless discriminator — no peer tracking needed.

The underlying conn is a net.PacketConn, which may be:

  • *net.UDPConn (IPv4-only, or dual-stack on Unix via [::]:port)
  • *dualPacketConn (Windows: udp4 + udp6 fan-in; see listen_dual_windows.go)

All source addresses are normalised by normalizeUDPAddr in readLoop so that v4-mapped IPv6 addresses (::ffff:x.x.x.x) are always unwrapped to pure IPv4 before reaching DHT or QUIC code.

func NewUDPMux

func NewUDPMux(conn net.PacketConn) *UDPMux

NewUDPMux wraps an existing UDP listener. Call StartReadLoop before use. conn may be a *net.UDPConn or a *dualPacketConn (Windows dual-stack).

func (*UDPMux) Close

func (m *UDPMux) Close() error

func (*UDPMux) DHTTransport

func (m *UDPMux) DHTTransport() *MuxDHTTransport

DHTTransport returns the DHT packet plane.

func (*UDPMux) QUICPacketConn

func (m *UDPMux) QUICPacketConn() net.PacketConn

QUICPacketConn returns a net.PacketConn that only sees QUIC datagrams.

func (*UDPMux) SendPacket added in v0.2.7

func (m *UDPMux) SendPacket(data []byte, addr net.Addr) error

SendPacket writes data to addr on the underlying shared UDP socket. Used by protocols (e.g. STUN) that need to send on the same fd as DHT/QUIC and receive replies via a SetPacketFilter hook.

func (*UDPMux) SetPacketFilter added in v0.2.7

func (m *UDPMux) SetPacketFilter(f func(data []byte, from net.Addr) (consumed bool))

SetPacketFilter registers an optional hook that is called for every incoming packet before DHT/QUIC routing. If the hook returns true, the packet is considered consumed and normal routing is skipped. Passing nil clears the hook. Safe to call at any time; the hook is invoked with only buf[:n] of the shared read buffer — the hook must copy if it needs to retain the data.

func (*UDPMux) StartReadLoop

func (m *UDPMux) StartReadLoop()

func (*UDPMux) WaitReadLoop

func (m *UDPMux) WaitReadLoop()

WaitReadLoop blocks until the read loop exits (after Close).

type UDPTransport

type UDPTransport struct {
	// contains filtered or unexported fields
}

UDPTransport wraps a UDP socket with a max read size (spec §3.7). conn is a net.PacketConn so that both *net.UDPConn (IPv4-only) and dualPacketConn (Windows dual-stack) can be used without code changes.

func ListenUDP

func ListenUDP(network, laddr string) (*UDPTransport, error)

ListenUDP listens on laddr (e.g. "udp4", ":0") and returns a UDPTransport.

func NewUDPTransport

func NewUDPTransport(c net.PacketConn) *UDPTransport

NewUDPTransport wraps an existing UDP connection.

func (*UDPTransport) Close

func (t *UDPTransport) Close() error

func (*UDPTransport) LocalAddr

func (t *UDPTransport) LocalAddr() net.Addr

func (*UDPTransport) Receive

func (t *UDPTransport) Receive() ([]byte, net.Addr, error)

func (*UDPTransport) Send

func (t *UDPTransport) Send(addr net.Addr, data []byte) error

Jump to

Keyboard shortcuts

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