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
04044c22
Commit
04044c22
authored
Jan 12, 2015
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
AddrMatch
parent
97da87cf
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
99 additions
and
0 deletions
+99
-0
net.go
net.go
+27
-0
net_test.go
net_test.go
+72
-0
No files found.
net.go
View file @
04044c22
...
@@ -266,3 +266,30 @@ func InterfaceMultiaddrs() ([]ma.Multiaddr, error) {
...
@@ -266,3 +266,30 @@ func InterfaceMultiaddrs() ([]ma.Multiaddr, error) {
}
}
return
maddrs
,
nil
return
maddrs
,
nil
}
}
// AddrMatch returns the Multiaddrs that match the protocol stack on addr
func
AddrMatch
(
match
ma
.
Multiaddr
,
addrs
[]
ma
.
Multiaddr
)
[]
ma
.
Multiaddr
{
// we should match transports entirely.
p1s
:=
match
.
Protocols
()
out
:=
make
([]
ma
.
Multiaddr
,
0
,
len
(
addrs
))
for
_
,
a
:=
range
addrs
{
p2s
:=
a
.
Protocols
()
if
len
(
p1s
)
!=
len
(
p2s
)
{
continue
}
match
:=
true
for
i
,
p2
:=
range
p2s
{
if
p1s
[
i
]
.
Code
!=
p2
.
Code
{
match
=
false
break
}
}
if
match
{
out
=
append
(
out
,
a
)
}
}
return
out
}
net_test.go
View file @
04044c22
...
@@ -364,3 +364,75 @@ func TestIP6LinkLocal(t *testing.T) {
...
@@ -364,3 +364,75 @@ func TestIP6LinkLocal(t *testing.T) {
}
}
}
}
}
}
func
TestAddrMatch
(
t
*
testing
.
T
)
{
test
:=
func
(
m
ma
.
Multiaddr
,
input
,
expect
[]
ma
.
Multiaddr
)
{
actual
:=
AddrMatch
(
m
,
input
)
testSliceEqual
(
t
,
expect
,
actual
)
}
a
:=
[]
ma
.
Multiaddr
{
newMultiaddr
(
t
,
"/ip4/1.2.3.4/tcp/1234"
),
newMultiaddr
(
t
,
"/ip4/1.2.3.4/tcp/2345"
),
newMultiaddr
(
t
,
"/ip4/1.2.3.4/tcp/1234/tcp/2345"
),
newMultiaddr
(
t
,
"/ip4/1.2.3.4/tcp/1234/tcp/2345"
),
newMultiaddr
(
t
,
"/ip4/1.2.3.4/tcp/1234/udp/1234"
),
newMultiaddr
(
t
,
"/ip4/1.2.3.4/tcp/1234/udp/1234"
),
newMultiaddr
(
t
,
"/ip4/1.2.3.4/tcp/1234/ip6/::1"
),
newMultiaddr
(
t
,
"/ip4/1.2.3.4/tcp/1234/ip6/::1"
),
newMultiaddr
(
t
,
"/ip6/::1/tcp/1234"
),
newMultiaddr
(
t
,
"/ip6/::1/tcp/2345"
),
newMultiaddr
(
t
,
"/ip6/::1/tcp/1234/tcp/2345"
),
newMultiaddr
(
t
,
"/ip6/::1/tcp/1234/tcp/2345"
),
newMultiaddr
(
t
,
"/ip6/::1/tcp/1234/udp/1234"
),
newMultiaddr
(
t
,
"/ip6/::1/tcp/1234/udp/1234"
),
newMultiaddr
(
t
,
"/ip6/::1/tcp/1234/ip6/::1"
),
newMultiaddr
(
t
,
"/ip6/::1/tcp/1234/ip6/::1"
),
}
test
(
a
[
0
],
a
,
[]
ma
.
Multiaddr
{
newMultiaddr
(
t
,
"/ip4/1.2.3.4/tcp/1234"
),
newMultiaddr
(
t
,
"/ip4/1.2.3.4/tcp/2345"
),
})
test
(
a
[
2
],
a
,
[]
ma
.
Multiaddr
{
newMultiaddr
(
t
,
"/ip4/1.2.3.4/tcp/1234/tcp/2345"
),
newMultiaddr
(
t
,
"/ip4/1.2.3.4/tcp/1234/tcp/2345"
),
})
test
(
a
[
4
],
a
,
[]
ma
.
Multiaddr
{
newMultiaddr
(
t
,
"/ip4/1.2.3.4/tcp/1234/udp/1234"
),
newMultiaddr
(
t
,
"/ip4/1.2.3.4/tcp/1234/udp/1234"
),
})
test
(
a
[
6
],
a
,
[]
ma
.
Multiaddr
{
newMultiaddr
(
t
,
"/ip4/1.2.3.4/tcp/1234/ip6/::1"
),
newMultiaddr
(
t
,
"/ip4/1.2.3.4/tcp/1234/ip6/::1"
),
})
test
(
a
[
8
],
a
,
[]
ma
.
Multiaddr
{
newMultiaddr
(
t
,
"/ip6/::1/tcp/1234"
),
newMultiaddr
(
t
,
"/ip6/::1/tcp/2345"
),
})
test
(
a
[
10
],
a
,
[]
ma
.
Multiaddr
{
newMultiaddr
(
t
,
"/ip6/::1/tcp/1234/tcp/2345"
),
newMultiaddr
(
t
,
"/ip6/::1/tcp/1234/tcp/2345"
),
})
test
(
a
[
12
],
a
,
[]
ma
.
Multiaddr
{
newMultiaddr
(
t
,
"/ip6/::1/tcp/1234/udp/1234"
),
newMultiaddr
(
t
,
"/ip6/::1/tcp/1234/udp/1234"
),
})
test
(
a
[
14
],
a
,
[]
ma
.
Multiaddr
{
newMultiaddr
(
t
,
"/ip6/::1/tcp/1234/ip6/::1"
),
newMultiaddr
(
t
,
"/ip6/::1/tcp/1234/ip6/::1"
),
})
}
func
testSliceEqual
(
t
*
testing
.
T
,
a
,
b
[]
ma
.
Multiaddr
)
{
if
len
(
a
)
!=
len
(
b
)
{
t
.
Error
(
"differ"
,
a
,
b
)
}
for
i
,
addrA
:=
range
a
{
if
!
addrA
.
Equal
(
b
[
i
])
{
t
.
Error
(
"differ"
,
a
,
b
)
}
}
}
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