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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// SPDX-License-Identifier: Apache-2.0

use crate::guest::alloc::{
    Allocator, Collect, Collector, Commit, Committer, InOut, Input, Output, Stage,
};
use crate::libc::{sockaddr_in, sockaddr_in6, sockaddr_storage, sockaddr_un, socklen_t, EOVERFLOW};
use crate::Result;

use core::alloc::Layout;
use core::ffi::c_void;
use core::mem::{align_of, size_of};
use core::ptr::NonNull;
use core::slice;

pub struct SockaddrInput<'a>(pub &'a [u8]);

pub type StagedSockaddrInput<'a> = Input<'a, [u8], &'a [u8]>;

impl<'a> From<&'a [u8]> for SockaddrInput<'a> {
    #[inline]
    fn from(addr: &'a [u8]) -> Self {
        Self(addr)
    }
}

impl<'a> From<&'a sockaddr_un> for SockaddrInput<'a> {
    #[inline]
    fn from(addr: &'a sockaddr_un) -> Self {
        Self(unsafe { slice::from_raw_parts(addr as *const _ as _, size_of::<sockaddr_un>()) })
    }
}

impl<'a> From<&'a sockaddr_in> for SockaddrInput<'a> {
    #[inline]
    fn from(addr: &'a sockaddr_in) -> Self {
        Self(unsafe { slice::from_raw_parts(addr as *const _ as _, size_of::<sockaddr_in>()) })
    }
}

impl<'a> From<&'a sockaddr_in6> for SockaddrInput<'a> {
    #[inline]
    fn from(addr: &'a sockaddr_in6) -> Self {
        Self(unsafe { slice::from_raw_parts(addr as *const _ as _, size_of::<sockaddr_in6>()) })
    }
}

impl<'a> Stage<'a> for SockaddrInput<'a> {
    type Item = StagedSockaddrInput<'a>;

    #[inline]
    fn stage(self, alloc: &mut impl Allocator) -> Result<Self::Item> {
        let layout = Layout::from_size_align(self.0.len(), align_of::<sockaddr_storage>())
            .map_err(|_| EOVERFLOW)?;
        let addr = alloc.allocate_input_layout(layout)?;
        Ok(unsafe { Input::new_unchecked(addr, self.0) })
    }
}

pub struct SockaddrOutput<'a> {
    pub addr: &'a mut [u8],
    pub addrlen: &'a mut socklen_t,
}

impl<'a> From<(&'a mut [u8], &'a mut socklen_t)> for SockaddrOutput<'a> {
    #[inline]
    fn from((addr, addrlen): (&'a mut [u8], &'a mut socklen_t)) -> Self {
        debug_assert_eq!(addr.len(), *addrlen as _);
        Self::new(addr, addrlen)
    }
}

impl<'a, T> From<(&'a mut T, &'a mut socklen_t)> for SockaddrOutput<'a> {
    #[inline]
    fn from((addr, addrlen): (&'a mut T, &'a mut socklen_t)) -> Self {
        debug_assert!(align_of::<T>() <= align_of::<sockaddr_storage>());
        debug_assert_eq!(size_of::<T>(), *addrlen as _);
        Self::new(
            unsafe { slice::from_raw_parts_mut(addr as *const _ as _, size_of::<T>()) },
            addrlen,
        )
    }
}

impl<'a> SockaddrOutput<'a> {
    #[inline]
    pub fn new(addr: &'a mut [u8], addrlen: &'a mut socklen_t) -> Self {
        Self { addr, addrlen }
    }
}

pub struct StagedSockaddrOutput<'a> {
    pub addr: Output<'a, [u8], &'a mut [u8]>,
    pub addrlen: InOut<'a, socklen_t, &'a mut socklen_t>,
}

pub struct CommittedSockaddrOutput<'a> {
    pub addr: Output<'a, [u8], &'a mut [u8]>,
    pub addrlen: Output<'a, socklen_t, &'a mut socklen_t>,
}

impl<'a> Stage<'a> for SockaddrOutput<'a> {
    type Item = StagedSockaddrOutput<'a>;

    #[inline]
    fn stage(self, alloc: &mut impl Allocator) -> Result<Self::Item> {
        let layout = Layout::from_size_align(self.addr.len(), align_of::<sockaddr_storage>())
            .map_err(|_| EOVERFLOW)?;
        let addr = alloc.allocate_output_layout(layout)?;
        let addrlen = InOut::stage(alloc, self.addrlen)?;
        Ok(Self::Item {
            addr: unsafe { Output::new_unchecked(addr, self.addr) },
            addrlen,
        })
    }
}

impl<'a> Commit for StagedSockaddrOutput<'a> {
    type Item = CommittedSockaddrOutput<'a>;

    #[inline]
    fn commit(self, com: &impl Committer) -> Self::Item {
        Self::Item {
            addr: self.addr,
            addrlen: self.addrlen.commit(com),
        }
    }
}

impl<'a> Collect for CommittedSockaddrOutput<'a> {
    type Item = ();

    #[inline]
    fn collect(self, col: &impl Collector) {
        let addrlen = *self.addrlen.collect(col);
        let len = self.addr.len().min(addrlen as _);
        unsafe { self.addr.collect_range(col, 0..len) };
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[allow(non_snake_case)]
pub struct MremapFlags {
    pub FIXED: Option<NonNull<c_void>>,
    pub DONTUNMAP: bool,
}