Add Metadata in std::os::fortanix_sgx::io::FromRawFd

This commit is contained in:
Mohsen Zohrevandi 2020-11-11 10:36:54 -08:00
parent cf9cf7c923
commit d56969656e
2 changed files with 33 additions and 10 deletions

View File

@ -25,8 +25,11 @@ pub trait AsRawFd {
/// descriptor.
#[unstable(feature = "sgx_platform", issue = "56975")]
pub trait FromRawFd {
/// An associated type that contains relevant metadata for `Self`.
type Metadata: Default;
/// Constructs a new instance of `Self` from the given raw file
/// descriptor.
/// descriptor and metadata.
///
/// This function **consumes ownership** of the specified file
/// descriptor. The returned object will take responsibility for closing
@ -38,7 +41,7 @@ pub trait FromRawFd {
/// accidentally allow violating this contract which can cause memory
/// unsafety in code that relies on it being true.
#[unstable(feature = "sgx_platform", issue = "56975")]
unsafe fn from_raw_fd(fd: RawFd) -> Self;
unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> Self;
}
/// A trait to express the ability to consume an object and acquire ownership of
@ -71,18 +74,38 @@ fn as_raw_fd(&self) -> RawFd {
}
}
/// Metadata for `TcpStream`.
#[derive(Debug, Clone, Default)]
pub struct TcpStreamMetadata {
/// Local address of the TCP stream
pub local_addr: Option<String>,
/// Peer address of the TCP stream
pub peer_addr: Option<String>,
}
impl FromRawFd for net::TcpStream {
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
type Metadata = TcpStreamMetadata;
unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> net::TcpStream {
let fd = sys::fd::FileDesc::from_inner(fd);
let socket = sys::net::Socket::from_inner(fd);
net::TcpStream::from_inner(sys::net::TcpStream::from_inner((socket, None)))
let socket = sys::net::Socket::from_inner((fd, metadata.local_addr));
net::TcpStream::from_inner(sys::net::TcpStream::from_inner((socket, metadata.peer_addr)))
}
}
/// Metadata for `TcpListener`.
#[derive(Debug, Clone, Default)]
pub struct TcpListenerMetadata {
/// Local address of the TCP listener
pub local_addr: Option<String>,
}
impl FromRawFd for net::TcpListener {
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
type Metadata = TcpListenerMetadata;
unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> net::TcpListener {
let fd = sys::fd::FileDesc::from_inner(fd);
let socket = sys::net::Socket::from_inner(fd);
let socket = sys::net::Socket::from_inner((fd, metadata.local_addr));
net::TcpListener::from_inner(sys::net::TcpListener::from_inner(socket))
}
}

View File

@ -37,9 +37,9 @@ fn try_into_inner(self) -> Result<FileDesc, Socket> {
}
}
impl FromInner<FileDesc> for Socket {
fn from_inner(inner: FileDesc) -> Socket {
Socket { inner: Arc::new(inner), local_addr: None }
impl FromInner<(FileDesc, Option<String>)> for Socket {
fn from_inner((inner, local_addr): (FileDesc, Option<String>)) -> Socket {
Socket { inner: Arc::new(inner), local_addr }
}
}