March Docs

Dns

DNS resolution module.

Resolves hostnames to IPv4 addresses via the platform's getaddrinfo(3). Results reflect the system's resolver configuration (e.g. /etc/hosts, /etc/resolv.conf, mDNS, and any configured search domains).

IPv4 only: every tcp_connect path opens an AF_INET socket, so an IPv6 answer would be an address nothing can dial. A host with only IPv6 addresses (or an IPv6 literal such as "::1") is NotFound. The list has one entry per address, in the resolver's order, with no duplicates; the interpreter and compiled code return the same list.

Public API: Dns.resolve(host) : Result(List(String), DnsError) Dns.resolve_one(host) : Result(String, DnsError) Dns.is_ip(s) : Bool Dns.error_message(e) : String

Types

typeDnsErrorDnsError#

Functions

fnerror_messageerror_message(e : DnsError) : String#

Return a human-readable description of a DnsError.

march> Dns.error_message(NotFound("nowhere.invalid"))
"no address found for: nowhere.invalid"
march> Dns.error_message(ResolveError("timed out"))
"DNS resolution error: timed out"
fnis_ipis_ip(s : String) : Bool#

Return true if s looks like an IPv4 address (four dot-separated octets). Does not validate octet ranges.

march> Dns.is_ip("127.0.0.1")
true
march> Dns.is_ip("example.com")
false
march> Dns.is_ip("999.1.1.1")  -- octets are not range-checked
true
fnresolveresolve(host : String) : Result(List(String), DnsError)#

Resolve hostname to a list of IPv4 address strings. Returns Ok(addrs) where addrs is non-empty, holds each address once, and keeps the resolver's order; or Err on failure (NotFound when the host has no IPv4 address, ResolveError for any other resolver failure). Performs a network lookup, so there is no runnable example.

match Dns.resolve("example.com") do
Ok(ips)  -> List.head_opt(ips)
Err(e)   -> panic(Dns.error_message(e))
end
fnresolve_oneresolve_one(host : String) : Result(String, DnsError)#

Resolve hostname to its primary IPv4 address: the first address resolve returns, in the resolver's order. Returns Ok(ip_string), or the same Err as resolve. Performs a network lookup, so there is no runnable example.

match Dns.resolve_one("example.com") do
Ok(ip) -> ip
Err(e) -> panic(Dns.error_message(e))
end