Skip to main content

wasmtime_wasi/p2/
network.rs

1use crate::TrappableError;
2use crate::p2::bindings::sockets::network::ErrorCode;
3use crate::p2::bindings::sockets::tcp::ShutdownType;
4
5pub type SocketResult<T> = Result<T, SocketError>;
6
7pub type SocketError = TrappableError<ErrorCode>;
8
9impl From<wasmtime::component::ResourceTableError> for SocketError {
10    fn from(error: wasmtime::component::ResourceTableError) -> Self {
11        Self::trap(error)
12    }
13}
14
15impl From<std::io::Error> for SocketError {
16    fn from(error: std::io::Error) -> Self {
17        ErrorCode::from(error).into()
18    }
19}
20
21impl From<rustix::io::Errno> for SocketError {
22    fn from(error: rustix::io::Errno) -> Self {
23        ErrorCode::from(error).into()
24    }
25}
26
27impl From<crate::sockets::ErrorCode> for SocketError {
28    fn from(error: crate::sockets::ErrorCode) -> Self {
29        ErrorCode::from(error).into()
30    }
31}
32
33impl From<crate::sockets::ErrorCode> for ErrorCode {
34    fn from(error: crate::sockets::ErrorCode) -> Self {
35        match error {
36            crate::sockets::ErrorCode::Other => Self::Unknown,
37            crate::sockets::ErrorCode::AccessDenied => Self::AccessDenied,
38            crate::sockets::ErrorCode::NotSupported => Self::NotSupported,
39            crate::sockets::ErrorCode::InvalidArgument => Self::InvalidArgument,
40            crate::sockets::ErrorCode::OutOfMemory => Self::OutOfMemory,
41            crate::sockets::ErrorCode::Timeout => Self::Timeout,
42            crate::sockets::ErrorCode::InvalidState => Self::InvalidState,
43            crate::sockets::ErrorCode::AddressNotBindable => Self::AddressNotBindable,
44            crate::sockets::ErrorCode::AddressInUse => Self::AddressInUse,
45            crate::sockets::ErrorCode::RemoteUnreachable => Self::RemoteUnreachable,
46            crate::sockets::ErrorCode::ConnectionRefused => Self::ConnectionRefused,
47            crate::sockets::ErrorCode::ConnectionBroken => Self::Unknown,
48            crate::sockets::ErrorCode::ConnectionReset => Self::ConnectionReset,
49            crate::sockets::ErrorCode::ConnectionAborted => Self::ConnectionAborted,
50            crate::sockets::ErrorCode::DatagramTooLarge => Self::DatagramTooLarge,
51        }
52    }
53}
54
55impl From<crate::sockets::ip_name_lookup::ErrorCode> for ErrorCode {
56    fn from(code: crate::sockets::ip_name_lookup::ErrorCode) -> Self {
57        match code {
58            crate::sockets::ip_name_lookup::ErrorCode::AccessDenied => Self::AccessDenied,
59            crate::sockets::ip_name_lookup::ErrorCode::InvalidArgument => Self::InvalidArgument,
60            crate::sockets::ip_name_lookup::ErrorCode::NameUnresolvable => Self::NameUnresolvable,
61            crate::sockets::ip_name_lookup::ErrorCode::TemporaryResolverFailure => {
62                Self::TemporaryResolverFailure
63            }
64            crate::sockets::ip_name_lookup::ErrorCode::PermanentResolverFailure => {
65                Self::PermanentResolverFailure
66            }
67            crate::sockets::ip_name_lookup::ErrorCode::Other => Self::Unknown,
68        }
69    }
70}
71
72impl From<ShutdownType> for std::net::Shutdown {
73    fn from(value: ShutdownType) -> Self {
74        match value {
75            ShutdownType::Receive => Self::Read,
76            ShutdownType::Send => Self::Write,
77            ShutdownType::Both => Self::Both,
78        }
79    }
80}
81
82/// A network resource representing the capability to use the networking
83/// APIs in WASI 0.2. The concept of network handles has been removed in
84/// WASI 0.3.
85///
86/// Note that in Wasmtime all permissions are checked through the `WasiCtx` and
87/// not through the `Network` resource itself. The `Network` resource is just a
88/// placeholder capability handle.
89pub struct Network {
90    pub(crate) _priv: (),
91}