-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
signalling for hole punching #1168
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3e4c436
hole punching protocol
aarshkshah1992 0cea0ab
implement the new retry logic (opening a new stream per hole punch)
marten-seemann 8f7f08e
rename the holepunch.HolePunchService to holepunch.Service
marten-seemann a19072e
remove stream handler when closing
marten-seemann 4903d38
remove misleading comment from protobuf, add link to spec
marten-seemann 49bc8ef
remove unnecessary netNotifiee.HolePunchService
marten-seemann 4023d84
simplify receiving from the timer channel
marten-seemann 18045c9
don't defer the closing of the hole punching stream
marten-seemann 43f5080
only accept hole punch streams from relayed peers
marten-seemann 380a2c4
refactor error logging
marten-seemann df6924a
fix incorrect counting of incoming hole punch attempts
marten-seemann 1be1555
rename HolePunch to DirectConnect
marten-seemann 93eebc0
fix usage of the wait group to close the hole punch service
marten-seemann 03d3a5d
use a gc to the garbage collect the holepunch tracer
marten-seemann e1edbca
check directionality of connection when accepting the hole punch stream
marten-seemann 2605890
add comment why we only call Connect once
marten-seemann 84900aa
move deduplication logic to the holepunch service
marten-seemann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |
"github.com/libp2p/go-libp2p-core/peerstore" | ||
"github.com/libp2p/go-libp2p-core/protocol" | ||
"github.com/libp2p/go-libp2p-core/record" | ||
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch" | ||
|
||
addrutil "github.com/libp2p/go-addr-util" | ||
"github.com/libp2p/go-eventbus" | ||
|
@@ -72,6 +73,7 @@ type BasicHost struct { | |
network network.Network | ||
mux *msmux.MultistreamMuxer | ||
ids *identify.IDService | ||
hps *holepunch.Service | ||
pings *ping.PingService | ||
natmgr NATManager | ||
maResolver *madns.Resolver | ||
|
@@ -136,6 +138,11 @@ type HostOpts struct { | |
|
||
// DisableSignedPeerRecord disables the generation of Signed Peer Records on this host. | ||
DisableSignedPeerRecord bool | ||
|
||
// EnableHolePunching enables the peer to initiate/respond to hole punching attempts for NAT traversal. | ||
EnableHolePunching bool | ||
// HolePunchingOptions are options for the hole punching service | ||
HolePunchingOptions []holepunch.Option | ||
} | ||
|
||
// NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network. | ||
|
@@ -208,6 +215,13 @@ func NewHost(ctx context.Context, n network.Network, opts *HostOpts) (*BasicHost | |
return nil, fmt.Errorf("failed to create Identify service: %s", err) | ||
} | ||
|
||
if opts.EnableHolePunching { | ||
h.hps, err = holepunch.NewService(h, h.ids, opts.HolePunchingOptions...) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create hole punch service: %w", err) | ||
} | ||
} | ||
|
||
if uint64(opts.NegotiationTimeout) != 0 { | ||
h.negtimeout = opts.NegotiationTimeout | ||
} | ||
|
@@ -610,7 +624,7 @@ func (h *BasicHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.I | |
} | ||
case <-ctx.Done(): | ||
s.Reset() | ||
// wait for the negotiation to cancel. | ||
// wait for `SelectOneOf` to error out because of resetting the stream. | ||
<-errCh | ||
return nil, ctx.Err() | ||
} | ||
|
@@ -643,8 +657,11 @@ func (h *BasicHost) Connect(ctx context.Context, pi peer.AddrInfo) error { | |
// absorb addresses into peerstore | ||
h.Peerstore().AddAddrs(pi.ID, pi.Addrs, peerstore.TempAddrTTL) | ||
|
||
if h.Network().Connectedness(pi.ID) == network.Connected { | ||
return nil | ||
forceDirect, _ := network.GetForceDirectDial(ctx) | ||
if !forceDirect { | ||
if h.Network().Connectedness(pi.ID) == network.Connected { | ||
return nil | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like we need some way to check for connections with specific properties, right? Or do we just want to always dial in this case anyways. |
||
|
||
resolved, err := h.resolveAddrs(ctx, h.Peerstore().PeerInfo(pi.ID)) | ||
|
@@ -991,6 +1008,10 @@ func (h *BasicHost) Close() error { | |
h.autoNat.Close() | ||
} | ||
|
||
if h.hps != nil { | ||
h.hps.Close() | ||
} | ||
|
||
_ = h.emitters.evtLocalProtocolsUpdated.Close() | ||
_ = h.emitters.evtLocalAddrsUpdated.Close() | ||
h.Network().Close() | ||
|
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Future: Dependency injection would make this a lot less invasive.