sysl

The net module

sysl.net — TCP, UDP and Unix domain sockets and the names they resolve: socket, bind, listen, accept, connect, send, recv, send_to, recv_from, shutdown, close; unix_listen and unix_connect; non_blocking and would_block; addresses as sysl values; and the hooks a target without a C library answers.

Every declaration in sysl.net, with its signature: the generated API page. This page is the argument — what the module is for, and how its pieces fit; that one is the list.

sysl.net is sockets: a TCP stream, a UDP socket, a Unix domain stream, an address to give each, and the calls that move bytes. It requires os.

import sysl.net.{ipv4, resolve}

val addrs = resolve("127.0.0.1", 8080).unwrap()

print(addrs.len() > 0)
print(addrs[0].text(), addrs[0].port(), addrs[0].family())
print(addrs[0])
print(addrs[0] == ipv4(127, 0, 0, 1, 8080))
true
127.0.0.1 8080 Ipv4
127.0.0.1:8080
true

Why it is sysl.net, and still speaks sockets

The library has two conventions, and the rule that separates them is visible in one pair: sysl.term is the portable surface sysl invented over a terminal, and sysl.posix.tty is termios presented as termios. Everything under sysl.posix is a POSIX API presented as itself, and this module is not one any more: every call it makes goes through the hooks of sysl.net.sys, which a hosted target’s sockets answer and a kernel with a network stack of its own answers just as well, and an address is a sysl value rather than a sockaddr. So it is sysl.net. It was sysl.posix.net until 0.1.0-alpha.6; the names inside are the same, and moving a program is its import line.

The vocabulary stays the sockets one — socket, bind, listen, accept, connect, send, recv, shutdown, close, with those meanings — because forty years of programs and documentation are written in it, and a reader who knows it should not have to translate. It is deliberately not a TcpStream and a TcpListener: the sockets API has one type for both, and so does this.

Blocking, unless asked

Every call here returns when its work is done, unless the socket was put in non-blocking mode (the section on polling, below). There is no event loop and no readiness notification in this module; readiness over many sockets at once is sysl.io.poll, which takes each socket’s fd().

That is the line Rust draws too, and for the same reason: std::net is blocking and every async runtime, tokio included, is a package outside the standard library. sysl-lang/libuv is where the event-loop story lives, and the two compose — a program that wants a loop uses the package, and one that wants a straight line uses this.

A client

Resolve, make a socket of the address’s family, connect.

import sysl.net.{resolve, socket, Socket}

// A listener of our own, so the page has something to connect to. Port 0 asks the machine for one
// it has free, and `local` is what says which it gave.
val here = resolve("127.0.0.1", 0).unwrap()[0]
var server = socket(here).unwrap()

server.reuse_address().unwrap()
server.bind(here).unwrap()
server.listen().unwrap()

val at = server.local().unwrap()

var client = socket(at).unwrap()

client.connect(at).unwrap()

var (accepted, from) = server.accept().unwrap()

client.send_all([104, 105]).unwrap()

var into: []u8 = [0; 8]
val got = accepted.recv(into).unwrap()

print(got, into[0] == u8('h'), into[1] == u8('i'))
print(from.family())

accepted.close().unwrap()
client.close().unwrap()
server.close().unwrap()
2 true true
Ipv4

resolve answers every address, in the order the machine ranked them, and both protocol versions come back. That is what makes a program work on a v6-only network without knowing it is on one, and the order is the system’s answer to which to prefer — RFC 6724 says how a machine sorts them, and second-guessing it here would be this module inventing policy that is not its to have. So the ordinary client is a loop over what came back, stopping at the first address that connects.

resolve_passive is the other question. A listener binds to the wildcard — every address the machine answers on — and nothing about the arguments alone says whether that or the loopback was meant. It is a separate call because a caller who got the wrong one binds to the loopback and wonders why nobody outside can reach it.

The bytes

send writes some of what it was given and says how many. A short write is not an error: a stream moves what it can and the caller comes back for the rest. send_all is that loop, and is what almost every caller wants — it also retries an interrupted call rather than reporting it, since a signal arriving mid-write is not a failure of the write.

recv reads into a slice and says how many bytes arrived. Zero means the peer has finished writing — end of stream, not an error and not a timeout. That is the one answer every reader has to handle, and it is why the count is a plain usize: a stream that is over is an ordinary outcome, and a loop that reads until zero is the whole protocol.

shutdown is not close

shutdown gives up one direction while the descriptor stays open, and the interesting case is Write: it sends the peer an end of stream, so a program that has finished asking can say so and then go on reading the answer. Closing would have said nothing and thrown the answer away.

import sysl.net.{resolve, socket, Shutdown}

val here = resolve("127.0.0.1", 0).unwrap()[0]
var server = socket(here).unwrap()

server.bind(here).unwrap()
server.listen().unwrap()

val at = server.local().unwrap()
var client = socket(at).unwrap()

client.connect(at).unwrap()

var (accepted, _) = server.accept().unwrap()

client.shutdown(Shutdown.Write).unwrap()

var into: []u8 = [0; 4]

// The client has finished writing, so the server sees end of stream -- and can still answer.
print(accepted.recv(into).unwrap())

accepted.send_all([121]).unwrap()

print(client.recv(into).unwrap(), into[0] == u8('y'))

accepted.close().unwrap()
client.close().unwrap()
server.close().unwrap()
0
1 true

A socket closes itself when its last copy goes, as Rust’s TcpStream does: out of scope, out of a Buf, with the struct that held it. A copy of a socket is a second name for one descriptor, not a second descriptor, so it stays open while any copy lives, and closing one copy closes them all — a second close answers NotOpen and closes nothing. close is still the call for a program that wants the descriptor back at a moment of its own, or wants to know whether that worked; the explicit closes above are that, and are no longer needed to stop a leak.

fd() lends the descriptor and into_raw_fd() gives it away, for a program handing it to C. A lent number is closed with the socket; a given one is the caller’s to close, and every copy of the socket is closed from then on. UdpSocket and UnixListener have the same three calls and close themselves the same way. A socket may be handed to another thread: the count the copies share is atomic.

The timeout, which a blocking call cannot do without

Otherwise “returns when the work is done” can mean never. read_timeout and write_timeout bound it in milliseconds, and zero — where a socket starts — means no limit.

import sysl.net.{resolve, socket, timed_out}

val here = resolve("127.0.0.1", 0).unwrap()[0]
var server = socket(here).unwrap()

server.bind(here).unwrap()
server.listen().unwrap()

val at = server.local().unwrap()
var client = socket(at).unwrap()

client.connect(at).unwrap()

var (accepted, _) = server.accept().unwrap()

accepted.read_timeout(50).unwrap()

var into: []u8 = [0; 8]

// Nothing was ever sent, so without the timeout this call would not return at all.
print(timed_out(accepted.recv(into).unwrap_err()))

accepted.close().unwrap()
client.close().unwrap()
server.close().unwrap()
true

timed_out is a function rather than a case of IoError. A timed-out call reports ETIMEDOUT, which is a number and not one of that enum’s named cases — and adding a variant to an error type every module in the library shares, for a condition only this one can produce, is a larger change than the question deserves.

The rest of the error half is sysl.fs‘s IoError, from the same errno numbers as everywhere else. A name that will not resolve is NotFound, whatever the resolver’s own reason was: the EAI_* codes are a numbering of their own and overlap errno‘s — EAI_AGAIN is 2 on Darwin, which is ENOENT — so passing them through would produce an error that means something else entirely.

Datagrams: UdpSocket

udp_socket(address) makes a datagram socket of that address’s family; bind gives it a port to receive on, and then each send_to is one datagram and each recv_from takes one whole, with who sent it — which is the whole of an echo server:

import sysl.net.{ipv4, udp_socket}

val here = ipv4(127, 0, 0, 1, 0)
var server = udp_socket(here).unwrap()

server.bind(here).unwrap()

val at = server.local().unwrap()
var client = udp_socket(at).unwrap()

client.bind(here).unwrap()
client.send_to([112, 105, 110, 103], at).unwrap()

var into: []u8 = [0; 64]
val (n, from) = server.recv_from(into).unwrap()

// Back to whoever asked.
server.send_to(into[0..<n], from).unwrap()

val (m, back) = client.recv_from(into).unwrap()

print(n, m, into[0] == u8('p'), back == at, from == client.local().unwrap())

server.close().unwrap()
client.close().unwrap()
4 4 true true true

A datagram is not a stream. One longer than the buffer it is received into keeps what fits and loses the rest; one sent may never arrive; two may arrive in either order. UDP is for the exchanges that can live with that — a query and its answer, a heartbeat, a sample — and the calls say nothing more than that. An empty datagram is a datagram, and recv_from answers zero for it.

connect on a UdpSocket fixes where it talks to: send then goes there without naming it, and recv takes datagrams from there and from nowhere else. A lost datagram is waited for forever unless read_timeout says otherwise, which is the reason a UDP client sets one.

Polling: non_blocking and would_block

A program that already has a loop of its own — a frame, a tick — cannot sit in accept waiting for a client that may never come. non_blocking() puts a socket in non-blocking mode, and then an accept, recv or send that would have had to wait answers an error would_block recognises instead of waiting, as Rust’s set_nonblocking does. non_blocking(false) takes it out again. It is on Socket, UdpSocket and UnixListener.

import sysl.net.{ipv4, socket, timed_out, would_block}

val here = ipv4(127, 0, 0, 1, 0)
var server = socket(here).unwrap()

server.bind(here).unwrap()
server.listen().unwrap()
server.non_blocking().unwrap()

// Nobody has connected yet: once a frame, this is all the loop learns.
val e = server.accept().unwrap_err()

print(would_block(e), timed_out(e))

val at = server.local().unwrap()
var client = socket(at).unwrap()

client.connect(at).unwrap()

// The connection may not be queued yet: ask again until it is.
var got = server.accept()

while got.is_err() && would_block(got.unwrap_err())
    got = server.accept()

var (accepted, _) = got.unwrap()

accepted.close().unwrap()
client.close().unwrap()
server.close().unwrap()
true false

A connection accept answers is blocking, whatever its listener is. A BSD hands an accepted socket its listener’s mode and Linux does not; here the two agree, so a program that polls its listener and then serves the request in a straight line gets the same connection on a Mac as under Linux, and one that wants the connection non-blocking says so.

would_block and timed_out are different answers. Both come from the kernel’s EAGAIN, and the socket’s mode decides which: a blocking socket whose timeout ran out is timed_out, and a non-blocking one with nothing ready is would_block — EAGAIN‘s own code, 35 on a BSD and 11 elsewhere.

A send to a peer that has gone is an error, not SIGPIPE

A send, send_all or send_to to a peer that has closed answers an IoError, which peer_closed(e) recognises (EPIPE, 32 everywhere, or ECONNRESET, 54 on a BSD and 104 elsewhere). It never raises SIGPIPE, whose default action ends the process silently with status 141. This holds for Unix and TCP sockets, connecting and accepted, blocking and non-blocking. A stream’s first send to a closed TCP peer can still succeed, because the peer’s reset has not arrived; a later one is refused. Unix sockets refuse the first.

It is done per socket on Darwin (SO_NOSIGPIPE, set on every socket made or accepted) and per call elsewhere (MSG_NOSIGNAL), so the process’s signal disposition is untouched: a program that installs its own SIGPIPE handler with sysl.signal still hears it for a write to a pipe or file, and does not hear it for a socket.

Buffer sizes

set_send_buffer(bytes) and set_recv_buffer(bytes) ask the kernel for a socket’s buffers, and send_buffer() and recv_buffer() read what it granted, on Socket and UdpSocket. The kernel may round the size — Linux doubles it and both clamp to a system limit — so compare the answer with >=, not ==. A size that is not positive is refused as EINVAL (22) and the buffer is left as it was. macOS gives a Unix stream socket 8 KB, so a 3 MB response takes 384 turns of a 8 KB-at-a-time loop; with 4 MB buffers on both ends the same transfer takes 48 turns, limited then by the 64 KB the reader takes per turn. Set the receiver’s buffer too: the sender’s buffer alone does not bound the turns.

Several writes as one: send_vectored

send_vectored(parts) takes a slice of byte slices and writes them as one gather write (sendmsg), so a header and a body go out in one system call without being copied into a buffer first. It answers the total number of bytes that went, and a short write is not an error and may stop partway through a piece, exactly as for send. Empty pieces are skipped; no pieces, or only empty ones, answers zero without asking the system. A platform limit on pieces per call (IOV_MAX) sends the first of them: come back for the rest.

import sysl.net.{resolve, socket}

val here = resolve("127.0.0.1", 0).unwrap()[0]
val server = socket(here).unwrap()

server.reuse_address().unwrap()
server.bind(here).unwrap()
server.listen().unwrap()

val at = server.local().unwrap()
val client = socket(at).unwrap()

client.connect(at).unwrap()

val (accepted, _) = server.accept().unwrap()

val parts: []const []const u8 = [[72, 105], [], [33]]

print(client.send_vectored(parts).unwrap())

var into: []u8 = [0; 8]

print(accepted.recv(into).unwrap())
3
3

From a file: send_file

send_file(file, offset, count) sends up to count bytes of a sysl.fs.File, starting offset bytes in, and the bytes go from the file to the socket inside the kernel (sendfile) without passing through the program. That is the call a static file server is made of: open the file, send the header with send, then send the body from the file.

It answers the number of bytes that went, and it answers as send does: a short count is not an error, so the caller comes back from offset plus the count for the rest. A non-blocking socket with no room answers an error would_block recognises, and a peer that has gone answers one peer_closed recognises without raising SIGPIPE. The end of the file is the end of what it sends: a count past it answers what was there, and an offset at or past it answers zero. A count of zero answers zero without asking the system.

import sysl.fs.{create, make_temp_dir, open, remove_dir_all}
import sysl.net.{resolve, socket}
import sysl.path.join
import sysl.text.from_utf8

val dir = make_temp_dir("send-file-").unwrap()
var w = create(join(dir, "page.html")).unwrap()

w.write("<p>hello</p>".bytes)
w.close().unwrap()

val here = resolve("127.0.0.1", 0).unwrap()[0]
val server = socket(here).unwrap()

server.bind(here).unwrap()
server.listen().unwrap()

val client = socket(server.local().unwrap()).unwrap()

client.connect(server.local().unwrap()).unwrap()

val (accepted, _) = server.accept().unwrap()
val page = open(join(dir, "page.html")).unwrap()

// Five bytes from the fourth, then everything past the end there is.
print(accepted.send_file(page, 3, 5).unwrap(), accepted.send_file(page, 8, 100).unwrap())

var into: []u8 = [0; 16]
var got: usize = 0

while got < 9
    got += client.recv(into[got..<16]).unwrap()

print(from_utf8(into[0..<got]).unwrap())
remove_dir_all(dir).unwrap()
5 4
hello</p>

The file’s position does not move, so send_file mixes with read and write as read_at does, and what the file still buffers for writing is handed over first, so every byte the program has written is sent. A file not opened for reading answers NotOpen, as a closed one does; an offset past what a long holds is refused with EINVAL (22). An offset past 4 GiB is an ordinary one — the offset is a u64 and the count a usize, so a file of any size is sent.

Where there is no sendfile, the library reads and sends instead. A target whose supplier answers sysl.sys.UNSUPPORTED for sysl_net_send_file, and a file the kernel will not send from (a pipe, or a filesystem with no page-cache path), go through a loop that reads the file 64 KB at a time with read_at and sends each piece, stopping at the count, the end of the file or a short send. Its answers mean the same; only the copy through the program is extra. A pipe, which has no offsets, is refused there as pread refuses it (ESPIPE, 29).

On Darwin it is sendfile(file, socket, offset, &len, NULL, 0), which takes a TCP or a Unix stream; it reports a peer that has gone as ENOTCONN, and the answer here is EPIPE, as send‘s is. Under Linux it is sendfile(socket, file, &offset, count), which sends at most 2 GiB less 4 KB per call (a short count, as any other), and which can raise SIGPIPE: it is blocked in the calling thread for the call and one it raised is taken back, so the process’s disposition is untouched here too.

Options: set_no_delay, set_reuse_port, set_v6_only, keepalive

Each is a setter that takes on = true and a getter of the same name that reads it back from the kernel: set_no_delay / no_delay (TCP_NODELAY, send small writes at once), set_reuse_port / reuse_port (SO_REUSEPORT, several sockets bind one port, set before bind; also on UdpSocket), and set_keepalive / keepalive (SO_KEEPALIVE). The keepalive timing is set_keepalive_idle(seconds), set_keepalive_interval(seconds) and set_keepalive_count(probes), each positive and read back by the same name without set_. set_v6_only / v6_only (IPV6_V6ONLY, on Socket and UdpSocket) makes an IPv6 socket take IPv6 only. Both Darwin and Linux default to dual-stack, where a listener on :: also holds the IPv4 side of the port and a second one on 0.0.0.0 is refused with “address in use”; with set_v6_only() on the :: socket the two bind one port, which is how a server runs one listener per family. Set it before bind: a bound socket refuses the change (Darwin InvalidInput, Linux the system’s “busy”) and keeps its setting. On an IPv4 socket both calls answer InvalidInput. The idle time is TCP_KEEPIDLE on Linux and TCP_KEEPALIVE on Darwin; the library picks. A closed socket answers NotOpen, a target without an option answers the “not supported” error, and a socket that is not TCP answers the system’s refusal of the TCP options.

Every Socket and UdpSocket method takes self, so a socket bound with val can be used without first copying it into a var; a copy shares one descriptor either way.

Unix domain sockets

A Unix domain socket is a stream between two processes on one machine, addressed by a path in the filesystem. unix(path) is the address, unix_listen makes the socket file and listens on it, and unix_connect answers a Socket — the type a TCP connection is — so the connection is read and written exactly as a TCP one is. UnixListener.accept answers the connection alone: a Unix client is ordinarily unnamed, so there is no peer address worth handing back.

import sysl.fs.{make_temp_dir, metadata, remove_dir_all, remove_file, set_permissions}
import sysl.net.{unix, unix_connect, unix_listen}
import sysl.path.join

val dir = make_temp_dir("net-doc-").unwrap()
val at = unix(join(dir, "control.sock")).unwrap()
var server = unix_listen(at).unwrap()

// Only this user may connect: who may is the socket file's permission bits.
set_permissions(at.path(), 0o600).unwrap()
print(f"${metadata(at.path()).unwrap().permissions()}%o")

var client = unix_connect(at).unwrap()
var accepted = server.accept().unwrap()

client.send_all("ping".bytes).unwrap()

var into: []u8 = [0; 8]

print(accepted.recv(into).unwrap())

accepted.close().unwrap()
client.close().unwrap()
server.close().unwrap()

// The socket file outlives its listener; removing it is the program's.
remove_file(at.path()).unwrap()
remove_dir_all(dir).unwrap()
600
4

The socket file is not removed by anything in the module. unix_listen refuses a path that already holds something — EADDRINUSE‘s code, 48 on a BSD and 98 under Linux — rather than replacing it, because what is there may be another program’s live socket or not a socket at all; a server that owns its path removes it with sysl.fs.remove_file first, and again after closing. Connecting to a path with nothing there is NotFound.

A path the platform’s address cannot carry is refused by unix itself, not at the bind: sockaddr_un‘s sun_path is 104 bytes on Darwin and 108 under glibc, one of them the terminator, so max_unix_path is 103 or 107 and a longer path is ENAMETOOLONG‘s code:

import sysl.net.{max_unix_path, unix}

var long = ""

for _ in 0..<max_unix_path + 1 do long += "a"

print(unix(long).is_err(), unix(long[0..<max_unix_path]).is_ok(), unix("").is_err())
true true true

Who is on the other end

Socket.peer_credentials() answers the user and group of the process at the other end of a Unix domain connection, and its process id where the system gives it — so a server can authenticate the operating-system user talking to it without a password. The ids are the u32s sysl.process.user() and group() answer, so a peer is compared with ==, and sysl.process.user_name turns an id into a name:

import sysl.fs.{make_temp_dir, remove_dir_all}
import sysl.net.{unix, unix_connect, unix_listen}
import sysl.path.join
import sysl.process.{group, pid, user, user_name}

val dir = make_temp_dir("net-doc-peer-").unwrap()
val at = unix(join(dir, "peer.sock")).unwrap()
var server = unix_listen(at).unwrap()
var client = unix_connect(at).unwrap()
var accepted = server.accept().unwrap()
val peer = accepted.peer_credentials().unwrap()

print(peer.uid == user(), peer.gid == group())
print(peer.pid == Some(pid()))
print(user_name(peer.uid).unwrap() == user_name(user()).unwrap())

accepted.close().unwrap()
client.close().unwrap()
server.close().unwrap()
remove_dir_all(dir).unwrap()
true true
true
true

Only a Unix domain socket has credentials. A TCP socket answers the not supported on this target error rather than a made-up user, and a closed socket answers NotOpen. pid is an Option: Linux (SO_PEERCRED) and macOS (LOCAL_PEERPID) give it, and a system that gives only the user and group answers None. The hook beneath is sysl_net_peer_credentials(fd, out) -> int, writing a sysl.net.sys.Credentials (uid, gid, and pid as an i64, -1 for unknown).

An address is a sysl value

Address is an IP address and a port: four bytes or sixteen, a port, and — for a link-local IPv6 address — the interface it is scoped to. It compares with == and hashes like any other value, so a server can key a table on whoever sent it something. resolve answers addresses, and ipv4 and ipv6 build one with no resolver at all, which is what a program on a machine without one writes:

import sysl.net.{ipv4, ipv6}

val a = ipv4(10, 0, 2, 15, 8007)

print(a, a.family(), a.octets()[3], a.with_port(7))
print(ipv6([0x2001, 0xdb8, 0, 0, 0, 0, 0, 1], 443))
print(ipv6([0xfe80, 0, 0, 0, 0, 0, 0, 1], 0, 4).text())
print(ipv6([0, 0, 0, 0, 0, 0xffff, 0x7f00, 1], 0).text())
10.0.2.15:8007 Ipv4 15 10.0.2.15:7
[2001:db8::1]:443
fe80::1%4
::ffff:127.0.0.1

text() is the numeric form and never a name: turning an address back into a hostname is a second lookup over the network, which is not what a program printing what it just connected to is asking for. An IPv6 address is written as RFC 5952 says every program should — lower-case hex, no leading zeros, the longest run of two or more zero groups as :: — and printing one brackets it, [::1]:8080, because a v6 address has colons in it and ::1:8080 cannot be read apart.

Family is Ipv4 and Ipv6, named by the version rather than by AF_INET‘s number, which is 30 on Darwin and 10 under glibc and is the hosted answer’s business. The sockaddr layouts — Darwin’s carry a length byte glibc’s do not — stay in that answer’s C, where the header decides them.

Answering the network on a target with no C library

Everything above reaches the network through sysl.net.sys and through nothing else: nineteen hooks, each an extern no module of the library defines.

hookwhat it answers
sysl_net_socket(family, kind) -> inta descriptor; family 4, 6 or unix_domain (1), kind stream (0) or datagram (1)
sysl_net_bind(fd, at) -> int, sysl_net_connect(fd, to) -> intzero
sysl_net_bind_path(fd, path, path_len) -> int, sysl_net_connect_path(fd, path, path_len) -> intzero; a Unix domain socket’s filesystem path
sysl_net_listen(fd, backlog) -> intzero
sysl_net_accept(fd, peer) -> intthe new connection’s descriptor, the peer written into peer
sysl_net_local(fd, at) -> intzero, the bound address written into at
sysl_net_send(fd, from, len) -> isize, sysl_net_recv(fd, into, room) -> isizea count of bytes
sysl_net_send_to(fd, from, len, to) -> isize, sysl_net_recv_from(fd, into, room, from) -> isizea count of bytes, the sender written into from
sysl_net_shutdown(fd, how) -> int, sysl_net_close(fd) -> intzero
sysl_net_option(fd, which, value) -> intzero; read_timeout, write_timeout (milliseconds), reuse_address, non_blocking, send_buffer or recv_buffer (bytes, positive), no_delay, reuse_port, keepalive (1 or 0), keepalive_idle, keepalive_interval, keepalive_count (seconds or probes, positive), v6_only (20, 1 or 0; EINVAL on a socket that is not IPv6); send_buffer_size and recv_buffer_size, and each of the six above plus read_back (14 to 19), and v6_only + read_back (26), ignore value and answer the setting
sysl_net_send_vectored(fd, pieces, count) -> isizea count of bytes; pieces is count Pieces, a base pointer and a length each, laid out as C’s struct iovec
sysl_net_send_file(fd, file, offset, count) -> isizea count of bytes, at most count of the open file file from offset (never negative; count never zero), fewer at its end; UNSUPPORTED makes send_file read and send instead
sysl_net_peer_credentials(fd, out) -> intzero, a Unix domain peer’s user, group and process id (-1 if unknown) written into out
sysl_net_resolve(host, host_len, port, passive, out, room) -> inthow many addresses it wrote

Each answers zero or more for success and the IoError code negated for a failure — the contract of every *.sys module (sysl.fs.sys, sysl.io.sys, sysl.process.sys) — and sysl.sys.UNSUPPORTED (-38 on every platform, not the host’s ENOSYS) for a call the target cannot make at all, which reaches the caller as not supported on this target. A call its timeout stopped answers ETIMEDOUT negated (60 on a BSD, 110 everywhere else), which is what timed_out asks, and one on a non-blocking socket that would have had to wait answers EAGAIN negated (35 on a BSD, 11 everywhere else), which is what would_block asks. An address crosses as sysl.net.sys.Endpoint: sixteen bytes in network order (an IPv4 address in the first four), a scope, a port as the number itself, and the family — 24 bytes laid out as C lays out the same four fields, so a supplier reads it without knowing anybody’s sockaddr.

On a hosted target the library answers them itself, under weak exports (a module may supply another module’s extern). A freestanding program answers each hook it reaches with an @export of its own — a kernel’s UDP echo server needs six of them:

import sysl.net.sys.Endpoint

@export("sysl_net_recv_from")
k_recv_from(fd: int, into: *u8, room: usize, from: *Endpoint) -> isize = -110

A hook the program reaches and leaves unanswered is refused when it is compiled, all of them in one sentence, rather than surfacing at the link as a symbol no line of the program names:

import sysl.net.{ipv4, udp_socket}

val here = ipv4(10, 0, 2, 15, 8007)

udp_socket(here) match
    Ok(s) ->
        var u = s
        val _ = u.bind(here)
    Err(_) -> ()
this program reaches 'sysl.net', and 'aarch64-freestanding' has no operating system under it for the standard library to answer a network with, so the program answers it: define 'sysl_net_bind', 'sysl_net_close', 'sysl_net_socket' with '@export', each taking what its 'extern' in 'sysl.net.sys' declares and answering a descriptor, a count or zero, or the code of an 'IoError' negated

sysl_net_close is asked for although the program never calls close: a socket closes itself when its last copy goes, so making one reaches it. The question is asked only of what the program reaches, so a freestanding program that builds an Address and never makes a socket is asked nothing.

What is deliberately absent

Multicast, broadcast, Unix datagram sockets, Linux’s abstract namespace, and every socket option beyond the two timeouts, the two buffer sizes, non_blocking, reuse_address (here because a listener that has just been stopped cannot otherwise be started again until TIME_WAIT runs out — a minute or two, during which the program looks broken), reuse_port, no_delay, v6_only and keepalive.

All of them are additive, so leaving them out costs a later release nothing, and guessing at them now would fix a shape before anybody has used one. Each would be a hook in sysl.net.sys and a call here, which is the shape everything above already has.

Search

Esc
↑↓ to navigate ↵ to open Esc to close