Skip to main content

std/os/unix/net/
datagram.rs

1#[cfg(any(
2    target_os = "linux",
3    target_os = "android",
4    target_os = "dragonfly",
5    target_os = "freebsd",
6    target_os = "openbsd",
7    target_os = "netbsd",
8    target_os = "solaris",
9    target_os = "illumos",
10    target_os = "haiku",
11    target_os = "nto",
12    target_os = "cygwin"
13))]
14use libc::MSG_NOSIGNAL;
15
16use super::{SocketAddr, sockaddr_un};
17#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "cygwin"))]
18use super::{SocketAncillary, recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to};
19#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "cygwin"))]
20use crate::io::{IoSlice, IoSliceMut};
21use crate::net::Shutdown;
22use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
23use crate::path::Path;
24use crate::sys::net::Socket;
25use crate::sys::{AsInner, FromInner, IntoInner, cvt};
26use crate::time::Duration;
27use crate::{fmt, io};
28#[cfg(not(any(
29    target_os = "linux",
30    target_os = "android",
31    target_os = "dragonfly",
32    target_os = "freebsd",
33    target_os = "openbsd",
34    target_os = "netbsd",
35    target_os = "solaris",
36    target_os = "illumos",
37    target_os = "haiku",
38    target_os = "nto",
39    target_os = "cygwin"
40)))]
41const MSG_NOSIGNAL: core::ffi::c_int = 0x0;
42
43/// A Unix datagram socket.
44///
45/// # Examples
46///
47/// ```no_run
48/// use std::os::unix::net::UnixDatagram;
49///
50/// fn main() -> std::io::Result<()> {
51///     let socket = UnixDatagram::bind("/path/to/my/socket")?;
52///     socket.send_to(b"hello world", "/path/to/other/socket")?;
53///     let mut buf = [0; 100];
54///     let (count, address) = socket.recv_from(&mut buf)?;
55///     println!("socket {:?} sent {:?}", address, &buf[..count]);
56///     Ok(())
57/// }
58/// ```
59#[stable(feature = "unix_socket", since = "1.10.0")]
60pub struct UnixDatagram(Socket);
61
62#[stable(feature = "unix_socket", since = "1.10.0")]
63impl fmt::Debug for UnixDatagram {
64    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
65        let mut builder = fmt.debug_struct("UnixDatagram");
66        builder.field("fd", self.0.as_inner());
67        if let Ok(addr) = self.local_addr() {
68            builder.field("local", &addr);
69        }
70        if let Ok(addr) = self.peer_addr() {
71            builder.field("peer", &addr);
72        }
73        builder.finish()
74    }
75}
76
77impl UnixDatagram {
78    /// Creates a Unix datagram socket bound to the given path.
79    ///
80    /// # Examples
81    ///
82    /// ```no_run
83    /// use std::os::unix::net::UnixDatagram;
84    ///
85    /// let sock = match UnixDatagram::bind("/path/to/the/socket") {
86    ///     Ok(sock) => sock,
87    ///     Err(e) => {
88    ///         println!("Couldn't bind: {e:?}");
89    ///         return
90    ///     }
91    /// };
92    /// ```
93    #[stable(feature = "unix_socket", since = "1.10.0")]
94    pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixDatagram> {
95        unsafe {
96            let socket = UnixDatagram::unbound()?;
97            let (addr, len) = sockaddr_un(path.as_ref())?;
98
99            cvt(libc::bind(socket.as_raw_fd(), (&raw const addr) as *const _, len as _))?;
100
101            Ok(socket)
102        }
103    }
104
105    /// Creates a Unix datagram socket bound to an address.
106    ///
107    /// # Examples
108    ///
109    /// ```no_run
110    /// use std::os::unix::net::{UnixDatagram};
111    ///
112    /// fn main() -> std::io::Result<()> {
113    ///     let sock1 = UnixDatagram::bind("path/to/socket")?;
114    ///     let addr = sock1.local_addr()?;
115    ///
116    ///     let sock2 = match UnixDatagram::bind_addr(&addr) {
117    ///         Ok(sock) => sock,
118    ///         Err(err) => {
119    ///             println!("Couldn't bind: {err:?}");
120    ///             return Err(err);
121    ///         }
122    ///     };
123    ///     Ok(())
124    /// }
125    /// ```
126    #[stable(feature = "unix_socket_abstract", since = "1.70.0")]
127    pub fn bind_addr(socket_addr: &SocketAddr) -> io::Result<UnixDatagram> {
128        unsafe {
129            let socket = UnixDatagram::unbound()?;
130            cvt(libc::bind(
131                socket.as_raw_fd(),
132                (&raw const socket_addr.addr) as *const _,
133                socket_addr.len as _,
134            ))?;
135            Ok(socket)
136        }
137    }
138
139    /// Creates a Unix Datagram socket which is not bound to any address.
140    ///
141    /// # Examples
142    ///
143    /// ```no_run
144    /// use std::os::unix::net::UnixDatagram;
145    ///
146    /// let sock = match UnixDatagram::unbound() {
147    ///     Ok(sock) => sock,
148    ///     Err(e) => {
149    ///         println!("Couldn't unbound: {e:?}");
150    ///         return
151    ///     }
152    /// };
153    /// ```
154    #[stable(feature = "unix_socket", since = "1.10.0")]
155    pub fn unbound() -> io::Result<UnixDatagram> {
156        let inner = Socket::new(libc::AF_UNIX, libc::SOCK_DGRAM)?;
157        Ok(UnixDatagram(inner))
158    }
159
160    /// Creates an unnamed pair of connected sockets.
161    ///
162    /// Returns two `UnixDatagrams`s which are connected to each other.
163    ///
164    /// # Examples
165    ///
166    /// ```no_run
167    /// use std::os::unix::net::UnixDatagram;
168    ///
169    /// let (sock1, sock2) = match UnixDatagram::pair() {
170    ///     Ok((sock1, sock2)) => (sock1, sock2),
171    ///     Err(e) => {
172    ///         println!("Couldn't unbound: {e:?}");
173    ///         return
174    ///     }
175    /// };
176    /// ```
177    #[stable(feature = "unix_socket", since = "1.10.0")]
178    pub fn pair() -> io::Result<(UnixDatagram, UnixDatagram)> {
179        let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_DGRAM)?;
180        Ok((UnixDatagram(i1), UnixDatagram(i2)))
181    }
182
183    /// Connects the socket to the specified path address.
184    ///
185    /// The [`send`] method may be used to send data to the specified address.
186    /// [`recv`] and [`recv_from`] will only receive data from that address.
187    ///
188    /// [`send`]: UnixDatagram::send
189    /// [`recv`]: UnixDatagram::recv
190    /// [`recv_from`]: UnixDatagram::recv_from
191    ///
192    /// # Examples
193    ///
194    /// ```no_run
195    /// use std::os::unix::net::UnixDatagram;
196    ///
197    /// fn main() -> std::io::Result<()> {
198    ///     let sock = UnixDatagram::unbound()?;
199    ///     match sock.connect("/path/to/the/socket") {
200    ///         Ok(sock) => sock,
201    ///         Err(e) => {
202    ///             println!("Couldn't connect: {e:?}");
203    ///             return Err(e)
204    ///         }
205    ///     };
206    ///     Ok(())
207    /// }
208    /// ```
209    #[stable(feature = "unix_socket", since = "1.10.0")]
210    pub fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
211        unsafe {
212            let (addr, len) = sockaddr_un(path.as_ref())?;
213
214            cvt(libc::connect(self.as_raw_fd(), (&raw const addr) as *const _, len))?;
215        }
216        Ok(())
217    }
218
219    /// Connects the socket to an address.
220    ///
221    /// # Examples
222    ///
223    /// ```no_run
224    /// use std::os::unix::net::{UnixDatagram};
225    ///
226    /// fn main() -> std::io::Result<()> {
227    ///     let bound = UnixDatagram::bind("/path/to/socket")?;
228    ///     let addr = bound.local_addr()?;
229    ///
230    ///     let sock = UnixDatagram::unbound()?;
231    ///     match sock.connect_addr(&addr) {
232    ///         Ok(sock) => sock,
233    ///         Err(e) => {
234    ///             println!("Couldn't connect: {e:?}");
235    ///             return Err(e)
236    ///         }
237    ///     };
238    ///     Ok(())
239    /// }
240    /// ```
241    #[stable(feature = "unix_socket_abstract", since = "1.70.0")]
242    pub fn connect_addr(&self, socket_addr: &SocketAddr) -> io::Result<()> {
243        unsafe {
244            cvt(libc::connect(
245                self.as_raw_fd(),
246                (&raw const socket_addr.addr) as *const _,
247                socket_addr.len,
248            ))?;
249        }
250        Ok(())
251    }
252
253    /// Creates a new independently owned handle to the underlying socket.
254    ///
255    /// The returned `UnixDatagram` is a reference to the same socket that this
256    /// object references. Both handles can be used to accept incoming
257    /// connections and options set on one side will affect the other.
258    ///
259    /// # Examples
260    ///
261    /// ```no_run
262    /// use std::os::unix::net::UnixDatagram;
263    ///
264    /// fn main() -> std::io::Result<()> {
265    ///     let sock = UnixDatagram::bind("/path/to/the/socket")?;
266    ///     let sock_copy = sock.try_clone().expect("try_clone failed");
267    ///     Ok(())
268    /// }
269    /// ```
270    #[stable(feature = "unix_socket", since = "1.10.0")]
271    pub fn try_clone(&self) -> io::Result<UnixDatagram> {
272        self.0.duplicate().map(UnixDatagram)
273    }
274
275    /// Returns the address of this socket.
276    ///
277    /// # Examples
278    ///
279    /// ```no_run
280    /// use std::os::unix::net::UnixDatagram;
281    ///
282    /// fn main() -> std::io::Result<()> {
283    ///     let sock = UnixDatagram::bind("/path/to/the/socket")?;
284    ///     let addr = sock.local_addr().expect("Couldn't get local address");
285    ///     Ok(())
286    /// }
287    /// ```
288    #[stable(feature = "unix_socket", since = "1.10.0")]
289    pub fn local_addr(&self) -> io::Result<SocketAddr> {
290        SocketAddr::new(|addr, len| unsafe { libc::getsockname(self.as_raw_fd(), addr, len) })
291    }
292
293    /// Returns the address of this socket's peer.
294    ///
295    /// The [`connect`] method will connect the socket to a peer.
296    ///
297    /// [`connect`]: UnixDatagram::connect
298    ///
299    /// # Examples
300    ///
301    /// ```no_run
302    /// use std::os::unix::net::UnixDatagram;
303    ///
304    /// fn main() -> std::io::Result<()> {
305    ///     let sock = UnixDatagram::unbound()?;
306    ///     sock.connect("/path/to/the/socket")?;
307    ///
308    ///     let addr = sock.peer_addr().expect("Couldn't get peer address");
309    ///     Ok(())
310    /// }
311    /// ```
312    #[stable(feature = "unix_socket", since = "1.10.0")]
313    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
314        SocketAddr::new(|addr, len| unsafe { libc::getpeername(self.as_raw_fd(), addr, len) })
315    }
316
317    fn recv_from_flags(
318        &self,
319        buf: &mut [u8],
320        flags: core::ffi::c_int,
321    ) -> io::Result<(usize, SocketAddr)> {
322        let mut count = 0;
323        let addr = SocketAddr::new(|addr, len| unsafe {
324            count = libc::recvfrom(
325                self.as_raw_fd(),
326                buf.as_mut_ptr() as *mut _,
327                buf.len(),
328                flags,
329                addr,
330                len,
331            );
332            if count > 0 {
333                1
334            } else if count == 0 {
335                0
336            } else {
337                -1
338            }
339        })?;
340
341        Ok((count as usize, addr))
342    }
343
344    /// Receives data from the socket.
345    ///
346    /// On success, returns the number of bytes read and the address from
347    /// whence the data came.
348    ///
349    /// # Examples
350    ///
351    /// ```no_run
352    /// use std::os::unix::net::UnixDatagram;
353    ///
354    /// fn main() -> std::io::Result<()> {
355    ///     let sock = UnixDatagram::unbound()?;
356    ///     let mut buf = vec![0; 10];
357    ///     let (size, sender) = sock.recv_from(buf.as_mut_slice())?;
358    ///     println!("received {size} bytes from {sender:?}");
359    ///     Ok(())
360    /// }
361    /// ```
362    #[stable(feature = "unix_socket", since = "1.10.0")]
363    pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
364        self.recv_from_flags(buf, 0)
365    }
366
367    /// Receives data from the socket.
368    ///
369    /// On success, returns the number of bytes read.
370    ///
371    /// # Examples
372    ///
373    /// ```no_run
374    /// use std::os::unix::net::UnixDatagram;
375    ///
376    /// fn main() -> std::io::Result<()> {
377    ///     let sock = UnixDatagram::bind("/path/to/the/socket")?;
378    ///     let mut buf = vec![0; 10];
379    ///     sock.recv(buf.as_mut_slice()).expect("recv function failed");
380    ///     Ok(())
381    /// }
382    /// ```
383    #[stable(feature = "unix_socket", since = "1.10.0")]
384    pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
385        self.0.read(buf)
386    }
387
388    /// Receives data and ancillary data from socket.
389    ///
390    /// On success, returns the number of bytes read, if the data was truncated and the address from whence the msg came.
391    ///
392    /// # Examples
393    ///
394    #[cfg_attr(
395        any(target_os = "android", target_os = "linux", target_os = "cygwin"),
396        doc = "```no_run"
397    )]
398    #[cfg_attr(
399        not(any(target_os = "android", target_os = "linux", target_os = "cygwin")),
400        doc = "```ignore"
401    )]
402    /// #![feature(unix_socket_ancillary_data)]
403    /// use std::os::unix::net::{UnixDatagram, SocketAncillary, AncillaryData};
404    /// use std::io::IoSliceMut;
405    ///
406    /// fn main() -> std::io::Result<()> {
407    ///     let sock = UnixDatagram::unbound()?;
408    ///     let mut buf1 = [1; 8];
409    ///     let mut buf2 = [2; 16];
410    ///     let mut buf3 = [3; 8];
411    ///     let mut bufs = &mut [
412    ///         IoSliceMut::new(&mut buf1),
413    ///         IoSliceMut::new(&mut buf2),
414    ///         IoSliceMut::new(&mut buf3),
415    ///     ][..];
416    ///     let mut fds = [0; 8];
417    ///     let mut ancillary_buffer = [0; 128];
418    ///     let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
419    ///     let (size, _truncated, sender) = sock.recv_vectored_with_ancillary_from(bufs, &mut ancillary)?;
420    ///     println!("received {size}");
421    ///     for ancillary_result in ancillary.messages() {
422    ///         if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
423    ///             for fd in scm_rights {
424    ///                 println!("receive file descriptor: {fd}");
425    ///             }
426    ///         }
427    ///     }
428    ///     Ok(())
429    /// }
430    /// ```
431    #[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "cygwin"))]
432    #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
433    pub fn recv_vectored_with_ancillary_from(
434        &self,
435        bufs: &mut [IoSliceMut<'_>],
436        ancillary: &mut SocketAncillary<'_>,
437    ) -> io::Result<(usize, bool, SocketAddr)> {
438        let (count, truncated, addr) = recv_vectored_with_ancillary_from(&self.0, bufs, ancillary)?;
439        let addr = addr?;
440
441        Ok((count, truncated, addr))
442    }
443
444    /// Receives data and ancillary data from socket.
445    ///
446    /// On success, returns the number of bytes read and if the data was truncated.
447    ///
448    /// # Examples
449    ///
450    #[cfg_attr(
451        any(target_os = "android", target_os = "linux", target_os = "cygwin"),
452        doc = "```no_run"
453    )]
454    #[cfg_attr(
455        not(any(target_os = "android", target_os = "linux", target_os = "cygwin")),
456        doc = "```ignore"
457    )]
458    /// #![feature(unix_socket_ancillary_data)]
459    /// use std::os::unix::net::{UnixDatagram, SocketAncillary, AncillaryData};
460    /// use std::io::IoSliceMut;
461    ///
462    /// fn main() -> std::io::Result<()> {
463    ///     let sock = UnixDatagram::unbound()?;
464    ///     let mut buf1 = [1; 8];
465    ///     let mut buf2 = [2; 16];
466    ///     let mut buf3 = [3; 8];
467    ///     let mut bufs = &mut [
468    ///         IoSliceMut::new(&mut buf1),
469    ///         IoSliceMut::new(&mut buf2),
470    ///         IoSliceMut::new(&mut buf3),
471    ///     ][..];
472    ///     let mut fds = [0; 8];
473    ///     let mut ancillary_buffer = [0; 128];
474    ///     let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
475    ///     let (size, _truncated) = sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
476    ///     println!("received {size}");
477    ///     for ancillary_result in ancillary.messages() {
478    ///         if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
479    ///             for fd in scm_rights {
480    ///                 println!("receive file descriptor: {fd}");
481    ///             }
482    ///         }
483    ///     }
484    ///     Ok(())
485    /// }
486    /// ```
487    #[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "cygwin"))]
488    #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
489    pub fn recv_vectored_with_ancillary(
490        &self,
491        bufs: &mut [IoSliceMut<'_>],
492        ancillary: &mut SocketAncillary<'_>,
493    ) -> io::Result<(usize, bool)> {
494        let (count, truncated, addr) = recv_vectored_with_ancillary_from(&self.0, bufs, ancillary)?;
495        addr?;
496
497        Ok((count, truncated))
498    }
499
500    /// Sends data on the socket to the specified address.
501    ///
502    /// On success, returns the number of bytes written.
503    ///
504    /// # Examples
505    ///
506    /// ```no_run
507    /// use std::os::unix::net::UnixDatagram;
508    ///
509    /// fn main() -> std::io::Result<()> {
510    ///     let sock = UnixDatagram::unbound()?;
511    ///     sock.send_to(b"omelette au fromage", "/some/sock").expect("send_to function failed");
512    ///     Ok(())
513    /// }
514    /// ```
515    #[stable(feature = "unix_socket", since = "1.10.0")]
516    pub fn send_to<P: AsRef<Path>>(&self, buf: &[u8], path: P) -> io::Result<usize> {
517        unsafe {
518            let (addr, len) = sockaddr_un(path.as_ref())?;
519
520            let count = cvt(libc::sendto(
521                self.as_raw_fd(),
522                buf.as_ptr() as *const _,
523                buf.len(),
524                MSG_NOSIGNAL,
525                (&raw const addr) as *const _,
526                len,
527            ))?;
528            Ok(count as usize)
529        }
530    }
531
532    /// Sends data on the socket to the specified [SocketAddr].
533    ///
534    /// On success, returns the number of bytes written.
535    ///
536    /// [SocketAddr]: crate::os::unix::net::SocketAddr
537    ///
538    /// # Examples
539    ///
540    /// ```no_run
541    /// use std::os::unix::net::{UnixDatagram};
542    ///
543    /// fn main() -> std::io::Result<()> {
544    ///     let bound = UnixDatagram::bind("/path/to/socket")?;
545    ///     let addr = bound.local_addr()?;
546    ///
547    ///     let sock = UnixDatagram::unbound()?;
548    ///     sock.send_to_addr(b"bacon egg and cheese", &addr).expect("send_to_addr function failed");
549    ///     Ok(())
550    /// }
551    /// ```
552    #[stable(feature = "unix_socket_abstract", since = "1.70.0")]
553    pub fn send_to_addr(&self, buf: &[u8], socket_addr: &SocketAddr) -> io::Result<usize> {
554        unsafe {
555            let count = cvt(libc::sendto(
556                self.as_raw_fd(),
557                buf.as_ptr() as *const _,
558                buf.len(),
559                MSG_NOSIGNAL,
560                (&raw const socket_addr.addr) as *const _,
561                socket_addr.len,
562            ))?;
563            Ok(count as usize)
564        }
565    }
566
567    /// Sends data on the socket to the socket's peer.
568    ///
569    /// The peer address may be set by the `connect` method, and this method
570    /// will return an error if the socket has not already been connected.
571    ///
572    /// On success, returns the number of bytes written.
573    ///
574    /// # Examples
575    ///
576    /// ```no_run
577    /// use std::os::unix::net::UnixDatagram;
578    ///
579    /// fn main() -> std::io::Result<()> {
580    ///     let sock = UnixDatagram::unbound()?;
581    ///     sock.connect("/some/sock").expect("Couldn't connect");
582    ///     sock.send(b"omelette au fromage").expect("send_to function failed");
583    ///     Ok(())
584    /// }
585    /// ```
586    #[stable(feature = "unix_socket", since = "1.10.0")]
587    pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
588        self.0.write(buf)
589    }
590
591    /// Sends data and ancillary data on the socket to the specified address.
592    ///
593    /// On success, returns the number of bytes written.
594    ///
595    /// # Examples
596    ///
597    #[cfg_attr(
598        any(target_os = "android", target_os = "linux", target_os = "cygwin"),
599        doc = "```no_run"
600    )]
601    #[cfg_attr(
602        not(any(target_os = "android", target_os = "linux", target_os = "cygwin")),
603        doc = "```ignore"
604    )]
605    /// #![feature(unix_socket_ancillary_data)]
606    /// use std::os::unix::net::{UnixDatagram, SocketAncillary};
607    /// use std::io::IoSlice;
608    ///
609    /// fn main() -> std::io::Result<()> {
610    ///     let sock = UnixDatagram::unbound()?;
611    ///     let buf1 = [1; 8];
612    ///     let buf2 = [2; 16];
613    ///     let buf3 = [3; 8];
614    ///     let bufs = &[
615    ///         IoSlice::new(&buf1),
616    ///         IoSlice::new(&buf2),
617    ///         IoSlice::new(&buf3),
618    ///     ][..];
619    ///     let fds = [0, 1, 2];
620    ///     let mut ancillary_buffer = [0; 128];
621    ///     let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
622    ///     ancillary.add_fds(&fds[..]);
623    ///     sock.send_vectored_with_ancillary_to(bufs, &mut ancillary, "/some/sock")
624    ///         .expect("send_vectored_with_ancillary_to function failed");
625    ///     Ok(())
626    /// }
627    /// ```
628    #[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "cygwin"))]
629    #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
630    pub fn send_vectored_with_ancillary_to<P: AsRef<Path>>(
631        &self,
632        bufs: &[IoSlice<'_>],
633        ancillary: &mut SocketAncillary<'_>,
634        path: P,
635    ) -> io::Result<usize> {
636        send_vectored_with_ancillary_to(&self.0, Some(path.as_ref()), bufs, ancillary)
637    }
638
639    /// Sends data and ancillary data on the socket.
640    ///
641    /// On success, returns the number of bytes written.
642    ///
643    /// # Examples
644    ///
645    #[cfg_attr(
646        any(target_os = "android", target_os = "linux", target_os = "cygwin"),
647        doc = "```no_run"
648    )]
649    #[cfg_attr(
650        not(any(target_os = "android", target_os = "linux", target_os = "cygwin")),
651        doc = "```ignore"
652    )]
653    /// #![feature(unix_socket_ancillary_data)]
654    /// use std::os::unix::net::{UnixDatagram, SocketAncillary};
655    /// use std::io::IoSlice;
656    ///
657    /// fn main() -> std::io::Result<()> {
658    ///     let sock = UnixDatagram::unbound()?;
659    ///     let buf1 = [1; 8];
660    ///     let buf2 = [2; 16];
661    ///     let buf3 = [3; 8];
662    ///     let bufs = &[
663    ///         IoSlice::new(&buf1),
664    ///         IoSlice::new(&buf2),
665    ///         IoSlice::new(&buf3),
666    ///     ][..];
667    ///     let fds = [0, 1, 2];
668    ///     let mut ancillary_buffer = [0; 128];
669    ///     let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
670    ///     ancillary.add_fds(&fds[..]);
671    ///     sock.send_vectored_with_ancillary(bufs, &mut ancillary)
672    ///         .expect("send_vectored_with_ancillary function failed");
673    ///     Ok(())
674    /// }
675    /// ```
676    #[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "cygwin"))]
677    #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
678    pub fn send_vectored_with_ancillary(
679        &self,
680        bufs: &[IoSlice<'_>],
681        ancillary: &mut SocketAncillary<'_>,
682    ) -> io::Result<usize> {
683        send_vectored_with_ancillary_to(&self.0, None, bufs, ancillary)
684    }
685
686    /// Sets the read timeout for the socket.
687    ///
688    /// If the provided value is [`None`], then [`recv`] and [`recv_from`] calls will
689    /// block indefinitely. An [`Err`] is returned if the zero [`Duration`]
690    /// is passed to this method.
691    ///
692    /// [`recv`]: UnixDatagram::recv
693    /// [`recv_from`]: UnixDatagram::recv_from
694    ///
695    /// # Examples
696    ///
697    /// ```
698    /// use std::os::unix::net::UnixDatagram;
699    /// use std::time::Duration;
700    ///
701    /// fn main() -> std::io::Result<()> {
702    ///     let sock = UnixDatagram::unbound()?;
703    ///     sock.set_read_timeout(Some(Duration::new(1, 0)))
704    ///         .expect("set_read_timeout function failed");
705    ///     Ok(())
706    /// }
707    /// ```
708    ///
709    /// An [`Err`] is returned if the zero [`Duration`] is passed to this
710    /// method:
711    ///
712    /// ```no_run
713    /// use std::io;
714    /// use std::os::unix::net::UnixDatagram;
715    /// use std::time::Duration;
716    ///
717    /// fn main() -> std::io::Result<()> {
718    ///     let socket = UnixDatagram::unbound()?;
719    ///     let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
720    ///     let err = result.unwrap_err();
721    ///     assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
722    ///     Ok(())
723    /// }
724    /// ```
725    #[stable(feature = "unix_socket", since = "1.10.0")]
726    pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
727        self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
728    }
729
730    /// Sets the write timeout for the socket.
731    ///
732    /// If the provided value is [`None`], then [`send`] and [`send_to`] calls will
733    /// block indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
734    /// method.
735    ///
736    /// [`send`]: UnixDatagram::send
737    /// [`send_to`]: UnixDatagram::send_to
738    ///
739    /// # Examples
740    ///
741    /// ```
742    /// use std::os::unix::net::UnixDatagram;
743    /// use std::time::Duration;
744    ///
745    /// fn main() -> std::io::Result<()> {
746    ///     let sock = UnixDatagram::unbound()?;
747    ///     sock.set_write_timeout(Some(Duration::new(1, 0)))
748    ///         .expect("set_write_timeout function failed");
749    ///     Ok(())
750    /// }
751    /// ```
752    ///
753    /// An [`Err`] is returned if the zero [`Duration`] is passed to this
754    /// method:
755    ///
756    /// ```no_run
757    /// use std::io;
758    /// use std::os::unix::net::UnixDatagram;
759    /// use std::time::Duration;
760    ///
761    /// fn main() -> std::io::Result<()> {
762    ///     let socket = UnixDatagram::unbound()?;
763    ///     let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
764    ///     let err = result.unwrap_err();
765    ///     assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
766    ///     Ok(())
767    /// }
768    /// ```
769    #[stable(feature = "unix_socket", since = "1.10.0")]
770    pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
771        self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
772    }
773
774    /// Returns the read timeout of this socket.
775    ///
776    /// # Examples
777    ///
778    // Ferrocene annotation: QNX does not return the same write_timeout as set
779    /// ```ignore-qnx
780    /// use std::os::unix::net::UnixDatagram;
781    /// use std::time::Duration;
782    ///
783    /// fn main() -> std::io::Result<()> {
784    ///     let sock = UnixDatagram::unbound()?;
785    ///     sock.set_read_timeout(Some(Duration::new(1, 0)))
786    ///         .expect("set_read_timeout function failed");
787    ///     assert_eq!(sock.read_timeout()?, Some(Duration::new(1, 0)));
788    ///     Ok(())
789    /// }
790    /// ```
791    #[stable(feature = "unix_socket", since = "1.10.0")]
792    pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
793        self.0.timeout(libc::SO_RCVTIMEO)
794    }
795
796    /// Returns the write timeout of this socket.
797    ///
798    /// # Examples
799    ///
800    // Ferrocene annotation: QNX does not return the same write_timeout as set
801    /// ```ignore-qnx
802    /// use std::os::unix::net::UnixDatagram;
803    /// use std::time::Duration;
804    ///
805    /// fn main() -> std::io::Result<()> {
806    ///     let sock = UnixDatagram::unbound()?;
807    ///     sock.set_write_timeout(Some(Duration::new(1, 0)))
808    ///         .expect("set_write_timeout function failed");
809    ///     assert_eq!(sock.write_timeout()?, Some(Duration::new(1, 0)));
810    ///     Ok(())
811    /// }
812    /// ```
813    #[stable(feature = "unix_socket", since = "1.10.0")]
814    pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
815        self.0.timeout(libc::SO_SNDTIMEO)
816    }
817
818    /// Moves the socket into or out of nonblocking mode.
819    ///
820    /// # Examples
821    ///
822    /// ```
823    /// use std::os::unix::net::UnixDatagram;
824    ///
825    /// fn main() -> std::io::Result<()> {
826    ///     let sock = UnixDatagram::unbound()?;
827    ///     sock.set_nonblocking(true).expect("set_nonblocking function failed");
828    ///     Ok(())
829    /// }
830    /// ```
831    #[stable(feature = "unix_socket", since = "1.10.0")]
832    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
833        self.0.set_nonblocking(nonblocking)
834    }
835
836    /// Set the id of the socket for network filtering purpose
837    ///
838    #[cfg_attr(
839        any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"),
840        doc = "```no_run"
841    )]
842    #[cfg_attr(
843        not(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd")),
844        doc = "```ignore"
845    )]
846    /// #![feature(unix_set_mark)]
847    /// use std::os::unix::net::UnixDatagram;
848    ///
849    /// fn main() -> std::io::Result<()> {
850    ///     let sock = UnixDatagram::unbound()?;
851    ///     sock.set_mark(32)?;
852    ///     Ok(())
853    /// }
854    /// ```
855    #[cfg(any(doc, target_os = "linux", target_os = "freebsd", target_os = "openbsd",))]
856    #[unstable(feature = "unix_set_mark", issue = "96467")]
857    pub fn set_mark(&self, mark: u32) -> io::Result<()> {
858        self.0.set_mark(mark)
859    }
860
861    /// Returns the value of the `SO_ERROR` option.
862    ///
863    /// # Examples
864    ///
865    /// ```no_run
866    /// use std::os::unix::net::UnixDatagram;
867    ///
868    /// fn main() -> std::io::Result<()> {
869    ///     let sock = UnixDatagram::unbound()?;
870    ///     if let Ok(Some(err)) = sock.take_error() {
871    ///         println!("Got error: {err:?}");
872    ///     }
873    ///     Ok(())
874    /// }
875    /// ```
876    #[stable(feature = "unix_socket", since = "1.10.0")]
877    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
878        self.0.take_error()
879    }
880
881    /// Shut down the read, write, or both halves of this connection.
882    ///
883    /// This function will cause all pending and future I/O calls on the
884    /// specified portions to immediately return with an appropriate value
885    /// (see the documentation of [`Shutdown`]).
886    ///
887    /// ```no_run
888    /// use std::os::unix::net::UnixDatagram;
889    /// use std::net::Shutdown;
890    ///
891    /// fn main() -> std::io::Result<()> {
892    ///     let sock = UnixDatagram::unbound()?;
893    ///     sock.shutdown(Shutdown::Both).expect("shutdown function failed");
894    ///     Ok(())
895    /// }
896    /// ```
897    #[stable(feature = "unix_socket", since = "1.10.0")]
898    pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
899        self.0.shutdown(how)
900    }
901
902    /// Receives data on the socket from the remote address to which it is
903    /// connected, without removing that data from the queue. On success,
904    /// returns the number of bytes peeked.
905    ///
906    /// Successive calls return the same data. This is accomplished by passing
907    /// `MSG_PEEK` as a flag to the underlying `recv` system call.
908    ///
909    /// # Examples
910    ///
911    /// ```no_run
912    /// #![feature(unix_socket_peek)]
913    ///
914    /// use std::os::unix::net::UnixDatagram;
915    ///
916    /// fn main() -> std::io::Result<()> {
917    ///     let socket = UnixDatagram::bind("/tmp/sock")?;
918    ///     let mut buf = [0; 10];
919    ///     let len = socket.peek(&mut buf).expect("peek failed");
920    ///     Ok(())
921    /// }
922    /// ```
923    #[unstable(feature = "unix_socket_peek", issue = "76923")]
924    pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
925        self.0.peek(buf)
926    }
927
928    /// Receives a single datagram message on the socket, without removing it from the
929    /// queue. On success, returns the number of bytes read and the origin.
930    ///
931    /// The function must be called with valid byte array `buf` of sufficient size to
932    /// hold the message bytes. If a message is too long to fit in the supplied buffer,
933    /// excess bytes may be discarded.
934    ///
935    /// Successive calls return the same data. This is accomplished by passing
936    /// `MSG_PEEK` as a flag to the underlying `recvfrom` system call.
937    ///
938    /// Do not use this function to implement busy waiting, instead use `libc::poll` to
939    /// synchronize IO events on one or more sockets.
940    ///
941    /// # Examples
942    ///
943    /// ```no_run
944    /// #![feature(unix_socket_peek)]
945    ///
946    /// use std::os::unix::net::UnixDatagram;
947    ///
948    /// fn main() -> std::io::Result<()> {
949    ///     let socket = UnixDatagram::bind("/tmp/sock")?;
950    ///     let mut buf = [0; 10];
951    ///     let (len, addr) = socket.peek_from(&mut buf).expect("peek failed");
952    ///     Ok(())
953    /// }
954    /// ```
955    #[unstable(feature = "unix_socket_peek", issue = "76923")]
956    pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
957        self.recv_from_flags(buf, libc::MSG_PEEK)
958    }
959}
960
961#[stable(feature = "unix_socket", since = "1.10.0")]
962impl AsRawFd for UnixDatagram {
963    #[inline]
964    fn as_raw_fd(&self) -> RawFd {
965        self.0.as_inner().as_raw_fd()
966    }
967}
968
969#[stable(feature = "unix_socket", since = "1.10.0")]
970impl FromRawFd for UnixDatagram {
971    #[inline]
972    unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram {
973        UnixDatagram(Socket::from_inner(FromInner::from_inner(OwnedFd::from_raw_fd(fd))))
974    }
975}
976
977#[stable(feature = "unix_socket", since = "1.10.0")]
978impl IntoRawFd for UnixDatagram {
979    #[inline]
980    fn into_raw_fd(self) -> RawFd {
981        self.0.into_inner().into_inner().into_raw_fd()
982    }
983}
984
985#[stable(feature = "io_safety", since = "1.63.0")]
986impl AsFd for UnixDatagram {
987    #[inline]
988    fn as_fd(&self) -> BorrowedFd<'_> {
989        self.0.as_inner().as_fd()
990    }
991}
992
993#[stable(feature = "io_safety", since = "1.63.0")]
994impl From<UnixDatagram> for OwnedFd {
995    /// Takes ownership of a [`UnixDatagram`]'s socket file descriptor.
996    #[inline]
997    fn from(unix_datagram: UnixDatagram) -> OwnedFd {
998        unsafe { OwnedFd::from_raw_fd(unix_datagram.into_raw_fd()) }
999    }
1000}
1001
1002#[stable(feature = "io_safety", since = "1.63.0")]
1003impl From<OwnedFd> for UnixDatagram {
1004    #[inline]
1005    fn from(owned: OwnedFd) -> Self {
1006        unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
1007    }
1008}
1009
1010impl AsInner<Socket> for UnixDatagram {
1011    #[inline]
1012    fn as_inner(&self) -> &Socket {
1013        &self.0
1014    }
1015}