wasmer_wasix/syscalls/wasix/
port_gateway_set.rs

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