From 68e2fa2014e97a7aee4c0f6a6f85f342fe9155d2 Mon Sep 17 00:00:00 2001 From: Simone Basso Date: Sat, 23 Nov 2024 12:56:19 +0100 Subject: [PATCH] refactor(netsim): move most code inside netsim/netstack The top-level package should contain more specific code handling scenarios rather than the netstack stub network. Let's make this possibly by moving the netstack into its own package. --- netsim/aliases.go | 24 ++++++++++++++++++++++++ netsim/{ => link}/link.go | 16 ++++++++-------- netsim/{ => netstack}/addr.go | 2 +- netsim/{ => netstack}/deadline.go | 2 +- netsim/{ => netstack}/dialer.go | 2 +- netsim/{ => netstack}/errno_unix.go | 2 +- netsim/{ => netstack}/errno_windows.go | 2 +- netsim/{ => netstack}/packet.go | 2 +- netsim/{ => netstack}/port.go | 2 +- netsim/{ => netstack}/stack.go | 6 +++--- netsim/{ => netstack}/tcpconn.go | 2 +- netsim/{ => netstack}/tcplistener.go | 2 +- netsim/{ => netstack}/udpconn.go | 2 +- 13 files changed, 45 insertions(+), 21 deletions(-) create mode 100644 netsim/aliases.go rename netsim/{ => link}/link.go (81%) rename netsim/{ => netstack}/addr.go (97%) rename netsim/{ => netstack}/deadline.go (99%) rename netsim/{ => netstack}/dialer.go (98%) rename netsim/{ => netstack}/errno_unix.go (98%) rename netsim/{ => netstack}/errno_windows.go (98%) rename netsim/{ => netstack}/packet.go (96%) rename netsim/{ => netstack}/port.go (99%) rename netsim/{ => netstack}/stack.go (98%) rename netsim/{ => netstack}/tcpconn.go (99%) rename netsim/{ => netstack}/tcplistener.go (98%) rename netsim/{ => netstack}/udpconn.go (97%) diff --git a/netsim/aliases.go b/netsim/aliases.go new file mode 100644 index 0000000..1688e15 --- /dev/null +++ b/netsim/aliases.go @@ -0,0 +1,24 @@ +// +// SPDX-License-Identifier: GPL-3.0-or-later +// +// Aliases +// + +package netsim + +import ( + "github.com/rbmk-project/x/netsim/link" + "github.com/rbmk-project/x/netsim/netstack" +) + +// Stack is an alias for [netstack.Stack]. +type Stack = netstack.Stack + +// Link is an alias for [link.Link]. +type Link = link.Link + +// NewStack is an alias for [netstack.New]. +var NewStack = netstack.New + +// NewLink is an alias for [link.New]. +var NewLink = link.New diff --git a/netsim/link.go b/netsim/link/link.go similarity index 81% rename from netsim/link.go rename to netsim/link/link.go index 0ec2b25..ca70be0 100644 --- a/netsim/link.go +++ b/netsim/link/link.go @@ -1,10 +1,7 @@ -// // SPDX-License-Identifier: GPL-3.0-or-later -// -// Packet and related definitions. -// -package netsim +// Package link models a point-to-point network link. +package link import ( "sync" @@ -15,9 +12,12 @@ import ( // LinkStack is the [*Stack] as seen by a [*Link]. type LinkStack = packet.NetworkDevice +// Packet is the [packet.Packet] alias used by this package. +type Packet = packet.Packet + // Link models a link between two [*Stack] instances. // -// The zero value is not ready to use; construct using [NewLink]. +// The zero value is not ready to use; construct using [New]. type Link struct { // eof unblocks any blocking channel operation. eof chan struct{} @@ -26,10 +26,10 @@ type Link struct { eofOnce sync.Once } -// NewLink creates a new [*Link] using two [*Stack] and +// New creates a new [*Link] using two [*Stack] and // sets up moving packets between the two stacks. Use Close // to shut down background goroutines. -func NewLink(left, right LinkStack) *Link { +func New(left, right LinkStack) *Link { lnk := &Link{ eof: make(chan struct{}), eofOnce: sync.Once{}, diff --git a/netsim/addr.go b/netsim/netstack/addr.go similarity index 97% rename from netsim/addr.go rename to netsim/netstack/addr.go index 146da87..244f549 100644 --- a/netsim/addr.go +++ b/netsim/netstack/addr.go @@ -4,7 +4,7 @@ // net.Addr implementation. // -package netsim +package netstack import ( "net" diff --git a/netsim/deadline.go b/netsim/netstack/deadline.go similarity index 99% rename from netsim/deadline.go rename to netsim/netstack/deadline.go index a93d069..73b590f 100644 --- a/netsim/deadline.go +++ b/netsim/netstack/deadline.go @@ -6,7 +6,7 @@ // Deadline management. // -package netsim +package netstack import ( "sync" diff --git a/netsim/dialer.go b/netsim/netstack/dialer.go similarity index 98% rename from netsim/dialer.go rename to netsim/netstack/dialer.go index 2f89f64..b4e3778 100644 --- a/netsim/dialer.go +++ b/netsim/netstack/dialer.go @@ -4,7 +4,7 @@ // Dialer implementation // -package netsim +package netstack import ( "context" diff --git a/netsim/errno_unix.go b/netsim/netstack/errno_unix.go similarity index 98% rename from netsim/errno_unix.go rename to netsim/netstack/errno_unix.go index 610b0dd..444cd7c 100644 --- a/netsim/errno_unix.go +++ b/netsim/netstack/errno_unix.go @@ -6,7 +6,7 @@ // UNIX errno definitions. // -package netsim +package netstack import "golang.org/x/sys/unix" diff --git a/netsim/errno_windows.go b/netsim/netstack/errno_windows.go similarity index 98% rename from netsim/errno_windows.go rename to netsim/netstack/errno_windows.go index 79d047b..f78d7fa 100644 --- a/netsim/errno_windows.go +++ b/netsim/netstack/errno_windows.go @@ -6,7 +6,7 @@ // Windows errno definitions. // -package netsim +package netstack import "golang.org/x/sys/windows" diff --git a/netsim/packet.go b/netsim/netstack/packet.go similarity index 96% rename from netsim/packet.go rename to netsim/netstack/packet.go index 6014ab5..e26dcd4 100644 --- a/netsim/packet.go +++ b/netsim/netstack/packet.go @@ -4,7 +4,7 @@ // Aliases for Packet and the related definitions. // -package netsim +package netstack import "github.com/rbmk-project/x/netsim/packet" diff --git a/netsim/port.go b/netsim/netstack/port.go similarity index 99% rename from netsim/port.go rename to netsim/netstack/port.go index fd6dbeb..1eb235b 100644 --- a/netsim/port.go +++ b/netsim/netstack/port.go @@ -4,7 +4,7 @@ // TCP/UDP port implementation. // -package netsim +package netstack import ( "fmt" diff --git a/netsim/stack.go b/netsim/netstack/stack.go similarity index 98% rename from netsim/stack.go rename to netsim/netstack/stack.go index 77864ee..c268f3b 100644 --- a/netsim/stack.go +++ b/netsim/netstack/stack.go @@ -4,7 +4,7 @@ // Network stack // -package netsim +package netstack import ( "context" @@ -49,10 +49,10 @@ type Stack struct { ports map[PortAddr]*Port } -// NewStack creates a new [*Stack] instance and starts a +// New creates a new [*Stack] instance and starts a // goroutine demuxing incoming traffic. Remember to invoke // Close to stop any muxing/demuxing goroutine. -func NewStack(addrs ...netip.Addr) *Stack { +func New(addrs ...netip.Addr) *Stack { const ( // firstEphemeralPort is the first ephemeral port // to use according to RFC6335. diff --git a/netsim/tcpconn.go b/netsim/netstack/tcpconn.go similarity index 99% rename from netsim/tcpconn.go rename to netsim/netstack/tcpconn.go index 5ecc9e2..6940dc0 100644 --- a/netsim/tcpconn.go +++ b/netsim/netstack/tcpconn.go @@ -4,7 +4,7 @@ // TCP Conn/PacketConn. // -package netsim +package netstack import ( "bytes" diff --git a/netsim/tcplistener.go b/netsim/netstack/tcplistener.go similarity index 98% rename from netsim/tcplistener.go rename to netsim/netstack/tcplistener.go index 3c42364..6a0f230 100644 --- a/netsim/tcplistener.go +++ b/netsim/netstack/tcplistener.go @@ -4,7 +4,7 @@ // TCP Listener/PacketListener. // -package netsim +package netstack import ( "net" diff --git a/netsim/udpconn.go b/netsim/netstack/udpconn.go similarity index 97% rename from netsim/udpconn.go rename to netsim/netstack/udpconn.go index 8e62575..5a6a130 100644 --- a/netsim/udpconn.go +++ b/netsim/netstack/udpconn.go @@ -4,7 +4,7 @@ // UDP Conn/PacketConn. // -package netsim +package netstack import "net"