-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
nat: add HasNAT method for checking NAT environments #2346
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -843,16 +843,14 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr { | |
|
||
finalAddrs = network.DedupAddrs(finalAddrs) | ||
|
||
// natmgr is nil if we do not use nat option; | ||
if h.natmgr != nil { | ||
// We have successfully mapped ports on our NAT. Use those | ||
// instead of observed addresses (mostly). | ||
|
||
// Next, apply this mapping to our addresses. | ||
// use nat mappings if we have them | ||
if h.natmgr != nil && h.natmgr.HasNAT() { | ||
// Use nat mappings for our addresses, if we have them. | ||
var unmapped []ma.Multiaddr | ||
for _, listen := range listenAddrs { | ||
extMaddr := h.natmgr.GetMapping(listen) | ||
if extMaddr == nil { | ||
// not mapped | ||
unmapped = append(unmapped, listen) | ||
continue | ||
} | ||
|
||
|
@@ -871,16 +869,18 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr { | |
} | ||
|
||
// No. | ||
// in case the router gives us a wrong address or we're behind a double-NAT. | ||
// also add observed addresses | ||
resolved, err := manet.ResolveUnspecifiedAddress(listen, allIfaceAddrs) | ||
// In case the router gives us a wrong address or we're behind a double-NAT, | ||
// also add observed addresses. Check for both the listen address | ||
// and the resolved addresses. It is important to check for the listen address | ||
// as the local address on connections for some transports is unspecified. | ||
addrs, err := manet.ResolveUnspecifiedAddress(listen, allIfaceAddrs) | ||
if err != nil { | ||
// This can happen if we try to resolve /ip6/::/... | ||
// without any IPv6 interface addresses. | ||
continue | ||
} | ||
|
||
for _, addr := range resolved { | ||
addrs = append(addrs, listen) | ||
for _, addr := range addrs { | ||
// Now, check if we have any observed addresses that | ||
// differ from the one reported by the router. Routers | ||
// don't always give the most accurate information. | ||
|
@@ -904,6 +904,19 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr { | |
} | ||
} | ||
} | ||
// Add observed addresses for umapped addresses | ||
for _, listen := range unmapped { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This didn't exist before. What did this fix for you? This codepath gets hit in the case we discover a NAT device, but fail to register a mapping, is that correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. If you are unable to do a nat mapping after discovering a NAT device this code path fixes it. |
||
addrs, err := manet.ResolveUnspecifiedAddress(listen, allIfaceAddrs) | ||
if err != nil { | ||
// This can happen if we try to resolve /ip6/::/... | ||
// without any IPv6 interface addresses. | ||
continue | ||
} | ||
addrs = append(addrs, listen) | ||
for _, addr := range addrs { | ||
finalAddrs = append(finalAddrs, h.ids.ObservedAddrsFor(addr)...) | ||
} | ||
} | ||
} else { | ||
var observedAddrs []ma.Multiaddr | ||
if h.ids != nil { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import ( | |
// and tries to obtain port mappings for those. | ||
type NATManager interface { | ||
GetMapping(ma.Multiaddr) ma.Multiaddr | ||
HasNAT() bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should call this Maybe call it |
||
io.Closer | ||
} | ||
|
||
|
@@ -86,6 +87,12 @@ func (nmgr *natManager) Close() error { | |
return nil | ||
} | ||
|
||
func (nmgr *natManager) HasNAT() bool { | ||
nmgr.natMx.RLock() | ||
defer nmgr.natMx.RUnlock() | ||
return nmgr.nat != nil | ||
} | ||
|
||
func (nmgr *natManager) background(ctx context.Context) { | ||
defer nmgr.refCount.Done() | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little suspicious about this code in general. Why do we trust the NAT device to give us the correct external port, but not the IP address? Or, in other words, how likely is it that the NAT device gives us the correct port mapping but the incorrect public IP address?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not about the NAT giving the IP address, this is about this line returning the correct addresses for the listen address.
#2346