Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't use invalid port 0 when NATMap fails #3058

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading