IP Address/UNIX Socket convenience functions for Go
Switch branches/tags
Nothing to show
Clone or download
angrycub and jefferai Expanded RegEx to allow wireless adapters on Windows. (#21)
The current regex would not see any wireless adapter because it was strictly looking for "Ethernet adapter" this allows a match on "Wireless LAN adapter" as well.
Latest commit 6d291a9 Mar 20, 2018
Permalink
Failed to load latest commit information.
cmd/sockaddr Portability fixes. May 23, 2017
template Add mask operation on IPv4 and IPv6 interfaces Sep 30, 2017
.gitignore Begin attempting to reachieve 100% test coverage. Nov 22, 2016
GNUmakefile Rename Makefile to GNUmakefile May 23, 2017
LICENSE Initial commit Mar 2, 2016
README.md Fix backticks in readme. Mar 24, 2017
doc.go Add a stub doc.go file. Nov 13, 2016
ifaddr.go Add `GetPrivateIPs`, `GetPublicIPs`, and `GetInterfaceIPs` helper fun… May 23, 2017
ifaddr_test.go Review changes to mask operation on IPv4 and IPv6 interfaces Oct 30, 2017
ifaddrs.go Expanded RegEx to allow wireless adapters on Windows. (#21) Mar 20, 2018
ifaddrs_test.go Fix formatting issues in tests Mar 2, 2018
ifattr.go Add support for IfAddr attributes and generic SockAddr Attr() support. Nov 22, 2016
ifattr_test.go Fix formatting issues in tests Mar 2, 2018
ipaddr.go Remove the CIDR from the network name. Dec 21, 2016
ipaddr_test.go Convert to new subtests ala t.Run(). Oct 31, 2016
ipaddrs.go Add ipaddrs tests and an updated version of ipaddrs. Sep 26, 2016
ipaddrs_test.go Improve test coverage to reasonably complete levels. Nov 28, 2016
ipv4addr.go Compile regexps once then reference copies in the future. Apr 25, 2017
ipv4addr_test.go Fix formatting issues in tests Mar 2, 2018
ipv6addr.go Clarify and improve the docs on a few functions to explicitly spell o… Nov 28, 2016
ipv6addr_test.go Fix formatting issues in tests Mar 2, 2018
rfc.go Add `GetPrivateIPs`, `GetPublicIPs`, and `GetInterfaceIPs` helper fun… May 23, 2017
rfc_test.go Test coverage golf nears an end... Nov 28, 2016
route_info.go Pull routing functions out into `route_info*` files. Dec 3, 2016
route_info_bsd.go Fix a few spelling errors. Feb 8, 2017
route_info_default.go Pull routing functions out into `route_info*` files. Dec 3, 2016
route_info_linux.go Lookup IP on $PATH to support unusual locations Jun 22, 2017
route_info_solaris.go Fix a few spelling errors. Feb 8, 2017
route_info_test.go Pull routing functions out into `route_info*` files. Dec 3, 2016
route_info_windows.go Fix a few spelling errors. Feb 8, 2017
sockaddr.go Add a marshaling/unmarshaling convenience type. (#14) Jun 27, 2017
sockaddr_test.go Add a marshaling/unmarshaling convenience type. (#14) Jun 27, 2017
sockaddrs.go Improve test coverage to reasonably complete levels. Nov 28, 2016
sockaddrs_test.go Fix formatting issues in tests Mar 2, 2018
unixsock.go Improve test coverage to reasonably complete levels. Nov 28, 2016
unixsock_test.go Add Equal() support to UnixSock. Nov 28, 2016

README.md

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 get -u github.com/hashicorp/go-sockaddr/cmd/sockaddr

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.