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
5b8fc748
Commit
5b8fc748
authored
Nov 05, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
IP unspecified addrs
parent
f05346f3
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
95 additions
and
58 deletions
+95
-58
net/convert.go
net/convert.go
+0
-57
net/ip.go
net/ip.go
+76
-0
net/net_test.go
net/net_test.go
+19
-1
No files found.
net/convert.go
View file @
5b8fc748
package
manet
package
manet
import
(
import
(
"bytes"
"fmt"
"fmt"
"net"
"net"
"strings"
"strings"
...
@@ -9,17 +8,6 @@ import (
...
@@ -9,17 +8,6 @@ import (
ma
"github.com/jbenet/go-multiaddr"
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"
)
// 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"
)
// FromNetAddr converts a net.Addr type to a Multiaddr.
// FromNetAddr converts a net.Addr type to a Multiaddr.
...
@@ -142,48 +130,3 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
...
@@ -142,48 +130,3 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
}
}
return
network
,
host
,
nil
return
network
,
host
,
nil
}
}
// IsThinWaist returns whether a Multiaddr starts with "Thin Waist" Protocols.
// This means: /{IP4, IP6}[/{TCP, UDP}]
func
IsThinWaist
(
m
ma
.
Multiaddr
)
bool
{
p
:=
m
.
Protocols
()
// nothing? not even a waist.
if
len
(
p
)
==
0
{
return
false
}
if
p
[
0
]
.
Code
!=
ma
.
P_IP4
&&
p
[
0
]
.
Code
!=
ma
.
P_IP6
{
return
false
}
// only IP? still counts.
if
len
(
p
)
==
1
{
return
true
}
switch
p
[
1
]
.
Code
{
case
ma
.
P_TCP
,
ma
.
P_UDP
,
ma
.
P_IP4
,
ma
.
P_IP6
:
return
true
default
:
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
{
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
}
net/ip.go
0 → 100644
View file @
5b8fc748
package
manet
import
(
"bytes"
ma
"github.com/jbenet/go-multiaddr"
)
// Loopback Addresses
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"
)
// IP6LinkLocalLoopback is the ip6 link-local loopback multiaddr
IP6LinkLocalLoopback
=
ma
.
StringCast
(
"/ip6/fe80::1"
)
)
// Unspecified Addresses (used for )
var
(
IP4Unspecified
=
ma
.
StringCast
(
"/ip4/0.0.0.0"
)
IP6Unspecified
=
ma
.
StringCast
(
"/ip6/::"
)
)
// IsThinWaist returns whether a Multiaddr starts with "Thin Waist" Protocols.
// This means: /{IP4, IP6}[/{TCP, UDP}]
func
IsThinWaist
(
m
ma
.
Multiaddr
)
bool
{
p
:=
m
.
Protocols
()
// nothing? not even a waist.
if
len
(
p
)
==
0
{
return
false
}
if
p
[
0
]
.
Code
!=
ma
.
P_IP4
&&
p
[
0
]
.
Code
!=
ma
.
P_IP6
{
return
false
}
// only IP? still counts.
if
len
(
p
)
==
1
{
return
true
}
switch
p
[
1
]
.
Code
{
case
ma
.
P_TCP
,
ma
.
P_UDP
,
ma
.
P_IP4
,
ma
.
P_IP6
:
return
true
default
:
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
{
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
}
// IsIPUnspecified returns whether a Multiaddr is am Unspecified IP address
// This means either /ip4/0.0.0.0 or /ip6/::
func
IsIPUnspecified
(
m
ma
.
Multiaddr
)
bool
{
return
IP4Unspecified
.
Equal
(
m
)
||
IP6Unspecified
.
Equal
(
m
)
}
net/net_test.go
View file @
5b8fc748
...
@@ -199,7 +199,7 @@ func TestListenAndDial(t *testing.T) {
...
@@ -199,7 +199,7 @@ func TestListenAndDial(t *testing.T) {
wg
.
Wait
()
wg
.
Wait
()
}
}
func
TestLoopback
(
t
*
testing
.
T
)
{
func
Test
IP
Loopback
(
t
*
testing
.
T
)
{
if
IP4Loopback
.
String
()
!=
"/ip4/127.0.0.1"
{
if
IP4Loopback
.
String
()
!=
"/ip4/127.0.0.1"
{
t
.
Error
(
"IP4Loopback incorrect:"
,
IP4Loopback
)
t
.
Error
(
"IP4Loopback incorrect:"
,
IP4Loopback
)
}
}
...
@@ -224,3 +224,21 @@ func TestLoopback(t *testing.T) {
...
@@ -224,3 +224,21 @@ func TestLoopback(t *testing.T) {
t
.
Error
(
"IsIPLoopback failed (IP6LinkLocalLoopback)"
)
t
.
Error
(
"IsIPLoopback failed (IP6LinkLocalLoopback)"
)
}
}
}
}
func
TestIPUnspecified
(
t
*
testing
.
T
)
{
if
IP4Unspecified
.
String
()
!=
"/ip4/0.0.0.0"
{
t
.
Error
(
"IP4Unspecified incorrect:"
,
IP4Unspecified
)
}
if
IP6Unspecified
.
String
()
!=
"/ip6/::"
{
t
.
Error
(
"IP6Unspecified incorrect:"
,
IP6Unspecified
)
}
if
!
IsIPUnspecified
(
IP4Unspecified
)
{
t
.
Error
(
"IsIPUnspecified failed (IP4Unspecified)"
)
}
if
!
IsIPUnspecified
(
IP6Unspecified
)
{
t
.
Error
(
"IsIPUnspecified failed (IP6Unspecified)"
)
}
}
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