ports

package
v1.19.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package ports finds out whether host ports can be bound and, if not, why and by whom. The CLI uses it to decide how to free the scheduler's host ports on Windows.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExcludedRanges

func ExcludedRanges() ([][2]int, error)

ExcludedRanges is empty off Windows.

func IsWinNATRunning

func IsWinNATRunning() bool

IsWinNATRunning is always false off Windows.

Types

type Conflict

type Conflict struct {
	Port   int
	Status Status
	Err    error
	Holder Holder
	// PID and Process identify the listener when Status is InUse and the
	// owner could be looked up.
	PID     int
	Process string
}

Conflict describes a host port that cannot be bound.

func Scan

func Scan(owner OwnerFunc, ports ...int) []Conflict

Scan returns a Conflict for every port that cannot be bound, in the order given. Each port is bound on the wildcard address and on both loopback addresses: WSL's localhost relay binds 127.0.0.1 or [::1] only, depending on the address family of the listener it forwards, and on Windows a wildcard bind succeeds alongside it, which would make a held port look free.

func (Conflict) String

func (c Conflict) String() string

type Holder

type Holder int

Holder is who holds a port that is in use.

const (
	HolderUnknown Holder = iota
	// HolderWSL is WSL: its localhost relay forwarding a listener from inside
	// a distro, or the VM itself with mirrored networking. `wsl --shutdown`
	// releases it.
	HolderWSL
	// HolderContainerRuntime is the container runtime's port proxy, either
	// for a running container or for one that was just removed and whose
	// port has not been released yet.
	HolderContainerRuntime
	// HolderOther is any other process. Shutting down WSL does not help.
	HolderOther
)

func ClassifyHolder

func ClassifyHolder(image string) Holder

ClassifyHolder maps a process image name to a Holder.

func (Holder) String

func (h Holder) String() string

type OwnerFunc

type OwnerFunc func(port int) (pid int, image string)

OwnerFunc returns the pid and process image name listening on port. It returns pid 0 when the owner cannot be determined. NewOwnerLookup returns one that resolves every port of a scan from a single system query.

func NewOwnerLookup

func NewOwnerLookup() OwnerFunc

NewOwnerLookup returns an OwnerFunc that never finds an owner: port remedies are only needed on Windows.

type Status

type Status int

Status is the result of trying to bind a host port.

const (
	Free Status = iota
	// Reserved means the bind was refused although nothing listens on the
	// port. On Windows this is WSAEACCES, returned for ports inside an
	// excluded port range reserved by Hyper-V/WinNAT
	// (`netsh int ipv4 show excludedportrange protocol=tcp`). Docker reports
	// it as "ports are not available", which reads like the port is in use.
	Reserved
	// InUse means a socket is listening on the port.
	InUse
	// Unavailable is any other bind failure.
	Unavailable
)

func Bind

func Bind(host string, port int) (Status, error)

Bind tries to listen on host:port ("" for the wildcard address) and classifies the result.

func ClassifyBindError

func ClassifyBindError(err error) Status

ClassifyBindError maps a net.Listen error to a Status.

func Probe

func Probe(port int) (Status, error)

Probe binds port on the wildcard address, on 127.0.0.1 and on [::1]. A listener on any of them makes the port InUse; otherwise the first failure on the wildcard address or 127.0.0.1 wins. Any other failure on [::1] is ignored, as it also fails to bind on hosts without IPv6.

func (Status) String

func (s Status) String() string

Jump to

Keyboard shortcuts

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