Skip to content

Commit

Permalink
swarm: remove /ip6zone component from listen addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Dec 6, 2023
1 parent 97b4ca0 commit 88a0008
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
22 changes: 14 additions & 8 deletions p2p/net/swarm/swarm_addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ func (s *Swarm) ListenAddresses() []ma.Multiaddr {
func (s *Swarm) listenAddressesNoLock() []ma.Multiaddr {
addrs := make([]ma.Multiaddr, 0, len(s.listeners.m)+10) // A bit extra so we may avoid an extra allocation in the for loop below.
for l := range s.listeners.m {
addrs = append(addrs, l.Multiaddr())
a := l.Multiaddr()
// remove ip6zone from the addresses
ma.ForEach(a, func(c ma.Component) bool {
if c.Protocol().Code == ma.P_IP6ZONE {
_, a = ma.SplitFirst(a)
}
return false
})
if a != nil {
addrs = append(addrs, a)
}
}
return addrs
}
Expand All @@ -29,25 +39,21 @@ const ifaceAddrsCacheDuration = 1 * time.Minute
// listens. It expands "any interface" addresses (/ip4/0.0.0.0, /ip6/::) to
// use the known local interfaces.
func (s *Swarm) InterfaceListenAddresses() ([]ma.Multiaddr, error) {
s.listeners.RLock() // RLock start

s.listeners.RLock()
ifaceListenAddres := s.listeners.ifaceListenAddres
isEOL := time.Now().After(s.listeners.cacheEOL)
s.listeners.RUnlock() // RLock end
s.listeners.RUnlock()

if !isEOL {
// Cache is valid, clone the slice
return append(ifaceListenAddres[:0:0], ifaceListenAddres...), nil
}

// Cache is not valid
// Perfrom double checked locking

s.listeners.Lock() // Lock start

ifaceListenAddres = s.listeners.ifaceListenAddres
isEOL = time.Now().After(s.listeners.cacheEOL)
if isEOL {
if time.Now().After(s.listeners.cacheEOL) {
// Cache is still invalid
listenAddres := s.listenAddressesNoLock()
if len(listenAddres) > 0 {
Expand Down
1 change: 1 addition & 0 deletions p2p/net/swarm/swarm_addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestDialBadAddrs(t *testing.T) {

test(m("/ip6/fe80::1")) // link local
test(m("/ip6/fe80::100")) // link local
test(m("/ip6zone/eth0/ip6/fe80::100")) // ip6zone
test(m("/ip4/127.0.0.1/udp/1234/utp")) // utp
}

Expand Down

0 comments on commit 88a0008

Please sign in to comment.