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 Dec 4, 2024
1 parent 288868c commit a0144ed
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,8 +905,23 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
continue
}

// Drop the IP from the external maddr
_, extMaddrNoIP := ma.SplitFirst(extMaddr)

// Determine whether the mapped port is valid
isValidPort := true
ma.ForEach(extMaddrNoIP, func(c ma.Component) bool {
switch c.Protocol().Code {
case ma.P_TCP, ma.P_UDP:
isValidPort = c.Value() != "0"
return false
default:
return true
}
})

// if the router reported a sane address
if !manet.IsIPUnspecified(extMaddr) {
if !manet.IsIPUnspecified(extMaddr) && isValidPort {
// Add in the mapped addr.
finalAddrs = append(finalAddrs, extMaddr)
} else {
Expand Down Expand Up @@ -939,17 +954,20 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
continue
}

// Drop the IP from the external maddr
_, extMaddrNoIP := ma.SplitFirst(extMaddr)

for _, obsMaddr := range observed {
// Extract a public observed addr.
ip, _ := ma.SplitFirst(obsMaddr)
if ip == nil || !manet.IsPublicAddr(ip) {
continue
}

finalAddrs = append(finalAddrs, ma.Join(ip, extMaddrNoIP))
// If the port is a valid port, concatenate the observed address and the mapped port;
// otherwise, add the observed address directly
if isValidPort {
finalAddrs = append(finalAddrs, ma.Join(ip, extMaddrNoIP))
} else {
finalAddrs = append(finalAddrs, obsMaddr)
}
}
}
}
Expand Down

0 comments on commit a0144ed

Please sign in to comment.