Skip to main content

std/os/fortanix_sgx/
io.rs

1//! SGX-specific extensions to general I/O primitives
2//!
3//! SGX file descriptors behave differently from Unix file descriptors. See the
4//! description of [`TryIntoRawFd`] for more details.
5#![unstable(feature = "sgx_platform", issue = "56975")]
6
7use crate::net;
8pub use crate::sys::abi::usercalls::raw::Fd as RawFd;
9use crate::sys::{self, AsInner, FromInner, IntoInner, TryIntoInner};
10
11/// A trait to extract the raw SGX file descriptor from an underlying
12/// object.
13#[unstable(feature = "sgx_platform", issue = "56975")]
14pub trait AsRawFd {
15    /// Extracts the raw file descriptor.
16    ///
17    /// This method does **not** pass ownership of the raw file descriptor
18    /// to the caller. The descriptor is only guaranteed to be valid while
19    /// the original object has not yet been destroyed.
20    #[unstable(feature = "sgx_platform", issue = "56975")]
21    fn as_raw_fd(&self) -> RawFd;
22}
23
24/// A trait to express the ability to construct an object from a raw file
25/// descriptor.
26#[unstable(feature = "sgx_platform", issue = "56975")]
27pub trait FromRawFd {
28    /// An associated type that contains relevant metadata for `Self`.
29    type Metadata: Default;
30
31    /// Constructs a new instance of `Self` from the given raw file
32    /// descriptor and metadata.
33    ///
34    /// This function is typically used to **consume ownership** of the
35    /// specified file descriptor. When used in this way, the returned object
36    /// will take responsibility for closing it when the object goes out of
37    /// scope.
38    ///
39    /// However, consuming ownership is not strictly required. Use a
40    /// [`From<OwnedFd>::from`] implementation for an API which strictly
41    /// consumes ownership.
42    ///
43    /// # Safety
44    ///
45    /// The `fd` passed in must be an [owned file descriptor][io-safety];
46    /// in particular, it must be open.
47    // FIXME: say something about `metadata`.
48    ///
49    /// [io-safety]: io#io-safety
50    #[unstable(feature = "sgx_platform", issue = "56975")]
51    unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> Self;
52}
53
54/// A trait to express the ability to consume an object and acquire ownership of
55/// its raw file descriptor.
56#[unstable(feature = "sgx_platform", issue = "56975")]
57pub trait TryIntoRawFd: Sized {
58    /// Consumes this object, returning the raw underlying file descriptor, if
59    /// this object is not cloned.
60    ///
61    /// This function **transfers ownership** of the underlying file descriptor
62    /// to the caller. Callers are then the unique owners of the file descriptor
63    /// and must close the descriptor once it's no longer needed.
64    ///
65    /// Unlike other platforms, on SGX, the file descriptor is shared between
66    /// all clones of an object. To avoid race conditions, this function will
67    /// only return `Ok` when called on the final clone.
68    #[unstable(feature = "sgx_platform", issue = "56975")]
69    fn try_into_raw_fd(self) -> Result<RawFd, Self>;
70}
71
72impl AsRawFd for net::TcpStream {
73    #[inline]
74    fn as_raw_fd(&self) -> RawFd {
75        *self.as_inner().as_inner().as_inner().as_inner()
76    }
77}
78
79impl AsRawFd for net::TcpListener {
80    #[inline]
81    fn as_raw_fd(&self) -> RawFd {
82        *self.as_inner().as_inner().as_inner().as_inner()
83    }
84}
85
86/// Metadata for `TcpStream`.
87#[derive(Debug, Clone, Default)]
88#[unstable(feature = "sgx_platform", issue = "56975")]
89pub struct TcpStreamMetadata {
90    /// Local address of the TCP stream
91    pub local_addr: Option<String>,
92    /// Peer address of the TCP stream
93    pub peer_addr: Option<String>,
94}
95
96impl FromRawFd for net::TcpStream {
97    type Metadata = TcpStreamMetadata;
98
99    #[inline]
100    unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> net::TcpStream {
101        let fd = sys::fd::FileDesc::from_inner(fd);
102        let socket = sys::net::Socket::from_inner((fd, metadata.local_addr));
103        net::TcpStream::from_inner(sys::net::TcpStream::from_inner((socket, metadata.peer_addr)))
104    }
105}
106
107/// Metadata for `TcpListener`.
108#[derive(Debug, Clone, Default)]
109#[unstable(feature = "sgx_platform", issue = "56975")]
110pub struct TcpListenerMetadata {
111    /// Local address of the TCP listener
112    pub local_addr: Option<String>,
113}
114
115impl FromRawFd for net::TcpListener {
116    type Metadata = TcpListenerMetadata;
117
118    #[inline]
119    unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> net::TcpListener {
120        let fd = sys::fd::FileDesc::from_inner(fd);
121        let socket = sys::net::Socket::from_inner((fd, metadata.local_addr));
122        net::TcpListener::from_inner(sys::net::TcpListener::from_inner(socket))
123    }
124}
125
126impl TryIntoRawFd for net::TcpStream {
127    #[inline]
128    fn try_into_raw_fd(self) -> Result<RawFd, Self> {
129        let (socket, peer_addr) = self.into_inner().into_inner();
130        match socket.try_into_inner() {
131            Ok(fd) => Ok(fd.into_inner()),
132            Err(socket) => {
133                let sys = sys::net::TcpStream::from_inner((socket, peer_addr));
134                Err(net::TcpStream::from_inner(sys))
135            }
136        }
137    }
138}
139
140impl TryIntoRawFd for net::TcpListener {
141    #[inline]
142    fn try_into_raw_fd(self) -> Result<RawFd, Self> {
143        match self.into_inner().into_inner().try_into_inner() {
144            Ok(fd) => Ok(fd.into_inner()),
145            Err(socket) => {
146                let sys = sys::net::TcpListener::from_inner(socket);
147                Err(net::TcpListener::from_inner(sys))
148            }
149        }
150    }
151}