aboutsummaryrefslogtreecommitdiff
path: root/src/protocol/message/handshake.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/protocol/message/handshake.rs')
-rw-r--r--src/protocol/message/handshake.rs99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/protocol/message/handshake.rs b/src/protocol/message/handshake.rs
new file mode 100644
index 0000000..918c424
--- /dev/null
+++ b/src/protocol/message/handshake.rs
@@ -0,0 +1,99 @@
+use crate::protocol::primitive::{String, StringList, Variant, VariantList};
+use crate::protocol::primitive::{serialize, deserialize, qread};
+
+mod types;
+pub use types::{VariantMap, HandshakeDeserialize, HandshakeSerialize, HandshakeQRead};
+
+use crate::match_variant;
+#[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) -> Vec<u8> {
+ 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]) -> (usize, Self) {
+ let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b);
+
+ return (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")
+ });
+ }
+}
+
+#[derive(Debug)]
+pub struct ClientInitReject {
+ pub error_string: String
+}
+
+impl HandshakeSerialize for ClientInitReject {
+ fn serialize(&self) -> Vec<u8> {
+ let mut values: VariantMap = VariantMap::with_capacity(2);
+ values.insert("MsgProtocol::Primitive".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]) -> (usize, Self) {
+ let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b);
+
+ return (len, Self {
+ error_string: match_variant!(values, Variant::String, "ErrorString")
+ });
+ }
+}
+
+#[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 backend_info: VariantList, // List of VariantMaps of info on available backends
+ pub authenticator_info: 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) -> Vec<u8> {
+ let mut values: VariantMap = VariantMap::with_capacity(2);
+ values.insert("MsgProtocol::Primitive".to_string(), Variant::String("ClientInitAck".to_string()));
+ values.insert("CoreFeatures".to_string(), Variant::u32(self.core_features));
+ values.insert("CoreConfigured".to_string(), Variant::bool(self.core_configured));
+ values.insert("BackendInfo".to_string(), Variant::VariantList(self.backend_info.clone()));
+ values.insert("AuthenticatorInfo".to_string(), Variant::VariantList(self.authenticator_info.clone()));
+ values.insert("FeatureList".to_string(), Variant::StringList(self.feature_list.clone()));
+ return HandshakeSerialize::serialize(&values);
+ }
+}
+
+impl HandshakeDeserialize for ClientInitAck {
+ fn parse(b: &[u8]) -> (usize, Self) {
+ let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b);
+
+ return (len, Self {
+ core_features: match_variant!(values, Variant::u32, "CoreFeatures"),
+ core_configured: match_variant!(values, Variant::bool, "CoreConfigured"),
+ backend_info: match_variant!(values, Variant::VariantList, "BackendInfo"),
+ authenticator_info: match_variant!(values, Variant::VariantList, "AuthenticatorInfo"),
+ feature_list: match_variant!(values, Variant::StringList, "FeatureList")
+ });
+ }
+}