Skip to content

Commit

Permalink
tcp: unexport the TcpTransport
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jun 11, 2022
1 parent 78b526e commit 1f8d5ba
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion p2p/net/swarm/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (s *Swarm) addConn(tc transport.CapableConn, dir network.Direction) (*Conn,
}

// we ONLY check upgraded connections here so we can send them a Disconnect message.
// If we do this in the Upgrader, we will not be able to do this.
// If we do this in the upgrader, we will not be able to do this.
if s.gater != nil {
if allow, _ := s.gater.InterceptUpgraded(c); !allow {
// TODO Send disconnect with reason here
Expand Down
2 changes: 1 addition & 1 deletion p2p/net/upgrader/upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func WithResourceManager(m network.ResourceManager) Option {
}
}

// Upgrader is a multistream upgrader that can upgrade an underlying connection
// upgrader is a multistream upgrader that can upgrade an underlying connection
// to a full transport connection (secure and multiplexed).
type upgrader struct {
secure sec.SecureMuxer
Expand Down
38 changes: 19 additions & 19 deletions p2p/transport/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/transport"
tpt "github.com/libp2p/go-libp2p-core/transport"

logging "github.com/ipfs/go-log/v2"
ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -91,27 +91,27 @@ func (ll *tcpListener) Accept() (manet.Conn, error) {
return c, nil
}

type Option func(*TcpTransport) error
type Option func(*transport) error

func DisableReuseport() Option {
return func(tr *TcpTransport) error {
return func(tr *transport) error {
tr.disableReuseport = true
return nil
}
}

func WithConnectionTimeout(d time.Duration) Option {
return func(tr *TcpTransport) error {
return func(tr *transport) error {
tr.connectTimeout = d
return nil
}
}

// TcpTransport is the TCP transport.
type TcpTransport struct {
// transport is the TCP transport.
type transport struct {
// Connection upgrader for upgrading insecure stream connections to
// secure multiplex connections.
upgrader transport.Upgrader
upgrader tpt.Upgrader

// Explicitly disable reuseport.
disableReuseport bool
Expand All @@ -124,15 +124,15 @@ type TcpTransport struct {
reuse reuseport.Transport
}

var _ transport.Transport = &TcpTransport{}
var _ tpt.Transport = &transport{}

// NewTCPTransport creates a tcp transport object that tracks dialers and listeners
// created. It represents an entire TCP stack (though it might not necessarily be).
func NewTCPTransport(upgrader transport.Upgrader, rcmgr network.ResourceManager, opts ...Option) (*TcpTransport, error) {
func NewTCPTransport(upgrader tpt.Upgrader, rcmgr network.ResourceManager, opts ...Option) (*transport, error) {
if rcmgr == nil {
rcmgr = network.NullResourceManager
}
tr := &TcpTransport{
tr := &transport{
upgrader: upgrader,
connectTimeout: defaultConnectTimeout, // can be set by using the WithConnectionTimeout option
rcmgr: rcmgr,
Expand All @@ -149,11 +149,11 @@ var dialMatcher = mafmt.And(mafmt.IP, mafmt.Base(ma.P_TCP))

// CanDial returns true if this transport believes it can dial the given
// multiaddr.
func (t *TcpTransport) CanDial(addr ma.Multiaddr) bool {
func (t *transport) CanDial(addr ma.Multiaddr) bool {
return dialMatcher.Matches(addr)
}

func (t *TcpTransport) maDial(ctx context.Context, raddr ma.Multiaddr) (manet.Conn, error) {
func (t *transport) maDial(ctx context.Context, raddr ma.Multiaddr) (manet.Conn, error) {
// Apply the deadline iff applicable
if t.connectTimeout > 0 {
var cancel context.CancelFunc
Expand All @@ -169,7 +169,7 @@ func (t *TcpTransport) maDial(ctx context.Context, raddr ma.Multiaddr) (manet.Co
}

// Dial dials the peer at the remote address.
func (t *TcpTransport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (transport.CapableConn, error) {
func (t *transport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (tpt.CapableConn, error) {
connScope, err := t.rcmgr.OpenConnection(network.DirOutbound, true)
if err != nil {
log.Debugw("resource manager blocked outgoing connection", "peer", p, "addr", raddr, "error", err)
Expand Down Expand Up @@ -203,19 +203,19 @@ func (t *TcpTransport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID)
}

// UseReuseport returns true if reuseport is enabled and available.
func (t *TcpTransport) UseReuseport() bool {
func (t *transport) UseReuseport() bool {
return !t.disableReuseport && ReuseportIsAvailable()
}

func (t *TcpTransport) maListen(laddr ma.Multiaddr) (manet.Listener, error) {
func (t *transport) maListen(laddr ma.Multiaddr) (manet.Listener, error) {
if t.UseReuseport() {
return t.reuse.Listen(laddr)
}
return manet.Listen(laddr)
}

// Listen listens on the given multiaddr.
func (t *TcpTransport) Listen(laddr ma.Multiaddr) (transport.Listener, error) {
func (t *transport) Listen(laddr ma.Multiaddr) (tpt.Listener, error) {
list, err := t.maListen(laddr)
if err != nil {
return nil, err
Expand All @@ -225,15 +225,15 @@ func (t *TcpTransport) Listen(laddr ma.Multiaddr) (transport.Listener, error) {
}

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

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

func (t *TcpTransport) String() string {
func (t *transport) String() string {
return "TCP"
}
6 changes: 3 additions & 3 deletions p2p/transport/tcp/tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/sec"
"github.com/libp2p/go-libp2p-core/sec/insecure"
"github.com/libp2p/go-libp2p-core/transport"
tpt "github.com/libp2p/go-libp2p-core/transport"

mocknetwork "github.com/libp2p/go-libp2p-testing/mocks/network"

Expand Down Expand Up @@ -102,7 +102,7 @@ func TestTcpTransportCantDialDNS(t *testing.T) {
dnsa, err := ma.NewMultiaddr("/dns4/example.com/tcp/1234")
require.NoError(t, err)

var u transport.Upgrader
var u tpt.Upgrader
tpt, err := NewTCPTransport(u, nil)
require.NoError(t, err)

Expand All @@ -120,7 +120,7 @@ func TestTcpTransportCantListenUtp(t *testing.T) {
utpa, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/0/utp")
require.NoError(t, err)

var u transport.Upgrader
var u tpt.Upgrader
tpt, err := NewTCPTransport(u, nil)
require.NoError(t, err)

Expand Down

0 comments on commit 1f8d5ba

Please sign in to comment.