From: Ian Jackson Date: Sat, 31 Jul 2021 13:05:47 +0000 (+0100) Subject: mimeswap X-Git-Tag: hippotat/1.0.0~416 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=f9c716f0d611053c8d442e6e08105df7e27d4484;p=hippotat.git mimeswap Signed-off-by: Ian Jackson --- diff --git a/src/bin/client.rs b/src/bin/client.rs index 9ba9b80..5f2362c 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -130,7 +130,9 @@ async fn run_client( let mut packet = packet.context("read from ipif")? .ok_or_else(|| io::Error::from(io::ErrorKind::UnexpectedEof))?; - if let Ok(()) = slip::check_checkmtu_mimeswap(&ic, SLIP_ESC, &mut packet) { + if let Ok(()) = slip::check_checkmtu_mimeswap + ::(&ic, &mut packet) + { let new_upbound_total = packet.len() + upbound_total + 1; if new_upbound_total > ic.max_batch_up.sat() { tx_defer = Some(packet); diff --git a/src/prelude.rs b/src/prelude.rs index c51cdba..48febde 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -53,6 +53,6 @@ pub const SLIP_END: u8 = 0o300; pub const SLIP_ESC: u8 = 0o333; pub const SLIP_ESC_END: u8 = 0o334; pub const SLIP_ESC_ESC: u8 = 0o335; -pub const SLIP_SWAP_ESC: u8 = b'-'; +pub const SLIP_MIME_ESC: u8 = b'-'; pub fn default() -> T { Default::default() } diff --git a/src/slip.rs b/src/slip.rs index 7fe3542..b7be70b 100644 --- a/src/slip.rs +++ b/src/slip.rs @@ -7,31 +7,24 @@ use crate::prelude::*; pub static SLIP_END_SLICE: &[u8] = &[SLIP_END]; #[throws(AE)] -pub fn check_checkmtu_mimeswap(ic: &InstanceConfig, input_esc: u8, - mut data: &mut [u8]) { - let mut sofar = 0; - let checkmtu = |_| Ok::<_,AE>(()); // xxx - while let Some((i, &c)) = data.iter().enumerate().find( - |(_,&c)| c == SLIP_ESC || c == SLIP_SWAP_ESC - ) { - let ni; let nc; - if c == input_esc { - match data.get(i+1) { +pub fn check_checkmtu_mimeswap + (ic: &InstanceConfig, data: &mut [u8]) +{ + for mut packet in data.split_mut(|&c| c == SLIP_END) { + if packet.len() > ic.mtu.sat() { + throw!(anyhow!("mtu exceeded ({} > {})", packet.len(), ic.mtu)); + } + + while let Some((i, _)) = packet.iter().enumerate().find( + |(_,&c)| c == if TO_MIME { SLIP_ESC } else { SLIP_MIME_ESC } + ) { + packet[i] = if TO_MIME { SLIP_MIME_ESC } else { SLIP_ESC }; + match packet.get(i+1) { Some(&SLIP_ESC_END) | Some(&SLIP_ESC_ESC) => Ok(()), _ => Err(anyhow!("SLIP escape not followed by ESC_END or ESC_ESC")), }?; - ni = i+2; - sofar += ni; - nc = if input_esc == SLIP_ESC { SLIP_SWAP_ESC } else { SLIP_ESC }; - } else { - ni = i+1; - sofar += ni; - checkmtu(sofar)?; - nc = input_esc; - }; - data[i] = nc; - data = &mut data[ni ..]; + packet = &mut packet[i+2 ..]; + } } - checkmtu(data.len())?; }