From 21ba6609efe09a7e27d62d2c29ff61bab88b970d Mon Sep 17 00:00:00 2001 From: Toby Lawrence Date: Mon, 15 Apr 2024 17:39:07 +0000 Subject: [PATCH] Add MsgHdrMut::control_len to get how much of control buffer was filled. --- src/lib.rs | 9 +++++++++ src/sys/unix.rs | 5 +++++ src/sys/windows.rs | 4 ++++ 3 files changed, 18 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 4f6bd789..89dce26e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -719,6 +719,15 @@ impl<'addr, 'bufs, 'control> MsgHdrMut<'addr, 'bufs, 'control> { pub fn flags(&self) -> RecvFlags { sys::msghdr_flags(&self.inner) } + + /// Gets the length of the control buffer. + /// + /// Can be used to determine how much, if any, of the control buffer was filled by `recvmsg`. + /// + /// Corresponds to `msg_controllen` on Unix and `Control.len` on Windows. + pub fn control_len(&self) -> usize { + sys::msghdr_control_len(&self.inner) + } } #[cfg(not(target_os = "redox"))] diff --git a/src/sys/unix.rs b/src/sys/unix.rs index 562cd60f..90d3eb1a 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -738,6 +738,11 @@ pub(crate) fn msghdr_flags(msg: &msghdr) -> RecvFlags { RecvFlags(msg.msg_flags) } +#[cfg(not(target_os = "redox"))] +pub(crate) fn msghdr_control_len(msg: &msghdr) -> usize { + msg.msg_controllen as _ +} + /// Unix only API. impl SockAddr { /// Constructs a `SockAddr` with the family `AF_VSOCK` and the provided CID/port. diff --git a/src/sys/windows.rs b/src/sys/windows.rs index 4c5d9879..11f2b7b0 100644 --- a/src/sys/windows.rs +++ b/src/sys/windows.rs @@ -215,6 +215,10 @@ pub(crate) fn msghdr_flags(msg: &msghdr) -> RecvFlags { RecvFlags(msg.dwFlags as c_int) } +pub(crate) fn msghdr_control_len(msg: &msghdr) -> usize { + msg.Control.len as _ +} + fn init() { static INIT: Once = Once::new();