Help us shape the future of Go! Take the 2026 Go Developer Survey

netdriver

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package netdriver is how containers reach a network: a NetworkDriver gives each container an Endpoint, and the engine's WASI host maps the WasmEdge socket functions of wasi_snapshot_preview1 (sock_open, sock_bind, sock_listen, sock_accept, sock_connect, sock_recv_from, sock_send_to, sock_getaddrinfo, ...) onto it.

The contract is the relay's (github.com/go-wasmcontainers/relay, docs/NETWORKDRIVER.md): addresses are the driver's, a connect blocks until the peer accepts or refuses, errors unwrap to the syscall.Errno a socket call returns. Local is an in-engine network with DNS by container name; package relaynet puts containers on a class network through the relay.

Index

Constants

View Source
const (
	// MaxDatagram bounds one datagram (the relay's limit).
	MaxDatagram = 8 << 10
	// DatagramQueue is how many datagrams a socket holds before dropping.
	DatagramQueue = 256
	// Backlog is how many connections wait for Accept before a connect is
	// refused.
	Backlog = 128
	// MaxEndpoints bounds a network's containers.
	MaxEndpoints = 4096
	// MaxSockets bounds the listeners and datagram sockets of one network.
	MaxSockets = 1 << 15
)
View Source
const DNSTTL = 5

DNSTTL is the time to live of every answer, in seconds: addresses are stable while a container is attached, but containers come and go.

View Source
const MaxDNSMessage = 512

MaxDNSMessage bounds a query and an answer: plain DNS over UDP, no EDNS.

View Source
const StreamWindow = 64 << 10

StreamWindow is how many bytes one direction of a local stream holds before its writer blocks (the relay's window is the same).

Variables

View Source
var DefaultPrefix = netip.MustParsePrefix("10.88.0.0/16")

DefaultPrefix is Local's network unless NewLocal is given another.

View Source
var Resolvers = []netip.Addr{
	netip.AddrFrom4([4]byte{127, 0, 0, 11}),
	netip.AddrFrom4([4]byte{127, 0, 0, 1}),
	netip.IPv6Loopback(),
}

Resolvers are the addresses where a container's stub resolver finds the network's DNS: 127.0.0.11 (Docker's embedded DNS) and 127.0.0.1 / ::1, which Go's and musl's resolvers use when /etc/resolv.conf is missing — as it is in most wasm images.

Functions

func Answer

func Answer(ctx context.Context, resolve func(context.Context, string) ([]netip.Addr, error), query []byte) []byte

Answer answers one DNS query (RFC 1035) from resolve: A and AAAA records of a name that resolves, NOERROR with no record for its other types, NXDOMAIN for a name that does not, SERVFAIL when resolve fails otherwise, FORMERR for a malformed query, NOTIMP for an opcode other than QUERY. It returns nil for what is not a query (a response, or too short to carry an ID). Answers fit in MaxDNSMessage bytes, truncated (TC) otherwise.

func ValidName

func ValidName(s string) bool

ValidName reports whether s is a DNS label a container may be attached under: [a-z0-9-]{1,63}, not starting or ending with '-'.

Types

type Endpoint

type Endpoint interface {
	Addr() netip.Addr
	// Listen is a SOCK_STREAM bind+listen ("tcp"); port 0 picks an
	// ephemeral port, which the listener's Addr says.
	Listen(ctx context.Context, network string, port uint16) (net.Listener, error)
	// Dial is a SOCK_STREAM connect ("tcp") or a connected SOCK_DGRAM
	// socket ("udp").
	Dial(ctx context.Context, network string, addr netip.AddrPort) (net.Conn, error)
	// ListenPacket is a SOCK_DGRAM bind ("udp").
	ListenPacket(ctx context.Context, network string, port uint16) (net.PacketConn, error)
	// Resolve is sock_getaddrinfo: a container's name, or an IP literal.
	Resolve(ctx context.Context, name string) ([]netip.Addr, error)
	Detach() error
}

Endpoint is one container's place on a network.

func WithDNS

func WithDNS(ep Endpoint) Endpoint

WithDNS returns ep with a DNS server on its resolver addresses: a UDP socket connected to one of Resolvers, port 53, is answered in the engine from ep.Resolve — A and AAAA records for the network's names, NXDOMAIN for anything else. Nothing is forwarded outside the network. So a stock Go or C program resolves container names with no sock_getaddrinfo. (A container's own UDP server on those addresses, port 53, is shadowed.)

type Local

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

Local is a network inside the engine: each attached container gets an address in Prefix, containers reach each other's listeners by address or by name (Resolve), and 127.0.0.1 is the container itself. Streams are in-memory and flow-controlled (StreamWindow per direction); datagrams are at most MaxDatagram bytes, and a full receive queue drops, as a socket buffer would.

func NewLocal

func NewLocal(prefix netip.Prefix) (*Local, error)

NewLocal returns an empty network on prefix (an IPv4 prefix of at least 2 host bits).

func (*Local) Attach

func (n *Local) Attach(ctx context.Context, container string) (Endpoint, error)

Attach implements NetworkDriver.

func (*Local) Close

func (n *Local) Close() error

Close detaches every container.

func (*Local) Prefix

func (n *Local) Prefix() netip.Prefix

Prefix is the network's address range.

type NetworkDriver

type NetworkDriver interface {
	// Attach gives container (a DNS label: [a-z0-9-]{1,63}) an endpoint.
	// Attaching the same name again returns the same address.
	Attach(ctx context.Context, container string) (Endpoint, error)
	Close() error
}

NetworkDriver gives containers endpoints on a network.

Directories

Path Synopsis
Package relaynet puts containers on a class network through the relay (github.com/go-wasmcontainers/relay): one driver.Session per engine, one endpoint per container, as the relay's docs/NETWORKDRIVER.md describes.
Package relaynet puts containers on a class network through the relay (github.com/go-wasmcontainers/relay): one driver.Session per engine, one endpoint per container, as the relay's docs/NETWORKDRIVER.md describes.

Jump to

Keyboard shortcuts

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