Skip to content

Commit

Permalink
Emit events when NAT status changes (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k authored and aschmahmann committed Feb 4, 2020
1 parent 53f3cc4 commit 373245d
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions p2p/host/autonat/autonat.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"
"time"

"github.com/libp2p/go-libp2p-core/event"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
Expand Down Expand Up @@ -61,6 +62,10 @@ type AmbientAutoNAT struct {
// If only a single autoNAT peer is known, then the confidence increases
// for each failure until it reaches 3.
confidence int

emitUnknown event.Emitter
emitPublic event.Emitter
emitPrivate event.Emitter
}

// NewAutoNAT creates a new ambient NAT autodiscovery instance attached to a host
Expand All @@ -70,12 +75,20 @@ func NewAutoNAT(ctx context.Context, h host.Host, getAddrs GetAddrs) AutoNAT {
getAddrs = h.Addrs
}

emitUnknown, _ := h.EventBus().Emitter(new(event.EvtLocalRoutabilityUnknown))
emitPublic, _ := h.EventBus().Emitter(new(event.EvtLocalRoutabilityPublic))
emitPrivate, _ := h.EventBus().Emitter(new(event.EvtLocalRoutabilityPrivate))

as := &AmbientAutoNAT{
ctx: ctx,
host: h,
getAddrs: getAddrs,
peers: make(map[peer.ID][]ma.Multiaddr),
status: NATStatusUnknown,

emitUnknown: emitUnknown,
emitPublic: emitPublic,
emitPrivate: emitPrivate,
}

h.Network().Notify(as)
Expand All @@ -90,6 +103,18 @@ func (as *AmbientAutoNAT) Status() NATStatus {
return as.status
}

func (as *AmbientAutoNAT) updateStatus(s NATStatus) {
as.status = s
switch s {
case NATStatusUnknown:
as.emitUnknown.Emit(event.EvtLocalRoutabilityUnknown{})
case NATStatusPublic:
as.emitPublic.Emit(event.EvtLocalRoutabilityPublic{})
case NATStatusPrivate:
as.emitPrivate.Emit(event.EvtLocalRoutabilityPrivate{})
}
}

func (as *AmbientAutoNAT) PublicAddr() (ma.Multiaddr, error) {
as.mx.Lock()
defer as.mx.Unlock()
Expand Down Expand Up @@ -194,7 +219,7 @@ func (as *AmbientAutoNAT) autodetect() {
} else if as.confidence < 3 {
as.confidence++
}
as.status = NATStatusPublic
as.updateStatus(NATStatusPublic)
as.addr = result.pubaddr
} else if result.private > 0 {
log.Debugf("NAT status is private")
Expand All @@ -204,14 +229,14 @@ func (as *AmbientAutoNAT) autodetect() {
} else if as.confidence < 3 {
as.confidence++
}
as.status = NATStatusPrivate
as.updateStatus(NATStatusPrivate)
as.addr = nil
} else if as.confidence > 0 {
// don't just flip to unknown, reduce confidence first
as.confidence--
} else {
log.Debugf("NAT status is unknown")
as.status = NATStatusUnknown
as.updateStatus(NATStatusUnknown)
as.addr = nil
}
as.mx.Unlock()
Expand Down

0 comments on commit 373245d

Please sign in to comment.