aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMax Audron <audron@cocaine.farm>2020-01-17 16:03:59 +0100
committerMax Audron <audron@cocaine.farm>2020-01-17 16:03:59 +0100
commit7cde729ee4b8f618727cd46ec2a2498595b5a058 (patch)
tree9b0ef90ebcb052c2540270123454d1c9f6be8def /src
parentadd error handling (diff)
add ClientLogin
Diffstat (limited to 'src')
-rw-r--r--src/net.rs22
-rw-r--r--src/protocol/message/handshake.rs148
-rw-r--r--src/protocol/message/handshake/types.rs2
-rw-r--r--src/tests/handshake_types.rs26
-rw-r--r--src/tests/mod.rs4
5 files changed, 183 insertions, 19 deletions
diff --git a/src/net.rs b/src/net.rs
index f1f7027..145256a 100644
--- a/src/net.rs
+++ b/src/net.rs
@@ -23,23 +23,35 @@ pub struct Client {
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 crate::protocol::message::handshake::{ClientInitAck, ClientLogin, ClientLoginAck, SessionInit};
self.tcp_stream.write(&client.serialize().unwrap()).unwrap();
let mut buf: Vec<u8> = [0; 2048].to_vec();
let len = VariantMap::read(&mut self.tcp_stream, &mut buf).unwrap();
buf.truncate(len);
+ let res = ClientInitAck::parse(&buf).unwrap();
+ println!("res: {:?}", res);
- let res = ClientInitAck::parse(&buf);
- println!("{:?}", res)
+ let login = ClientLogin {user: user.to_string(), password: pass.to_string()};
+ self.tcp_stream.write(&login.serialize().unwrap()).unwrap();
+
+ let mut buf: Vec<u8> = [0; 2048].to_vec();
+ let len = VariantMap::read(&mut self.tcp_stream, &mut buf).unwrap();
+ buf.truncate(len);
+ let _res = ClientLoginAck::parse(&buf).unwrap();
+
+ let mut buf: Vec<u8> = [0; 2048].to_vec();
+ let len = VariantMap::read(&mut self.tcp_stream, &mut buf).unwrap();
+ buf.truncate(len);
+ let res = SessionInit::parse(&buf).unwrap();
+
+ println!("res: {:?}", res);
}
}
pub fn connect(address: &'static str, port: u32, tls: bool, compression: bool) -> Result<Client, Error> {
- use crate::protocol::primitive::serialize::Serialize;
use crate::protocol::primitive::deserialize::Deserialize;
- use crate::protocol::primitive::qread::QRead;
//let mut s = BufWriter::new(TcpStream::connect(format!("{}:{}", address, port)).unwrap());
let mut s = TcpStream::connect(format!("{}:{}", address, port)).unwrap();
diff --git a/src/protocol/message/handshake.rs b/src/protocol/message/handshake.rs
index 7713ed9..44c1073 100644
--- a/src/protocol/message/handshake.rs
+++ b/src/protocol/message/handshake.rs
@@ -99,7 +99,7 @@ pub struct ClientInitAck {
impl HandshakeSerialize for ClientInitAck {
fn serialize(&self) -> Result<Vec<u8>, ErrorKind> {
- let mut values: VariantMap = VariantMap::with_capacity(2);
+ 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));
@@ -135,3 +135,149 @@ impl HandshakeDeserialize for ClientInitAck {
}
}
}
+
+#[derive(Debug)]
+pub struct ClientLogin {
+ pub user: String,
+ pub password: String
+}
+
+impl HandshakeSerialize for ClientLogin {
+ fn serialize(&self) -> Result<Vec<u8>, ErrorKind> {
+ 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), ErrorKind> {
+ let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?;
+
+ let msgtypev = &values["MsgType"];
+ let msgtype;
+ match msgtypev {
+ Variant::String(x) => msgtype = x,
+ Variant::StringUTF8(x) => msgtype = x,
+ _ => return Err(ErrorKind::WrongVariant)
+ };
+
+ if msgtype == "ClientLogin" {
+ return Ok((len, Self {
+ user: match_variant!(values, Variant::String, "User"),
+ password: match_variant!(values, Variant::String, "Password")
+ }));
+ } else {
+ return Err(ErrorKind::WrongMsgType);
+ }
+ }
+}
+
+#[derive(Debug)]
+pub struct ClientLoginAck;
+
+impl HandshakeSerialize for ClientLoginAck {
+ fn serialize(&self) -> Result<Vec<u8>, ErrorKind> {
+ 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), ErrorKind> {
+ let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?;
+
+ let msgtypev = &values["MsgType"];
+ let msgtype;
+ match msgtypev {
+ Variant::String(x) => msgtype = x,
+ Variant::StringUTF8(x) => msgtype = x,
+ _ => return Err(ErrorKind::WrongVariant)
+ };
+
+ if msgtype == "ClientLogin" {
+ return Ok((len, Self {}));
+ } else {
+ return Err(ErrorKind::WrongMsgType);
+ }
+ }
+}
+
+#[derive(Debug)]
+pub struct ClientLoginReject {
+ error: String
+}
+
+impl HandshakeSerialize for ClientLoginReject {
+ fn serialize(&self) -> Result<Vec<u8>, ErrorKind> {
+ 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), ErrorKind> {
+ let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?;
+
+ let msgtypev = &values["MsgType"];
+ let msgtype;
+ match msgtypev {
+ Variant::String(x) => msgtype = x,
+ Variant::StringUTF8(x) => msgtype = x,
+ _ => return Err(ErrorKind::WrongVariant)
+ };
+
+ if msgtype == "ClientLogin" {
+ return Ok((len, Self { error: match_variant!(values, Variant::String, "ErrorString")}));
+ } else {
+ return Err(ErrorKind::WrongMsgType);
+ }
+ }
+}
+
+#[derive(Debug)]
+pub struct SessionInit {
+ identities: VariantList,
+ buffers: VariantList,
+ network_ids: VariantList,
+}
+
+impl HandshakeSerialize for SessionInit {
+ fn serialize(&self) -> Result<Vec<u8>, ErrorKind> {
+ 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), ErrorKind> {
+ let (len, values): (usize, VariantMap) = HandshakeDeserialize::parse(b)?;
+
+ let msgtypev = &values["MsgType"];
+ let msgtype;
+ match msgtypev {
+ Variant::String(x) => msgtype = x,
+ Variant::StringUTF8(x) => msgtype = x,
+ _ => return Err(ErrorKind::WrongVariant)
+ };
+
+ 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 {
+ return Err(ErrorKind::WrongMsgType);
+ }
+ }
+}
diff --git a/src/protocol/message/handshake/types.rs b/src/protocol/message/handshake/types.rs
index be290e9..f099b0f 100644
--- a/src/protocol/message/handshake/types.rs
+++ b/src/protocol/message/handshake/types.rs
@@ -81,7 +81,7 @@ impl HandshakeQRead for VariantMap {
let mut pos = 8;
let len: usize = len as usize;
loop {
- if pos >= (len - 4) { break; }
+ if pos >= len { break; }
pos += Variant::read(s, &mut b[pos..])?;
pos += Variant::read(s, &mut b[pos..])?;
}
diff --git a/src/tests/handshake_types.rs b/src/tests/handshake_types.rs
index a664128..701b871 100644
--- a/src/tests/handshake_types.rs
+++ b/src/tests/handshake_types.rs
@@ -18,18 +18,25 @@ pub fn serialize_variantmap() {
pub fn read_variantmap() {
use std::io::Cursor;
- let test_bytes: Vec<u8> = vec![0, 0, 0, 39, 0, 0, 0, 10, 0, 0, 0, 10, 0,
+ let test_bytes: Vec<u8> = vec![0, 0, 0, 67, 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];
+ 0, 0, 0, 1, 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];
- let mut buf: Vec<u8> = [0; 43].to_vec();
+ let mut buf: Vec<u8> = [0; 78].to_vec();
let len = VariantMap::read(&mut Cursor::new(&test_bytes), &mut buf).unwrap();
- assert_eq!(len, 43);
+ assert_eq!(len, 78);
- let result_bytes: Vec<u8> = vec![0, 0, 0, 39, 0, 0, 0, 10, 0, 0, 0, 10, 0,
+ let result_bytes: Vec<u8> = vec![0, 0, 0, 67, 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, 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];
assert_eq!(buf, result_bytes);
}
@@ -50,8 +57,11 @@ pub fn deserialize_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];
+ 0, 0, 0, 10, 67, 111, 110, 102, 105, 103, 117, 114, 101, 100,
+ 0, 0, 0, 1, 0, 1,
+// 0, 0, 0, 10, 67, 111, 110, 102, 105, 103, 117, 114
+// 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));
diff --git a/src/tests/mod.rs b/src/tests/mod.rs
index 873046b..882f75c 100644
--- a/src/tests/mod.rs
+++ b/src/tests/mod.rs
@@ -2,7 +2,3 @@
pub mod handshake_types;
pub mod variant_types;
pub mod base_types;
-
-use handshake_types::*;
-use variant_types::*;
-use base_types::*;