Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
Moving the QTransport abstration layer in his own package.
Browse files Browse the repository at this point in the history
That allow other packages which need to translate Transport to QTransport to do it.
  • Loading branch information
Jorropo committed Feb 29, 2020
1 parent 83882e9 commit 507080a
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 129 deletions.
137 changes: 137 additions & 0 deletions QTransport/swarm_QTransport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package QTransport
// This file contains the whole Transport to QTransport abstraction layer.

import (
"context"
"fmt"

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

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

// 2^31
var defaultNonProxyQuality uint32 = 2147483648
// 2^31+2^30
var defaultProxyQuality uint32 = 3221225472

var _ transport.QTransport = TransportUpgrader{}

// Used to upgrade `transport.Transport` to `transport.QTransport`.
// Only use it with `BaseTransport` castable to `transport.Transport`.
type TransportUpgrader struct {
transport.BaseTransport
}

func (t TransportUpgrader) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (transport.QCapableConn, error) {
conn, err := t.BaseTransport.(transport.Transport).Dial(ctx, raddr, p)
if err != nil {
return nil, err
}
return upgradedCapableConn{
listenedUpgradedCapableConn{BaseCapableConn: conn, t: t},
}, nil
}

func (t TransportUpgrader) Listen(laddr ma.Multiaddr) (transport.QListener, error) {
l, err := t.BaseTransport.(transport.Transport).Listen(laddr)
if err != nil {
return nil, err
}
return upgradedListener{BaseListener: l, t: t}, nil
}

func (t TransportUpgrader) Score(raddr ma.Multiaddr, _ peer.ID) (transport.Score, error) {
if !t.CanDial(raddr) {
return transport.Score{}, fmt.Errorf("\"%s\" does not match %s.", raddr, t.BaseTransport)
}
if t.Proxy() {
if manet.IsIPLoopback(raddr) {
return transport.Score{
Quality: defaultProxyQuality >> 16,
IsQuality: true,
Fd: 1,
}, nil
}
if manet.IsPrivateAddr(raddr) {
return transport.Score{
Quality: defaultProxyQuality >> 8,
IsQuality: true,
Fd: 1,
}, nil
}
return transport.Score{
Quality: defaultProxyQuality,
IsQuality: true,
Fd: 1,
}, nil
}
if manet.IsIPLoopback(raddr) {
return transport.Score{
Quality: defaultNonProxyQuality >> 16,
IsQuality: true,
Fd: 1,
}, nil
}
if manet.IsPrivateAddr(raddr) {
return transport.Score{
Quality: defaultNonProxyQuality >> 8,
IsQuality: true,
Fd: 1,
}, nil
}
return transport.Score{
Quality: defaultNonProxyQuality,
IsQuality: true,
Fd: 1,
}, nil
}

// Used to upgrade `transport.CapableConn` to `transport.QCapableConn`.
type upgradedCapableConn struct {
listenedUpgradedCapableConn
}

func (c upgradedCapableConn) Quality() uint32 {
raddr := c.RemoteMultiaddr()
if c.Transport().Proxy() {
if manet.IsIPLoopback(raddr) {
return defaultProxyQuality >> 16
}
if manet.IsPrivateAddr(raddr) {
return defaultProxyQuality >> 8
}
return defaultProxyQuality
}
if manet.IsIPLoopback(raddr) {
return defaultNonProxyQuality >> 16
}
if manet.IsPrivateAddr(raddr) {
return defaultNonProxyQuality >> 8
}
return defaultNonProxyQuality
}

type listenedUpgradedCapableConn struct {
transport.BaseCapableConn
t transport.QTransport
}

func (c listenedUpgradedCapableConn) Transport() transport.QTransport {
return c.t
}

type upgradedListener struct {
transport.BaseListener
t transport.QTransport
}

func (l upgradedListener) Accept() (transport.ListenedQCapableConn, error) {
c, err := l.BaseListener.(transport.Listener).Accept()
if err != nil {
return nil, err
}
return listenedUpgradedCapableConn{BaseCapableConn: c, t: l.t}, nil
}
132 changes: 3 additions & 129 deletions swarm_transport.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package swarm

import (
"context"
"fmt"
"strings"

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

"github.com/libp2p/go-libp2p-swarm/QTransport"

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

// 2^31
const defaultNonProxyQuality = 2147483648

// 2^31+2^30
const defaultProxyQuality = 3221225472

// TransportForDialing retrieves the appropriate transport for dialing the given
// multiaddr.
func (s *Swarm) TransportForDialing(a ma.Multiaddr) transport.QTransport {
Expand Down Expand Up @@ -85,7 +78,7 @@ func (s *Swarm) AddTransport(ot transport.BaseTransport) error {
case transport.QTransport:
t = rt
case transport.Transport:
t = transportUpgrader{BaseTransport: rt}
t = QTransport.TransportUpgrader{BaseTransport: rt}
default:
return errTransportUncastable
}
Expand Down Expand Up @@ -120,122 +113,3 @@ func (s *Swarm) AddTransport(ot transport.BaseTransport) error {
}
return nil
}

var _ transport.QTransport = transportUpgrader{}

// Used to upgrade `transport.Transport` to `transport.QTransport`.
// Only use it with `BaseTransport` castable to `transport.Transport`.
type transportUpgrader struct {
transport.BaseTransport
}

func (t transportUpgrader) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (transport.QCapableConn, error) {
conn, err := t.BaseTransport.(transport.Transport).Dial(ctx, raddr, p)
if err != nil {
return nil, err
}
return upgradedCapableConn{
listenedUpgradedCapableConn{BaseCapableConn: conn, t: t},
}, nil
}

func (t transportUpgrader) Listen(laddr ma.Multiaddr) (transport.QListener, error) {
l, err := t.BaseTransport.(transport.Transport).Listen(laddr)
if err != nil {
return nil, err
}
return upgradedListener{BaseListener: l, t: t}, nil
}

func (t transportUpgrader) Score(raddr ma.Multiaddr, _ peer.ID) (transport.Score, error) {
if !t.CanDial(raddr) {
return transport.Score{}, fmt.Errorf("\"%s\" does not match %s.", raddr, t.BaseTransport)
}
if t.Proxy() {
if manet.IsIPLoopback(raddr) {
return transport.Score{
Quality: defaultProxyQuality >> 16,
IsQuality: true,
Fd: 1,
}, nil
}
if manet.IsPrivateAddr(raddr) {
return transport.Score{
Quality: defaultProxyQuality >> 8,
IsQuality: true,
Fd: 1,
}, nil
}
return transport.Score{
Quality: defaultProxyQuality,
IsQuality: true,
Fd: 1,
}, nil
}
if manet.IsIPLoopback(raddr) {
return transport.Score{
Quality: defaultNonProxyQuality >> 16,
IsQuality: true,
Fd: 1,
}, nil
}
if manet.IsPrivateAddr(raddr) {
return transport.Score{
Quality: defaultNonProxyQuality >> 8,
IsQuality: true,
Fd: 1,
}, nil
}
return transport.Score{
Quality: defaultNonProxyQuality,
IsQuality: true,
Fd: 1,
}, nil
}

// Used to upgrade `transport.CapableConn` to `transport.QCapableConn`.
type upgradedCapableConn struct {
listenedUpgradedCapableConn
}

func (c upgradedCapableConn) Quality() uint32 {
raddr := c.RemoteMultiaddr()
if c.Transport().Proxy() {
if manet.IsIPLoopback(raddr) {
return defaultProxyQuality >> 16
}
if manet.IsPrivateAddr(raddr) {
return defaultProxyQuality >> 8
}
return defaultProxyQuality
}
if manet.IsIPLoopback(raddr) {
return defaultNonProxyQuality >> 16
}
if manet.IsPrivateAddr(raddr) {
return defaultNonProxyQuality >> 8
}
return defaultNonProxyQuality
}

type listenedUpgradedCapableConn struct {
transport.BaseCapableConn
t transport.QTransport
}

func (c listenedUpgradedCapableConn) Transport() transport.QTransport {
return c.t
}

type upgradedListener struct {
transport.BaseListener
t transport.QTransport
}

func (l upgradedListener) Accept() (transport.ListenedQCapableConn, error) {
c, err := l.BaseListener.(transport.Listener).Accept()
if err != nil {
return nil, err
}
return listenedUpgradedCapableConn{BaseCapableConn: c, t: l.t}, nil
}

0 comments on commit 507080a

Please sign in to comment.