Skip to content

Commit

Permalink
Merge #2012
Browse files Browse the repository at this point in the history
2012: Enable socket and select on redox r=asomers a=coolreader18



Co-authored-by: Noa <[email protected]>
  • Loading branch information
bors[bot] and coolreader18 authored Apr 3, 2023
2 parents 7b0cd34 + fff2ca4 commit 6e3b9c5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 41 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
`sys::kevent::Kqueue::kevent`, and `sys::event::kqueue` is deprecated in
favor of `sys::kevent::Kqueue::new`.
([#1943](https://github.com/nix-rust/nix/pull/1943))
- `nix::socket` and `nix::select` are now available on Redox.
([#2012](https://github.com/nix-rust/nix/pull/2012))

### Fixed
- Fix `SockaddrIn6` bug that was swapping flowinfo and scope_id byte ordering.
Expand Down
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ targets = [
]

[dependencies]
libc = { git = "https://github.com/rust-lang/libc", rev = "44cc30c6b68427d3628926868758d35fe561bbe6", features = [ "extra_traits" ] }
libc = { git = "https://github.com/rust-lang/libc", rev = "60bf6d7fa9d561820ea562751ee455ccf67d3015", features = [ "extra_traits" ] }
bitflags = "1.1"
cfg-if = "1.0"
pin-utils = { version = "0.1.0", optional = true }
static_assertions = "1"

[target.'cfg(not(target_os = "redox"))'.dependencies]
memoffset = { version = "0.8", optional = true }

[features]
Expand Down
2 changes: 0 additions & 2 deletions src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ feature! {
pub mod resource;
}

#[cfg(not(target_os = "redox"))]
feature! {
#![feature = "poll"]
pub mod select;
Expand Down Expand Up @@ -138,7 +137,6 @@ feature! {
pub mod signalfd;
}

#[cfg(not(target_os = "redox"))]
feature! {
#![feature = "socket"]
#[allow(missing_docs)]
Expand Down
29 changes: 19 additions & 10 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ pub enum AddressFamily {
#[cfg_attr(docsrs, doc(cfg(all())))]
Ax25 = libc::AF_AX25,
/// IPX - Novell protocols
#[cfg(not(target_os = "redox"))]
Ipx = libc::AF_IPX,
/// AppleTalk
#[cfg(not(target_os = "redox"))]
AppleTalk = libc::AF_APPLETALK,
/// AX.25 packet layer protocol.
/// (see [netrom(4)](https://www.unix.com/man-page/linux/4/netrom/))
Expand Down Expand Up @@ -130,7 +132,7 @@ pub enum AddressFamily {
#[cfg_attr(docsrs, doc(cfg(all())))]
Rose = libc::AF_ROSE,
/// DECet protocol sockets.
#[cfg(not(target_os = "haiku"))]
#[cfg(not(any(target_os = "haiku", target_os = "redox")))]
Decnet = libc::AF_DECnet,
/// Reserved for "802.2LLC project"; never used.
#[cfg(any(target_os = "android", target_os = "linux"))]
Expand Down Expand Up @@ -162,7 +164,7 @@ pub enum AddressFamily {
#[cfg_attr(docsrs, doc(cfg(all())))]
Rds = libc::AF_RDS,
/// IBM SNA
#[cfg(not(target_os = "haiku"))]
#[cfg(not(any(target_os = "haiku", target_os = "redox")))]
Sna = libc::AF_SNA,
/// Socket interface over IrDA
#[cfg(any(target_os = "android", target_os = "linux"))]
Expand Down Expand Up @@ -202,7 +204,8 @@ pub enum AddressFamily {
target_os = "illumos",
target_os = "ios",
target_os = "macos",
target_os = "solaris"
target_os = "solaris",
target_os = "redox",
)))]
#[cfg_attr(docsrs, doc(cfg(all())))]
Bluetooth = libc::AF_BLUETOOTH,
Expand All @@ -219,7 +222,8 @@ pub enum AddressFamily {
#[cfg(not(any(
target_os = "illumos",
target_os = "solaris",
target_os = "haiku"
target_os = "haiku",
target_os = "redox",
)))]
#[cfg_attr(docsrs, doc(cfg(all())))]
Isdn = libc::AF_ISDN,
Expand Down Expand Up @@ -460,7 +464,8 @@ pub struct UnixAddr {
target_os = "android",
target_os = "fuchsia",
target_os = "illumos",
target_os = "linux"
target_os = "linux",
target_os = "redox",
))]
sun_len: u8,
}
Expand Down Expand Up @@ -624,7 +629,8 @@ impl UnixAddr {
if #[cfg(any(target_os = "android",
target_os = "fuchsia",
target_os = "illumos",
target_os = "linux"
target_os = "linux",
target_os = "redox",
))]
{
UnixAddr { sun, sun_len }
Expand Down Expand Up @@ -690,7 +696,8 @@ impl UnixAddr {
if #[cfg(any(target_os = "android",
target_os = "fuchsia",
target_os = "illumos",
target_os = "linux"
target_os = "linux",
target_os = "redox",
))]
{
self.sun_len
Expand Down Expand Up @@ -736,7 +743,8 @@ impl SockaddrLike for UnixAddr {
if #[cfg(any(target_os = "android",
target_os = "fuchsia",
target_os = "illumos",
target_os = "linux"
target_os = "linux",
target_os = "redox",
))] {
let su_len = len.unwrap_or(
mem::size_of::<libc::sockaddr_un>() as libc::socklen_t
Expand Down Expand Up @@ -1221,7 +1229,7 @@ pub union SockaddrStorage {
#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg_attr(docsrs, doc(cfg(all())))]
alg: AlgAddr,
#[cfg(feature = "net")]
#[cfg(all(feature = "net", not(target_os = "redox")))]
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
dl: LinkAddr,
#[cfg(any(target_os = "android", target_os = "linux"))]
Expand Down Expand Up @@ -2338,6 +2346,7 @@ mod tests {
}
}

#[cfg(not(target_os = "redox"))]
mod link {
#![allow(clippy::cast_ptr_alignment)]

Expand Down Expand Up @@ -2534,7 +2543,7 @@ mod tests {
nix_sin6.0.sin6_flowinfo = 0x12345678;
nix_sin6.0.sin6_scope_id = 0x9abcdef0;

let std_sin6 : std::net::SocketAddrV6 = nix_sin6.into();
let std_sin6: std::net::SocketAddrV6 = nix_sin6.into();
assert_eq!(nix_sin6, std_sin6.into());
}
}
Expand Down
50 changes: 28 additions & 22 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@
#[cfg(target_os = "linux")]
#[cfg(feature = "uio")]
use crate::sys::time::TimeSpec;
#[cfg(not(target_os = "redox"))]
#[cfg(feature = "uio")]
use crate::sys::time::TimeVal;
use crate::{errno::Errno, Result};
use cfg_if::cfg_if;
use libc::{self, c_int, c_void, size_t, socklen_t};
#[cfg(all(feature = "uio", not(target_os = "redox")))]
use libc::{
self, c_int, c_void, iovec, size_t, socklen_t, CMSG_DATA, CMSG_FIRSTHDR,
CMSG_LEN, CMSG_NXTHDR,
iovec, CMSG_DATA, CMSG_FIRSTHDR, CMSG_LEN, CMSG_NXTHDR, CMSG_SPACE,
};
#[cfg(not(target_os = "redox"))]
use std::io::{IoSlice, IoSliceMut};
#[cfg(feature = "net")]
use std::net;
use std::os::unix::io::RawFd;
use std::{mem, ptr, slice};
use std::{mem, ptr};

#[deny(missing_docs)]
mod addr;
Expand All @@ -38,14 +41,16 @@ pub use self::addr::{AddressFamily, UnixAddr};
#[cfg(not(any(
target_os = "illumos",
target_os = "solaris",
target_os = "haiku"
target_os = "haiku",
target_os = "redox",
)))]
#[cfg(feature = "net")]
pub use self::addr::{LinkAddr, SockaddrIn, SockaddrIn6};
#[cfg(any(
target_os = "illumos",
target_os = "solaris",
target_os = "haiku"
target_os = "haiku",
target_os = "redox",
))]
#[cfg(feature = "net")]
pub use self::addr::{SockaddrIn, SockaddrIn6};
Expand All @@ -60,16 +65,12 @@ pub use crate::sys::socket::addr::sys_control::SysControlAddr;
#[cfg(any(target_os = "android", target_os = "linux"))]
pub use crate::sys::socket::addr::vsock::VsockAddr;

#[cfg(feature = "uio")]
#[cfg(all(feature = "uio", not(target_os = "redox")))]
pub use libc::{cmsghdr, msghdr};
pub use libc::{sa_family_t, sockaddr, sockaddr_storage, sockaddr_un};
#[cfg(feature = "net")]
pub use libc::{sockaddr_in, sockaddr_in6};

// Needed by the cmsg_space macro
#[doc(hidden)]
pub use libc::{c_uint, CMSG_SPACE};

#[cfg(feature = "net")]
use crate::sys::socket::addr::{ipv4addr_to_libc, ipv6addr_to_libc};

Expand All @@ -92,10 +93,11 @@ pub enum SockType {
/// entire packet with each input system call.
SeqPacket = libc::SOCK_SEQPACKET,
/// Provides raw network protocol access.
#[cfg(not(target_os = "redox"))]
Raw = libc::SOCK_RAW,
/// Provides a reliable datagram layer that does not
/// guarantee ordering.
#[cfg(not(any(target_os = "haiku")))]
#[cfg(not(any(target_os = "haiku", target_os = "redox")))]
Rdm = libc::SOCK_RDM,
}
// The TryFrom impl could've been derived using libc_enum!. But for
Expand All @@ -109,8 +111,9 @@ impl TryFrom<i32> for SockType {
libc::SOCK_STREAM => Ok(Self::Stream),
libc::SOCK_DGRAM => Ok(Self::Datagram),
libc::SOCK_SEQPACKET => Ok(Self::SeqPacket),
#[cfg(not(target_os = "redox"))]
libc::SOCK_RAW => Ok(Self::Raw),
#[cfg(not(any(target_os = "haiku")))]
#[cfg(not(any(target_os = "haiku", target_os = "redox")))]
libc::SOCK_RDM => Ok(Self::Rdm),
_ => Err(Errno::EINVAL),
}
Expand Down Expand Up @@ -239,7 +242,7 @@ libc_bitflags! {
///
/// For use with [`Timestamping`][sockopt::Timestamping].
/// [Further reading](https://www.kernel.org/doc/html/latest/networking/timestamping.html)
pub struct TimestampingFlag: c_uint {
pub struct TimestampingFlag: libc::c_uint {
/// Report any software timestamps when available.
SOF_TIMESTAMPING_SOFTWARE;
/// Report hardware timestamps as generated by SOF_TIMESTAMPING_TX_HARDWARE when available.
Expand Down Expand Up @@ -456,7 +459,7 @@ cfg_if! {
/// Returns a list group identifiers (the first one being the effective GID)
pub fn groups(&self) -> &[libc::gid_t] {
unsafe {
slice::from_raw_parts(
std::slice::from_raw_parts(
self.0.cmcred_groups.as_ptr() as *const libc::gid_t,
self.0.cmcred_ngroups as _
)
Expand Down Expand Up @@ -549,6 +552,7 @@ impl Ipv6MembershipRequest {
}
}

#[cfg(not(target_os = "redox"))]
feature! {
#![feature = "uio"]

Expand Down Expand Up @@ -578,18 +582,19 @@ feature! {
macro_rules! cmsg_space {
( $( $x:ty ),* ) => {
{
let mut space = 0;
$(
// CMSG_SPACE is always safe
space += unsafe {
$crate::sys::socket::CMSG_SPACE(::std::mem::size_of::<$x>() as $crate::sys::socket::c_uint)
} as usize;
)*
let space = 0 $(+ $crate::sys::socket::cmsg_space::<$x>())*;
Vec::<u8>::with_capacity(space)
}
}
}

#[inline]
#[doc(hidden)]
pub fn cmsg_space<T>() -> usize {
// SAFETY: CMSG_SPACE is always safe
unsafe { libc::CMSG_SPACE(mem::size_of::<T>() as libc::c_uint) as usize }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
/// Contains outcome of sending or receiving a message
///
Expand Down Expand Up @@ -984,7 +989,7 @@ impl ControlMessageOwned {
ControlMessageOwned::Ipv6OrigDstAddr(dl)
},
(_, _) => {
let sl = slice::from_raw_parts(p, len);
let sl = std::slice::from_raw_parts(p, len);
let ucmsg = UnknownCmsg(*header, Vec::<u8>::from(sl));
ControlMessageOwned::Unknown(ucmsg)
}
Expand Down Expand Up @@ -2392,6 +2397,7 @@ pub fn shutdown(df: RawFd, how: Shutdown) -> Result<()> {

#[cfg(test)]
mod tests {
#[cfg(not(target_os = "redox"))]
#[test]
fn can_use_cmsg_space() {
let _ = cmsg_space!(u8);
Expand Down
8 changes: 4 additions & 4 deletions src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,13 +544,13 @@ cfg_if! {
sockopt_impl!(
/// The maximum segment size for outgoing TCP packets.
TcpMaxSeg, Both, libc::IPPROTO_TCP, libc::TCP_MAXSEG, u32);
} else {
} else if #[cfg(not(target_os = "redox"))] {
sockopt_impl!(
/// The maximum segment size for outgoing TCP packets.
TcpMaxSeg, GetOnly, libc::IPPROTO_TCP, libc::TCP_MAXSEG, u32);
}
}
#[cfg(not(any(target_os = "openbsd", target_os = "haiku")))]
#[cfg(not(any(target_os = "openbsd", target_os = "haiku", target_os = "redox")))]
#[cfg(feature = "net")]
sockopt_impl!(
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
Expand All @@ -572,7 +572,7 @@ sockopt_impl!(
libc::TCP_REPAIR,
u32
);
#[cfg(not(any(target_os = "openbsd", target_os = "haiku")))]
#[cfg(not(any(target_os = "openbsd", target_os = "haiku", target_os = "redox")))]
#[cfg(feature = "net")]
sockopt_impl!(
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
Expand Down Expand Up @@ -693,7 +693,7 @@ sockopt_impl!(
libc::SO_TIMESTAMPING,
super::TimestampingFlag
);
#[cfg(not(target_os = "haiku"))]
#[cfg(not(any(target_os = "haiku", target_os = "redox")))]
sockopt_impl!(
/// Enable or disable the receiving of the `SO_TIMESTAMP` control message.
ReceiveTimestamp,
Expand Down

0 comments on commit 6e3b9c5

Please sign in to comment.