Documentation
¶
Overview ¶
Package loopback provides an in-memory loopback network implementation that simulates network operations without using actual network sockets. It implements the gonnect.Network, gonnect.InterfaceNetwork, and gonnect.Resolver interfaces, providing TCP and UDP communication between clients within the same process.
Index ¶
- Variables
- func PipeTCP() (client, server gonnect.TCPConn)
- func PipeUDP() (conn1, conn2 gonnect.UDPConn)
- type LoopbackNetwork
- func (ln *LoopbackNetwork) Dial(ctx context.Context, network, address string) (net.Conn, error)
- func (ln *LoopbackNetwork) DialTCP(ctx context.Context, network, laddr, raddr string) (gonnect.TCPConn, error)
- func (ln *LoopbackNetwork) DialUDP(ctx context.Context, network, laddr, raddr string) (gonnect.UDPConn, error)
- func (ln *LoopbackNetwork) Down() error
- func (ln *LoopbackNetwork) InterfaceAddrs() ([]net.Addr, error)
- func (ln *LoopbackNetwork) Interfaces() ([]gonnect.NetworkInterface, error)
- func (ln *LoopbackNetwork) InterfacesByIndex(index int) ([]gonnect.NetworkInterface, error)
- func (ln *LoopbackNetwork) InterfacesByName(name string) ([]gonnect.NetworkInterface, error)
- func (ln *LoopbackNetwork) IsNative() bool
- func (ln *LoopbackNetwork) IsUp() (bool, error)
- func (ln *LoopbackNetwork) Listen(ctx context.Context, network, address string) (net.Listener, error)
- func (ln *LoopbackNetwork) ListenPacket(ctx context.Context, network, address string) (gonnect.PacketConn, error)
- func (ln *LoopbackNetwork) ListenTCP(ctx context.Context, network, laddr string) (gonnect.TCPListener, error)
- func (ln *LoopbackNetwork) ListenUDP(ctx context.Context, network, laddr string) (gonnect.UDPConn, error)
- func (ln *LoopbackNetwork) LookupAddr(ctx context.Context, addr string) (names []string, err error)
- func (ln *LoopbackNetwork) LookupCNAME(ctx context.Context, host string) (cname string, err error)
- func (ln *LoopbackNetwork) LookupHost(ctx context.Context, host string) (addrs []string, err error)
- func (ln *LoopbackNetwork) LookupIP(ctx context.Context, network, address string) (addrs []net.IP, err error)
- func (ln *LoopbackNetwork) LookupIPAddr(ctx context.Context, host string) (addrs []net.IPAddr, err error)
- func (ln *LoopbackNetwork) LookupMX(ctx context.Context, name string) ([]*net.MX, error)
- func (ln *LoopbackNetwork) LookupNS(ctx context.Context, name string) ([]*net.NS, error)
- func (ln *LoopbackNetwork) LookupNetIP(ctx context.Context, network, address string) (addrs []netip.Addr, err error)
- func (ln *LoopbackNetwork) LookupPort(ctx context.Context, network, service string) (port int, err error)
- func (ln *LoopbackNetwork) LookupSRV(ctx context.Context, service, proto, name string) (string, []*net.SRV, error)
- func (ln *LoopbackNetwork) LookupTXT(ctx context.Context, name string) ([]string, error)
- func (ln *LoopbackNetwork) PacketDial(ctx context.Context, network, address string) (gonnect.PacketConn, error)
- func (ln *LoopbackNetwork) Up() error
Constants ¶
This section is empty.
Variables ¶
var ErrNetworkDown = &net.OpError{ Op: "network", Net: "down", Err: errors.New("network is down"), }
Functions ¶
func PipeTCP ¶
PipeTCP creates a pair of connected loopbackTCPConn using a buffered in-memory full-duplex transport. The connections communicate directly through an in-memory pipe without using actual network sockets. Both connections have their local and remote addresses set to point to each other. This is analogous to net.Pipe() but includes per-direction buffering.
func PipeUDP ¶
PipeUDP creates a pair of connected loopbackUDPConn that communicate via buffered channels. The connections are set up to send packets to each other directly without using actual network sockets or registry lookup. This is analogous to net.Pipe() but for UDP-style packet communication.
Types ¶
type LoopbackNetwork ¶
type LoopbackNetwork struct {
// contains filtered or unexported fields
}
LoopbackNetwork is an in-memory network implementation that simulates loopback network operations. It provides TCP and UDP communication using buffered in-memory transports for TCP and buffered channels for UDP, all without creating actual network sockets.
func NewLoopbackNetwok ¶
func NewLoopbackNetwok() *LoopbackNetwork
NewLoopbackNetwok creates and returns a new loopback network instance. The returned network provides simulated TCP and UDP communication on IPv4 (127.0.0.1) and IPv6 (::1) loopback addresses.
func (*LoopbackNetwork) Dial ¶
Dial establishes a connection to the address on the specified network. It routes to DialTCP for TCP networks ("tcp", "tcp4", "tcp6") or to DialUDP for UDP networks ("udp", "udp4", "udp6"). Returns an error for unknown network types.
func (*LoopbackNetwork) DialTCP ¶
func (ln *LoopbackNetwork) DialTCP( ctx context.Context, network, laddr, raddr string, ) (gonnect.TCPConn, error)
DialTCP establishes a TCP connection to the remote address using the specified network. It accepts "tcp", "tcp4", or "tcp6" as valid network types. If laddr is not empty, it is used as the local address for the connection. The connection is established using a buffered in-memory transport between the client and server. Returns an error if no listener is bound to the remote address.
func (*LoopbackNetwork) DialUDP ¶
func (ln *LoopbackNetwork) DialUDP( ctx context.Context, network, laddr, raddr string, ) (gonnect.UDPConn, error)
DialUDP establishes a UDP connection to the remote address using the specified network. It accepts "udp", "udp4", or "udp6" as valid network types. If laddr is not empty, it is used as the local address for the connection. The returned UDPConn is an in-memory UDP connection that communicates via buffered channels.
func (*LoopbackNetwork) Down ¶
func (ln *LoopbackNetwork) Down() error
Down shuts down the network by closing all tracked connections and listeners. After calling Down, the network will reject new operations until Up() is called.
func (*LoopbackNetwork) InterfaceAddrs ¶
func (ln *LoopbackNetwork) InterfaceAddrs() ([]net.Addr, error)
InterfaceAddrs returns the unicast interface addresses for the loopback interface. It returns the IPv4 loopback range 127.0.0.0/8 to be permissive.
func (*LoopbackNetwork) Interfaces ¶
func (ln *LoopbackNetwork) Interfaces() ([]gonnect.NetworkInterface, error)
Interfaces returns a slice containing the loopback network interface. It returns a single interface representing "lo" with index 1, MTU 65536, and the net.FlagLoopback and net.FlagUp flags set.
func (*LoopbackNetwork) InterfacesByIndex ¶
func (ln *LoopbackNetwork) InterfacesByIndex( index int, ) ([]gonnect.NetworkInterface, error)
InterfacesByIndex returns the network interface with the given index. It returns the loopback interface only if index is 1, otherwise returns an error indicating the interface was not found.
func (*LoopbackNetwork) InterfacesByName ¶
func (ln *LoopbackNetwork) InterfacesByName( name string, ) ([]gonnect.NetworkInterface, error)
InterfacesByName returns the network interface with the given name. It returns the loopback interface only if name is "lo", otherwise returns an error indicating the interface was not found.
func (*LoopbackNetwork) IsNative ¶ added in v0.7.1
func (ln *LoopbackNetwork) IsNative() bool
func (*LoopbackNetwork) IsUp ¶
func (ln *LoopbackNetwork) IsUp() (bool, error)
IsUp returns whether the network is currently active.
func (*LoopbackNetwork) Listen ¶
func (ln *LoopbackNetwork) Listen( ctx context.Context, network, address string, ) (net.Listener, error)
Listen announces on the specified network and address. It delegates to ListenTCP for TCP-based networks. The returned listener is wrapped with loopback-specific error handling.
func (*LoopbackNetwork) ListenPacket ¶
func (ln *LoopbackNetwork) ListenPacket( ctx context.Context, network, address string, ) (gonnect.PacketConn, error)
ListenPacket announces on the specified network and address for packet-oriented protocols. It delegates to ListenUDP for UDP-based networks. The returned PacketConn is wrapped with loopback-specific error handling.
func (*LoopbackNetwork) ListenTCP ¶
func (ln *LoopbackNetwork) ListenTCP( ctx context.Context, network, laddr string, ) (gonnect.TCPListener, error)
ListenTCP announces on the specified TCP network and address. It accepts "tcp", "tcp4", or "tcp6" as valid network types. The returned TCPListener is an in-memory listener that accepts connections via a buffered in-memory TCP transport.
func (*LoopbackNetwork) ListenUDP ¶
func (ln *LoopbackNetwork) ListenUDP( ctx context.Context, network, laddr string, ) (gonnect.UDPConn, error)
ListenUDP announces on the specified UDP network and address. It accepts "udp", "udp4", or "udp6" as valid network types. The returned UDPConn is an in-memory UDP connection that communicates via buffered channels.
func (*LoopbackNetwork) LookupAddr ¶
func (ln *LoopbackNetwork) LookupAddr( ctx context.Context, addr string, ) (names []string, err error)
LookupAddr performs a reverse lookup for the given address. It returns ["localhost"] for local addresses, or an error for non-local addresses.
func (*LoopbackNetwork) LookupCNAME ¶
func (ln *LoopbackNetwork) LookupCNAME( ctx context.Context, host string, ) (cname string, err error)
LookupCNAME returns an error indicating no CNAME exists for the given host. The loopback network does not support DNS CNAME lookups.
func (*LoopbackNetwork) LookupHost ¶
func (ln *LoopbackNetwork) LookupHost( ctx context.Context, host string, ) (addrs []string, err error)
LookupHost looks up the host and returns a slice of IP address strings. It returns ["127.0.0.1", "::1"] for local hosts, or an error for non-local hosts.
func (*LoopbackNetwork) LookupIP ¶
func (ln *LoopbackNetwork) LookupIP( ctx context.Context, network, address string, ) (addrs []net.IP, err error)
LookupIP looks up the host and returns a slice of net.IP values. The network parameter specifies the IP version: "ip4" returns IPv4 only, "ip6" returns IPv6 only, and other values return both IPv4 and IPv6. Returns an error for non-local addresses.
func (*LoopbackNetwork) LookupIPAddr ¶
func (ln *LoopbackNetwork) LookupIPAddr( ctx context.Context, host string, ) (addrs []net.IPAddr, err error)
LookupIPAddr looks up the host and returns a slice of net.IPAddr values. It returns both IPv4 and IPv6 loopback addresses for local hosts, or an error for non-local hosts.
func (*LoopbackNetwork) LookupMX ¶
LookupMX returns an error indicating no MX records exist for the given name. The loopback network does not support DNS MX record lookups.
func (*LoopbackNetwork) LookupNS ¶
LookupNS returns an error indicating no NS records exist for the given name. The loopback network does not support DNS NS record lookups.
func (*LoopbackNetwork) LookupNetIP ¶
func (ln *LoopbackNetwork) LookupNetIP( ctx context.Context, network, address string, ) (addrs []netip.Addr, err error)
LookupNetIP looks up the host and returns a slice of netip.Addr values. The network parameter specifies the IP version: "ip4" returns IPv4 only, "ip6" returns IPv6 only, and other values return both IPv4 and IPv6. Returns an error for non-local addresses.
func (*LoopbackNetwork) LookupPort ¶
func (ln *LoopbackNetwork) LookupPort( ctx context.Context, network, service string, ) (port int, err error)
LookupPort looks up the port number for the given network and service. It delegates to gonnect.LookupPortOffline for offline port resolution.
func (*LoopbackNetwork) LookupSRV ¶
func (ln *LoopbackNetwork) LookupSRV( ctx context.Context, service, proto, name string, ) (string, []*net.SRV, error)
LookupSRV returns an error indicating no SRV records exist for the given service. The loopback network does not support DNS SRV record lookups.
func (*LoopbackNetwork) LookupTXT ¶
LookupTXT returns an empty slice for local addresses, or an error for non-local addresses. The loopback network does not support DNS TXT record lookups for external hosts.
func (*LoopbackNetwork) PacketDial ¶ added in v0.4.0
func (ln *LoopbackNetwork) PacketDial( ctx context.Context, network, address string, ) (gonnect.PacketConn, error)
PacketDial establishes a UDP connection to the remote address using the specified network. The returned PacketConn is wrapped with callbacks for automatic tracking.
func (*LoopbackNetwork) Up ¶
func (ln *LoopbackNetwork) Up() error
Up re-enables the network after it has been shut down with Down().