bind

package
v0.5.12 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: MIT Imports: 6 Imported by: 2

Documentation

Overview

Package bind provides mechanisms to facilitate listening TCP and UDP ports

Index

Constants

View Source
const (
	// DefaultPortAttempts indicates how many times we will try binding a port
	DefaultPortAttempts = 4

	// DefaultMaxRecvBufferSize indicates the receive buffer size of UDP listeners
	DefaultMaxRecvBufferSize = 2 * 1024 * 1024

	// MinimumMaxRecvBufferSize indicates the minimum receive buffer size of UDP listeners
	// If a lower value is given, it will be reset to DefaultMaxRecvBufferSize
	MinimumMaxRecvBufferSize = 32
)

Variables

This section is empty.

Functions

func AddrPortSliceTCPListener

func AddrPortSliceTCPListener(s []*net.TCPListener) ([]netip.AddrPort, bool)

AddrPortSliceTCPListener attempts to extract netip.AddrPort from a slice of *net.TCPListener, it also confirms if all entries were converted

func AddrPortSliceUDPConn

func AddrPortSliceUDPConn(s []*net.UDPConn) ([]netip.AddrPort, bool)

AddrPortSliceUDPConn attempts to extract netip.AddrPort from a slice of *net.UDPConn, it also confirms if all entries were converted

func Bind

func Bind(cfg *Config) ([]*net.TCPListener, []*net.UDPConn, error)

Bind attempts to listen all addresses specified by the given configuration. TCP and UDP on the same port for all.

func IPAddresses

func IPAddresses(s []netip.AddrPort) ([]netip.Addr, bool)

IPAddresses extract all valid unique addresses on a slice, and if all were unique and valid

func SamePort

func SamePort(s []netip.AddrPort) (uint16, bool)

SamePort verifies if all addresses on a slice point to the same port, SamePort verifies if all addresses on a slice point to the same port, and tells us which. In case of error it will return the first port found.

func StringIPAddresses

func StringIPAddresses(s []netip.AddrPort) ([]string, bool)

StringIPAddresses extract all valid unique addresses on a slice, and if all were unique and valid

Types

type AllListener

type AllListener interface {
	ListenAll(network string, addr []string) ([]net.Listener, error)
	ListenAllPacket(network string, addr []string) ([]net.PacketConn, error)
}

AllListener is equivalent to Listener but takes an array of addresses

type AllTCPListener

type AllTCPListener interface {
	ListenAllTCP(network string, ladders []*net.TCPAddr) ([]*net.TCPListener, error)
}

AllTCPListener is equivalent to TCPListener but takes an array of addresses

type AllUDPListener

type AllUDPListener interface {
	ListenAllUDP(network string, ladders []*net.UDPAddr) ([]*net.UDPConn, error)
}

AllUDPListener is equivalent to UDPListener but takes an array of addresses

type Config

type Config struct {
	// Interface is the list of interfaces to listen on
	Interfaces []string
	// Addresses is the list of addresses to listen on
	Addresses []string
	// Port is the port to listen on, for both TCP and UDP
	Port uint16
	// PortStrict tells us not to try other ports
	PortStrict bool
	// PortAttempts indicates how many times we will try finding a port
	PortAttempts int
	// Defaultport indicates the port to try on the first attempt if Port is zero
	DefaultPort uint16

	// OnlyTCP tells Bind to skip listening UDP ports
	OnlyTCP bool
	// OnlyUDP tells Bind to skip listening TCP ports
	OnlyUDP bool

	// ListenTCP is the helper to use to listen on TCP ports
	ListenTCP func(network string, laddr *net.TCPAddr) (*net.TCPListener, error)
	// ListenUDP is the helper to use to listen on UDP ports
	ListenUDP func(network string, laddr *net.UDPAddr) (*net.UDPConn, error)

	// MaxRecvBufferSize is the buffer size we will attempt to set to
	// UDP listeners
	MaxRecvBufferSize int
}

Config is the configuration for Bind()

func (*Config) Addrs

func (cfg *Config) Addrs() ([]net.IP, error)

Addrs returns the Addresses list parsed into net.IP

func (*Config) Bind

func (cfg *Config) Bind() ([]*net.TCPListener, []*net.UDPConn, error)

Bind attempts to listen all specified addresses. TCP and UDP on the same port for all.

func (*Config) Refresh

func (cfg *Config) Refresh(s []netip.AddrPort) bool

Refresh attempts to update a Config based on a given slice of netip.AddrPort corresponding to the listeners

func (*Config) RefreshFromTCPListeners

func (cfg *Config) RefreshFromTCPListeners(tcpListeners []*net.TCPListener) bool

RefreshFromTCPListeners attempts to update a Config based on a given slice of *net.TCPListener

func (*Config) RefreshFromUDPConn

func (cfg *Config) RefreshFromUDPConn(udpListeners []*net.UDPConn) bool

RefreshFromUDPConn attempts to update a Config based on a given slice of *net.UDPConn

func (*Config) SetDefaults

func (cfg *Config) SetDefaults() error

SetDefaults attempts to fill any configuration gap, specially the IP Addresses when interfaces are provided instead

func (*Config) UseListener

func (cfg *Config) UseListener(lc TCPUDPListener)

UseListener sets Bind's Config to use the provided ListenConfig

type ListenConfig

type ListenConfig struct {
	net.ListenConfig

	// Context used when registering the listeners
	Context context.Context
}

ListenConfig extends the standard net.ListeConfig with a central holder for the Context bound to the listeners

func NewListenConfig

func NewListenConfig(ctx context.Context, keepalive time.Duration) *ListenConfig

NewListenConfig assists creating a ListenConfig due to the two-layer definition making difficult static declaration when `net` is shadowed

func (ListenConfig) Listen

func (lc ListenConfig) Listen(network, addr string) (net.Listener, error)

Listen acts like the standard net.Listen but using the context.Context, KeepAlive, and optional Control function from our ListenConfig struct

func (ListenConfig) ListenAll

func (lc ListenConfig) ListenAll(network string, addrs []string) ([]net.Listener, error)

ListenAll acts like Listen but on a list of addresses

func (ListenConfig) ListenAllPacket

func (lc ListenConfig) ListenAllPacket(network string, addrs []string) ([]net.PacketConn, error)

ListenAllPacket acts like ListenPacket but on a list of addresses

func (ListenConfig) ListenAllTCP

func (lc ListenConfig) ListenAllTCP(network string, laddrs []*net.TCPAddr) (
	[]*net.TCPListener, error)

ListenAllTCP acts like ListenTCP but on a list of addresses

func (ListenConfig) ListenAllUDP

func (lc ListenConfig) ListenAllUDP(network string, laddrs []*net.UDPAddr) ([]*net.UDPConn, error)

ListenAllUDP acts like ListenUDP but on a list of addresses

func (ListenConfig) ListenPacket

func (lc ListenConfig) ListenPacket(network, addr string) (net.PacketConn, error)

ListenPacket acts like the standard net.ListenPacket but using the context.Context, KeepAlive, and optional Control function from our ListenConfig struct

func (ListenConfig) ListenTCP

func (lc ListenConfig) ListenTCP(network string, laddr *net.TCPAddr) (*net.TCPListener, error)

ListenTCP acts like the standard net.ListenTCP but using the context.Context, KeepAlive, and optional Control function from our ListenConfig struct

func (ListenConfig) ListenUDP

func (lc ListenConfig) ListenUDP(network string, laddr *net.UDPAddr) (*net.UDPConn, error)

ListenUDP acts like the standard net.ListenUDP but using the context.Context, KeepAlive, and optional Control function from our ListenConfig struct

func (ListenConfig) WithUpgrader

func (lc ListenConfig) WithUpgrader(upg Upgrader) *UpgraderListenConfig

WithUpgrader creates a new ListenConfig using the provided Upgrader

type Listener

type Listener interface {
	Listen(network, addr string) (net.Listener, error)
	ListenPacket(network, addr string) (net.PacketConn, error)
}

Listener mimics net.ListenConfig providing a configuration context for net.Listen() and net.ListenPacket() alternatives

type TCPListener

type TCPListener interface {
	ListenTCP(network string, laddr *net.TCPAddr) (*net.TCPListener, error)
}

TCPListener provides a context-aware alternative to net.ListenTCP

type TCPUDPListener

type TCPUDPListener interface {
	TCPListener
	UDPListener
}

TCPUDPListener provides the callbacks used by Bind(). ListenTCP() and ListenUDP()

type UDPListener

type UDPListener interface {
	ListenUDP(network string, laddr *net.UDPAddr) (*net.UDPConn, error)
}

UDPListener provides a context-aware alternative to net.ListenUDP

type Upgrader

type Upgrader interface {
	ListenWithCallback(network, addr string,
		callback func(network, addr string) (net.Listener, error)) (net.Listener, error)
	ListenPacketWithCallback(network, addr string,
		callback func(netowkr, addr string) (net.PacketConn, error)) (net.PacketConn, error)
}

Upgrader represents a tool that keep account of listening ports but allows us to provide our own helper to do the last step

type UpgraderListenConfig

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

UpgraderListenConfig represents an object equivalent to a ListenConfig but using a Upgrader in the middle

func (UpgraderListenConfig) Listen

func (lu UpgraderListenConfig) Listen(network, addr string) (net.Listener, error)

Listen acts like the standard net.Listen but using our ListenConfig and via an Upgrader tool

func (UpgraderListenConfig) ListenAll

func (lu UpgraderListenConfig) ListenAll(network string, addrs []string) ([]net.Listener, error)

ListenAll acts like Listen but on a list of addresses

func (UpgraderListenConfig) ListenAllPacket

func (lu UpgraderListenConfig) ListenAllPacket(network string, addrs []string) (
	[]net.PacketConn, error)

ListenAllPacket acts like ListenPacket but on a list of addresses

func (UpgraderListenConfig) ListenAllTCP

func (lu UpgraderListenConfig) ListenAllTCP(network string, laddrs []*net.TCPAddr) (
	[]*net.TCPListener, error)

ListenAllTCP acts like ListenTCP but on a list of addresses

func (UpgraderListenConfig) ListenAllUDP

func (lu UpgraderListenConfig) ListenAllUDP(network string, laddrs []*net.UDPAddr) (
	[]*net.UDPConn, error)

ListenAllUDP acts like ListenUDP but on a list of addresses

func (UpgraderListenConfig) ListenPacket

func (lu UpgraderListenConfig) ListenPacket(network, addr string) (net.PacketConn, error)

ListenPacket acts like the standard net.ListenPacket but using our ListenConfig and via an Upgrader tool

func (UpgraderListenConfig) ListenTCP

func (lu UpgraderListenConfig) ListenTCP(network string, laddr *net.TCPAddr) (
	*net.TCPListener, error)

ListenTCP acts like the standard net.ListenTCP but using our ListenConfig and the UpgraderListen

func (UpgraderListenConfig) ListenUDP

func (lu UpgraderListenConfig) ListenUDP(network string, laddr *net.UDPAddr) (
	*net.UDPConn, error)

ListenUDP acts like the standard net.ListenUDP but using our ListenConfig and the UpgraderListen

Jump to

Keyboard shortcuts

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