1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use std::net::SocketAddr;

use wasmer_wasix_types::wasi::{Addressfamily, SockProto, Socktype};

use crate::{
    fs::Kind,
    net::socket::{InodeSocket, InodeSocketKind, SocketProperties},
};

use super::*;

impl JournalEffector {
    pub fn save_sock_accepted(
        ctx: &mut FunctionEnvMut<'_, WasiEnv>,
        listen_fd: Fd,
        fd: Fd,
        addr: SocketAddr,
        peer_addr: SocketAddr,
        fd_flags: Fdflags,
        nonblocking: bool,
    ) -> anyhow::Result<()> {
        Self::save_event(
            ctx,
            JournalEntry::SocketAcceptedV1 {
                listen_fd,
                fd,
                local_addr: addr,
                peer_addr,
                fd_flags,
                non_blocking: nonblocking,
            },
        )
    }

    pub fn apply_sock_accepted(
        ctx: &mut FunctionEnvMut<'_, WasiEnv>,
        _listen_fd: Fd,
        fd: Fd,
        addr: SocketAddr,
        peer_addr: SocketAddr,
        fd_flags: Fdflags,
        nonblocking: bool,
    ) -> anyhow::Result<()> {
        let kind = Kind::Socket {
            socket: InodeSocket::new(InodeSocketKind::RemoteSocket {
                is_dead: true,
                local_addr: addr,
                peer_addr,
                ttl: 0,
                multicast_ttl: 0,
                props: SocketProperties {
                    family: match peer_addr.is_ipv4() {
                        true => Addressfamily::Inet4,
                        false => Addressfamily::Inet6,
                    },
                    ty: Socktype::Stream,
                    pt: SockProto::Tcp,
                    only_v6: false,
                    reuse_port: false,
                    reuse_addr: false,
                    no_delay: None,
                    keep_alive: None,
                    dont_route: None,
                    send_buf_size: None,
                    recv_buf_size: None,
                    write_timeout: None,
                    read_timeout: None,
                    accept_timeout: None,
                    connect_timeout: None,
                    handler: None,
                },
            }),
        };

        let env = ctx.data();
        let state = env.state();
        let inodes = &state.inodes;
        let inode = state
            .fs
            .create_inode_with_default_stat(inodes, kind, false, "socket".into());

        let mut new_flags = Fdflags::empty();
        if nonblocking {
            new_flags.set(Fdflags::NONBLOCK, true);
        }

        let mut new_flags = Fdflags::empty();
        if fd_flags.contains(Fdflags::NONBLOCK) {
            new_flags.set(Fdflags::NONBLOCK, true);
        }

        let rights = Rights::all_socket();
        let ret = state.fs.with_fd(rights, rights, new_flags, 0, inode, fd);
        ret.map_err(|err| {
            anyhow::format_err!(
                "journal restore error: failed to create remote accepted socket - {}",
                err
            )
        })?;

        Ok(())
    }
}