]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/windows/net.rs
Merge commit '54a20a02ecd0e1352a871aa0990bcc8b8b03173e' into clippyup
[rust.git] / library / std / src / sys / windows / net.rs
1 #![unstable(issue = "none", feature = "windows_net")]
2
3 use crate::cmp;
4 use crate::io::{self, IoSlice, IoSliceMut, Read};
5 use crate::mem;
6 use crate::net::{Shutdown, SocketAddr};
7 use crate::ptr;
8 use crate::sync::Once;
9 use crate::sys;
10 use crate::sys::c;
11 use crate::sys_common::net;
12 use crate::sys_common::{AsInner, FromInner, IntoInner};
13 use crate::time::Duration;
14
15 use libc::{c_int, c_long, c_ulong};
16
17 pub type wrlen_t = i32;
18
19 pub mod netc {
20     pub use crate::sys::c::ADDRESS_FAMILY as sa_family_t;
21     pub use crate::sys::c::ADDRINFOA as addrinfo;
22     pub use crate::sys::c::SOCKADDR as sockaddr;
23     pub use crate::sys::c::SOCKADDR_STORAGE_LH as sockaddr_storage;
24     pub use crate::sys::c::*;
25 }
26
27 pub struct Socket(c::SOCKET);
28
29 static INIT: Once = Once::new();
30
31 /// Checks whether the Windows socket interface has been started already, and
32 /// if not, starts it.
33 pub fn init() {
34     INIT.call_once(|| unsafe {
35         let mut data: c::WSADATA = mem::zeroed();
36         let ret = c::WSAStartup(
37             0x202, // version 2.2
38             &mut data,
39         );
40         assert_eq!(ret, 0);
41     });
42 }
43
44 pub fn cleanup() {
45     if INIT.is_completed() {
46         // only close the socket interface if it has actually been started
47         unsafe {
48             c::WSACleanup();
49         }
50     }
51 }
52
53 /// Returns the last error from the Windows socket interface.
54 fn last_error() -> io::Error {
55     io::Error::from_raw_os_error(unsafe { c::WSAGetLastError() })
56 }
57
58 #[doc(hidden)]
59 pub trait IsMinusOne {
60     fn is_minus_one(&self) -> bool;
61 }
62
63 macro_rules! impl_is_minus_one {
64     ($($t:ident)*) => ($(impl IsMinusOne for $t {
65         fn is_minus_one(&self) -> bool {
66             *self == -1
67         }
68     })*)
69 }
70
71 impl_is_minus_one! { i8 i16 i32 i64 isize }
72
73 /// Checks if the signed integer is the Windows constant `SOCKET_ERROR` (-1)
74 /// and if so, returns the last error from the Windows socket interface. This
75 /// function must be called before another call to the socket API is made.
76 pub fn cvt<T: IsMinusOne>(t: T) -> io::Result<T> {
77     if t.is_minus_one() { Err(last_error()) } else { Ok(t) }
78 }
79
80 /// A variant of `cvt` for `getaddrinfo` which return 0 for a success.
81 pub fn cvt_gai(err: c_int) -> io::Result<()> {
82     if err == 0 { Ok(()) } else { Err(last_error()) }
83 }
84
85 /// Just to provide the same interface as sys/unix/net.rs
86 pub fn cvt_r<T, F>(mut f: F) -> io::Result<T>
87 where
88     T: IsMinusOne,
89     F: FnMut() -> T,
90 {
91     cvt(f())
92 }
93
94 impl Socket {
95     pub fn new(addr: &SocketAddr, ty: c_int) -> io::Result<Socket> {
96         let family = match *addr {
97             SocketAddr::V4(..) => c::AF_INET,
98             SocketAddr::V6(..) => c::AF_INET6,
99         };
100         let socket = unsafe {
101             c::WSASocketW(
102                 family,
103                 ty,
104                 0,
105                 ptr::null_mut(),
106                 0,
107                 c::WSA_FLAG_OVERLAPPED | c::WSA_FLAG_NO_HANDLE_INHERIT,
108             )
109         };
110
111         if socket != c::INVALID_SOCKET {
112             Ok(Self(socket))
113         } else {
114             let error = unsafe { c::WSAGetLastError() };
115
116             if error != c::WSAEPROTOTYPE && error != c::WSAEINVAL {
117                 return Err(io::Error::from_raw_os_error(error));
118             }
119
120             let socket =
121                 unsafe { c::WSASocketW(family, ty, 0, ptr::null_mut(), 0, c::WSA_FLAG_OVERLAPPED) };
122
123             if socket == c::INVALID_SOCKET {
124                 return Err(last_error());
125             }
126
127             let socket = Self(socket);
128             socket.set_no_inherit()?;
129             Ok(socket)
130         }
131     }
132
133     pub fn connect_timeout(&self, addr: &SocketAddr, timeout: Duration) -> io::Result<()> {
134         self.set_nonblocking(true)?;
135         let result = {
136             let (addrp, len) = addr.into_inner();
137             let result = unsafe { c::connect(self.0, addrp, len) };
138             cvt(result).map(drop)
139         };
140         self.set_nonblocking(false)?;
141
142         match result {
143             Err(ref error) if error.kind() == io::ErrorKind::WouldBlock => {
144                 if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
145                     return Err(io::Error::new_const(
146                         io::ErrorKind::InvalidInput,
147                         &"cannot set a 0 duration timeout",
148                     ));
149                 }
150
151                 let mut timeout = c::timeval {
152                     tv_sec: timeout.as_secs() as c_long,
153                     tv_usec: (timeout.subsec_nanos() / 1000) as c_long,
154                 };
155
156                 if timeout.tv_sec == 0 && timeout.tv_usec == 0 {
157                     timeout.tv_usec = 1;
158                 }
159
160                 let fds = {
161                     let mut fds = unsafe { mem::zeroed::<c::fd_set>() };
162                     fds.fd_count = 1;
163                     fds.fd_array[0] = self.0;
164                     fds
165                 };
166
167                 let mut writefds = fds;
168                 let mut errorfds = fds;
169
170                 let count = {
171                     let result = unsafe {
172                         c::select(1, ptr::null_mut(), &mut writefds, &mut errorfds, &timeout)
173                     };
174                     cvt(result)?
175                 };
176
177                 match count {
178                     0 => {
179                         Err(io::Error::new_const(io::ErrorKind::TimedOut, &"connection timed out"))
180                     }
181                     _ => {
182                         if writefds.fd_count != 1 {
183                             if let Some(e) = self.take_error()? {
184                                 return Err(e);
185                             }
186                         }
187
188                         Ok(())
189                     }
190                 }
191             }
192             _ => result,
193         }
194     }
195
196     pub fn accept(&self, storage: *mut c::SOCKADDR, len: *mut c_int) -> io::Result<Socket> {
197         let socket = unsafe { c::accept(self.0, storage, len) };
198
199         match socket {
200             c::INVALID_SOCKET => Err(last_error()),
201             _ => Ok(Self(socket)),
202         }
203     }
204
205     pub fn duplicate(&self) -> io::Result<Socket> {
206         let mut info = unsafe { mem::zeroed::<c::WSAPROTOCOL_INFO>() };
207         let result = unsafe { c::WSADuplicateSocketW(self.0, c::GetCurrentProcessId(), &mut info) };
208         cvt(result)?;
209         let socket = unsafe {
210             c::WSASocketW(
211                 info.iAddressFamily,
212                 info.iSocketType,
213                 info.iProtocol,
214                 &mut info,
215                 0,
216                 c::WSA_FLAG_OVERLAPPED | c::WSA_FLAG_NO_HANDLE_INHERIT,
217             )
218         };
219
220         if socket != c::INVALID_SOCKET {
221             Ok(Self(socket))
222         } else {
223             let error = unsafe { c::WSAGetLastError() };
224
225             if error != c::WSAEPROTOTYPE && error != c::WSAEINVAL {
226                 return Err(io::Error::from_raw_os_error(error));
227             }
228
229             let socket = unsafe {
230                 c::WSASocketW(
231                     info.iAddressFamily,
232                     info.iSocketType,
233                     info.iProtocol,
234                     &mut info,
235                     0,
236                     c::WSA_FLAG_OVERLAPPED,
237                 )
238             };
239
240             if socket == c::INVALID_SOCKET {
241                 return Err(last_error());
242             }
243
244             let socket = Self(socket);
245             socket.set_no_inherit()?;
246             Ok(socket)
247         }
248     }
249
250     fn recv_with_flags(&self, buf: &mut [u8], flags: c_int) -> io::Result<usize> {
251         // On unix when a socket is shut down all further reads return 0, so we
252         // do the same on windows to map a shut down socket to returning EOF.
253         let length = cmp::min(buf.len(), i32::MAX as usize) as i32;
254         let result = unsafe { c::recv(self.0, buf.as_mut_ptr() as *mut _, length, flags) };
255
256         match result {
257             c::SOCKET_ERROR => {
258                 let error = unsafe { c::WSAGetLastError() };
259
260                 if error == c::WSAESHUTDOWN {
261                     Ok(0)
262                 } else {
263                     Err(io::Error::from_raw_os_error(error))
264                 }
265             }
266             _ => Ok(result as usize),
267         }
268     }
269
270     pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
271         self.recv_with_flags(buf, 0)
272     }
273
274     pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
275         // On unix when a socket is shut down all further reads return 0, so we
276         // do the same on windows to map a shut down socket to returning EOF.
277         let length = cmp::min(bufs.len(), c::DWORD::MAX as usize) as c::DWORD;
278         let mut nread = 0;
279         let mut flags = 0;
280         let result = unsafe {
281             c::WSARecv(
282                 self.0,
283                 bufs.as_mut_ptr() as *mut c::WSABUF,
284                 length,
285                 &mut nread,
286                 &mut flags,
287                 ptr::null_mut(),
288                 ptr::null_mut(),
289             )
290         };
291
292         match result {
293             0 => Ok(nread as usize),
294             _ => {
295                 let error = unsafe { c::WSAGetLastError() };
296
297                 if error == c::WSAESHUTDOWN {
298                     Ok(0)
299                 } else {
300                     Err(io::Error::from_raw_os_error(error))
301                 }
302             }
303         }
304     }
305
306     #[inline]
307     pub fn is_read_vectored(&self) -> bool {
308         true
309     }
310
311     pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
312         self.recv_with_flags(buf, c::MSG_PEEK)
313     }
314
315     fn recv_from_with_flags(
316         &self,
317         buf: &mut [u8],
318         flags: c_int,
319     ) -> io::Result<(usize, SocketAddr)> {
320         let mut storage = unsafe { mem::zeroed::<c::SOCKADDR_STORAGE_LH>() };
321         let mut addrlen = mem::size_of_val(&storage) as c::socklen_t;
322         let length = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
323
324         // On unix when a socket is shut down all further reads return 0, so we
325         // do the same on windows to map a shut down socket to returning EOF.
326         let result = unsafe {
327             c::recvfrom(
328                 self.0,
329                 buf.as_mut_ptr() as *mut _,
330                 length,
331                 flags,
332                 &mut storage as *mut _ as *mut _,
333                 &mut addrlen,
334             )
335         };
336
337         match result {
338             c::SOCKET_ERROR => {
339                 let error = unsafe { c::WSAGetLastError() };
340
341                 if error == c::WSAESHUTDOWN {
342                     Ok((0, net::sockaddr_to_addr(&storage, addrlen as usize)?))
343                 } else {
344                     Err(io::Error::from_raw_os_error(error))
345                 }
346             }
347             _ => Ok((result as usize, net::sockaddr_to_addr(&storage, addrlen as usize)?)),
348         }
349     }
350
351     pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
352         self.recv_from_with_flags(buf, 0)
353     }
354
355     pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
356         self.recv_from_with_flags(buf, c::MSG_PEEK)
357     }
358
359     pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
360         let length = cmp::min(bufs.len(), c::DWORD::MAX as usize) as c::DWORD;
361         let mut nwritten = 0;
362         let result = unsafe {
363             c::WSASend(
364                 self.0,
365                 bufs.as_ptr() as *const c::WSABUF as *mut _,
366                 length,
367                 &mut nwritten,
368                 0,
369                 ptr::null_mut(),
370                 ptr::null_mut(),
371             )
372         };
373         cvt(result).map(|_| nwritten as usize)
374     }
375
376     #[inline]
377     pub fn is_write_vectored(&self) -> bool {
378         true
379     }
380
381     pub fn set_timeout(&self, dur: Option<Duration>, kind: c_int) -> io::Result<()> {
382         let timeout = match dur {
383             Some(dur) => {
384                 let timeout = sys::dur2timeout(dur);
385                 if timeout == 0 {
386                     return Err(io::Error::new_const(
387                         io::ErrorKind::InvalidInput,
388                         &"cannot set a 0 duration timeout",
389                     ));
390                 }
391                 timeout
392             }
393             None => 0,
394         };
395         net::setsockopt(self, c::SOL_SOCKET, kind, timeout)
396     }
397
398     pub fn timeout(&self, kind: c_int) -> io::Result<Option<Duration>> {
399         let raw: c::DWORD = net::getsockopt(self, c::SOL_SOCKET, kind)?;
400         if raw == 0 {
401             Ok(None)
402         } else {
403             let secs = raw / 1000;
404             let nsec = (raw % 1000) * 1000000;
405             Ok(Some(Duration::new(secs as u64, nsec as u32)))
406         }
407     }
408
409     #[cfg(not(target_vendor = "uwp"))]
410     fn set_no_inherit(&self) -> io::Result<()> {
411         sys::cvt(unsafe { c::SetHandleInformation(self.0 as c::HANDLE, c::HANDLE_FLAG_INHERIT, 0) })
412             .map(drop)
413     }
414
415     #[cfg(target_vendor = "uwp")]
416     fn set_no_inherit(&self) -> io::Result<()> {
417         Err(io::Error::new_const(io::ErrorKind::Unsupported, &"Unavailable on UWP"))
418     }
419
420     pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
421         let how = match how {
422             Shutdown::Write => c::SD_SEND,
423             Shutdown::Read => c::SD_RECEIVE,
424             Shutdown::Both => c::SD_BOTH,
425         };
426         let result = unsafe { c::shutdown(self.0, how) };
427         cvt(result).map(drop)
428     }
429
430     pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
431         let mut nonblocking = nonblocking as c_ulong;
432         let result = unsafe { c::ioctlsocket(self.0, c::FIONBIO as c_int, &mut nonblocking) };
433         cvt(result).map(drop)
434     }
435
436     pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
437         net::setsockopt(self, c::IPPROTO_TCP, c::TCP_NODELAY, nodelay as c::BYTE)
438     }
439
440     pub fn nodelay(&self) -> io::Result<bool> {
441         let raw: c::BYTE = net::getsockopt(self, c::IPPROTO_TCP, c::TCP_NODELAY)?;
442         Ok(raw != 0)
443     }
444
445     pub fn take_error(&self) -> io::Result<Option<io::Error>> {
446         let raw: c_int = net::getsockopt(self, c::SOL_SOCKET, c::SO_ERROR)?;
447         if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
448     }
449 }
450
451 #[unstable(reason = "not public", issue = "none", feature = "fd_read")]
452 impl<'a> Read for &'a Socket {
453     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
454         (**self).read(buf)
455     }
456 }
457
458 impl Drop for Socket {
459     fn drop(&mut self) {
460         let _ = unsafe { c::closesocket(self.0) };
461     }
462 }
463
464 impl AsInner<c::SOCKET> for Socket {
465     fn as_inner(&self) -> &c::SOCKET {
466         &self.0
467     }
468 }
469
470 impl FromInner<c::SOCKET> for Socket {
471     fn from_inner(sock: c::SOCKET) -> Socket {
472         Socket(sock)
473     }
474 }
475
476 impl IntoInner<c::SOCKET> for Socket {
477     fn into_inner(self) -> c::SOCKET {
478         let ret = self.0;
479         mem::forget(self);
480         ret
481     }
482 }