Current section
Files
Jump to
Current section
Files
src/barnacle@internal@dns.erl
-module(barnacle@internal@dns).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([discover_nodes/2]).
-export_type([ip_address/0]).
-type ip_address() :: {ip_v4, integer(), integer(), integer(), integer()} |
{ip_v6,
integer(),
integer(),
integer(),
integer(),
integer(),
integer(),
integer(),
integer()}.
-spec lookup_ip(binary()) -> list(ip_address()).
lookup_ip(Hostname) ->
A_records = begin
_pipe = barnacle_ffi:lookup_a(Hostname),
gleam@list:map(
_pipe,
fun(Ip) ->
{A, B, C, D} = Ip,
{ip_v4, A, B, C, D}
end
)
end,
Aaaa_records = begin
_pipe@1 = barnacle_ffi:lookup_aaaa(Hostname),
gleam@list:map(
_pipe@1,
fun(Ip@1) ->
{A@1, B@1, C@1, D@1, E, F, G, H} = Ip@1,
{ip_v6, A@1, B@1, C@1, D@1, E, F, G, H}
end
)
end,
gleam@list:concat([A_records, Aaaa_records]).
-spec format_ip_address(ip_address()) -> binary().
format_ip_address(Ip) ->
case Ip of
{ip_v4, A, B, C, D} ->
_pipe = [A, B, C, D],
_pipe@1 = gleam@list:map(_pipe, fun gleam@int:to_string/1),
gleam@string:join(_pipe@1, <<"."/utf8>>);
{ip_v6, A@1, B@1, C@1, D@1, E, F, G, H} ->
_pipe@2 = [A@1, B@1, C@1, D@1, E, F, G, H],
_pipe@3 = gleam@list:map(_pipe@2, fun(Value) -> case Value of
0 ->
<<""/utf8>>;
_ ->
gleam@int:to_base16(Value)
end end),
_pipe@4 = gleam@string:join(_pipe@3, <<":"/utf8>>),
gleam@string:lowercase(_pipe@4)
end.
-spec discover_nodes(binary(), binary()) -> {ok,
list(gleam@erlang@atom:atom_())} |
{error, nil}.
discover_nodes(Basename, Hostname_query) ->
_pipe = lookup_ip(Hostname_query),
_pipe@2 = gleam@list:map(
_pipe,
fun(Ip) ->
Ip_string = format_ip_address(Ip),
_pipe@1 = (<<<<Basename/binary, "@"/utf8>>/binary,
Ip_string/binary>>),
erlang:binary_to_atom(_pipe@1)
end
),
{ok, _pipe@2}.