Skip to content

Commit

Permalink
add apply_settings switch for Linux configutation
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Dec 6, 2023
1 parent 11d136f commit 46a6677
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fn main() {
#[cfg(target_os = "linux")]
config.platform(|config| {
config.packet_information(true);
config.apply_settings(true);
});

let mut dev = tun::create(&config).unwrap();
Expand Down
1 change: 1 addition & 0 deletions examples/ping-tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(target_os = "linux")]
config.platform(|config| {
config.packet_information(true);
config.apply_settings(true);
});

#[cfg(target_os = "windows")]
Expand Down
1 change: 1 addition & 0 deletions examples/read-async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ async fn main() {
#[cfg(target_os = "linux")]
config.platform(|config| {
config.packet_information(true);
config.apply_settings(true);
});

let dev = tun::create_as_async(&config).unwrap();
Expand Down
1 change: 1 addition & 0 deletions examples/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(target_os = "linux")]
config.platform(|config| {
config.packet_information(true);
config.apply_settings(true);
});

let mut dev = tun::create(&config)?;
Expand Down
4 changes: 3 additions & 1 deletion src/platform/linux/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ impl Device {
Device { name, queues, ctl }
};

device.configure(config)?;
if config.platform.apply_settings {
device.configure(config)?;
}

Ok(device)
}
Expand Down
18 changes: 17 additions & 1 deletion src/platform/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,19 @@ use crate::configuration::Configuration as C;
use crate::error::*;

/// Linux-only interface configuration.
#[derive(Copy, Clone, Default, Debug)]
#[derive(Copy, Clone, Debug)]
pub struct Configuration {
pub(crate) packet_information: bool,
pub(crate) apply_settings: bool,
}

impl Default for Configuration {
fn default() -> Self {
Configuration {
packet_information: false,
apply_settings: true,
}
}
}

impl Configuration {
Expand All @@ -35,6 +45,12 @@ impl Configuration {
self.packet_information = value;
self
}

/// Enable or disable to assign IP/netmask/destination etc.
pub fn apply_settings(&mut self, value: bool) -> &mut Self {
self.apply_settings = value;
self
}
}

/// Create a TUN device with the given name.
Expand Down

0 comments on commit 46a6677

Please sign in to comment.