Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new ReturnFlags type for flags returned from recvmsg #1288

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/backend/libc/net/read_sockaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ unsafe fn read_ss_family(storage: *const c::sockaddr_storage) -> u16 {
(*storage.cast::<sockaddr_header>()).ss_family.into()
}

/// Read the first byte of the `sun_path` field, assuming we have an `AF_UNIX`
/// socket address.
#[cfg(apple)]
#[inline]
unsafe fn read_sun_path0(storage: *const c::sockaddr_storage) -> u8 {
// In `read_ss_family` we assert that we know the layout of `sockaddr`.
storage
.cast::<u8>()
.add(super::addr::offsetof_sun_path())
.read()
}

/// Set the `ss_family` field of a socket address to `AF_UNSPEC`, so that we
/// can test for `AF_UNSPEC` to test whether it was stored to.
pub(crate) unsafe fn initialize_family_to_unspec(storage: *mut c::sockaddr_storage) {
Expand Down Expand Up @@ -233,10 +245,17 @@ pub(crate) unsafe fn maybe_read_sockaddr_os(
assert!(len >= size_of::<c::sa_family_t>());
let family = read_ss_family(storage).into();
if family == c::AF_UNSPEC {
None
} else {
Some(inner_read_sockaddr_os(family, storage, len))
return None;
}

// On macOS, if we get an `AF_UNIX` with an empty path, treat it as
// an absent address.
#[cfg(apple)]
if family == c::AF_UNIX && read_sun_path0(storage) == 0 {
return None;
}

Some(inner_read_sockaddr_os(family, storage, len))
}

/// Read a socket address returned from the OS.
Expand Down
36 changes: 35 additions & 1 deletion src/backend/libc/net/send_recv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ bitflags! {
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct RecvFlags: u32 {
/// `MSG_CMSG_CLOEXEC`
#[cfg(not(any(
apple,
solarish,
Expand All @@ -73,7 +74,6 @@ bitflags! {
target_os = "nto",
target_os = "vita",
)))]
/// `MSG_CMSG_CLOEXEC`
const CMSG_CLOEXEC = bitcast!(c::MSG_CMSG_CLOEXEC);
/// `MSG_DONTWAIT`
#[cfg(not(windows))]
Expand All @@ -96,6 +96,9 @@ bitflags! {
/// `MSG_PEEK`
const PEEK = bitcast!(c::MSG_PEEK);
/// `MSG_TRUNC`
// Apple has `MSG_TRUNC` but it's not documented for use with
// `recv` and friends, and in practice appears to be ignored.
#[cfg(not(apple))]
const TRUNC = bitcast!(c::MSG_TRUNC);
/// `MSG_WAITALL`
const WAITALL = bitcast!(c::MSG_WAITALL);
Expand All @@ -104,3 +107,34 @@ bitflags! {
const _ = !0;
}
}

bitflags! {
/// `MSG_*` flags returned from [`recvmsg`], in the `flags` field of
/// [`RecvMsgReturn`]
///
/// [`recvmsg`]: crate::net::recvmsg
/// [`RecvMsgReturn`]: crate::net::RecvMsgReturn
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct ReturnFlags: u32 {
/// `MSG_OOB`
const OOB = bitcast!(c::MSG_OOB);
/// `MSG_EOR`
#[cfg(not(windows))]
const EOR = bitcast!(c::MSG_EOR);
/// `MSG_TRUNC`
const TRUNC = bitcast!(c::MSG_TRUNC);
/// `MSG_CTRUNC`
const CTRUNC = bitcast!(c::MSG_CTRUNC);

/// `MSG_CMSG_CLOEXEC`
#[cfg(linux_kernel)]
const CMSG_CLOEXEC = bitcast!(c::MSG_CMSG_CLOEXEC);
/// `MSG_ERRQUEUE`
#[cfg(linux_kernel)]
const ERRQUEUE = bitcast!(c::MSG_ERRQUEUE);

/// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
const _ = !0;
}
}
4 changes: 2 additions & 2 deletions src/backend/libc/net/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use {
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
use {
super::read_sockaddr::{initialize_family_to_unspec, maybe_read_sockaddr_os, read_sockaddr_os},
super::send_recv::{RecvFlags, SendFlags},
super::send_recv::{RecvFlags, ReturnFlags, SendFlags},
super::write_sockaddr::{encode_sockaddr_v4, encode_sockaddr_v6},
crate::net::{AddressFamily, Protocol, Shutdown, SocketFlags, SocketType},
core::ptr::null_mut,
Expand Down Expand Up @@ -344,7 +344,7 @@ pub(crate) fn recvmsg(
RecvMsgReturn {
bytes,
address: addr,
flags: RecvFlags::from_bits_retain(bitcast!(msghdr.msg_flags)),
flags: ReturnFlags::from_bits_retain(bitcast!(msghdr.msg_flags)),
}
})
})
Expand Down
12 changes: 6 additions & 6 deletions src/backend/linux_raw/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ pub(crate) use linux_raw_sys::{
IPV6_MULTICAST_LOOP, IPV6_RECVTCLASS, IPV6_TCLASS, IPV6_UNICAST_HOPS, IPV6_V6ONLY,
IP_ADD_MEMBERSHIP, IP_ADD_SOURCE_MEMBERSHIP, IP_DROP_MEMBERSHIP, IP_DROP_SOURCE_MEMBERSHIP,
IP_FREEBIND, IP_MULTICAST_LOOP, IP_MULTICAST_TTL, IP_RECVTOS, IP_TOS, IP_TTL,
MSG_CMSG_CLOEXEC, MSG_CONFIRM, MSG_DONTROUTE, MSG_DONTWAIT, MSG_EOR, MSG_ERRQUEUE,
MSG_MORE, MSG_NOSIGNAL, MSG_OOB, MSG_PEEK, MSG_TRUNC, MSG_WAITALL, SCM_CREDENTIALS,
SCM_RIGHTS, SHUT_RD, SHUT_RDWR, SHUT_WR, SOCK_DGRAM, SOCK_RAW, SOCK_RDM, SOCK_SEQPACKET,
SOCK_STREAM, SOL_SOCKET, SOL_XDP, SO_ACCEPTCONN, SO_BROADCAST, SO_COOKIE, SO_DOMAIN,
SO_ERROR, SO_INCOMING_CPU, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE, SO_ORIGINAL_DST,
SO_PASSCRED, SO_PROTOCOL, SO_RCVBUF, SO_RCVBUFFORCE, SO_RCVTIMEO_NEW,
MSG_CMSG_CLOEXEC, MSG_CONFIRM, MSG_CTRUNC, MSG_DONTROUTE, MSG_DONTWAIT, MSG_EOR,
MSG_ERRQUEUE, MSG_MORE, MSG_NOSIGNAL, MSG_OOB, MSG_PEEK, MSG_TRUNC, MSG_WAITALL,
SCM_CREDENTIALS, SCM_RIGHTS, SHUT_RD, SHUT_RDWR, SHUT_WR, SOCK_DGRAM, SOCK_RAW, SOCK_RDM,
SOCK_SEQPACKET, SOCK_STREAM, SOL_SOCKET, SOL_XDP, SO_ACCEPTCONN, SO_BROADCAST, SO_COOKIE,
SO_DOMAIN, SO_ERROR, SO_INCOMING_CPU, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE,
SO_ORIGINAL_DST, SO_PASSCRED, SO_PROTOCOL, SO_RCVBUF, SO_RCVBUFFORCE, SO_RCVTIMEO_NEW,
SO_RCVTIMEO_NEW as SO_RCVTIMEO, SO_RCVTIMEO_OLD, SO_REUSEADDR, SO_REUSEPORT, SO_SNDBUF,
SO_SNDTIMEO_NEW, SO_SNDTIMEO_NEW as SO_SNDTIMEO, SO_SNDTIMEO_OLD, SO_TYPE, TCP_CONGESTION,
TCP_CORK, TCP_KEEPCNT, TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_NODELAY, TCP_QUICKACK,
Expand Down
27 changes: 27 additions & 0 deletions src/backend/linux_raw/net/send_recv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,30 @@ bitflags! {
const _ = !0;
}
}

bitflags! {
/// `MSG_*` flags returned from [`recvmsg`], in the `flags` field of
/// [`RecvMsgReturn`]
///
/// [`recvmsg`]: crate::net::recvmsg
/// [`RecvMsgReturn`]: crate::net::RecvMsgReturn
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct ReturnFlags: u32 {
/// `MSG_OOB`
const OOB = c::MSG_OOB;
/// `MSG_EOR`
const EOR = c::MSG_EOR;
/// `MSG_TRUNC`
const TRUNC = c::MSG_TRUNC;
/// `MSG_CTRUNC`
const CTRUNC = c::MSG_CTRUNC;
/// `MSG_ERRQUEUE`
const ERRQUEUE = c::MSG_ERRQUEUE;
/// `MSG_CMSG_CLOEXEC`
const CMSG_CLOEXEC = c::MSG_CMSG_CLOEXEC;

/// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
const _ = !0;
}
}
4 changes: 2 additions & 2 deletions src/backend/linux_raw/net/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::msghdr::{
with_noaddr_msghdr, with_recv_msghdr, with_unix_msghdr, with_v4_msghdr, with_v6_msghdr,
};
use super::read_sockaddr::{initialize_family_to_unspec, maybe_read_sockaddr_os, read_sockaddr_os};
use super::send_recv::{RecvFlags, SendFlags};
use super::send_recv::{RecvFlags, ReturnFlags, SendFlags};
#[cfg(target_os = "linux")]
use super::write_sockaddr::encode_sockaddr_xdp;
use super::write_sockaddr::{encode_sockaddr_v4, encode_sockaddr_v6};
Expand Down Expand Up @@ -293,7 +293,7 @@ pub(crate) fn recvmsg(
RecvMsgReturn {
bytes,
address: addr,
flags: RecvFlags::from_bits_retain(msghdr.msg_flags),
flags: ReturnFlags::from_bits_retain(msghdr.msg_flags),
}
})
})
Expand Down
2 changes: 1 addition & 1 deletion src/net/send_recv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use backend::fd::{AsFd, BorrowedFd};
use core::cmp::min;
use core::mem::MaybeUninit;

pub use backend::net::send_recv::{RecvFlags, SendFlags};
pub use backend::net::send_recv::{RecvFlags, ReturnFlags, SendFlags};

#[cfg(not(any(
windows,
Expand Down
22 changes: 19 additions & 3 deletions src/net/send_recv/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ use crate::fd::{AsFd, BorrowedFd, OwnedFd};
use crate::io::{self, IoSlice, IoSliceMut};
#[cfg(linux_kernel)]
use crate::net::UCred;

#[cfg(feature = "std")]
use core::fmt;
use core::iter::FusedIterator;
use core::marker::PhantomData;
use core::mem::{align_of, size_of, size_of_val, take};
#[cfg(linux_kernel)]
use core::ptr::addr_of;
use core::{ptr, slice};

use super::{RecvFlags, SendFlags, SocketAddrAny, SocketAddrV4, SocketAddrV6};
use super::{RecvFlags, ReturnFlags, SendFlags, SocketAddrAny, SocketAddrV4, SocketAddrV6};

/// Macro for defining the amount of space to allocate in a buffer for use with
/// [`RecvAncillaryBuffer::new`] and [`SendAncillaryBuffer::new`].
Expand Down Expand Up @@ -819,15 +820,30 @@ pub fn recvmsg(
/// The result of a successful [`recvmsg`] call.
pub struct RecvMsgReturn {
/// The number of bytes received.
///
/// When `RecvFlags::TRUNC` is in use, this may be greater than the
/// length of the buffer, as it reflects the number of bytes received
/// before truncation into the buffer.
pub bytes: usize,

/// The flags received.
pub flags: RecvFlags,
pub flags: ReturnFlags,

/// The address of the socket we received from, if any.
pub address: Option<SocketAddrAny>,
}

#[cfg(feature = "std")]
impl fmt::Debug for RecvMsgReturn {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("RecvMsgReturn")
.field("bytes", &self.bytes)
.field("flags", &self.flags)
.field("address", &self.address)
.finish()
}
}

/// An iterator over data in an ancillary buffer.
pub struct AncillaryIter<'data, T> {
/// The data we're iterating over.
Expand Down
6 changes: 5 additions & 1 deletion src/termios/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,24 +1211,28 @@ impl core::fmt::Debug for SpecialCodeIndex {
Self::VKILL => write!(f, "VKILL"),
#[cfg(not(any(
solarish,
target_os = "aix",
all(linux_kernel, any(target_arch = "sparc", target_arch = "sparc64"))
)))]
Self::VEOF => write!(f, "VEOF"),
#[cfg(not(any(
solarish,
target_os = "aix",
all(linux_kernel, any(target_arch = "sparc", target_arch = "sparc64"))
)))]
Self::VTIME => write!(f, "VTIME"),
#[cfg(not(any(
solarish,
target_os = "aix",
all(linux_kernel, any(target_arch = "sparc", target_arch = "sparc64"))
)))]
Self::VMIN => write!(f, "VMIN"),

// On Solarish platforms, and Linux on SPARC, `VMIN` and `VTIME`
// On Solarish platforms, Linux on SPARC, and AIX, `VMIN` and `VTIME`
// have the same value as `VEOF` and `VEOL`.
#[cfg(any(
solarish,
target_os = "aix",
all(linux_kernel, any(target_arch = "sparc", target_arch = "sparc64"))
))]
Self::VMIN => write!(f, "VMIN/VEOF"),
Expand Down
Loading
Loading