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
tlsrok::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 Ipv4Addrpub 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) -> Ipv4Addrpub const fn to_bits(self) -> u32The address as a big-endian number (
a.b.c.d=a << 24 | ..).pub const fn is_loopback(self) -> boolpub const fn is_unspecified(self) -> boolpub const fn is_multicast(self) -> bool
Trait implementations 4
impl PartialEq for Ipv4Addrimpl Display for Ipv4Addrimpl Debug for Ipv4Addrimpl FromStr for Ipv4Addr
Ipv6Addr
pub struct Ipv6Addrpub 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) -> Ipv6Addrpub const fn is_loopback(self) -> bool
Trait implementations 4
impl PartialEq for Ipv6Addrimpl Display for Ipv6Addrimpl Debug for Ipv6Addrimpl FromStr for Ipv6Addr
SocketAddrV4
pub struct SocketAddrV4pub ip: Ipv4Addrpub port: u16
SocketAddrV6
pub struct SocketAddrV6pub ip: Ipv6Addrpub port: u16pub flow_info: u32pub scope_id: u32
TcpStream
pub struct TcpStreamA TCP connection. Copies share the socket; close() (or leaving a
using block) closes it for all.
pub fn connect(addr: SocketAddr) throws (IoError) -> TcpStreamConnect, blocking until the handshake is done.
pub fn connect_timeout(addr: SocketAddr, dur: Duration) throws (IoError) -> TcpStreampub async fn connect_async(addr: SocketAddr) throws (IoError) -> TcpStreamconnectthat suspends the task during the handshake.pub const fn fd(self) -> i32The OS socket descriptor (
-1once closed).pub fn read(self, buf: List<u8>) throws (IoError) -> usizeRead up to
buf.len()bytes intobuf[0..n]; 0 at end of stream.pub async fn read_async(self, buf: List<u8>) throws (IoError) -> usizereadthat 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) -> usizeWrite some prefix of
buf; its length.pub async fn write_async<B: AsBytes>(self, buf: B) throws (IoError) -> usizepub 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) -> SocketAddrpub fn local_addr(self) throws (IoError) -> SocketAddrpub 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
aliveidle (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 | nonepub const fn write_timeout(self) -> Duration | nonepub const fn set_nonblocking(self, nb: bool) throws (IoError)Make the blocking calls throw
WouldBlockinstead of waiting.pub fn try_clone(self) throws (IoError) -> TcpStreampub fn close(self)Close now; later calls fail with
InvalidInput.pub fn sendfile(self, ref file: File, offset: u64, count: u64) throws (IoError) -> u64file[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) -> u64Up to
countbytes fromsrc's offset to the socket through a kernel pipe (splice(2)): the bytes moved, fewer ifsrcends.pub fn write_zerocopy(self, buf: List<u8>) throws (IoError) -> usizeSome prefix of
bufsent withMSG_ZEROCOPY(Linux 4.14+): the kernel readsbufin place; returns once it no longer needs it.
Trait implementations 3
impl Reader for TcpStreamimpl Writer for TcpStreamimpl Drop for TcpStream
TcpListener
pub struct TcpListenerA listening TCP socket.
pub fn bind(addr: SocketAddr) throws (IoError) -> TcpListenerBind and listen (
SO_REUSEADDR, backlog 128). Port 0 picks one: seelocal_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)acceptthat suspends the task until a connection arrives.pub fn local_addr(self) throws (IoError) -> SocketAddrpub fn try_clone(self) throws (IoError) -> TcpListenerpub const fn set_nonblocking(self, nb: bool) throws (IoError)pub const fn incoming(self) -> Incomingfor c in l.incoming() { let s = try c.get(); .. }: connections forever (each item is the stream or the erroracceptthrew).pub const fn fd(self) -> i32pub fn close(self)
Trait implementations 1
impl Drop for TcpListener
Incoming
pub struct IncomingTcpListener::incoming.
Trait implementations 1
impl Iterator for Incoming
UdpSocket
pub struct UdpSocketA UDP socket. Copies share the socket.
pub fn bind(addr: SocketAddr) throws (IoError) -> UdpSocketpub fn send_to<B: AsBytes>(self, buf: B, addr: SocketAddr) throws (IoError) -> usizeSend the datagram
buftoaddr; bytes sent.pub async fn send_to_async<B: AsBytes>(self, buf: B, addr: SocketAddr) throws (IoError) -> usizepub fn recv_from(self, buf: List<u8>) throws (IoError) -> (usize, SocketAddr)The next datagram into
buf[0..n](truncated tobuf.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/recvthen talk to it only.pub fn send<B: AsBytes>(self, buf: B) throws (IoError) -> usizepub async fn send_async<B: AsBytes>(self, buf: B) throws (IoError) -> usizepub fn recv(self, buf: List<u8>) throws (IoError) -> usizepub async fn recv_async(self, buf: List<u8>) throws (IoError) -> usizepub fn local_addr(self) throws (IoError) -> SocketAddrpub fn peer_addr(self) throws (IoError) -> SocketAddrpub 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);Unsupportedoff 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) -> UdpSocketpub const fn fd(self) -> i32pub fn close(self)
Trait implementations 1
impl Drop for UdpSocket
UnixStream
pub struct UnixStreamA 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) -> UnixStreampub fn pair() throws (IoError) -> (UnixStream, UnixStream)Two connected ends (
socketpair).pub fn read(self, buf: List<u8>) throws (IoError) -> usizepub async fn read_async(self, buf: List<u8>) throws (IoError) -> usizepub fn read_to_end(self) throws (IoError) -> List<u8>pub fn write<B: AsBytes>(self, buf: B) throws (IoError) -> usizepub async fn write_async<B: AsBytes>(self, buf: B) throws (IoError) -> usizepub 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) -> usizebufwith fdfdalongside (SCM_RIGHTS): the bytes sent.pub fn recv_with_fd(self, buf: List<u8>) throws (IoError) -> (usize, RawFd | none)One read into
bufand the fd passed with it, if any (the caller owns it; it is close-on-exec).pub fn peer_addr(self) throws (IoError) -> Path | nonepub fn local_addr(self) throws (IoError) -> Path | nonepub 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) -> UnixStreampub const fn fd(self) -> i32pub fn close(self)
Trait implementations 3
impl Reader for UnixStreamimpl Writer for UnixStreamimpl Drop for UnixStream
UnixListener
pub struct UnixListenerA listening Unix stream socket bound to a path.
pub fn bind<P: AsPath>(path: P) throws (IoError) -> UnixListenerBind
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 | nonepub const fn set_nonblocking(self, nb: bool) throws (IoError)pub fn try_clone(self) throws (IoError) -> UnixListenerpub const fn fd(self) -> i32pub fn close(self)
Trait implementations 1
impl Drop for UnixListener
UnixDatagram
pub struct UnixDatagramA Unix datagram socket.
pub fn bind<P: AsPath>(path: P) throws (IoError) -> UnixDatagramA datagram socket bound to
path.pub fn connect<P: AsPath>(path: P) throws (IoError) -> UnixDatagramA 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) -> usizepub fn recv(self, buf: List<u8>) throws (IoError) -> usizepub fn send_with_fd<B: AsBytes>(self, buf: B, fd: RawFd) throws (IoError) -> usizepub fn recv_with_fd(self, buf: List<u8>) throws (IoError) -> (usize, RawFd | none)pub fn local_addr(self) throws (IoError) -> Path | nonepub 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) -> i32pub fn close(self)
Trait implementations 1
impl Drop for UnixDatagram
Enums
SocketAddr
pub enum SocketAddrV4(SocketAddrV4)V6(SocketAddrV6)pub const fn v4(ip: Ipv4Addr, port: u16) -> SocketAddrpub const fn v6(ip: Ipv6Addr, port: u16) -> SocketAddrpub const fn port(self) -> u16pub const fn with_port(self, port: u16) -> SocketAddrpub const fn is_ipv4(self) -> boolpub const fn is_ipv6(self) -> bool
Trait implementations 4
impl PartialEq for SocketAddrimpl Display for SocketAddrimpl Debug for SocketAddrimpl FromStr for SocketAddr
Shutdown
pub enum ShutdownReadWriteBoth
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) -> SocketAddrThe first address of host with port.
tcp_connect
pub fn tcp_connect(addr: str | SocketAddr) throws (IoError) -> TcpStreamConnect 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) -> TcpStreamtcp_connect that suspends the task while connecting.
tcp_listen
pub fn tcp_listen(addr: str | SocketAddr) throws (IoError) -> TcpListenerudp_bind
pub fn udp_bind(addr: str | SocketAddr) throws (IoError) -> UdpSocket