Skip to content

Commit 1cb9a0e

Browse files
authored
Merge pull request #40 from multiformats/fix/39
fix loopback addresses
2 parents ceed2dc + 5d0dbe0 commit 1cb9a0e

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sudo: true
66
language: go
77

88
go:
9-
- 1.9.x
9+
- 1.11
1010

1111
install:
1212
- make deps

ip.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ var (
1414
// IP6Loopback is the ip6 loopback multiaddr
1515
IP6Loopback = ma.StringCast("/ip6/::1")
1616

17-
// IP6LinkLocalLoopback is the ip6 link-local loopback multiaddr
18-
IP6LinkLocalLoopback = ma.StringCast("/ip6/fe80::1")
17+
// IP4MappedIP6Loopback is the IPv4 Mapped IPv6 loopback address.
18+
IP4MappedIP6Loopback = ma.StringCast("/ip6/::ffff:127.0.0.1")
1919
)
2020

2121
// Unspecified Addresses (used for )
@@ -28,8 +28,8 @@ var (
2828
// following byte sequences is considered a loopback multiaddr.
2929
var loopbackPrefixes = [][]byte{
3030
{ma.P_IP4, 127}, // 127.*
31-
IP6LinkLocalLoopback.Bytes(),
32-
IP6Loopback.Bytes(),
31+
{ma.P_IP6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 127}, // ::ffff:127.*
32+
IP6Loopback.Bytes(), // ::1
3333
}
3434

3535
// IsThinWaist returns whether a Multiaddr starts with "Thin Waist" Protocols.
@@ -60,7 +60,7 @@ func IsThinWaist(m ma.Multiaddr) bool {
6060
}
6161

6262
// IsIPLoopback returns whether a Multiaddr is a "Loopback" IP address
63-
// This means either /ip4/127.*.*.*, /ip6/::1, or /ip6/fe80::1
63+
// This means either /ip4/127.*.*.*, /ip6/::1, or /ip6/::ffff:127.*.*.*.*
6464
func IsIPLoopback(m ma.Multiaddr) bool {
6565
b := m.Bytes()
6666
for _, prefix := range loopbackPrefixes {

net_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ func TestIPLoopback(t *testing.T) {
306306
t.Error("IP6Loopback incorrect:", IP6Loopback)
307307
}
308308

309-
if IP6LinkLocalLoopback.String() != "/ip6/fe80::1" {
310-
t.Error("IP6LinkLocalLoopback incorrect:", IP6Loopback)
309+
if IP4MappedIP6Loopback.String() != "/ip6/127.0.0.1" {
310+
t.Error("IP4MappedIP6Loopback incorrect:", IP4MappedIP6Loopback)
311311
}
312312

313313
if !IsIPLoopback(IP4Loopback) {
@@ -330,8 +330,16 @@ func TestIPLoopback(t *testing.T) {
330330
t.Error("IsIPLoopback failed (IP6Loopback)")
331331
}
332332

333-
if !IsIPLoopback(IP6LinkLocalLoopback) {
334-
t.Error("IsIPLoopback failed (IP6LinkLocalLoopback)")
333+
if !IsIPLoopback(newMultiaddr(t, "/ip6/127.0.0.1")) {
334+
t.Error("IsIPLoopback failed (/ip6/127.0.0.1)")
335+
}
336+
337+
if !IsIPLoopback(newMultiaddr(t, "/ip6/127.99.3.2")) {
338+
t.Error("IsIPLoopback failed (/ip6/127.99.3.2)")
339+
}
340+
341+
if IsIPLoopback(newMultiaddr(t, "/ip6/::fffa:127.99.3.2")) {
342+
t.Error("IsIPLoopback false positive (/ip6/::fffa:127.99.3.2)")
335343
}
336344
}
337345

@@ -354,10 +362,6 @@ func TestIPUnspecified(t *testing.T) {
354362
}
355363

356364
func TestIP6LinkLocal(t *testing.T) {
357-
if !IsIP6LinkLocal(IP6LinkLocalLoopback) {
358-
t.Error("IsIP6LinkLocal failed (IP6LinkLocalLoopback)")
359-
}
360-
361365
for a := 0; a < 65536; a++ {
362366
isLinkLocal := (a == 0xfe80)
363367
m := newMultiaddr(t, fmt.Sprintf("/ip6/%x::1", a))

0 commit comments

Comments
 (0)