Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions lib/mint/core/transport/ssl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -329,22 +329,29 @@ defmodule Mint.Core.Transport.SSL do

opts = ssl_opts(String.to_charlist(hostname), opts)

if inet6? do
# Try inet6 first, then fall back to the defaults provided by
# ssl/gen_tcp if connection fails.
case :ssl.connect(address, port, [:inet6 | opts], timeout) do
{:ok, sslsocket} ->
{:ok, sslsocket}

_error when inet4? ->
wrap_err(:ssl.connect(address, port, opts, timeout))

error ->
wrap_err(error)
end
else
# Use the defaults provided by ssl/gen_tcp.
wrap_err(:ssl.connect(address, port, opts, timeout))
case Mint.Core.Util.address_family(address) do
# IP literals can only be reached over their own family, so
# connect once with it and ignore :inet6/:inet4.
family when family in [:inet, :inet6] ->
wrap_err(:ssl.connect(address, port, [family | opts], timeout))

:hostname when inet6? ->
# Try inet6 first, then fall back to the defaults provided by
# ssl/gen_tcp if connection fails.
case :ssl.connect(address, port, [:inet6 | opts], timeout) do
{:ok, sslsocket} ->
{:ok, sslsocket}

_error when inet4? ->
wrap_err(:ssl.connect(address, port, opts, timeout))

error ->
wrap_err(error)
end

:hostname ->
# Use the defaults provided by ssl/gen_tcp.
wrap_err(:ssl.connect(address, port, opts, timeout))
end
end

Expand Down
39 changes: 23 additions & 16 deletions lib/mint/core/transport/tcp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,29 @@ defmodule Mint.Core.Transport.TCP do
|> Keyword.merge(@transport_opts)
|> Keyword.drop([:alpn_advertised_protocols, :timeout, :inet4, :inet6])

if inet6? do
# Try inet6 first, then fall back to the defaults provided by
# gen_tcp if connection fails.
case :gen_tcp.connect(address, port, [:inet6 | opts], timeout) do
{:ok, socket} ->
{:ok, socket}

_error when inet4? ->
wrap_err(:gen_tcp.connect(address, port, opts, timeout))

error ->
wrap_err(error)
end
else
# Use the defaults provided by gen_tcp.
wrap_err(:gen_tcp.connect(address, port, opts, timeout))
case Mint.Core.Util.address_family(address) do
# IP literals can only be reached over their own family, so
# connect once with it and ignore :inet6/:inet4.
family when family in [:inet, :inet6] ->
wrap_err(:gen_tcp.connect(address, port, [family | opts], timeout))

:hostname when inet6? ->
# Try inet6 first, then fall back to the defaults provided by
# gen_tcp if connection fails.
case :gen_tcp.connect(address, port, [:inet6 | opts], timeout) do
{:ok, socket} ->
{:ok, socket}

_error when inet4? ->
wrap_err(:gen_tcp.connect(address, port, opts, timeout))

error ->
wrap_err(error)
end

:hostname ->
# Use the defaults provided by gen_tcp.
wrap_err(:gen_tcp.connect(address, port, opts, timeout))
end
end

Expand Down
17 changes: 17 additions & 0 deletions lib/mint/core/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ defmodule Mint.Core.Util do

alias Mint.Types

# Returns the address family of an IP literal, or :hostname when the
# address needs a lookup. Connecting to a literal with the wrong family
# makes :gen_tcp resolve it as a name, which fails with :nxdomain.
@spec address_family(Types.address() | charlist()) :: :inet | :inet6 | :hostname
def address_family(address) when tuple_size(address) == 4, do: :inet
def address_family(address) when tuple_size(address) == 8, do: :inet6

def address_family(address) when is_list(address) do
case :inet.parse_strict_address(address) do
{:ok, ip} when tuple_size(ip) == 4 -> :inet
{:ok, ip} when tuple_size(ip) == 8 -> :inet6
{:error, _reason} -> :hostname
end
end

def address_family(_address), do: :hostname

@spec hostname(keyword(), Types.address()) :: String.t()
def hostname(opts, address) when is_list(opts) do
case Keyword.fetch(opts, :hostname) do
Expand Down
4 changes: 4 additions & 0 deletions lib/mint/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ defmodule Mint.HTTP do
Defaults to `true` and may be overridden by the caller. *Available since
v1.6.0*.

`:inet6` and `:inet4` only apply when `address` is a hostname. When it is
an IPv4 or IPv6 literal, such as `"127.0.0.1"`, `"::1"`, or an IP tuple,
Mint connects once using that address family.

Options for `:https` only:

* `:alpn_advertised_protocols` - managed by Mint. Cannot be overridden.
Expand Down
27 changes: 27 additions & 0 deletions test/mint/core/transport/ssl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,33 @@ defmodule Mint.Core.Transport.SSLTest do
verify: :verify_none
)
end

test "returns the connection error for IP literals instead of a lookup error" do
v6_port = closed_port(:inet6, {0, 0, 0, 0, 0, 0, 0, 1})
v4_port = closed_port(:inet, {127, 0, 0, 1})

for {address, port, opts} <- [
{"::1", v6_port, []},
{"::1", v6_port, [inet6: true]},
{"127.0.0.1", v4_port, [inet6: true, inet4: false]},
{{127, 0, 0, 1}, v4_port, [inet6: true, inet4: false, hostname: "localhost"]}
] do
assert {:error, %Mint.TransportError{reason: :econnrefused}} =
SSL.connect(
address,
port,
[active: false, timeout: 1000, verify: :verify_none] ++ opts
),
"for #{inspect(address)} with #{inspect(opts)}"
end
end
end

defp closed_port(family, ip) do
{:ok, socket} = :gen_tcp.listen(0, [family, ip: ip])
{:ok, port} = :inet.port(socket)
:ok = :gen_tcp.close(socket)
port
end

describe "controlling_process/2" do
Expand Down
56 changes: 56 additions & 0 deletions test/mint/core/transport/tcp_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,62 @@ defmodule Mint.Core.Transport.TCPTest do
timeout: 1000
)
end

test "connects to IPv6 literals without the :inet6 option" do
{:ok, listen_socket} =
:gen_tcp.listen(0, [:inet6, ip: {0, 0, 0, 0, 0, 0, 0, 1}, active: false])

{:ok, port} = :inet.port(listen_socket)

task = Task.async(fn -> :gen_tcp.accept(listen_socket) end)

assert {:ok, _socket} = TCP.connect("::1", port, active: false, timeout: 1000)
assert {:ok, _server_socket} = Task.await(task)
end

test "connects to IPv4 literals when IPv4 is disabled" do
{:ok, listen_socket} = :gen_tcp.listen(0, [:inet, ip: {127, 0, 0, 1}, active: false])
{:ok, port} = :inet.port(listen_socket)

for address <- ["127.0.0.1", {127, 0, 0, 1}] do
task = Task.async(fn -> :gen_tcp.accept(listen_socket) end)

assert {:ok, _socket} =
TCP.connect(address, port,
active: false,
inet6: true,
inet4: false,
timeout: 1000
)

assert {:ok, _server_socket} = Task.await(task)
end
end

test "returns the connection error for IP literals instead of a lookup error" do
v6_port = closed_port(:inet6, {0, 0, 0, 0, 0, 0, 0, 1})
v4_port = closed_port(:inet, {127, 0, 0, 1})

for {address, port, opts} <- [
{"::1", v6_port, []},
{"::1", v6_port, [inet6: true]},
{"::1", v6_port, [inet6: true, inet4: false]},
{"127.0.0.1", v4_port, [inet6: true]},
{"127.0.0.1", v4_port, [inet6: true, inet4: false]},
{{127, 0, 0, 1}, v4_port, [inet6: true, inet4: false]}
] do
assert {:error, %Mint.TransportError{reason: :econnrefused}} =
TCP.connect(address, port, [active: false, timeout: 1000] ++ opts),
"for #{inspect(address)} with #{inspect(opts)}"
end
end
end

defp closed_port(family, ip) do
{:ok, socket} = :gen_tcp.listen(0, [family, ip: ip])
{:ok, port} = :inet.port(socket)
:ok = :gen_tcp.close(socket)
port
end

describe "controlling_process/2" do
Expand Down
Loading