ports

package
v0.0.0-...-73a4147 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2024 License: Apache-2.0, MIT Imports: 7 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BitFlags

type BitFlags uint32

BitFlags is a bitset representation of Flags.

const (
	// MostRecentFlag represents Flags.MostRecent.
	MostRecentFlag BitFlags = 1 << iota

	// LoadBalancedFlag represents Flags.LoadBalanced.
	LoadBalancedFlag

	// TupleOnlyFlag represents Flags.TupleOnly.
	TupleOnlyFlag

	// FlagMask is a bit mask for BitFlags.
	FlagMask = nextFlag - 1

	// MultiBindFlagMask contains the flags that allow binding the same
	// tuple multiple times.
	MultiBindFlagMask = MostRecentFlag | LoadBalancedFlag
)

func (BitFlags) ToFlags

func (f BitFlags) ToFlags() Flags

ToFlags converts the bitset into a Flags struct.

type FlagCounter

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

FlagCounter counts how many references each flag combination has.

func (*FlagCounter) AddRef

func (c *FlagCounter) AddRef(flags BitFlags)

AddRef increases the reference count for a specific flag combination.

func (FlagCounter) AllRefsHave

func (c FlagCounter) AllRefsHave(flags BitFlags) bool

AllRefsHave returns if all references have all specified flags.

func (*FlagCounter) DropRef

func (c *FlagCounter) DropRef(flags BitFlags)

DropRef decreases the reference count for a specific flag combination.

func (FlagCounter) FlagRefs

func (c FlagCounter) FlagRefs(flags BitFlags) int

FlagRefs returns the number of references with all specified flags.

func (FlagCounter) SharedFlags

func (c FlagCounter) SharedFlags() BitFlags

SharedFlags returns the set of flags shared by all references.

func (FlagCounter) TotalRefs

func (c FlagCounter) TotalRefs() int

TotalRefs calculates the total number of references for all flag combinations.

type Flags

type Flags struct {
	// MostRecent represents UDP SO_REUSEADDR.
	MostRecent bool

	// LoadBalanced indicates SO_REUSEPORT.
	//
	// LoadBalanced takes precidence over MostRecent.
	LoadBalanced bool

	// TupleOnly represents TCP SO_REUSEADDR.
	TupleOnly bool
}

Flags represents the type of port reservation.

+stateify savable

func (Flags) Bits

func (f Flags) Bits() BitFlags

Bits converts the Flags to their bitset form.

func (Flags) Effective

func (f Flags) Effective() Flags

Effective returns the effective behavior of a flag config.

func (*Flags) StateFields

func (f *Flags) StateFields() []string

func (*Flags) StateLoad

func (f *Flags) StateLoad(stateSourceObject state.Source)

+checklocksignore

func (*Flags) StateSave

func (f *Flags) StateSave(stateSinkObject state.Sink)

+checklocksignore

func (*Flags) StateTypeName

func (f *Flags) StateTypeName() string

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) PickEphemeralPort

func (pm *PortManager) PickEphemeralPort(rng *rand.Rand, testPort PortTester) (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.

func (*PortManager) PickEphemeralPortStable

func (pm *PortManager) PickEphemeralPortStable(offset uint32, testPort PortTester) (port uint16, err tcpip.Error)

PickEphemeralPortStable starts at the specified offset + pm.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) PortRange

func (pm *PortManager) PortRange() (uint16, uint16)

PortRange returns the UDP and TCP inclusive range of ephemeral ports used in both IPv4 and IPv6.

func (*PortManager) ReleasePort

func (pm *PortManager) ReleasePort(res Reservation)

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

func (*PortManager) ReservePort

func (pm *PortManager) ReservePort(rng *rand.Rand, res Reservation, testPort PortTester) (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.

An optional PortTester can be passed in which if provided will be used to test if the picked port can be used. The function should return true if the port is safe to use, false otherwise.

func (*PortManager) ReserveTuple

func (pm *PortManager) ReserveTuple(res Reservation) bool

ReserveTuple adds a port reservation for the tuple on all given protocol.

func (*PortManager) SetPortRange

func (pm *PortManager) SetPortRange(start uint16, end uint16) tcpip.Error

SetPortRange sets the UDP and TCP IPv4 and IPv6 ephemeral port range (inclusive).

type PortTester

type PortTester func(port uint16) (good bool, err tcpip.Error)

PortTester indicates whether the passed in port is suitable. Returning an error causes the function to which the PortTester is passed to return that error.

type Reservation

type Reservation struct {
	// Networks is a list of network protocols to which the reservation
	// applies. Can be IPv4, IPv6, or both.
	Networks []tcpip.NetworkProtocolNumber

	// Transport is the transport protocol to which the reservation applies.
	Transport tcpip.TransportProtocolNumber

	// Addr is the address of the local endpoint.
	Addr tcpip.Address

	// Port is the local port number.
	Port uint16

	// Flags describe features of the reservation.
	Flags Flags

	// BindToDevice is the NIC to which the reservation applies.
	BindToDevice tcpip.NICID

	// Dest is the destination address.
	Dest tcpip.FullAddress
}

Reservation describes a port reservation.

Jump to

Keyboard shortcuts

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