Unverified Commit e67650e7 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #48 from multiformats/fix/is-ip-starts-with

make the Is* commands only check if addrs start with
parents 4ae0494b a7b912ae
......@@ -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/<any value>/ip6/<one of the preceding ip6 values>
// IsIPLoopback returns whether a Multiaddr starts with a "Loopback" IP address
// This means either /ip4/127.*.*.*/*, /ip6/::1/*, or /ip6/::ffff:127.*.*.*.*/*,
// or /ip6zone/<any value>/ip6/<one of the preceding ip6 values>/*
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,...]
......
......@@ -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)")
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment