Skip to content
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

add get_by_mdns/2 #98

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/mdns_lite.ex
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,25 @@ defmodule MdnsLite do
end
end

@doc """
Lookup a PTR hostname using mDNS

The hostname should be a .local name since the query only goes out via mDNS.
On success, a domain is returned.
"""
@spec get_by_mdns(String.t(), non_neg_integer()) :: any()
def get_by_mdns(hostname, timeout \\ @default_timeout) do
q = MdnsLite.DNS.dns_query(class: :in, type: :ptr, domain: to_charlist(hostname))

case query(q, timeout) do
%{answer: [first | _]} ->
{:ok, dns_rr(first, :domain)}

%{answer: []} ->
{:error, :nxdomain}
end
end

defp to_addr(addr) when is_tuple(addr), do: addr
defp to_addr(<<a, b, c, d>>), do: {a, b, c, d}

Expand Down