Skip to content

Commit

Permalink
replace {:ip, addr} with {:bind_to_device, ifname} in connectivity ch…
Browse files Browse the repository at this point in the history
…ecks

Signed-off-by: Connor Rigby <[email protected]>
  • Loading branch information
ConnorRigby authored and fhunleth committed Jun 17, 2024
1 parent 80241bf commit 894d113
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
5 changes: 2 additions & 3 deletions lib/vintage_net/connectivity/tcp_ping.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ defmodule VintageNet.Connectivity.TCPPing do
# interface. I.e., errors on other interfaces mess up DNS even if the
# one of interest is ok.
with {:ok, dest_ip} <- VintageNet.IP.ip_to_tuple(host),
{:ok, tcp} <-
:gen_tcp.connect(dest_ip, port, bind_to_device_option(ifname), @ping_timeout) do
{:ok, tcp} <- :gen_tcp.connect(dest_ip, port, bind_to_device(ifname), @ping_timeout) do
_ = :gen_tcp.close(tcp)
{:ok, {evaluate_result(dest_ip), []}}
else
Expand All @@ -94,7 +93,7 @@ defmodule VintageNet.Connectivity.TCPPing do
end
end

defp bind_to_device_option(ifname) do
defp bind_to_device(ifname) do
case :os.type() do
{:unix, :linux} -> [bind_to_device: ifname]
_ -> []
Expand Down
10 changes: 4 additions & 6 deletions lib/vintage_net/connectivity/when_where.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ defmodule VintageNet.Connectivity.WhenWhere do
"""

@behaviour VintageNet.Connectivity.Check
import VintageNet.Connectivity.TCPPing, only: [get_interface_address: 2]
import VintageNet.Connectivity.WebRequest, only: [validate_url: 1]

alias VintageNet.Connectivity.HTTPClient
Expand Down Expand Up @@ -45,8 +44,7 @@ defmodule VintageNet.Connectivity.WhenWhere do
def check(ifname, {__MODULE__, options}) do
nonce = Base.encode16(:rand.bytes(4))

with {:ok, src_ip} <- get_interface_address(ifname, :inet),
{:ok, headers, reply} <- make_request(src_ip, nonce, options),
with {:ok, headers, reply} <- make_request(ifname, nonce, options),
:ok <- validate_nonce(headers, nonce),
properties <- build_props(Enum.to_list(reply), []) do
{:ok, {:internet, properties}}
Expand All @@ -62,12 +60,12 @@ defmodule VintageNet.Connectivity.WhenWhere do
end
end

@spec make_request(:inet.ip_address(), String.t(), Keyword.t()) ::
@spec make_request(VintageNet.ifname(), String.t(), Keyword.t()) ::
{:ok, [{String.t(), String.t()}], map()} | {:error, term()}
defp make_request(src_ip, nonce, options) do
defp make_request(ifname, nonce, options) do
url = %{options[:url] | query: "nonce=#{nonce}"}
request_headers = [{"Content-Type", "application/x-erlang-binary"}]
request = HTTPClient.create_request(url, src_ip, request_headers)
request = HTTPClient.create_request(url, ifname, request_headers)

case HTTPClient.make_request(request, options[:max_response_size], options[:timeout_millis]) do
{:ok, {{_version, 200, _status_message}, headers, body}} ->
Expand Down

0 comments on commit 894d113

Please sign in to comment.