net/interfaces: sort returned addresses from LocalAddresses

Also change the type to netaddr.IP while here, because it made sorting
easier.

Updates tailscale/corp#1397

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/1451/head
Brad Fitzpatrick 3 years ago committed by Brad Fitzpatrick
parent ad6edf5ecd
commit 829eb8363a

@ -78,7 +78,7 @@ func isProblematicInterface(nif *net.Interface) bool {
// LocalAddresses returns the machine's IP addresses, separated by // LocalAddresses returns the machine's IP addresses, separated by
// whether they're loopback addresses. // whether they're loopback addresses.
func LocalAddresses() (regular, loopback []string, err error) { func LocalAddresses() (regular, loopback []netaddr.IP, err error) {
// TODO(crawshaw): don't serve interface addresses that we are routing // TODO(crawshaw): don't serve interface addresses that we are routing
ifaces, err := net.Interfaces() ifaces, err := net.Interfaces()
if err != nil { if err != nil {
@ -117,16 +117,22 @@ func LocalAddresses() (regular, loopback []string, err error) {
continue continue
} }
if ip.IsLoopback() || ifcIsLoopback { if ip.IsLoopback() || ifcIsLoopback {
loopback = append(loopback, ip.String()) loopback = append(loopback, ip)
} else { } else {
regular = append(regular, ip.String()) regular = append(regular, ip)
} }
} }
} }
} }
sortIPs(regular)
sortIPs(loopback)
return regular, loopback, nil return regular, loopback, nil
} }
func sortIPs(s []netaddr.IP) {
sort.Slice(s, func(i, j int) bool { return s[i].Less(s[j]) })
}
// Interface is a wrapper around Go's net.Interface with some extra methods. // Interface is a wrapper around Go's net.Interface with some extra methods.
type Interface struct { type Interface struct {
*net.Interface *net.Interface

@ -1055,8 +1055,8 @@ func (c *Conn) determineEndpoints(ctx context.Context) (ipPorts []string, reason
ips = loopback ips = loopback
reason = "loopback" reason = "loopback"
} }
for _, ipStr := range ips { for _, ip := range ips {
addAddr(net.JoinHostPort(ipStr, fmt.Sprint(localAddr.Port)), reason) addAddr(netaddr.IPPort{IP: ip, Port: uint16(localAddr.Port)}.String(), reason)
} }
} else { } else {
// Our local endpoint is bound to a particular address. // Our local endpoint is bound to a particular address.

Loading…
Cancel
Save