more winapi to windows-sys

This commit is contained in:
Jeong, YunWon
2023-12-28 23:39:45 +09:00
parent 4729ca3af0
commit 756088e7fb
6 changed files with 65 additions and 28 deletions

1
Cargo.lock generated
View File

@@ -2213,6 +2213,7 @@ dependencies = [
"uuid",
"widestring",
"winapi",
"windows-sys 0.52.0",
"xml-rs",
]

View File

@@ -112,8 +112,16 @@ widestring = { workspace = true }
[target.'cfg(windows)'.dependencies.winapi]
version = "0.3.9"
features = [
"winsock2", "ws2def", "std", "wincrypt", "fileapi",
"impl-default", "vcruntime", "ifdef", "netioapi", "profileapi",
"winsock2", "ifdef", "netioapi",
]
[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.52.0"
features = [
"Win32_Networking_WinSock",
"Win32_NetworkManagement_IpHelper",
"Win32_NetworkManagement_Ndis",
"Win32_Security_Cryptography",
]
[target.'cfg(target_os = "macos")'.dependencies]

View File

@@ -4,11 +4,11 @@ pub(crate) use _multiprocessing::make_module;
#[pymodule]
mod _multiprocessing {
use crate::vm::{function::ArgBytesLike, stdlib::os, PyResult, VirtualMachine};
use winapi::um::winsock2::{self, SOCKET};
use windows_sys::Win32::Networking::WinSock::{self, SOCKET};
#[pyfunction]
fn closesocket(socket: usize, vm: &VirtualMachine) -> PyResult<()> {
let res = unsafe { winsock2::closesocket(socket as SOCKET) };
let res = unsafe { WinSock::closesocket(socket as SOCKET) };
if res == 0 {
Err(os::errno_err(vm))
} else {
@@ -20,7 +20,7 @@ mod _multiprocessing {
fn recv(socket: usize, size: usize, vm: &VirtualMachine) -> PyResult<libc::c_int> {
let mut buf = vec![0; size];
let nread =
unsafe { winsock2::recv(socket as SOCKET, buf.as_mut_ptr() as *mut _, size as i32, 0) };
unsafe { WinSock::recv(socket as SOCKET, buf.as_mut_ptr() as *mut _, size as i32, 0) };
if nread < 0 {
Err(os::errno_err(vm))
} else {
@@ -31,7 +31,7 @@ mod _multiprocessing {
#[pyfunction]
fn send(socket: usize, buf: ArgBytesLike, vm: &VirtualMachine) -> PyResult<libc::c_int> {
let ret = buf.with_ref(|b| unsafe {
winsock2::send(socket as SOCKET, b.as_ptr() as *const _, b.len() as i32, 0)
WinSock::send(socket as SOCKET, b.as_ptr() as *const _, b.len() as i32, 0)
});
if ret < 0 {
Err(os::errno_err(vm))

View File

@@ -30,8 +30,8 @@ mod platform {
#[allow(non_snake_case)]
#[cfg(windows)]
mod platform {
use winapi::um::winsock2;
pub use winsock2::{fd_set, select, timeval, FD_SETSIZE, SOCKET as RawFd};
use windows_sys::Win32::Networking::WinSock;
pub use WinSock::{select, FD_SET as fd_set, FD_SETSIZE, SOCKET as RawFd, TIMEVAL as timeval};
// based off winsock2.h: https://gist.github.com/piscisaureus/906386#file-winsock2-h-L128-L141
@@ -45,7 +45,7 @@ mod platform {
slot = slot.add(1);
}
// slot == &fd_array[fd_count] at this point
if fd_count < FD_SETSIZE as u32 {
if fd_count < FD_SETSIZE {
*slot = fd as RawFd;
(*set).fd_count += 1;
}
@@ -56,12 +56,12 @@ mod platform {
}
pub unsafe fn FD_ISSET(fd: RawFd, set: *mut fd_set) -> bool {
use winapi::um::winsock2::__WSAFDIsSet;
use WinSock::__WSAFDIsSet;
__WSAFDIsSet(fd as _, set) != 0
}
pub fn check_err(x: i32) -> bool {
x == winsock2::SOCKET_ERROR
x == WinSock::SOCKET_ERROR
}
}

View File

@@ -34,18 +34,46 @@ mod _socket {
use libc as c;
#[cfg(windows)]
mod c {
pub use winapi::shared::ifdef::IF_MAX_STRING_SIZE as IF_NAMESIZE;
pub use winapi::shared::mstcpip::*;
pub use winapi::shared::netioapi::{if_indextoname, if_nametoindex};
pub use winapi::shared::ws2def::*;
pub use winapi::shared::ws2ipdef::*;
pub use winapi::um::winsock2::{
IPPORT_RESERVED, SD_BOTH as SHUT_RDWR, SD_RECEIVE as SHUT_RD, SD_SEND as SHUT_WR,
SOCK_DGRAM, SOCK_RAW, SOCK_RDM, SOCK_SEQPACKET, SOCK_STREAM, SOL_SOCKET, SO_BROADCAST,
SO_ERROR, SO_EXCLUSIVEADDRUSE, SO_LINGER, SO_OOBINLINE, SO_REUSEADDR, SO_TYPE,
SO_USELOOPBACK, *,
pub use winapi::shared::ws2def::{
INADDR_ANY, INADDR_BROADCAST, INADDR_LOOPBACK, INADDR_NONE,
};
pub use winapi::um::ws2tcpip::*;
pub use winapi::um::winsock2::{
getprotobyname, getservbyname, getservbyport, getsockopt, setsockopt,
SO_EXCLUSIVEADDRUSE,
};
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_DECnet, AF_APPLETALK, AF_IPX, AF_LINK, AI_ADDRCONFIG, AI_ALL, AI_CANONNAME,
AI_NUMERICSERV, AI_V4MAPPED, IPPORT_RESERVED, IPPROTO_AH, IPPROTO_DSTOPTS, IPPROTO_EGP,
IPPROTO_ESP, IPPROTO_FRAGMENT, IPPROTO_GGP, IPPROTO_HOPOPTS, IPPROTO_ICMP,
IPPROTO_ICMPV6, IPPROTO_IDP, IPPROTO_IGMP, IPPROTO_IP, IPPROTO_IP as IPPROTO_IPIP,
IPPROTO_IPV4, IPPROTO_IPV6, IPPROTO_ND, IPPROTO_NONE, IPPROTO_PIM, IPPROTO_PUP,
IPPROTO_RAW, IPPROTO_ROUTING, IPPROTO_TCP, IPPROTO_UDP, IPV6_CHECKSUM, IPV6_DONTFRAG,
IPV6_HOPLIMIT, IPV6_HOPOPTS, IPV6_JOIN_GROUP, IPV6_LEAVE_GROUP, IPV6_MULTICAST_HOPS,
IPV6_MULTICAST_IF, IPV6_MULTICAST_LOOP, IPV6_PKTINFO, IPV6_RECVRTHDR, IPV6_RECVTCLASS,
IPV6_RTHDR, IPV6_TCLASS, IPV6_UNICAST_HOPS, IPV6_V6ONLY, IP_ADD_MEMBERSHIP,
IP_DROP_MEMBERSHIP, IP_HDRINCL, IP_MULTICAST_IF, IP_MULTICAST_LOOP, IP_MULTICAST_TTL,
IP_OPTIONS, IP_RECVDSTADDR, IP_TOS, IP_TTL, MSG_BCAST, MSG_CTRUNC, MSG_DONTROUTE,
MSG_MCAST, MSG_OOB, MSG_PEEK, MSG_TRUNC, MSG_WAITALL, NI_DGRAM, NI_MAXHOST, NI_MAXSERV,
NI_NAMEREQD, NI_NOFQDN, NI_NUMERICHOST, NI_NUMERICSERV, RCVALL_IPLEVEL, RCVALL_OFF,
RCVALL_ON, RCVALL_SOCKETLEVELONLY, SD_BOTH as SHUT_RDWR, SD_RECEIVE as SHUT_RD,
SD_SEND as SHUT_WR, SIO_KEEPALIVE_VALS, SIO_LOOPBACK_FAST_PATH, SIO_RCVALL, SOCK_DGRAM,
SOCK_RAW, SOCK_RDM, SOCK_SEQPACKET, SOCK_STREAM, SOL_SOCKET, SOMAXCONN, SO_BROADCAST,
SO_ERROR, SO_LINGER, SO_OOBINLINE, SO_REUSEADDR, SO_TYPE, SO_USELOOPBACK, TCP_NODELAY,
WSAEBADF, WSAECONNRESET, WSAENOTSOCK, WSAEWOULDBLOCK,
};
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 _;
pub const AF_INET: i32 = windows_sys::Win32::Networking::WinSock::AF_INET as _;
pub const AF_INET6: i32 = windows_sys::Win32::Networking::WinSock::AF_INET6 as _;
pub const AI_PASSIVE: i32 = windows_sys::Win32::Networking::WinSock::AI_PASSIVE as _;
pub const AI_NUMERICHOST: i32 =
windows_sys::Win32::Networking::WinSock::AI_NUMERICHOST as _;
}
// constants
#[pyattr(name = "has_ipv6")]
@@ -658,7 +686,7 @@ mod _socket {
#[cfg(windows)]
#[pyattr]
use winapi::shared::ws2def::{
use windows_sys::Win32::Networking::WinSock::{
IPPROTO_CBT, IPPROTO_ICLFXBM, IPPROTO_IGP, IPPROTO_L2TP, IPPROTO_PGM, IPPROTO_RDP,
IPPROTO_SCTP, IPPROTO_ST,
};
@@ -2216,7 +2244,7 @@ mod _socket {
}
#[cfg(windows)]
{
winapi::um::winsock2::INVALID_SOCKET as RawSocket
windows_sys::Win32::Networking::WinSock::INVALID_SOCKET as RawSocket
}
};
@@ -2329,7 +2357,7 @@ mod _socket {
#[cfg(unix)]
use libc::close;
#[cfg(windows)]
use winapi::um::winsock2::closesocket as close;
use windows_sys::Win32::Networking::WinSock::closesocket as close;
let ret = unsafe { close(x as _) };
if ret < 0 {
let err = crate::common::os::errno();

View File

@@ -1453,7 +1453,7 @@ mod windows {
#[pyfunction]
fn enum_certificates(store_name: PyStrRef, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
use schannel::{cert_context::ValidUses, cert_store::CertStore, RawPointer};
use winapi::um::wincrypt;
use windows_sys::Win32::Security::Cryptography;
// TODO: check every store for it, not just 2 of them:
// https://github.com/python/cpython/blob/3.8/Modules/_ssl.c#L5603-L5610
@@ -1465,12 +1465,12 @@ mod windows {
let certs = stores.iter().flat_map(|s| s.certs()).map(|c| {
let cert = vm.ctx.new_bytes(c.to_der().to_owned());
let enc_type = unsafe {
let ptr = c.as_ptr() as wincrypt::PCCERT_CONTEXT;
let ptr = c.as_ptr() as *const Cryptography::CERT_CONTEXT;
(*ptr).dwCertEncodingType
};
let enc_type = match enc_type {
wincrypt::X509_ASN_ENCODING => vm.new_pyobj(ascii!("x509_asn")),
wincrypt::PKCS_7_ASN_ENCODING => vm.new_pyobj(ascii!("pkcs_7_asn")),
Cryptography::X509_ASN_ENCODING => vm.new_pyobj(ascii!("x509_asn")),
Cryptography::PKCS_7_ASN_ENCODING => vm.new_pyobj(ascii!("pkcs_7_asn")),
other => vm.new_pyobj(other),
};
let usage: PyObjectRef = match c.valid_uses()? {