]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/unix/ext/net.rs
Add a comment why rustdoc loads crates from the sysroot
[rust.git] / library / std / src / sys / unix / ext / net.rs
1 //! Unix-specific networking functionality.
2
3 #![stable(feature = "unix_socket", since = "1.10.0")]
4
5 #[cfg(all(test, not(target_os = "emscripten")))]
6 mod tests;
7
8 // FIXME(#43348): Make libc adapt #[doc(cfg(...))] so we don't need these fake definitions here?
9 #[cfg(not(unix))]
10 #[allow(non_camel_case_types)]
11 mod libc {
12     pub use libc::c_int;
13     pub type socklen_t = u32;
14     pub struct sockaddr;
15     #[derive(Clone)]
16     pub struct sockaddr_un;
17 }
18
19 use crate::ascii;
20 use crate::ffi::OsStr;
21 use crate::fmt;
22 use crate::io::{self, Initializer, IoSlice, IoSliceMut};
23 use crate::mem;
24 use crate::net::{self, Shutdown};
25 use crate::os::unix::ffi::OsStrExt;
26 use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
27 use crate::path::Path;
28 use crate::sys::net::Socket;
29 use crate::sys::{self, cvt};
30 use crate::sys_common::{self, AsInner, FromInner, IntoInner};
31 use crate::time::Duration;
32
33 #[cfg(any(
34     target_os = "linux",
35     target_os = "android",
36     target_os = "dragonfly",
37     target_os = "freebsd",
38     target_os = "openbsd",
39     target_os = "netbsd",
40     target_os = "haiku"
41 ))]
42 use libc::MSG_NOSIGNAL;
43 #[cfg(not(any(
44     target_os = "linux",
45     target_os = "android",
46     target_os = "dragonfly",
47     target_os = "freebsd",
48     target_os = "openbsd",
49     target_os = "netbsd",
50     target_os = "haiku"
51 )))]
52 const MSG_NOSIGNAL: libc::c_int = 0x0;
53
54 fn sun_path_offset(addr: &libc::sockaddr_un) -> usize {
55     // Work with an actual instance of the type since using a null pointer is UB
56     let base = addr as *const _ as usize;
57     let path = &addr.sun_path as *const _ as usize;
58     path - base
59 }
60
61 unsafe fn sockaddr_un(path: &Path) -> io::Result<(libc::sockaddr_un, libc::socklen_t)> {
62     let mut addr: libc::sockaddr_un = mem::zeroed();
63     addr.sun_family = libc::AF_UNIX as libc::sa_family_t;
64
65     let bytes = path.as_os_str().as_bytes();
66
67     if bytes.contains(&0) {
68         return Err(io::Error::new(
69             io::ErrorKind::InvalidInput,
70             "paths may not contain interior null bytes",
71         ));
72     }
73
74     if bytes.len() >= addr.sun_path.len() {
75         return Err(io::Error::new(
76             io::ErrorKind::InvalidInput,
77             "path must be shorter than SUN_LEN",
78         ));
79     }
80     for (dst, src) in addr.sun_path.iter_mut().zip(bytes.iter()) {
81         *dst = *src as libc::c_char;
82     }
83     // null byte for pathname addresses is already there because we zeroed the
84     // struct
85
86     let mut len = sun_path_offset(&addr) + bytes.len();
87     match bytes.get(0) {
88         Some(&0) | None => {}
89         Some(_) => len += 1,
90     }
91     Ok((addr, len as libc::socklen_t))
92 }
93
94 enum AddressKind<'a> {
95     Unnamed,
96     Pathname(&'a Path),
97     Abstract(&'a [u8]),
98 }
99
100 /// An address associated with a Unix socket.
101 ///
102 /// # Examples
103 ///
104 /// ```
105 /// use std::os::unix::net::UnixListener;
106 ///
107 /// let socket = match UnixListener::bind("/tmp/sock") {
108 ///     Ok(sock) => sock,
109 ///     Err(e) => {
110 ///         println!("Couldn't bind: {:?}", e);
111 ///         return
112 ///     }
113 /// };
114 /// let addr = socket.local_addr().expect("Couldn't get local address");
115 /// ```
116 #[derive(Clone)]
117 #[stable(feature = "unix_socket", since = "1.10.0")]
118 pub struct SocketAddr {
119     addr: libc::sockaddr_un,
120     len: libc::socklen_t,
121 }
122
123 impl SocketAddr {
124     fn new<F>(f: F) -> io::Result<SocketAddr>
125     where
126         F: FnOnce(*mut libc::sockaddr, *mut libc::socklen_t) -> libc::c_int,
127     {
128         unsafe {
129             let mut addr: libc::sockaddr_un = mem::zeroed();
130             let mut len = mem::size_of::<libc::sockaddr_un>() as libc::socklen_t;
131             cvt(f(&mut addr as *mut _ as *mut _, &mut len))?;
132             SocketAddr::from_parts(addr, len)
133         }
134     }
135
136     fn from_parts(addr: libc::sockaddr_un, mut len: libc::socklen_t) -> io::Result<SocketAddr> {
137         if len == 0 {
138             // When there is a datagram from unnamed unix socket
139             // linux returns zero bytes of address
140             len = sun_path_offset(&addr) as libc::socklen_t; // i.e., zero-length address
141         } else if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {
142             return Err(io::Error::new(
143                 io::ErrorKind::InvalidInput,
144                 "file descriptor did not correspond to a Unix socket",
145             ));
146         }
147
148         Ok(SocketAddr { addr, len })
149     }
150
151     /// Returns `true` if the address is unnamed.
152     ///
153     /// # Examples
154     ///
155     /// A named address:
156     ///
157     /// ```no_run
158     /// use std::os::unix::net::UnixListener;
159     ///
160     /// fn main() -> std::io::Result<()> {
161     ///     let socket = UnixListener::bind("/tmp/sock")?;
162     ///     let addr = socket.local_addr().expect("Couldn't get local address");
163     ///     assert_eq!(addr.is_unnamed(), false);
164     ///     Ok(())
165     /// }
166     /// ```
167     ///
168     /// An unnamed address:
169     ///
170     /// ```
171     /// use std::os::unix::net::UnixDatagram;
172     ///
173     /// fn main() -> std::io::Result<()> {
174     ///     let socket = UnixDatagram::unbound()?;
175     ///     let addr = socket.local_addr().expect("Couldn't get local address");
176     ///     assert_eq!(addr.is_unnamed(), true);
177     ///     Ok(())
178     /// }
179     /// ```
180     #[stable(feature = "unix_socket", since = "1.10.0")]
181     pub fn is_unnamed(&self) -> bool {
182         if let AddressKind::Unnamed = self.address() { true } else { false }
183     }
184
185     /// Returns the contents of this address if it is a `pathname` address.
186     ///
187     /// # Examples
188     ///
189     /// With a pathname:
190     ///
191     /// ```no_run
192     /// use std::os::unix::net::UnixListener;
193     /// use std::path::Path;
194     ///
195     /// fn main() -> std::io::Result<()> {
196     ///     let socket = UnixListener::bind("/tmp/sock")?;
197     ///     let addr = socket.local_addr().expect("Couldn't get local address");
198     ///     assert_eq!(addr.as_pathname(), Some(Path::new("/tmp/sock")));
199     ///     Ok(())
200     /// }
201     /// ```
202     ///
203     /// Without a pathname:
204     ///
205     /// ```
206     /// use std::os::unix::net::UnixDatagram;
207     ///
208     /// fn main() -> std::io::Result<()> {
209     ///     let socket = UnixDatagram::unbound()?;
210     ///     let addr = socket.local_addr().expect("Couldn't get local address");
211     ///     assert_eq!(addr.as_pathname(), None);
212     ///     Ok(())
213     /// }
214     /// ```
215     #[stable(feature = "unix_socket", since = "1.10.0")]
216     pub fn as_pathname(&self) -> Option<&Path> {
217         if let AddressKind::Pathname(path) = self.address() { Some(path) } else { None }
218     }
219
220     fn address(&self) -> AddressKind<'_> {
221         let len = self.len as usize - sun_path_offset(&self.addr);
222         let path = unsafe { mem::transmute::<&[libc::c_char], &[u8]>(&self.addr.sun_path) };
223
224         // macOS seems to return a len of 16 and a zeroed sun_path for unnamed addresses
225         if len == 0
226             || (cfg!(not(any(target_os = "linux", target_os = "android")))
227                 && self.addr.sun_path[0] == 0)
228         {
229             AddressKind::Unnamed
230         } else if self.addr.sun_path[0] == 0 {
231             AddressKind::Abstract(&path[1..len])
232         } else {
233             AddressKind::Pathname(OsStr::from_bytes(&path[..len - 1]).as_ref())
234         }
235     }
236 }
237
238 #[stable(feature = "unix_socket", since = "1.10.0")]
239 impl fmt::Debug for SocketAddr {
240     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
241         match self.address() {
242             AddressKind::Unnamed => write!(fmt, "(unnamed)"),
243             AddressKind::Abstract(name) => write!(fmt, "{} (abstract)", AsciiEscaped(name)),
244             AddressKind::Pathname(path) => write!(fmt, "{:?} (pathname)", path),
245         }
246     }
247 }
248
249 struct AsciiEscaped<'a>(&'a [u8]);
250
251 impl<'a> fmt::Display for AsciiEscaped<'a> {
252     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
253         write!(fmt, "\"")?;
254         for byte in self.0.iter().cloned().flat_map(ascii::escape_default) {
255             write!(fmt, "{}", byte as char)?;
256         }
257         write!(fmt, "\"")
258     }
259 }
260
261 /// A Unix stream socket.
262 ///
263 /// # Examples
264 ///
265 /// ```no_run
266 /// use std::os::unix::net::UnixStream;
267 /// use std::io::prelude::*;
268 ///
269 /// fn main() -> std::io::Result<()> {
270 ///     let mut stream = UnixStream::connect("/path/to/my/socket")?;
271 ///     stream.write_all(b"hello world")?;
272 ///     let mut response = String::new();
273 ///     stream.read_to_string(&mut response)?;
274 ///     println!("{}", response);
275 ///     Ok(())
276 /// }
277 /// ```
278 #[stable(feature = "unix_socket", since = "1.10.0")]
279 pub struct UnixStream(Socket);
280
281 #[stable(feature = "unix_socket", since = "1.10.0")]
282 impl fmt::Debug for UnixStream {
283     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
284         let mut builder = fmt.debug_struct("UnixStream");
285         builder.field("fd", self.0.as_inner());
286         if let Ok(addr) = self.local_addr() {
287             builder.field("local", &addr);
288         }
289         if let Ok(addr) = self.peer_addr() {
290             builder.field("peer", &addr);
291         }
292         builder.finish()
293     }
294 }
295
296 impl UnixStream {
297     /// Connects to the socket named by `path`.
298     ///
299     /// # Examples
300     ///
301     /// ```no_run
302     /// use std::os::unix::net::UnixStream;
303     ///
304     /// let socket = match UnixStream::connect("/tmp/sock") {
305     ///     Ok(sock) => sock,
306     ///     Err(e) => {
307     ///         println!("Couldn't connect: {:?}", e);
308     ///         return
309     ///     }
310     /// };
311     /// ```
312     #[stable(feature = "unix_socket", since = "1.10.0")]
313     pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
314         fn inner(path: &Path) -> io::Result<UnixStream> {
315             unsafe {
316                 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
317                 let (addr, len) = sockaddr_un(path)?;
318
319                 cvt(libc::connect(*inner.as_inner(), &addr as *const _ as *const _, len))?;
320                 Ok(UnixStream(inner))
321             }
322         }
323         inner(path.as_ref())
324     }
325
326     /// Creates an unnamed pair of connected sockets.
327     ///
328     /// Returns two `UnixStream`s which are connected to each other.
329     ///
330     /// # Examples
331     ///
332     /// ```no_run
333     /// use std::os::unix::net::UnixStream;
334     ///
335     /// let (sock1, sock2) = match UnixStream::pair() {
336     ///     Ok((sock1, sock2)) => (sock1, sock2),
337     ///     Err(e) => {
338     ///         println!("Couldn't create a pair of sockets: {:?}", e);
339     ///         return
340     ///     }
341     /// };
342     /// ```
343     #[stable(feature = "unix_socket", since = "1.10.0")]
344     pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
345         let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_STREAM)?;
346         Ok((UnixStream(i1), UnixStream(i2)))
347     }
348
349     /// Creates a new independently owned handle to the underlying socket.
350     ///
351     /// The returned `UnixStream` is a reference to the same stream that this
352     /// object references. Both handles will read and write the same stream of
353     /// data, and options set on one stream will be propagated to the other
354     /// stream.
355     ///
356     /// # Examples
357     ///
358     /// ```no_run
359     /// use std::os::unix::net::UnixStream;
360     ///
361     /// fn main() -> std::io::Result<()> {
362     ///     let socket = UnixStream::connect("/tmp/sock")?;
363     ///     let sock_copy = socket.try_clone().expect("Couldn't clone socket");
364     ///     Ok(())
365     /// }
366     /// ```
367     #[stable(feature = "unix_socket", since = "1.10.0")]
368     pub fn try_clone(&self) -> io::Result<UnixStream> {
369         self.0.duplicate().map(UnixStream)
370     }
371
372     /// Returns the socket address of the local half of this connection.
373     ///
374     /// # Examples
375     ///
376     /// ```no_run
377     /// use std::os::unix::net::UnixStream;
378     ///
379     /// fn main() -> std::io::Result<()> {
380     ///     let socket = UnixStream::connect("/tmp/sock")?;
381     ///     let addr = socket.local_addr().expect("Couldn't get local address");
382     ///     Ok(())
383     /// }
384     /// ```
385     #[stable(feature = "unix_socket", since = "1.10.0")]
386     pub fn local_addr(&self) -> io::Result<SocketAddr> {
387         SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
388     }
389
390     /// Returns the socket address of the remote half of this connection.
391     ///
392     /// # Examples
393     ///
394     /// ```no_run
395     /// use std::os::unix::net::UnixStream;
396     ///
397     /// fn main() -> std::io::Result<()> {
398     ///     let socket = UnixStream::connect("/tmp/sock")?;
399     ///     let addr = socket.peer_addr().expect("Couldn't get peer address");
400     ///     Ok(())
401     /// }
402     /// ```
403     #[stable(feature = "unix_socket", since = "1.10.0")]
404     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
405         SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
406     }
407
408     /// Sets the read timeout for the socket.
409     ///
410     /// If the provided value is [`None`], then [`read`] calls will block
411     /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
412     /// method.
413     ///
414     /// [`read`]: io::Read::read
415     ///
416     /// # Examples
417     ///
418     /// ```no_run
419     /// use std::os::unix::net::UnixStream;
420     /// use std::time::Duration;
421     ///
422     /// fn main() -> std::io::Result<()> {
423     ///     let socket = UnixStream::connect("/tmp/sock")?;
424     ///     socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
425     ///     Ok(())
426     /// }
427     /// ```
428     ///
429     /// An [`Err`] is returned if the zero [`Duration`] is passed to this
430     /// method:
431     ///
432     /// ```no_run
433     /// use std::io;
434     /// use std::os::unix::net::UnixStream;
435     /// use std::time::Duration;
436     ///
437     /// fn main() -> std::io::Result<()> {
438     ///     let socket = UnixStream::connect("/tmp/sock")?;
439     ///     let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
440     ///     let err = result.unwrap_err();
441     ///     assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
442     ///     Ok(())
443     /// }
444     /// ```
445     #[stable(feature = "unix_socket", since = "1.10.0")]
446     pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
447         self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
448     }
449
450     /// Sets the write timeout for the socket.
451     ///
452     /// If the provided value is [`None`], then [`write`] calls will block
453     /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is
454     /// passed to this method.
455     ///
456     /// [`read`]: io::Read::read
457     ///
458     /// # Examples
459     ///
460     /// ```no_run
461     /// use std::os::unix::net::UnixStream;
462     /// use std::time::Duration;
463     ///
464     /// fn main() -> std::io::Result<()> {
465     ///     let socket = UnixStream::connect("/tmp/sock")?;
466     ///     socket.set_write_timeout(Some(Duration::new(1, 0)))
467     ///         .expect("Couldn't set write timeout");
468     ///     Ok(())
469     /// }
470     /// ```
471     ///
472     /// An [`Err`] is returned if the zero [`Duration`] is passed to this
473     /// method:
474     ///
475     /// ```no_run
476     /// use std::io;
477     /// use std::net::UdpSocket;
478     /// use std::time::Duration;
479     ///
480     /// fn main() -> std::io::Result<()> {
481     ///     let socket = UdpSocket::bind("127.0.0.1:34254")?;
482     ///     let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
483     ///     let err = result.unwrap_err();
484     ///     assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
485     ///     Ok(())
486     /// }
487     /// ```
488     #[stable(feature = "unix_socket", since = "1.10.0")]
489     pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
490         self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
491     }
492
493     /// Returns the read timeout of this socket.
494     ///
495     /// # Examples
496     ///
497     /// ```no_run
498     /// use std::os::unix::net::UnixStream;
499     /// use std::time::Duration;
500     ///
501     /// fn main() -> std::io::Result<()> {
502     ///     let socket = UnixStream::connect("/tmp/sock")?;
503     ///     socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
504     ///     assert_eq!(socket.read_timeout()?, Some(Duration::new(1, 0)));
505     ///     Ok(())
506     /// }
507     /// ```
508     #[stable(feature = "unix_socket", since = "1.10.0")]
509     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
510         self.0.timeout(libc::SO_RCVTIMEO)
511     }
512
513     /// Returns the write timeout of this socket.
514     ///
515     /// # Examples
516     ///
517     /// ```no_run
518     /// use std::os::unix::net::UnixStream;
519     /// use std::time::Duration;
520     ///
521     /// fn main() -> std::io::Result<()> {
522     ///     let socket = UnixStream::connect("/tmp/sock")?;
523     ///     socket.set_write_timeout(Some(Duration::new(1, 0)))
524     ///         .expect("Couldn't set write timeout");
525     ///     assert_eq!(socket.write_timeout()?, Some(Duration::new(1, 0)));
526     ///     Ok(())
527     /// }
528     /// ```
529     #[stable(feature = "unix_socket", since = "1.10.0")]
530     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
531         self.0.timeout(libc::SO_SNDTIMEO)
532     }
533
534     /// Moves the socket into or out of nonblocking mode.
535     ///
536     /// # Examples
537     ///
538     /// ```no_run
539     /// use std::os::unix::net::UnixStream;
540     ///
541     /// fn main() -> std::io::Result<()> {
542     ///     let socket = UnixStream::connect("/tmp/sock")?;
543     ///     socket.set_nonblocking(true).expect("Couldn't set nonblocking");
544     ///     Ok(())
545     /// }
546     /// ```
547     #[stable(feature = "unix_socket", since = "1.10.0")]
548     pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
549         self.0.set_nonblocking(nonblocking)
550     }
551
552     /// Returns the value of the `SO_ERROR` option.
553     ///
554     /// # Examples
555     ///
556     /// ```no_run
557     /// use std::os::unix::net::UnixStream;
558     ///
559     /// fn main() -> std::io::Result<()> {
560     ///     let socket = UnixStream::connect("/tmp/sock")?;
561     ///     if let Ok(Some(err)) = socket.take_error() {
562     ///         println!("Got error: {:?}", err);
563     ///     }
564     ///     Ok(())
565     /// }
566     /// ```
567     ///
568     /// # Platform specific
569     /// On Redox this always returns `None`.
570     #[stable(feature = "unix_socket", since = "1.10.0")]
571     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
572         self.0.take_error()
573     }
574
575     /// Shuts down the read, write, or both halves of this connection.
576     ///
577     /// This function will cause all pending and future I/O calls on the
578     /// specified portions to immediately return with an appropriate value
579     /// (see the documentation of [`Shutdown`]).
580     ///
581     /// # Examples
582     ///
583     /// ```no_run
584     /// use std::os::unix::net::UnixStream;
585     /// use std::net::Shutdown;
586     ///
587     /// fn main() -> std::io::Result<()> {
588     ///     let socket = UnixStream::connect("/tmp/sock")?;
589     ///     socket.shutdown(Shutdown::Both).expect("shutdown function failed");
590     ///     Ok(())
591     /// }
592     /// ```
593     #[stable(feature = "unix_socket", since = "1.10.0")]
594     pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
595         self.0.shutdown(how)
596     }
597
598     /// Receives data on the socket from the remote address to which it is
599     /// connected, without removing that data from the queue. On success,
600     /// returns the number of bytes peeked.
601     ///
602     /// Successive calls return the same data. This is accomplished by passing
603     /// `MSG_PEEK` as a flag to the underlying `recv` system call.
604     ///
605     /// # Examples
606     ///
607     /// ```no_run
608     /// #![feature(unix_socket_peek)]
609     ///
610     /// use std::os::unix::net::UnixStream;
611     ///
612     /// fn main() -> std::io::Result<()> {
613     ///     let socket = UnixStream::connect("/tmp/sock")?;
614     ///     let mut buf = [0; 10];
615     ///     let len = socket.peek(&mut buf).expect("peek failed");
616     ///     Ok(())
617     /// }
618     /// ```
619     #[unstable(feature = "unix_socket_peek", issue = "none")]
620     pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
621         self.0.peek(buf)
622     }
623 }
624
625 #[stable(feature = "unix_socket", since = "1.10.0")]
626 impl io::Read for UnixStream {
627     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
628         io::Read::read(&mut &*self, buf)
629     }
630
631     fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
632         io::Read::read_vectored(&mut &*self, bufs)
633     }
634
635     #[inline]
636     fn is_read_vectored(&self) -> bool {
637         io::Read::is_read_vectored(&&*self)
638     }
639
640     #[inline]
641     unsafe fn initializer(&self) -> Initializer {
642         Initializer::nop()
643     }
644 }
645
646 #[stable(feature = "unix_socket", since = "1.10.0")]
647 impl<'a> io::Read for &'a UnixStream {
648     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
649         self.0.read(buf)
650     }
651
652     fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
653         self.0.read_vectored(bufs)
654     }
655
656     #[inline]
657     fn is_read_vectored(&self) -> bool {
658         self.0.is_read_vectored()
659     }
660
661     #[inline]
662     unsafe fn initializer(&self) -> Initializer {
663         Initializer::nop()
664     }
665 }
666
667 #[stable(feature = "unix_socket", since = "1.10.0")]
668 impl io::Write for UnixStream {
669     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
670         io::Write::write(&mut &*self, buf)
671     }
672
673     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
674         io::Write::write_vectored(&mut &*self, bufs)
675     }
676
677     #[inline]
678     fn is_write_vectored(&self) -> bool {
679         io::Write::is_write_vectored(&&*self)
680     }
681
682     fn flush(&mut self) -> io::Result<()> {
683         io::Write::flush(&mut &*self)
684     }
685 }
686
687 #[stable(feature = "unix_socket", since = "1.10.0")]
688 impl<'a> io::Write for &'a UnixStream {
689     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
690         self.0.write(buf)
691     }
692
693     fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
694         self.0.write_vectored(bufs)
695     }
696
697     #[inline]
698     fn is_write_vectored(&self) -> bool {
699         self.0.is_write_vectored()
700     }
701
702     fn flush(&mut self) -> io::Result<()> {
703         Ok(())
704     }
705 }
706
707 #[stable(feature = "unix_socket", since = "1.10.0")]
708 impl AsRawFd for UnixStream {
709     fn as_raw_fd(&self) -> RawFd {
710         *self.0.as_inner()
711     }
712 }
713
714 #[stable(feature = "unix_socket", since = "1.10.0")]
715 impl FromRawFd for UnixStream {
716     unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
717         UnixStream(Socket::from_inner(fd))
718     }
719 }
720
721 #[stable(feature = "unix_socket", since = "1.10.0")]
722 impl IntoRawFd for UnixStream {
723     fn into_raw_fd(self) -> RawFd {
724         self.0.into_inner()
725     }
726 }
727
728 #[stable(feature = "rust1", since = "1.0.0")]
729 impl AsRawFd for net::TcpStream {
730     fn as_raw_fd(&self) -> RawFd {
731         *self.as_inner().socket().as_inner()
732     }
733 }
734
735 #[stable(feature = "rust1", since = "1.0.0")]
736 impl AsRawFd for net::TcpListener {
737     fn as_raw_fd(&self) -> RawFd {
738         *self.as_inner().socket().as_inner()
739     }
740 }
741
742 #[stable(feature = "rust1", since = "1.0.0")]
743 impl AsRawFd for net::UdpSocket {
744     fn as_raw_fd(&self) -> RawFd {
745         *self.as_inner().socket().as_inner()
746     }
747 }
748
749 #[stable(feature = "from_raw_os", since = "1.1.0")]
750 impl FromRawFd for net::TcpStream {
751     unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
752         let socket = sys::net::Socket::from_inner(fd);
753         net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(socket))
754     }
755 }
756
757 #[stable(feature = "from_raw_os", since = "1.1.0")]
758 impl FromRawFd for net::TcpListener {
759     unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
760         let socket = sys::net::Socket::from_inner(fd);
761         net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(socket))
762     }
763 }
764
765 #[stable(feature = "from_raw_os", since = "1.1.0")]
766 impl FromRawFd for net::UdpSocket {
767     unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
768         let socket = sys::net::Socket::from_inner(fd);
769         net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(socket))
770     }
771 }
772
773 #[stable(feature = "into_raw_os", since = "1.4.0")]
774 impl IntoRawFd for net::TcpStream {
775     fn into_raw_fd(self) -> RawFd {
776         self.into_inner().into_socket().into_inner()
777     }
778 }
779 #[stable(feature = "into_raw_os", since = "1.4.0")]
780 impl IntoRawFd for net::TcpListener {
781     fn into_raw_fd(self) -> RawFd {
782         self.into_inner().into_socket().into_inner()
783     }
784 }
785 #[stable(feature = "into_raw_os", since = "1.4.0")]
786 impl IntoRawFd for net::UdpSocket {
787     fn into_raw_fd(self) -> RawFd {
788         self.into_inner().into_socket().into_inner()
789     }
790 }
791
792 /// A structure representing a Unix domain socket server.
793 ///
794 /// # Examples
795 ///
796 /// ```no_run
797 /// use std::thread;
798 /// use std::os::unix::net::{UnixStream, UnixListener};
799 ///
800 /// fn handle_client(stream: UnixStream) {
801 ///     // ...
802 /// }
803 ///
804 /// fn main() -> std::io::Result<()> {
805 ///     let listener = UnixListener::bind("/path/to/the/socket")?;
806 ///
807 ///     // accept connections and process them, spawning a new thread for each one
808 ///     for stream in listener.incoming() {
809 ///         match stream {
810 ///             Ok(stream) => {
811 ///                 /* connection succeeded */
812 ///                 thread::spawn(|| handle_client(stream));
813 ///             }
814 ///             Err(err) => {
815 ///                 /* connection failed */
816 ///                 break;
817 ///             }
818 ///         }
819 ///     }
820 ///     Ok(())
821 /// }
822 /// ```
823 #[stable(feature = "unix_socket", since = "1.10.0")]
824 pub struct UnixListener(Socket);
825
826 #[stable(feature = "unix_socket", since = "1.10.0")]
827 impl fmt::Debug for UnixListener {
828     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
829         let mut builder = fmt.debug_struct("UnixListener");
830         builder.field("fd", self.0.as_inner());
831         if let Ok(addr) = self.local_addr() {
832             builder.field("local", &addr);
833         }
834         builder.finish()
835     }
836 }
837
838 impl UnixListener {
839     /// Creates a new `UnixListener` bound to the specified socket.
840     ///
841     /// # Examples
842     ///
843     /// ```no_run
844     /// use std::os::unix::net::UnixListener;
845     ///
846     /// let listener = match UnixListener::bind("/path/to/the/socket") {
847     ///     Ok(sock) => sock,
848     ///     Err(e) => {
849     ///         println!("Couldn't connect: {:?}", e);
850     ///         return
851     ///     }
852     /// };
853     /// ```
854     #[stable(feature = "unix_socket", since = "1.10.0")]
855     pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixListener> {
856         fn inner(path: &Path) -> io::Result<UnixListener> {
857             unsafe {
858                 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
859                 let (addr, len) = sockaddr_un(path)?;
860
861                 cvt(libc::bind(*inner.as_inner(), &addr as *const _ as *const _, len as _))?;
862                 cvt(libc::listen(*inner.as_inner(), 128))?;
863
864                 Ok(UnixListener(inner))
865             }
866         }
867         inner(path.as_ref())
868     }
869
870     /// Accepts a new incoming connection to this listener.
871     ///
872     /// This function will block the calling thread until a new Unix connection
873     /// is established. When established, the corresponding [`UnixStream`] and
874     /// the remote peer's address will be returned.
875     ///
876     /// [`UnixStream`]: crate::os::unix::net::UnixStream
877     ///
878     /// # Examples
879     ///
880     /// ```no_run
881     /// use std::os::unix::net::UnixListener;
882     ///
883     /// fn main() -> std::io::Result<()> {
884     ///     let listener = UnixListener::bind("/path/to/the/socket")?;
885     ///
886     ///     match listener.accept() {
887     ///         Ok((socket, addr)) => println!("Got a client: {:?}", addr),
888     ///         Err(e) => println!("accept function failed: {:?}", e),
889     ///     }
890     ///     Ok(())
891     /// }
892     /// ```
893     #[stable(feature = "unix_socket", since = "1.10.0")]
894     pub fn accept(&self) -> io::Result<(UnixStream, SocketAddr)> {
895         let mut storage: libc::sockaddr_un = unsafe { mem::zeroed() };
896         let mut len = mem::size_of_val(&storage) as libc::socklen_t;
897         let sock = self.0.accept(&mut storage as *mut _ as *mut _, &mut len)?;
898         let addr = SocketAddr::from_parts(storage, len)?;
899         Ok((UnixStream(sock), addr))
900     }
901
902     /// Creates a new independently owned handle to the underlying socket.
903     ///
904     /// The returned `UnixListener` is a reference to the same socket that this
905     /// object references. Both handles can be used to accept incoming
906     /// connections and options set on one listener will affect the other.
907     ///
908     /// # Examples
909     ///
910     /// ```no_run
911     /// use std::os::unix::net::UnixListener;
912     ///
913     /// fn main() -> std::io::Result<()> {
914     ///     let listener = UnixListener::bind("/path/to/the/socket")?;
915     ///     let listener_copy = listener.try_clone().expect("try_clone failed");
916     ///     Ok(())
917     /// }
918     /// ```
919     #[stable(feature = "unix_socket", since = "1.10.0")]
920     pub fn try_clone(&self) -> io::Result<UnixListener> {
921         self.0.duplicate().map(UnixListener)
922     }
923
924     /// Returns the local socket address of this listener.
925     ///
926     /// # Examples
927     ///
928     /// ```no_run
929     /// use std::os::unix::net::UnixListener;
930     ///
931     /// fn main() -> std::io::Result<()> {
932     ///     let listener = UnixListener::bind("/path/to/the/socket")?;
933     ///     let addr = listener.local_addr().expect("Couldn't get local address");
934     ///     Ok(())
935     /// }
936     /// ```
937     #[stable(feature = "unix_socket", since = "1.10.0")]
938     pub fn local_addr(&self) -> io::Result<SocketAddr> {
939         SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
940     }
941
942     /// Moves the socket into or out of nonblocking mode.
943     ///
944     /// This will result in the `accept` operation becoming nonblocking,
945     /// i.e., immediately returning from their calls. If the IO operation is
946     /// successful, `Ok` is returned and no further action is required. If the
947     /// IO operation could not be completed and needs to be retried, an error
948     /// with kind [`io::ErrorKind::WouldBlock`] is returned.
949     ///
950     /// # Examples
951     ///
952     /// ```no_run
953     /// use std::os::unix::net::UnixListener;
954     ///
955     /// fn main() -> std::io::Result<()> {
956     ///     let listener = UnixListener::bind("/path/to/the/socket")?;
957     ///     listener.set_nonblocking(true).expect("Couldn't set non blocking");
958     ///     Ok(())
959     /// }
960     /// ```
961     #[stable(feature = "unix_socket", since = "1.10.0")]
962     pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
963         self.0.set_nonblocking(nonblocking)
964     }
965
966     /// Returns the value of the `SO_ERROR` option.
967     ///
968     /// # Examples
969     ///
970     /// ```no_run
971     /// use std::os::unix::net::UnixListener;
972     ///
973     /// fn main() -> std::io::Result<()> {
974     ///     let listener = UnixListener::bind("/tmp/sock")?;
975     ///
976     ///     if let Ok(Some(err)) = listener.take_error() {
977     ///         println!("Got error: {:?}", err);
978     ///     }
979     ///     Ok(())
980     /// }
981     /// ```
982     ///
983     /// # Platform specific
984     /// On Redox this always returns `None`.
985     #[stable(feature = "unix_socket", since = "1.10.0")]
986     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
987         self.0.take_error()
988     }
989
990     /// Returns an iterator over incoming connections.
991     ///
992     /// The iterator will never return [`None`] and will also not yield the
993     /// peer's [`SocketAddr`] structure.
994     ///
995     /// # Examples
996     ///
997     /// ```no_run
998     /// use std::thread;
999     /// use std::os::unix::net::{UnixStream, UnixListener};
1000     ///
1001     /// fn handle_client(stream: UnixStream) {
1002     ///     // ...
1003     /// }
1004     ///
1005     /// fn main() -> std::io::Result<()> {
1006     ///     let listener = UnixListener::bind("/path/to/the/socket")?;
1007     ///
1008     ///     for stream in listener.incoming() {
1009     ///         match stream {
1010     ///             Ok(stream) => {
1011     ///                 thread::spawn(|| handle_client(stream));
1012     ///             }
1013     ///             Err(err) => {
1014     ///                 break;
1015     ///             }
1016     ///         }
1017     ///     }
1018     ///     Ok(())
1019     /// }
1020     /// ```
1021     #[stable(feature = "unix_socket", since = "1.10.0")]
1022     pub fn incoming(&self) -> Incoming<'_> {
1023         Incoming { listener: self }
1024     }
1025 }
1026
1027 #[stable(feature = "unix_socket", since = "1.10.0")]
1028 impl AsRawFd for UnixListener {
1029     fn as_raw_fd(&self) -> RawFd {
1030         *self.0.as_inner()
1031     }
1032 }
1033
1034 #[stable(feature = "unix_socket", since = "1.10.0")]
1035 impl FromRawFd for UnixListener {
1036     unsafe fn from_raw_fd(fd: RawFd) -> UnixListener {
1037         UnixListener(Socket::from_inner(fd))
1038     }
1039 }
1040
1041 #[stable(feature = "unix_socket", since = "1.10.0")]
1042 impl IntoRawFd for UnixListener {
1043     fn into_raw_fd(self) -> RawFd {
1044         self.0.into_inner()
1045     }
1046 }
1047
1048 #[stable(feature = "unix_socket", since = "1.10.0")]
1049 impl<'a> IntoIterator for &'a UnixListener {
1050     type Item = io::Result<UnixStream>;
1051     type IntoIter = Incoming<'a>;
1052
1053     fn into_iter(self) -> Incoming<'a> {
1054         self.incoming()
1055     }
1056 }
1057
1058 /// An iterator over incoming connections to a [`UnixListener`].
1059 ///
1060 /// It will never return [`None`].
1061 ///
1062 /// # Examples
1063 ///
1064 /// ```no_run
1065 /// use std::thread;
1066 /// use std::os::unix::net::{UnixStream, UnixListener};
1067 ///
1068 /// fn handle_client(stream: UnixStream) {
1069 ///     // ...
1070 /// }
1071 ///
1072 /// fn main() -> std::io::Result<()> {
1073 ///     let listener = UnixListener::bind("/path/to/the/socket")?;
1074 ///
1075 ///     for stream in listener.incoming() {
1076 ///         match stream {
1077 ///             Ok(stream) => {
1078 ///                 thread::spawn(|| handle_client(stream));
1079 ///             }
1080 ///             Err(err) => {
1081 ///                 break;
1082 ///             }
1083 ///         }
1084 ///     }
1085 ///     Ok(())
1086 /// }
1087 /// ```
1088 #[derive(Debug)]
1089 #[stable(feature = "unix_socket", since = "1.10.0")]
1090 pub struct Incoming<'a> {
1091     listener: &'a UnixListener,
1092 }
1093
1094 #[stable(feature = "unix_socket", since = "1.10.0")]
1095 impl<'a> Iterator for Incoming<'a> {
1096     type Item = io::Result<UnixStream>;
1097
1098     fn next(&mut self) -> Option<io::Result<UnixStream>> {
1099         Some(self.listener.accept().map(|s| s.0))
1100     }
1101
1102     fn size_hint(&self) -> (usize, Option<usize>) {
1103         (usize::MAX, None)
1104     }
1105 }
1106
1107 /// A Unix datagram socket.
1108 ///
1109 /// # Examples
1110 ///
1111 /// ```no_run
1112 /// use std::os::unix::net::UnixDatagram;
1113 ///
1114 /// fn main() -> std::io::Result<()> {
1115 ///     let socket = UnixDatagram::bind("/path/to/my/socket")?;
1116 ///     socket.send_to(b"hello world", "/path/to/other/socket")?;
1117 ///     let mut buf = [0; 100];
1118 ///     let (count, address) = socket.recv_from(&mut buf)?;
1119 ///     println!("socket {:?} sent {:?}", address, &buf[..count]);
1120 ///     Ok(())
1121 /// }
1122 /// ```
1123 #[stable(feature = "unix_socket", since = "1.10.0")]
1124 pub struct UnixDatagram(Socket);
1125
1126 #[stable(feature = "unix_socket", since = "1.10.0")]
1127 impl fmt::Debug for UnixDatagram {
1128     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1129         let mut builder = fmt.debug_struct("UnixDatagram");
1130         builder.field("fd", self.0.as_inner());
1131         if let Ok(addr) = self.local_addr() {
1132             builder.field("local", &addr);
1133         }
1134         if let Ok(addr) = self.peer_addr() {
1135             builder.field("peer", &addr);
1136         }
1137         builder.finish()
1138     }
1139 }
1140
1141 impl UnixDatagram {
1142     /// Creates a Unix datagram socket bound to the given path.
1143     ///
1144     /// # Examples
1145     ///
1146     /// ```no_run
1147     /// use std::os::unix::net::UnixDatagram;
1148     ///
1149     /// let sock = match UnixDatagram::bind("/path/to/the/socket") {
1150     ///     Ok(sock) => sock,
1151     ///     Err(e) => {
1152     ///         println!("Couldn't bind: {:?}", e);
1153     ///         return
1154     ///     }
1155     /// };
1156     /// ```
1157     #[stable(feature = "unix_socket", since = "1.10.0")]
1158     pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixDatagram> {
1159         fn inner(path: &Path) -> io::Result<UnixDatagram> {
1160             unsafe {
1161                 let socket = UnixDatagram::unbound()?;
1162                 let (addr, len) = sockaddr_un(path)?;
1163
1164                 cvt(libc::bind(*socket.0.as_inner(), &addr as *const _ as *const _, len as _))?;
1165
1166                 Ok(socket)
1167             }
1168         }
1169         inner(path.as_ref())
1170     }
1171
1172     /// Creates a Unix Datagram socket which is not bound to any address.
1173     ///
1174     /// # Examples
1175     ///
1176     /// ```no_run
1177     /// use std::os::unix::net::UnixDatagram;
1178     ///
1179     /// let sock = match UnixDatagram::unbound() {
1180     ///     Ok(sock) => sock,
1181     ///     Err(e) => {
1182     ///         println!("Couldn't unbound: {:?}", e);
1183     ///         return
1184     ///     }
1185     /// };
1186     /// ```
1187     #[stable(feature = "unix_socket", since = "1.10.0")]
1188     pub fn unbound() -> io::Result<UnixDatagram> {
1189         let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_DGRAM)?;
1190         Ok(UnixDatagram(inner))
1191     }
1192
1193     /// Creates an unnamed pair of connected sockets.
1194     ///
1195     /// Returns two `UnixDatagrams`s which are connected to each other.
1196     ///
1197     /// # Examples
1198     ///
1199     /// ```no_run
1200     /// use std::os::unix::net::UnixDatagram;
1201     ///
1202     /// let (sock1, sock2) = match UnixDatagram::pair() {
1203     ///     Ok((sock1, sock2)) => (sock1, sock2),
1204     ///     Err(e) => {
1205     ///         println!("Couldn't unbound: {:?}", e);
1206     ///         return
1207     ///     }
1208     /// };
1209     /// ```
1210     #[stable(feature = "unix_socket", since = "1.10.0")]
1211     pub fn pair() -> io::Result<(UnixDatagram, UnixDatagram)> {
1212         let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_DGRAM)?;
1213         Ok((UnixDatagram(i1), UnixDatagram(i2)))
1214     }
1215
1216     /// Connects the socket to the specified address.
1217     ///
1218     /// The [`send`] method may be used to send data to the specified address.
1219     /// [`recv`] and [`recv_from`] will only receive data from that address.
1220     ///
1221     /// [`send`]: UnixDatagram::send
1222     /// [`recv`]: UnixDatagram::recv
1223     /// [`recv_from`]: UnixDatagram::recv_from
1224     ///
1225     /// # Examples
1226     ///
1227     /// ```no_run
1228     /// use std::os::unix::net::UnixDatagram;
1229     ///
1230     /// fn main() -> std::io::Result<()> {
1231     ///     let sock = UnixDatagram::unbound()?;
1232     ///     match sock.connect("/path/to/the/socket") {
1233     ///         Ok(sock) => sock,
1234     ///         Err(e) => {
1235     ///             println!("Couldn't connect: {:?}", e);
1236     ///             return Err(e)
1237     ///         }
1238     ///     };
1239     ///     Ok(())
1240     /// }
1241     /// ```
1242     #[stable(feature = "unix_socket", since = "1.10.0")]
1243     pub fn connect<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
1244         fn inner(d: &UnixDatagram, path: &Path) -> io::Result<()> {
1245             unsafe {
1246                 let (addr, len) = sockaddr_un(path)?;
1247
1248                 cvt(libc::connect(*d.0.as_inner(), &addr as *const _ as *const _, len))?;
1249
1250                 Ok(())
1251             }
1252         }
1253         inner(self, path.as_ref())
1254     }
1255
1256     /// Creates a new independently owned handle to the underlying socket.
1257     ///
1258     /// The returned `UnixDatagram` is a reference to the same socket that this
1259     /// object references. Both handles can be used to accept incoming
1260     /// connections and options set on one side will affect the other.
1261     ///
1262     /// # Examples
1263     ///
1264     /// ```no_run
1265     /// use std::os::unix::net::UnixDatagram;
1266     ///
1267     /// fn main() -> std::io::Result<()> {
1268     ///     let sock = UnixDatagram::bind("/path/to/the/socket")?;
1269     ///     let sock_copy = sock.try_clone().expect("try_clone failed");
1270     ///     Ok(())
1271     /// }
1272     /// ```
1273     #[stable(feature = "unix_socket", since = "1.10.0")]
1274     pub fn try_clone(&self) -> io::Result<UnixDatagram> {
1275         self.0.duplicate().map(UnixDatagram)
1276     }
1277
1278     /// Returns the address of this socket.
1279     ///
1280     /// # Examples
1281     ///
1282     /// ```no_run
1283     /// use std::os::unix::net::UnixDatagram;
1284     ///
1285     /// fn main() -> std::io::Result<()> {
1286     ///     let sock = UnixDatagram::bind("/path/to/the/socket")?;
1287     ///     let addr = sock.local_addr().expect("Couldn't get local address");
1288     ///     Ok(())
1289     /// }
1290     /// ```
1291     #[stable(feature = "unix_socket", since = "1.10.0")]
1292     pub fn local_addr(&self) -> io::Result<SocketAddr> {
1293         SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
1294     }
1295
1296     /// Returns the address of this socket's peer.
1297     ///
1298     /// The [`connect`] method will connect the socket to a peer.
1299     ///
1300     /// [`connect`]: UnixDatagram::connect
1301     ///
1302     /// # Examples
1303     ///
1304     /// ```no_run
1305     /// use std::os::unix::net::UnixDatagram;
1306     ///
1307     /// fn main() -> std::io::Result<()> {
1308     ///     let sock = UnixDatagram::unbound()?;
1309     ///     sock.connect("/path/to/the/socket")?;
1310     ///
1311     ///     let addr = sock.peer_addr().expect("Couldn't get peer address");
1312     ///     Ok(())
1313     /// }
1314     /// ```
1315     #[stable(feature = "unix_socket", since = "1.10.0")]
1316     pub fn peer_addr(&self) -> io::Result<SocketAddr> {
1317         SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
1318     }
1319
1320     fn recv_from_flags(
1321         &self,
1322         buf: &mut [u8],
1323         flags: libc::c_int,
1324     ) -> io::Result<(usize, SocketAddr)> {
1325         let mut count = 0;
1326         let addr = SocketAddr::new(|addr, len| unsafe {
1327             count = libc::recvfrom(
1328                 *self.0.as_inner(),
1329                 buf.as_mut_ptr() as *mut _,
1330                 buf.len(),
1331                 flags,
1332                 addr,
1333                 len,
1334             );
1335             if count > 0 {
1336                 1
1337             } else if count == 0 {
1338                 0
1339             } else {
1340                 -1
1341             }
1342         })?;
1343
1344         Ok((count as usize, addr))
1345     }
1346
1347     /// Receives data from the socket.
1348     ///
1349     /// On success, returns the number of bytes read and the address from
1350     /// whence the data came.
1351     ///
1352     /// # Examples
1353     ///
1354     /// ```no_run
1355     /// use std::os::unix::net::UnixDatagram;
1356     ///
1357     /// fn main() -> std::io::Result<()> {
1358     ///     let sock = UnixDatagram::unbound()?;
1359     ///     let mut buf = vec![0; 10];
1360     ///     let (size, sender) = sock.recv_from(buf.as_mut_slice())?;
1361     ///     println!("received {} bytes from {:?}", size, sender);
1362     ///     Ok(())
1363     /// }
1364     /// ```
1365     #[stable(feature = "unix_socket", since = "1.10.0")]
1366     pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
1367         self.recv_from_flags(buf, 0)
1368     }
1369
1370     /// Receives data from the socket.
1371     ///
1372     /// On success, returns the number of bytes read.
1373     ///
1374     /// # Examples
1375     ///
1376     /// ```no_run
1377     /// use std::os::unix::net::UnixDatagram;
1378     ///
1379     /// fn main() -> std::io::Result<()> {
1380     ///     let sock = UnixDatagram::bind("/path/to/the/socket")?;
1381     ///     let mut buf = vec![0; 10];
1382     ///     sock.recv(buf.as_mut_slice()).expect("recv function failed");
1383     ///     Ok(())
1384     /// }
1385     /// ```
1386     #[stable(feature = "unix_socket", since = "1.10.0")]
1387     pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
1388         self.0.read(buf)
1389     }
1390
1391     /// Sends data on the socket to the specified address.
1392     ///
1393     /// On success, returns the number of bytes written.
1394     ///
1395     /// # Examples
1396     ///
1397     /// ```no_run
1398     /// use std::os::unix::net::UnixDatagram;
1399     ///
1400     /// fn main() -> std::io::Result<()> {
1401     ///     let sock = UnixDatagram::unbound()?;
1402     ///     sock.send_to(b"omelette au fromage", "/some/sock").expect("send_to function failed");
1403     ///     Ok(())
1404     /// }
1405     /// ```
1406     #[stable(feature = "unix_socket", since = "1.10.0")]
1407     pub fn send_to<P: AsRef<Path>>(&self, buf: &[u8], path: P) -> io::Result<usize> {
1408         fn inner(d: &UnixDatagram, buf: &[u8], path: &Path) -> io::Result<usize> {
1409             unsafe {
1410                 let (addr, len) = sockaddr_un(path)?;
1411
1412                 let count = cvt(libc::sendto(
1413                     *d.0.as_inner(),
1414                     buf.as_ptr() as *const _,
1415                     buf.len(),
1416                     MSG_NOSIGNAL,
1417                     &addr as *const _ as *const _,
1418                     len,
1419                 ))?;
1420                 Ok(count as usize)
1421             }
1422         }
1423         inner(self, buf, path.as_ref())
1424     }
1425
1426     /// Sends data on the socket to the socket's peer.
1427     ///
1428     /// The peer address may be set by the `connect` method, and this method
1429     /// will return an error if the socket has not already been connected.
1430     ///
1431     /// On success, returns the number of bytes written.
1432     ///
1433     /// # Examples
1434     ///
1435     /// ```no_run
1436     /// use std::os::unix::net::UnixDatagram;
1437     ///
1438     /// fn main() -> std::io::Result<()> {
1439     ///     let sock = UnixDatagram::unbound()?;
1440     ///     sock.connect("/some/sock").expect("Couldn't connect");
1441     ///     sock.send(b"omelette au fromage").expect("send_to function failed");
1442     ///     Ok(())
1443     /// }
1444     /// ```
1445     #[stable(feature = "unix_socket", since = "1.10.0")]
1446     pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
1447         self.0.write(buf)
1448     }
1449
1450     /// Sets the read timeout for the socket.
1451     ///
1452     /// If the provided value is [`None`], then [`recv`] and [`recv_from`] calls will
1453     /// block indefinitely. An [`Err`] is returned if the zero [`Duration`]
1454     /// is passed to this method.
1455     ///
1456     /// [`recv`]: UnixDatagram::recv
1457     /// [`recv_from`]: UnixDatagram::recv_from
1458     ///
1459     /// # Examples
1460     ///
1461     /// ```
1462     /// use std::os::unix::net::UnixDatagram;
1463     /// use std::time::Duration;
1464     ///
1465     /// fn main() -> std::io::Result<()> {
1466     ///     let sock = UnixDatagram::unbound()?;
1467     ///     sock.set_read_timeout(Some(Duration::new(1, 0)))
1468     ///         .expect("set_read_timeout function failed");
1469     ///     Ok(())
1470     /// }
1471     /// ```
1472     ///
1473     /// An [`Err`] is returned if the zero [`Duration`] is passed to this
1474     /// method:
1475     ///
1476     /// ```no_run
1477     /// use std::io;
1478     /// use std::os::unix::net::UnixDatagram;
1479     /// use std::time::Duration;
1480     ///
1481     /// fn main() -> std::io::Result<()> {
1482     ///     let socket = UnixDatagram::unbound()?;
1483     ///     let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
1484     ///     let err = result.unwrap_err();
1485     ///     assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
1486     ///     Ok(())
1487     /// }
1488     /// ```
1489     #[stable(feature = "unix_socket", since = "1.10.0")]
1490     pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
1491         self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
1492     }
1493
1494     /// Sets the write timeout for the socket.
1495     ///
1496     /// If the provided value is [`None`], then [`send`] and [`send_to`] calls will
1497     /// block indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
1498     /// method.
1499     ///
1500     /// [`send`]: UnixDatagram::send
1501     /// [`send_to`]: UnixDatagram::send_to
1502     ///
1503     /// # Examples
1504     ///
1505     /// ```
1506     /// use std::os::unix::net::UnixDatagram;
1507     /// use std::time::Duration;
1508     ///
1509     /// fn main() -> std::io::Result<()> {
1510     ///     let sock = UnixDatagram::unbound()?;
1511     ///     sock.set_write_timeout(Some(Duration::new(1, 0)))
1512     ///         .expect("set_write_timeout function failed");
1513     ///     Ok(())
1514     /// }
1515     /// ```
1516     ///
1517     /// An [`Err`] is returned if the zero [`Duration`] is passed to this
1518     /// method:
1519     ///
1520     /// ```no_run
1521     /// use std::io;
1522     /// use std::os::unix::net::UnixDatagram;
1523     /// use std::time::Duration;
1524     ///
1525     /// fn main() -> std::io::Result<()> {
1526     ///     let socket = UnixDatagram::unbound()?;
1527     ///     let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
1528     ///     let err = result.unwrap_err();
1529     ///     assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
1530     ///     Ok(())
1531     /// }
1532     /// ```
1533     #[stable(feature = "unix_socket", since = "1.10.0")]
1534     pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
1535         self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
1536     }
1537
1538     /// Returns the read timeout of this socket.
1539     ///
1540     /// # Examples
1541     ///
1542     /// ```
1543     /// use std::os::unix::net::UnixDatagram;
1544     /// use std::time::Duration;
1545     ///
1546     /// fn main() -> std::io::Result<()> {
1547     ///     let sock = UnixDatagram::unbound()?;
1548     ///     sock.set_read_timeout(Some(Duration::new(1, 0)))
1549     ///         .expect("set_read_timeout function failed");
1550     ///     assert_eq!(sock.read_timeout()?, Some(Duration::new(1, 0)));
1551     ///     Ok(())
1552     /// }
1553     /// ```
1554     #[stable(feature = "unix_socket", since = "1.10.0")]
1555     pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
1556         self.0.timeout(libc::SO_RCVTIMEO)
1557     }
1558
1559     /// Returns the write timeout of this socket.
1560     ///
1561     /// # Examples
1562     ///
1563     /// ```
1564     /// use std::os::unix::net::UnixDatagram;
1565     /// use std::time::Duration;
1566     ///
1567     /// fn main() -> std::io::Result<()> {
1568     ///     let sock = UnixDatagram::unbound()?;
1569     ///     sock.set_write_timeout(Some(Duration::new(1, 0)))
1570     ///         .expect("set_write_timeout function failed");
1571     ///     assert_eq!(sock.write_timeout()?, Some(Duration::new(1, 0)));
1572     ///     Ok(())
1573     /// }
1574     /// ```
1575     #[stable(feature = "unix_socket", since = "1.10.0")]
1576     pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
1577         self.0.timeout(libc::SO_SNDTIMEO)
1578     }
1579
1580     /// Moves the socket into or out of nonblocking mode.
1581     ///
1582     /// # Examples
1583     ///
1584     /// ```
1585     /// use std::os::unix::net::UnixDatagram;
1586     ///
1587     /// fn main() -> std::io::Result<()> {
1588     ///     let sock = UnixDatagram::unbound()?;
1589     ///     sock.set_nonblocking(true).expect("set_nonblocking function failed");
1590     ///     Ok(())
1591     /// }
1592     /// ```
1593     #[stable(feature = "unix_socket", since = "1.10.0")]
1594     pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
1595         self.0.set_nonblocking(nonblocking)
1596     }
1597
1598     /// Returns the value of the `SO_ERROR` option.
1599     ///
1600     /// # Examples
1601     ///
1602     /// ```no_run
1603     /// use std::os::unix::net::UnixDatagram;
1604     ///
1605     /// fn main() -> std::io::Result<()> {
1606     ///     let sock = UnixDatagram::unbound()?;
1607     ///     if let Ok(Some(err)) = sock.take_error() {
1608     ///         println!("Got error: {:?}", err);
1609     ///     }
1610     ///     Ok(())
1611     /// }
1612     /// ```
1613     #[stable(feature = "unix_socket", since = "1.10.0")]
1614     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
1615         self.0.take_error()
1616     }
1617
1618     /// Shut down the read, write, or both halves of this connection.
1619     ///
1620     /// This function will cause all pending and future I/O calls on the
1621     /// specified portions to immediately return with an appropriate value
1622     /// (see the documentation of [`Shutdown`]).
1623     ///
1624     /// ```no_run
1625     /// use std::os::unix::net::UnixDatagram;
1626     /// use std::net::Shutdown;
1627     ///
1628     /// fn main() -> std::io::Result<()> {
1629     ///     let sock = UnixDatagram::unbound()?;
1630     ///     sock.shutdown(Shutdown::Both).expect("shutdown function failed");
1631     ///     Ok(())
1632     /// }
1633     /// ```
1634     #[stable(feature = "unix_socket", since = "1.10.0")]
1635     pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
1636         self.0.shutdown(how)
1637     }
1638
1639     /// Receives data on the socket from the remote address to which it is
1640     /// connected, without removing that data from the queue. On success,
1641     /// returns the number of bytes peeked.
1642     ///
1643     /// Successive calls return the same data. This is accomplished by passing
1644     /// `MSG_PEEK` as a flag to the underlying `recv` system call.
1645     ///
1646     /// # Examples
1647     ///
1648     /// ```no_run
1649     /// #![feature(unix_socket_peek)]
1650     ///
1651     /// use std::os::unix::net::UnixDatagram;
1652     ///
1653     /// fn main() -> std::io::Result<()> {
1654     ///     let socket = UnixDatagram::bind("/tmp/sock")?;
1655     ///     let mut buf = [0; 10];
1656     ///     let len = socket.peek(&mut buf).expect("peek failed");
1657     ///     Ok(())
1658     /// }
1659     /// ```
1660     #[unstable(feature = "unix_socket_peek", issue = "none")]
1661     pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
1662         self.0.peek(buf)
1663     }
1664
1665     /// Receives a single datagram message on the socket, without removing it from the
1666     /// queue. On success, returns the number of bytes read and the origin.
1667     ///
1668     /// The function must be called with valid byte array `buf` of sufficient size to
1669     /// hold the message bytes. If a message is too long to fit in the supplied buffer,
1670     /// excess bytes may be discarded.
1671     ///
1672     /// Successive calls return the same data. This is accomplished by passing
1673     /// `MSG_PEEK` as a flag to the underlying `recvfrom` system call.
1674     ///
1675     /// Do not use this function to implement busy waiting, instead use `libc::poll` to
1676     /// synchronize IO events on one or more sockets.
1677     ///
1678     /// # Examples
1679     ///
1680     /// ```no_run
1681     /// #![feature(unix_socket_peek)]
1682     ///
1683     /// use std::os::unix::net::UnixDatagram;
1684     ///
1685     /// fn main() -> std::io::Result<()> {
1686     ///     let socket = UnixDatagram::bind("/tmp/sock")?;
1687     ///     let mut buf = [0; 10];
1688     ///     let (len, addr) = socket.peek_from(&mut buf).expect("peek failed");
1689     ///     Ok(())
1690     /// }
1691     /// ```
1692     #[unstable(feature = "unix_socket_peek", issue = "none")]
1693     pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
1694         self.recv_from_flags(buf, libc::MSG_PEEK)
1695     }
1696 }
1697
1698 #[stable(feature = "unix_socket", since = "1.10.0")]
1699 impl AsRawFd for UnixDatagram {
1700     fn as_raw_fd(&self) -> RawFd {
1701         *self.0.as_inner()
1702     }
1703 }
1704
1705 #[stable(feature = "unix_socket", since = "1.10.0")]
1706 impl FromRawFd for UnixDatagram {
1707     unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram {
1708         UnixDatagram(Socket::from_inner(fd))
1709     }
1710 }
1711
1712 #[stable(feature = "unix_socket", since = "1.10.0")]
1713 impl IntoRawFd for UnixDatagram {
1714     fn into_raw_fd(self) -> RawFd {
1715         self.0.into_inner()
1716     }
1717 }