aboutsummaryrefslogtreecommitdiff
path: root/src/message/handshake/init.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/message/handshake/init.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/message/handshake/init.rs b/src/message/handshake/init.rs
new file mode 100644
index 0000000..e4c0fa9
--- /dev/null
+++ b/src/message/handshake/init.rs
@@ -0,0 +1,61 @@
+use crate::Deserialize;
+use crate::Serialize;
+
+/// The first few bytes sent to the core to initialize the connection and setup if we want to use tls and compression
+pub struct Init {
+ pub tls: bool,
+ pub compression: bool,
+}
+
+impl Init {
+ pub fn new() -> Self {
+ Self {
+ tls: false,
+ compression: false,
+ }
+ }
+
+ pub fn compression(mut self, v: bool) {
+ self.compression = v
+ }
+
+ pub fn tls(mut self, v: bool) {
+ self.tls = v
+ }
+
+ pub fn serialize(self) -> Vec<u8> {
+ // The handshake message
+ let mut handshake: u32 = 0x42b33f00;
+
+ // If TLS is enabled set the TLS bit on the handshake
+ if self.tls {
+ handshake |= 0x01;
+ }
+
+ // If COMPRESSION is enabled set the COMPRESSION bit on the handshake
+ if self.compression {
+ handshake |= 0x02;
+ }
+
+ return handshake.serialize().unwrap();
+ }
+
+ pub fn parse(buf: &[u8]) -> Self {
+ let (_, handshake) = u32::parse(&buf[0..4]).unwrap();
+
+ let mut init = Self {
+ tls: false,
+ compression: false,
+ };
+
+ if (handshake & 0x01) >= 1 {
+ init.tls = true
+ }
+
+ if (handshake & 0x02) >= 1 {
+ init.tls = true
+ }
+
+ return init;
+ }
+}