Documentation
¶
Index ¶
- Constants
- Variables
- func IntToPublicIPv4(n int) net.IP
- type ConnStats
- type DelayedPacketReciever
- type FixedLatencyRouter
- type LinkSettings
- type NodeBiDiLinkSettings
- type Packet
- type PacketReceiver
- type PerfectRouter
- type Router
- type SimConn
- func (c *SimConn) Close() error
- func (c *SimConn) LocalAddr() net.Addr
- func (c *SimConn) ReadFrom(p []byte) (n int, addr net.Addr, err error)
- func (c *SimConn) RecvPacket(p Packet)
- func (c *SimConn) SetDeadline(t time.Time) error
- func (c *SimConn) SetLocalAddr(addr net.Addr)
- func (c *SimConn) SetReadBuffer(n int) error
- func (c *SimConn) SetReadDeadline(t time.Time) error
- func (c *SimConn) SetWriteBuffer(n int) error
- func (c *SimConn) SetWriteDeadline(t time.Time) error
- func (c *SimConn) Stats() ConnStats
- func (c *SimConn) UnicastAddr() net.Addr
- func (c *SimConn) WriteTo(p []byte, addr net.Addr) (n int, err error)
- type Simnet
- type SimpleFirewallRouter
- func (r *SimpleFirewallRouter) AddNode(addr net.Addr, conn PacketReceiver)
- func (r *SimpleFirewallRouter) RemoveNode(addr net.Addr)
- func (r *SimpleFirewallRouter) SendPacket(p Packet) error
- func (r *SimpleFirewallRouter) SetAddrPubliclyReachable(addr net.Addr)
- func (r *SimpleFirewallRouter) String() string
- type SimulatedLink
Examples ¶
Constants ¶
const Mibps = 1_000_000
Variables ¶
var ErrDeadlineExceeded = errors.New("deadline exceeded")
Functions ¶
func IntToPublicIPv4 ¶
Types ¶
type DelayedPacketReciever ¶
type DelayedPacketReciever struct {
// contains filtered or unexported fields
}
func (*DelayedPacketReciever) RecvPacket ¶
func (r *DelayedPacketReciever) 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 {
// BitsPerSecond specifies the bandwidth limit in bits per second.
// This controls the rate at which data can be transmitted over the link.
BitsPerSecond int
// Latency specifies the network delay to add to each packet.
// This simulates the time it takes for a packet to travel across the network.
Latency time.Duration
// MTU (Maximum Transmission Unit) specifies the maximum packet size in bytes.
// Packets larger than this size will be dropped by the simulated link.
MTU int
}
LinkSettings defines the network characteristics for a simulated link direction. These settings control bandwidth, latency, and MTU for either uplink or downlink traffic.
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
}
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 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 ¶
NewBlockingSimConn creates a new simulated connection that blocks if the receive buffer is full. Does not drop packets.
func NewSimConn ¶
NewSimConn creates a new simulated connection that drops packets if the receive buffer is full.
func (*SimConn) RecvPacket ¶
func (*SimConn) SetDeadline ¶
SetDeadline implements net.PacketConn
func (*SimConn) SetLocalAddr ¶
SetLocalAddr only changes what `.LocalAddr()` returns. Packets will still come From the initially configured addr.
func (*SimConn) SetReadBuffer ¶
SetReadBuffer only exists to quell the warning message from quic-go
func (*SimConn) SetReadDeadline ¶
SetReadDeadline implements net.PacketConn
func (*SimConn) SetWriteBuffer ¶
SetReadBuffer only exists to quell the warning message from quic-go
func (*SimConn) SetWriteDeadline ¶
SetWriteDeadline implements net.PacketConn
func (*SimConn) UnicastAddr ¶
type Simnet ¶
type Simnet struct {
// contains filtered or unexported fields
}
Simnet is a simulated network that manages connections between nodes with configurable network conditions.
Example (Echo) ¶
Example showing a simple echo using Simnet and the returned net.PacketConn.
package main
import (
"fmt"
"net"
"time"
"github.com/marcopolo/simnet"
)
func main() {
// Create the simulated network and two endpoints
n := &simnet.Simnet{}
settings := simnet.NodeBiDiLinkSettings{
Downlink: simnet.LinkSettings{BitsPerSecond: 10 * simnet.Mibps, Latency: 5 * time.Millisecond},
Uplink: simnet.LinkSettings{BitsPerSecond: 10 * simnet.Mibps, Latency: 5 * time.Millisecond},
}
addrA := &net.UDPAddr{IP: net.ParseIP("1.0.0.1"), Port: 9001}
addrB := &net.UDPAddr{IP: net.ParseIP("1.0.0.2"), Port: 9002}
client := n.NewEndpoint(addrA, settings)
server := n.NewEndpoint(addrB, settings)
_ = n.Start()
defer n.Close()
// Simple echo server using the returned PacketConn
done := make(chan struct{})
go func() {
defer close(done)
buf := make([]byte, 1024)
server.SetReadDeadline(time.Now().Add(2 * time.Second))
n, src, err := server.ReadFrom(buf)
if err != nil {
return
}
server.WriteTo(append([]byte("echo: "), buf[:n]...), src)
}()
// Client sends a message and waits for the echo response
client.SetReadDeadline(time.Now().Add(2 * time.Second))
_, _ = client.WriteTo([]byte("ping"), addrB)
buf := make([]byte, 1024)
nRead, _, _ := client.ReadFrom(buf)
fmt.Println(string(buf[:nRead]))
<-done
}
Output: echo: ping
func (*Simnet) NewEndpoint ¶
func (n *Simnet) NewEndpoint(addr *net.UDPAddr, linkSettings NodeBiDiLinkSettings) *SimConn
type SimpleFirewallRouter ¶
type SimpleFirewallRouter struct {
// contains filtered or unexported fields
}
func (*SimpleFirewallRouter) AddNode ¶
func (r *SimpleFirewallRouter) AddNode(addr net.Addr, conn PacketReceiver)
func (*SimpleFirewallRouter) RemoveNode ¶
func (r *SimpleFirewallRouter) RemoveNode(addr net.Addr)
func (*SimpleFirewallRouter) SendPacket ¶
func (r *SimpleFirewallRouter) SendPacket(p Packet) error
func (*SimpleFirewallRouter) SetAddrPubliclyReachable ¶
func (r *SimpleFirewallRouter) SetAddrPubliclyReachable(addr net.Addr)
func (*SimpleFirewallRouter) String ¶
func (r *SimpleFirewallRouter) String() string
type SimulatedLink ¶
type SimulatedLink struct {
// Configuration for link characteristics
UplinkSettings LinkSettings // bandwidth, latency, MTU for uplink direction
DownlinkSettings LinkSettings // bandwidth, latency, MTU for downlink direction
// Packet routing interfaces
UploadPacket Router // Handles packets sent out
// contains filtered or unexported fields
}
SimulatedLink simulates a bidirectional network link with configurable bandwidth, latency, and MTU settings for both uplink and downlink directions.
The link provides realistic network behavior by:
- Rate limiting packets based on bandwidth settings
- Adding configurable latency to packet delivery
- Enforcing MTU limits (dropping oversized packets)
- Buffering packets up to the bandwidth-delay product
Usage:
link := &SimulatedLink{
UplinkSettings: LinkSettings{BitsPerSecond: 1000000, Latency: 50*time.Millisecond, MTU: 1400},
DownlinkSettings: LinkSettings{BitsPerSecond: 1000000, Latency: 50*time.Millisecond, MTU: 1400},
UploadPacket: upstream,
DownloadPacket: downstream,
}
link.Start()
defer link.Close()
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()