Skip to content

Commit

Permalink
Rollup merge of rust-lang#78953 - mzohreva:mz/from_raw_fd, r=Mark-Sim…
Browse files Browse the repository at this point in the history
…ulacrum

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

Needed for fortanix/rust-sgx#291

cc `@jethrogb`
  • Loading branch information
Dylan-DPC authored Nov 24, 2020
2 parents f32a0cc + 084b0da commit 04db18d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
39 changes: 32 additions & 7 deletions library/std/src/sys/sgx/ext/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -71,18 +74,40 @@ impl AsRawFd for net::TcpListener {
}
}

/// Metadata for `TcpStream`.
#[derive(Debug, Clone, Default)]
#[unstable(feature = "sgx_platform", issue = "56975")]
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)]
#[unstable(feature = "sgx_platform", issue = "56975")]
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))
}
}
Expand Down
6 changes: 3 additions & 3 deletions library/std/src/sys/sgx/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ impl TryIntoInner<FileDesc> for 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 }
}
}

Expand Down

0 comments on commit 04db18d

Please sign in to comment.