1 //! SGX-specific extensions to general I/O primitives
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")]
8 pub use crate::sys::abi::usercalls::raw::Fd as RawFd;
9 use crate::sys::{self, AsInner, FromInner, IntoInner, TryIntoInner};
11 /// A trait to extract the raw SGX file descriptor from an underlying
13 #[unstable(feature = "sgx_platform", issue = "56975")]
15 /// Extracts the raw file descriptor.
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;
24 /// A trait to express the ability to construct an object from a raw file
26 #[unstable(feature = "sgx_platform", issue = "56975")]
28 /// An associated type that contains relevant metadata for `Self`.
29 type Metadata: Default;
31 /// Constructs a new instance of `Self` from the given raw file
32 /// descriptor and metadata.
34 /// This function **consumes ownership** of the specified file
35 /// descriptor. The returned object will take responsibility for closing
36 /// it when the object goes out of scope.
38 /// This function is also unsafe as the primitives currently returned
39 /// have the contract that they are the sole owner of the file
40 /// descriptor they are wrapping. Usage of this function could
41 /// accidentally allow violating this contract which can cause memory
42 /// unsafety in code that relies on it being true.
43 #[unstable(feature = "sgx_platform", issue = "56975")]
44 unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> Self;
47 /// A trait to express the ability to consume an object and acquire ownership of
48 /// its raw file descriptor.
49 #[unstable(feature = "sgx_platform", issue = "56975")]
50 pub trait TryIntoRawFd: Sized {
51 /// Consumes this object, returning the raw underlying file descriptor, if
52 /// this object is not cloned.
54 /// This function **transfers ownership** of the underlying file descriptor
55 /// to the caller. Callers are then the unique owners of the file descriptor
56 /// and must close the descriptor once it's no longer needed.
58 /// Unlike other platforms, on SGX, the file descriptor is shared between
59 /// all clones of an object. To avoid race conditions, this function will
60 /// only return `Ok` when called on the final clone.
61 #[unstable(feature = "sgx_platform", issue = "56975")]
62 fn try_into_raw_fd(self) -> Result<RawFd, Self>;
65 impl AsRawFd for net::TcpStream {
67 fn as_raw_fd(&self) -> RawFd {
68 *self.as_inner().as_inner().as_inner().as_inner()
72 impl AsRawFd for net::TcpListener {
74 fn as_raw_fd(&self) -> RawFd {
75 *self.as_inner().as_inner().as_inner().as_inner()
79 /// Metadata for `TcpStream`.
80 #[derive(Debug, Clone, Default)]
81 #[unstable(feature = "sgx_platform", issue = "56975")]
82 pub struct TcpStreamMetadata {
83 /// Local address of the TCP stream
84 pub local_addr: Option<String>,
85 /// Peer address of the TCP stream
86 pub peer_addr: Option<String>,
89 impl FromRawFd for net::TcpStream {
90 type Metadata = TcpStreamMetadata;
93 unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> net::TcpStream {
94 let fd = sys::fd::FileDesc::from_inner(fd);
95 let socket = sys::net::Socket::from_inner((fd, metadata.local_addr));
96 net::TcpStream::from_inner(sys::net::TcpStream::from_inner((socket, metadata.peer_addr)))
100 /// Metadata for `TcpListener`.
101 #[derive(Debug, Clone, Default)]
102 #[unstable(feature = "sgx_platform", issue = "56975")]
103 pub struct TcpListenerMetadata {
104 /// Local address of the TCP listener
105 pub local_addr: Option<String>,
108 impl FromRawFd for net::TcpListener {
109 type Metadata = TcpListenerMetadata;
112 unsafe fn from_raw_fd(fd: RawFd, metadata: Self::Metadata) -> net::TcpListener {
113 let fd = sys::fd::FileDesc::from_inner(fd);
114 let socket = sys::net::Socket::from_inner((fd, metadata.local_addr));
115 net::TcpListener::from_inner(sys::net::TcpListener::from_inner(socket))
119 impl TryIntoRawFd for net::TcpStream {
121 fn try_into_raw_fd(self) -> Result<RawFd, Self> {
122 let (socket, peer_addr) = self.into_inner().into_inner();
123 match socket.try_into_inner() {
124 Ok(fd) => Ok(fd.into_inner()),
126 let sys = sys::net::TcpStream::from_inner((socket, peer_addr));
127 Err(net::TcpStream::from_inner(sys))
133 impl TryIntoRawFd for net::TcpListener {
135 fn try_into_raw_fd(self) -> Result<RawFd, Self> {
136 match self.into_inner().into_inner().try_into_inner() {
137 Ok(fd) => Ok(fd.into_inner()),
139 let sys = sys::net::TcpListener::from_inner(socket);
140 Err(net::TcpListener::from_inner(sys))