]> git.proxmox.com Git - rustc.git/blobdiff - src/libstd/sys/unix/net.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / libstd / sys / unix / net.rs
index 75750b5c4e588abd857e1f99bbe4f14d0623c910..946b2b9d8decfa1ccc04c57738b7c5756242112e 100644 (file)
@@ -1,13 +1,13 @@
+use crate::cmp;
 use crate::ffi::CStr;
 use crate::io::{self, IoSlice, IoSliceMut};
 use crate::mem;
-use crate::net::{SocketAddr, Shutdown};
+use crate::net::{Shutdown, SocketAddr};
 use crate::str;
 use crate::sys::fd::FileDesc;
-use crate::sys_common::{AsInner, FromInner, IntoInner};
 use crate::sys_common::net::{getsockopt, setsockopt, sockaddr_to_addr};
+use crate::sys_common::{AsInner, FromInner, IntoInner};
 use crate::time::{Duration, Instant};
-use crate::cmp;
 
 use libc::{c_int, c_void, size_t, sockaddr, socklen_t, EAI_SYSTEM, MSG_PEEK};
 
@@ -42,23 +42,23 @@ pub fn init() {}
 
 pub fn cvt_gai(err: c_int) -> io::Result<()> {
     if err == 0 {
-        return Ok(())
+        return Ok(());
     }
 
     // We may need to trigger a glibc workaround. See on_resolver_failure() for details.
     on_resolver_failure();
 
     if err == EAI_SYSTEM {
-        return Err(io::Error::last_os_error())
+        return Err(io::Error::last_os_error());
     }
 
     let detail = unsafe {
-        str::from_utf8(CStr::from_ptr(libc::gai_strerror(err)).to_bytes()).unwrap()
-            .to_owned()
+        str::from_utf8(CStr::from_ptr(libc::gai_strerror(err)).to_bytes()).unwrap().to_owned()
     };
-    Err(io::Error::new(io::ErrorKind::Other,
-                       &format!("failed to lookup address information: {}",
-                                detail)[..]))
+    Err(io::Error::new(
+        io::ErrorKind::Other,
+        &format!("failed to lookup address information: {}", detail)[..],
+    ))
 }
 
 impl Socket {
@@ -106,7 +106,7 @@ impl Socket {
                     Ok(_) => {
                         return Ok((Socket(FileDesc::new(fds[0])), Socket(FileDesc::new(fds[1]))));
                     }
-                    Err(ref e) if e.raw_os_error() == Some(libc::EINVAL) => {},
+                    Err(ref e) if e.raw_os_error() == Some(libc::EINVAL) => {}
                     Err(e) => return Err(e),
                 }
             }
@@ -135,15 +135,13 @@ impl Socket {
             Err(e) => return Err(e),
         }
 
-        let mut pollfd = libc::pollfd {
-            fd: self.0.raw(),
-            events: libc::POLLOUT,
-            revents: 0,
-        };
+        let mut pollfd = libc::pollfd { fd: self.0.raw(), events: libc::POLLOUT, revents: 0 };
 
         if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
-            return Err(io::Error::new(io::ErrorKind::InvalidInput,
-                                      "cannot set a 0 duration timeout"));
+            return Err(io::Error::new(
+                io::ErrorKind::InvalidInput,
+                "cannot set a 0 duration timeout",
+            ));
         }
 
         let start = Instant::now();
@@ -155,7 +153,8 @@ impl Socket {
             }
 
             let timeout = timeout - elapsed;
-            let mut timeout = timeout.as_secs()
+            let mut timeout = timeout
+                .as_secs()
                 .saturating_mul(1_000)
                 .saturating_add(timeout.subsec_nanos() as u64 / 1_000_000);
             if timeout == 0 {
@@ -176,10 +175,9 @@ impl Socket {
                     // linux returns POLLOUT|POLLERR|POLLHUP for refused connections (!), so look
                     // for POLLHUP rather than read readiness
                     if pollfd.revents & libc::POLLHUP != 0 {
-                        let e = self.take_error()?
-                            .unwrap_or_else(|| {
-                                io::Error::new(io::ErrorKind::Other, "no error set after POLLHUP")
-                            });
+                        let e = self.take_error()?.unwrap_or_else(|| {
+                            io::Error::new(io::ErrorKind::Other, "no error set after POLLHUP")
+                        });
                         return Err(e);
                     }
 
@@ -189,8 +187,7 @@ impl Socket {
         }
     }
 
-    pub fn accept(&self, storage: *mut sockaddr, len: *mut socklen_t)
-                  -> io::Result<Socket> {
+    pub fn accept(&self, storage: *mut sockaddr, len: *mut socklen_t) -> io::Result<Socket> {
         // Unfortunately the only known way right now to accept a socket and
         // atomically set the CLOEXEC flag is to use the `accept4` syscall on
         // Linux. This was added in 2.6.28, however, and because we support
@@ -204,9 +201,7 @@ impl Socket {
                     flags: c_int
                 ) -> c_int
             }
-            let res = cvt_r(|| unsafe {
-                accept4(self.0.raw(), storage, len, SOCK_CLOEXEC)
-            });
+            let res = cvt_r(|| unsafe { accept4(self.0.raw(), storage, len, SOCK_CLOEXEC) });
             match res {
                 Ok(fd) => return Ok(Socket(FileDesc::new(fd))),
                 Err(ref e) if e.raw_os_error() == Some(libc::ENOSYS) => {}
@@ -214,9 +209,7 @@ impl Socket {
             }
         }
 
-        let fd = cvt_r(|| unsafe {
-            libc::accept(self.0.raw(), storage, len)
-        })?;
+        let fd = cvt_r(|| unsafe { libc::accept(self.0.raw(), storage, len) })?;
         let fd = FileDesc::new(fd);
         fd.set_cloexec()?;
         Ok(Socket(fd))
@@ -228,10 +221,7 @@ impl Socket {
 
     fn recv_with_flags(&self, buf: &mut [u8], flags: c_int) -> io::Result<usize> {
         let ret = cvt(unsafe {
-            libc::recv(self.0.raw(),
-                       buf.as_mut_ptr() as *mut c_void,
-                       buf.len(),
-                       flags)
+            libc::recv(self.0.raw(), buf.as_mut_ptr() as *mut c_void, buf.len(), flags)
         })?;
         Ok(ret as usize)
     }
@@ -248,18 +238,23 @@ impl Socket {
         self.0.read_vectored(bufs)
     }
 
-    fn recv_from_with_flags(&self, buf: &mut [u8], flags: c_int)
-                            -> io::Result<(usize, SocketAddr)> {
+    fn recv_from_with_flags(
+        &self,
+        buf: &mut [u8],
+        flags: c_int,
+    ) -> io::Result<(usize, SocketAddr)> {
         let mut storage: libc::sockaddr_storage = unsafe { mem::zeroed() };
         let mut addrlen = mem::size_of_val(&storage) as libc::socklen_t;
 
         let n = cvt(unsafe {
-            libc::recvfrom(self.0.raw(),
-                        buf.as_mut_ptr() as *mut c_void,
-                        buf.len(),
-                        flags,
-                        &mut storage as *mut _ as *mut _,
-                        &mut addrlen)
+            libc::recvfrom(
+                self.0.raw(),
+                buf.as_mut_ptr() as *mut c_void,
+                buf.len(),
+                flags,
+                &mut storage as *mut _ as *mut _,
+                &mut addrlen,
+            )
         })?;
         Ok((n as usize, sockaddr_to_addr(&storage, addrlen as usize)?))
     }
@@ -284,8 +279,10 @@ impl Socket {
         let timeout = match dur {
             Some(dur) => {
                 if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
-                    return Err(io::Error::new(io::ErrorKind::InvalidInput,
-                                              "cannot set a 0 duration timeout"));
+                    return Err(io::Error::new(
+                        io::ErrorKind::InvalidInput,
+                        "cannot set a 0 duration timeout",
+                    ));
                 }
 
                 let secs = if dur.as_secs() > libc::time_t::max_value() as u64 {
@@ -302,12 +299,7 @@ impl Socket {
                 }
                 timeout
             }
-            None => {
-                libc::timeval {
-                    tv_sec: 0,
-                    tv_usec: 0,
-                }
-            }
+            None => libc::timeval { tv_sec: 0, tv_usec: 0 },
         };
         setsockopt(self, libc::SOL_SOCKET, kind, timeout)
     }
@@ -349,24 +341,26 @@ impl Socket {
 
     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
         let raw: c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)?;
-        if raw == 0 {
-            Ok(None)
-        } else {
-            Ok(Some(io::Error::from_raw_os_error(raw as i32)))
-        }
+        if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
     }
 }
 
 impl AsInner<c_int> for Socket {
-    fn as_inner(&self) -> &c_int { self.0.as_inner() }
+    fn as_inner(&self) -> &c_int {
+        self.0.as_inner()
+    }
 }
 
 impl FromInner<c_int> for Socket {
-    fn from_inner(fd: c_int) -> Socket { Socket(FileDesc::new(fd)) }
+    fn from_inner(fd: c_int) -> Socket {
+        Socket(FileDesc::new(fd))
+    }
 }
 
 impl IntoInner<c_int> for Socket {
-    fn into_inner(self) -> c_int { self.0.into_raw() }
+    fn into_inner(self) -> c_int {
+        self.0.into_raw()
+    }
 }
 
 // In versions of glibc prior to 2.26, there's a bug where the DNS resolver