Unverified Commit 0e7485e8 authored by Sukun's avatar Sukun Committed by GitHub

fix nat64 well-known prefix check (#205)

* fix nat64 well-known prefix check

* don't use pointer

* make NAT64WellKnownPrefix private
parent e9b02fcd
package manet package manet
import ( import (
"bytes"
"net" "net"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
...@@ -118,13 +117,20 @@ func zoneless(m ma.Multiaddr) ma.Multiaddr { ...@@ -118,13 +117,20 @@ func zoneless(m ma.Multiaddr) ma.Multiaddr {
} }
} }
var NAT64WellKnownPrefix = [4]byte{0x0, 0x64, 0xff, 0x9b} var nat64WellKnownPrefix net.IPNet
// IsNAT64IPv4ConvertedIPv6Addr returns whether addr is an IPv6 address that begins with func init() {
// the well-known prefix "64:ff9b" used for NAT64 Translation _, np, err := net.ParseCIDR("64:ff9b::/96")
// see RFC 6052 if err != nil {
panic(err)
}
nat64WellKnownPrefix = *np
}
// IsNAT64IPv4ConvertedIPv6Addr returns whether addr is a well-known prefix "64:ff9b::/96" addr
// used for NAT64 Translation. See RFC 6052
func IsNAT64IPv4ConvertedIPv6Addr(addr ma.Multiaddr) bool { func IsNAT64IPv4ConvertedIPv6Addr(addr ma.Multiaddr) bool {
c, _ := ma.SplitFirst(addr) c, _ := ma.SplitFirst(addr)
return c != nil && c.Protocol().Code == ma.P_IP6 && return c != nil && c.Protocol().Code == ma.P_IP6 &&
bytes.HasPrefix(c.RawValue(), NAT64WellKnownPrefix[:]) nat64WellKnownPrefix.Contains(net.IP(c.RawValue()))
} }
...@@ -33,6 +33,11 @@ func TestIsWellKnownPrefixIPv4ConvertedIPv6Address(t *testing.T) { ...@@ -33,6 +33,11 @@ func TestIsWellKnownPrefixIPv4ConvertedIPv6Address(t *testing.T) {
want: true, want: true,
failureReason: "ip6 address begins with well-known prefix", failureReason: "ip6 address begins with well-known prefix",
}, },
{
addr: ma.StringCast("/ip6/64:ff9b::1:192.0.1.2/tcp/1234"),
want: false,
failureReason: "64:ff9b::1 is not well-known prefix",
},
} }
for i, tc := range cases { for i, tc := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
......
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