simnet

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 9 Imported by: 0

README

simnet

This package is based on @MarcoPolo's simnet package.

A small Go library for simulating packet networks in-process. It provides drop-in net.PacketConn endpoints connected through configurable virtual links with latency and MTU constraints. Useful for testing networking code without sockets or root privileges.

  • Drop-in API: implements net.PacketConn
  • Realistic links: per-direction latency and MTU
  • Packet queuing: priority queue for scheduled packet delivery
  • Routers: perfect delivery, fixed-latency, simple firewall/NAT-like routing
  • Deterministic testing: opt-in synctest-based tests for time control

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDeadlineExceeded = errors.New("deadline exceeded")

Functions

This section is empty.

Types

type ConnStats

type ConnStats struct {
	BytesSent   int
	BytesRcvd   int
	PacketsSent int
	PacketsRcvd int
}

type DelayedPacketReceiver

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

func (*DelayedPacketReceiver) RecvPacket

func (r *DelayedPacketReceiver) RecvPacket(p Packet)

type FixedLatencyRouter

type FixedLatencyRouter struct {
	PerfectRouter
	// contains filtered or unexported fields
}

func (*FixedLatencyRouter) AddNode

func (r *FixedLatencyRouter) AddNode(addr net.Addr, conn PacketReceiver)

func (*FixedLatencyRouter) SendPacket

func (r *FixedLatencyRouter) SendPacket(p Packet) error

type LinkSettings

type LinkSettings struct {
	// MTU (Maximum Transmission Unit) specifies the maximum packet size in bytes
	MTU int
}

LinkSettings defines the network characteristics for a simulated link direction

type NodeBiDiLinkSettings

type NodeBiDiLinkSettings struct {
	// Downlink configures the settings for incoming traffic to this node
	Downlink LinkSettings
	// Uplink configures the settings for outgoing traffic from this node
	Uplink LinkSettings

	// Latency specifies a fixed network delay for downlink packets only
	// If both Latency and LatencyFunc are set, LatencyFunc takes precedence
	Latency time.Duration

	// LatencyFunc computes the network delay for each downlink packet
	// This allows variable latency based on packet source/destination
	// If nil, Latency field is used instead
	LatencyFunc func(Packet) time.Duration
}

NodeBiDiLinkSettings defines the bidirectional link settings for a network node. It specifies separate configurations for downlink (incoming) and uplink (outgoing) traffic, allowing asymmetric network conditions to be simulated.

type Packet

type Packet struct {
	To   net.Addr
	From net.Addr

	Data []byte
}

type PacketReceiver

type PacketReceiver interface {
	RecvPacket(p Packet)
}

type PerfectRouter

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

PerfectRouter is a router that has no latency or jitter and can route to every node

func (*PerfectRouter) AddNode

func (r *PerfectRouter) AddNode(addr net.Addr, conn PacketReceiver)

func (*PerfectRouter) RemoveNode

func (r *PerfectRouter) RemoveNode(addr net.Addr)

func (*PerfectRouter) SendPacket

func (r *PerfectRouter) SendPacket(p Packet) error

SendPacket implements Router.

type Router

type Router interface {
	SendPacket(p Packet) error
	AddNode(addr net.Addr, receiver PacketReceiver)
}

Router handles routing of packets between simulated connections. Implementations are responsible for delivering packets to their destinations.

type SimConn

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

SimConn is a simulated network connection that implements net.PacketConn. It provides packet-based communication through a Router for testing and simulation purposes. All send/recv operations are handled through the Router's packet delivery mechanism.

func NewBlockingSimConn

func NewBlockingSimConn(addr *net.UDPAddr, rtr Router) *SimConn

NewBlockingSimConn creates a new simulated connection that blocks if the receive buffer is full. Does not drop packets.

func NewSimConn

func NewSimConn(addr *net.UDPAddr, rtr Router) *SimConn

NewSimConn creates a new simulated connection that drops packets if the receive buffer is full.

func (*SimConn) Close

func (c *SimConn) Close() error

func (*SimConn) LocalAddr

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

func (*SimConn) ReadFrom

func (c *SimConn) ReadFrom(p []byte) (n int, addr net.Addr, err error)

func (*SimConn) RecvPacket

func (c *SimConn) RecvPacket(p Packet)

func (*SimConn) SetDeadline

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

func (*SimConn) SetReadBuffer

func (c *SimConn) SetReadBuffer(n int) error

SetReadBuffer only exists to quell the warning message from quic-go

func (*SimConn) SetReadDeadline

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

func (*SimConn) SetWriteBuffer

func (c *SimConn) SetWriteBuffer(n int) error

SetWriteBuffer only exists to quell the warning message from quic-go

func (*SimConn) SetWriteDeadline

func (c *SimConn) SetWriteDeadline(t time.Time) error

func (*SimConn) Stats

func (c *SimConn) Stats() ConnStats

func (*SimConn) UnicastAddr

func (c *SimConn) UnicastAddr() net.Addr

func (*SimConn) WriteTo

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

type Simnet

type Simnet struct {
	Router Router
	// contains filtered or unexported fields
}

Simnet is a simulated network that manages connections between nodes with configurable network conditions.

func (*Simnet) Close

func (n *Simnet) Close() error

func (*Simnet) NewEndpoint

func (n *Simnet) NewEndpoint(addr *net.UDPAddr, linkSettings NodeBiDiLinkSettings) *SimConn

func (*Simnet) Start

func (n *Simnet) Start() error
type SimulatedLink struct {

	// Configuration for link characteristics
	UplinkSettings   LinkSettings
	DownlinkSettings LinkSettings

	// Latency specifies a fixed network delay for downlink packets
	// If both Latency and LatencyFunc are set, LatencyFunc takes precedence
	Latency time.Duration

	// LatencyFunc computes the network delay for each downlink packet
	// This allows variable latency based on packet source/destination
	// If nil, Latency field is used instead
	LatencyFunc func(Packet) time.Duration

	// Packet routing interfaces
	UploadPacket Router
	// contains filtered or unexported fields
}

SimulatedLink simulates a bidirectional network link with variable latency and MTU constraints

func (*SimulatedLink) AddNode

func (l *SimulatedLink) AddNode(addr net.Addr, receiver PacketReceiver)

func (*SimulatedLink) Close

func (l *SimulatedLink) Close() error

func (*SimulatedLink) RecvPacket

func (l *SimulatedLink) RecvPacket(p Packet)

func (*SimulatedLink) SendPacket

func (l *SimulatedLink) SendPacket(p Packet) error

func (*SimulatedLink) Start

func (l *SimulatedLink) Start()

Jump to

Keyboard shortcuts

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