From 433f0e8d1fbd25693172f71e5a577bb9625b45b0 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 8 Apr 2015 15:43:37 -0700 Subject: [PATCH] std: Set overlap/noinherit flags on windows sockets This commit modifies the socket creation functions on windows to always specify the `WSA_FLAG_OVERLAPPED` and `WSA_FLAG_NO_HANDLE_INHERIT` flags by default. The overlapped flag enables IOCP APIs on Windows to be used with the socket at no cost, enabling better interoperation with external libraries. The no handle inherit flag mirrors the upcoming change to Unix to set CLOEXEC by default for all handles. Closes #24206 --- src/libstd/sys/windows/c.rs | 2 ++ src/libstd/sys/windows/net.rs | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/libstd/sys/windows/c.rs b/src/libstd/sys/windows/c.rs index 4804f650441..2d3c2582698 100644 --- a/src/libstd/sys/windows/c.rs +++ b/src/libstd/sys/windows/c.rs @@ -44,6 +44,8 @@ pub const WSA_WAIT_TIMEOUT: libc::DWORD = libc::consts::os::extra::WAIT_TIMEOUT; pub const WSA_WAIT_EVENT_0: libc::DWORD = libc::consts::os::extra::WAIT_OBJECT_0; pub const WSA_WAIT_FAILED: libc::DWORD = libc::consts::os::extra::WAIT_FAILED; pub const WSAESHUTDOWN: libc::c_int = 10058; +pub const WSA_FLAG_OVERLAPPED: libc::DWORD = 0x01; +pub const WSA_FLAG_NO_HANDLE_INHERIT: libc::DWORD = 0x80; pub const ERROR_NO_MORE_FILES: libc::DWORD = 18; pub const TOKEN_READ: libc::DWORD = 0x20008; diff --git a/src/libstd/sys/windows/net.rs b/src/libstd/sys/windows/net.rs index 12a8ef99d76..f47c785bd26 100644 --- a/src/libstd/sys/windows/net.rs +++ b/src/libstd/sys/windows/net.rs @@ -80,7 +80,11 @@ impl Socket { SocketAddr::V4(..) => libc::AF_INET, SocketAddr::V6(..) => libc::AF_INET6, }; - match unsafe { libc::socket(fam, ty, 0) } { + let socket = unsafe { + c::WSASocketW(fam, ty, 0, 0 as *mut _, 0, + c::WSA_FLAG_OVERLAPPED | c::WSA_FLAG_NO_HANDLE_INHERIT) + }; + match socket { INVALID_SOCKET => Err(last_error()), n => Ok(Socket(n)), } @@ -103,7 +107,9 @@ impl Socket { match c::WSASocketW(info.iAddressFamily, info.iSocketType, info.iProtocol, - &mut info, 0, 0) { + &mut info, 0, + c::WSA_FLAG_OVERLAPPED | + c::WSA_FLAG_NO_HANDLE_INHERIT) { INVALID_SOCKET => Err(last_error()), n => Ok(Socket(n)), }