Skip to content

Commit 0c4d189

Browse files
ericmjdch
andauthored
Connect to IP literals using their own address family (#552)
* transport: Ensure that numeric IP literals use correct family When the address was an IP literal, gen_tcp got the wrong family and treated it as a hostname. Mint then returned :nxdomain from the IPv4 fallback instead of the real error, after an unneeded DNS lookup and timeouts. - connect to IPv6 literals over inet6 and IPv4 literals over inet, ignoring :inet6 and :inet4 - keep the inet6-then-inet4 fallback for hostnames - apply the same change to TCP and SSL * Merge address family clauses and drop changelog entry Connect IP literals through one clause for both families, drop the redundant is_tuple/1 guards, and remove the Unreleased changelog section since the changelog is written at release time. --------- Co-authored-by: Dave Cottlehuber <dch@skunkwerks.at>
1 parent bd0b65b commit 0c4d189

6 files changed

Lines changed: 150 additions & 32 deletions

File tree

‎lib/mint/core/transport/ssl.ex‎

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -329,22 +329,29 @@ defmodule Mint.Core.Transport.SSL do
329329

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

332-
if inet6? do
333-
# Try inet6 first, then fall back to the defaults provided by
334-
# ssl/gen_tcp if connection fails.
335-
case :ssl.connect(address, port, [:inet6 | opts], timeout) do
336-
{:ok, sslsocket} ->
337-
{:ok, sslsocket}
338-
339-
_error when inet4? ->
340-
wrap_err(:ssl.connect(address, port, opts, timeout))
341-
342-
error ->
343-
wrap_err(error)
344-
end
345-
else
346-
# Use the defaults provided by ssl/gen_tcp.
347-
wrap_err(:ssl.connect(address, port, opts, timeout))
332+
case Mint.Core.Util.address_family(address) do
333+
# IP literals can only be reached over their own family, so
334+
# connect once with it and ignore :inet6/:inet4.
335+
family when family in [:inet, :inet6] ->
336+
wrap_err(:ssl.connect(address, port, [family | opts], timeout))
337+
338+
:hostname when inet6? ->
339+
# Try inet6 first, then fall back to the defaults provided by
340+
# ssl/gen_tcp if connection fails.
341+
case :ssl.connect(address, port, [:inet6 | opts], timeout) do
342+
{:ok, sslsocket} ->
343+
{:ok, sslsocket}
344+
345+
_error when inet4? ->
346+
wrap_err(:ssl.connect(address, port, opts, timeout))
347+
348+
error ->
349+
wrap_err(error)
350+
end
351+
352+
:hostname ->
353+
# Use the defaults provided by ssl/gen_tcp.
354+
wrap_err(:ssl.connect(address, port, opts, timeout))
348355
end
349356
end
350357

‎lib/mint/core/transport/tcp.ex‎

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,29 @@ defmodule Mint.Core.Transport.TCP do
2727
|> Keyword.merge(@transport_opts)
2828
|> Keyword.drop([:alpn_advertised_protocols, :timeout, :inet4, :inet6])
2929

30-
if inet6? do
31-
# Try inet6 first, then fall back to the defaults provided by
32-
# gen_tcp if connection fails.
33-
case :gen_tcp.connect(address, port, [:inet6 | opts], timeout) do
34-
{:ok, socket} ->
35-
{:ok, socket}
36-
37-
_error when inet4? ->
38-
wrap_err(:gen_tcp.connect(address, port, opts, timeout))
39-
40-
error ->
41-
wrap_err(error)
42-
end
43-
else
44-
# Use the defaults provided by gen_tcp.
45-
wrap_err(:gen_tcp.connect(address, port, opts, timeout))
30+
case Mint.Core.Util.address_family(address) do
31+
# IP literals can only be reached over their own family, so
32+
# connect once with it and ignore :inet6/:inet4.
33+
family when family in [:inet, :inet6] ->
34+
wrap_err(:gen_tcp.connect(address, port, [family | opts], timeout))
35+
36+
:hostname when inet6? ->
37+
# Try inet6 first, then fall back to the defaults provided by
38+
# gen_tcp if connection fails.
39+
case :gen_tcp.connect(address, port, [:inet6 | opts], timeout) do
40+
{:ok, socket} ->
41+
{:ok, socket}
42+
43+
_error when inet4? ->
44+
wrap_err(:gen_tcp.connect(address, port, opts, timeout))
45+
46+
error ->
47+
wrap_err(error)
48+
end
49+
50+
:hostname ->
51+
# Use the defaults provided by gen_tcp.
52+
wrap_err(:gen_tcp.connect(address, port, opts, timeout))
4653
end
4754
end
4855

‎lib/mint/core/util.ex‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ defmodule Mint.Core.Util do
33

44
alias Mint.Types
55

6+
# Returns the address family of an IP literal, or :hostname when the
7+
# address needs a lookup. Connecting to a literal with the wrong family
8+
# makes :gen_tcp resolve it as a name, which fails with :nxdomain.
9+
@spec address_family(Types.address() | charlist()) :: :inet | :inet6 | :hostname
10+
def address_family(address) when tuple_size(address) == 4, do: :inet
11+
def address_family(address) when tuple_size(address) == 8, do: :inet6
12+
13+
def address_family(address) when is_list(address) do
14+
case :inet.parse_strict_address(address) do
15+
{:ok, ip} when tuple_size(ip) == 4 -> :inet
16+
{:ok, ip} when tuple_size(ip) == 8 -> :inet6
17+
{:error, _reason} -> :hostname
18+
end
19+
end
20+
21+
def address_family(_address), do: :hostname
22+
623
@spec hostname(keyword(), Types.address()) :: String.t()
724
def hostname(opts, address) when is_list(opts) do
825
case Keyword.fetch(opts, :hostname) do

‎lib/mint/http.ex‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ defmodule Mint.HTTP do
366366
Defaults to `true` and may be overridden by the caller. *Available since
367367
v1.6.0*.
368368
369+
`:inet6` and `:inet4` only apply when `address` is a hostname. When it is
370+
an IPv4 or IPv6 literal, such as `"127.0.0.1"`, `"::1"`, or an IP tuple,
371+
Mint connects once using that address family.
372+
369373
Options for `:https` only:
370374
371375
* `:alpn_advertised_protocols` - managed by Mint. Cannot be overridden.

‎test/mint/core/transport/ssl_test.exs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,33 @@ defmodule Mint.Core.Transport.SSLTest do
275275
verify: :verify_none
276276
)
277277
end
278+
279+
test "returns the connection error for IP literals instead of a lookup error" do
280+
v6_port = closed_port(:inet6, {0, 0, 0, 0, 0, 0, 0, 1})
281+
v4_port = closed_port(:inet, {127, 0, 0, 1})
282+
283+
for {address, port, opts} <- [
284+
{"::1", v6_port, []},
285+
{"::1", v6_port, [inet6: true]},
286+
{"127.0.0.1", v4_port, [inet6: true, inet4: false]},
287+
{{127, 0, 0, 1}, v4_port, [inet6: true, inet4: false, hostname: "localhost"]}
288+
] do
289+
assert {:error, %Mint.TransportError{reason: :econnrefused}} =
290+
SSL.connect(
291+
address,
292+
port,
293+
[active: false, timeout: 1000, verify: :verify_none] ++ opts
294+
),
295+
"for #{inspect(address)} with #{inspect(opts)}"
296+
end
297+
end
298+
end
299+
300+
defp closed_port(family, ip) do
301+
{:ok, socket} = :gen_tcp.listen(0, [family, ip: ip])
302+
{:ok, port} = :inet.port(socket)
303+
:ok = :gen_tcp.close(socket)
304+
port
278305
end
279306

280307
describe "controlling_process/2" do

‎test/mint/core/transport/tcp_test.exs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,62 @@ defmodule Mint.Core.Transport.TCPTest do
8585
timeout: 1000
8686
)
8787
end
88+
89+
test "connects to IPv6 literals without the :inet6 option" do
90+
{:ok, listen_socket} =
91+
:gen_tcp.listen(0, [:inet6, ip: {0, 0, 0, 0, 0, 0, 0, 1}, active: false])
92+
93+
{:ok, port} = :inet.port(listen_socket)
94+
95+
task = Task.async(fn -> :gen_tcp.accept(listen_socket) end)
96+
97+
assert {:ok, _socket} = TCP.connect("::1", port, active: false, timeout: 1000)
98+
assert {:ok, _server_socket} = Task.await(task)
99+
end
100+
101+
test "connects to IPv4 literals when IPv4 is disabled" do
102+
{:ok, listen_socket} = :gen_tcp.listen(0, [:inet, ip: {127, 0, 0, 1}, active: false])
103+
{:ok, port} = :inet.port(listen_socket)
104+
105+
for address <- ["127.0.0.1", {127, 0, 0, 1}] do
106+
task = Task.async(fn -> :gen_tcp.accept(listen_socket) end)
107+
108+
assert {:ok, _socket} =
109+
TCP.connect(address, port,
110+
active: false,
111+
inet6: true,
112+
inet4: false,
113+
timeout: 1000
114+
)
115+
116+
assert {:ok, _server_socket} = Task.await(task)
117+
end
118+
end
119+
120+
test "returns the connection error for IP literals instead of a lookup error" do
121+
v6_port = closed_port(:inet6, {0, 0, 0, 0, 0, 0, 0, 1})
122+
v4_port = closed_port(:inet, {127, 0, 0, 1})
123+
124+
for {address, port, opts} <- [
125+
{"::1", v6_port, []},
126+
{"::1", v6_port, [inet6: true]},
127+
{"::1", v6_port, [inet6: true, inet4: false]},
128+
{"127.0.0.1", v4_port, [inet6: true]},
129+
{"127.0.0.1", v4_port, [inet6: true, inet4: false]},
130+
{{127, 0, 0, 1}, v4_port, [inet6: true, inet4: false]}
131+
] do
132+
assert {:error, %Mint.TransportError{reason: :econnrefused}} =
133+
TCP.connect(address, port, [active: false, timeout: 1000] ++ opts),
134+
"for #{inspect(address)} with #{inspect(opts)}"
135+
end
136+
end
137+
end
138+
139+
defp closed_port(family, ip) do
140+
{:ok, socket} = :gen_tcp.listen(0, [family, ip: ip])
141+
{:ok, port} = :inet.port(socket)
142+
:ok = :gen_tcp.close(socket)
143+
port
88144
end
89145

90146
describe "controlling_process/2" do

0 commit comments

Comments
 (0)