mirror of
https://github.com/logos-messaging/go-multiaddr.git
synced 2026-01-02 13:03:11 +00:00
fix loopback/link-local checks
1. Ensure we check the entire addr, not just a prefix. 2. Make the LinkLocal test skip zones. 3. Consider link-local multicast to be link-local. 4. Defer to *go* to determine if something is, in fact, a loopback/link-local address.
This commit is contained in:
parent
3895ff188b
commit
03e871bdbe
51
ip.go
51
ip.go
@ -1,7 +1,7 @@
|
||||
package manet
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net"
|
||||
|
||||
ma "github.com/multiformats/go-multiaddr"
|
||||
)
|
||||
@ -24,14 +24,6 @@ var (
|
||||
IP6Unspecified = ma.StringCast("/ip6/::")
|
||||
)
|
||||
|
||||
// Loopback multiaddr prefixes. Any multiaddr beginning with one of the
|
||||
// following byte sequences is considered a loopback multiaddr.
|
||||
var loopbackPrefixes = [][]byte{
|
||||
{ma.P_IP4, 127}, // 127.*
|
||||
{ma.P_IP6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 127}, // ::ffff:127.*
|
||||
IP6Loopback.Bytes(), // ::1
|
||||
}
|
||||
|
||||
// IsThinWaist returns whether a Multiaddr starts with "Thin Waist" Protocols.
|
||||
// This means: /{IP4, IP6}[/{TCP, UDP}]
|
||||
func IsThinWaist(m ma.Multiaddr) bool {
|
||||
@ -62,22 +54,41 @@ 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.*.*.*.*
|
||||
func IsIPLoopback(m ma.Multiaddr) bool {
|
||||
b := m.Bytes()
|
||||
for _, prefix := range loopbackPrefixes {
|
||||
if bytes.HasPrefix(b, prefix) {
|
||||
return true
|
||||
}
|
||||
c, rest := ma.SplitFirst(m)
|
||||
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()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsIP6LinkLocal returns if a multiaddress is an IPv6 local link. These
|
||||
// addresses are non routable. The prefix is technically
|
||||
// fe80::/10, but we test fe80::/16 for simplicity (no need to mask).
|
||||
// So far, no hardware interfaces exist long enough to use those 2 bits.
|
||||
// Send a PR if there is.
|
||||
// IsIP6LinkLocal returns if a an IPv6 link-local multiaddress (with zero or
|
||||
// more leading zones). These addresses are non routable.
|
||||
func IsIP6LinkLocal(m ma.Multiaddr) bool {
|
||||
return bytes.HasPrefix(m.Bytes(), []byte{ma.P_IP6, 0xfe, 0x80})
|
||||
matched := false
|
||||
ma.ForEach(m, func(c ma.Component) bool {
|
||||
// Too much.
|
||||
if matched {
|
||||
matched = false
|
||||
return false
|
||||
}
|
||||
|
||||
switch c.Protocol().Code {
|
||||
case ma.P_IP6ZONE:
|
||||
return true
|
||||
case ma.P_IP6:
|
||||
ip := net.IP(c.RawValue())
|
||||
matched = ip.IsLinkLocalMulticast() || ip.IsLinkLocalUnicast()
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
})
|
||||
return matched
|
||||
}
|
||||
|
||||
// IsIPUnspecified returns whether a Multiaddr is am Unspecified IP address
|
||||
|
||||
@ -322,6 +322,10 @@ 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)")
|
||||
}
|
||||
@ -363,7 +367,7 @@ func TestIPUnspecified(t *testing.T) {
|
||||
|
||||
func TestIP6LinkLocal(t *testing.T) {
|
||||
for a := 0; a < 65536; a++ {
|
||||
isLinkLocal := (a == 0xfe80)
|
||||
isLinkLocal := (a&0xffc0 == 0xfe80 || a&0xff0f == 0xff02)
|
||||
m := newMultiaddr(t, fmt.Sprintf("/ip6/%x::1", a))
|
||||
if IsIP6LinkLocal(m) != isLinkLocal {
|
||||
t.Errorf("IsIP6LinkLocal failed (%s != %v)", m, isLinkLocal)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user