diff options
author | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-08-14 10:48:08 +0200 |
---|---|---|
committer | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-08-14 10:48:08 +0200 |
commit | 22f450b85b88efb7a6fe2dd7e2a8572aeb94867b (patch) | |
tree | 7244ef26f0d900f2d12c764c026d0c55b5e1d048 | |
parent | 8d73b76d0bf8fcf396c79267a99a70c1672b2130 (diff) |
reconfigure tunnel if dhcp6 reconnects
-rw-r--r-- | Cargo.lock | 21 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/error.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 46 |
4 files changed, 58 insertions, 12 deletions
@@ -649,6 +649,24 @@ dependencies = [ ] [[package]] +name = "notify" +version = "6.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5738a2795d57ea20abec2d6d76c6081186709c0024187cd5977265eda6598b51" +dependencies = [ + "bitflags 1.3.2", + "crossbeam-channel", + "filetime", + "fsevent-sys", + "inotify", + "kqueue", + "libc", + "mio", + "walkdir", + "windows-sys 0.45.0", +] + +[[package]] name = "num_cpus" version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -849,6 +867,7 @@ name = "rsdsl_dslite" version = "0.1.0" dependencies = [ "ipnet", + "notify 6.0.1", "rsdsl_ip_config", "rsdsl_netlinkd", "rsdsl_netlinkd_sys", @@ -875,7 +894,7 @@ dependencies = [ "futures-util", "ipnet", "netlink-packet-route", - "notify", + "notify 5.2.0", "rsdsl_ip_config", "rsdsl_pd_config", "rtnetlink", @@ -7,6 +7,7 @@ edition = "2021" [dependencies] ipnet = "2.8.0" +notify = "6.0.1" rsdsl_ip_config = { git = "https://github.com/rsdsl/ip_config.git", version = "0.2.2" } rsdsl_netlinkd = { git = "https://github.com/rsdsl/netlinkd.git", version = "0.6.2" } rsdsl_netlinkd_sys = { git = "https://github.com/rsdsl/netlinkd.git", version = "0.4.9" } diff --git a/src/error.rs b/src/error.rs index 5264e41..03145e1 100644 --- a/src/error.rs +++ b/src/error.rs @@ -14,6 +14,8 @@ pub enum Error { #[error("ipnet prefix len: {0}")] IpnetPrefixLen(#[from] ipnet::PrefixLenError), + #[error("notify: {0}")] + Notify(#[from] notify::Error), #[error("rsdsl_netlinkd: {0}")] RsdslNetlinkd(#[from] rsdsl_netlinkd::error::Error), #[error("rsdsl_netlinkd_sys: {0}")] diff --git a/src/main.rs b/src/main.rs index aaeede0..bce8ce8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,8 @@ use std::thread; use std::time::Duration; use ipnet::Ipv6Net; +use notify::event::{CreateKind, ModifyKind}; +use notify::{Event, EventKind, RecursiveMode, Watcher}; use rsdsl_dslite::{Error, Result}; use rsdsl_ip_config::DsConfig; use rsdsl_netlinkd::{addr, link, route}; @@ -29,19 +31,41 @@ fn main() -> Result<()> { thread::sleep(Duration::from_secs(8)); } - let mut file = File::open(rsdsl_pd_config::LOCATION)?; - let pdconfig: PdConfig = serde_json::from_reader(&mut file)?; + let mut tnl = None; - let _tnl; - if let Some(ref aftr) = pdconfig.aftr { - let local = local_address(&pdconfig)?; - let remote = multitry_resolve6(&pdconfig, aftr)?; - _tnl = IpIp6::new("dslite0", "ppp0", local, remote)?; + let do_setup = |tnl: &mut Option<IpIp6>| -> Result<()> { + let mut file = File::open(rsdsl_pd_config::LOCATION)?; + let pdconfig: PdConfig = serde_json::from_reader(&mut file)?; - configure_dslite(); - } else { - println!("no aftr"); - } + if let Some(ref aftr) = pdconfig.aftr { + let local = local_address(&pdconfig)?; + let remote = multitry_resolve6(&pdconfig, aftr)?; + *tnl = Some(IpIp6::new("dslite0", "ppp0", local, remote)?); + + configure_dslite(); + } else { + println!("no aftr"); + } + + Ok(()) + }; + let setup = move |tnl: &mut Option<IpIp6>| match do_setup(tnl) { + Ok(_) => {} + Err(e) => println!("can't create dslite0: {}", e), + }; + + setup(&mut tnl); + + let mut watcher = notify::recommended_watcher(move |res: notify::Result<Event>| match res { + Ok(event) => match event.kind { + EventKind::Create(kind) if kind == CreateKind::File => setup(&mut tnl), + EventKind::Modify(kind) if matches!(kind, ModifyKind::Data(_)) => setup(&mut tnl), + _ => {} + }, + Err(e) => println!("watch error: {:?}", e), + })?; + + watcher.watch(pd_config, RecursiveMode::NonRecursive)?; loop { thread::sleep(Duration::MAX); |