Skip to content

Commit

Permalink
fix: don't prefer local ports from other addresses when dialing
Browse files Browse the repository at this point in the history
This address may already be in-use (on that other address) somewhere
else.

Thanks to @schomatis for figuring this out.

fixes #1611
  • Loading branch information
Stebalien committed Aug 12, 2022
1 parent 24b27cc commit f2c0786
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 169 deletions.
57 changes: 3 additions & 54 deletions p2p/net/reuseport/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ package reuseport

import (
"context"
"net"

"github.com/libp2p/go-reuseport"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)

type dialer interface {
Dial(network, addr string) (net.Conn, error)
DialContext(ctx context.Context, network, addr string) (net.Conn, error)
}

// Dial dials the given multiaddr, reusing ports we're currently listening on if
// possible.
//
Expand All @@ -31,7 +24,7 @@ func (t *Transport) DialContext(ctx context.Context, raddr ma.Multiaddr) (manet.
if err != nil {
return nil, err
}
var d dialer
var d *dialer
switch network {
case "tcp4":
d = t.v4.getDialer(network)
Expand All @@ -52,7 +45,7 @@ func (t *Transport) DialContext(ctx context.Context, raddr ma.Multiaddr) (manet.
return maconn, nil
}

func (n *network) getDialer(network string) dialer {
func (n *network) getDialer(network string) *dialer {
n.mu.RLock()
d := n.dialer
n.mu.RUnlock()
Expand All @@ -61,53 +54,9 @@ func (n *network) getDialer(network string) dialer {
defer n.mu.Unlock()

if n.dialer == nil {
n.dialer = n.makeDialer(network)
n.dialer = newDialer(n.listeners)
}
d = n.dialer
}
return d
}

func (n *network) makeDialer(network string) dialer {
if !reuseport.Available() {
log.Debug("reuseport not available")
return &net.Dialer{}
}

var unspec net.IP
switch network {
case "tcp4":
unspec = net.IPv4zero
case "tcp6":
unspec = net.IPv6unspecified
default:
panic("invalid network: must be either tcp4 or tcp6")
}

// How many ports are we listening on.
var port = 0
for l := range n.listeners {
newPort := l.Addr().(*net.TCPAddr).Port
switch {
case newPort == 0: // Any port, ignore (really, we shouldn't get this case...).
case port == 0: // Haven't selected a port yet, choose this one.
port = newPort
case newPort == port: // Same as the selected port, continue...
default: // Multiple ports, use the multi dialer
return newMultiDialer(unspec, n.listeners)
}
}

// None.
if port == 0 {
return &net.Dialer{}
}

// One. Always dial from the single port we're listening on.
laddr := &net.TCPAddr{
IP: unspec,
Port: port,
}

return (*singleDialer)(laddr)
}
91 changes: 91 additions & 0 deletions p2p/net/reuseport/dialer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package reuseport

import (
"context"
"fmt"
"math/rand"
"net"

"github.com/libp2p/go-netroute"
)

type dialer struct {
specific []*net.TCPAddr
loopback []*net.TCPAddr
unspecified []*net.TCPAddr
}

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.
// Dialing preference is
// * If there is a listener on the local interface the OS expects to use to route towards addr, use that.
// * If there is a listener on a loopback address, addr is loopback, use that.
// * If there is a listener on an undefined address (0.0.0.0 or ::), use that.
// * Otherwise, dial with a random port on the unspecified address.
func (d *dialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
if len(d.specific) > 0 || len(d.loopback) > 0 {
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 len(d.specific) > 0 {
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)
}
}
}
}
}

if len(d.loopback) > 0 && ip.IsLoopback() {
return reuseDial(ctx, randAddr(d.loopback), network, addr)
}
}

if len(d.unspecified) > 0 {
return reuseDial(ctx, randAddr(d.unspecified), network, addr)
}

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,
}
}
90 changes: 0 additions & 90 deletions p2p/net/reuseport/multidialer.go

This file was deleted.

16 changes: 0 additions & 16 deletions p2p/net/reuseport/singledialer.go

This file was deleted.

2 changes: 1 addition & 1 deletion p2p/net/reuseport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ type Transport struct {
type network struct {
mu sync.RWMutex
listeners map[*listener]struct{}
dialer dialer
dialer *dialer
}
8 changes: 0 additions & 8 deletions p2p/net/reuseport/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)

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) {
Expand Down

0 comments on commit f2c0786

Please sign in to comment.