{"id":13645884,"url":"https://github.com/hashicorp/go-sockaddr","last_synced_at":"2025-05-13T23:04:37.154Z","repository":{"id":13089709,"uuid":"52921796","full_name":"hashicorp/go-sockaddr","owner":"hashicorp","description":"IP Address/UNIX Socket convenience functions for Go","archived":false,"fork":false,"pushed_at":"2025-04-04T12:13:04.000Z","size":1030,"stargazers_count":275,"open_issues_count":20,"forks_count":53,"subscribers_count":296,"default_branch":"master","last_synced_at":"2025-04-10T00:14:31.316Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hashicorp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-03-02T00:52:45.000Z","updated_at":"2025-04-08T16:28:26.000Z","dependencies_parsed_at":"2023-11-10T19:23:55.776Z","dependency_job_id":"ede99622-5c3c-4cbe-be45-7212b8c314b0","html_url":"https://github.com/hashicorp/go-sockaddr","commit_stats":{"total_commits":255,"total_committers":20,"mean_commits":12.75,"dds":"0.12941176470588234","last_synced_commit":"618ad49bee98a3591554941c0d4d1bd5b617515f"},"previous_names":["hashicorp/go-netaddr"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fgo-sockaddr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fgo-sockaddr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fgo-sockaddr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fgo-sockaddr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hashicorp","download_url":"https://codeload.github.com/hashicorp/go-sockaddr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248131315,"owners_count":21052819,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T01:02:44.160Z","updated_at":"2025-04-10T00:14:38.253Z","avatar_url":"https://github.com/hashicorp.png","language":"Go","readme":"# go-sockaddr\n\n## `sockaddr` Library\n\nSocket address convenience functions for Go.  `go-sockaddr` is a convenience\nlibrary that makes doing the right thing with IP addresses easy.  `go-sockaddr`\nis loosely modeled after the UNIX `sockaddr_t` and creates a union of the family\nof `sockaddr_t` types (see below for an ascii diagram).  Library documentation\nis available\nat\n[https://godoc.org/github.com/hashicorp/go-sockaddr](https://godoc.org/github.com/hashicorp/go-sockaddr).\nThe primary intent of the library was to make it possible to define heuristics\nfor selecting the correct IP addresses when a configuration is evaluated at\nruntime.  See\nthe\n[docs](https://godoc.org/github.com/hashicorp/go-sockaddr),\n[`template` package](https://godoc.org/github.com/hashicorp/go-sockaddr/template),\ntests,\nand\n[CLI utility](https://github.com/hashicorp/go-sockaddr/tree/master/cmd/sockaddr)\nfor details and hints as to how to use this library.\n\nFor example, with this library it is possible to find an IP address that:\n\n* is attached to a default route\n  ([`GetDefaultInterfaces()`](https://godoc.org/github.com/hashicorp/go-sockaddr#GetDefaultInterfaces))\n* is contained within a CIDR block ([`IfByNetwork()`](https://godoc.org/github.com/hashicorp/go-sockaddr#IfByNetwork))\n* is an RFC1918 address\n  ([`IfByRFC(\"1918\")`](https://godoc.org/github.com/hashicorp/go-sockaddr#IfByRFC))\n* is ordered\n  ([`OrderedIfAddrBy(args)`](https://godoc.org/github.com/hashicorp/go-sockaddr#OrderedIfAddrBy) where\n  `args` includes, but is not limited\n  to,\n  [`AscIfType`](https://godoc.org/github.com/hashicorp/go-sockaddr#AscIfType),\n  [`AscNetworkSize`](https://godoc.org/github.com/hashicorp/go-sockaddr#AscNetworkSize))\n* excludes all IPv6 addresses\n  ([`IfByType(\"^(IPv4)$\")`](https://godoc.org/github.com/hashicorp/go-sockaddr#IfByType))\n* is larger than a `/32`\n  ([`IfByMaskSize(32)`](https://godoc.org/github.com/hashicorp/go-sockaddr#IfByMaskSize))\n* is not on a `down` interface\n  ([`ExcludeIfs(\"flags\", \"down\")`](https://godoc.org/github.com/hashicorp/go-sockaddr#ExcludeIfs))\n* preferences an IPv6 address over an IPv4 address\n  ([`SortIfByType()`](https://godoc.org/github.com/hashicorp/go-sockaddr#SortIfByType) +\n  [`ReverseIfAddrs()`](https://godoc.org/github.com/hashicorp/go-sockaddr#ReverseIfAddrs)); and\n* excludes any IP in RFC6890 address\n  ([`IfByRFC(\"6890\")`](https://godoc.org/github.com/hashicorp/go-sockaddr#IfByRFC))\n\nOr any combination or variation therein.\n\nThere are also a few simple helper functions such as `GetPublicIP` and\n`GetPrivateIP` which both return strings and select the first public or private\nIP address on the default interface, respectively.  Similarly, there is also a\nhelper function called `GetInterfaceIP` which returns the first usable IP\naddress on the named interface.\n\n## `sockaddr` CLI\n\nGiven the possible complexity of the `sockaddr` library, there is a CLI utility\nthat accompanies the library, also\ncalled\n[`sockaddr`](https://github.com/hashicorp/go-sockaddr/tree/master/cmd/sockaddr).\nThe\n[`sockaddr`](https://github.com/hashicorp/go-sockaddr/tree/master/cmd/sockaddr)\nutility exposes nearly all of the functionality of the library and can be used\neither as an administrative tool or testing tool.  To install\nthe\n[`sockaddr`](https://github.com/hashicorp/go-sockaddr/tree/master/cmd/sockaddr),\nrun:\n\n```text\n$ go install github.com/hashicorp/go-sockaddr/cmd/sockaddr@latest\n```\n\nIf you're familiar with UNIX's `sockaddr` struct's, the following diagram\nmapping the C `sockaddr` (top) to `go-sockaddr` structs (bottom) and\ninterfaces will be helpful:\n\n```\n+-------------------------------------------------------+\n|                                                       |\n|                        sockaddr                       |\n|                        SockAddr                       |\n|                                                       |\n| +--------------+ +----------------------------------+ |\n| | sockaddr_un  | |                                  | |\n| | SockAddrUnix | |           sockaddr_in{,6}        | |\n| +--------------+ |                IPAddr            | |\n|                  |                                  | |\n|                  | +-------------+ +--------------+ | |\n|                  | | sockaddr_in | | sockaddr_in6 | | |\n|                  | |   IPv4Addr  | |   IPv6Addr   | | |\n|                  | +-------------+ +--------------+ | |\n|                  |                                  | |\n|                  +----------------------------------+ |\n|                                                       |\n+-------------------------------------------------------+\n```\n\n## Inspiration and Design\n\nThere were many subtle inspirations that led to this design, but the most direct\ninspiration for the filtering syntax was\nOpenBSD's\n[`pf.conf(5)`](https://www.freebsd.org/cgi/man.cgi?query=pf.conf\u0026apropos=0\u0026sektion=0\u0026arch=default\u0026format=html#PARAMETERS) firewall\nsyntax that lets you select the first IP address on a given named interface.\nThe original problem stemmed from:\n\n* needing to create immutable images using [Packer](https://www.packer.io) that\n  ran the [Consul](https://www.consul.io) process (Consul can only use one IP\n  address at a time);\n* images that may or may not have multiple interfaces or IP addresses at\n  runtime; and\n* we didn't want to rely on configuration management to render out the correct\n  IP address if the VM image was being used in an auto-scaling group.\n\nInstead we needed some way to codify a heuristic that would correctly select the\nright IP address but the input parameters were not known when the image was\ncreated.\n","funding_links":[],"categories":["Go"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhashicorp%2Fgo-sockaddr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhashicorp%2Fgo-sockaddr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhashicorp%2Fgo-sockaddr/lists"}