diff options
author | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-07-29 14:44:08 +0200 |
---|---|---|
committer | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-07-29 14:44:08 +0200 |
commit | 6178e15abe1d13899baaefa7c5252792dc2b01b8 (patch) | |
tree | c2f2e17054750bec550a31990c20efaabbdee7cb | |
parent | c97fe89d45d8873392609bf7293a0bdcde2b3511 (diff) |
high level termination logic
-rw-r--r-- | src/main.rs | 61 | ||||
-rw-r--r-- | src/state.rs | 2 |
2 files changed, 63 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs index ad5c94e..acc788e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ use socket2::Socket; const PPPOE_UPLINK: &str = "eth1"; const MAX_ATTEMPTS: usize = 10; +const MAX_STATUS_ATTEMPTS: usize = 2; static PPPOE_XMIT_INTERVAL: Duration = Duration::from_secs(3); static SESSION_INIT_GRACE_PERIOD: Duration = Duration::from_secs(1); @@ -359,6 +360,50 @@ fn session( } Ppp::Auth(_) => {} Ppp::Active => {} + Ppp::Terminate(ref reason, attempt) => { + if attempt >= MAX_ATTEMPTS { + *ppp_state = Ppp::Terminate2( + String::from_utf8(reason.clone()).unwrap_or(String::new()), + ); + continue; + } + + PppPkt::new_lcp(LcpPkt::new_terminate_request( + rand::random(), + reason.clone(), + )) + .serialize(&mut ctl_w)?; + ctl_w.flush()?; + + let reason_pretty = + String::from_utf8(reason.clone()).unwrap_or(format!("{:?}", reason)); + + println!( + " -> lcp terminate-request {}/{}, reason: {}", + attempt, MAX_STATUS_ATTEMPTS, reason_pretty + ); + + *ppp_state = Ppp::Terminate(reason.clone(), attempt + 1); + } + Ppp::Terminate2(ref reason) => { + PppoePkt::new_padt( + remote_mac, + local_mac, + session_id, + vec![PppoeVal::GenericError(reason.clone()).into()], + ) + .serialize(&mut sock_disc_w)?; + sock_disc_w.flush()?; + + println!( + " -> [{}] padt, session id: {}, reason: {}", + remote_mac, session_id, reason + ); + + *ppp_state = Ppp::Terminated; + *pppoe_state.lock().expect("pppoe state mutex is poisoned") = Pppoe::Init; + break; + } Ppp::Terminated => { break; } @@ -596,6 +641,22 @@ fn handle_lcp(lcp: LcpPkt, ctl_w: &mut BufWriter<File>, state: Arc<Mutex<Ppp>>) Ok(()) } + LcpData::TerminateAck(..) => { + let mut state = state.lock().expect("ppp state mutex is poisoned"); + match *state { + Ppp::Terminate(ref reason, ..) => { + *state = + Ppp::Terminate2(String::from_utf8(reason.clone()).unwrap_or(String::new())) + } + _ => { + println!(" <- unexpected lcp terminate-ack {}", lcp.identifier); + return Ok(()); + } + } + + println!(" <- lcp terminate-ack {}", lcp.identifier); + Ok(()) + } _ => Ok(()), } } diff --git a/src/state.rs b/src/state.rs index a88f2c9..8f41d9c 100644 --- a/src/state.rs +++ b/src/state.rs @@ -21,6 +21,8 @@ pub enum Ppp { SyncAcked(usize), Auth(Option<AuthProto>), Active, + Terminate(Vec<u8>, usize), + Terminate2(String), Terminated, Err, } |