about summary refs log tree commit diff
path: root/library/std/src/net
diff options
context:
space:
mode:
authorjoboet <jonasboettiger@icloud.com>2025-02-07 18:34:09 +0100
committerjoboet <jonasboettiger@icloud.com>2025-02-12 14:13:35 +0100
commit80c60fe78375cb10a2fed6540dc2b29751fc4263 (patch)
tree207a56b85129112a5c9d217777bd8fca36a35526 /library/std/src/net
parentc182ce9cbc8c29ebc1b4559d027df545e6cdd287 (diff)
downloadrust-80c60fe78375cb10a2fed6540dc2b29751fc4263.tar.gz
rust-80c60fe78375cb10a2fed6540dc2b29751fc4263.zip
std: replace the `FromInner` implementation for addresses with private conversion functions
Having these implementation available crate-wide means that platforms not using sockets for their networking code have to stub out the libc definitions required to support them. This PR moves the conversions to private helper functions that are only available where actually needed.

I also fixed the signature of the function converting from a C socket address to a Rust one: taking a reference to a `sockaddr_storage` resulted in unsound usage inside  `LookupHost::next`, which could create a reference to a structure smaller than `sockaddr_storage`. Thus I've replaced the argument type with a pointer and made the function `unsafe`.
Diffstat (limited to 'library/std/src/net')
-rw-r--r--library/std/src/net/ip_addr.rs29
-rw-r--r--library/std/src/net/socket_addr.rs46
2 files changed, 2 insertions, 73 deletions
diff --git a/library/std/src/net/ip_addr.rs b/library/std/src/net/ip_addr.rs
index 4d673a1d66d..7262899b3bb 100644
--- a/library/std/src/net/ip_addr.rs
+++ b/library/std/src/net/ip_addr.rs
@@ -8,32 +8,3 @@ pub use core::net::IpAddr;
 pub use core::net::Ipv6MulticastScope;
 #[stable(feature = "rust1", since = "1.0.0")]
 pub use core::net::{Ipv4Addr, Ipv6Addr};
-
-use crate::sys::net::netc as c;
-use crate::sys_common::{FromInner, IntoInner};
-
-impl IntoInner<c::in_addr> for Ipv4Addr {
-    #[inline]
-    fn into_inner(self) -> c::in_addr {
-        // `s_addr` is stored as BE on all machines and the array is in BE order.
-        // So the native endian conversion method is used so that it's never swapped.
-        c::in_addr { s_addr: u32::from_ne_bytes(self.octets()) }
-    }
-}
-impl FromInner<c::in_addr> for Ipv4Addr {
-    fn from_inner(addr: c::in_addr) -> Ipv4Addr {
-        Ipv4Addr::from(addr.s_addr.to_ne_bytes())
-    }
-}
-
-impl IntoInner<c::in6_addr> for Ipv6Addr {
-    fn into_inner(self) -> c::in6_addr {
-        c::in6_addr { s6_addr: self.octets() }
-    }
-}
-impl FromInner<c::in6_addr> for Ipv6Addr {
-    #[inline]
-    fn from_inner(addr: c::in6_addr) -> Ipv6Addr {
-        Ipv6Addr::from(addr.s6_addr)
-    }
-}
diff --git a/library/std/src/net/socket_addr.rs b/library/std/src/net/socket_addr.rs
index e8355cc31d7..4c8905c0d46 100644
--- a/library/std/src/net/socket_addr.rs
+++ b/library/std/src/net/socket_addr.rs
@@ -6,50 +6,8 @@ mod tests;
 pub use core::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
 
 use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr};
-use crate::sys::net::{LookupHost, netc as c};
-use crate::sys_common::{FromInner, IntoInner};
-use crate::{io, iter, mem, option, slice, vec};
-
-impl FromInner<c::sockaddr_in> for SocketAddrV4 {
-    fn from_inner(addr: c::sockaddr_in) -> SocketAddrV4 {
-        SocketAddrV4::new(Ipv4Addr::from_inner(addr.sin_addr), u16::from_be(addr.sin_port))
-    }
-}
-
-impl FromInner<c::sockaddr_in6> for SocketAddrV6 {
-    fn from_inner(addr: c::sockaddr_in6) -> SocketAddrV6 {
-        SocketAddrV6::new(
-            Ipv6Addr::from_inner(addr.sin6_addr),
-            u16::from_be(addr.sin6_port),
-            addr.sin6_flowinfo,
-            addr.sin6_scope_id,
-        )
-    }
-}
-
-impl IntoInner<c::sockaddr_in> for SocketAddrV4 {
-    fn into_inner(self) -> c::sockaddr_in {
-        c::sockaddr_in {
-            sin_family: c::AF_INET as c::sa_family_t,
-            sin_port: self.port().to_be(),
-            sin_addr: self.ip().into_inner(),
-            ..unsafe { mem::zeroed() }
-        }
-    }
-}
-
-impl IntoInner<c::sockaddr_in6> for SocketAddrV6 {
-    fn into_inner(self) -> c::sockaddr_in6 {
-        c::sockaddr_in6 {
-            sin6_family: c::AF_INET6 as c::sa_family_t,
-            sin6_port: self.port().to_be(),
-            sin6_addr: self.ip().into_inner(),
-            sin6_flowinfo: self.flowinfo(),
-            sin6_scope_id: self.scope_id(),
-            ..unsafe { mem::zeroed() }
-        }
-    }
-}
+use crate::sys::net::LookupHost;
+use crate::{io, iter, option, slice, vec};
 
 /// A trait for objects which can be converted or resolved to one or more
 /// [`SocketAddr`] values.