diff options
Diffstat (limited to 'src/message/handshake')
| -rw-r--r-- | src/message/handshake/init.rs | 19 | ||||
| -rw-r--r-- | src/message/handshake/protocol.rs | 15 | ||||
| -rw-r--r-- | src/message/handshake/types.rs | 2 |
3 files changed, 21 insertions, 15 deletions
diff --git a/src/message/handshake/init.rs b/src/message/handshake/init.rs index e303802..33c6fbb 100644 --- a/src/message/handshake/init.rs +++ b/src/message/handshake/init.rs @@ -1,4 +1,7 @@ -use crate::serialize::{Deserialize, Serialize}; +use crate::{ + serialize::{Deserialize, Serialize}, + ProtocolError, +}; /// The first few bytes sent to the core to initialize the connection and setup if we want to use tls and compression #[derive(Clone, Debug, Default)] @@ -22,7 +25,7 @@ impl Init { self } - pub fn serialize(self) -> Vec<u8> { + pub fn serialize(self) -> Result<Vec<u8>, ProtocolError> { // The handshake message let mut handshake: u32 = 0x42b33f00; @@ -41,14 +44,14 @@ impl Init { let mut init: Vec<u8> = vec![]; // Add handshake and protocol to our buffer - init.extend(handshake.serialize().unwrap()); - init.extend(crate::message::Protocol::Datastream.serialize()); + init.extend(handshake.serialize()?); + init.extend(crate::message::Protocol::Datastream.serialize()?); - init + Ok(init) } - pub fn parse(buf: &[u8]) -> Self { - let (_, handshake) = u32::parse(&buf[0..4]).unwrap(); + pub fn parse(buf: &[u8]) -> Result<Self, ProtocolError> { + let (_, handshake) = u32::parse(&buf[0..4])?; let mut init = Self { tls: false, @@ -63,6 +66,6 @@ impl Init { init.tls = true } - init + Ok(init) } } diff --git a/src/message/handshake/protocol.rs b/src/message/handshake/protocol.rs index 2a7d9ac..0dc419f 100644 --- a/src/message/handshake/protocol.rs +++ b/src/message/handshake/protocol.rs @@ -1,4 +1,7 @@ -use crate::serialize::{Deserialize, Serialize}; +use crate::{ + serialize::{Deserialize, Serialize}, + ProtocolError, +}; #[derive(Debug, Default)] pub enum Protocol { @@ -12,17 +15,17 @@ impl Protocol { Protocol::default() } - pub fn serialize(self) -> Vec<u8> { + pub fn serialize(self) -> Result<Vec<u8>, ProtocolError> { let proto: u32 = 0x80000002; - proto.serialize().unwrap() + proto.serialize() } - pub fn parse(buf: &[u8]) -> Self { + pub fn parse(buf: &[u8]) -> Result<Self, ProtocolError> { let mut protolist: Vec<u32> = Vec::new(); let mut pos = 0; loop { - let (_, proto) = u32::parse(&buf[pos..(pos + 4)]).unwrap(); + let (_, proto) = u32::parse(&buf[pos..(pos + 4)])?; if (proto & 0x80000000) >= 1 { protolist.push(proto - 0x80000000); break; @@ -32,6 +35,6 @@ impl Protocol { } } - Protocol::Datastream + Ok(Protocol::Datastream) } } diff --git a/src/message/handshake/types.rs b/src/message/handshake/types.rs index 9bd8aec..91248c4 100644 --- a/src/message/handshake/types.rs +++ b/src/message/handshake/types.rs @@ -19,7 +19,7 @@ impl HandshakeSerialize for VariantMap { res.extend(v.serialize()?); } - let len: i32 = (self.len() * 2).try_into().unwrap(); + let len: i32 = (self.len() * 2).try_into()?; util::insert_bytes(0, &mut res, &mut (len).to_be_bytes()); Ok(res) |
