Commit f4bd862c authored by Jeromy's avatar Jeromy

fix ip4 vs ip6 dial failures

parent 169ff665
......@@ -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 {
......
......@@ -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")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment