|
| 1 | +import gleam/erlang/atom |
| 2 | +import gleam/int |
| 3 | +import gleam/list |
| 4 | +import gleam/result |
| 5 | +import gleam/string |
| 6 | + |
| 7 | +/// A DNS error that can occur when discovering nodes. |
| 8 | +pub type LookupError { |
| 9 | + FormatError |
| 10 | + QueryFormatError |
| 11 | + ServerFailure |
| 12 | + NoSuchDomain |
| 13 | + Timeout |
| 14 | + NotImplemented |
| 15 | + Refused |
| 16 | + BadVersion |
| 17 | + PosixError(String) |
| 18 | + Unknown |
| 19 | +} |
| 20 | + |
| 21 | +@external(erlang, "barnacle_ffi", "lookup_a") |
| 22 | +fn lookup_a( |
| 23 | + hostname: String, |
| 24 | + timeout: Int, |
| 25 | +) -> Result(List(#(Int, Int, Int, Int)), LookupError) |
| 26 | + |
| 27 | +@external(erlang, "barnacle_ffi", "lookup_aaaa") |
| 28 | +fn lookup_aaaa( |
| 29 | + hostname: String, |
| 30 | + timeout: Int, |
| 31 | +) -> Result(List(#(Int, Int, Int, Int, Int, Int, Int, Int)), LookupError) |
| 32 | + |
| 33 | +pub type IpAddress { |
| 34 | + IpV4(Int, Int, Int, Int) |
| 35 | + IpV6(Int, Int, Int, Int, Int, Int, Int, Int) |
| 36 | +} |
| 37 | + |
| 38 | +/// Perform a DNS lookup against a hostname. This will return a list of IP |
| 39 | +/// addresses. Queries will be performed using the `A` and `AAAA` record types. |
| 40 | +pub fn dns_lookup( |
| 41 | + hostname: String, |
| 42 | + timeout: Int, |
| 43 | +) -> Result(List(IpAddress), LookupError) { |
| 44 | + use a_records <- result.try(lookup_a(hostname, timeout)) |
| 45 | + |
| 46 | + let a_records = |
| 47 | + a_records |
| 48 | + |> list.map(fn(ip) { |
| 49 | + let #(a, b, c, d) = ip |
| 50 | + IpV4(a, b, c, d) |
| 51 | + }) |
| 52 | + |
| 53 | + use aaaa_records <- result.try(lookup_aaaa(hostname, timeout)) |
| 54 | + |
| 55 | + let aaaa_records = |
| 56 | + aaaa_records |
| 57 | + |> list.map(fn(ip) { |
| 58 | + let #(a, b, c, d, e, f, g, h) = ip |
| 59 | + IpV6(a, b, c, d, e, f, g, h) |
| 60 | + }) |
| 61 | + |
| 62 | + Ok(list.concat([a_records, aaaa_records])) |
| 63 | +} |
| 64 | + |
| 65 | +fn format_ip_address(ip: IpAddress) -> String { |
| 66 | + case ip { |
| 67 | + IpV4(a, b, c, d) -> { |
| 68 | + [a, b, c, d] |
| 69 | + |> list.map(int.to_string) |
| 70 | + |> string.join(".") |
| 71 | + } |
| 72 | + IpV6(a, b, c, d, e, f, g, h) -> { |
| 73 | + [a, b, c, d, e, f, g, h] |
| 74 | + |> list.map(fn(value) { |
| 75 | + case value { |
| 76 | + 0 -> "" |
| 77 | + _ -> int.to_base16(value) |
| 78 | + } |
| 79 | + }) |
| 80 | + |> string.join(":") |
| 81 | + |> string.lowercase |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/// Discover nodes using DNS. The first argument is the basename of the nodes. |
| 87 | +/// Currently, Barnacle only supports connecting to nodes with the same basename. |
| 88 | +/// |
| 89 | +/// The second argument is the hostname to query against. Both A and AAAA records |
| 90 | +/// will be queried. The final argument is an optional timeout for the DNS |
| 91 | +/// lookup. This defaults to 5000ms if not supplied. |
| 92 | +pub fn discover_nodes( |
| 93 | + basename: String, |
| 94 | + hostname_query: String, |
| 95 | + timeout: Int, |
| 96 | +) -> Result(List(atom.Atom), LookupError) { |
| 97 | + use ip_addresses <- result.try(dns_lookup(hostname_query, timeout)) |
| 98 | + |
| 99 | + ip_addresses |
| 100 | + |> list.map(fn(ip) { |
| 101 | + let ip_string = format_ip_address(ip) |
| 102 | + { basename <> "@" <> ip_string } |> atom.create_from_string |
| 103 | + }) |
| 104 | + |> Ok |
| 105 | +} |
0 commit comments