diff options
Diffstat (limited to 'vendor/rustix/src/net/addr.rs')
-rw-r--r-- | vendor/rustix/src/net/addr.rs | 204 |
1 files changed, 44 insertions, 160 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) } } |