compio_net/cmsg/mod.rs
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
use std::marker::PhantomData;
cfg_if::cfg_if! {
if #[cfg(windows)] {
#[path = "windows.rs"]
mod sys;
} else if #[cfg(unix)] {
#[path = "unix.rs"]
mod sys;
}
}
/// Reference to a control message.
pub struct CMsgRef<'a>(sys::CMsgRef<'a>);
impl CMsgRef<'_> {
/// Returns the level of the control message.
pub fn level(&self) -> i32 {
self.0.level()
}
/// Returns the type of the control message.
pub fn ty(&self) -> i32 {
self.0.ty()
}
/// Returns the length of the control message.
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.0.len() as _
}
/// Returns a reference to the data of the control message.
///
/// # Safety
///
/// The data part must be properly aligned and contains an initialized
/// instance of `T`.
pub unsafe fn data<T>(&self) -> &T {
self.0.data()
}
}
/// An iterator for control messages.
pub struct CMsgIter<'a> {
inner: sys::CMsgIter,
_p: PhantomData<&'a ()>,
}
impl<'a> CMsgIter<'a> {
/// Create [`CMsgIter`] with the given buffer.
///
/// # Panics
///
/// This function will panic if the buffer is too short or not properly
/// aligned.
///
/// # Safety
///
/// The buffer should contain valid control messages.
pub unsafe fn new(buffer: &'a [u8]) -> Self {
Self {
inner: sys::CMsgIter::new(buffer.as_ptr(), buffer.len()),
_p: PhantomData,
}
}
}
impl<'a> Iterator for CMsgIter<'a> {
type Item = CMsgRef<'a>;
fn next(&mut self) -> Option<Self::Item> {
unsafe {
let cmsg = self.inner.current();
self.inner.next();
cmsg.map(CMsgRef)
}
}
}
/// Helper to construct control message.
pub struct CMsgBuilder<'a> {
inner: sys::CMsgIter,
len: usize,
_p: PhantomData<&'a mut ()>,
}
impl<'a> CMsgBuilder<'a> {
/// Create [`CMsgBuilder`] with the given buffer. The buffer will be zeroed
/// on creation.
///
/// # Panics
///
/// This function will panic if the buffer is too short or not properly
/// aligned.
pub fn new(buffer: &'a mut [u8]) -> Self {
buffer.fill(0);
Self {
inner: sys::CMsgIter::new(buffer.as_ptr(), buffer.len()),
len: 0,
_p: PhantomData,
}
}
/// Finishes building, returns length of the control message.
pub fn finish(self) -> usize {
self.len
}
/// Try to append a control message entry into the buffer. If the buffer
/// does not have enough space or is not properly aligned with the value
/// type, returns `None`.
pub fn try_push<T>(&mut self, level: i32, ty: i32, value: T) -> Option<()> {
if !self.inner.is_aligned::<T>() || !self.inner.is_space_enough::<T>() {
return None;
}
// SAFETY: the buffer is zeroed and the pointer is valid and aligned
unsafe {
let mut cmsg = self.inner.current_mut()?;
cmsg.set_level(level);
cmsg.set_ty(ty);
self.len += cmsg.set_data(value);
self.inner.next();
}
Some(())
}
}