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")]
6
pub(crate) mod async_std;
7

            
8
#[cfg(feature = "tokio")]
9
pub(crate) mod tokio;
10

            
11
#[cfg(feature = "smol")]
12
pub(crate) mod smol;
13

            
14
#[cfg(feature = "rustls")]
15
pub(crate) mod rustls;
16

            
17
#[cfg(feature = "native-tls")]
18
pub(crate) mod native_tls;
19

            
20
pub(crate) mod streamops;
21
pub(crate) mod unimpl_tls;
22

            
23
use tor_error::warn_report;
24

            
25
/// Connection backlog size to use for `listen()` calls on IP sockets.
26
//
27
// How this was chosen:
28
//
29
// 1. The rust standard library uses a backlog of 128 for TCP sockets. This matches `SOMAXCONN` on
30
//    most systems.
31
//
32
// 2. Mio (backend for tokio) previously used 1024. But they recently (confusingly) copied the logic
33
//    from the standard library's unix socket implementation, which uses different values on
34
//    different platforms. These values were tuned for unix sockets, so I think we should ignore
35
//    them and mio's implementation here.
36
//    https://github.com/tokio-rs/mio/pull/1896
37
//
38
// 3. Tor first tries using `INT_MAX`, and if that fails falls back to `SOMAXCONN` (using a global
39
//    to remember if it did the fallback for future listen() calls; see `tor_listen`).
40
//
41
// 4. On supported platforms, if you use a backlog that is too large, the system will supposedly
42
//    silently cap the value instead of failing.
43
//
44
//     Linux:
45
//     listen(2)
46
//     > If the backlog argument is greater than the value in /proc/sys/net/core/somaxconn, then it
47
//     > is silently capped to that value.
48
//
49
//     FreeBSD:
50
//     listen(2)
51
//     > The sysctl(3) MIB variable kern.ipc.soacceptqueue specifies a hard limit on backlog; if a
52
//     > value greater than kern.ipc.soacceptqueue or less than zero is specified, backlog is
53
//     > silently forced to kern.ipc.soacceptqueue.
54
//
55
//     OpenBSD:
56
//     listen(2)
57
//     > [BUGS] The backlog is currently limited (silently) to the value of the kern.somaxconn
58
//     > sysctl, which defaults to 128.
59
//
60
//     Windows:
61
//     https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-listen
62
//     > The backlog parameter is limited (silently) to a reasonable value as determined by the
63
//     > underlying service provider. Illegal values are replaced by the nearest legal value.
64
//
65
//     Mac OS:
66
//     Archived listen(2) docs
67
//     https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/listen.2.html
68
//     > [BUGS] The backlog is currently limited (silently) to 128.
69
//
70
// 5. While the rust APIs take a `u32`, the libc API uses `int`. So we shouldn't use a value larger
71
//    than `c_int::MAX`.
72
//
73
// 6. We should be careful not to set this too large, as supposedly some systems will truncate this to
74
//    16 bits. So for example a value of `65536` would cause a backlog of 1. But maybe they are just
75
//    referring to systems where `int` is 16 bits?
76
//    https://bugs.python.org/issue38699#msg357957
77
//
78
// Here we use `u16::MAX`. We assume that this will succeed on all supported platforms. Unlike tor,
79
// we do not try again with a smaller value since this doesn't seem to be needed on modern systems.
80
// We can add it if we find that it's needed.
81
//
82
// A value of `u16::MAX` is arguably too high, since a smaller value like 4096 would be large enough
83
// for legitimate traffic, and illegitimate traffic would be better handled by the kernel with
84
// something like SYN cookies. But it's easier for users to reduce the max using
85
// `/proc/sys/net/core/somaxconn` than to increase this max by recompiling arti.
86
const LISTEN_BACKLOG: i32 = u16::MAX as i32;
87

            
88
/// Open a listening TCP socket.
89
///
90
/// The socket will be non-blocking, and the socket handle will be close-on-exec/non-inheritable.
91
/// Other socket options may also be set depending on the socket type and platform.
92
///
93
/// Historically we relied on the runtime to create a listening socket, but we need some specific
94
/// socket options set, and not all runtimes will behave the same. It's better for us to create the
95
/// socket with the options we need and with consistent behaviour across all runtimes. For example
96
/// if each runtime were using a different `listen()` backlog size, it might be difficult to debug
97
/// related issues.
98
22
pub(crate) fn tcp_listen(addr: &std::net::SocketAddr) -> std::io::Result<std::net::TcpListener> {
99
    use socket2::{Domain, Socket, Type};
100

            
101
    // `socket2::Socket::new()`:
102
    // > This function corresponds to `socket(2)` on Unix and `WSASocketW` on Windows.
103
    // >
104
    // > On Unix-like systems, the close-on-exec flag is set on the new socket. Additionally, on
105
    // > Apple platforms `SOCK_NOSIGPIPE` is set. On Windows, the socket is made non-inheritable.
106
22
    let socket = match addr {
107
22
        std::net::SocketAddr::V4(_) => Socket::new(Domain::IPV4, Type::STREAM, None)?,
108
        std::net::SocketAddr::V6(_) => {
109
            let socket = Socket::new(Domain::IPV6, Type::STREAM, None)?;
110

            
111
            // On `cfg(unix)` systems, set `IPV6_V6ONLY` so that we can bind AF_INET and
112
            // AF_INET6 sockets to the same port.
113
            // This is `cfg(unix)` as I'm not sure what the socket option does (if anything) on
114
            // non-unix platforms.
115
            #[cfg(unix)]
116
            if let Err(e) = socket.set_only_v6(true) {
117
                // If we see this, we should exclude more platforms.
118
                warn_report!(
119
                    e,
120
                    "Failed to set `IPV6_V6ONLY` on `AF_INET6` socket. \
121
                    Please report this bug at https://gitlab.torproject.org/tpo/core/arti/-/issues",
122
                );
123
            }
124

            
125
            socket
126
        }
127
    };
128

            
129
    // Below we try to match what a `tokio::net::TcpListener::bind()` would do. This is a bit tricky
130
    // since tokio documents "Calling TcpListener::bind("127.0.0.1:8080") is equivalent to:" with
131
    // some provided example code, but this logic actually appears to happen in the mio crate, and
132
    // doesn't match exactly with tokio's documentation. So here we acknowledge that we likely do
133
    // deviate from `tokio::net::TcpListener::bind()` a bit.
134

            
135
22
    socket.set_nonblocking(true)?;
136

            
137
    // The docs for `tokio::net::TcpSocket` say:
138
    //
139
    // > // On platforms with Berkeley-derived sockets, this allows to quickly
140
    // > // rebind a socket, without needing to wait for the OS to clean up the
141
    // > // previous one.
142
    // >
143
    // > // On Windows, this allows rebinding sockets which are actively in use,
144
    // > // which allows "socket hijacking", so we explicitly don't set it here.
145
    // > // https://docs.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse
146
    //
147
    // This appears to be a comment that tokio copied from mio.
148
    //
149
    // So here we only set SO_REUSEADDR for `cfg(unix)` to match tokio.
150
    #[cfg(unix)]
151
22
    socket.set_reuse_address(true)?;
152

            
153
22
    socket.bind(&(*addr).into())?;
154

            
155
22
    socket.listen(LISTEN_BACKLOG)?;
156

            
157
22
    Ok(socket.into())
158
22
}
159

            
160
/// Helper: Implement an unreachable NetProvider<unix::SocketAddr> for a given runtime.
161
#[cfg(not(unix))]
162
macro_rules! impl_unix_non_provider {
163
    { $for_type:ty } => {
164

            
165
        #[async_trait]
166
        impl crate::traits::NetStreamProvider<tor_general_addr::unix::SocketAddr> for $for_type {
167
            type Stream = crate::unimpl::FakeStream;
168
            type Listener = crate::unimpl::FakeListener<tor_general_addr::unix::SocketAddr>;
169
            async fn connect(&self, _a: &tor_general_addr::unix::SocketAddr) -> IoResult<Self::Stream> {
170
                Err(tor_general_addr::unix::NoAfUnixSocketSupport::default().into())
171

            
172
            }
173
            async fn listen(&self, _a: &tor_general_addr::unix::SocketAddr) -> IoResult<Self::Listener> {
174
                Err(tor_general_addr::unix::NoAfUnixSocketSupport::default().into())
175
            }
176
        }
177
    }
178
}
179
#[cfg(not(unix))]
180
pub(crate) use impl_unix_non_provider;