Skip to content

Commit

Permalink
Replace winapi with windows-sys
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh authored and ghedo committed Jan 10, 2025
1 parent b17904e commit d249d3f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 42 deletions.
2 changes: 1 addition & 1 deletion quiche/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ sfv = { version = "0.9", optional = true }
smallvec = { version = "1.10", features = ["serde", "union"] }

[target."cfg(windows)".dependencies]
winapi = { version = "0.3", features = ["wincrypt", "ws2def", "ws2ipdef", "ws2tcpip"] }
windows-sys = { version = "0.59", features = ["Win32_Networking_WinSock", "Win32_Security_Cryptography"] }

[dev-dependencies]
mio = { version = "0.8", features = ["net", "os-poll"] }
Expand Down
62 changes: 27 additions & 35 deletions quiche/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,54 +48,54 @@ use libc::timespec;
#[cfg(not(windows))]
use libc::AF_INET;
#[cfg(windows)]
use winapi::shared::ws2def::AF_INET;
use windows_sys::Win32::Networking::WinSock::AF_INET;

#[cfg(not(windows))]
use libc::AF_INET6;
#[cfg(windows)]
use winapi::shared::ws2def::AF_INET6;
use windows_sys::Win32::Networking::WinSock::AF_INET6;

#[cfg(not(windows))]
use libc::in_addr;
#[cfg(windows)]
use winapi::shared::inaddr::IN_ADDR as in_addr;
use windows_sys::Win32::Networking::WinSock::IN_ADDR as in_addr;

#[cfg(not(windows))]
use libc::in6_addr;
#[cfg(windows)]
use winapi::shared::in6addr::IN6_ADDR as in6_addr;
use windows_sys::Win32::Networking::WinSock::IN6_ADDR as in6_addr;

#[cfg(not(windows))]
use libc::sa_family_t;
#[cfg(windows)]
use winapi::shared::ws2def::ADDRESS_FAMILY as sa_family_t;
use windows_sys::Win32::Networking::WinSock::ADDRESS_FAMILY as sa_family_t;

#[cfg(not(windows))]
use libc::sockaddr_in;
#[cfg(windows)]
use winapi::shared::ws2def::SOCKADDR_IN as sockaddr_in;
use windows_sys::Win32::Networking::WinSock::SOCKADDR_IN as sockaddr_in;

#[cfg(not(windows))]
use libc::sockaddr_in6;
#[cfg(windows)]
use winapi::shared::ws2ipdef::SOCKADDR_IN6_LH as sockaddr_in6;
use windows_sys::Win32::Networking::WinSock::SOCKADDR_IN6 as sockaddr_in6;

#[cfg(not(windows))]
use libc::sockaddr_storage;
#[cfg(windows)]
use winapi::shared::ws2def::SOCKADDR_STORAGE_LH as sockaddr_storage;
use windows_sys::Win32::Networking::WinSock::SOCKADDR_STORAGE as sockaddr_storage;

#[cfg(windows)]
use libc::c_int as socklen_t;
#[cfg(not(windows))]
use libc::socklen_t;

#[cfg(windows)]
use winapi::shared::in6addr::in6_addr_u;
use windows_sys::Win32::Networking::WinSock::IN6_ADDR_0;
#[cfg(windows)]
use winapi::shared::inaddr::in_addr_S_un;
use windows_sys::Win32::Networking::WinSock::IN_ADDR_0;
#[cfg(windows)]
use winapi::shared::ws2ipdef::SOCKADDR_IN6_LH_u;
use windows_sys::Win32::Networking::WinSock::SOCKADDR_IN6_0;

use crate::*;

Expand Down Expand Up @@ -1840,7 +1840,7 @@ fn optional_std_addr_from_c(
}

fn std_addr_from_c(addr: &sockaddr, addr_len: socklen_t) -> SocketAddr {
match addr.sa_family as i32 {
match addr.sa_family {
AF_INET => {
assert!(addr_len as usize == std::mem::size_of::<sockaddr_in>());

Expand All @@ -1850,7 +1850,7 @@ fn std_addr_from_c(addr: &sockaddr, addr_len: socklen_t) -> SocketAddr {
let ip_addr = Ipv4Addr::from(u32::from_be(in4.sin_addr.s_addr));
#[cfg(windows)]
let ip_addr = {
let ip_bytes = unsafe { in4.sin_addr.S_un.S_un_b() };
let ip_bytes = unsafe { in4.sin_addr.S_un.S_un_b };

Ipv4Addr::from([
ip_bytes.s_b1,
Expand All @@ -1876,15 +1876,15 @@ fn std_addr_from_c(addr: &sockaddr, addr_len: socklen_t) -> SocketAddr {
#[cfg(not(windows))]
in6.sin6_addr.s6_addr,
#[cfg(windows)]
*unsafe { in6.sin6_addr.u.Byte() },
unsafe { in6.sin6_addr.u.Byte },
);

let port = u16::from_be(in6.sin6_port);

#[cfg(not(windows))]
let scope_id = in6.sin6_scope_id;
#[cfg(windows)]
let scope_id = unsafe { *in6.u.sin6_scope_id() };
let scope_id = unsafe { in6.Anonymous.sin6_scope_id };

let out =
SocketAddrV6::new(ip_addr, port, in6.sin6_flowinfo, scope_id);
Expand All @@ -1909,10 +1909,10 @@ fn std_addr_to_c(addr: &SocketAddr, out: &mut sockaddr_storage) -> socklen_t {
#[cfg(not(windows))]
let sin_addr = in_addr { s_addr };
#[cfg(windows)]
let sin_addr = {
let mut s_un = std::mem::zeroed::<in_addr_S_un>();
*s_un.S_addr_mut() = s_addr;
in_addr { S_un: s_un }
let sin_addr = in_addr {
S_un: IN_ADDR_0 {
S_addr: s_addr,
}
};

*out_in = sockaddr_in {
Expand Down Expand Up @@ -1948,17 +1948,10 @@ fn std_addr_to_c(addr: &SocketAddr, out: &mut sockaddr_storage) -> socklen_t {
s6_addr: addr.ip().octets(),
};
#[cfg(windows)]
let sin6_addr = {
let mut u = std::mem::zeroed::<in6_addr_u>();
*u.Byte_mut() = addr.ip().octets();
in6_addr { u }
};

#[cfg(windows)]
let u = {
let mut u = std::mem::zeroed::<SOCKADDR_IN6_LH_u>();
*u.sin6_scope_id_mut() = addr.scope_id();
u
let sin6_addr = in6_addr {
u: IN6_ADDR_0 {
Byte: addr.ip().octets()
},
};

*out_in6 = sockaddr_in6 {
Expand All @@ -1984,7 +1977,9 @@ fn std_addr_to_c(addr: &SocketAddr, out: &mut sockaddr_storage) -> socklen_t {
#[cfg(not(windows))]
sin6_scope_id: addr.scope_id(),
#[cfg(windows)]
u,
Anonymous: SOCKADDR_IN6_0 {
sin6_scope_id: addr.scope_id(),
},
};

sa_len as socklen_t
Expand Down Expand Up @@ -2014,12 +2009,9 @@ fn std_time_to_c(_time: &std::time::Instant, out: &mut timespec) {
mod tests {
use super::*;

#[cfg(not(windows))]
use libc::c_void;
#[cfg(windows)]
use winapi::ctypes::c_void;
#[cfg(windows)]
use winapi::um::ws2tcpip::inet_ntop;
use windows_sys::Win32::Networking::WinSock::inet_ntop;

#[test]
fn addr_v4() {
Expand Down
12 changes: 6 additions & 6 deletions quiche/src/tls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ impl Context {
fn load_ca_certs(&mut self) -> Result<()> {
unsafe {
let cstr = ffi::CString::new("Root").map_err(|_| Error::TlsFail)?;
let sys_store = winapi::um::wincrypt::CertOpenSystemStoreA(
let sys_store = windows_sys::Win32::Security::Cryptography::CertOpenSystemStoreA(
0,
cstr.as_ptr() as winapi::um::winnt::LPCSTR,
cstr.as_ptr() as windows_sys::core::PCSTR,
);
if sys_store.is_null() {
return Err(Error::TlsFail);
Expand All @@ -227,7 +227,7 @@ impl Context {
return Err(Error::TlsFail);
}

let mut ctx_p = winapi::um::wincrypt::CertEnumCertificatesInStore(
let mut ctx_p = windows_sys::Win32::Security::Cryptography::CertEnumCertificatesInStore(
sys_store,
ptr::null(),
);
Expand All @@ -246,14 +246,14 @@ impl Context {

X509_free(cert);

ctx_p = winapi::um::wincrypt::CertEnumCertificatesInStore(
ctx_p = windows_sys::Win32::Security::Cryptography::CertEnumCertificatesInStore(
sys_store, ctx_p,
);
}

// tidy up
winapi::um::wincrypt::CertFreeCertificateContext(ctx_p);
winapi::um::wincrypt::CertCloseStore(sys_store, 0);
windows_sys::Win32::Security::Cryptography::CertFreeCertificateContext(ctx_p);
windows_sys::Win32::Security::Cryptography::CertCloseStore(sys_store, 0);
}

Ok(())
Expand Down

0 comments on commit d249d3f

Please sign in to comment.