Net

lib/Net.gob

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:

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.

type NetError = IOError

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.

fun lookup : [Timeout t] fun(String, t) -> Either IOError (Array IP)

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.

fun lookupUnbounded : fun(String) -> Either IOError (Array IP)

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.

fun parse : fun(String) -> Option IP

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.

let publicInternet : ConnectPolicy
let anywhere : ConnectPolicy
fun allowlist : fun(Array ConnectRule) -> ConnectPolicy

A policy that allows what rules name and nothing else. An AddressRange whose prefix is longer than its address panics.

fun resolve : [Timeout t] fun(String, Int, ConnectPolicy, t) -> Either IOError Destination

host's addresses that policy allows, for a connection to port, resolved within timeout.

fun host : fun(Destination) -> String

The host name d was resolved from, as it was written.

fun port : fun(Destination) -> Int
fun addresses : fun(Destination) -> Array IP

The addresses d allows, in the order a connection tries them. Never empty.

class Connection c : Read c, Write c

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.

fun setDeadline : [Connection c, Timeout t] fun(c, t) -> Unit

Bound every read and write on c from now on by timeout: a Duration from now, or a Deadline. One that passes is TimedOut.

fun clearDeadline : [Connection c] fun(c) -> Unit

Let reads and writes on c wait without limit again.

fun setLimits : [Connection c] fun(c, Option Deadline, Duration) -> Unit

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.

fun shutdownWrite : [Connection c] fun(c) -> Either (WriteError c) Unit

Send the end of input: the peer reads it after everything written before, and can go on writing. A write after it fails.

fun close : [Connection c] fun(c) -> Either (WriteError c) Unit

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.

fun operationDeadline : fun(Option Deadline, Duration) -> Deadline

Returns the deadline of an operation that begins now under setLimits' deadline and each: the sooner of the two.

class Listener l

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.

fun accept : [Listener l] fun(l) -> Either IOError (Accepted l)
fun closeListener : [Listener l] fun(l) -> Either IOError Unit

Stop l accepting: an accept waiting on it ends with Closed, as does every later one. Connections it accepted stay open.

type Accepted l
type ConnectionInfo = ConnectionInfo { local : Option (IP, Int), peer : Option (IP, Int), tls : Option TLSInfo }

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, None where 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.
type TLSInfo = TLSInfo { serverName : Option String, alpn : Option String }
class HasConnectionInfo c

A connection that reports what ConnectionInfo holds about it.

fun connectionInfo : [HasConnectionInfo c] fun(c) -> ConnectionInfo
type ConnectPolicy

Allowlist's names are kept normalised.

type ConnectRule = HostName(String) | AddressRange(IP, Int)

What allowlist allows: a host name, compared after normalising both sides; or the addresses whose first bits bits are those of ip.

type Destination

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.

type IP = V4(Int) | V6(Int, Int, Int)

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.

instance Show Destination
instance Show IP

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.