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.

Another know issue on Windows, is that there is no crate providing
support for `fcntl(fd, F_GETFL, 0)` on a socket.

Signed-off-by: Harald Hoyer <harald@profian.com>
This commit is contained in:
Harald Hoyer
2022-01-21 14:42:43 +01:00
committed by Dan Gohman
parent 8ba3294881
commit 853a025613
18 changed files with 650 additions and 30 deletions

View File

@@ -36,17 +36,20 @@
pub mod clocks;
pub mod dir;
pub mod file;
pub mod net;
pub mod sched;
pub mod stdio;
pub use cap_std::ambient_authority;
pub use cap_std::fs::Dir;
pub use cap_std::net::TcpListener;
pub use clocks::clocks_ctx;
pub use sched::sched_ctx;
use crate::net::Socket;
use cap_rand::RngCore;
use std::path::Path;
use wasi_common::{table::Table, Error, WasiCtx, WasiFile};
use wasi_common::{file::FileCaps, table::Table, Error, WasiCtx, WasiFile};
pub struct WasiCtxBuilder(WasiCtx);
@@ -120,6 +123,18 @@ impl WasiCtxBuilder {
self.0.push_preopened_dir(dir, guest_path)?;
Ok(self)
}
pub fn preopened_socket(mut self, fd: u32, socket: impl Into<Socket>) -> Result<Self, Error> {
let socket: Socket = socket.into();
let file: Box<dyn WasiFile> = socket.into();
let caps = FileCaps::FDSTAT_SET_FLAGS
| FileCaps::FILESTAT_GET
| FileCaps::READ
| FileCaps::POLL_READWRITE;
self.0.insert_file(fd, file, caps);
Ok(self)
}
pub fn build(self) -> WasiCtx {
self.0
}