diff --git a/ip.go b/ip.go index 296d3c1..1cf9a77 100644 --- a/ip.go +++ b/ip.go @@ -55,19 +55,15 @@ func IsThinWaist(m ma.Multiaddr) bool { } } -// IsIPLoopback returns whether a Multiaddr is a "Loopback" IP address -// This means either /ip4/127.*.*.*, /ip6/::1, or /ip6/::ffff:127.*.*.*.*, -// or /ip6zone//ip6/ +// IsIPLoopback returns whether a Multiaddr starts with a "Loopback" IP address +// This means either /ip4/127.*.*.*/*, /ip6/::1/*, or /ip6/::ffff:127.*.*.*.*/*, +// or /ip6zone//ip6//* func IsIPLoopback(m ma.Multiaddr) bool { m = zoneless(m) - c, rest := ma.SplitFirst(m) + c, _ := ma.SplitFirst(m) if c == nil { return false } - if rest != nil { - // Not *just* an IPv4 addr - return false - } switch c.Protocol().Code { case ma.P_IP4, ma.P_IP6: return net.IP(c.RawValue()).IsLoopback() @@ -88,14 +84,15 @@ func IsIP6LinkLocal(m ma.Multiaddr) bool { return ip.IsLinkLocalMulticast() || ip.IsLinkLocalUnicast() } -// IsIPUnspecified returns whether a Multiaddr is am Unspecified IP address -// This means either /ip4/0.0.0.0 or /ip6/:: +// IsIPUnspecified returns whether a Multiaddr starts with an Unspecified IP address +// This means either /ip4/0.0.0.0/* or /ip6/::/* func IsIPUnspecified(m ma.Multiaddr) bool { m = zoneless(m) if m == nil { return false } - return IP4Unspecified.Equal(m) || IP6Unspecified.Equal(m) + c, _ := ma.SplitFirst(m) + return net.IP(c.RawValue()).IsUnspecified() } // If m matches [zone,ip6,...], return [ip6,...] diff --git a/net_test.go b/net_test.go index 1c70997..cd1d7ec 100644 --- a/net_test.go +++ b/net_test.go @@ -330,10 +330,6 @@ func TestIPLoopback(t *testing.T) { t.Error("IsIPLoopback false positive (/ip4/112.123.11.1)") } - if IsIPLoopback(newMultiaddr(t, "/ip4/127.0.0.1/ip4/127.0.0.1")) { - t.Error("IsIPLoopback false positive (/ip4/127.0.0.1/127.0.0.1)") - } - if IsIPLoopback(newMultiaddr(t, "/ip4/192.168.0.1/ip6/::1")) { t.Error("IsIPLoopback false positive (/ip4/192.168.0.1/ip6/::1)") } @@ -362,10 +358,6 @@ func TestIPLoopback(t *testing.T) { t.Error("IsIPLoopback failed (/ip6zone/xxx/ip6/::1)") } - if IsIPLoopback(newMultiaddr(t, "/ip6zone/0/ip6/::1/tcp/3333")) { - t.Error("IsIPLoopback failed (/ip6zone/0/ip6/::1/tcp/3333)") - } - if IsIPLoopback(newMultiaddr(t, "/ip6zone/0/ip6/1::1")) { t.Errorf("IsIPLoopback false positive (/ip6zone/0/ip6/1::1)") }