addr

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2025 License: Apache-2.0 Imports: 6 Imported by: 4

Documentation

Overview

Package addr provides functions to retrieve local IP addresses from device interfaces.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrIPNotFound no IP address found, and explicit IP not provided.
	ErrIPNotFound = errors.New("no IP address found, and explicit IP not provided")
	// ErrNoAddress is returned when no address is provided.
	ErrNoAddress = errors.New("no adddress provided")
	// ErrPortInvalid is returned when the provided port is below 0.
	ErrPortInvalid = errors.New("invalid port provided, must be between 0 and 65535")
	// ErrInvalidIP is returned an invalid IP is provided.
	ErrInvalidIP = errors.New("invalid IP provided")
)

Functions

func Extract

func Extract(addr string) (string, error)

Extract returns a valid IP address. If the address provided is a valid address, it will be returned directly. Otherwise, the available interfaces will be iterated over to find an IP address, preferably private.

func FindIP added in v0.1.0

func FindIP(addresses []net.Addr) (net.IP, error)

FindIP will return the first private IP available in the list. If no private IP is available it will return the first public IP, if present. If no public IP is available, it will return the first loopback IP, if present.

func GetAddress

func GetAddress(addr string) (string, error)

GetAddress will validate the address if one is provided, otherwise it will return an interface and port to listen on.

If you want to listen on all interfaces, you have to explicityly set '0.0.0.0:<port>'.

If no IP address is provides, as ':8080' the first private interface will be selected to listen on. If none are available, a public IP is selected.

func HostPort added in v0.1.0

func HostPort(addr string, port any) string

HostPort formats addr and port suitable for dial.

func IPs

func IPs() []string

IPs returns all available interface IP addresses.

func IsLocal

func IsLocal(addr string) bool

IsLocal checks whether an IP belongs to one of the device's interfaces.

func ParsePort

func ParsePort(address string) (int, error)

ParsePort will take the port from an address and return it as int.

func ValidateAddress

func ValidateAddress(address string) error

ValidateAddress will do basic validation on an address string.

Address example:

  • :8080
  • 192.168.1.1:8080
  • [2001:db8::1]:8080

Types

This section is empty.

Jump to

Keyboard shortcuts

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