Skip to content

Commit 4167d73

Browse files
committed
Auto merge of #78953 - mzohreva:mz/from_raw_fd, r=Mark-Simulacrum
Add Metadata in std::os::fortanix_sgx::io::FromRawFd Needed for fortanix/rust-sgx#291 cc `@jethrogb`
2 parents cb5b871 + 084b0da commit 4167d73

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

library/std/src/sys/sgx/ext/io.rs

+32-7
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ pub trait AsRawFd {
2525
/// descriptor.
2626
#[unstable(feature = "sgx_platform", issue = "56975")]
2727
pub trait FromRawFd {
28+
/// An associated type that contains relevant metadata for `Self`.
29+
type Metadata: Default;
30+
2831
/// Constructs a new instance of `Self` from the given raw file
29-
/// descriptor.
32+
/// descriptor and metadata.
3033
///
3134
/// This function **consumes ownership** of the specified file
3235
/// descriptor. The returned object will take responsibility for closing
@@ -38,7 +41,7 @@ pub trait FromRawFd {
3841
/// accidentally allow violating this contract which can cause memory
3942
/// unsafety in code that relies on it being true.
4043
#[unstable(feature = "sgx_platform", issue = "56975")]
41-
unsafe fn from_raw_fd(fd: RawFd) -> Self;
44+
unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> Self;
4245
}
4346

4447
/// A trait to express the ability to consume an object and acquire ownership of
@@ -71,18 +74,40 @@ impl AsRawFd for net::TcpListener {
7174
}
7275
}
7376

77+
/// Metadata for `TcpStream`.
78+
#[derive(Debug, Clone, Default)]
79+
#[unstable(feature = "sgx_platform", issue = "56975")]
80+
pub struct TcpStreamMetadata {
81+
/// Local address of the TCP stream
82+
pub local_addr: Option<String>,
83+
/// Peer address of the TCP stream
84+
pub peer_addr: Option<String>,
85+
}
86+
7487
impl FromRawFd for net::TcpStream {
75-
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
88+
type Metadata = TcpStreamMetadata;
89+
90+
unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> net::TcpStream {
7691
let fd = sys::fd::FileDesc::from_inner(fd);
77-
let socket = sys::net::Socket::from_inner(fd);
78-
net::TcpStream::from_inner(sys::net::TcpStream::from_inner((socket, None)))
92+
let socket = sys::net::Socket::from_inner((fd, metadata.local_addr));
93+
net::TcpStream::from_inner(sys::net::TcpStream::from_inner((socket, metadata.peer_addr)))
7994
}
8095
}
8196

97+
/// Metadata for `TcpListener`.
98+
#[derive(Debug, Clone, Default)]
99+
#[unstable(feature = "sgx_platform", issue = "56975")]
100+
pub struct TcpListenerMetadata {
101+
/// Local address of the TCP listener
102+
pub local_addr: Option<String>,
103+
}
104+
82105
impl FromRawFd for net::TcpListener {
83-
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
106+
type Metadata = TcpListenerMetadata;
107+
108+
unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> net::TcpListener {
84109
let fd = sys::fd::FileDesc::from_inner(fd);
85-
let socket = sys::net::Socket::from_inner(fd);
110+
let socket = sys::net::Socket::from_inner((fd, metadata.local_addr));
86111
net::TcpListener::from_inner(sys::net::TcpListener::from_inner(socket))
87112
}
88113
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ impl TryIntoInner<FileDesc> for Socket {
3737
}
3838
}
3939

40-
impl FromInner<FileDesc> for Socket {
41-
fn from_inner(inner: FileDesc) -> Socket {
42-
Socket { inner: Arc::new(inner), local_addr: None }
40+
impl FromInner<(FileDesc, Option<String>)> for Socket {
41+
fn from_inner((inner, local_addr): (FileDesc, Option<String>)) -> Socket {
42+
Socket { inner: Arc::new(inner), local_addr }
4343
}
4444
}
4545

0 commit comments

Comments
 (0)