aboutsummaryrefslogtreecommitdiff
path: root/src/message/handshake/connack.rs
diff options
context:
space:
mode:
authorMax Audron <audron@cocaine.farm>2020-09-26 12:01:27 +0200
committerMax Audron <audron@cocaine.farm>2020-09-26 12:03:01 +0200
commit3bdae21716d10032f70a0a889070766bbbe4d141 (patch)
tree78a1ddf98601fbbc03a9375626dfa7923ab85521 /src/message/handshake/connack.rs
parentadd parsing of signalproxy messages (diff)
split handshake.rs
Diffstat (limited to 'src/message/handshake/connack.rs')
-rw-r--r--src/message/handshake/connack.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/message/handshake/connack.rs b/src/message/handshake/connack.rs
new file mode 100644
index 0000000..222c08c
--- /dev/null
+++ b/src/message/handshake/connack.rs
@@ -0,0 +1,59 @@
+use failure::Error;
+
+/// Data received right after initializing the connection
+///
+/// ConnAck is serialized sequentially
+#[derive(Debug)]
+pub struct ConnAck {
+ /// The Flag 0x01 for TLS
+ /// and 0x02 for Deflate Compression
+ pub flags: u8,
+ /// Some extra protocol version specific data
+ /// So far unused
+ pub extra: i16,
+ /// The version of the protocol
+ /// 0x00000001 for the legacy protocol
+ /// 0x00000002 for the datastream protocol
+ ///
+ /// Only the datastream protocol is supported by this crate
+ pub version: i8,
+}
+
+impl Default for ConnAck {
+ fn default() -> Self {
+ Self {
+ flags: 0x00,
+ extra: 0x00,
+ version: 0x00000002,
+ }
+ }
+}
+
+impl crate::Serialize for ConnAck {
+ fn serialize(&self) -> Result<Vec<std::primitive::u8>, Error> {
+ let mut bytes: Vec<u8> = Vec::new();
+
+ bytes.append(&mut self.flags.serialize()?);
+ bytes.append(&mut self.extra.serialize()?);
+ bytes.append(&mut self.version.serialize()?);
+
+ Ok(bytes)
+ }
+}
+
+impl crate::Deserialize for ConnAck {
+ fn parse(b: &[u8]) -> Result<(usize, Self), Error> {
+ let (flen, flags) = u8::parse(b)?;
+ let (elen, extra) = i16::parse(&b[flen..])?;
+ let (vlen, version) = i8::parse(&b[(flen + elen)..])?;
+
+ return Ok((
+ flen + elen + vlen,
+ Self {
+ flags,
+ extra,
+ version,
+ },
+ ));
+ }
+}