aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2023-08-27 18:42:41 +0200
committerHimbeerserverDE <himbeerserverde@gmail.com>2023-08-27 18:42:41 +0200
commit4dd764a24bc58556262b14c6df48637b4a05fb9c (patch)
treee033bf2ab29175afbec0d443f5a2a98ef2014e94
parent15175ff56fe5ea130b47c5d7d44e694a78091376 (diff)
get a new lease when pppoe reconnects0.1.0
-rw-r--r--Cargo.lock40
-rw-r--r--Cargo.toml1
-rw-r--r--src/error.rs2
-rw-r--r--src/main.rs29
4 files changed, 65 insertions, 7 deletions
diff --git a/Cargo.lock b/Cargo.lock
index adc501c..0fefa8d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -62,6 +62,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
+name = "bitflags"
+version = "2.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635"
+
+[[package]]
name = "byteorder"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -327,7 +333,7 @@ version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8069d3ec154eb856955c1c0fbffefbf5f3c40a104ec912d4797314c1801abff"
dependencies = [
- "bitflags",
+ "bitflags 1.3.2",
"inotify-sys",
"libc",
]
@@ -369,7 +375,7 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed9625ffda8729b85e45cf04090035ac368927b8cebc34898e7c120f52e4838b"
dependencies = [
- "bitflags",
+ "bitflags 1.3.2",
"libc",
]
@@ -442,7 +448,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6de2fe935f44cbdfcab77dce2150d68eda75be715cd42d4d6f52b0bd4dcc5b1"
dependencies = [
"anyhow",
- "bitflags",
+ "bitflags 1.3.2",
"byteorder",
"libc",
"netlink-packet-core",
@@ -495,7 +501,7 @@ version = "0.26.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfdda3d196821d6af13126e40375cdf7da646a96114af134d5f417a9a1dc8e1a"
dependencies = [
- "bitflags",
+ "bitflags 1.3.2",
"cfg-if",
"libc",
"static_assertions",
@@ -507,7 +513,7 @@ version = "5.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "729f63e1ca555a43fe3efa4f3efdf4801c479da85b432242a7b726f353c88486"
dependencies = [
- "bitflags",
+ "bitflags 1.3.2",
"crossbeam-channel",
"filetime",
"fsevent-sys",
@@ -520,6 +526,25 @@ dependencies = [
]
[[package]]
+name = "notify"
+version = "6.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6205bd8bb1e454ad2e27422015fb5e4f2bcc7e08fa8f27058670d208324a4d2d"
+dependencies = [
+ "bitflags 2.4.0",
+ "crossbeam-channel",
+ "filetime",
+ "fsevent-sys",
+ "inotify",
+ "kqueue",
+ "libc",
+ "log",
+ "mio",
+ "walkdir",
+ "windows-sys 0.48.0",
+]
+
+[[package]]
name = "num_cpus"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -628,7 +653,7 @@ version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29"
dependencies = [
- "bitflags",
+ "bitflags 1.3.2",
]
[[package]]
@@ -637,6 +662,7 @@ version = "0.1.0"
dependencies = [
"dhcproto",
"libc",
+ "notify 6.1.1",
"rand",
"rsdsl_ip_config",
"rsdsl_netlinkd",
@@ -664,7 +690,7 @@ dependencies = [
"futures-util",
"ipnet",
"netlink-packet-route",
- "notify",
+ "notify 5.2.0",
"rsdsl_ip_config",
"rsdsl_pd_config",
"rtnetlink",
diff --git a/Cargo.toml b/Cargo.toml
index 9043d41..a65565b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
dhcproto = "0.9.0"
libc = "0.2.147"
+notify = "6.1.1"
rand = "0.8.5"
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" }
diff --git a/src/error.rs b/src/error.rs
index 5999027..6ade514 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -32,6 +32,8 @@ pub enum Error {
DhcprotoDecode(#[from] dhcproto::error::DecodeError),
#[error("dhcproto encode: {0}")]
DhcprotoEncode(#[from] dhcproto::error::EncodeError),
+ #[error("notify: {0}")]
+ Notify(#[from] notify::Error),
#[error("rsdsl_netlinkd: {0}")]
RsdslNetlinkd(#[from] rsdsl_netlinkd::error::Error),
#[error("serde_json: {0}")]
diff --git a/src/main.rs b/src/main.rs
index 4054785..6b50a0d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,6 +3,7 @@ use std::fs::File;
use std::mem::MaybeUninit;
use std::net::{Ipv6Addr, SocketAddr, SocketAddrV6};
use std::os::fd::AsRawFd;
+use std::path::Path;
use std::process;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
@@ -11,6 +12,8 @@ use std::time::{Duration, Instant};
use dhcproto::v6::{duid::Duid, DhcpOption, IAPrefix, Message, MessageType, OptionCode, IAPD, ORO};
use dhcproto::{Decodable, Decoder, Encodable, Encoder, Name};
+use notify::event::{CreateKind, ModifyKind};
+use notify::{Event, EventKind, RecursiveMode, Watcher};
use rsdsl_dhcp6::util::setsockopt;
use rsdsl_dhcp6::{Error, Result};
use rsdsl_ip_config::DsConfig;
@@ -44,6 +47,13 @@ fn main() -> Result<()> {
println!("wait for up ppp0");
link::wait_up("ppp0".into())?;
+ let ds_config = Path::new(rsdsl_ip_config::LOCATION);
+
+ println!("wait for pppoe");
+ while !ds_config.exists() {
+ thread::sleep(Duration::from_secs(8));
+ }
+
let mut file = File::open(rsdsl_ip_config::LOCATION)?;
let dsconfig: DsConfig = serde_json::from_reader(&mut file)?;
@@ -98,6 +108,20 @@ fn main() -> Result<()> {
thread::sleep(Duration::from_secs(3));
});
+ let state2 = state.clone();
+ 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 => restart(state2.clone()),
+ EventKind::Modify(kind) if matches!(kind, ModifyKind::Data(_)) => {
+ restart(state2.clone())
+ }
+ _ => {}
+ },
+ Err(e) => println!("watch error: {:?}", e),
+ })?;
+
+ watcher.watch(ds_config, RecursiveMode::NonRecursive)?;
+
loop {
let mut buf = [MaybeUninit::new(0); BUFSIZE];
let (n, remote) = sock.recv_from(&mut buf)?;
@@ -550,3 +574,8 @@ fn write_pdconfig(ia_prefix: &IAPrefix, dnss: &[Ipv6Addr], aftr: &Option<String>
Ok(())
}
+
+fn restart(state: Arc<Mutex<State>>) {
+ *state.lock().expect("state mutex is poisoned") = State::default();
+ println!("reinitialize reconnected pppoe");
+}