Skip to content

Commit

Permalink
fix: don't use invalid port 0 when NATMap fails
Browse files Browse the repository at this point in the history
  • Loading branch information
wlynxg committed Nov 21, 2024
1 parent 288868c commit 9970d8e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,8 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {

finalAddrs = ma.Unique(finalAddrs)

// counts the number of addresses that are correctly port mapped
successMapCnt := 0
// use nat mappings if we have them
if h.natmgr != nil && h.natmgr.HasDiscoveredNAT() {
// We have successfully mapped ports on our NAT. Use those
Expand Down Expand Up @@ -929,6 +931,8 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
continue
}

successMapCnt++

for _, addr := range resolved {
// Now, check if we have any observed addresses that
// differ from the one reported by the router. Routers
Expand All @@ -953,7 +957,10 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
}
}
}
} else {
}

// if there is no properly handled port mapping address, add the observed address directly
if successMapCnt > 0 {
var observedAddrs []ma.Multiaddr
if h.ids != nil {
observedAddrs = h.ids.OwnObservedAddrs()
Expand Down
2 changes: 1 addition & 1 deletion p2p/net/nat/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (nat *NAT) GetMapping(protocol string, port int) (addr netip.AddrPort, foun
return netip.AddrPort{}, false
}
extPort, found := nat.mappings[entry{protocol: protocol, port: port}]
if !found {
if !found || extPort == 0 {
return netip.AddrPort{}, false
}
return netip.AddrPortFrom(nat.extAddr, uint16(extPort)), true
Expand Down

0 comments on commit 9970d8e

Please sign in to comment.