mirror of
https://github.com/logos-messaging/go-multiaddr.git
synced 2026-01-02 21:13:12 +00:00
Cast and Loopback
This commit is contained in:
parent
e94d248a76
commit
5d64ba1250
14
convert.go
14
convert.go
@ -8,6 +8,14 @@ import (
|
||||
ma "github.com/jbenet/go-multiaddr"
|
||||
)
|
||||
|
||||
var (
|
||||
// IP4Loopback is the ip4 loopback multiaddr
|
||||
IP4Loopback = ma.StringCast("/ip4/127.0.0.1")
|
||||
|
||||
// IP6Loopback is the ip6 loopback multiaddr
|
||||
IP6Loopback = ma.StringCast("/ip6/::1")
|
||||
)
|
||||
|
||||
var errIncorrectNetAddr = fmt.Errorf("incorrect network addr conversion")
|
||||
|
||||
// FromNetAddr converts a net.Addr type to a Multiaddr.
|
||||
@ -150,3 +158,9 @@ func IsThinWaist(m ma.Multiaddr) bool {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// IsIPLoopback returns whether a Multiaddr is a "Loopback" IP address
|
||||
// This means either /ip4/127.0.0.1 or /ip6/::1
|
||||
func IsIPLoopback(m ma.Multiaddr) bool {
|
||||
return m.Equal(IP4Loopback) || m.Equal(IP6Loopback)
|
||||
}
|
||||
|
||||
18
net_test.go
18
net_test.go
@ -198,3 +198,21 @@ func TestListenAndDial(t *testing.T) {
|
||||
cA.Close()
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestLoopback(t *testing.T) {
|
||||
if IP4Loopback.String() != "/ip4/127.0.0.1" {
|
||||
t.Error("IP4Loopback incorrect:", IP4Loopback)
|
||||
}
|
||||
|
||||
if IP6Loopback.String() != "/ip6/::1" {
|
||||
t.Error("IP6Loopback incorrect:", IP6Loopback)
|
||||
}
|
||||
|
||||
if !IsIPLoopback(IP4Loopback) {
|
||||
t.Error("IsIPLoopback failed (IP4)")
|
||||
}
|
||||
|
||||
if !IsIPLoopback(IP6Loopback) {
|
||||
t.Error("IsIPLoopback failed (IP6)")
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user