dial.go 2.58 KB
Newer Older
Steven Allen's avatar
Steven Allen committed
1 2 3 4 5 6 7
package tcpreuse

import (
	"context"
	"net"

	ma "github.com/multiformats/go-multiaddr"
8
	manet "github.com/multiformats/go-multiaddr/net"
tavit ohanian's avatar
tavit ohanian committed
9
	"gitlab.dms3.io/p2p/go-reuseport"
Steven Allen's avatar
Steven Allen committed
10 11 12 13 14 15 16
)

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

Steven Allen's avatar
Steven Allen committed
17 18 19 20 21 22 23
// Dial dials the given multiaddr, reusing ports we're currently listening on if
// possible.
//
// Dial attempts to be smart about choosing the source port. For example, If
// we're dialing a loopback address and we're listening on one or more loopback
// ports, Dial will randomly choose one of the loopback ports and addresses and
// reuse it.
Steven Allen's avatar
Steven Allen committed
24 25 26 27
func (t *Transport) Dial(raddr ma.Multiaddr) (manet.Conn, error) {
	return t.DialContext(context.Background(), raddr)
}

Steven Allen's avatar
Steven Allen committed
28
// DialContext is like Dial but takes a context.
Steven Allen's avatar
Steven Allen committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
func (t *Transport) DialContext(ctx context.Context, raddr ma.Multiaddr) (manet.Conn, error) {
	network, addr, err := manet.DialArgs(raddr)
	if err != nil {
		return nil, err
	}
	var d dialer
	switch network {
	case "tcp4":
		d = t.v4.getDialer(network)
	case "tcp6":
		d = t.v6.getDialer(network)
	default:
		return nil, ErrWrongProto
	}
	conn, err := d.DialContext(ctx, network, addr)
	if err != nil {
		return nil, err
	}
	maconn, err := manet.WrapNetConn(conn)
	if err != nil {
		conn.Close()
		return nil, err
	}
	return maconn, nil
}

func (n *network) getDialer(network string) dialer {
	n.mu.RLock()
	d := n.dialer
	n.mu.RUnlock()
	if d == nil {
		n.mu.Lock()
		defer n.mu.Unlock()

		if n.dialer == nil {
			n.dialer = n.makeDialer(network)
		}
		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 {
90 91 92 93 94 95 96
		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
Steven Allen's avatar
Steven Allen committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
			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)
}