netplane

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 19 Imported by: 0 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 netplane connects the emulator process to the container networks it creates (Section 3.3). Every container the emulator runs is attached to an internal "services" network on which the emulator's own endpoints (gateway, metadata, DNS, registry, ...) are reachable at the network's gateway address through forwarders, and optionally to an "external" network that provides internet egress (public IPs). On a VM runtime the VM relay (ADR 0005) listens for those endpoints instead, and carries the emulator's dials to container addresses (DialContext).

Index

Constants

This section is empty.

Variables

View Source
var ErrInContainer = errors.New("the Host attachment needs the emulator on the host; it runs in a container")

ErrInContainer is returned when the emulator runs in a container (ADR 0003): its loopback is not the host's, so it has no Host attachment.

Functions

func Contains

func Contains(cidr, ip string) bool

Contains reports whether ip lies inside cidr.

func HostPort

func HostPort(addr string) (string, int)

HostPort splits "ip:port" into its parts with a numeric port.

func ListenUDP added in v0.4.0

func ListenUDP(ctx context.Context, host, port string) (net.PacketConn, error)

ListenUDP binds a UDP socket for DNS: the emulator's server and its forwarder on the services network. Port 5353 is also the mDNS port: avahi-daemon, systemd-resolved and browsers hold UDP 0.0.0.0:5353 with SO_REUSEADDR on most Linux desktops, as mDNSResponder and browsers do on macOS, so a plain bind of 127.0.0.1:5353 (or of the services gateway) fails with "address already in use". Darwin behaves the same as Linux here (checked on macOS 26.6; TestStartBesideMDNS runs on both). On Windows, Chrome and the DNS Client service hold the port the same way and the plain bind fails with WSAEACCES instead. Setting SO_REUSEADDR on our socket lets it coexist; unicast queries to the specific bind address reach our socket while multicast mDNS traffic keeps going to the wildcard sockets. On a wildcard bind we would share (and steal) mDNS traffic, so reuse is only enabled for specific addresses. A wildcard bind is dual-stack ([::]); on macOS, which keeps the IPv4 and IPv6 port spaces apart, it still fails because mDNS daemons hold the port on IPv6 too (mDNSResponder and browsers do). Beside an IPv4-only holder it would bind but receive only IPv6 traffic (issue #262). The TCP listener on the same port is bound first without reuse, so a second emulator instance still fails cleanly.

func Splice added in v0.7.0

func Splice(a, b net.Conn)

Splice copies both ways until both directions end.

Types

type Exposed added in v0.6.0

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

Exposed is a host port that Expose serves.

func (*Exposed) Addr added in v0.6.0

func (e *Exposed) Addr() string

Addr is the host address bound.

func (*Exposed) Close added in v0.6.0

func (e *Exposed) Close() error

Close stops accepting connections; open ones run on.

type Net

type Net struct {
	// Name is the runtime network name.
	Name string
	// Subnet is the IPv4 CIDR.
	Subnet string
	// Gateway is the host-side address on the network.
	Gateway string
	// Bridge is the host interface holding Gateway, as the runtime
	// reports it ("" if it doesn't).
	Bridge string
}

Net describes a container network.

func (Net) PickAddr added in v0.12.0

func (n Net) PickAddr(preferred string) (string, error)

PickAddr chooses a fixed address for a container on n: preferred while it is a host address inside n's subnet, otherwise a random host address in the upper half of the subnet, where it rarely meets the addresses the runtime hands out from the bottom of the range. The runtime may still refuse it as taken; the caller then picks again.

type Plane

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

Plane manages the services and external networks of one instance.

func New

func New(rt *runtime.Manager, endpoints func() map[string]string, bind string, log *slog.Logger) *Plane

New returns a plane. endpoints returns the current host endpoint map (name → host:port), as recorded by emu.Endpoints; bind is the address the Instance binds them on (--bind).

func (*Plane) Addr

func (p *Plane) Addr(ctx context.Context, name string) (string, error)

Addr returns the address at which containers on the services network reach the named emulator endpoint ("gateway", "metadata", "dns", "ar", ...), starting a forwarder on the services gateway, or having the VM relay listen, if needed. The port is the same as the host endpoint's when it is free.

func (*Plane) Close

func (p *Plane) Close(ctx context.Context)

Close stops all forwarders and removes the VM relay, for as long as ctx allows. Networks, and a relay ctx cuts short, are left to runtime cleanup.

func (*Plane) DialContext added in v0.6.0

func (p *Plane) DialContext(ctx context.Context, network, addr string) (net.Conn, error)

DialContext is the ContainerDialer: it connects to an address on one of the Instance's container networks, through the VM relay when there is one and directly otherwise. Addresses elsewhere are always dialed directly.

func (*Plane) Expose added in v0.6.0

func (p *Plane) Expose(name, addr, target string, fallback bool) (*Exposed, error)

Expose binds addr, a loopback ip:port on the host, or a free port on the same IP when addr is taken and fallback is set, and pipes each connection it accepts to target, a container address, through DialContext. On a VM runtime this is how host clients reach a container port that appears while the Instance runs, which the runtime cannot publish then. While target cannot be reached, connections are accepted and closed, and a warning naming name, if set, and target is logged once; without a name, each failure is logged at Debug.

func (*Plane) ExposeConn added in v0.12.0

func (p *Plane) ExposeConn(addr string, fallback bool, dial func(ctx context.Context, c net.Conn) (net.Conn, error)) (*Exposed, error)

ExposeConn binds addr as Expose does and pipes each connection c it accepts to the connection dial opens for it, which may write to it first; dial sets its own timeouts.

func (*Plane) External

func (p *Plane) External(ctx context.Context) (Net, error)

External returns the external (egress-capable) network, creating it on first use.

func (*Plane) HostAddress added in v0.7.0

func (p *Plane) HostAddress(ctx context.Context, n Net) (string, error)

HostAddress returns the Host address of the container network n: the address the VM relay holds on it, joining it first if needed, or n's gateway where the host routes to it.

func (*Plane) Offer added in v0.7.0

func (p *Plane) Offer(ctx context.Context, hostAddr string, port int, udp bool) error

Offer serves port, over UDP when udp and TCP otherwise, at hostAddr (a HostAddress), forwarding to the same port on the host's 127.0.0.1. It is idempotent, and a restarted relay offers it again. Where the host routes to the network, a port that a host process holds on the wildcard address cannot be offered: the error names that bind.

func (*Plane) Relay added in v0.6.0

func (p *Plane) Relay() *vmrelay.Relay

Relay returns the running VM relay, or nil.

func (*Plane) RelayDecision added in v0.6.0

func (p *Plane) RelayDecision() (vmrelay.Decision, bool)

RelayDecision reports whether the Instance uses the VM relay, once the services network exists.

func (*Plane) Reset added in v0.6.0

func (p *Plane) Reset(ctx context.Context) error

Reset removes the VM relay; the next use of the plane starts a new one, which listens for the same endpoints on the same addresses.

func (*Plane) Runtime

func (p *Plane) Runtime() *runtime.Manager

Runtime returns the runtime manager.

func (*Plane) Serve added in v0.6.0

func (p *Plane) Serve(l net.Listener, targets func() []string) *Exposed

Serve pipes each connection l accepts to the first of targets, container addresses, that DialContext reaches; targets is read for each connection. It owns l.

func (*Plane) ServeDial added in v0.7.0

func (p *Plane) ServeDial(l net.Listener, dial func(ctx context.Context) (net.Conn, error)) *Exposed

ServeDial pipes each connection l accepts to the connection dial opens for it; dial sets its own timeouts. It owns l.

func (*Plane) Services

func (p *Plane) Services(ctx context.Context) (Net, error)

Services returns the internal services network, creating it on first use, and starts the VM relay if the Instance uses one.

func (*Plane) Withdraw added in v0.7.0

func (p *Plane) Withdraw(ctx context.Context, hostAddr string, port int, udp bool) error

Withdraw stops serving an Offered port; connections already forwarded run on.

Jump to

Keyboard shortcuts

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