zerocopy

package
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2025 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package zerocopy defines interfaces and helper functions for zero-copy read/write operations.

Index

Constants

View Source
const (
	IPv4HeaderLength = 20
	IPv6HeaderLength = 40
	UDPHeaderLength  = 8

	// Next Header + Hdr Ext Len + Option Type + Opt Data Len + Jumbo Payload Length (u32be)
	JumboPayloadOptionLength = 8
)

Used in packet size calculations.

Variables

View Source
var (
	ErrPacketTooSmall = errors.New("packet too small to unpack")
	ErrPayloadTooBig  = errors.New("payload too big to pack")
)

Functions

func ClientServerPackerUnpackerTestFunc added in v1.4.0

func ClientServerPackerUnpackerTestFunc(t tester, clientPacker ClientPacker, clientUnpacker ClientUnpacker, serverPacker ServerPacker, serverUnpacker ServerUnpacker)

ClientServerPackerUnpackerTestFunc tests the client and server following these steps: 1. Client packer packs. 2. Server unpacker unpacks. 3. Server packer packs. 4. Client unpacker unpacks.

func MaxPacketSizeForAddr

func MaxPacketSizeForAddr(mtu int, addr netip.Addr) int

MaxPacketSizeForAddr calculates the maximum packet size for the given address based on the MTU and the address family.

func NoopClose added in v1.8.0

func NoopClose() error

NoopClose is a no-op close function.

Types

type ClientPackUnpacker added in v1.1.0

type ClientPackUnpacker interface {
	ClientPacker
	ClientUnpacker
}

ClientPackUnpacker implements both ClientPacker and ClientUnpacker interfaces.

type ClientPacker added in v1.1.0

type ClientPacker interface {
	// ClientPackerInfo returns information about the client packer.
	ClientPackerInfo() ClientPackerInfo

	// PackInPlace packs the payload in-place into a packet ready for sending and returns
	// the destination address, packet start offset, packet length, or an error if packing fails.
	PackInPlace(ctx context.Context, b []byte, targetAddr conn.Addr, payloadStart, payloadLen int) (destAddrPort netip.AddrPort, packetStart, packetLen int, err error)
}

ClientPacker processes raw payload into packets ready to be sent to servers.

type ClientPackerInfo added in v1.6.0

type ClientPackerInfo struct {
	Headroom Headroom
}

ClientPackerInfo contains information about a client packer.

type ClientUnpacker added in v1.1.0

type ClientUnpacker interface {
	// ClientUnpackerInfo returns information about the client unpacker.
	ClientUnpackerInfo() ClientUnpackerInfo

	// UnpackInPlace unpacks the packet in-place and returns packet source address, payload start offset, payload length, or an error if unpacking fails.
	UnpackInPlace(b []byte, packetSourceAddrPort netip.AddrPort, packetStart, packetLen int) (payloadSourceAddrPort netip.AddrPort, payloadStart, payloadLen int, err error)
}

ClientUnpacker processes packets received from the server into raw payload.

type ClientUnpackerInfo added in v1.6.0

type ClientUnpackerInfo struct {
	Headroom Headroom
}

ClientUnpackerInfo contains information about a client unpacker.

type Headroom

type Headroom struct {
	// Front is the minimum space required at the beginning of the buffer before payload.
	Front int

	// Rear is the minimum space required at the end of the buffer after payload.
	Rear int
}

Headroom reports the amount of extra space required in read/write buffers besides the payload.

func MaxHeadroom added in v1.6.0

func MaxHeadroom(first, second Headroom) Headroom

MaxHeadroom returns the maximum front and rear headroom of the two headroom pairs.

func UDPRelayHeadroom added in v1.6.0

func UDPRelayHeadroom(packerHeadroom, unpackerHeadroom Headroom) Headroom

UDPRelayHeadroom returns the packer headroom subtracted by the unpacker headroom.

type ServerPackUnpacker added in v1.1.0

type ServerPackUnpacker interface {
	ServerPacker
	ServerUnpacker
}

ServerPackUnpacker implements both ServerPacker and ServerUnpacker interfaces.

type ServerPacker added in v1.1.0

type ServerPacker interface {
	// ServerPackerInfo returns information about the server packer.
	ServerPackerInfo() ServerPackerInfo

	// PackInPlace packs the payload in-place into a packet ready for sending and returns
	// packet start offset, packet length, or an error if packing fails.
	PackInPlace(b []byte, sourceAddrPort netip.AddrPort, payloadStart, payloadLen, maxPacketLen int) (packetStart, packetLen int, err error)
}

ServerPacker processes raw payload into packets ready to be sent to clients.

type ServerPackerInfo added in v1.6.0

type ServerPackerInfo struct {
	Headroom Headroom
}

ServerPackerInfo contains information about a server packer.

type ServerUnpacker added in v1.1.0

type ServerUnpacker interface {
	// ServerUnpackerInfo returns information about the server unpacker.
	ServerUnpackerInfo() ServerUnpackerInfo

	// UnpackInPlace unpacks the packet in-place and returns target address, payload start offset, payload length, or an error if unpacking fails.
	UnpackInPlace(b []byte, sourceAddrPort netip.AddrPort, packetStart, packetLen int) (targetAddr conn.Addr, payloadStart, payloadLen int, err error)

	// NewPacker creates a new server session for the current client session and returns the server session's packer, or an error.
	NewPacker() (ServerPacker, error)
}

ServerUnpacker processes packets received from the client into raw payload.

type ServerUnpackerInfo added in v1.6.0

type ServerUnpackerInfo struct {
	Headroom Headroom
}

ServerUnpackerInfo contains information about a server unpacker.

type UDPClient

type UDPClient interface {
	// Info returns information about the client.
	Info() UDPClientInfo

	// NewSession creates a new client session, and returns the created session or an error.
	// The returned [UDPClientSessionInfo] is always valid, even when session creation fails.
	NewSession(ctx context.Context) (UDPClientSessionInfo, UDPClientSession, error)
}

UDPClient stores information for creating new client sessions.

type UDPClientInfo added in v1.6.0

type UDPClientInfo struct {
	// Name is the name of the UDP client.
	Name string

	// PackerHeadroom is the headroom required by the packet packer.
	PackerHeadroom Headroom
}

UDPClientInfo contains information about a UDP client.

type UDPClientSession added in v1.8.0

type UDPClientSession struct {
	// MaxPacketSize is the maximum size of outgoing packets.
	MaxPacketSize int

	// Packer is the packet packer for the session.
	Packer ClientPacker

	// Unpacker is the packet unpacker for the session.
	Unpacker ClientUnpacker

	// Close closes the session.
	Close func() error
}

UDPClientSession is a client session for handling UDP packets.

type UDPClientSessionInfo added in v1.13.0

type UDPClientSessionInfo struct {
	// Name is the name of the UDP client.
	Name string

	// PackerHeadroom is the headroom required by the packet packer.
	PackerHeadroom Headroom

	// MTU is the MTU of the client's designated network path.
	MTU int

	// ListenConfig is the [conn.ListenConfig] for opening client sockets.
	ListenConfig conn.ListenConfig
}

UDPClientSessionInfo contains information about a UDP client session.

type UDPNATServer added in v1.1.0

type UDPNATServer interface {
	// Info returns information about the server.
	Info() UDPNATServerInfo

	// NewUnpacker creates a new packet unpacker for the session.
	//
	// The returned unpacker is then used by the caller to unpack the incoming packet.
	// Upon successful unpacking, the unpacker's NewPacker method can be called to create
	// a corresponding packet packer.
	NewUnpacker() (ServerUnpacker, error)
}

UDPNATServer stores information for creating new server sessions.

type UDPNATServerInfo added in v1.6.0

type UDPNATServerInfo struct {
	// UnpackerHeadroom is the headroom required by the packet unpacker.
	UnpackerHeadroom Headroom
}

UDPNATServerInfo contains information about a UDP NAT server.

type UDPSessionServer added in v1.1.0

type UDPSessionServer interface {
	// Info returns information about the server.
	Info() UDPSessionServerInfo

	// SessionInfo extracts session ID from a received packet b.
	//
	// The returned session ID is then used by the caller to look up the session table.
	// If no matching entries were found, NewUnpacker should be called to create a new
	// packet unpacker for the packet.
	SessionInfo(b []byte) (csid uint64, err error)

	// NewUnpacker creates a new packet unpacker for the specified client session.
	//
	// The returned unpacker is then used by the caller to unpack the incoming packet.
	// Upon successful unpacking, the unpacker's NewPacker method can be called to create
	// a corresponding server session.
	NewUnpacker(b []byte, csid uint64) (serverUnpacker ServerUnpacker, username string, err error)
}

UDPSessionServer deals with incoming sessions.

type UDPSessionServerInfo added in v1.6.0

type UDPSessionServerInfo struct {
	// UnpackerHeadroom is the headroom required by the packet unpacker.
	UnpackerHeadroom Headroom

	// MinNATTimeout is the server's minimum allowed NAT timeout.
	// 0 means no requirement.
	MinNATTimeout time.Duration
}

UDPSessionServerInfo contains information about a UDP session server.

Jump to

Keyboard shortcuts

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