Skip to content

Commit

Permalink
chore!: refine windows platform configuration (meh#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
cavivie authored Jun 13, 2024
1 parent f6cab50 commit 8ec27d1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/ping-tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async fn main_entry(mut quit: Receiver<()>) -> Result<(), BoxError> {

#[cfg(target_os = "windows")]
config.platform_config(|config| {
config.device_guid(Some(9099482345783245345345_u128));
config.device_guid(9099482345783245345345_u128);
});

let dev = tun2::create_as_async(&config)?;
Expand Down
2 changes: 1 addition & 1 deletion examples/read-async-codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async fn main_entry(mut quit: Receiver<()>) -> Result<(), BoxError> {

#[cfg(target_os = "windows")]
config.platform_config(|config| {
config.device_guid(Some(9099482345783245345345_u128));
config.device_guid(9099482345783245345345_u128);
});

let dev = tun2::create_as_async(&config)?;
Expand Down
5 changes: 1 addition & 4 deletions src/platform/windows/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ pub struct Device {
impl Device {
/// Create a new `Device` for the given `Configuration`.
pub fn new(config: &Configuration) -> Result<Self> {
let wintun_path = match &config.platform_config.wintun_path {
Some(path) => path.clone(),
None => "wintun.dll".to_string(),
};
let wintun_path = &config.platform_config.wintun_path;
let wintun = unsafe {
let wintun = libloading::Library::new(wintun_path)?;
wintun::load_from_library(wintun)?
Expand Down
21 changes: 15 additions & 6 deletions src/platform/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,32 @@ use crate::configuration::Configuration;
use crate::error::Result;

/// Windows-only interface configuration.
#[derive(Clone, Default, Debug)]
#[derive(Clone, Debug)]
pub struct PlatformConfig {
pub(crate) device_guid: Option<u128>,
pub(crate) wintun_path: Option<String>,
pub(crate) wintun_path: String,
}

impl Default for PlatformConfig {
fn default() -> Self {
Self {
device_guid: None,
wintun_path: "wintun".to_string(),
}
}
}

impl PlatformConfig {
pub fn device_guid(&mut self, device_guid: Option<u128>) {
pub fn device_guid(&mut self, device_guid: u128) {
log::trace!("Windows configuration device GUID");
self.device_guid = device_guid;
self.device_guid = Some(device_guid);
}

/// Use a custom path to the wintun.dll instead of looking in the working directory.
/// Security note: It is up to the caller to ensure that the library can be safely loaded from
/// the indicated path.
pub fn custom_wintun_path(&mut self, wintun_path: Option<String>) {
self.wintun_path = wintun_path;
pub fn custom_wintun_path(&mut self, wintun_path: &str) {
self.wintun_path = wintun_path.to_string();
}
}

Expand Down

0 comments on commit 8ec27d1

Please sign in to comment.