kobako 0.30.0 → 0.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.release-please-manifest.json +1 -1
- data/CHANGELOG.md +335 -0
- data/Cargo.lock +469 -11
- data/README.md +43 -3
- data/ROADMAP.md +1 -1
- data/crates/kobako-runtime/CHANGELOG.md +219 -0
- data/crates/kobako-runtime/Cargo.toml +51 -3
- data/crates/kobako-runtime/README.md +7 -4
- data/crates/kobako-runtime/src/cred.rs +124 -0
- data/crates/kobako-runtime/src/dispatch.rs +4 -5
- data/crates/kobako-runtime/src/endpoint/buffer.rs +153 -44
- data/crates/kobako-runtime/src/endpoint/dispatch.rs +80 -41
- data/crates/kobako-runtime/src/endpoint/invoke.rs +23 -36
- data/crates/kobako-runtime/src/endpoint.rs +137 -36
- data/crates/kobako-runtime/src/errno.rs +391 -0
- data/crates/kobako-runtime/src/error.rs +31 -10
- data/crates/kobako-runtime/src/filesystem/anon_inodes.rs +52 -0
- data/crates/kobako-runtime/src/filesystem/bind/lookup.rs +52 -14
- data/crates/kobako-runtime/src/filesystem/bind/walk.rs +19 -20
- data/crates/kobako-runtime/src/filesystem/bind.rs +437 -245
- data/crates/kobako-runtime/src/filesystem/overlay/file.rs +6 -6
- data/crates/kobako-runtime/src/filesystem/overlay/path.rs +43 -65
- data/crates/kobako-runtime/src/filesystem/overlay/stack/copy_up.rs +9 -16
- data/crates/kobako-runtime/src/filesystem/overlay/stack/dir.rs +47 -72
- data/crates/kobako-runtime/src/filesystem/overlay/stack/namei.rs +12 -12
- data/crates/kobako-runtime/src/filesystem/overlay/stack/readdir.rs +4 -7
- data/crates/kobako-runtime/src/filesystem/overlay/stack.rs +121 -37
- data/crates/kobako-runtime/src/filesystem/overlay.rs +302 -343
- data/crates/kobako-runtime/src/filesystem/pipe.rs +12 -9
- data/crates/kobako-runtime/src/filesystem/rdir.rs +15 -32
- data/crates/kobako-runtime/src/filesystem/tmpfs/seed.rs +32 -0
- data/crates/kobako-runtime/src/filesystem/tmpfs/tmpfile.rs +3 -3
- data/crates/kobako-runtime/src/filesystem/tmpfs/whiteout.rs +57 -35
- data/crates/kobako-runtime/src/filesystem/tmpfs/xattr.rs +2 -2
- data/crates/kobako-runtime/src/filesystem/tmpfs.rs +172 -160
- data/crates/kobako-runtime/src/filesystem/wrap.rs +36 -52
- data/crates/kobako-runtime/src/filesystem.rs +6 -3
- data/crates/kobako-runtime/src/fork.rs +96 -0
- data/crates/kobako-runtime/src/grant.rs +3 -0
- data/crates/kobako-runtime/src/hashtable.rs +4 -2
- data/crates/kobako-runtime/src/lib.rs +18 -7
- data/crates/kobako-runtime/src/limits.rs +195 -0
- data/crates/kobako-runtime/src/locking.rs +10 -0
- data/crates/kobako-runtime/src/memcg.rs +383 -0
- data/crates/kobako-runtime/src/mount.rs +67 -9
- data/crates/kobako-runtime/src/net/backends.rs +43 -0
- data/crates/kobako-runtime/src/net/connections.rs +179 -0
- data/crates/kobako-runtime/src/net/credential.rs +125 -0
- data/crates/kobako-runtime/src/net/egress.rs +160 -0
- data/crates/kobako-runtime/src/net/family.rs +181 -0
- data/crates/kobako-runtime/src/net/http.rs +2733 -0
- data/crates/kobako-runtime/src/net/lookup.rs +285 -0
- data/crates/kobako-runtime/src/net/mock.rs +251 -0
- data/crates/kobako-runtime/src/net/namespace.rs +1366 -0
- data/crates/kobako-runtime/src/net/reactor.rs +531 -0
- data/crates/kobako-runtime/src/net/resolv/darwin.rs +275 -0
- data/crates/kobako-runtime/src/net/resolv/stub/tests.rs +469 -0
- data/crates/kobako-runtime/src/net/resolv/stub.rs +536 -0
- data/crates/kobako-runtime/src/net/resolv/windows.rs +248 -0
- data/crates/kobako-runtime/src/net/resolv.rs +227 -0
- data/crates/kobako-runtime/src/net/socket.rs +870 -0
- data/crates/kobako-runtime/src/net/tcp.rs +1588 -0
- data/crates/kobako-runtime/src/net/tls.rs +58 -0
- data/crates/kobako-runtime/src/net/transport.rs +279 -0
- data/crates/kobako-runtime/src/net/udp.rs +1003 -0
- data/crates/kobako-runtime/src/net.rs +42 -0
- data/crates/kobako-runtime/src/network/builder.rs +177 -0
- data/crates/kobako-runtime/src/network/ruleset.rs +777 -0
- data/crates/kobako-runtime/src/network.rs +551 -0
- data/crates/kobako-runtime/src/policy.rs +16 -0
- data/crates/kobako-runtime/src/profile.rs +29 -144
- data/crates/kobako-runtime/src/runtime.rs +3 -5
- data/crates/kobako-runtime/src/sched.rs +130 -29
- data/crates/kobako-runtime/src/security/entry_policy.rs +22 -0
- data/crates/kobako-runtime/src/{vfs/cred.rs → security/hooks.rs} +52 -144
- data/crates/kobako-runtime/src/security/mount_policy.rs +158 -0
- data/crates/kobako-runtime/src/security.rs +101 -196
- data/crates/kobako-runtime/src/snapshot.rs +121 -2
- data/crates/kobako-runtime/src/vfs/attr.rs +5 -5
- data/crates/kobako-runtime/src/vfs/dentry.rs +143 -89
- data/crates/kobako-runtime/src/vfs/file.rs +199 -203
- data/crates/kobako-runtime/src/vfs/inode.rs +41 -45
- data/crates/kobako-runtime/src/vfs/kern_path.rs +38 -42
- data/crates/kobako-runtime/src/vfs/mount.rs +10 -9
- data/crates/kobako-runtime/src/vfs/namei.rs +829 -700
- data/crates/kobako-runtime/src/vfs/namespace.rs +162 -0
- data/crates/kobako-runtime/src/vfs/open_files.rs +69 -0
- data/crates/kobako-runtime/src/vfs/openat2.rs +3 -3
- data/crates/kobako-runtime/src/vfs/path.rs +6 -6
- data/crates/kobako-runtime/src/vfs/readdir.rs +6 -6
- data/crates/kobako-runtime/src/vfs/stat.rs +159 -23
- data/crates/kobako-runtime/src/vfs/super_block.rs +18 -14
- data/crates/kobako-runtime/src/vfs/utimes.rs +37 -27
- data/crates/kobako-runtime/src/vfs.rs +9 -6
- data/crates/kobako-runtime/src/wait.rs +356 -0
- data/crates/kobako-transport/CHANGELOG.md +31 -0
- data/crates/kobako-transport/Cargo.toml +1 -1
- data/crates/kobako-transport/README.md +3 -4
- data/crates/kobako-transport/src/abi.rs +78 -72
- data/crates/kobako-transport/src/envelope/bytes.rs +17 -6
- data/crates/kobako-transport/src/envelope/call.rs +32 -4
- data/crates/kobako-transport/src/envelope/error_record.rs +21 -0
- data/crates/kobako-transport/src/envelope/name.rs +99 -0
- data/crates/kobako-transport/src/envelope/outcome.rs +9 -9
- data/crates/kobako-transport/src/envelope/preamble.rs +24 -21
- data/crates/kobako-transport/src/envelope/reply.rs +27 -27
- data/crates/kobako-transport/src/envelope/unsupported.rs +10 -10
- data/crates/kobako-transport/src/envelope.rs +13 -8
- data/crates/kobako-wasmtime/CHANGELOG.md +161 -0
- data/crates/kobako-wasmtime/Cargo.toml +26 -7
- data/crates/kobako-wasmtime/README.md +3 -2
- data/crates/kobako-wasmtime/build/preview2.rs +1339 -0
- data/crates/kobako-wasmtime/build.rs +16 -0
- data/crates/kobako-wasmtime/src/abi.rs +17 -37
- data/crates/kobako-wasmtime/src/ambient.rs +33 -18
- data/crates/kobako-wasmtime/src/cache.rs +23 -15
- data/crates/kobako-wasmtime/src/config.rs +52 -68
- data/crates/kobako-wasmtime/src/driver.rs +48 -63
- data/crates/kobako-wasmtime/src/exports.rs +143 -17
- data/crates/kobako-wasmtime/src/guest.rs +12 -6
- data/crates/kobako-wasmtime/src/guest_binary.rs +14 -0
- data/crates/kobako-wasmtime/src/imports.rs +42 -18
- data/crates/kobako-wasmtime/src/instance_pre.rs +4 -4
- data/crates/kobako-wasmtime/src/invocation.rs +74 -12
- data/crates/kobako-wasmtime/src/lib.rs +5 -3
- data/crates/kobako-wasmtime/src/limiter.rs +70 -5
- data/crates/kobako-wasmtime/src/preview1/descriptors.rs +309 -0
- data/crates/kobako-wasmtime/src/preview1/differential/mount.rs +5 -15
- data/crates/kobako-wasmtime/src/preview1/differential.rs +27 -31
- data/crates/kobako-wasmtime/src/preview1/errno.rs +55 -66
- data/crates/kobako-wasmtime/src/preview1/fd.rs +327 -296
- data/crates/kobako-wasmtime/src/preview1/memory.rs +21 -12
- data/crates/kobako-wasmtime/src/preview1/mount_view/wrap.rs +34 -53
- data/crates/kobako-wasmtime/src/preview1/mount_view.rs +231 -217
- data/crates/kobako-wasmtime/src/preview1/path.rs +56 -121
- data/crates/kobako-wasmtime/src/preview1/poll.rs +244 -120
- data/crates/kobako-wasmtime/src/preview1/sock.rs +10 -8
- data/crates/kobako-wasmtime/src/preview1/testsuite.rs +5 -15
- data/crates/kobako-wasmtime/src/preview1.rs +46 -42
- data/crates/kobako-wasmtime/src/preview2/abi.rs +227 -0
- data/crates/kobako-wasmtime/src/preview2/error.rs +118 -0
- data/crates/kobako-wasmtime/src/preview2/filesystem.rs +122 -0
- data/crates/kobako-wasmtime/src/preview2/host.rs +3959 -0
- data/crates/kobako-wasmtime/src/preview2/http.rs +1503 -0
- data/crates/kobako-wasmtime/src/preview2/io.rs +315 -0
- data/crates/kobako-wasmtime/src/preview2/sockets.rs +379 -0
- data/crates/kobako-wasmtime/src/preview2/table.rs +305 -0
- data/crates/kobako-wasmtime/src/preview2.rs +57 -0
- data/crates/kobako-wasmtime/src/trap.rs +24 -9
- data/crates/kobako-wasmtime/src/wasi.rs +210 -81
- data/crates/kobako-wasmtime/wit/deps/cli.wit +233 -0
- data/crates/kobako-wasmtime/wit/deps/clocks.wit +162 -0
- data/crates/kobako-wasmtime/wit/deps/filesystem.wit +589 -0
- data/crates/kobako-wasmtime/wit/deps/http.wit +733 -0
- data/crates/kobako-wasmtime/wit/deps/io.wit +299 -0
- data/crates/kobako-wasmtime/wit/deps/random.wit +92 -0
- data/crates/kobako-wasmtime/wit/deps/sockets.wit +949 -0
- data/data/kobako.wasm +0 -0
- data/ext/kobako/Cargo.toml +1 -1
- data/ext/kobako/src/{runtime/errors.rs → errors.rs} +34 -26
- data/ext/kobako/src/{runtime/escape.rs → escape.rs} +42 -11
- data/ext/kobako/src/{runtime/filesystem → filesystem}/grants.rs +30 -26
- data/ext/kobako/src/{runtime/filesystem → filesystem}/ruby.rs +149 -168
- data/ext/kobako/src/{runtime/filesystem.rs → filesystem.rs} +24 -15
- data/ext/kobako/src/gvl.rs +306 -0
- data/ext/kobako/src/lib.rs +64 -3
- data/ext/kobako/src/network/mock.rs +187 -0
- data/ext/kobako/src/network.rs +591 -0
- data/ext/kobako/src/runtime/bridge.rs +30 -29
- data/ext/kobako/src/runtime/snapshot.rs +4 -4
- data/ext/kobako/src/runtime.rs +221 -143
- data/ext/kobako/src/wire.rs +42 -0
- data/lib/kobako/catalog/extensions.rb +14 -2
- data/lib/kobako/catalog/flags.rb +3 -5
- data/lib/kobako/catalog/grants.rb +5 -4
- data/lib/kobako/catalog/handles.rb +5 -27
- data/lib/kobako/catalog/mount.rb +11 -3
- data/lib/kobako/catalog/network.rb +35 -0
- data/lib/kobako/catalog/replacements.rb +28 -0
- data/lib/kobako/catalog/services.rb +5 -19
- data/lib/kobako/catalog/snippets.rb +3 -18
- data/lib/kobako/catalog.rb +3 -0
- data/lib/kobako/codec/{utils.rb → boundary.rb} +4 -2
- data/lib/kobako/codec/decoder.rb +4 -4
- data/lib/kobako/codec/ext_types.rb +3 -3
- data/lib/kobako/codec/handle_walk.rb +3 -43
- data/lib/kobako/codec/value_budget.rb +132 -40
- data/lib/kobako/codec.rb +1 -1
- data/lib/kobako/context.rb +24 -11
- data/lib/kobako/errors.rb +15 -12
- data/lib/kobako/extension.rb +3 -2
- data/lib/kobako/filesystem.rb +9 -2
- data/lib/kobako/handle.rb +2 -2
- data/lib/kobako/names.rb +28 -0
- data/lib/kobako/network.rb +210 -0
- data/lib/kobako/outcome.rb +7 -7
- data/lib/kobako/pool.rb +45 -23
- data/lib/kobako/runtime.rb +3 -2
- data/lib/kobako/sandbox.rb +92 -35
- data/lib/kobako/sandbox_options.rb +41 -106
- data/lib/kobako/transport/argument_wrap.rb +64 -0
- data/lib/kobako/transport/block_failure.rb +1 -1
- data/lib/kobako/transport/dispatcher.rb +6 -6
- data/lib/kobako/transport/exposure.rb +26 -15
- data/lib/kobako/transport/reflection.rb +21 -4
- data/lib/kobako/transport/run.rb +8 -23
- data/lib/kobako/transport/yielder.rb +2 -9
- data/lib/kobako/transport.rb +1 -0
- data/lib/kobako/version.rb +1 -1
- data/lib/kobako.rb +6 -2
- data/release-please-config.json +15 -0
- data/sig/core/thread.rbs +6 -0
- data/sig/kobako/catalog/extensions.rbs +2 -0
- data/sig/kobako/catalog/grants.rbs +7 -4
- data/sig/kobako/catalog/handles.rbs +0 -2
- data/sig/kobako/catalog/mount.rbs +3 -1
- data/sig/kobako/catalog/network.rbs +20 -0
- data/sig/kobako/catalog/replacements.rbs +14 -0
- data/sig/kobako/catalog/services.rbs +0 -4
- data/sig/kobako/catalog/snippets.rbs +0 -2
- data/sig/kobako/codec/{utils.rbs → boundary.rbs} +2 -2
- data/sig/kobako/codec/handle_walk.rbs +0 -6
- data/sig/kobako/codec/value_budget.rbs +47 -4
- data/sig/kobako/context.rbs +3 -1
- data/sig/kobako/errors.rbs +4 -4
- data/sig/kobako/filesystem.rbs +21 -3
- data/sig/kobako/network.rbs +115 -0
- data/sig/kobako/outcome.rbs +1 -1
- data/sig/kobako/pool.rbs +7 -3
- data/sig/kobako/runtime.rbs +32 -3
- data/sig/kobako/sandbox.rbs +19 -2
- data/sig/kobako/sandbox_options.rbs +29 -38
- data/sig/kobako/transport/argument_wrap.rbs +15 -0
- data/sig/kobako/transport/error.rbs +1 -1
- data/sig/kobako/transport/exposure.rbs +2 -4
- data/sig/kobako/transport/reflection.rbs +10 -0
- data/sig/kobako/transport/run.rbs +0 -2
- data/sig/kobako/transport/yielder.rbs +0 -3
- data/sig/kobako/transport.rbs +1 -1
- data/sig/kobako.rbs +13 -0
- metadata +84 -35
- data/crates/kobako-runtime/src/endpoint/capture.rs +0 -115
- data/crates/kobako-runtime/src/endpoint/memory_cap.rs +0 -238
- data/crates/kobako-runtime/src/filesystem/path.rs +0 -21
- data/crates/kobako-runtime/src/vfs/errno.rs +0 -153
- data/crates/kobako-runtime/tests/linux_baseline/calls.rs +0 -171
- data/crates/kobako-runtime/tests/linux_baseline/concurrent.rs +0 -276
- data/crates/kobako-runtime/tests/linux_baseline/kobako.rs +0 -323
- data/crates/kobako-runtime/tests/linux_baseline/linux-baseline-expected.txt +0 -16
- data/crates/kobako-runtime/tests/linux_baseline/linux.rs +0 -449
- data/crates/kobako-runtime/tests/linux_baseline/main.rs +0 -611
- data/crates/kobako-runtime/tests/linux_baseline/permissions.rs +0 -236
- data/crates/kobako-wasmtime/src/descriptors.rs +0 -200
- data/crates/kobako-wasmtime/tests/integration/compile_posture.rs +0 -43
- data/crates/kobako-wasmtime/tests/integration/guest_file.rs +0 -1085
- data/crates/kobako-wasmtime/tests/integration/main.rs +0 -29
- data/crates/kobako-wasmtime/tests/integration/preamble_flags.rs +0 -70
- data/crates/kobako-wasmtime/tests/integration/snippet_compile.rs +0 -95
- data/crates/kobako-wasmtime/tests/integration/support/mod.rs +0 -30
- data/crates/kobako-wasmtime/tests/integration/unanswered_call.rs +0 -86
- data/crates/kobako-wasmtime/tests/integration/unsupported_call.rs +0 -51
- data/ext/kobako/src/runtime/gvl.rs +0 -147
- data/lib/kobako/constant_name.rb +0 -8
- data/sig/kobako/constant_name.rbs +0 -3
- /data/ext/kobako/src/{runtime/pinned_value.rs → pinned_value.rs} +0 -0
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
//! The `wasi:io` resources: the streams a guest reads and writes, the
|
|
2
|
+
//! files each stands on, their errors, and the pollables a wait holds.
|
|
3
|
+
|
|
4
|
+
use std::sync::atomic::{AtomicBool, Ordering};
|
|
5
|
+
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
|
|
6
|
+
use std::time::Instant;
|
|
7
|
+
|
|
8
|
+
use kobako_runtime::net::{Exchange, TcpSock};
|
|
9
|
+
use kobako_runtime::profile::Profile;
|
|
10
|
+
use kobako_runtime::vfs::{File, PollMask};
|
|
11
|
+
use kobako_runtime::wait::PollTable;
|
|
12
|
+
use kobako_runtime::Errno;
|
|
13
|
+
|
|
14
|
+
use super::bindings::wasi_sockets_network_0_2_12::IpAddressFamily;
|
|
15
|
+
use super::MAX_READ_SIZE_ALLOC;
|
|
16
|
+
|
|
17
|
+
/// A file every view of it shares, as handles on one `struct file` do.
|
|
18
|
+
pub(crate) type SharedFile = Arc<Mutex<File>>;
|
|
19
|
+
|
|
20
|
+
/// The shared `file`, which a read or a write takes `&mut`.
|
|
21
|
+
pub(crate) fn lock(file: &SharedFile) -> MutexGuard<'_, File> {
|
|
22
|
+
file.lock().unwrap_or_else(PoisonError::into_inner)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/// One socket file every view of a guest socket shares, as handles on one
|
|
26
|
+
/// `struct file` share one `struct socket`: the protocol's half is `S`, and
|
|
27
|
+
/// the last view dropped releases the socket. It keeps the family the guest
|
|
28
|
+
/// made it of, as wasmtime's socket does, so an address of another family is
|
|
29
|
+
/// refused before the runtime is asked.
|
|
30
|
+
pub(crate) struct SocketFile<S> {
|
|
31
|
+
pub(crate) sock: Arc<S>,
|
|
32
|
+
pub(crate) family: IpAddressFamily,
|
|
33
|
+
file: SharedFile,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
impl<S> Clone for SocketFile<S> {
|
|
37
|
+
fn clone(&self) -> Self {
|
|
38
|
+
Self {
|
|
39
|
+
sock: self.sock.clone(),
|
|
40
|
+
family: self.family,
|
|
41
|
+
file: self.file.clone(),
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
impl<S> SocketFile<S> {
|
|
47
|
+
pub(crate) fn new(sock: Arc<S>, family: IpAddressFamily, file: File) -> Self {
|
|
48
|
+
Self {
|
|
49
|
+
sock,
|
|
50
|
+
family,
|
|
51
|
+
file: Arc::new(Mutex::new(file)),
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// The file, which a read or a write takes `&mut`, so the views share it
|
|
56
|
+
/// behind a lock.
|
|
57
|
+
pub(crate) fn file(&self) -> MutexGuard<'_, File> {
|
|
58
|
+
lock(&self.file)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/// Another view of the file, sharing the one the socket holds.
|
|
62
|
+
pub(crate) fn shared_file(&self) -> SharedFile {
|
|
63
|
+
self.file.clone()
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/// What an `input-stream` or an `output-stream` reads or writes: a socket's
|
|
68
|
+
/// file, whose socket holds what was written until the host takes it, or any
|
|
69
|
+
/// other file.
|
|
70
|
+
#[derive(Clone)]
|
|
71
|
+
pub(crate) enum Stream {
|
|
72
|
+
Socket(SocketFile<TcpSock>),
|
|
73
|
+
File(SharedFile),
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
impl From<File> for Stream {
|
|
77
|
+
/// A stream over `file`, which it holds alone.
|
|
78
|
+
fn from(file: File) -> Self {
|
|
79
|
+
Stream::File(Arc::new(Mutex::new(file)))
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
impl Stream {
|
|
84
|
+
/// Another view of the file the stream stands on, as Linux's `get_file` takes a
|
|
85
|
+
/// reference to one `struct file`.
|
|
86
|
+
pub(crate) fn shared_file(&self) -> SharedFile {
|
|
87
|
+
match self {
|
|
88
|
+
Stream::Socket(socket) => socket.shared_file(),
|
|
89
|
+
Stream::File(file) => file.clone(),
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/// The file the stream stands on, which a read or a write takes `&mut`.
|
|
94
|
+
pub(crate) fn lock(&self) -> MutexGuard<'_, File> {
|
|
95
|
+
match self {
|
|
96
|
+
Stream::Socket(socket) => socket.file(),
|
|
97
|
+
Stream::File(file) => lock(file),
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/// Read what the stream holds now; `WouldBlock` while it holds nothing
|
|
102
|
+
/// yet, as `io.wit` makes a read non-blocking.
|
|
103
|
+
pub(crate) fn read(&self, buf: &mut [u8]) -> Result<usize, Errno> {
|
|
104
|
+
self.lock().read(buf)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/// Write what the stream takes now, never waiting.
|
|
108
|
+
pub(crate) fn write(&self, buf: &[u8]) -> Result<usize, Errno> {
|
|
109
|
+
self.lock().write(buf)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/// How many bytes a write takes now.
|
|
113
|
+
pub(crate) fn room(&self) -> usize {
|
|
114
|
+
match self {
|
|
115
|
+
Stream::Socket(socket) => socket.sock.send_room(),
|
|
116
|
+
// A file takes any write, so it offers what wasmtime-wasi offers
|
|
117
|
+
// a file's output stream.
|
|
118
|
+
Stream::File(_) => MAX_READ_SIZE_ALLOC,
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/// Whether what was written has left for the host, so a flush is done.
|
|
123
|
+
pub(crate) fn is_drained(&self) -> bool {
|
|
124
|
+
match self {
|
|
125
|
+
Stream::Socket(socket) => socket.sock.is_drained(),
|
|
126
|
+
Stream::File(_) => true,
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/// What a poll finds, registering on `table` for a poll that will sleep.
|
|
131
|
+
pub(crate) fn poll(&self, table: Option<&mut PollTable>) -> PollMask {
|
|
132
|
+
self.lock().poll(table)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/// A stream either way, which `io.wit` ends alike: once a call fails or
|
|
137
|
+
/// answers `closed`, every later call answers `closed`.
|
|
138
|
+
pub(crate) trait EndingStream {
|
|
139
|
+
/// Whether the stream reached that end.
|
|
140
|
+
fn closed(&self) -> &AtomicBool;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
pub(crate) struct InputStream {
|
|
144
|
+
pub(crate) stream: Stream,
|
|
145
|
+
/// The stream's end, which its TCP socket shares, so a shutdown of the
|
|
146
|
+
/// receiving side closes the stream, as `tcp.wit` has it.
|
|
147
|
+
closed: Arc<AtomicBool>,
|
|
148
|
+
/// The exchange whose body the stream reads, which bounds each wait.
|
|
149
|
+
pub(crate) exchange: Option<Arc<Exchange>>,
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
impl InputStream {
|
|
153
|
+
pub(crate) fn new(stream: Stream) -> Self {
|
|
154
|
+
Self::with_closed(stream, Arc::default())
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/// A stream whose end `closed` shares with the socket it reads.
|
|
158
|
+
pub(crate) fn with_closed(stream: Stream, closed: Arc<AtomicBool>) -> Self {
|
|
159
|
+
Self {
|
|
160
|
+
stream,
|
|
161
|
+
closed,
|
|
162
|
+
exchange: None,
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/// A stream reading `exchange`'s body.
|
|
167
|
+
pub(crate) fn for_body(stream: Stream, exchange: Arc<Exchange>) -> Self {
|
|
168
|
+
Self {
|
|
169
|
+
exchange: Some(exchange),
|
|
170
|
+
..Self::new(stream)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/// A pollable ready once the stream holds something to read, or a
|
|
175
|
+
/// timeout ends the wait for it.
|
|
176
|
+
pub(crate) fn pollable(&self) -> Pollable {
|
|
177
|
+
let file = self.stream.shared_file();
|
|
178
|
+
match &self.exchange {
|
|
179
|
+
Some(exchange) => Pollable::Exchange(file, exchange.clone()),
|
|
180
|
+
None => Pollable::File(file, PollMask::IN),
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
impl EndingStream for InputStream {
|
|
186
|
+
fn closed(&self) -> &AtomicBool {
|
|
187
|
+
&self.closed
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
pub(crate) struct OutputStream {
|
|
192
|
+
pub(crate) stream: Stream,
|
|
193
|
+
/// Whether a flush is under way, as `io.wit` has `check-write` answer zero
|
|
194
|
+
/// until it completes; the stream's pollables read it too.
|
|
195
|
+
pub(crate) flushing: Arc<AtomicBool>,
|
|
196
|
+
/// What the last `check-write` permitted, which each write carrying bytes
|
|
197
|
+
/// spends.
|
|
198
|
+
pub(crate) permit: usize,
|
|
199
|
+
/// The stream's end, which its pollables read too: a closed stream's
|
|
200
|
+
/// pollable is ready at once.
|
|
201
|
+
pub(crate) closed: Arc<AtomicBool>,
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
impl EndingStream for OutputStream {
|
|
205
|
+
fn closed(&self) -> &AtomicBool {
|
|
206
|
+
&self.closed
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
impl OutputStream {
|
|
211
|
+
pub(crate) fn new(stream: Stream) -> Self {
|
|
212
|
+
Self {
|
|
213
|
+
stream,
|
|
214
|
+
flushing: Arc::default(),
|
|
215
|
+
permit: 0,
|
|
216
|
+
closed: Arc::default(),
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/// Whether a flush is under way and what it flushes has not all left.
|
|
221
|
+
pub(crate) fn is_flushing(stream: &Stream, flushing: &AtomicBool) -> bool {
|
|
222
|
+
flushing.load(Ordering::Acquire) && !stream.is_drained()
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/// A stream's failure, as `wasi:io/error` hands one out.
|
|
227
|
+
pub(crate) struct Error(pub(crate) Errno);
|
|
228
|
+
|
|
229
|
+
/// What a pollable waits for.
|
|
230
|
+
#[derive(Clone)]
|
|
231
|
+
pub(crate) enum Pollable {
|
|
232
|
+
/// A clock, ready once `at` passes in host time.
|
|
233
|
+
Clock { at: Instant },
|
|
234
|
+
/// A file a host-side producer fills, ready once its poll finds
|
|
235
|
+
/// `PollMask`, or an error or a hang-up, as Linux's `poll` reports both
|
|
236
|
+
/// whatever was asked.
|
|
237
|
+
File(SharedFile, PollMask),
|
|
238
|
+
/// An output stream, its flush under way, and whether it is closed: ready
|
|
239
|
+
/// once the stream polls writable and no flush is still under way, or it
|
|
240
|
+
/// failed or closed, as `io.wit` has a closed stream's pollable ready at
|
|
241
|
+
/// once.
|
|
242
|
+
Output(Stream, Arc<AtomicBool>, Arc<AtomicBool>),
|
|
243
|
+
/// An exchange's response or body file, ready once it polls readable, or
|
|
244
|
+
/// once a timeout the exchange holds passes, which ends its wait.
|
|
245
|
+
Exchange(SharedFile, Arc<Exchange>),
|
|
246
|
+
/// A TCP socket, ready whenever no connect is under way as it is polled,
|
|
247
|
+
/// as wasmtime's socket pollable reads its operation in progress then,
|
|
248
|
+
/// so one pollable serves the socket's whole life.
|
|
249
|
+
TcpSocket(SocketFile<TcpSock>),
|
|
250
|
+
/// Something that is always ready, as a bind that settles as it starts
|
|
251
|
+
/// is.
|
|
252
|
+
Ready,
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
impl Pollable {
|
|
256
|
+
/// This pollable as a wait on `profile` holds it, beside a host-side
|
|
257
|
+
/// producer when `has_producer` says so: a clock is due as the profile has
|
|
258
|
+
/// it.
|
|
259
|
+
pub(crate) fn for_wait(self, profile: Profile, has_producer: bool) -> Self {
|
|
260
|
+
match self {
|
|
261
|
+
Pollable::Clock { at } => Pollable::Clock {
|
|
262
|
+
at: profile.clock_due(at, has_producer),
|
|
263
|
+
},
|
|
264
|
+
other => other,
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/// Whether it is ready, registering on `table` for a poll that will sleep.
|
|
269
|
+
pub(crate) fn is_ready(&self, table: Option<&mut PollTable>) -> bool {
|
|
270
|
+
match self {
|
|
271
|
+
Pollable::Clock { at } => Instant::now() >= *at,
|
|
272
|
+
Pollable::File(file, mask) => lock(file).poll(table).is_ready_for(*mask),
|
|
273
|
+
Pollable::Exchange(file, _) => lock(file).poll(table).is_ready_for(PollMask::IN),
|
|
274
|
+
Pollable::Output(stream, flushing, closed) => {
|
|
275
|
+
// Registering comes before the flush is read, so a writer
|
|
276
|
+
// draining it in between still wakes this wait. A flush under
|
|
277
|
+
// way asks for nothing more than an error or a hang-up, which
|
|
278
|
+
// every poll is answered.
|
|
279
|
+
let found = stream.poll(table);
|
|
280
|
+
let asked = if OutputStream::is_flushing(stream, flushing) {
|
|
281
|
+
PollMask::ERR
|
|
282
|
+
} else {
|
|
283
|
+
PollMask::OUT
|
|
284
|
+
};
|
|
285
|
+
closed.load(Ordering::Acquire) || found.is_ready_for(asked)
|
|
286
|
+
}
|
|
287
|
+
Pollable::TcpSocket(socket) => {
|
|
288
|
+
lock(&socket.file).poll(table);
|
|
289
|
+
!socket.sock.is_connecting()
|
|
290
|
+
}
|
|
291
|
+
Pollable::Ready => true,
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/// The soonest this could become ready by itself.
|
|
296
|
+
pub(crate) fn due(&self) -> Option<Instant> {
|
|
297
|
+
match self {
|
|
298
|
+
Pollable::Clock { at } => Some(*at),
|
|
299
|
+
Pollable::Exchange(_, exchange) => exchange.deadline(),
|
|
300
|
+
_ => None,
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/// Whether a host-side producer makes this ready, as a socket's peer, a
|
|
305
|
+
/// resolver, or a server does.
|
|
306
|
+
pub(crate) fn is_producer(&self) -> bool {
|
|
307
|
+
matches!(
|
|
308
|
+
self,
|
|
309
|
+
Pollable::File(..)
|
|
310
|
+
| Pollable::Exchange(..)
|
|
311
|
+
| Pollable::Output(..)
|
|
312
|
+
| Pollable::TcpSocket(..)
|
|
313
|
+
)
|
|
314
|
+
}
|
|
315
|
+
}
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
//! The `wasi:sockets` resources a guest holds, and the translations between
|
|
2
|
+
//! `wasi:sockets` and the runtime: an address, and the error code each POSIX
|
|
3
|
+
//! name maps to, as the WIT's "typical errors" lists pair them.
|
|
4
|
+
|
|
5
|
+
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
|
|
6
|
+
use std::sync::atomic::AtomicBool;
|
|
7
|
+
use std::sync::Arc;
|
|
8
|
+
|
|
9
|
+
use kobako_runtime::net::{LookupError, NameLookup, TcpSock, UdpSock};
|
|
10
|
+
use kobako_runtime::Errno;
|
|
11
|
+
|
|
12
|
+
use super::bindings::wasi_sockets_network_0_2_12::{
|
|
13
|
+
ErrorCode, IpAddress, IpAddressFamily, IpSocketAddress, Ipv4SocketAddress, Ipv6SocketAddress,
|
|
14
|
+
};
|
|
15
|
+
use super::io::{SharedFile, SocketFile};
|
|
16
|
+
|
|
17
|
+
/// A UDP socket, its file made as the socket is, and where WASI's state
|
|
18
|
+
/// machine has walked it.
|
|
19
|
+
pub(crate) struct UdpSocket {
|
|
20
|
+
pub(crate) socket: SocketFile<UdpSock>,
|
|
21
|
+
pub(crate) state: UdpState,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/// The steps of a UDP socket only WASI has: its bind starts and finishes
|
|
25
|
+
/// apart. Streaming once bound leaves a bind under way standing, as
|
|
26
|
+
/// wasmtime keeps its in-progress bind apart from its streams.
|
|
27
|
+
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
28
|
+
pub(crate) enum UdpState {
|
|
29
|
+
Unbound,
|
|
30
|
+
Binding,
|
|
31
|
+
Bound,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
impl UdpState {
|
|
35
|
+
/// Whether a bind began, which binds a UDP socket at once, so wasmtime's
|
|
36
|
+
/// `is_bound` reads it bound before `finish-bind`.
|
|
37
|
+
pub(crate) fn is_bound(&self) -> bool {
|
|
38
|
+
!matches!(self, UdpState::Unbound)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/// The one network a Sandbox's Grants make, as `instance-network` hands it
|
|
43
|
+
/// out.
|
|
44
|
+
pub(crate) struct Network;
|
|
45
|
+
|
|
46
|
+
/// A lookup, the file made for it as it starts, and how many of its
|
|
47
|
+
/// addresses were handed out.
|
|
48
|
+
pub(crate) struct ResolveAddressStream {
|
|
49
|
+
pub(crate) lookup: Arc<NameLookup>,
|
|
50
|
+
pub(crate) file: SharedFile,
|
|
51
|
+
pub(crate) handed: usize,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/// An incoming datagram stream and the peer it was made with, the only one
|
|
55
|
+
/// whose datagrams it hands out, as wasmtime keeps a stream's connected
|
|
56
|
+
/// address.
|
|
57
|
+
pub(crate) struct IncomingDatagramStream {
|
|
58
|
+
pub(crate) socket: SocketFile<UdpSock>,
|
|
59
|
+
pub(crate) peer: Option<SocketAddr>,
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/// An outgoing datagram stream, and how many datagrams its last `check-send`
|
|
63
|
+
/// permitted, as wasmtime keeps the permit a send spends.
|
|
64
|
+
pub(crate) struct OutgoingDatagramStream {
|
|
65
|
+
pub(crate) socket: SocketFile<UdpSock>,
|
|
66
|
+
pub(crate) permit: u64,
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/// A TCP socket, its file made as the socket is, and where WASI's state
|
|
70
|
+
/// machine has walked it.
|
|
71
|
+
pub(crate) struct TcpSocket {
|
|
72
|
+
pub(crate) socket: SocketFile<TcpSock>,
|
|
73
|
+
pub(crate) state: TcpState,
|
|
74
|
+
/// The end of the input stream a connect hands out, which a shutdown of
|
|
75
|
+
/// the receiving side sets.
|
|
76
|
+
pub(crate) input_closed: Arc<AtomicBool>,
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/// The steps of a TCP socket only WASI has: its streams are handed out once
|
|
80
|
+
/// `finish-connect` sees the connect land.
|
|
81
|
+
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
82
|
+
pub(crate) enum TcpState {
|
|
83
|
+
Unbound,
|
|
84
|
+
Binding,
|
|
85
|
+
Bound,
|
|
86
|
+
Connecting,
|
|
87
|
+
/// A connect that failed as it began, its error kept for `finish-connect`
|
|
88
|
+
/// to answer, as wasmtime's connecting future resolves to it.
|
|
89
|
+
ConnectFailed(ErrorCode),
|
|
90
|
+
Connected,
|
|
91
|
+
/// A connect that failed with its error, after which the socket takes no
|
|
92
|
+
/// other, as `tcp.wit` closes it.
|
|
93
|
+
Closed(ErrorCode),
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
impl TcpState {
|
|
97
|
+
/// Whether a bind or a connect is under way, a failed connect's error
|
|
98
|
+
/// not yet answered, as wasmtime keeps one `in_progress_operation`.
|
|
99
|
+
pub(crate) fn is_in_progress(self) -> bool {
|
|
100
|
+
matches!(
|
|
101
|
+
self,
|
|
102
|
+
Self::Binding | Self::Connecting | Self::ConnectFailed(_)
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/// Whether a socket option may be read or set now: as wasmtime's, a
|
|
107
|
+
/// socket connecting answers `invalid-state`, and one whose connect
|
|
108
|
+
/// failed answers its error.
|
|
109
|
+
pub(crate) fn ensure_takes_options(self) -> Result<(), ErrorCode> {
|
|
110
|
+
match self {
|
|
111
|
+
Self::Connecting | Self::ConnectFailed(_) => Err(ErrorCode::InvalidState),
|
|
112
|
+
Self::Closed(code) => Err(code),
|
|
113
|
+
Self::Unbound | Self::Binding | Self::Bound | Self::Connected => Ok(()),
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const NANOS_PER_SEC: u64 = 1_000_000_000;
|
|
119
|
+
|
|
120
|
+
/// A keepalive time in whole seconds, a part of one rounding up as rustix's
|
|
121
|
+
/// `duration_to_secs` does for wasmtime, clamped to 1 through `max`, the
|
|
122
|
+
/// runtime's ceiling; zero has none, as WIT answers it `invalid-argument`.
|
|
123
|
+
pub(crate) fn to_keepalive_secs(nanos: u64, max: u32) -> Option<u32> {
|
|
124
|
+
let secs = nanos.div_ceil(NANOS_PER_SEC).clamp(1, u64::from(max));
|
|
125
|
+
(nanos != 0).then(|| u32::try_from(secs).expect("clamped within u32"))
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/// The size a guest asked for, as a socket's queue holds twice it; wasmtime
|
|
129
|
+
/// halves Linux's answer the same way.
|
|
130
|
+
pub(crate) fn to_asked_size(held: u32) -> u64 {
|
|
131
|
+
u64::from(held / 2)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/// The hop limit an `IP_TTL` read answers, which the runtime keeps within a
|
|
135
|
+
/// byte.
|
|
136
|
+
pub(crate) fn to_hop_limit(ttl: u32) -> u8 {
|
|
137
|
+
u8::try_from(ttl).expect("the runtime keeps a hop limit within a byte")
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/// A buffer size a set takes; zero has none, as WIT answers it
|
|
141
|
+
/// `invalid-argument`.
|
|
142
|
+
pub(crate) fn to_buffer_size(value: u64) -> Option<u32> {
|
|
143
|
+
(value != 0).then(|| u32::try_from(value).unwrap_or(u32::MAX))
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/// The address the guest named for a socket of `family`, once it is of that
|
|
147
|
+
/// family, as wasmtime's `is_valid_address_family` asks: an IPv6 socket takes
|
|
148
|
+
/// no IPv4-mapped address and no deprecated IPv4-compatible one, which `::`
|
|
149
|
+
/// and `::1` are not, and an address of the other family has no answer.
|
|
150
|
+
pub(crate) fn to_socket_addr(
|
|
151
|
+
addr: &IpSocketAddress,
|
|
152
|
+
family: IpAddressFamily,
|
|
153
|
+
) -> Option<SocketAddr> {
|
|
154
|
+
match (family, addr) {
|
|
155
|
+
(
|
|
156
|
+
IpAddressFamily::Ipv4,
|
|
157
|
+
IpSocketAddress::Ipv4(Ipv4SocketAddress {
|
|
158
|
+
port,
|
|
159
|
+
address: (a, b, c, d),
|
|
160
|
+
}),
|
|
161
|
+
) => Some(SocketAddr::V4(SocketAddrV4::new(
|
|
162
|
+
Ipv4Addr::new(*a, *b, *c, *d),
|
|
163
|
+
*port,
|
|
164
|
+
))),
|
|
165
|
+
(
|
|
166
|
+
IpAddressFamily::Ipv6,
|
|
167
|
+
IpSocketAddress::Ipv6(Ipv6SocketAddress {
|
|
168
|
+
port,
|
|
169
|
+
flow_info,
|
|
170
|
+
address: (a, b, c, d, e, f, g, h),
|
|
171
|
+
scope_id,
|
|
172
|
+
}),
|
|
173
|
+
) => {
|
|
174
|
+
let ip = Ipv6Addr::new(*a, *b, *c, *d, *e, *f, *g, *h);
|
|
175
|
+
let embeds_ipv4 = ip.to_ipv4_mapped().is_some()
|
|
176
|
+
|| (matches!(ip.segments(), [0, 0, 0, 0, 0, 0, _, _])
|
|
177
|
+
&& ip != Ipv6Addr::UNSPECIFIED
|
|
178
|
+
&& ip != Ipv6Addr::LOCALHOST);
|
|
179
|
+
(!embeds_ipv4)
|
|
180
|
+
.then(|| SocketAddr::V6(SocketAddrV6::new(ip, *port, *flow_info, *scope_id)))
|
|
181
|
+
}
|
|
182
|
+
_ => None,
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/// Whether a TCP socket may bind or connect to `addr`, as wasmtime's
|
|
187
|
+
/// `is_valid_unicast_address` asks only of TCP: no multicast and no broadcast
|
|
188
|
+
/// address.
|
|
189
|
+
pub(crate) fn is_valid_unicast_address(addr: SocketAddr) -> bool {
|
|
190
|
+
match addr {
|
|
191
|
+
SocketAddr::V4(v4) => !v4.ip().is_multicast() && !v4.ip().is_broadcast(),
|
|
192
|
+
SocketAddr::V6(v6) => !v6.ip().is_multicast(),
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/// The peer the guest named, if it may be reached, as wasmtime's
|
|
197
|
+
/// `is_valid_remote_address` asks: neither unspecified nor at port 0.
|
|
198
|
+
pub(crate) fn to_remote_addr(
|
|
199
|
+
addr: &IpSocketAddress,
|
|
200
|
+
family: IpAddressFamily,
|
|
201
|
+
) -> Option<SocketAddr> {
|
|
202
|
+
to_socket_addr(addr, family).filter(|addr| !addr.ip().is_unspecified() && addr.port() != 0)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/// `ip` as the guest reads it.
|
|
206
|
+
pub(crate) fn to_ip_address(ip: IpAddr) -> IpAddress {
|
|
207
|
+
match ip {
|
|
208
|
+
IpAddr::V4(v4) => {
|
|
209
|
+
let [a, b, c, d] = v4.octets();
|
|
210
|
+
IpAddress::Ipv4((a, b, c, d))
|
|
211
|
+
}
|
|
212
|
+
IpAddr::V6(v6) => {
|
|
213
|
+
let [a, b, c, d, e, f, g, h] = v6.segments();
|
|
214
|
+
IpAddress::Ipv6((a, b, c, d, e, f, g, h))
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/// `addr` as the guest reads it.
|
|
220
|
+
pub(crate) fn from_socket_addr(addr: SocketAddr) -> IpSocketAddress {
|
|
221
|
+
match addr {
|
|
222
|
+
SocketAddr::V4(v4) => {
|
|
223
|
+
let [a, b, c, d] = v4.ip().octets();
|
|
224
|
+
IpSocketAddress::Ipv4(Ipv4SocketAddress {
|
|
225
|
+
port: v4.port(),
|
|
226
|
+
address: (a, b, c, d),
|
|
227
|
+
})
|
|
228
|
+
}
|
|
229
|
+
SocketAddr::V6(v6) => {
|
|
230
|
+
let [a, b, c, d, e, f, g, h] = v6.ip().segments();
|
|
231
|
+
IpSocketAddress::Ipv6(Ipv6SocketAddress {
|
|
232
|
+
port: v6.port(),
|
|
233
|
+
flow_info: v6.flowinfo(),
|
|
234
|
+
address: (a, b, c, d, e, f, g, h),
|
|
235
|
+
scope_id: v6.scope_id(),
|
|
236
|
+
})
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/// The error code a lookup's failure answers, as `ip-name-lookup` names
|
|
242
|
+
/// each `EAI_` code.
|
|
243
|
+
pub(crate) fn to_lookup_error_code(err: LookupError) -> ErrorCode {
|
|
244
|
+
match err {
|
|
245
|
+
LookupError::InvalidName => ErrorCode::InvalidArgument,
|
|
246
|
+
LookupError::NoName => ErrorCode::NameUnresolvable,
|
|
247
|
+
LookupError::Again => ErrorCode::TemporaryResolverFailure,
|
|
248
|
+
_ => ErrorCode::PermanentResolverFailure,
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/// The error code a failure's POSIX name maps to.
|
|
253
|
+
pub(crate) fn to_error_code(err: &Errno) -> ErrorCode {
|
|
254
|
+
match err {
|
|
255
|
+
Errno::PermissionDenied | Errno::NotPermitted => ErrorCode::AccessDenied,
|
|
256
|
+
Errno::Unsupported | Errno::AddressFamilyNotSupported | Errno::ProtocolNotAvailable => {
|
|
257
|
+
ErrorCode::NotSupported
|
|
258
|
+
}
|
|
259
|
+
Errno::InvalidInput => ErrorCode::InvalidArgument,
|
|
260
|
+
Errno::OutOfMemory | Errno::NoBufferSpace => ErrorCode::OutOfMemory,
|
|
261
|
+
Errno::TimedOut => ErrorCode::Timeout,
|
|
262
|
+
Errno::WouldBlock => ErrorCode::WouldBlock,
|
|
263
|
+
Errno::TooManyOpenFiles | Errno::TooManyOpenFilesInSystem => ErrorCode::NewSocketLimit,
|
|
264
|
+
Errno::AddressNotAvailable => ErrorCode::AddressNotBindable,
|
|
265
|
+
Errno::AddressInUse => ErrorCode::AddressInUse,
|
|
266
|
+
Errno::HostUnreachable
|
|
267
|
+
| Errno::HostDown
|
|
268
|
+
| Errno::NetworkUnreachable
|
|
269
|
+
| Errno::NetworkDown
|
|
270
|
+
| Errno::MachineNotOnNetwork => ErrorCode::RemoteUnreachable,
|
|
271
|
+
Errno::ConnectionRefused => ErrorCode::ConnectionRefused,
|
|
272
|
+
Errno::ConnectionReset => ErrorCode::ConnectionReset,
|
|
273
|
+
Errno::ConnectionAborted => ErrorCode::ConnectionAborted,
|
|
274
|
+
Errno::NotConnected | Errno::AlreadyConnected | Errno::DestinationAddressRequired => {
|
|
275
|
+
ErrorCode::InvalidState
|
|
276
|
+
}
|
|
277
|
+
Errno::MessageTooLong => ErrorCode::DatagramTooLarge,
|
|
278
|
+
// WASI 0.2 names no broken pipe, so EPIPE answers `unknown`, as
|
|
279
|
+
// wasmtime-wasi's preview2 does.
|
|
280
|
+
_ => ErrorCode::Unknown,
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
#[cfg(test)]
|
|
285
|
+
mod tests {
|
|
286
|
+
use super::*;
|
|
287
|
+
|
|
288
|
+
fn ipv6(address: (u16, u16, u16, u16, u16, u16, u16, u16)) -> IpSocketAddress {
|
|
289
|
+
IpSocketAddress::Ipv6(Ipv6SocketAddress {
|
|
290
|
+
port: 80,
|
|
291
|
+
flow_info: 0,
|
|
292
|
+
address,
|
|
293
|
+
scope_id: 0,
|
|
294
|
+
})
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
#[test]
|
|
298
|
+
fn an_ipv6_socket_takes_only_an_address_that_embeds_no_ipv4_one() {
|
|
299
|
+
let taken = [
|
|
300
|
+
ipv6((0, 0, 0, 0, 0, 0, 0, 1)),
|
|
301
|
+
ipv6((0, 0, 0, 0, 0, 0, 0, 0)),
|
|
302
|
+
ipv6((0x2001, 0xdb8, 0, 0, 0, 0, 0, 1)),
|
|
303
|
+
ipv6((0, 0, 0, 0, 0, 0xffff, 0x7f00, 1)),
|
|
304
|
+
ipv6((0, 0, 0, 0, 0, 0, 0x7f00, 1)),
|
|
305
|
+
IpSocketAddress::Ipv4(Ipv4SocketAddress {
|
|
306
|
+
port: 80,
|
|
307
|
+
address: (127, 0, 0, 1),
|
|
308
|
+
}),
|
|
309
|
+
]
|
|
310
|
+
.map(|addr| to_socket_addr(&addr, IpAddressFamily::Ipv6).is_some());
|
|
311
|
+
assert_eq!(
|
|
312
|
+
taken,
|
|
313
|
+
[true, true, true, false, false, false],
|
|
314
|
+
"an IPv6 socket must take ::1, ::, and 2001:db8::1, and refuse ::ffff:127.0.0.1, \
|
|
315
|
+
::127.0.0.1, and an IPv4 address, as wasmtime-wasi's address family check does"
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// @behavior NW-228
|
|
320
|
+
#[test]
|
|
321
|
+
fn a_lookup_failure_answers_the_code_its_eai_kind_names() {
|
|
322
|
+
assert_eq!(
|
|
323
|
+
[LookupError::NoName, LookupError::Again, LookupError::Fail].map(to_lookup_error_code),
|
|
324
|
+
[
|
|
325
|
+
ErrorCode::NameUnresolvable,
|
|
326
|
+
ErrorCode::TemporaryResolverFailure,
|
|
327
|
+
ErrorCode::PermanentResolverFailure,
|
|
328
|
+
],
|
|
329
|
+
"a lookup failing as EAI_NONAME, EAI_AGAIN, and EAI_FAIL must answer \
|
|
330
|
+
name-unresolvable, temporary-resolver-failure, and permanent-resolver-failure"
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// @behavior NW-074
|
|
335
|
+
#[test]
|
|
336
|
+
fn a_host_failure_answers_the_code_wasmtime_wasi_gives_it() {
|
|
337
|
+
assert_eq!(
|
|
338
|
+
[
|
|
339
|
+
Errno::BrokenPipe,
|
|
340
|
+
Errno::AddressInUse,
|
|
341
|
+
Errno::ConnectionReset,
|
|
342
|
+
Errno::NetworkDown,
|
|
343
|
+
Errno::HostDown,
|
|
344
|
+
Errno::MachineNotOnNetwork,
|
|
345
|
+
Errno::NoBufferSpace,
|
|
346
|
+
Errno::DestinationAddressRequired,
|
|
347
|
+
Errno::ProtocolNotAvailable,
|
|
348
|
+
Errno::TooManyOpenFiles,
|
|
349
|
+
Errno::TooManyOpenFilesInSystem,
|
|
350
|
+
Errno::MessageTooLong,
|
|
351
|
+
Errno::WouldBlock,
|
|
352
|
+
Errno::InvalidInput,
|
|
353
|
+
]
|
|
354
|
+
.map(|err| to_error_code(&err)),
|
|
355
|
+
[
|
|
356
|
+
ErrorCode::Unknown,
|
|
357
|
+
ErrorCode::AddressInUse,
|
|
358
|
+
ErrorCode::ConnectionReset,
|
|
359
|
+
ErrorCode::RemoteUnreachable,
|
|
360
|
+
ErrorCode::RemoteUnreachable,
|
|
361
|
+
ErrorCode::RemoteUnreachable,
|
|
362
|
+
ErrorCode::OutOfMemory,
|
|
363
|
+
ErrorCode::InvalidState,
|
|
364
|
+
ErrorCode::NotSupported,
|
|
365
|
+
ErrorCode::NewSocketLimit,
|
|
366
|
+
ErrorCode::NewSocketLimit,
|
|
367
|
+
ErrorCode::DatagramTooLarge,
|
|
368
|
+
ErrorCode::WouldBlock,
|
|
369
|
+
ErrorCode::InvalidArgument,
|
|
370
|
+
],
|
|
371
|
+
"EPIPE, EADDRINUSE, ECONNRESET, ENETDOWN, EHOSTDOWN, ENONET, ENOBUFS, EDESTADDRREQ, \
|
|
372
|
+
ENOPROTOOPT, EMFILE, ENFILE, EMSGSIZE, EAGAIN, and EINVAL must answer unknown, \
|
|
373
|
+
address-in-use, connection-reset, remote-unreachable three times, out-of-memory, \
|
|
374
|
+
invalid-state, not-supported, \
|
|
375
|
+
new-socket-limit twice, datagram-too-large, would-block, and invalid-argument, as \
|
|
376
|
+
wasmtime-wasi maps them"
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
}
|