From 46a66771c0e53607e4c4936487c43b1c538b3cce Mon Sep 17 00:00:00 2001 From: ssrlive <30760636+ssrlive@users.noreply.github.com> Date: Wed, 6 Dec 2023 21:03:26 +0800 Subject: [PATCH] add apply_settings switch for Linux configutation --- README.md | 1 + examples/ping-tun.rs | 1 + examples/read-async.rs | 1 + examples/read.rs | 1 + src/platform/linux/device.rs | 4 +++- src/platform/linux/mod.rs | 18 +++++++++++++++++- 6 files changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7c468188..834d2f15 100644 --- a/README.md +++ b/README.md @@ -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(); diff --git a/examples/ping-tun.rs b/examples/ping-tun.rs index ce914941..1d56502e 100644 --- a/examples/ping-tun.rs +++ b/examples/ping-tun.rs @@ -29,6 +29,7 @@ async fn main() -> Result<(), Box> { #[cfg(target_os = "linux")] config.platform(|config| { config.packet_information(true); + config.apply_settings(true); }); #[cfg(target_os = "windows")] diff --git a/examples/read-async.rs b/examples/read-async.rs index 9fe9def6..a026ef31 100644 --- a/examples/read-async.rs +++ b/examples/read-async.rs @@ -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(); diff --git a/examples/read.rs b/examples/read.rs index 5beda850..c534ba6f 100644 --- a/examples/read.rs +++ b/examples/read.rs @@ -26,6 +26,7 @@ fn main() -> Result<(), Box> { #[cfg(target_os = "linux")] config.platform(|config| { config.packet_information(true); + config.apply_settings(true); }); let mut dev = tun::create(&config)?; diff --git a/src/platform/linux/device.rs b/src/platform/linux/device.rs index 41ce0958..1655a5be 100644 --- a/src/platform/linux/device.rs +++ b/src/platform/linux/device.rs @@ -108,7 +108,9 @@ impl Device { Device { name, queues, ctl } }; - device.configure(config)?; + if config.platform.apply_settings { + device.configure(config)?; + } Ok(device) } diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index 455a2791..20003113 100644 --- a/src/platform/linux/mod.rs +++ b/src/platform/linux/mod.rs @@ -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 { @@ -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.