Skip to content

Commit

Permalink
wasi: enable TcpListener and TcpStream
Browse files Browse the repository at this point in the history
With the addition of `sock_accept()` to snapshot1, simple networking via
a passed `TcpListener` is possible. This patch implements the basics to
make a simple server work.

Signed-off-by: Harald Hoyer <harald@profian.com>
  • Loading branch information
haraldh committed Jan 28, 2022
1 parent 00cbc8d commit d2a1369
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 13 deletions.
20 changes: 20 additions & 0 deletions library/std/src/os/wasi/net/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
//! WASI-specific networking functionality
#![unstable(feature = "wasi_ext", issue = "71213")]

use crate::io;
use crate::net;
use crate::sys_common::AsInner;

/// WASI-specific extensions to [`std::net::TcpListener`].
///
/// [`std::net::TcpListener`]: crate::net::TcpListener
pub trait TcpListenerExt {
/// Accept a socket.
///
/// This corresponds to the `sock_accept` syscall.
fn sock_accept(&self, flags: u16) -> io::Result<u32>;
}

impl TcpListenerExt for net::TcpListener {
fn sock_accept(&self, flags: u16) -> io::Result<u32> {
self.as_inner().as_inner().as_inner().sock_accept(flags)
}
}
4 changes: 4 additions & 0 deletions library/std/src/sys/wasi/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ impl WasiFd {
unsafe { wasi::path_remove_directory(self.as_raw_fd() as wasi::Fd, path).map_err(err2io) }
}

pub fn sock_accept(&self, flags: wasi::Fdflags) -> io::Result<wasi::Fd> {
unsafe { wasi::sock_accept(self.as_raw_fd() as wasi::Fd, flags).map_err(err2io) }
}

pub fn sock_recv(
&self,
ri_data: &mut [IoSliceMut<'_>],
Expand Down
66 changes: 53 additions & 13 deletions library/std/src/sys/wasi/net.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![deny(unsafe_op_in_unsafe_fn)]

use super::err2io;
use super::fd::WasiFd;
use crate::convert::TryFrom;
use crate::fmt;
Expand Down Expand Up @@ -87,24 +88,24 @@ impl TcpStream {
unsupported()
}

pub fn read(&self, _: &mut [u8]) -> io::Result<usize> {
unsupported()
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
self.read_vectored(&mut [IoSliceMut::new(buf)])
}

pub fn read_vectored(&self, _: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
unsupported()
pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
self.socket().as_inner().read(bufs)
}

pub fn is_read_vectored(&self) -> bool {
true
}

pub fn write(&self, _: &[u8]) -> io::Result<usize> {
unsupported()
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
self.write_vectored(&[IoSlice::new(buf)])
}

pub fn write_vectored(&self, _: &[IoSlice<'_>]) -> io::Result<usize> {
unsupported()
pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
self.socket().as_inner().write(bufs)
}

pub fn is_write_vectored(&self) -> bool {
Expand Down Expand Up @@ -155,8 +156,23 @@ impl TcpStream {
unsupported()
}

pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
unsupported()
pub fn set_nonblocking(&self, state: bool) -> io::Result<()> {
let fdstat = unsafe {
wasi::fd_fdstat_get(self.socket().as_inner().as_raw_fd() as wasi::Fd).map_err(err2io)?
};

let mut flags = fdstat.fs_flags;

if state {
flags |= wasi::FDFLAGS_NONBLOCK;
} else {
flags &= !wasi::FDFLAGS_NONBLOCK;
}

unsafe {
wasi::fd_fdstat_set_flags(self.socket().as_inner().as_raw_fd() as wasi::Fd, flags)
.map_err(err2io)
}
}

pub fn socket(&self) -> &Socket {
Expand Down Expand Up @@ -194,7 +210,16 @@ impl TcpListener {
}

pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
unsupported()
let fd = unsafe {
wasi::sock_accept(self.as_inner().as_inner().as_raw_fd() as _, 0).map_err(err2io)?
};

Ok((
TcpStream::from_inner(unsafe { Socket::from_raw_fd(fd as _) }),
// WASI has no concept of SocketAddr yet
// return an unspecified IPv4Addr
SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 0),
))
}

pub fn duplicate(&self) -> io::Result<TcpListener> {
Expand All @@ -221,8 +246,23 @@ impl TcpListener {
unsupported()
}

pub fn set_nonblocking(&self, _: bool) -> io::Result<()> {
unsupported()
pub fn set_nonblocking(&self, state: bool) -> io::Result<()> {
let fdstat = unsafe {
wasi::fd_fdstat_get(self.socket().as_inner().as_raw_fd() as wasi::Fd).map_err(err2io)?
};

let mut flags = fdstat.fs_flags;

if state {
flags |= wasi::FDFLAGS_NONBLOCK;
} else {
flags &= !wasi::FDFLAGS_NONBLOCK;
}

unsafe {
wasi::fd_fdstat_set_flags(self.socket().as_inner().as_raw_fd() as wasi::Fd, flags)
.map_err(err2io)
}
}

pub fn socket(&self) -> &Socket {
Expand Down

0 comments on commit d2a1369

Please sign in to comment.