diff --git a/apps/src/recvfrom.rs b/apps/src/recvfrom.rs index 16412cdd28..b46af74410 100644 --- a/apps/src/recvfrom.rs +++ b/apps/src/recvfrom.rs @@ -2,8 +2,6 @@ use dgram::RecvData; use std::io; -// TODO: migrate setup code to use [dgram::SocketCapabilities]. - /// For Linux, try to detect if GRO is available. If it is, the /// [`UdpGroSegment`] socket option will be set on the passed socket. /// @@ -41,26 +39,21 @@ pub fn recv_from( use dgram::RecvMsgSettings; use std::os::unix::io::AsRawFd; - let mut recvmsg_cmsg_settings = RecvMsgSettings { - store_cmsgs: false, - cmsg_space: vec![], - }; - + let mut recvmsg_cmsg_settings = RecvMsgSettings::default(); socket.try_io(|| { let fd = unsafe { std::os::fd::BorrowedFd::borrow_raw(socket.as_raw_fd()) }; - dgram::sync::recv_from(&fd, buf, None, &mut recvmsg_cmsg_settings) + dgram::sync::recv_from(&fd, buf, &mut recvmsg_cmsg_settings) }) } -// TODO: shoould it be up to the user to handle blocking errors? #[cfg(not(target_os = "linux"))] fn recv_from( socket: &mio::net::UdpSocket, buf: &mut [u8], ) -> std::io::Result { match socket.recv_from(buf) { Ok((read, from)) => Ok(RecvData::new(Some(from), read, 0)), - Err(_) => {}, + Err(e) => Err(e), } } diff --git a/dgram/src/lib.rs b/dgram/src/lib.rs index 8868874cd5..416935c41f 100644 --- a/dgram/src/lib.rs +++ b/dgram/src/lib.rs @@ -13,6 +13,7 @@ use libc::in_pktinfo; use libc::sockaddr_in; use libc::sockaddr_in6; use nix::sys::socket::ControlMessageOwned; +use nix::sys::socket::MsgFlags; /// Settings for handling control messages when sending data. #[cfg(target_os = "linux")] @@ -30,10 +31,10 @@ pub struct SendMsgSettings { /// Settings for handling control messages when receiving data. #[cfg(target_os = "linux")] -#[derive(Clone, Default)] +#[derive(Clone)] pub struct RecvMsgSettings { - // TODO: deprecate store_cmsgs and only store based on what cmsg_space can - // handle. + // TODO(evanrittenhouse): deprecate store_cmsgs and only store based on what + // cmsg_space can handle. /// If cmsgs should be stored when receiving a message. If set, cmsgs will /// be stored in the `cmsg_space` vector. pub store_cmsgs: bool, @@ -45,6 +46,20 @@ pub struct RecvMsgSettings { /// /// [`cmsg_space`]: https://docs.rs/nix/latest/nix/macro.cmsg_space.html pub cmsg_space: Vec, + /// Flags for [`recvmsg`]. See [MsgFlags] for more. + /// + /// [`recvmsg`]: [nix::sys::socket::recvmsg] + pub msg_flags: MsgFlags, +} + +impl Default for RecvMsgSettings { + fn default() -> Self { + Self { + msg_flags: MsgFlags::empty(), + store_cmsgs: false, + cmsg_space: vec![], + } + } } /// Output of a `recvmsg` call. diff --git a/dgram/src/sync.rs b/dgram/src/sync.rs index c1a4dd48c7..9d13a80f64 100644 --- a/dgram/src/sync.rs +++ b/dgram/src/sync.rs @@ -25,15 +25,9 @@ pub fn send_to( #[cfg(target_os = "linux")] pub fn recv_from( - fd: &impl AsFd, read_buf: &mut [u8], msg_flags: Option, - store_cmsg_settings: &mut RecvMsgSettings, + fd: &impl AsFd, read_buf: &mut [u8], recvmsg_settings: &mut RecvMsgSettings, ) -> Result { - let recvd = recv_msg( - fd, - read_buf, - msg_flags.unwrap_or(MsgFlags::empty()), - store_cmsg_settings, - ); + let recvd = recv_msg(fd, read_buf, recvmsg_settings); match recvd { Ok(r) => Ok(r), diff --git a/dgram/src/syscalls.rs b/dgram/src/syscalls.rs index a9da25c450..ccdeec011d 100644 --- a/dgram/src/syscalls.rs +++ b/dgram/src/syscalls.rs @@ -95,14 +95,14 @@ pub fn send_msg( /// to set any relevant socket options. #[cfg(target_os = "linux")] pub fn recv_msg( - fd: impl AsFd, read_buf: &mut [u8], msg_flags: MsgFlags, - recvmsg_settings: &mut RecvMsgSettings, + fd: impl AsFd, read_buf: &mut [u8], recvmsg_settings: &mut RecvMsgSettings, ) -> SyscallResult { use crate::IpOrigDstAddr; let RecvMsgSettings { store_cmsgs, ref mut cmsg_space, + msg_flags, } = recvmsg_settings; cmsg_space.clear(); @@ -115,7 +115,7 @@ pub fn recv_msg( borrowed.as_raw_fd(), iov_s, Some(cmsg_space), - msg_flags, + *msg_flags, ) { Ok(r) => { let bytes = r.bytes; @@ -260,12 +260,8 @@ mod tests { sendmsg(send.as_raw_fd(), &iov, &[], MsgFlags::empty(), Some(&addr))?; let mut read_buf = [0; 4]; - let recv_data = recv_msg( - recv, - &mut read_buf, - MsgFlags::empty(), - &mut RecvMsgSettings::default(), - )?; + let recv_data = + recv_msg(recv, &mut read_buf, &mut RecvMsgSettings::default())?; assert_eq!(recv_data.bytes, 4); assert_eq!(&read_buf, b"jets"); @@ -325,15 +321,11 @@ mod tests { let mut recvmsg_settings = RecvMsgSettings { store_cmsgs: true, cmsg_space, + msg_flags: MsgFlags::empty(), }; let mut read_buf = [0; 4]; - let recv_data = recv_msg( - recv, - &mut read_buf, - MsgFlags::empty(), - &mut recvmsg_settings, - )?; + let recv_data = recv_msg(recv, &mut read_buf, &mut recvmsg_settings)?; assert_eq!(recv_data.bytes, 4); assert_eq!(&read_buf, b"jets");