pack_mhdr_to_receive: pass pointer and capacity directly
This is already an unsafe function, dealing with pointers directly
does not make it much more unsafe but simplifies lifetimes later on
and similar to a previous commit allows to alocate a single buffer
to store all the control messages
@@ -1616,10 +1616,14 @@ pub fn recvmmsg<'a, I, S>(
.into_boxed_slice();
let results: Vec<_> = iter.enumerate().map(|(i, d)| {
- let (msg_controllen, mhdr) = unsafe {
+ let (msg_control, msg_controllen) = d.cmsg_buffer.as_mut()
+ .map(|v| (v.as_mut_ptr(), v.capacity()))
+ .unwrap_or((ptr::null_mut(), 0));
+ let mhdr = unsafe {
pack_mhdr_to_receive(
d.iov.as_ref(),
- &mut d.cmsg_buffer,
+ msg_control,
+ msg_controllen,
addresses[i].as_mut_ptr(),
)
};
@@ -1693,18 +1697,14 @@ unsafe fn read_mhdr<'a, S>(
unsafe fn pack_mhdr_to_receive<'outer, 'inner, I, S>(
iov: I,
- cmsg_buffer: &mut Option<&mut Vec<u8>>,
+ cmsg_buffer: *const u8,
+ cmsg_capacity: usize,
address: *mut S,
-) -> (usize, msghdr)
+) -> msghdr
where
I: AsRef<[IoSliceMut<'inner>]> + 'outer,
S: SockaddrLike + 'outer
{
- let (msg_control, msg_controllen) = cmsg_buffer.as_mut()
- .map(|v| (v.as_mut_ptr(), v.capacity()))
- .unwrap_or((ptr::null_mut(), 0));
-
- let mhdr = {
// Musl's msghdr has private fields, so this is the only way to
// initialize it.
let mut mhdr = mem::MaybeUninit::<msghdr>::zeroed();
@@ -1713,13 +1713,10 @@ unsafe fn pack_mhdr_to_receive<'outer, 'inner, I, S>(
(*p).msg_namelen = S::size();
(*p).msg_iov = iov.as_ref().as_ptr() as *mut iovec;
(*p).msg_iovlen = iov.as_ref().len() as _;
- (*p).msg_control = msg_control as *mut c_void;
- (*p).msg_controllen = msg_controllen as _;
+ (*p).msg_control = cmsg_buffer as *mut c_void;
+ (*p).msg_controllen = cmsg_capacity as _;
(*p).msg_flags = 0;
mhdr.assume_init()
- };
-
- (msg_controllen, mhdr)
}
fn pack_mhdr_to_send<'a, I, C, S>(
@@ -1796,8 +1793,11 @@ pub fn recvmsg<'a, 'outer, 'inner, S>(fd: RawFd, iov: &'outer mut [IoSliceMut<'i
{
let mut address = mem::MaybeUninit::uninit();
- let (msg_controllen, mut mhdr) = unsafe {
- pack_mhdr_to_receive::<_, S>(iov, &mut cmsg_buffer, address.as_mut_ptr())
+ let (msg_control, msg_controllen) = cmsg_buffer.as_mut()
+ .map(|v| (v.as_mut_ptr(), v.capacity()))
+ .unwrap_or((ptr::null_mut(), 0));
+ let mut mhdr = unsafe {
+ pack_mhdr_to_receive::<_, S>(iov, msg_control, msg_controllen, address.as_mut_ptr())
};
let ret = unsafe { libc::recvmsg(fd, &mut mhdr, flags.bits()) };
