sysl

Module

sysl.net

Sockets – a TCP stream, a UDP socket, a Unix domain stream – and the names a host resolves to.

Import import sysl.net
Requires
requires { os }

Everything here goes through sysl.net.sys and through nothing else. On a hosted POSIX target the library answers those hooks over the platform’s sockets; on a target with no C library – a kernel, a board with a network stack of its own – the program answers them, and one it reaches and leaves unanswered is refused when it is compiled. So the module names no C function and no sockaddr: an Address is a sysl value, four or sixteen bytes and a port, built with ipv4 or ipv6 or handed back by resolve.

The vocabulary is still the sockets one – socket, bind, listen, accept, connect, send, recv, shutdown, close – because forty years of programs and of documentation are written in it, and a reader who knows it should not have to translate.

Blocking, unless asked

Every call here returns when its work is done. There is no event loop and no readiness notification in this module, and that is the line Rust draws too – std::net is blocking and every async runtime is a package outside it. sysl-lang/libuv is where the event-loop story lives. Readiness over many sockets at once is sysl.io.poll: register each socket’s fd(), set non-blocking, and one thread learns which of thousands would not block.

A timeout is the one thing a blocking call cannot do without, because otherwise “returns when the work is done” can mean never. read_timeout and write_timeout are what bound it, and a call that hits one answers an error timed_out recognises.

non_blocking is for a program that already has a loop of its own – a frame, a tick – and polls a socket once per turn: a call that would have had to wait answers an error would_block recognises instead, as Rust’s set_nonblocking does.

Unix domain sockets

unix(path) is an address, unix_listen makes the socket file and listens on it, and unix_connect answers a Socket, so a connection between two processes on one machine is read and written exactly as a TCP one is.

What is deliberately out

Multicast, broadcast, Unix datagram sockets and Linux’s abstract namespace, and every socket option beyond the timeouts, non-blocking mode and the one a listener needs. All of them are additive, so leaving them out costs a later release nothing.

Index

max_unix_path ipv4 ipv6 peer_closed resolve resolve_passive socket timed_out udp_socket unix unix_connect unix_listen would_block Address Family PeerCredentials Shutdown Socket UdpSocket UnixAddress UnixListener Display for Address Display for UnixAddress

Constants

max_unix_path

const max_unix_path: usize = 103

Functions

ipv4

ipv4(a: u8, b: u8, c: u8, d: u8, port: u16) -> Address

The IPv4 address a.b.c.d at port. ipv4(127, 0, 0, 1, 8080) is the loopback, and ipv4(0, 0, 0, 0, port) the wildcard a listener binds to.

ipv6

ipv6(groups: [8]u16, port: u16, scope: u32 = 0) -> Address

The IPv6 address of eight 16-bit groups, as written left to right, at port – ::1 is [0, 0, 0, 0, 0, 0, 0, 1]. scope is the interface a link-local address belongs to, and zero for none.

peer_closed

peer_closed(e: IoError) -> bool

Whether an error is a send to a peer that has gone: EPIPE (32 on every hosted platform), or ECONNRESET (54 on a BSD, 104 elsewhere) when the peer reset rather than closed. Such a send answers this error and never raises SIGPIPE, so the process is not ended by it.

resolve

resolve(host: string, port: int) -> Result[Buf[Address], IoError]

Every address a host and a port resolve to, in the order the machine ranked them.

Both protocol versions are asked for and both 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. So the ordinary client is a loop:

for a in resolve("example.com", 80)?
    var s = socket(a)?
    if s.connect(a).is_ok() then break
    s.close()

A failure to resolve is reported as NotFound, whatever the resolver’s own reason was. A numeric host – 127.0.0.1, ::1 – resolves to itself, and where no resolver is wanted ipv4 and ipv6 build an address directly.

resolve_passive

resolve_passive(port: int) -> Result[Buf[Address], IoError]

Where a listener binds: the wildcard, meaning every address this machine answers on.

A separate call rather than an empty host, because the same question has two answers and nothing about the arguments alone says which is wanted – a caller that got the wrong one binds to the loopback and wonders why nobody can reach it.

socket

socket(for_address: Address) -> Result[Socket, IoError]

A stream socket of the family an address belongs to, ready to be connected or bound.

The family comes from the address rather than being said separately: a socket of the wrong one cannot be connected to it, so letting the two be given apart would only make it possible to disagree.

timed_out

timed_out(e: IoError) -> bool

Whether an error is a call that ran out of its timeout rather than one that failed.

It is a function rather than a case of IoError because the alternative is adding a variant to an error type every module in the library shares, for a condition only this one can produce.

udp_socket

udp_socket(for_address: Address) -> Result[UdpSocket, IoError]

A datagram socket of the family an address belongs to, ready to be bound or connected.

unix

unix(path: string) -> Result[UnixAddress, IoError]

The Unix domain address at path, absolute or relative to the working directory.

A path the platform’s sockaddr_un cannot carry is refused here rather than at the bind: one longer than max_unix_path bytes (103 on a BSD, 107 elsewhere – the rest of sun_path is its terminator) is ENAMETOOLONG‘s code, and an empty one or one holding a zero byte is EINVAL‘s. Linux’s abstract namespace, a path starting with a zero byte, is not offered.

unix_connect

unix_connect(to: UnixAddress) -> Result[Socket, IoError]

A stream connected to the Unix domain socket at to. Nothing listening there is NotFound when there is no file, and refused (ECONNREFUSED) when the file is a socket nobody is listening on.

unix_listen

unix_listen(at: UnixAddress, backlog: int = 128) -> Result[UnixListener, IoError]

Make the socket file at at and listen on it, with backlog connections allowed to queue.

A path that already holds something is refused – EADDRINUSE‘s code, 48 on a BSD and 98 under Linux – rather than replaced, 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 first with sysl.fs.remove_file.

would_block

would_block(e: IoError) -> bool

Whether an error is a call on a non-blocking socket that would have had to wait – an accept with nobody connecting, a recv with nothing arrived, a send with the buffer full – rather than one that failed. It is EAGAIN‘s code, 35 on a BSD and 11 elsewhere, and only a socket in non-blocking mode answers it: a blocking one whose timeout ran out answers timed_out instead.

Types

Address

struct Address
    private[net] at: Endpoint

Somewhere a socket can be connected to, bound to, or sent a datagram: an IP address and a port.

It is a sysl value, not the platform’s bytes. Four bytes and a port, or sixteen and a port, compared with == and hashed like any other value; what a sockaddr_in looks like on this machine is the hosted answer’s business and nobody else’s. Built by ipv4 and ipv6, or handed back by resolve, accept and recv_from.

MemberSignatureDescription
familyfamily(self) -> FamilyWhich protocol version this is for.
portport(self) -> intThe port.
octetsoctets(self) -> [16]u8The address’s bytes, most significant first: all sixteen of an IPv6 one, and the first four of an IPv4 one with the rest zero.
with_portwith_port(self, port: u16) -> AddressThe same address at another port – where a reply goes, or a second service on one host.
texttext(self) -> stringThe numeric form, as a person writes it – 127.0.0.1, or ::1, or fe80::1%4 with the interface an IPv6 address is scoped to.

Family

enum Family
    Ipv4
    Ipv6

Which version of the protocol an address is for.

PeerCredentials

struct PeerCredentials
    uid: u32
    gid: u32
    pid: Option[u32]

The process at the other end of a Unix domain connection, as Socket.peer_credentials reports it. uid and gid are the ids sysl.process.user and group answer, so a peer is compared with == user(); pid is None where the system does not say.

Shutdown

enum Shutdown
    Read
    Write
    Both

How much of a connection is being given up.

Socket

struct Socket
    private[net] inner: &sync SocketState

A connection, or something listening for one. One type, because the sockets API has one.

It closes itself: when the last copy of a socket goes – out of scope, out of a Buf, with the struct that held it – its descriptor is closed, as Rust’s TcpStream does. close is still there for a program that wants to give it back at a moment of its own, or to read whether that worked, and defer s.close() remains correct without being needed. Every copy shares one descriptor: closing one closes them all, a second close answers NotOpen and closes nothing, and the descriptor stays open while any copy lives. into_raw_fd takes the descriptor away from the socket altogether.

Every member reaches the descriptor through its atomic state and writes nothing else, so a socket reached through a &sync is used through its members like a lock is: @unchecked(Share) says so.

MemberSignatureDescription
connectconnect(self, to: Address) -> Result[unit, IoError]Connect to somewhere.
bindbind(self, to: Address) -> Result[unit, IoError]Take a local address, which is what a listener does before it listens.
listenlisten(self, backlog: int = 128) -> Result[unit, IoError]Start accepting connections, with backlog of them allowed to queue.
acceptaccept(self) -> Result[(Socket, Address), IoError]Wait for a connection, and answer it together with where it came from.
locallocal(self) -> Result[Address, IoError]Where this socket is, which is how a listener that asked for port 0 learns which port it got.
peer_credentialspeer_credentials(self) -> Result[PeerCredentials, IoError]Who is at the other end of this Unix domain connection: the user and group the peer process had, and its process id where the system gives it.
sendsend(self, bytes: []const u8) -> Result[usize, IoError]Write some of bytes, and say how many.
send_allsend_all(self, bytes: []const u8) -> Result[unit, IoError]Every byte of bytes, however many calls that takes.
recvrecv(self, into: []u8) -> Result[usize, IoError]Read into into, and say how many bytes arrived.
shutdownshutdown(self, how: Shutdown = Both) -> Result[unit, IoError]Stop reading, stop writing, or stop both – while the descriptor stays open.
closeclose(self) -> Result[unit, IoError]Give the descriptor back now, rather than when the last copy goes, and say whether that worked.
fdfd(self) -> Result[int, IoError]The descriptor, lent: the socket still closes it, explicitly or when its last copy goes, so a number kept past that names nothing.
into_raw_fdinto_raw_fd(self) -> Result[int, IoError]The descriptor, given rather than lent: the socket lets go of it and closing it is from now on the caller’s to do – Rust’s into_raw_fd.
read_timeoutread_timeout(self, ms: int) -> Result[unit, IoError]How long a recv may wait before giving up, in milliseconds.
write_timeoutwrite_timeout(self, ms: int) -> Result[unit, IoError]How long a send may wait before giving up, in milliseconds.
reuse_addressreuse_address(self, on: bool = true) -> Result[unit, IoError]Let a listener take a port that a recently stopped one was using, so that a server just stopped can be started again without waiting out TIME_WAIT.
non_blockingnon_blocking(self, on: bool = true) -> Result[unit, IoError]Put this socket in non-blocking mode, or take it out again.
set_send_bufferset_send_buffer(self, bytes: int) -> Result[unit, IoError]Ask the kernel for a send buffer of bytes.
set_recv_bufferset_recv_buffer(self, bytes: int) -> Result[unit, IoError]Ask the kernel for a receive buffer of bytes; see set_send_buffer.
send_buffersend_buffer(self) -> Result[int, IoError]The send buffer the kernel gave this socket, in bytes.
recv_bufferrecv_buffer(self) -> Result[int, IoError]The receive buffer the kernel gave this socket, in bytes.
send_vectoredsend_vectored(self, parts: []const []const u8) -> Result[usize, IoError]Write the pieces of parts as one gather write – writev‘s job, one system call and no copying them into a buffer first – and say how many bytes went in all.
send_filesend_file(self, file: File, offset: u64, count: usize) -> Result[usize, IoError]Send up to count bytes of file, starting offset bytes in, and say how many went – the bytes go from the file to the socket in the kernel (sendfile), never through the program.
set_no_delayset_no_delay(self, on: bool = true) -> Result[unit, IoError]Send small writes at once (TCP_NODELAY) rather than waiting to coalesce them – what a request/response protocol wants.
no_delayno_delay(self) -> Result[bool, IoError]Whether set_no_delay is on.
set_reuse_portset_reuse_port(self, on: bool = true) -> Result[unit, IoError]Let several sockets bind one port (SO_REUSEPORT), the kernel spreading incoming connections among them – how a server runs one listener per thread or process.
reuse_portreuse_port(self) -> Result[bool, IoError]Whether set_reuse_port is on.
set_v6_onlyset_v6_only(self, on: bool = true) -> Result[unit, IoError]Make an IPv6 socket take IPv6 only (IPV6_V6ONLY).
v6_onlyv6_only(self) -> Result[bool, IoError]Whether set_v6_only is on.
set_keepaliveset_keepalive(self, on: bool = true) -> Result[unit, IoError]Probe an idle connection (SO_KEEPALIVE), so one whose peer vanished without a word is found out rather than held for ever.
keepalivekeepalive(self) -> Result[bool, IoError]Whether set_keepalive is on.
set_keepalive_idleset_keepalive_idle(self, seconds: int) -> Result[unit, IoError]How many seconds a connection idles before the first keepalive probe (TCP_KEEPIDLE, which Darwin spells TCP_KEEPALIVE).
keepalive_idlekeepalive_idle(self) -> Result[int, IoError]The idle seconds before the first probe.
set_keepalive_intervalset_keepalive_interval(self, seconds: int) -> Result[unit, IoError]The seconds between keepalive probes (TCP_KEEPINTVL).
keepalive_intervalkeepalive_interval(self) -> Result[int, IoError]The seconds between probes.
set_keepalive_countset_keepalive_count(self, probes: int) -> Result[unit, IoError]How many unanswered keepalive probes (TCP_KEEPCNT) end the connection.
keepalive_countkeepalive_count(self) -> Result[int, IoError]The number of probes.

UdpSocket

struct UdpSocket
    private inner: &sync SocketState

A datagram socket: UDP. Each send_to is one datagram and each recv_from takes one whole, with who sent it.

A datagram is not a stream. One that is longer than the buffer it is received into loses the rest, one sent may never arrive, and two may arrive in either order. What UDP is for is exactly the exchanges that can live with that – a query and its answer, a heartbeat, a sample – and the calls here say nothing more than that.

It closes itself when its last copy goes, exactly as Socket does, and is shared as Socket is.

MemberSignatureDescription
bindbind(self, to: Address) -> Result[unit, IoError]Take a local address: the port this socket receives on.
connectconnect(self, to: Address) -> Result[unit, IoError]Fix where this socket talks to: send then goes there without naming it, and recv takes datagrams from there and from nowhere else.
send_tosend_to(self, bytes: []const u8, to: Address) -> Result[usize, IoError]Send bytes as one datagram to to, and say how many went – all of them, or an error.
recv_fromrecv_from(self, into: []u8) -> Result[(usize, Address), IoError]Wait for one datagram, read as much of it as into holds, and answer how many bytes that was together with who sent it.
sendsend(self, bytes: []const u8) -> Result[usize, IoError]Send bytes as one datagram to where connect pointed this socket.
recvrecv(self, into: []u8) -> Result[usize, IoError]Wait for one datagram from where connect pointed this socket, and answer how many of its bytes into held.
locallocal(self) -> Result[Address, IoError]Where this socket is bound.
read_timeoutread_timeout(self, ms: int) -> Result[unit, IoError]How long a receive may wait before giving up, in milliseconds.
write_timeoutwrite_timeout(self, ms: int) -> Result[unit, IoError]How long a send may wait before giving up, in milliseconds.
non_blockingnon_blocking(self, on: bool = true) -> Result[unit, IoError]Put this socket in non-blocking mode, or take it out again: a receive with no datagram waiting then answers an error would_block recognises instead of waiting.
set_send_bufferset_send_buffer(self, bytes: int) -> Result[unit, IoError]Ask the kernel for a send buffer of bytes; see Socket.set_send_buffer.
set_recv_bufferset_recv_buffer(self, bytes: int) -> Result[unit, IoError]Ask the kernel for a receive buffer of bytes; see Socket.set_send_buffer.
send_buffersend_buffer(self) -> Result[int, IoError]The send buffer the kernel gave this socket, in bytes.
recv_bufferrecv_buffer(self) -> Result[int, IoError]The receive buffer the kernel gave this socket, in bytes.
set_reuse_portset_reuse_port(self, on: bool = true) -> Result[unit, IoError]Let several datagram sockets bind one port; see Socket.set_reuse_port.
reuse_portreuse_port(self) -> Result[bool, IoError]Whether set_reuse_port is on.
set_v6_onlyset_v6_only(self, on: bool = true) -> Result[unit, IoError]Make an IPv6 datagram socket take IPv6 only (IPV6_V6ONLY).
v6_onlyv6_only(self) -> Result[bool, IoError]Whether set_v6_only is on.
closeclose(self) -> Result[unit, IoError]Give the descriptor back now, closing every copy; a second close answers NotOpen and closes nothing.
fdfd(self) -> Result[int, IoError]The descriptor, lent; see Socket.fd.
into_raw_fdinto_raw_fd(self) -> Result[int, IoError]The descriptor, given rather than lent; see Socket.into_raw_fd.

UnixAddress

struct UnixAddress
    private at: string

Where a Unix domain socket is: a path in the filesystem, which unix checks the platform’s address can carry.

The socket file it names is made by unix_listen and is not removed by anything here – sysl.fs.remove_file is the call, before listening again at the same path and after the listener is closed. Who may connect is the file’s permission bits, which sysl.fs.set_permissions sets.

MemberSignatureDescription
pathpath(self) -> stringThe path, exactly as it was given.

UnixListener

struct UnixListener
    private inner: &sync SocketState

A Unix domain socket listening for connections. It closes itself when its last copy goes, exactly as Socket does; the socket file stays where it is either way. It is shared as Socket is.

MemberSignatureDescription
acceptaccept(*self) -> Result[Socket, IoError]Wait for a connection, and answer it.
non_blockingnon_blocking(*self, on: bool = true) -> Result[unit, IoError]Put this listener in non-blocking mode, or take it out again.
closeclose(*self) -> Result[unit, IoError]Give the descriptor back now, closing every copy; a second close answers NotOpen and closes nothing.
fdfd(self) -> Result[int, IoError]The descriptor, lent; see Socket.fd.
into_raw_fdinto_raw_fd(*self) -> Result[int, IoError]The descriptor, given rather than lent; see Socket.into_raw_fd.

Implementations

Display for Address

impl Display for Address

So that printing one says where it is rather than how it is stored: 127.0.0.1:8080, and [::1]:8080 – the brackets are not decoration, a v6 address having colons in it, so ::1:8080 cannot be read apart. It is the form the URL syntax settled on.

Display for UnixAddress

impl Display for UnixAddress

So that printing one prints the path.

Search

Esc
↑↓ to navigate ↵ to open Esc to close