From fc64e11cdd35051a2ea87237f548ae0497a2f7f9 Mon Sep 17 00:00:00 2001 From: Max Audron Date: Wed, 29 Apr 2020 00:00:44 +0200 Subject: refactor everything --- src/client/mod.rs | 40 ++- src/consts.rs | 0 src/error/mod.rs | 37 +++ src/frame/mod.rs | 416 ++++++++++++++++++++++++++++++ src/lib.rs | 57 ++++- src/message/handshake.rs | 435 ++++++++++++++++++++++++++++++++ src/message/handshake/types.rs | 56 ++++ src/message/login.rs | 1 + src/message/mod.rs | 5 + src/primitive/bufferinfo.rs | 81 ++++++ src/primitive/datetime.rs | 107 ++++++++ src/primitive/message.rs | 201 +++++++++++++++ src/primitive/mod.rs | 70 +++++ src/primitive/signedint.rs | 62 +++++ src/primitive/string.rs | 94 +++++++ src/primitive/stringlist.rs | 49 ++++ src/primitive/unsignedint.rs | 81 ++++++ src/primitive/variant.rs | 304 ++++++++++++++++++++++ src/primitive/variantlist.rs | 49 ++++ src/primitive/variantmap.rs | 58 +++++ src/protocol/error/mod.rs | 37 --- src/protocol/frame/mod.rs | 402 ----------------------------- src/protocol/message/handshake.rs | 352 -------------------------- src/protocol/message/handshake/types.rs | 66 ----- src/protocol/message/login.rs | 1 - src/protocol/message/mod.rs | 5 - src/protocol/mod.rs | 9 - src/protocol/primitive/bufferinfo.rs | 74 ------ src/protocol/primitive/datetime.rs | 93 ------- src/protocol/primitive/message.rs | 184 -------------- src/protocol/primitive/mod.rs | 74 ------ src/protocol/primitive/signedint.rs | 62 ----- src/protocol/primitive/string.rs | 91 ------- src/protocol/primitive/stringlist.rs | 45 ---- src/protocol/primitive/unsignedint.rs | 81 ------ src/protocol/primitive/variant.rs | 290 --------------------- src/protocol/primitive/variantlist.rs | 46 ---- src/protocol/primitive/variantmap.rs | 63 ----- src/tests/base_types.rs | 6 +- src/tests/frame.rs | 37 ++- src/tests/handshake_types.rs | 4 +- src/tests/variant_types.rs | 14 +- src/util.rs | 49 ++-- 43 files changed, 2222 insertions(+), 2066 deletions(-) delete mode 100644 src/consts.rs create mode 100644 src/error/mod.rs create mode 100644 src/frame/mod.rs create mode 100644 src/message/handshake.rs create mode 100644 src/message/handshake/types.rs create mode 100644 src/message/login.rs create mode 100644 src/message/mod.rs create mode 100644 src/primitive/bufferinfo.rs create mode 100644 src/primitive/datetime.rs create mode 100644 src/primitive/message.rs create mode 100644 src/primitive/mod.rs create mode 100644 src/primitive/signedint.rs create mode 100644 src/primitive/string.rs create mode 100644 src/primitive/stringlist.rs create mode 100644 src/primitive/unsignedint.rs create mode 100644 src/primitive/variant.rs create mode 100644 src/primitive/variantlist.rs create mode 100644 src/primitive/variantmap.rs delete mode 100644 src/protocol/error/mod.rs delete mode 100644 src/protocol/frame/mod.rs delete mode 100644 src/protocol/message/handshake.rs delete mode 100644 src/protocol/message/handshake/types.rs delete mode 100644 src/protocol/message/login.rs delete mode 100644 src/protocol/message/mod.rs delete mode 100644 src/protocol/mod.rs delete mode 100644 src/protocol/primitive/bufferinfo.rs delete mode 100644 src/protocol/primitive/datetime.rs delete mode 100644 src/protocol/primitive/message.rs delete mode 100644 src/protocol/primitive/mod.rs delete mode 100644 src/protocol/primitive/signedint.rs delete mode 100644 src/protocol/primitive/string.rs delete mode 100644 src/protocol/primitive/stringlist.rs delete mode 100644 src/protocol/primitive/unsignedint.rs delete mode 100644 src/protocol/primitive/variant.rs delete mode 100644 src/protocol/primitive/variantlist.rs delete mode 100644 src/protocol/primitive/variantmap.rs diff --git a/src/client/mod.rs b/src/client/mod.rs index fbb5b35..5c9699e 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -15,13 +15,13 @@ use tokio_util::codec::Framed; use futures_util::stream::StreamExt; use futures::SinkExt; -use crate::protocol::frame::QuasselCodec; +use crate::frame::QuasselCodec; use failure::Error; use log::{trace, debug, info, error}; -use crate::protocol::message::ConnAck; +use crate::message::ConnAck; extern crate log; @@ -39,9 +39,9 @@ pub enum ClientState { impl Client { pub async fn run(&mut self) { - use crate::protocol::primitive::StringList; - use crate::protocol::message::handshake::ClientInit; - use crate::protocol::message::handshake::HandshakeSerialize; + use crate::primitive::StringList; + use crate::message::ClientInit; + use crate::HandshakeSerialize; info!(target: "init", "Setting Features"); @@ -49,6 +49,7 @@ impl Client { features.push("SynchronizedMarkerLine".to_string()); features.push("Authenticators".to_string()); features.push("ExtendedFeatures".to_string()); + features.push("BufferActivitySync".to_string()); let client_init = ClientInit { client_version:String::from("Rust 0.0.0"), client_date: String::from("1579009211"), @@ -123,15 +124,16 @@ impl Client { } pub async fn handle_login_message(client: &mut Client, buf: &[u8]) -> Result<(), Error> { - use crate::protocol::message::ClientLogin; - use crate::protocol::message::handshake::{HandshakeSerialize, HandshakeDeserialize, VariantMap}; - use crate::util::get_msg_type; + use crate::{HandshakeSerialize, HandshakeDeserialize}; + use crate::message::ClientLogin; + use crate::primitive::{VariantMap, Variant}; trace!(target: "message", "Received bytes: {:x?}", buf); let (_, res) = VariantMap::parse(buf)?; debug!(target: "init", "Received Messsage: {:#?}", res); - let msgtype = get_msg_type(&res["MsgType"])?; - match msgtype { + + let msgtype = match_variant!(&res["MsgType"], Variant::String); + match msgtype.as_str() { "ClientInitAck" => { info!(target: "init", "Initialization successfull"); info!(target: "login", "Starting Login"); @@ -145,7 +147,6 @@ pub async fn handle_login_message(client: &mu info!(target: "login", "Login successfull"); }, "SessionInit" => { - info!(target: "message", "Received SessionInit: {:#?}", res); info!(target: "login", "Session Initialization finished. Switching to Connected state"); client.state = ClientState::Connected; } @@ -156,30 +157,25 @@ pub async fn handle_login_message(client: &mu error!(target: "client", "Error: WrongMsgType: {:#?}", res); } } + return Ok(()); } pub async fn handle_message(client: &mut Client, buf: &[u8]) -> Result<(), Error> { - use crate::protocol::primitive::VariantList; - use crate::protocol::primitive::deserialize::Deserialize; - use crate::protocol::primitive::serialize::Serialize; - use crate::util::get_msg_type; + use crate::primitive::VariantList; + use crate::Deserialize; + use crate::Serialize; trace!(target: "message", "Received bytes: {:x?}", buf); let (_, res) = VariantList::parse(buf)?; debug!(target: "init", "Received Messsage: {:#?}", res); - // let msgtype = get_msg_type(&res["MsgType"])?; - // match msgtype { - // _ => { - // error!(target: "client", "Error: WrongMsgType: {:#?}", res); - // } - // } + return Ok(()); } // Send the initialization message to the stream pub async fn init(stream: &mut TcpStream, tls: bool, compression: bool) -> Result { - use crate::protocol::primitive::deserialize::Deserialize; + use crate::Deserialize; // Buffer for our initialization let mut init: Vec = vec![]; diff --git a/src/consts.rs b/src/consts.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/error/mod.rs b/src/error/mod.rs new file mode 100644 index 0000000..72a9e59 --- /dev/null +++ b/src/error/mod.rs @@ -0,0 +1,37 @@ + #[derive(Debug, Fail)] +pub enum ProtocolError { + #[fail(display = "message has wrong type")] + WrongMsgType, + #[fail(display = "bool value is neither 0 nor 1")] + BoolOutOfRange, + #[fail(display = "QVariant is not known")] + UnknownVariant, + #[fail(display = "wrong variant has been given")] + WrongVariant, + #[fail(display = "io error")] + IOError(std::io::Error), + #[fail(display = "could not convert from int")] + TryFromIntError(std::num::TryFromIntError), + #[fail(display = "utf8 error")] + Utf8Error(std::string::FromUtf8Error), + } + +// impl std::error::Error for ErrorKind {} +// +// impl std::convert::From for ErrorKind { +// fn from(error: std::io::Error) -> Self { +// ErrorKind::IOError(error) +// } +// } +// +// impl std::convert::From for ErrorKind { +// fn from(error: std::num::TryFromIntError) -> Self { +// ErrorKind::TryFromIntError(error) +// } +// } +// +// impl std::convert::From for ErrorKind { +// fn from(error: std::string::FromUtf8Error) -> Self { +// ErrorKind::Utf8Error(error) +// } +// } diff --git a/src/frame/mod.rs b/src/frame/mod.rs new file mode 100644 index 0000000..709d3af --- /dev/null +++ b/src/frame/mod.rs @@ -0,0 +1,416 @@ +use std::convert::TryInto; +use std::error::Error as StdError; +use std::fmt; +use std::io::{self, Cursor}; + +use bytes::{Buf, BufMut, BytesMut}; + +use tokio::io::{AsyncRead, AsyncWrite}; + +use tokio_util::codec::{Decoder, Encoder, Framed, FramedRead, FramedWrite}; + +use flate2::Compress; +use flate2::Compression; +use flate2::Decompress; +use flate2::FlushCompress; +use flate2::FlushDecompress; + +/// Builder for the QuasselCodec +#[derive(Debug, Clone, Copy)] +pub struct Builder { + /// Enable or Disable Compression + compression: bool, + /// The level of Compression + compression_level: Compression, + + /// Maximum length of the frame + max_frame_len: usize, +} + +// An error when the number of bytes read is more than max frame length. +pub struct QuasselCodecError { + _priv: (), +} + +/// QuasselCodec provides the base layer of frameing and compression +#[derive(Debug)] +pub struct QuasselCodec { + builder: Builder, + state: DecodeState, + comp: Compress, + decomp: Decompress, +} + +#[derive(Debug, Clone, Copy)] +enum DecodeState { + Head, + Data(usize), +} + +impl QuasselCodec { + /// Creates a new quassel codec with default values + pub fn new() -> Self { + Self { + builder: Builder::new(), + state: DecodeState::Head, + comp: Compress::new(Compression::default(), true), + decomp: Decompress::new(true), + } + } + + /// Creates a new quassel codec builder with default configuration + /// values. + pub fn builder() -> Builder { + Builder::new() + } + + /// Gets the maximum frame length + pub fn max_frame_length(&self) -> usize { + self.builder.max_frame_len + } + + pub fn compression(&self) -> bool { + self.builder.compression + } + + pub fn compression_level(&self) -> Compression { + self.builder.compression_level + } + + /// Gets the maximum frame length + pub fn set_max_frame_length(&mut self, val: usize) { + self.builder.max_frame_length(val); + } + + pub fn set_compression(&mut self, val: bool) { + self.builder.compression(val); + } + + pub fn set_compression_level(&mut self, val: Compression) { + self.builder.compression_level(val); + } + + fn decode_head(&mut self, src: &mut BytesMut) -> io::Result> { + let head_len = 4; + + if src.len() < head_len { + // Not enough data + return Ok(None); + } + + let field_len = { + let mut src = Cursor::new(&mut *src); + + let field_len = src.get_uint(head_len); + + if field_len > self.builder.max_frame_len as u64 { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + QuasselCodecError { _priv: () }, + )); + } + + // The check above ensures there is no overflow + field_len as usize + }; + + // Strip header + let _ = src.split_to(head_len); + + // Ensure that the buffer has enough space to read the incoming + // payload + src.reserve(field_len); + + Ok(Some(field_len)) + } + + fn decode_data(&self, n: usize, src: &mut BytesMut) -> io::Result> { + // At this point, the buffer has already had the required capacity + // reserved. All there is to do is read. + if src.len() < n { + return Ok(None); + } + + Ok(Some(src.split_to(n))) + } +} + +impl Decoder for QuasselCodec { + type Item = BytesMut; + type Error = io::Error; + + fn decode(&mut self, src: &mut BytesMut) -> Result, io::Error> { + // Create Unified Buffer for compressed and not compressed datastream + let mut buf: &mut BytesMut = &mut BytesMut::new(); + + if self.builder.compression == true { + // Buffer to shove uncompressed stream into + let mut msg = Vec::with_capacity(self.builder.max_frame_len); + + let before_in = self.decomp.total_in(); + let before_out = self.decomp.total_out(); + + self.decomp + .decompress_vec(&src, &mut msg, FlushDecompress::None)?; + // Clear the src buffer, decompress() only peeks at content. + // without this we will endlessly loop over the same frame. + src.clear(); + + let after_in = self.decomp.total_in(); + let after_out = self.decomp.total_out(); + + let len = (after_out - before_out).try_into().unwrap(); + + // Reserve length of uncompressed stream + // and put bytes into there + buf.reserve(len); + buf.put(&msg[..]); + } else { + buf = src; + } + + let n = match self.state { + DecodeState::Head => match self.decode_head(buf)? { + Some(n) => { + self.state = DecodeState::Data(n); + n + } + None => return Ok(None), + }, + DecodeState::Data(n) => n, + }; + + match self.decode_data(n, buf)? { + Some(data) => { + // Update the decode state + self.state = DecodeState::Head; + + // Make sure the buffer has enough space to read the next head + buf.reserve(4); + + Ok(Some(data)) + } + None => Ok(None), + } + } +} + +impl Encoder for QuasselCodec { + type Item = Vec; + type Error = io::Error; + + fn encode(&mut self, data: Vec, dst: &mut BytesMut) -> Result<(), io::Error> { + let buf = &mut BytesMut::new(); + + let n = (&data).len(); + + if n > self.builder.max_frame_len { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + QuasselCodecError { _priv: () }, + )); + } + + // Reserve capacity in the destination buffer to fit the frame and + // length field (plus adjustment). + buf.reserve(4 + n); + + buf.put_uint(n as u64, 4); + + // Write the frame to the buffer + buf.extend_from_slice(&data[..]); + + if self.builder.compression { + let mut cbuf: Vec = vec![0; 4 + n]; + + let before_in = self.comp.total_in(); + let before_out = self.comp.total_out(); + + self.comp.compress(buf, &mut cbuf, FlushCompress::Full)?; + + let after_in = self.comp.total_in(); + let after_out = self.comp.total_out(); + + cbuf.truncate((after_out - before_out).try_into().unwrap()); + *dst = BytesMut::from(&cbuf[..]); + } else { + *dst = buf.clone(); + } + + Ok(()) + } +} + +impl Default for QuasselCodec { + fn default() -> Self { + Self::new() + } +} + +// ===== impl Builder ===== + +impl Builder { + /// Creates a new codec builder with default configuration + /// values. + /// + /// # Examples + /// + /// ``` + /// # use tokio::io::AsyncRead; + /// use libquassel::frame::QuasselCodec; + /// + /// # fn bind_read(io: T) { + /// QuasselCodec::builder() + /// .new_read(io); + /// # } + /// # pub fn main() {} + /// ``` + pub fn new() -> Builder { + Builder { + compression: false, + compression_level: Compression::default(), + max_frame_len: 64 * 1024 * 1024, + } + } + + /// Enables or disables the compression + pub fn compression(&mut self, val: bool) -> &mut Self { + self.compression = val; + self + } + + /// Sets the level of compression to + pub fn compression_level(&mut self, val: Compression) -> &mut Self { + self.compression_level = val; + self + } + + /// Sets the max frame length + /// + /// This configuration option applies to both encoding and decoding. The + /// default value is 67MB. + /// + /// When decoding, the length field read from the byte stream is checked + /// against this setting **before** any adjustments are applied. When + /// encoding, the length of the submitted payload is checked against this + /// setting. + /// + /// When frames exceed the max length, an `io::Error` with the custom value + /// of the `QuasselCodecError` type will be returned. + /// + /// # Examples + /// + /// ``` + /// # use tokio::io::AsyncRead; + /// use libquassel::frame::QuasselCodec; + /// + /// # fn bind_read(io: T) { + /// QuasselCodec::builder() + /// .max_frame_length(8 * 1024) + /// .new_read(io); + /// # } + /// # pub fn main() {} + /// ``` + pub fn max_frame_length(&mut self, val: usize) -> &mut Self { + self.max_frame_len = val; + self + } + + /// Create a configured `QuasselCodec` + /// + /// # Examples + /// + /// ``` + /// use libquassel::frame::QuasselCodec; + /// # pub fn main() { + /// QuasselCodec::builder() + /// .new_codec(); + /// # } + /// ``` + pub fn new_codec(&self) -> QuasselCodec { + QuasselCodec { + builder: *self, + state: DecodeState::Head, + comp: Compress::new(self.compression_level, true), + decomp: Decompress::new(true), + } + } + + /// Create a configured `FramedRead` + /// + /// # Examples + /// + /// ``` + /// # use tokio::io::AsyncRead; + /// use libquassel::frame::QuasselCodec; + /// + /// # fn bind_read(io: T) { + /// QuasselCodec::builder() + /// .new_read(io); + /// # } + /// # pub fn main() {} + /// ``` + pub fn new_read(&self, upstream: T) -> FramedRead + where + T: AsyncRead, + { + FramedRead::new(upstream, self.new_codec()) + } + + /// Create a configured `FramedWrite` + /// + /// # Examples + /// + /// ``` + /// # use tokio::io::AsyncWrite; + /// # use libquassel::frame::QuasselCodec; + /// # fn write_frame(io: T) { + /// QuasselCodec::builder() + /// .new_write(io); + /// # } + /// # pub fn main() {} + /// ``` + pub fn new_write(&self, inner: T) -> FramedWrite + where + T: AsyncWrite, + { + FramedWrite::new(inner, self.new_codec()) + } + + /// Create a configured `Framed` + /// + /// # Examples + /// + /// ``` + /// # use tokio::io::{AsyncRead, AsyncWrite}; + /// # use libquassel::frame::QuasselCodec; + /// # fn write_frame(io: T) { + /// # let _ = + /// QuasselCodec::builder() + /// .new_framed(io); + /// # } + /// # pub fn main() {} + /// ``` + pub fn new_framed(&self, inner: T) -> Framed + where + T: AsyncRead + AsyncWrite, + { + Framed::new(inner, self.new_codec()) + } +} + +// ===== impl LengthDelimitedCodecError ===== + +impl fmt::Debug for QuasselCodecError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("QuasselCodecError").finish() + } +} + +impl fmt::Display for QuasselCodecError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("frame size too big") + } +} + +impl StdError for QuasselCodecError {} diff --git a/src/lib.rs b/src/lib.rs index ed05773..215dcfc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,5 @@ -pub mod consts; -pub mod protocol; - #[macro_use] -pub mod util; +mod util; #[cfg(feature = "client")] pub mod client; @@ -12,3 +9,55 @@ pub mod tests; #[macro_use] extern crate failure; + +pub mod message; +pub mod primitive; + +#[allow(dead_code)] +pub mod error; + +#[allow(unused_variables, dead_code)] +#[cfg(feature = "framing")] +pub mod frame; + +use failure::Error; + +/// Serialization of types and structs to the quassel byteprotocol +pub trait Serialize { + fn serialize(&self) -> Result, Error>; +} + +/// Serialization of UTF-8 based Strings to the quassel byteprotocol +pub trait SerializeUTF8 { + fn serialize_utf8(&self) -> Result, Error>; +} + +/// Deserialization of types and structs to the quassel byteprotocol +pub trait Deserialize { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> + where + Self: std::marker::Sized; +} + +/// Deserialization of UTF-8 based Strings to the quassel byteprotocol +pub trait DeserializeUTF8 { + fn parse_utf8(b: &[u8]) -> Result<(usize, Self), Error> + where + Self: std::marker::Sized; +} + +/// HandshakeSerialize implements the serialization needed during the handhake phase. +/// +/// The protocol has some minor differences during this phase compared to the regular parsing. +pub trait HandshakeSerialize { + fn serialize(&self) -> Result, Error>; +} + +/// HandshakeDeserialize implements the deserialization needed during the handhake phase. +/// +/// The protocol has some minor differences during this phase compared to the regular parsing. +pub trait HandshakeDeserialize { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> + where + Self: std::marker::Sized; +} diff --git a/src/message/handshake.rs b/src/message/handshake.rs new file mode 100644 index 0000000..753488e --- /dev/null +++ b/src/message/handshake.rs @@ -0,0 +1,435 @@ +use failure::Error; +use std::result::Result; + +use crate::error::ProtocolError; +use crate::primitive::{StringList, Variant, VariantList}; +mod types; +use crate::primitive::VariantMap; +use crate::{HandshakeDeserialize, HandshakeSerialize}; + +use crate::match_variant; + +/// Data received right after initializing the connection +/// +/// ConnAck is serialized sequentially +#[derive(Debug)] +pub struct ConnAck { + /// The Flag 0x01 for TLS + /// and 0x02 for Deflate Compression + flags: u8, + /// Some extra protocol version specific data + /// So far unused + extra: i16, + /// The version of the protocol + /// 0x00000001 for the legacy protocol + /// 0x00000002 for the datastream protocol + /// + /// Only the datastream protocol is supported by this crate + version: i8, +} + +impl Default for ConnAck { + fn default() -> Self { + Self { + flags: 0x00, + extra: 0x00, + version: 0x00000002, + } + } +} + +impl crate::Serialize for ConnAck { + fn serialize(&self) -> Result, Error> { + let mut bytes: Vec = Vec::new(); + + bytes.append(&mut self.flags.serialize()?); + bytes.append(&mut self.extra.serialize()?); + bytes.append(&mut self.version.serialize()?); + + Ok(bytes) + } +} + +impl crate::Deserialize for ConnAck { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let (flen, flags) = u8::parse(b)?; + let (elen, extra) = i16::parse(&b[flen..])?; + let (vlen, version) = i8::parse(&b[(flen + elen)..])?; + + return Ok(( + flen + elen + vlen, + Self { + flags, + extra, + version, + }, + )); + } +} + +/// ClientInit is the Initial message send to the core after establishing a base layer comunication. +/// +/// Features +/// +/// | Flag | Name | Description | +/// | ---- | ---- | ----------- | +/// | 0x00000001 | SynchronizedMarkerLine | -- | +/// | 0x00000002 | SaslAuthentication | -- | +/// | 0x00000004 | SaslExternal | -- | +/// | 0x00000008 | HideInactiveNetworks | -- | +/// | 0x00000010 | PasswordChange | -- | +/// | 0x00000020 | CapNegotiation | IRCv3 capability negotiation, account tracking | +/// | 0x00000040 | VerifyServerSSL | IRC server SSL validation | +/// | 0x00000080 | CustomRateLimits | IRC server custom message rate limits | +/// | 0x00000100 | DccFileTransfer | Currently not supported | +/// | 0x00000200 | AwayFormatTimestamp | Timestamp formatting in away (e.g. %%hh:mm%%) | +/// | 0x00000400 | Authenticators | Support for exchangeable auth backends | +/// | 0x00000800 | BufferActivitySync | Sync buffer activity status | +/// | 0x00001000 | CoreSideHighlights | Core-Side highlight configuration and matching | +/// | 0x00002000 | SenderPrefixes | Show prefixes for senders in backlog | +/// | 0x00004000 | RemoteDisconnect | Supports RPC call disconnectFromCore to remotely disconnect a client | +/// | 0x00008000 | ExtendedFeatures | Transmit features as list of strings | +/// | -- | LongTime | Serialize message time as 64-bit | +/// | -- | RichMessages | Real Name and Avatar URL in backlog | +/// | -- | BacklogFilterType | Backlogmanager supports filtering backlog by messagetype | +/// | -- | EcdsaCertfpKeys | ECDSA keys for CertFP in identities | +/// | -- | LongMessageId | 64-bit IDs for messages | +/// | -- | SyncedCoreInfo | CoreInfo dynamically updated using signals | +#[derive(Debug)] +pub struct ClientInit { + /// Version of the client + pub client_version: String, + /// Build date of the client + pub client_date: String, + /// supported features as bitflags + pub client_features: u32, + /// List of supported extended features + pub feature_list: StringList, +} + +impl HandshakeSerialize for ClientInit { + fn serialize(&self) -> Result, Error> { + let mut values: VariantMap = VariantMap::with_capacity(5); + values.insert( + "MsgType".to_string(), + Variant::String("ClientInit".to_string()), + ); + values.insert( + "ClientVersion".to_string(), + Variant::String(self.client_version.clone()), + ); + values.insert( + "ClientDate".to_string(), + Variant::String(self.client_date.clone()), + ); + values.insert("Features".to_string(), Variant::u32(self.client_features)); + values.insert( + "FeatureList".to_string(), + Variant::StringList(self.feature_list.clone()), + ); + return HandshakeSerialize::serialize(&values); + } +} + +impl HandshakeDeserialize for ClientInit { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; + + let msgtype = match_variant!(&values["MsgType"], Variant::StringUTF8); + + if msgtype == "ClientInit" { + return Ok(( + len, + Self { + client_version: match_variant!(values["ClientVersion"], Variant::String), + client_date: match_variant!(values["ClientDate"], Variant::String), + feature_list: match_variant!(values["FeatureList"], Variant::StringList), + client_features: match_variant!(values["Features"], Variant::u32), + }, + )); + } else { + bail!(ProtocolError::WrongMsgType); + } + } +} + +/// ClientInitReject is received when the initialization fails +#[derive(Debug)] +pub struct ClientInitReject { + /// String with an error message of what went wrong + pub error_string: String, +} + +impl HandshakeSerialize for ClientInitReject { + fn serialize(&self) -> Result, Error> { + let mut values: VariantMap = VariantMap::with_capacity(2); + values.insert( + "MsgType".to_string(), + Variant::String("ClientInitReject".to_string()), + ); + values.insert( + "ErrorString".to_string(), + Variant::String(self.error_string.clone()), + ); + return HandshakeSerialize::serialize(&values); + } +} + +impl HandshakeDeserialize for ClientInitReject { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; + + let msgtype = match_variant!(&values["MsgType"], Variant::StringUTF8); + + if msgtype == "ClientInitReject" { + return Ok(( + len, + Self { + error_string: match_variant!(values["ErrorString"], Variant::String), + }, + )); + } else { + bail!(ProtocolError::WrongMsgType); + } + } +} + +/// ClientInitAck is received when the initialization was successfull +#[derive(Debug)] +pub struct ClientInitAck { + /// Flags of supported legacy features + pub core_features: u32, + /// If the core has already been configured + pub core_configured: bool, + /// List of VariantMaps of info on available backends + pub storage_backends: VariantList, + /// List of VariantMaps of info on available authenticators + pub authenticators: VariantList, + /// List of supported extended features + pub feature_list: StringList, +} + +impl HandshakeSerialize for ClientInitAck { + fn serialize(&self) -> Result, Error> { + let mut values: VariantMap = VariantMap::with_capacity(6); + values.insert( + "MsgType".to_string(), + Variant::String("ClientInitAck".to_string()), + ); + values.insert("CoreFeatures".to_string(), Variant::u32(self.core_features)); + values.insert( + "Configured".to_string(), + Variant::bool(self.core_configured), + ); + values.insert( + "StorageBackends".to_string(), + Variant::VariantList(self.storage_backends.clone()), + ); + values.insert( + "Authenticators".to_string(), + Variant::VariantList(self.authenticators.clone()), + ); + values.insert( + "FeatureList".to_string(), + Variant::StringList(self.feature_list.clone()), + ); + return HandshakeSerialize::serialize(&values); + } +} + +impl HandshakeDeserialize for ClientInitAck { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; + + let msgtype = match_variant!(&values["MsgType"], Variant::StringUTF8); + + if msgtype == "ClientInitAck" { + return Ok(( + len, + Self { + core_features: 0x00008000, + core_configured: match_variant!(values["Configured"], Variant::bool), + storage_backends: match_variant!( + values["StorageBackends"], + Variant::VariantList + ), + authenticators: match_variant!(values["Authenticators"], Variant::VariantList), + feature_list: match_variant!(values["FeatureList"], Variant::StringList), + }, + )); + } else { + bail!(ProtocolError::WrongMsgType); + } + } +} + +/// Login to the core with user data +/// username and password are transmitted in plain text +#[derive(Debug)] +pub struct ClientLogin { + pub user: String, + pub password: String, +} + +impl HandshakeSerialize for ClientLogin { + fn serialize(&self) -> Result, Error> { + let mut values: VariantMap = VariantMap::new(); + values.insert( + "MsgType".to_string(), + Variant::String("ClientLogin".to_string()), + ); + values.insert("User".to_string(), Variant::String(self.user.clone())); + values.insert( + "Password".to_string(), + Variant::String(self.password.clone()), + ); + return HandshakeSerialize::serialize(&values); + } +} + +impl HandshakeDeserialize for ClientLogin { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; + + let msgtype = match_variant!(&values["MsgType"], Variant::StringUTF8); + + if msgtype == "ClientLogin" { + return Ok(( + len, + Self { + user: match_variant!(values["User"], Variant::String), + password: match_variant!(values["Password"], Variant::String), + }, + )); + } else { + bail!(ProtocolError::WrongMsgType); + } + } +} + +/// ClientLoginAck is received after the client has successfully logged in +/// it has no fields +#[derive(Debug)] +pub struct ClientLoginAck; + +impl HandshakeSerialize for ClientLoginAck { + fn serialize(&self) -> Result, Error> { + let mut values: VariantMap = VariantMap::with_capacity(1); + values.insert( + "MsgType".to_string(), + Variant::String("ClientLoginAck".to_string()), + ); + return HandshakeSerialize::serialize(&values); + } +} + +impl HandshakeDeserialize for ClientLoginAck { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; + + let msgtype = match_variant!(&values["MsgType"], Variant::StringUTF8); + + if msgtype == "ClientLogin" { + return Ok((len, Self {})); + } else { + bail!(ProtocolError::WrongMsgType); + } + } +} + +/// ClientLoginReject is received after the client failed to login +/// It contains an error message as String +#[derive(Debug)] +pub struct ClientLoginReject { + error: String, +} + +impl HandshakeSerialize for ClientLoginReject { + fn serialize(&self) -> Result, Error> { + let mut values: VariantMap = VariantMap::with_capacity(1); + values.insert( + "MsgType".to_string(), + Variant::String("ClientLoginReject".to_string()), + ); + values.insert( + "ErrorString".to_string(), + Variant::String(self.error.clone()), + ); + return HandshakeSerialize::serialize(&values); + } +} + +impl HandshakeDeserialize for ClientLoginReject { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; + + let msgtype = match_variant!(&values["MsgType"], Variant::StringUTF8); + + if msgtype == "ClientLogin" { + return Ok(( + len, + Self { + error: match_variant!(values["ErrorString"], Variant::String), + }, + )); + } else { + bail!(ProtocolError::WrongMsgType); + } + } +} + +/// SessionInit is received along with ClientLoginAck to initialize that user Session +// TODO Replace with proper types +#[derive(Debug)] +pub struct SessionInit { + /// List of all configured identities + identities: VariantList, + /// List of all existing buffers + buffers: VariantList, + /// Ids of all networks + network_ids: VariantList, +} + +impl HandshakeSerialize for SessionInit { + fn serialize(&self) -> Result, Error> { + let mut values: VariantMap = VariantMap::with_capacity(1); + values.insert( + "MsgType".to_string(), + Variant::String("SessionInit".to_string()), + ); + values.insert( + "Identities".to_string(), + Variant::VariantList(self.identities.clone()), + ); + values.insert( + "BufferInfos".to_string(), + Variant::VariantList(self.buffers.clone()), + ); + values.insert( + "NetworkIds".to_string(), + Variant::VariantList(self.network_ids.clone()), + ); + return HandshakeSerialize::serialize(&values); + } +} + +impl HandshakeDeserialize for SessionInit { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; + + let msgtype = match_variant!(&values["MsgType"], Variant::StringUTF8); + + if msgtype == "ClientLogin" { + return Ok(( + len, + Self { + identities: match_variant!(values["Identities"], Variant::VariantList), + buffers: match_variant!(values["BufferInfos"], Variant::VariantList), + network_ids: match_variant!(values["NetworkIds"], Variant::VariantList), + }, + )); + } else { + bail!(ProtocolError::WrongMsgType); + } + } +} diff --git a/src/message/handshake/types.rs b/src/message/handshake/types.rs new file mode 100644 index 0000000..04e1dd0 --- /dev/null +++ b/src/message/handshake/types.rs @@ -0,0 +1,56 @@ +use std::convert::TryInto; +use std::result::Result; +use std::vec::Vec; + +use failure::Error; + +use crate::error::ProtocolError; +use crate::primitive::Variant; +use crate::Deserialize; +use crate::Serialize; +use crate::util; + +use crate::primitive::VariantMap; +use crate::{HandshakeDeserialize, HandshakeSerialize}; + +impl HandshakeSerialize for VariantMap { + fn serialize<'a>(&'a self) -> Result, Error> { + let mut res: Vec = Vec::new(); + + for (k, v) in self { + let key = Variant::String(k.clone()); + res.extend(key.serialize()?); + res.extend(v.serialize()?); + } + + let len: i32 = (self.len() * 2).try_into().unwrap(); + util::insert_bytes(0, &mut res, &mut (len).to_be_bytes()); + + return Ok(res); + } +} + +impl HandshakeDeserialize for VariantMap { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let (_, len) = i32::parse(&b[0..4])?; + + let mut pos: usize = 4; + let mut map = VariantMap::new(); + + for _ in 0..(len / 2) { + let (nlen, name) = Variant::parse(&b[pos..])?; + pos += nlen; + + let (vlen, value) = Variant::parse(&b[pos..])?; + pos += vlen; + + match name { + Variant::String(x) => map.insert(x, value), + Variant::StringUTF8(x) => map.insert(x, value), + _ => bail!(ProtocolError::WrongVariant), + }; + } + + return Ok((pos, map)); + } +} diff --git a/src/message/login.rs b/src/message/login.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/message/login.rs @@ -0,0 +1 @@ + diff --git a/src/message/mod.rs b/src/message/mod.rs new file mode 100644 index 0000000..b390e2f --- /dev/null +++ b/src/message/mod.rs @@ -0,0 +1,5 @@ +mod handshake; +mod login; + +pub use handshake::*; +pub use login::*; diff --git a/src/primitive/bufferinfo.rs b/src/primitive/bufferinfo.rs new file mode 100644 index 0000000..9cbaa2d --- /dev/null +++ b/src/primitive/bufferinfo.rs @@ -0,0 +1,81 @@ +use std::vec::Vec; + +use failure::Error; + +use crate::{Deserialize, DeserializeUTF8}; +use crate::{Serialize, SerializeUTF8}; + +extern crate bytes; + +/// The BufferInfo struct represents a BufferInfo as received in IRC +/// +/// BufferInfo is, like all other struct based types, serialized sequentially. +#[derive(Clone, Debug, std::cmp::PartialEq)] +pub struct BufferInfo { + /// a unique, sequential id for the buffer + pub id: i32, + /// NetworkId of the network the buffer belongs to + pub network_id: i32, + /// The Type of the Buffer + pub buffer_type: BufferType, + /// BufferName as displayed to the user + pub name: String, +} + +impl Serialize for BufferInfo { + fn serialize(&self) -> Result, Error> { + let mut values: Vec = Vec::new(); + + values.append(&mut i32::serialize(&self.id)?); + values.append(&mut i32::serialize(&self.network_id)?); + values.append(&mut i16::serialize(&(self.buffer_type as i16))?); + values.append(&mut vec![0, 0, 0, 0]); + values.append(&mut String::serialize_utf8(&self.name)?); + + Ok(values) + } +} + +impl Deserialize for BufferInfo { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let (_, id) = i32::parse(&b[0..4])?; + let (_, network_id) = i32::parse(&b[4..8])?; + let (_, buffer_type) = i16::parse(&b[8..10])?; + + // There are 4 additional undocumented Bytes in the BufferInfo + // so we start at byte 14 + let (size, name) = String::parse_utf8(&b[14..])?; + + return Ok(( + 14 + size, + Self { + id, + network_id, + buffer_type: BufferType::from(buffer_type), + name, + }, + )); + } +} + +/// The Type of the Buffer +#[repr(i16)] +#[derive(Copy, Clone, Debug, std::cmp::PartialEq)] +pub enum BufferType { + Status = 0x01, + Channel = 0x02, + Query = 0x04, + Group = 0x08, +} + +impl From for BufferType { + fn from(value: i16) -> Self { + match value { + 0x01 => return Self::Status, + 0x02 => return Self::Channel, + 0x04 => return Self::Query, + 0x08 => return Self::Group, + _ => unimplemented!(), + } + } +} diff --git a/src/primitive/datetime.rs b/src/primitive/datetime.rs new file mode 100644 index 0000000..e6946b9 --- /dev/null +++ b/src/primitive/datetime.rs @@ -0,0 +1,107 @@ +use crate::Deserialize; +use crate::Serialize; + +/// The DateTime struct represents a DateTime as received in IRC +/// +/// DateTime is, like all other struct based types, serialized sequentially. +#[derive(Clone, Debug, std::cmp::PartialEq)] +pub struct DateTime { + /// Day in Julian calendar, unknown if signed or unsigned + julian_day: i32, + /// Milliseconds since start of day + millis_of_day: i32, + /// Timezone of DateTime, 0x00 is local, 0x01 is UTC + zone: u8, +} + +impl Serialize for DateTime { + fn serialize(&self) -> Result, failure::Error> { + let mut values: Vec = Vec::new(); + + values.append(&mut i32::serialize(&self.julian_day)?); + values.append(&mut i32::serialize(&self.millis_of_day)?); + values.append(&mut u8::serialize(&(self.zone))?); + + Ok(values) + } +} + +impl Deserialize for DateTime { + fn parse(b: &[std::primitive::u8]) -> Result<(std::primitive::usize, Self), failure::Error> + where + Self: Sized, + { + let (_, julian_day) = i32::parse(&b[0..4])?; + let (_, millis_of_day) = i32::parse(&b[4..8])?; + let (_, zone) = u8::parse(&b[8..9])?; + + return Ok(( + 9, + DateTime { + julian_day, + millis_of_day, + zone, + }, + )); + } +} + +/// The Date struct represents a Date as received in IRC +/// +/// Date is, like all other struct based types, serialized sequentially. +#[derive(Clone, Debug, std::cmp::PartialEq)] +pub struct Date { + /// Day in Julian calendar, unknown if signed or unsigned + julian_day: i32, +} + +impl Serialize for Date { + fn serialize(&self) -> Result, failure::Error> { + let mut values: Vec = Vec::new(); + + values.append(&mut i32::serialize(&self.julian_day)?); + + Ok(values) + } +} + +impl Deserialize for Date { + fn parse(b: &[std::primitive::u8]) -> Result<(std::primitive::usize, Self), failure::Error> + where + Self: Sized, + { + let (_, julian_day) = i32::parse(&b[0..4])?; + + return Ok((9, Date { julian_day })); + } +} + +/// The Time struct represents a Time as received in IRC +/// +/// Time is, like all other struct based types, serialized sequentially. +#[derive(Clone, Debug, std::cmp::PartialEq)] +pub struct Time { + /// Milliseconds since start of day + millis_of_day: i32, +} + +impl Serialize for Time { + fn serialize(&self) -> Result, failure::Error> { + let mut values: Vec = Vec::new(); + + values.append(&mut i32::serialize(&self.millis_of_day)?); + + Ok(values) + } +} + +impl Deserialize for Time { + fn parse(b: &[std::primitive::u8]) -> Result<(std::primitive::usize, Self), failure::Error> + where + Self: Sized, + { + let (_, millis_of_day) = i32::parse(&b[0..4])?; + + return Ok((4, Time { millis_of_day })); + } +} diff --git a/src/primitive/message.rs b/src/primitive/message.rs new file mode 100644 index 0000000..64b132d --- /dev/null +++ b/src/primitive/message.rs @@ -0,0 +1,201 @@ +use std::vec::Vec; + +use failure::Error; + +use crate::{Deserialize, DeserializeUTF8}; +use crate::{Serialize, SerializeUTF8}; + +use crate::primitive::BufferInfo; + +extern crate bytes; + +/// The Message struct represents a Message as received in IRC +/// +/// Messages are, like all other struct based types, serialized sequentially. +#[derive(Clone, Debug, std::cmp::PartialEq)] +pub struct Message { + /// The unique, sequential id for the message + pub msg_id: i32, + /// The timestamp of the message in UNIX time (32-bit, seconds, 64-bit if LONGMESSAGE feature enabled) + pub timestamp: i64, + /// The message type as it's own type serialized as i32 + pub msg_type: MessageType, + /// The flags + pub flags: i8, + /// The buffer the message belongs to, usually everything but BufferId is set to NULL + pub buffer: BufferInfo, + /// The sender as nick!ident@host + pub sender: String, + /// The prefix modes of the sender. + /// Only Some when the SenderPrefix features is enabled + pub sender_prefixes: Option, + /// The realName of the sender + /// Only Some when the RichMessage features is enabled + pub real_name: Option, + /// The avatarUrl of the sender, if available + /// Only Some when the RichMessage features is enabled + pub avatar_url: Option, + /// The message content, already stripped from CTCP formatting, but containing mIRC format codes + pub content: String, +} + +impl Serialize for Message { + fn serialize(&self) -> Result, Error> { + let mut values: Vec = Vec::new(); + + values.append(&mut i32::serialize(&self.msg_id)?); + + // TODO LONGMESSAGE feature + if false { + values.append(&mut i64::serialize(&self.timestamp)?); + } else { + values.append(&mut i32::serialize(&(self.timestamp as i32))?); + } + + values.append(&mut i32::serialize(&(self.msg_type as i32))?); + values.append(&mut i8::serialize(&(self.flags as i8))?); + values.append(&mut BufferInfo::serialize(&self.buffer)?); + values.append(&mut String::serialize_utf8(&self.sender)?); + + // TODO SenderPrefixes feature + if false { + if let Some(x) = &self.sender_prefixes { + values.append(&mut String::serialize_utf8(&x)?); + } + } + + // TODO RichMessages feature + if false { + if let Some(x) = &self.real_name { + values.append(&mut String::serialize_utf8(&x)?); + } + if let Some(x) = &self.avatar_url { + values.append(&mut String::serialize_utf8(&x)?); + } + } + + values.append(&mut String::serialize_utf8(&self.content)?); + + return Ok(values); + } +} + +impl Deserialize for Message { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let mut pos = 0; + let (parsed, msg_id) = i32::parse(&b[pos..])?; + pos += parsed; + + // TODO LONGMESSAGES feature + let timestamp; + if false { + let (parsed, temp_timestamp) = i64::parse(&b[pos..])?; + pos += parsed; + timestamp = temp_timestamp; + } else { + let (parsed, temp_timestamp) = i32::parse(&b[pos..])?; + pos += parsed; + timestamp = temp_timestamp as i64; + } + + let (parsed, msg_type) = i32::parse(&b[pos..])?; + pos += parsed; + let (parsed, flags) = i8::parse(&b[pos..])?; + pos += parsed; + let (parsed, buffer) = BufferInfo::parse(&b[pos..])?; + pos += parsed; + let (parsed, sender) = String::parse_utf8(&b[pos..])?; + pos += parsed; + + // TODO SenderPrefixes feature + let mut sender_prefixes = None; + if false { + let (parsed, temp) = String::parse_utf8(&b[pos..])?; + sender_prefixes = Some(temp); + pos += parsed; + } + + // TODO SenderPrefixes feature + let mut real_name = None; + let mut avatar_url = None; + if false { + let (parsed, temp) = String::parse_utf8(&b[pos..])?; + real_name = Some(temp); + pos += parsed; + + let (parsed, temp) = String::parse_utf8(&b[pos..])?; + avatar_url = Some(temp); + pos += parsed; + } + + let (parsed, content) = String::parse_utf8(&b[pos..])?; + pos += parsed; + + return Ok(( + pos, + Self { + msg_id, + timestamp, + msg_type: MessageType::from(msg_type), + flags, + buffer, + sender, + sender_prefixes, + real_name, + avatar_url, + content, + }, + )); + } +} + +#[repr(i32)] +#[derive(Copy, Clone, Debug, std::cmp::PartialEq)] +pub enum MessageType { + Plain = 0x00000001, + Notice = 0x00000002, + Action = 0x00000004, + Nick = 0x00000008, + Mode = 0x00000010, + Join = 0x00000020, + Part = 0x00000040, + Quit = 0x00000080, + Kick = 0x00000100, + Kill = 0x00000200, + Server = 0x00000400, + Info = 0x00000800, + Error = 0x00001000, + DayChange = 0x00002000, + Topic = 0x00004000, + NetsplitJoin = 0x00008000, + NetsplitQuit = 0x00010000, + Invite = 0x00020000, + Markerline = 0x00040000, +} + +impl From for MessageType { + fn from(val: i32) -> Self { + match val { + 0x00000001 => MessageType::Plain, + 0x00000002 => MessageType::Notice, + 0x00000004 => MessageType::Action, + 0x00000008 => MessageType::Nick, + 0x00000010 => MessageType::Mode, + 0x00000020 => MessageType::Join, + 0x00000040 => MessageType::Part, + 0x00000080 => MessageType::Quit, + 0x00000100 => MessageType::Kick, + 0x00000200 => MessageType::Kill, + 0x00000400 => MessageType::Server, + 0x00000800 => MessageType::Info, + 0x00001000 => MessageType::Error, + 0x00002000 => MessageType::DayChange, + 0x00004000 => MessageType::Topic, + 0x00008000 => MessageType::NetsplitJoin, + 0x00010000 => MessageType::NetsplitQuit, + 0x00020000 => MessageType::Invite, + 0x00040000 => MessageType::Markerline, + _ => unimplemented!(), + } + } +} diff --git a/src/primitive/mod.rs b/src/primitive/mod.rs new file mode 100644 index 0000000..a3d2dcd --- /dev/null +++ b/src/primitive/mod.rs @@ -0,0 +1,70 @@ +mod bufferinfo; +mod datetime; +mod message; +mod signedint; +mod string; +mod stringlist; +mod unsignedint; +mod variant; +mod variantlist; +mod variantmap; + +pub use bufferinfo::*; +pub use datetime::*; +pub use message::*; +pub use signedint::*; +pub use string::*; +pub use stringlist::*; +pub use unsignedint::*; +pub use variant::*; +pub use variantlist::*; +pub use variantmap::*; + +/// Byte Representation of the type used in Variant to identify it +pub const VOID: u32 = 0x00000000; +/// Byte Representation of the type used in Variant to identify it +pub const BOOL: u32 = 0x00000001; +/// Byte Representation of the type used in Variant to identify it +pub const QCHAR: u32 = 0x00000007; + +/// Byte Representation of the type used in Variant to identify it +pub const QVARIANT: u32 = 0x00000090; +/// Byte Representation of the type used in Variant to identify it +pub const QVARIANTMAP: u32 = 0x00000008; +/// Byte Representation of the type used in Variant to identify it +pub const QVARIANTLIST: u32 = 0x00000009; + +/// Byte Representation of the type used in Variant to identify it +pub const QSTRING: u32 = 0x0000000a; +/// Byte Representation of the type used in Variant to identify it +pub const QSTRINGLIST: u32 = 0x0000000b; +/// Byte Representation of the type used in Variant to identify it +pub const QBYTEARRAY: u32 = 0x0000000c; + +/// Byte Representation of the type used in Variant to identify it +pub const QDATE: u32 = 0x0000000e; +/// Byte Representation of the type used in Variant to identify it +pub const QTIME: u32 = 0x0000000f; +/// Byte Representation of the type used in Variant to identify it +pub const QDATETIME: u32 = 0x00000010; +/// Byte Representation of the type used in Variant to identify it +pub const USERTYPE: u32 = 0x0000007f; + +// Basic types +/// Byte Representation of the type used in Variant to identify it +pub const LONG: u32 = 0x00000081; // int64_t +/// Byte Representation of the type used in Variant to identify it +pub const INT: u32 = 0x00000002; // int32_t +/// Byte Representation of the type used in Variant to identify it +pub const SHORT: u32 = 0x00000082; // int16_t +/// Byte Representation of the type used in Variant to identify it +pub const CHAR: u32 = 0x00000083; // int8_t + +/// Byte Representation of the type used in Variant to identify it +pub const ULONG: u32 = 0x00000084; // uint64_t +/// Byte Representation of the type used in Variant to identify it +pub const UINT: u32 = 0x00000003; // uint32_t +/// Byte Representation of the type used in Variant to identify it +pub const USHORT: u32 = 0x00000085; // uint16_t +/// Byte Representation of the type used in Variant to identify it +pub const UCHAR: u32 = 0x00000086; // uint8_t diff --git a/src/primitive/signedint.rs b/src/primitive/signedint.rs new file mode 100644 index 0000000..4c21a69 --- /dev/null +++ b/src/primitive/signedint.rs @@ -0,0 +1,62 @@ +extern crate byteorder; +use byteorder::{BigEndian, ReadBytesExt}; +use std::io::Cursor; + +use std::convert::TryInto; +use std::result::Result; +use std::vec::Vec; + +use failure::Error; + +use crate::{Deserialize, Serialize}; + +impl Serialize for i64 { + fn serialize(&self) -> Result, Error> { + Ok(Vec::from(self.to_be_bytes())) + } +} + +impl Deserialize for i64 { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let mut rdr = Cursor::new(&b[0..8]); + return Ok((8, rdr.read_i64::()?)); + } +} + +impl Serialize for i32 { + fn serialize(&self) -> Result, Error> { + Ok(Vec::from(self.to_be_bytes())) + } +} + +impl Deserialize for i32 { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let mut rdr = Cursor::new(&b[0..4]); + return Ok((4, rdr.read_i32::()?)); + } +} + +impl Serialize for i16 { + fn serialize(&self) -> Result, Error> { + Ok(Vec::from(self.to_be_bytes())) + } +} + +impl Deserialize for i16 { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let mut rdr = Cursor::new(&b[0..2]); + return Ok((2, rdr.read_i16::()?)); + } +} + +impl Serialize for i8 { + fn serialize(&self) -> Result, Error> { + Ok(Vec::from(self.to_be_bytes())) + } +} + +impl Deserialize for i8 { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + return Ok((1, b[0].try_into()?)); + } +} diff --git a/src/primitive/string.rs b/src/primitive/string.rs new file mode 100644 index 0000000..86bcdec --- /dev/null +++ b/src/primitive/string.rs @@ -0,0 +1,94 @@ +extern crate byteorder; + +use std::result::Result; +use std::vec::Vec; + +use failure::Error; + +use log::trace; + +use crate::{Deserialize, DeserializeUTF8, Serialize, SerializeUTF8}; +use crate::util; + +/// We Shadow the String type here as we can only use impl on types in our own scope. +/// +/// Strings are serialized as an i32 for the length in bytes, then the chars represented in UTF-16 in bytes. +/// +/// Strings can only be serialized as UTF-8 null-terminated ByteArrays with (de)serialize_utf8(). +impl Serialize for String { + fn serialize(&self) -> Result, Error> { + let mut res: Vec = Vec::new(); + + let utf16: Vec = self.encode_utf16().collect(); + for i in utf16 { + res.extend(i.to_be_bytes().iter()); + } + + util::prepend_byte_len(&mut res); + return Ok(res); + } +} + +impl SerializeUTF8 for String { + fn serialize_utf8(&self) -> Result, Error> { + let mut res: Vec = Vec::new(); + res.extend(self.clone().into_bytes()); + res.extend(vec![0x00]); + util::prepend_byte_len(&mut res); + return Ok(res); + } +} + +impl Deserialize for String { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + // Parse Length + let (_, len) = i32::parse(&b[0..4])?; + trace!(target: "primitive::String", "Parsing with length: {:?}, from bytes: {:x?}", len, &b[0..4]); + + if len == -1 { + return Ok((4, "".to_string())); + } + + // length as usize + let ulen = len as usize; + let mut pos: usize = 4; + let mut chars: Vec = Vec::new(); + loop { + // if position is behind the length plus our 4 bytes of the length we already parsed + if pos >= (ulen + 4) { + break; + } + let (slen, uchar) = u16::parse(&b[pos..(pos + 2)])?; + chars.push(uchar); + pos += slen; + } + + let res: String = String::from_utf16(&chars).unwrap(); + return Ok((pos, res)); + } +} + +impl DeserializeUTF8 for String { + fn parse_utf8(b: &[u8]) -> Result<(usize, Self), Error> { + let (_, len) = i32::parse(&b[0..4])?; + + trace!(target: "primitive::String", "Parsing with length: {:?}, from bytes: {:x?}", len, &b[0..4]); + + if len <= 0 { + return Ok((4, "".to_string())); + } + + let ulen = len as usize; + + let mut res: String = String::from_utf8(b[4..(ulen + 4)].to_vec())?; + + // If the last byte is zero remove it + // Receiving a string as bytearray will sometimes have + // the string null terminated + if res.chars().last().unwrap() == '\u{0}' { + let _ = res.pop(); + } + + return Ok((ulen + 4, res)); + } +} diff --git a/src/primitive/stringlist.rs b/src/primitive/stringlist.rs new file mode 100644 index 0000000..e5d1a44 --- /dev/null +++ b/src/primitive/stringlist.rs @@ -0,0 +1,49 @@ +extern crate byteorder; + +use std::convert::TryInto; +use std::result::Result; +use std::vec::Vec; + +use failure::Error; + +use log::trace; + +use crate::{Deserialize, Serialize}; + +/// StringList are represented as a Vec of Strings +/// +/// StringLists are serialized as an i32 of the amount of elements and then each element as a String +pub type StringList = Vec; + +impl Serialize for StringList { + fn serialize(&self) -> Result, Error> { + let len: i32 = self.len().try_into()?; + let mut res: Vec = Vec::new(); + + res.extend(len.to_be_bytes().iter()); + for x in self { + res.extend(x.serialize()?); + } + + return Ok(res); + } +} + +impl Deserialize for StringList { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let (_, len) = i32::parse(&b[0..4])?; + trace!(target: "primitive::StringList", "Parsing with length: {:?}, from bytes: {:x?}", len, &b[0..4]); + let mut res: StringList = StringList::new(); + + let mut pos = 4; + if len > 0 { + for _ in 0..len { + let (lpos, val) = String::parse(&b[pos..])?; + pos += lpos; + res.push(val); + } + } + + return Ok((pos, res)); + } +} diff --git a/src/primitive/unsignedint.rs b/src/primitive/unsignedint.rs new file mode 100644 index 0000000..6e91e2a --- /dev/null +++ b/src/primitive/unsignedint.rs @@ -0,0 +1,81 @@ +extern crate byteorder; +use byteorder::{BigEndian, ReadBytesExt}; +use std::io::Cursor; + +use std::result::Result; +use std::vec::Vec; + +use failure::Error; + +use crate::error::ProtocolError; +use crate::{Deserialize, Serialize}; + +impl Serialize for bool { + fn serialize(&self) -> Result, Error> { + Ok({ + let i = *self as i8; + Vec::from(i.to_be_bytes()) + }) + } +} +impl Deserialize for bool { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + if b[0] == 0 { + return Ok((1, false)); + } else if b[0] == 1 { + return Ok((1, true)); + } else { + bail!(ProtocolError::BoolOutOfRange); + }; + } +} +impl Serialize for u64 { + fn serialize(&self) -> Result, Error> { + Ok(Vec::from(self.to_be_bytes())) + } +} + +impl Deserialize for u64 { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let mut rdr = Cursor::new(&b[0..8]); + return Ok((8, rdr.read_u64::()?)); + } +} + +impl Serialize for u32 { + fn serialize(&self) -> Result, Error> { + Ok(Vec::from(self.to_be_bytes())) + } +} + +impl Deserialize for u32 { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let mut rdr = Cursor::new(&b[0..4]); + return Ok((4, rdr.read_u32::()?)); + } +} + +impl Serialize for u16 { + fn serialize(&self) -> Result, Error> { + Ok(Vec::from(self.to_be_bytes())) + } +} + +impl Deserialize for u16 { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let mut rdr = Cursor::new(&b[0..2]); + return Ok((2, rdr.read_u16::()?)); + } +} + +impl Serialize for u8 { + fn serialize(&self) -> Result, Error> { + Ok(Vec::from(self.to_be_bytes())) + } +} + +impl Deserialize for u8 { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + return Ok((1, b[0])); + } +} diff --git a/src/primitive/variant.rs b/src/primitive/variant.rs new file mode 100644 index 0000000..71ddc4a --- /dev/null +++ b/src/primitive/variant.rs @@ -0,0 +1,304 @@ +use std::vec::Vec; + +use failure::Error; + +use log::{error, trace}; + +use crate::error::ProtocolError; +use crate::primitive; +use crate::primitive::StringList; +use crate::{Deserialize, DeserializeUTF8}; +use crate::{Serialize, SerializeUTF8}; + +extern crate bytes; + +use crate::primitive::{ + BufferInfo, Date, DateTime, Message, Time, VariantList, VariantMap, +}; + +/// Variant represents the possible types we can receive +/// +/// Variant's are serizalized as the Type as a i32 and then the Type in it's own format +/// +/// BufferInfo and Message are UserTypes +/// but we represent them as a native Type here. +/// +/// StringUTF8 is de-/serialized as a C ByteArray. +#[allow(non_camel_case_types, dead_code)] +#[derive(Clone, Debug, std::cmp::PartialEq)] +pub enum Variant { + Unknown, + UserType(String, Vec), + BufferInfo(BufferInfo), + Message(Message), + Time(Time), + Date(Date), + DateTime(DateTime), + VariantMap(VariantMap), + VariantList(VariantList), + String(String), + StringUTF8(String), + StringList(StringList), + bool(bool), + u64(u64), + u32(u32), + u16(u16), + u8(u8), + i64(i64), + i32(i32), + i16(i16), + i8(i8), +} + +impl Serialize for Variant { + fn serialize(&self) -> Result, Error> { + let unknown: u8 = 0x00; + let mut res: Vec = Vec::new(); + + match self { + Variant::Unknown => { + bail!(ProtocolError::UnknownVariant); + } + Variant::VariantMap(v) => { + res.extend(primitive::QVARIANTMAP.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.extend(v.serialize()?.iter()); + } + Variant::VariantList(v) => { + res.extend(primitive::QVARIANTLIST.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.extend(v.serialize()?.iter()); + } + Variant::String(v) => { + res.extend(primitive::QSTRING.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.extend(v.serialize()?.iter()); + } + Variant::StringUTF8(v) => { + res.extend(primitive::QBYTEARRAY.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.extend(v.serialize_utf8()?.iter()); + } + Variant::StringList(v) => { + res.extend(primitive::QSTRINGLIST.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.extend(v.serialize()?.iter()); + } + Variant::bool(v) => { + res.extend(primitive::BOOL.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + let i = *v as i8; + res.extend(i.to_be_bytes().iter()); + } + Variant::u64(v) => { + res.extend(primitive::ULONG.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.extend(v.to_be_bytes().iter()); + } + Variant::u32(v) => { + res.extend(primitive::UINT.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.extend(v.to_be_bytes().iter()); + } + Variant::u16(v) => { + res.extend(primitive::USHORT.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.extend(v.to_be_bytes().iter()); + } + Variant::u8(v) => { + res.extend(primitive::UCHAR.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.extend(v.to_be_bytes().iter()); + } + Variant::i64(v) => { + res.extend(primitive::LONG.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.extend(v.to_be_bytes().iter()); + } + Variant::i32(v) => { + res.extend(primitive::INT.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.extend(v.to_be_bytes().iter()); + } + Variant::i16(v) => { + res.extend(primitive::SHORT.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.extend(v.to_be_bytes().iter()); + } + Variant::i8(v) => { + res.extend(primitive::CHAR.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.extend(v.to_be_bytes().iter()); + } + Variant::UserType(name, bytes) => { + res.extend(primitive::USERTYPE.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.append(&mut name.serialize_utf8()?); + res.extend(bytes); + } + Variant::BufferInfo(v) => { + let bytes = BufferInfo::serialize(v)?; + let user = Variant::UserType("BufferInfo".to_string(), bytes); + Variant::serialize(&user).unwrap(); + } + Variant::Message(v) => { + let bytes = Message::serialize(v)?; + let user = Variant::UserType("Message".to_string(), bytes); + Variant::serialize(&user).unwrap(); + } + Variant::DateTime(v) => { + res.extend(primitive::QDATETIME.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.append(&mut v.serialize()?); + } + Variant::Time(v) => { + res.extend(primitive::QTIME.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.append(&mut v.serialize()?); + } + Variant::Date(v) => { + res.extend(primitive::QDATE.to_be_bytes().iter()); + res.extend(unknown.to_be_bytes().iter()); + res.append(&mut v.serialize()?); + } + } + + return Ok(res); + } +} + +impl Deserialize for Variant { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let (_, qtype) = i32::parse(&b[0..4])?; + let qtype = qtype as u32; + + #[allow(unused_variables)] + let unknown: u8 = b[4]; + + let len = 5; + match qtype { + primitive::QVARIANTMAP => { + trace!(target: "primitive::Variant", "Parsing Variant: VariantMap"); + let (vlen, value) = VariantMap::parse(&b[len..])?; + return Ok((len + vlen, Variant::VariantMap(value))); + } + primitive::QVARIANTLIST => { + trace!(target: "primitive::Variant", "Parsing Variant: VariantList"); + let (vlen, value) = VariantList::parse(&b[len..])?; + return Ok((len + vlen, Variant::VariantList(value))); + } + primitive::QSTRING => { + trace!(target: "primitive::Variant", "Parsing Variant: String"); + let (vlen, value) = String::parse(&b[len..])?; + return Ok((len + vlen, Variant::String(value.clone()))); + } + primitive::QBYTEARRAY => { + trace!(target: "primitive::Variant", "Parsing Variant: ByteArray"); + let (vlen, value) = String::parse_utf8(&b[len..])?; + return Ok((len + vlen, Variant::StringUTF8(value.clone()))); + } + primitive::QSTRINGLIST => { + trace!(target: "primitive::Variant", "Parsing Variant: StringList"); + let (vlen, value) = StringList::parse(&b[len..])?; + return Ok((len + vlen, Variant::StringList(value.clone()))); + } + primitive::QDATETIME => { + trace!(target: "primitive::Variant", "Parsing Variant: Date"); + let (vlen, value) = Date::parse(&b[len..])?; + return Ok((len + vlen, Variant::Date(value.clone()))); + } + primitive::QDATE => { + trace!(target: "primitive::Variant", "Parsing Variant: Date"); + let (vlen, value) = Date::parse(&b[len..])?; + return Ok((len + vlen, Variant::Date(value.clone()))); + } + primitive::QTIME => { + trace!(target: "primitive::Variant", "Parsing Variant: Time"); + let (vlen, value) = Time::parse(&b[len..])?; + return Ok((len + vlen, Variant::Time(value.clone()))); + } + primitive::BOOL => { + let (vlen, value) = bool::parse(&b[len..])?; + return Ok((len + vlen, Variant::bool(value))); + } + primitive::ULONG => { + let (vlen, value) = u64::parse(&b[len..])?; + return Ok((len + vlen, Variant::u64(value))); + } + primitive::UINT => { + let (vlen, value) = u32::parse(&b[len..])?; + return Ok((len + vlen, Variant::u32(value))); + } + primitive::USHORT => { + let (vlen, value) = u16::parse(&b[len..])?; + return Ok((len + vlen, Variant::u16(value))); + } + primitive::UCHAR => { + let (vlen, value) = u8::parse(&b[len..])?; + return Ok((len + vlen, Variant::u8(value))); + } + primitive::LONG => { + let (vlen, value) = i64::parse(&b[len..])?; + return Ok((len + vlen, Variant::i64(value))); + } + primitive::INT => { + let (vlen, value) = i32::parse(&b[len..])?; + return Ok((len + vlen, Variant::i32(value))); + } + primitive::SHORT => { + let (vlen, value) = i16::parse(&b[len..])?; + return Ok((len + vlen, Variant::i16(value))); + } + primitive::CHAR => { + let (vlen, value) = i8::parse(&b[len..])?; + return Ok((len + vlen, Variant::i8(value))); + } + primitive::USERTYPE => { + trace!(target: "primitive::Variant", "Parsing UserType"); + // Parse UserType name + let (user_type_len, user_type) = String::parse_utf8(&b[len..])?; + + trace!(target: "primitive::Variant", "Parsing UserType: {:?}", user_type); + + // TODO implement all these types + // Match Possible User Types to basic structures + match user_type.as_str() { + // As VariantMap + "IrcUser" | "IrcChannel" | "Identity" | "NetworkInfo" | "Network::Server" => { + trace!(target: "primitive::Variant", "UserType is VariantMap"); + let (vlen, value) = VariantMap::parse(&b[(len + user_type_len)..])?; + return Ok((len + user_type_len + vlen, Variant::VariantMap(value))); + } + // As i32 + "BufferId" | "IdentityId" | "NetworkId" | "MsgId" => { + trace!(target: "primitive::Variant", "UserType is i32"); + + let (vlen, value) = i32::parse(&b[(len + user_type_len)..])?; + return Ok((len + user_type_len + vlen, Variant::i32(value))); + } + // As i64 + "PeerPtr" => { + trace!(target: "primitive::Variant", "UserType is i64"); + let (vlen, value) = i64::parse(&b[(len + user_type_len)..])?; + return Ok((len + user_type_len + vlen, Variant::i64(value))); + } + "BufferInfo" => { + trace!(target: "primitive::Variant", "UserType is BufferInfo"); + let (vlen, value) = BufferInfo::parse(&b[(len + user_type_len)..])?; + return Ok((len + user_type_len + vlen, Variant::BufferInfo(value))); + } + "Message" => { + trace!(target: "primitive::Variant", "UserType is Message"); + let (vlen, value) = Message::parse(&b[(len + user_type_len)..])?; + return Ok((len + user_type_len + vlen, Variant::Message(value))); + } + _ => unimplemented!(), + } + } + err => { + error!(target: "parser", "UnknownVariant: {:x?}", err); + bail!(ProtocolError::UnknownVariant); + } + } + } +} diff --git a/src/primitive/variantlist.rs b/src/primitive/variantlist.rs new file mode 100644 index 0000000..452b927 --- /dev/null +++ b/src/primitive/variantlist.rs @@ -0,0 +1,49 @@ +use std::convert::TryInto; +use std::vec::Vec; + +use failure::Error; + +use log::trace; + +use crate::{Deserialize, Serialize}; + +extern crate bytes; + +use crate::primitive::Variant; + +/// VariantLists are represented as a Vec of Variants. +/// +/// They are serialized as the amount of entries as a i32 and then a Variant for each entry +pub type VariantList = Vec; + +impl Serialize for VariantList { + fn serialize(&self) -> Result, Error> { + let len: i32 = self.len().try_into()?; + let mut res: Vec = Vec::new(); + + res.extend(len.to_be_bytes().iter()); + for v in self { + res.extend(v.serialize()?.iter()); + } + + return Ok(res); + } +} + +impl Deserialize for VariantList { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let (_, len) = i32::parse(&b[0..4])?; + trace!(target: "primitive::VariantList", "Parsing VariantList with {:?} elements", len); + + let mut res: VariantList = VariantList::new(); + let mut pos: usize = 4; + for i in 0..len { + trace!(target: "primitive::VariantList", "Parsing VariantList element: {:?}", i); + let (vlen, val) = Variant::parse(&b[pos..])?; + res.push(val); + pos += vlen; + } + + return Ok((pos, res)); + } +} diff --git a/src/primitive/variantmap.rs b/src/primitive/variantmap.rs new file mode 100644 index 0000000..4f017f3 --- /dev/null +++ b/src/primitive/variantmap.rs @@ -0,0 +1,58 @@ +use std::collections::HashMap; +use std::{convert::TryInto, vec::Vec}; + +use failure::Error; + +use log::trace; + +use crate::Deserialize; +use crate::Serialize; + +use crate::primitive::Variant; +use crate::util; + +extern crate bytes; + +/// VariantMaps are represented as a HashMap with String as key and Variant as value +/// +/// They are serialized as the amount of keys as an i32 then for each entry a String and a Variant. +pub type VariantMap = HashMap; + +impl Serialize for VariantMap { + fn serialize<'a>(&'a self) -> Result, Error> { + let mut res: Vec = Vec::new(); + + for (k, v) in self { + res.extend(k.serialize()?); + res.extend(v.serialize()?); + } + + let len: i32 = self.len().try_into()?; + util::insert_bytes(0, &mut res, &mut len.to_be_bytes()); + + return Ok(res); + } +} + +impl Deserialize for VariantMap { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + let (_, len) = i32::parse(&b[0..4])?; + trace!(target: "primitive::VariantMap", "Parsing VariantMap with {:?} elements", len); + + let mut pos: usize = 4; + let mut map = VariantMap::new(); + for _ in 0..len { + trace!(target: "primitive::VariantMap", "Parsing entry name"); + let (nlen, name) = String::parse(&b[pos..])?; + pos += nlen; + + trace!(target: "primitive::VariantMap", "Parsing entry: {:?} with len {:?}", name, &b[(pos)..(pos + 4)]); + let (vlen, value) = Variant::parse(&b[(pos)..])?; + pos += vlen; + + map.insert(name, value); + } + + return Ok((pos, map)); + } +} diff --git a/src/protocol/error/mod.rs b/src/protocol/error/mod.rs deleted file mode 100644 index 72a9e59..0000000 --- a/src/protocol/error/mod.rs +++ /dev/null @@ -1,37 +0,0 @@ - #[derive(Debug, Fail)] -pub enum ProtocolError { - #[fail(display = "message has wrong type")] - WrongMsgType, - #[fail(display = "bool value is neither 0 nor 1")] - BoolOutOfRange, - #[fail(display = "QVariant is not known")] - UnknownVariant, - #[fail(display = "wrong variant has been given")] - WrongVariant, - #[fail(display = "io error")] - IOError(std::io::Error), - #[fail(display = "could not convert from int")] - TryFromIntError(std::num::TryFromIntError), - #[fail(display = "utf8 error")] - Utf8Error(std::string::FromUtf8Error), - } - -// impl std::error::Error for ErrorKind {} -// -// impl std::convert::From for ErrorKind { -// fn from(error: std::io::Error) -> Self { -// ErrorKind::IOError(error) -// } -// } -// -// impl std::convert::From for ErrorKind { -// fn from(error: std::num::TryFromIntError) -> Self { -// ErrorKind::TryFromIntError(error) -// } -// } -// -// impl std::convert::From for ErrorKind { -// fn from(error: std::string::FromUtf8Error) -> Self { -// ErrorKind::Utf8Error(error) -// } -// } diff --git a/src/protocol/frame/mod.rs b/src/protocol/frame/mod.rs deleted file mode 100644 index 8c5a8d3..0000000 --- a/src/protocol/frame/mod.rs +++ /dev/null @@ -1,402 +0,0 @@ -use std::convert::TryInto; -use std::error::Error as StdError; -use std::fmt; -use std::io::{self, Cursor}; - -use bytes::{Buf, BufMut, BytesMut}; - -use tokio::io::{AsyncRead, AsyncWrite}; - -use tokio_util::codec::{Decoder, Encoder, Framed, FramedRead, FramedWrite}; - -use flate2::Compress; -use flate2::Compression; -use flate2::Decompress; -use flate2::FlushCompress; -use flate2::FlushDecompress; - -#[derive(Debug, Clone, Copy)] -pub struct Builder { - // Maximum frame length - compression: bool, - compression_level: Compression, - - // Maximum frame length - max_frame_len: usize, -} - -// An error when the number of bytes read is more than max frame length. -pub struct QuasselCodecError { - _priv: (), -} - -#[derive(Debug)] -pub struct QuasselCodec { - builder: Builder, - state: DecodeState, - comp: Compress, - decomp: Decompress, -} - -#[derive(Debug, Clone, Copy)] -enum DecodeState { - Head, - Data(usize), -} - -impl QuasselCodec { - // Creates a new quassel codec with default values - pub fn new() -> Self { - Self { - builder: Builder::new(), - state: DecodeState::Head, - comp: Compress::new(Compression::default(), true), - decomp: Decompress::new(true), - } - } - - /// Creates a new quassel codec builder with default configuration - /// values. - pub fn builder() -> Builder { - Builder::new() - } - - pub fn max_frame_length(&self) -> usize { - self.builder.max_frame_len - } - - pub fn compression(&self) -> bool { - self.builder.compression - } - - pub fn compression_level(&self) -> Compression { - self.builder.compression_level - } - - pub fn set_compression(&mut self, val: bool) { - self.builder.compression(val); - } - - pub fn set_compression_level(&mut self, val: Compression) { - self.builder.compression_level(val); - } - - fn decode_head(&mut self, src: &mut BytesMut) -> io::Result> { - let head_len = 4; - - if src.len() < head_len { - // Not enough data - return Ok(None); - } - - let field_len = { - let mut src = Cursor::new(&mut *src); - - let field_len = src.get_uint(head_len); - - if field_len > self.builder.max_frame_len as u64 { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - QuasselCodecError { _priv: () }, - )); - } - - // The check above ensures there is no overflow - field_len as usize - }; - - // Strip header - let _ = src.split_to(head_len); - - // Ensure that the buffer has enough space to read the incoming - // payload - src.reserve(field_len); - - Ok(Some(field_len)) - } - - fn decode_data(&self, n: usize, src: &mut BytesMut) -> io::Result> { - // At this point, the buffer has already had the required capacity - // reserved. All there is to do is read. - if src.len() < n { - return Ok(None); - } - - Ok(Some(src.split_to(n))) - } -} - -impl Decoder for QuasselCodec { - type Item = BytesMut; - type Error = io::Error; - - fn decode(&mut self, src: &mut BytesMut) -> Result, io::Error> { - // Create Unified Buffer for compressed and not compressed datastream - let mut buf: &mut BytesMut = &mut BytesMut::new(); - - if self.builder.compression == true { - // Buffer to shove uncompressed stream into - let mut msg = Vec::with_capacity(self.builder.max_frame_len); - - let before_in = self.decomp.total_in(); - let before_out = self.decomp.total_out(); - - self.decomp - .decompress_vec(&src, &mut msg, FlushDecompress::None)?; - // Clear the src buffer, decompress() only peeks at content. - // without this we will endlessly loop over the same frame. - src.clear(); - - let after_in = self.decomp.total_in(); - let after_out = self.decomp.total_out(); - - let len = (after_out - before_out).try_into().unwrap(); - - // Reserve length of uncompressed stream - // and put bytes into there - buf.reserve(len); - buf.put(&msg[..]); - } else { - buf = src; - } - - let n = match self.state { - DecodeState::Head => match self.decode_head(buf)? { - Some(n) => { - self.state = DecodeState::Data(n); - n - } - None => return Ok(None), - }, - DecodeState::Data(n) => n, - }; - - match self.decode_data(n, buf)? { - Some(data) => { - // Update the decode state - self.state = DecodeState::Head; - - // Make sure the buffer has enough space to read the next head - buf.reserve(4); - - Ok(Some(data)) - } - None => Ok(None), - } - } -} - -impl Encoder for QuasselCodec { - type Item = Vec; - type Error = io::Error; - - fn encode(&mut self, data: Vec, dst: &mut BytesMut) -> Result<(), io::Error> { - let buf = &mut BytesMut::new(); - - let n = (&data).len(); - - if n > self.builder.max_frame_len { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - QuasselCodecError { _priv: () }, - )); - } - - // Reserve capacity in the destination buffer to fit the frame and - // length field (plus adjustment). - buf.reserve(4 + n); - - buf.put_uint(n as u64, 4); - - // Write the frame to the buffer - buf.extend_from_slice(&data[..]); - - if self.builder.compression { - let mut cbuf: Vec = vec![0; 4 + n]; - let before_in = self.comp.total_in(); - let before_out = self.comp.total_out(); - self.comp.compress(buf, &mut cbuf, FlushCompress::Full)?; - let after_in = self.comp.total_in(); - let after_out = self.comp.total_out(); - - cbuf.truncate((after_out - before_out).try_into().unwrap()); - *dst = BytesMut::from(&cbuf[..]); - } else { - *dst = buf.clone(); - } - - Ok(()) - } -} - -impl Default for QuasselCodec { - fn default() -> Self { - Self::new() - } -} - -// ===== impl Builder ===== - -impl Builder { - /// Creates a new length delimited codec builder with default configuration - /// values. - /// - /// # Examples - /// - /// ``` - /// # use tokio::io::AsyncRead; - /// use libquassel::protocol::frame::QuasselCodec; - /// - /// # fn bind_read(io: T) { - /// QuasselCodec::builder() - /// .new_read(io); - /// # } - /// # pub fn main() {} - /// ``` - pub fn new() -> Builder { - Builder { - compression: false, - compression_level: Compression::default(), - max_frame_len: 64 * 1024 * 1024, - } - } - - pub fn compression(&mut self, val: bool) -> &mut Self { - self.compression = val; - self - } - - pub fn compression_level(&mut self, val: Compression) -> &mut Self { - self.compression_level = val; - self - } - - /// Sets the max frame length - /// - /// This configuration option applies to both encoding and decoding. The - /// default value is 8MB. - /// - /// When decoding, the length field read from the byte stream is checked - /// against this setting **before** any adjustments are applied. When - /// encoding, the length of the submitted payload is checked against this - /// setting. - /// - /// When frames exceed the max length, an `io::Error` with the custom value - /// of the `QuasselCodecError` type will be returned. - /// - /// # Examples - /// - /// ``` - /// # use tokio::io::AsyncRead; - /// use libquassel::protocol::frame::QuasselCodec; - /// - /// # fn bind_read(io: T) { - /// QuasselCodec::builder() - /// .max_frame_length(8 * 1024) - /// .new_read(io); - /// # } - /// # pub fn main() {} - /// ``` - pub fn max_frame_length(&mut self, val: usize) -> &mut Self { - self.max_frame_len = val; - self - } - - /// Create a configured length delimited `QuasselCodec` - /// - /// # Examples - /// - /// ``` - /// use libquassel::protocol::frame::QuasselCodec; - /// # pub fn main() { - /// QuasselCodec::builder() - /// .new_codec(); - /// # } - /// ``` - pub fn new_codec(&self) -> QuasselCodec { - QuasselCodec { - builder: *self, - state: DecodeState::Head, - comp: Compress::new(self.compression_level, true), - decomp: Decompress::new(true), - } - } - - /// Create a configured length delimited `FramedRead` - /// - /// # Examples - /// - /// ``` - /// # use tokio::io::AsyncRead; - /// use libquassel::protocol::frame::QuasselCodec; - /// - /// # fn bind_read(io: T) { - /// QuasselCodec::builder() - /// .new_read(io); - /// # } - /// # pub fn main() {} - /// ``` - pub fn new_read(&self, upstream: T) -> FramedRead - where - T: AsyncRead, - { - FramedRead::new(upstream, self.new_codec()) - } - - /// Create a configured length delimited `FramedWrite` - /// - /// # Examples - /// - /// ``` - /// # use tokio::io::AsyncWrite; - /// # use libquassel::protocol::frame::QuasselCodec; - /// # fn write_frame(io: T) { - /// QuasselCodec::builder() - /// .new_write(io); - /// # } - /// # pub fn main() {} - /// ``` - pub fn new_write(&self, inner: T) -> FramedWrite - where - T: AsyncWrite, - { - FramedWrite::new(inner, self.new_codec()) - } - - /// Create a configured length delimited `Framed` - /// - /// # Examples - /// - /// ``` - /// # use tokio::io::{AsyncRead, AsyncWrite}; - /// # use libquassel::protocol::frame::QuasselCodec; - /// # fn write_frame(io: T) { - /// # let _ = - /// QuasselCodec::builder() - /// .new_framed(io); - /// # } - /// # pub fn main() {} - /// ``` - pub fn new_framed(&self, inner: T) -> Framed - where - T: AsyncRead + AsyncWrite, - { - Framed::new(inner, self.new_codec()) - } -} - -// ===== impl LengthDelimitedCodecError ===== - -impl fmt::Debug for QuasselCodecError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("QuasselCodecError").finish() - } -} - -impl fmt::Display for QuasselCodecError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("frame size too big") - } -} - -impl StdError for QuasselCodecError {} diff --git a/src/protocol/message/handshake.rs b/src/protocol/message/handshake.rs deleted file mode 100644 index 357d1a4..0000000 --- a/src/protocol/message/handshake.rs +++ /dev/null @@ -1,352 +0,0 @@ -use failure::Error; -use std::result::Result; - -use crate::protocol::error::ProtocolError; -use crate::protocol::primitive::{String, StringList, Variant, VariantList}; -use crate::util::get_msg_type; - -mod types; -pub use types::{HandshakeDeserialize, HandshakeSerialize, VariantMap}; - -use crate::match_variant; - -#[derive(Debug)] -pub struct ConnAck { - flags: u8, - extra: i16, - version: i8, -} - -impl crate::protocol::primitive::deserialize::Deserialize for ConnAck { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let (flen, flags) = u8::parse(b)?; - let (elen, extra) = i16::parse(&b[flen..])?; - let (vlen, version) = i8::parse(&b[(flen + elen)..])?; - - return Ok(( - flen + elen + vlen, - Self { - flags, - extra, - version, - }, - )); - } -} - -#[derive(Debug)] -pub struct ClientInit { - pub client_version: String, // Version of the client - pub client_date: String, // Build date of the client - pub client_features: u32, - pub feature_list: StringList, // List of supported extended features -} - -impl HandshakeSerialize for ClientInit { - fn serialize(&self) -> Result, Error> { - let mut values: VariantMap = VariantMap::with_capacity(5); - values.insert( - "MsgType".to_string(), - Variant::String("ClientInit".to_string()), - ); - values.insert( - "ClientVersion".to_string(), - Variant::String(self.client_version.clone()), - ); - values.insert( - "ClientDate".to_string(), - Variant::String(self.client_date.clone()), - ); - values.insert("Features".to_string(), Variant::u32(self.client_features)); - values.insert( - "FeatureList".to_string(), - Variant::StringList(self.feature_list.clone()), - ); - return HandshakeSerialize::serialize(&values); - } -} - -impl HandshakeDeserialize for ClientInit { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; - - let msgtype = get_msg_type(&values["MsgType"])?; - - if msgtype == "ClientInit" { - return Ok(( - len, - Self { - client_version: match_variant!(values, Variant::String, "ClientVersion"), - client_date: match_variant!(values, Variant::String, "ClientDate"), - feature_list: match_variant!(values, Variant::StringList, "FeatureList"), - client_features: match_variant!(values, Variant::u32, "Features"), - }, - )); - } else { - bail!(ProtocolError::WrongMsgType); - } - } -} - -#[derive(Debug)] -pub struct ClientInitReject { - pub error_string: String, -} - -impl HandshakeSerialize for ClientInitReject { - fn serialize(&self) -> Result, Error> { - let mut values: VariantMap = VariantMap::with_capacity(2); - values.insert( - "MsgType".to_string(), - Variant::String("ClientInitReject".to_string()), - ); - values.insert( - "ErrorString".to_string(), - Variant::String(self.error_string.clone()), - ); - return HandshakeSerialize::serialize(&values); - } -} - -impl HandshakeDeserialize for ClientInitReject { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; - - let msgtype = get_msg_type(&values["MsgType"])?; - - if msgtype == "ClientInitReject" { - return Ok(( - len, - Self { - error_string: match_variant!(values, Variant::String, "ErrorString"), - }, - )); - } else { - bail!(ProtocolError::WrongMsgType); - } - } -} - -#[derive(Debug)] -pub struct ClientInitAck { - pub core_features: u32, // Flags of supported legacy features - pub core_configured: bool, // If the core has already been configured - pub storage_backends: VariantList, // List of VariantMaps of info on available backends - pub authenticators: VariantList, // List of VariantMaps of info on available authenticators - pub feature_list: StringList, // List of supported extended features -} - -impl HandshakeSerialize for ClientInitAck { - fn serialize(&self) -> Result, Error> { - let mut values: VariantMap = VariantMap::with_capacity(6); - values.insert( - "MsgType".to_string(), - Variant::String("ClientInitAck".to_string()), - ); - values.insert("CoreFeatures".to_string(), Variant::u32(self.core_features)); - values.insert( - "Configured".to_string(), - Variant::bool(self.core_configured), - ); - values.insert( - "StorageBackends".to_string(), - Variant::VariantList(self.storage_backends.clone()), - ); - values.insert( - "Authenticators".to_string(), - Variant::VariantList(self.authenticators.clone()), - ); - values.insert( - "FeatureList".to_string(), - Variant::StringList(self.feature_list.clone()), - ); - return HandshakeSerialize::serialize(&values); - } -} - -impl HandshakeDeserialize for ClientInitAck { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; - - let msgtype = get_msg_type(&values["MsgType"])?; - - if msgtype == "ClientInitAck" { - return Ok(( - len, - Self { - core_features: 0x00008000, - core_configured: match_variant!(values, Variant::bool, "Configured"), - storage_backends: match_variant!( - values, - Variant::VariantList, - "StorageBackends" - ), - authenticators: match_variant!(values, Variant::VariantList, "Authenticators"), - feature_list: match_variant!(values, Variant::StringList, "FeatureList"), - }, - )); - } else { - bail!(ProtocolError::WrongMsgType); - } - } -} - -#[derive(Debug)] -pub struct ClientLogin { - pub user: String, - pub password: String, -} - -impl HandshakeSerialize for ClientLogin { - fn serialize(&self) -> Result, Error> { - let mut values: VariantMap = VariantMap::new(); - values.insert( - "MsgType".to_string(), - Variant::String("ClientLogin".to_string()), - ); - values.insert("User".to_string(), Variant::String(self.user.clone())); - values.insert( - "Password".to_string(), - Variant::String(self.password.clone()), - ); - return HandshakeSerialize::serialize(&values); - } -} - -impl HandshakeDeserialize for ClientLogin { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; - - let msgtype = get_msg_type(&values["MsgType"])?; - - if msgtype == "ClientLogin" { - return Ok(( - len, - Self { - user: match_variant!(values, Variant::String, "User"), - password: match_variant!(values, Variant::String, "Password"), - }, - )); - } else { - bail!(ProtocolError::WrongMsgType); - } - } -} - -#[derive(Debug)] -pub struct ClientLoginAck; - -impl HandshakeSerialize for ClientLoginAck { - fn serialize(&self) -> Result, Error> { - let mut values: VariantMap = VariantMap::with_capacity(1); - values.insert( - "MsgType".to_string(), - Variant::String("ClientLoginAck".to_string()), - ); - return HandshakeSerialize::serialize(&values); - } -} - -impl HandshakeDeserialize for ClientLoginAck { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; - - let msgtype = get_msg_type(&values["MsgType"])?; - - if msgtype == "ClientLogin" { - return Ok((len, Self {})); - } else { - bail!(ProtocolError::WrongMsgType); - } - } -} - -#[derive(Debug)] -pub struct ClientLoginReject { - error: String, -} - -impl HandshakeSerialize for ClientLoginReject { - fn serialize(&self) -> Result, Error> { - let mut values: VariantMap = VariantMap::with_capacity(1); - values.insert( - "MsgType".to_string(), - Variant::String("ClientLoginReject".to_string()), - ); - values.insert( - "ErrorString".to_string(), - Variant::String(self.error.clone()), - ); - return HandshakeSerialize::serialize(&values); - } -} - -impl HandshakeDeserialize for ClientLoginReject { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; - - let msgtype = get_msg_type(&values["MsgType"])?; - - if msgtype == "ClientLogin" { - return Ok(( - len, - Self { - error: match_variant!(values, Variant::String, "ErrorString"), - }, - )); - } else { - bail!(ProtocolError::WrongMsgType); - } - } -} - -#[derive(Debug)] -pub struct SessionInit { - identities: VariantList, - buffers: VariantList, - network_ids: VariantList, -} - -impl HandshakeSerialize for SessionInit { - fn serialize(&self) -> Result, Error> { - let mut values: VariantMap = VariantMap::with_capacity(1); - values.insert( - "MsgType".to_string(), - Variant::String("SessionInit".to_string()), - ); - values.insert( - "Identities".to_string(), - Variant::VariantList(self.identities.clone()), - ); - values.insert( - "BufferInfos".to_string(), - Variant::VariantList(self.buffers.clone()), - ); - values.insert( - "NetworkIds".to_string(), - Variant::VariantList(self.network_ids.clone()), - ); - return HandshakeSerialize::serialize(&values); - } -} - -impl HandshakeDeserialize for SessionInit { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?; - - let msgtype = get_msg_type(&values["MsgType"])?; - - if msgtype == "ClientLogin" { - return Ok(( - len, - Self { - identities: match_variant!(values, Variant::VariantList, "Identities"), - buffers: match_variant!(values, Variant::VariantList, "BufferInfos"), - network_ids: match_variant!(values, Variant::VariantList, "NetworkIds"), - }, - )); - } else { - bail!(ProtocolError::WrongMsgType); - } - } -} diff --git a/src/protocol/message/handshake/types.rs b/src/protocol/message/handshake/types.rs deleted file mode 100644 index 99864b9..0000000 --- a/src/protocol/message/handshake/types.rs +++ /dev/null @@ -1,66 +0,0 @@ -use std::collections::HashMap; -use std::convert::TryInto; -use std::result::Result; -use std::vec::Vec; - -use failure::Error; - -use crate::protocol::error::ProtocolError; -use crate::protocol::primitive::deserialize::Deserialize; -use crate::protocol::primitive::serialize::Serialize; -use crate::protocol::primitive::{String, Variant}; -use crate::util; - -pub trait HandshakeSerialize { - fn serialize(&self) -> Result, Error>; -} - -pub trait HandshakeDeserialize { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> - where - Self: std::marker::Sized; -} - -pub type VariantMap = HashMap; - -impl HandshakeSerialize for VariantMap { - fn serialize<'a>(&'a self) -> Result, Error> { - let mut res: Vec = Vec::new(); - - for (k, v) in self { - let key = Variant::String(k.clone()); - res.extend(key.serialize()?); - res.extend(v.serialize()?); - } - - let len: i32 = (self.len() * 2).try_into().unwrap(); - util::insert_bytes(0, &mut res, &mut (len).to_be_bytes()); - - return Ok(res); - } -} - -impl HandshakeDeserialize for VariantMap { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let (_, len) = i32::parse(&b[0..4])?; - - let mut pos: usize = 4; - let mut map = VariantMap::new(); - - for _ in 0..(len / 2) { - let (nlen, name) = Variant::parse(&b[pos..])?; - pos += nlen; - - let (vlen, value) = Variant::parse(&b[pos..])?; - pos += vlen; - - match name { - Variant::String(x) => map.insert(x, value), - Variant::StringUTF8(x) => map.insert(x, value), - _ => bail!(ProtocolError::WrongVariant), - }; - } - - return Ok((pos, map)); - } -} diff --git a/src/protocol/message/login.rs b/src/protocol/message/login.rs deleted file mode 100644 index 8b13789..0000000 --- a/src/protocol/message/login.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/protocol/message/mod.rs b/src/protocol/message/mod.rs deleted file mode 100644 index f1d4750..0000000 --- a/src/protocol/message/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -pub mod handshake; -pub use handshake::*; - -pub mod login; -pub use login::*; diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs deleted file mode 100644 index 3630fab..0000000 --- a/src/protocol/mod.rs +++ /dev/null @@ -1,9 +0,0 @@ -pub mod message; -pub mod primitive; - -#[allow(dead_code)] -pub mod error; - -#[allow(unused_variables, dead_code)] -#[cfg(feature = "framing")] -pub mod frame; diff --git a/src/protocol/primitive/bufferinfo.rs b/src/protocol/primitive/bufferinfo.rs deleted file mode 100644 index 4c69286..0000000 --- a/src/protocol/primitive/bufferinfo.rs +++ /dev/null @@ -1,74 +0,0 @@ -use std::vec::Vec; - -use failure::Error; - -use crate::protocol::primitive::deserialize::{Deserialize, DeserializeUTF8}; -use crate::protocol::primitive::serialize::{Serialize, SerializeUTF8}; -use crate::protocol::primitive::String; - -extern crate bytes; - -#[derive(Clone, Debug, std::cmp::PartialEq)] -pub struct BufferInfo { - pub id: i32, // a unique, sequential id for the buffer - pub network_id: i32, // NetworkId of the network the buffer belongs to - pub buffer_type: BufferType, - pub name: String, // BufferName as displayed to the user -} - -impl Serialize for BufferInfo { - fn serialize(&self) -> Result, Error> { - let mut values: Vec = Vec::new(); - - values.append(&mut i32::serialize(&self.id)?); - values.append(&mut i32::serialize(&self.network_id)?); - values.append(&mut i16::serialize(&(self.buffer_type as i16))?); - values.append(&mut vec![0, 0, 0, 0]); - values.append(&mut String::serialize_utf8(&self.name)?); - - Ok(values) - } -} - -impl Deserialize for BufferInfo { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let (_, id) = i32::parse(&b[0..4])?; - let (_, network_id) = i32::parse(&b[4..8])?; - let (_, buffer_type) = i16::parse(&b[8..10])?; - - // There are 4 additional undocumted Bytes in the BufferInfo - // so we start at byte 14 - let (size, name) = String::parse_utf8(&b[14..])?; - - return Ok(( - 14 + size, - Self { - id, - network_id, - buffer_type: BufferType::from(buffer_type), - name, - }, - )); - } -} - -#[repr(i16)] -#[derive(Copy, Clone, Debug, std::cmp::PartialEq)] -pub enum BufferType { - Status = 0x01, - Channel = 0x02, - Query = 0x04, - Group = 0x08, -} - -impl From for BufferType { - fn from(value: i16) -> Self { - match value { - 0x01 => return Self::Status, - 0x02 => return Self::Channel, - 0x04 => return Self::Query, - 0x08 => return Self::Group, - _ => unimplemented!(), - } - } -} diff --git a/src/protocol/primitive/datetime.rs b/src/protocol/primitive/datetime.rs deleted file mode 100644 index 688a022..0000000 --- a/src/protocol/primitive/datetime.rs +++ /dev/null @@ -1,93 +0,0 @@ -use crate::protocol::primitive::deserialize::Deserialize; -use crate::protocol::primitive::serialize::Serialize; - -#[derive(Clone, Debug, std::cmp::PartialEq)] -pub struct DateTime { - julian_day: i32, // Day in Julian calendar, unknown if signed or unsigned - millis_of_day: i32, // Milliseconds since start of day - zone: u8, // Timezone of DateTime, 0x00 is local, 0x01 is UTC -} - -impl Serialize for DateTime { - fn serialize(&self) -> Result, failure::Error> { - let mut values: Vec = Vec::new(); - - values.append(&mut i32::serialize(&self.julian_day)?); - values.append(&mut i32::serialize(&self.millis_of_day)?); - values.append(&mut u8::serialize(&(self.zone))?); - - Ok(values) - } -} - -impl Deserialize for DateTime { - fn parse(b: &[std::primitive::u8]) -> Result<(std::primitive::usize, Self), failure::Error> - where - Self: Sized, - { - let (_, julian_day) = i32::parse(&b[0..4])?; - let (_, millis_of_day) = i32::parse(&b[4..8])?; - let (_, zone) = u8::parse(&b[8..9])?; - - return Ok(( - 9, - DateTime { - julian_day, - millis_of_day, - zone, - }, - )); - } -} - -#[derive(Clone, Debug, std::cmp::PartialEq)] -pub struct Date { - julian_day: i32, // Day in Julian calendar, unknown if signed or unsigned -} - -impl Serialize for Date { - fn serialize(&self) -> Result, failure::Error> { - let mut values: Vec = Vec::new(); - - values.append(&mut i32::serialize(&self.julian_day)?); - - Ok(values) - } -} - -impl Deserialize for Date { - fn parse(b: &[std::primitive::u8]) -> Result<(std::primitive::usize, Self), failure::Error> - where - Self: Sized, - { - let (_, julian_day) = i32::parse(&b[0..4])?; - - return Ok((9, Date { julian_day })); - } -} - -#[derive(Clone, Debug, std::cmp::PartialEq)] -pub struct Time { - millis_of_day: i32, // Milliseconds since start of day -} - -impl Serialize for Time { - fn serialize(&self) -> Result, failure::Error> { - let mut values: Vec = Vec::new(); - - values.append(&mut i32::serialize(&self.millis_of_day)?); - - Ok(values) - } -} - -impl Deserialize for Time { - fn parse(b: &[std::primitive::u8]) -> Result<(std::primitive::usize, Self), failure::Error> - where - Self: Sized, - { - let (_, millis_of_day) = i32::parse(&b[0..4])?; - - return Ok((4, Time { millis_of_day })); - } -} diff --git a/src/protocol/primitive/message.rs b/src/protocol/primitive/message.rs deleted file mode 100644 index 4ae895d..0000000 --- a/src/protocol/primitive/message.rs +++ /dev/null @@ -1,184 +0,0 @@ -use std::vec::Vec; - -use failure::Error; - -use crate::protocol::primitive::deserialize::{Deserialize, DeserializeUTF8}; -use crate::protocol::primitive::serialize::{Serialize, SerializeUTF8}; - -use crate::protocol::primitive::BufferInfo; -use crate::protocol::primitive::String; - -extern crate bytes; - -#[derive(Clone, Debug, std::cmp::PartialEq)] -pub struct Message { - pub msg_id: i32, // The unique, sequential id for the message - pub timestamp: i64, // The timestamp of the message in UNIX time (32-bit, seconds, 64-bit if LONGMESSAGE feature enabled) - pub msg_type: MessageType, - pub flags: i8, - pub buffer: BufferInfo, // The buffer the message belongs to, usually everything but BufferId is set to NULL - pub sender: String, // The sender as nick!ident@host - pub sender_prefixes: Option, // The prefix modes of the sender - pub real_name: Option, // The realName of the sender - pub avatar_url: Option, // The avatarUrl of the sender, if available - pub content: String, // The message content, already stripped from CTCP formatting, but containing mIRC format codes -} - -impl Serialize for Message { - fn serialize(&self) -> Result, Error> { - let mut values: Vec = Vec::new(); - - values.append(&mut i32::serialize(&self.msg_id)?); - - // TODO LONGMESSAGE feature - if false { - values.append(&mut i64::serialize(&self.timestamp)?); - } else { - values.append(&mut i32::serialize(&(self.timestamp as i32))?); - } - - values.append(&mut i32::serialize(&(self.msg_type as i32))?); - values.append(&mut i8::serialize(&(self.flags as i8))?); - values.append(&mut BufferInfo::serialize(&self.buffer)?); - values.append(&mut String::serialize_utf8(&self.sender)?); - - // TODO SenderPrefixes feature - if false { - if let Some(x) = &self.sender_prefixes { - values.append(&mut String::serialize_utf8(&x)?); - } - } - - // TODO RichMessages feature - if false { - if let Some(x) = &self.real_name { - values.append(&mut String::serialize_utf8(&x)?); - } - if let Some(x) = &self.avatar_url { - values.append(&mut String::serialize_utf8(&x)?); - } - } - - values.append(&mut String::serialize_utf8(&self.content)?); - - return Ok(values); - } -} - -impl Deserialize for Message { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let mut pos = 0; - let (parsed, msg_id) = i32::parse(&b[pos..])?; - pos += parsed; - - // TODO LONGMESSAGES feature - let timestamp; - if false { - let (parsed, temp_timestamp) = i64::parse(&b[pos..])?; - pos += parsed; - timestamp = temp_timestamp; - } else { - let (parsed, temp_timestamp) = i32::parse(&b[pos..])?; - pos += parsed; - timestamp = temp_timestamp as i64; - } - - let (parsed, msg_type) = i32::parse(&b[pos..])?; - pos += parsed; - let (parsed, flags) = i8::parse(&b[pos..])?; - pos += parsed; - let (parsed, buffer) = BufferInfo::parse(&b[pos..])?; - pos += parsed; - let (parsed, sender) = String::parse_utf8(&b[pos..])?; - pos += parsed; - - // TODO SenderPrefixes feature - let mut sender_prefixes = None; - if false { - let (parsed, temp) = String::parse_utf8(&b[pos..])?; - sender_prefixes = Some(temp); - pos += parsed; - } - - // TODO SenderPrefixes feature - let mut real_name = None; - let mut avatar_url = None; - if false { - let (parsed, temp) = String::parse_utf8(&b[pos..])?; - real_name = Some(temp); - pos += parsed; - - let (parsed, temp) = String::parse_utf8(&b[pos..])?; - avatar_url = Some(temp); - pos += parsed; - } - - let (parsed, content) = String::parse_utf8(&b[pos..])?; - pos += parsed; - - return Ok(( - pos, - Self { - msg_id, - timestamp, - msg_type: MessageType::from(msg_type), - flags, - buffer, - sender, - sender_prefixes, - real_name, - avatar_url, - content, - }, - )); - } -} - -#[repr(i32)] -#[derive(Copy, Clone, Debug, std::cmp::PartialEq)] -pub enum MessageType { - Plain = 0x00000001, - Notice = 0x00000002, - Action = 0x00000004, - Nick = 0x00000008, - Mode = 0x00000010, - Join = 0x00000020, - Part = 0x00000040, - Quit = 0x00000080, - Kick = 0x00000100, - Kill = 0x00000200, - Server = 0x00000400, - Info = 0x00000800, - Error = 0x00001000, - DayChange = 0x00002000, - Topic = 0x00004000, - NetsplitJoin = 0x00008000, - NetsplitQuit = 0x00010000, - Invite = 0x00020000, -} - -impl From for MessageType { - fn from(val: i32) -> Self { - match val { - 0x00000001 => MessageType::Plain, - 0x00000002 => MessageType::Notice, - 0x00000004 => MessageType::Action, - 0x00000008 => MessageType::Nick, - 0x00000010 => MessageType::Mode, - 0x00000020 => MessageType::Join, - 0x00000040 => MessageType::Part, - 0x00000080 => MessageType::Quit, - 0x00000100 => MessageType::Kick, - 0x00000200 => MessageType::Kill, - 0x00000400 => MessageType::Server, - 0x00000800 => MessageType::Info, - 0x00001000 => MessageType::Error, - 0x00002000 => MessageType::DayChange, - 0x00004000 => MessageType::Topic, - 0x00008000 => MessageType::NetsplitJoin, - 0x00010000 => MessageType::NetsplitQuit, - 0x00020000 => MessageType::Invite, - _ => unimplemented!(), - } - } -} diff --git a/src/protocol/primitive/mod.rs b/src/protocol/primitive/mod.rs deleted file mode 100644 index 5656d71..0000000 --- a/src/protocol/primitive/mod.rs +++ /dev/null @@ -1,74 +0,0 @@ -pub mod bufferinfo; -pub mod datetime; -pub mod message; -pub mod signedint; -pub mod string; -pub mod stringlist; -pub mod unsignedint; -pub mod variant; -pub mod variantlist; -pub mod variantmap; - -pub use bufferinfo::*; -pub use datetime::*; -pub use message::*; -pub use signedint::*; -pub use string::*; -pub use stringlist::*; -pub use unsignedint::*; -pub use variant::*; -pub use variantlist::*; -pub use variantmap::*; - -// Static Type Definitions -pub const VOID: u32 = 0x00000000; -pub const BOOL: u32 = 0x00000001; -pub const QCHAR: u32 = 0x00000007; - -pub const QVARIANT: u32 = 0x00000090; -pub const QVARIANTMAP: u32 = 0x00000008; -pub const QVARIANTLIST: u32 = 0x00000009; - -pub const QSTRING: u32 = 0x0000000a; -pub const QSTRINGLIST: u32 = 0x0000000b; -pub const QBYTEARRAY: u32 = 0x0000000c; - -pub const QDATE: u32 = 0x0000000e; -pub const QTIME: u32 = 0x0000000f; -pub const QDATETIME: u32 = 0x00000010; -pub const USERTYPE: u32 = 0x0000007f; - -// Basic types -pub const LONG: u32 = 0x00000081; // int64_t -pub const INT: u32 = 0x00000002; // int32_t -pub const SHORT: u32 = 0x00000082; // int16_t -pub const CHAR: u32 = 0x00000083; // int8_t - -pub const ULONG: u32 = 0x00000084; // uint64_t -pub const UINT: u32 = 0x00000003; // uint32_t -pub const USHORT: u32 = 0x00000085; // uint16_t -pub const UCHAR: u32 = 0x00000086; // uint8_t - -pub mod serialize { - use failure::Error; - pub trait Serialize { - fn serialize(&self) -> Result, Error>; - } - pub trait SerializeUTF8 { - fn serialize_utf8(&self) -> Result, Error>; - } -} - -pub mod deserialize { - use failure::Error; - pub trait Deserialize { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> - where - Self: std::marker::Sized; - } - pub trait DeserializeUTF8 { - fn parse_utf8(b: &[u8]) -> Result<(usize, Self), Error> - where - Self: std::marker::Sized; - } -} diff --git a/src/protocol/primitive/signedint.rs b/src/protocol/primitive/signedint.rs deleted file mode 100644 index 67ffb9d..0000000 --- a/src/protocol/primitive/signedint.rs +++ /dev/null @@ -1,62 +0,0 @@ -extern crate byteorder; -use byteorder::{BigEndian, ReadBytesExt}; -use std::io::Cursor; - -use std::convert::TryInto; -use std::result::Result; -use std::vec::Vec; - -use failure::Error; - -use crate::protocol::primitive::{deserialize, serialize}; - -impl serialize::Serialize for i64 { - fn serialize(&self) -> Result, Error> { - Ok(Vec::from(self.to_be_bytes())) - } -} - -impl deserialize::Deserialize for i64 { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let mut rdr = Cursor::new(&b[0..8]); - return Ok((8, rdr.read_i64::()?)); - } -} - -impl serialize::Serialize for i32 { - fn serialize(&self) -> Result, Error> { - Ok(Vec::from(self.to_be_bytes())) - } -} - -impl deserialize::Deserialize for i32 { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let mut rdr = Cursor::new(&b[0..4]); - return Ok((4, rdr.read_i32::()?)); - } -} - -impl serialize::Serialize for i16 { - fn serialize(&self) -> Result, Error> { - Ok(Vec::from(self.to_be_bytes())) - } -} - -impl deserialize::Deserialize for i16 { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let mut rdr = Cursor::new(&b[0..2]); - return Ok((2, rdr.read_i16::()?)); - } -} - -impl serialize::Serialize for i8 { - fn serialize(&self) -> Result, Error> { - Ok(Vec::from(self.to_be_bytes())) - } -} - -impl deserialize::Deserialize for i8 { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - return Ok((1, b[0].try_into()?)); - } -} diff --git a/src/protocol/primitive/string.rs b/src/protocol/primitive/string.rs deleted file mode 100644 index 470f018..0000000 --- a/src/protocol/primitive/string.rs +++ /dev/null @@ -1,91 +0,0 @@ -extern crate byteorder; - -use std::result::Result; -use std::vec::Vec; - -use failure::Error; - -use log::trace; - -use crate::protocol::primitive::{deserialize, serialize}; -use crate::util; - -pub type String = std::string::String; -impl serialize::Serialize for String { - fn serialize(&self) -> Result, Error> { - let mut res: Vec = Vec::new(); - - let utf16: Vec = self.encode_utf16().collect(); - for i in utf16 { - res.extend(i.to_be_bytes().iter()); - } - - util::prepend_byte_len(&mut res); - return Ok(res); - } -} - -impl serialize::SerializeUTF8 for String { - fn serialize_utf8(&self) -> Result, Error> { - let mut res: Vec = Vec::new(); - res.extend(self.clone().into_bytes()); - res.extend(vec![0x00]); - util::prepend_byte_len(&mut res); - return Ok(res); - } -} - -impl deserialize::Deserialize for String { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - // Parse Length - let (_, len) = i32::parse(&b[0..4])?; - trace!(target: "protocol::primitive::String", "Parsing with length: {:?}, from bytes: {:x?}", len, &b[0..4]); - - if len == -1 { - return Ok((4, "".to_string())); - } - - // length as usize - let ulen = len as usize; - let mut pos: usize = 4; - let mut chars: Vec = Vec::new(); - loop { - // if position is behind the length plus our 4 bytes of the length we already parsed - if pos >= (ulen + 4) { - break; - } - let (slen, uchar) = u16::parse(&b[pos..(pos + 2)])?; - chars.push(uchar); - pos += slen; - } - - let res: String = String::from_utf16(&chars).unwrap(); - return Ok((pos, res)); - } -} - -impl deserialize::DeserializeUTF8 for String { - fn parse_utf8(b: &[u8]) -> Result<(usize, Self), Error> { - use crate::protocol::primitive::deserialize::Deserialize; - let (_, len) = i32::parse(&b[0..4])?; - - trace!(target: "protocol::primitive::String", "Parsing with length: {:?}, from bytes: {:x?}", len, &b[0..4]); - - if len <= 0 { - return Ok((4, "".to_string())); - } - - let ulen = len as usize; - - let mut res: String = String::from_utf8(b[4..(ulen + 4)].to_vec())?; - - // If the last byte is zero remove it - // Receiving a string as bytearray will sometimes have - // the string null terminated - if res.chars().last().unwrap() == '\u{0}' { - let _ = res.pop(); - } - - return Ok((ulen + 4, res)); - } -} diff --git a/src/protocol/primitive/stringlist.rs b/src/protocol/primitive/stringlist.rs deleted file mode 100644 index d2902f2..0000000 --- a/src/protocol/primitive/stringlist.rs +++ /dev/null @@ -1,45 +0,0 @@ -extern crate byteorder; - -use std::convert::TryInto; -use std::result::Result; -use std::vec::Vec; - -use failure::Error; - -use log::trace; - -use crate::protocol::primitive::{deserialize, serialize}; - -pub type StringList = Vec; -impl serialize::Serialize for StringList { - fn serialize(&self) -> Result, Error> { - let len: i32 = self.len().try_into()?; - let mut res: Vec = Vec::new(); - - res.extend(len.to_be_bytes().iter()); - for x in self { - res.extend(x.serialize()?); - } - - return Ok(res); - } -} - -impl deserialize::Deserialize for StringList { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let (_, len) = i32::parse(&b[0..4])?; - trace!(target: "protocol::primitive::StringList", "Parsing with length: {:?}, from bytes: {:x?}", len, &b[0..4]); - let mut res: StringList = StringList::new(); - - let mut pos = 4; - if len > 0 { - for _ in 0..len { - let (lpos, val) = String::parse(&b[pos..])?; - pos += lpos; - res.push(val); - } - } - - return Ok((pos, res)); - } -} diff --git a/src/protocol/primitive/unsignedint.rs b/src/protocol/primitive/unsignedint.rs deleted file mode 100644 index 5b42e3c..0000000 --- a/src/protocol/primitive/unsignedint.rs +++ /dev/null @@ -1,81 +0,0 @@ -extern crate byteorder; -use byteorder::{BigEndian, ReadBytesExt}; -use std::io::Cursor; - -use std::result::Result; -use std::vec::Vec; - -use failure::Error; - -use crate::protocol::error::ProtocolError; -use crate::protocol::primitive::{deserialize, serialize}; - -impl serialize::Serialize for bool { - fn serialize(&self) -> Result, Error> { - Ok({ - let i = *self as i8; - Vec::from(i.to_be_bytes()) - }) - } -} -impl deserialize::Deserialize for bool { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - if b[0] == 0 { - return Ok((1, false)); - } else if b[0] == 1 { - return Ok((1, true)); - } else { - bail!(ProtocolError::BoolOutOfRange); - }; - } -} -impl serialize::Serialize for u64 { - fn serialize(&self) -> Result, Error> { - Ok(Vec::from(self.to_be_bytes())) - } -} - -impl deserialize::Deserialize for u64 { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let mut rdr = Cursor::new(&b[0..8]); - return Ok((8, rdr.read_u64::()?)); - } -} - -impl serialize::Serialize for u32 { - fn serialize(&self) -> Result, Error> { - Ok(Vec::from(self.to_be_bytes())) - } -} - -impl deserialize::Deserialize for u32 { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let mut rdr = Cursor::new(&b[0..4]); - return Ok((4, rdr.read_u32::()?)); - } -} - -impl serialize::Serialize for u16 { - fn serialize(&self) -> Result, Error> { - Ok(Vec::from(self.to_be_bytes())) - } -} - -impl deserialize::Deserialize for u16 { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let mut rdr = Cursor::new(&b[0..2]); - return Ok((2, rdr.read_u16::()?)); - } -} - -impl serialize::Serialize for u8 { - fn serialize(&self) -> Result, Error> { - Ok(Vec::from(self.to_be_bytes())) - } -} - -impl deserialize::Deserialize for u8 { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - return Ok((1, b[0])); - } -} diff --git a/src/protocol/primitive/variant.rs b/src/protocol/primitive/variant.rs deleted file mode 100644 index 84150a8..0000000 --- a/src/protocol/primitive/variant.rs +++ /dev/null @@ -1,290 +0,0 @@ -use std::vec::Vec; - -use failure::Error; - -use log::{error, trace}; - -use crate::protocol::error::ProtocolError; -use crate::protocol::primitive; -use crate::protocol::primitive::deserialize::{Deserialize, DeserializeUTF8}; -use crate::protocol::primitive::serialize::{Serialize, SerializeUTF8}; -use crate::protocol::primitive::{String, StringList}; - -extern crate bytes; -use bytes::BytesMut; - -use crate::protocol::primitive::{ - BufferInfo, Date, DateTime, Message, Time, VariantList, VariantMap, -}; - -#[allow(non_camel_case_types, dead_code)] -#[derive(Clone, Debug, std::cmp::PartialEq)] -pub enum Variant { - Unknown, - UserType(String, BytesMut), - BufferInfo(BufferInfo), - Message(Message), - Time(Time), - Date(Date), - DateTime(DateTime), - VariantMap(VariantMap), - VariantList(VariantList), - String(String), - StringUTF8(String), - ByteArray(String), - StringList(StringList), - bool(bool), - u64(u64), - u32(u32), - u16(u16), - u8(u8), - i64(i64), - i32(i32), - i16(i16), - i8(i8), -} - -impl Serialize for Variant { - fn serialize(&self) -> Result, Error> { - let unknown: u8 = 0x00; - let mut res: Vec = Vec::new(); - - match self { - Variant::Unknown => { - bail!(ProtocolError::UnknownVariant); - } - Variant::VariantMap(v) => { - res.extend(primitive::QVARIANTMAP.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.extend(v.serialize()?.iter()); - } - Variant::VariantList(v) => { - res.extend(primitive::QVARIANTLIST.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.extend(v.serialize()?.iter()); - } - Variant::String(v) => { - res.extend(primitive::QSTRING.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.extend(v.serialize()?.iter()); - } - Variant::StringUTF8(v) => { - res.extend(primitive::QBYTEARRAY.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.extend(v.serialize_utf8()?.iter()); - } - Variant::ByteArray(v) => { - res.extend(primitive::QBYTEARRAY.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.extend(v.serialize_utf8()?.iter()); - res.extend(vec![0x00]); - } - Variant::StringList(v) => { - res.extend(primitive::QSTRINGLIST.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.extend(v.serialize()?.iter()); - } - Variant::bool(v) => { - res.extend(primitive::BOOL.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - let i = *v as i8; - res.extend(i.to_be_bytes().iter()); - } - Variant::u64(v) => { - res.extend(primitive::ULONG.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.extend(v.to_be_bytes().iter()); - } - Variant::u32(v) => { - res.extend(primitive::UINT.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.extend(v.to_be_bytes().iter()); - } - Variant::u16(v) => { - res.extend(primitive::USHORT.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.extend(v.to_be_bytes().iter()); - } - Variant::u8(v) => { - res.extend(primitive::UCHAR.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.extend(v.to_be_bytes().iter()); - } - Variant::i64(v) => { - res.extend(primitive::LONG.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.extend(v.to_be_bytes().iter()); - } - Variant::i32(v) => { - res.extend(primitive::INT.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.extend(v.to_be_bytes().iter()); - } - Variant::i16(v) => { - res.extend(primitive::SHORT.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.extend(v.to_be_bytes().iter()); - } - Variant::i8(v) => { - res.extend(primitive::CHAR.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.extend(v.to_be_bytes().iter()); - } - Variant::UserType(_, _) => unimplemented!(), - Variant::BufferInfo(_) => unimplemented!(), - Variant::Message(_) => unimplemented!(), - Variant::DateTime(v) => { - res.extend(primitive::QDATETIME.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.append(&mut v.serialize()?); - } - Variant::Time(v) => { - res.extend(primitive::QTIME.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.append(&mut v.serialize()?); - } - Variant::Date(v) => { - res.extend(primitive::QDATE.to_be_bytes().iter()); - res.extend(unknown.to_be_bytes().iter()); - res.append(&mut v.serialize()?); - } - } - - return Ok(res); - } -} - -impl Deserialize for Variant { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let (_, qtype) = i32::parse(&b[0..4])?; - let qtype = qtype as u32; - - #[allow(unused_variables)] - let unknown: u8 = b[4]; - - let len = 5; - match qtype { - primitive::QVARIANTMAP => { - trace!(target: "protocol::primitive::Variant", "Parsing Variant: VariantMap"); - let (vlen, value) = VariantMap::parse(&b[len..])?; - return Ok((len + vlen, Variant::VariantMap(value))); - } - primitive::QVARIANTLIST => { - trace!(target: "protocol::primitive::Variant", "Parsing Variant: VariantList"); - let (vlen, value) = VariantList::parse(&b[len..])?; - return Ok((len + vlen, Variant::VariantList(value))); - } - primitive::QSTRING => { - trace!(target: "protocol::primitive::Variant", "Parsing Variant: String"); - let (vlen, value) = String::parse(&b[len..])?; - return Ok((len + vlen, Variant::String(value.clone()))); - } - primitive::QBYTEARRAY => { - trace!(target: "protocol::primitive::Variant", "Parsing Variant: ByteArray"); - let (vlen, value) = String::parse_utf8(&b[len..])?; - return Ok((len + vlen, Variant::StringUTF8(value.clone()))); - } - primitive::QSTRINGLIST => { - trace!(target: "protocol::primitive::Variant", "Parsing Variant: StringList"); - let (vlen, value) = StringList::parse(&b[len..])?; - return Ok((len + vlen, Variant::StringList(value.clone()))); - } - primitive::QDATETIME => { - trace!(target: "protocol::primitive::Variant", "Parsing Variant: Date"); - let (vlen, value) = Date::parse(&b[len..])?; - return Ok((len + vlen, Variant::Date(value.clone()))); - } - primitive::QDATE => { - trace!(target: "protocol::primitive::Variant", "Parsing Variant: Date"); - let (vlen, value) = Date::parse(&b[len..])?; - return Ok((len + vlen, Variant::Date(value.clone()))); - } - primitive::QTIME => { - trace!(target: "protocol::primitive::Variant", "Parsing Variant: Time"); - let (vlen, value) = Time::parse(&b[len..])?; - return Ok((len + vlen, Variant::Time(value.clone()))); - } - primitive::BOOL => { - let (vlen, value) = bool::parse(&b[len..])?; - return Ok((len + vlen, Variant::bool(value))); - } - primitive::ULONG => { - let (vlen, value) = u64::parse(&b[len..])?; - return Ok((len + vlen, Variant::u64(value))); - } - primitive::UINT => { - let (vlen, value) = u32::parse(&b[len..])?; - return Ok((len + vlen, Variant::u32(value))); - } - primitive::USHORT => { - let (vlen, value) = u16::parse(&b[len..])?; - return Ok((len + vlen, Variant::u16(value))); - } - primitive::UCHAR => { - let (vlen, value) = u8::parse(&b[len..])?; - return Ok((len + vlen, Variant::u8(value))); - } - primitive::LONG => { - let (vlen, value) = i64::parse(&b[len..])?; - return Ok((len + vlen, Variant::i64(value))); - } - primitive::INT => { - let (vlen, value) = i32::parse(&b[len..])?; - return Ok((len + vlen, Variant::i32(value))); - } - primitive::SHORT => { - let (vlen, value) = i16::parse(&b[len..])?; - return Ok((len + vlen, Variant::i16(value))); - } - primitive::CHAR => { - let (vlen, value) = i8::parse(&b[len..])?; - return Ok((len + vlen, Variant::i8(value))); - } - primitive::USERTYPE => { - trace!(target: "protocol::primitive::Variant", "Parsing UserType"); - // Parse UserType name - let (user_type_len, user_type) = String::parse_utf8(&b[len..])?; - - trace!(target: "protocol::primitive::Variant", "Parsing UserType: {:?}", user_type); - - // Match Possible User Types to basic structures - match user_type.as_str() { - // As VariantMap - "IrcUser" | "IrcChannel" | "Identity" | "NetworkInfo" | "Network::Server" => { - trace!(target: "protocol::primitive::Variant", "UserType is VariantMap"); - let (vlen, value) = VariantMap::parse(&b[(len + user_type_len)..])?; - return Ok((len + user_type_len + vlen, Variant::VariantMap(value))); - } - // As i32 - "BufferId" | "IdentityId" | "NetworkId" | "MsgId" => { - trace!(target: "protocol::primitive::Variant", "UserType is i32"); - - let (vlen, value) = i32::parse(&b[(len + user_type_len)..])?; - return Ok((len + user_type_len + vlen, Variant::i32(value))); - } - // As i64 - "PeerPtr" => { - trace!(target: "protocol::primitive::Variant", "UserType is i64"); - let (vlen, value) = i64::parse(&b[(len + user_type_len)..])?; - return Ok((len + user_type_len + vlen, Variant::i64(value))); - } - "BufferInfo" => { - trace!(target: "protocol::primitive::Variant", "UserType is BufferInfo"); - let (vlen, value) = BufferInfo::parse(&b[(len + user_type_len)..])?; - return Ok((len + user_type_len + vlen, Variant::BufferInfo(value))); - } - "Message" => { - trace!(target: "protocol::primitive::Variant", "UserType is Message"); - let (vlen, value) = Message::parse(&b[(len + user_type_len)..])?; - return Ok((len + user_type_len + vlen, Variant::Message(value))); - } - _ => unimplemented!(), - } - } - err => { - error!(target: "parser", "UnknownVariant: {:x?}", err); - bail!(ProtocolError::UnknownVariant); - } - } - } -} diff --git a/src/protocol/primitive/variantlist.rs b/src/protocol/primitive/variantlist.rs deleted file mode 100644 index 2481b32..0000000 --- a/src/protocol/primitive/variantlist.rs +++ /dev/null @@ -1,46 +0,0 @@ -use std::convert::TryInto; -use std::vec::Vec; - -use failure::Error; - -use log::trace; - -use crate::protocol::primitive::{deserialize::Deserialize, serialize::Serialize}; - -extern crate bytes; - -use crate::protocol::primitive::Variant; - -pub type VariantList = Vec; - -impl Serialize for VariantList { - fn serialize(&self) -> Result, Error> { - let len: i32 = self.len().try_into()?; - let mut res: Vec = Vec::new(); - - res.extend(len.to_be_bytes().iter()); - for v in self { - res.extend(v.serialize()?.iter()); - } - - return Ok(res); - } -} - -impl Deserialize for VariantList { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let (_, len) = i32::parse(&b[0..4])?; - trace!(target: "protocol::primitive::VariantList", "Parsing VariantList with {:?} elements", len); - - let mut res: VariantList = VariantList::new(); - let mut pos: usize = 4; - for i in 0..len { - trace!(target: "protocol::primitive::VariantList", "Parsing VariantList element: {:?}", i); - let (vlen, val) = Variant::parse(&b[pos..])?; - res.push(val); - pos += vlen; - } - - return Ok((pos, res)); - } -} diff --git a/src/protocol/primitive/variantmap.rs b/src/protocol/primitive/variantmap.rs deleted file mode 100644 index 22ca0f1..0000000 --- a/src/protocol/primitive/variantmap.rs +++ /dev/null @@ -1,63 +0,0 @@ -use std::collections::HashMap; -use std::{convert::TryInto, vec::Vec}; - -use failure::Error; - -use log::trace; - -use crate::protocol::error::ProtocolError; -use crate::protocol::primitive::deserialize::Deserialize; -use crate::protocol::primitive::serialize::Serialize; -use crate::protocol::primitive::String; - -use crate::protocol::primitive::Variant; -use crate::util; - -extern crate bytes; - -pub type VariantMap = HashMap; - -impl Serialize for VariantMap { - fn serialize<'a>(&'a self) -> Result, Error> { - let mut res: Vec = Vec::new(); - - for (k, v) in self { - res.extend(k.serialize()?); - res.extend(v.serialize()?); - } - - let len: i32 = self.len().try_into()?; - util::insert_bytes(0, &mut res, &mut len.to_be_bytes()); - - return Ok(res); - } -} - -impl Deserialize for VariantMap { - fn parse(b: &[u8]) -> Result<(usize, Self), Error> { - let (_, len) = i32::parse(&b[0..4])?; - trace!(target: "protocol::primitive::VariantMap", "Parsing VariantMap with {:?} elements", len); - - let mut pos: usize = 4; - let mut map = VariantMap::new(); - for _ in 0..len { - trace!(target: "protocol::primitive::VariantMap", "Parsing entry name"); - // let (nlen, name) = Variant::parse(&b[pos..])?; - let (nlen, name) = String::parse(&b[pos..])?; - pos += nlen; - - trace!(target: "protocol::primitive::VariantMap", "Parsing entry: {:?} with len {:?}", name, &b[(pos)..(pos + 4)]); - let (vlen, value) = Variant::parse(&b[(pos)..])?; - pos += vlen; - - // match name { - // Variant::String(x) => map.insert(x, value), - // Variant::StringUTF8(x) => map.insert(x, value), - // _ => bail!(ProtocolError::WrongVariant), - // }; - map.insert(name, value); - } - - return Ok((pos, map)); - } -} diff --git a/src/tests/base_types.rs b/src/tests/base_types.rs index 4cc56ae..bbd2fc3 100644 --- a/src/tests/base_types.rs +++ b/src/tests/base_types.rs @@ -1,7 +1,7 @@ -use crate::protocol::primitive::deserialize::{Deserialize, DeserializeUTF8}; -use crate::protocol::primitive::serialize::{Serialize, SerializeUTF8}; +use crate::{Deserialize, DeserializeUTF8}; +use crate::{Serialize, SerializeUTF8}; -use crate::protocol::primitive::*; +use crate::primitive::*; #[test] pub fn serialize_string() { diff --git a/src/tests/frame.rs b/src/tests/frame.rs index 0bc87ad..878379f 100644 --- a/src/tests/frame.rs +++ b/src/tests/frame.rs @@ -20,7 +20,7 @@ use flate2::Decompress; use flate2::FlushCompress; use flate2::FlushDecompress; -use crate::protocol::frame::QuasselCodec; +use crate::frame::QuasselCodec; macro_rules! mock { ($($x:expr,)*) => {{ @@ -131,24 +131,23 @@ pub fn read_single_frame_compressed() { assert_done!(io); } -// TODO shit doens't work for whatever reason -// #[test] -// pub fn read_multi_frame_compressed() { -// let io = FramedRead::new( -// mock! { -// data( -// b"\x78\x9c\x63\x60\x60\xe0\x4c\x4c\x4a\x4e\x49\x4d\x4b\xcf\xc8\x04\x00\x11\xec\x03\x97\x78\x9c\x63\x60\x60\x60\x36\x34\x32\x06\x00\x01\x3d\x00\x9a\x78\x9c\x63\x60\x60\xe0\xce\x48\xcd\xc9\xc9\x57\x28\xcf\x2f\xca\x49\x01\x00\x1a\x93\x04\x68", -// ), -// }, -// QuasselCodec::builder().compression(true).new_codec(), -// ); -// pin_mut!(io); -// -// assert_next_eq!(io, b"abcdefghi"); -// assert_next_eq!(io, b"123"); -// assert_next_eq!(io, b"hello world"); -// assert_done!(io); -// } +#[test] +pub fn read_multi_frame_compressed() { + let io = FramedRead::new( + mock! { + data( + b"\x78\x9c\x63\x60\x60\xe0\x4c\x4c\x4a\x4e\x49\x4d\x4b\xcf\xc8\x04\x00\x11\xec\x03\x97\x78\x9c\x63\x60\x60\x60\x36\x34\x32\x06\x00\x01\x3d\x00\x9a\x78\x9c\x63\x60\x60\xe0\xce\x48\xcd\xc9\xc9\x57\x28\xcf\x2f\xca\x49\x01\x00\x1a\x93\x04\x68", + ), + }, + QuasselCodec::builder().compression(true).new_codec(), + ); + pin_mut!(io); + + assert_next_eq!(io, b"abcdefghi"); + assert_next_eq!(io, b"123"); + assert_next_eq!(io, b"hello world"); + assert_done!(io); +} // ====================== // ===== Test utils ===== diff --git a/src/tests/handshake_types.rs b/src/tests/handshake_types.rs index d18ec8a..1e789c1 100644 --- a/src/tests/handshake_types.rs +++ b/src/tests/handshake_types.rs @@ -1,5 +1,5 @@ -use crate::protocol::message::handshake::{HandshakeDeserialize, HandshakeSerialize, VariantMap}; -use crate::protocol::primitive::Variant; +use crate::primitive::{Variant, VariantMap}; +use crate::{HandshakeDeserialize, HandshakeSerialize}; #[test] pub fn serialize_variantmap() { diff --git a/src/tests/variant_types.rs b/src/tests/variant_types.rs index 0381f07..7ba4166 100644 --- a/src/tests/variant_types.rs +++ b/src/tests/variant_types.rs @@ -1,9 +1,7 @@ -use crate::protocol::primitive::deserialize::Deserialize; -use crate::protocol::primitive::serialize::Serialize; +use crate::Deserialize; +use crate::Serialize; -use crate::protocol::primitive::{ - BufferInfo, BufferType, Message, Variant, VariantList, VariantMap, -}; +use crate::primitive::{BufferInfo, BufferType, Message, Variant, VariantList, VariantMap}; #[test] pub fn serialize_variant_bool() { @@ -56,13 +54,13 @@ pub fn serialize_variantmap() { #[test] pub fn deserialize_variantmap() { let test_bytes: &[u8] = &[ - 0, 0, 0, 1, 0, 0, 0, 10, 0, 0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, - 117, 0, 114, 0, 101, 0, 100, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, + 0, 0, 0, 1, 0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, + 101, 0, 100, 0, 0, 0, 1, 0, 1, ]; let (len, res) = VariantMap::parse(test_bytes).unwrap(); let mut test_variantmap = VariantMap::new(); test_variantmap.insert("Configured".to_string(), Variant::bool(true)); - assert_eq!(len, 39); + assert_eq!(len, 34); assert_eq!(res, test_variantmap); } diff --git a/src/util.rs b/src/util.rs index 33735f1..dd87f7f 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,38 +1,21 @@ -#[macro_export] -macro_rules! parse_match { - ( $matchee:expr, $pos:expr, $map:expr, $bytes:expr, $name:expr, $(($pattern:pat, $type:ty, $variant:expr)),* ) => { - match $matchee { - $( - $pattern => { - let value: $type; - - $pos = $pos + value.parse(&$bytes[($pos)..]); - $map.insert($name, $variant(value)); - }, - )* - }; - }; -} - +/// Match a VariantMaps field and return it's contents if successfull +/// +/// # Example +/// +/// ``` +/// use libquassel::primitive::{VariantMap, Variant}; +/// +/// let var = Variant::String("test string"); +/// let result = match_variant!(var, Variant::String); +/// ``` #[macro_export] macro_rules! match_variant { - ( $values:expr, $x:path, $field:expr ) => { - match &$values[$field] { - $x(x) => { Ok(x.clone()) }, - _ => { Err("") } - }.unwrap(); - } -} - -use crate::protocol::primitive::{Variant}; -use crate::protocol::error::ProtocolError; -use failure::Error; - -pub fn get_msg_type(val: &Variant) -> Result<&str, Error> { - match val { - Variant::String(x) => return Ok(x), - Variant::StringUTF8(x) => return Ok(x), - _ => bail!(ProtocolError::WrongVariant) + ( $values:expr, $x:path ) => { + match &$values { + $x(x) => Ok(x.clone()), + _ => Err(""), + } + .unwrap(); }; } -- cgit v1.2.3