loopback

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package loopback reports whether the host has loopback addresses beyond 127.0.0.1 (ADR 0005), and on macOS whether each is a /32. Linux routes all of 127.0.0.0/8 to loopback; a macOS host has only 127.0.0.1 on lo0 until `gcpemu loopback install` adds aliases for 127.0.0.2 to 127.0.0.254.

Index

Constants

View Source
const DarwinPorts = goruntime.GOOS == "darwin"

DarwinPorts is set on macOS, which lets an unprivileged process bind a port below 1024 only on the wildcard address.

View Source
const Host = "127.0.0.1"

Host is the one loopback address every host has.

View Source
const InstallHint = "sudo gcpemu loopback install"

InstallHint is the command that adds the aliases on a host without them.

View Source
const WideHint = "a /8 loopback alias keeps mDNSResponder at 100% CPU on macOS 27 and slows every DNS lookup"

WideHint names the harm of AliasesWide.

Variables

This section is empty.

Functions

func AddrInUse

func AddrInUse(err error) bool

AddrInUse reports whether err is a bind failing because another socket holds the address.

func Aliases

func Aliases() bool

Aliases reports whether 127.0.0.x addresses other than 127.0.0.1 can be bound, by binding 127.0.0.2:0.

func ExcludedError added in v0.9.0

func ExcludedError(err error, network string) error

ExcludedError wraps err, a bind refused as Excluded, with the command that lists the excluded ranges of network ("tcp" or "udp").

func IsLoopback

func IsLoopback(a net.Addr) bool

IsLoopback reports whether a is a loopback TCP address.

func Listen

func Listen(ip string, port int, inUse bool) (ln net.Listener, addr string, err error)

Listen binds ip:port for host clients. When the port is in a Windows excluded range, it fails. When the host refuses the port as privileged, it binds the wildcard address on the port on macOS, serving only connections to a loopback address, and otherwise (or when another socket holds the wildcard) a free port on ip. When another socket holds ip:port, it binds a free port on ip if inUse is set, and fails otherwise. addr is the address clients use: ip and the port served.

Types

type AliasState added in v0.12.0

type AliasState int

AliasState is what lo0's addresses say about the aliases on macOS.

const (
	// AliasesMissing: lo0 has no 127.0.0.x address other than 127.0.0.1.
	AliasesMissing AliasState = iota
	// AliasesNarrow: every loopback alias is a /32.
	AliasesNarrow
	// AliasesWide: some loopback alias has another netmask. `ifconfig lo0 alias`
	// without one gives the classful /8, and 253 overlapping
	// 127.0.0.0/8s keep mDNSResponder at 100% CPU on macOS 27 (#677).
	AliasesWide
)

func CheckAliases added in v0.12.0

func CheckAliases(addrs []net.Addr) AliasState

CheckAliases reads the aliases from addrs, lo0's addresses.

func Lo0Aliases added in v0.12.0

func Lo0Aliases() AliasState

Lo0Aliases is CheckAliases of the host's lo0, AliasesMissing when lo0 can't be read.

func (AliasState) String added in v0.12.0

func (s AliasState) String() string

type LocalFilter

type LocalFilter struct {
	net.Listener
	Allow func(net.Addr) bool
}

LocalFilter serves only the connections whose local address Allow accepts, and closes the others unserved. It keeps a wildcard listener from serving clients on other hosts.

func (*LocalFilter) Accept

func (f *LocalFilter) Accept() (net.Conn, error)

type PortRange added in v0.9.0

type PortRange struct{ Start, End int }

PortRange is one of Windows' excluded port ranges, Start to End inclusive.

func ExcludedPort added in v0.9.0

func ExcludedPort(ranges []PortRange, port int) (PortRange, bool)

ExcludedPort returns the range of ranges that holds port, if any.

func ExcludedRanges added in v0.9.0

func ExcludedRanges(network string) ([]PortRange, error)

ExcludedRanges reads Windows' excluded port ranges of network ("tcp" or "udp") with netsh.

func ParseExcludedRanges added in v0.9.0

func ParseExcludedRanges(out []byte) []PortRange

ParseExcludedRanges reads the output of `netsh int ipv4 show excludedportrange`: its rows are a start and an end port, and a "*" on administered exclusions. The headings, which Windows translates, are skipped.

func (PortRange) String added in v0.9.0

func (r PortRange) String() string

type Refusal added in v0.9.0

type Refusal int

Refusal is what a bind refused for lack of permission means.

const (
	// NotRefused: the bind succeeded, or failed for another reason.
	NotRefused Refusal = iota
	// Privileged: a privileged port, which only a privileged process
	// binds.
	Privileged
	// PrivilegedWildcard: a privileged port on macOS, which an
	// unprivileged process binds only on the wildcard address.
	PrivilegedWildcard
	// Excluded: a port in one of Windows' excluded port ranges, which
	// Hyper-V, WSL and others reserve. Windows has no privileged ports, and
	// no address or relay binds an excluded one (ADR 0015).
	Excluded
)

func RefusalOn added in v0.9.0

func RefusalOn(goos string) Refusal

RefusalOn is what a bind refused for lack of permission means on goos.

func Refused added in v0.9.0

func Refused(err error) Refusal

Refused reports why the host refused the bind that returned err.

func RefusedOn added in v0.9.0

func RefusedOn(goos string, err error) Refusal

RefusedOn is Refused on goos.

func (Refusal) String added in v0.9.0

func (r Refusal) String() string

Jump to

Keyboard shortcuts

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