wasmer_wasix/syscalls/wasix/
port_addr_add.rs

1use virtual_net::IpCidr;
2
3use super::*;
4use crate::syscalls::*;
5
6/// ### `port_addr_add()`
7/// Adds another static address to the local port
8///
9/// ## Parameters
10///
11/// * `addr` - Address to be added
12#[instrument(level = "trace", skip_all, fields(ip = field::Empty), ret)]
13pub fn port_addr_add<M: MemorySize>(
14    mut ctx: FunctionEnvMut<'_, WasiEnv>,
15    ip: WasmPtr<__wasi_cidr_t, M>,
16) -> Result<Errno, WasiError> {
17    WasiEnv::do_pending_operations(&mut ctx)?;
18
19    let env = ctx.data();
20    let memory = unsafe { env.memory_view(&ctx) };
21
22    let cidr = wasi_try_ok!(crate::net::read_cidr(&memory, ip));
23    Span::current().record("ip", format!("{cidr:?}"));
24
25    wasi_try_ok!(port_addr_add_internal(&mut ctx, cidr)?);
26
27    #[cfg(feature = "journal")]
28    if ctx.data().enable_journal {
29        JournalEffector::save_port_addr_add(&mut ctx, cidr).map_err(|err| {
30            tracing::error!("failed to save port_addr_add event - {}", err);
31            WasiError::Exit(ExitCode::from(Errno::Fault))
32        })?;
33    }
34
35    Ok(Errno::Success)
36}
37
38pub(crate) fn port_addr_add_internal(
39    ctx: &mut FunctionEnvMut<'_, WasiEnv>,
40    cidr: IpCidr,
41) -> Result<Result<(), Errno>, WasiError> {
42    let env = ctx.data();
43    let net = env.net().clone();
44    wasi_try_ok_ok!(__asyncify(ctx, None, async {
45        net.ip_add(cidr.ip, cidr.prefix)
46            .await
47            .map_err(net_error_into_wasi_err)
48    })?);
49    Ok(Ok(()))
50}