Skip to content

Commit

Permalink
Fix crash on resolving mDNS candidate when mDNS resolver is not started
Browse files Browse the repository at this point in the history
  • Loading branch information
mickel8 committed Dec 4, 2024
1 parent da16b05 commit f9da7ca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/ex_ice/priv/ice_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,19 @@ defmodule ExICE.Priv.ICEAgent do
defp resolve_address(remote_cand) when is_binary(remote_cand.address) do
Logger.debug("Trying to resolve addr: #{remote_cand.address}")

case ExICE.Priv.MDNS.Resolver.gethostbyname(remote_cand.address) do
{:ok, addr} ->
Logger.debug("Successfully resolved #{remote_cand.address} to #{inspect(addr)}")
remote_cand = %ExICE.Candidate{remote_cand | address: addr}
{:ok, remote_cand}

with pid when is_pid(pid) <- Process.whereis(ExICE.Priv.MDNS.Resolver),
{:ok, addr} <- ExICE.Priv.MDNS.Resolver.gethostbyname(remote_cand.address) do
Logger.debug("Successfully resolved #{remote_cand.address} to #{inspect(addr)}")
remote_cand = %ExICE.Candidate{remote_cand | address: addr}
{:ok, remote_cand}
else
{:error, reason} = err ->
Logger.debug("Couldn't resolve #{remote_cand.address}, reason: #{reason}")
err

nil ->
Logger.debug("Couldn't resolve #{remote_cand.address}, reason: MDNS reslover not alive.")
{:error, :mdns_resolver_not_alive}
end
end

Expand Down
15 changes: 15 additions & 0 deletions test/priv/ice_agent_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ defmodule ExICE.Priv.ICEAgentTest do
cand = "1 1 UDP 1686052863 someincalidmdnsadddress 57940 typ srflx raddr 0.0.0.0 rport 0"
assert {:error, _reason} = ICEAgent.unmarshal_remote_candidate(cand)
end

test "with MDNS resolver not started" do
# this test checks what happens when MDNS resolver has not been started due to too old Erlang version

# stop MDNS resolver
pid = Process.whereis(ExICE.Priv.MDNS.Resolver)
assert :ok == GenServer.stop(pid)
assert nil == Process.whereis(ExICE.Priv.MDNS.Resolver)

# try to resolve some address
cand = "1 1 UDP 1686052863 example.local 57940 typ srflx raddr 0.0.0.0 rport 0"

assert {:error, {:resolve_address, :mdns_resolver_not_alive}} =
ICEAgent.unmarshal_remote_candidate(cand)
end
end

describe "add_remote_candidate/2" do
Expand Down

0 comments on commit f9da7ca

Please sign in to comment.