Skip to main content

dope_core/io/socket/
msg.rs

1use core::ptr;
2
3#[derive(Clone, Copy, Debug)]
4#[repr(transparent)]
5pub struct IoVec {
6    raw: libc::iovec,
7}
8
9impl IoVec {
10    #[must_use]
11    pub const fn empty() -> Self {
12        Self {
13            raw: libc::iovec {
14                iov_base: ptr::null_mut(),
15                iov_len: 0,
16            },
17        }
18    }
19
20    #[must_use]
21    pub fn from_slice(buf: &[u8]) -> Self {
22        Self {
23            raw: libc::iovec {
24                iov_base: buf.as_ptr() as *mut libc::c_void,
25                iov_len: buf.len(),
26            },
27        }
28    }
29
30    #[must_use]
31    pub fn from_mut_slice(buf: &mut [u8]) -> Self {
32        Self {
33            raw: libc::iovec {
34                iov_base: buf.as_mut_ptr() as *mut libc::c_void,
35                iov_len: buf.len(),
36            },
37        }
38    }
39
40    #[must_use]
41    pub fn len(&self) -> usize {
42        self.raw.iov_len
43    }
44
45    #[must_use]
46    pub fn is_empty(&self) -> bool {
47        self.raw.iov_len == 0
48    }
49
50    #[must_use]
51    pub fn as_ptr(&self) -> *const u8 {
52        self.raw.iov_base as *const u8
53    }
54}
55
56#[derive(Clone, Copy, Debug)]
57#[repr(transparent)]
58pub struct MsgHdr {
59    raw: libc::msghdr,
60}
61
62impl MsgHdr {
63    pub fn empty() -> Self {
64        Self {
65            raw: libc::msghdr {
66                msg_name: ptr::null_mut(),
67                msg_namelen: 0,
68                msg_iov: ptr::null_mut(),
69                msg_iovlen: 0,
70                msg_control: ptr::null_mut(),
71                msg_controllen: 0,
72                msg_flags: 0,
73            },
74        }
75    }
76
77    pub fn set_name_ptr(&mut self, ptr: *mut libc::c_void, len: u32) {
78        self.raw.msg_name = ptr;
79        self.raw.msg_namelen = len;
80    }
81
82    pub fn set_namelen(&mut self, len: u32) {
83        self.raw.msg_namelen = len;
84    }
85
86    pub fn set_iov(&mut self, iov: &[IoVec]) {
87        self.raw.msg_iov = iov.as_ptr() as *mut libc::iovec;
88        self.raw.msg_iovlen = iov.len() as _;
89    }
90
91    pub fn set_control(&mut self, ptr: *mut libc::c_void, len: usize) {
92        self.raw.msg_control = ptr;
93        self.raw.msg_controllen = len as _;
94    }
95
96    pub fn flags(&self) -> libc::c_int {
97        self.raw.msg_flags
98    }
99
100    pub fn raw(&self) -> &libc::msghdr {
101        &self.raw
102    }
103
104    pub fn as_mut_ptr(&mut self) -> *mut libc::msghdr {
105        ptr::addr_of_mut!(self.raw)
106    }
107}