| 1 |
use std::{ |
| 2 |
ffi::{c_int, c_uchar}, |
| 3 |
mem, ptr, |
| 4 |
}; |
| 5 |
|
| 6 |
#[cfg(unix)] |
| 7 |
#[path = "unix.rs"] |
| 8 |
mod imp; |
| 9 |
|
| 10 |
#[cfg(windows)] |
| 11 |
#[path = "windows.rs"] |
| 12 |
mod imp; |
| 13 |
|
| 14 |
pub(crate) use imp::Aligned; |
| 15 |
|
| 16 |
/// Helper to encode a series of control messages (native "cmsgs") to a buffer for use in `sendmsg` |
| 17 |
// like API. |
| 18 |
/// |
| 19 |
/// The operation must be "finished" for the native msghdr to be usable, either by calling `finish` |
| 20 |
/// explicitly or by dropping the `Encoder`. |
| 21 |
pub(crate) struct Encoder<'a, M: MsgHdr> { |
| 22 |
hdr: &'a mut M, |
| 23 |
cmsg: Option<&'a mut M::ControlMessage>, |
| 24 |
len: usize, |
| 25 |
} |
| 26 |
|
| 27 |
impl<'a, M: MsgHdr> Encoder<'a, M> { |
| 28 |
/// # Safety |
| 29 |
/// - `hdr` must contain a suitably aligned pointer to a big enough buffer to hold control messages |
| 30 |
/// bytes. All bytes of this buffer can be safely written. |
| 31 |
/// - The `Encoder` must be dropped before `hdr` is passed to a system call, and must not be leaked. |
| 32 |
pub(crate) unsafe fn new(hdr: &'a mut M) -> Self { |
| 33 |
Self { |
| 34 |
cmsg: hdr.cmsg_first_hdr().as_mut(), |
| 35 |
hdr, |
| 36 |
len: 0, |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/// Append a control message to the buffer. |
| 41 |
/// |
| 42 |
/// # Panics |
| 43 |
/// - If insufficient buffer space remains. |
| 44 |
/// - If `T` has stricter alignment requirements than `M::ControlMessage` |
| 45 |
pub(crate) fn push<T: Copy>(&mut self, level: c_int, ty: c_int, value: T) { |
| 46 |
assert!(mem::align_of::<T>() <= mem::align_of::<M::ControlMessage>()); |
| 47 |
let space = M::ControlMessage::cmsg_space(mem::size_of_val(&value)); |
| 48 |
assert!( |
| 49 |
self.hdr.control_len() >= self.len + space, |
| 50 |
"control message buffer too small. Required: {}, Available: {}", |
| 51 |
self.len + space, |
| 52 |
self.hdr.control_len() |
| 53 |
); |
| 54 |
let cmsg = self.cmsg.take().expect("no control buffer space remaining"); |
| 55 |
cmsg.set( |
| 56 |
level, |
| 57 |
ty, |
| 58 |
M::ControlMessage::cmsg_len(mem::size_of_val(&value)), |
| 59 |
); |
| 60 |
unsafe { |
| 61 |
ptr::write(cmsg.cmsg_data() as *const T as *mut T, value); |
| 62 |
} |
| 63 |
self.len += space; |
| 64 |
self.cmsg = unsafe { self.hdr.cmsg_nxt_hdr(cmsg).as_mut() }; |
| 65 |
} |
| 66 |
|
| 67 |
/// Finishes appending control messages to the buffer |
| 68 |
pub(crate) fn finish(self) { |
| 69 |
// Delegates to the `Drop` impl |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// Statically guarantees that the encoding operation is "finished" before the control buffer is read |
| 74 |
// by `sendmsg` like API. |
| 75 |
impl<M: MsgHdr> Drop for Encoder<'_, M> { |
| 76 |
fn drop(&mut self) { |
| 77 |
self.hdr.set_control_len(self.len as _); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/// # Safety |
| 82 |
/// |
| 83 |
/// `cmsg` must refer to a native cmsg containing a payload of type `T` |
| 84 |
pub(crate) unsafe fn decode<T: Copy, C: CMsgHdr>(cmsg: &impl CMsgHdr) -> T { |
| 85 |
assert!(mem::align_of::<T>() <= mem::align_of::<C>()); |
| 86 |
debug_assert_eq!(cmsg.len(), C::cmsg_len(mem::size_of::<T>())); |
| 87 |
ptr::read(cmsg.cmsg_data() as *const T) |
| 88 |
} |
| 89 |
|
| 90 |
pub(crate) struct Iter<'a, M: MsgHdr> { |
| 91 |
hdr: &'a M, |
| 92 |
cmsg: Option<&'a M::ControlMessage>, |
| 93 |
} |
| 94 |
|
| 95 |
impl<'a, M: MsgHdr> Iter<'a, M> { |
| 96 |
/// # Safety |
| 97 |
/// |
| 98 |
/// `hdr` must hold a pointer to memory outliving `'a` which can be soundly read for the |
| 99 |
/// lifetime of the constructed `Iter` and contains a buffer of native cmsgs, i.e. is aligned |
| 100 |
// for native `cmsghdr`, is fully initialized, and has correct internal links. |
| 101 |
pub(crate) unsafe fn new(hdr: &'a M) -> Self { |
| 102 |
Self { |
| 103 |
hdr, |
| 104 |
cmsg: hdr.cmsg_first_hdr().as_ref(), |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
impl<'a, M: MsgHdr> Iterator for Iter<'a, M> { |
| 110 |
type Item = &'a M::ControlMessage; |
| 111 |
|
| 112 |
fn next(&mut self) -> Option<Self::Item> { |
| 113 |
let current = self.cmsg.take()?; |
| 114 |
self.cmsg = unsafe { self.hdr.cmsg_nxt_hdr(current).as_ref() }; |
| 115 |
|
| 116 |
#[cfg(apple_fast)] |
| 117 |
{ |
| 118 |
// On MacOS < 14 CMSG_NXTHDR might continuously return a zeroed cmsg. In |
| 119 |
// such case, return `None` instead, thus indicating the end of |
| 120 |
// the cmsghdr chain. |
| 121 |
if current.len() < mem::size_of::<M::ControlMessage>() { |
| 122 |
return None; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
Some(current) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// Helper traits for native types for control messages |
| 131 |
pub(crate) trait MsgHdr { |
| 132 |
type ControlMessage: CMsgHdr; |
| 133 |
|
| 134 |
fn cmsg_first_hdr(&self) -> *mut Self::ControlMessage; |
| 135 |
|
| 136 |
fn cmsg_nxt_hdr(&self, cmsg: &Self::ControlMessage) -> *mut Self::ControlMessage; |
| 137 |
|
| 138 |
/// Sets the number of control messages added to this `struct msghdr`. |
| 139 |
/// |
| 140 |
/// Note that this is a destructive operation and should only be done as a finalisation |
| 141 |
/// step. |
| 142 |
fn set_control_len(&mut self, len: usize); |
| 143 |
|
| 144 |
fn control_len(&self) -> usize; |
| 145 |
} |
| 146 |
|
| 147 |
pub(crate) trait CMsgHdr { |
| 148 |
fn cmsg_len(length: usize) -> usize; |
| 149 |
|
| 150 |
fn cmsg_space(length: usize) -> usize; |
| 151 |
|
| 152 |
fn cmsg_data(&self) -> *mut c_uchar; |
| 153 |
|
| 154 |
fn set(&mut self, level: c_int, ty: c_int, len: usize); |
| 155 |
|
| 156 |
fn len(&self) -> usize; |
| 157 |
} |