Skip to content

Commit

Permalink
Merge pull request #7 from libp2p/feat/peer-addrs
Browse files Browse the repository at this point in the history
track autonat peer addresses
  • Loading branch information
vyzo authored Dec 21, 2018
2 parents a45f261 + 179ba24 commit 77a2de0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
28 changes: 15 additions & 13 deletions p2p/host/autonat/autonat.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
host "github.com/libp2p/go-libp2p-host"
inet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
ma "github.com/multiformats/go-multiaddr"
)

Expand Down Expand Up @@ -51,7 +52,7 @@ type AmbientAutoNAT struct {
getAddrs GetAddrs

mx sync.Mutex
peers map[peer.ID]struct{}
peers map[peer.ID][]ma.Multiaddr
status NATStatus
addr ma.Multiaddr
// Reflects the confidence on of the NATStatus being private, as a single
Expand All @@ -73,7 +74,7 @@ func NewAutoNAT(ctx context.Context, h host.Host, getAddrs GetAddrs) AutoNAT {
ctx: ctx,
host: h,
getAddrs: getAddrs,
peers: make(map[peer.ID]struct{}),
peers: make(map[peer.ID][]ma.Multiaddr),
status: NATStatusUnknown,
}

Expand Down Expand Up @@ -134,14 +135,15 @@ func (as *AmbientAutoNAT) autodetect() {
cli := NewAutoNATClient(as.host, as.getAddrs)
failures := 0

for _, p := range peers {
for _, pi := range peers {
ctx, cancel := context.WithTimeout(as.ctx, AutoNATRequestTimeout)
a, err := cli.DialBack(ctx, p)
as.host.Peerstore().AddAddrs(pi.ID, pi.Addrs, pstore.TempAddrTTL)
a, err := cli.DialBack(ctx, pi.ID)
cancel()

switch {
case err == nil:
log.Debugf("NAT status is public; address through %s: %s", p.Pretty(), a.String())
log.Debugf("NAT status is public; address through %s: %s", pi.ID.Pretty(), a.String())
as.mx.Lock()
as.addr = a
as.status = NATStatusPublic
Expand All @@ -150,7 +152,7 @@ func (as *AmbientAutoNAT) autodetect() {
return

case IsDialError(err):
log.Debugf("dial error through %s: %s", p.Pretty(), err.Error())
log.Debugf("dial error through %s: %s", pi.ID.Pretty(), err.Error())
failures++
if failures >= 3 || as.confidence >= 3 { // 3 times is enemy action
log.Debugf("NAT status is private")
Expand All @@ -162,7 +164,7 @@ func (as *AmbientAutoNAT) autodetect() {
}

default:
log.Debugf("Error dialing through %s: %s", p.Pretty(), err.Error())
log.Debugf("Error dialing through %s: %s", pi.ID.Pretty(), err.Error())
}
}

Expand All @@ -179,21 +181,21 @@ func (as *AmbientAutoNAT) autodetect() {
as.mx.Unlock()
}

func (as *AmbientAutoNAT) getPeers() []peer.ID {
func (as *AmbientAutoNAT) getPeers() []pstore.PeerInfo {
as.mx.Lock()
defer as.mx.Unlock()

if len(as.peers) == 0 {
return nil
}

var connected, others []peer.ID
var connected, others []pstore.PeerInfo

for p := range as.peers {
for p, addrs := range as.peers {
if as.host.Network().Connectedness(p) == inet.Connected {
connected = append(connected, p)
connected = append(connected, pstore.PeerInfo{ID: p, Addrs: addrs})
} else {
others = append(others, p)
others = append(others, pstore.PeerInfo{ID: p, Addrs: addrs})
}
}

Expand All @@ -207,7 +209,7 @@ func (as *AmbientAutoNAT) getPeers() []peer.ID {
}
}

func shufflePeers(peers []peer.ID) {
func shufflePeers(peers []pstore.PeerInfo) {
for i := range peers {
j := rand.Intn(i + 1)
peers[i], peers[j] = peers[j], peers[i]
Expand Down
2 changes: 1 addition & 1 deletion p2p/host/autonat/autonat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func newDialResponseError(status pb.Message_ResponseStatus, text string) *pb.Mes
func makeAutoNAT(ctx context.Context, t *testing.T, ash host.Host) (host.Host, AutoNAT) {
h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
a := NewAutoNAT(ctx, h, nil)
a.(*AmbientAutoNAT).peers[ash.ID()] = struct{}{}
a.(*AmbientAutoNAT).peers[ash.ID()] = ash.Addrs()

return h, a
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/host/autonat/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (as *AmbientAutoNAT) Connected(net inet.Network, c inet.Conn) {
if len(protos) > 0 {
log.Infof("Discovered AutoNAT peer %s", p.Pretty())
as.mx.Lock()
as.peers[p] = struct{}{}
as.peers[p] = as.host.Peerstore().Addrs(p)
as.mx.Unlock()
}
}()
Expand Down

0 comments on commit 77a2de0

Please sign in to comment.