This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Support for Hole punching #233
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7d13adb
support for forced direct connections draft 1
aarshkshah1992 59a5292
open streams over direct connections
aarshkshah1992 db3134d
update ctx and docs
aarshkshah1992 979b039
do not dial private addrs for hole punching
aarshkshah1992 fca9031
force direct connections
aarshkshah1992 bd88bb2
Merge branch 'master' into feat/hole-punching
aarshkshah1992 3c21711
fmt
aarshkshah1992 b55ba31
address review
aarshkshah1992 f433114
fix conflicts
aarshkshah1992 56ecb0f
address changes
aarshkshah1992 8b8bc9a
restore newline
aarshkshah1992 874c817
fixed loop
aarshkshah1992 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
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 |
---|---|---|
|
@@ -251,9 +251,14 @@ func (s *Swarm) dialPeer(ctx context.Context, p peer.ID) (*Conn, error) { | |
|
||
defer log.EventBegin(ctx, "swarmDialAttemptSync", p).Done() | ||
|
||
// check if we already have an open connection first | ||
conn := s.bestConnToPeer(p) | ||
if conn != nil { | ||
forceDirect, _ := network.GetForceDirectDial(ctx) | ||
if forceDirect { | ||
vyzo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if isDirectConn(conn) { | ||
return conn, nil | ||
} | ||
} else if conn != nil { | ||
// check if we already have an open connection first | ||
return conn, nil | ||
} | ||
|
||
|
@@ -287,8 +292,13 @@ func (s *Swarm) doDial(ctx context.Context, p peer.ID) (*Conn, error) { | |
// Short circuit. | ||
// By the time we take the dial lock, we may already *have* a connection | ||
// to the peer. | ||
forceDirect, _ := network.GetForceDirectDial(ctx) | ||
c := s.bestConnToPeer(p) | ||
if c != nil { | ||
if forceDirect { | ||
if isDirectConn(c) { | ||
return c, nil | ||
} | ||
} else if c != nil { | ||
return c, nil | ||
} | ||
|
||
|
@@ -301,12 +311,17 @@ func (s *Swarm) doDial(ctx context.Context, p peer.ID) (*Conn, error) { | |
conn, err := s.dial(ctx, p) | ||
if err != nil { | ||
conn = s.bestConnToPeer(p) | ||
if conn != nil { | ||
if forceDirect { | ||
if isDirectConn(conn) { | ||
log.Debugf("ignoring dial error because we already have a direct connection: %s", err) | ||
return conn, nil | ||
} | ||
} else if conn != nil { | ||
// Hm? What error? | ||
// Could have canceled the dial because we received a | ||
// connection or some other random reason. | ||
// Just ignore the error and return the connection. | ||
log.Debugf("ignoring dial error because we have a connection: %s", err) | ||
log.Debugf("ignoring dial error because we already have a connection: %s", err) | ||
return conn, nil | ||
} | ||
|
||
vyzo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -321,6 +336,11 @@ func (s *Swarm) canDial(addr ma.Multiaddr) bool { | |
return t != nil && t.CanDial(addr) | ||
} | ||
|
||
func (s *Swarm) nonProxyAddr(addr ma.Multiaddr) bool { | ||
t := s.TransportForDialing(addr) | ||
return !t.Proxy() | ||
Comment on lines
+340
to
+341
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. This could panic. I know we don't call it in a way that can panic, but it could still panic and someone else could call it. |
||
} | ||
|
||
// ranks addresses in descending order of preference for dialing | ||
// Private UDP > Public UDP > Private TCP > Public TCP > UDP Relay server > TCP Relay server | ||
func (s *Swarm) rankAddrs(addrs []ma.Multiaddr) []ma.Multiaddr { | ||
|
@@ -362,6 +382,7 @@ func (s *Swarm) rankAddrs(addrs []ma.Multiaddr) []ma.Multiaddr { | |
|
||
// dial is the actual swarm's dial logic, gated by Dial. | ||
func (s *Swarm) dial(ctx context.Context, p peer.ID) (*Conn, error) { | ||
forceDirect, _ := network.GetForceDirectDial(ctx) | ||
var logdial = lgbl.Dial("swarm", s.LocalPeer(), p, nil, nil) | ||
if p == s.local { | ||
log.Event(ctx, "swarmDialDoDialSelf", logdial) | ||
|
@@ -383,20 +404,25 @@ func (s *Swarm) dial(ctx context.Context, p peer.ID) (*Conn, error) { | |
return nil, &DialError{Peer: p, Cause: ErrNoAddresses} | ||
} | ||
goodAddrs := s.filterKnownUndialables(p, peerAddrs) | ||
if forceDirect { | ||
goodAddrs = addrutil.FilterAddrs(goodAddrs, s.nonProxyAddr) | ||
} | ||
if len(goodAddrs) == 0 { | ||
return nil, &DialError{Peer: p, Cause: ErrNoGoodAddresses} | ||
} | ||
|
||
/////// Check backoff andnRank addresses | ||
var nonBackoff bool | ||
for _, a := range goodAddrs { | ||
// skip addresses in back-off | ||
if !s.backf.Backoff(p, a) { | ||
nonBackoff = true | ||
if !forceDirect { | ||
/////// Check backoff andnRank addresses | ||
var nonBackoff bool | ||
for _, a := range goodAddrs { | ||
// skip addresses in back-off | ||
if !s.backf.Backoff(p, a) { | ||
nonBackoff = true | ||
} | ||
} | ||
if !nonBackoff { | ||
return nil, ErrDialBackoff | ||
} | ||
} | ||
if !nonBackoff { | ||
return nil, ErrDialBackoff | ||
} | ||
|
||
connC, dialErr := s.dialAddrs(ctx, p, s.rankAddrs(goodAddrs)) | ||
|
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.
This needs to use the background context. Otherwise, if the first dialer cancels, we'll cancel the overall dial.
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.
This is also why the tests are failing.