Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p-quic-transport
Commits
a9630561
Commit
a9630561
authored
Jul 14, 2018
by
Marten Seemann
Browse files
obey the multiaddr's IP version when listening
parent
baafdd62
Changes
2
Show whitespace changes
Inline
Side-by-side
listener.go
View file @
a9630561
...
...
@@ -27,11 +27,19 @@ type listener struct {
var
_
tpt
.
Listener
=
&
listener
{}
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
{
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
{
return
nil
,
err
}
...
...
listener_test.go
View file @
a9630561
...
...
@@ -15,10 +15,7 @@ import (
)
var
_
=
Describe
(
"Listener"
,
func
()
{
var
(
t
tpt
.
Transport
localAddr
ma
.
Multiaddr
)
var
t
tpt
.
Transport
BeforeEach
(
func
()
{
rsaKey
,
err
:=
rsa
.
GenerateKey
(
rand
.
Reader
,
1024
)
...
...
@@ -27,11 +24,11 @@ var _ = Describe("Listener", func() {
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
t
,
err
=
NewTransport
(
key
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
localAddr
,
err
=
ma
.
NewMultiaddr
(
"/ip4/127.0.0.1/udp/0/quic"
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
})
Context
(
"listening on the right address"
,
func
()
{
It
(
"returns the address it is listening on"
,
func
()
{
localAddr
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/127.0.0.1/udp/0/quic"
)
ln
,
err
:=
t
.
Listen
(
localAddr
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
netAddr
:=
ln
.
Addr
()
...
...
@@ -41,6 +38,40 @@ var _ = Describe("Listener", func() {
Expect
(
ln
.
Multiaddr
()
.
String
())
.
To
(
Equal
(
fmt
.
Sprintf
(
"/ip4/127.0.0.1/udp/%d/quic"
,
port
)))
})
It
(
"returns the address it is listening on, for listening on IPv4"
,
func
()
{
localAddr
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/0.0.0.0/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
(
"/ip4/0.0.0.0/udp/%d/quic"
,
port
)))
})
It
(
"returns the address it is listening on, for listening on IPv6"
,
func
()
{
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
)))
})
})
Context
(
"accepting connections"
,
func
()
{
var
localAddr
ma
.
Multiaddr
BeforeEach
(
func
()
{
var
err
error
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
())
...
...
@@ -64,4 +95,5 @@ var _ = Describe("Listener", func() {
_
,
err
=
ln
.
Accept
()
Expect
(
err
)
.
To
(
HaveOccurred
())
})
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment