Files
LLeMbas-TTY/src/server/codec.rs
T
2019-03-22 20:41:50 -07:00

241 lines
7.0 KiB
Rust

//! encode and decode the frames for the mux protocol.
//! The frames include the length of a PDU as well as an identifier
//! that informs us how to decode it. The length, ident and serial
//! number are encoded using a variable length integer encoding.
//! Rather than rely solely on serde to serialize and deserialize an
//! enum, we encode the enum variants with a version/identifier tag
//! for ourselves. This will make it a little easier to manage
//! client and server instances that are built from different versions
//! of this code; in this way the client and server can more gracefully
//! manage unknown enum variants.
#![allow(dead_code)]
use crate::mux::tab::TabId;
use bincode;
use failure::Error;
use std::collections::HashMap;
use varu64;
fn encode_raw<W: std::io::Write>(
ident: u64,
serial: u64,
data: &[u8],
mut w: W,
) -> Result<(), std::io::Error> {
let len = data.len() + varu64::encoding_length(ident) + varu64::encoding_length(serial);
varu64::encode_write(len as u64, w.by_ref())?;
varu64::encode_write(serial, w.by_ref())?;
varu64::encode_write(ident, w.by_ref())?;
w.write_all(data)
}
fn read_u64<R: std::io::Read>(mut r: R) -> Result<u64, std::io::Error> {
let mut intbuf = [0u8; 9];
r.read_exact(&mut intbuf[0..1])?;
let len = match intbuf[0] {
0...247 => 0,
248 => 1,
249 => 2,
250 => 3,
251 => 4,
252 => 5,
253 => 6,
254 => 7,
255 => 8,
_ => unreachable!(),
};
if len > 0 {
r.read_exact(&mut intbuf[1..=len])?;
}
let (value, _) = varu64::decode(&intbuf[0..=len])
.map_err(|(err, _)| std::io::Error::new(std::io::ErrorKind::Other, format!("{}", err)))?;
Ok(value)
}
#[derive(Debug)]
struct Decoded {
ident: u64,
serial: u64,
data: Vec<u8>,
}
fn decode_raw<R: std::io::Read>(mut r: R) -> Result<Decoded, std::io::Error> {
let len = read_u64(r.by_ref())? as usize;
let serial = read_u64(r.by_ref())?;
let ident = read_u64(r.by_ref())?;
let data_len = len - (varu64::encoding_length(ident) + varu64::encoding_length(serial));
let mut data = vec![0u8; data_len];
r.read_exact(&mut data)?;
Ok(Decoded {
ident,
serial,
data,
})
}
#[derive(Debug, PartialEq)]
pub struct DecodedPdu {
pub serial: u64,
pub pdu: Pdu,
}
macro_rules! pdu {
($( $name:ident:$vers:expr),* $(,)?) => {
#[derive(PartialEq, Debug)]
pub enum Pdu {
Invalid{ident: u64},
$(
$name($name)
,)*
}
impl Pdu {
pub fn encode<W: std::io::Write>(&self, w: W, serial: u64) -> Result<(), Error> {
match self {
Pdu::Invalid{..} => bail!("attempted to serialize Pdu::Invalid"),
$(
Pdu::$name(s) => {
let data = bincode::serialize(s)?;
encode_raw($vers, serial, &data, w)?;
Ok(())
}
,)*
}
}
pub fn decode<R: std::io::Read>(r:R) -> Result<DecodedPdu, Error> {
let decoded = decode_raw(r)?;
match decoded.ident {
$(
$vers => {
Ok(DecodedPdu {
serial: decoded.serial,
pdu: Pdu::$name(bincode::deserialize(&decoded.data)?)
})
}
,)*
_ => Ok(DecodedPdu {
serial: decoded.serial,
pdu: Pdu::Invalid{ident:decoded.ident}
}),
}
}
}
}
}
/// Defines the Pdu enum.
/// Each struct has an explicit identifying number.
/// This allows removal of obsolete structs,
/// and defining newer structs as the protocol evolves.
pdu! {
Ping: 1,
Pong: 2,
ListTabs: 3,
ListTabsResponse: 4,
}
#[derive(Deserialize, Serialize, PartialEq, Debug)]
pub struct Ping {}
#[derive(Deserialize, Serialize, PartialEq, Debug)]
pub struct Pong {}
#[derive(Deserialize, Serialize, PartialEq, Debug)]
pub struct ListTabs {}
#[derive(Deserialize, Serialize, PartialEq, Debug)]
pub struct ListTabsResponse {
pub tabs: HashMap<TabId, String>,
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_frame() {
let mut encoded = Vec::new();
encode_raw(0x81, 0x42, b"hello", &mut encoded).unwrap();
assert_eq!(&encoded, b"\x07\x42\x81hello");
let decoded = decode_raw(encoded.as_slice()).unwrap();
assert_eq!(decoded.ident, 0x81);
assert_eq!(decoded.serial, 0x42);
assert_eq!(decoded.data, b"hello");
}
#[test]
fn test_frame_lengths() {
let mut serial = 1;
for target_len in &[128, 247, 256, 65536, 16777216] {
let mut payload = Vec::with_capacity(*target_len);
payload.resize(*target_len, b'a');
let mut encoded = Vec::new();
encode_raw(0x42, serial, payload.as_slice(), &mut encoded).unwrap();
let decoded = decode_raw(encoded.as_slice()).unwrap();
assert_eq!(decoded.ident, 0x42);
assert_eq!(decoded.serial, serial);
assert_eq!(decoded.data, payload);
serial += 1;
}
}
#[test]
fn test_pdu_ping() {
let mut encoded = Vec::new();
Pdu::Ping(Ping {}).encode(&mut encoded, 0x40).unwrap();
assert_eq!(&encoded, &[2, 0x40, 1]);
assert_eq!(
DecodedPdu {
serial: 0x40,
pdu: Pdu::Ping(Ping {})
},
Pdu::decode(encoded.as_slice()).unwrap()
);
}
#[test]
fn test_pdu_ping_base91() {
let mut encoded = Vec::new();
{
let mut encoder = base91::Base91Encoder::new(&mut encoded);
Pdu::Ping(Ping {}).encode(&mut encoder, 0x41).unwrap();
}
assert_eq!(&encoded, &[60, 67, 75, 65]);
let decoded = base91::decode(&encoded);
assert_eq!(
DecodedPdu {
serial: 0x41,
pdu: Pdu::Ping(Ping {})
},
Pdu::decode(decoded.as_slice()).unwrap()
);
}
#[test]
fn test_pdu_pong() {
let mut encoded = Vec::new();
Pdu::Pong(Pong {}).encode(&mut encoded, 0x42).unwrap();
assert_eq!(&encoded, &[2, 0x42, 2]);
assert_eq!(
DecodedPdu {
serial: 0x42,
pdu: Pdu::Pong(Pong {})
},
Pdu::decode(encoded.as_slice()).unwrap()
);
}
#[test]
fn test_bogus_pdu() {
let mut encoded = Vec::new();
encode_raw(0xdeadbeef, 0x42, b"hello", &mut encoded).unwrap();
assert_eq!(
DecodedPdu {
serial: 0x42,
pdu: Pdu::Invalid { ident: 0xdeadbeef }
},
Pdu::decode(encoded.as_slice()).unwrap()
);
}
}