diff options
author | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-03-24 16:13:05 +0100 |
---|---|---|
committer | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-03-24 16:13:05 +0100 |
commit | 0ae3d73709ac6fb7d15686310fbaf71fb00464d5 (patch) | |
tree | 5036b4505f5ba6e5f4ba7cd1751ac86c4f3779cb | |
parent | d08baaff6f1c332033d04a3d884169d5a349f3f8 (diff) |
don't crash if ip datagrams are sent while the session is down
-rw-r--r-- | src/client.rs | 15 | ||||
-rw-r--r-- | src/error.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 24 |
3 files changed, 31 insertions, 10 deletions
diff --git a/src/client.rs b/src/client.rs index 5010fd7..1a0ac03 100644 --- a/src/client.rs +++ b/src/client.rs @@ -4,7 +4,7 @@ use crate::error::{Error, Result}; use std::net::Ipv4Addr; use std::num::NonZeroU16; use std::sync::mpsc; -use std::sync::{Arc, RwLock}; +use std::sync::{Arc, Mutex, RwLock}; use std::thread; use std::time::Duration; @@ -69,6 +69,7 @@ impl Client { pub fn run( self, ip_tx: mpsc::Sender<Vec<u8>>, + ip_rx: Arc<Mutex<mpsc::Receiver<Option<Vec<u8>>>>>, ipchange_tx: mpsc::Sender<IpConfig>, ) -> Result<()> { if !self.inner.read().unwrap().started { @@ -77,6 +78,18 @@ impl Client { self.discover()?; + thread::spawn(move || { + for buf in &*ip_rx.lock().unwrap() { + match buf { + Some(buf) => match self.send_ipv4(&buf) { + Ok(_) => {} + Err(e) => println!("ip transmission failed: {}", e), + }, + None => return, + } + } + }); + Ok(handle.join().unwrap()?) } else { Err(Error::AlreadyActive) diff --git a/src/error.rs b/src/error.rs index 205ee67..f9587f4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -52,6 +52,8 @@ pub enum Error { #[error("mpsc send error")] MpscSendBytes(#[from] mpsc::SendError<Vec<u8>>), #[error("mpsc send error")] + MpscSendBytesOpt(#[from] mpsc::SendError<Option<Vec<u8>>>), + #[error("mpsc send error")] MpscSendIpConfig(#[from] mpsc::SendError<IpConfig>), #[error("mpsc receive error")] MpscRecv(#[from] mpsc::RecvError), diff --git a/src/main.rs b/src/main.rs index b13cf5f..9b9c9cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,7 +23,7 @@ where tmp } -fn tun2ppp(clt: Client, tun: Arc<Iface>) -> Result<()> { +fn tun2ppp(tx: mpsc::Sender<Option<Vec<u8>>>, tun: Arc<Iface>) -> Result<()> { loop { let mut buf = [0; 4 + 1492]; let n = tun.recv(&mut buf)?; @@ -38,7 +38,7 @@ fn tun2ppp(clt: Client, tun: Arc<Iface>) -> Result<()> { continue; } - clt.send_ipv4(&buf[4..])?; + tx.send(Some(buf[4..].to_vec()))?; } } @@ -80,26 +80,30 @@ fn main() -> Result<()> { thread::sleep(Duration::from_secs(8)); } - let (tx, rx) = mpsc::channel(); let tun = Arc::new(Iface::new("rsppp0", Mode::Tun)?); - let rx = Arc::new(Mutex::new(rx)); + let (recv_tx, recv_rx) = mpsc::channel(); + let recv_rx = Arc::new(Mutex::new(recv_rx)); + + let (send_tx, send_rx) = mpsc::channel(); + let send_rx = Arc::new(Mutex::new(send_rx)); loop { println!("connecting..."); let clt = Client::new(config.clone())?; - let rx2 = rx.clone(); + let recv_rx2 = recv_rx.clone(); + let send_tx2 = send_tx.clone(); let tun2 = tun.clone(); let tun3 = tun.clone(); - let clt2 = clt.clone(); - thread::spawn(move || match tun2ppp(clt2, tun2) { + + thread::spawn(move || match tun2ppp(send_tx2, tun2) { Ok(_) => {} Err(e) => panic!("tun2ppp error: {}", e), }); - thread::spawn(move || match ppp2tun(rx2, tun3) { + thread::spawn(move || match ppp2tun(recv_rx2, tun3) { Ok(_) => {} Err(e) => panic!("ppp2tun error: {}", e), }); @@ -110,7 +114,9 @@ fn main() -> Result<()> { Err(e) => panic!("write_config error: {}", e), }); - clt.run(tx.clone(), ipchange_tx)?; + clt.run(recv_tx.clone(), send_rx.clone(), ipchange_tx)?; + + send_tx.send(None)?; println!("connection lost"); } } |