From 75e798f1790a47c8792b746b1602d4abcc3ca81f Mon Sep 17 00:00:00 2001 From: Bryant Mairs Date: Tue, 5 Dec 2017 19:09:47 -0800 Subject: [PATCH 1/8] Expand derives for all libc_bitflags! use --- src/macros.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 15f7505c3a..3d1b0e4b76 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -90,7 +90,7 @@ macro_rules! libc_enum { } ) => { $($attrs)* - #[derive(Clone, Copy, Debug, PartialEq, Eq)] + #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] enum $BitFlags { $($entries)* } @@ -106,7 +106,7 @@ macro_rules! libc_enum { } ) => { $($attrs)* - #[derive(Clone, Copy, Debug, PartialEq, Eq)] + #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] pub enum $BitFlags { $($entries)* } From de9d72dd2e4ec534343c2c1a94bb9867296e1a0c Mon Sep 17 00:00:00 2001 From: Bryant Mairs Date: Tue, 5 Dec 2017 19:10:44 -0800 Subject: [PATCH 2/8] Add more derives for SignalFd and various enums --- CHANGELOG.md | 2 ++ src/sys/epoll.rs | 2 +- src/sys/signalfd.rs | 2 +- src/sys/socket/mod.rs | 6 ++++-- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed34ef41dc..4cee749a5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Added the `from_raw()` method to `WaitStatus` for converting raw status values to `WaitStatus` independent of syscalls. ([#741](https://github.com/nix-rust/nix/pull/741)) +- Added more standard trait implementations for various types. + ([#814](https://github.com/nix-rust/nix/pull/814)) ### Changed - Use native `pipe2` on all BSD targets. Users should notice no difference. diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index 43a543ab12..c6c2eab33c 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -28,7 +28,7 @@ libc_bitflags!( } ); -#[derive(Clone, Copy, Eq, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] #[repr(i32)] pub enum EpollOp { EpollCtlAdd = libc::EPOLL_CTL_ADD, diff --git a/src/sys/signalfd.rs b/src/sys/signalfd.rs index 7ea09ee349..0da9187e7f 100644 --- a/src/sys/signalfd.rs +++ b/src/sys/signalfd.rs @@ -79,7 +79,7 @@ pub fn signalfd(fd: RawFd, mask: &SigSet, flags: SfdFlags) -> Result { /// Err(err) => (), // some error happend /// } /// ``` -#[derive(Debug)] +#[derive(Clone, Debug, Eq, Hash, PartialEq)] pub struct SignalFd(RawFd); impl SignalFd { diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index bd8764b70c..2102c1cee4 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -77,6 +77,7 @@ pub enum SockType { /// Constants used in [`socket`](fn.socket.html) and [`socketpair`](fn.socketpair.html) /// to specify the protocol to use. #[repr(i32)] +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub enum SockProtocol { /// TCP protocol ([ip(7)](http://man7.org/linux/man-pages/man7/ip.7.html)) Tcp = libc::IPPROTO_TCP, @@ -825,13 +826,14 @@ pub struct ucred { /// /// [Further reading](http://pubs.opengroup.org/onlinepubs/9699919799/functions/setsockopt.html) #[repr(i32)] +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub enum SockLevel { Socket = libc::SOL_SOCKET, Tcp = libc::IPPROTO_TCP, Ip = libc::IPPROTO_IP, Ipv6 = libc::IPPROTO_IPV6, Udp = libc::IPPROTO_UDP, - #[cfg(any(target_os = "linux", target_os = "android"))] + #[cfg(any(target_os = "android", target_os = "linux"))] Netlink = libc::SOL_NETLINK, } @@ -938,7 +940,7 @@ pub unsafe fn sockaddr_storage_to_addr( } -#[derive(Clone, Copy, PartialEq, Eq, Debug)] +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub enum Shutdown { /// Further receptions will be disallowed. Read, From e95cf8827ce26f4ebb99523ac9dabf827a31966c Mon Sep 17 00:00:00 2001 From: Bryant Mairs Date: Tue, 5 Dec 2017 19:36:53 -0800 Subject: [PATCH 3/8] Remove internal kernel flags --- CHANGELOG.md | 4 ++++ src/mount.rs | 3 --- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cee749a5e..2b293b5fe1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -97,6 +97,10 @@ This project adheres to [Semantic Versioning](http://semver.org/). ([#747](https://github.com/nix-rust/nix/pull/747)) - The `Errno` variants are no longer reexported from the `errno` module. `Errno` itself is no longer reexported from the crate root and instead must be accessed using the `errno` module. ([#696](https://github.com/nix-rust/nix/pull/696)) +- Removed `MS_VERBOSE`, `MS_NOSEC`, and `MS_BORN` from `MsFlags`. These + are internal kernel flags and should never have been exposed. + ([#814](https://github.com/nix-rust/nix/pull/814)) + ## [0.9.0] 2017-07-23 diff --git a/src/mount.rs b/src/mount.rs index fb43e24cb1..2969154307 100644 --- a/src/mount.rs +++ b/src/mount.rs @@ -18,7 +18,6 @@ bitflags!( const MS_BIND = libc::MS_BIND; // Linux 2.4.0 - Bind directory at different place const MS_MOVE = libc::MS_MOVE; const MS_REC = libc::MS_REC; - const MS_VERBOSE = 1 << 15; // Deprecated const MS_SILENT = libc::MS_SILENT; const MS_POSIXACL = libc::MS_POSIXACL; const MS_UNBINDABLE = libc::MS_UNBINDABLE; @@ -29,8 +28,6 @@ bitflags!( const MS_KERNMOUNT = libc::MS_KERNMOUNT; const MS_I_VERSION = libc::MS_I_VERSION; const MS_STRICTATIME = libc::MS_STRICTATIME; - const MS_NOSEC = 1 << 28; - const MS_BORN = 1 << 29; const MS_ACTIVE = libc::MS_ACTIVE; const MS_NOUSER = libc::MS_NOUSER; const MS_RMT_MASK = libc::MS_RMT_MASK; From 3924361d8517e367ee8eef4779390e49e5f77e49 Mon Sep 17 00:00:00 2001 From: Bryant Mairs Date: Tue, 5 Dec 2017 19:37:10 -0800 Subject: [PATCH 4/8] Migrate more bitflags to use libc_bitflags! --- src/mount.rs | 69 +++++++++++++++++++++++++++------------------- src/sys/statvfs.rs | 26 ++++++++--------- 2 files changed, 53 insertions(+), 42 deletions(-) diff --git a/src/mount.rs b/src/mount.rs index 2969154307..8fe995135c 100644 --- a/src/mount.rs +++ b/src/mount.rs @@ -3,36 +3,47 @@ use libc; use {Result, NixPath}; use errno::Errno; -bitflags!( +libc_bitflags!( pub struct MsFlags: c_ulong { - const MS_RDONLY = libc::MS_RDONLY; // Mount read-only - const MS_NOSUID = libc::MS_NOSUID; // Ignore suid and sgid bits - const MS_NODEV = libc::MS_NODEV; // Disallow access to device special files - const MS_NOEXEC = libc::MS_NOEXEC; // Disallow program execution - const MS_SYNCHRONOUS = libc::MS_SYNCHRONOUS; // Writes are synced at once - const MS_REMOUNT = libc::MS_REMOUNT; // Alter flags of a mounted FS - const MS_MANDLOCK = libc::MS_MANDLOCK; // Allow mandatory locks on a FS - const MS_DIRSYNC = libc::MS_DIRSYNC; // Directory modifications are synchronous - const MS_NOATIME = libc::MS_NOATIME; // Do not update access times - const MS_NODIRATIME = libc::MS_NODIRATIME; // Do not update directory access times - const MS_BIND = libc::MS_BIND; // Linux 2.4.0 - Bind directory at different place - const MS_MOVE = libc::MS_MOVE; - const MS_REC = libc::MS_REC; - const MS_SILENT = libc::MS_SILENT; - const MS_POSIXACL = libc::MS_POSIXACL; - const MS_UNBINDABLE = libc::MS_UNBINDABLE; - const MS_PRIVATE = libc::MS_PRIVATE; - const MS_SLAVE = libc::MS_SLAVE; - const MS_SHARED = libc::MS_SHARED; - const MS_RELATIME = libc::MS_RELATIME; - const MS_KERNMOUNT = libc::MS_KERNMOUNT; - const MS_I_VERSION = libc::MS_I_VERSION; - const MS_STRICTATIME = libc::MS_STRICTATIME; - const MS_ACTIVE = libc::MS_ACTIVE; - const MS_NOUSER = libc::MS_NOUSER; - const MS_RMT_MASK = libc::MS_RMT_MASK; - const MS_MGC_VAL = libc::MS_MGC_VAL; - const MS_MGC_MSK = libc::MS_MGC_MSK; + /// Mount read-only + MS_RDONLY; + /// Ignore suid and sgid bits + MS_NOSUID; + /// Disallow access to device special files + MS_NODEV; + /// Disallow program execution + MS_NOEXEC; + /// Writes are synced at once + MS_SYNCHRONOUS; + /// Alter flags of a mounted FS + MS_REMOUNT; + /// Allow mandatory locks on a FS + MS_MANDLOCK; + /// Directory modifications are synchronous + MS_DIRSYNC; + /// Do not update access times + MS_NOATIME; + /// Do not update directory access times + MS_NODIRATIME; + /// Linux 2.4.0 - Bind directory at different place + MS_BIND; + MS_MOVE; + MS_REC; + MS_SILENT; + MS_POSIXACL; + MS_UNBINDABLE; + MS_PRIVATE; + MS_SLAVE; + MS_SHARED; + MS_RELATIME; + MS_KERNMOUNT; + MS_I_VERSION; + MS_STRICTATIME; + MS_ACTIVE; + MS_NOUSER; + MS_RMT_MASK; + MS_MGC_VAL; + MS_MGC_MSK; } ); diff --git a/src/sys/statvfs.rs b/src/sys/statvfs.rs index 0a0dab7fe8..fbd0570eff 100644 --- a/src/sys/statvfs.rs +++ b/src/sys/statvfs.rs @@ -10,45 +10,45 @@ use libc::{self, c_ulong}; use {Result, NixPath}; use errno::Errno; -bitflags!( +libc_bitflags!( /// File system mount Flags #[repr(C)] #[derive(Default)] pub struct FsFlags: c_ulong { /// Read Only - const RDONLY = libc::ST_RDONLY; + ST_RDONLY; /// Do not allow the set-uid bits to have an effect - const NOSUID = libc::ST_NOSUID; + ST_NOSUID; /// Do not interpret character or block-special devices #[cfg(any(target_os = "android", target_os = "linux"))] - const NODEV = libc::ST_NODEV; + ST_NODEV; /// Do not allow execution of binaries on the filesystem #[cfg(any(target_os = "android", target_os = "linux"))] - const NOEXEC = libc::ST_NOEXEC; + ST_NOEXEC; /// All IO should be done synchronously #[cfg(any(target_os = "android", target_os = "linux"))] - const SYNCHRONOUS = libc::ST_SYNCHRONOUS; + ST_SYNCHRONOUS; /// Allow mandatory locks on the filesystem #[cfg(any(target_os = "android", target_os = "linux"))] - const MANDLOCK = libc::ST_MANDLOCK; + ST_MANDLOCK; /// Write on file/directory/symlink #[cfg(any(target_os = "android", target_os = "linux"))] - const WRITE = libc::ST_WRITE; + ST_WRITE; /// Append-only file #[cfg(any(target_os = "android", target_os = "linux"))] - const APPEND = libc::ST_APPEND; + ST_APPEND; /// Immutable file #[cfg(any(target_os = "android", target_os = "linux"))] - const IMMUTABLE = libc::ST_IMMUTABLE; + ST_IMMUTABLE; /// Do not update access times on files #[cfg(any(target_os = "android", target_os = "linux"))] - const NOATIME = libc::ST_NOATIME; + ST_NOATIME; /// Do not update access times on files #[cfg(any(target_os = "android", target_os = "linux"))] - const NODIRATIME = libc::ST_NODIRATIME; + ST_NODIRATIME; /// Update access time relative to modify/change time #[cfg(any(target_os = "android", all(target_os = "linux", not(target_env = "musl"))))] - const RELATIME = libc::ST_RELATIME; + ST_RELATIME; } ); From ff79dcb21935e9892f46b3f95db188b64488d4a0 Mon Sep 17 00:00:00 2001 From: Bryant Mairs Date: Wed, 6 Dec 2017 07:52:22 -0800 Subject: [PATCH 5/8] Use libc::linger --- src/sys/socket/mod.rs | 7 ------- src/sys/socket/sockopt.rs | 2 +- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index 2102c1cee4..bf2e330802 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -800,13 +800,6 @@ pub fn send(fd: RawFd, buf: &[u8], flags: MsgFlags) -> Result { Errno::result(ret).map(|r| r as usize) } -#[repr(C)] -#[derive(Clone, Copy, Debug)] -pub struct linger { - pub l_onoff: c_int, - pub l_linger: c_int -} - #[repr(C)] #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub struct ucred { diff --git a/src/sys/socket/sockopt.rs b/src/sys/socket/sockopt.rs index 4741ec6640..fa8d71e2fe 100644 --- a/src/sys/socket/sockopt.rs +++ b/src/sys/socket/sockopt.rs @@ -132,7 +132,7 @@ macro_rules! sockopt_impl { sockopt_impl!(Both, ReuseAddr, libc::SOL_SOCKET, libc::SO_REUSEADDR, bool); sockopt_impl!(Both, ReusePort, libc::SOL_SOCKET, libc::SO_REUSEPORT, bool); sockopt_impl!(Both, TcpNoDelay, libc::IPPROTO_TCP, libc::TCP_NODELAY, bool); -sockopt_impl!(Both, Linger, libc::SOL_SOCKET, libc::SO_LINGER, super::linger); +sockopt_impl!(Both, Linger, libc::SOL_SOCKET, libc::SO_LINGER, libc::linger); sockopt_impl!(SetOnly, IpAddMembership, libc::IPPROTO_IP, libc::IP_ADD_MEMBERSHIP, super::ip_mreq); sockopt_impl!(SetOnly, IpDropMembership, libc::IPPROTO_IP, libc::IP_DROP_MEMBERSHIP, super::ip_mreq); #[cfg(not(any(target_os = "dragonfly", target_os = "freebsd", target_os = "ios", target_os = "macos", target_os = "netbsd", target_os = "openbsd")))] From 0d78c04d80feb8db1f70a3150e40cd3d29426c01 Mon Sep 17 00:00:00 2001 From: Bryant Mairs Date: Wed, 6 Dec 2017 07:58:10 -0800 Subject: [PATCH 6/8] Replace custom ucred struct with newtype wrapper around libc::ucred --- CHANGELOG.md | 7 +++-- src/sys/socket/mod.rs | 57 ++++++++++++++++++++++++++++++++------- src/sys/socket/sockopt.rs | 4 +-- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b293b5fe1..3a1e266073 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ([#739](https://github.com/nix-rust/nix/pull/739)) - Expose `signalfd` module on Android as well. ([#739](https://github.com/nix-rust/nix/pull/739)) -- Added nix::sys::ptrace::detach. +- Added nix::sys::ptrace::detach. ([#749](https://github.com/nix-rust/nix/pull/749)) - Added timestamp socket control message variant: `nix::sys::socket::ControlMessage::ScmTimestamp` @@ -77,11 +77,14 @@ This project adheres to [Semantic Versioning](http://semver.org/). ([#731](https://github.com/nix-rust/nix/pull/731)) - Marked `pty::ptsname` function as `unsafe` ([#744](https://github.com/nix-rust/nix/pull/744)) -- Moved constants ptrace request, event and options to enums and updated ptrace functions and argument types accordingly. +- Moved constants ptrace request, event and options to enums and updated ptrace functions and argument types accordingly. ([#749](https://github.com/nix-rust/nix/pull/749)) - `AioCb::Drop` will now panic if the `AioCb` is still in-progress ([#715](https://github.com/nix-rust/nix/pull/715)) - Restricted `nix::sys::socket::UnixAddr::new_abstract` to Linux and Android only. ([#785](https://github.com/nix-rust/nix/pull/785)) +- The `ucred` struct has been removed in favor of a `UserCredentials` struct that + contains only getters for its fields. + ([#814](https://github.com/nix-rust/nix/pull/814)) ### Fixed diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index bf2e330802..e456ee5378 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -4,7 +4,7 @@ use {Error, Result}; use errno::Errno; use features; -use libc::{self, c_void, c_int, socklen_t, size_t, pid_t, uid_t, gid_t}; +use libc::{self, c_void, c_int, socklen_t, size_t}; use std::{mem, ptr, slice}; use std::os::unix::io::RawFd; use sys::time::TimeVal; @@ -175,6 +175,53 @@ libc_bitflags!{ } } +cfg_if! { + if #[cfg(all(target_os = "linux", not(target_arch = "arm")))] { + use std::fmt; + + /// Unix credentials of the sending process. + /// + /// This struct is used with the `SO_PEERCRED` ancillary message for UNIX sockets. + #[repr(C)] + #[derive(Clone, Copy)] + pub struct UnixCredentials(libc::ucred); + + impl UnixCredentials { + /// Returns the process identifier + pub fn pid(&self) -> libc::pid_t { + self.0.pid + } + + /// Returns the user identifier + pub fn uid(&self) -> libc::uid_t { + self.0.uid + } + + /// Returns the group identifier + pub fn gid(&self) -> libc::gid_t { + self.0.gid + } + } + + impl PartialEq for UnixCredentials { + fn eq(&self, other: &Self) -> bool { + self.0.pid == other.0.pid && self.0.uid == other.0.uid && self.0.gid == other.0.gid + } + } + impl Eq for UnixCredentials {} + + impl fmt::Debug for UnixCredentials { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("UnixCredentials") + .field("pid", &self.0.pid) + .field("uid", &self.0.uid) + .field("gid", &self.0.gid) + .finish() + } + } + } +} + /// Copy the in-memory representation of src into the byte slice dst, /// updating the slice to point to the remainder of dst only. Unsafe /// because it exposes all bytes in src, which may be UB if some of them @@ -800,14 +847,6 @@ pub fn send(fd: RawFd, buf: &[u8], flags: MsgFlags) -> Result { Errno::result(ret).map(|r| r as usize) } -#[repr(C)] -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -pub struct ucred { - pid: pid_t, - uid: uid_t, - gid: gid_t, -} - /* * * ===== Socket Options ===== diff --git a/src/sys/socket/sockopt.rs b/src/sys/socket/sockopt.rs index fa8d71e2fe..6737433f68 100644 --- a/src/sys/socket/sockopt.rs +++ b/src/sys/socket/sockopt.rs @@ -152,7 +152,7 @@ sockopt_impl!(Both, OobInline, libc::SOL_SOCKET, libc::SO_OOBINLINE, bool); sockopt_impl!(GetOnly, SocketError, libc::SOL_SOCKET, libc::SO_ERROR, i32); sockopt_impl!(Both, KeepAlive, libc::SOL_SOCKET, libc::SO_KEEPALIVE, bool); #[cfg(all(target_os = "linux", not(target_arch="arm")))] -sockopt_impl!(GetOnly, PeerCredentials, libc::SOL_SOCKET, libc::SO_PEERCRED, super::ucred); +sockopt_impl!(GetOnly, PeerCredentials, libc::SOL_SOCKET, libc::SO_PEERCRED, super::UnixCredentials); #[cfg(any(target_os = "macos", target_os = "ios"))] sockopt_impl!(Both, TcpKeepAlive, libc::IPPROTO_TCP, libc::TCP_KEEPALIVE, u32); @@ -384,7 +384,7 @@ mod test { let a_cred = getsockopt(a, super::PeerCredentials).unwrap(); let b_cred = getsockopt(b, super::PeerCredentials).unwrap(); assert_eq!(a_cred, b_cred); - assert!(a_cred.pid != 0); + assert!(a_cred.pid() != 0); } #[test] From 2c4315ac4f81c1ed60ae96ec4a58dc52689b6def Mon Sep 17 00:00:00 2001 From: Bryant Mairs Date: Tue, 5 Dec 2017 20:06:23 -0800 Subject: [PATCH 7/8] Replace ip(v6)_mreq structs with libc equivalent --- CHANGELOG.md | 3 ++ src/sys/socket/mod.rs | 87 +++++++++++++++++++++++++++++++------ src/sys/socket/multicast.rs | 40 ----------------- src/sys/socket/sockopt.rs | 26 ++++++----- 4 files changed, 92 insertions(+), 64 deletions(-) delete mode 100644 src/sys/socket/multicast.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a1e266073..5e8008d850 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,6 +85,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). - The `ucred` struct has been removed in favor of a `UserCredentials` struct that contains only getters for its fields. ([#814](https://github.com/nix-rust/nix/pull/814)) +- Both `ip_mreq` and `ipv6_mreq` have been replaced with `IpMembershipRequest` and + `Ipv6MembershipRequest`. + ([#814](https://github.com/nix-rust/nix/pull/814)) ### Fixed diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index e456ee5378..6292bb0209 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -5,14 +5,13 @@ use {Error, Result}; use errno::Errno; use features; use libc::{self, c_void, c_int, socklen_t, size_t}; -use std::{mem, ptr, slice}; +use std::{fmt, mem, ptr, slice}; use std::os::unix::io::RawFd; use sys::time::TimeVal; use sys::uio::IoVec; mod addr; mod ffi; -mod multicast; pub mod sockopt; /* @@ -34,22 +33,14 @@ pub use self::addr::{ pub use ::sys::socket::addr::netlink::NetlinkAddr; pub use libc::{ - in_addr, - in6_addr, + sa_family_t, sockaddr, sockaddr_in, sockaddr_in6, + sockaddr_storage, sockaddr_un, - sa_family_t, -}; - -pub use self::multicast::{ - ip_mreq, - ipv6_mreq, }; -pub use libc::sockaddr_storage; - /// These constants are used to specify the communication semantics /// when creating a socket with [`socket()`](fn.socket.html) #[derive(Clone, Copy, PartialEq, Eq, Debug)] @@ -177,8 +168,6 @@ libc_bitflags!{ cfg_if! { if #[cfg(all(target_os = "linux", not(target_arch = "arm")))] { - use std::fmt; - /// Unix credentials of the sending process. /// /// This struct is used with the `SO_PEERCRED` ancillary message for UNIX sockets. @@ -222,6 +211,76 @@ cfg_if! { } } +/// Request for multicast socket operations +/// +/// This is a wrapper type around `ip_mreq`. +#[repr(C)] +#[derive(Clone, Copy)] +pub struct IpMembershipRequest(libc::ip_mreq); + +impl IpMembershipRequest { + /// Instantiate a new `IpMembershipRequest` + /// + /// If `interface` is `None`, then `Ipv4Addr::any()` will be used for the interface. + pub fn new(group: Ipv4Addr, interface: Option) -> Self { + IpMembershipRequest(libc::ip_mreq { + imr_multiaddr: group.0, + imr_interface: interface.unwrap_or(Ipv4Addr::any()).0, + }) + } +} + +impl PartialEq for IpMembershipRequest { + fn eq(&self, other: &Self) -> bool { + self.0.imr_multiaddr.s_addr == other.0.imr_multiaddr.s_addr + && self.0.imr_interface.s_addr == other.0.imr_interface.s_addr + } +} +impl Eq for IpMembershipRequest {} + +impl fmt::Debug for IpMembershipRequest { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("IpMembershipRequest") + .field("imr_multiaddr", &self.0.imr_multiaddr.s_addr) + .field("imr_interface", &self.0.imr_interface.s_addr) + .finish() + } +} + +/// Request for ipv6 multicast socket operations +/// +/// This is a wrapper type around `ipv6_mreq`. +#[repr(C)] +#[derive(Clone, Copy)] +pub struct Ipv6MembershipRequest(libc::ipv6_mreq); + +impl Ipv6MembershipRequest { + /// Instantiate a new `Ipv6MembershipRequest` + pub fn new(group: Ipv6Addr) -> Self { + Ipv6MembershipRequest(libc::ipv6_mreq { + ipv6mr_multiaddr: group.0, + ipv6mr_interface: 0, + }) + } +} + +impl PartialEq for Ipv6MembershipRequest { + fn eq(&self, other: &Self) -> bool { + self.0.ipv6mr_multiaddr.s6_addr == other.0.ipv6mr_multiaddr.s6_addr && + self.0.ipv6mr_interface == other.0.ipv6mr_interface + } +} +impl Eq for Ipv6MembershipRequest {} + +impl fmt::Debug for Ipv6MembershipRequest { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("Ipv6MembershipRequest") + .field("ipv6mr_multiaddr", &self.0.ipv6mr_multiaddr.s6_addr) + .field("ipv6mr_interface", &self.0.ipv6mr_interface) + .finish() + } +} + /// Copy the in-memory representation of src into the byte slice dst, /// updating the slice to point to the remainder of dst only. Unsafe /// because it exposes all bytes in src, which may be UB if some of them diff --git a/src/sys/socket/multicast.rs b/src/sys/socket/multicast.rs deleted file mode 100644 index 7561bb3595..0000000000 --- a/src/sys/socket/multicast.rs +++ /dev/null @@ -1,40 +0,0 @@ -use super::addr::{Ipv4Addr, Ipv6Addr}; -use libc::{in_addr, in6_addr, c_uint}; -use std::fmt; - -#[repr(C)] -#[derive(Clone, Copy)] -pub struct ip_mreq { - pub imr_multiaddr: in_addr, - pub imr_interface: in_addr, -} - -impl fmt::Debug for ip_mreq { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "ip_mreq {{ imr_multiaddr: {{ s_addr: 0x{:x} }}, imr_interface: {{ s_addr: 0x{:x} }} }}", - self.imr_multiaddr.s_addr, self.imr_interface.s_addr) - } -} - -impl ip_mreq { - pub fn new(group: Ipv4Addr, interface: Option) -> ip_mreq { - ip_mreq { - imr_multiaddr: group.0, - imr_interface: interface.unwrap_or(Ipv4Addr::any()).0 - } - } -} - -pub struct ipv6_mreq { - pub ipv6mr_multiaddr: in6_addr, - pub ipv6mr_interface: c_uint, -} - -impl ipv6_mreq { - pub fn new(group: Ipv6Addr) -> ipv6_mreq { - ipv6_mreq { - ipv6mr_multiaddr: group.0, - ipv6mr_interface: 0, - } - } -} diff --git a/src/sys/socket/sockopt.rs b/src/sys/socket/sockopt.rs index 6737433f68..065d3101cf 100644 --- a/src/sys/socket/sockopt.rs +++ b/src/sys/socket/sockopt.rs @@ -133,16 +133,22 @@ sockopt_impl!(Both, ReuseAddr, libc::SOL_SOCKET, libc::SO_REUSEADDR, bool); sockopt_impl!(Both, ReusePort, libc::SOL_SOCKET, libc::SO_REUSEPORT, bool); sockopt_impl!(Both, TcpNoDelay, libc::IPPROTO_TCP, libc::TCP_NODELAY, bool); sockopt_impl!(Both, Linger, libc::SOL_SOCKET, libc::SO_LINGER, libc::linger); -sockopt_impl!(SetOnly, IpAddMembership, libc::IPPROTO_IP, libc::IP_ADD_MEMBERSHIP, super::ip_mreq); -sockopt_impl!(SetOnly, IpDropMembership, libc::IPPROTO_IP, libc::IP_DROP_MEMBERSHIP, super::ip_mreq); -#[cfg(not(any(target_os = "dragonfly", target_os = "freebsd", target_os = "ios", target_os = "macos", target_os = "netbsd", target_os = "openbsd")))] -sockopt_impl!(SetOnly, Ipv6AddMembership, libc::IPPROTO_IPV6, libc::IPV6_ADD_MEMBERSHIP, super::ipv6_mreq); -#[cfg(not(any(target_os = "dragonfly", target_os = "freebsd", target_os = "ios", target_os = "macos", target_os = "netbsd", target_os = "openbsd")))] -sockopt_impl!(SetOnly, Ipv6DropMembership, libc::IPPROTO_IPV6, libc::IPV6_DROP_MEMBERSHIP, super::ipv6_mreq); -#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "ios", target_os = "macos", target_os = "netbsd", target_os = "openbsd"))] -sockopt_impl!(SetOnly, Ipv6AddMembership, libc::IPPROTO_IPV6, libc::IPV6_JOIN_GROUP, super::ipv6_mreq); -#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "ios", target_os = "macos", target_os = "netbsd", target_os = "openbsd"))] -sockopt_impl!(SetOnly, Ipv6DropMembership, libc::IPPROTO_IPV6, libc::IPV6_LEAVE_GROUP, super::ipv6_mreq); +sockopt_impl!(SetOnly, IpAddMembership, libc::IPPROTO_IP, libc::IP_ADD_MEMBERSHIP, super::IpMembershipRequest); +sockopt_impl!(SetOnly, IpDropMembership, libc::IPPROTO_IP, libc::IP_DROP_MEMBERSHIP, super::IpMembershipRequest); +cfg_if! { + if #[cfg(any(target_os = "android", target_os = "linux"))] { + sockopt_impl!(SetOnly, Ipv6AddMembership, libc::IPPROTO_IPV6, libc::IPV6_ADD_MEMBERSHIP, super::Ipv6MembershipRequest); + sockopt_impl!(SetOnly, Ipv6DropMembership, libc::IPPROTO_IPV6, libc::IPV6_DROP_MEMBERSHIP, super::Ipv6MembershipRequest); + } else if #[cfg(any(target_os = "dragonfly", + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd"))] { + sockopt_impl!(SetOnly, Ipv6AddMembership, libc::IPPROTO_IPV6, libc::IPV6_JOIN_GROUP, super::Ipv6MembershipRequest); + sockopt_impl!(SetOnly, Ipv6DropMembership, libc::IPPROTO_IPV6, libc::IPV6_LEAVE_GROUP, super::Ipv6MembershipRequest); + } +} sockopt_impl!(Both, IpMulticastTtl, libc::IPPROTO_IP, libc::IP_MULTICAST_TTL, u8); sockopt_impl!(Both, IpMulticastLoop, libc::IPPROTO_IP, libc::IP_MULTICAST_LOOP, bool); sockopt_impl!(Both, ReceiveTimeout, libc::SOL_SOCKET, libc::SO_RCVTIMEO, TimeVal); From 111950df8f21023e80249bb67c0f9cb8d0b2342f Mon Sep 17 00:00:00 2001 From: Bryant Mairs Date: Sat, 9 Dec 2017 08:56:45 -0800 Subject: [PATCH 8/8] Remove references to developer docs This hasn't been available for a while now. And was not updated for even longer before that. Let's not mislead our users any longer. --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 317567f5f3..15f0cff776 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,6 @@ [Documentation (Releases)](https://docs.rs/nix/) -[Documentation (Development)](https://nix-rust.github.io/nix/nix/index.html) - Nix seeks to provide friendly bindings to various *nix platform APIs (Linux, Darwin, ...). The goal is to not provide a 100% unified interface, but to unify what can be while still providing platform specific APIs.