Documentation
¶
Overview ¶
Package netutil provides utility functions for networking, including parsing IP addresses and prefixes.
Index ¶
- func EnsureHostPort(addr, port string) string
- func HTTPServerAddr(addrPort netip.AddrPort) string
- func ParseAddrDefaultPort(addr, defaultPort string) (netip.AddrPort, error)
- func ParseAddrIgnoringPort(addr string) (netip.Addr, error)
- func ParseAddrOrPrefix(addr string) (netip.Prefix, error)
- func Resolve(addr string) stringdeprecated
- func ResolveAll(addr string) ([]net.IP, error)
- func ResolveFirst(addr string) (string, error)
- func TrimIPv6(addr string) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnsureHostPort ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
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 ¶
ResolveAll returns all IP addresses resolved for the host component of addr.
func ResolveFirst ¶
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.
Types ¶
This section is empty.