-
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
fix: don't prefer local ports from other addresses when dialing #1673
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package reuseport | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/rand" | ||
"net" | ||
|
||
"github.com/libp2p/go-netroute" | ||
) | ||
|
||
type dialer struct { | ||
// All address that are _not_ loopback or unspecified (0.0.0.0 or ::). | ||
specific []*net.TCPAddr | ||
// All loopback addresses (127.*.*.*, ::1). | ||
loopback []*net.TCPAddr | ||
// Unspecified addresses (0.0.0.0, ::) | ||
unspecified []*net.TCPAddr | ||
marten-seemann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func (d *dialer) Dial(network, addr string) (net.Conn, error) { | ||
return d.DialContext(context.Background(), network, addr) | ||
} | ||
|
||
func randAddr(addrs []*net.TCPAddr) *net.TCPAddr { | ||
if len(addrs) > 0 { | ||
return addrs[rand.Intn(len(addrs))] | ||
} | ||
return nil | ||
} | ||
|
||
// DialContext dials a target addr. | ||
// | ||
// In-order: | ||
// | ||
// 1. If we're _explicitly_ listening on the prefered source address for the destination address | ||
// (per the system's routes), we'll use that listener's port as the source port. | ||
// 2. If we're listening on one or more _unspecified_ addresses (zero address), we'll pick a source | ||
// port from one of these listener's. | ||
// 3. Otherwise, we'll let the system pick the source port. | ||
func (d *dialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) { | ||
// We only check this case if the user is listening on a specific address (loopback or | ||
// otherwise). Generally, users will listen on the "unspecified" address (0.0.0.0 or ::) and | ||
// we can skip this section. | ||
// | ||
// This lets us avoid resolving the address twice, in most cases. | ||
if len(d.specific) > 0 || len(d.loopback) > 0 { | ||
marten-seemann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tcpAddr, err := net.ResolveTCPAddr(network, addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ip := tcpAddr.IP | ||
if !ip.IsLoopback() && !ip.IsGlobalUnicast() { | ||
return nil, fmt.Errorf("undialable IP: %s", ip) | ||
} | ||
|
||
// If we're listening on some specific address and that specific address happens to | ||
// be the preferred source address for the target destination address, we try to | ||
// dial with that address/port. | ||
// | ||
// We skip this check if we _aren't_ listening on any specific addresses, because | ||
// checking routing tables can be expensive and users rarely listen on specific IP | ||
// addresses. | ||
if len(d.specific) > 0 { | ||
marten-seemann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if router, err := netroute.New(); err == nil { | ||
if _, _, preferredSrc, err := router.Route(ip); err == nil { | ||
for _, optAddr := range d.specific { | ||
if optAddr.IP.Equal(preferredSrc) { | ||
return reuseDial(ctx, optAddr, network, addr) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Otherwise, if we are listening on a loopback address and the destination is also | ||
// a loopback address, use the port from our loopback listener. | ||
if len(d.loopback) > 0 && ip.IsLoopback() { | ||
return reuseDial(ctx, randAddr(d.loopback), network, addr) | ||
} | ||
} | ||
|
||
// If we're listening on any uspecified addresses, use a randomly chosen port from one of | ||
// these listeners. | ||
if len(d.unspecified) > 0 { | ||
return reuseDial(ctx, randAddr(d.unspecified), network, addr) | ||
} | ||
|
||
// Finally, just pick a random port. | ||
var dialer net.Dialer | ||
return dialer.DialContext(ctx, network, addr) | ||
} | ||
|
||
func newDialer(listeners map[*listener]struct{}) *dialer { | ||
specific := make([]*net.TCPAddr, 0) | ||
loopback := make([]*net.TCPAddr, 0) | ||
unspecified := make([]*net.TCPAddr, 0) | ||
|
||
for l := range listeners { | ||
addr := l.Addr().(*net.TCPAddr) | ||
if addr.IP.IsLoopback() { | ||
loopback = append(loopback, addr) | ||
} else if addr.IP.IsUnspecified() { | ||
unspecified = append(unspecified, addr) | ||
} else { | ||
specific = append(specific, addr) | ||
} | ||
} | ||
return &dialer{ | ||
specific: specific, | ||
loopback: loopback, | ||
unspecified: unspecified, | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -141,7 +141,6 @@ func TestGlobalPreferenceV4(t *testing.T) { | |
testPrefer(t, loopbackV4, loopbackV4, globalV4) | ||
t.Logf("when listening on %v, should prefer %v over %v", loopbackV4, unspecV4, globalV4) | ||
testPrefer(t, loopbackV4, unspecV4, globalV4) | ||
|
||
t.Logf("when listening on %v, should prefer %v over %v", globalV4, unspecV4, loopbackV4) | ||
testPrefer(t, globalV4, unspecV4, loopbackV4) | ||
} | ||
|
@@ -177,20 +176,13 @@ func testPrefer(t *testing.T, listen, prefer, avoid ma.Multiaddr) { | |
} | ||
defer listenerB1.Close() | ||
|
||
dialOne(t, &trB, listenerA, listenerB1.Addr().(*net.TCPAddr).Port) | ||
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. We now only prefer source ports if the dialing addresses match. |
||
|
||
listenerB2, err := trB.Listen(prefer) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer listenerB2.Close() | ||
|
||
dialOne(t, &trB, listenerA, listenerB2.Addr().(*net.TCPAddr).Port) | ||
|
||
// Closing the listener should reset the dialer. | ||
listenerB2.Close() | ||
|
||
dialOne(t, &trB, listenerA, listenerB1.Addr().(*net.TCPAddr).Port) | ||
} | ||
|
||
func TestV6V4(t *testing.T) { | ||
|
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.
We now have one dialer, so there's no need to abstract over them.