ports

package
v0.0.0-...-494ab94 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2020 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package ports provides PortManager that manages allocating, reserving and releasing ports.

Index

Constants

View Source
const (
	// FirstEphemeral is the first ephemeral port.
	//
	// FirstEphemeral 是第一个临时端口。
	FirstEphemeral = 16000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type PortManager

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

PortManager manages allocating, reserving and releasing ports.

端口管理器管理端口的分配、保留和释放。

func NewPortManager

func NewPortManager() *PortManager

NewPortManager creates new PortManager.

func (*PortManager) IsPortAvailable

func (s *PortManager) IsPortAvailable(networks []tcpip.NetworkProtocolNumber, transport tcpip.TransportProtocolNumber, addr tcpip.Address, port uint16, reuse bool, bindToDevice tcpip.NICID) bool

IsPortAvailable tests if the given port is available on all given protocols.

func (*PortManager) PickEphemeralPort

func (s *PortManager) PickEphemeralPort(testPort func(p uint16) (bool, *tcpip.Error)) (port uint16, err *tcpip.Error)

PickEphemeralPort randomly chooses a starting point and iterates over all possible ephemeral ports, allowing the caller to decide whether a given port is suitable for its needs, and stopping when a port is found or an error occurs.

PickEphemeralPort 随机选择一个起始位置,遍历所有可能的临时端口, 让调用者通过 testPort 决定被遍历的端口是否符合要求,当找到一个端口或发生错误时,就会停止遍历。

func (*PortManager) PickEphemeralPortStable

func (s *PortManager) PickEphemeralPortStable(offset uint32, testPort func(p uint16) (bool, *tcpip.Error)) (port uint16, err *tcpip.Error)

PickEphemeralPortStable starts at the specified offset + s.portHint and iterates over all ephemeral ports, allowing the caller to decide whether a given port is suitable for its needs and stopping when a port is found or an error occurs.

func (*PortManager) ReleasePort

func (s *PortManager) ReleasePort(
	networks []tcpip.NetworkProtocolNumber,
	transport tcpip.TransportProtocolNumber,
	addr tcpip.Address,
	port uint16,
	bindToDevice tcpip.NICID,
)

ReleasePort releases the reservation on a port/IP combination so that it can be reserved by other endpoints.

func (*PortManager) ReservePort

func (s *PortManager) ReservePort(
	networks []tcpip.NetworkProtocolNumber,
	transport tcpip.TransportProtocolNumber,
	addr tcpip.Address,
	port uint16,
	reuse bool,
	bindToDevice tcpip.NICID,
) (reservedPort uint16, err *tcpip.Error)

ReservePort marks a port/IP combination as reserved so that it cannot be reserved by another endpoint. If port is zero, ReservePort will search for an unreserved ephemeral port and reserve it, returning its value in the "port" return value.

ReservePort 将一个 端口/IP 组合 标记为保留,这样它就不能被其他端点保留。 如果 port 为零,ReservePort() 将搜索一个未保留的短暂端口并保留它, 并在返回值 "reservedPort" 中返回它的值。

Jump to

Keyboard shortcuts

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