Skip to content

Commit d569696

Browse files
committed
Add Metadata in std::os::fortanix_sgx::io::FromRawFd
1 parent cf9cf7c commit d569696

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

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

+30-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,38 @@ impl AsRawFd for net::TcpListener {
7174
}
7275
}
7376

77+
/// Metadata for `TcpStream`.
78+
#[derive(Debug, Clone, Default)]
79+
pub struct TcpStreamMetadata {
80+
/// Local address of the TCP stream
81+
pub local_addr: Option<String>,
82+
/// Peer address of the TCP stream
83+
pub peer_addr: Option<String>,
84+
}
85+
7486
impl FromRawFd for net::TcpStream {
75-
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
87+
type Metadata = TcpStreamMetadata;
88+
89+
unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> net::TcpStream {
7690
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)))
91+
let socket = sys::net::Socket::from_inner((fd, metadata.local_addr));
92+
net::TcpStream::from_inner(sys::net::TcpStream::from_inner((socket, metadata.peer_addr)))
7993
}
8094
}
8195

96+
/// Metadata for `TcpListener`.
97+
#[derive(Debug, Clone, Default)]
98+
pub struct TcpListenerMetadata {
99+
/// Local address of the TCP listener
100+
pub local_addr: Option<String>,
101+
}
102+
82103
impl FromRawFd for net::TcpListener {
83-
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
104+
type Metadata = TcpListenerMetadata;
105+
106+
unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> net::TcpListener {
84107
let fd = sys::fd::FileDesc::from_inner(fd);
85-
let socket = sys::net::Socket::from_inner(fd);
108+
let socket = sys::net::Socket::from_inner((fd, metadata.local_addr));
86109
net::TcpListener::from_inner(sys::net::TcpListener::from_inner(socket))
87110
}
88111
}

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)