Skip to content

Commit 0f85778

Browse files
committed
Implement sock_accept
Signed-off-by: Harald Hoyer <[email protected]>
1 parent 5fc01ba commit 0f85778

File tree

18 files changed

+608
-30
lines changed

18 files changed

+608
-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: 20 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::Listener;
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,22 @@ impl WasiCtxBuilder {
120123
self.0.push_preopened_dir(dir, guest_path)?;
121124
Ok(self)
122125
}
126+
pub fn preopened_listener(
127+
mut self,
128+
fd: u32,
129+
listener: impl Into<Listener>,
130+
) -> Result<Self, Error> {
131+
let listener: Listener = listener.into();
132+
let file: Box<dyn WasiFile> = listener.into();
133+
134+
let caps = FileCaps::FDSTAT_SET_FLAGS
135+
| FileCaps::FILESTAT_GET
136+
| FileCaps::READ
137+
| FileCaps::POLL_READWRITE;
138+
139+
self.0.insert_file(fd, file, caps);
140+
Ok(self)
141+
}
123142
pub fn build(self) -> WasiCtx {
124143
self.0
125144
}

0 commit comments

Comments
 (0)