1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-07 21:37:04 +09:00

GO-4807 p2p: iface ip conversion fix

This commit is contained in:
Roman Khafizianov 2025-03-24 20:08:53 +01:00
parent 520223346d
commit e2c1bf41c7
No known key found for this signature in database
GPG key ID: F07A7D55A2684852
2 changed files with 22 additions and 8 deletions

View file

@ -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
}
}

View file

@ -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 {