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