Skip to content

Add support for Windows Registered I/O #604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use windows_sys::Win32::Networking::WinSock::{
self, tcp_keepalive, FIONBIO, IN6_ADDR, IN6_ADDR_0, INVALID_SOCKET, IN_ADDR, IN_ADDR_0,
POLLERR, POLLHUP, POLLRDNORM, POLLWRNORM, SD_BOTH, SD_RECEIVE, SD_SEND, SIO_KEEPALIVE_VALS,
SOCKET_ERROR, WSABUF, WSAEMSGSIZE, WSAESHUTDOWN, WSAPOLLFD, WSAPROTOCOL_INFOW,
WSA_FLAG_NO_HANDLE_INHERIT, WSA_FLAG_OVERLAPPED,
WSA_FLAG_NO_HANDLE_INHERIT, WSA_FLAG_OVERLAPPED, WSA_FLAG_REGISTERED_IO,
};
#[cfg(feature = "all")]
use windows_sys::Win32::Networking::WinSock::{
Expand Down Expand Up @@ -125,13 +125,21 @@ impl Type {
/// Our custom flag to set `WSA_FLAG_NO_HANDLE_INHERIT` on socket creation.
/// Trying to mimic `Type::cloexec` on windows.
const NO_INHERIT: c_int = 1 << ((size_of::<c_int>() * 8) - 1); // Last bit.
/// Our custom flag to set `WSA_FLAG_REGISTERED_IO` on socket creation.
const REGISTERED_IO: c_int = 1 << ((size_of::<c_int>() * 8) - 2); // Second last bit.

/// Set `WSA_FLAG_NO_HANDLE_INHERIT` on the socket.
#[cfg(feature = "all")]
pub const fn no_inherit(self) -> Type {
self._no_inherit()
}

/// Set `WSA_FLAG_REGISTERED_IO` on the socket.
#[cfg(feature = "all")]
pub const fn registered_io(self) -> Type {
Type(self.0 | Type::REGISTERED_IO)
}

pub(crate) const fn _no_inherit(self) -> Type {
Type(self.0 | Type::NO_INHERIT)
}
Expand Down Expand Up @@ -252,13 +260,19 @@ pub(crate) fn socket_into_raw(socket: Socket) -> RawSocket {
pub(crate) fn socket(family: c_int, mut ty: c_int, protocol: c_int) -> io::Result<RawSocket> {
init();

// Check if we set our custom flag.
// Check if we set our custom flags.
let flags = if ty & Type::NO_INHERIT != 0 {
ty = ty & !Type::NO_INHERIT;
WSA_FLAG_NO_HANDLE_INHERIT
} else {
0
};
let flags = if ty & Type::REGISTERED_IO != 0 {
ty = ty & !Type::REGISTERED_IO;
flags | WSA_FLAG_REGISTERED_IO
} else {
flags
};

syscall!(
WSASocketW(
Expand Down
42 changes: 42 additions & 0 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,48 @@ where
);
}

#[cfg(all(feature = "all", windows))]
#[test]
fn type_registered_io() {
let ty = Type::DGRAM.registered_io();
let socket = Socket::new(Domain::IPV4, ty, None).unwrap();
assert_registered_io(&socket);
}

/// Assert that registered I/O is enabled on `socket`.
#[cfg(windows)]
#[track_caller]
pub fn assert_registered_io<S>(socket: &S)
where
S: AsRawSocket,
{
use std::ptr;
use windows_sys::core::GUID;
use windows_sys::Win32::Networking::WinSock;

let mut table = MaybeUninit::<WinSock::RIO_EXTENSION_FUNCTION_TABLE>::uninit();
let guid = WinSock::WSAID_MULTIPLE_RIO;
let mut bytes = 0;

let r = unsafe {
WinSock::WSAIoctl(
socket.as_raw_socket() as _,
WinSock::SIO_GET_MULTIPLE_EXTENSION_FUNCTION_POINTER,
(&guid as *const GUID) as *const _,
size_of_val(&guid) as u32,
table.as_mut_ptr() as *mut _,
size_of_val(&table) as u32,
(&mut bytes as *mut i32) as *mut _,
ptr::null_mut(),
None,
)
};
if r != 0 {
let err = io::Error::last_os_error();
panic!("unexpected error: {err}");
}
}

#[cfg(all(
feature = "all",
any(
Expand Down
Loading