[r3.5] p2p: re-resolve STUN external IP at runtime#22188
Merged
Conversation
nat=stun resolved the public IP only once at startup, so a node whose public IP changed while running (dynamic ISP IP behind a NAT router, or a directly-connected PPPoE/DHCP host) kept advertising a stale enode. Add a tracker scoped to the nat.STUN case that re-resolves every 10 minutes and applies the address only when it changes. The value is set via SetStaticIP (authoritative), so the spoofable discovery endpoint predictor cannot override it, and the resolved IP is validated before it reaches the local record. Other NAT modes (none, extip, upnp, pmp) are left untouched. Follow-up (separate PR): trigger an immediate, rate-limited re-resolve from OS network-change events (netlink/route socket/IP Helper) to cut detection latency on directly-connected dynamic-IP hosts.
Reject the IPv4 broadcast address and other non-unicast specials by requiring IsGlobalUnicast (which excludes broadcast/multicast/loopback/ unspecified/link-local) while still excluding private ranges.
AskAlexSharov
approved these changes
Jul 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cherry-pick of #22105 to release/3.5.
nat=stunresolved the public IP only once at startup, so a node whose public IP changed while running (dynamic ISP IP behind a NAT router, or a directly-connected PPPoE/DHCP host) kept advertising a stale enode. This re-resolves every 10 minutes and applies the address only when it changes to a usable (global-unicast, non-private) value, committed via SetStaticIP so the spoofable discovery endpoint predictor cannot override it. Other NAT modes (none, extip, upnp, pmp) are untouched.Includes the follow-up global-unicast validation tightening from the same PR.
Clean cherry-pick (rebased onto current release/3.5; the two touched files do not overlap the intervening changes).
Not included
The OS network-change event trigger (#22112, netlink/route-socket/IP-Helper) is still in review on main and will be backported separately once merged. This backport is the poll-based fix only, which is complete and correct on its own.
Verification
go test ./p2p/ -race— passmake lint(p2p) — 0 issues