Net
Source: std/net.mw.
Sockets are plain file descriptors held in i64. IPv4 addresses are packed into a u32; build one with ip4(a, b, c, d).
TCP and UDP over IPv4, plus a small DNS resolver. No libc: every function is built on the socket calls of the platform layer.
Addresses are u32 values in host order (a.b.c.d -> a<<24 | b<<16 | c<<8 | d). Functions returning i64 give a file descriptor or a negative errno.
Timeval
Section titled “Timeval”struct Timeval { sec: i64; usec: i64;}Functions
Section titled “Functions”fn ip4(a: i64, b: i64, c: i64, d: i64) -> u32ip4_octet
Section titled “ip4_octet”fn ip4_octet(ip: u32, index: i64) -> i64ip4_parse
Section titled “ip4_parse”fn ip4_parse(s: *u8, out: *u32) -> boolParses “a.b.c.d” (exactly four decimal parts, each 0..255).
ip4_format
Section titled “ip4_format”fn ip4_format(buf: *u8, ip: u32) -> i64“a.b.c.d” into buf (at most 15 bytes); returns the length.
net_close
Section titled “net_close”fn net_close(fd: i64)net_set_timeout_ms
Section titled “net_set_timeout_ms”fn net_set_timeout_ms(fd: i64, ms: i64) -> i64net_set_nodelay
Section titled “net_set_nodelay”fn net_set_nodelay(fd: i64) -> i64net_sockaddr
Section titled “net_sockaddr”fn net_sockaddr(buf: *u8, ip: u32, port: i64)tcp_connect_ip
Section titled “tcp_connect_ip”fn tcp_connect_ip(ip: u32, port: i64) -> i64tcp_listen_on
Section titled “tcp_listen_on”fn tcp_listen_on(ip: u32, port: i64, backlog: i64) -> i64Listens on ip:port (port 0 picks a free one; see net_local_port).
tcp_listen
Section titled “tcp_listen”fn tcp_listen(port: i64, backlog: i64) -> i64All interfaces.
tcp_accept
Section titled “tcp_accept”fn tcp_accept(fd: i64, peer: *u32) -> i64Accepts one connection. peer (may be null) receives the client address.
net_local_port
Section titled “net_local_port”fn net_local_port(fd: i64) -> i64Port a socket is bound to (host order), or a negative errno.
net_read
Section titled “net_read”fn net_read(fd: i64, buf: *u8, n: i64) -> i64Reads up to n bytes (0 means the peer closed the connection).
net_write_all
Section titled “net_write_all”fn net_write_all(fd: i64, buf: *u8, n: i64) -> i64Sends every byte. Returns 0 on success or a negative errno.
net_write_str
Section titled “net_write_str”fn net_write_str(fd: i64, s: *u8) -> i64net_shutdown_write
Section titled “net_shutdown_write”fn net_shutdown_write(fd: i64)Stops sending (the peer then reads end-of-file) but keeps reading open.
udp_socket
Section titled “udp_socket”fn udp_socket() -> i64udp_send_to
Section titled “udp_send_to”fn udp_send_to(fd: i64, ip: u32, port: i64, buf: *u8, n: i64) -> i64udp_recv
Section titled “udp_recv”fn udp_recv(fd: i64, buf: *u8, n: i64) -> i64be16_at
Section titled “be16_at”fn be16_at(buf: *u8, i: i64) -> i64dns_skip_name
Section titled “dns_skip_name”fn dns_skip_name(buf: *u8, len: i64, i: i64) -> i64Skips a (possibly compressed) domain name; returns the index after it.
dns_build_query
Section titled “dns_build_query”fn dns_build_query(buf: *u8, name: *u8, id: i64) -> i64Builds an A query for name into buf; returns its length or -1.
dns_parse_response
Section titled “dns_parse_response”fn dns_parse_response(buf: *u8, len: i64, id: i64, out: *u32) -> boolExtracts the first IPv4 address of a response to our query.
dns_nameserver
Section titled “dns_nameserver”fn dns_nameserver(out: *u32) -> boolFirst nameserver line of /etc/resolv.conf that is an IPv4 address.
dns_from_hosts
Section titled “dns_from_hosts”fn dns_from_hosts(name: *u8, out: *u32) -> boolLooks name up in /etc/hosts (IPv4 entries only).
dns_resolve
Section titled “dns_resolve”fn dns_resolve(name: *u8, out: *u32) -> boolResolves a host name or dotted address to IPv4. Tries, in order: literal address, “localhost”, /etc/hosts, then a UDP query to the first IPv4 nameserver of /etc/resolv.conf.
tcp_connect
Section titled “tcp_connect”fn tcp_connect(host: *u8, port: i64) -> i64Resolves host and connects.