Skip to content

Commit a7b912a

Browse files
committed
make the Is* commands only check if addrs start with
It turns out we *were* relying on this. Furthermore, this is generally more useful (especially given ip6zones).
1 parent 4ae0494 commit a7b912a

File tree

2 files changed

+8
-19
lines changed

2 files changed

+8
-19
lines changed

ip.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,15 @@ func IsThinWaist(m ma.Multiaddr) bool {
5555
}
5656
}
5757

58-
// IsIPLoopback returns whether a Multiaddr is a "Loopback" IP address
59-
// This means either /ip4/127.*.*.*, /ip6/::1, or /ip6/::ffff:127.*.*.*.*,
60-
// or /ip6zone/<any value>/ip6/<one of the preceding ip6 values>
58+
// IsIPLoopback returns whether a Multiaddr starts with a "Loopback" IP address
59+
// This means either /ip4/127.*.*.*/*, /ip6/::1/*, or /ip6/::ffff:127.*.*.*.*/*,
60+
// or /ip6zone/<any value>/ip6/<one of the preceding ip6 values>/*
6161
func IsIPLoopback(m ma.Multiaddr) bool {
6262
m = zoneless(m)
63-
c, rest := ma.SplitFirst(m)
63+
c, _ := ma.SplitFirst(m)
6464
if c == nil {
6565
return false
6666
}
67-
if rest != nil {
68-
// Not *just* an IPv4 addr
69-
return false
70-
}
7167
switch c.Protocol().Code {
7268
case ma.P_IP4, ma.P_IP6:
7369
return net.IP(c.RawValue()).IsLoopback()
@@ -88,14 +84,15 @@ func IsIP6LinkLocal(m ma.Multiaddr) bool {
8884
return ip.IsLinkLocalMulticast() || ip.IsLinkLocalUnicast()
8985
}
9086

91-
// IsIPUnspecified returns whether a Multiaddr is am Unspecified IP address
92-
// This means either /ip4/0.0.0.0 or /ip6/::
87+
// IsIPUnspecified returns whether a Multiaddr starts with an Unspecified IP address
88+
// This means either /ip4/0.0.0.0/* or /ip6/::/*
9389
func IsIPUnspecified(m ma.Multiaddr) bool {
9490
m = zoneless(m)
9591
if m == nil {
9692
return false
9793
}
98-
return IP4Unspecified.Equal(m) || IP6Unspecified.Equal(m)
94+
c, _ := ma.SplitFirst(m)
95+
return net.IP(c.RawValue()).IsUnspecified()
9996
}
10097

10198
// If m matches [zone,ip6,...], return [ip6,...]

net_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,6 @@ func TestIPLoopback(t *testing.T) {
330330
t.Error("IsIPLoopback false positive (/ip4/112.123.11.1)")
331331
}
332332

333-
if IsIPLoopback(newMultiaddr(t, "/ip4/127.0.0.1/ip4/127.0.0.1")) {
334-
t.Error("IsIPLoopback false positive (/ip4/127.0.0.1/127.0.0.1)")
335-
}
336-
337333
if IsIPLoopback(newMultiaddr(t, "/ip4/192.168.0.1/ip6/::1")) {
338334
t.Error("IsIPLoopback false positive (/ip4/192.168.0.1/ip6/::1)")
339335
}
@@ -362,10 +358,6 @@ func TestIPLoopback(t *testing.T) {
362358
t.Error("IsIPLoopback failed (/ip6zone/xxx/ip6/::1)")
363359
}
364360

365-
if IsIPLoopback(newMultiaddr(t, "/ip6zone/0/ip6/::1/tcp/3333")) {
366-
t.Error("IsIPLoopback failed (/ip6zone/0/ip6/::1/tcp/3333)")
367-
}
368-
369361
if IsIPLoopback(newMultiaddr(t, "/ip6zone/0/ip6/1::1")) {
370362
t.Errorf("IsIPLoopback false positive (/ip6zone/0/ip6/1::1)")
371363
}

0 commit comments

Comments
 (0)