Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
mf
go-multiaddr
Commits
c9dc72e5
Unverified
Commit
c9dc72e5
authored
Jul 04, 2023
by
Sukun
Committed by
GitHub
Jul 03, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
manet: add function to test if address is NAT64 IPv4 converted IPv6 address (#202)
parent
44887f80
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
0 deletions
+56
-0
net/ip.go
net/ip.go
+12
-0
net/ip_test.go
net/ip_test.go
+44
-0
No files found.
net/ip.go
View file @
c9dc72e5
package
manet
import
(
"bytes"
"net"
ma
"github.com/multiformats/go-multiaddr"
...
...
@@ -116,3 +117,14 @@ func zoneless(m ma.Multiaddr) ma.Multiaddr {
return
m
}
}
var
NAT64WellKnownPrefix
=
[
4
]
byte
{
0x0
,
0x64
,
0xff
,
0x9b
}
// 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
IsNAT64IPv4ConvertedIPv6Addr
(
addr
ma
.
Multiaddr
)
bool
{
c
,
_
:=
ma
.
SplitFirst
(
addr
)
return
c
!=
nil
&&
c
.
Protocol
()
.
Code
==
ma
.
P_IP6
&&
bytes
.
HasPrefix
(
c
.
RawValue
(),
NAT64WellKnownPrefix
[
:
])
}
net/ip_test.go
0 → 100644
View file @
c9dc72e5
package
manet
import
(
"fmt"
"testing"
ma
"github.com/multiformats/go-multiaddr"
)
func
TestIsWellKnownPrefixIPv4ConvertedIPv6Address
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
addr
ma
.
Multiaddr
want
bool
failureReason
string
}{
{
addr
:
ma
.
StringCast
(
"/ip4/1.2.3.4/tcp/1234"
),
want
:
false
,
failureReason
:
"ip4 addresses should return false"
,
},
{
addr
:
ma
.
StringCast
(
"/ip6/1::4/tcp/1234"
),
want
:
false
,
failureReason
:
"ip6 addresses doesn't have well-known prefix"
,
},
{
addr
:
ma
.
StringCast
(
"/ip6/::1/tcp/1234"
),
want
:
false
,
failureReason
:
"localhost addresses should return false"
,
},
{
addr
:
ma
.
StringCast
(
"/ip6/64:ff9b::192.0.1.2/tcp/1234"
),
want
:
true
,
failureReason
:
"ip6 address begins with well-known prefix"
,
},
}
for
i
,
tc
:=
range
cases
{
t
.
Run
(
fmt
.
Sprintf
(
"%d"
,
i
),
func
(
t
*
testing
.
T
)
{
if
IsNAT64IPv4ConvertedIPv6Addr
(
tc
.
addr
)
!=
tc
.
want
{
t
.
Fatalf
(
"%s %s"
,
tc
.
addr
,
tc
.
failureReason
)
}
})
}
}
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