Skip to content
This repository has been archived by the owner on Oct 27, 2024. It is now read-only.

Commit

Permalink
Add support for dynamic Wintun library loading and update dependencies (
Browse files Browse the repository at this point in the history
  • Loading branch information
mokhtarabadi authored May 26, 2024
1 parent d446317 commit 078a52d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ target/
**/*.rs.bk
Cargo.lock
wintun.dll
.history
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ nix = { version = "0.29", features = ["ioctl"] }

[target.'cfg(target_os = "windows")'.dependencies]
wintun = { version = "0.4", features = ["panic_on_unsent_packets"] }
libloading = "0.8.3"

[target.'cfg(any(target_os = "macos", target_os = "freebsd"))'.dependencies]
ipnet = "2"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,5 @@ pub extern "C" fn start_tun(fd: std::os::raw::c_int) {
Windows
-----
You need to copy the [wintun.dll](https://wintun.net/) file which matches your architecture to
the same directory as your executable and run your program as administrator.
the same directory as your executable or can set `WINTUN_LIBARAY_PATH` to the dll file and run your program as administrator.

4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ pub enum Error {
#[cfg(target_os = "windows")]
#[error(transparent)]
WintunError(#[from] wintun::Error),

#[cfg(target_os = "windows")]
#[error(transparent)]
LibloadingError(#[from] libloading::Error),
}

impl From<Error> for std::io::Error {
Expand Down
7 changes: 6 additions & 1 deletion src/platform/windows/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ pub struct Device {
impl Device {
/// Create a new `Device` for the given `Configuration`.
pub fn new(config: &Configuration) -> Result<Self> {
let wintun = unsafe { wintun::load()? };
let wintun = unsafe {
let wintun_libray_path =
std::env::var("WINTUN_LIBARAY_PATH").unwrap_or("wintun.dll".to_string());
let wintun = libloading::Library::new(wintun_libray_path)?;
wintun::load_from_library(wintun)?
};
let tun_name = config.tun_name.as_deref().unwrap_or("wintun");
let guid = config.platform_config.device_guid;
let adapter = match wintun::Adapter::open(&wintun, tun_name) {
Expand Down

0 comments on commit 078a52d

Please sign in to comment.