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 address advertisement bugs #974

Merged
merged 9 commits into from
Jul 7, 2020
24 changes: 13 additions & 11 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,24 +287,24 @@ func (h *BasicHost) updateLocalIpAddr() {
// Resolve the interface addresses
ifaceAddrs, err := manet.InterfaceMultiaddrs()
if err != nil {
// This usually shouldn't happen, but we could be in some kind
// of funky restricted environment.
log.Errorw("failed to resolve local interface addresses", "error", err)
} else {
for _, addr := range ifaceAddrs {
// Skip link-local addrs, they're mostly useless.
if !manet.IsIP6LinkLocal(addr) {
h.allInterfaceAddrs = append(h.allInterfaceAddrs, addr)
}
}
}

// If we failed to lookup interface addrs.
if len(h.allInterfaceAddrs) == 0 {
// Add the loopback addresses to the filtered addrs and use them as the non-filtered addrs.
// Then bail. There's nothing else we can do here.
h.filteredInterfaceAddrs = append(h.filteredInterfaceAddrs, manet.IP4Loopback, manet.IP6Loopback)
h.allInterfaceAddrs = h.filteredInterfaceAddrs
return
}

for _, addr := range ifaceAddrs {
// Skip link-local addrs, they're mostly useless.
if !manet.IsIP6LinkLocal(addr) {
h.allInterfaceAddrs = append(h.allInterfaceAddrs, addr)
}
}

// If netroute failed to get us any interface addresses, use all of
// them.
if len(h.filteredInterfaceAddrs) == 0 {
Expand Down Expand Up @@ -904,6 +904,8 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
if !manet.IsIPUnspecified(extMaddr) {
// Add in the mapped addr.
finalAddrs = append(finalAddrs, extMaddr)
} else {
log.Warn("NAT device reported an unspecified IP as it's external address")
}

// Did the router give us a routable public addr?
Expand All @@ -913,7 +915,7 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
}

// No.
// in case router give us a wrong address.
// in case router give us a wrong address or we're behind a double-NAT.
Stebalien marked this conversation as resolved.
Show resolved Hide resolved
// also add observed addresses
resolved, err := addrutil.ResolveUnspecifiedAddress(listen, allIfaceAddrs)
if err != nil {
Expand Down