Skip to content

Commit 6060897

Browse files
jhprattgitbot
authored and
gitbot
committed
Rollup merge of rust-lang#136699 - joboet:netaddr_from_inner, r=cuviper
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`.
2 parents 9b53cbb + 256c502 commit 6060897

File tree

14 files changed

+155
-348
lines changed

14 files changed

+155
-348
lines changed

std/src/net/ip_addr.rs

-29
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,3 @@ pub use core::net::IpAddr;
88
pub use core::net::Ipv6MulticastScope;
99
#[stable(feature = "rust1", since = "1.0.0")]
1010
pub use core::net::{Ipv4Addr, Ipv6Addr};
11-
12-
use crate::sys::net::netc as c;
13-
use crate::sys_common::{FromInner, IntoInner};
14-
15-
impl IntoInner<c::in_addr> for Ipv4Addr {
16-
#[inline]
17-
fn into_inner(self) -> c::in_addr {
18-
// `s_addr` is stored as BE on all machines and the array is in BE order.
19-
// So the native endian conversion method is used so that it's never swapped.
20-
c::in_addr { s_addr: u32::from_ne_bytes(self.octets()) }
21-
}
22-
}
23-
impl FromInner<c::in_addr> for Ipv4Addr {
24-
fn from_inner(addr: c::in_addr) -> Ipv4Addr {
25-
Ipv4Addr::from(addr.s_addr.to_ne_bytes())
26-
}
27-
}
28-
29-
impl IntoInner<c::in6_addr> for Ipv6Addr {
30-
fn into_inner(self) -> c::in6_addr {
31-
c::in6_addr { s6_addr: self.octets() }
32-
}
33-
}
34-
impl FromInner<c::in6_addr> for Ipv6Addr {
35-
#[inline]
36-
fn from_inner(addr: c::in6_addr) -> Ipv6Addr {
37-
Ipv6Addr::from(addr.s6_addr)
38-
}
39-
}

std/src/net/socket_addr.rs

+2-44
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,8 @@ mod tests;
66
pub use core::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
77

88
use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr};
9-
use crate::sys::net::{LookupHost, netc as c};
10-
use crate::sys_common::{FromInner, IntoInner};
11-
use crate::{io, iter, mem, option, slice, vec};
12-
13-
impl FromInner<c::sockaddr_in> for SocketAddrV4 {
14-
fn from_inner(addr: c::sockaddr_in) -> SocketAddrV4 {
15-
SocketAddrV4::new(Ipv4Addr::from_inner(addr.sin_addr), u16::from_be(addr.sin_port))
16-
}
17-
}
18-
19-
impl FromInner<c::sockaddr_in6> for SocketAddrV6 {
20-
fn from_inner(addr: c::sockaddr_in6) -> SocketAddrV6 {
21-
SocketAddrV6::new(
22-
Ipv6Addr::from_inner(addr.sin6_addr),
23-
u16::from_be(addr.sin6_port),
24-
addr.sin6_flowinfo,
25-
addr.sin6_scope_id,
26-
)
27-
}
28-
}
29-
30-
impl IntoInner<c::sockaddr_in> for SocketAddrV4 {
31-
fn into_inner(self) -> c::sockaddr_in {
32-
c::sockaddr_in {
33-
sin_family: c::AF_INET as c::sa_family_t,
34-
sin_port: self.port().to_be(),
35-
sin_addr: self.ip().into_inner(),
36-
..unsafe { mem::zeroed() }
37-
}
38-
}
39-
}
40-
41-
impl IntoInner<c::sockaddr_in6> for SocketAddrV6 {
42-
fn into_inner(self) -> c::sockaddr_in6 {
43-
c::sockaddr_in6 {
44-
sin6_family: c::AF_INET6 as c::sa_family_t,
45-
sin6_port: self.port().to_be(),
46-
sin6_addr: self.ip().into_inner(),
47-
sin6_flowinfo: self.flowinfo(),
48-
sin6_scope_id: self.scope_id(),
49-
..unsafe { mem::zeroed() }
50-
}
51-
}
52-
}
9+
use crate::sys::net::LookupHost;
10+
use crate::{io, iter, option, slice, vec};
5311

5412
/// A trait for objects which can be converted or resolved to one or more
5513
/// [`SocketAddr`] values.

std/src/os/solid/io.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl BorrowedFd<'_> {
122122
/// Creates a new `OwnedFd` instance that shares the same underlying file
123123
/// description as the existing `BorrowedFd` instance.
124124
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedFd> {
125-
let fd = sys::net::cvt(unsafe { sys::net::netc::dup(self.as_raw_fd()) })?;
125+
let fd = sys::net::cvt(unsafe { crate::sys::abi::sockets::dup(self.as_raw_fd()) })?;
126126
Ok(unsafe { OwnedFd::from_raw_fd(fd) })
127127
}
128128
}
@@ -168,7 +168,7 @@ impl FromRawFd for OwnedFd {
168168
impl Drop for OwnedFd {
169169
#[inline]
170170
fn drop(&mut self) {
171-
unsafe { sys::net::netc::close(self.fd.as_inner()) };
171+
unsafe { crate::sys::abi::sockets::close(self.fd.as_inner()) };
172172
}
173173
}
174174

std/src/sys/net/connection/sgx.rs

-35
Original file line numberDiff line numberDiff line change
@@ -499,38 +499,3 @@ impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
499499
LookupHost::new(format!("{host}:{port}"))
500500
}
501501
}
502-
503-
#[allow(bad_style)]
504-
pub mod netc {
505-
pub const AF_INET: u8 = 0;
506-
pub const AF_INET6: u8 = 1;
507-
pub type sa_family_t = u8;
508-
509-
#[derive(Copy, Clone)]
510-
pub struct in_addr {
511-
pub s_addr: u32,
512-
}
513-
514-
#[derive(Copy, Clone)]
515-
pub struct sockaddr_in {
516-
#[allow(dead_code)]
517-
pub sin_family: sa_family_t,
518-
pub sin_port: u16,
519-
pub sin_addr: in_addr,
520-
}
521-
522-
#[derive(Copy, Clone)]
523-
pub struct in6_addr {
524-
pub s6_addr: [u8; 16],
525-
}
526-
527-
#[derive(Copy, Clone)]
528-
pub struct sockaddr_in6 {
529-
#[allow(dead_code)]
530-
pub sin6_family: sa_family_t,
531-
pub sin6_port: u16,
532-
pub sin6_addr: in6_addr,
533-
pub sin6_flowinfo: u32,
534-
pub sin6_scope_id: u32,
535-
}
536-
}

0 commit comments

Comments
 (0)