| 2361 | #[cfg(windows)] |
| 2362 | #[pymethod] |
| 2363 | fn ioctl( |
| 2364 | &self, |
| 2365 | cmd: PyObjectRef, |
| 2366 | option: PyObjectRef, |
| 2367 | vm: &VirtualMachine, |
| 2368 | ) -> Result<u32, IoOrPyException> { |
| 2369 | use crate::vm::builtins::PyInt; |
| 2370 | use crate::vm::convert::TryFromObject; |
| 2371 | |
| 2372 | let sock = self.sock()?; |
| 2373 | let fd = sock_fileno(&sock); |
| 2374 | let mut recv: u32 = 0; |
| 2375 | |
| 2376 | // Convert cmd to u32, returning ValueError for invalid/negative values |
| 2377 | let cmd_int = cmd |
| 2378 | .downcast::<PyInt>() |
| 2379 | .map_err(|_| vm.new_type_error("an integer is required"))?; |
| 2380 | let cmd_val = cmd_int.as_bigint(); |
| 2381 | let cmd: u32 = cmd_val |
| 2382 | .to_u32() |
| 2383 | .ok_or_else(|| vm.new_value_error(format!("invalid ioctl command {}", cmd_val)))?; |
| 2384 | |
| 2385 | match cmd { |
| 2386 | c::SIO_RCVALL | c::SIO_LOOPBACK_FAST_PATH => { |
| 2387 | // Option must be an integer, not None |
| 2388 | if vm.is_none(&option) { |
| 2389 | return Err(vm |
| 2390 | .new_type_error("an integer is required (got type NoneType)") |
| 2391 | .into()); |
| 2392 | } |
| 2393 | let option_val: u32 = TryFromObject::try_from_object(vm, option)?; |
| 2394 | let ret = unsafe { |
| 2395 | c::WSAIoctl( |
| 2396 | fd as _, |
| 2397 | cmd, |
| 2398 | &option_val as *const u32 as *const _, |
| 2399 | core::mem::size_of::<u32>() as u32, |
| 2400 | core::ptr::null_mut(), |
| 2401 | 0, |
| 2402 | &mut recv, |
| 2403 | core::ptr::null_mut(), |
| 2404 | None, |
| 2405 | ) |
| 2406 | }; |
| 2407 | if ret == c::SOCKET_ERROR { |
| 2408 | return Err(Self::wsa_error().into()); |
| 2409 | } |
| 2410 | Ok(recv) |
| 2411 | } |
| 2412 | c::SIO_KEEPALIVE_VALS => { |
| 2413 | let tuple: PyTupleRef = option |
| 2414 | .downcast() |
| 2415 | .map_err(|_| vm.new_type_error("SIO_KEEPALIVE_VALS requires a tuple"))?; |
| 2416 | if tuple.len() != 3 { |
| 2417 | return Err(vm |
| 2418 | .new_type_error( |
| 2419 | "SIO_KEEPALIVE_VALS requires (onoff, keepalivetime, keepaliveinterval)", |
| 2420 | ) |