Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Remove winapi dependency and upgrade windows-sys to 0.59 #5581

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 8 commits into from
Mar 5, 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
7 changes: 3 additions & 4 deletions 7 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 2 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ thiserror = "2.0"
thread_local = "1.1.8"
unicode_names2 = "1.3.0"
widestring = "1.1.0"
windows-sys = "0.52.0"
windows-sys = "0.59.0"
wasm-bindgen = "0.2.100"

# Lints
Expand Down
10 changes: 5 additions & 5 deletions 10 common/src/fileutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub mod windows {
let h = h?;
// reset stat?

let file_type = unsafe { GetFileType(h) };
let file_type = unsafe { GetFileType(h as _) };
if file_type == FILE_TYPE_UNKNOWN {
return Err(std::io::Error::last_os_error());
}
Expand All @@ -138,10 +138,10 @@ pub mod windows {
let mut basic_info: FILE_BASIC_INFO = unsafe { std::mem::zeroed() };
let mut id_info: FILE_ID_INFO = unsafe { std::mem::zeroed() };

if unsafe { GetFileInformationByHandle(h, &mut info) } == 0
if unsafe { GetFileInformationByHandle(h as _, &mut info) } == 0
|| unsafe {
GetFileInformationByHandleEx(
h,
h as _,
FileBasicInfo,
&mut basic_info as *mut _ as *mut _,
std::mem::size_of_val(&basic_info) as u32,
Expand All @@ -153,7 +153,7 @@ pub mod windows {

let p_id_info = if unsafe {
GetFileInformationByHandleEx(
h,
h as _,
FileIdInfo,
&mut id_info as *mut _ as *mut _,
std::mem::size_of_val(&id_info) as u32,
Expand Down Expand Up @@ -320,7 +320,7 @@ pub mod windows {
.get_or_init(|| {
let library_name = OsString::from("api-ms-win-core-file-l2-1-4").to_wide_with_nul();
let module = unsafe { LoadLibraryW(library_name.as_ptr()) };
if module == 0 {
if module.is_null() {
return None;
}
let name = CString::new("GetFileInformationByName").unwrap();
Expand Down
7 changes: 1 addition & 6 deletions 7 stdlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,6 @@ paste = { workspace = true }
schannel = { workspace = true }
widestring = { workspace = true }

[target.'cfg(windows)'.dependencies.winapi]
version = "0.3.9"
features = [
"winsock2", "ifdef", "netioapi", "ws2tcpip",
]

[target.'cfg(windows)'.dependencies.windows-sys]
workspace = true
features = [
Expand All @@ -131,6 +125,7 @@ features = [
"Win32_NetworkManagement_Ndis",
"Win32_Security_Cryptography",
"Win32_System_Environment",
"Win32_System_IO"
]

[target.'cfg(target_os = "macos")'.dependencies]
Expand Down
45 changes: 27 additions & 18 deletions 45 stdlib/src/overlapped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ mod _overlapped {
use windows_sys::Win32::{
Foundation::{
ERROR_IO_PENDING, ERROR_NETNAME_DELETED, ERROR_OPERATION_ABORTED, ERROR_PIPE_BUSY,
ERROR_PORT_UNREACHABLE, ERROR_SEM_TIMEOUT, INVALID_HANDLE_VALUE,
ERROR_PORT_UNREACHABLE, ERROR_SEM_TIMEOUT,
},
Networking::WinSock::{
SO_UPDATE_ACCEPT_CONTEXT, SO_UPDATE_CONNECT_CONTEXT, TF_REUSE_SOCKET,
},
System::Threading::INFINITE,
};
#[pyattr(once)]
fn INVALID_HANDLE_VALUE(_vm: &VirtualMachine) -> isize {
windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE as isize
}
#[pyattr]
const NULL: isize = 0;

Expand Down Expand Up @@ -126,7 +130,7 @@ mod _overlapped {

fn mark_as_completed(ov: &mut OVERLAPPED) {
ov.Internal = 0;
if ov.hEvent != 0 {
if !ov.hEvent.is_null() {
unsafe { windows_sys::Win32::System::Threading::SetEvent(ov.hEvent) };
}
}
Expand Down Expand Up @@ -164,7 +168,7 @@ mod _overlapped {

fn WSARecv_inner(
inner: &mut OverlappedInner,
handle: HANDLE,
handle: isize,
buf: &[u8],
mut flags: u32,
vm: &VirtualMachine,
Expand Down Expand Up @@ -209,7 +213,7 @@ mod _overlapped {
#[pymethod]
fn WSARecv(
zelf: &Py<Self>,
handle: HANDLE,
handle: isize,
size: u32,
flags: u32,
vm: &VirtualMachine,
Expand All @@ -224,9 +228,9 @@ mod _overlapped {

let buf = vec![0u8; std::cmp::max(size, 1) as usize];
let buf = vm.ctx.new_bytes(buf);
inner.handle = handle;
inner.handle = handle as _;

let r = Self::WSARecv_inner(&mut inner, handle, buf.as_bytes(), flags, vm);
let r = Self::WSARecv_inner(&mut inner, handle as _, buf.as_bytes(), flags, vm);
inner.data = OverlappedData::Read(buf);
r
}
Expand Down Expand Up @@ -256,17 +260,17 @@ mod _overlapped {
}

impl Constructor for Overlapped {
type Args = (HANDLE,);
type Args = (isize,);

fn py_new(cls: PyTypeRef, (mut event,): Self::Args, vm: &VirtualMachine) -> PyResult {
if event == INVALID_HANDLE_VALUE {
if event == INVALID_HANDLE_VALUE(vm) {
event = unsafe {
windows_sys::Win32::System::Threading::CreateEventA(
std::ptr::null(),
Foundation::TRUE,
Foundation::FALSE,
std::ptr::null(),
)
) as isize
};
if event == NULL {
return Err(errno_err(vm));
Expand All @@ -275,11 +279,11 @@ mod _overlapped {

let mut overlapped: OVERLAPPED = unsafe { std::mem::zeroed() };
if event != NULL {
overlapped.hEvent = event;
overlapped.hEvent = event as _;
}
let inner = OverlappedInner {
overlapped,
handle: NULL,
handle: NULL as _,
error: 0,
data: OverlappedData::None,
};
Expand All @@ -292,29 +296,34 @@ mod _overlapped {

#[pyfunction]
fn CreateIoCompletionPort(
handle: HANDLE,
port: HANDLE,
handle: isize,
port: isize,
key: usize,
concurrency: u32,
vm: &VirtualMachine,
) -> PyResult<HANDLE> {
) -> PyResult<isize> {
let r = unsafe {
windows_sys::Win32::System::IO::CreateIoCompletionPort(handle, port, key, concurrency)
windows_sys::Win32::System::IO::CreateIoCompletionPort(
handle as _,
port as _,
key,
concurrency,
) as isize
};
if r == 0 {
if r as usize == 0 {
return Err(errno_err(vm));
}
Ok(r)
}

#[pyfunction]
fn GetQueuedCompletionStatus(port: HANDLE, msecs: u32, vm: &VirtualMachine) -> PyResult {
fn GetQueuedCompletionStatus(port: isize, msecs: u32, vm: &VirtualMachine) -> PyResult {
let mut bytes_transferred = 0;
let mut completion_key = 0;
let mut overlapped: *mut OVERLAPPED = std::ptr::null_mut();
let ret = unsafe {
windows_sys::Win32::System::IO::GetQueuedCompletionStatus(
port,
port as _,
&mut bytes_transferred,
&mut completion_key,
&mut overlapped,
Expand Down
57 changes: 30 additions & 27 deletions 57 stdlib/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,11 @@ mod _socket {
use libc as c;
#[cfg(windows)]
mod c {
pub use winapi::shared::netioapi::{if_indextoname, if_nametoindex};
pub use winapi::shared::ws2def::{
pub use windows_sys::Win32::NetworkManagement::IpHelper::{if_indextoname, if_nametoindex};
pub use windows_sys::Win32::Networking::WinSock::{
INADDR_ANY, INADDR_BROADCAST, INADDR_LOOPBACK, INADDR_NONE,
};
pub use winapi::um::winsock2::{
SO_EXCLUSIVEADDRUSE, getprotobyname, getservbyname, getservbyport, getsockopt,
setsockopt,
};
pub use winapi::um::ws2tcpip::{
EAI_AGAIN, EAI_BADFLAGS, EAI_FAIL, EAI_FAMILY, EAI_MEMORY, EAI_NODATA, EAI_NONAME,
EAI_SERVICE, EAI_SOCKTYPE,
};

pub use windows_sys::Win32::Networking::WinSock::{
AF_APPLETALK, AF_DECnet, AF_IPX, AF_LINK, AI_ADDRCONFIG, AI_ALL, AI_CANONNAME,
AI_NUMERICSERV, AI_V4MAPPED, IP_ADD_MEMBERSHIP, IP_DROP_MEMBERSHIP, IP_HDRINCL,
Expand All @@ -68,6 +61,17 @@ mod _socket {
SOL_SOCKET, SOMAXCONN, TCP_NODELAY, WSAEBADF, WSAECONNRESET, WSAENOTSOCK,
WSAEWOULDBLOCK,
};
pub use windows_sys::Win32::Networking::WinSock::{
SO_REUSEADDR as SO_EXCLUSIVEADDRUSE, getprotobyname, getservbyname, getservbyport,
getsockopt, setsockopt,
};
pub use windows_sys::Win32::Networking::WinSock::{
WSA_NOT_ENOUGH_MEMORY as EAI_MEMORY, WSAEAFNOSUPPORT as EAI_FAMILY,
WSAEINVAL as EAI_BADFLAGS, WSAESOCKTNOSUPPORT as EAI_SOCKTYPE,
WSAHOST_NOT_FOUND as EAI_NODATA, WSAHOST_NOT_FOUND as EAI_NONAME,
WSANO_RECOVERY as EAI_FAIL, WSATRY_AGAIN as EAI_AGAIN,
WSATYPE_NOT_FOUND as EAI_SERVICE,
};
pub const IF_NAMESIZE: usize =
windows_sys::Win32::NetworkManagement::Ndis::IF_MAX_STRING_SIZE as _;
pub const AF_UNSPEC: i32 = windows_sys::Win32::Networking::WinSock::AF_UNSPEC as _;
Expand Down Expand Up @@ -755,7 +759,7 @@ mod _socket {
}

#[cfg(windows)]
use winapi::shared::netioapi;
use windows_sys::Win32::NetworkManagement::IpHelper;

fn get_raw_sock(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<RawSocket> {
#[cfg(unix)]
Expand Down Expand Up @@ -1755,7 +1759,7 @@ mod _socket {
.map(|s| s.to_cstring(vm))
.transpose()?;
let cstr_proto = cstr_opt_as_ptr(&cstr_proto);
let serv = unsafe { c::getservbyname(cstr_name.as_ptr(), cstr_proto) };
let serv = unsafe { c::getservbyname(cstr_name.as_ptr() as _, cstr_proto as _) };
if serv.is_null() {
return Err(vm.new_os_error("service/proto not found".to_owned()));
}
Expand All @@ -1777,11 +1781,11 @@ mod _socket {
.map(|s| s.to_cstring(vm))
.transpose()?;
let cstr_proto = cstr_opt_as_ptr(&cstr_proto);
let serv = unsafe { c::getservbyport(port.to_be() as _, cstr_proto) };
let serv = unsafe { c::getservbyport(port.to_be() as _, cstr_proto as _) };
if serv.is_null() {
return Err(vm.new_os_error("port/proto not found".to_owned()));
}
let s = unsafe { ffi::CStr::from_ptr((*serv).s_name) };
let s = unsafe { ffi::CStr::from_ptr((*serv).s_name as _) };
Ok(s.to_string_lossy().into_owned())
}

Expand Down Expand Up @@ -2033,7 +2037,7 @@ mod _socket {
#[pyfunction]
fn getprotobyname(name: PyStrRef, vm: &VirtualMachine) -> PyResult {
let cstr = name.to_cstring(vm)?;
let proto = unsafe { c::getprotobyname(cstr.as_ptr()) };
let proto = unsafe { c::getprotobyname(cstr.as_ptr() as _) };
if proto.is_null() {
return Err(vm.new_os_error("protocol not found".to_owned()));
}
Expand Down Expand Up @@ -2111,14 +2115,14 @@ mod _socket {
#[cfg(all(unix, not(target_os = "redox")))]
type IfIndex = c::c_uint;
#[cfg(windows)]
type IfIndex = winapi::shared::ifdef::NET_IFINDEX;
type IfIndex = u32; // NET_IFINDEX but windows-sys 0.59 doesn't have it

#[cfg(not(target_os = "redox"))]
#[pyfunction]
fn if_nametoindex(name: FsPath, vm: &VirtualMachine) -> PyResult<IfIndex> {
let name = name.to_cstring(vm)?;

let ret = unsafe { c::if_nametoindex(name.as_ptr()) };
let ret = unsafe { c::if_nametoindex(name.as_ptr() as _) };
if ret == 0 {
Err(vm.new_os_error("no interface with this name".to_owned()))
} else {
Expand All @@ -2134,7 +2138,7 @@ mod _socket {
if ret.is_null() {
Err(crate::vm::stdlib::os::errno_err(vm))
} else {
let buf = unsafe { ffi::CStr::from_ptr(buf.as_ptr()) };
let buf = unsafe { ffi::CStr::from_ptr(buf.as_ptr() as _) };
Ok(buf.to_string_lossy().into_owned())
}
}
Expand Down Expand Up @@ -2170,6 +2174,7 @@ mod _socket {
#[cfg(windows)]
{
use std::ptr;
use windows_sys::Win32::NetworkManagement::Ndis::NET_LUID_LH;

let table = MibTable::get_raw().map_err(|err| err.into_pyexception(vm))?;
let list = table.as_slice().iter().map(|entry| {
Expand All @@ -2181,12 +2186,10 @@ mod _socket {
let list = list.collect::<PyResult<_>>()?;
return Ok(list);

fn get_name(
luid: &winapi::shared::ifdef::NET_LUID,
) -> io::Result<widestring::WideCString> {
fn get_name(luid: &NET_LUID_LH) -> io::Result<widestring::WideCString> {
let mut buf = [0; c::IF_NAMESIZE + 1];
let ret = unsafe {
netioapi::ConvertInterfaceLuidToNameW(luid, buf.as_mut_ptr(), buf.len())
IpHelper::ConvertInterfaceLuidToNameW(luid, buf.as_mut_ptr(), buf.len())
};
if ret == 0 {
Ok(widestring::WideCString::from_ustr_truncate(
Expand All @@ -2197,12 +2200,12 @@ mod _socket {
}
}
struct MibTable {
ptr: ptr::NonNull<netioapi::MIB_IF_TABLE2>,
ptr: ptr::NonNull<IpHelper::MIB_IF_TABLE2>,
}
impl MibTable {
fn get_raw() -> io::Result<Self> {
let mut ptr = ptr::null_mut();
let ret = unsafe { netioapi::GetIfTable2Ex(netioapi::MibIfTableRaw, &mut ptr) };
let ret = unsafe { IpHelper::GetIfTable2Ex(IpHelper::MibIfTableRaw, &mut ptr) };
if ret == 0 {
let ptr = unsafe { ptr::NonNull::new_unchecked(ptr) };
Ok(Self { ptr })
Expand All @@ -2212,17 +2215,17 @@ mod _socket {
}
}
impl MibTable {
fn as_slice(&self) -> &[netioapi::MIB_IF_ROW2] {
fn as_slice(&self) -> &[IpHelper::MIB_IF_ROW2] {
unsafe {
let p = self.ptr.as_ptr();
let ptr = &raw const (*p).Table as *const netioapi::MIB_IF_ROW2;
let ptr = &raw const (*p).Table as *const IpHelper::MIB_IF_ROW2;
std::slice::from_raw_parts(ptr, (*p).NumEntries as usize)
}
}
}
impl Drop for MibTable {
fn drop(&mut self) {
unsafe { netioapi::FreeMibTable(self.ptr.as_ptr() as *mut _) }
unsafe { IpHelper::FreeMibTable(self.ptr.as_ptr() as *mut _) };
}
}
}
Expand Down
Loading
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.