aboutsummaryrefslogtreecommitdiff
path: root/src/protocol
diff options
context:
space:
mode:
authorMax Audron <audron@cocaine.farm>2020-01-17 10:47:50 +0100
committerMax Audron <audron@cocaine.farm>2020-01-17 10:48:44 +0100
commitde973723312c56a58651f12146668500697543c0 (patch)
treef7cc1e5f9039101bc199e611901b162aa4ed13b1 /src/protocol
parentrefactor parse impl (diff)
finish main parsing
Diffstat (limited to 'src/protocol')
-rw-r--r--src/protocol/message/handshake.rs22
-rw-r--r--src/protocol/message/handshake/types.rs31
-rw-r--r--src/protocol/primitive/basic.rs29
-rw-r--r--src/protocol/primitive/mod.rs2
-rw-r--r--src/protocol/primitive/variant.rs48
5 files changed, 60 insertions, 72 deletions
diff --git a/src/protocol/message/handshake.rs b/src/protocol/message/handshake.rs
index 918c424..b346d8e 100644
--- a/src/protocol/message/handshake.rs
+++ b/src/protocol/message/handshake.rs
@@ -46,7 +46,7 @@ pub struct ClientInitReject {
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("MsgType".to_string(), Variant::String("ClientInitReject".to_string()));
values.insert("ErrorString".to_string(), Variant::String(self.error_string.clone()));
return HandshakeSerialize::serialize(&values);
}
@@ -66,19 +66,19 @@ impl HandshakeDeserialize for ClientInitReject {
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 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) -> Vec<u8> {
let mut values: VariantMap = VariantMap::with_capacity(2);
- values.insert("MsgProtocol::Primitive".to_string(), Variant::String("ClientInitAck".to_string()));
+ values.insert("MsgType".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("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);
}
@@ -89,10 +89,10 @@ impl HandshakeDeserialize for ClientInitAck {
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"),
+ 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")
});
}
diff --git a/src/protocol/message/handshake/types.rs b/src/protocol/message/handshake/types.rs
index d6ea346..dadd058 100644
--- a/src/protocol/message/handshake/types.rs
+++ b/src/protocol/message/handshake/types.rs
@@ -1,6 +1,5 @@
use std::io::Read;
use std::vec::Vec;
-use std::net::TcpStream;
use std::convert::TryInto;
use std::collections::HashMap;
@@ -19,10 +18,9 @@ pub trait HandshakeDeserialize {
}
pub trait HandshakeQRead {
- fn read(stream: &mut std::net::TcpStream, buf: &mut [u8]) -> usize;
+ fn read<T: Read>(stream: &mut T, buf: &mut [u8]) -> usize;
}
-
pub type VariantMap = HashMap<String, Variant>;
impl HandshakeSerialize for VariantMap {
@@ -38,8 +36,7 @@ impl HandshakeSerialize for VariantMap {
util::insert_bytes(0, &mut res, &mut [0, 0, 0, 10]);
let len: i32 = res.len().try_into().unwrap();
- util::insert_bytes(0, &mut res, &mut ((len + 4).to_be_bytes()));
- println!("len: {:?}", len + 4);
+ util::insert_bytes(0, &mut res, &mut ((len).to_be_bytes()));
return res;
}
@@ -54,15 +51,17 @@ impl HandshakeDeserialize for VariantMap {
let ulen: usize = len as usize;
loop {
if (pos) >= ulen { break; }
- let (nlen, name) = Variant::parse(&b[(pos)..]);
+ let (nlen, name) = Variant::parse(&b[pos..]);
pos += nlen;
- let (vlen, value) = Variant::parse(&b[(pos)..]);
+ let (vlen, value) = Variant::parse(&b[pos..]);
pos += vlen;
- if let Variant::StringUTF8(x) = name {
- map.insert(x, value);
- }
+ match name {
+ Variant::String(x) => map.insert(x, value),
+ Variant::StringUTF8(x) => map.insert(x, value),
+ _ => panic!()
+ };
}
return (pos, map);
@@ -70,21 +69,19 @@ impl HandshakeDeserialize for VariantMap {
}
impl HandshakeQRead for VariantMap {
- fn read(mut s: &mut TcpStream, b: &mut [u8]) -> usize {
+ fn read<T: Read>(s: &mut T, b: &mut [u8]) -> usize {
s.read(&mut b[0..4]).unwrap();
let (_, len) = i32::parse(&b[0..4]);
- println!("len: {:?}", len);
// Read the 00 00 00 0a VariantType bytes and discard
- let mut tbuf = [0; 4];
- s.read(&mut tbuf).unwrap();
+ s.read(&mut b[4..8]).unwrap();
- let mut pos = 4;
+ let mut pos = 8;
let len: usize = len as usize;
loop {
if pos >= (len - 4) { break; }
- pos += Variant::read(&mut s, &mut b[pos..]);
- pos += Variant::read(&mut s, &mut b[(pos+4..)]);
+ pos += Variant::read(s, &mut b[pos..]);
+ pos += Variant::read(s, &mut b[pos..]);
}
return pos;
diff --git a/src/protocol/primitive/basic.rs b/src/protocol/primitive/basic.rs
index 100da20..30de1c7 100644
--- a/src/protocol/primitive/basic.rs
+++ b/src/protocol/primitive/basic.rs
@@ -33,7 +33,6 @@ use byteorder::{ByteOrder, BigEndian, ReadBytesExt};
use std::io::Read;
use std::vec::Vec;
-use std::net::TcpStream;
use std::convert::TryInto;
use crate::util;
@@ -46,8 +45,8 @@ impl deserialize::Deserialize for bool {
}
impl qread::QRead for bool {
- fn read(s: &mut TcpStream, b: &mut [u8]) -> usize {
- s.read(&mut [b[0]]).unwrap()
+ fn read<T: Read>(s: &mut T, b: &mut [u8]) -> usize {
+ s.read(&mut b[0..1]).unwrap()
}
}
@@ -58,7 +57,7 @@ impl deserialize::Deserialize for u64 {
}
impl qread::QRead for u64 {
- fn read(s: &mut TcpStream, b: &mut [u8]) -> usize {
+ fn read<T: Read>(s: &mut T, b: &mut [u8]) -> usize {
s.read(&mut b[0..8]).unwrap()
}
}
@@ -71,7 +70,7 @@ impl deserialize::Deserialize for u32 {
}
impl qread::QRead for u32 {
- fn read(s: &mut TcpStream, b: &mut [u8]) -> usize {
+ fn read<T: Read>(s: &mut T, b: &mut [u8]) -> usize {
s.read(&mut b[0..4]).unwrap()
}
}
@@ -83,7 +82,7 @@ impl deserialize::Deserialize for u16 {
}
impl qread::QRead for u16 {
- fn read(s: &mut TcpStream, b: &mut [u8]) -> usize {
+ fn read<T: Read>(s: &mut T, b: &mut [u8]) -> usize {
s.read(&mut b[0..2]).unwrap()
}
}
@@ -95,7 +94,7 @@ impl deserialize::Deserialize for u8 {
}
impl qread::QRead for u8 {
- fn read(s: &mut TcpStream, b: &mut [u8]) -> usize {
+ fn read<T: Read>(s: &mut T, b: &mut [u8]) -> usize {
s.read(&mut [b[0]]).unwrap()
}
}
@@ -107,7 +106,7 @@ impl deserialize::Deserialize for i64 {
}
impl qread::QRead for i64 {
- fn read(s: &mut TcpStream, b: &mut [u8]) -> usize {
+ fn read<T: Read>(s: &mut T, b: &mut [u8]) -> usize {
s.read(&mut b[0..8]).unwrap()
}
}
@@ -120,7 +119,7 @@ impl deserialize::Deserialize for i32 {
}
impl qread::QRead for i32 {
- fn read(s: &mut TcpStream, b: &mut [u8]) -> usize {
+ fn read<T: Read>(s: &mut T, b: &mut [u8]) -> usize {
s.read(&mut b[0..4]).unwrap()
}
}
@@ -132,7 +131,7 @@ impl deserialize::Deserialize for i16 {
}
impl qread::QRead for i16 {
- fn read(s: &mut TcpStream, b: &mut [u8]) -> usize {
+ fn read<T: Read>(s: &mut T, b: &mut [u8]) -> usize {
s.read(&mut b[0..2]).unwrap()
}
}
@@ -144,7 +143,7 @@ impl deserialize::Deserialize for i8 {
}
impl qread::QRead for i8 {
- fn read(s: &mut TcpStream, b: &mut [u8]) -> usize {
+ fn read<T: Read>(s: &mut T, b: &mut [u8]) -> usize {
s.read(&mut [b[0]]).unwrap()
}
}
@@ -207,7 +206,7 @@ impl deserialize::DeserializeUTF8 for String {
}
impl qread::QRead for String {
- fn read(s: &mut TcpStream, b: &mut [u8]) -> usize {
+ fn read<T: Read>(s: &mut T, b: &mut [u8]) -> usize {
use crate::protocol::primitive::deserialize::Deserialize;
s.read(&mut b[0..4]).unwrap();
@@ -254,17 +253,17 @@ impl deserialize::Deserialize for StringList {
}
impl qread::QRead for StringList {
- fn read(s: &mut TcpStream, b: &mut [u8]) -> usize {
+ fn read<T: Read>(s: &mut T, b: &mut [u8]) -> usize {
use crate::protocol::primitive::deserialize::Deserialize;
s.read(&mut b[0..4]).unwrap();
let (_, len) = i32::parse(&b[0..4]);
- let mut pos: usize = 0;
+ let mut pos: usize = 4;
for _ in 0..len {
pos += String::read(s, &mut b[pos..]);
}
- return 4 + pos;
+ return pos;
}
}
diff --git a/src/protocol/primitive/mod.rs b/src/protocol/primitive/mod.rs
index 03c62bc..2cdd20a 100644
--- a/src/protocol/primitive/mod.rs
+++ b/src/protocol/primitive/mod.rs
@@ -25,6 +25,6 @@ pub mod deserialize {
pub mod qread {
pub trait QRead {
- fn read(stream: &mut std::net::TcpStream, buf: &mut [u8]) -> usize;
+ fn read<T: std::io::Read>(stream: &mut T, buf: &mut [u8]) -> usize;
}
}
diff --git a/src/protocol/primitive/variant.rs b/src/protocol/primitive/variant.rs
index f868e9f..fdf93ad 100644
--- a/src/protocol/primitive/variant.rs
+++ b/src/protocol/primitive/variant.rs
@@ -3,8 +3,6 @@ use std::convert::TryInto;
use std::collections::HashMap;
use std::io::Read;
-use std::net::TcpStream;
-
use crate::util;
use crate::protocol::primitive::serialize::{Serialize, SerializeUTF8};
use crate::protocol::primitive::deserialize::{Deserialize, DeserializeUTF8};
@@ -33,19 +31,15 @@ impl Serialize for VariantMap {
impl Deserialize for VariantMap {
fn parse(b: &[u8]) -> (usize, Self) {
let (_, len) = i32::parse(&b[0..4]);
- println!("len: {:?}", len);
let mut pos = 4;
let mut map = VariantMap::new();
for _ in 0..len {
- println!("pos: {:?}", pos);
let (nlen, name) = String::parse(&b[(pos)..]);
pos += nlen;
- println!("pos: {:?}", pos);
let (vlen, value) = Variant::parse(&b[(pos)..]);
pos += vlen;
- println!("pos: {:?}", pos);
map.insert(name, value);
}
@@ -55,7 +49,7 @@ impl Deserialize for VariantMap {
}
impl QRead for VariantMap {
- fn read(mut s: &mut TcpStream, b: &mut [u8]) -> usize {
+ fn read<T: Read>(s: &mut T, b: &mut [u8]) -> usize {
s.read(&mut b[0..4]).unwrap();
@@ -63,8 +57,8 @@ impl QRead for VariantMap {
let mut pos = 4;
for _ in 0..len {
- pos += String::read(&mut s, &mut b[pos..]);
- pos += Variant::read(&mut s, &mut b[(pos+3..)]);
+ pos += String::read(s, &mut b[pos..]);
+ pos += Variant::read(s, &mut b[(pos+3..)]);
}
return pos;
@@ -105,14 +99,14 @@ impl Deserialize for VariantList {
}
impl QRead for VariantList {
- fn read(mut s: &mut TcpStream, b: &mut [u8]) -> usize {
+ fn read<T: Read>(s: &mut T, b: &mut [u8]) -> usize {
s.read(&mut b[0..4]).unwrap();
let (_, len) = i32::parse(&b[0..4]);
let mut pos = 4;
for _ in 0..len {
- pos += Variant::read(&mut s, &mut b[(pos+3..)]);
+ pos += Variant::read(s, &mut b[(pos+3..)]);
}
return pos;
@@ -300,32 +294,30 @@ impl Deserialize for Variant {
}
impl QRead for Variant {
- fn read(s: &mut TcpStream, b: &mut [u8]) -> usize {
+ fn read<T: Read>(s: &mut T, b: &mut [u8]) -> usize {
s.read(&mut b[0..4]).unwrap();
let (_, qtype) = i32::parse(&b[0..4]);
let qtype = qtype as u32;
s.read(&mut [b[4]]).unwrap();
- #[allow(unused_variables)]
- let unknown: u8 = b[4];
let mut len = 5;
match qtype {
- primitive::QVARIANTMAP => len += VariantMap::read(s, &mut b[5..]),
- primitive::QVARIANTLIST => len += VariantList::read(s, &mut b[5..]),
- primitive::QSTRING => len += String::read(s, &mut b[5..]),
- primitive::QBYTEARRAY => len += String::read(s, &mut b[5..]),
- primitive::QSTRINGLIST => len += StringList::read(s, &mut b[5..]),
- primitive::BOOL => len += bool::read(s, &mut b[5..]),
- primitive::ULONG => len += u64::read(s, &mut b[5..]),
- primitive::UINT => len += u32::read(s, &mut b[5..]),
- primitive::USHORT => len += u16::read(s, &mut b[5..]),
- primitive::UCHAR => len += u8::read(s, &mut b[5..]),
- primitive::LONG => len += i64::read(s, &mut b[5..]),
- primitive::INT => len += i32::read(s, &mut b[5..]),
- primitive::SHORT => len += i16::read(s, &mut b[5..]),
- primitive::CHAR => len += i8::read(s, &mut b[5..]),
+ primitive::QVARIANTMAP => len += VariantMap::read(s, &mut b[len..]),
+ primitive::QVARIANTLIST => len += VariantList::read(s, &mut b[len..]),
+ primitive::QSTRING => len += String::read(s, &mut b[len..]),
+ primitive::QBYTEARRAY => len += String::read(s, &mut b[len..]),
+ primitive::QSTRINGLIST => len += StringList::read(s, &mut b[len..]),
+ primitive::BOOL => len += bool::read(s, &mut b[len..]),
+ primitive::ULONG => len += u64::read(s, &mut b[len..]),
+ primitive::UINT => len += u32::read(s, &mut b[len..]),
+ primitive::USHORT => len += u16::read(s, &mut b[len..]),
+ primitive::UCHAR => len += u8::read(s, &mut b[len..]),
+ primitive::LONG => len += i64::read(s, &mut b[len..]),
+ primitive::INT => len += i32::read(s, &mut b[len..]),
+ primitive::SHORT => len += i16::read(s, &mut b[len..]),
+ primitive::CHAR => len += i8::read(s, &mut b[len..]),
_ => return len
}