Skip to content

Commit ba5fa61

Browse files
committed
Implement sock_accept
With the addition of `sock_accept()` in `wasi-0.11.0`, wasmtime can now implement basic networking for pre-opened sockets. For Windows `AsHandle` was replaced with `AsRawHandleOrSocket` to cope with the duality of Handles and Sockets. For Unix a `wasi_cap_std_sync::net::Socket` enum was created to handle the {Tcp,Unix}{Listener,Stream} more efficiently in `WasiCtxBuilder::preopened_socket()`. The addition of that many `WasiFile` implementors was mainly necessary, because of the difference in the `num_ready_bytes()` function. A known issue is Windows now busy polling on sockets, because except for `stdin`, nothing is querying the status of windows handles/sockets. Signed-off-by: Harald Hoyer <[email protected]>
1 parent 90bfa12 commit ba5fa61

File tree

18 files changed

+630
-30
lines changed

18 files changed

+630
-30
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/wasi-common/cap-std-sync/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ rustix = "0.31.0"
3131
winapi = "0.3"
3232
lazy_static = "1.4"
3333
atty = "0.2.14"
34+
io-extras = "0.12.0"
3435

3536
[dev-dependencies]
3637
tempfile = "3.1.0"

crates/wasi-common/cap-std-sync/src/file.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ impl WasiFile for File {
2525
fn as_any(&self) -> &dyn Any {
2626
self
2727
}
28+
async fn sock_accept(&mut self, _fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
29+
Err(Error::badf())
30+
}
2831
async fn datasync(&self) -> Result<(), Error> {
2932
self.0.sync_data()?;
3033
Ok(())
@@ -171,8 +174,19 @@ impl AsHandle for File {
171174
}
172175
}
173176

177+
#[cfg(windows)]
178+
use io_extras::os::windows::{AsRawHandleOrSocket, RawHandleOrSocket};
179+
#[cfg(windows)]
180+
impl AsRawHandleOrSocket for File {
181+
#[inline]
182+
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
183+
self.0.as_raw_handle_or_socket()
184+
}
185+
}
186+
174187
#[cfg(unix)]
175188
use io_lifetimes::{AsFd, BorrowedFd};
189+
176190
#[cfg(unix)]
177191
impl AsFd for File {
178192
fn as_fd(&self) -> BorrowedFd<'_> {

crates/wasi-common/cap-std-sync/src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,20 @@
3636
pub mod clocks;
3737
pub mod dir;
3838
pub mod file;
39+
pub mod net;
3940
pub mod sched;
4041
pub mod stdio;
4142

4243
pub use cap_std::ambient_authority;
4344
pub use cap_std::fs::Dir;
45+
pub use cap_std::net::TcpListener;
4446
pub use clocks::clocks_ctx;
4547
pub use sched::sched_ctx;
4648

49+
use crate::net::Socket;
4750
use cap_rand::RngCore;
4851
use std::path::Path;
49-
use wasi_common::{table::Table, Error, WasiCtx, WasiFile};
52+
use wasi_common::{file::FileCaps, table::Table, Error, WasiCtx, WasiFile};
5053

5154
pub struct WasiCtxBuilder(WasiCtx);
5255

@@ -120,6 +123,18 @@ impl WasiCtxBuilder {
120123
self.0.push_preopened_dir(dir, guest_path)?;
121124
Ok(self)
122125
}
126+
pub fn preopened_socket(mut self, fd: u32, socket: impl Into<Socket>) -> Result<Self, Error> {
127+
let socket: Socket = socket.into();
128+
let file: Box<dyn WasiFile> = socket.into();
129+
130+
let caps = FileCaps::FDSTAT_SET_FLAGS
131+
| FileCaps::FILESTAT_GET
132+
| FileCaps::READ
133+
| FileCaps::POLL_READWRITE;
134+
135+
self.0.insert_file(fd, file, caps);
136+
Ok(self)
137+
}
123138
pub fn build(self) -> WasiCtx {
124139
self.0
125140
}

0 commit comments

Comments
 (0)