diff options
author | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-03-23 20:36:03 +0100 |
---|---|---|
committer | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-03-23 20:36:03 +0100 |
commit | 139a11a1244c895b30b4f1afb09e336056fd072c (patch) | |
tree | e40153efa18c97aebc2b286e6d14f4e883acdda4 | |
parent | 7be6bcaa049ff13244dae93e3e32702768ecb101 (diff) |
prevent crash from terminated connections
-rw-r--r-- | src/main.rs | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs index ec1869c..934eb65 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,8 +46,7 @@ fn ppp2tun(rx: mpsc::Receiver<Vec<u8>>, tun: Arc<Iface>) -> Result<()> { let mut packet_info = [0; 4]; NE::write_u16(&mut packet_info[2..4], IPV4); - loop { - let mut buf = rx.recv()?; + while let Ok(mut buf) = rx.recv() { buf = prepend(buf, &packet_info); let n = tun.send(&buf)?; @@ -55,15 +54,17 @@ fn ppp2tun(rx: mpsc::Receiver<Vec<u8>>, tun: Arc<Iface>) -> Result<()> { return Err(Error::PartialTransmission); } } + + Ok(()) } fn write_config(rx: mpsc::Receiver<IpConfig>) -> Result<()> { - loop { - let ip_config = rx.recv()?; - + while let Ok(ip_config) = rx.recv() { let mut file = File::create(rsdsl_ip_config::LOCATION)?; serde_json::to_writer_pretty(&mut file, &ip_config)?; } + + Ok(()) } fn main() -> Result<()> { @@ -84,23 +85,21 @@ fn main() -> Result<()> { let tun2 = tun.clone(); let clt2 = clt.clone(); thread::spawn(move || match tun2ppp(clt2, tun2) { - Ok(_) => unreachable!(), + Ok(_) => {} Err(e) => panic!("tun2ppp error: {}", e), }); thread::spawn(move || match ppp2tun(rx, tun) { - Ok(_) => unreachable!(), + Ok(_) => {} Err(e) => panic!("ppp2tun error: {}", e), }); let (ipchange_tx, ipchange_rx) = mpsc::channel(); thread::spawn(move || match write_config(ipchange_rx) { - Ok(_) => unreachable!(), + Ok(_) => {} Err(e) => panic!("write_config error: {}", e), }); - // clone so that ppp2tun doesn't panic when ppp link closes - #[allow(clippy::redundant_clone)] - clt.run(tx.clone(), ipchange_tx.clone())?; + clt.run(tx, ipchange_tx)?; Ok(()) } |