mirror of
https://github.com/waku-org/go-multiaddr.git
synced 2025-02-24 03:58:17 +00:00
added ipv6 link-local loopback
This commit is contained in:
parent
fb33680d08
commit
2bea5f93a2
18
convert.go
18
convert.go
@ -1,6 +1,7 @@
|
|||||||
package manet
|
package manet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
@ -14,6 +15,9 @@ var (
|
|||||||
|
|
||||||
// IP6Loopback is the ip6 loopback multiaddr
|
// IP6Loopback is the ip6 loopback multiaddr
|
||||||
IP6Loopback = ma.StringCast("/ip6/::1")
|
IP6Loopback = ma.StringCast("/ip6/::1")
|
||||||
|
|
||||||
|
// IP6LinkLocalLoopback is the ip6 link-local loopback multiaddr
|
||||||
|
IP6LinkLocalLoopback = ma.StringCast("/ip6/fe80::1")
|
||||||
)
|
)
|
||||||
|
|
||||||
var errIncorrectNetAddr = fmt.Errorf("incorrect network addr conversion")
|
var errIncorrectNetAddr = fmt.Errorf("incorrect network addr conversion")
|
||||||
@ -169,5 +173,17 @@ func IsThinWaist(m ma.Multiaddr) bool {
|
|||||||
// IsIPLoopback returns whether a Multiaddr is a "Loopback" IP address
|
// IsIPLoopback returns whether a Multiaddr is a "Loopback" IP address
|
||||||
// This means either /ip4/127.0.0.1 or /ip6/::1
|
// This means either /ip4/127.0.0.1 or /ip6/::1
|
||||||
func IsIPLoopback(m ma.Multiaddr) bool {
|
func IsIPLoopback(m ma.Multiaddr) bool {
|
||||||
return m.Equal(IP4Loopback) || m.Equal(IP6Loopback)
|
b := m.Bytes()
|
||||||
|
|
||||||
|
// /ip4/127 prefix (_entire_ /8 is loopback...)
|
||||||
|
if bytes.HasPrefix(b, []byte{4, 127}) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// /ip6/::1
|
||||||
|
if IP6Loopback.Equal(m) || IP6LinkLocalLoopback.Equal(m) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
12
net_test.go
12
net_test.go
@ -208,11 +208,19 @@ func TestLoopback(t *testing.T) {
|
|||||||
t.Error("IP6Loopback incorrect:", IP6Loopback)
|
t.Error("IP6Loopback incorrect:", IP6Loopback)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if IP6LinkLocalLoopback.String() != "/ip6/fe80::1" {
|
||||||
|
t.Error("IP6LinkLocalLoopback incorrect:", IP6Loopback)
|
||||||
|
}
|
||||||
|
|
||||||
if !IsIPLoopback(IP4Loopback) {
|
if !IsIPLoopback(IP4Loopback) {
|
||||||
t.Error("IsIPLoopback failed (IP4)")
|
t.Error("IsIPLoopback failed (IP4Loopback)")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !IsIPLoopback(IP6Loopback) {
|
if !IsIPLoopback(IP6Loopback) {
|
||||||
t.Error("IsIPLoopback failed (IP6)")
|
t.Error("IsIPLoopback failed (IP6Loopback)")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !IsIPLoopback(IP6LinkLocalLoopback) {
|
||||||
|
t.Error("IsIPLoopback failed (IP6LinkLocalLoopback)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user