diff --git a/p2p/transport/quic/conn.go b/p2p/transport/quic/conn.go index 312da6d1..fef58739 100644 --- a/p2p/transport/quic/conn.go +++ b/p2p/transport/quic/conn.go @@ -1,15 +1,12 @@ package libp2pquic import ( - "net" - ic "github.com/libp2p/go-libp2p-crypto" peer "github.com/libp2p/go-libp2p-peer" tpt "github.com/libp2p/go-libp2p-transport" smux "github.com/libp2p/go-stream-muxer" quic "github.com/lucas-clemente/quic-go" ma "github.com/multiformats/go-multiaddr" - manet "github.com/multiformats/go-multiaddr-net" ) type conn struct { @@ -81,15 +78,3 @@ func (c *conn) RemoteMultiaddr() ma.Multiaddr { func (c *conn) Transport() tpt.Transport { return c.transport } - -func quicMultiaddr(na net.Addr) (ma.Multiaddr, error) { - udpMA, err := manet.FromNetAddr(na) - if err != nil { - return nil, err - } - quicMA, err := ma.NewMultiaddr("/quic") - if err != nil { - return nil, err - } - return udpMA.Encapsulate(quicMA), nil -} diff --git a/p2p/transport/quic/listener.go b/p2p/transport/quic/listener.go index a3638dbe..52558872 100644 --- a/p2p/transport/quic/listener.go +++ b/p2p/transport/quic/listener.go @@ -35,7 +35,7 @@ func newListener(addr ma.Multiaddr, transport tpt.Transport, localPeer peer.ID, if err != nil { return nil, err } - localMultiaddr, err := quicMultiaddr(ln.Addr()) + localMultiaddr, err := toQuicMultiaddr(ln.Addr()) if err != nil { return nil, err } @@ -73,7 +73,7 @@ func (l *listener) setupConn(sess quic.Session) (tpt.Conn, error) { if err != nil { return nil, err } - remoteMultiaddr, err := quicMultiaddr(sess.RemoteAddr()) + remoteMultiaddr, err := toQuicMultiaddr(sess.RemoteAddr()) if err != nil { return nil, err } diff --git a/p2p/transport/quic/quic_multiaddr.go b/p2p/transport/quic/quic_multiaddr.go new file mode 100644 index 00000000..8b182b76 --- /dev/null +++ b/p2p/transport/quic/quic_multiaddr.go @@ -0,0 +1,30 @@ +package libp2pquic + +import ( + "net" + + ma "github.com/multiformats/go-multiaddr" + manet "github.com/multiformats/go-multiaddr-net" +) + +var quicMA ma.Multiaddr + +func init() { + var err error + quicMA, err = ma.NewMultiaddr("/quic") + if err != nil { + panic(err) + } +} + +func toQuicMultiaddr(na net.Addr) (ma.Multiaddr, error) { + udpMA, err := manet.FromNetAddr(na) + if err != nil { + return nil, err + } + return udpMA.Encapsulate(quicMA), nil +} + +func fromQuicMultiaddr(addr ma.Multiaddr) (net.Addr, error) { + return manet.ToNetAddr(addr.Decapsulate(quicMA)) +} diff --git a/p2p/transport/quic/quic_multiaddr_test.go b/p2p/transport/quic/quic_multiaddr_test.go new file mode 100644 index 00000000..48949d3a --- /dev/null +++ b/p2p/transport/quic/quic_multiaddr_test.go @@ -0,0 +1,30 @@ +package libp2pquic + +import ( + "net" + + ma "github.com/multiformats/go-multiaddr" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("QUIC Multiaddr", func() { + It("converts a net.Addr to a QUIC Multiaddr", func() { + addr := &net.UDPAddr{IP: net.IPv4(192, 168, 0, 42), Port: 1337} + maddr, err := toQuicMultiaddr(addr) + Expect(err).ToNot(HaveOccurred()) + Expect(maddr.String()).To(Equal("/ip4/192.168.0.42/udp/1337/quic")) + }) + + It("converts a QUIC Multiaddr to a net.Addr", func() { + maddr, err := ma.NewMultiaddr("/ip4/192.168.0.42/udp/1337/quic") + Expect(err).ToNot(HaveOccurred()) + addr, err := fromQuicMultiaddr(maddr) + Expect(err).ToNot(HaveOccurred()) + Expect(addr).To(BeAssignableToTypeOf(&net.UDPAddr{})) + udpAddr := addr.(*net.UDPAddr) + Expect(udpAddr.IP).To(Equal(net.IPv4(192, 168, 0, 42))) + Expect(udpAddr.Port).To(Equal(1337)) + }) +}) diff --git a/p2p/transport/quic/transport.go b/p2p/transport/quic/transport.go index a763c734..909f8508 100644 --- a/p2p/transport/quic/transport.go +++ b/p2p/transport/quic/transport.go @@ -86,7 +86,7 @@ func (t *transport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (tp if err != nil { return nil, err } - localMultiaddr, err := quicMultiaddr(sess.LocalAddr()) + localMultiaddr, err := toQuicMultiaddr(sess.LocalAddr()) if err != nil { return nil, err }