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};
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::mem;
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::net::{Shutdown, SocketAddr};
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::ptr;
|
|
|
|
|
use crate::sync::Once;
|
|
|
|
|
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;
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::sys_common::{self, AsInner, FromInner, IntoInner};
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::time::Duration;
|
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
use libc::{c_int, c_long, c_ulong, c_void};
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Socket(c::SOCKET);
|
2015-02-05 16:50:11 -08: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() {
|
2015-05-27 11:18:36 +03:00
|
|
|
static START: Once = Once::new();
|
2015-02-05 16:50:11 -08:00
|
|
|
|
|
|
|
|
START.call_once(|| unsafe {
|
|
|
|
|
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);
|
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let _ = sys_common::at_exit(|| {
|
|
|
|
|
c::WSACleanup();
|
|
|
|
|
});
|
2015-02-05 16:50:11 -08: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> {
|
std: Stabilize the `net` module
This commit performs a stabilization pass over the std::net module,
incorporating the changes from RFC 923. Specifically, the following actions were
taken:
Stable functionality:
* `net` (the name)
* `Shutdown`
* `Shutdown::{Read, Write, Both}`
* `lookup_host`
* `LookupHost`
* `SocketAddr`
* `SocketAddr::{V4, V6}`
* `SocketAddr::port`
* `SocketAddrV4`
* `SocketAddrV4::{new, ip, port}`
* `SocketAddrV6`
* `SocketAddrV4::{new, ip, port, flowinfo, scope_id}`
* Common trait impls for socket addr structures
* `ToSocketAddrs`
* `ToSocketAddrs::Iter`
* `ToSocketAddrs::to_socket_addrs`
* `ToSocketAddrs for {SocketAddr*, (Ipv*Addr, u16), str, (str, u16)}`
* `Ipv4Addr`
* `Ipv4Addr::{new, octets, to_ipv6_compatible, to_ipv6_mapped}`
* `Ipv6Addr`
* `Ipv6Addr::{new, segments, to_ipv4}`
* `TcpStream`
* `TcpStream::connect`
* `TcpStream::{peer_addr, local_addr, shutdown, try_clone}`
* `{Read,Write} for {TcpStream, &TcpStream}`
* `TcpListener`
* `TcpListener::bind`
* `TcpListener::{local_addr, try_clone, accept, incoming}`
* `Incoming`
* `UdpSocket`
* `UdpSocket::bind`
* `UdpSocket::{recv_from, send_to, local_addr, try_clone}`
Unstable functionality:
* Extra methods on `Ipv{4,6}Addr` for various methods of inspecting the address
and determining qualities of it.
* Extra methods on `TcpStream` to configure various protocol options.
* Extra methods on `UdpSocket` to configure various protocol options.
Deprecated functionality:
* The `socket_addr` method has been renamed to `local_addr`
This commit is a breaking change due to the restructuring of the `SocketAddr`
type as well as the renaming of the `socket_addr` method. Migration should be
fairly straightforward, however, after accounting for the new level of
abstraction in `SocketAddr` (protocol distinction at the socket address level,
not the IP address).
[breaking-change]
2015-03-13 14:22:33 -07:00
|
|
|
let fam = 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 {
|
2019-12-22 17:42:04 -05:00
|
|
|
match c::WSASocketW(
|
|
|
|
|
fam,
|
|
|
|
|
ty,
|
|
|
|
|
0,
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
0,
|
|
|
|
|
c::WSA_FLAG_OVERLAPPED | c::WSA_FLAG_NO_HANDLE_INHERIT,
|
|
|
|
|
) {
|
|
|
|
|
c::INVALID_SOCKET => match c::WSAGetLastError() {
|
|
|
|
|
c::WSAEPROTOTYPE | c::WSAEINVAL => {
|
|
|
|
|
match c::WSASocketW(fam, ty, 0, ptr::null_mut(), 0, c::WSA_FLAG_OVERLAPPED)
|
|
|
|
|
{
|
|
|
|
|
c::INVALID_SOCKET => Err(last_error()),
|
|
|
|
|
n => {
|
|
|
|
|
let s = Socket(n);
|
|
|
|
|
s.set_no_inherit()?;
|
|
|
|
|
Ok(s)
|
2019-05-27 16:51:29 +02:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-05-27 16:51:29 +02:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
n => Err(io::Error::from_raw_os_error(n)),
|
2019-05-27 16:51:29 +02:00
|
|
|
},
|
2015-06-29 09:46:35 -07:00
|
|
|
n => Ok(Socket(n)),
|
|
|
|
|
}
|
2016-03-22 22:01:37 -05:00
|
|
|
}?;
|
2015-06-29 09:46:35 -07:00
|
|
|
Ok(socket)
|
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)?;
|
|
|
|
|
let r = unsafe {
|
|
|
|
|
let (addrp, len) = addr.into_inner();
|
|
|
|
|
cvt(c::connect(self.0, addrp, len))
|
|
|
|
|
};
|
|
|
|
|
self.set_nonblocking(false)?;
|
|
|
|
|
|
|
|
|
|
match r {
|
|
|
|
|
Ok(_) => return Ok(()),
|
|
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {}
|
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if timeout.as_secs() == 0 && timeout.subsec_nanos() == 0 {
|
2019-12-22 17:42:04 -05:00
|
|
|
return Err(io::Error::new(
|
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
|
"cannot set a 0 duration timeout",
|
|
|
|
|
));
|
2017-07-04 23:46:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut timeout = c::timeval {
|
|
|
|
|
tv_sec: timeout.as_secs() as c_long,
|
|
|
|
|
tv_usec: (timeout.subsec_nanos() / 1000) as c_long,
|
|
|
|
|
};
|
|
|
|
|
if timeout.tv_sec == 0 && timeout.tv_usec == 0 {
|
|
|
|
|
timeout.tv_usec = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let fds = unsafe {
|
|
|
|
|
let mut fds = mem::zeroed::<c::fd_set>();
|
|
|
|
|
fds.fd_count = 1;
|
|
|
|
|
fds.fd_array[0] = self.0;
|
|
|
|
|
fds
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut writefds = fds;
|
|
|
|
|
let mut errorfds = fds;
|
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let n =
|
|
|
|
|
unsafe { cvt(c::select(1, ptr::null_mut(), &mut writefds, &mut errorfds, &timeout))? };
|
2017-07-04 23:46:24 -07:00
|
|
|
|
|
|
|
|
match n {
|
|
|
|
|
0 => Err(io::Error::new(io::ErrorKind::TimedOut, "connection timed out")),
|
|
|
|
|
_ => {
|
|
|
|
|
if writefds.fd_count != 1 {
|
|
|
|
|
if let Some(e) = self.take_error()? {
|
|
|
|
|
return Err(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn accept(&self, storage: *mut c::SOCKADDR, len: *mut c_int) -> io::Result<Socket> {
|
2016-03-22 22:01:37 -05:00
|
|
|
let socket = unsafe {
|
2015-11-02 16:23:22 -08:00
|
|
|
match c::accept(self.0, storage, len) {
|
|
|
|
|
c::INVALID_SOCKET => Err(last_error()),
|
2015-06-29 09:46:35 -07:00
|
|
|
n => Ok(Socket(n)),
|
|
|
|
|
}
|
2016-03-22 22:01:37 -05:00
|
|
|
}?;
|
2015-06-29 09:46:35 -07:00
|
|
|
Ok(socket)
|
2015-02-05 16:50:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn duplicate(&self) -> io::Result<Socket> {
|
2016-03-22 22:01:37 -05:00
|
|
|
let socket = unsafe {
|
2015-02-05 16:50:11 -08:00
|
|
|
let mut info: c::WSAPROTOCOL_INFO = mem::zeroed();
|
2019-12-22 17:42:04 -05:00
|
|
|
cvt(c::WSADuplicateSocketW(self.0, c::GetCurrentProcessId(), &mut info))?;
|
|
|
|
|
|
|
|
|
|
match c::WSASocketW(
|
|
|
|
|
info.iAddressFamily,
|
|
|
|
|
info.iSocketType,
|
|
|
|
|
info.iProtocol,
|
|
|
|
|
&mut info,
|
|
|
|
|
0,
|
|
|
|
|
c::WSA_FLAG_OVERLAPPED | c::WSA_FLAG_NO_HANDLE_INHERIT,
|
|
|
|
|
) {
|
|
|
|
|
c::INVALID_SOCKET => match c::WSAGetLastError() {
|
|
|
|
|
c::WSAEPROTOTYPE | c::WSAEINVAL => {
|
|
|
|
|
match c::WSASocketW(
|
|
|
|
|
info.iAddressFamily,
|
|
|
|
|
info.iSocketType,
|
|
|
|
|
info.iProtocol,
|
|
|
|
|
&mut info,
|
|
|
|
|
0,
|
|
|
|
|
c::WSA_FLAG_OVERLAPPED,
|
|
|
|
|
) {
|
|
|
|
|
c::INVALID_SOCKET => Err(last_error()),
|
|
|
|
|
n => {
|
|
|
|
|
let s = Socket(n);
|
|
|
|
|
s.set_no_inherit()?;
|
|
|
|
|
Ok(s)
|
2019-05-27 16:51:29 +02:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-05-27 16:51:29 +02:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
n => Err(io::Error::from_raw_os_error(n)),
|
2019-05-27 16:51:29 +02:00
|
|
|
},
|
2015-02-05 16:50:11 -08:00
|
|
|
n => Ok(Socket(n)),
|
|
|
|
|
}
|
2016-03-22 22:01:37 -05:00
|
|
|
}?;
|
2015-06-29 09:46:35 -07:00
|
|
|
Ok(socket)
|
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.
|
2016-02-23 23:22:58 -08:00
|
|
|
let len = cmp::min(buf.len(), i32::max_value() as usize) as i32;
|
2015-02-05 16:50:11 -08:00
|
|
|
unsafe {
|
2017-01-10 19:11:56 -08:00
|
|
|
match c::recv(self.0, buf.as_mut_ptr() as *mut c_void, len, flags) {
|
2015-02-05 16:50:11 -08:00
|
|
|
-1 if c::WSAGetLastError() == c::WSAESHUTDOWN => Ok(0),
|
|
|
|
|
-1 => Err(last_error()),
|
2019-12-22 17:42:04 -05:00
|
|
|
n => Ok(n 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.
|
|
|
|
|
let len = cmp::min(bufs.len(), c::DWORD::max_value() as usize) as c::DWORD;
|
|
|
|
|
let mut nread = 0;
|
|
|
|
|
let mut flags = 0;
|
|
|
|
|
unsafe {
|
|
|
|
|
let ret = c::WSARecv(
|
|
|
|
|
self.0,
|
|
|
|
|
bufs.as_mut_ptr() as *mut c::WSABUF,
|
|
|
|
|
len,
|
|
|
|
|
&mut nread,
|
|
|
|
|
&mut flags,
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
);
|
|
|
|
|
match ret {
|
|
|
|
|
0 => Ok(nread as usize),
|
|
|
|
|
_ if c::WSAGetLastError() == c::WSAESHUTDOWN => Ok(0),
|
|
|
|
|
_ => Err(last_error()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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)> {
|
2017-01-10 19:11:56 -08:00
|
|
|
let mut storage: c::SOCKADDR_STORAGE_LH = unsafe { mem::zeroed() };
|
|
|
|
|
let mut addrlen = mem::size_of_val(&storage) as c::socklen_t;
|
|
|
|
|
let len = cmp::min(buf.len(), <wrlen_t>::max_value() as usize) as wrlen_t;
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
unsafe {
|
2019-12-22 17:42:04 -05:00
|
|
|
match c::recvfrom(
|
|
|
|
|
self.0,
|
|
|
|
|
buf.as_mut_ptr() as *mut c_void,
|
|
|
|
|
len,
|
|
|
|
|
flags,
|
|
|
|
|
&mut storage as *mut _ as *mut _,
|
|
|
|
|
&mut addrlen,
|
|
|
|
|
) {
|
2017-01-10 19:11:56 -08:00
|
|
|
-1 if c::WSAGetLastError() == c::WSAESHUTDOWN => {
|
|
|
|
|
Ok((0, net::sockaddr_to_addr(&storage, addrlen as usize)?))
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2017-01-10 19:11:56 -08:00
|
|
|
-1 => Err(last_error()),
|
|
|
|
|
n => Ok((n as usize, net::sockaddr_to_addr(&storage, addrlen as usize)?)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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> {
|
2019-02-08 20:42:34 +01:00
|
|
|
let len = cmp::min(bufs.len(), c::DWORD::max_value() as usize) as c::DWORD;
|
|
|
|
|
let mut nwritten = 0;
|
|
|
|
|
unsafe {
|
|
|
|
|
cvt(c::WSASend(
|
|
|
|
|
self.0,
|
|
|
|
|
bufs.as_ptr() as *const c::WSABUF as *mut c::WSABUF,
|
|
|
|
|
len,
|
|
|
|
|
&mut nwritten,
|
|
|
|
|
0,
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
ptr::null_mut(),
|
|
|
|
|
))?;
|
|
|
|
|
}
|
|
|
|
|
Ok(nwritten as usize)
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2019-12-22 17:42:04 -05:00
|
|
|
return Err(io::Error::new(
|
|
|
|
|
io::ErrorKind::InvalidInput,
|
|
|
|
|
"cannot set a 0 duration timeout",
|
|
|
|
|
));
|
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
|
|
|
|
2019-05-27 16:51:29 +02:00
|
|
|
#[cfg(not(target_vendor = "uwp"))]
|
2015-06-29 09:46:35 -07:00
|
|
|
fn set_no_inherit(&self) -> io::Result<()> {
|
2019-12-22 17:42:04 -05:00
|
|
|
sys::cvt(unsafe { c::SetHandleInformation(self.0 as c::HANDLE, c::HANDLE_FLAG_INHERIT, 0) })
|
2020-01-02 08:56:12 +00:00
|
|
|
.map(drop)
|
2015-06-29 09:46:35 -07:00
|
|
|
}
|
2015-11-02 16:23:22 -08:00
|
|
|
|
2019-05-27 16:51:29 +02:00
|
|
|
#[cfg(target_vendor = "uwp")]
|
|
|
|
|
fn set_no_inherit(&self) -> io::Result<()> {
|
|
|
|
|
Err(io::Error::new(io::ErrorKind::Other, "Unavailable on UWP"))
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
};
|
2016-03-22 22:01:37 -05:00
|
|
|
cvt(unsafe { c::shutdown(self.0, how) })?;
|
2015-11-02 16:23:22 -08:00
|
|
|
Ok(())
|
|
|
|
|
}
|
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;
|
|
|
|
|
let r = unsafe { c::ioctlsocket(self.0, c::FIONBIO as c_int, &mut nonblocking) };
|
2019-12-22 17:42:04 -05:00
|
|
|
if r == 0 { Ok(()) } else { Err(io::Error::last_os_error()) }
|
2016-02-27 14:15:19 -08:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-05 16:50:11 -08:00
|
|
|
impl Drop for Socket {
|
|
|
|
|
fn drop(&mut self) {
|
2015-11-02 16:23:22 -08:00
|
|
|
let _ = unsafe { c::closesocket(self.0) };
|
2015-02-05 16:50:11 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
impl AsInner<c::SOCKET> for Socket {
|
2019-12-22 17:42:04 -05:00
|
|
|
fn as_inner(&self) -> &c::SOCKET {
|
|
|
|
|
&self.0
|
|
|
|
|
}
|
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
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
impl FromInner<c::SOCKET> for Socket {
|
2019-12-22 17:42:04 -05:00
|
|
|
fn from_inner(sock: c::SOCKET) -> Socket {
|
|
|
|
|
Socket(sock)
|
|
|
|
|
}
|
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
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
impl IntoInner<c::SOCKET> for Socket {
|
|
|
|
|
fn into_inner(self) -> c::SOCKET {
|
2015-07-15 23:31:24 -07:00
|
|
|
let ret = self.0;
|
|
|
|
|
mem::forget(self);
|
|
|
|
|
ret
|
|
|
|
|
}
|
|
|
|
|
}
|