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
p2p
go-tcp-transport
Commits
f4bd862c
Commit
f4bd862c
authored
Mar 21, 2017
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix ip4 vs ip6 dial failures
parent
169ff665
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
1 deletion
+54
-1
tcp.go
tcp.go
+22
-1
tcp_test.go
tcp_test.go
+32
-0
No files found.
tcp.go
View file @
f4bd862c
...
...
@@ -124,6 +124,7 @@ type tcpDialer struct {
rd
reuseport
.
Dialer
madialer
manet
.
Dialer
pattern
mafmt
.
Pattern
transport
tpt
.
Transport
}
...
...
@@ -135,6 +136,15 @@ func (t *TcpTransport) newTcpDialer(base manet.Dialer, laddr ma.Multiaddr, doReu
return
nil
,
err
// something wrong with laddr.
}
var
pattern
mafmt
.
Pattern
if
TCP4
.
Matches
(
laddr
)
{
pattern
=
TCP4
}
else
if
TCP6
.
Matches
(
laddr
)
{
pattern
=
TCP6
}
else
{
return
nil
,
fmt
.
Errorf
(
"local addr did not match TCP4 or TCP6: %s"
,
laddr
)
}
if
doReuse
&&
ReuseportIsAvailable
()
{
rd
:=
reuseport
.
Dialer
{
D
:
net
.
Dialer
{
...
...
@@ -149,6 +159,7 @@ func (t *TcpTransport) newTcpDialer(base manet.Dialer, laddr ma.Multiaddr, doReu
rd
:
rd
,
madialer
:
base
,
transport
:
t
,
pattern
:
pattern
,
},
nil
}
...
...
@@ -165,6 +176,13 @@ func (d *tcpDialer) Dial(raddr ma.Multiaddr) (tpt.Conn, error) {
}
func
(
d
*
tcpDialer
)
DialContext
(
ctx
context
.
Context
,
raddr
ma
.
Multiaddr
)
(
tpt
.
Conn
,
error
)
{
if
raddr
==
nil
{
zaddr
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/0.0.0.0/tcp/0"
)
if
err
!=
nil
{
return
nil
,
err
}
raddr
=
zaddr
}
var
c
manet
.
Conn
var
err
error
if
d
.
doReuse
{
...
...
@@ -214,8 +232,11 @@ func (d *tcpDialer) reuseDial(ctx context.Context, raddr ma.Multiaddr) (manet.Co
return
d
.
madialer
.
DialContext
(
ctx
,
raddr
)
}
var
TCP4
=
mafmt
.
And
(
mafmt
.
Base
(
ma
.
P_IP4
),
mafmt
.
Base
(
ma
.
P_TCP
))
var
TCP6
=
mafmt
.
And
(
mafmt
.
Base
(
ma
.
P_IP6
),
mafmt
.
Base
(
ma
.
P_TCP
))
func
(
d
*
tcpDialer
)
Matches
(
a
ma
.
Multiaddr
)
bool
{
return
mafmt
.
TCP
.
Matches
(
a
)
return
d
.
pattern
.
Matches
(
a
)
}
type
tcpListener
struct
{
...
...
tcp_test.go
View file @
f4bd862c
...
...
@@ -3,6 +3,7 @@ package tcp
import
(
"testing"
tpt
"github.com/libp2p/go-libp2p-transport"
utils
"github.com/libp2p/go-libp2p-transport/test"
ma
"github.com/multiformats/go-multiaddr"
)
...
...
@@ -27,3 +28,34 @@ func TestTcpTransportCantListenUtp(t *testing.T) {
t
.
Fatal
(
"shouldnt be able to listen on utp addr with tcp transport"
)
}
}
func
TestCorrectIPVersionMatching
(
t
*
testing
.
T
)
{
ta
:=
NewTCPTransport
()
addr4
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/0.0.0.0/tcp/0"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
addr6
,
err
:=
ma
.
NewMultiaddr
(
"/ip6/::1/tcp/0"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
d4
,
err
:=
ta
.
Dialer
(
addr4
,
tpt
.
ReuseportOpt
(
true
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
d6
,
err
:=
ta
.
Dialer
(
addr6
,
tpt
.
ReuseportOpt
(
true
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
d4
.
Matches
(
addr6
)
{
t
.
Fatal
(
"tcp4 dialer should not match ipv6 address"
)
}
if
d6
.
Matches
(
addr4
)
{
t
.
Fatal
(
"tcp4 dialer should not match ipv6 address"
)
}
}
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