|
| 1 | +defmodule ExICE.MDNS.Resolver do |
| 2 | + @moduledoc false |
| 3 | + use GenServer, restart: :transient |
| 4 | + |
| 5 | + require Logger |
| 6 | + |
| 7 | + @mdns_port 5353 |
| 8 | + @multicast_addr {{224, 0, 0, 251}, @mdns_port} |
| 9 | + @response_timeout_ms 300 |
| 10 | + |
| 11 | + @spec start_link(module()) :: GenServer.on_start() |
| 12 | + def start_link(transport_module \\ :gen_udp) do |
| 13 | + GenServer.start_link(__MODULE__, transport_module, name: __MODULE__) |
| 14 | + end |
| 15 | + |
| 16 | + @spec gethostbyname(String.t()) :: {:ok, :inet.ip_address()} | {:error, term()} |
| 17 | + def gethostbyname(addr) do |
| 18 | + try do |
| 19 | + GenServer.call(__MODULE__, {:gethostbyname, addr}) |
| 20 | + catch |
| 21 | + :exit, {:timeout, _} -> |
| 22 | + {:error, :timeout} |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + @impl true |
| 27 | + def init(transport_module) do |
| 28 | + Logger.debug("Starting MDNS Resolver") |
| 29 | + {:ok, %{transport_module: transport_module}, {:continue, nil}} |
| 30 | + end |
| 31 | + |
| 32 | + @impl true |
| 33 | + def handle_continue(_, state) do |
| 34 | + ret = |
| 35 | + state.transport_module.open( |
| 36 | + # Listen on the port specific to mDNS traffic. |
| 37 | + # `add_membership` option only defines an address. |
| 38 | + @mdns_port, |
| 39 | + mode: :binary, |
| 40 | + reuseaddr: true, |
| 41 | + active: true, |
| 42 | + # Allow other apps to bind to @mdns_port. |
| 43 | + # If there are multiple sockets, bound to the same port, |
| 44 | + # and subscribed to the same group (in fact, if one socket |
| 45 | + # subscribes to some group, all other sockets bound to |
| 46 | + # the same port also join this gorup), all those sockets |
| 47 | + # will receive every message. In other words, `reuseport` for |
| 48 | + # multicast works differently than for casual sockets. |
| 49 | + reuseport: true, |
| 50 | + # Support running two ICE agents on a single machine. |
| 51 | + # In other case, our request won't be delivered to the mDNS address owner |
| 52 | + # running on the same machine (e.g., a web browser). |
| 53 | + multicast_loop: true, |
| 54 | + # Receive responses - they are sent to the multicast address. |
| 55 | + # The second argument specifies interfaces where we should listen |
| 56 | + # for multicast traffic. |
| 57 | + # This option works on interfaces i.e. it affects all sockets |
| 58 | + # bound to the same port. |
| 59 | + add_membership: {{224, 0, 0, 251}, {0, 0, 0, 0}} |
| 60 | + ) |
| 61 | + |
| 62 | + case ret do |
| 63 | + {:ok, socket} -> |
| 64 | + state = Map.merge(state, %{socket: socket, queries: %{}}) |
| 65 | + {:noreply, state} |
| 66 | + |
| 67 | + {:error, reason} -> |
| 68 | + Logger.warning(""" |
| 69 | + Couldn't start MDNS resolver, reason: #{reason}. MDNS candidates won't be resolved. |
| 70 | + """) |
| 71 | + |
| 72 | + {:stop, {:shutdown, reason}, state} |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + @impl true |
| 77 | + def handle_call({:gethostbyname, addr}, from, state) do |
| 78 | + Logger.debug("Trying to resolve addr: #{addr}") |
| 79 | + |
| 80 | + query = |
| 81 | + %ExICE.DNS.Message{ |
| 82 | + question: [ |
| 83 | + %{ |
| 84 | + qname: addr, |
| 85 | + qtype: :a, |
| 86 | + qclass: :in, |
| 87 | + unicast_response: false |
| 88 | + } |
| 89 | + ] |
| 90 | + } |
| 91 | + |> ExICE.DNS.Message.encode() |
| 92 | + |
| 93 | + case state.transport_module.send(state.socket, @multicast_addr, query) do |
| 94 | + :ok -> |
| 95 | + state = put_in(state, [:queries, addr], from) |
| 96 | + Process.send_after(self(), {:response_timeout, addr}, @response_timeout_ms) |
| 97 | + {:noreply, state} |
| 98 | + |
| 99 | + {:error, reason} -> |
| 100 | + {:reply, {:error, reason}, state} |
| 101 | + end |
| 102 | + end |
| 103 | + |
| 104 | + @impl true |
| 105 | + def handle_info({:udp, _socket, _ip, _port, packet}, state) do |
| 106 | + case ExICE.DNS.Message.decode(packet) do |
| 107 | + # only accept query response with one resource record |
| 108 | + {:ok, %{qr: true, aa: true, answer: [%{type: :a, class: :in, rdata: <<a, b, c, d>>} = rr]}} -> |
| 109 | + {from, state} = pop_in(state, [:queries, rr.name]) |
| 110 | + |
| 111 | + if from do |
| 112 | + addr = {a, b, c, d} |
| 113 | + GenServer.reply(from, {:ok, addr}) |
| 114 | + end |
| 115 | + |
| 116 | + {:noreply, state} |
| 117 | + |
| 118 | + _other -> |
| 119 | + {:noreply, state} |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + @impl true |
| 124 | + def handle_info({:response_timeout, addr}, state) do |
| 125 | + case pop_in(state, [:queries, addr]) do |
| 126 | + {nil, state} -> |
| 127 | + {:noreply, state} |
| 128 | + |
| 129 | + {from, state} -> |
| 130 | + GenServer.reply(from, {:error, :timeout}) |
| 131 | + {:noreply, state} |
| 132 | + end |
| 133 | + end |
| 134 | +end |
0 commit comments