hashicorp / go-sockaddr

IP Address/UNIX Socket convenience functions for Go
Mozilla Public License 2.0
266 stars 53 forks source link

Fix convenience functions for dual-homed hosts #10

Closed sean- closed 7 years ago

sean- commented 7 years ago

Previously, on dual-homed hosts the GetPrivateIP, GetPublicIP, GetPrivateInterfaces, and GetPublicInterfaces methods could return incorrect values.

Suppose the hypothetical host:

Under this scenario, GetPublicIP and GetPublicInterfaces would succeed, but GetPrivateIP and GetPrivateInterfaces would not because GetPrivateInterfaces was incorrectly using GetDefaultInterfaces at its initial seed of IfAddrs. Instead, change the behavior to use GetAllInterfaces and then sort based on whether or not the interface has a default route, meaning we prefer IfAddr entries that have a default route, but it isn't required. Provide the same logic for GetPublicInterface.

Fix a bug where the flags selector was ORing two conditions: flags "forwardable|up" was incorrect, it should have been flags "forwardable" | flags "up". Without this change it was possible that GetPrivateIP could return the loopback address because it is up even if it is not forwardable.