Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
mf
go-multiaddr
Commits
bb34b84e
Commit
bb34b84e
authored
Nov 05, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cast and Loopback
parent
5f35e6d9
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
0 deletions
+50
-0
multiaddr.go
multiaddr.go
+18
-0
net/convert.go
net/convert.go
+14
-0
net/net_test.go
net/net_test.go
+18
-0
No files found.
multiaddr.go
View file @
bb34b84e
...
...
@@ -124,3 +124,21 @@ func (m *multiaddr) Split() []Multiaddr {
}
return
addrs
}
// Cast re-casts a byte slice as a multiaddr. will panic if it fails to parse.
func
Cast
(
b
[]
byte
)
Multiaddr
{
_
,
err
:=
bytesToString
(
b
)
if
err
!=
nil
{
panic
(
fmt
.
Errorf
(
"multiaddr failed to parse: %s"
,
err
))
}
return
&
multiaddr
{
bytes
:
b
}
}
// StringCast like Cast, but parses a string. Will also panic if it fails to parse.
func
StringCast
(
s
string
)
Multiaddr
{
m
,
err
:=
NewMultiaddr
(
s
)
if
err
!=
nil
{
panic
(
fmt
.
Errorf
(
"multiaddr failed to parse: %s"
,
err
))
}
return
m
}
net/convert.go
View file @
bb34b84e
...
...
@@ -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
)
}
net/net_test.go
View file @
bb34b84e
...
...
@@ -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)"
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment