Skip to content

Commit

Permalink
Move MsgFlags onto RecvMsgSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
evanrittenhouse committed Jun 14, 2024
1 parent c611b61 commit 6c869ba
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 36 deletions.
13 changes: 3 additions & 10 deletions apps/src/recvfrom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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<RecvData> {
match socket.recv_from(buf) {
Ok((read, from)) => Ok(RecvData::new(Some(from), read, 0)),
Err(_) => {},
Err(e) => Err(e),
}
}
21 changes: 18 additions & 3 deletions dgram/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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,
Expand All @@ -45,6 +46,20 @@ pub struct RecvMsgSettings {
///
/// [`cmsg_space`]: https://docs.rs/nix/latest/nix/macro.cmsg_space.html
pub cmsg_space: Vec<u8>,
/// 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.
Expand Down
10 changes: 2 additions & 8 deletions dgram/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MsgFlags>,
store_cmsg_settings: &mut RecvMsgSettings,
fd: &impl AsFd, read_buf: &mut [u8], recvmsg_settings: &mut RecvMsgSettings,
) -> Result<RecvData> {
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),
Expand Down
22 changes: 7 additions & 15 deletions dgram/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RecvData> {
use crate::IpOrigDstAddr;

let RecvMsgSettings {
store_cmsgs,
ref mut cmsg_space,
msg_flags,
} = recvmsg_settings;

cmsg_space.clear();
Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 6c869ba

Please sign in to comment.