• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

cmd/sockaddr/H08-Mar-2019-

template/H08-Mar-2019-

.gitignoreH A D08-Mar-2019292

GNUmakefileH A D08-Mar-20192 KiB

LICENSEH A D08-Mar-201916.3 KiB

README.mdH A D08-Mar-20195.5 KiB

doc.goH A D08-Mar-2019130

go.modH A D08-Mar-2019215

go.sumH A D08-Mar-20192.2 KiB

ifaddr.goH A D08-Mar-20196.2 KiB

ifaddr_test.goH A D08-Mar-201913.8 KiB

ifaddrs.goH A D08-Mar-201938.9 KiB

ifaddrs_test.goH A D08-Mar-201947.7 KiB

ifattr.goH A D08-Mar-20191.3 KiB

ifattr_test.goH A D08-Mar-20192.7 KiB

ipaddr.goH A D08-Mar-20193.7 KiB

ipaddr_test.goH A D08-Mar-20195.9 KiB

ipaddrs.goH A D08-Mar-20193.3 KiB

ipaddrs_test.goH A D08-Mar-201911.8 KiB

ipv4addr.goH A D08-Mar-201915.3 KiB

ipv4addr_test.goH A D08-Mar-201934.3 KiB

ipv6addr.goH A D08-Mar-201915.9 KiB

ipv6addr_test.goH A D08-Mar-201927.5 KiB

rfc.goH A D08-Mar-201939.3 KiB

rfc_test.goH A D08-Mar-20191.1 KiB

route_info.goH A D08-Mar-2019627

route_info_android.goH A D08-Mar-2019815

route_info_bsd.goH A D08-Mar-2019840

route_info_default.goH A D08-Mar-2019266

route_info_linux.goH A D08-Mar-2019982

route_info_solaris.goH A D08-Mar-2019845

route_info_test.goH A D08-Mar-20196.1 KiB

route_info_windows.goH A D08-Mar-2019943

sockaddr.goH A D08-Mar-20195.2 KiB

sockaddr_test.goH A D08-Mar-201911.5 KiB

sockaddrs.goH A D08-Mar-20194.8 KiB

sockaddrs_test.goH A D08-Mar-20198.4 KiB

unixsock.goH A D08-Mar-20193.3 KiB

unixsock_test.goH A D08-Mar-20192.6 KiB

README.md

1# go-sockaddr
2
3## `sockaddr` Library
4
5Socket address convenience functions for Go.  `go-sockaddr` is a convenience
6library that makes doing the right thing with IP addresses easy.  `go-sockaddr`
7is loosely modeled after the UNIX `sockaddr_t` and creates a union of the family
8of `sockaddr_t` types (see below for an ascii diagram).  Library documentation
9is available
10at
11[https://godoc.org/github.com/hashicorp/go-sockaddr](https://godoc.org/github.com/hashicorp/go-sockaddr).
12The primary intent of the library was to make it possible to define heuristics
13for selecting the correct IP addresses when a configuration is evaluated at
14runtime.  See
15the
16[docs](https://godoc.org/github.com/hashicorp/go-sockaddr),
17[`template` package](https://godoc.org/github.com/hashicorp/go-sockaddr/template),
18tests,
19and
20[CLI utility](https://github.com/hashicorp/go-sockaddr/tree/master/cmd/sockaddr)
21for details and hints as to how to use this library.
22
23For example, with this library it is possible to find an IP address that:
24
25* is attached to a default route
26  ([`GetDefaultInterfaces()`](https://godoc.org/github.com/hashicorp/go-sockaddr#GetDefaultInterfaces))
27* is contained within a CIDR block ([`IfByNetwork()`](https://godoc.org/github.com/hashicorp/go-sockaddr#IfByNetwork))
28* is an RFC1918 address
29  ([`IfByRFC("1918")`](https://godoc.org/github.com/hashicorp/go-sockaddr#IfByRFC))
30* is ordered
31  ([`OrderedIfAddrBy(args)`](https://godoc.org/github.com/hashicorp/go-sockaddr#OrderedIfAddrBy) where
32  `args` includes, but is not limited
33  to,
34  [`AscIfType`](https://godoc.org/github.com/hashicorp/go-sockaddr#AscIfType),
35  [`AscNetworkSize`](https://godoc.org/github.com/hashicorp/go-sockaddr#AscNetworkSize))
36* excludes all IPv6 addresses
37  ([`IfByType("^(IPv4)$")`](https://godoc.org/github.com/hashicorp/go-sockaddr#IfByType))
38* is larger than a `/32`
39  ([`IfByMaskSize(32)`](https://godoc.org/github.com/hashicorp/go-sockaddr#IfByMaskSize))
40* is not on a `down` interface
41  ([`ExcludeIfs("flags", "down")`](https://godoc.org/github.com/hashicorp/go-sockaddr#ExcludeIfs))
42* preferences an IPv6 address over an IPv4 address
43  ([`SortIfByType()`](https://godoc.org/github.com/hashicorp/go-sockaddr#SortIfByType) +
44  [`ReverseIfAddrs()`](https://godoc.org/github.com/hashicorp/go-sockaddr#ReverseIfAddrs)); and
45* excludes any IP in RFC6890 address
46  ([`IfByRFC("6890")`](https://godoc.org/github.com/hashicorp/go-sockaddr#IfByRFC))
47
48Or any combination or variation therein.
49
50There are also a few simple helper functions such as `GetPublicIP` and
51`GetPrivateIP` which both return strings and select the first public or private
52IP address on the default interface, respectively.  Similarly, there is also a
53helper function called `GetInterfaceIP` which returns the first usable IP
54address on the named interface.
55
56## `sockaddr` CLI
57
58Given the possible complexity of the `sockaddr` library, there is a CLI utility
59that accompanies the library, also
60called
61[`sockaddr`](https://github.com/hashicorp/go-sockaddr/tree/master/cmd/sockaddr).
62The
63[`sockaddr`](https://github.com/hashicorp/go-sockaddr/tree/master/cmd/sockaddr)
64utility exposes nearly all of the functionality of the library and can be used
65either as an administrative tool or testing tool.  To install
66the
67[`sockaddr`](https://github.com/hashicorp/go-sockaddr/tree/master/cmd/sockaddr),
68run:
69
70```text
71$ go get -u github.com/hashicorp/go-sockaddr/cmd/sockaddr
72```
73
74If you're familiar with UNIX's `sockaddr` struct's, the following diagram
75mapping the C `sockaddr` (top) to `go-sockaddr` structs (bottom) and
76interfaces will be helpful:
77
78```
79+-------------------------------------------------------+
80|                                                       |
81|                        sockaddr                       |
82|                        SockAddr                       |
83|                                                       |
84| +--------------+ +----------------------------------+ |
85| | sockaddr_un  | |                                  | |
86| | SockAddrUnix | |           sockaddr_in{,6}        | |
87| +--------------+ |                IPAddr            | |
88|                  |                                  | |
89|                  | +-------------+ +--------------+ | |
90|                  | | sockaddr_in | | sockaddr_in6 | | |
91|                  | |   IPv4Addr  | |   IPv6Addr   | | |
92|                  | +-------------+ +--------------+ | |
93|                  |                                  | |
94|                  +----------------------------------+ |
95|                                                       |
96+-------------------------------------------------------+
97```
98
99## Inspiration and Design
100
101There were many subtle inspirations that led to this design, but the most direct
102inspiration for the filtering syntax was
103OpenBSD's
104[`pf.conf(5)`](https://www.freebsd.org/cgi/man.cgi?query=pf.conf&apropos=0&sektion=0&arch=default&format=html#PARAMETERS) firewall
105syntax that lets you select the first IP address on a given named interface.
106The original problem stemmed from:
107
108* needing to create immutable images using [Packer](https://www.packer.io) that
109  ran the [Consul](https://www.consul.io) process (Consul can only use one IP
110  address at a time);
111* images that may or may not have multiple interfaces or IP addresses at
112  runtime; and
113* we didn't want to rely on configuration management to render out the correct
114  IP address if the VM image was being used in an auto-scaling group.
115
116Instead we needed some way to codify a heuristic that would correctly select the
117right IP address but the input parameters were not known when the image was
118created.
119