From de973723312c56a58651f12146668500697543c0 Mon Sep 17 00:00:00 2001 From: Max Audron Date: Fri, 17 Jan 2020 10:47:50 +0100 Subject: finish main parsing --- scratch.rs | 116 -------------------------------- src/main.rs | 4 +- src/net.rs | 10 +-- src/protocol/message/handshake.rs | 22 +++--- src/protocol/message/handshake/types.rs | 31 ++++----- src/protocol/primitive/basic.rs | 29 ++++---- src/protocol/primitive/mod.rs | 2 +- src/protocol/primitive/variant.rs | 48 ++++++------- src/tests/base_types.rs | 33 ++++++++- src/tests/handshake_types.rs | 63 ++++++++++++----- 10 files changed, 146 insertions(+), 212 deletions(-) delete mode 100644 scratch.rs diff --git a/scratch.rs b/scratch.rs deleted file mode 100644 index f8a2fb8..0000000 --- a/scratch.rs +++ /dev/null @@ -1,116 +0,0 @@ - -// pos: 0 -// pos: 18 -// pos: 23 -// pos: 23 -// pos: 43 -// pos: 51 -// pos: 51 -// pos: 71 -// pos: 76 -// pos: 76 -// pos: 91 -// pos: 125 -// pos: 125 -// pos: 148 -// pos: 156 -// pos: 156 - -// -// 0000 00 00 01 30 00 00 00 0a 00 00 00 0a|00|00 00 00 ...0............ -// 0010 16|00 46 00 65 00 61 00 74 00 75 00 72 00 65 00 ..F.e.a.t.u.r.e. -// 0020 4c 00 69 00 73 00 74|00 00 00 0b|00|00 00 00 01| L.i.s.t......... -// 0030 00 00 00 2c|00 53 00 79 00 6e 00 63 00 68 00 72 ...,.S.y.n.c.h.r -// 0040 00 6f 00 6e 00 69 00 7a 00 65 00 64 00 4d 00 61 .o.n.i.z.e.d.M.a -// 0050 00 72 00 6b 00 65 00 72 00 4c 00 69 00 6e 00 65| .r.k.e.r.L.i.n.e -// 0060 00 00 00 0a|00|00 00 00 1a|00 43 00 6c 00 69 00 ..........C.l.i. -// 0070 65 00 6e 00 74 00 56 00 65 00 72 00 73 00 69 00 e.n.t.V.e.r.s.i. -// 0080 6f 00 6e|00 00 00 0a|00|00 00 00 14|00 52 00 75 o.n..........R.u -// 0090 00 73 00 74 00 20 00 30 00 2e 00 30 00 2e 00 30| .s.t. .0...0...0 -// 00a0 00 00 00 0a|00|00 00 00 14|00 43 00 6c 00 69 00 ..........C.l.i. -// 00b0 65 00 6e 00 74 00 44 00 61 00 74 00 65|00 00 00 e.n.t.D.a.t.e... -// 00c0 0a|00|00 00 00 14|00 31 00 35 00 37 00 39 00 30 .......1.5.7.9.0 -// 00d0 00 30 00 39 00 32 00 31 00 31 00 00 00 0a 00 00 .0.9.2.1.1...... -// 00e0 00 00 10 00 46 00 65 00 61 00 74 00 75 00 72 00 ....F.e.a.t.u.r. -// 00f0 65 00 73 00 00 00 03 00 00 00 00 00 00 00 00 0a e.s............. -// 0100 00 00 00 00 0e 00 4d 00 73 00 67 00 54 00 79 00 ......M.s.g.T.y. -// 0110 70 00 65 00 00 00 0a 00 00 00 00 14 00 43 00 6c p.e..........C.l -// 0120 00 69 00 65 00 6e 00 74 00 49 00 6e 00 69 00 74 .i.e.n.t.I.n.i.t - -// 0000 00 00 04 cc 00 00 00 0a 00 00 00 0a|00|00 00 00 ................ -// 0010 0e|00 4d 00 73 00 67 00 54 00 79 00 70 00 65|00 ..M.s.g.T.y.p.e. -// 0020 00 00 0a|00|00 00 00 14|00 43 00 6c 00 69 00 65 .........C.l.i.e -// 0030 00 6e 00 74 00 49 00 6e 00 69 00 74 00 00 00 0a .n.t.I.n.i.t.... -// 0040 00 00 00 00 1a 00 43 00 6c 00 69 00 65 00 6e 00 ......C.l.i.e.n. -// 0050 74 00 56 00 65 00 72 00 73 00 69 00 6f 00 6e 00 t.V.e.r.s.i.o.n. -// 0060 00 00 0a 00 00 00 01 06 00 51 00 75 00 61 00 73 .........Q.u.a.s -// 0070 00 73 00 65 00 6c 00 64 00 72 00 6f 00 69 00 64 .s.e.l.d.r.o.i.d -// 0080 00 20 00 3c 00 61 00 20 00 68 00 72 00 65 00 66 . .<.a. .h.r.e.f -// 0090 00 3d 00 22 00 68 00 74 00 74 00 70 00 73 00 3a .=.".h.t.t.p.s.: -// 00a0 00 2f 00 2f 00 67 00 69 00 74 00 2e 00 6b 00 75 ././.g.i.t...k.u -// 00b0 00 73 00 63 00 68 00 6b 00 75 00 2e 00 64 00 65 .s.c.h.k.u...d.e -// 00c0 00 2f 00 6a 00 75 00 73 00 74 00 4a 00 61 00 6e ./.j.u.s.t.J.a.n -// 00d0 00 6e 00 65 00 2f 00 51 00 75 00 61 00 73 00 73 .n.e./.Q.u.a.s.s -// 00e0 00 65 00 6c 00 44 00 72 00 6f 00 69 00 64 00 2d .e.l.D.r.o.i.d.- -// 00f0 00 6e 00 67 00 2f 00 63 00 6f 00 6d 00 6d 00 69 .n.g./.c.o.m.m.i -// 0100 00 74 00 2f 00 65 00 34 00 62 00 37 00 64 00 65 .t./.e.4.b.7.d.e -// 0110 00 37 00 39 00 33 00 37 00 37 00 38 00 34 00 65 .7.9.3.7.7.8.4.e -// 0120 00 32 00 64 00 31 00 31 00 66 00 62 00 32 00 32 .2.d.1.1.f.b.2.2 -// 0130 00 35 00 66 00 39 00 38 00 31 00 62 00 36 00 37 .5.f.9.8.1.b.6.7 -// 0140 00 37 00 39 00 65 00 35 00 36 00 38 00 33 00 32 .7.9.e.5.6.8.3.2 -// 0150 00 34 00 35 00 22 00 3e 00 76 00 31 00 2e 00 32 .4.5.".>.v.1...2 -// 0160 00 2e 00 31 00 30 00 3c 00 2f 00 61 00 3e 00 00 ...1.0.<./.a.>.. -// 0170 00 0a 00 00 00 00 14 00 43 00 6c 00 69 00 65 00 ........C.l.i.e. -// 0180 6e 00 74 00 44 00 61 00 74 00 65 00 00 00 0a 00 n.t.D.a.t.e..... -// 0190 00 00 00 14 00 31 00 35 00 36 00 33 00 37 00 33 .....1.5.6.3.7.3 -// 01a0 00 37 00 31 00 34 00 35 00 00 00 0a 00 00 00 00 .7.1.4.5........ -// 01b0 10 00 46 00 65 00 61 00 74 00 75 00 72 00 65 00 ..F.e.a.t.u.r.e. -// 01c0 73 00 00 00 03 00 00 00 fe ff 00 00 00 0a 00 00 s............... -// 01d0 00 00 16 00 46 00 65 00 61 00 74 00 75 00 72 00 ....F.e.a.t.u.r. -// 01e0 65 00 4c 00 69 00 73 00 74|00 00 00 0b|00|00 00 e.L.i.s.t....... -// 01f0 00 15|00 00 00 2c 00 53 00 79 00 6e 00 63 00 68 .....,.S.y.n.c.h -// 0200 00 72 00 6f 00 6e 00 69 00 7a 00 65 00 64 00 4d .r.o.n.i.z.e.d.M -// 0210 00 61 00 72 00 6b 00 65 00 72 00 4c 00 69 00 6e .a.r.k.e.r.L.i.n -// 0220 00 65 00 00 00 24 00 53 00 61 00 73 00 6c 00 41 .e...$.S.a.s.l.A -// 0230 00 75 00 74 00 68 00 65 00 6e 00 74 00 69 00 63 .u.t.h.e.n.t.i.c -// 0240 00 61 00 74 00 69 00 6f 00 6e 00 00 00 18 00 53 .a.t.i.o.n.....S -// 0250 00 61 00 73 00 6c 00 45 00 78 00 74 00 65 00 72 .a.s.l.E.x.t.e.r -// 0260 00 6e 00 61 00 6c 00 00 00 28 00 48 00 69 00 64 .n.a.l...(.H.i.d -// 0270 00 65 00 49 00 6e 00 61 00 63 00 74 00 69 00 76 .e.I.n.a.c.t.i.v -// 0280 00 65 00 4e 00 65 00 74 00 77 00 6f 00 72 00 6b .e.N.e.t.w.o.r.k -// 0290 00 73 00 00 00 1c 00 50 00 61 00 73 00 73 00 77 .s.....P.a.s.s.w -// 02a0 00 6f 00 72 00 64 00 43 00 68 00 61 00 6e 00 67 .o.r.d.C.h.a.n.g -// 02b0 00 65 00 00 00 1c 00 43 00 61 00 70 00 4e 00 65 .e.....C.a.p.N.e -// 02c0 00 67 00 6f 00 74 00 69 00 61 00 74 00 69 00 6f .g.o.t.i.a.t.i.o -// 02d0 00 6e 00 00 00 1e 00 56 00 65 00 72 00 69 00 66 .n.....V.e.r.i.f -// 02e0 00 79 00 53 00 65 00 72 00 76 00 65 00 72 00 53 .y.S.e.r.v.e.r.S -// 02f0 00 53 00 4c 00 00 00 20 00 43 00 75 00 73 00 74 .S.L... .C.u.s.t -// 0300 00 6f 00 6d 00 52 00 61 00 74 00 65 00 4c 00 69 .o.m.R.a.t.e.L.i -// 0310 00 6d 00 69 00 74 00 73 00 00 00 26 00 41 00 77 .m.i.t.s...&.A.w -// 0320 00 61 00 79 00 46 00 6f 00 72 00 6d 00 61 00 74 .a.y.F.o.r.m.a.t -// 0330 00 54 00 69 00 6d 00 65 00 73 00 74 00 61 00 6d .T.i.m.e.s.t.a.m -// 0340 00 70 00 00 00 1c 00 41 00 75 00 74 00 68 00 65 .p.....A.u.t.h.e -// 0350 00 6e 00 74 00 69 00 63 00 61 00 74 00 6f 00 72 .n.t.i.c.a.t.o.r -// 0360 00 73 00 00 00 24 00 42 00 75 00 66 00 66 00 65 .s...$.B.u.f.f.e -// 0370 00 72 00 41 00 63 00 74 00 69 00 76 00 69 00 74 .r.A.c.t.i.v.i.t -// 0380 00 79 00 53 00 79 00 6e 00 63 00 00 00 24 00 43 .y.S.y.n.c...$.C -// 0390 00 6f 00 72 00 65 00 53 00 69 00 64 00 65 00 48 .o.r.e.S.i.d.e.H -// 03a0 00 69 00 67 00 68 00 6c 00 69 00 67 00 68 00 74 .i.g.h.l.i.g.h.t -// 03b0 00 73 00 00 00 1c 00 53 00 65 00 6e 00 64 00 65 .s.....S.e.n.d.e -// 03c0 00 72 00 50 00 72 00 65 00 66 00 69 00 78 00 65 .r.P.r.e.f.i.x.e -// 03d0 00 73 00 00 00 20 00 52 00 65 00 6d 00 6f 00 74 .s... .R.e.m.o.t -// 03e0 00 65 00 44 00 69 00 73 00 63 00 6f 00 6e 00 6e .e.D.i.s.c.o.n.n -// 03f0 00 65 00 63 00 74 00 00 00 20 00 45 00 78 00 74 .e.c.t... .E.x.t -// 0400 00 65 00 6e 00 64 00 65 00 64 00 46 00 65 00 61 .e.n.d.e.d.F.e.a -// 0410 00 74 00 75 00 72 00 65 00 73 00 00 00 10 00 4c .t.u.r.e.s.....L -// 0420 00 6f 00 6e 00 67 00 54 00 69 00 6d 00 65 00 00 .o.n.g.T.i.m.e.. -// 0430 00 18 00 52 00 69 00 63 00 68 00 4d 00 65 00 73 ...R.i.c.h.M.e.s -// 0440 00 73 00 61 00 67 00 65 00 73 00 00 00 22 00 42 .s.a.g.e.s...".B -// 0450 00 61 00 63 00 6b 00 6c 00 6f 00 67 00 46 00 69 .a.c.k.l.o.g.F.i -// 0460 00 6c 00 74 00 65 00 72 00 54 00 79 00 70 00 65 .l.t.e.r.T.y.p.e -// 0470 00 00 00 1e 00 45 00 63 00 64 00 73 00 61 00 43 .....E.c.d.s.a.C -// 0480 00 65 00 72 00 74 00 66 00 70 00 4b 00 65 00 79 .e.r.t.f.p.K.e.y -// 0490 00 73 00 00 00 1a 00 4c 00 6f 00 6e 00 67 00 4d .s.....L.o.n.g.M -// 04a0 00 65 00 73 00 73 00 61 00 67 00 65 00 49 00 64 .e.s.s.a.g.e.I.d -// 04b0 00 00 00 1c 00 53 00 79 00 6e 00 63 00 65 00 64 .....S.y.n.c.e.d -// 04c0 00 43 00 6f 00 72 00 65 00 49 00 6e 00 66 00 6f .C.o.r.e.I.n.f.o diff --git a/src/main.rs b/src/main.rs index 78a4e2b..28d175c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,11 +23,13 @@ fn main() -> std::io::Result<()> { let mut features = StringList::new(); features.push("SynchronizedMarkerLine".to_string()); + features.push("Authenticators".to_string()); + features.push("ExtendedFeatures".to_string()); let client = ClientInit { client_version:String::from("Rust 0.0.0"), client_date: String::from("1579009211"), feature_list: features, - client_features: 0, + client_features: 0x00008000, }; server.login("audron", "audron", client); diff --git a/src/net.rs b/src/net.rs index 5dd9552..f20dd39 100644 --- a/src/net.rs +++ b/src/net.rs @@ -23,17 +23,13 @@ impl Client { pub fn login(&mut self, user: &'static str, pass: &'static str, client: message::ClientInit) { use crate::protocol::message::handshake::{HandshakeDeserialize, HandshakeSerialize, HandshakeQRead, VariantMap}; use crate::protocol::message::handshake::{ClientInit, ClientInitAck}; - use std::convert::TryInto; - let sclientinit = &client.serialize(); - let len: u32 = sclientinit.len().try_into().unwrap(); -// self.tcp_stream.write(&len.to_be_bytes()).unwrap(); - self.tcp_stream.write(sclientinit).unwrap(); + self.tcp_stream.write(&client.serialize()).unwrap(); let mut buf: Vec = [0; 2048].to_vec(); - VariantMap::read(&mut self.tcp_stream, &mut buf); + let len = VariantMap::read(&mut self.tcp_stream, &mut buf); + buf.truncate(len); - // println!("{:?}", buf); let res = ClientInitAck::parse(&buf); println!("{:?}", res) } 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 { 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 { 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(stream: &mut T, buf: &mut [u8]) -> usize; } - pub type VariantMap = HashMap; 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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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 } diff --git a/src/tests/base_types.rs b/src/tests/base_types.rs index a35f243..99e6cd5 100644 --- a/src/tests/base_types.rs +++ b/src/tests/base_types.rs @@ -1,5 +1,6 @@ use crate::protocol::primitive::serialize::{Serialize, SerializeUTF8}; use crate::protocol::primitive::deserialize::{Deserialize, DeserializeUTF8}; +use crate::protocol::primitive::qread::QRead; use crate::protocol::primitive::*; @@ -17,6 +18,21 @@ pub fn serialize_string_utf8() { assert_eq!(test_string.serialize_utf8(), [0, 0, 0, 10, 67, 111, 110, 102, 105, 103, 117, 114, 101, 100]); } +#[test] +pub fn read_string() { + use std::io::Cursor; + + let test_bytes: Vec = vec![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]; + + let mut buf: Vec = [0; 24].to_vec(); + let len = String::read(&mut Cursor::new(&test_bytes), &mut buf); + + assert_eq!(len, 24); + + let result_bytes: Vec = vec![0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100]; + assert_eq!(buf, result_bytes); +} + #[test] pub fn deserialize_string() { let test_bytes: &[u8] = &[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]; @@ -43,9 +59,24 @@ pub fn serialize_string_list() { ) } +#[test] +pub fn read_string_list() { + use std::io::Cursor; + + let test_bytes: Vec = vec![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]; + + let mut buf: Vec = [0; 28].to_vec(); + let len = StringList::read(&mut Cursor::new(&test_bytes), &mut buf); + + assert_eq!(len, 28); + + let result_bytes: Vec = vec![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]; + assert_eq!(buf, result_bytes); +} + #[test] pub fn deserialize_string_list() { - let test_bytes: &[u8] = &[0, 0, 0, 24, 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]; + let test_bytes: &[u8] = &[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]; let mut test_list = StringList::new(); test_list.push("Configured".to_string()); println!("aaaaa"); diff --git a/src/tests/handshake_types.rs b/src/tests/handshake_types.rs index 99fc1ed..dd3387f 100644 --- a/src/tests/handshake_types.rs +++ b/src/tests/handshake_types.rs @@ -1,11 +1,11 @@ -use crate::protocol::message::handshake::{VariantMap, HandshakeSerialize, HandshakeDeserialize}; +use crate::protocol::message::handshake::{VariantMap, HandshakeSerialize, HandshakeDeserialize, HandshakeQRead}; use crate::protocol::primitive::{Variant}; #[test] pub fn serialize_variantmap() { let mut test_variantmap = VariantMap::new(); test_variantmap.insert("Configured".to_string(), Variant::bool(true)); - let bytes = [0, 0, 0, 43, 0, 0, 0, 10, 0, 0, 0, 10, 0, + let bytes = [0, 0, 0, 39, 0, 0, 0, 10, 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].to_vec(); assert_eq!( @@ -14,16 +14,49 @@ pub fn serialize_variantmap() { ); } -// #[test] -// pub fn deserialize_variantmap() { -// let test_bytes: &[u8] = &[0, 0, 0, 43, 0, 0, 0, 10, 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]; -// let mut test_variantmap = VariantMap::new(); -// test_variantmap.insert("Configured".to_string(), Variant::bool(true)); -// -// let (len, res) = VariantMap::parse(test_bytes); -// -// assert_eq!(len, 43); -// assert_eq!(res, test_variantmap); -// } +#[test] +pub fn read_variantmap() { + use std::io::Cursor; + + let test_bytes: Vec = vec![0, 0, 0, 39, 0, 0, 0, 10, 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]; + + let mut buf: Vec = [0; 43].to_vec(); + let len = VariantMap::read(&mut Cursor::new(&test_bytes), &mut buf); + + assert_eq!(len, 43); + + let result_bytes: Vec = vec![0, 0, 0, 39, 0, 0, 0, 10, 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]; + assert_eq!(buf, result_bytes); +} + +#[test] +pub fn deserialize_variantmap() { + let test_bytes: &[u8] = &[0, 0, 0, 39, 0, 0, 0, 10, 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]; + let mut test_variantmap = VariantMap::new(); + test_variantmap.insert("Configured".to_string(), Variant::bool(true)); + + let (len, res) = VariantMap::parse(test_bytes); + + assert_eq!(len, 43); + assert_eq!(res, test_variantmap); +} + +#[test] +pub fn deserialize_variantmap_utf8() { + let test_bytes: &[u8] = &[0, 0, 0, 29, 0, 0, 0, 10, 0, 0, 0, 12, 0, + 0, 0, 0, 10, 67, 111, 110, 102, 105, 103, 117, 114, 101, 100, + 0, 0, 0, 1, 0, 1, 0, 0, 0, 1]; + let mut test_variantmap = VariantMap::new(); + test_variantmap.insert("Configured".to_string(), Variant::bool(true)); + + let (len, res) = VariantMap::parse(test_bytes); + + assert_eq!(len, 33); + assert_eq!(res, test_variantmap); +} -- cgit v1.2.3