asupersync 0.3.0

Spec-first, cancel-correct, capability-secure async runtime for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! TCP socket configuration.

use crate::net::tcp::listener::TcpListener;
use crate::net::tcp::stream::TcpStream;
use parking_lot::Mutex;
use std::io;
use std::net::SocketAddr;

/// A TCP socket used for configuring options before connect/listen.
#[derive(Debug)]
pub struct TcpSocket {
    state: Mutex<TcpSocketState>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TcpSocketFamily {
    V4,
    V6,
}

#[derive(Debug)]
struct TcpSocketState {
    family: TcpSocketFamily,
    bound: Option<SocketAddr>,
    reuseaddr: bool,
    reuseport: bool,
}

impl TcpSocket {
    /// Creates a new IPv4 TCP socket.
    #[inline]
    pub fn new_v4() -> io::Result<Self> {
        Ok(Self {
            state: Mutex::new(TcpSocketState {
                family: TcpSocketFamily::V4,
                bound: None,
                reuseaddr: false,
                reuseport: false,
            }),
        })
    }

    /// Creates a new IPv6 TCP socket.
    #[inline]
    pub fn new_v6() -> io::Result<Self> {
        Ok(Self {
            state: Mutex::new(TcpSocketState {
                family: TcpSocketFamily::V6,
                bound: None,
                reuseaddr: false,
                reuseport: false,
            }),
        })
    }

    /// Sets the SO_REUSEADDR option on this socket.
    pub fn set_reuseaddr(&self, reuseaddr: bool) -> io::Result<()> {
        self.state.lock().reuseaddr = reuseaddr;
        Ok(())
    }

    /// Sets the SO_REUSEPORT option on this socket (Unix only).
    #[cfg(unix)]
    pub fn set_reuseport(&self, reuseport: bool) -> io::Result<()> {
        self.state.lock().reuseport = reuseport;
        Ok(())
    }

    /// Binds this socket to the given local address.
    pub fn bind(&self, addr: SocketAddr) -> io::Result<()> {
        {
            let mut state = self.state.lock();
            if !family_matches(state.family, addr) {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "address family does not match socket",
                ));
            }
            if state.bound.is_some() {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "socket is already bound",
                ));
            }
            state.bound = Some(addr);
        }
        Ok(())
    }

    /// Starts listening, returning a TCP listener.
    pub fn listen(self, backlog: u32) -> io::Result<TcpListener> {
        #[cfg(target_arch = "wasm32")]
        {
            let _ = self;
            let _ = backlog;
            Err(super::browser_tcp_unsupported("TcpSocket::listen"))
        }

        #[cfg(not(target_arch = "wasm32"))]
        {
            let state = self.state.into_inner();
            let addr = state.bound.ok_or_else(|| {
                io::Error::new(io::ErrorKind::InvalidInput, "socket is not bound")
            })?;

            let domain = match state.family {
                TcpSocketFamily::V4 => socket2::Domain::IPV4,
                TcpSocketFamily::V6 => socket2::Domain::IPV6,
            };
            let socket =
                socket2::Socket::new(domain, socket2::Type::STREAM, Some(socket2::Protocol::TCP))?;

            if state.reuseaddr {
                socket.set_reuse_address(true)?;
            }

            #[cfg(unix)]
            if state.reuseport {
                socket.set_reuse_port(true)?;
            }

            socket.bind(&socket2::SockAddr::from(addr))?;
            socket.listen(i32::try_from(backlog).unwrap_or(i32::MAX))?;
            socket.set_nonblocking(true)?;

            TcpListener::from_std(socket.into())
        }
    }

    /// Connects this socket, returning a TCP stream.
    pub async fn connect(self, addr: SocketAddr) -> io::Result<TcpStream> {
        #[cfg(target_arch = "wasm32")]
        {
            let _ = self;
            let _ = addr;
            Err(super::browser_tcp_unsupported("TcpSocket::connect"))
        }

        #[cfg(not(target_arch = "wasm32"))]
        {
            let state = self.state.into_inner();

            if !family_matches(state.family, addr) {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "address family does not match socket",
                ));
            }

            let domain = match state.family {
                TcpSocketFamily::V4 => socket2::Domain::IPV4,
                TcpSocketFamily::V6 => socket2::Domain::IPV6,
            };
            let socket =
                socket2::Socket::new(domain, socket2::Type::STREAM, Some(socket2::Protocol::TCP))?;

            if state.reuseaddr {
                socket.set_reuse_address(true)?;
            }

            #[cfg(unix)]
            if state.reuseport {
                socket.set_reuse_port(true)?;
            }

            if let Some(bound) = state.bound {
                socket.bind(&socket2::SockAddr::from(bound))?;
            }

            // Async connect using the configured socket
            TcpStream::connect_from_socket(socket, addr).await
        }
    }
}

fn family_matches(family: TcpSocketFamily, addr: SocketAddr) -> bool {
    match family {
        TcpSocketFamily::V4 => addr.is_ipv4(),
        TcpSocketFamily::V6 => addr.is_ipv6(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::net::{self, Ipv4Addr, Ipv6Addr, SocketAddr};
    use std::time::Duration;

    fn init_test(name: &str) {
        crate::test_utils::init_test_logging();
        crate::test_phase!(name);
    }

    #[test]
    fn test_bind_family_match_v4() {
        init_test("test_bind_family_match_v4");
        let socket = TcpSocket::new_v4().expect("new_v4");
        let addr = SocketAddr::from((Ipv4Addr::LOCALHOST, 0));
        let result = socket.bind(addr);
        crate::assert_with_log!(result.is_ok(), "bind v4", true, result.is_ok());
        crate::test_complete!("test_bind_family_match_v4");
    }

    #[test]
    fn test_bind_family_mismatch() {
        init_test("test_bind_family_mismatch");
        let socket = TcpSocket::new_v4().expect("new_v4");
        let addr = SocketAddr::from((Ipv6Addr::LOCALHOST, 0));
        let err = socket.bind(addr).expect_err("expected mismatch error");
        crate::assert_with_log!(
            err.kind() == io::ErrorKind::InvalidInput,
            "bind mismatch kind",
            io::ErrorKind::InvalidInput,
            err.kind()
        );
        crate::test_complete!("test_bind_family_mismatch");
    }

    #[test]
    fn test_bind_rejects_rebind_and_preserves_original_local_identity() {
        init_test("test_bind_rejects_rebind_and_preserves_original_local_identity");
        let socket = TcpSocket::new_v4().expect("new_v4");
        let first = SocketAddr::from((Ipv4Addr::LOCALHOST, 0));
        let second = SocketAddr::from((Ipv4Addr::UNSPECIFIED, 0));

        socket.bind(first).expect("first bind");
        let err = socket.bind(second).expect_err("second bind should fail");
        crate::assert_with_log!(
            err.kind() == io::ErrorKind::InvalidInput,
            "rebind rejected",
            io::ErrorKind::InvalidInput,
            err.kind()
        );
        crate::assert_with_log!(
            socket.state.lock().bound == Some(first),
            "first bind preserved in socket state",
            Some(first),
            socket.state.lock().bound
        );

        let listener = socket.listen(128).expect("listen after rejected rebind");
        let local = listener.local_addr().expect("listener local_addr");
        crate::assert_with_log!(
            local.ip() == first.ip(),
            "listen uses original local identity",
            first.ip(),
            local.ip()
        );
        crate::test_complete!("test_bind_rejects_rebind_and_preserves_original_local_identity");
    }

    #[test]
    fn test_listen_requires_bind() {
        init_test("test_listen_requires_bind");
        let socket = TcpSocket::new_v4().expect("new_v4");
        let err = socket.listen(128).expect_err("listen without bind");
        crate::assert_with_log!(
            err.kind() == io::ErrorKind::InvalidInput,
            "listen requires bind",
            io::ErrorKind::InvalidInput,
            err.kind()
        );
        crate::test_complete!("test_listen_requires_bind");
    }

    #[test]
    fn test_listen_with_reuseaddr() {
        init_test("test_listen_with_reuseaddr");
        let socket = TcpSocket::new_v4().expect("new_v4");
        socket
            .bind(SocketAddr::from((Ipv4Addr::LOCALHOST, 0)))
            .expect("bind");
        socket.set_reuseaddr(true).expect("set_reuseaddr");
        let listener = socket
            .listen(128)
            .expect("listen with reuseaddr should succeed");
        let addr = listener.local_addr().expect("local_addr");
        crate::assert_with_log!(addr.port() > 0, "bound port", true, addr.port() > 0);
        crate::test_complete!("test_listen_with_reuseaddr");
    }

    #[cfg(unix)]
    #[test]
    fn test_listen_with_reuseport() {
        init_test("test_listen_with_reuseport");
        let socket = TcpSocket::new_v4().expect("new_v4");
        socket
            .bind(SocketAddr::from((Ipv4Addr::LOCALHOST, 0)))
            .expect("bind");
        socket.set_reuseport(true).expect("set_reuseport");
        let listener = socket
            .listen(128)
            .expect("listen with reuseport should succeed");
        let addr = listener.local_addr().expect("local_addr");
        crate::assert_with_log!(addr.port() > 0, "bound port", true, addr.port() > 0);
        crate::test_complete!("test_listen_with_reuseport");
    }

    #[test]
    fn test_listen_success_v4() {
        init_test("test_listen_success_v4");
        let socket = TcpSocket::new_v4().expect("new_v4");
        socket
            .bind(SocketAddr::from((Ipv4Addr::LOCALHOST, 0)))
            .expect("bind");
        let listener = socket.listen(128).expect("listen");
        let local = listener.local_addr().expect("local_addr");
        crate::assert_with_log!(
            local.ip() == Ipv4Addr::LOCALHOST,
            "local addr ip",
            Ipv4Addr::LOCALHOST,
            local.ip()
        );
        crate::assert_with_log!(
            local.port() != 0,
            "local port assigned",
            true,
            local.port() != 0
        );
        crate::test_complete!("test_listen_success_v4");
    }

    #[test]
    fn test_connect_with_bind_success() {
        init_test("test_connect_with_bind_success");
        let listener = net::TcpListener::bind(SocketAddr::from((Ipv4Addr::LOCALHOST, 0)))
            .expect("bind listener");
        let listen_addr = listener.local_addr().expect("local addr");
        let (tx, rx) = std::sync::mpsc::channel();
        let handle = std::thread::spawn(move || {
            let _ = listener.accept().expect("accept");
            let _ = tx.send(());
        });

        futures_lite::future::block_on(async {
            let socket = TcpSocket::new_v4().expect("new_v4");
            // Bind to an ephemeral port
            socket
                .bind(SocketAddr::from((Ipv4Addr::LOCALHOST, 0)))
                .expect("bind");

            let stream = socket.connect(listen_addr).await;
            crate::assert_with_log!(stream.is_ok(), "connect with bind ok", true, stream.is_ok());

            if let Ok(stream) = stream {
                let local = stream.local_addr().expect("local addr");
                // Verify we are indeed bound to local loopback (port will be non-zero)
                crate::assert_with_log!(
                    local.ip() == Ipv4Addr::LOCALHOST,
                    "local ip",
                    Ipv4Addr::LOCALHOST,
                    local.ip()
                );
            }
        });

        let accepted = rx.recv_timeout(Duration::from_secs(1)).is_ok();
        crate::assert_with_log!(accepted, "accepted connection", true, accepted);
        handle.join().expect("join accept thread");
        crate::test_complete!("test_connect_with_bind_success");
    }

    #[test]
    fn test_connect_family_mismatch() {
        init_test("test_connect_family_mismatch");
        futures_lite::future::block_on(async {
            let socket = TcpSocket::new_v4().expect("new_v4");
            let err = socket
                .connect(SocketAddr::from((Ipv6Addr::LOCALHOST, 80)))
                .await
                .expect_err("connect should reject IPv6");
            crate::assert_with_log!(
                err.kind() == io::ErrorKind::InvalidInput,
                "connect family mismatch",
                io::ErrorKind::InvalidInput,
                err.kind()
            );
        });
        crate::test_complete!("test_connect_family_mismatch");
    }

    #[test]
    fn test_connect_success_v4() {
        init_test("test_connect_success_v4");
        let listener = net::TcpListener::bind(SocketAddr::from((Ipv4Addr::LOCALHOST, 0)))
            .expect("bind listener");
        let addr = listener.local_addr().expect("local addr");
        let (tx, rx) = std::sync::mpsc::channel();
        let handle = std::thread::spawn(move || {
            let _ = listener.accept().expect("accept");
            let _ = tx.send(());
        });

        futures_lite::future::block_on(async {
            let stream = TcpSocket::new_v4().expect("new_v4").connect(addr).await;
            crate::assert_with_log!(stream.is_ok(), "connect ok", true, stream.is_ok());
            if let Ok(stream) = stream {
                let peer = stream.peer_addr().expect("peer addr");
                crate::assert_with_log!(peer.ip() == addr.ip(), "peer ip", addr.ip(), peer.ip());
            }
        });

        let accepted = rx.recv_timeout(Duration::from_secs(1)).is_ok();
        crate::assert_with_log!(accepted, "accepted connection", true, accepted);
        handle.join().expect("join accept thread");
        crate::test_complete!("test_connect_success_v4");
    }

    #[test]
    fn test_listen_reuseaddr() {
        init_test("test_listen_reuseaddr");
        let socket = TcpSocket::new_v4().expect("new_v4");
        socket.set_reuseaddr(true).expect("set_reuseaddr");
        socket
            .bind(SocketAddr::from((Ipv4Addr::LOCALHOST, 0)))
            .expect("bind");
        let listener = socket.listen(128);
        crate::assert_with_log!(listener.is_ok(), "listen ok", true, listener.is_ok());
        crate::test_complete!("test_listen_reuseaddr");
    }
}