Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
Add basic TCP socket APIs.
Browse files Browse the repository at this point in the history
This is based on @npmccullum's [wasi-snapshot-preview2 draft] which is
in turn based on the [wasi-sockets proposal], though for simplify for
now it omits UDP sockets and some other features.

It's also based on the [pseudo-streams PR]; so that should proceed
first before this.

[draft wasi-snapshot-preview2]: https://github.com/npmccallum/wasi-snapshot-preview2
[wasi-sockets proposal]: https://github.com/WebAssembly/wasi-sockets
[pseudo-streams PR]: #29
  • Loading branch information
sunfishcode committed Jan 11, 2023
1 parent d56b897 commit c76c7bd
Show file tree
Hide file tree
Showing 3 changed files with 606 additions and 44 deletions.
127 changes: 123 additions & 4 deletions host/src/tcp.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,136 @@
#![allow(unused_variables)]

use crate::{
wasi_tcp::{self, BytesResult, Socket, WasiTcp},
HostResult, WasiCtx,
wasi_tcp::{
self, ConnectionFlags, Errno, IpSocketAddress, ListenerFlags, Network, Size, TcpConnection,
TcpListener, WasiTcp,
},
HostResult, WasiCtx, WasiStream,
};

#[async_trait::async_trait]
impl WasiTcp for WasiCtx {
async fn bytes_readable(&mut self, socket: Socket) -> HostResult<BytesResult, wasi_tcp::Error> {
async fn listen(
&mut self,
network: Network,
address: IpSocketAddress,
backlog: Option<Size>,
flags: ListenerFlags,
) -> HostResult<TcpListener, Errno> {
todo!()
}

async fn accept(
&mut self,
listener: TcpListener,
flags: ConnectionFlags,
) -> HostResult<(TcpConnection, IpSocketAddress), Errno> {
todo!()
}

async fn connect(
&mut self,
network: Network,
local_address: IpSocketAddress,
remote_address: IpSocketAddress,
flags: ConnectionFlags,
) -> HostResult<TcpConnection, Errno> {
todo!()
}

async fn send(&mut self, connection: TcpConnection, bytes: Vec<u8>) -> HostResult<Size, Errno> {
todo!()
}

async fn receive(
&mut self,
connection: TcpConnection,
length: Size,
) -> HostResult<(Vec<u8>, bool), Errno> {
todo!()
}

async fn get_listener_local_address(
&mut self,
listener: TcpListener,
) -> HostResult<IpSocketAddress, Errno> {
todo!()
}

async fn get_tcp_connection_local_address(
&mut self,
connection: TcpConnection,
) -> HostResult<IpSocketAddress, Errno> {
todo!()
}

async fn get_remote_address(
&mut self,
connection: TcpConnection,
) -> HostResult<IpSocketAddress, Errno> {
todo!()
}

async fn get_flags(&mut self, connection: TcpConnection) -> HostResult<ConnectionFlags, Errno> {
todo!()
}

async fn set_flags(
&mut self,
connection: TcpConnection,
flags: ConnectionFlags,
) -> HostResult<(), Errno> {
todo!()
}

async fn get_receive_buffer_size(
&mut self,
connection: TcpConnection,
) -> HostResult<Size, Errno> {
todo!()
}

async fn set_receive_buffer_size(
&mut self,
connection: TcpConnection,
value: Size,
) -> HostResult<(), Errno> {
todo!()
}

async fn get_send_buffer_size(&mut self, connection: TcpConnection) -> HostResult<Size, Errno> {
todo!()
}

async fn set_send_buffer_size(
&mut self,
connection: TcpConnection,
value: Size,
) -> HostResult<(), Errno> {
todo!()
}

async fn bytes_readable(&mut self, socket: TcpConnection) -> HostResult<Size, Errno> {
drop(socket);
todo!()
}

async fn bytes_writable(&mut self, socket: Socket) -> HostResult<BytesResult, wasi_tcp::Error> {
async fn bytes_writable(&mut self, socket: TcpConnection) -> HostResult<Size, Errno> {
drop(socket);
todo!()
}

async fn read_via_stream(
&mut self,
fd: wasi_tcp::TcpConnection,
) -> HostResult<WasiStream, wasi_tcp::Errno> {
todo!()
}

async fn write_via_stream(
&mut self,
fd: wasi_tcp::TcpConnection,
) -> HostResult<WasiStream, wasi_tcp::Errno> {
todo!()
}
}
26 changes: 13 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,20 +1430,20 @@ impl Drop for Futures {
}
}

impl From<wasi_tcp::Error> for Errno {
fn from(error: wasi_tcp::Error) -> Errno {
use wasi_tcp::Error::*;
impl From<wasi_tcp::Errno> for Errno {
fn from(error: wasi_tcp::Errno) -> Errno {
use wasi_tcp::Errno::*;

match error {
// Use a black box to prevent the optimizer from generating a
// lookup table, which would require a static initializer.
ConnectionAborted => black_box(ERRNO_CONNABORTED),
ConnectionRefused => ERRNO_CONNREFUSED,
ConnectionReset => ERRNO_CONNRESET,
HostUnreachable => ERRNO_HOSTUNREACH,
NetworkDown => ERRNO_NETDOWN,
NetworkUnreachable => ERRNO_NETUNREACH,
Timeout => ERRNO_TIMEDOUT,
Connaborted => black_box(ERRNO_CONNABORTED),
Connrefused => ERRNO_CONNREFUSED,
Connreset => ERRNO_CONNRESET,
Hostunreach => ERRNO_HOSTUNREACH,
Netdown => ERRNO_NETDOWN,
Netunreach => ERRNO_NETUNREACH,
Timedout => ERRNO_TIMEDOUT,
}
}
}
Expand Down Expand Up @@ -1961,8 +1961,8 @@ enum StreamType {
/// Streaming data with a file.
File(File),

/// Streaming data with a socket.
Socket(wasi_tcp::Socket),
/// Streaming data with a TCP socket connection.
Socket(wasi_tcp::TcpConnection),
}

impl Drop for Descriptor {
Expand Down Expand Up @@ -2298,7 +2298,7 @@ impl State {
}

#[allow(dead_code)] // until Socket is implemented
fn get_socket(&self, fd: Fd) -> Result<wasi_tcp::Socket, Errno> {
fn get_socket(&self, fd: Fd) -> Result<wasi_tcp::TcpConnection, Errno> {
match self.get(fd)? {
Descriptor::Streams(Streams {
type_: StreamType::Socket(socket),
Expand Down
Loading

0 comments on commit c76c7bd

Please sign in to comment.