]> git.lizzy.rs Git - rust.git/blob - library/std/src/os/fortanix_sgx/io.rs
Rollup merge of #91641 - dtolnay:cchar-if, r=Mark-Simulacrum
[rust.git] / library / std / src / 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
7 use crate::net;
8 pub use crate::sys::abi::usercalls::raw::Fd as RawFd;
9 use 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")]
14 pub 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")]
27 pub 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 **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.
37     ///
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;
45 }
46
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.
53     ///
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.
57     ///
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>;
63 }
64
65 impl AsRawFd for net::TcpStream {
66     #[inline]
67     fn as_raw_fd(&self) -> RawFd {
68         *self.as_inner().as_inner().as_inner().as_inner()
69     }
70 }
71
72 impl AsRawFd for net::TcpListener {
73     #[inline]
74     fn as_raw_fd(&self) -> RawFd {
75         *self.as_inner().as_inner().as_inner().as_inner()
76     }
77 }
78
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>,
87 }
88
89 impl FromRawFd for net::TcpStream {
90     type Metadata = TcpStreamMetadata;
91
92     #[inline]
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)))
97     }
98 }
99
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>,
106 }
107
108 impl FromRawFd for net::TcpListener {
109     type Metadata = TcpListenerMetadata;
110
111     #[inline]
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))
116     }
117 }
118
119 impl TryIntoRawFd for net::TcpStream {
120     #[inline]
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()),
125             Err(socket) => {
126                 let sys = sys::net::TcpStream::from_inner((socket, peer_addr));
127                 Err(net::TcpStream::from_inner(sys))
128             }
129         }
130     }
131 }
132
133 impl TryIntoRawFd for net::TcpListener {
134     #[inline]
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()),
138             Err(socket) => {
139                 let sys = sys::net::TcpListener::from_inner(socket);
140                 Err(net::TcpListener::from_inner(sys))
141             }
142         }
143     }
144 }