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 ¶
- type Conn
- func (c *Conn) Close() error
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) ReadFrom(p []byte) (int, net.Addr, error)
- func (c *Conn) SetDeadline(t time.Time) error
- func (c *Conn) SetReadBuffer(int) error
- func (c *Conn) SetReadDeadline(t time.Time) error
- func (c *Conn) SetWriteBuffer(int) error
- func (c *Conn) SetWriteDeadline(time.Time) error
- func (c *Conn) WriteTo(p []byte, addr net.Addr) (int, error)
- func (c *Conn) WriteToTTL(p []byte, addr net.Addr, ttl int) (int, error)
- type Host
- type NAT
- func (b *NAT) Burn(n int)
- func (b *NAT) Expire(dst netip.AddrPort)
- func (b *NAT) Forward(port uint16, inside netip.AddrPort) netip.AddrPort
- func (b *NAT) Host(privateIP string) *Host
- func (b *NAT) Rebind()
- func (b *NAT) SetDistance(n int)
- func (b *NAT) SetHairpin(on bool)
- func (b *NAT) SetIsolation(on bool)
- func (b *NAT) SetPortStep(step int)
- func (b *NAT) SetRandomPorts(on bool)
- type NATKind
- type Net
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) SetDeadline ¶
SetDeadline implements net.PacketConn. Writes never block, so only the read deadline has an effect.
func (*Conn) SetReadBuffer ¶
SetReadBuffer and SetWriteBuffer exist so quic-go does not warn about a socket whose buffers it cannot size.
func (*Conn) SetReadDeadline ¶
SetReadDeadline implements net.PacketConn.
func (*Conn) SetWriteBuffer ¶
func (*Conn) SetWriteDeadline ¶
SetWriteDeadline implements net.PacketConn.
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) MustListen ¶
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 ¶
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 ¶
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 ¶
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) 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 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) SetHops ¶
SetHops sets how many routers a packet crosses between two public addresses. Zero puts every public address on one segment.
func (*Net) SetLatency ¶
SetLatency delays every packet crossing the public internet by d.