atap 0.1.0

Threadsafe futureless async runtime for macOS
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! # UDP task
//! The tasks `Udp::bind` and a `UdpSocket` return, and
//! everything they do once run

use crate::modules::input::Token;
use crate::{
    RuntimeError,
    constants::INLINE_PAYLOAD,
    futures::{
        net::{
            address::{Target, family, from_raw, local_of, to_raw},
            datagram::{recv_datagram, send_datagram},
            socket::{Options, open},
            step::{Progress, settle, wait_on},
        },
        task::{
            Nothing, Task,
            sealed::{self, Step},
        },
        udp::udp_socket::UdpSocket,
    },
    modules::{int_check::IntCheck, park},
};
use std::{mem, net::SocketAddr, ptr, sync::Arc};

// Anything larger costs a page mapping per task
const _: () = assert!(mem::size_of::<Result<UdpSocket, RuntimeError>>() <= INLINE_PAYLOAD);
const _: () =
    assert!(mem::size_of::<Result<(Vec<u8>, SocketAddr), RuntimeError>>() <= INLINE_PAYLOAD);

/// Opens a UDP socket
///
/// ## Returns
/// The socket, bound to the first address that takes
#[derive(Debug, Clone)]
#[must_use = "a task does nothing until it is run or spawned"]
pub struct BindTask {
    /// Where to bind
    target: Target,

    /// What the socket is set up with
    options: Options,
}

impl BindTask {
    /// Binds to `target`
    pub(crate) fn new(target: Target) -> Self {
        Self {
            target,
            options: Options::default(),
        }
    }

    /// Lets the socket send to a broadcast address
    ///
    /// ## Returns
    /// The task. Calling it twice keeps the last
    pub fn broadcast(mut self, broadcast: bool) -> Self {
        self.options.broadcast = broadcast;
        self
    }

    /// Lets other sockets bind the same port, each set up the same
    /// way, so all of them hear multicast traffic to it
    ///
    /// ## Returns
    /// The task. Calling it twice keeps the last
    pub fn reuse_port(mut self, reuse: bool) -> Self {
        self.options.reuse_port = reuse;
        self
    }

    /// Takes only IPv6 traffic on an IPv6 address
    ///
    /// ## Returns
    /// The task. Calling it twice keeps the last
    pub fn v6_only(mut self, only: bool) -> Self {
        self.options.v6_only = only;
        self
    }

    /// Binds
    fn bind(&self) -> Result<UdpSocket, RuntimeError> {
        let found = self.target.resolve()?;

        let mut failure = RuntimeError::BadAddress;

        for addr in found {
            match bind_one(&addr, &self.options) {
                Ok(socket) => return Ok(socket),
                Err(error) => failure = error,
            }
        }

        Err(failure)
    }
}

/// Binds a fresh socket to `addr`
fn bind_one(addr: &SocketAddr, options: &Options) -> Result<UdpSocket, RuntimeError> {
    let fd = open(family(addr), libc::SOCK_DGRAM)?;

    options.apply(fd.raw(), addr.is_ipv6())?;
    let (raw, len) = to_raw(addr);

    unsafe {
        libc::bind(
            fd.raw(),
            (&raw as *const libc::sockaddr_storage).cast::<libc::sockaddr>(),
            len,
        )
    }
    .check()?;

    // Port 0 was a free one picked by the kernel, so this is the
    // only way to know which
    let local = local_of(fd.raw())?;

    Ok(UdpSocket::new(fd, local))
}

/// Sends one datagram
///
/// ## Returns
/// The number of bytes sent, which is all of them
#[derive(Debug, Clone)]
#[must_use = "a task does nothing until it is run or spawned"]
pub struct SendToTask {
    /// What to send from
    socket: UdpSocket,

    /// Where to send
    target: Target,

    /// What to send
    data: Arc<[u8]>,

    /// Where this run is sending, once it has been looked up
    dest: Progress<Option<SocketAddr>>,
}

impl SendToTask {
    /// Sends `data` from `socket` to `target`
    pub(crate) fn new(socket: UdpSocket, target: Target, data: Arc<[u8]>) -> Self {
        Self {
            socket,
            target,
            data,
            dest: Progress::default(),
        }
    }

    /// Sends, if the socket has room
    fn advance(&mut self) -> Result<Step<Result<usize, RuntimeError>>, RuntimeError> {
        let dest = match self.dest.0 {
            Some(dest) => dest,
            None => {
                let dest = pick(&self.target, self.socket.local_addr())?;
                self.dest.0 = Some(dest);

                dest
            }
        };

        let fd = self.socket.fd();
        let (raw, len) = to_raw(&dest);

        let sent = send_datagram(
            fd,
            &self.data,
            (&raw as *const libc::sockaddr_storage).cast::<libc::sockaddr>(),
            len,
        )?;

        match sent {
            Some(sent) => Ok(Step::Done(Ok(sent))),
            None => wait_on(fd, libc::EVFILT_WRITE),
        }
    }
}

/// The first address `target` names that a socket bound to
/// `local` can send to
///
/// ## Returns
/// `BadAddress` when none of them are in the socket's family
fn pick(target: &Target, local: SocketAddr) -> Result<SocketAddr, RuntimeError> {
    target
        .resolve()?
        .into_iter()
        .find(|addr| addr.is_ipv4() == local.is_ipv4())
        .ok_or(RuntimeError::BadAddress)
}

/// Receives one datagram
///
/// ## Returns
/// The whole datagram, and the address it came from
#[derive(Debug, Clone)]
#[must_use = "a task does nothing until it is run or spawned"]
pub struct RecvFromTask {
    /// Where to receive
    socket: UdpSocket,

    /// Whether the datagram is left for the next receive
    peek: bool,
}

impl RecvFromTask {
    /// Receives on `socket`
    pub(crate) fn new(socket: UdpSocket) -> Self {
        Self {
            socket,
            peek: false,
        }
    }

    /// Looks at the next datagram without taking it, so the next
    /// receive gets it again
    ///
    /// ## Returns
    /// The task
    pub fn peek(mut self) -> Self {
        self.peek = true;
        self
    }

    /// Takes a datagram if one is waiting
    fn advance(
        &mut self,
    ) -> Result<Step<Result<(Vec<u8>, SocketAddr), RuntimeError>>, RuntimeError> {
        let fd = self.socket.fd();

        match recv_datagram(fd, self.peek)? {
            Some((data, storage, _)) => {
                let from = from_raw(&storage).ok_or(RuntimeError::BadAddress)?;

                Ok(Step::Done(Ok((data, from))))
            }

            None => wait_on(fd, libc::EVFILT_READ),
        }
    }
}

/// Fixes the one address a socket sends to and hears from
///
/// ## Returns
/// Nothing, once the kernel has taken the address
#[derive(Debug, Clone)]
#[must_use = "a task does nothing until it is run or spawned"]
pub struct UdpConnectTask {
    /// The socket to fix
    socket: UdpSocket,

    /// Where to
    target: Target,
}

impl UdpConnectTask {
    pub(crate) fn new(socket: UdpSocket, target: Target) -> Self {
        Self { socket, target }
    }

    fn connect(&self) -> Result<(), RuntimeError> {
        let peer = pick(&self.target, self.socket.local_addr())?;
        let (raw, len) = to_raw(&peer);

        loop {
            let done = unsafe {
                libc::connect(
                    self.socket.fd(),
                    (&raw as *const libc::sockaddr_storage).cast::<libc::sockaddr>(),
                    len,
                )
            }
            .check();

            match done {
                Ok(_) => return Ok(()),
                Err(RuntimeError::CheckError(Some(libc::EINTR))) => {}
                Err(error) => return Err(error),
            }
        }
    }
}

/// Sends one datagram to the address a socket is connected to
///
/// ## Returns
/// The number of bytes sent, which is all of them
#[derive(Debug, Clone)]
#[must_use = "a task does nothing until it is run or spawned"]
pub struct UdpSendTask {
    socket: UdpSocket,
    data: Arc<[u8]>,
}

impl UdpSendTask {
    pub(crate) fn new(socket: UdpSocket, data: Arc<[u8]>) -> Self {
        Self { socket, data }
    }

    fn advance(&mut self) -> Result<Step<Result<usize, RuntimeError>>, RuntimeError> {
        let fd = self.socket.fd();

        match send_datagram(fd, &self.data, ptr::null(), 0)? {
            Some(sent) => Ok(Step::Done(Ok(sent))),
            None => wait_on(fd, libc::EVFILT_WRITE),
        }
    }
}

/// Receives one datagram from the address a socket is connected to
///
/// ## Returns
/// The whole datagram
#[derive(Debug, Clone)]
#[must_use = "a task does nothing until it is run or spawned"]
pub struct UdpRecvTask {
    socket: UdpSocket,

    /// Whether the datagram is left for the next receive
    peek: bool,
}

impl UdpRecvTask {
    pub(crate) fn new(socket: UdpSocket) -> Self {
        Self {
            socket,
            peek: false,
        }
    }

    /// Looks at the next datagram without taking it, so the next
    /// receive gets it again
    ///
    /// ## Returns
    /// The task
    pub fn peek(mut self) -> Self {
        self.peek = true;
        self
    }

    fn advance(&mut self) -> Result<Step<Result<Vec<u8>, RuntimeError>>, RuntimeError> {
        let fd = self.socket.fd();

        match recv_datagram(fd, self.peek)? {
            Some((data, _, _)) => Ok(Step::Done(Ok(data))),
            None => wait_on(fd, libc::EVFILT_READ),
        }
    }
}

impl sealed::Sealed for UdpConnectTask {}
impl sealed::Sealed for UdpSendTask {}
impl sealed::Sealed for UdpRecvTask {}

impl Task for UdpConnectTask {
    type Output = Result<(), RuntimeError>;
    type Input = Nothing;

    fn execute(&self, _token: Token, _reactor_id: i32, _task_id: usize) -> Self::Output {
        self.connect()
    }

    /// A name lookup blocks, so only a literal address keeps it on
    /// a worker
    fn blocking(&self, _token: Token) -> bool {
        self.target.needs_lookup()
    }
}

impl Task for UdpSendTask {
    type Output = Result<usize, RuntimeError>;
    type Input = Nothing;

    /// Waits on this thread, for `Runtime::block`
    fn execute(&self, _token: Token, reactor_id: i32, task_id: usize) -> Self::Output {
        park::drive(self.clone(), reactor_id, task_id)
    }

    fn step(&mut self, _token: Token, _reactor_id: i32, _task_id: usize) -> Step<Self::Output> {
        settle(self.advance())
    }
}

impl Task for UdpRecvTask {
    type Output = Result<Vec<u8>, RuntimeError>;
    type Input = Nothing;

    /// Waits on this thread, for `Runtime::block`
    fn execute(&self, _token: Token, reactor_id: i32, task_id: usize) -> Self::Output {
        park::drive(self.clone(), reactor_id, task_id)
    }

    fn step(&mut self, _token: Token, _reactor_id: i32, _task_id: usize) -> Step<Self::Output> {
        settle(self.advance())
    }
}

impl sealed::Sealed for BindTask {}
impl sealed::Sealed for SendToTask {}
impl sealed::Sealed for RecvFromTask {}

impl Task for BindTask {
    type Output = Result<UdpSocket, RuntimeError>;
    type Input = Nothing;

    /// Never waits on the socket, so this is the whole task
    fn execute(&self, _token: Token, _reactor_id: i32, _task_id: usize) -> Self::Output {
        self.bind()
    }

    /// A name lookup blocks, so only a literal address keeps it on
    /// a worker
    fn blocking(&self, _token: Token) -> bool {
        self.target.needs_lookup()
    }
}

impl Task for SendToTask {
    type Output = Result<usize, RuntimeError>;
    type Input = Nothing;

    /// Waits on this thread, for `Runtime::block`
    fn execute(&self, _token: Token, reactor_id: i32, task_id: usize) -> Self::Output {
        park::drive(self.clone(), reactor_id, task_id)
    }

    fn prepare(&mut self, _token: Token) {
        self.dest = Progress::default();
    }

    /// A name lookup blocks, so only a literal address keeps it on
    /// a worker
    fn blocking(&self, _token: Token) -> bool {
        self.target.needs_lookup()
    }

    fn step(&mut self, _token: Token, _reactor_id: i32, _task_id: usize) -> Step<Self::Output> {
        settle(self.advance())
    }
}

impl Task for RecvFromTask {
    type Output = Result<(Vec<u8>, SocketAddr), RuntimeError>;
    type Input = Nothing;

    /// Waits on this thread, for `Runtime::block`
    fn execute(&self, _token: Token, reactor_id: i32, task_id: usize) -> Self::Output {
        park::drive(self.clone(), reactor_id, task_id)
    }

    fn step(&mut self, _token: Token, _reactor_id: i32, _task_id: usize) -> Step<Self::Output> {
        settle(self.advance())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::futures::net::address::sealed::Sealed;

    /// A send goes to the first address in the socket's own family
    #[test]
    fn a_send_picks_an_address_in_its_own_family() {
        let v4: SocketAddr = "127.0.0.1:0".parse().unwrap();
        let v6: SocketAddr = "[::1]:0".parse().unwrap();

        let to_v4: SocketAddr = "127.0.0.1:9".parse().unwrap();
        let to_v6: SocketAddr = "[::1]:9".parse().unwrap();

        assert_eq!(pick(&"127.0.0.1:9".target(), v4), Ok(to_v4));
        assert_eq!(pick(&"[::1]:9".target(), v6), Ok(to_v6));
        assert_eq!(pick(&"[::1]:9".target(), v4), Err(RuntimeError::BadAddress));
    }
}