Hostname resolver that returns IPv4 addresses and suspends the calling context during a lookup.


DnsResolver

Methods

resolve

(host -- addresses error) Resolves host to IPv4 addresses.
  • host is a known Text, a String, or a StringView: a hostname or a dotted-decimal IPv4 literal.
  • addresses is an Array of host-order Nat32 addresses, as connectTcp takes them; error is a String, empty on success. When error is not empty, addresses is empty or incomplete.
  • If the calling context is already canceled, the call returns the error canceled and starts no lookup.
  • An empty host gives the error empty host.
  • A host that parseIpv4Literal accepts gives that one address without a lookup.
  • Any other host goes to the platform's name lookup, which is asked for IPv4 addresses only; addresses holds the answer in the lookup's order, duplicates included. While the lookup is pending, the calling context is suspended, and canceling that context ends the call with the error canceled.
  • A failed lookup gives an error that names the platform operation and its error code, such as getaddrinfo_a failed, result=-2 on Linux.
  • Linux: lookup notifications use the lowest available real-time signal, __libc_current_sigrtmin. Importing a sync/ module blocks that signal during module initialization.

Examples

Example: literal and empty host

An IPv4 literal gives its address without a lookup, and an empty host is refused with an error text.

"String"           use
"control"          use
"sync/DnsResolver" use
"sync/sync"        use

{} Int32 {} [
  resolver: DnsResolver;
  addresses: error: "127.0.0.1" resolver.resolve;;
  ("count=" addresses.size " address=" 0 addresses @ ipv4ToString " error=[" error "]" LF) printList
  noAddresses: emptyError: "" resolver.resolve;;
  ("count=" noAddresses.size " error=[" emptyError "]" LF) printList
  0
] "main" exportFunction

Expected Output

count=1 address=127.0.0.1 error=[]
count=0 error=[empty host]

See also