Net
Names to addresses, and where a program may connect: what a host name
stands for, as the system's resolver says, and which of those addresses a
policy lets a connection reach. Net.TCP connects to what resolve
answers.
match Net.lookup("example.com", Duration.seconds(5)) {
Right(addresses) -> for a in addresses { print(a) }
Left(e) -> print(e)
}
The system's resolver. A lookup asks getaddrinfo, as other programs
on the machine do, so it gets their answers: the hosts file, DNS as the
system configures it, and on Linux whatever else nsswitch names. An
address written as text -- 127.0.0.1, ::1, fe80::1%2 -- is read by
parse instead, and is its own answer, so it resolves without a network.
So does localhost, from the hosts file.
The answer is every address the name has, IPv4 and IPv6, each once, in
the order the resolver prefers them, which is the order to try them in.
A name with no address is Left with the kind HostNotFound; a resolver
that could not answer, Resolver with its code, which differs by system;
a name with a NUL byte in it, Other with EINVAL. The error's op is
"lookup" and its path the name.
The timeout bounds the whole lookup, waiting for a turn included. When
it passes first the answer is TimedOut, at once. The resolver cannot be
stopped, so it goes on to its end, and what it finds is thrown away.
lookupUnbounded has no timeout: it waits as long as the slowest name
server the resolver asks, and holds a thread for all of that time.
Many at once. A lookup waits without holding a worker, so other tasks run meanwhile, and lookups from several tasks run at once, up to 128 in the resolver together; more wait their turn.
Destinations. A Destination is a host, a port, and the addresses the
host resolved to that a ConnectPolicy allowed, and resolve is the only
way to make one. Net.TCP.connect dials those addresses and no others, so
the addresses checked are the addresses connected to: a name cannot
resolve to one address for the check and another for the connection.
let d = Net.resolve("api.example.com", 443, Net.publicInternet, Duration.seconds(10))?
let s = TCP.connect(d, Duration.seconds(10))?
The policies:
publicInternetallows an address only if it is on the public internet. It refuses, for IPv4:0.0.0.0/8("this network"), the private10.0.0.0/8,172.16.0.0/12and192.168.0.0/16, the shared100.64.0.0/10(carrier-grade NAT), the loopback127.0.0.0/8, the link-local169.254.0.0/16(cloud metadata services among it),192.0.0.0/24(IETF protocol assignments), the documentation192.0.2.0/24,198.51.100.0/24and203.0.113.0/24, the benchmarking198.18.0.0/15, the multicast224.0.0.0/4, and the reserved240.0.0.0/4, broadcast among it. For IPv6:::/96(unspecified, loopback and the IPv4-compatible addresses), the unique localfc00::/7, the link-localfe80::/10, the multicastff00::/8, the documentation2001:db8::/32and3fff::/20, and NAT64 for local use,64:ff9b:1::/48.allowlist(rules)allows what its rules name. AHostNamerule allows a host name, and then every address that name resolves to. AnAddressRange(ip, bits)rule allows each resolved address whose firstbitsbits areip's, whatever the name.anywhereallows every address.
Before an address is checked, an IPv4-mapped address
(::ffff:127.0.0.1) or one under the well-known NAT64 prefix
(64:ff9b::7f00:1) is unwrapped to the IPv4 address inside it, which is
where a connection to it goes. An AddressRange rule matches an address
either as written or unwrapped.
Before a name is compared with a HostName rule, both are normalised:
the brackets around an IPv6 literal ([::1]) and one trailing dot are
removed, and ASCII letters are lowercased. Example.COM. and
example.com are the same name to a resolver, so they are the same name to
a rule. Only the comparison normalises: the lookup, and the host a
Destination keeps, are the name as written, trailing dot included, since
a trailing dot marks a name absolute and stops the resolver trying the
search domains first. The lookup drops the brackets alone, which are how a
URL writes an IPv6 address and are not part of it.
Refused. A name whose every address the policy refuses is Left with
the kind Forbidden, op "resolve" and the name as its path. A port
outside 1 to 65535 is Other with EINVAL. A name the policy allows but
that has some addresses it refuses keeps only the others.
What a lookup, a connection, or a read or a write on one fails with: the
same IOError as files and pipes, so that ? carries a failure from any
of them without a conversion.
The addresses host names, waiting at most timeout for them: a
Duration, or a Deadline made earlier so that one limit covers this and
other steps.
lookup with no limit on the wait. The task makes the call itself, and the
thread it runs on stays in getaddrinfo until the resolver answers.
The address text writes, if it writes one: four decimal parts from 0 to
255 without leading zeros; or eight hexadecimal groups, one run of zero
groups written ::, perhaps an IPv4 address as the last two, and perhaps
a scope as % and a number.
A policy that allows what rules name and nothing else. An AddressRange
whose prefix is longer than its address panics.
host's addresses that policy allows, for a connection to port,
resolved within timeout.
The host name d was resolved from, as it was written.
The addresses d allows, in the order a connection tries them. Never
empty.
A connection a program reads and writes, bounds and closes:
Net.TCP.Socket, and Net.TLS.TLS over one. It is what Net.TLS layers
over, and what a server serves.
Bound every read and write on c from now on by timeout: a
Duration from now, or a Deadline. One that passes is TimedOut.
Let reads and writes on c wait without limit again.
Bound every read and write on c from now on by deadline, if there
is one, and each of them by each from when it begins. One that
reaches either is TimedOut. Replaces what
setDeadline set, as it and clearDeadline replace this.
An instance may start an operation's each at its first wait rather
than when it begins, since nothing between the two waits. A connection
whose two directions share one deadline underneath may let an
operation that begins extend one under way in the other direction.
Send the end of input: the peer reads it after everything written before, and can go on writing. A write after it fails.
Close c. A read or a write parked on it, in any task, ends with
Closed, as does every later one. Closing it again does nothing.
Returns the deadline of an operation that begins now under setLimits'
deadline and each: the sooner of the two.
Where connections arrive: a TCP listener, a TLS acceptor over one, or an
in-memory listener for tests. accept waits for the next connection, and
returns Closed once the listener is closed, before or during the wait.
Any other failure is one a later accept may not repeat.
Stop l accepting: an accept waiting on it ends with Closed, as
does every later one. Connections it accepted stay open.
What a server can tell about a connection it accepted, for its logs and its access rules:
local: the address and port it arrived at,Nonewhere the connection has none (an in-memory pipe), or the system would not say.peer: the address and port it came from, likewise.tls: for a TLS connection, the name the client asked for by SNI and the protocol ALPN chose (alpn), once the handshake is done.
A connection that reports what ConnectionInfo holds about it.
Allowlist's names are kept normalised.
What allowlist allows: a host name, compared after normalising both
sides; or the addresses whose first bits bits are those of ip.
A host, a port, and the addresses the host resolved to that the policy
allowed, in the resolver's order: never empty. host is as the caller
wrote it.
An IPv4 address, as the 32-bit number whose bytes are its four parts, the first the most significant; or an IPv6 address, as its first and last 64 bits and its scope: the number of the interface a link-local address is on, or 0.
The address's usual text: dotted decimal for IPv4. For IPv6, RFC 5952's:
lowercase hexadecimal groups without leading zeros, the longest run of two
or more zero groups (the first, of equal runs) written ::, an
IPv4-mapped address's last 32 bits in dotted decimal, and a scope as %
and its number.