Merge pull request #40 from multiformats/fix/39

fix loopback addresses
This commit is contained in:
Steven Allen 2018-10-01 00:38:29 +00:00 committed by GitHub
commit 1cb9a0e8a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 14 deletions

View File

@ -6,7 +6,7 @@ sudo: true
language: go language: go
go: go:
- 1.9.x - 1.11
install: install:
- make deps - make deps

10
ip.go
View File

@ -14,8 +14,8 @@ var (
// IP6Loopback is the ip6 loopback multiaddr // IP6Loopback is the ip6 loopback multiaddr
IP6Loopback = ma.StringCast("/ip6/::1") IP6Loopback = ma.StringCast("/ip6/::1")
// IP6LinkLocalLoopback is the ip6 link-local loopback multiaddr // IP4MappedIP6Loopback is the IPv4 Mapped IPv6 loopback address.
IP6LinkLocalLoopback = ma.StringCast("/ip6/fe80::1") IP4MappedIP6Loopback = ma.StringCast("/ip6/::ffff:127.0.0.1")
) )
// Unspecified Addresses (used for ) // Unspecified Addresses (used for )
@ -28,8 +28,8 @@ var (
// following byte sequences is considered a loopback multiaddr. // following byte sequences is considered a loopback multiaddr.
var loopbackPrefixes = [][]byte{ var loopbackPrefixes = [][]byte{
{ma.P_IP4, 127}, // 127.* {ma.P_IP4, 127}, // 127.*
IP6LinkLocalLoopback.Bytes(), {ma.P_IP6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 127}, // ::ffff:127.*
IP6Loopback.Bytes(), IP6Loopback.Bytes(), // ::1
} }
// IsThinWaist returns whether a Multiaddr starts with "Thin Waist" Protocols. // IsThinWaist returns whether a Multiaddr starts with "Thin Waist" Protocols.
@ -60,7 +60,7 @@ func IsThinWaist(m ma.Multiaddr) bool {
} }
// IsIPLoopback returns whether a Multiaddr is a "Loopback" IP address // IsIPLoopback returns whether a Multiaddr is a "Loopback" IP address
// This means either /ip4/127.*.*.*, /ip6/::1, or /ip6/fe80::1 // This means either /ip4/127.*.*.*, /ip6/::1, or /ip6/::ffff:127.*.*.*.*
func IsIPLoopback(m ma.Multiaddr) bool { func IsIPLoopback(m ma.Multiaddr) bool {
b := m.Bytes() b := m.Bytes()
for _, prefix := range loopbackPrefixes { for _, prefix := range loopbackPrefixes {

View File

@ -306,8 +306,8 @@ func TestIPLoopback(t *testing.T) {
t.Error("IP6Loopback incorrect:", IP6Loopback) t.Error("IP6Loopback incorrect:", IP6Loopback)
} }
if IP6LinkLocalLoopback.String() != "/ip6/fe80::1" { if IP4MappedIP6Loopback.String() != "/ip6/127.0.0.1" {
t.Error("IP6LinkLocalLoopback incorrect:", IP6Loopback) t.Error("IP4MappedIP6Loopback incorrect:", IP4MappedIP6Loopback)
} }
if !IsIPLoopback(IP4Loopback) { if !IsIPLoopback(IP4Loopback) {
@ -330,8 +330,16 @@ func TestIPLoopback(t *testing.T) {
t.Error("IsIPLoopback failed (IP6Loopback)") t.Error("IsIPLoopback failed (IP6Loopback)")
} }
if !IsIPLoopback(IP6LinkLocalLoopback) { if !IsIPLoopback(newMultiaddr(t, "/ip6/127.0.0.1")) {
t.Error("IsIPLoopback failed (IP6LinkLocalLoopback)") t.Error("IsIPLoopback failed (/ip6/127.0.0.1)")
}
if !IsIPLoopback(newMultiaddr(t, "/ip6/127.99.3.2")) {
t.Error("IsIPLoopback failed (/ip6/127.99.3.2)")
}
if IsIPLoopback(newMultiaddr(t, "/ip6/::fffa:127.99.3.2")) {
t.Error("IsIPLoopback false positive (/ip6/::fffa:127.99.3.2)")
} }
} }
@ -354,10 +362,6 @@ func TestIPUnspecified(t *testing.T) {
} }
func TestIP6LinkLocal(t *testing.T) { func TestIP6LinkLocal(t *testing.T) {
if !IsIP6LinkLocal(IP6LinkLocalLoopback) {
t.Error("IsIP6LinkLocal failed (IP6LinkLocalLoopback)")
}
for a := 0; a < 65536; a++ { for a := 0; a < 65536; a++ {
isLinkLocal := (a == 0xfe80) isLinkLocal := (a == 0xfe80)
m := newMultiaddr(t, fmt.Sprintf("/ip6/%x::1", a)) m := newMultiaddr(t, fmt.Sprintf("/ip6/%x::1", a))