diff --git a/Cargo.lock b/Cargo.lock index c4bcfc5..bcec242 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -478,6 +478,8 @@ dependencies = [ name = "msquic-rs" version = "0.1.0" dependencies = [ + "lazy_static", + "libloading", "mssf-pal", "windows", "windows-core", diff --git a/crates/libs/msquic-rs/Cargo.toml b/crates/libs/msquic-rs/Cargo.toml index 5bd6995..2bf9131 100644 --- a/crates/libs/msquic-rs/Cargo.toml +++ b/crates/libs/msquic-rs/Cargo.toml @@ -11,6 +11,8 @@ edition = "2021" windows-core = "0.57.0" windows = { version = "0.57.0", features = [ "Win32_Foundation", "Win32_Networking_WinSock"]} mssf-pal = "0" +libloading = "0.8" +lazy_static = "1" [features] Foundation = [] diff --git a/crates/libs/msquic-rs/src/Microsoft.rs b/crates/libs/msquic-rs/src/Microsoft.rs index 1dbb51c..c446abc 100644 --- a/crates/libs/msquic-rs/src/Microsoft.rs +++ b/crates/libs/msquic-rs/src/Microsoft.rs @@ -157,22 +157,6 @@ pub mod MsQuic { } } } - #[inline] - pub unsafe fn MsQuicClose(quicapi: *const core::ffi::c_void) { - #[link(name = "msquic")] - extern "system" { - pub fn MsQuicClose(quicapi: *const core::ffi::c_void); - } - MsQuicClose(quicapi) - } - #[inline] - pub unsafe fn MsQuicOpenVersion(version: u32, quicapi: *mut *mut core::ffi::c_void) -> i32 { - #[link(name = "msquic")] - extern "system" { - pub fn MsQuicOpenVersion(version: u32, quicapi: *mut *mut core::ffi::c_void) -> i32; - } - MsQuicOpenVersion(version, quicapi) - } pub const QUIC_ALLOWED_CIPHER_SUITE_AES_128_GCM_SHA256: QUIC_ALLOWED_CIPHER_SUITE_FLAGS = QUIC_ALLOWED_CIPHER_SUITE_FLAGS(1i32); pub const QUIC_ALLOWED_CIPHER_SUITE_AES_256_GCM_SHA384: QUIC_ALLOWED_CIPHER_SUITE_FLAGS = diff --git a/crates/libs/msquic-rs/src/lib.rs b/crates/libs/msquic-rs/src/lib.rs index 3842331..6de36ab 100644 --- a/crates/libs/msquic-rs/src/lib.rs +++ b/crates/libs/msquic-rs/src/lib.rs @@ -25,3 +25,6 @@ pub use win_status::*; mod unix_status; #[cfg(target_os = "linux")] pub use unix_status::*; + +mod load; +pub use load::{MsQuicClose, MsQuicOpenVersion}; diff --git a/crates/libs/msquic-rs/src/load.rs b/crates/libs/msquic-rs/src/load.rs new file mode 100644 index 0000000..7f1ea0c --- /dev/null +++ b/crates/libs/msquic-rs/src/load.rs @@ -0,0 +1,15 @@ +//! Dynamically load shared lib +//! +//! + +use crate::Microsoft::MsQuic::QUIC_API_TABLE; + +const LIB_NAME: &str = "msquic.dll"; + +lazy_static::lazy_static! { + static ref LIB: libloading::Library = unsafe { libloading::Library::new(LIB_NAME) }.expect("cannot load msquic shared lib"); + pub static ref MsQuicOpenVersion: libloading::Symbol<'static,unsafe extern "C" fn(u32, *mut *mut QUIC_API_TABLE) -> i32> + = unsafe { LIB.get(b"MsQuicOpenVersion") }.expect("cannot load open fn"); + pub static ref MsQuicClose: libloading::Symbol<'static, unsafe extern "C" fn(*const QUIC_API_TABLE)> + = unsafe { LIB.get(b"MsQuicClose") }.expect("cannot load open fn"); +} diff --git a/crates/samples/simple/src/lib.rs b/crates/samples/simple/src/lib.rs index 68d9426..1ebca4f 100644 --- a/crates/samples/simple/src/lib.rs +++ b/crates/samples/simple/src/lib.rs @@ -1,18 +1,18 @@ -use std::{ffi::c_void, sync::Arc}; +use std::sync::Arc; use msquic_rs::{ Microsoft::MsQuic::{ - MsQuicClose, MsQuicOpenVersion, HQUIC, QUIC_API_TABLE, QUIC_API_VERSION_2, - QUIC_EXECUTION_PROFILE, QUIC_EXECUTION_PROFILE_LOW_LATENCY, - QUIC_EXECUTION_PROFILE_TYPE_MAX_THROUGHPUT, QUIC_EXECUTION_PROFILE_TYPE_REAL_TIME, - QUIC_EXECUTION_PROFILE_TYPE_SCAVENGER, QUIC_HANDLE, QUIC_REGISTRATION_CONFIG, + HQUIC, QUIC_API_TABLE, QUIC_API_VERSION_2, QUIC_EXECUTION_PROFILE, + QUIC_EXECUTION_PROFILE_LOW_LATENCY, QUIC_EXECUTION_PROFILE_TYPE_MAX_THROUGHPUT, + QUIC_EXECUTION_PROFILE_TYPE_REAL_TIME, QUIC_EXECUTION_PROFILE_TYPE_SCAVENGER, QUIC_HANDLE, + QUIC_REGISTRATION_CONFIG, }, - QStatus, + MsQuicClose, MsQuicOpenVersion, QStatus, }; use windows_core::PCSTR; struct ApiInner { - inner: *const QUIC_API_TABLE, + inner: *mut QUIC_API_TABLE, } unsafe impl Send for ApiInner {} @@ -21,17 +21,13 @@ unsafe impl Sync for ApiInner {} impl ApiInner { pub fn new() -> Self { let mut ret = ApiInner { - inner: std::ptr::null(), + inner: std::ptr::null_mut(), }; - let ec = unsafe { - MsQuicOpenVersion( - QUIC_API_VERSION_2, - std::ptr::addr_of_mut!(ret.inner) as *mut *mut c_void, - ) - }; + let ec = + unsafe { MsQuicOpenVersion(QUIC_API_VERSION_2, std::ptr::addr_of_mut!(ret.inner)) }; QStatus::from_raw(ec).unwrap(); - assert_ne!(ret.inner, std::ptr::null()); + assert_ne!(ret.inner, std::ptr::null_mut()); ret } @@ -45,8 +41,8 @@ impl Drop for ApiInner { if self.inner.is_null() { return; } - unsafe { MsQuicClose(self.inner as *const c_void) }; - self.inner = std::ptr::null(); + unsafe { MsQuicClose(self.inner) }; + self.inner = std::ptr::null_mut(); } } diff --git a/crates/tools/api/src/main.rs b/crates/tools/api/src/main.rs index ecffa74..0df7ae1 100644 --- a/crates/tools/api/src/main.rs +++ b/crates/tools/api/src/main.rs @@ -11,9 +11,11 @@ fn main() -> Result<()> { "--in", "./.windows/winmd/Microsoft.MsQuic.winmd", "--out", - "crates/libs/msquic-sys/src/Microsoft.rs", + "crates/libs/msquic-rs/src/Microsoft.rs", "--filter", "Microsoft", + "!Microsoft.MsQuic.MsQuicOpenVersion", // Skip functions that needs linking + "!Microsoft.MsQuic.MsQuicClose", "--config", "implement", ])?;