Axle v0.14.1
Package

std/net/dns

std/dns — host resolution and IP-literal utilities.

Free functions in this module — no stateful object is needed;
the module namespace (use std::dns::*) is the grouping mechanism.

• resolve a hostname to its first IP for a connect() call.
• check reachability with a real TCP probe + timeout.
• classify literal IP strings without DNS round-trips.

Free functions

TypeMethod and description
string
dnsResolve(host : string) : string Resolves host to its first IP address.
Future<string>
dnsResolveAsync(host : string) : Future<string> Async twin of dnsResolve — the lookup runs on a worker thread;
the future resolves to the first IP ("" when none).
bool
dnsResolveAny(host : string) : bool Whether host resolves to at least one IP address.
bool
isReachable(host : string, port : i32, timeoutMillis : i32) : bool TCP reachability probe: opens a connection on port and closes it.
timeoutMillis is enforced on both the DNS leg and the connect. Use
port 80 / 443 for HTTP probes, 22 for SSH, etc.
bool
__isDecimalByte(c : i32) : bool Whether byte c is an ASCII decimal digit.
i32
__hexDigitValue(c : i32) : i32 The value of the hex digit byte c, or -1 when it is not one.
i64
__ipv4SpanValue(s : string, lo : i64, hi : i64) : i64 The dotted-quad in s[lo..hi) as one 32-bit value, or -1 when the
span is not one.

A leading zero is refused. 010 is an octal reading to some resolvers
and a decimal one to others, so accepting the text here would let one
spelling name two different addresses.
bool
__spanHasDot(s : string, lo : i64, hi : i64) : bool Whether s[lo..hi) carries a ., which is what marks a group as an
embedded dotted-quad rather than hex.
i64
__hexGroupValue(s : string, lo : i64, hi : i64) : i64 The hex group in s[lo..hi), or -1 when the span is not one to four
hex digits.
i64
__readIpv6Run(s : string, lo : i64, hi : i64, allowV4Tail : bool, out : i32[], at : i64) : i64 Read the colon-separated groups of s[lo..hi) into out, starting at
at, and answer how many were written — -1 when the span is not a
run of groups.

allowV4Tail says whether this side ends the address. A dotted-quad
is only ever an address's last element, so 1.2.3.4:: is refused here
rather than read as a leading group.
bool
__parseIpv6(s : string, out : i32[]) : bool Read the IPv6 literal s into the eight slots of out, answering
whether it is one.

The values are produced rather than a yes-or-no, because the literal
test and the loopback test are then one parser: a second reading of the
same text would be a second chance for the two to disagree about what
an address is.
bool
__isLocalhostName(host : string) : bool Whether host is the name localhost, in any ASCII casing.
bool
isIpv4Literal(s : string) : bool Whether s is a valid IPv4 dotted-quad literal (no DNS lookup).
bool
isIpv6Literal(s : string) : bool Whether s is a valid IPv6 literal (no DNS lookup).
bool
isLoopback(host : string) : bool Whether host names the loopback — 127.0.0.0/8, ::1, or "localhost".

Method detail

#dnsResolve

dnsResolve(host : string) : string ! IOException

Resolves host to its first IP address.

Parameters
host hostname or IP literal to resolve
Returns the first resolved IP as a string, or "" if none resolved
Throws
IOException on a resolver failure

#dnsResolveAsync

dnsResolveAsync(host : string) : Future<string> ! IOException

Async twin of dnsResolve — the lookup runs on a worker thread;
the future resolves to the first IP ("" when none).

Parameters
host hostname or IP literal to resolve
Returns a Future<string> that resolves to the first IP, or "" on no result
Throws
IOException on a resolver failure (raised at the await site)

#dnsResolveAny

dnsResolveAny(host : string) : bool

Whether host resolves to at least one IP address.

Parameters
host hostname or IP literal to probe
Returns true if at least one address resolves, false otherwise

#isReachable

isReachable(host : string, port : i32, timeoutMillis : i32) : bool

TCP reachability probe: opens a connection on port and closes it.
timeoutMillis is enforced on both the DNS leg and the connect. Use
port 80 / 443 for HTTP probes, 22 for SSH, etc.

Parameters
host hostname or IP literal to probe
port TCP port to attempt the connection on, 1..65535
timeoutMillis connection timeout in milliseconds
Returns true if the TCP connection succeeded within the deadline

#__isDecimalByte

__isDecimalByte(c : i32) : bool

Whether byte c is an ASCII decimal digit.

#__hexDigitValue

__hexDigitValue(c : i32) : i32

The value of the hex digit byte c, or -1 when it is not one.

Parameters
c candidate byte

#__ipv4SpanValue

__ipv4SpanValue(s : string, lo : i64, hi : i64) : i64

The dotted-quad in s[lo..hi) as one 32-bit value, or -1 when the
span is not one.

A leading zero is refused. 010 is an octal reading to some resolvers
and a decimal one to others, so accepting the text here would let one
spelling name two different addresses.

Parameters
s text the span indexes into
lo first byte of the span
hi one past its last byte
Returns the packed address, or -1

#__spanHasDot

__spanHasDot(s : string, lo : i64, hi : i64) : bool

Whether s[lo..hi) carries a ., which is what marks a group as an
embedded dotted-quad rather than hex.

Parameters
s text the span indexes into
lo first byte of the span
hi one past its last byte

#__hexGroupValue

__hexGroupValue(s : string, lo : i64, hi : i64) : i64

The hex group in s[lo..hi), or -1 when the span is not one to four
hex digits.

Parameters
s text the span indexes into
lo first byte of the span
hi one past its last byte

#__readIpv6Run

__readIpv6Run(s : string, lo : i64, hi : i64, allowV4Tail : bool, out : i32[], at : i64) : i64

Read the colon-separated groups of s[lo..hi) into out, starting at
at, and answer how many were written — -1 when the span is not a
run of groups.

allowV4Tail says whether this side ends the address. A dotted-quad
is only ever an address's last element, so 1.2.3.4:: is refused here
rather than read as a leading group.

Parameters
s text the span indexes into
lo first byte of the span
hi one past its last byte
allowV4Tail whether a trailing dotted-quad is accepted
out eight-slot destination for the group values
at first slot to write
Returns groups written, or -1

#__parseIpv6

__parseIpv6(s : string, out : i32[]) : bool

Read the IPv6 literal s into the eight slots of out, answering
whether it is one.

The values are produced rather than a yes-or-no, because the literal
test and the loopback test are then one parser: a second reading of the
same text would be a second chance for the two to disagree about what
an address is.

Parameters
s candidate literal
out eight-slot destination for the group values

#__isLocalhostName

__isLocalhostName(host : string) : bool

Whether host is the name localhost, in any ASCII casing.

Parameters
host candidate name

#isIpv4Literal

isIpv4Literal(s : string) : bool

Whether s is a valid IPv4 dotted-quad literal (no DNS lookup).

Parameters
s candidate IP string to classify
Returns true if s parses as a valid IPv4 address (e.g. "192.168.1.1")

#isIpv6Literal

isIpv6Literal(s : string) : bool

Whether s is a valid IPv6 literal (no DNS lookup).

Parameters
s candidate IP string to classify
Returns true if s parses as a valid IPv6 address (e.g. "::1", "2001:db8::1")

#isLoopback

isLoopback(host : string) : bool

Whether host names the loopback — 127.0.0.0/8, ::1, or "localhost".

Parameters
host hostname or IP literal to test
Returns true for any loopback address or the literal string "localhost"