Skip to content

Commit

Permalink
Deprecate IpAddr, Ipv4Addr, and Ipv6Addr
Browse files Browse the repository at this point in the history
Because they're redundant with types in the standard library.

Fixes #1681
  • Loading branch information
asomers committed Mar 22, 2022
1 parent 0fe6682 commit e7c2321
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- Deprecated `InetAddr` and `SockAddr` in favor of `SockaddrIn`, `SockaddrIn6`,
and `SockaddrStorage`.
(#[1684](https://github.com/nix-rust/nix/pull/1684))
- Deprecated `IpAddr`, `Ipv4Addr`, and `Ipv6Addr` in favor of their equivalents
from the standard library.
(#[1685](https://github.com/nix-rust/nix/pull/1685))

### Fixed

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ targets = [
libc = { version = "0.2.121", features = [ "extra_traits" ] }
bitflags = "1.1"
cfg-if = "1.0"
static_assertions = "1.1.0"

[target.'cfg(not(target_os = "redox"))'.dependencies]
memoffset = { version = "0.6.3", optional = true }
Expand Down
79 changes: 71 additions & 8 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ use crate::sys::socket::addr::sys_control::SysControlAddr;
pub use self::datalink::LinkAddr;
#[cfg(any(target_os = "android", target_os = "linux"))]
pub use self::vsock::VsockAddr;
#[cfg(feature = "net")]
use static_assertions::const_assert_eq;

/// Convert a std::net::Ipv4Addr into the libc form.
// In the standard library, this is a simple newtype, but it doesn't expose
// the inner libc type, so we must use a pointer cast.
#[cfg(feature = "net")]
pub(crate) const fn ipv4addr_to_libc(addr: net::Ipv4Addr) -> libc::in_addr {
const_assert_eq!(mem::size_of::<libc::in_addr>(),
mem::size_of::<net::Ipv4Addr>());
unsafe {
*(&addr as *const net::Ipv4Addr as *const libc::in_addr)
}
}

/// Convert a std::net::Ipv6Addr into the libc form.
// In the standard library, this is a simple newtype, but it doesn't expose
// the inner libc type, so we must use a pointer cast.
#[cfg(feature = "net")]
pub(crate) const fn ipv6addr_to_libc(addr: &net::Ipv6Addr) -> &libc::in6_addr {
const_assert_eq!(mem::size_of::<libc::in6_addr>(),
mem::size_of::<net::Ipv6Addr>());
unsafe {
&*(addr as *const net::Ipv6Addr as *const libc::in6_addr)
}
}

/// These constants specify the protocol family to be used
/// in [`socket`](fn.socket.html) and [`socketpair`](fn.socketpair.html)
Expand Down Expand Up @@ -497,14 +523,20 @@ impl fmt::Display for InetAddr {
* ===== IpAddr =====
*
*/
#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
#[allow(missing_docs)] // Since they're all deprecated anyway
#[allow(deprecated)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[deprecated(
since = "0.24.0",
note = "Use std::net::IpAddr instead"
)]
pub enum IpAddr {
V4(Ipv4Addr),
V6(Ipv6Addr),
}

#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
#[allow(deprecated)]
#[allow(missing_docs)] // Since they're all deprecated anyway
impl IpAddr {
/// Create a new IpAddr that contains an IPv4 address.
///
Expand Down Expand Up @@ -537,6 +569,7 @@ impl IpAddr {
}
}

#[allow(deprecated)]
impl fmt::Display for IpAddr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Expand All @@ -552,12 +585,17 @@ impl fmt::Display for IpAddr {
*
*/

#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
#[deprecated(
since = "0.24.0",
note = "Use std::net::Ipv4Addr instead"
)]
#[allow(missing_docs)] // Since they're all deprecated anyway
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[repr(transparent)]
pub struct Ipv4Addr(pub libc::in_addr);

#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
#[allow(deprecated)]
#[allow(missing_docs)] // Since they're all deprecated anyway
impl Ipv4Addr {
#[allow(clippy::identity_op)] // More readable this way
pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr {
Expand Down Expand Up @@ -591,6 +629,7 @@ impl Ipv4Addr {
}
}

#[allow(deprecated)]
impl fmt::Display for Ipv4Addr {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let octets = self.octets();
Expand All @@ -604,7 +643,11 @@ impl fmt::Display for Ipv4Addr {
*
*/

#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
#[deprecated(
since = "0.24.0",
note = "Use std::net::Ipv6Addr instead"
)]
#[allow(missing_docs)] // Since they're all deprecated anyway
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[repr(transparent)]
pub struct Ipv6Addr(pub libc::in6_addr);
Expand All @@ -625,7 +668,8 @@ macro_rules! to_u16_array {
}
}

#[allow(missing_docs)] // https://github.com/nix-rust/nix/issues/1681
#[allow(deprecated)]
#[allow(missing_docs)] // Since they're all deprecated anyway
impl Ipv6Addr {
#[allow(clippy::many_single_char_names)]
#[allow(clippy::too_many_arguments)]
Expand All @@ -649,6 +693,7 @@ impl Ipv6Addr {
}
}

#[allow(deprecated)]
impl fmt::Display for Ipv6Addr {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.to_std().fmt(fmt)
Expand Down Expand Up @@ -1172,7 +1217,7 @@ impl From<net::SocketAddrV4> for SockaddrIn {
sin_len: mem::size_of::<libc::sockaddr_in>() as u8,
sin_family: AddressFamily::Inet as sa_family_t,
sin_port: addr.port().to_be(), // network byte order
sin_addr: Ipv4Addr::from_std(addr.ip()).0,
sin_addr: ipv4addr_to_libc(*addr.ip()),
.. unsafe { mem::zeroed() }
})
}
Expand Down Expand Up @@ -1266,7 +1311,7 @@ impl From<net::SocketAddrV6> for SockaddrIn6 {
sin6_len: mem::size_of::<libc::sockaddr_in6>() as u8,
sin6_family: AddressFamily::Inet6 as sa_family_t,
sin6_port: addr.port().to_be(), // network byte order
sin6_addr: Ipv6Addr::from_std(addr.ip()).0,
sin6_addr: *ipv6addr_to_libc(&addr.ip()),
sin6_flowinfo: addr.flowinfo(), // host byte order
sin6_scope_id: addr.scope_id(), // host byte order
.. unsafe { mem::zeroed() }
Expand Down Expand Up @@ -2545,6 +2590,24 @@ pub mod vsock {
mod tests {
use super::*;

mod types {
use super::*;

#[test]
fn test_ipv4addr_to_libc() {
let s = std::net::Ipv4Addr::new(1, 2, 3, 4);
let l = ipv4addr_to_libc(s);
assert_eq!(l.s_addr, u32::to_be(0x01020304));
}

#[test]
fn test_ipv6addr_to_libc() {
let s = std::net::Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8);
let l = ipv6addr_to_libc(&s);
assert_eq!(l.s6_addr, [0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8]);
}
}

mod link {
#[cfg(any(target_os = "ios",
target_os = "macos",
Expand Down
19 changes: 14 additions & 5 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use libc::{self, c_void, c_int, iovec, socklen_t, size_t,
use std::convert::TryInto;
use std::{mem, ptr, slice};
use std::os::unix::io::RawFd;
#[cfg(feature = "net")]
use std::net;
#[cfg(target_os = "linux")]
#[cfg(feature = "uio")]
use crate::sys::time::TimeSpec;
Expand Down Expand Up @@ -93,6 +95,9 @@ pub use libc::{sockaddr_in, sockaddr_in6};
#[doc(hidden)]
pub use libc::{c_uint, CMSG_SPACE};

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

/// These constants are used to specify the communication semantics
/// when creating a socket with [`socket()`](fn.socket.html)
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -496,10 +501,14 @@ 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<Ipv4Addr>) -> Self {
pub fn new(group: net::Ipv4Addr, interface: Option<net::Ipv4Addr>)
-> Self
{
IpMembershipRequest(libc::ip_mreq {
imr_multiaddr: group.0,
imr_interface: interface.unwrap_or_else(Ipv4Addr::any).0,
imr_multiaddr: ipv4addr_to_libc(group),
imr_interface: ipv4addr_to_libc(
interface.unwrap_or(net::Ipv4Addr::UNSPECIFIED)
),
})
}
}
Expand All @@ -513,9 +522,9 @@ pub struct Ipv6MembershipRequest(libc::ipv6_mreq);

impl Ipv6MembershipRequest {
/// Instantiate a new `Ipv6MembershipRequest`
pub const fn new(group: Ipv6Addr) -> Self {
pub const fn new(group: net::Ipv6Addr) -> Self {
Ipv6MembershipRequest(libc::ipv6_mreq {
ipv6mr_multiaddr: group.0,
ipv6mr_multiaddr: *ipv6addr_to_libc(&group),
ipv6mr_interface: 0,
})
}
Expand Down

0 comments on commit e7c2321

Please sign in to comment.