rokdocs
v0.1

rok::std::net

rok::std::net (stdlib 302): TCP / UDP / Unix sockets, addresses, DNS.

Sockets are non-blocking underneath. Blocking calls wait in poll, so set_*_timeout bounds that wait (async uses task::timeout), and set_nonblocking(true) makes them throw WouldBlock. *_async calls suspend until the I/O driver sees the fd ready (refs-lang 7.7).

Spec 302 lets the plain calls suspend inside a task; here they block the worker, because a sync fn cannot suspend a stackless coroutine.

read(buf) fills buf[0..n] of a pre-sized List<u8>; writes take any AsBytes. A Unix listener's accept gives the peer's bound path (none: unnamed). sendfile / splice are Linux only; no IPv6 multicast or MSG_ZEROCOPY (302.7). TLS 1.3 is tls (302.5).

Modules

  • tls rok::std::net::tls - TLS 1.3 (stdlib spec 3.2.5), implemented here in rok on `rok::core::crypto`: no OS or external TLS library.

Structs

Ipv4Addr

pub struct Ipv4Addr
  • pub octets: [u8; 4]
  • pub const LOCALHOST: Ipv4Addr = Ipv4Addr { octets: [127u8, 0u8, 0u8, 1u8] }
  • pub const UNSPECIFIED: Ipv4Addr = Ipv4Addr { octets: [0u8, 0u8, 0u8, 0u8] }
  • pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr
  • pub const fn to_bits(self) -> u32

    The address as a big-endian number (a.b.c.d = a << 24 | ..).

  • pub const fn is_loopback(self) -> bool
  • pub const fn is_unspecified(self) -> bool
  • pub const fn is_multicast(self) -> bool
Trait implementations 4
  • impl PartialEq for Ipv4Addr
  • impl Display for Ipv4Addr
  • impl Debug for Ipv4Addr
  • impl FromStr for Ipv4Addr

Ipv6Addr

pub struct Ipv6Addr
  • pub segments: [u16; 8]
  • pub const LOCALHOST: Ipv6Addr = Ipv6Addr { segments: [0u16, 0u16, 0u16, 0u16, 0u16, 0u16, 0u16, 1u16] }
  • pub const UNSPECIFIED: Ipv6Addr = Ipv6Addr { segments: [0u16, 0u16, 0u16, 0u16, 0u16, 0u16, 0u16, 0u16] }
  • pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> Ipv6Addr
  • pub const fn is_loopback(self) -> bool
Trait implementations 4
  • impl PartialEq for Ipv6Addr
  • impl Display for Ipv6Addr
  • impl Debug for Ipv6Addr
  • impl FromStr for Ipv6Addr

SocketAddrV4

pub struct SocketAddrV4
  • pub ip: Ipv4Addr
  • pub port: u16

SocketAddrV6

pub struct SocketAddrV6
  • pub ip: Ipv6Addr
  • pub port: u16
  • pub flow_info: u32
  • pub scope_id: u32

TcpStream

pub struct TcpStream

A TCP connection. Copies share the socket; close() (or leaving a using block) closes it for all.

  • pub fn connect(addr: SocketAddr) throws (IoError) -> TcpStream

    Connect, blocking until the handshake is done.

  • pub fn connect_timeout(addr: SocketAddr, dur: Duration) throws (IoError) -> TcpStream
  • pub async fn connect_async(addr: SocketAddr) throws (IoError) -> TcpStream

    connect that suspends the task during the handshake.

  • pub const fn fd(self) -> i32

    The OS socket descriptor (-1 once closed).

  • pub fn read(self, buf: List<u8>) throws (IoError) -> usize

    Read up to buf.len() bytes into buf[0..n]; 0 at end of stream.

  • pub async fn read_async(self, buf: List<u8>) throws (IoError) -> usize

    read that suspends the task until data arrives.

  • pub fn read_to_end(self) throws (IoError) -> List<u8>

    Everything until the peer closes its side.

  • pub async fn read_to_end_async(self) throws (IoError) -> List<u8>
  • pub fn write<B: AsBytes>(self, buf: B) throws (IoError) -> usize

    Write some prefix of buf; its length.

  • pub async fn write_async<B: AsBytes>(self, buf: B) throws (IoError) -> usize
  • pub fn write_all<B: AsBytes>(self, buf: B) throws (IoError)
  • pub async fn write_all_async<B: AsBytes>(self, buf: B) throws (IoError)
  • pub fn write_str(self, s: str) throws (IoError)
  • pub async fn write_str_async(self, s: str) throws (IoError)
  • pub const fn flush(self) throws (IoError)

    Nothing to flush: writes go straight to the socket.

  • pub fn peer_addr(self) throws (IoError) -> SocketAddr
  • pub fn local_addr(self) throws (IoError) -> SocketAddr
  • pub fn shutdown(self, how: Shutdown) throws (IoError)
  • pub fn set_nodelay(self, nodelay: bool) throws (IoError)
  • pub fn set_keepalive(self, alive: Duration | none) throws (IoError)

    Keepalive probes after alive idle (whole seconds); none = off.

  • pub const fn set_read_timeout(self, dur: Duration | none) throws (IoError)
  • pub const fn set_write_timeout(self, dur: Duration | none) throws (IoError)
  • pub const fn read_timeout(self) -> Duration | none
  • pub const fn write_timeout(self) -> Duration | none
  • pub const fn set_nonblocking(self, nb: bool) throws (IoError)

    Make the blocking calls throw WouldBlock instead of waiting.

  • pub fn try_clone(self) throws (IoError) -> TcpStream
  • pub fn close(self)

    Close now; later calls fail with InvalidInput.

  • pub fn sendfile(self, ref file: File, offset: u64, count: u64) throws (IoError) -> u64

    file[offset..offset + count] to the socket in the kernel (sendfile(2)): the bytes sent, fewer if the file ends first.

  • pub fn splice(self, ref src: File, count: u64) throws (IoError) -> u64

    Up to count bytes from src's offset to the socket through a kernel pipe (splice(2)): the bytes moved, fewer if src ends.

  • pub fn write_zerocopy(self, buf: List<u8>) throws (IoError) -> usize

    Some prefix of buf sent with MSG_ZEROCOPY (Linux 4.14+): the kernel reads buf in place; returns once it no longer needs it.

Trait implementations 3
  • impl Reader for TcpStream
  • impl Writer for TcpStream
  • impl Drop for TcpStream

TcpListener

pub struct TcpListener

A listening TCP socket.

  • pub fn bind(addr: SocketAddr) throws (IoError) -> TcpListener

    Bind and listen (SO_REUSEADDR, backlog 128). Port 0 picks one: see local_addr.

  • pub fn inherited() -> List<TcpListener>

    The listening sockets the host handed this program, in order: WASI's pre-opened sockets (wasmtime -S tcplisten=ADDR, preview 1: wasm32-wasi-threads), systemd socket activation (LISTEN_FDS) on Unix; none on Windows. Where a program cannot bind sockets itself, these are how it serves.

  • pub fn accept(self) throws (IoError) -> (TcpStream, SocketAddr)

    The next connection and its peer, blocking until one arrives.

  • pub async fn accept_async(self) throws (IoError) -> (TcpStream, SocketAddr)

    accept that suspends the task until a connection arrives.

  • pub fn local_addr(self) throws (IoError) -> SocketAddr
  • pub fn try_clone(self) throws (IoError) -> TcpListener
  • pub const fn set_nonblocking(self, nb: bool) throws (IoError)
  • pub const fn incoming(self) -> Incoming

    for c in l.incoming() { let s = try c.get(); .. }: connections forever (each item is the stream or the error accept threw).

  • pub const fn fd(self) -> i32
  • pub fn close(self)
Trait implementations 1
  • impl Drop for TcpListener

Incoming

pub struct Incoming

TcpListener::incoming.

Trait implementations 1
  • impl Iterator for Incoming

UdpSocket

pub struct UdpSocket

A UDP socket. Copies share the socket.

  • pub fn bind(addr: SocketAddr) throws (IoError) -> UdpSocket
  • pub fn send_to<B: AsBytes>(self, buf: B, addr: SocketAddr) throws (IoError) -> usize

    Send the datagram buf to addr; bytes sent.

  • pub async fn send_to_async<B: AsBytes>(self, buf: B, addr: SocketAddr) throws (IoError) -> usize
  • pub fn recv_from(self, buf: List<u8>) throws (IoError) -> (usize, SocketAddr)

    The next datagram into buf[0..n] (truncated to buf.len()) and its sender.

  • pub async fn recv_from_async(self, buf: List<u8>) throws (IoError) -> (usize, SocketAddr)
  • pub fn connect(self, addr: SocketAddr) throws (IoError)

    Fix the peer: send / recv then talk to it only.

  • pub fn send<B: AsBytes>(self, buf: B) throws (IoError) -> usize
  • pub async fn send_async<B: AsBytes>(self, buf: B) throws (IoError) -> usize
  • pub fn recv(self, buf: List<u8>) throws (IoError) -> usize
  • pub async fn recv_async(self, buf: List<u8>) throws (IoError) -> usize
  • pub fn local_addr(self) throws (IoError) -> SocketAddr
  • pub fn peer_addr(self) throws (IoError) -> SocketAddr
  • pub fn set_broadcast(self, on: bool) throws (IoError)
  • pub fn join_multicast_v4(self, addr: Ipv4Addr, iface: Ipv4Addr) throws (IoError)
  • pub fn leave_multicast_v4(self, addr: Ipv4Addr, iface: Ipv4Addr) throws (IoError)
  • pub fn set_multicast_loop_v4(self, on: bool) throws (IoError)
  • pub fn set_multicast_ttl_v4(self, ttl: u32) throws (IoError)
  • pub fn set_segment_size(self, size: u16) throws (IoError)

    UDP_SEGMENT (GSO); Unsupported off Linux.

  • pub const fn set_read_timeout(self, dur: Duration | none) throws (IoError)
  • pub const fn set_write_timeout(self, dur: Duration | none) throws (IoError)
  • pub const fn set_nonblocking(self, nb: bool) throws (IoError)
  • pub fn try_clone(self) throws (IoError) -> UdpSocket
  • pub const fn fd(self) -> i32
  • pub fn close(self)
Trait implementations 1
  • impl Drop for UdpSocket

UnixStream

pub struct UnixStream

A Unix stream socket. Copies share the socket; close() (or leaving a using block) closes it for all.

  • pub fn connect<P: AsPath>(path: P) throws (IoError) -> UnixStream
  • pub fn pair() throws (IoError) -> (UnixStream, UnixStream)

    Two connected ends (socketpair).

  • pub fn read(self, buf: List<u8>) throws (IoError) -> usize
  • pub async fn read_async(self, buf: List<u8>) throws (IoError) -> usize
  • pub fn read_to_end(self) throws (IoError) -> List<u8>
  • pub fn write<B: AsBytes>(self, buf: B) throws (IoError) -> usize
  • pub async fn write_async<B: AsBytes>(self, buf: B) throws (IoError) -> usize
  • pub fn write_all<B: AsBytes>(self, buf: B) throws (IoError)
  • pub fn write_str(self, s: str) throws (IoError)
  • pub const fn flush(self) throws (IoError)
  • pub fn send_with_fd<B: AsBytes>(self, buf: B, fd: RawFd) throws (IoError) -> usize

    buf with fd fd alongside (SCM_RIGHTS): the bytes sent.

  • pub fn recv_with_fd(self, buf: List<u8>) throws (IoError) -> (usize, RawFd | none)

    One read into buf and the fd passed with it, if any (the caller owns it; it is close-on-exec).

  • pub fn peer_addr(self) throws (IoError) -> Path | none
  • pub fn local_addr(self) throws (IoError) -> Path | none
  • pub fn shutdown(self, how: Shutdown) throws (IoError)
  • pub const fn set_read_timeout(self, dur: Duration | none) throws (IoError)
  • pub const fn set_write_timeout(self, dur: Duration | none) throws (IoError)
  • pub const fn set_nonblocking(self, nb: bool) throws (IoError)
  • pub fn try_clone(self) throws (IoError) -> UnixStream
  • pub const fn fd(self) -> i32
  • pub fn close(self)
Trait implementations 3
  • impl Reader for UnixStream
  • impl Writer for UnixStream
  • impl Drop for UnixStream

UnixListener

pub struct UnixListener

A listening Unix stream socket bound to a path.

  • pub fn bind<P: AsPath>(path: P) throws (IoError) -> UnixListener

    Bind path (it must not exist yet) and listen (backlog 128).

  • pub fn accept(self) throws (IoError) -> (UnixStream, Path | none)

    The next connection and the path its peer is bound to (none: unnamed, the usual case), blocking until one arrives.

  • pub async fn accept_async(self) throws (IoError) -> (UnixStream, Path | none)
  • pub fn local_addr(self) throws (IoError) -> Path | none
  • pub const fn set_nonblocking(self, nb: bool) throws (IoError)
  • pub fn try_clone(self) throws (IoError) -> UnixListener
  • pub const fn fd(self) -> i32
  • pub fn close(self)
Trait implementations 1
  • impl Drop for UnixListener

UnixDatagram

pub struct UnixDatagram

A Unix datagram socket.

  • pub fn bind<P: AsPath>(path: P) throws (IoError) -> UnixDatagram

    A datagram socket bound to path.

  • pub fn connect<P: AsPath>(path: P) throws (IoError) -> UnixDatagram

    A datagram socket connected to path (unbound itself).

  • pub fn pair() throws (IoError) -> (UnixDatagram, UnixDatagram)

    Two connected datagram ends (socketpair).

  • pub fn send<B: AsBytes>(self, buf: B) throws (IoError) -> usize
  • pub fn recv(self, buf: List<u8>) throws (IoError) -> usize
  • pub fn send_with_fd<B: AsBytes>(self, buf: B, fd: RawFd) throws (IoError) -> usize
  • pub fn recv_with_fd(self, buf: List<u8>) throws (IoError) -> (usize, RawFd | none)
  • pub fn local_addr(self) throws (IoError) -> Path | none
  • pub const fn set_read_timeout(self, dur: Duration | none) throws (IoError)
  • pub const fn set_write_timeout(self, dur: Duration | none) throws (IoError)
  • pub const fn set_nonblocking(self, nb: bool) throws (IoError)
  • pub const fn fd(self) -> i32
  • pub fn close(self)
Trait implementations 1
  • impl Drop for UnixDatagram

Enums

SocketAddr

pub enum SocketAddr
  • V4(SocketAddrV4)
  • V6(SocketAddrV6)
  • pub const fn v4(ip: Ipv4Addr, port: u16) -> SocketAddr
  • pub const fn v6(ip: Ipv6Addr, port: u16) -> SocketAddr
  • pub const fn port(self) -> u16
  • pub const fn with_port(self, port: u16) -> SocketAddr
  • pub const fn is_ipv4(self) -> bool
  • pub const fn is_ipv6(self) -> bool
Trait implementations 4
  • impl PartialEq for SocketAddr
  • impl Display for SocketAddr
  • impl Debug for SocketAddr
  • impl FromStr for SocketAddr

Shutdown

pub enum Shutdown
  • Read
  • Write
  • Both

Functions

resolve

pub fn resolve(host: str) throws (IoError) -> List<SocketAddr>

Addresses of host: "name:port", "[v6]:port", a socket address, or a bare name / IP (port 0).

resolve_one

pub fn resolve_one(host: str, port: u16) throws (IoError) -> SocketAddr

The first address of host with port.

tcp_connect

pub fn tcp_connect(addr: str | SocketAddr) throws (IoError) -> TcpStream

Connect to the first address of addr that accepts (names are resolved).

tcp_connect_async

pub async fn tcp_connect_async(addr: str | SocketAddr) throws (IoError) -> TcpStream

tcp_connect that suspends the task while connecting.

tcp_listen

pub fn tcp_listen(addr: str | SocketAddr) throws (IoError) -> TcpListener

udp_bind

pub fn udp_bind(addr: str | SocketAddr) throws (IoError) -> UdpSocket