-
Notifications
You must be signed in to change notification settings - Fork 290
Expand file tree
/
Copy pathcmsg.rs
More file actions
202 lines (177 loc) · 6.56 KB
/
cmsg.rs
File metadata and controls
202 lines (177 loc) · 6.56 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use std::fmt;
use std::mem;
/// Returns the space required in a control message buffer for a single message
/// with `data_len` bytes of ancillary data.
///
/// Returns `None` if `data_len` does not fit in `libc::c_uint`.
///
/// Corresponds to `CMSG_SPACE(3)`.
pub fn cmsg_space(data_len: usize) -> Option<usize> {
let len = libc::c_uint::try_from(data_len).ok()?;
// SAFETY: pure arithmetic.
usize::try_from(unsafe { libc::CMSG_SPACE(len) }).ok()
}
/// A control message parsed from a `recvmsg(2)` control buffer.
///
/// Returned by [`ControlMessages`].
pub struct ControlMessage<'a> {
cmsg_level: i32,
cmsg_type: i32,
data: &'a [u8],
}
impl<'a> ControlMessage<'a> {
/// Corresponds to `cmsg_level` in `cmsghdr`.
pub fn cmsg_level(&self) -> i32 {
self.cmsg_level
}
/// Corresponds to `cmsg_type` in `cmsghdr`.
pub fn cmsg_type(&self) -> i32 {
self.cmsg_type
}
/// The ancillary data payload.
///
/// Corresponds to the data portion following the `cmsghdr`.
pub fn data(&self) -> &'a [u8] {
self.data
}
}
impl<'a> fmt::Debug for ControlMessage<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
"ControlMessage".fmt(fmt)
}
}
/// Iterator over control messages in a `recvmsg(2)` control buffer.
///
/// See [`crate::MsgHdrMut::with_control`] and [`crate::MsgHdrMut::control_len`].
pub struct ControlMessages<'a> {
buf: &'a [u8],
offset: usize,
}
impl<'a> ControlMessages<'a> {
/// Create a new `ControlMessages` from the filled control buffer.
///
/// Pass `&raw_buf[..msg.control_len()]` where `raw_buf` is the slice
/// passed to [`crate::MsgHdrMut::with_control`] before calling `recvmsg(2)`.
pub fn new(buf: &'a [u8]) -> Self {
Self { buf, offset: 0 }
}
}
impl<'a> Iterator for ControlMessages<'a> {
type Item = ControlMessage<'a>;
#[allow(clippy::useless_conversion)]
fn next(&mut self) -> Option<Self::Item> {
let hdr_size = mem::size_of::<libc::cmsghdr>();
// SAFETY: pure arithmetic; gives CMSG_ALIGN(sizeof(cmsghdr)).
let data_offset: usize =
usize::try_from(unsafe { libc::CMSG_LEN(0) }).unwrap_or(usize::MAX);
if self.offset + hdr_size > self.buf.len() {
return None;
}
// SAFETY: range is within `buf`; read_unaligned handles any alignment.
let cmsg: libc::cmsghdr = unsafe {
std::ptr::read_unaligned(self.buf.as_ptr().add(self.offset) as *const libc::cmsghdr)
};
let total_len = usize::try_from(cmsg.cmsg_len).unwrap_or(0);
if total_len < data_offset {
return None;
}
let data_len = total_len - data_offset;
let data_abs_start = self.offset + data_offset;
let data_abs_end = data_abs_start.saturating_add(data_len);
if data_abs_end > self.buf.len() {
return None;
}
let item = ControlMessage {
cmsg_level: cmsg.cmsg_level,
cmsg_type: cmsg.cmsg_type,
data: &self.buf[data_abs_start..data_abs_end],
};
// SAFETY: pure arithmetic; CMSG_SPACE(data_len) == CMSG_ALIGN(total_len).
let advance = match libc::c_uint::try_from(data_len) {
Ok(dl) => usize::try_from(unsafe { libc::CMSG_SPACE(dl) }).unwrap_or(usize::MAX),
Err(_) => return None,
};
self.offset = self.offset.saturating_add(advance);
Some(item)
}
}
impl<'a> fmt::Debug for ControlMessages<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
"ControlMessages".fmt(fmt)
}
}
/// Builds a control message buffer for use with `sendmsg(2)`.
///
/// See [`crate::MsgHdr::with_control`] and [`cmsg_space`].
pub struct ControlMessageEncoder<'a> {
buf: &'a mut [u8],
len: usize,
}
impl<'a> ControlMessageEncoder<'a> {
/// Create a new `ControlMessageEncoder` backed by `buf`.
///
/// Zeroes `buf` on creation to ensure padding bytes are clean.
/// Allocate `buf` with the sum of [`cmsg_space`] for each intended message.
pub fn new(buf: &'a mut [u8]) -> Self {
buf.fill(0);
Self { buf, len: 0 }
}
/// Append a control message carrying `data`.
///
/// Returns `Err` if `data` exceeds `c_uint::MAX` or the buffer is too small.
pub fn push(&mut self, cmsg_level: i32, cmsg_type: i32, data: &[u8]) -> std::io::Result<()> {
let data_len_uint = libc::c_uint::try_from(data.len()).map_err(|_| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"ancillary data payload too large (exceeds c_uint::MAX)",
)
})?;
// SAFETY: pure arithmetic.
let space: usize =
usize::try_from(unsafe { libc::CMSG_SPACE(data_len_uint) }).unwrap_or(usize::MAX);
if self.len + space > self.buf.len() {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"control message buffer too small",
));
}
// SAFETY: pure arithmetic.
let cmsg_len = unsafe { libc::CMSG_LEN(data_len_uint) };
unsafe {
// SAFETY: offset is within buf; write_unaligned handles alignment 1.
// Use zeroed() + field assignment to handle platform-specific padding
// (e.g. musl adds __pad1); buf is pre-zeroed but the write must be
// self-contained for correctness.
let cmsg_ptr = self.buf.as_mut_ptr().add(self.len) as *mut libc::cmsghdr;
let mut hdr: libc::cmsghdr = mem::zeroed();
hdr.cmsg_len = cmsg_len as _;
hdr.cmsg_level = cmsg_level;
hdr.cmsg_type = cmsg_type;
std::ptr::write_unaligned(cmsg_ptr, hdr);
// SAFETY: CMSG_DATA gives the correct offset past alignment padding.
let data_ptr = libc::CMSG_DATA(cmsg_ptr);
std::ptr::copy_nonoverlapping(data.as_ptr(), data_ptr, data.len());
}
self.len += space;
Ok(())
}
/// Returns the encoded bytes.
///
/// Corresponds to the slice to pass to [`crate::MsgHdr::with_control`].
pub fn as_bytes(&self) -> &[u8] {
&self.buf[..self.len]
}
/// Returns the number of bytes written.
pub fn len(&self) -> usize {
self.len
}
/// Returns `true` if no control messages have been pushed.
pub fn is_empty(&self) -> bool {
self.len == 0
}
}
impl<'a> fmt::Debug for ControlMessageEncoder<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
"ControlMessageEncoder".fmt(fmt)
}
}