Commit bd2c36a8 authored by Marten Seemann's avatar Marten Seemann
Browse files

obey the multiaddr's IP version when dialing

parent a9630561
......@@ -18,6 +18,10 @@ before_install:
- export GOARCH=$TRAVIS_GOARCH
- go env # for debugging
# see https://github.com/travis-ci/travis-ci/issues/8361#issuecomment-350090030
before_script:
- sudo sh -c 'echo 0 > /proc/sys/net/ipv6/conf/all/disable_ipv6'
script:
- ginkgo -r --cover --randomizeAllSpecs --randomizeSuites --trace --progress
......
......@@ -35,12 +35,12 @@ var _ = Describe("Connection", func() {
return id, priv
}
runServer := func(tr tpt.Transport) (ma.Multiaddr, <-chan tpt.Conn) {
runServer := func(tr tpt.Transport, multiaddr string) (ma.Multiaddr, <-chan tpt.Conn) {
addrChan := make(chan ma.Multiaddr)
connChan := make(chan tpt.Conn)
go func() {
defer GinkgoRecover()
addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/0/quic")
addr, err := ma.NewMultiaddr(multiaddr)
Expect(err).ToNot(HaveOccurred())
ln, err := tr.Listen(addr)
Expect(err).ToNot(HaveOccurred())
......@@ -62,10 +62,30 @@ var _ = Describe("Connection", func() {
clientID, clientKey = createPeer()
})
It("handshakes", func() {
It("handshakes on IPv4", func() {
serverTransport, err := NewTransport(serverKey)
Expect(err).ToNot(HaveOccurred())
serverAddr, serverConnChan := runServer(serverTransport)
serverAddr, serverConnChan := runServer(serverTransport, "/ip4/127.0.0.1/udp/0/quic")
clientTransport, err := NewTransport(clientKey)
Expect(err).ToNot(HaveOccurred())
conn, err := clientTransport.Dial(context.Background(), serverAddr, serverID)
Expect(err).ToNot(HaveOccurred())
serverConn := <-serverConnChan
Expect(conn.LocalPeer()).To(Equal(clientID))
Expect(conn.LocalPrivateKey()).To(Equal(clientKey))
Expect(conn.RemotePeer()).To(Equal(serverID))
Expect(conn.RemotePublicKey()).To(Equal(serverKey.GetPublic()))
Expect(serverConn.LocalPeer()).To(Equal(serverID))
Expect(serverConn.LocalPrivateKey()).To(Equal(serverKey))
Expect(serverConn.RemotePeer()).To(Equal(clientID))
Expect(serverConn.RemotePublicKey()).To(Equal(clientKey.GetPublic()))
})
It("handshakes on IPv6", func() {
serverTransport, err := NewTransport(serverKey)
Expect(err).ToNot(HaveOccurred())
serverAddr, serverConnChan := runServer(serverTransport, "/ip6/::1/udp/0/quic")
clientTransport, err := NewTransport(clientKey)
Expect(err).ToNot(HaveOccurred())
......@@ -85,7 +105,7 @@ var _ = Describe("Connection", func() {
It("opens and accepts streams", func() {
serverTransport, err := NewTransport(serverKey)
Expect(err).ToNot(HaveOccurred())
serverAddr, serverConnChan := runServer(serverTransport)
serverAddr, serverConnChan := runServer(serverTransport, "/ip4/127.0.0.1/udp/0/quic")
clientTransport, err := NewTransport(clientKey)
Expect(err).ToNot(HaveOccurred())
......@@ -110,7 +130,7 @@ var _ = Describe("Connection", func() {
serverTransport, err := NewTransport(serverKey)
Expect(err).ToNot(HaveOccurred())
serverAddr, serverConnChan := runServer(serverTransport)
serverAddr, serverConnChan := runServer(serverTransport, "/ip4/127.0.0.1/udp/0/quic")
clientTransport, err := NewTransport(clientKey)
Expect(err).ToNot(HaveOccurred())
......@@ -124,7 +144,7 @@ var _ = Describe("Connection", func() {
It("fails if the client presents an invalid cert chain", func() {
serverTransport, err := NewTransport(serverKey)
Expect(err).ToNot(HaveOccurred())
serverAddr, serverConnChan := runServer(serverTransport)
serverAddr, serverConnChan := runServer(serverTransport, "/ip4/127.0.0.1/udp/0/quic")
clientTransport, err := NewTransport(clientKey)
invalidateCertChain(clientTransport.(*transport).tlsConf)
......@@ -139,7 +159,7 @@ var _ = Describe("Connection", func() {
serverTransport, err := NewTransport(serverKey)
invalidateCertChain(serverTransport.(*transport).tlsConf)
Expect(err).ToNot(HaveOccurred())
serverAddr, serverConnChan := runServer(serverTransport)
serverAddr, serverConnChan := runServer(serverTransport, "/ip4/127.0.0.1/udp/0/quic")
clientTransport, err := NewTransport(clientKey)
Expect(err).ToNot(HaveOccurred())
......@@ -152,7 +172,7 @@ var _ = Describe("Connection", func() {
It("keeps accepting connections after a failed connection attempt", func() {
serverTransport, err := NewTransport(serverKey)
Expect(err).ToNot(HaveOccurred())
serverAddr, serverConnChan := runServer(serverTransport)
serverAddr, serverConnChan := runServer(serverTransport, "/ip4/127.0.0.1/udp/0/quic")
// first dial with an invalid cert chain
clientTransport1, err := NewTransport(clientKey)
......@@ -175,10 +195,10 @@ var _ = Describe("Connection", func() {
serverTransport, err := NewTransport(serverKey)
Expect(err).ToNot(HaveOccurred())
serverAddr, serverConnChan := runServer(serverTransport)
serverAddr, serverConnChan := runServer(serverTransport, "/ip4/127.0.0.1/udp/0/quic")
serverTransport2, err := NewTransport(serverKey2)
Expect(err).ToNot(HaveOccurred())
serverAddr2, serverConnChan2 := runServer(serverTransport2)
serverAddr2, serverConnChan2 := runServer(serverTransport2, "/ip4/127.0.0.1/udp/0/quic")
data := bytes.Repeat([]byte{'a'}, 5*1<<20) // 5 MB
// wait for both servers to accept a connection
......
......@@ -5,7 +5,9 @@ import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net"
"sync"
ic "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
......@@ -27,12 +29,47 @@ var quicConfig = &quic.Config{
KeepAlive: true,
}
type connManager struct {
connIPv4Once sync.Once
connIPv4 net.PacketConn
connIPv6Once sync.Once
connIPv6 net.PacketConn
}
func (c *connManager) GetConnForAddr(network string) (net.PacketConn, error) {
switch network {
case "udp4":
var err error
c.connIPv4Once.Do(func() {
c.connIPv4, err = c.createConn(network, "0.0.0.0:0")
})
return c.connIPv4, err
case "udp6":
var err error
c.connIPv6Once.Do(func() {
c.connIPv6, err = c.createConn(network, ":0")
})
return c.connIPv6, err
default:
return nil, fmt.Errorf("unsupported network: %s", network)
}
}
func (c *connManager) createConn(network, host string) (net.PacketConn, error) {
addr, err := net.ResolveUDPAddr(network, host)
if err != nil {
return nil, err
}
return net.ListenUDP(network, addr)
}
// The Transport implements the tpt.Transport interface for QUIC connections.
type transport struct {
privKey ic.PrivKey
localPeer peer.ID
tlsConf *tls.Config
pconn net.PacketConn
privKey ic.PrivKey
localPeer peer.ID
tlsConf *tls.Config
connManager *connManager
}
var _ tpt.Transport = &transport{}
......@@ -48,27 +85,21 @@ func NewTransport(key ic.PrivKey) (tpt.Transport, error) {
return nil, err
}
// create a packet conn for outgoing connections
addr, err := net.ResolveUDPAddr("udp", "localhost:0")
if err != nil {
return nil, err
}
conn, err := net.ListenUDP("udp", addr)
if err != nil {
return nil, err
}
return &transport{
privKey: key,
localPeer: localPeer,
tlsConf: tlsConf,
pconn: conn,
privKey: key,
localPeer: localPeer,
tlsConf: tlsConf,
connManager: &connManager{},
}, nil
}
// Dial dials a new QUIC connection
func (t *transport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (tpt.Conn, error) {
_, host, err := manet.DialArgs(raddr)
network, host, err := manet.DialArgs(raddr)
if err != nil {
return nil, err
}
pconn, err := t.connManager.GetConnForAddr(network)
if err != nil {
return nil, err
}
......@@ -100,7 +131,7 @@ func (t *transport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (tp
}
return nil
}
sess, err := quic.DialContext(ctx, t.pconn, addr, host, tlsConf, quicConfig)
sess, err := quic.DialContext(ctx, pconn, addr, host, tlsConf, quicConfig)
if err != nil {
return nil, err
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment