native

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: CC0-1.0 Imports: 14 Imported by: 0

Documentation

Overview

Package native provides a native implementation of the gonnect network interfaces based Go's standard net package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Filter is an optional filter function that can reject network operations.
	// It should return true to reject the operation.
	//
	// NOTE: filtering works only for connections establishing, unbinded DNS sockset can be used to bypass it
	Filter gonnect.Filter
	// ResolverCfg configures the DNS resolver used by the Network.
	// If nil, new one will be built.
	ResolverCfg *gonnect.ResolverCfg

	// PreferIP specifies IP version preference:
	// 4 for IPv4, 6 for IPv6, or 0 for no preference.
	PreferIP int

	// ListenCfg configures the listen operations. If nil, defaults are used.
	ListenCfg *net.ListenConfig

	// net.Dialer options
	Timeout         time.Duration
	Deadline        time.Time
	LocalAddr       net.Addr
	FallbackDelay   time.Duration
	KeepAlive       time.Duration
	KeepAliveConfig net.KeepAliveConfig
	Control         func(network, address string, c syscall.RawConn) error
	ControlContext  func(ctx context.Context, network, address string, c syscall.RawConn) error
}

Config holds configuration options for building a Network.

func (Config) Build

func (c Config) Build() *Network

Build creates and returns a new Network instance from the configuration.

type Network

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

Network is a filtered network provider that implements gonnect.Network, gonnect.InterfaceNetwork, and gonnect.Resolver interfaces. It wraps Go's standard net package to provide controlled dialing, listening, and DNS resolution with optional filtering and connection tracking.

func (*Network) Dial

func (n *Network) Dial(
	ctx context.Context,
	network, address string,
) (net.Conn, error)

Dial establishes a connection to the address on the specified network. It applies filtering before dialing and tracks the connection for cleanup. The returned connection is wrapped with callbacks for automatic tracking.

func (*Network) DialTCP

func (n *Network) DialTCP(
	ctx context.Context,
	network, laddr, raddr string,
) (gonnect.TCPConn, error)

DialTCP establishes a TCP connection to the remote address using the specified network. If laddr is not empty, it is used as the local address for the connection. The returned TCPConn is wrapped with callbacks for automatic tracking.

func (*Network) DialUDP

func (n *Network) DialUDP(
	ctx context.Context,
	network, laddr, raddr string,
) (gonnect.UDPConn, error)

DialUDP establishes a UDP connection to the remote address using the specified network. If laddr is not empty, it is used as the local address for the connection. The returned UDPConn is wrapped with callbacks for automatic tracking.

func (*Network) Down

func (n *Network) 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 (*Network) InterfaceAddrs

func (n *Network) InterfaceAddrs() ([]net.Addr, error)

InterfaceAddrs returns the unicast interface addresses associated with the system. This method delegates to net.InterfaceAddrs.

func (*Network) Interfaces

func (n *Network) Interfaces() ([]gonnect.NetworkInterface, error)

Interfaces returns all network interfaces available on the system. This method delegates to net.Interfaces.

func (*Network) InterfacesByIndex

func (n *Network) InterfacesByIndex(
	index int,
) ([]gonnect.NetworkInterface, error)

InterfacesByIndex returns the network interface with the given index. This method delegates to net.InterfaceByIndex.

func (*Network) InterfacesByName

func (n *Network) InterfacesByName(
	name string,
) ([]gonnect.NetworkInterface, error)

InterfacesByName returns the network interface with the given name. This method delegates to net.InterfaceByName.

func (*Network) IsNative added in v0.7.1

func (n *Network) IsNative() bool

func (*Network) IsUp

func (n *Network) IsUp() (bool, error)

IsUp returns whether the network is currently active.

func (*Network) Listen

func (n *Network) Listen(
	ctx context.Context,
	network, address string,
) (net.Listener, error)

Listen announces on the specified network and address. It resolves the address, applies filtering, and creates a listener. The returned listener is wrapped with callbacks for automatic connection tracking.

func (*Network) ListenPacket

func (n *Network) ListenPacket(
	ctx context.Context,
	network, address string,
) (gonnect.PacketConn, error)

ListenPacket announces on the specified network and address for packet-oriented protocols. It resolves the address, applies filtering, and creates a packet connection. The returned PacketConn is wrapped with callbacks for automatic tracking.

func (*Network) ListenPacketConfig added in v0.8.0

func (n *Network) ListenPacketConfig(
	ctx context.Context,
	lc *gonnect.ListenConfig,
	network, address string,
) (gonnect.PacketConn, error)

ListenPacketConfig announces on the specified network and address for packet-oriented protocols using the provided listen configuration. It resolves the address, applies filtering, and creates a packet connection. The returned PacketConn is wrapped with callbacks for automatic tracking.

func (*Network) ListenTCP

func (n *Network) ListenTCP(
	ctx context.Context,
	network, laddr string,
) (gonnect.TCPListener, error)

ListenTCP announces on the specified network and address for TCP connections. It resolves the address, applies filtering, and creates a TCP listener. The returned TCPListener is wrapped with callbacks for automatic connection tracking.

func (*Network) ListenUDP

func (n *Network) ListenUDP(
	ctx context.Context,
	network, laddr string,
) (gonnect.UDPConn, error)

ListenUDP announces on the specified network and address for UDP connections. It resolves the address, applies filtering, and creates a UDP connection. The returned UDPConn is wrapped with callbacks for automatic tracking.

func (*Network) ListenUDPConfig added in v0.8.0

func (n *Network) ListenUDPConfig(
	ctx context.Context,
	lc *gonnect.ListenConfig,
	network, laddr string,
) (gonnect.UDPConn, error)

ListenUDPConfig announces on the specified network and address for UDP connections using the provided listen configuration. Since net.ListenConfig does not expose ListenUDP, this is implemented via ListenPacket and narrowed back to UDPConn.

func (*Network) LookupAddr

func (n *Network) LookupAddr(
	ctx context.Context,
	addr string,
) ([]string, error)

LookupAddr performs a reverse lookup for the given address, returning a slice of names mapping to that address. This method applies filtering before performing the lookup.

func (*Network) LookupCNAME

func (n *Network) LookupCNAME(
	ctx context.Context,
	host string,
) (string, error)

LookupCNAME returns the canonical name for the given host. This method applies filtering before performing the lookup.

func (*Network) LookupHost

func (n *Network) LookupHost(
	ctx context.Context,
	host string,
) ([]string, error)

LookupHost looks up the host and returns a slice of IP address strings. This method applies filtering before performing the lookup.

func (*Network) LookupIP

func (n *Network) LookupIP(
	ctx context.Context,
	network, address string,
) ([]net.IP, error)

LookupIP looks up the host and returns a slice of its IPv4 and IPv6 addresses. The network parameter specifies the network type ("ip", "ip4", or "ip6"). This method applies filtering before performing the lookup.

func (*Network) LookupIPAddr

func (n *Network) LookupIPAddr(
	ctx context.Context,
	host string,
) ([]net.IPAddr, error)

LookupIPAddr looks up the host and returns a slice of IPAddr structures. This method applies filtering before performing the lookup.

func (*Network) LookupMX

func (n *Network) LookupMX(
	ctx context.Context,
	name string,
) ([]*net.MX, error)

LookupMX returns the DNS MX records for the given domain name, sorted by preference. This method applies filtering before performing the lookup.

func (*Network) LookupNS

func (n *Network) LookupNS(
	ctx context.Context,
	name string,
) ([]*net.NS, error)

LookupNS returns the DNS NS records for the given domain name. This method applies filtering before performing the lookup.

func (*Network) LookupNetAddr

func (n *Network) LookupNetAddr(
	ctx context.Context,
	network, addr string,
) (net.IP, int, error)

LookupNetAddr resolves a network address string (e.g., "localhost:8080") into an IP address and port number. The network parameter specifies the network type (e.g., "tcp4", "udp6", "tcp"). This method applies filtering before performing the resolution.

func (*Network) LookupNetIP

func (n *Network) LookupNetIP(
	ctx context.Context,
	network, host string,
) ([]netip.Addr, error)

LookupNetIP looks up the host and returns a slice of netip.Addr values. The network parameter specifies the network type ("ip", "ip4", or "ip6"). This method applies filtering before performing the lookup.

func (*Network) LookupPort

func (n *Network) LookupPort(
	ctx context.Context,
	network, service string,
) (int, error)

LookupPort looks up the port number for the given network and service. This method applies filtering before performing the lookup.

func (*Network) LookupSRV

func (n *Network) LookupSRV(
	ctx context.Context,
	service, proto, name string,
) (string, []*net.SRV, error)

LookupSRV tries to resolve an SRV query for the given service, protocol, and domain name. The proto parameter is "tcp" or "udp". Returns the canonical host name and a slice of SRV records. This method applies filtering before performing the lookup.

func (*Network) LookupTXT

func (n *Network) LookupTXT(
	ctx context.Context,
	name string,
) ([]string, error)

LookupTXT returns the DNS TXT records for the given domain name. This method applies filtering before performing the lookup.

func (*Network) PacketDial added in v0.4.0

func (n *Network) 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 (*Network) Register added in v0.9.0

func (n *Network) Register(id uint64, c io.Closer) error

func (*Network) RegisterConnCallback added in v0.9.0

func (n *Network) RegisterConnCallback(conn net.Conn) (net.Conn, error)

RegisterConnCallback wraps an accepted connection with tracking callbacks. It rejects the connection if the network is down.

func (*Network) RegisterTCPConnCallback added in v0.9.0

func (n *Network) RegisterTCPConnCallback(
	conn gonnect.TCPConn,
) (gonnect.TCPConn, error)

RegisterTCPConnCallback wraps an accepted TCP connection with tracking callbacks. It rejects the connection if the network is down.

func (*Network) Unregister added in v0.9.0

func (n *Network) Unregister(id uint64)

Unregister removes a connection or listener from tracking by ID.

func (*Network) Up

func (n *Network) Up() error

Up re-enables the network after it has been shut down with Down().

Jump to

Keyboard shortcuts

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