wasix_net/lib.rs
1//! TCP sockets and HTTP for WASI programs in slicc's kernel.
2//!
3//! Rust std for `wasm32-wasip1` stubs `TcpStream::connect` and
4//! `TcpListener::bind`. slicc's kernel implements the WASIX socket calls
5//! (`sock_open`, `sock_connect`, `sock_bind`, `sock_listen`,
6//! `sock_accept_v2`) on its own loopback network, so on WASI preview1 this
7//! crate's [`TcpStream`] and [`TcpListener`] use them. On every other target
8//! they are std's, so code written against them needs no cfg: switch the
9//! import and keep to the API both share.
10//!
11//! [`http`] is a small blocking HTTP/1.1 client over those sockets. It has
12//! no TLS: in the realm every request leaves through the kernel's proxy
13//! (`https_proxy` / `http_proxy`), which takes absolute-form requests and
14//! does TLS itself, while hosts in `no_proxy` (the realm's loopback) are
15//! reached directly.
16
17#[cfg(all(target_os = "wasi", target_env = "p1"))]
18mod wasix;
19
20#[cfg(all(target_os = "wasi", target_env = "p1"))]
21pub use wasix::{Incoming, TcpListener, TcpStream, ToSocketAddrs};
22
23#[cfg(not(all(target_os = "wasi", target_env = "p1")))]
24pub use std::net::{Incoming, TcpListener, TcpStream, ToSocketAddrs};
25
26pub use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr, SocketAddrV4, SocketAddrV6};
27
28pub mod http;
29
30/// The addresses of `host` (a name or a literal address) at `port`.
31///
32/// In the realm only loopback names resolve (`localhost`), besides literal
33/// addresses; elsewhere this is the system resolver.
34pub fn resolve(host: &str, port: u16) -> std::io::Result<Vec<SocketAddr>> {
35 #[cfg(all(target_os = "wasi", target_env = "p1"))]
36 return wasix::resolve(host, port);
37 #[cfg(not(all(target_os = "wasi", target_env = "p1")))]
38 {
39 let host = host.trim_start_matches('[').trim_end_matches(']');
40 Ok(ToSocketAddrs::to_socket_addrs(&(host, port))?.collect())
41 }
42}