2019-12-21 13:16:18 +02:00
|
|
|
#![unstable(issue = "none", feature = "windows_net")]
|
2016-02-12 10:29:25 -08:00
|
|
|
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::cmp;
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::io::{self, IoSlice, IoSliceMut, Read};
|
2021-05-24 23:58:20 +02:00
|
|
|
use crate::lazy::SyncOnceCell;
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::mem;
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::net::{Shutdown, SocketAddr};
|
2021-06-30 21:44:30 -07:00
|
|
|
use crate::os::windows::io::{
|
|
|
|
|
AsRawSocket, AsSocket, BorrowedSocket, FromRawSocket, IntoRawSocket, OwnedSocket, RawSocket,
|
|
|
|
|
};
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::ptr;
|
|
|
|
|
use crate::sys;
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::sys::c;
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::sys_common::net;
|
2021-04-11 07:05:39 +02:00
|
|
|
use crate::sys_common::{AsInner, FromInner, IntoInner};
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::time::Duration;
|
|
|
|
|
|
2021-08-30 13:02:15 -04:00
|
|
|
use libc::{c_int, c_long, c_ulong, c_ushort};
|
2015-02-05 16:50:11 -08:00
|
|
|
|
|
|
|
|
pub type wrlen_t = i32;
|
|
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
pub mod netc {
|
2019-12-22 17:42:04 -05:00
|
|
|
pub use crate::sys::c::ADDRESS_FAMILY as sa_family_t;
|
|
|
|
|
pub use crate::sys::c::ADDRINFOA as addrinfo;
|
2019-02-11 04:23:21 +09:00
|
|
|
pub use crate::sys::c::SOCKADDR as sockaddr;
|
|
|
|
|
pub use crate::sys::c::SOCKADDR_STORAGE_LH as sockaddr_storage;
|
2019-12-22 17:42:04 -05:00
|
|
|
pub use crate::sys::c::*;
|
2015-11-02 16:23:22 -08:00
|
|
|
}
|
|
|
|
|
|
2021-06-30 21:44:30 -07:00
|
|
|
pub struct Socket(OwnedSocket);
|
2015-02-05 16:50:11 -08:00
|
|
|
|
2021-10-02 22:10:00 +03:00
|
|
|
static WSA_CLEANUP: SyncOnceCell<unsafe extern "system" fn() -> i32> = SyncOnceCell::new();
|
2021-04-18 05:13:53 +02:00
|
|
|
|
2015-02-23 23:50:32 +01:00
|
|
|
/// Checks whether the Windows socket interface has been started already, and
|
|
|
|
|
/// if not, starts it.
|
2015-02-05 16:50:11 -08:00
|
|
|
pub fn init() {
|
2021-10-02 22:10:00 +03:00
|
|
|
let _ = WSA_CLEANUP.get_or_init(|| unsafe {
|
2015-02-05 16:50:11 -08:00
|
|
|
let mut data: c::WSADATA = mem::zeroed();
|
2019-12-22 17:42:04 -05:00
|
|
|
let ret = c::WSAStartup(
|
|
|
|
|
0x202, // version 2.2
|
|
|
|
|
&mut data,
|
|
|
|
|
);
|
2015-02-05 16:50:11 -08:00
|
|
|
assert_eq!(ret, 0);
|
2021-05-24 23:58:20 +02:00
|
|
|
|
|
|
|
|
// Only register `WSACleanup` if `WSAStartup` is actually ever called.
|
|
|
|
|
// Workaround to prevent linking to `WS2_32.dll` when no network functionality is used.
|
|
|
|
|
// See issue #85441.
|
|
|
|
|
c::WSACleanup
|
2015-02-05 16:50:11 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 07:05:39 +02:00
|
|
|
pub fn cleanup() {
|
2021-05-24 23:58:20 +02:00
|
|
|
// only perform cleanup if network functionality was actually initialized
|
2021-10-02 22:10:00 +03:00
|
|
|
if let Some(cleanup) = WSA_CLEANUP.get() {
|
2021-04-18 05:13:53 +02:00
|
|
|
unsafe {
|
2021-05-24 23:58:20 +02:00
|
|
|
cleanup();
|
2021-04-18 05:13:53 +02:00
|
|
|
}
|
2021-04-11 07:05:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-23 23:50:32 +01:00
|
|
|
/// Returns the last error from the Windows socket interface.
|
2015-02-05 16:50:11 -08:00
|
|
|
fn last_error() -> io::Error {
|
2015-04-08 16:41:14 -07:00
|
|
|
io::Error::from_raw_os_error(unsafe { c::WSAGetLastError() })
|
2015-02-05 16:50:11 -08:00
|
|
|
}
|
|
|
|
|
|
2016-06-28 08:56:56 -07:00
|
|
|
#[doc(hidden)]
|
|
|
|
|
pub trait IsMinusOne {
|
|
|
|
|
fn is_minus_one(&self) -> bool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! impl_is_minus_one {
|
|
|
|
|
($($t:ident)*) => ($(impl IsMinusOne for $t {
|
|
|
|
|
fn is_minus_one(&self) -> bool {
|
|
|
|
|
*self == -1
|
|
|
|
|
}
|
|
|
|
|
})*)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl_is_minus_one! { i8 i16 i32 i64 isize }
|
|
|
|
|
|
2015-02-23 23:50:32 +01:00
|
|
|
/// Checks if the signed integer is the Windows constant `SOCKET_ERROR` (-1)
|
2014-11-06 00:40:08 +08:00
|
|
|
/// and if so, returns the last error from the Windows socket interface. This
|
2015-02-23 23:50:32 +01:00
|
|
|
/// function must be called before another call to the socket API is made.
|
2016-06-28 08:56:56 -07:00
|
|
|
pub fn cvt<T: IsMinusOne>(t: T) -> io::Result<T> {
|
2019-12-22 17:42:04 -05:00
|
|
|
if t.is_minus_one() { Err(last_error()) } else { Ok(t) }
|
2015-02-05 16:50:11 -08:00
|
|
|
}
|
|
|
|
|
|
2014-11-06 00:40:08 +08:00
|
|
|
/// A variant of `cvt` for `getaddrinfo` which return 0 for a success.
|
2015-02-05 16:50:11 -08:00
|
|
|
pub fn cvt_gai(err: c_int) -> io::Result<()> {
|
2019-12-22 17:42:04 -05:00
|
|
|
if err == 0 { Ok(()) } else { Err(last_error()) }
|
2015-02-05 16:50:11 -08:00
|
|
|
}
|
|
|
|
|
|
2014-11-06 00:40:08 +08:00
|
|
|
/// Just to provide the same interface as sys/unix/net.rs
|
2015-04-17 23:45:55 -07:00
|
|
|
pub fn cvt_r<T, F>(mut f: F) -> io::Result<T>
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
|
T: IsMinusOne,
|
|
|
|
|
F: FnMut() -> T,
|
2015-04-17 23:45:55 -07:00
|
|
|
{
|
2015-02-05 16:50:11 -08:00
|
|
|
cvt(f())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Socket {
|
|
|
|
|
pub fn new(addr: &SocketAddr, ty: c_int) -> io::Result<Socket> {
|
2021-05-28 20:32:42 +01:00
|
|
|
let family = match *addr {
|
2015-11-02 16:23:22 -08:00
|
|
|
SocketAddr::V4(..) => c::AF_INET,
|
|
|
|
|
SocketAddr::V6(..) => c::AF_INET6,
|
2015-02-05 16:50:11 -08:00
|
|
|
};
|
2016-03-22 22:01:37 -05:00
|
|
|
let socket = unsafe {
|
2021-05-28 20:32:42 +01:00
|
|
|
c::WSASocketW(
|
|
|
|
|
family,
|
2019-12-22 17:42:04 -05:00
|
|
|
ty,
|
|
|
|
|
0,
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
0,
|
|
|
|
|
c::WSA_FLAG_OVERLAPPED | c::WSA_FLAG_NO_HANDLE_INHERIT,
|
2021-05-28 20:32:42 +01:00
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if socket != c::INVALID_SOCKET {
|
2021-06-30 21:44:30 -07:00
|
|
|
unsafe { Ok(Self::from_raw_socket(socket)) }
|
2021-05-28 20:32:42 +01:00
|
|
|
} else {
|
|
|
|
|
let error = unsafe { c::WSAGetLastError() };
|
|
|
|
|
|
|
|
|
|
if error != c::WSAEPROTOTYPE && error != c::WSAEINVAL {
|
|
|
|
|
return Err(io::Error::from_raw_os_error(error));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let socket =
|
|
|
|
|
unsafe { c::WSASocketW(family, ty, 0, ptr::null_mut(), 0, c::WSA_FLAG_OVERLAPPED) };
|
|
|
|
|
|
|
|
|
|
if socket == c::INVALID_SOCKET {
|
|
|
|
|
return Err(last_error());
|
2015-06-29 09:46:35 -07:00
|
|
|
}
|
2021-05-28 20:32:42 +01:00
|
|
|
|
2021-06-30 21:44:30 -07:00
|
|
|
unsafe {
|
|
|
|
|
let socket = Self::from_raw_socket(socket);
|
2021-09-09 15:30:17 -07:00
|
|
|
socket.0.set_no_inherit()?;
|
2021-06-30 21:44:30 -07:00
|
|
|
Ok(socket)
|
|
|
|
|
}
|
2021-05-28 20:32:42 +01:00
|
|
|
}
|
2015-02-05 16:50:11 -08:00
|
|
|
}
|
|
|
|
|
|
2017-07-04 23:46:24 -07:00
|
|
|
pub fn connect_timeout(&self, addr: &SocketAddr, timeout: Duration) -> io::Result<()> {
|
|
|
|
|
self.set_nonblocking(true)?;
|
2021-05-28 20:32:42 +01:00
|
|
|
let result = {
|
2017-07-04 23:46:24 -07:00
|
|
|
let (addrp, len) = addr.into_inner();
|
2021-06-30 21:44:30 -07:00
|
|
|
let result = unsafe { c::connect(self.as_raw_socket(), addrp, len) };
|
2021-05-28 20:32:42 +01:00
|
|
|
cvt(result).map(drop)
|
2017-07-04 23:46:24 -07:00
|
|
|
};
|
|
|
|
|
self.set_nonblocking(false)?;
|
|
|
|
|
|
2021-05-28 20:32:42 +01:00
|
|
|
match result {
|
|
|
|
|
Err(ref error) if error.kind() == io::ErrorKind::WouldBlock => {
|
|
|
|
|
if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
|
|
|
|
|
return Err(io::Error::new_const(
|
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
|
&"cannot set a 0 duration timeout",
|
|
|
|
|
));
|
|
|
|
|
}
|
2017-07-04 23:46:24 -07:00
|
|
|
|
2021-05-28 20:32:42 +01:00
|
|
|
let mut timeout = c::timeval {
|
|
|
|
|
tv_sec: timeout.as_secs() as c_long,
|
|
|
|
|
tv_usec: (timeout.subsec_nanos() / 1000) as c_long,
|
|
|
|
|
};
|
2017-07-04 23:46:24 -07:00
|
|
|
|
2021-05-28 20:32:42 +01:00
|
|
|
if timeout.tv_sec == 0 && timeout.tv_usec == 0 {
|
|
|
|
|
timeout.tv_usec = 1;
|
|
|
|
|
}
|
2017-07-04 23:46:24 -07:00
|
|
|
|
2021-05-28 20:32:42 +01:00
|
|
|
let fds = {
|
|
|
|
|
let mut fds = unsafe { mem::zeroed::<c::fd_set>() };
|
|
|
|
|
fds.fd_count = 1;
|
2021-06-30 21:44:30 -07:00
|
|
|
fds.fd_array[0] = self.as_raw_socket();
|
2021-05-28 20:32:42 +01:00
|
|
|
fds
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut writefds = fds;
|
|
|
|
|
let mut errorfds = fds;
|
|
|
|
|
|
|
|
|
|
let count = {
|
|
|
|
|
let result = unsafe {
|
|
|
|
|
c::select(1, ptr::null_mut(), &mut writefds, &mut errorfds, &timeout)
|
|
|
|
|
};
|
|
|
|
|
cvt(result)?
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match count {
|
|
|
|
|
0 => {
|
|
|
|
|
Err(io::Error::new_const(io::ErrorKind::TimedOut, &"connection timed out"))
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
if writefds.fd_count != 1 {
|
|
|
|
|
if let Some(e) = self.take_error()? {
|
|
|
|
|
return Err(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-04 23:46:24 -07:00
|
|
|
|
2021-05-28 20:32:42 +01:00
|
|
|
Ok(())
|
2017-07-04 23:46:24 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-28 20:32:42 +01:00
|
|
|
_ => result,
|
2017-07-04 23:46:24 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn accept(&self, storage: *mut c::SOCKADDR, len: *mut c_int) -> io::Result<Socket> {
|
2021-06-30 21:44:30 -07:00
|
|
|
let socket = unsafe { c::accept(self.as_raw_socket(), storage, len) };
|
2021-05-28 20:32:42 +01:00
|
|
|
|
|
|
|
|
match socket {
|
|
|
|
|
c::INVALID_SOCKET => Err(last_error()),
|
2021-06-30 21:44:30 -07:00
|
|
|
_ => unsafe { Ok(Self::from_raw_socket(socket)) },
|
2021-05-28 20:32:42 +01:00
|
|
|
}
|
2015-02-05 16:50:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn duplicate(&self) -> io::Result<Socket> {
|
2021-09-09 14:44:54 -07:00
|
|
|
Ok(Self(self.0.try_clone()?))
|
2015-02-05 16:50:11 -08:00
|
|
|
}
|
|
|
|
|
|
2017-01-10 19:11:56 -08:00
|
|
|
fn recv_with_flags(&self, buf: &mut [u8], flags: c_int) -> io::Result<usize> {
|
2015-02-05 16:50:11 -08:00
|
|
|
// On unix when a socket is shut down all further reads return 0, so we
|
|
|
|
|
// do the same on windows to map a shut down socket to returning EOF.
|
2021-05-28 20:32:42 +01:00
|
|
|
let length = cmp::min(buf.len(), i32::MAX as usize) as i32;
|
2021-06-30 21:44:30 -07:00
|
|
|
let result =
|
|
|
|
|
unsafe { c::recv(self.as_raw_socket(), buf.as_mut_ptr() as *mut _, length, flags) };
|
2021-05-28 20:32:42 +01:00
|
|
|
|
|
|
|
|
match result {
|
|
|
|
|
c::SOCKET_ERROR => {
|
|
|
|
|
let error = unsafe { c::WSAGetLastError() };
|
|
|
|
|
|
|
|
|
|
if error == c::WSAESHUTDOWN {
|
|
|
|
|
Ok(0)
|
|
|
|
|
} else {
|
|
|
|
|
Err(io::Error::from_raw_os_error(error))
|
|
|
|
|
}
|
2015-02-05 16:50:11 -08:00
|
|
|
}
|
2021-05-28 20:32:42 +01:00
|
|
|
_ => Ok(result as usize),
|
2015-02-05 16:50:11 -08:00
|
|
|
}
|
|
|
|
|
}
|
2015-05-26 23:47:03 -07:00
|
|
|
|
2017-01-10 19:11:56 -08:00
|
|
|
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
|
self.recv_with_flags(buf, 0)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-27 08:34:08 -07:00
|
|
|
pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
2019-02-08 20:42:34 +01:00
|
|
|
// On unix when a socket is shut down all further reads return 0, so we
|
|
|
|
|
// do the same on windows to map a shut down socket to returning EOF.
|
2021-05-28 20:32:42 +01:00
|
|
|
let length = cmp::min(bufs.len(), c::DWORD::MAX as usize) as c::DWORD;
|
2019-02-08 20:42:34 +01:00
|
|
|
let mut nread = 0;
|
|
|
|
|
let mut flags = 0;
|
2021-05-28 20:32:42 +01:00
|
|
|
let result = unsafe {
|
|
|
|
|
c::WSARecv(
|
2021-06-30 21:44:30 -07:00
|
|
|
self.as_raw_socket(),
|
2019-02-08 20:42:34 +01:00
|
|
|
bufs.as_mut_ptr() as *mut c::WSABUF,
|
2021-05-28 20:32:42 +01:00
|
|
|
length,
|
2019-02-08 20:42:34 +01:00
|
|
|
&mut nread,
|
|
|
|
|
&mut flags,
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
ptr::null_mut(),
|
2021-05-28 20:32:42 +01:00
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match result {
|
|
|
|
|
0 => Ok(nread as usize),
|
|
|
|
|
_ => {
|
|
|
|
|
let error = unsafe { c::WSAGetLastError() };
|
|
|
|
|
|
|
|
|
|
if error == c::WSAESHUTDOWN {
|
|
|
|
|
Ok(0)
|
|
|
|
|
} else {
|
|
|
|
|
Err(io::Error::from_raw_os_error(error))
|
|
|
|
|
}
|
2019-02-08 20:42:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 11:26:05 -08:00
|
|
|
#[inline]
|
2020-03-11 18:02:52 -07:00
|
|
|
pub fn is_read_vectored(&self) -> bool {
|
2020-01-03 11:26:05 -08:00
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-10 19:11:56 -08:00
|
|
|
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
|
self.recv_with_flags(buf, c::MSG_PEEK)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn recv_from_with_flags(
|
|
|
|
|
&self,
|
|
|
|
|
buf: &mut [u8],
|
|
|
|
|
flags: c_int,
|
|
|
|
|
) -> io::Result<(usize, SocketAddr)> {
|
2021-05-28 20:32:42 +01:00
|
|
|
let mut storage = unsafe { mem::zeroed::<c::SOCKADDR_STORAGE_LH>() };
|
2017-01-10 19:11:56 -08:00
|
|
|
let mut addrlen = mem::size_of_val(&storage) as c::socklen_t;
|
2021-05-28 20:32:42 +01:00
|
|
|
let length = cmp::min(buf.len(), <wrlen_t>::MAX as usize) as wrlen_t;
|
2017-01-10 19:11:56 -08:00
|
|
|
|
|
|
|
|
// On unix when a socket is shut down all further reads return 0, so we
|
|
|
|
|
// do the same on windows to map a shut down socket to returning EOF.
|
2021-05-28 20:32:42 +01:00
|
|
|
let result = unsafe {
|
|
|
|
|
c::recvfrom(
|
2021-06-30 21:44:30 -07:00
|
|
|
self.as_raw_socket(),
|
2021-05-28 20:32:42 +01:00
|
|
|
buf.as_mut_ptr() as *mut _,
|
|
|
|
|
length,
|
2019-12-22 17:42:04 -05:00
|
|
|
flags,
|
|
|
|
|
&mut storage as *mut _ as *mut _,
|
|
|
|
|
&mut addrlen,
|
2021-05-28 20:32:42 +01:00
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match result {
|
|
|
|
|
c::SOCKET_ERROR => {
|
|
|
|
|
let error = unsafe { c::WSAGetLastError() };
|
|
|
|
|
|
|
|
|
|
if error == c::WSAESHUTDOWN {
|
2017-01-10 19:11:56 -08:00
|
|
|
Ok((0, net::sockaddr_to_addr(&storage, addrlen as usize)?))
|
2021-05-28 20:32:42 +01:00
|
|
|
} else {
|
|
|
|
|
Err(io::Error::from_raw_os_error(error))
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2017-01-10 19:11:56 -08:00
|
|
|
}
|
2021-05-28 20:32:42 +01:00
|
|
|
_ => Ok((result as usize, net::sockaddr_to_addr(&storage, addrlen as usize)?)),
|
2017-01-10 19:11:56 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
|
|
|
|
|
self.recv_from_with_flags(buf, 0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
|
|
|
|
|
self.recv_from_with_flags(buf, c::MSG_PEEK)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-27 08:34:08 -07:00
|
|
|
pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
2021-05-28 20:32:42 +01:00
|
|
|
let length = cmp::min(bufs.len(), c::DWORD::MAX as usize) as c::DWORD;
|
2019-02-08 20:42:34 +01:00
|
|
|
let mut nwritten = 0;
|
2021-05-28 20:32:42 +01:00
|
|
|
let result = unsafe {
|
|
|
|
|
c::WSASend(
|
2021-06-30 21:44:30 -07:00
|
|
|
self.as_raw_socket(),
|
2021-05-28 20:32:42 +01:00
|
|
|
bufs.as_ptr() as *const c::WSABUF as *mut _,
|
|
|
|
|
length,
|
2019-02-08 20:42:34 +01:00
|
|
|
&mut nwritten,
|
|
|
|
|
0,
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
ptr::null_mut(),
|
2021-05-28 20:32:42 +01:00
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
cvt(result).map(|_| nwritten as usize)
|
2019-02-08 20:42:34 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-03 11:26:05 -08:00
|
|
|
#[inline]
|
2020-03-11 18:02:52 -07:00
|
|
|
pub fn is_write_vectored(&self) -> bool {
|
2020-01-03 11:26:05 -08:00
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn set_timeout(&self, dur: Option<Duration>, kind: c_int) -> io::Result<()> {
|
2015-05-26 23:47:03 -07:00
|
|
|
let timeout = match dur {
|
|
|
|
|
Some(dur) => {
|
|
|
|
|
let timeout = sys::dur2timeout(dur);
|
|
|
|
|
if timeout == 0 {
|
2021-03-21 20:22:38 +01:00
|
|
|
return Err(io::Error::new_const(
|
2019-12-22 17:42:04 -05:00
|
|
|
io::ErrorKind::InvalidInput,
|
2021-03-21 20:22:38 +01:00
|
|
|
&"cannot set a 0 duration timeout",
|
2019-12-22 17:42:04 -05:00
|
|
|
));
|
2015-05-26 23:47:03 -07:00
|
|
|
}
|
|
|
|
|
timeout
|
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
None => 0,
|
2015-05-26 23:47:03 -07:00
|
|
|
};
|
2015-11-02 16:23:22 -08:00
|
|
|
net::setsockopt(self, c::SOL_SOCKET, kind, timeout)
|
2015-05-26 23:47:03 -07:00
|
|
|
}
|
|
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
pub fn timeout(&self, kind: c_int) -> io::Result<Option<Duration>> {
|
2016-03-22 22:01:37 -05:00
|
|
|
let raw: c::DWORD = net::getsockopt(self, c::SOL_SOCKET, kind)?;
|
2015-05-26 23:47:03 -07:00
|
|
|
if raw == 0 {
|
|
|
|
|
Ok(None)
|
|
|
|
|
} else {
|
|
|
|
|
let secs = raw / 1000;
|
|
|
|
|
let nsec = (raw % 1000) * 1000000;
|
|
|
|
|
Ok(Some(Duration::new(secs as u64, nsec as u32)))
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-29 09:46:35 -07:00
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
|
|
|
|
|
let how = match how {
|
|
|
|
|
Shutdown::Write => c::SD_SEND,
|
|
|
|
|
Shutdown::Read => c::SD_RECEIVE,
|
|
|
|
|
Shutdown::Both => c::SD_BOTH,
|
|
|
|
|
};
|
2021-06-30 21:44:30 -07:00
|
|
|
let result = unsafe { c::shutdown(self.as_raw_socket(), how) };
|
2021-05-28 20:32:42 +01:00
|
|
|
cvt(result).map(drop)
|
2015-11-02 16:23:22 -08:00
|
|
|
}
|
2016-02-27 14:15:19 -08:00
|
|
|
|
2016-02-27 21:05:32 -08:00
|
|
|
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
|
|
|
|
|
let mut nonblocking = nonblocking as c_ulong;
|
2021-06-30 21:44:30 -07:00
|
|
|
let result =
|
|
|
|
|
unsafe { c::ioctlsocket(self.as_raw_socket(), c::FIONBIO as c_int, &mut nonblocking) };
|
2021-05-28 20:32:42 +01:00
|
|
|
cvt(result).map(drop)
|
2016-02-27 14:15:19 -08:00
|
|
|
}
|
|
|
|
|
|
2021-08-30 13:02:15 -04:00
|
|
|
pub fn set_linger(&self, linger: Option<Duration>) -> io::Result<()> {
|
|
|
|
|
let linger = c::linger {
|
|
|
|
|
l_onoff: linger.is_some() as c_ushort,
|
2021-08-30 14:00:21 -04:00
|
|
|
l_linger: linger.unwrap_or_default().as_secs() as c_ushort,
|
2021-08-30 13:02:15 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
net::setsockopt(self, c::SOL_SOCKET, c::SO_LINGER, linger)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn linger(&self) -> io::Result<Option<Duration>> {
|
|
|
|
|
let val: c::linger = net::getsockopt(self, c::SOL_SOCKET, c::SO_LINGER)?;
|
|
|
|
|
|
|
|
|
|
Ok((val.l_onoff != 0).then(|| Duration::from_secs(val.l_linger as u64)))
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-27 21:05:32 -08:00
|
|
|
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
|
|
|
|
|
net::setsockopt(self, c::IPPROTO_TCP, c::TCP_NODELAY, nodelay as c::BYTE)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn nodelay(&self) -> io::Result<bool> {
|
2016-03-22 22:01:37 -05:00
|
|
|
let raw: c::BYTE = net::getsockopt(self, c::IPPROTO_TCP, c::TCP_NODELAY)?;
|
2016-02-27 21:05:32 -08:00
|
|
|
Ok(raw != 0)
|
2016-02-27 14:15:19 -08:00
|
|
|
}
|
2016-03-16 20:50:45 -07:00
|
|
|
|
|
|
|
|
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
|
2016-03-22 22:01:37 -05:00
|
|
|
let raw: c_int = net::getsockopt(self, c::SOL_SOCKET, c::SO_ERROR)?;
|
2019-12-22 17:42:04 -05:00
|
|
|
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
|
2016-03-16 20:50:45 -07:00
|
|
|
}
|
2021-06-30 21:44:30 -07:00
|
|
|
|
|
|
|
|
// This is used by sys_common code to abstract over Windows and Unix.
|
|
|
|
|
pub fn as_raw(&self) -> RawSocket {
|
|
|
|
|
self.as_inner().as_raw_socket()
|
|
|
|
|
}
|
2015-02-05 16:50:11 -08:00
|
|
|
}
|
|
|
|
|
|
2019-12-21 13:16:18 +02:00
|
|
|
#[unstable(reason = "not public", issue = "none", feature = "fd_read")]
|
2016-02-12 00:17:24 -08:00
|
|
|
impl<'a> Read for &'a Socket {
|
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
|
(**self).read(buf)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-30 21:44:30 -07:00
|
|
|
impl AsInner<OwnedSocket> for Socket {
|
|
|
|
|
fn as_inner(&self) -> &OwnedSocket {
|
|
|
|
|
&self.0
|
2015-02-05 16:50:11 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-30 21:44:30 -07:00
|
|
|
impl FromInner<OwnedSocket> for Socket {
|
|
|
|
|
fn from_inner(sock: OwnedSocket) -> Socket {
|
|
|
|
|
Socket(sock)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2015-02-05 16:50:11 -08:00
|
|
|
}
|
std: Stabilize parts of std::os::platform::io
This commit stabilizes the platform-specific `io` modules, specifically around
the traits having to do with the raw representation of each object on each
platform.
Specifically, the following material was stabilized:
* `AsRaw{Fd,Socket,Handle}`
* `RawFd` (renamed from `Fd`)
* `RawHandle` (renamed from `Handle`)
* `RawSocket` (renamed from `Socket`)
* `AsRaw{Fd,Socket,Handle}` implementations
* `std::os::{unix, windows}::io`
The following material was added as `#[unstable]`:
* `FromRaw{Fd,Socket,Handle}`
* Implementations for various primitives
There are a number of future improvements that are possible to make to this
module, but this should cover a good bit of functionality desired from these
modules for now. Some specific future additions may include:
* `IntoRawXXX` traits to consume the raw representation and cancel the
auto-destructor.
* `Fd`, `Socket`, and `Handle` abstractions that behave like Rust objects and
have nice methods for various syscalls.
At this time though, these are considered backwards-compatible extensions and
will not be stabilized at this time.
This commit is a breaking change due to the addition of `Raw` in from of the
type aliases in each of the platform-specific modules.
[breaking-change]
2015-03-26 16:18:29 -07:00
|
|
|
|
2021-06-30 21:44:30 -07:00
|
|
|
impl IntoInner<OwnedSocket> for Socket {
|
|
|
|
|
fn into_inner(self) -> OwnedSocket {
|
|
|
|
|
self.0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AsSocket for Socket {
|
|
|
|
|
fn as_socket(&self) -> BorrowedSocket<'_> {
|
|
|
|
|
self.0.as_socket()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AsRawSocket for Socket {
|
|
|
|
|
fn as_raw_socket(&self) -> RawSocket {
|
|
|
|
|
self.0.as_raw_socket()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IntoRawSocket for Socket {
|
|
|
|
|
fn into_raw_socket(self) -> RawSocket {
|
|
|
|
|
self.0.into_raw_socket()
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
std: Stabilize parts of std::os::platform::io
This commit stabilizes the platform-specific `io` modules, specifically around
the traits having to do with the raw representation of each object on each
platform.
Specifically, the following material was stabilized:
* `AsRaw{Fd,Socket,Handle}`
* `RawFd` (renamed from `Fd`)
* `RawHandle` (renamed from `Handle`)
* `RawSocket` (renamed from `Socket`)
* `AsRaw{Fd,Socket,Handle}` implementations
* `std::os::{unix, windows}::io`
The following material was added as `#[unstable]`:
* `FromRaw{Fd,Socket,Handle}`
* Implementations for various primitives
There are a number of future improvements that are possible to make to this
module, but this should cover a good bit of functionality desired from these
modules for now. Some specific future additions may include:
* `IntoRawXXX` traits to consume the raw representation and cancel the
auto-destructor.
* `Fd`, `Socket`, and `Handle` abstractions that behave like Rust objects and
have nice methods for various syscalls.
At this time though, these are considered backwards-compatible extensions and
will not be stabilized at this time.
This commit is a breaking change due to the addition of `Raw` in from of the
type aliases in each of the platform-specific modules.
[breaking-change]
2015-03-26 16:18:29 -07:00
|
|
|
}
|
2015-07-15 23:31:24 -07:00
|
|
|
|
2021-06-30 21:44:30 -07:00
|
|
|
impl FromRawSocket for Socket {
|
|
|
|
|
unsafe fn from_raw_socket(raw_socket: RawSocket) -> Self {
|
|
|
|
|
Self(FromRawSocket::from_raw_socket(raw_socket))
|
2015-07-15 23:31:24 -07:00
|
|
|
}
|
|
|
|
|
}
|