Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix nat64 well-known prefix check #205

Merged
merged 3 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions net/ip.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package manet

import (
"bytes"
"net"

ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -118,13 +117,21 @@
}
}

var NAT64WellKnownPrefix = [4]byte{0x0, 0x64, 0xff, 0x9b}
var NAT64WellKnownPrefix net.IPNet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to be exposed, right?


// IsNAT64IPv4ConvertedIPv6Addr returns whether addr is an IPv6 address that begins with
// the well-known prefix "64:ff9b" used for NAT64 Translation
// see RFC 6052
func init() {
var err error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var err error

_, np, err := net.ParseCIDR("64:ff9b::/96")
if err != nil {
panic(err)

Check warning on line 126 in net/ip.go

View check run for this annotation

Codecov / codecov/patch

net/ip.go#L126

Added line #L126 was not covered by tests
}
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 {
c, _ := ma.SplitFirst(addr)
return c != nil && c.Protocol().Code == ma.P_IP6 &&
bytes.HasPrefix(c.RawValue(), NAT64WellKnownPrefix[:])
NAT64WellKnownPrefix.Contains(net.IP(c.RawValue()))
}
5 changes: 5 additions & 0 deletions net/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func TestIsWellKnownPrefixIPv4ConvertedIPv6Address(t *testing.T) {
want: true,
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 {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
Expand Down