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

Emit event for User's NAT Type i.e. Hard NAT or Easy NAT #1042

Merged
merged 14 commits into from Feb 19, 2021
5 changes: 3 additions & 2 deletions go.mod
Expand Up @@ -3,7 +3,7 @@ module github.com/libp2p/go-libp2p
go 1.12

require (
github.com/gogo/protobuf v1.3.2
github.com/gogo/protobuf v1.3.1
github.com/ipfs/go-cid v0.0.7
github.com/ipfs/go-datastore v0.4.5
github.com/ipfs/go-detect-race v0.0.1
Expand All @@ -17,7 +17,7 @@ require (
github.com/libp2p/go-libp2p-autonat v0.4.0
github.com/libp2p/go-libp2p-blankhost v0.2.0
github.com/libp2p/go-libp2p-circuit v0.4.0
github.com/libp2p/go-libp2p-core v0.8.2
github.com/libp2p/go-libp2p-core v0.8.4
github.com/libp2p/go-libp2p-discovery v0.5.0
github.com/libp2p/go-libp2p-loggables v0.1.0
github.com/libp2p/go-libp2p-mplex v0.4.1
Expand All @@ -41,4 +41,5 @@ require (
github.com/multiformats/go-multistream v0.2.1
github.com/stretchr/testify v1.6.1
github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9
go.uber.org/zap v1.16.0 // indirect
)
37 changes: 10 additions & 27 deletions go.sum

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions p2p/host/basic/basic_host.go
Expand Up @@ -209,9 +209,12 @@ func NewHost(ctx context.Context, n network.Network, opts *HostOpts) (*BasicHost

// we can't set this as a default above because it depends on the *BasicHost.
if h.disableSignedPeerRecord {
h.ids = identify.NewIDService(h, identify.UserAgent(opts.UserAgent), identify.DisableSignedPeerRecord())
h.ids, err = identify.NewIDService(h, identify.UserAgent(opts.UserAgent), identify.DisableSignedPeerRecord())
} else {
h.ids = identify.NewIDService(h, identify.UserAgent(opts.UserAgent))
h.ids, err = identify.NewIDService(h, identify.UserAgent(opts.UserAgent))
}
if err != nil {
return nil, fmt.Errorf("failed to create Identify service: %s", err)
}

if uint64(opts.NegotiationTimeout) != 0 {
Expand Down
17 changes: 11 additions & 6 deletions p2p/protocol/identify/id.go
Expand Up @@ -119,7 +119,7 @@ type IDService struct {

// NewIDService constructs a new *IDService and activates it by
// attaching its stream handler to the given host.Host.
func NewIDService(h host.Host, opts ...Option) *IDService {
func NewIDService(h host.Host, opts ...Option) (*IDService, error) {
var cfg config
for _, opt := range opts {
opt(&cfg)
Expand All @@ -135,10 +135,9 @@ func NewIDService(h host.Host, opts ...Option) *IDService {
Host: h,
UserAgent: userAgent,

ctx: hostCtx,
ctxCancel: cancel,
conns: make(map[network.Conn]chan struct{}),
observedAddrs: NewObservedAddrManager(hostCtx, h),
ctx: hostCtx,
ctxCancel: cancel,
conns: make(map[network.Conn]chan struct{}),

disableSignedPeerRecord: cfg.disableSignedPeerRecord,

Expand All @@ -149,6 +148,12 @@ func NewIDService(h host.Host, opts ...Option) *IDService {
// handle local protocol handler updates, and push deltas to peers.
var err error

observedAddrs, err := NewObservedAddrManager(hostCtx, h)
if err != nil {
return nil, fmt.Errorf("failed to create observed address manager: %s", err)
}
s.observedAddrs = observedAddrs

s.refCount.Add(1)
go s.loop()

Expand All @@ -171,7 +176,7 @@ func NewIDService(h host.Host, opts ...Option) *IDService {
h.SetStreamHandler(IDPush, s.pushHandler)

h.Network().Notify((*netNotifiee)(s))
return s
return s, nil
}

func (ids *IDService) loop() {
Expand Down
6 changes: 4 additions & 2 deletions p2p/protocol/identify/id_glass_test.go
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/stretchr/testify/require"

blhost "github.com/libp2p/go-libp2p-blankhost"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
Expand All @@ -20,7 +21,8 @@ func TestFastDisconnect(t *testing.T) {

target := blhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
defer target.Close()
ids := NewIDService(target)
ids, err := NewIDService(target)
require.NoError(t, err)
defer ids.Close()

sync := make(chan struct{})
Expand All @@ -46,7 +48,7 @@ func TestFastDisconnect(t *testing.T) {
source := blhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
defer source.Close()

err := source.Connect(ctx, peer.AddrInfo{ID: target.ID(), Addrs: target.Addrs()})
err = source.Connect(ctx, peer.AddrInfo{ID: target.ID(), Addrs: target.Addrs()})
if err != nil {
t.Fatal(err)
}
Expand Down
87 changes: 64 additions & 23 deletions p2p/protocol/identify/id_test.go
Expand Up @@ -43,8 +43,12 @@ func subtestIDService(t *testing.T) {
h1p := h1.ID()
h2p := h2.ID()

ids1 := identify.NewIDService(h1)
ids2 := identify.NewIDService(h2)
ids1, err := identify.NewIDService(h1)
require.NoError(t, err)

ids2, err := identify.NewIDService(h2)
require.NoError(t, err)

defer ids1.Close()
defer ids2.Close()

Expand Down Expand Up @@ -337,9 +341,15 @@ func TestLocalhostAddrFiltering(t *testing.T) {
Addrs: p2addrs[1:],
})

ids1 := identify.NewIDService(p1)
ids2 := identify.NewIDService(p2)
ids3 := identify.NewIDService(p3)
ids1, err := identify.NewIDService(p1)
require.NoError(t, err)

ids2, err := identify.NewIDService(p2)
require.NoError(t, err)

ids3, err := identify.NewIDService(p3)
require.NoError(t, err)

defer func() {
ids1.Close()
ids2.Close()
Expand Down Expand Up @@ -380,8 +390,12 @@ func TestIdentifyDeltaOnProtocolChange(t *testing.T) {

h2.SetStreamHandler(protocol.TestingID, func(_ network.Stream) {})

ids1 := identify.NewIDService(h1)
ids2 := identify.NewIDService(h2)
ids1, err := identify.NewIDService(h1)
require.NoError(t, err)

ids2, err := identify.NewIDService(h2)
require.NoError(t, err)

defer func() {
ids1.Close()
ids2.Close()
Expand Down Expand Up @@ -505,8 +519,12 @@ func TestIdentifyDeltaWhileIdentifyingConn(t *testing.T) {
defer h2.Close()
defer h1.Close()

ids1 := identify.NewIDService(h1)
ids2 := identify.NewIDService(h2)
ids1, err := identify.NewIDService(h1)
require.NoError(t, err)

ids2, err := identify.NewIDService(h2)
require.NoError(t, err)

defer ids1.Close()
defer ids2.Close()

Expand Down Expand Up @@ -573,8 +591,11 @@ func TestIdentifyPushOnAddrChange(t *testing.T) {
h1p := h1.ID()
h2p := h2.ID()

ids1 := identify.NewIDService(h1)
ids2 := identify.NewIDService(h2)
ids1, err := identify.NewIDService(h1)
require.NoError(t, err)
ids2, err := identify.NewIDService(h2)
require.NoError(t, err)

defer ids1.Close()
defer ids2.Close()

Expand Down Expand Up @@ -724,14 +745,18 @@ func TestSendPushIfDeltaNotSupported(t *testing.T) {
defer h2.Close()
defer h1.Close()

ids1 := identify.NewIDService(h1)
ids2 := identify.NewIDService(h2)
ids1, err := identify.NewIDService(h1)
require.NoError(t, err)

ids2, err := identify.NewIDService(h2)
require.NoError(t, err)

defer func() {
ids1.Close()
ids2.Close()
}()

err := h1.Connect(ctx, peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()})
err = h1.Connect(ctx, peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()})
require.NoError(t, err)

// wait for them to Identify each other
Expand Down Expand Up @@ -795,8 +820,12 @@ func TestLargeIdentifyMessage(t *testing.T) {
h1p := h1.ID()
h2p := h2.ID()

ids1 := identify.NewIDService(h1)
ids2 := identify.NewIDService(h2)
ids1, err := identify.NewIDService(h1)
require.NoError(t, err)

ids2, err := identify.NewIDService(h2)
require.NoError(t, err)

defer ids1.Close()
defer ids2.Close()

Expand Down Expand Up @@ -905,8 +934,12 @@ func TestLargePushMessage(t *testing.T) {
h1p := h1.ID()
h2p := h2.ID()

ids1 := identify.NewIDService(h1)
ids2 := identify.NewIDService(h2)
ids1, err := identify.NewIDService(h1)
require.NoError(t, err)

ids2, err := identify.NewIDService(h2)
require.NoError(t, err)

defer ids1.Close()
defer ids2.Close()

Expand Down Expand Up @@ -996,8 +1029,12 @@ func TestIdentifyResponseReadTimeout(t *testing.T) {
defer h2.Close()

h2p := h2.ID()
ids1 := identify.NewIDService(h1)
ids2 := identify.NewIDService(h2)
ids1, err := identify.NewIDService(h1)
require.NoError(t, err)

ids2, err := identify.NewIDService(h2)
require.NoError(t, err)

defer ids1.Close()
defer ids2.Close()
// remote stream handler will just hang and not send back an identify response
Expand Down Expand Up @@ -1039,16 +1076,20 @@ func TestIncomingIDStreamsTimeout(t *testing.T) {
defer h1.Close()
defer h2.Close()

ids1 := identify.NewIDService(h1)
ids2 := identify.NewIDService(h2)
ids1, err := identify.NewIDService(h1)
require.NoError(t, err)

ids2, err := identify.NewIDService(h2)
require.NoError(t, err)

defer ids1.Close()
defer ids2.Close()

h2p := h2.ID()
h2pi := h2.Peerstore().PeerInfo(h2p)
require.NoError(t, h1.Connect(ctx, h2pi))

_, err := h1.NewStream(ctx, h2p, p)
_, err = h1.NewStream(ctx, h2p, p)
require.NoError(t, err)

// remote peer should eventually reset stream
Expand Down