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

autonat: don't emit reachability changed events on address change #2092

Merged
merged 4 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
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
20 changes: 6 additions & 14 deletions p2p/host/autonat/autonat.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,13 @@ func (as *AmbientAutoNAT) scheduleProbe() time.Duration {
// Update the current status based on an observed result.
func (as *AmbientAutoNAT) recordObservation(observation autoNATResult) {
currentStatus := as.status.Load()

if observation.Reachability == network.ReachabilityPublic {
log.Debugf("NAT status is public")
changed := false
if currentStatus.Reachability != network.ReachabilityPublic {
// Aggressively switch to public from other states ignoring confidence

// we are flipping our NATStatus, so confidence drops to 0
as.confidence = 0
if as.service != nil {
Expand All @@ -299,21 +302,13 @@ func (as *AmbientAutoNAT) recordObservation(observation autoNATResult) {
} else if as.confidence < 3 {
as.confidence++
}
if observation.address != nil {
if !changed && currentStatus.address != nil && !observation.address.Equal(currentStatus.address) {
as.confidence--
}
if currentStatus.address == nil || !observation.address.Equal(currentStatus.address) {
changed = true
}
as.status.Store(&observation)
}
if observation.address != nil && changed {
as.status.Store(&observation)
if changed {
as.emitStatus()
}
} else if observation.Reachability == network.ReachabilityPrivate {
log.Debugf("NAT status is private")
if currentStatus.Reachability == network.ReachabilityPublic {
if currentStatus.Reachability != network.ReachabilityPrivate {
if as.confidence > 0 {
as.confidence--
} else {
Expand All @@ -328,9 +323,6 @@ func (as *AmbientAutoNAT) recordObservation(observation autoNATResult) {
} else if as.confidence < 3 {
as.confidence++
as.status.Store(&observation)
if currentStatus.Reachability != network.ReachabilityPrivate {
as.emitStatus()
}
}
} else if as.confidence > 0 {
// don't just flip to unknown, reduce confidence first
Expand Down
24 changes: 11 additions & 13 deletions p2p/host/autonat/autonat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,19 +209,6 @@ func TestAutoNATObservationRecording(t *testing.T) {
t.Fatalf("failed to subscribe to event EvtLocalRoutabilityPublic, err=%s", err)
}

// pubic observation without address should be ignored.
an.recordObservation(autoNATResult{network.ReachabilityPublic, nil})
if an.Status() != network.ReachabilityUnknown {
t.Fatalf("unexpected transition")
}

select {
case <-s.Out():
t.Fatal("not expecting a public reachability event")
default:
// expected
}

addr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
an.recordObservation(autoNATResult{network.ReachabilityPublic, addr})
if an.Status() != network.ReachabilityPublic {
Expand Down Expand Up @@ -252,6 +239,17 @@ func TestAutoNATObservationRecording(t *testing.T) {
t.Fatalf("too-extreme private transition.")
}

// don't emit events on observed address change
newAddr, _ := ma.NewMultiaddr("/ip4/127.0.0.1/udp/12345")
an.recordObservation(autoNATResult{network.ReachabilityPublic, newAddr})
if an.Status() != network.ReachabilityPublic {
t.Fatalf("reachability should stay public")
}
select {
case <-s.Out():
t.Fatal("received event without state transition")
case <-time.After(300 * time.Millisecond):
}
}

func TestStaticNat(t *testing.T) {
Expand Down