Skip to content

Commit

Permalink
swarm: fix selection of transport for dialing (#1653)
Browse files Browse the repository at this point in the history
With WebTransport's /webtransport/certhash/xyz addresses, the assumption that
the last component of a multiaddr identifies the transport to use for dialing
doesn't hold any more.
Note that WebRTC will probably also use the certhash multiaddr component to
encode its certificate hashes.
  • Loading branch information
marten-seemann authored Sep 16, 2022
1 parent 70c1f81 commit 8b7d151
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
46 changes: 46 additions & 0 deletions p2p/net/swarm/swarm_addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@ package swarm_test

import (
"context"
"fmt"
"testing"

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

ic "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/core/test"
"github.com/libp2p/go-libp2p/p2p/net/swarm"
swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
circuitv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
quic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
webtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport"

"github.com/minio/sha256-simd"
ma "github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multibase"
"github.com/multiformats/go-multihash"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -56,3 +68,37 @@ func TestAddressesWithoutListening(t *testing.T) {
require.NoError(t, err)
require.Empty(t, a1, "expected to be listening on no addresses")
}

func TestDialAddressSelection(t *testing.T) {
priv, _, err := test.RandTestKeyPair(ic.Ed25519, 256)
require.NoError(t, err)
id, err := peer.IDFromPrivateKey(priv)
require.NoError(t, err)
s, err := swarm.NewSwarm("local", nil)
require.NoError(t, err)

tcpTr, err := tcp.NewTCPTransport(nil, nil)
require.NoError(t, err)
require.NoError(t, s.AddTransport(tcpTr))
quicTr, err := quic.NewTransport(priv, nil, nil, nil)
require.NoError(t, err)
require.NoError(t, s.AddTransport(quicTr))
webtransportTr, err := webtransport.New(priv, nil, nil)
require.NoError(t, err)
require.NoError(t, s.AddTransport(webtransportTr))
h := sha256.Sum256([]byte("foo"))
hash, err := multihash.Encode(h[:], multihash.SHA2_256)
require.NoError(t, err)
certHash, err := multibase.Encode(multibase.Base58BTC, hash)
require.NoError(t, err)
circuitTr, err := circuitv2.New(nil, nil)
require.NoError(t, err)
require.NoError(t, s.AddTransport(circuitTr))

require.Equal(t, tcpTr, s.TransportForDialing(ma.StringCast("/ip4/127.0.0.1/tcp/1234")))
require.Equal(t, quicTr, s.TransportForDialing(ma.StringCast("/ip4/127.0.0.1/udp/1234/quic")))
require.Equal(t, circuitTr, s.TransportForDialing(ma.StringCast(fmt.Sprintf("/ip4/127.0.0.1/udp/1234/quic/p2p-circuit/p2p/%s", id))))
require.Equal(t, webtransportTr, s.TransportForDialing(ma.StringCast(fmt.Sprintf("/ip4/127.0.0.1/udp/1234/quic/webtransport/certhash/%s", certHash))))
require.Nil(t, s.TransportForDialing(ma.StringCast("/ip4/1.2.3.4")))
require.Nil(t, s.TransportForDialing(ma.StringCast("/ip4/1.2.3.4/tcp/443/ws")))
}
18 changes: 8 additions & 10 deletions p2p/net/swarm/swarm_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,23 @@ func (s *Swarm) TransportForDialing(a ma.Multiaddr) transport.Transport {

s.transports.RLock()
defer s.transports.RUnlock()

if len(s.transports.m) == 0 {
// make sure we're not just shutting down.
if s.transports.m != nil {
log.Error("you have no transports configured")
}
return nil
}

for _, p := range protocols {
transport, ok := s.transports.m[p.Code]
if !ok {
continue
}
if transport.Proxy() {
return transport
if isRelayAddr(a) {
return s.transports.m[ma.P_CIRCUIT]
}
for _, t := range s.transports.m {
if t.CanDial(a) {
return t
}
}

return s.transports.m[protocols[len(protocols)-1].Code]
return nil
}

// TransportForListening retrieves the appropriate transport for listening on
Expand Down

0 comments on commit 8b7d151

Please sign in to comment.