-
Notifications
You must be signed in to change notification settings - Fork 6
Add MDNS resolver #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| defmodule ExICE.App do | ||
| @moduledoc false | ||
| use Application | ||
|
|
||
| @impl true | ||
| def start(_type, _args) do | ||
| children = [{ExICE.MDNS.Resolver, :gen_udp}] | ||
| Supervisor.start_link(children, strategy: :one_for_one) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| defmodule ExICE.MDNS.Resolver do | ||
| @moduledoc false | ||
| # This is based on https://datatracker.ietf.org/doc/html/draft-ietf-mmusic-mdns-ice-candidates#section-3.2.1 | ||
|
|
||
| use GenServer, restart: :transient | ||
|
|
||
| require Logger | ||
|
|
||
| @mdns_port 5353 | ||
| @multicast_addr {{224, 0, 0, 251}, @mdns_port} | ||
| @response_timeout_ms 300 | ||
|
|
||
| @spec start_link(module()) :: GenServer.on_start() | ||
| def start_link(transport_module \\ :gen_udp) do | ||
| GenServer.start_link(__MODULE__, transport_module, name: __MODULE__) | ||
| end | ||
|
|
||
| @spec gethostbyname(String.t()) :: {:ok, :inet.ip_address()} | {:error, term()} | ||
| def gethostbyname(addr) do | ||
| try do | ||
| GenServer.call(__MODULE__, {:gethostbyname, addr}) | ||
| catch | ||
| :exit, {:timeout, _} -> | ||
| {:error, :timeout} | ||
| end | ||
| end | ||
|
|
||
| @impl true | ||
| def init(transport_module) do | ||
| Logger.debug("Starting MDNS Resolver") | ||
| {:ok, %{transport_module: transport_module}, {:continue, nil}} | ||
| end | ||
|
|
||
| @impl true | ||
| def handle_continue(_, state) do | ||
| ret = | ||
| state.transport_module.open( | ||
| # Listen on the port specific to mDNS traffic. | ||
| # `add_membership` option only defines an address. | ||
| @mdns_port, | ||
| mode: :binary, | ||
| reuseaddr: true, | ||
| active: true, | ||
| # Allow other apps to bind to @mdns_port. | ||
| # If there are multiple sockets, bound to the same port, | ||
| # and subscribed to the same group (in fact, if one socket | ||
| # subscribes to some group, all other sockets bound to | ||
| # the same port also join this gorup), all those sockets | ||
| # will receive every message. In other words, `reuseport` for | ||
| # multicast works differently than for casual sockets. | ||
| reuseport: true, | ||
| # Support running two ICE agents on a single machine. | ||
| # In other case, our request won't be delivered to the mDNS address owner | ||
| # running on the same machine (e.g., a web browser). | ||
| multicast_loop: true, | ||
| # Receive responses - they are sent to the multicast address. | ||
| # The second argument specifies interfaces where we should listen | ||
| # for multicast traffic. | ||
| # This option works on interfaces i.e. it affects all sockets | ||
| # bound to the same port. | ||
| add_membership: {{224, 0, 0, 251}, {0, 0, 0, 0}} | ||
| ) | ||
|
|
||
| case ret do | ||
| {:ok, socket} -> | ||
| state = Map.merge(state, %{socket: socket, queries: %{}}) | ||
| {:noreply, state} | ||
|
|
||
| {:error, reason} -> | ||
| Logger.warning(""" | ||
| Couldn't start MDNS resolver, reason: #{reason}. MDNS candidates won't be resolved. | ||
| """) | ||
|
|
||
| {:stop, {:shutdown, reason}, state} | ||
| end | ||
| end | ||
|
|
||
| @impl true | ||
| def handle_call({:gethostbyname, addr}, from, state) do | ||
| query = | ||
| %ExICE.DNS.Message{ | ||
| question: [ | ||
| %{ | ||
| qname: addr, | ||
| qtype: :a, | ||
| qclass: :in, | ||
| unicast_response: true | ||
| } | ||
| ] | ||
| } | ||
| |> ExICE.DNS.Message.encode() | ||
|
|
||
| case state.transport_module.send(state.socket, @multicast_addr, query) do | ||
| :ok -> | ||
| state = put_in(state, [:queries, addr], from) | ||
| Process.send_after(self(), {:response_timeout, addr}, @response_timeout_ms) | ||
| {:noreply, state} | ||
|
|
||
| {:error, reason} -> | ||
| {:reply, {:error, reason}, state} | ||
| end | ||
| end | ||
|
|
||
| @impl true | ||
| def handle_info({:udp, _socket, _ip, _port, packet}, state) do | ||
| case ExICE.DNS.Message.decode(packet) do | ||
| # Only accept query response with one resource record. | ||
| # See https://datatracker.ietf.org/doc/html/draft-ietf-mmusic-mdns-ice-candidates#section-3.2.2 | ||
| {:ok, %{qr: true, aa: true, answer: [%{type: :a, class: :in, rdata: <<a, b, c, d>>} = rr]}} -> | ||
| {from, state} = pop_in(state, [:queries, rr.name]) | ||
|
|
||
| if from do | ||
| addr = {a, b, c, d} | ||
| GenServer.reply(from, {:ok, addr}) | ||
| end | ||
|
|
||
| {:noreply, state} | ||
|
|
||
| _other -> | ||
| {:noreply, state} | ||
| end | ||
| end | ||
|
|
||
| @impl true | ||
| def handle_info({:response_timeout, addr}, state) do | ||
| case pop_in(state, [:queries, addr]) do | ||
| {nil, state} -> | ||
| {:noreply, state} | ||
|
|
||
| {from, state} -> | ||
| GenServer.reply(from, {:error, :timeout}) | ||
| {:noreply, state} | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering why this isn't just a private function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I put it here as it is very specific to the place where it is used