aboutsummaryrefslogtreecommitdiff
path: root/src/message/handshake/init.rs
blob: 860df8a71fa16333da0244ab50c330a13134526c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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;
        }

        // Select Protocol 2: Datastream

        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());

        return init;
    }

    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;
    }
}