tcp.go 3.77 KB
Newer Older
1 2 3 4
package tcp

import (
	"context"
5
	"net"
Steven Allen's avatar
Steven Allen committed
6
	"time"
7 8

	logging "github.com/ipfs/go-log"
9
	peer "github.com/libp2p/go-libp2p-peer"
10
	tpt "github.com/libp2p/go-libp2p-transport"
11 12
	tptu "github.com/libp2p/go-libp2p-transport-upgrader"
	rtpt "github.com/libp2p/go-reuseport-transport"
Jeromy's avatar
Jeromy committed
13 14
	ma "github.com/multiformats/go-multiaddr"
	manet "github.com/multiformats/go-multiaddr-net"
15 16 17
	mafmt "github.com/whyrusleeping/mafmt"
)

Steven Allen's avatar
Steven Allen committed
18 19 20 21
// DefaultConnectTimeout is the (default) maximum amount of time the TCP
// transport will spend on the initial TCP connect before giving up.
var DefaultConnectTimeout = 5 * time.Second

22 23
var log = logging.Logger("tcp-tpt")

24 25
// try to set linger on the connection, if possible.
func tryLinger(conn net.Conn, sec int) {
Steven Allen's avatar
Steven Allen committed
26
	type canLinger interface {
27
		SetLinger(int) error
Steven Allen's avatar
Steven Allen committed
28 29 30
	}

	if lingerConn, ok := conn.(canLinger); ok {
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
		_ = lingerConn.SetLinger(sec)
	}
}

type lingerListener struct {
	manet.Listener
	sec int
}

func (ll *lingerListener) Accept() (manet.Conn, error) {
	c, err := ll.Listener.Accept()
	if err != nil {
		return nil, err
	}
	tryLinger(c, ll.sec)
	return c, nil
}

49
// TcpTransport is the TCP transport.
50
type TcpTransport struct {
51 52 53
	// Connection upgrader for upgrading insecure stream connections to
	// secure multiplex connections.
	Upgrader *tptu.Upgrader
54

55 56 57
	// Explicitly disable reuseport.
	DisableReuseport bool

Steven Allen's avatar
Steven Allen committed
58 59 60
	// TCP connect timeout
	ConnectTimeout time.Duration

61
	reuse rtpt.Transport
62 63
}

64 65
var _ tpt.Transport = &TcpTransport{}

66 67
// NewTCPTransport creates a tcp transport object that tracks dialers and listeners
// created. It represents an entire tcp stack (though it might not necessarily be)
68
func NewTCPTransport(upgrader *tptu.Upgrader) *TcpTransport {
Steven Allen's avatar
Steven Allen committed
69
	return &TcpTransport{Upgrader: upgrader, ConnectTimeout: DefaultConnectTimeout}
70 71
}

72 73 74 75
// CanDial returns true if this transport believes it can dial the given
// multiaddr.
func (t *TcpTransport) CanDial(addr ma.Multiaddr) bool {
	return mafmt.TCP.Matches(addr)
76 77
}

78
func (t *TcpTransport) maDial(ctx context.Context, raddr ma.Multiaddr) (manet.Conn, error) {
Steven Allen's avatar
Steven Allen committed
79 80 81 82 83 84 85 86 87 88
	// Apply the deadline iff applicable
	if t.ConnectTimeout > 0 {
		deadline := time.Now().Add(t.ConnectTimeout)
		if d, ok := ctx.Deadline(); !ok || deadline.Before(d) {
			var cancel func()
			ctx, cancel = context.WithDeadline(ctx, deadline)
			defer cancel()
		}
	}

89 90
	if t.UseReuseport() {
		return t.reuse.DialContext(ctx, raddr)
91
	}
92 93
	var d manet.Dialer
	return d.DialContext(ctx, raddr)
94 95
}

96 97 98
// Dial dials the peer at the remote address.
func (t *TcpTransport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (tpt.Conn, error) {
	conn, err := t.maDial(ctx, raddr)
99
	if err != nil {
100
		return nil, err
101
	}
102 103 104 105
	// Set linger to 0 so we never get stuck in the TIME-WAIT state. When
	// linger is 0, connections are _reset_ instead of closed with a FIN.
	// This means we can immediately reuse the 5-tuple and reconnect.
	tryLinger(conn, 0)
106
	return t.Upgrader.UpgradeOutbound(ctx, t, conn, p)
107 108
}

109 110 111
// UseReuseport returns true if reuseport is enabled and available.
func (t *TcpTransport) UseReuseport() bool {
	return !t.DisableReuseport && ReuseportIsAvailable()
112 113
}

114 115 116
func (t *TcpTransport) maListen(laddr ma.Multiaddr) (manet.Listener, error) {
	if t.UseReuseport() {
		return t.reuse.Listen(laddr)
117
	}
118
	return manet.Listen(laddr)
119 120
}

121 122 123
// Listen listens on the given multiaddr.
func (t *TcpTransport) Listen(laddr ma.Multiaddr) (tpt.Listener, error) {
	list, err := t.maListen(laddr)
124 125 126
	if err != nil {
		return nil, err
	}
127
	list = &lingerListener{list, 0}
128
	return t.Upgrader.UpgradeListener(t, list), nil
129 130
}

131 132 133
// Protocols returns the list of terminal protocols this transport can dial.
func (t *TcpTransport) Protocols() []int {
	return []int{ma.P_TCP}
134 135
}

136 137 138
// Proxy always returns false for the TCP transport.
func (t *TcpTransport) Proxy() bool {
	return false
139 140
}

141 142
func (t *TcpTransport) String() string {
	return "TCP"
143
}