Skip to content

Commit

Permalink
fix(net/nat): data race problem of extAddr
Browse files Browse the repository at this point in the history
  • Loading branch information
wlynxg committed Jan 16, 2025
1 parent 4651a0d commit bdfee17
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions p2p/net/nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/netip"
"sync"
"sync/atomic"
"time"

logging "github.com/ipfs/go-log/v2"
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -103,15 +104,15 @@ 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}]
// The mapping may have an invalid port.
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.
Expand Down Expand Up @@ -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)))
Expand Down

0 comments on commit bdfee17

Please sign in to comment.