diff --git a/p2p/net/nat/nat.go b/p2p/net/nat/nat.go index 23af58075a..ebaa167568 100644 --- a/p2p/net/nat/nat.go +++ b/p2p/net/nat/nat.go @@ -6,6 +6,7 @@ import ( "fmt" "net/netip" "sync" + "sync/atomic" "time" logging "github.com/ipfs/go-log/v2" @@ -56,11 +57,11 @@ func DiscoverNAT(ctx context.Context) (*NAT, error) { ctx, cancel := context.WithCancel(context.Background()) nat := &NAT{ nat: natInstance, - extAddr: extAddr, mappings: make(map[entry]int), ctx: ctx, ctxCancel: cancel, } + nat.extAddr.Store(&extAddr) nat.refCount.Add(1) go func() { defer nat.refCount.Done() @@ -77,7 +78,7 @@ type NAT struct { natmu sync.Mutex nat nat.NAT // External IP of the NAT. Will be renewed periodically (every CacheTime). - extAddr netip.Addr + extAddr atomic.Pointer[netip.Addr] refCount sync.WaitGroup ctx context.Context @@ -103,7 +104,7 @@ func (nat *NAT) GetMapping(protocol string, port int) (addr netip.AddrPort, foun nat.mappingmu.Lock() defer nat.mappingmu.Unlock() - if !nat.extAddr.IsValid() { + if !nat.extAddr.Load().IsValid() { return netip.AddrPort{}, false } extPort, found := nat.mappings[entry{protocol: protocol, port: port}] @@ -111,7 +112,7 @@ func (nat *NAT) GetMapping(protocol string, port int) (addr netip.AddrPort, foun if !found || extPort == 0 { return netip.AddrPort{}, false } - return netip.AddrPortFrom(nat.extAddr, uint16(extPort)), true + return netip.AddrPortFrom(*nat.extAddr.Load(), uint16(extPort)), true } // AddMapping attempts to construct a mapping on protocol and internal port. @@ -206,7 +207,7 @@ func (nat *NAT) background() { if err == nil { extAddr, _ = netip.AddrFromSlice(extIP) } - nat.extAddr = extAddr + nat.extAddr.Store(&extAddr) nextAddrUpdate = time.Now().Add(CacheTime) } t.Reset(time.Until(minTime(nextAddrUpdate, nextMappingUpdate)))