diff options
Diffstat (limited to 'vendor/rustix/src/net')
-rw-r--r-- | vendor/rustix/src/net/addr.rs | 204 | ||||
-rw-r--r-- | vendor/rustix/src/net/ip.rs | 423 | ||||
-rw-r--r-- | vendor/rustix/src/net/send_recv.rs | 28 | ||||
-rw-r--r-- | vendor/rustix/src/net/socket.rs | 62 | ||||
-rw-r--r-- | vendor/rustix/src/net/socket_addr_any.rs | 20 | ||||
-rw-r--r-- | vendor/rustix/src/net/socketpair.rs | 6 | ||||
-rw-r--r-- | vendor/rustix/src/net/sockopt.rs | 102 | ||||
-rw-r--r-- | vendor/rustix/src/net/wsa.rs | 4 |
8 files changed, 388 insertions, 461 deletions
diff --git a/vendor/rustix/src/net/addr.rs b/vendor/rustix/src/net/addr.rs index 59f4a55d7..af9e51a8a 100644 --- a/vendor/rustix/src/net/addr.rs +++ b/vendor/rustix/src/net/addr.rs @@ -1,22 +1,17 @@ //! The following is derived from Rust's -//! library/std/src/net/addr.rs at revision -//! dca3f1b786efd27be3b325ed1e01e247aa589c3b. +//! library/std/src/net/socket_addr.rs at revision +//! f7e8ba28a4785e698a55fb95e4b3e803302de0ff. //! -//! This defines `SocketAddr`, `SocketAddrV4`, and `SocketAddrV6`. These are -//! conceptually platform-independent, however in practice OS's have differing -//! representations. +//! All code in this file is licensed MIT or Apache 2.0 at your option. +//! +//! This defines `SocketAddr`, `SocketAddrV4`, and `SocketAddrV6` in a +//! platform-independent way. It is not the native representation. #![allow(unsafe_code)] -use crate::imp::c; -use crate::imp::net::ext::{ - in6_addr_s6_addr, in_addr_s_addr, sockaddr_in6_new, sockaddr_in6_sin6_scope_id, - sockaddr_in6_sin6_scope_id_mut, -}; use crate::net::ip::{IpAddr, Ipv4Addr, Ipv6Addr}; use core::cmp::Ordering; use core::hash; -use core::mem; /// An internet socket address, either IPv4 or IPv6. /// @@ -76,12 +71,11 @@ pub enum SocketAddr { /// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1)); /// assert_eq!(socket.port(), 8080); /// ``` -#[derive(Copy)] +#[derive(Copy, Clone, Eq, PartialEq)] #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] pub struct SocketAddrV4 { - // Do not assume that this struct is implemented as the underlying system representation. - // The memory layout is not part of the stable interface that std exposes. - pub(crate) inner: c::sockaddr_in, + ip: Ipv4Addr, + port: u16, } /// An IPv6 socket address. @@ -110,12 +104,13 @@ pub struct SocketAddrV4 { /// assert_eq!(socket.ip(), &Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1)); /// assert_eq!(socket.port(), 8080); /// ``` -#[derive(Copy)] +#[derive(Copy, Clone, Eq, PartialEq)] #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] pub struct SocketAddrV6 { - // Do not assume that this struct is implemented as the underlying system representation. - // The memory layout is not part of the stable interface that std exposes. - pub(crate) inner: c::sockaddr_in6, + ip: Ipv6Addr, + port: u16, + flowinfo: u32, + scope_id: u32, } impl SocketAddr { @@ -134,34 +129,14 @@ impl SocketAddr { /// ``` #[cfg_attr(staged_api, stable(feature = "ip_addr", since = "1.7.0"))] #[must_use] - pub fn new(ip: IpAddr, port: u16) -> SocketAddr { - match ip { - IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)), - IpAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(a, port, 0, 0)), - } - } - - /// Returns the IP address associated with this socket address. - /// - /// # Examples - /// - /// ``` - /// use std::net::{IpAddr, Ipv4Addr, SocketAddr}; - /// - /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080); - /// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))); - /// ``` - #[cfg(const_raw_ptr_deref)] - #[must_use] - #[cfg_attr(staged_api, stable(feature = "ip_addr", since = "1.7.0"))] #[cfg_attr( staged_api, rustc_const_unstable(feature = "const_socketaddr", issue = "82485") )] - pub const fn ip(&self) -> IpAddr { - match *self { - SocketAddr::V4(ref a) => IpAddr::V4(*a.ip()), - SocketAddr::V6(ref a) => IpAddr::V6(*a.ip()), + pub const fn new(ip: IpAddr, port: u16) -> SocketAddr { + match ip { + IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)), + IpAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(a, port, 0, 0)), } } @@ -175,14 +150,13 @@ impl SocketAddr { /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080); /// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))); /// ``` - #[cfg(not(const_raw_ptr_deref))] #[must_use] #[cfg_attr(staged_api, stable(feature = "ip_addr", since = "1.7.0"))] #[cfg_attr( staged_api, rustc_const_unstable(feature = "const_socketaddr", issue = "82485") )] - pub fn ip(&self) -> IpAddr { + pub const fn ip(&self) -> IpAddr { match *self { SocketAddr::V4(ref a) => IpAddr::V4(*a.ip()), SocketAddr::V6(ref a) => IpAddr::V6(*a.ip()), @@ -317,38 +291,12 @@ impl SocketAddrV4 { /// ``` #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] #[must_use] - pub fn new(ip: Ipv4Addr, port: u16) -> SocketAddrV4 { - SocketAddrV4 { - inner: c::sockaddr_in { - sin_family: c::AF_INET as c::sa_family_t, - sin_port: port.to_be(), - sin_addr: ip.inner, - ..unsafe { mem::zeroed() } - }, - } - } - - /// Returns the IP address associated with this socket address. - /// - /// # Examples - /// - /// ``` - /// use std::net::{SocketAddrV4, Ipv4Addr}; - /// - /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080); - /// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1)); - /// ``` - #[cfg(const_raw_ptr_deref)] - #[must_use] - #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] #[cfg_attr( staged_api, rustc_const_unstable(feature = "const_socketaddr", issue = "82485") )] - pub const fn ip(&self) -> &Ipv4Addr { - // SAFETY: `Ipv4Addr` is `#[repr(C)] struct { _: in_addr; }`. - // It is safe to cast from `&in_addr` to `&Ipv4Addr`. - unsafe { &*(&self.inner.sin_addr as *const c::in_addr as *const Ipv4Addr) } + pub const fn new(ip: Ipv4Addr, port: u16) -> SocketAddrV4 { + SocketAddrV4 { ip, port } } /// Returns the IP address associated with this socket address. @@ -361,17 +309,14 @@ impl SocketAddrV4 { /// let socket = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 8080); /// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1)); /// ``` - #[cfg(not(const_raw_ptr_deref))] #[must_use] #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] #[cfg_attr( staged_api, rustc_const_unstable(feature = "const_socketaddr", issue = "82485") )] - pub fn ip(&self) -> &Ipv4Addr { - // SAFETY: `Ipv4Addr` is `#[repr(C)] struct { _: in_addr; }`. - // It is safe to cast from `&in_addr` to `&Ipv4Addr`. - unsafe { &*(&self.inner.sin_addr as *const c::in_addr as *const Ipv4Addr) } + pub const fn ip(&self) -> &Ipv4Addr { + &self.ip } /// Changes the IP address associated with this socket address. @@ -387,7 +332,7 @@ impl SocketAddrV4 { /// ``` #[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))] pub fn set_ip(&mut self, new_ip: Ipv4Addr) { - self.inner.sin_addr = new_ip.inner + self.ip = new_ip; } /// Returns the port number associated with this socket address. @@ -407,7 +352,7 @@ impl SocketAddrV4 { rustc_const_unstable(feature = "const_socketaddr", issue = "82485") )] pub const fn port(&self) -> u16 { - u16::from_be(self.inner.sin_port) + self.port } /// Changes the port number associated with this socket address. @@ -423,7 +368,7 @@ impl SocketAddrV4 { /// ``` #[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))] pub fn set_port(&mut self, new_port: u16) { - self.inner.sin_port = new_port.to_be(); + self.port = new_port; } } @@ -446,37 +391,17 @@ impl SocketAddrV6 { /// ``` #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] #[must_use] - pub fn new(ip: Ipv6Addr, port: u16, flowinfo: u32, scope_id: u32) -> SocketAddrV6 { - SocketAddrV6 { - inner: sockaddr_in6_new( - c::AF_INET6 as c::sa_family_t, - port.to_be(), - flowinfo, - ip.inner, - scope_id, - ), - } - } - - /// Returns the IP address associated with this socket address. - /// - /// # Examples - /// - /// ``` - /// use std::net::{SocketAddrV6, Ipv6Addr}; - /// - /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0); - /// assert_eq!(socket.ip(), &Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); - /// ``` - #[cfg(const_raw_ptr_deref)] - #[must_use] - #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] #[cfg_attr( staged_api, rustc_const_unstable(feature = "const_socketaddr", issue = "82485") )] - pub const fn ip(&self) -> &Ipv6Addr { - unsafe { &*(&self.inner.sin6_addr as *const c::in6_addr as *const Ipv6Addr) } + pub const fn new(ip: Ipv6Addr, port: u16, flowinfo: u32, scope_id: u32) -> SocketAddrV6 { + SocketAddrV6 { + ip, + port, + flowinfo, + scope_id, + } } /// Returns the IP address associated with this socket address. @@ -489,15 +414,14 @@ impl SocketAddrV6 { /// let socket = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 8080, 0, 0); /// assert_eq!(socket.ip(), &Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); /// ``` - #[cfg(not(const_raw_ptr_deref))] #[must_use] #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] #[cfg_attr( staged_api, rustc_const_unstable(feature = "const_socketaddr", issue = "82485") )] - pub fn ip(&self) -> &Ipv6Addr { - unsafe { &*(&self.inner.sin6_addr as *const c::in6_addr as *const Ipv6Addr) } + pub const fn ip(&self) -> &Ipv6Addr { + &self.ip } /// Changes the IP address associated with this socket address. @@ -513,7 +437,7 @@ impl SocketAddrV6 { /// ``` #[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))] pub fn set_ip(&mut self, new_ip: Ipv6Addr) { - self.inner.sin6_addr = new_ip.inner + self.ip = new_ip; } /// Returns the port number associated with this socket address. @@ -533,7 +457,7 @@ impl SocketAddrV6 { rustc_const_unstable(feature = "const_socketaddr", issue = "82485") )] pub const fn port(&self) -> u16 { - u16::from_be(self.inner.sin6_port) + self.port } /// Changes the port number associated with this socket address. @@ -549,7 +473,7 @@ impl SocketAddrV6 { /// ``` #[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))] pub fn set_port(&mut self, new_port: u16) { - self.inner.sin6_port = new_port.to_be(); + self.port = new_port; } /// Returns the flow information associated with this address. @@ -579,7 +503,7 @@ impl SocketAddrV6 { rustc_const_unstable(feature = "const_socketaddr", issue = "82485") )] pub const fn flowinfo(&self) -> u32 { - self.inner.sin6_flowinfo + self.flowinfo } /// Changes the flow information associated with this socket address. @@ -597,7 +521,7 @@ impl SocketAddrV6 { /// ``` #[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))] pub fn set_flowinfo(&mut self, new_flowinfo: u32) { - self.inner.sin6_flowinfo = new_flowinfo; + self.flowinfo = new_flowinfo; } /// Returns the scope ID associated with this address. @@ -622,7 +546,7 @@ impl SocketAddrV6 { rustc_const_unstable(feature = "const_socketaddr", issue = "82485") )] pub const fn scope_id(&self) -> u32 { - sockaddr_in6_sin6_scope_id(self.inner) + self.scope_id } /// Changes the scope ID associated with this socket address. @@ -640,7 +564,7 @@ impl SocketAddrV6 { /// ``` #[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))] pub fn set_scope_id(&mut self, new_scope_id: u32) { - *sockaddr_in6_sin6_scope_id_mut(&mut self.inner) = new_scope_id; + self.scope_id = new_scope_id; } } @@ -673,40 +597,6 @@ impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr { } } -#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] -impl Clone for SocketAddrV4 { - fn clone(&self) -> SocketAddrV4 { - *self - } -} -#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] -impl Clone for SocketAddrV6 { - fn clone(&self) -> SocketAddrV6 { - *self - } -} - -#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] -impl PartialEq for SocketAddrV4 { - fn eq(&self, other: &SocketAddrV4) -> bool { - self.inner.sin_port == other.inner.sin_port - && in_addr_s_addr(self.inner.sin_addr) == in_addr_s_addr(other.inner.sin_addr) - } -} -#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] -impl PartialEq for SocketAddrV6 { - fn eq(&self, other: &SocketAddrV6) -> bool { - self.inner.sin6_port == other.inner.sin6_port - && in6_addr_s6_addr(self.inner.sin6_addr) == in6_addr_s6_addr(self.inner.sin6_addr) - && self.inner.sin6_flowinfo == other.inner.sin6_flowinfo - && sockaddr_in6_sin6_scope_id(self.inner) == sockaddr_in6_sin6_scope_id(other.inner) - } -} -#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] -impl Eq for SocketAddrV4 {} -#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] -impl Eq for SocketAddrV6 {} - #[cfg_attr(staged_api, stable(feature = "socketaddr_ordering", since = "1.45.0"))] impl PartialOrd for SocketAddrV4 { fn partial_cmp(&self, other: &SocketAddrV4) -> Option<Ordering> { @@ -742,18 +632,12 @@ impl Ord for SocketAddrV6 { #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] impl hash::Hash for SocketAddrV4 { fn hash<H: hash::Hasher>(&self, s: &mut H) { - (self.inner.sin_port, in_addr_s_addr(self.inner.sin_addr)).hash(s) + (self.port, self.ip).hash(s) } } #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] impl hash::Hash for SocketAddrV6 { fn hash<H: hash::Hasher>(&self, s: &mut H) { - ( - self.inner.sin6_port, - &in6_addr_s6_addr(self.inner.sin6_addr), - self.inner.sin6_flowinfo, - sockaddr_in6_sin6_scope_id(self.inner), - ) - .hash(s) + (self.port, &self.ip, self.flowinfo, self.scope_id).hash(s) } } diff --git a/vendor/rustix/src/net/ip.rs b/vendor/rustix/src/net/ip.rs index 7c587cc7a..4d921cc10 100644 --- a/vendor/rustix/src/net/ip.rs +++ b/vendor/rustix/src/net/ip.rs @@ -1,6 +1,8 @@ //! The following is derived from Rust's -//! library/std/src/net/ip.rs at revision -//! dca3f1b786efd27be3b325ed1e01e247aa589c3b. +//! library/std/src/net/ip_addr.rs at revision +//! 14230a7f8e117aa049d3ae661fa00ded7edefc68. +//! +//! All code in this file is licensed MIT or Apache 2.0 at your option. //! //! This defines `IpAddr`, `Ipv4Addr`, and `Ipv6Addr`. Ideally, these should be //! defined in `core`. See [RFC 2832]. @@ -9,10 +11,7 @@ #![allow(unsafe_code)] -use crate::imp::c; -use crate::imp::net::ext::{in6_addr_new, in6_addr_s6_addr, in_addr_new, in_addr_s_addr}; use core::cmp::Ordering; -use core::hash; use core::mem::transmute; /// An IP address, either IPv4 or IPv6. @@ -20,9 +19,6 @@ use core::mem::transmute; /// This enum can contain either an [`Ipv4Addr`] or an [`Ipv6Addr`], see their /// respective documentation for more details. /// -/// The size of an `IpAddr` instance may vary depending on the target operating -/// system. -/// /// # Examples /// /// ``` @@ -55,9 +51,6 @@ pub enum IpAddr { /// /// See [`IpAddr`] for a type encompassing both IPv4 and IPv6 addresses. /// -/// The size of an `Ipv4Addr` struct may vary depending on the target operating -/// system. -/// /// [IETF RFC 791]: https://tools.ietf.org/html/rfc791 /// /// # Textual representation @@ -82,10 +75,10 @@ pub enum IpAddr { /// assert!("0000000.0.0.0".parse::<Ipv4Addr>().is_err()); // first octet is a zero in octal /// assert!("0xcb.0x0.0x71.0x00".parse::<Ipv4Addr>().is_err()); // all octets are in hex /// ``` -#[derive(Copy)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] pub struct Ipv4Addr { - pub(crate) inner: c::in_addr, + octets: [u8; 4], } /// An IPv6 address. @@ -93,9 +86,6 @@ pub struct Ipv4Addr { /// IPv6 addresses are defined as 128-bit integers in [IETF RFC 4291]. /// They are usually represented as eight 16-bit segments. /// -/// The size of an `Ipv6Addr` struct may vary depending on the target operating -/// system. -/// /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291 /// /// # Embedding IPv4 Addresses @@ -143,6 +133,8 @@ pub struct Ipv4Addr { /// /// To convert from an IPv4 address to an IPv4-mapped IPv6 address, use [`Ipv4Addr::to_ipv6_mapped`]. /// Use [`Ipv6Addr::to_ipv4`] to convert an IPv4-mapped IPv6 address to the canonical IPv4 address. +/// Note that this will also convert the IPv6 loopback address `::1` to `0.0.0.1`. Use +/// [`Ipv6Addr::to_ipv4_mapped`] to avoid this. /// /// [IETF RFC 4291 Section 2.5.5.2]: https://datatracker.ietf.org/doc/html/rfc4291#section-2.5.5.2 /// @@ -165,10 +157,10 @@ pub struct Ipv4Addr { /// assert_eq!("::1".parse(), Ok(localhost)); /// assert_eq!(localhost.is_loopback(), true); /// ``` -#[derive(Copy)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] pub struct Ipv6Addr { - pub(crate) inner: c::in6_addr, + octets: [u8; 16], } /// Scope of an [IPv6 multicast address] as defined in [IETF RFC 7346 section 2]. @@ -240,7 +232,10 @@ impl IpAddr { /// assert_eq!(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)).is_unspecified(), true); /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)).is_unspecified(), true); /// ``` - #[cfg_attr(staged_api, rustc_const_stable(feature = "const_ip", since = "1.50.0"))] + #[cfg_attr( + staged_api, + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") + )] #[cfg_attr(staged_api, stable(feature = "ip_shared", since = "1.12.0"))] #[must_use] #[inline] @@ -264,7 +259,10 @@ impl IpAddr { /// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).is_loopback(), true); /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1)).is_loopback(), true); /// ``` - #[cfg_attr(staged_api, rustc_const_stable(feature = "const_ip", since = "1.50.0"))] + #[cfg_attr( + staged_api, + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") + )] #[cfg_attr(staged_api, stable(feature = "ip_shared", since = "1.12.0"))] #[must_use] #[inline] @@ -317,7 +315,10 @@ impl IpAddr { /// assert_eq!(IpAddr::V4(Ipv4Addr::new(224, 254, 0, 0)).is_multicast(), true); /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0)).is_multicast(), true); /// ``` - #[cfg_attr(staged_api, rustc_const_stable(feature = "const_ip", since = "1.50.0"))] + #[cfg_attr( + staged_api, + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") + )] #[cfg_attr(staged_api, stable(feature = "ip_shared", since = "1.12.0"))] #[must_use] #[inline] @@ -398,7 +399,10 @@ impl IpAddr { /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv4(), true); /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv4(), false); /// ``` - #[cfg_attr(staged_api, rustc_const_stable(feature = "const_ip", since = "1.50.0"))] + #[cfg_attr( + staged_api, + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") + )] #[cfg_attr(staged_api, stable(feature = "ipaddr_checker", since = "1.16.0"))] #[must_use] #[inline] @@ -419,7 +423,10 @@ impl IpAddr { /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv6(), false); /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv6(), true); /// ``` - #[cfg_attr(staged_api, rustc_const_stable(feature = "const_ip", since = "1.50.0"))] + #[cfg_attr( + staged_api, + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") + )] #[cfg_attr(staged_api, stable(feature = "ipaddr_checker", since = "1.16.0"))] #[must_use] #[inline] @@ -470,16 +477,14 @@ impl Ipv4Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv4", since = "1.32.0") + rustc_const_stable(feature = "const_ip_32", since = "1.32.0") )] #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] #[must_use] #[inline] pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr { - // `s_addr` is stored as BE on all machine and the array is in BE order. - // So the native endian conversion method is used so that it's never swapped. Ipv4Addr { - inner: in_addr_new(u32::from_ne_bytes([a, b, c, d])), + octets: [a, b, c, d], } } @@ -537,14 +542,13 @@ impl Ipv4Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv4", since = "1.50.0") + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") )] #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] #[must_use] #[inline] pub const fn octets(&self) -> [u8; 4] { - // This returns the order we want because s_addr is stored in big-endian. - in_addr_s_addr(self.inner).to_ne_bytes() + self.octets } /// Returns [`true`] for the special 'unspecified' address (`0.0.0.0`). @@ -564,13 +568,13 @@ impl Ipv4Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv4", since = "1.32.0") + rustc_const_stable(feature = "const_ip_32", since = "1.32.0") )] #[cfg_attr(staged_api, stable(feature = "ip_shared", since = "1.12.0"))] #[must_use] #[inline] pub const fn is_unspecified(&self) -> bool { - in_addr_s_addr(self.inner) == 0 + u32::from_be_bytes(self.octets) == 0 } /// Returns [`true`] if this is a loopback address (`127.0.0.0/8`). @@ -589,7 +593,7 @@ impl Ipv4Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv4", since = "1.50.0") + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") )] #[cfg_attr(staged_api, stable(since = "1.7.0", feature = "ip_17"))] #[must_use] @@ -623,7 +627,7 @@ impl Ipv4Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv4", since = "1.50.0") + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") )] #[cfg_attr(staged_api, stable(since = "1.7.0", feature = "ip_17"))] #[must_use] @@ -654,7 +658,7 @@ impl Ipv4Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv4", since = "1.50.0") + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") )] #[cfg_attr(staged_api, stable(since = "1.7.0", feature = "ip_17"))] #[must_use] @@ -663,25 +667,31 @@ impl Ipv4Addr { matches!(self.octets(), [169, 254, ..]) } - /// Returns [`true`] if the address appears to be globally routable. - /// See [iana-ipv4-special-registry][ipv4-sr]. + /// Returns [`true`] if the address appears to be globally reachable + /// as specified by the [IANA IPv4 Special-Purpose Address Registry]. + /// Whether or not an address is practically reachable will depend on your network configuration. /// - /// The following return [`false`]: + /// Most IPv4 addresses are globally reachable; + /// unless they are specifically defined as *not* globally reachable. /// - /// - private addresses (see [`Ipv4Addr::is_private()`]) - /// - the loopback address (see [`Ipv4Addr::is_loopback()`]) - /// - the link-local address (see [`Ipv4Addr::is_link_local()`]) - /// - the broadcast address (see [`Ipv4Addr::is_broadcast()`]) - /// - addresses used for documentation (see [`Ipv4Addr::is_documentation()`]) - /// - the unspecified address (see [`Ipv4Addr::is_unspecified()`]), and the whole - /// `0.0.0.0/8` block - /// - addresses reserved for future protocols, except - /// `192.0.0.9/32` and `192.0.0.10/32` which are globally routable - /// - addresses reserved for future use (see [`Ipv4Addr::is_reserved()`] - /// - addresses reserved for networking devices benchmarking (see - /// [`Ipv4Addr::is_benchmarking()`]) + /// Non-exhaustive list of notable addresses that are not globally reachable: /// - /// [ipv4-sr]: https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml + /// - The [unspecified address] ([`is_unspecified`](Ipv4Addr::is_unspecified)) + /// - Addresses reserved for private use ([`is_private`](Ipv4Addr::is_private)) + /// - Addresses in the shared address space ([`is_shared`](Ipv4Addr::is_shared)) + /// - Loopback addresses ([`is_loopback`](Ipv4Addr::is_loopback)) + /// - Link-local addresses ([`is_link_local`](Ipv4Addr::is_link_local)) + /// - Addresses reserved for documentation ([`is_documentation`](Ipv4Addr::is_documentation)) + /// - Addresses reserved for benchmarking ([`is_benchmarking`](Ipv4Addr::is_benchmarking)) + /// - Reserved addresses ([`is_reserved`](Ipv4Addr::is_reserved)) + /// - The [broadcast address] ([`is_broadcast`](Ipv4Addr::is_broadcast)) + /// + /// For the complete overview of which addresses are globally reachable, see the table at the [IANA IPv4 Special-Purpose Address Registry]. + /// + /// [IANA IPv4 Special-Purpose Address Registry]: https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml + /// [unspecified address]: Ipv4Addr::UNSPECIFIED + /// [broadcast address]: Ipv4Addr::BROADCAST + /// /// # Examples /// @@ -690,46 +700,44 @@ impl Ipv4Addr { /// /// use std::net::Ipv4Addr; /// - /// // private addresses are not global + /// // Most IPv4 addresses are globally reachable: + /// assert_eq!(Ipv4Addr::new(80, 9, 12, 3).is_global(), true); + /// + /// // However some addresses have been assigned a special meaning + /// // that makes them not globally reachable. Some examples are: + /// + /// // The unspecified address (`0.0.0.0`) + /// assert_eq!(Ipv4Addr::UNSPECIFIED.is_global(), false); + /// + /// // Addresses reserved for private use (`10.0.0.0/8`, `172.16.0.0/12`, 192.168.0.0/16) /// assert_eq!(Ipv4Addr::new(10, 254, 0, 0).is_global(), false); /// assert_eq!(Ipv4Addr::new(192, 168, 10, 65).is_global(), false); /// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_global(), false); /// - /// // the 0.0.0.0/8 block is not global - /// assert_eq!(Ipv4Addr::new(0, 1, 2, 3).is_global(), false); - /// // in particular, the unspecified address is not global - /// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_global(), false); + /// // Addresses in the shared address space (`100.64.0.0/10`) + /// assert_eq!(Ipv4Addr::new(100, 100, 0, 0).is_global(), false); /// - /// // the loopback address is not global - /// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_global(), false); + /// // The loopback addresses (`127.0.0.0/8`) + /// assert_eq!(Ipv4Addr::LOCALHOST.is_global(), false); /// - /// // link local addresses are not global + /// // Link-local addresses (`169.254.0.0/16`) /// assert_eq!(Ipv4Addr::new(169, 254, 45, 1).is_global(), false); /// - /// // the broadcast address is not global - /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_global(), false); - /// - /// // the address space designated for documentation is not global + /// // Addresses reserved for documentation (`192.0.2.0/24`, `198.51.100.0/24`, `203.0.113.0/24`) /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_global(), false); /// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_global(), false); /// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_global(), false); /// - /// // shared addresses are not global - /// assert_eq!(Ipv4Addr::new(100, 100, 0, 0).is_global(), false); - /// - /// // addresses reserved for protocol assignment are not global - /// assert_eq!(Ipv4Addr::new(192, 0, 0, 0).is_global(), false); - /// assert_eq!(Ipv4Addr::new(192, 0, 0, 255).is_global(), false); + /// // Addresses reserved for benchmarking (`198.18.0.0/15`) + /// assert_eq!(Ipv4Addr::new(198, 18, 0, 0).is_global(), false); /// - /// // addresses reserved for future use are not global + /// // Reserved addresses (`240.0.0.0/4`) /// assert_eq!(Ipv4Addr::new(250, 10, 20, 30).is_global(), false); /// - /// // addresses reserved for network devices benchmarking are not global - /// assert_eq!(Ipv4Addr::new(198, 18, 0, 0).is_global(), false); + /// // The broadcast address (`255.255.255.255`) + /// assert_eq!(Ipv4Addr::BROADCAST.is_global(), false); /// - /// // All the other addresses are global - /// assert_eq!(Ipv4Addr::new(1, 1, 1, 1).is_global(), true); - /// assert_eq!(Ipv4Addr::new(80, 9, 12, 3).is_global(), true); + /// // For a complete overview see the IANA IPv4 Special-Purpose Address Registry. /// ``` #[cfg_attr( staged_api, @@ -739,25 +747,17 @@ impl Ipv4Addr { #[must_use] #[inline] pub const fn is_global(&self) -> bool { - // check if this address is 192.0.0.9 or 192.0.0.10. These addresses are the only two - // globally routable addresses in the 192.0.0.0/24 range. - if u32::from_be_bytes(self.octets()) == 0xc0000009 - || u32::from_be_bytes(self.octets()) == 0xc000000a - { - return true; - } - !self.is_private() - && !self.is_loopback() - && !self.is_link_local() - && !self.is_broadcast() - && !self.is_documentation() - && !self.is_shared() + !(self.octets()[0] == 0 // "This network" + || self.is_private() + || self.is_shared() + || self.is_loopback() + || self.is_link_local() // addresses reserved for future protocols (`192.0.0.0/24`) - && !(self.octets()[0] == 192 && self.octets()[1] == 0 && self.octets()[2] == 0) - && !self.is_reserved() - && !self.is_benchmarking() - // Make sure the address is not in 0.0.0.0/8 - && self.octets()[0] != 0 + ||(self.octets()[0] == 192 && self.octets()[1] == 0 && self.octets()[2] == 0) + || self.is_documentation() + || self.is_benchmarking() + || self.is_reserved() + || self.is_broadcast()) } /// Returns [`true`] if this address is part of the Shared Address Space defined in @@ -871,7 +871,7 @@ impl Ipv4Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv4", since = "1.50.0") + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") )] #[cfg_attr(staged_api, stable(since = "1.7.0", feature = "ip_17"))] #[must_use] @@ -896,7 +896,7 @@ impl Ipv4Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv4", since = "1.50.0") + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") )] #[cfg_attr(staged_api, stable(since = "1.7.0", feature = "ip_17"))] #[must_use] @@ -927,18 +927,16 @@ impl Ipv4Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv4", since = "1.50.0") + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") )] #[cfg_attr(staged_api, stable(since = "1.7.0", feature = "ip_17"))] #[must_use] #[inline] pub const fn is_documentation(&self) -> bool { - match self.octets() { - [192, 0, 2, _] => true, - [198, 51, 100, _] => true, - [203, 0, 113, _] => true, - _ => false, - } + matches!( + self.octets(), + [192, 0, 2, _] | [198, 51, 100, _] | [203, 0, 113, _] + ) } /// Converts this address to an [IPv4-compatible] [`IPv6` address]. @@ -963,7 +961,7 @@ impl Ipv4Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv4", since = "1.50.0") + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") )] #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] #[must_use = "this returns the result of the operation, \ @@ -972,7 +970,7 @@ impl Ipv4Addr { pub const fn to_ipv6_compatible(&self) -> Ipv6Addr { let [a, b, c, d] = self.octets(); Ipv6Addr { - inner: in6_addr_new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, a, b, c, d]), + octets: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, a, b, c, d], } } @@ -993,7 +991,7 @@ impl Ipv4Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv4", since = "1.50.0") + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") )] #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] #[must_use = "this returns the result of the operation, \ @@ -1002,7 +1000,7 @@ impl Ipv4Addr { pub const fn to_ipv6_mapped(&self) -> Ipv6Addr { let [a, b, c, d] = self.octets(); Ipv6Addr { - inner: in6_addr_new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, a, b, c, d]), + octets: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, a, b, c, d], } } } @@ -1051,22 +1049,6 @@ impl From<Ipv6Addr> for IpAddr { } } -#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] -impl Clone for Ipv4Addr { - #[inline] - fn clone(&self) -> Ipv4Addr { - *self - } -} - -#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] -impl PartialEq for Ipv4Addr { - #[inline] - fn eq(&self, other: &Ipv4Addr) -> bool { - in_addr_s_addr(self.inner) == in_addr_s_addr(other.inner) - } -} - #[cfg_attr(staged_api, stable(feature = "ip_cmp", since = "1.16.0"))] impl PartialEq<Ipv4Addr> for IpAddr { #[inline] @@ -1090,21 +1072,6 @@ impl PartialEq<IpAddr> for Ipv4Addr { } #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] -impl Eq for Ipv4Addr {} - -#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] -impl hash::Hash for Ipv4Addr { - #[inline] - fn hash<H: hash::Hasher>(&self, s: &mut H) { - // NOTE: - // * hash in big endian order - // * in netbsd, `in_addr` has `repr(packed)`, we need to - // copy `s_addr` to avoid unsafe borrowing - { in_addr_s_addr(self.inner) }.hash(s) - } -} - -#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] impl PartialOrd for Ipv4Addr { #[inline] fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> { @@ -1138,8 +1105,7 @@ impl PartialOrd<IpAddr> for Ipv4Addr { impl Ord for Ipv4Addr { #[inline] fn cmp(&self, other: &Ipv4Addr) -> Ordering { - // Compare as native endian - u32::from_be(in_addr_s_addr(self.inner)).cmp(&u32::from_be(in_addr_s_addr(other.inner))) + self.octets.cmp(&other.octets) } } @@ -1152,13 +1118,12 @@ impl From<Ipv4Addr> for u32 { /// ``` /// use std::net::Ipv4Addr; /// - /// let addr = Ipv4Addr::new(0xca, 0xfe, 0xba, 0xbe); - /// assert_eq!(0xcafebabe, u32::from(addr)); + /// let addr = Ipv4Addr::new(0x12, 0x34, 0x56, 0x78); + /// assert_eq!(0x12345678, u32::from(addr)); /// ``` #[inline] fn from(ip: Ipv4Addr) -> u32 { - let ip = ip.octets(); - u32::from_be_bytes(ip) + u32::from_be_bytes(ip.octets) } } @@ -1171,12 +1136,14 @@ impl From<u32> for Ipv4Addr { /// ``` /// use std::net::Ipv4Addr; /// - /// let addr = Ipv4Addr::from(0xcafebabe); - /// assert_eq!(Ipv4Addr::new(0xca, 0xfe, 0xba, 0xbe), addr); + /// let addr = Ipv4Addr::from(0x12345678); + /// assert_eq!(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78), addr); /// ``` #[inline] fn from(ip: u32) -> Ipv4Addr { - Ipv4Addr::from(ip.to_be_bytes()) + Ipv4Addr { + octets: ip.to_be_bytes(), + } } } @@ -1194,7 +1161,7 @@ impl From<[u8; 4]> for Ipv4Addr { /// ``` #[inline] fn from(octets: [u8; 4]) -> Ipv4Addr { - Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]) + Ipv4Addr { octets } } } @@ -1230,7 +1197,7 @@ impl Ipv6Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv6", since = "1.32.0") + rustc_const_stable(feature = "const_ip_32", since = "1.32.0") )] #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] #[must_use] @@ -1249,9 +1216,7 @@ impl Ipv6Addr { Ipv6Addr { // All elements in `addr16` are big endian. // SAFETY: `[u16; 8]` is always safe to transmute to `[u8; 16]`. - // rustc_allow_const_fn_unstable: the transmute could be written as stable const - // code, but that leads to worse code generation (#75085) - inner: in6_addr_new(unsafe { transmute::<_, [u8; 16]>(addr16) }), + octets: unsafe { transmute::<_, [u8; 16]>(addr16) }, } } @@ -1293,18 +1258,15 @@ impl Ipv6Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv6", since = "1.50.0") + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") )] #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] #[must_use] #[inline] pub const fn segments(&self) -> [u16; 8] { - // All elements in `s6_addr` must be big endian. + // All elements in `self.octets` must be big endian. // SAFETY: `[u8; 16]` is always safe to transmute to `[u16; 8]`. - // rustc_allow_const_fn_unstable: the transmute could be written as stable const code, but - // that leads to worse code generation (#75085) - let [a, b, c, d, e, f, g, h] = - unsafe { transmute::<_, [u16; 8]>(in6_addr_s6_addr(self.inner)) }; + let [a, b, c, d, e, f, g, h] = unsafe { transmute::<_, [u16; 8]>(self.octets) }; // We want native endian u16 [ u16::from_be(a), @@ -1334,7 +1296,7 @@ impl Ipv6Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv6", since = "1.50.0") + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") )] #[cfg_attr(staged_api, stable(since = "1.7.0", feature = "ip_17"))] #[must_use] @@ -1361,7 +1323,7 @@ impl Ipv6Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv6", since = "1.50.0") + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") )] #[cfg_attr(staged_api, stable(since = "1.7.0", feature = "ip_17"))] #[must_use] @@ -1370,13 +1332,33 @@ impl Ipv6Addr { u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::LOCALHOST.octets()) } - /// Returns [`true`] if the address appears to be globally routable. + /// Returns [`true`] if the address appears to be globally reachable + /// as specified by the [IANA IPv6 Special-Purpose Address Registry]. + /// Whether or not an address is practically reachable will depend on your network configuration. /// - /// The following return [`false`]: + /// Most IPv6 addresses are globally reachable; + /// unless they are specifically defined as *not* globally reachable. /// - /// - the loopback address - /// - link-local and unique local unicast addresses - /// - interface-, link-, realm-, admin- and site-local multicast addresses + /// Non-exhaustive list of notable addresses that are not globally reachable: + /// - The [unspecified address] ([`is_unspecified`](Ipv6Addr::is_unspecified)) + /// - The [loopback address] ([`is_loopback`](Ipv6Addr::is_loopback)) + /// - IPv4-mapped addresses + /// - Addresses reserved for benchmarking + /// - Addresses reserved for documentation ([`is_documentation`](Ipv6Addr::is_documentation)) + /// - Unique local addresses ([`is_unique_local`](Ipv6Addr::is_unique_local)) + /// - Unicast addresses with link-local scope ([`is_unicast_link_local`](Ipv6Addr::is_unicast_link_local)) + /// + /// For the complete overview of which addresses are globally reachable, see the table at the [IANA IPv6 Special-Purpose Address Registry]. + /// + /// Note that an address having global scope is not the same as being globally reachable, + /// and there is no direct relation between the two concepts: There exist addresses with global scope + /// that are not globally reachable (for example unique local addresses), + /// and addresses that are globally reachable without having global scope + /// (multicast addresses with non-global scope). + /// + /// [IANA IPv6 Special-Purpose Address Registry]: https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml + /// [unspecified address]: Ipv6Addr::UNSPECIFIED + /// [loopback address]: Ipv6Addr::LOCALHOST /// /// # Examples /// @@ -1385,9 +1367,34 @@ impl Ipv6Addr { /// /// use std::net::Ipv6Addr; /// - /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_global(), true); - /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_global(), false); - /// assert_eq!(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1).is_global(), true); + /// // Most IPv6 addresses are globally reachable: + /// assert_eq!(Ipv6Addr::new(0x26, 0, 0x1c9, 0, 0, 0xafc8, 0x10, 0x1).is_global(), true); + /// + /// // However some addresses have been assigned a special meaning + /// // that makes them not globally reachable. Some examples are: + /// + /// // The unspecified address (`::`) + /// assert_eq!(Ipv6Addr::UNSPECIFIED.is_global(), false); + /// + /// // The loopback address (`::1`) + /// assert_eq!(Ipv6Addr::LOCALHOST.is_global(), false); + /// + /// // IPv4-mapped addresses (`::ffff:0:0/96`) + /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_global(), false); + /// + /// // Addresses reserved for benchmarking (`2001:2::/48`) + /// assert_eq!(Ipv6Addr::new(0x2001, 2, 0, 0, 0, 0, 0, 1,).is_global(), false); + /// + /// // Addresses reserved for documentation (`2001:db8::/32`) + /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1).is_global(), false); + /// + /// // Unique local addresses (`fc00::/7`) + /// assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 1).is_global(), false); + /// + /// // Unicast addresses with link-local scope (`fe80::/10`) + /// assert_eq!(Ipv6Addr::new(0xfe81, 0, 0, 0, 0, 0, 0, 1).is_global(), false); + /// + /// // For a complete overview see the IANA IPv6 Special-Purpose Address Registry. /// ``` #[cfg_attr( staged_api, @@ -1397,11 +1404,31 @@ impl Ipv6Addr { #[must_use] #[inline] pub const fn is_global(&self) -> bool { - match self.multicast_scope() { - Some(Ipv6MulticastScope::Global) => true, - None => self.is_unicast_global(), - _ => false, - } + !(self.is_unspecified() + || self.is_loopback() + // IPv4-mapped Address (`::ffff:0:0/96`) + || matches!(self.segments(), [0, 0, 0, 0, 0, 0xffff, _, _]) + // IPv4-IPv6 Translat. (`64:ff9b:1::/48`) + || matches!(self.segments(), [0x64, 0xff9b, 1, _, _, _, _, _]) + // Discard-Only Address Block (`100::/64`) + || matches!(self.segments(), [0x100, 0, 0, 0, _, _, _, _]) + // IETF Protocol Assignments (`2001::/23`) + || (matches!(self.segments(), [0x2001, b, _, _, _, _, _, _] if b < 0x200) + && !( + // Port Control Protocol Anycast (`2001:1::1`) + u128::from_be_bytes(self.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0001 + // Traversal Using Relays around NAT Anycast (`2001:1::2`) + || u128::from_be_bytes(self.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0002 + // AMT (`2001:3::/32`) + || matches!(self.segments(), [0x2001, 3, _, _, _, _, _, _]) + // AS112-v6 (`2001:4:112::/48`) + || matches!(self.segments(), [0x2001, 4, 0x112, _, _, _, _, _]) + // ORCHIDv2 (`2001:20::/28`) + || matches!(self.segments(), [0x2001, b, _, _, _, _, _, _] if b >= 0x20 && b <= 0x2F) + )) + || self.is_documentation() + || self.is_unique_local() + || self.is_unicast_link_local()) } /// Returns [`true`] if this is a unique local address (`fc00::/7`). @@ -1613,6 +1640,7 @@ impl Ipv6Addr { && !self.is_unique_local() && !self.is_unspecified() && !self.is_documentation() + && !self.is_benchmarking() } /// Returns the address's multicast scope if the address is multicast. @@ -1670,7 +1698,7 @@ impl Ipv6Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv6", since = "1.50.0") + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") )] #[cfg_attr(staged_api, stable(since = "1.7.0", feature = "ip_17"))] #[must_use] @@ -1692,8 +1720,6 @@ impl Ipv6Addr { /// # Examples /// /// ``` - /// #![feature(ip)] - /// /// use std::net::{Ipv4Addr, Ipv6Addr}; /// /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4_mapped(), None); @@ -1705,7 +1731,7 @@ impl Ipv6Addr { staged_api, rustc_const_unstable(feature = "const_ipv6", issue = "76205") )] - #[cfg_attr(staged_api, unstable(feature = "ip", issue = "27709"))] + #[cfg_attr(staged_api, stable(feature = "ipv6_to_ipv4_mapped", since = "1.63.0"))] #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] @@ -1723,7 +1749,10 @@ impl Ipv6Addr { /// or an [IPv4-mapped] address as defined in [IETF RFC 4291 section 2.5.5.2], /// otherwise returns [`None`]. /// - /// `::a.b.c.d` and `::ffff:a.b.c.d` become `a.b.c.d` + /// Note that this will return an [`IPv4` address] for the IPv6 loopback address `::1`. Use + /// [`Ipv6Addr::to_ipv4_mapped`] to avoid this. + /// + /// `::a.b.c.d` and `::ffff:a.b.c.d` become `a.b.c.d`. `::1` becomes `0.0.0.1`. /// All addresses *not* starting with either all zeroes or `::ffff` will return `None`. /// /// [`IPv4` address]: Ipv4Addr @@ -1745,7 +1774,7 @@ impl Ipv6Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv6", since = "1.50.0") + rustc_const_stable(feature = "const_ip_50", since = "1.50.0") )] #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] #[must_use = "this returns the result of the operation, \ @@ -1798,29 +1827,13 @@ impl Ipv6Addr { /// ``` #[cfg_attr( staged_api, - rustc_const_stable(feature = "const_ipv6", since = "1.32.0") + rustc_const_stable(feature = "const_ip_32", since = "1.32.0") )] #[cfg_attr(staged_api, stable(feature = "ipv6_to_octets", since = "1.12.0"))] #[must_use] #[inline] pub const fn octets(&self) -> [u8; 16] { - in6_addr_s6_addr(self.inner) - } -} - -#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] -impl Clone for Ipv6Addr { - #[inline] - fn clone(&self) -> Ipv6Addr { - *self - } -} - -#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] -impl PartialEq for Ipv6Addr { - #[inline] - fn eq(&self, other: &Ipv6Addr) -> bool { - in6_addr_s6_addr(self.inner) == in6_addr_s6_addr(other.inner) + self.octets } } @@ -1847,17 +1860,6 @@ impl PartialEq<Ipv6Addr> for IpAddr { } #[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] -impl Eq for Ipv6Addr {} - -#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] -impl hash::Hash for Ipv6Addr { - #[inline] - fn hash<H: hash::Hasher>(&self, s: &mut H) { - in6_addr_s6_addr(self.inner).hash(s) - } -} - -#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] impl PartialOrd for Ipv6Addr { #[inline] fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> { @@ -1912,8 +1914,7 @@ impl From<Ipv6Addr> for u128 { /// ``` #[inline] fn from(ip: Ipv6Addr) -> u128 { - let ip = ip.octets(); - u128::from_be_bytes(ip) + u128::from_be_bytes(ip.octets) } } #[cfg_attr(staged_api, stable(feature = "i128", since = "1.26.0"))] @@ -1964,9 +1965,7 @@ impl From<[u8; 16]> for Ipv6Addr { /// ``` #[inline] fn from(octets: [u8; 16]) -> Ipv6Addr { - Ipv6Addr { - inner: in6_addr_new(octets), - } + Ipv6Addr { octets } } } diff --git a/vendor/rustix/src/net/send_recv.rs b/vendor/rustix/src/net/send_recv.rs index 83ccc79d4..041077266 100644 --- a/vendor/rustix/src/net/send_recv.rs +++ b/vendor/rustix/src/net/send_recv.rs @@ -3,10 +3,10 @@ #[cfg(unix)] use crate::net::SocketAddrUnix; use crate::net::{SocketAddr, SocketAddrAny, SocketAddrV4, SocketAddrV6}; -use crate::{imp, io}; -use imp::fd::{AsFd, BorrowedFd}; +use crate::{backend, io}; +use backend::fd::{AsFd, BorrowedFd}; -pub use imp::net::send_recv::{RecvFlags, SendFlags}; +pub use backend::net::send_recv::{RecvFlags, SendFlags}; /// `recv(fd, buf, flags)`—Reads data from a socket. /// @@ -22,7 +22,7 @@ pub use imp::net::send_recv::{RecvFlags, SendFlags}; /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-recv #[inline] pub fn recv<Fd: AsFd>(fd: Fd, buf: &mut [u8], flags: RecvFlags) -> io::Result<usize> { - imp::net::syscalls::recv(fd.as_fd(), buf, flags) + backend::net::syscalls::recv(fd.as_fd(), buf, flags) } /// `send(fd, buf, flags)`—Writes data to a socket. @@ -39,7 +39,7 @@ pub fn recv<Fd: AsFd>(fd: Fd, buf: &mut [u8], flags: RecvFlags) -> io::Result<us /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-send #[inline] pub fn send<Fd: AsFd>(fd: Fd, buf: &[u8], flags: SendFlags) -> io::Result<usize> { - imp::net::syscalls::send(fd.as_fd(), buf, flags) + backend::net::syscalls::send(fd.as_fd(), buf, flags) } /// `recvfrom(fd, buf, flags, addr, len)`—Reads data from a socket and @@ -61,7 +61,7 @@ pub fn recvfrom<Fd: AsFd>( buf: &mut [u8], flags: RecvFlags, ) -> io::Result<(usize, Option<SocketAddrAny>)> { - imp::net::syscalls::recvfrom(fd.as_fd(), buf, flags) + backend::net::syscalls::recvfrom(fd.as_fd(), buf, flags) } /// `sendto(fd, buf, flags, addr)`—Writes data to a socket to a specific IP @@ -93,8 +93,8 @@ fn _sendto( addr: &SocketAddr, ) -> io::Result<usize> { match addr { - SocketAddr::V4(v4) => imp::net::syscalls::sendto_v4(fd, buf, flags, v4), - SocketAddr::V6(v6) => imp::net::syscalls::sendto_v6(fd, buf, flags, v6), + SocketAddr::V4(v4) => backend::net::syscalls::sendto_v4(fd, buf, flags, v4), + SocketAddr::V6(v6) => backend::net::syscalls::sendto_v6(fd, buf, flags, v6), } } @@ -127,10 +127,10 @@ fn _sendto_any( addr: &SocketAddrAny, ) -> io::Result<usize> { match addr { - SocketAddrAny::V4(v4) => imp::net::syscalls::sendto_v4(fd, buf, flags, v4), - SocketAddrAny::V6(v6) => imp::net::syscalls::sendto_v6(fd, buf, flags, v6), + SocketAddrAny::V4(v4) => backend::net::syscalls::sendto_v4(fd, buf, flags, v4), + SocketAddrAny::V6(v6) => backend::net::syscalls::sendto_v6(fd, buf, flags, v6), #[cfg(unix)] - SocketAddrAny::Unix(unix) => imp::net::syscalls::sendto_unix(fd, buf, flags, unix), + SocketAddrAny::Unix(unix) => backend::net::syscalls::sendto_unix(fd, buf, flags, unix), } } @@ -155,7 +155,7 @@ pub fn sendto_v4<Fd: AsFd>( flags: SendFlags, addr: &SocketAddrV4, ) -> io::Result<usize> { - imp::net::syscalls::sendto_v4(fd.as_fd(), buf, flags, addr) + backend::net::syscalls::sendto_v4(fd.as_fd(), buf, flags, addr) } /// `sendto(fd, buf, flags, addr, sizeof(struct sockaddr_in6))`—Writes data @@ -179,7 +179,7 @@ pub fn sendto_v6<Fd: AsFd>( flags: SendFlags, addr: &SocketAddrV6, ) -> io::Result<usize> { - imp::net::syscalls::sendto_v6(fd.as_fd(), buf, flags, addr) + backend::net::syscalls::sendto_v6(fd.as_fd(), buf, flags, addr) } /// `sendto(fd, buf, flags, addr, sizeof(struct sockaddr_un))`—Writes data to @@ -204,7 +204,7 @@ pub fn sendto_unix<Fd: AsFd>( flags: SendFlags, addr: &SocketAddrUnix, ) -> io::Result<usize> { - imp::net::syscalls::sendto_unix(fd.as_fd(), buf, flags, addr) + backend::net::syscalls::sendto_unix(fd.as_fd(), buf, flags, addr) } // TODO: `recvmsg`, `sendmsg` diff --git a/vendor/rustix/src/net/socket.rs b/vendor/rustix/src/net/socket.rs index cd8f149f6..687022afe 100644 --- a/vendor/rustix/src/net/socket.rs +++ b/vendor/rustix/src/net/socket.rs @@ -1,11 +1,11 @@ -use crate::imp; -use crate::io::{self, OwnedFd}; +use crate::fd::OwnedFd; use crate::net::{SocketAddr, SocketAddrAny, SocketAddrV4, SocketAddrV6}; -use imp::fd::{AsFd, BorrowedFd}; +use crate::{backend, io}; +use backend::fd::{AsFd, BorrowedFd}; #[cfg(unix)] -pub use imp::net::addr::SocketAddrUnix; -pub use imp::net::types::{ +pub use backend::net::addr::SocketAddrUnix; +pub use backend::net::types::{ AcceptFlags, AddressFamily, Protocol, Shutdown, SocketFlags, SocketType, }; @@ -34,7 +34,7 @@ impl Default for Protocol { /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-socket #[inline] pub fn socket(domain: AddressFamily, type_: SocketType, protocol: Protocol) -> io::Result<OwnedFd> { - imp::net::syscalls::socket(domain, type_, protocol) + backend::net::syscalls::socket(domain, type_, protocol) } /// `socket_with(domain, type_ | flags, protocol)`—Creates a socket, with @@ -64,7 +64,7 @@ pub fn socket_with( flags: SocketFlags, protocol: Protocol, ) -> io::Result<OwnedFd> { - imp::net::syscalls::socket_with(domain, type_, flags, protocol) + backend::net::syscalls::socket_with(domain, type_, flags, protocol) } /// `bind(sockfd, addr)`—Binds a socket to an IP address. @@ -85,8 +85,8 @@ pub fn bind<Fd: AsFd>(sockfd: Fd, addr: &SocketAddr) -> io::Result<()> { fn _bind(sockfd: BorrowedFd<'_>, addr: &SocketAddr) -> io::Result<()> { match addr { - SocketAddr::V4(v4) => imp::net::syscalls::bind_v4(sockfd, v4), - SocketAddr::V6(v6) => imp::net::syscalls::bind_v6(sockfd, v6), + SocketAddr::V4(v4) => backend::net::syscalls::bind_v4(sockfd, v4), + SocketAddr::V6(v6) => backend::net::syscalls::bind_v6(sockfd, v6), } } @@ -109,10 +109,10 @@ pub fn bind_any<Fd: AsFd>(sockfd: Fd, addr: &SocketAddrAny) -> io::Result<()> { fn _bind_any(sockfd: BorrowedFd<'_>, addr: &SocketAddrAny) -> io::Result<()> { match addr { - SocketAddrAny::V4(v4) => imp::net::syscalls::bind_v4(sockfd, v4), - SocketAddrAny::V6(v6) => imp::net::syscalls::bind_v6(sockfd, v6), + SocketAddrAny::V4(v4) => backend::net::syscalls::bind_v4(sockfd, v4), + SocketAddrAny::V6(v6) => backend::net::syscalls::bind_v6(sockfd, v6), #[cfg(unix)] - SocketAddrAny::Unix(unix) => imp::net::syscalls::bind_unix(sockfd, unix), + SocketAddrAny::Unix(unix) => backend::net::syscalls::bind_unix(sockfd, unix), } } @@ -132,7 +132,7 @@ fn _bind_any(sockfd: BorrowedFd<'_>, addr: &SocketAddrAny) -> io::Result<()> { #[inline] #[doc(alias = "bind")] pub fn bind_v4<Fd: AsFd>(sockfd: Fd, addr: &SocketAddrV4) -> io::Result<()> { - imp::net::syscalls::bind_v4(sockfd.as_fd(), addr) + backend::net::syscalls::bind_v4(sockfd.as_fd(), addr) } /// `bind(sockfd, addr, sizeof(struct sockaddr_in6))`—Binds a socket to an @@ -151,7 +151,7 @@ pub fn bind_v4<Fd: AsFd>(sockfd: Fd, addr: &SocketAddrV4) -> io::Result<()> { #[inline] #[doc(alias = "bind")] pub fn bind_v6<Fd: AsFd>(sockfd: Fd, addr: &SocketAddrV6) -> io::Result<()> { - imp::net::syscalls::bind_v6(sockfd.as_fd(), addr) + backend::net::syscalls::bind_v6(sockfd.as_fd(), addr) } /// `bind(sockfd, addr, sizeof(struct sockaddr_un))`—Binds a socket to a @@ -171,7 +171,7 @@ pub fn bind_v6<Fd: AsFd>(sockfd: Fd, addr: &SocketAddrV6) -> io::Result<()> { #[inline] #[doc(alias = "bind")] pub fn bind_unix<Fd: AsFd>(sockfd: Fd, addr: &SocketAddrUnix) -> io::Result<()> { - imp::net::syscalls::bind_unix(sockfd.as_fd(), addr) + backend::net::syscalls::bind_unix(sockfd.as_fd(), addr) } /// `connect(sockfd, addr)`—Initiates a connection to an IP address. @@ -192,8 +192,8 @@ pub fn connect<Fd: AsFd>(sockfd: Fd, addr: &SocketAddr) -> io::Result<()> { fn _connect(sockfd: BorrowedFd<'_>, addr: &SocketAddr) -> io::Result<()> { match addr { - SocketAddr::V4(v4) => imp::net::syscalls::connect_v4(sockfd, v4), - SocketAddr::V6(v6) => imp::net::syscalls::connect_v6(sockfd, v6), + SocketAddr::V4(v4) => backend::net::syscalls::connect_v4(sockfd, v4), + SocketAddr::V6(v6) => backend::net::syscalls::connect_v6(sockfd, v6), } } @@ -216,10 +216,10 @@ pub fn connect_any<Fd: AsFd>(sockfd: Fd, addr: &SocketAddrAny) -> io::Result<()> fn _connect_any(sockfd: BorrowedFd<'_>, addr: &SocketAddrAny) -> io::Result<()> { match addr { - SocketAddrAny::V4(v4) => imp::net::syscalls::connect_v4(sockfd, v4), - SocketAddrAny::V6(v6) => imp::net::syscalls::connect_v6(sockfd, v6), + SocketAddrAny::V4(v4) => backend::net::syscalls::connect_v4(sockfd, v4), + SocketAddrAny::V6(v6) => backend::net::syscalls::connect_v6(sockfd, v6), #[cfg(unix)] - SocketAddrAny::Unix(unix) => imp::net::syscalls::connect_unix(sockfd, unix), + SocketAddrAny::Unix(unix) => backend::net::syscalls::connect_unix(sockfd, unix), } } @@ -239,7 +239,7 @@ fn _connect_any(sockfd: BorrowedFd<'_>, addr: &SocketAddrAny) -> io::Result<()> #[inline] #[doc(alias = "connect")] pub fn connect_v4<Fd: AsFd>(sockfd: Fd, addr: &SocketAddrV4) -> io::Result<()> { - imp::net::syscalls::connect_v4(sockfd.as_fd(), addr) + backend::net::syscalls::connect_v4(sockfd.as_fd(), addr) } /// `connect(sockfd, addr, sizeof(struct sockaddr_in6))`—Initiates a @@ -258,7 +258,7 @@ pub fn connect_v4<Fd: AsFd>(sockfd: Fd, addr: &SocketAddrV4) -> io::Result<()> { #[inline] #[doc(alias = "connect")] pub fn connect_v6<Fd: AsFd>(sockfd: Fd, addr: &SocketAddrV6) -> io::Result<()> { - imp::net::syscalls::connect_v6(sockfd.as_fd(), addr) + backend::net::syscalls::connect_v6(sockfd.as_fd(), addr) } /// `connect(sockfd, addr, sizeof(struct sockaddr_un))`—Initiates a @@ -278,7 +278,7 @@ pub fn connect_v6<Fd: AsFd>(sockfd: Fd, addr: &SocketAddrV6) -> io::Result<()> { #[inline] #[doc(alias = "connect")] pub fn connect_unix<Fd: AsFd>(sockfd: Fd, addr: &SocketAddrUnix) -> io::Result<()> { - imp::net::syscalls::connect_unix(sockfd.as_fd(), addr) + backend::net::syscalls::connect_unix(sockfd.as_fd(), addr) } /// `listen(fd, backlog)`—Enables listening for incoming connections. @@ -295,7 +295,7 @@ pub fn connect_unix<Fd: AsFd>(sockfd: Fd, addr: &SocketAddrUnix) -> io::Result<( /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-listen #[inline] pub fn listen<Fd: AsFd>(sockfd: Fd, backlog: i32) -> io::Result<()> { - imp::net::syscalls::listen(sockfd.as_fd(), backlog) + backend::net::syscalls::listen(sockfd.as_fd(), backlog) } /// `accept(fd, NULL, NULL)`—Accepts an incoming connection. @@ -319,7 +319,7 @@ pub fn listen<Fd: AsFd>(sockfd: Fd, backlog: i32) -> io::Result<()> { #[inline] #[doc(alias = "accept4")] pub fn accept<Fd: AsFd>(sockfd: Fd) -> io::Result<OwnedFd> { - imp::net::syscalls::accept(sockfd.as_fd()) + backend::net::syscalls::accept(sockfd.as_fd()) } /// `accept4(fd, NULL, NULL, flags)`—Accepts an incoming connection, with @@ -341,7 +341,7 @@ pub fn accept<Fd: AsFd>(sockfd: Fd) -> io::Result<OwnedFd> { #[inline] #[doc(alias = "accept4")] pub fn accept_with<Fd: AsFd>(sockfd: Fd, flags: AcceptFlags) -> io::Result<OwnedFd> { - imp::net::syscalls::accept_with(sockfd.as_fd(), flags) + backend::net::syscalls::accept_with(sockfd.as_fd(), flags) } /// `accept(fd, &addr, &len)`—Accepts an incoming connection and returns the @@ -362,7 +362,7 @@ pub fn accept_with<Fd: AsFd>(sockfd: Fd, flags: AcceptFlags) -> io::Result<Owned #[inline] #[doc(alias = "accept4")] pub fn acceptfrom<Fd: AsFd>(sockfd: Fd) -> io::Result<(OwnedFd, Option<SocketAddrAny>)> { - imp::net::syscalls::acceptfrom(sockfd.as_fd()) + backend::net::syscalls::acceptfrom(sockfd.as_fd()) } /// `accept4(fd, &addr, &len, flags)`—Accepts an incoming connection and @@ -383,7 +383,7 @@ pub fn acceptfrom_with<Fd: AsFd>( sockfd: Fd, flags: AcceptFlags, ) -> io::Result<(OwnedFd, Option<SocketAddrAny>)> { - imp::net::syscalls::acceptfrom_with(sockfd.as_fd(), flags) + backend::net::syscalls::acceptfrom_with(sockfd.as_fd(), flags) } /// `shutdown(fd, how)`—Closes the read and/or write sides of a stream. @@ -400,7 +400,7 @@ pub fn acceptfrom_with<Fd: AsFd>( /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-shutdown #[inline] pub fn shutdown<Fd: AsFd>(sockfd: Fd, how: Shutdown) -> io::Result<()> { - imp::net::syscalls::shutdown(sockfd.as_fd(), how) + backend::net::syscalls::shutdown(sockfd.as_fd(), how) } /// `getsockname(fd, addr, len)`—Returns the address a socket is bound to. @@ -417,7 +417,7 @@ pub fn shutdown<Fd: AsFd>(sockfd: Fd, how: Shutdown) -> io::Result<()> { /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-getsockname #[inline] pub fn getsockname<Fd: AsFd>(sockfd: Fd) -> io::Result<SocketAddrAny> { - imp::net::syscalls::getsockname(sockfd.as_fd()) + backend::net::syscalls::getsockname(sockfd.as_fd()) } /// `getpeername(fd, addr, len)`—Returns the address a socket is connected @@ -435,5 +435,5 @@ pub fn getsockname<Fd: AsFd>(sockfd: Fd) -> io::Result<SocketAddrAny> { /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-getpeername #[inline] pub fn getpeername<Fd: AsFd>(sockfd: Fd) -> io::Result<Option<SocketAddrAny>> { - imp::net::syscalls::getpeername(sockfd.as_fd()) + backend::net::syscalls::getpeername(sockfd.as_fd()) } diff --git a/vendor/rustix/src/net/socket_addr_any.rs b/vendor/rustix/src/net/socket_addr_any.rs index 4970dbf74..287c6c1ee 100644 --- a/vendor/rustix/src/net/socket_addr_any.rs +++ b/vendor/rustix/src/net/socket_addr_any.rs @@ -12,11 +12,11 @@ #[cfg(unix)] use crate::net::SocketAddrUnix; use crate::net::{AddressFamily, SocketAddrV4, SocketAddrV6}; -use crate::{imp, io}; +use crate::{backend, io}; #[cfg(feature = "std")] use core::fmt; -pub use imp::net::addr::SocketAddrStorage; +pub use backend::net::addr::SocketAddrStorage; /// `struct sockaddr_storage` as a Rust enum. #[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] @@ -37,10 +37,10 @@ impl SocketAddrAny { #[inline] pub const fn address_family(&self) -> AddressFamily { match self { - SocketAddrAny::V4(_) => AddressFamily::INET, - SocketAddrAny::V6(_) => AddressFamily::INET6, + Self::V4(_) => AddressFamily::INET, + Self::V6(_) => AddressFamily::INET6, #[cfg(unix)] - SocketAddrAny::Unix(_) => AddressFamily::UNIX, + Self::Unix(_) => AddressFamily::UNIX, } } @@ -53,7 +53,7 @@ impl SocketAddrAny { /// `storage` must point to valid memory for encoding the socket /// address. pub unsafe fn write(&self, storage: *mut SocketAddrStorage) -> usize { - imp::net::write_sockaddr::write_sockaddr(self, storage) + backend::net::write_sockaddr::write_sockaddr(self, storage) } /// Reads a platform-specific encoding of a socket address from @@ -64,7 +64,7 @@ impl SocketAddrAny { /// `storage` must point to valid memory for decoding a socket /// address. pub unsafe fn read(storage: *const SocketAddrStorage, len: usize) -> io::Result<Self> { - imp::net::read_sockaddr::read_sockaddr(storage, len) + backend::net::read_sockaddr::read_sockaddr(storage, len) } } @@ -72,10 +72,10 @@ impl SocketAddrAny { impl fmt::Debug for SocketAddrAny { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - SocketAddrAny::V4(v4) => v4.fmt(fmt), - SocketAddrAny::V6(v6) => v6.fmt(fmt), + Self::V4(v4) => v4.fmt(fmt), + Self::V6(v6) => v6.fmt(fmt), #[cfg(unix)] - SocketAddrAny::Unix(unix) => unix.fmt(fmt), + Self::Unix(unix) => unix.fmt(fmt), } } } diff --git a/vendor/rustix/src/net/socketpair.rs b/vendor/rustix/src/net/socketpair.rs index 68a58f3e9..4a9568ce7 100644 --- a/vendor/rustix/src/net/socketpair.rs +++ b/vendor/rustix/src/net/socketpair.rs @@ -1,6 +1,6 @@ -use crate::imp; -use crate::io::{self, OwnedFd}; +use crate::fd::OwnedFd; use crate::net::{AddressFamily, Protocol, SocketFlags, SocketType}; +use crate::{backend, io}; /// `socketpair(domain, type_ | accept_flags, protocol)` /// @@ -17,5 +17,5 @@ pub fn socketpair( flags: SocketFlags, protocol: Protocol, ) -> io::Result<(OwnedFd, OwnedFd)> { - imp::net::syscalls::socketpair(domain, type_, flags, protocol) + backend::net::syscalls::socketpair(domain, type_, flags, protocol) } diff --git a/vendor/rustix/src/net/sockopt.rs b/vendor/rustix/src/net/sockopt.rs index f5b149b37..436d5bf86 100644 --- a/vendor/rustix/src/net/sockopt.rs +++ b/vendor/rustix/src/net/sockopt.rs @@ -7,11 +7,11 @@ #![doc(alias = "setsockopt")] use crate::net::{Ipv4Addr, Ipv6Addr, SocketType}; -use crate::{imp, io}; +use crate::{backend, io}; +use backend::fd::AsFd; use core::time::Duration; -use imp::fd::AsFd; -pub use imp::net::types::Timeout; +pub use backend::net::types::Timeout; /// `getsockopt(fd, SOL_SOCKET, SO_TYPE)`—Returns the type of a socket. /// @@ -32,7 +32,7 @@ pub use imp::net::types::Timeout; #[inline] #[doc(alias = "SO_TYPE")] pub fn get_socket_type<Fd: AsFd>(fd: Fd) -> io::Result<SocketType> { - imp::net::syscalls::sockopt::get_socket_type(fd.as_fd()) + backend::net::syscalls::sockopt::get_socket_type(fd.as_fd()) } /// `setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, value)` @@ -54,7 +54,7 @@ pub fn get_socket_type<Fd: AsFd>(fd: Fd) -> io::Result<SocketType> { #[inline] #[doc(alias = "SO_REUSEADDR")] pub fn set_socket_reuseaddr<Fd: AsFd>(fd: Fd, value: bool) -> io::Result<()> { - imp::net::syscalls::sockopt::set_socket_reuseaddr(fd.as_fd(), value) + backend::net::syscalls::sockopt::set_socket_reuseaddr(fd.as_fd(), value) } /// `setsockopt(fd, SOL_SOCKET, SO_BROADCAST, broadcast)` @@ -76,7 +76,7 @@ pub fn set_socket_reuseaddr<Fd: AsFd>(fd: Fd, value: bool) -> io::Result<()> { #[inline] #[doc(alias = "SO_BROADCAST")] pub fn set_socket_broadcast<Fd: AsFd>(fd: Fd, broadcast: bool) -> io::Result<()> { - imp::net::syscalls::sockopt::set_socket_broadcast(fd.as_fd(), broadcast) + backend::net::syscalls::sockopt::set_socket_broadcast(fd.as_fd(), broadcast) } /// `getsockopt(fd, SOL_SOCKET, SO_BROADCAST)` @@ -98,7 +98,7 @@ pub fn set_socket_broadcast<Fd: AsFd>(fd: Fd, broadcast: bool) -> io::Result<()> #[inline] #[doc(alias = "SO_BROADCAST")] pub fn get_socket_broadcast<Fd: AsFd>(fd: Fd) -> io::Result<bool> { - imp::net::syscalls::sockopt::get_socket_broadcast(fd.as_fd()) + backend::net::syscalls::sockopt::get_socket_broadcast(fd.as_fd()) } /// `setsockopt(fd, SOL_SOCKET, SO_LINGER, linger)` @@ -120,7 +120,7 @@ pub fn get_socket_broadcast<Fd: AsFd>(fd: Fd) -> io::Result<bool> { #[inline] #[doc(alias = "SO_LINGER")] pub fn set_socket_linger<Fd: AsFd>(fd: Fd, linger: Option<Duration>) -> io::Result<()> { - imp::net::syscalls::sockopt::set_socket_linger(fd.as_fd(), linger) + backend::net::syscalls::sockopt::set_socket_linger(fd.as_fd(), linger) } /// `getsockopt(fd, SOL_SOCKET, SO_LINGER)` @@ -142,7 +142,7 @@ pub fn set_socket_linger<Fd: AsFd>(fd: Fd, linger: Option<Duration>) -> io::Resu #[inline] #[doc(alias = "SO_LINGER")] pub fn get_socket_linger<Fd: AsFd>(fd: Fd) -> io::Result<Option<Duration>> { - imp::net::syscalls::sockopt::get_socket_linger(fd.as_fd()) + backend::net::syscalls::sockopt::get_socket_linger(fd.as_fd()) } /// `setsockopt(fd, SOL_SOCKET, SO_PASSCRED, passcred)` @@ -157,7 +157,7 @@ pub fn get_socket_linger<Fd: AsFd>(fd: Fd) -> io::Result<Option<Duration>> { #[inline] #[doc(alias = "SO_PASSCRED")] pub fn set_socket_passcred<Fd: AsFd>(fd: Fd, passcred: bool) -> io::Result<()> { - imp::net::syscalls::sockopt::set_socket_passcred(fd.as_fd(), passcred) + backend::net::syscalls::sockopt::set_socket_passcred(fd.as_fd(), passcred) } /// `getsockopt(fd, SOL_SOCKET, SO_PASSCRED)` @@ -172,7 +172,7 @@ pub fn set_socket_passcred<Fd: AsFd>(fd: Fd, passcred: bool) -> io::Result<()> { #[inline] #[doc(alias = "SO_PASSCRED")] pub fn get_socket_passcred<Fd: AsFd>(fd: Fd) -> io::Result<bool> { - imp::net::syscalls::sockopt::get_socket_passcred(fd.as_fd()) + backend::net::syscalls::sockopt::get_socket_passcred(fd.as_fd()) } /// `setsockopt(fd, SOL_SOCKET, id, timeout)`—Set the sending @@ -200,7 +200,7 @@ pub fn set_socket_timeout<Fd: AsFd>( id: Timeout, timeout: Option<Duration>, ) -> io::Result<()> { - imp::net::syscalls::sockopt::set_socket_timeout(fd.as_fd(), id, timeout) + backend::net::syscalls::sockopt::set_socket_timeout(fd.as_fd(), id, timeout) } /// `getsockopt(fd, SOL_SOCKET, id)`—Get the sending or receiving timeout. @@ -223,7 +223,7 @@ pub fn set_socket_timeout<Fd: AsFd>( #[doc(alias = "SO_RCVTIMEO")] #[doc(alias = "SO_SNDTIMEO")] pub fn get_socket_timeout<Fd: AsFd>(fd: Fd, id: Timeout) -> io::Result<Option<Duration>> { - imp::net::syscalls::sockopt::get_socket_timeout(fd.as_fd(), id) + backend::net::syscalls::sockopt::get_socket_timeout(fd.as_fd(), id) } /// `setsockopt(fd, IPPROTO_IP, IP_TTL, ttl)` @@ -244,7 +244,7 @@ pub fn get_socket_timeout<Fd: AsFd>(fd: Fd, id: Timeout) -> io::Result<Option<Du #[inline] #[doc(alias = "IP_TTL")] pub fn set_ip_ttl<Fd: AsFd>(fd: Fd, ttl: u32) -> io::Result<()> { - imp::net::syscalls::sockopt::set_ip_ttl(fd.as_fd(), ttl) + backend::net::syscalls::sockopt::set_ip_ttl(fd.as_fd(), ttl) } /// `getsockopt(fd, IPPROTO_IP, IP_TTL)` @@ -266,7 +266,7 @@ pub fn set_ip_ttl<Fd: AsFd>(fd: Fd, ttl: u32) -> io::Result<()> { #[inline] #[doc(alias = "IP_TTL")] pub fn get_ip_ttl<Fd: AsFd>(fd: Fd) -> io::Result<u32> { - imp::net::syscalls::sockopt::get_ip_ttl(fd.as_fd()) + backend::net::syscalls::sockopt::get_ip_ttl(fd.as_fd()) } /// `setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, only_v6)` @@ -288,7 +288,7 @@ pub fn get_ip_ttl<Fd: AsFd>(fd: Fd) -> io::Result<u32> { #[inline] #[doc(alias = "IPV6_V6ONLY")] pub fn set_ipv6_v6only<Fd: AsFd>(fd: Fd, only_v6: bool) -> io::Result<()> { - imp::net::syscalls::sockopt::set_ipv6_v6only(fd.as_fd(), only_v6) + backend::net::syscalls::sockopt::set_ipv6_v6only(fd.as_fd(), only_v6) } /// `getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY)` @@ -310,7 +310,7 @@ pub fn set_ipv6_v6only<Fd: AsFd>(fd: Fd, only_v6: bool) -> io::Result<()> { #[inline] #[doc(alias = "IPV6_V6ONLY")] pub fn get_ipv6_v6only<Fd: AsFd>(fd: Fd) -> io::Result<bool> { - imp::net::syscalls::sockopt::get_ipv6_v6only(fd.as_fd()) + backend::net::syscalls::sockopt::get_ipv6_v6only(fd.as_fd()) } /// `setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, multicast_loop)` @@ -332,7 +332,7 @@ pub fn get_ipv6_v6only<Fd: AsFd>(fd: Fd) -> io::Result<bool> { #[inline] #[doc(alias = "IP_MULTICAST_LOOP")] pub fn set_ip_multicast_loop<Fd: AsFd>(fd: Fd, multicast_loop: bool) -> io::Result<()> { - imp::net::syscalls::sockopt::set_ip_multicast_loop(fd.as_fd(), multicast_loop) + backend::net::syscalls::sockopt::set_ip_multicast_loop(fd.as_fd(), multicast_loop) } /// `getsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP)` @@ -354,7 +354,7 @@ pub fn set_ip_multicast_loop<Fd: AsFd>(fd: Fd, multicast_loop: bool) -> io::Resu #[inline] #[doc(alias = "IP_MULTICAST_LOOP")] pub fn get_ip_multicast_loop<Fd: AsFd>(fd: Fd) -> io::Result<bool> { - imp::net::syscalls::sockopt::get_ip_multicast_loop(fd.as_fd()) + backend::net::syscalls::sockopt::get_ip_multicast_loop(fd.as_fd()) } /// `setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, multicast_ttl)` @@ -376,7 +376,7 @@ pub fn get_ip_multicast_loop<Fd: AsFd>(fd: Fd) -> io::Result<bool> { #[inline] #[doc(alias = "IP_MULTICAST_TTL")] pub fn set_ip_multicast_ttl<Fd: AsFd>(fd: Fd, multicast_ttl: u32) -> io::Result<()> { - imp::net::syscalls::sockopt::set_ip_multicast_ttl(fd.as_fd(), multicast_ttl) + backend::net::syscalls::sockopt::set_ip_multicast_ttl(fd.as_fd(), multicast_ttl) } /// `getsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL)` @@ -398,7 +398,7 @@ pub fn set_ip_multicast_ttl<Fd: AsFd>(fd: Fd, multicast_ttl: u32) -> io::Result< #[inline] #[doc(alias = "IP_MULTICAST_TTL")] pub fn get_ip_multicast_ttl<Fd: AsFd>(fd: Fd) -> io::Result<u32> { - imp::net::syscalls::sockopt::get_ip_multicast_ttl(fd.as_fd()) + backend::net::syscalls::sockopt::get_ip_multicast_ttl(fd.as_fd()) } /// `setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, multicast_loop)` @@ -420,7 +420,7 @@ pub fn get_ip_multicast_ttl<Fd: AsFd>(fd: Fd) -> io::Result<u32> { #[inline] #[doc(alias = "IPV6_MULTICAST_LOOP")] pub fn set_ipv6_multicast_loop<Fd: AsFd>(fd: Fd, multicast_loop: bool) -> io::Result<()> { - imp::net::syscalls::sockopt::set_ipv6_multicast_loop(fd.as_fd(), multicast_loop) + backend::net::syscalls::sockopt::set_ipv6_multicast_loop(fd.as_fd(), multicast_loop) } /// `getsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP)` @@ -442,7 +442,51 @@ pub fn set_ipv6_multicast_loop<Fd: AsFd>(fd: Fd, multicast_loop: bool) -> io::Re #[inline] #[doc(alias = "IPV6_MULTICAST_LOOP")] pub fn get_ipv6_multicast_loop<Fd: AsFd>(fd: Fd) -> io::Result<bool> { - imp::net::syscalls::sockopt::get_ipv6_multicast_loop(fd.as_fd()) + backend::net::syscalls::sockopt::get_ipv6_multicast_loop(fd.as_fd()) +} + +/// `setsockopt(fd, IPPROTO_IP, IPV6_MULTICAST_HOPS, multicast_hops)` +/// +/// # References +/// - [POSIX `setsockopt`] +/// - [POSIX `netinet/in.h`] +/// - [Linux `setsockopt`] +/// - [Linux `ipv6`] +/// - [Winsock2 `setsockopt`] +/// - [Winsock2 `IPPROTO_IPV6` options] +/// +/// [POSIX `setsockopt`]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/setsockopt.html +/// [POSIX `netinet/in.h`]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/netinet_in.h.html +/// [Linux `setsockopt`]: https://man7.org/linux/man-pages/man2/setsockopt.2.html +/// [Linux `ipv6`]: https://man7.org/linux/man-pages/man7/ipv6.7.html +/// [Winsock2 `setsockopt`]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-setsockopt +/// [Winsock2 `IPPROTO_IPV6` options]: https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ipv6-socket-options +#[inline] +#[doc(alias = "IP_MULTICAST_TTL")] +pub fn set_ipv6_multicast_hops<Fd: AsFd>(fd: Fd, multicast_hops: u32) -> io::Result<()> { + backend::net::syscalls::sockopt::set_ipv6_multicast_hops(fd.as_fd(), multicast_hops) +} + +/// `getsockopt(fd, IPPROTO_IP, IPV6_MULTICAST_HOPS)` +/// +/// # References +/// - [POSIX `getsockopt`] +/// - [POSIX `netinet/in.h`] +/// - [Linux `getsockopt`] +/// - [Linux `ipv6`] +/// - [Winsock2 `getsockopt`] +/// - [Winsock2 `IPPROTO_IPV6` options] +/// +/// [POSIX `getsockopt`]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockopt.html +/// [POSIX `netinet/in.h`]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/netinet_in.h.html +/// [Linux `getsockopt`]: https://man7.org/linux/man-pages/man2/getsockopt.2.html +/// [Linux `ipv6`]: https://man7.org/linux/man-pages/man7/ipv6.7.html +/// [Winsock2 `getsockopt`]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-getsockopt +/// [Winsock2 `IPPROTO_IPV6` options]: https://docs.microsoft.com/en-us/windows/win32/winsock/ipproto-ipv6-socket-options +#[inline] +#[doc(alias = "IP_MULTICAST_TTL")] +pub fn get_ipv6_multicast_hops<Fd: AsFd>(fd: Fd) -> io::Result<u32> { + backend::net::syscalls::sockopt::get_ipv6_multicast_hops(fd.as_fd()) } /// `setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, multiaddr, interface)` @@ -468,7 +512,7 @@ pub fn set_ip_add_membership<Fd: AsFd>( multiaddr: &Ipv4Addr, interface: &Ipv4Addr, ) -> io::Result<()> { - imp::net::syscalls::sockopt::set_ip_add_membership(fd.as_fd(), multiaddr, interface) + backend::net::syscalls::sockopt::set_ip_add_membership(fd.as_fd(), multiaddr, interface) } /// `setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, multiaddr, interface)` @@ -497,7 +541,7 @@ pub fn set_ipv6_add_membership<Fd: AsFd>( multiaddr: &Ipv6Addr, interface: u32, ) -> io::Result<()> { - imp::net::syscalls::sockopt::set_ipv6_add_membership(fd.as_fd(), multiaddr, interface) + backend::net::syscalls::sockopt::set_ipv6_add_membership(fd.as_fd(), multiaddr, interface) } /// `setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, multiaddr, interface)` @@ -523,7 +567,7 @@ pub fn set_ip_drop_membership<Fd: AsFd>( multiaddr: &Ipv4Addr, interface: &Ipv4Addr, ) -> io::Result<()> { - imp::net::syscalls::sockopt::set_ip_drop_membership(fd.as_fd(), multiaddr, interface) + backend::net::syscalls::sockopt::set_ip_drop_membership(fd.as_fd(), multiaddr, interface) } /// `setsockopt(fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, multiaddr, interface)` @@ -552,7 +596,7 @@ pub fn set_ipv6_drop_membership<Fd: AsFd>( multiaddr: &Ipv6Addr, interface: u32, ) -> io::Result<()> { - imp::net::syscalls::sockopt::set_ipv6_drop_membership(fd.as_fd(), multiaddr, interface) + backend::net::syscalls::sockopt::set_ipv6_drop_membership(fd.as_fd(), multiaddr, interface) } /// `setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, nodelay)` @@ -574,7 +618,7 @@ pub fn set_ipv6_drop_membership<Fd: AsFd>( #[inline] #[doc(alias = "TCP_NODELAY")] pub fn set_tcp_nodelay<Fd: AsFd>(fd: Fd, nodelay: bool) -> io::Result<()> { - imp::net::syscalls::sockopt::set_tcp_nodelay(fd.as_fd(), nodelay) + backend::net::syscalls::sockopt::set_tcp_nodelay(fd.as_fd(), nodelay) } /// `getsockopt(fd, IPPROTO_TCP, TCP_NODELAY)` @@ -596,5 +640,5 @@ pub fn set_tcp_nodelay<Fd: AsFd>(fd: Fd, nodelay: bool) -> io::Result<()> { #[inline] #[doc(alias = "TCP_NODELAY")] pub fn get_tcp_nodelay<Fd: AsFd>(fd: Fd) -> io::Result<bool> { - imp::net::syscalls::sockopt::get_tcp_nodelay(fd.as_fd()) + backend::net::syscalls::sockopt::get_tcp_nodelay(fd.as_fd()) } diff --git a/vendor/rustix/src/net/wsa.rs b/vendor/rustix/src/net/wsa.rs index e2a70c97b..3367ca95c 100644 --- a/vendor/rustix/src/net/wsa.rs +++ b/vendor/rustix/src/net/wsa.rs @@ -1,6 +1,6 @@ use crate::io; use core::mem::MaybeUninit; -use windows_sys::Win32::Networking::WinSock::{WSACleanup, WSAData, WSAGetLastError, WSAStartup}; +use windows_sys::Win32::Networking::WinSock::{WSACleanup, WSAGetLastError, WSAStartup, WSADATA}; /// `WSAStartup()`—Initialize process-wide Windows support for sockets. /// @@ -11,7 +11,7 @@ use windows_sys::Win32::Networking::WinSock::{WSACleanup, WSAData, WSAGetLastErr /// - [Winsock2] /// /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsastartup -pub fn wsa_startup() -> io::Result<WSAData> { +pub fn wsa_startup() -> io::Result<WSADATA> { // Request version 2.2, which has been the latest version since far older // versions of Windows than we support here. For more information about // the version, see [here]. |