Skip to content

Commit

Permalink
add ablility to set device GUID for wintun
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Dec 2, 2023
1 parent 9d6c749 commit d9072c4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/ping-tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

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

let dev = tun::create_as_async(&config)?;
Expand Down
3 changes: 1 addition & 2 deletions src/platform/windows/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ pub struct Device {

impl Device {
/// Create a new `Device` for the given `Configuration`.
pub fn new(config: &Configuration) -> Result<Self> {
pub fn new(config: &Configuration, guid: Option<u128>) -> Result<Self> {
let wintun = unsafe { wintun::load()? };
let tun_name = config.name.as_deref().unwrap_or("wintun");
let guid = Some(9099482345783245345345_u128);
let adapter = match wintun::Adapter::open(&wintun, tun_name) {
Ok(a) => a,
Err(_) => wintun::Adapter::create(&wintun, tun_name, tun_name, guid)?,
Expand Down
13 changes: 11 additions & 2 deletions src/platform/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,26 @@ pub use device::{Device, Queue};
use crate::configuration::Configuration as C;
use crate::error::*;

static DEVICE_GUID: std::sync::OnceLock<Option<u128>> = std::sync::OnceLock::new();

/// Windows-only interface configuration.
#[derive(Copy, Clone, Default, Debug)]
pub struct Configuration {}

impl Configuration {
pub fn initialize(&mut self) {
pub fn initialize(&mut self, device_guid: Option<u128>) {
log::trace!("Windows configuration initialize");
if let Err(err) = DEVICE_GUID.set(device_guid) {
log::error!("set device GUID error \"{:?}\"", err);
}
}
}

/// Create a TUN device with the given name.
pub fn create(configuration: &C) -> Result<Device> {
Device::new(configuration)
let guid = match DEVICE_GUID.get() {
Some(g) => *g,
None => None,
};
Device::new(configuration, guid)
}

0 comments on commit d9072c4

Please sign in to comment.