Skip to main content

tor_rtcompat/impls/
tokio.rs

1//! Re-exports of the tokio runtime for use with arti.
2//!
3//! This crate helps define a slim API around our async runtime so that we
4//! can easily swap it out.
5
6/// Types used for networking (tokio implementation)
7pub(crate) mod net {
8    use crate::{impls, traits};
9    use async_trait::async_trait;
10    #[cfg(unix)]
11    use tor_general_addr::unix;
12
13    pub(crate) use tokio_crate::net::{
14        TcpListener as TokioTcpListener, TcpStream as TokioTcpStream, UdpSocket as TokioUdpSocket,
15    };
16    #[cfg(unix)]
17    pub(crate) use tokio_crate::net::{
18        UnixListener as TokioUnixListener, UnixStream as TokioUnixStream,
19    };
20
21    use futures::io::{AsyncRead, AsyncWrite};
22    use paste::paste;
23    use tokio_util::compat::{Compat, TokioAsyncReadCompatExt as _};
24
25    use std::io::Result as IoResult;
26    use std::net::SocketAddr;
27    use std::pin::Pin;
28    use std::task::{Context, Poll};
29
30    /// Provide a set of network stream wrappers for a single stream type.
31    macro_rules! stream_impl {
32        {
33            $kind:ident,
34            $addr:ty,
35            $cvt_addr:ident
36        } => {paste!{
37            /// Wrapper for Tokio's
38            #[doc = stringify!($kind)]
39            /// streams,
40            /// that implements the standard
41            /// AsyncRead and AsyncWrite.
42            pub struct [<$kind Stream>] {
43                /// Underlying tokio_util::compat::Compat wrapper.
44                s: Compat<[<Tokio $kind Stream>]>,
45            }
46            impl From<[<Tokio $kind Stream>]> for [<$kind Stream>] {
47                fn from(s: [<Tokio $kind Stream>]) ->  [<$kind Stream>] {
48                    let s = s.compat();
49                    [<$kind Stream>] { s }
50                }
51            }
52            impl AsyncRead for  [<$kind Stream>] {
53                fn poll_read(
54                    mut self: Pin<&mut Self>,
55                    cx: &mut Context<'_>,
56                    buf: &mut [u8],
57                ) -> Poll<IoResult<usize>> {
58                    Pin::new(&mut self.s).poll_read(cx, buf)
59                }
60            }
61            impl AsyncWrite for  [<$kind Stream>] {
62                fn poll_write(
63                    mut self: Pin<&mut Self>,
64                    cx: &mut Context<'_>,
65                    buf: &[u8],
66                ) -> Poll<IoResult<usize>> {
67                    Pin::new(&mut self.s).poll_write(cx, buf)
68                }
69                fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<IoResult<()>> {
70                    Pin::new(&mut self.s).poll_flush(cx)
71                }
72                fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<IoResult<()>> {
73                    Pin::new(&mut self.s).poll_close(cx)
74                }
75            }
76
77            /// Wrap a Tokio
78            #[doc = stringify!($kind)]
79            /// Listener to behave as a futures::io::TcpListener.
80            pub struct [<$kind Listener>] {
81                /// The underlying listener.
82                pub(super) lis: [<Tokio $kind Listener>],
83            }
84
85            /// Asynchronous stream that yields incoming connections from a
86            #[doc = stringify!($kind)]
87            /// Listener.
88            ///
89            /// This is analogous to async_std::net::Incoming.
90            pub struct [<Incoming $kind Streams>] {
91                /// Reference to the underlying listener.
92                pub(super) lis: [<Tokio $kind Listener>],
93            }
94
95            impl futures::stream::Stream for [<Incoming $kind Streams>] {
96                type Item = IoResult<([<$kind Stream>], $addr)>;
97
98                fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
99                    match self.lis.poll_accept(cx) {
100                        Poll::Ready(Ok((s, a))) => Poll::Ready(Some(Ok((s.into(), $cvt_addr(a)? )))),
101                        Poll::Ready(Err(e)) => Poll::Ready(Some(Err(e))),
102                        Poll::Pending => Poll::Pending,
103                    }
104                }
105            }
106            impl traits::NetStreamListener<$addr> for [<$kind Listener>] {
107                type Stream = [<$kind Stream>];
108                type Incoming = [<Incoming $kind Streams>];
109                fn incoming(self) -> Self::Incoming {
110                    [<Incoming $kind Streams>] { lis: self.lis }
111                }
112                fn local_addr(&self) -> IoResult<$addr> {
113                    $cvt_addr(self.lis.local_addr()?)
114                }
115            }
116        }}
117    }
118
119    /// Try to convert a tokio `unix::SocketAddr` into a crate::SocketAddr.
120    ///
121    /// Frustratingly, this information is _right there_: Tokio's SocketAddr has a
122    /// std::unix::net::SocketAddr internally, but there appears to be no way to get it out.
123    #[cfg(unix)]
124    #[allow(clippy::needless_pass_by_value)]
125    fn try_cvt_tokio_unix_addr(
126        addr: tokio_crate::net::unix::SocketAddr,
127    ) -> IoResult<unix::SocketAddr> {
128        if addr.is_unnamed() {
129            crate::unix::new_unnamed_socketaddr()
130        } else if let Some(p) = addr.as_pathname() {
131            unix::SocketAddr::from_pathname(p)
132        } else {
133            Err(crate::unix::UnsupportedAfUnixAddressType.into())
134        }
135    }
136
137    /// Wrapper for (not) converting std::net::SocketAddr to itself.
138    #[allow(clippy::unnecessary_wraps)]
139    fn identity_fn_socketaddr(addr: std::net::SocketAddr) -> IoResult<std::net::SocketAddr> {
140        Ok(addr)
141    }
142
143    stream_impl! { Tcp, std::net::SocketAddr, identity_fn_socketaddr }
144    #[cfg(unix)]
145    stream_impl! { Unix, unix::SocketAddr, try_cvt_tokio_unix_addr }
146
147    /// Wrap a Tokio UdpSocket
148    pub struct UdpSocket {
149        /// The underelying UdpSocket
150        socket: TokioUdpSocket,
151    }
152
153    impl UdpSocket {
154        /// Bind a UdpSocket
155        pub async fn bind(addr: SocketAddr) -> IoResult<Self> {
156            TokioUdpSocket::bind(addr)
157                .await
158                .map(|socket| UdpSocket { socket })
159        }
160    }
161
162    #[async_trait]
163    impl traits::UdpSocket for UdpSocket {
164        async fn recv(&self, buf: &mut [u8]) -> IoResult<(usize, SocketAddr)> {
165            self.socket.recv_from(buf).await
166        }
167
168        async fn send(&self, buf: &[u8], target: &SocketAddr) -> IoResult<usize> {
169            self.socket.send_to(buf, target).await
170        }
171
172        fn local_addr(&self) -> IoResult<SocketAddr> {
173            self.socket.local_addr()
174        }
175    }
176
177    impl traits::StreamOps for TcpStream {
178        fn set_tcp_notsent_lowat(&self, notsent_lowat: u32) -> IoResult<()> {
179            impls::streamops::set_tcp_notsent_lowat(&self.s, notsent_lowat)
180        }
181
182        #[cfg(target_os = "linux")]
183        fn new_handle(&self) -> Box<dyn traits::StreamOps + Send + Unpin> {
184            Box::new(impls::streamops::TcpSockFd::from_fd(&self.s))
185        }
186    }
187
188    #[cfg(unix)]
189    impl traits::StreamOps for UnixStream {
190        fn set_tcp_notsent_lowat(&self, _notsent_lowat: u32) -> IoResult<()> {
191            Err(traits::UnsupportedStreamOp::new(
192                "set_tcp_notsent_lowat",
193                "unsupported on Unix streams",
194            )
195            .into())
196        }
197    }
198}
199
200// ==============================
201
202use crate::network::{TcpConnectOptions, TcpListenOptions};
203#[cfg(unix)]
204use crate::network::{UnixConnectOptions, UnixListenOptions};
205use crate::traits::*;
206use async_trait::async_trait;
207use futures::Future;
208use std::any::Any;
209use std::io::Result as IoResult;
210use std::time::Duration;
211#[cfg(unix)]
212use tor_general_addr::unix;
213use tracing::instrument;
214
215impl SleepProvider for TokioRuntimeHandle {
216    type SleepFuture = tokio_crate::time::Sleep;
217    fn sleep(&self, duration: Duration) -> Self::SleepFuture {
218        tokio_crate::time::sleep(duration)
219    }
220}
221
222#[async_trait]
223impl crate::traits::NetStreamProvider for TokioRuntimeHandle {
224    type Stream = net::TcpStream;
225    type Listener = net::TcpListener;
226    type ConnectOptions = TcpConnectOptions;
227    type ListenOptions = TcpListenOptions;
228
229    #[instrument(skip_all, level = "trace")]
230    async fn connect(
231        &self,
232        addr: &std::net::SocketAddr,
233        options: &Self::ConnectOptions,
234    ) -> IoResult<Self::Stream> {
235        // The socket before connect() has been called.
236        let socket = super::tcp_pre_connect(addr, options)?;
237
238        // It might seem a little weird to convert the `socket2::Socket` to a std `TcpStream` before
239        // it's connected, but this is the approach recommended by tokio.
240        //
241        // https://docs.rs/tokio/latest/tokio/net/struct.TcpSocket.html#method.from_std_stream
242        //
243        // > Converts a `std::net::TcpStream` into a `TcpSocket`. The provided socket must not have
244        // > been connected prior to calling this function. This function is typically used together
245        // > with crates such as socket2 to configure socket options that are not available on
246        // > `TcpSocket`.
247        //
248        // The socket will already be non-blocking.
249        let socket = std::net::TcpStream::from(socket);
250        let socket = tokio_crate::net::TcpSocket::from_std_stream(socket);
251
252        // Let tokio handle the connection.
253        let socket = socket.connect(*addr).await?;
254
255        Ok(socket.into())
256    }
257    async fn listen(
258        &self,
259        addr: &std::net::SocketAddr,
260        options: &Self::ListenOptions,
261    ) -> IoResult<Self::Listener> {
262        // Use an implementation that's the same across all runtimes.
263        let lis = net::TokioTcpListener::from_std(super::tcp_listen(addr, options)?)?;
264
265        Ok(net::TcpListener { lis })
266    }
267}
268
269#[cfg(unix)]
270#[async_trait]
271impl crate::traits::NetStreamProvider<unix::SocketAddr> for TokioRuntimeHandle {
272    type Stream = net::UnixStream;
273    type Listener = net::UnixListener;
274    type ConnectOptions = UnixConnectOptions;
275    type ListenOptions = UnixListenOptions;
276
277    #[instrument(skip_all, level = "trace")]
278    async fn connect(
279        &self,
280        addr: &unix::SocketAddr,
281        options: &Self::ConnectOptions,
282    ) -> IoResult<Self::Stream> {
283        // Will fail to compile if we add options without handling them here.
284        let UnixConnectOptions {} = options;
285
286        let path = addr
287            .as_pathname()
288            .ok_or(crate::unix::UnsupportedAfUnixAddressType)?;
289        let s = net::TokioUnixStream::connect(path).await?;
290        Ok(s.into())
291    }
292    async fn listen(
293        &self,
294        addr: &unix::SocketAddr,
295        options: &Self::ListenOptions,
296    ) -> IoResult<Self::Listener> {
297        // Will fail to compile if we add options without handling them here.
298        let UnixListenOptions {} = options;
299
300        let path = addr
301            .as_pathname()
302            .ok_or(crate::unix::UnsupportedAfUnixAddressType)?;
303        let lis = net::TokioUnixListener::bind(path)?;
304        Ok(net::UnixListener { lis })
305    }
306}
307
308#[cfg(not(unix))]
309crate::impls::impl_unix_non_provider! { TokioRuntimeHandle }
310
311#[async_trait]
312impl crate::traits::UdpProvider for TokioRuntimeHandle {
313    type UdpSocket = net::UdpSocket;
314
315    async fn bind(&self, addr: &std::net::SocketAddr) -> IoResult<Self::UdpSocket> {
316        net::UdpSocket::bind(*addr).await
317    }
318}
319
320/// Create and return a new Tokio multithreaded runtime.
321pub(crate) fn create_runtime() -> IoResult<TokioRuntimeHandle> {
322    let runtime = async_executors::exec::TokioTp::new().map_err(std::io::Error::other)?;
323    Ok(runtime.into())
324}
325
326/// Wrapper around a Handle to a tokio runtime.
327///
328/// Ideally, this type would go away, and we would just use
329/// `tokio::runtime::Handle` directly.  Unfortunately, we can't implement
330/// `futures::Spawn` on it ourselves because of Rust's orphan rules, so we need
331/// to define a new type here.
332///
333/// # Limitations
334///
335/// Note that Arti requires that the runtime should have working implementations
336/// for Tokio's time, net, and io facilities, but we have no good way to check
337/// that when creating this object.
338#[derive(Clone, Debug)]
339pub struct TokioRuntimeHandle {
340    /// If present, the tokio executor that we've created (and which we own).
341    ///
342    /// We never access this directly; only through `handle`.  We keep it here
343    /// so that our Runtime types can be agnostic about whether they own the
344    /// executor.
345    owned: Option<async_executors::TokioTp>,
346    /// The underlying Handle.
347    handle: tokio_crate::runtime::Handle,
348}
349
350impl TokioRuntimeHandle {
351    /// Wrap a tokio runtime handle into a format that Arti can use.
352    ///
353    /// # Limitations
354    ///
355    /// Note that Arti requires that the runtime should have working
356    /// implementations for Tokio's time, net, and io facilities, but we have
357    /// no good way to check that when creating this object.
358    pub(crate) fn new(handle: tokio_crate::runtime::Handle) -> Self {
359        handle.into()
360    }
361
362    /// Return true if this handle owns the executor that it points to.
363    pub fn is_owned(&self) -> bool {
364        self.owned.is_some()
365    }
366}
367
368impl From<tokio_crate::runtime::Handle> for TokioRuntimeHandle {
369    fn from(handle: tokio_crate::runtime::Handle) -> Self {
370        Self {
371            owned: None,
372            handle,
373        }
374    }
375}
376
377impl From<async_executors::TokioTp> for TokioRuntimeHandle {
378    fn from(owner: async_executors::TokioTp) -> TokioRuntimeHandle {
379        let handle = owner.block_on(async { tokio_crate::runtime::Handle::current() });
380        Self {
381            owned: Some(owner),
382            handle,
383        }
384    }
385}
386
387impl ToplevelBlockOn for TokioRuntimeHandle {
388    #[track_caller]
389    fn block_on<F: Future>(&self, f: F) -> F::Output {
390        self.handle.block_on(f)
391    }
392}
393
394impl Blocking for TokioRuntimeHandle {
395    type ThreadHandle<T: Send + 'static> = async_executors::BlockingHandle<T>;
396
397    #[track_caller]
398    fn spawn_blocking<F, T>(&self, f: F) -> async_executors::BlockingHandle<T>
399    where
400        F: FnOnce() -> T + Send + 'static,
401        T: Send + 'static,
402    {
403        async_executors::BlockingHandle::tokio(self.handle.spawn_blocking(f))
404    }
405
406    #[track_caller]
407    fn reenter_block_on<F: Future>(&self, future: F) -> F::Output {
408        self.handle.block_on(future)
409    }
410
411    #[track_caller]
412    fn blocking_io<F, T>(&self, f: F) -> impl Future<Output = T>
413    where
414        F: FnOnce() -> T + Send + 'static,
415        T: Send + 'static,
416    {
417        let r = tokio_crate::task::block_in_place(f);
418        std::future::ready(r)
419    }
420}
421
422impl futures::task::Spawn for TokioRuntimeHandle {
423    #[track_caller]
424    fn spawn_obj(
425        &self,
426        future: futures::task::FutureObj<'static, ()>,
427    ) -> Result<(), futures::task::SpawnError> {
428        let join_handle = self.handle.spawn(future);
429        drop(join_handle); // this makes the task detached.
430        Ok(())
431    }
432}
433
434#[async_trait]
435impl<T: Send + 'static> JoinSet<T> for tokio_crate::task::JoinSet<T> {
436    fn spawn<F>(&mut self, task: F) -> Result<(), futures::task::SpawnError>
437    where
438        F: Future<Output = T> + Send + 'static,
439    {
440        let _ = self.spawn(task);
441        Ok(())
442    }
443
444    async fn join_next(&mut self) -> Option<Result<T, Box<dyn Any + Send>>> {
445        Some(self.join_next().await?.map_err(|e| {
446            // Note: this will panic if the error was not caused
447            // by the task panicking (i.e. if the task was cancelled).
448            // In practice, however, our API doesn't expose any way
449            // of cancelling the tasks added to a JoinSet,
450            // so this can never panic.
451            //
452            // If we ever *do* implement cancellation facilities,
453            // we need to avoid calling into_panic() without checking
454            // if e.is_panic() first.
455            e.into_panic()
456        }))
457    }
458}
459
460impl JoinSetProvider for TokioRuntimeHandle {
461    type JoinSet<T: Send + Sync + 'static> = tokio_crate::task::JoinSet<T>;
462
463    fn new_join_set<T>(&self) -> Self::JoinSet<T>
464    where
465        T: Send + Sync + 'static,
466    {
467        tokio_crate::task::JoinSet::new()
468    }
469}