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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
use std::os::unix::io::{RawFd, AsRawFd, FromRawFd, IntoRawFd};
use std::os::unix::net::{UnixStream, UnixListener, UnixDatagram};
use std::io::{self, IoSlice, IoSliceMut, ErrorKind};

use libc::{SOCK_DGRAM, SOCK_STREAM, MSG_PEEK, c_void, recvfrom, sendto};

use crate::addr::UnixSocketAddr;
use crate::helpers::*;
use crate::ancillary::*;
use crate::credentials::*;

/// Extension trait for `std::os::unix::net::UnixDatagram` and nonblocking equivalents.
pub trait UnixStreamExt: AsRawFd + FromRawFd + Sized {
    /// Get the address of this socket, as a type that fully supports abstract addresses.
    fn local_unix_addr(&self) -> Result<UnixSocketAddr, io::Error> {
        get_unix_addr(self.as_raw_fd(), GetAddr::LOCAL)
    }
    /// Returns the address of the other end of this stream,
    /// as a type that fully supports abstract addresses.
    fn peer_unix_addr(&self) -> Result<UnixSocketAddr, io::Error> {
        get_unix_addr(self.as_raw_fd(), GetAddr::PEER)
    }

    /// Creates a connection to a listening path-based or abstract named socket.
    fn connect_to_unix_addr(addr: &UnixSocketAddr) -> Result<Self, io::Error>;

    /// Creates a path-based or abstract-named socket and connect to a listening socket.
    fn connect_from_to_unix_addr(from: &UnixSocketAddr,  to: &UnixSocketAddr)
    -> Result<Self, io::Error>;

    /// Sends file descriptors in addition to bytes.
    fn send_fds(&self,  bytes: &[u8],  fds: &[RawFd]) -> Result<usize, io::Error> {
        send_ancillary(self.as_raw_fd(), None, 0, &[IoSlice::new(bytes)], fds, None)
    }
    /// Receives file descriptors in addition to bytes.
    fn recv_fds(&self,  buf: &mut[u8],  fd_buf: &mut[RawFd]) -> Result<(usize, usize), io::Error> {
        recv_fds(self.as_raw_fd(), None, &mut[IoSliceMut::new(buf)], fd_buf)
            .map(|(bytes, _, fds)| (bytes, fds) )
    }

    /// Returns the credentials of the process that created the other end of this stream.
    fn initial_peer_credentials(&self) -> Result<ConnCredentials, io::Error> {
        peer_credentials(self.as_raw_fd())
    }
    /// Returns the SELinux security context of the process that created the other end of this stream.
    ///
    /// Will return an error on other operating systems than Linux or Android,
    /// and also if running under kubernetes.
    /// On success the number of bytes used is returned. (like `Read`)
    ///
    /// The default security context is `unconfined`, without any trailing NUL.  
    /// A buffor of 50 bytes is probably always big enough.
    fn initial_peer_selinux_context(&self,  buffer: &mut[u8]) -> Result<usize, io::Error> {
        selinux_context(self.as_raw_fd(), buffer)
    }
}

impl UnixStreamExt for UnixStream {
    fn connect_to_unix_addr(addr: &UnixSocketAddr) -> Result<Self, io::Error> {
        let socket = Socket::new(SOCK_STREAM, false)?;
        set_unix_addr(socket.as_raw_fd(), SetAddr::PEER, addr)?;
        Ok(unsafe { Self::from_raw_fd(socket.into_raw_fd()) })
    }
    fn connect_from_to_unix_addr(from: &UnixSocketAddr,  to: &UnixSocketAddr)
    -> Result<Self, io::Error> {
        let socket = Socket::new(SOCK_STREAM, false)?;
        set_unix_addr(socket.as_raw_fd(), SetAddr::LOCAL, from)?;
        set_unix_addr(socket.as_raw_fd(), SetAddr::PEER, to)?;
        Ok(unsafe { Self::from_raw_fd(socket.into_raw_fd()) })
    }
}

#[cfg(feature="mio-uds")]
impl UnixStreamExt for mio_uds::UnixStream {
    fn connect_to_unix_addr(addr: &UnixSocketAddr) -> Result<Self, io::Error> {
        let socket = Socket::new(SOCK_STREAM, true)?;
        set_unix_addr(socket.as_raw_fd(), SetAddr::PEER, addr)?;
        Ok(unsafe { Self::from_raw_fd(socket.into_raw_fd()) })
    }
    fn connect_from_to_unix_addr(from: &UnixSocketAddr,  to: &UnixSocketAddr)
    -> Result<Self, io::Error> {
        let socket = Socket::new(SOCK_STREAM, true)?;
        set_unix_addr(socket.as_raw_fd(), SetAddr::LOCAL, from)?;
        set_unix_addr(socket.as_raw_fd(), SetAddr::PEER, to)?;
        Ok(unsafe { Self::from_raw_fd(socket.into_raw_fd()) })
    }
}

#[cfg(feature="mio_07")]
impl UnixStreamExt for mio_07::net::UnixStream {
    fn connect_to_unix_addr(addr: &UnixSocketAddr) -> Result<Self, io::Error> {
        let socket = Socket::new(SOCK_STREAM, true)?;
        set_unix_addr(socket.as_raw_fd(), SetAddr::PEER, addr)?;
        Ok(unsafe { Self::from_raw_fd(socket.into_raw_fd()) })
    }
    fn connect_from_to_unix_addr(from: &UnixSocketAddr,  to: &UnixSocketAddr)
    -> Result<Self, io::Error> {
        let socket = Socket::new(SOCK_STREAM, true)?;
        set_unix_addr(socket.as_raw_fd(), SetAddr::LOCAL, from)?;
        set_unix_addr(socket.as_raw_fd(), SetAddr::PEER, to)?;
        Ok(unsafe { Self::from_raw_fd(socket.into_raw_fd()) })
    }
}



/// Extension trait for using [`UnixSocketAddr`](struct.UnixSocketAddr.html) with `UnixListener` types.
pub trait UnixListenerExt: AsRawFd + FromRawFd + Sized {
    /// The type represeting the stream connection returned by `accept_unix_addr()`.
    type Conn: FromRawFd;

    /// Creates a socket bound to a `UnixSocketAddr` and start listening on it.
    fn bind_unix_addr(on: &UnixSocketAddr) -> Result<Self, io::Error>;

    /// Returns the address this socket is listening on.
    fn local_unix_addr(&self) -> Result<UnixSocketAddr, io::Error> {
        get_unix_addr(self.as_raw_fd(), GetAddr::LOCAL)
    }

    /// Accepts a connection and return the client's address as
    /// an `uds::UnixSocketAddr`.
    fn accept_unix_addr(&self) -> Result<(Self::Conn, UnixSocketAddr), io::Error>;
}

impl UnixListenerExt for UnixListener {
    type Conn = UnixStream;

    fn bind_unix_addr(on: &UnixSocketAddr) -> Result<Self, io::Error> {
        let socket = Socket::new(SOCK_STREAM, false)?;
        set_unix_addr(socket.as_raw_fd(), SetAddr::LOCAL, on)?;
        socket.start_listening()?;
        Ok(unsafe { Self::from_raw_fd(socket.into_raw_fd()) })
    }

    fn accept_unix_addr(&self) -> Result<(Self::Conn, UnixSocketAddr), io::Error> {
        let (socket, addr) = Socket::accept_from(self.as_raw_fd(), false)?;
        let conn = unsafe { Self::Conn::from_raw_fd(socket.into_raw_fd()) };
        Ok((conn, addr))
    }
}

#[cfg(feature="mio-uds")]
impl UnixListenerExt for mio_uds::UnixListener {
    type Conn = mio_uds::UnixStream;

    fn bind_unix_addr(on: &UnixSocketAddr) -> Result<Self, io::Error> {
        let socket = Socket::new(SOCK_STREAM, true)?;
        set_unix_addr(socket.as_raw_fd(), SetAddr::LOCAL, on)?;
        socket.start_listening()?;
        Ok(unsafe { Self::from_raw_fd(socket.into_raw_fd()) })
    }

    fn accept_unix_addr(&self) -> Result<(Self::Conn, UnixSocketAddr), io::Error> {
        let (socket, addr) = Socket::accept_from(self.as_raw_fd(), true)?;
        let conn = unsafe { Self::Conn::from_raw_fd(socket.into_raw_fd()) };
        Ok((conn, addr))
    }
}

#[cfg(feature="mio_07")]
impl UnixListenerExt for mio_07::net::UnixListener {
    type Conn = mio_07::net::UnixStream;

    fn bind_unix_addr(on: &UnixSocketAddr) -> Result<Self, io::Error> {
        let socket = Socket::new(SOCK_STREAM, true)?;
        set_unix_addr(socket.as_raw_fd(), SetAddr::LOCAL, on)?;
        socket.start_listening()?;
        Ok(unsafe { Self::from_raw_fd(socket.into_raw_fd()) })
    }

    fn accept_unix_addr(&self) -> Result<(Self::Conn, UnixSocketAddr), io::Error> {
        let (socket, addr) = Socket::accept_from(self.as_raw_fd(), true)?;
        let conn = unsafe { Self::Conn::from_raw_fd(socket.into_raw_fd()) };
        Ok((conn, addr))
    }
}



/// Extension trait for `std::os::unix::net::UnixDatagram` and nonblocking equivalents.
pub trait UnixDatagramExt: AsRawFd + FromRawFd + Sized {
    /// Create a socket bound to a path or abstract name.
    ///
    /// # Examples
    ///
    #[cfg_attr(any(target_os="linux", target_os="android"), doc="```")]
    #[cfg_attr(not(any(target_os="linux", target_os="android")), doc="```no_run")]
    /// # use std::os::unix::net::UnixDatagram;
    /// # use uds::{UnixDatagramExt, UnixSocketAddr};
    /// #
    /// # fn main() -> Result<(), std::io::Error> {
    /// let addr = UnixSocketAddr::new("@abstract")?;
    /// let socket = UnixDatagram::bind_unix_addr(&addr)?;
    /// # let _ = socket.send_to_unix_addr(b"where are you", &addr);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// This is equivalent of:
    ///
    /// ```
    /// # use std::os::unix::net::UnixDatagram;
    /// # use uds::{UnixDatagramExt, UnixSocketAddr};
    /// #
    /// # fn main() -> Result<(), std::io::Error> {
    /// # let addr = UnixSocketAddr::new("me")?;
    /// let socket = UnixDatagram::unbound()?;
    /// socket.bind_to_unix_addr(&addr)?;
    /// # let _ = std::fs::remove_file("me");
    /// # Ok(())
    /// # }
    /// ```
    fn bind_unix_addr(addr: &UnixSocketAddr) -> Result<Self, io::Error> {
        let socket = Socket::new(SOCK_DGRAM, true)?;
        let socket = unsafe { Self::from_raw_fd(socket.into_raw_fd()) };
        socket.bind_to_unix_addr(addr)?;
        Ok(socket)
    }

    /// Returns the address of this socket, as a type that fully supports abstract addresses.
    fn local_unix_addr(&self) -> Result<UnixSocketAddr, io::Error> {
        get_unix_addr(self.as_raw_fd(), GetAddr::LOCAL)
    }
    /// Returns the address of the connected socket, as a type that fully supports abstract addresses.
    fn peer_unix_addr(&self) -> Result<UnixSocketAddr, io::Error> {
        get_unix_addr(self.as_raw_fd(), GetAddr::PEER)
    }

    /// Creates a path or abstract name for the socket.
    fn bind_to_unix_addr(&self,  addr: &UnixSocketAddr) -> Result<(), io::Error> {
        set_unix_addr(self.as_raw_fd(), SetAddr::LOCAL, addr)
    }
    /// Connects the socket to a path-based or abstract named socket.
    fn connect_to_unix_addr(&self,  addr: &UnixSocketAddr) -> Result<(), io::Error> {
        set_unix_addr(self.as_raw_fd(), SetAddr::PEER, addr)
    }

    /// Sends to the specified address, using an address type that
    /// supports abstract addresses.
    ///
    /// # Examples
    ///
    /// Send to an abstract address:
    ///
    #[cfg_attr(any(target_os="linux", target_os="android"), doc="```")]
    #[cfg_attr(not(any(target_os="linux", target_os="android")), doc="```no_run")]
    /// # use std::os::unix::net::UnixDatagram;
    /// # use uds::{UnixDatagramExt, UnixSocketAddr};
    /// #
    /// let socket = UnixDatagram::unbound().expect("create datagram socket");
    /// let _ = socket.send_to_unix_addr(
    ///     b"Is there anyone there?",
    ///     &UnixSocketAddr::from_abstract("somewhere").expect("OS supports abstract addresses"),
    /// );
    /// ```
    fn send_to_unix_addr(&self,  datagram: &[u8],  addr: &UnixSocketAddr)
    -> Result<usize, io::Error> {
        unsafe {
            let (sockaddr, socklen) = addr.as_raw_general();
            cvt_r!(sendto(
                self.as_raw_fd(),
                datagram.as_ptr() as *const c_void,
                datagram.len(),
                MSG_NOSIGNAL,
                sockaddr,
                socklen,
            )).map(|signed| signed as usize )
        }
    }
    /// Sends a datagram created from multiple segments to the specified address,
    /// using an address type that supports abstract addresses.
    ///
    /// # Examples
    ///
    /// Send a datagram with a fixed header:
    ///
    /// ```
    /// # use std::os::unix::net::UnixDatagram;
    /// # use std::io::IoSlice;
    /// # use uds::{UnixDatagramExt, UnixSocketAddr};
    /// #
    /// let socket = UnixDatagram::unbound().expect("create datagram socket");
    /// let to = UnixSocketAddr::new("/var/run/someone.sock").unwrap();
    /// let msg = [
    ///     IoSlice::new(b"hello "),
    ///     IoSlice::new(to.as_pathname().unwrap().to_str().unwrap().as_bytes()),
    /// ];
    /// let _ = socket.send_vectored_to_unix_addr(&msg, &to);
    /// ```
    fn send_vectored_to_unix_addr(&self,  datagram: &[IoSlice],  addr: &UnixSocketAddr)
    -> Result<usize, io::Error> {
        send_ancillary(self.as_raw_fd(), Some(addr), 0, datagram, &[], None)
    }
    /// Receives from any peer, storing its address in a type that exposes
    /// abstract addresses.
    ///
    /// # Examples
    ///
    /// Respond to the received datagram, regardsless of where it was sent from:
    ///
    /// ```
    /// use std::os::unix::net::UnixDatagram;
    /// use uds::{UnixSocketAddr, UnixDatagramExt};
    ///
    /// let server = UnixDatagram::bind("echo.sock").expect("create server socket");
    ///
    /// let client_addr = UnixSocketAddr::new("@echo_client")
    ///     .or(UnixSocketAddr::new("echo_client.sock"))
    ///     .unwrap();
    /// let client = UnixDatagram::unbound().expect("create client ocket");
    /// client.bind_to_unix_addr(&client_addr).expect("create client socket");
    /// client.connect_to_unix_addr(&UnixSocketAddr::new("echo.sock").unwrap())
    ///     .expect("connect to server");
    /// client.send(b"hello").expect("send");
    ///
    /// let mut buf = [0; 1024];
    /// let (len, from) = server.recv_from_unix_addr(&mut buf).expect("receive");
    /// server.send_to_unix_addr(&buf[..len], &from).expect("respond");
    ///
    /// let len = client.recv(&mut buf).expect("receive response");
    /// assert_eq!(&buf[..len], "hello".as_bytes());
    ///
    /// let _ = std::fs::remove_file("echo.sock");
    /// if let Some(client_path) = client_addr.as_pathname() {
    ///     let _ = std::fs::remove_file(client_path);
    /// }
    /// ```
    fn recv_from_unix_addr(&self,  buf: &mut[u8]) -> Result<(usize, UnixSocketAddr), io::Error> {
        UnixSocketAddr::new_from_ffi(|addr, len| {
            unsafe {
                cvt_r!(recvfrom(
                    self.as_raw_fd(),
                    buf.as_ptr() as *mut c_void,
                    buf.len(),
                    MSG_NOSIGNAL,
                    addr,
                    len,
                )).map(|signed| signed as usize )
            }
        })
    }
    /// Uses multiple buffers to receive from any peer, storing its address in
    /// a type that exposes abstract addresses.
    ///
    /// # Examples
    ///
    /// Read content into a separate buffer than header:
    ///
    #[cfg_attr(feature="mio_07", doc="```")]
    #[cfg_attr(not(feature="mio_07"), doc="```no_compile")]
    /// use mio_07::net::UnixDatagram;
    /// use uds::UnixDatagramExt;
    /// use std::io::IoSliceMut;
    ///
    /// let server = UnixDatagram::bind("cat.sock").expect("create cat.sock");
    /// let mut received = Vec::new();
    ///
    /// let client = UnixDatagram::unbound().expect("create client socket");
    /// client.send_to(b"cat\x01one", "cat.sock").expect("send");
    /// client.send_to(b"cat\x01two", "cat.sock").expect("send");
    /// client.send_to(b"cat\x01three", "cat.sock").expect("send");
    ///
    /// let mut header = [0; 4];
    /// loop {
    ///     let current_len = received.len();
    ///     received.resize(current_len+1024, 0);
    ///     let mut bufs = [
    ///         IoSliceMut::new(&mut header),
    ///         IoSliceMut::new(&mut received[current_len..]),
    ///     ];
    ///     match server.recv_vectored_from_unix_addr(&mut bufs) {
    ///         Ok((len, _addr)) if len > 4  &&  header == *b"cat\x01" => {
    ///             received.truncate(current_len+len-4); // keep it
    ///         },
    ///         Ok((_, _)) => received.truncate(current_len), // discard it
    ///         Err(_) => {
    ///             received.truncate(current_len); // discard it
    ///             break;
    ///         }
    ///     }
    /// }
    ///
    /// assert_eq!(&received, &b"onetwothree");
    /// # let _ = std::fs::remove_file("cat.sock");
    /// ```
    fn recv_vectored_from_unix_addr(&self,  bufs: &mut[IoSliceMut])
    -> Result<(usize, UnixSocketAddr), io::Error> {
        let mut addr = UnixSocketAddr::default();
        recv_fds(self.as_raw_fd(), Some(&mut addr), bufs, &mut[])
            .map(|(bytes, _, _)| (bytes, addr) )
    }
    /// Reads the next datagram without removing it from the queue.
    ///
    /// # Examples
    ///
    /// Discard datagram if it's the wrong protocol:
    ///
    /// ```
    /// # use std::os::unix::net::UnixDatagram;
    /// # use uds::{UnixSocketAddr, UnixDatagramExt};
    /// #
    /// let checker = UnixDatagram::bind("checker.sock").expect("create receiver socket");
    ///
    /// let client = UnixDatagram::unbound().expect("create client ocket");
    /// client.send_to(b"hello", "checker.sock").expect("send");
    ///
    /// let mut header = [0; 4];
    /// let (len, _from) = checker.peek_from_unix_addr(&mut header).expect("receive");
    /// if len != 4  ||  header != *b"WTFP" {
    ///     let _ = checker.recv(&mut header); // discard
    /// } else {
    ///     // call function that receives and processes it
    /// }
    /// #
    /// # let _ = std::fs::remove_file("checker.sock");
    /// ```
    fn peek_from_unix_addr(&self,  buf: &mut[u8]) -> Result<(usize, UnixSocketAddr), io::Error> {
        UnixSocketAddr::new_from_ffi(|addr, len| {
            unsafe {
                cvt_r!(recvfrom(
                    self.as_raw_fd(),
                    buf.as_ptr() as *mut c_void,
                    buf.len(),
                    MSG_PEEK | MSG_NOSIGNAL,
                    addr,
                    len,
                )).map(|signed| signed as usize )
            }
        })
    }
    /// Uses multiple buffers to read the next datagram without removing it
    /// from the queue.
    ///
    /// # Examples
    ///
    #[cfg_attr(any(target_os="linux", target_os="android"), doc="```")]
    #[cfg_attr(not(any(target_os="linux", target_os="android")), doc="```no_run")]
    /// use std::os::unix::net::UnixDatagram;
    /// use std::io::IoSliceMut;
    /// use uds::{UnixDatagramExt, UnixSocketAddr};
    ///
    /// # let _ = std::fs::remove_file("datagram_server.sock");
    /// let server = UnixDatagram::bind("datagram_server.sock").unwrap();
    ///
    /// // get a random abstract address on Linux
    /// let client = UnixDatagram::unbound().unwrap();
    /// client.bind_to_unix_addr(&UnixSocketAddr::new_unspecified()).unwrap();
    /// client.connect("datagram_server.sock").unwrap();
    /// client.send(b"headerbodybody").unwrap();
    ///
    /// let (mut buf_a, mut buf_b) = ([0; 6], [0; 12]);
    /// let mut vector = [IoSliceMut::new(&mut buf_a), IoSliceMut::new(&mut buf_b)];
    /// let (bytes, addr) = server.peek_vectored_from_unix_addr(&mut vector).unwrap();
    /// assert_eq!(addr, client.local_unix_addr().unwrap());
    /// assert_eq!(bytes, 14);
    /// assert_eq!(&buf_a, b"header");
    /// assert_eq!(&buf_b[..8], b"bodybody");
    /// #
    /// # std::fs::remove_file("datagram_server.sock").unwrap();
    /// ```
    fn peek_vectored_from_unix_addr(&self,  bufs: &mut[IoSliceMut])
    -> Result<(usize, UnixSocketAddr), io::Error> {
        let mut addr = UnixSocketAddr::default();
        recv_ancillary(
            self.as_raw_fd(),
            Some(&mut addr),
            MSG_PEEK | MSG_NOSIGNAL,
            bufs,
            &mut[]
        ).map(|(bytes, _)| (bytes, addr) )
    }

    /// Sends file descriptors along with the datagram, on an unconnected socket.
    fn send_fds_to(&self,  datagram: &[u8],  fds: &[RawFd],  addr: &UnixSocketAddr)
    -> Result<usize, io::Error> {
        send_ancillary(self.as_raw_fd(), Some(addr), 0, &[IoSlice::new(datagram)], fds, None)
    }
    /// Sends file descriptors along with the datagram, on a connected socket.
    fn send_fds(&self,  datagram: &[u8],  fds: &[RawFd]) -> Result<usize, io::Error> {
        send_ancillary(self.as_raw_fd(), None, 0, &[IoSlice::new(datagram)], fds, None)
    }
    /// Receives file descriptors along with the datagram, on an unconnected socket
    fn recv_fds_from(&self,  buf: &mut[u8],  fd_buf: &mut[RawFd])
    -> Result<(usize, usize, UnixSocketAddr), io::Error> {
        let mut addr = UnixSocketAddr::default();
        recv_fds(self.as_raw_fd(), Some(&mut addr), &mut[IoSliceMut::new(buf)], fd_buf)
            .map(|(bytes, _, fds)| (bytes, fds, addr) )
    }
    /// Receives file descriptors along with the datagram, on a connected socket
    fn recv_fds(&self,  buf: &mut[u8],  fd_buf: &mut[RawFd]) -> Result<(usize, usize), io::Error> {
        recv_fds(self.as_raw_fd(), None, &mut[IoSliceMut::new(buf)], fd_buf)
            .map(|(bytes, _, fds)| (bytes, fds) )
    }

    /// Returns the credentials of the process that created a socket pair.
    ///
    /// This information is only available on Linux, and only for sockets that
    /// was created with `pair()` or the underlying `socketpair()`.
    /// For sockets that have merely been "connected" to an address
    /// or not connected at all, an error of kind `NotConnected`
    /// or `InvalidInput` is returned.
    ///
    /// The use cases of this function gotta be very narrow:
    ///
    /// * It will return the credentials of the current process unless
    ///   the side of the socket this method is called on was received via
    ///   FD-passing or inherited from a parent.
    /// * If it was created by the direct parent process,
    ///   one might as well use `getppid()` and go from there?
    /// * A returned pid can be repurposed by the OS before the call returns.
    /// * uids or groups will be those in effect when the pair was created,
    ///   and will not reflect changes in privileges.
    ///
    /// Despite these limitations, the feature is supported by Linux at least
    /// (but not macOS or FreeBSD), so might as well expose it.
    fn initial_pair_credentials(&self) -> Result<ConnCredentials, io::Error> {
        peer_credentials(self.as_raw_fd())
    }
    /// Returns the SELinux security context of the process that created a socket pair.
    ///
    /// Has the same limitations and gotchas as `initial_pair_credentials()`,
    /// and will return an error on other OSes than Linux or Android
    /// or if running under kubernetes.
    ///
    /// The default security context is the string `unconfined`.
    fn initial_pair_selinux_context(&self,  buffer: &mut[u8]) -> Result<usize, io::Error> {
        selinux_context(self.as_raw_fd(), buffer)
    }
}

impl UnixDatagramExt for UnixDatagram {
    fn bind_unix_addr(addr: &UnixSocketAddr) -> Result<Self, io::Error> {
        match UnixDatagram::unbound() {
            Ok(socket) => match socket.bind_to_unix_addr(addr) {
                Ok(()) => Ok(socket),
                Err(e) => Err(e),
            }
            Err(e) => Err(e)
        }
    }
}

#[cfg(feature="mio-uds")]
impl UnixDatagramExt for mio_uds::UnixDatagram {
    fn bind_unix_addr(addr: &UnixSocketAddr) -> Result<Self, io::Error> {
        match mio_uds::UnixDatagram::unbound() {
            Ok(socket) => match socket.bind_to_unix_addr(addr) {
                Ok(()) => Ok(socket),
                Err(e) => Err(e),
            }
            Err(e) => Err(e)
        }
    }
}

#[cfg(feature="mio_07")]
impl UnixDatagramExt for mio_07::net::UnixDatagram {
    fn bind_unix_addr(addr: &UnixSocketAddr) -> Result<Self, io::Error> {
        match mio_07::net::UnixDatagram::unbound() {
            Ok(socket) => match socket.bind_to_unix_addr(addr) {
                Ok(()) => Ok(socket),
                Err(e) => Err(e),
            }
            Err(e) => Err(e)
        }
    }
}