|
| 1 | +use crate::error::{BluefinIoError, BluefinIoResult}; |
| 2 | +use libc::c_int; |
| 3 | + |
| 4 | +// This is a wrapper on the following structure: |
| 5 | +// see: https://man7.org/linux/man-pages/man3/cmsg.3.html |
| 6 | +// struct cmsghdr { |
| 7 | +// size_t cmsg_len; /* Data byte count, including header |
| 8 | +// (type is socklen_t in POSIX) */ |
| 9 | +// int cmsg_level; /* Originating protocol */ |
| 10 | +// int cmsg_type; /* Protocol-specific type */ |
| 11 | +// /* followed by |
| 12 | +// unsigned char cmsg_data[]; */ |
| 13 | +// }; |
| 14 | +pub(crate) struct CmsghdrBufferHandler<'a> { |
| 15 | + // This is a ref to the msghdr struct which holds this cmsghdr buffer. We need to keep this |
| 16 | + // reference because the CMSG_NXTHDR() requires the msghdr as an input parameter. |
| 17 | + src_msghdr: &'a libc::msghdr, |
| 18 | + // Mutable reference to the first cmsghdr in the ancillary data buffer. This value |
| 19 | + // must be derived from the CMSG_FIRSTHDR() macro defined in cmsg (3). |
| 20 | + // According to the man pages, this pointer can be null if there is not enough space allocated. |
| 21 | + // We will need to use the size + total_bytes fields below to safely unwrap this value. |
| 22 | + // The `append` call is responsible for advancing the pointer to the next free address if |
| 23 | + // more data was successfully appended. |
| 24 | + cmsghdr_ptr: Option<&'a mut libc::cmsghdr>, |
| 25 | + // The amount of bytes we have written to the ancillary buffer. This must not exceed |
| 26 | + // the `total_bytes` value below. |
| 27 | + size: usize, |
| 28 | + // Total bytes allocated to the ancillary data buffer. |
| 29 | + total_bytes: usize, |
| 30 | +} |
| 31 | + |
| 32 | +impl<'a> CmsghdrBufferHandler<'a> { |
| 33 | + pub(crate) fn new(msghdr: &'a libc::msghdr) -> Self { |
| 34 | + let cmsghdr_ptr = unsafe { libc::CMSG_FIRSTHDR(msghdr).as_mut() }; |
| 35 | + Self { |
| 36 | + src_msghdr: msghdr, |
| 37 | + cmsghdr_ptr, |
| 38 | + size: 0, |
| 39 | + total_bytes: msghdr.msg_controllen as _, |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + pub(crate) fn append<T>( |
| 44 | + &mut self, |
| 45 | + cmsg_level: c_int, |
| 46 | + cmsg_type: c_int, |
| 47 | + cmsg_data: T, |
| 48 | + ) -> BluefinIoResult<()> { |
| 49 | + // Need to use CMSG_SPACE macro to determine how much space cmsg_data would occupy |
| 50 | + let val_size: usize = unsafe { libc::CMSG_SPACE(size_of_val(&cmsg_data) as _) } as _; |
| 51 | + if self.size + val_size > self.total_bytes { |
| 52 | + return Err(BluefinIoError::InsufficientBufferSize( |
| 53 | + "Could not add value to ancillary data as data exceeds available free space" |
| 54 | + .to_string(), |
| 55 | + )); |
| 56 | + } |
| 57 | + |
| 58 | + // Since there is enough space, we can get unwrap safely and get the mutable ref. |
| 59 | + let cmsghdr = self.cmsghdr_ptr.take().unwrap(); |
| 60 | + |
| 61 | + // Set the level, type and len. |
| 62 | + cmsghdr.cmsg_level = cmsg_level; |
| 63 | + cmsghdr.cmsg_type = cmsg_type; |
| 64 | + // size_of_val() is insufficient here. As stated in cmsg(3), we need to consider alignment. |
| 65 | + cmsghdr.cmsg_len = unsafe { libc::CMSG_LEN(size_of_val(&cmsg_data) as _) as _ }; |
| 66 | + |
| 67 | + // Now finally write the data |
| 68 | + // First use CMSG_DATA() to get a pointer to the data portion of cmsghdr. |
| 69 | + let cmsghdr_data_ptr = unsafe { libc::CMSG_DATA(cmsghdr) }; |
| 70 | + // Now, write the data. |
| 71 | + unsafe { |
| 72 | + // Need to cast this to the correct pointer type |
| 73 | + std::ptr::write(cmsghdr_data_ptr as *mut T, cmsg_data); |
| 74 | + }; |
| 75 | + |
| 76 | + // Account for the space we used |
| 77 | + self.size += val_size; |
| 78 | + |
| 79 | + // Finally, we must advance self.cmsghdr_ptr. As directed in the man pages we will use |
| 80 | + // CMSG_NXTHDR(). |
| 81 | + let next_cmsghdr = unsafe { libc::CMSG_NXTHDR(self.src_msghdr, cmsghdr).as_mut() }; |
| 82 | + self.cmsghdr_ptr = next_cmsghdr; |
| 83 | + Ok(()) |
| 84 | + } |
| 85 | +} |
0 commit comments