tor_rtcompat/
impls.rs

1//! Different implementations of a common async API for use in arti
2//!
3//! Currently only async_std, tokio and smol are provided.
4
5#[cfg(feature = "async-std")]
6pub(crate) mod async_std;
7
8#[cfg(feature = "tokio")]
9pub(crate) mod tokio;
10
11#[cfg(feature = "smol")]
12pub(crate) mod smol;
13
14#[cfg(feature = "rustls")]
15pub(crate) mod rustls;
16
17#[cfg(feature = "native-tls")]
18pub(crate) mod native_tls;
19
20pub(crate) mod streamops;
21
22use tor_error::warn_report;
23
24/// Connection backlog size to use for `listen()` calls on IP sockets.
25//
26// How this was chosen:
27//
28// 1. The rust standard library uses a backlog of 128 for TCP sockets. This matches `SOMAXCONN` on
29//    most systems.
30//
31// 2. Mio (backend for tokio) previously used 1024. But they recently (confusingly) copied the logic
32//    from the standard library's unix socket implementation, which uses different values on
33//    different platforms. These values were tuned for unix sockets, so I think we should ignore
34//    them and mio's implementation here.
35//    https://github.com/tokio-rs/mio/pull/1896
36//
37// 3. Tor first tries using `INT_MAX`, and if that fails falls back to `SOMAXCONN` (using a global
38//    to remember if it did the fallback for future listen() calls; see `tor_listen`).
39//
40// 4. On supported platforms, if you use a backlog that is too large, the system will supposedly
41//    silently cap the value instead of failing.
42//
43//     Linux:
44//     listen(2)
45//     > If the backlog argument is greater than the value in /proc/sys/net/core/somaxconn, then it
46//     > is silently capped to that value.
47//
48//     FreeBSD:
49//     listen(2)
50//     > The sysctl(3) MIB variable kern.ipc.soacceptqueue specifies a hard limit on backlog; if a
51//     > value greater than kern.ipc.soacceptqueue or less than zero is specified, backlog is
52//     > silently forced to kern.ipc.soacceptqueue.
53//
54//     OpenBSD:
55//     listen(2)
56//     > [BUGS] The backlog is currently limited (silently) to the value of the kern.somaxconn
57//     > sysctl, which defaults to 128.
58//
59//     Windows:
60//     https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-listen
61//     > The backlog parameter is limited (silently) to a reasonable value as determined by the
62//     > underlying service provider. Illegal values are replaced by the nearest legal value.
63//
64//     Mac OS:
65//     Archived listen(2) docs
66//     https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/listen.2.html
67//     > [BUGS] The backlog is currently limited (silently) to 128.
68//
69// 5. While the rust APIs take a `u32`, the libc API uses `int`. So we shouldn't use a value larger
70//    than `c_int::MAX`.
71//
72// 6. We should be careful not to set this too large, as supposedly some systems will truncate this to
73//    16 bits. So for example a value of `65536` would cause a backlog of 1. But maybe they are just
74//    referring to systems where `int` is 16 bits?
75//    https://bugs.python.org/issue38699#msg357957
76//
77// Here we use `u16::MAX`. We assume that this will succeed on all supported platforms. Unlike tor,
78// we do not try again with a smaller value since this doesn't seem to be needed on modern systems.
79// We can add it if we find that it's needed.
80//
81// A value of `u16::MAX` is arguably too high, since a smaller value like 4096 would be large enough
82// for legitimate traffic, and illegitimate traffic would be better handled by the kernel with
83// something like SYN cookies. But it's easier for users to reduce the max using
84// `/proc/sys/net/core/somaxconn` than to increase this max by recompiling arti.
85const LISTEN_BACKLOG: i32 = u16::MAX as i32;
86
87/// Open a listening TCP socket.
88///
89/// The socket will be non-blocking, and the socket handle will be close-on-exec/non-inheritable.
90/// Other socket options may also be set depending on the socket type and platform.
91///
92/// Historically we relied on the runtime to create a listening socket, but we need some specific
93/// socket options set, and not all runtimes will behave the same. It's better for us to create the
94/// socket with the options we need and with consistent behaviour across all runtimes. For example
95/// if each runtime were using a different `listen()` backlog size, it might be difficult to debug
96/// related issues.
97pub(crate) fn tcp_listen(addr: &std::net::SocketAddr) -> std::io::Result<std::net::TcpListener> {
98    use socket2::{Domain, Socket, Type};
99
100    // `socket2::Socket::new()`:
101    // > This function corresponds to `socket(2)` on Unix and `WSASocketW` on Windows.
102    // >
103    // > On Unix-like systems, the close-on-exec flag is set on the new socket. Additionally, on
104    // > Apple platforms `SOCK_NOSIGPIPE` is set. On Windows, the socket is made non-inheritable.
105    let socket = match addr {
106        std::net::SocketAddr::V4(_) => Socket::new(Domain::IPV4, Type::STREAM, None)?,
107        std::net::SocketAddr::V6(_) => {
108            let socket = Socket::new(Domain::IPV6, Type::STREAM, None)?;
109
110            // On `cfg(unix)` systems, set `IPV6_V6ONLY` so that we can bind AF_INET and
111            // AF_INET6 sockets to the same port.
112            // This is `cfg(unix)` as I'm not sure what the socket option does (if anything) on
113            // non-unix platforms.
114            #[cfg(unix)]
115            if let Err(e) = socket.set_only_v6(true) {
116                // If we see this, we should exclude more platforms.
117                warn_report!(
118                    e,
119                    "Failed to set `IPV6_V6ONLY` on `AF_INET6` socket. \
120                    Please report this bug at https://gitlab.torproject.org/tpo/core/arti/-/issues",
121                );
122            }
123
124            socket
125        }
126    };
127
128    // Below we try to match what a `tokio::net::TcpListener::bind()` would do. This is a bit tricky
129    // since tokio documents "Calling TcpListener::bind("127.0.0.1:8080") is equivalent to:" with
130    // some provided example code, but this logic actually appears to happen in the mio crate, and
131    // doesn't match exactly with tokio's documentation. So here we acknowledge that we likely do
132    // deviate from `tokio::net::TcpListener::bind()` a bit.
133
134    socket.set_nonblocking(true)?;
135
136    // The docs for `tokio::net::TcpSocket` say:
137    //
138    // > // On platforms with Berkeley-derived sockets, this allows to quickly
139    // > // rebind a socket, without needing to wait for the OS to clean up the
140    // > // previous one.
141    // >
142    // > // On Windows, this allows rebinding sockets which are actively in use,
143    // > // which allows "socket hijacking", so we explicitly don't set it here.
144    // > // https://docs.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse
145    //
146    // This appears to be a comment that tokio copied from mio.
147    //
148    // So here we only set SO_REUSEADDR for `cfg(unix)` to match tokio.
149    #[cfg(unix)]
150    socket.set_reuse_address(true)?;
151
152    socket.bind(&(*addr).into())?;
153
154    socket.listen(LISTEN_BACKLOG)?;
155
156    Ok(socket.into())
157}
158
159/// Helper: Implement an unreachable NetProvider<unix::SocketAddr> for a given runtime.
160#[cfg(not(unix))]
161macro_rules! impl_unix_non_provider {
162    { $for_type:ty } => {
163
164        #[async_trait]
165        impl crate::traits::NetStreamProvider<tor_general_addr::unix::SocketAddr> for $for_type {
166            type Stream = crate::unimpl::FakeStream;
167            type Listener = crate::unimpl::FakeListener<tor_general_addr::unix::SocketAddr>;
168            async fn connect(&self, _a: &tor_general_addr::unix::SocketAddr) -> IoResult<Self::Stream> {
169                Err(tor_general_addr::unix::NoAfUnixSocketSupport::default().into())
170
171            }
172            async fn listen(&self, _a: &tor_general_addr::unix::SocketAddr) -> IoResult<Self::Listener> {
173                Err(tor_general_addr::unix::NoAfUnixSocketSupport::default().into())
174            }
175        }
176    }
177}
178#[cfg(not(unix))]
179pub(crate) use impl_unix_non_provider;