aboutsummaryrefslogtreecommitdiff
path: root/src/protocol/message
diff options
context:
space:
mode:
Diffstat (limited to 'src/protocol/message')
-rw-r--r--src/protocol/message/handshake.rs352
-rw-r--r--src/protocol/message/handshake/types.rs66
-rw-r--r--src/protocol/message/login.rs1
-rw-r--r--src/protocol/message/mod.rs5
4 files changed, 0 insertions, 424 deletions
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<Vec<u8>, 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<Vec<u8>, 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<Vec<u8>, 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<Vec<u8>, 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<Vec<u8>, 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<Vec<u8>, 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<Vec<u8>, 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<Vec<u8>, Error>;
-}
-
-pub trait HandshakeDeserialize {
- fn parse(b: &[u8]) -> Result<(usize, Self), Error>
- where
- Self: std::marker::Sized;
-}
-
-pub type VariantMap = HashMap<String, Variant>;
-
-impl HandshakeSerialize for VariantMap {
- fn serialize<'a>(&'a self) -> Result<Vec<u8>, Error> {
- let mut res: Vec<u8> = 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::*;