Categorygithub.com/hashicorp/go-sockaddr
modulepackage
1.0.7
Repository: https://github.com/hashicorp/go-sockaddr.git
Documentation: pkg.go.dev

# README

go-sockaddr

sockaddr Library

Socket address convenience functions for Go. go-sockaddr is a convenience library that makes doing the right thing with IP addresses easy. go-sockaddr is loosely modeled after the UNIX sockaddr_t and creates a union of the family of sockaddr_t types (see below for an ascii diagram). Library documentation is available at https://godoc.org/github.com/hashicorp/go-sockaddr. The primary intent of the library was to make it possible to define heuristics for selecting the correct IP addresses when a configuration is evaluated at runtime. See the docs, template package, tests, and CLI utility for details and hints as to how to use this library.

For example, with this library it is possible to find an IP address that:

Or any combination or variation therein.

There are also a few simple helper functions such as GetPublicIP and GetPrivateIP which both return strings and select the first public or private IP address on the default interface, respectively. Similarly, there is also a helper function called GetInterfaceIP which returns the first usable IP address on the named interface.

sockaddr CLI

Given the possible complexity of the sockaddr library, there is a CLI utility that accompanies the library, also called sockaddr. The sockaddr utility exposes nearly all of the functionality of the library and can be used either as an administrative tool or testing tool. To install the sockaddr, run:

$ go install github.com/hashicorp/go-sockaddr/cmd/sockaddr@latest

If you're familiar with UNIX's sockaddr struct's, the following diagram mapping the C sockaddr (top) to go-sockaddr structs (bottom) and interfaces will be helpful:

+-------------------------------------------------------+
|                                                       |
|                        sockaddr                       |
|                        SockAddr                       |
|                                                       |
| +--------------+ +----------------------------------+ |
| | sockaddr_un  | |                                  | |
| | SockAddrUnix | |           sockaddr_in{,6}        | |
| +--------------+ |                IPAddr            | |
|                  |                                  | |
|                  | +-------------+ +--------------+ | |
|                  | | sockaddr_in | | sockaddr_in6 | | |
|                  | |   IPv4Addr  | |   IPv6Addr   | | |
|                  | +-------------+ +--------------+ | |
|                  |                                  | |
|                  +----------------------------------+ |
|                                                       |
+-------------------------------------------------------+

Inspiration and Design

There were many subtle inspirations that led to this design, but the most direct inspiration for the filtering syntax was OpenBSD's pf.conf(5) firewall syntax that lets you select the first IP address on a given named interface. The original problem stemmed from:

  • needing to create immutable images using Packer that ran the Consul process (Consul can only use one IP address at a time);
  • images that may or may not have multiple interfaces or IP addresses at runtime; and
  • we didn't want to rely on configuration management to render out the correct IP address if the VM image was being used in an auto-scaling group.

Instead we needed some way to codify a heuristic that would correctly select the right IP address but the input parameters were not known when the image was created.

# Packages

No description provided by the author
Package sockaddr/template provides a text/template interface the SockAddr helper functions.

# Functions

AscAddress is a sorting function to sort SockAddrs by their respective address type.
AscIfAddress is a sorting function to sort IfAddrs by their respective address type.
AscIfDefault is a sorting function to sort IfAddrs by whether or not they have a default route or not.
AscIfName is a sorting function to sort IfAddrs by their interface names.
AscIfNetworkSize is a sorting function to sort IfAddrs by their respective network mask size.
AscIfPort is a sorting function to sort IfAddrs by their respective port type.
AscIfPrivate is a sorting function to sort IfAddrs by "private" values before "public" values.
AscIfType is a sorting function to sort IfAddrs by their respective address type.
AscNetworkSize is a sorting function to sort SockAddrs based on their network size.
AscPort is a sorting function to sort SockAddrs by their respective address type.
AscPrivate is a sorting function to sort "more secure" private values before "more public" values.
AscType is a sorting function to sort "more secure" types before "less-secure" types.
Attr returns the named attribute as a string.
DescIfAddress is identical to AscIfAddress but reverse ordered.
DescIfDefault is identical to AscIfDefault but reverse ordered.
DescIfName is identical to AscIfName but reverse ordered.
DescIfNetworkSize is identical to AscIfNetworkSize but reverse ordered.
DescIfPort is identical to AscIfPort but reverse ordered.
DescIfPrivate is identical to AscIfPrivate but reverse ordered.
DescIfType is identical to AscIfType but reverse ordered.
ExcludeIfs returns an IfAddrs based on the passed in selector.
FilterIfByType filters IfAddrs and returns a list of the matching type.
GetAllInterfaces iterates over all available network interfaces and finds all available IP addresses on each interface and converts them to sockaddr.IPAddrs, and returning the result as an array of IfAddr.
GetDefaultInterfaces returns IfAddrs of the addresses attached to the default route.
GetInterfaceIP returns a string with a single IP address sorted by the size of the network (i.e.
GetInterfaceIPs returns a string with all IPs, sorted by the size of the network (i.e.
GetPrivateInterfaces returns an IfAddrs that are part of RFC 6890 and have a default route.
GetPrivateIP returns a string with a single IP address that is part of RFC 6890 and has a default route.
GetPrivateIPs returns a string with all IP addresses that are part of RFC 6890 (regardless of whether or not there is a default route, unlike GetPublicIP).
GetPublicInterfaces returns an IfAddrs that are NOT part of RFC 6890 and has a default route.
GetPublicIP returns a string with a single IP address that is NOT part of RFC 6890 and has a default route.
GetPublicIPs returns a string with all IP addresses that are NOT part of RFC 6890 (regardless of whether or not there is a default route, unlike GetPublicIP).
IfAddrAttr returns a string representation of an attribute for the given IfAddr.
IfAddrAttrs returns a list of attributes supported by the IfAddr type.
IfAddrMath will return a new IfAddr struct with a mutated value.
IfAddrsMath will apply an IfAddrMath operation each IfAddr struct.
IfAttr forwards the selector to IfAttr.Attr() for resolution.
IfAttrs forwards the selector to IfAttrs.Attr() for resolution.
IfByAddress returns a list of matched and non-matched IfAddrs, or an error if the regexp fails to compile.
IfByFlag returns a list of matching and non-matching IfAddrs that match the specified type.
IfByMaskSize returns a list of matched and non-matched IfAddrs that have the matching mask size.
IfByName returns a list of matched and non-matched IfAddrs, or an error if the regexp fails to compile.
IfByNetwork returns an IfAddrs that are equal to or included within the network passed in by selector.
IfByPort returns a list of matched and non-matched IfAddrs, or an error if the regexp fails to compile.
IfByRFC returns a list of matched and non-matched IfAddrs that contain the relevant RFC-specified traits.
IfByRFCs returns a list of matched and non-matched IfAddrs that contain the relevant RFC-specified traits.
IfByType returns a list of matching and non-matching IfAddr that match the specified type.
IncludeIfs returns an IfAddrs based on the passed in selector.
IPAddrAttr returns a string representation of an attribute for the given IPAddr.
IPAttrs returns a list of attributes supported by the IPAddr type.
IPv4AddrAttr returns a string representation of an attribute for the given IPv4Addr.
IPv4Attrs returns a list of attributes supported by the IPv4Addr type.
IPv6AddrAttr returns a string representation of an attribute for the given IPv6Addr.
IPv6Attrs returns a list of attributes supported by the IPv6Addr type.
IsRFC tests to see if an SockAddr matches the specified RFC.
JoinIfAddrs joins an IfAddrs and returns a string.
KnownRFCs returns an initial set of known RFCs.
LimitIfAddrs returns a slice of IfAddrs based on the specified limit.
MustIPAddr is a helper method that must return an IPAddr or panic on invalid input.
MustIPv4Addr is a helper method that must return an IPv4Addr or panic on invalid input.
MustIPv6Addr is a helper method that must return an IPv6Addr or panic on invalid input.
MustUnixSock is a helper method that must return an UnixSock or panic on invalid input.
NewIPAddr creates a new IPAddr from a string.
NewIPv4Addr creates an IPv4Addr from a string.
NewIPv6Addr creates an IPv6Addr from a string.
NewRouteInfo returns a Linux-specific implementation of the RouteInfo interface.
New creates a new SockAddr from the string.
NewUnixSock creates an UnixSock from a string path.
OffsetIfAddrs returns a slice of IfAddrs based on the specified offset.
OrderedAddrBy sorts SockAddr by the list of sort function pointers.
OrderedIfAddrBy sorts SockAddr by the list of sort function pointers.
SockAddrAttr returns a string representation of an attribute for the given SockAddr.
UnixSockAttrs returns a list of attributes supported by the UnixSock type.
SortIfBy returns an IfAddrs sorted based on the passed in selector.
ToIPAddr returns an IPAddr type or nil if the type conversion fails.
ToIPv4Addr returns an IPv4Addr type or nil if the type conversion fails.
ToIPv6Addr returns an IPv6Addr type or nil if the type conversion fails.
ToUnixSock returns a UnixSock type or nil if the type conversion fails.
UniqueIfAddrsBy creates a unique set of IfAddrs based on the matching selector.
UnixSockAttr returns a string representation of an attribute for the given UnixSock.
UnixSockAttrs returns a list of attributes supported by the UnixSockAddr type.
VisitAllRFCs iterates over all known RFCs and calls the visitor.

# Constants

ForwardingBlacklist is a faux RFC that includes a list of non-forwardable IP blocks.
No description provided by the author
Constants for the sizes of IPv3, IPv4, and IPv6 address types.
IPv4HostMask is a constant represents a /32 IPv4 Address (i.e.
Constants for the sizes of IPv3, IPv4, and IPv6 address types.
IPv6HostPrefix is a constant represents a /128 IPv6 Prefix.
Constants for the sizes of IPv3, IPv4, and IPv6 address types.
TypeIP is the union of TypeIPv4 and TypeIPv6.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author

# Structs

IfAddr is a union of a SockAddr and a net.Interface.
IPv4Addr implements a convenience wrapper around the union of Go's built-in net.IP and net.IPNet types.
IPv6Addr implements a convenience wrapper around the union of Go's built-in net.IP and net.IPNet types.
Although this is pretty trivial to do in a program, having the logic here is useful all around.
SortIPAddrsByBroadMaskLen is a type that satisfies sort.Interface and can be used by the routines in this package.
SortIPAddrsBySpecificMaskLen is a type that satisfies sort.Interface and can be used by the routines in this package.
SortIPAddrsBySpecificMaskLen is a type that satisfies sort.Interface and can be used by the routines in this package.
No description provided by the author

# Interfaces

IPAddr is a generic IP address interface for IPv4 and IPv6 addresses, networks, and socket endpoints.
RouteInterface specifies an interface for obtaining memoized route table and network information from a given OS.
No description provided by the author

# Type aliases

No description provided by the author
CmpAddrFunc is the function signature that must be met to be used in the OrderedAddrBy multiAddrSorter.
CmpIfFunc is the function signature that must be met to be used in the OrderedIfAddrBy multiIfAddrSorter.
IfAddrs is a slice of IfAddr.
No description provided by the author
IPPort is the type for an IP port number for the TCP and UDP IP transports.
IPPrefixLen is a typed integer representing the prefix length for a given IPAddr.
No description provided by the author
No description provided by the author
No description provided by the author
SockAddrs is a slice of SockAddrs.
No description provided by the author
No description provided by the author