xnet

package
v0.0.0-...-c06bd77 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package xnet provides high-level network helpers on top of Go stdlib.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoUsableAddress = errors.New("xnet: no usable local address")

ErrNoUsableAddress indicates no address matches requested selection policy.

Functions

func ListLocalAddrs

func ListLocalAddrs(ctx context.Context, opts LocalAddrOptions) ([]netip.Addr, error)

ListLocalAddrs returns unique local addresses filtered and sorted by options.

func ListenTCPRandom

func ListenTCPRandom(
	ctx context.Context,
	opts ListenOptions,
) (net.Listener, netip.AddrPort, error)

ListenTCPRandom listens on a random TCP port and returns an open listener.

func SelectLocalAddr

func SelectLocalAddr(ctx context.Context, opts LocalAddrOptions) (netip.Addr, error)

SelectLocalAddr selects one local address according to LocalAddrOptions.

Types

type Family

type Family uint8

Family controls IP family filtering for local address selection.

const (
	// FamilyAny allows both IPv4 and IPv6.
	FamilyAny Family = iota
	// FamilyIPv4 allows only IPv4 addresses.
	FamilyIPv4
	// FamilyIPv6 allows only IPv6 addresses.
	FamilyIPv6
)

type ListenOptions

type ListenOptions struct {
	// Network must be "", "tcp", "tcp4", or "tcp6".
	Network string
	// Host defaults to 127.0.0.1 for tcp/tcp4 and ::1 for tcp6.
	Host string
}

ListenOptions controls random TCP listen behavior.

type LocalAddrOptions

type LocalAddrOptions struct {
	// Family filters address family. Zero value means FamilyAny.
	Family Family
	// PreferPublic prefers public global unicast addresses when true.
	PreferPublic bool
	// ExcludeCGNAT excludes 100.64.0.0/10 from private candidates when true.
	ExcludeCGNAT bool
	// IncludeLoopback includes loopback addresses when true.
	IncludeLoopback bool
	// IncludeLinkLocal includes link-local addresses when true.
	IncludeLinkLocal bool
}

LocalAddrOptions controls local address listing and selection behavior.

Jump to

Keyboard shortcuts

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