netutil

package
v0.0.0-...-da98fe9 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: Apache-2.0 Imports: 5 Imported by: 2

README

Package cloudeng.io/net/netutil

import cloudeng.io/net/netutil

Package netutil provides utility functions for networking, including parsing IP addresses and prefixes.

Functions

Func EnsureHostPort
func EnsureHostPort(addr, port string) string

EnsureHostPort returns addr that is guaranteed to have a port. If addr already has a port, it is returned unchanged. If addr does not have a port, the supplied port is appended. If addr is an IPv6 address, it will be enclosed in brackets if it is not already.

Func HTTPServerAddr
func HTTPServerAddr(addrPort netip.AddrPort) string

HTTPServerAddr returns the address of an HTTP server based on the address and port of the server in a form to be used with http.Server.Addr. If the address is unspecified then it will be replaced with an empty string. If the port is 80 then "http" will be appended to the address, if the port is 443 then "https" will be appended to the address, otherwise the numeric port will be used.

Func ParseAddrDefaultPort
func ParseAddrDefaultPort(addr, defaultPort string) (netip.AddrPort, error)

ParseAddrDefaultPort parses an IP address string. If the address string already contains a port, it is parsed and returned. Otherwise, the supplied default port is used to construct and parse an address with that port. If the address contains only a port an address of "::" is used. ParseAddrDefaultPort calls ResolveFirst to resolve the address before parsing it.

Func ParseAddrIgnoringPort
func ParseAddrIgnoringPort(addr string) (netip.Addr, error)

ParseAddrIgnoringPort parses an IP address string and returns the address. If the string is an address with a port, it will be parsed as an address with a port and the address will be returned, ignoring the port. ParseAddrIgnoringPort calls ResolveFirst to resolve the address before parsing it.

Func ParseAddrOrPrefix
func ParseAddrOrPrefix(addr string) (netip.Prefix, error)

ParseAddrOrPrefix parses an IP address or prefix string and returns a netip.Prefix. If the string is an IP address without a prefix, it is treated as a full-bit prefix (/32 for IPv4, /128 for IPv6). ParseAddrOrPrefix calls ResolveFirst to resolve the address before parsing it.

Func Resolve
func Resolve(addr string) string

Resolve replaces the host component of addr with the first IP address resolved for that host. If the host component of addr cannot be resolved, addr is returned unchanged.

Deprecated: Use ResolveFirst instead, which returns an error if the host cannot be resolved instead of returning the original address.

Func ResolveAll
func ResolveAll(addr string) ([]net.IP, error)

ResolveAll returns all IP addresses resolved for the host component of addr.

Func ResolveFirst
func ResolveFirst(addr string) (string, error)

ResolveFirst returns addr with the host component replaced by the first IP address resolved for that host; any port is preserved unchanged. If the host component is empty (e.g. "" or ":80") addr is returned unchanged since there is nothing to resolve. If the host cannot be resolved, an error is returned.

Documentation

Overview

Package netutil provides utility functions for networking, including parsing IP addresses and prefixes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureHostPort

func EnsureHostPort(addr, port string) string

EnsureHostPort returns addr that is guaranteed to have a port. If addr already has a port, it is returned unchanged. If addr does not have a port, the supplied port is appended. If addr is an IPv6 address, it will be enclosed in brackets if it is not already.

func HTTPServerAddr

func HTTPServerAddr(addrPort netip.AddrPort) string

HTTPServerAddr returns the address of an HTTP server based on the address and port of the server in a form to be used with http.Server.Addr. If the address is unspecified then it will be replaced with an empty string. If the port is 80 then "http" will be appended to the address, if the port is 443 then "https" will be appended to the address, otherwise the numeric port will be used.

func ParseAddrDefaultPort

func ParseAddrDefaultPort(addr, defaultPort string) (netip.AddrPort, error)

ParseAddrDefaultPort parses an IP address string. If the address string already contains a port, it is parsed and returned. Otherwise, the supplied default port is used to construct and parse an address with that port. If the address contains only a port an address of "::" is used. ParseAddrDefaultPort calls ResolveFirst to resolve the address before parsing it.

func ParseAddrIgnoringPort

func ParseAddrIgnoringPort(addr string) (netip.Addr, error)

ParseAddrIgnoringPort parses an IP address string and returns the address. If the string is an address with a port, it will be parsed as an address with a port and the address will be returned, ignoring the port. ParseAddrIgnoringPort calls ResolveFirst to resolve the address before parsing it.

func ParseAddrOrPrefix

func ParseAddrOrPrefix(addr string) (netip.Prefix, error)

ParseAddrOrPrefix parses an IP address or prefix string and returns a netip.Prefix. If the string is an IP address without a prefix, it is treated as a full-bit prefix (/32 for IPv4, /128 for IPv6). ParseAddrOrPrefix calls ResolveFirst to resolve the address before parsing it.

func Resolve deprecated

func Resolve(addr string) string

Resolve replaces the host component of addr with the first IP address resolved for that host. If the host component of addr cannot be resolved, addr is returned unchanged.

Deprecated: Use ResolveFirst instead, which returns an error if the host cannot be resolved instead of returning the original address.

func ResolveAll

func ResolveAll(addr string) ([]net.IP, error)

ResolveAll returns all IP addresses resolved for the host component of addr.

func ResolveFirst

func ResolveFirst(addr string) (string, error)

ResolveFirst returns addr with the host component replaced by the first IP address resolved for that host; any port is preserved unchanged. If the host component is empty (e.g. "" or ":80") addr is returned unchanged since there is nothing to resolve. If the host cannot be resolved, an error is returned.

func TrimIPv6

func TrimIPv6(addr string) string

TrimIPv6 removes brackets from an IPv6 address if they are present.

Types

This section is empty.

Jump to

Keyboard shortcuts

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