use core::iter::FusedIterator; use core::marker::PhantomData; use core::mem::{align_of, size_of, size_of_val, take}; #[cfg(linux_kernel)] use core::ptr::addr_of; use core::{ptr, slice};
/// Macro for defining the amount of space to allocate in a buffer for use with /// [`RecvAncillaryBuffer::new`] and [`SendAncillaryBuffer::new`]. /// /// # Examples /// /// Allocate a buffer for a single file descriptor: /// ``` /// # use rustix::cmsg_space; /// let mut space = [0; rustix::cmsg_space!(ScmRights(1))]; /// ``` /// /// Allocate a buffer for credentials: /// ``` /// # #[cfg(linux_kernel)] /// # { /// # use rustix::cmsg_space; /// let mut space = [0; rustix::cmsg_space!(ScmCredentials(1))]; /// # } /// ``` /// /// Allocate a buffer for two file descriptors and credentials: /// ``` /// # #[cfg(linux_kernel)] /// # { /// # use rustix::cmsg_space; /// let mut space = [0; rustix::cmsg_space!(ScmRights(2), ScmCredentials(1))]; /// # } /// ``` #[macro_export]
macro_rules! cmsg_space { // Base Rules
(ScmRights($len:expr)) => {
$crate::net::__cmsg_space(
$len * ::core::mem::size_of::<$crate::fd::BorrowedFd<'static>>(),
)
};
(ScmCredentials($len:expr)) => {
$crate::net::__cmsg_space(
$len * ::core::mem::size_of::<$crate::net::UCred>(),
)
};
// Combo Rules
($firstid:ident($firstex:expr), $($restid:ident($restex:expr)),*) => {{ // We only have to add `cmsghdr` alignment once; all other times we can // use `cmsg_aligned_space`. let sum = $crate::cmsg_space!($firstid($firstex));
$( let sum = sum + $crate::cmsg_aligned_space!($restid($restex));
)*
sum
}};
}
/// Like `cmsg_space`, but doesn't add padding for `cmsghdr` alignment. #[doc(hidden)] #[macro_export]
macro_rules! cmsg_aligned_space { // Base Rules
(ScmRights($len:expr)) => {
$crate::net::__cmsg_aligned_space(
$len * ::core::mem::size_of::<$crate::fd::BorrowedFd<'static>>(),
)
};
(ScmCredentials($len:expr)) => {
$crate::net::__cmsg_aligned_space(
$len * ::core::mem::size_of::<$crate::net::UCred>(),
)
};
// Combo Rules
($firstid:ident($firstex:expr), $($restid:ident($restex:expr)),*) => {{ let sum = cmsg_aligned_space!($firstid($firstex));
$( let sum = sum + cmsg_aligned_space!($restid($restex));
)*
sum
}};
}
#[doc(hidden)] pubconstfn __cmsg_space(len: usize) -> usize { // Add `align_of::<c::cmsghdr>()` so that we can align the user-provided // `&[u8]` to the required alignment boundary. let len = len + align_of::<c::cmsghdr>();
__cmsg_aligned_space(len)
}
#[doc(hidden)] pubconstfn __cmsg_aligned_space(len: usize) -> usize { // Convert `len` to `u32` for `CMSG_SPACE`. This would be `try_into()` if // we could call that in a `const fn`. let converted_len = len as u32; if converted_len as usize != len {
unreachable!(); // `CMSG_SPACE` size overflow
}
unsafe { c::CMSG_SPACE(converted_len) as usize }
}
impl SendAncillaryMessage<'_, '_> { /// Get the maximum size of an ancillary message. /// /// This can be helpful in determining the size of the buffer you allocate. pubconstfn size(&self) -> usize { matchself { Self::ScmRights(slice) => cmsg_space!(ScmRights(slice.len())), #[cfg(linux_kernel)] Self::ScmCredentials(_) => cmsg_space!(ScmCredentials(1)),
}
}
}
/// Ancillary message for [`recvmsg`]. #[non_exhaustive] pubenum RecvAncillaryMessage<'a> { /// Received file descriptors. #[doc(alias = "SCM_RIGHTS")]
ScmRights(AncillaryIter<'a, OwnedFd>), /// Received process credentials. #[cfg(linux_kernel)] #[doc(alias = "SCM_CREDENTIALS")]
ScmCredentials(UCred),
}
/// Buffer for sending ancillary messages with [`sendmsg`], [`sendmsg_v4`], /// [`sendmsg_v6`], [`sendmsg_unix`], and [`sendmsg_any`]. /// /// Use the [`push`] function to add messages to send. /// /// [`push`]: SendAncillaryBuffer::push pubstruct SendAncillaryBuffer<'buf, 'slice, 'fd> { /// Raw byte buffer for messages.
buffer: &'buf mut [u8],
/// The amount of the buffer that is used.
length: usize,
/// Phantom data for lifetime of `&'slice [BorrowedFd<'fd>]`.
_phantom: PhantomData<&'slice [BorrowedFd<'fd>]>,
}
impl<'buf, 'slice, 'fd> SendAncillaryBuffer<'buf, 'slice, 'fd> { /// Create a new, empty `SendAncillaryBuffer` from a raw byte buffer. /// /// The buffer size may be computed with [`cmsg_space`], or it may be /// zero for an empty buffer, however in that case, consider `default()` /// instead, or even using [`send`] instead of `sendmsg`. /// /// # Examples /// /// Allocate a buffer for a single file descriptor: /// ``` /// # use rustix::cmsg_space; /// # use rustix::net::SendAncillaryBuffer; /// let mut space = [0; rustix::cmsg_space!(ScmRights(1))]; /// let mut cmsg_buffer = SendAncillaryBuffer::new(&mut space); /// ``` /// /// Allocate a buffer for credentials: /// ``` /// # #[cfg(linux_kernel)] /// # { /// # use rustix::cmsg_space; /// # use rustix::net::SendAncillaryBuffer; /// let mut space = [0; rustix::cmsg_space!(ScmCredentials(1))]; /// let mut cmsg_buffer = SendAncillaryBuffer::new(&mut space); /// # } /// ``` /// /// Allocate a buffer for two file descriptors and credentials: /// ``` /// # #[cfg(linux_kernel)] /// # { /// # use rustix::cmsg_space; /// # use rustix::net::SendAncillaryBuffer; /// let mut space = [0; rustix::cmsg_space!(ScmRights(2), ScmCredentials(1))]; /// let mut cmsg_buffer = SendAncillaryBuffer::new(&mut space); /// # } /// ``` /// /// [`send`]: crate::net::send #[inline] pubfn new(buffer: &'buf mut [u8]) -> Self { Self {
buffer: align_for_cmsghdr(buffer),
length: 0,
_phantom: PhantomData,
}
}
/// Returns a pointer to the message data. pub(crate) fn as_control_ptr(&mutself) -> *mut u8 { // When the length is zero, we may be using a `&[]` address, which may // be an invalid but non-null pointer, and on some platforms, that // causes `sendmsg` to fail with `EFAULT` or `EINVAL` #[cfg(not(linux_kernel))] ifself.length == 0 { return core::ptr::null_mut();
}
self.buffer.as_mut_ptr()
}
/// Returns the length of the message data. pub(crate) fn control_len(&self) -> usize { self.length
}
/// Delete all messages from the buffer. pubfn clear(&mutself) { self.length = 0;
}
/// Add an ancillary message to the buffer. /// /// Returns `true` if the message was added successfully. pubfn push(&mutself, msg: SendAncillaryMessage<'slice, 'fd>) -> bool { match msg {
SendAncillaryMessage::ScmRights(fds) => { let fds_bytes = unsafe { slice::from_raw_parts(fds.as_ptr().cast::<u8>(), size_of_val(fds)) }; self.push_ancillary(fds_bytes, c::SOL_SOCKET as _, c::SCM_RIGHTS as _)
} #[cfg(linux_kernel)]
SendAncillaryMessage::ScmCredentials(ucred) => { let ucred_bytes = unsafe {
slice::from_raw_parts(addr_of!(ucred).cast::<u8>(), size_of_val(&ucred))
}; self.push_ancillary(ucred_bytes, c::SOL_SOCKET as _, c::SCM_CREDENTIALS as _)
}
}
}
/// Pushes an ancillary message to the buffer. fn push_ancillary(&mutself, source: &[u8], cmsg_level: c::c_int, cmsg_type: c::c_int) -> bool {
macro_rules! leap {
($e:expr) => {{ match ($e) {
Some(x) => x,
None => returnfalse,
}
}};
}
// Calculate the length of the message. let source_len = leap!(u32::try_from(source.len()).ok());
// Calculate the new length of the buffer. let additional_space = unsafe { c::CMSG_SPACE(source_len) }; let new_length = leap!(self.length.checked_add(additional_space as usize)); let buffer = leap!(self.buffer.get_mut(..new_length));
// Fill the new part of the buffer with zeroes.
buffer[self.length..new_length].fill(0); self.length = new_length;
// Get the last header in the buffer. let last_header = leap!(messages::Messages::new(buffer).last());
// Set the header fields.
last_header.cmsg_len = unsafe { c::CMSG_LEN(source_len) } as _;
last_header.cmsg_level = cmsg_level;
last_header.cmsg_type = cmsg_type;
// Get the pointer to the payload and copy the data. unsafe { let payload = c::CMSG_DATA(last_header);
ptr::copy_nonoverlapping(source.as_ptr(), payload, source_len as _);
}
true
}
}
impl<'slice, 'fd> Extend<SendAncillaryMessage<'slice, 'fd>> for SendAncillaryBuffer<'_, 'slice, 'fd>
{ fn extend<T: IntoIterator<Item = SendAncillaryMessage<'slice, 'fd>>>(&>mutself, iter: T) { // TODO: This could be optimized to add every message in one go.
iter.into_iter().all(|msg| self.push(msg));
}
}
/// Buffer for receiving ancillary messages with [`recvmsg`]. /// /// Use the [`drain`] function to iterate over the received messages. /// /// [`drain`]: RecvAncillaryBuffer::drain #[derive(Default)] pubstruct RecvAncillaryBuffer<'buf> { /// Raw byte buffer for messages.
buffer: &'buf mut [u8],
/// The portion of the buffer we've read from already.
read: usize,
/// The amount of the buffer that is used.
length: usize,
}
impl<'buf> RecvAncillaryBuffer<'buf> { /// Create a new, empty `RecvAncillaryBuffer` from a raw byte buffer. /// /// The buffer size may be computed with [`cmsg_space`], or it may be /// zero for an empty buffer, however in that case, consider `default()` /// instead, or even using [`recv`] instead of `recvmsg`. /// /// # Examples /// /// Allocate a buffer for a single file descriptor: /// ``` /// # use rustix::cmsg_space; /// # use rustix::net::RecvAncillaryBuffer; /// let mut space = [0; rustix::cmsg_space!(ScmRights(1))]; /// let mut cmsg_buffer = RecvAncillaryBuffer::new(&mut space); /// ``` /// /// Allocate a buffer for credentials: /// ``` /// # #[cfg(linux_kernel)] /// # { /// # use rustix::cmsg_space; /// # use rustix::net::RecvAncillaryBuffer; /// let mut space = [0; rustix::cmsg_space!(ScmCredentials(1))]; /// let mut cmsg_buffer = RecvAncillaryBuffer::new(&mut space); /// # } /// ``` /// /// Allocate a buffer for two file descriptors and credentials: /// ``` /// # #[cfg(linux_kernel)] /// # { /// # use rustix::cmsg_space; /// # use rustix::net::RecvAncillaryBuffer; /// let mut space = [0; rustix::cmsg_space!(ScmRights(2), ScmCredentials(1))]; /// let mut cmsg_buffer = RecvAncillaryBuffer::new(&mut space); /// # } /// ``` /// /// [`recv`]: crate::net::recv #[inline] pubfn new(buffer: &'buf mut [u8]) -> Self { Self {
buffer: align_for_cmsghdr(buffer),
read: 0,
length: 0,
}
}
/// Returns a pointer to the message data. pub(crate) fn as_control_ptr(&mutself) -> *mut u8 { // When the length is zero, we may be using a `&[]` address, which may // be an invalid but non-null pointer, and on some platforms, that // causes `sendmsg` to fail with `EFAULT` or `EINVAL` #[cfg(not(linux_kernel))] ifself.buffer.is_empty() { return core::ptr::null_mut();
}
self.buffer.as_mut_ptr()
}
/// Returns the length of the message data. pub(crate) fn control_len(&self) -> usize { self.buffer.len()
}
/// Set the length of the message data. /// /// # Safety /// /// The buffer must be filled with valid message data. pub(crate) unsafefn set_control_len(&mutself, len: usize) { self.length = len; self.read = 0;
}
/// Delete all messages from the buffer. pub(crate) fn clear(&mutself) { self.drain().for_each(drop);
}
/// Drain all messages from the buffer. pubfn drain(&mutself) -> AncillaryDrain<'_> {
AncillaryDrain {
messages: messages::Messages::new(&mutself.buffer[self.read..][..self.length]),
read_and_length: Some((&mutself.read, &>mutself.length)),
}
}
}
impl Drop for RecvAncillaryBuffer<'_> { fn drop(&mutself) { self.clear();
}
}
/// Return a slice of `buffer` starting at the first `cmsghdr` alignment /// boundary. #[inline] fn align_for_cmsghdr(buffer: &mut [u8]) -> &mut [u8] { // If the buffer is empty, we won't be writing anything into it, so it // doesn't need to be aligned. if buffer.is_empty() { return buffer;
}
let align = align_of::<c::cmsghdr>(); let addr = buffer.as_ptr() as usize; let adjusted = (addr + (align - 1)) & align.wrapping_neg();
&mut buffer[adjusted - addr..]
}
/// An iterator that drains messages from a [`RecvAncillaryBuffer`]. pubstruct AncillaryDrain<'buf> { /// Inner iterator over messages.
messages: messages::Messages<'buf>,
/// Increment the number of messages we've read. /// Decrement the total length.
read_and_length: Option<(&'buf mut usize, &'buf mut usize)>,
}
impl<'buf> AncillaryDrain<'buf> { /// Create an iterator for control messages that were received without /// [`RecvAncillaryBuffer`]. /// /// # Safety /// /// The buffer must contain valid message data (or be empty). pubunsafefn parse(buffer: &'buf mut [u8]) -> Self { Self {
messages: messages::Messages::new(buffer),
read_and_length: None,
}
}
/// A closure that converts a message into a [`RecvAncillaryMessage`]. fn cvt_msg(msg: &c::cmsghdr) -> Option<RecvAncillaryMessage<'buf>> { unsafe { // Get a pointer to the payload. let payload = c::CMSG_DATA(msg); let payload_len = msg.cmsg_len as usize - c::CMSG_LEN(0) as usize;
// Get a mutable slice of the payload. let payload: &'buf mut [u8] = slice::from_raw_parts_mut(payload, payload_len);
// Determine what type it is. let (level, msg_type) = (msg.cmsg_level, msg.cmsg_type); match (level as _, msg_type as _) {
(c::SOL_SOCKET, c::SCM_RIGHTS) => { // Create an iterator that reads out the file descriptors. let fds = AncillaryIter::new(payload);
impl<T> DoubleEndedIterator for AncillaryIter<'_, T> { fn next_back(&mutself) -> Option<Self::Item> { // See if there is a next item. ifself.data.len() < size_of::<T>() { return None;
}
// Get the next item. let item = unsafe { let ptr = self.data.as_ptr().add(self.data.len() - size_of::<T>());
ptr.cast::<T>().read_unaligned()
};
// Move forward. let len = self.data.len(); let data = take(&mutself.data); self.data = &mut data[..len - size_of::<T>()];
Some(item)
}
}
mod messages { usecrate::backend::c; usecrate::backend::net::msghdr; use core::iter::FusedIterator; use core::marker::PhantomData; use core::ptr::NonNull;
/// An iterator over the messages in an ancillary buffer. pub(super) struct Messages<'buf> { /// The message header we're using to iterate over the messages.
msghdr: c::msghdr,
/// The current pointer to the next message header to return. /// /// This has a lifetime of `'buf`.
header: Option<NonNull<c::cmsghdr>>,
/// Capture the original lifetime of the buffer.
_buffer: PhantomData<&'buf mut [u8]>,
}
impl<'buf> Messages<'buf> { /// Create a new iterator over messages from a byte buffer. pub(super) fn new(buf: &'buf mut [u8]) -> Self { let msghdr = { letmut h = msghdr::zero_msghdr();
h.msg_control = buf.as_mut_ptr().cast();
h.msg_controllen = buf.len().try_into().expect("buffer too large for msghdr");
h
};
// Get the first header. let header = NonNull::new(unsafe { c::CMSG_FIRSTHDR(&msghdr) });
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.