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
1e156256
Commit
1e156256
authored
Nov 19, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added utp support to net/
parent
59f6cfc9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
13 deletions
+67
-13
net/convert.go
net/convert.go
+33
-0
net/convert_test.go
net/convert_test.go
+34
-13
No files found.
net/convert.go
View file @
1e156256
...
...
@@ -5,6 +5,7 @@ import (
"net"
"strings"
utp
"github.com/h2so5/utp"
ma
"github.com/jbenet/go-multiaddr"
)
...
...
@@ -55,6 +56,33 @@ func FromNetAddr(a net.Addr) (ma.Multiaddr, error) {
// Encapsulate
return
ipm
.
Encapsulate
(
udpm
),
nil
case
"utp"
,
"utp4"
,
"utp6"
:
acc
,
ok
:=
a
.
(
*
utp
.
UTPAddr
)
if
!
ok
{
return
nil
,
errIncorrectNetAddr
}
// Get UDP Addr
ac
,
ok
:=
acc
.
Addr
.
(
*
net
.
UDPAddr
)
if
!
ok
{
return
nil
,
errIncorrectNetAddr
}
// Get IP Addr
ipm
,
err
:=
FromIP
(
ac
.
IP
)
if
err
!=
nil
{
return
nil
,
errIncorrectNetAddr
}
// Get UDP Addr
utpm
,
err
:=
ma
.
NewMultiaddr
(
fmt
.
Sprintf
(
"/udp/%d/utp"
,
ac
.
Port
))
if
err
!=
nil
{
return
nil
,
errIncorrectNetAddr
}
// Encapsulate
return
ipm
.
Encapsulate
(
utpm
),
nil
case
"ip"
,
"ip4"
,
"ip6"
:
ac
,
ok
:=
a
.
(
*
net
.
IPAddr
)
if
!
ok
{
...
...
@@ -88,6 +116,8 @@ func ToNetAddr(maddr ma.Multiaddr) (net.Addr, error) {
return
net
.
ResolveTCPAddr
(
network
,
host
)
case
"udp"
:
return
net
.
ResolveUDPAddr
(
network
,
host
)
case
"utp"
:
return
utp
.
ResolveUTPAddr
(
network
,
host
)
case
"ip"
:
return
net
.
ResolveIPAddr
(
network
,
host
)
}
...
...
@@ -121,6 +151,9 @@ func DialArgs(m ma.Multiaddr) (string, string, error) {
}
network
:=
parts
[
2
]
if
parts
[
2
]
==
"udp"
&&
len
(
parts
)
>
4
&&
parts
[
4
]
==
"utp"
{
network
=
parts
[
4
]
}
var
host
string
switch
parts
[
0
]
{
case
"ip4"
:
...
...
net/convert_test.go
View file @
1e156256
...
...
@@ -4,6 +4,7 @@ import (
"net"
"testing"
utp
"github.com/h2so5/utp"
ma
"github.com/jbenet/go-multiaddr"
)
...
...
@@ -88,17 +89,31 @@ func TestFromUDP(t *testing.T) {
})
}
func
TestFromUTP
(
t
*
testing
.
T
)
{
testConvert
(
t
,
"/ip4/10.20.30.40/udp/1234/utp"
,
func
()
(
ma
.
Multiaddr
,
error
)
{
return
FromNetAddr
(
&
utp
.
UTPAddr
{
Addr
:
&
net
.
UDPAddr
{
IP
:
net
.
ParseIP
(
"10.20.30.40"
),
Port
:
1234
,
},
})
})
}
func
TestThinWaist
(
t
*
testing
.
T
)
{
addrs
:=
map
[
string
]
bool
{
"/ip4/127.0.0.1/udp/1234"
:
true
,
"/ip4/127.0.0.1/tcp/1234"
:
true
,
"/ip4/127.0.0.1/udp/1234/utp"
:
true
,
"/ip4/127.0.0.1/udp/1234/tcp/1234"
:
true
,
"/ip4/127.0.0.1/tcp/12345/ip4/1.2.3.4"
:
true
,
"/ip6/::1/tcp/80"
:
true
,
"/ip6/::1/udp/80"
:
true
,
"/ip6/::1"
:
true
,
"/ip6/::1/utp"
:
false
,
"/tcp/1234/ip4/1.2.3.4"
:
false
,
"/tcp/1234"
:
false
,
"/tcp/1234/utp"
:
false
,
"/tcp/1234/udp/1234"
:
false
,
"/ip4/1.2.3.4/ip4/2.3.4.5"
:
true
,
"/ip6/::1/ip4/2.3.4.5"
:
true
,
...
...
@@ -117,21 +132,27 @@ func TestThinWaist(t *testing.T) {
}
func
TestDialArgs
(
t
*
testing
.
T
)
{
m
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/127.0.0.1/udp/1234"
)
if
err
!=
nil
{
t
.
Fatal
(
"failed to construct"
,
"/ip4/127.0.0.1/udp/1234"
)
}
test
:=
func
(
e_maddr
,
e_nw
,
e_host
string
)
{
m
,
err
:=
ma
.
NewMultiaddr
(
e_maddr
)
if
err
!=
nil
{
t
.
Fatal
(
"failed to construct"
,
"/ip4/127.0.0.1/udp/1234"
,
e_maddr
)
}
nw
,
host
,
err
:=
DialArgs
(
m
)
if
err
!=
nil
{
t
.
Fatal
(
"failed to get dial args"
,
"/ip4/127.0.0.1/udp/1234"
,
err
)
}
nw
,
host
,
err
:=
DialArgs
(
m
)
if
err
!=
nil
{
t
.
Fatal
(
"failed to get dial args"
,
e_maddr
,
m
,
err
)
}
if
nw
!=
"udp"
{
t
.
Error
(
"failed to get udp network Dial Arg"
)
}
if
nw
!=
e_nw
{
t
.
Error
(
"failed to get udp network Dial Arg"
,
e_nw
,
nw
)
}
if
host
!=
"127.0.0.1:1234"
{
t
.
Error
(
"failed to get host:port Dial Arg"
)
if
host
!=
e_host
{
t
.
Error
(
"failed to get host:port Dial Arg"
,
e_host
,
host
)
}
}
test
(
"/ip4/127.0.0.1/udp/1234"
,
"udp"
,
"127.0.0.1:1234"
)
test
(
"/ip4/127.0.0.1/tcp/4321"
,
"tcp"
,
"127.0.0.1:4321"
)
test
(
"/ip4/127.0.0.1/udp/1234/utp"
,
"utp"
,
"127.0.0.1:1234"
)
}
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