This repository has been archived by the owner on May 26, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a function to decapsulate a quic multiaddr
- Loading branch information
1 parent
b9d0283
commit 2a2c677
Showing
5 changed files
with
63 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters