diff --git a/net/addrs/common.go b/net/addrs/common.go index 80cb93995..4f576bdca 100644 --- a/net/addrs/common.go +++ b/net/addrs/common.go @@ -53,6 +53,17 @@ func WrapInterfaces(ifaces []net.Interface) []NetInterfaceWithAddrCache { return m } +func AddrToIP(addr net.Addr) (net.IP, bool) { + switch ip := addr.(type) { + case *net.IPNet: + return ip.IP, true + case *net.IPAddr: + return ip.IP, true + default: + return nil, false + } +} + // GetAddr returns ipv4 only addresses for interface or cached one if set func (i NetInterfaceWithAddrCache) GetAddr() []net.Addr { if i.cachedAddrs != nil { @@ -67,8 +78,8 @@ func (i NetInterfaceWithAddrCache) GetAddr() []net.Addr { } // filter-out ipv6 i.cachedAddrs = slice.Filter(i.cachedAddrs, func(addr net.Addr) bool { - if ip, ok := addr.(*net.IPNet); ok { - if ip.IP.To4() == nil { + if ip, ok := AddrToIP(addr); ok { + if ip.To4() == nil { return false } } diff --git a/space/spacecore/localdiscovery/common.go b/space/spacecore/localdiscovery/common.go index b2cad7d67..d47970c13 100644 --- a/space/spacecore/localdiscovery/common.go +++ b/space/spacecore/localdiscovery/common.go @@ -88,13 +88,13 @@ func (l *localDiscovery) getDiscoveryPossibility(newAddrs addrs.InterfacesAddrs) continue } } - addrs := iface.GetAddr() - if len(addrs) == 0 { + ifaceAddrs := iface.GetAddr() + if len(ifaceAddrs) == 0 { continue } - for _, addr := range addrs { - if ip, ok := addr.(*gonet.IPNet); ok { - ipv4 := ip.IP.To4() + for _, addr := range ifaceAddrs { + if ip, ok := addrs.AddrToIP(addr); ok { + ipv4 := ip.To4() if ipv4 == nil { continue } @@ -147,7 +147,10 @@ func (l *localDiscovery) RegisterDiscoveryPossibilityHook(hook func(state Discov func (l *localDiscovery) getAddresses() (ipv4, ipv6 []gonet.IP) { for _, iface := range l.interfacesAddrs.Interfaces { for _, addr := range iface.GetAddr() { - ip := addr.(*gonet.IPNet).IP + ip, ok := addrs.AddrToIP(addr) + if !ok { + continue + } if ip.To4() != nil { ipv4 = append(ipv4, ip) } else {