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
2a2c6774
Commit
2a2c6774
authored
Jun 17, 2018
by
Marten Seemann
Browse files
add a function to decapsulate a quic multiaddr
parent
b9d0283f
Changes
5
Hide whitespace changes
Inline
Side-by-side
conn.go
View file @
2a2c6774
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
}
listener.go
View file @
2a2c6774
...
...
@@ -35,7 +35,7 @@ func newListener(addr ma.Multiaddr, transport tpt.Transport, localPeer peer.ID,
if
err
!=
nil
{
return
nil
,
err
}
localMultiaddr
,
err
:=
q
uicMultiaddr
(
ln
.
Addr
())
localMultiaddr
,
err
:=
toQ
uicMultiaddr
(
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
:=
q
uicMultiaddr
(
sess
.
RemoteAddr
())
remoteMultiaddr
,
err
:=
toQ
uicMultiaddr
(
sess
.
RemoteAddr
())
if
err
!=
nil
{
return
nil
,
err
}
...
...
quic_multiaddr.go
0 → 100644
View file @
2a2c6774
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
))
}
quic_multiaddr_test.go
0 → 100644
View file @
2a2c6774
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
))
})
})
transport.go
View file @
2a2c6774
...
...
@@ -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
:=
q
uicMultiaddr
(
sess
.
LocalAddr
())
localMultiaddr
,
err
:=
toQ
uicMultiaddr
(
sess
.
LocalAddr
())
if
err
!=
nil
{
return
nil
,
err
}
...
...
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