Hostname resolver that returns IPv4 addresses and suspends the calling context during a lookup.
DnsResolver
Methods
hostis a knownText, aString, or aStringView: a hostname or a dotted-decimal IPv4 literal.addressesis anArrayof host-orderNat32addresses, asconnectTcptakes them;erroris aString, empty on success. Whenerroris not empty,addressesis empty or incomplete.- If the calling context is already canceled, the call returns the error
canceledand starts no lookup. - An empty
hostgives the errorempty host. - A
hostthatparseIpv4Literalaccepts gives that one address without a lookup. - Any other
hostgoes to the platform's name lookup, which is asked for IPv4 addresses only;addressesholds 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 errorcanceled. - A failed lookup gives an error that names the platform operation and its error code, such as
getaddrinfo_a failed, result=-2on 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
- ipAddressUtils: Dotted-decimal IPv4 literal parser.
- sync/sync: Cross-platform scheduling, sleep, time, IPv4 formatting, and TCP helpers.