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

obey the multiaddr's IP version when listening

parent baafdd62
...@@ -27,11 +27,19 @@ type listener struct { ...@@ -27,11 +27,19 @@ type listener struct {
var _ tpt.Listener = &listener{} var _ tpt.Listener = &listener{}
func newListener(addr ma.Multiaddr, transport tpt.Transport, localPeer peer.ID, key ic.PrivKey, tlsConf *tls.Config) (tpt.Listener, error) { func newListener(addr ma.Multiaddr, transport tpt.Transport, localPeer peer.ID, key ic.PrivKey, tlsConf *tls.Config) (tpt.Listener, error) {
_, host, err := manet.DialArgs(addr) lnet, host, err := manet.DialArgs(addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
ln, err := quicListenAddr(host, tlsConf, quicConfig) laddr, err := net.ResolveUDPAddr(lnet, host)
if err != nil {
return nil, err
}
conn, err := net.ListenUDP(lnet, laddr)
if err != nil {
return nil, err
}
ln, err := quic.Listen(conn, tlsConf, quicConfig)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -15,10 +15,7 @@ import ( ...@@ -15,10 +15,7 @@ import (
) )
var _ = Describe("Listener", func() { var _ = Describe("Listener", func() {
var ( var t tpt.Transport
t tpt.Transport
localAddr ma.Multiaddr
)
BeforeEach(func() { BeforeEach(func() {
rsaKey, err := rsa.GenerateKey(rand.Reader, 1024) rsaKey, err := rsa.GenerateKey(rand.Reader, 1024)
...@@ -27,41 +24,76 @@ var _ = Describe("Listener", func() { ...@@ -27,41 +24,76 @@ var _ = Describe("Listener", func() {
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
t, err = NewTransport(key) t, err = NewTransport(key)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
localAddr, err = ma.NewMultiaddr("/ip4/127.0.0.1/udp/0/quic")
Expect(err).ToNot(HaveOccurred())
}) })
It("returns the address it is listening on", func() { Context("listening on the right address", func() {
ln, err := t.Listen(localAddr) It("returns the address it is listening on", func() {
Expect(err).ToNot(HaveOccurred()) localAddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/0/quic")
netAddr := ln.Addr() ln, err := t.Listen(localAddr)
Expect(netAddr).To(BeAssignableToTypeOf(&net.UDPAddr{})) Expect(err).ToNot(HaveOccurred())
port := netAddr.(*net.UDPAddr).Port netAddr := ln.Addr()
Expect(port).ToNot(BeZero()) Expect(netAddr).To(BeAssignableToTypeOf(&net.UDPAddr{}))
Expect(ln.Multiaddr().String()).To(Equal(fmt.Sprintf("/ip4/127.0.0.1/udp/%d/quic", port))) port := netAddr.(*net.UDPAddr).Port
}) Expect(port).ToNot(BeZero())
Expect(ln.Multiaddr().String()).To(Equal(fmt.Sprintf("/ip4/127.0.0.1/udp/%d/quic", port)))
})
It("returns Accept when it is closed", func() { It("returns the address it is listening on, for listening on IPv4", func() {
addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/0/quic") localAddr, err := ma.NewMultiaddr("/ip4/0.0.0.0/udp/0/quic")
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
ln, err := t.Listen(addr) ln, err := t.Listen(localAddr)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
done := make(chan struct{}) netAddr := ln.Addr()
go func() { Expect(netAddr).To(BeAssignableToTypeOf(&net.UDPAddr{}))
defer GinkgoRecover() port := netAddr.(*net.UDPAddr).Port
ln.Accept() Expect(port).ToNot(BeZero())
close(done) Expect(ln.Multiaddr().String()).To(Equal(fmt.Sprintf("/ip4/0.0.0.0/udp/%d/quic", port)))
}() })
Consistently(done).ShouldNot(BeClosed())
Expect(ln.Close()).To(Succeed()) It("returns the address it is listening on, for listening on IPv6", func() {
Eventually(done).Should(BeClosed()) localAddr, err := ma.NewMultiaddr("/ip6/::/udp/0/quic")
Expect(err).ToNot(HaveOccurred())
ln, err := t.Listen(localAddr)
Expect(err).ToNot(HaveOccurred())
netAddr := ln.Addr()
Expect(netAddr).To(BeAssignableToTypeOf(&net.UDPAddr{}))
port := netAddr.(*net.UDPAddr).Port
Expect(port).ToNot(BeZero())
Expect(ln.Multiaddr().String()).To(Equal(fmt.Sprintf("/ip6/::/udp/%d/quic", port)))
})
}) })
It("doesn't accept Accept calls after it is closed", func() { Context("accepting connections", func() {
ln, err := t.Listen(localAddr) var localAddr ma.Multiaddr
Expect(err).ToNot(HaveOccurred())
Expect(ln.Close()).To(Succeed()) BeforeEach(func() {
_, err = ln.Accept() var err error
Expect(err).To(HaveOccurred()) localAddr, err = ma.NewMultiaddr("/ip4/127.0.0.1/udp/0/quic")
Expect(err).ToNot(HaveOccurred())
})
It("returns Accept when it is closed", func() {
addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/0/quic")
Expect(err).ToNot(HaveOccurred())
ln, err := t.Listen(addr)
Expect(err).ToNot(HaveOccurred())
done := make(chan struct{})
go func() {
defer GinkgoRecover()
ln.Accept()
close(done)
}()
Consistently(done).ShouldNot(BeClosed())
Expect(ln.Close()).To(Succeed())
Eventually(done).Should(BeClosed())
})
It("doesn't accept Accept calls after it is closed", func() {
ln, err := t.Listen(localAddr)
Expect(err).ToNot(HaveOccurred())
Expect(ln.Close()).To(Succeed())
_, err = ln.Accept()
Expect(err).To(HaveOccurred())
})
}) })
}) })
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