nuclei 0.5.1

Proactive IO & runtime system
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
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
//! Unix domain sockets: listeners, streams and datagrams, and [`UCred`], the
//! credentials of a stream's peer; and Unix pipes ([`pipe`]).

pub mod pipe;
use core::future::poll_fn;
use core::pin::Pin;
use core::task::{Context, Poll};
use std::io;
use std::os::fd::OwnedFd;
use std::os::unix::net::{SocketAddr as UnixSocketAddr, UnixDatagram, UnixListener, UnixStream};
use std::path::Path;

use futures_core::Stream;

use crate::io::source::write::{Dest, Peer};
use crate::io::source::{Addr, Kind};
use crate::io::sys::UnixAddr;
use crate::io::{Handle, Interest, IoObject, Ready, sys};

/// The credentials of a Unix stream's peer: its user and group, and its
/// process where the system tells it (Linux, Android, macOS, iOS).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct UCred {
    uid: u32,
    gid: u32,
    pid: Option<i32>,
}

impl UCred {
    /// The peer's user id.
    pub fn uid(&self) -> u32 {
        self.uid
    }

    /// The peer's group id.
    pub fn gid(&self) -> u32 {
        self.gid
    }

    /// The peer's process id, where the system tells it.
    pub fn pid(&self) -> Option<i32> {
        self.pid
    }
}

#[cfg(any(target_os = "linux", target_os = "android"))]
fn peer_cred(fd: std::os::fd::RawFd) -> io::Result<UCred> {
    let mut cred = libc::ucred {
        pid: 0,
        uid: 0,
        gid: 0,
    };
    let mut len = size_of::<libc::ucred>() as libc::socklen_t;
    // SAFETY: SO_PEERCRED answers a `ucred`, written into our own.
    if unsafe {
        libc::getsockopt(
            fd,
            libc::SOL_SOCKET,
            libc::SO_PEERCRED,
            core::ptr::from_mut(&mut cred).cast(),
            &mut len,
        )
    } != 0
    {
        return Err(io::Error::last_os_error());
    }
    Ok(UCred {
        uid: cred.uid,
        gid: cred.gid,
        pid: Some(cred.pid),
    })
}

#[cfg(not(any(target_os = "linux", target_os = "android")))]
fn peer_cred(fd: std::os::fd::RawFd) -> io::Result<UCred> {
    let (mut uid, mut gid) = (0, 0);
    // SAFETY: the call writes the two ids into our own.
    if unsafe { libc::getpeereid(fd, &mut uid, &mut gid) } != 0 {
        return Err(io::Error::last_os_error());
    }
    #[cfg(any(target_os = "macos", target_os = "ios"))]
    let pid = {
        let mut pid: libc::pid_t = 0;
        let mut len = size_of::<libc::pid_t>() as libc::socklen_t;
        // SAFETY: LOCAL_PEERPID answers a `pid_t`, written into our own.
        let r = unsafe {
            libc::getsockopt(
                fd,
                libc::SOL_LOCAL,
                libc::LOCAL_PEERPID,
                core::ptr::from_mut(&mut pid).cast(),
                &mut len,
            )
        };
        (r == 0).then_some(pid)
    };
    #[cfg(not(any(target_os = "macos", target_os = "ios")))]
    let pid = None;
    Ok(UCred { uid, gid, pid })
}

fn unix(addr: Addr) -> io::Result<UnixSocketAddr> {
    match addr {
        Addr::Unix(addr) => Ok(addr),
        Addr::Inet(_) => Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "a Unix socket's peer has an internet address",
        )),
    }
}

impl Handle<UnixListener> {
    /// A listener bound to `path`.
    ///
    /// # Errors
    ///
    /// As [`UnixListener::bind`].
    pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<Handle<UnixListener>> {
        Handle::adopt(UnixListener::bind(path)?, Kind::UnixListener)
    }

    /// A listener bound to `addr`: a path, or on Linux a name in the
    /// abstract namespace.
    ///
    /// # Errors
    ///
    /// As [`UnixListener::bind_addr`].
    pub fn bind_addr(addr: &UnixSocketAddr) -> io::Result<Handle<UnixListener>> {
        Handle::adopt(UnixListener::bind_addr(addr)?, Kind::UnixListener)
    }

    /// The next connection, with its peer's address.
    ///
    /// # Errors
    ///
    /// As `accept(2)`.
    pub async fn accept(&self) -> io::Result<(Handle<UnixStream>, UnixSocketAddr)> {
        poll_fn(|cx| self.poll_accept(cx)).await
    }

    fn poll_accept(
        &self,
        cx: &mut Context<'_>,
    ) -> Poll<io::Result<(Handle<UnixStream>, UnixSocketAddr)>> {
        self.source().poll_accept(cx).map(|accepted| {
            let (fd, addr) = accepted?;
            Ok((stream(fd), unix(addr)?))
        })
    }

    /// The connections as they come, without their addresses; the stream
    /// never ends, and an error is one of its items.
    pub fn incoming(
        &self,
    ) -> impl Stream<Item = io::Result<Handle<UnixStream>>> + Send + Unpin + '_ {
        Incoming { listener: self }
    }
}

fn stream(fd: OwnedFd) -> Handle<UnixStream> {
    Handle::from_nonblocking(UnixStream::from(fd), Kind::Stream)
}

struct Incoming<'a> {
    listener: &'a Handle<UnixListener>,
}

impl Stream for Incoming<'_> {
    type Item = io::Result<Handle<UnixStream>>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        self.listener
            .poll_accept(cx)
            .map(|accepted| Some(accepted.map(|(stream, _)| stream)))
    }
}

/// The connect of the non-blocking Unix stream `socket` to `addr`.
pub(super) async fn connect(socket: OwnedFd, addr: &UnixAddr) -> io::Result<Handle<UnixStream>> {
    let stream = Handle::from_nonblocking(UnixStream::from(socket), Kind::Stream);
    match poll_fn(|cx| stream.source().poll_connect(cx, Peer::Unix(addr))).await {
        Ok(()) => Ok(stream),
        // The listener's backlog is full: nothing tells a socket when it
        // has room, so a thread of the blocking pool waits in a blocking
        // connect instead.
        Err(err) if err.kind() == io::ErrorKind::WouldBlock => {
            drop(stream);
            let addr = *addr;
            let connected =
                crate::rt::blocking::call(move || sys::connect_unix_waiting(&addr)).await?;
            Ok(Handle::from_nonblocking(
                UnixStream::from(connected),
                Kind::Stream,
            ))
        }
        Err(err) => Err(err),
    }
}

impl Handle<UnixStream> {
    /// A stream connected to the listener at `path`.
    ///
    /// # Errors
    ///
    /// As `connect(2)`.
    pub async fn connect<P: AsRef<Path>>(path: P) -> io::Result<Handle<UnixStream>> {
        connect(
            sys::socket(libc::AF_UNIX, libc::SOCK_STREAM)?,
            &UnixAddr::path(path.as_ref())?,
        )
        .await
    }

    /// A stream connected to the listener at `addr`: a path, or on Linux a
    /// name in the abstract namespace.
    ///
    /// # Errors
    ///
    /// As `connect(2)`; an unnamed address names no listener.
    pub async fn connect_addr(addr: &UnixSocketAddr) -> io::Result<Handle<UnixStream>> {
        connect(
            sys::socket(libc::AF_UNIX, libc::SOCK_STREAM)?,
            &UnixAddr::of(addr)?,
        )
        .await
    }

    /// The credentials of the process at the other end, as the connection
    /// was made.
    ///
    /// # Errors
    ///
    /// When the system cannot tell them.
    pub fn peer_cred(&self) -> io::Result<UCred> {
        peer_cred(self.raw_io())
    }

    /// Two streams connected to each other.
    ///
    /// # Errors
    ///
    /// As `socketpair(2)`.
    pub fn pair() -> io::Result<(Handle<UnixStream>, Handle<UnixStream>)> {
        let (a, b) = UnixStream::pair()?;
        Ok((
            Handle::adopt(a, Kind::Stream)?,
            Handle::adopt(b, Kind::Stream)?,
        ))
    }

    /// Sends bytes; returns how many were taken.
    ///
    /// # Errors
    ///
    /// As `send(2)`, or the error that stopped earlier bytes.
    pub async fn send(&self, buf: &[u8]) -> io::Result<usize> {
        poll_fn(|cx| self.source().poll_write(cx, buf)).await
    }

    /// Receives bytes; zero at the end of the stream.
    ///
    /// # Errors
    ///
    /// As `recv(2)`.
    pub async fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
        poll_fn(|cx| self.source().poll_read(cx, buf, false)).await
    }

    /// Looks at the bytes the next receive returns, without taking them.
    ///
    /// # Errors
    ///
    /// As `recv(2)` with `MSG_PEEK`.
    pub async fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
        poll_fn(|cx| self.source().poll_read(cx, buf, true)).await
    }
}

impl Handle<UnixDatagram> {
    /// A socket bound to `path`.
    ///
    /// # Errors
    ///
    /// As [`UnixDatagram::bind`].
    pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<Handle<UnixDatagram>> {
        Handle::adopt(UnixDatagram::bind(path)?, Kind::UnixDatagram)
    }

    /// A socket bound to `addr`: a path, or on Linux a name in the abstract
    /// namespace.
    ///
    /// # Errors
    ///
    /// As [`UnixDatagram::bind_addr`].
    pub fn bind_addr(addr: &UnixSocketAddr) -> io::Result<Handle<UnixDatagram>> {
        Handle::adopt(UnixDatagram::bind_addr(addr)?, Kind::UnixDatagram)
    }

    /// A socket bound to no address: it sends with `send_to`, or to the peer
    /// its `connect` names.
    ///
    /// # Errors
    ///
    /// When the system has no socket to give.
    pub fn unbound() -> io::Result<Handle<UnixDatagram>> {
        Ok(Handle::from_nonblocking(
            UnixDatagram::from(sys::socket(libc::AF_UNIX, libc::SOCK_DGRAM)?),
            Kind::UnixDatagram,
        ))
    }

    /// Two sockets connected to each other.
    ///
    /// # Errors
    ///
    /// As `socketpair(2)`.
    pub fn pair() -> io::Result<(Handle<UnixDatagram>, Handle<UnixDatagram>)> {
        let (a, b) = UnixDatagram::pair()?;
        Ok((
            Handle::adopt(a, Kind::UnixDatagram)?,
            Handle::adopt(b, Kind::UnixDatagram)?,
        ))
    }

    /// Sends a datagram to the connected peer.
    ///
    /// # Errors
    ///
    /// As `send(2)`, or an earlier datagram's.
    pub async fn send(&self, buf: &[u8]) -> io::Result<usize> {
        poll_fn(|cx| self.source().poll_send(cx, buf, Dest::Connected)).await
    }

    /// Receives a datagram from the connected peer; what does not fit `buf`
    /// is cut.
    ///
    /// # Errors
    ///
    /// As `recv(2)`.
    pub async fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
        self.receive(buf, false).await.map(|(n, _)| n)
    }

    /// Looks at the next datagram without taking it.
    ///
    /// # Errors
    ///
    /// As `recv(2)` with `MSG_PEEK`.
    pub async fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
        self.receive(buf, true).await.map(|(n, _)| n)
    }

    /// Sends a datagram to the socket at `path`.
    ///
    /// # Errors
    ///
    /// As `sendto(2)`, or an earlier datagram's.
    pub async fn send_to<P: AsRef<Path>>(&self, buf: &[u8], path: P) -> io::Result<usize> {
        let addr = UnixAddr::path(path.as_ref())?;
        poll_fn(|cx| self.source().poll_send(cx, buf, Dest::Unix(&addr))).await
    }

    /// Receives a datagram with its sender's address.
    ///
    /// # Errors
    ///
    /// As `recvfrom(2)`.
    pub async fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, UnixSocketAddr)> {
        self.receive(buf, false).await
    }

    /// Looks at the next datagram and its sender without taking it.
    ///
    /// # Errors
    ///
    /// As `recvfrom(2)` with `MSG_PEEK`.
    pub async fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, UnixSocketAddr)> {
        self.receive(buf, true).await
    }

    async fn receive(&self, buf: &mut [u8], peek: bool) -> io::Result<(usize, UnixSocketAddr)> {
        poll_fn(|cx| self.poll_receive(cx, buf, peek)).await
    }

    fn poll_receive(
        &self,
        cx: &mut Context<'_>,
        buf: &mut [u8],
        peek: bool,
    ) -> Poll<io::Result<(usize, UnixSocketAddr)>> {
        let (n, addr) = core::task::ready!(self.source().poll_receive(cx, buf, peek))?;
        Poll::Ready(unix(addr).map(|addr| (n, addr)))
    }

    /// Waits until a receive would end at once: a datagram came, or an
    /// error (which the receive reports).
    ///
    /// # Errors
    ///
    /// When the socket cannot be watched.
    pub async fn readable(&self) -> io::Result<()> {
        self.ready(Interest::READABLE).await.map(drop)
    }

    /// Waits until a send would be taken at once.
    ///
    /// # Errors
    ///
    /// When the socket cannot be watched.
    pub async fn writable(&self) -> io::Result<()> {
        self.ready(Interest::WRITABLE).await.map(drop)
    }

    /// Waits until some of `interest` is ready, and says what is.
    ///
    /// # Errors
    ///
    /// When the socket cannot be watched.
    pub async fn ready(&self, interest: Interest) -> io::Result<Ready> {
        poll_fn(|cx| super::poll_ready(self.source(), cx, interest)).await
    }

    /// Polls until a receive would end at once.
    ///
    /// # Errors
    ///
    /// When the socket cannot be watched.
    pub fn poll_recv_ready(&self, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        self.source().poll_read_ready(cx).map(|r| r.map(drop))
    }

    /// Polls until a send would be taken at once.
    ///
    /// # Errors
    ///
    /// When the socket cannot be watched.
    pub fn poll_send_ready(&self, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        self.source().poll_write_ready(cx).map(|r| r.map(drop))
    }

    /// Sends a datagram to the connected peer without waiting.
    ///
    /// # Errors
    ///
    /// `WouldBlock` when a datagram is still queued, and as `send(2)`.
    pub fn try_send(&self, buf: &[u8]) -> io::Result<usize> {
        super::at_once(|cx| self.source().poll_send(cx, buf, Dest::Connected))
    }

    /// Sends a datagram to the socket at `path` without waiting.
    ///
    /// # Errors
    ///
    /// `WouldBlock` when a datagram is still queued, and as `sendto(2)`.
    pub fn try_send_to<P: AsRef<Path>>(&self, buf: &[u8], path: P) -> io::Result<usize> {
        let addr = UnixAddr::path(path.as_ref())?;
        super::at_once(|cx| self.source().poll_send(cx, buf, Dest::Unix(&addr)))
    }

    /// Receives a datagram from the connected peer without waiting; what
    /// does not fit `buf` is cut.
    ///
    /// # Errors
    ///
    /// `WouldBlock` when none came yet, and as `recv(2)`.
    pub fn try_recv(&self, buf: &mut [u8]) -> io::Result<usize> {
        super::at_once(|cx| self.poll_receive(cx, buf, false)).map(|(n, _)| n)
    }

    /// Receives a datagram and its sender's address without waiting.
    ///
    /// # Errors
    ///
    /// `WouldBlock` when none came yet, and as `recvfrom(2)`.
    pub fn try_recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, UnixSocketAddr)> {
        super::at_once(|cx| self.poll_receive(cx, buf, false))
    }
}