From 46964f62dba635441cb61afb9c1ee1fb4b5f5f2e Mon Sep 17 00:00:00 2001 From: Erik Grinaker Date: Mon, 4 Jan 2021 16:05:43 +0100 Subject: [PATCH] p2p: fix IPv6 address handling in new transport API (#5853) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The old code naïvely concatenated IP and port, which doesn't work for IPv6 addresses where `:` can be part of the IP as well. --- p2p/transport.go | 2 +- p2p/transport_mconn.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/p2p/transport.go b/p2p/transport.go index 5a27a13a4..8d49b9538 100644 --- a/p2p/transport.go +++ b/p2p/transport.go @@ -69,7 +69,7 @@ func (e Endpoint) String() string { if len(e.IP) > 0 { u.Host = e.IP.String() if e.Port > 0 { - u.Host += fmt.Sprintf(":%v", e.Port) + u.Host = net.JoinHostPort(u.Host, fmt.Sprintf("%v", e.Port)) } } else if e.Path != "" { u.Opaque = e.Path diff --git a/p2p/transport_mconn.go b/p2p/transport_mconn.go index d44796779..d59a58cea 100644 --- a/p2p/transport_mconn.go +++ b/p2p/transport_mconn.go @@ -253,7 +253,8 @@ func (m *MConnTransport) Dial(ctx context.Context, endpoint Endpoint) (Connectio defer cancel() dialer := net.Dialer{} - tcpConn, err := dialer.DialContext(ctx, "tcp", fmt.Sprintf("%v:%v", endpoint.IP, endpoint.Port)) + tcpConn, err := dialer.DialContext(ctx, "tcp", + net.JoinHostPort(endpoint.IP.String(), fmt.Sprintf("%v", endpoint.Port))) if err != nil { return nil, err }