vnet

package
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: AGPL-3.0 Imports: 8 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 vnet is an in-process UDP network with simulated NAT boxes, for testing rendezvous and hole punching without real routers.

A Net is the public internet. Hosts attach to it directly (public address) or behind a NAT that owns one public address and translates for the private hosts on its LAN. Host.Listen returns a net.PacketConn, so real protocol stacks (quic-go) run over the simulation unchanged.

The NAT models follow the classic taxonomy:

  • FullCone: endpoint-independent mapping, no inbound filtering.
  • RestrictedCone: endpoint-independent mapping; inbound allowed only from IP addresses the socket has sent to.
  • PortRestrictedCone: endpoint-independent mapping; inbound allowed only from exact address:port pairs the socket has sent to.
  • Symmetric: a fresh public port per destination address:port, inbound allowed only from that destination. Ports are handed out in sequence (SetPortStep sets the stride, Burn simulates other traffic on the LAN taking some), or at random with SetRandomPorts.
  • ConntrackCone: a port-restricted cone that, like a Linux router whose input chain accepts unsolicited packets, remembers a packet it had no mapping for. When the inside socket later sends to that packet's source, the remembered flow is in the way and the NAT gives this one destination a fresh public port.

Packets carry a TTL. A NAT forwards a packet only if it arrives with a TTL of at least 2 and decrements it; the public internet between any two public addresses counts as a configurable number of routers (SetHops) that do the same. A packet whose TTL runs out is dropped silently.

It is test support: delivery is immediate (plus optional latency and loss), there is no MTU unless SetMTU sets one (then larger packets are dropped silently, as on a path whose ICMP is filtered), and mappings never expire unless NAT.Rebind drops them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

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

Conn is a simulated UDP socket.

func (*Conn) Close

func (c *Conn) Close() error

Close implements net.PacketConn.

func (*Conn) LocalAddr

func (c *Conn) LocalAddr() net.Addr

LocalAddr implements net.PacketConn.

func (*Conn) ReadFrom

func (c *Conn) ReadFrom(p []byte) (int, net.Addr, error)

ReadFrom implements net.PacketConn.

func (*Conn) SetDeadline

func (c *Conn) SetDeadline(t time.Time) error

SetDeadline implements net.PacketConn. Writes never block, so only the read deadline has an effect.

func (*Conn) SetReadBuffer

func (c *Conn) SetReadBuffer(int) error

SetReadBuffer and SetWriteBuffer exist so quic-go does not warn about a socket whose buffers it cannot size.

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(t time.Time) error

SetReadDeadline implements net.PacketConn.

func (*Conn) SetWriteBuffer

func (c *Conn) SetWriteBuffer(int) error

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(time.Time) error

SetWriteDeadline implements net.PacketConn.

func (*Conn) WriteTo

func (c *Conn) WriteTo(p []byte, addr net.Addr) (int, error)

WriteTo implements net.PacketConn.

func (*Conn) WriteToTTL

func (c *Conn) WriteToTTL(p []byte, addr net.Addr, ttl int) (int, error)

WriteToTTL sends one packet with the given TTL.

type Host

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

Host is one machine: a set of UDP sockets on a single address.

func (*Host) Addr

func (h *Host) Addr() netip.Addr

Addr is the host's own (interface) address.

func (*Host) Listen

func (h *Host) Listen(port int) (*Conn, error)

Listen opens a UDP socket on port, or on a free port if port is 0.

func (*Host) MustListen

func (h *Host) MustListen(port int) *Conn

MustListen is Listen for test setup code.

type NAT

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

NAT is one address-translating router.

func (*NAT) Burn

func (b *NAT) Burn(n int)

Burn makes the allocator hand out n public ports without mapping anything, as other traffic from the LAN would have taken them.

func (*NAT) Expire

func (b *NAT) Expire(dst netip.AddrPort)

Expire forgets the mappings towards dst that a Symmetric NAT made for it, as a router does when such a mapping has been idle for its timeout. The next packet to dst gets a new public port.

func (*NAT) Forward

func (b *NAT) Forward(port uint16, inside netip.AddrPort) netip.AddrPort

Forward forwards the NAT's public port to the inside address, as a router's port forward (or one set up by PCP, NAT-PMP or UPnP) does: whatever arrives at the port is let in, whatever the NAT kind, and the inside socket's replies to those senders leave from the port. Its other traffic is translated as before. It returns the public address.

func (*NAT) Host

func (b *NAT) Host(privateIP string) *Host

Host attaches a machine to the NAT's LAN with a private address.

func (*NAT) Rebind

func (b *NAT) Rebind()

Rebind forgets every mapping, as a router does when it reboots or its public address changes: the next packet out of each socket gets a new public port, and packets to the old ones are dropped.

func (*NAT) SetDistance

func (b *NAT) SetDistance(n int)

SetDistance puts n routers between the machines on the NAT's LAN and the NAT itself, as in an office network whose firewall sits behind switches and core routers, or with a carrier-grade NAT some hops into a provider's network: a packet needs a TTL of at least n+2 to be translated at all. The default is 0, a home router.

func (*NAT) SetHairpin

func (b *NAT) SetHairpin(on bool)

SetHairpin controls whether a packet from the LAN addressed to the NAT's own public address is looped back inside. Off by default, like many consumer routers.

func (*NAT) SetIsolation

func (b *NAT) SetIsolation(on bool)

SetIsolation keeps the machines on the NAT's LAN from reaching each other directly, as Wi-Fi client isolation or a guest network does; they still reach the router and through it the internet.

func (*NAT) SetPortStep

func (b *NAT) SetPortStep(step int)

SetPortStep sets how far apart the public ports of successive new mappings are (1 by default; negative counts down), as with a router whose allocator moves on by a fixed stride.

func (*NAT) SetRandomPorts

func (b *NAT) SetRandomPorts(on bool)

SetRandomPorts makes the NAT give each new mapping a random free public port, as NATs that randomise their ports do, instead of the next one in sequence.

type NATKind

type NATKind int

NATKind selects a NAT box's mapping and filtering behaviour.

const (
	FullCone NATKind = iota
	RestrictedCone
	PortRestrictedCone
	Symmetric
	ConntrackCone
)

func (NATKind) String

func (k NATKind) String() string

type Net

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

Net is the simulated public internet.

func New

func New() *Net

New returns an empty network whose public internet is three routers wide.

func (*Net) Host

func (n *Net) Host(ip string) *Host

Host attaches a machine with a public address.

func (*Net) NAT

func (n *Net) NAT(publicIP string, kind NATKind) *NAT

NAT attaches a NAT box with the given public address.

func (*Net) SetHops

func (n *Net) SetHops(hops int)

SetHops sets how many routers a packet crosses between two public addresses. Zero puts every public address on one segment.

func (*Net) SetLatency

func (n *Net) SetLatency(d time.Duration)

SetLatency delays every packet crossing the public internet by d.

func (*Net) SetLoss

func (n *Net) SetLoss(p float64)

SetLoss drops each packet crossing the public internet with probability p.

func (*Net) SetMTU

func (n *Net) SetMTU(mtu int)

SetMTU drops every packet crossing the public internet whose IPv4 packet (UDP payload plus 28 bytes of headers) would exceed mtu. Zero is no limit.

func (*Net) SetTap

func (n *Net) SetTap(f func(src, dst netip.AddrPort, data []byte))

SetTap calls f for every packet that enters the public internet, before TTL, loss or MTU decide its fate, so a test can see what was sent where. f must not block; nil removes the tap.

Jump to

Keyboard shortcuts

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