summaryrefslogtreecommitdiff
path: root/src/session/tuple.rs
blob: 16616cb8e29e9f681ddc96985f8fee4f30883234 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/******************************************************************************
 * Struct
 ******************************************************************************/

#[derive(Debug, PartialEq)]
pub struct FiveTuple {
    src_ip: String,
    src_port: u16,
    dst_ip: String,
    dst_port: u16,
    protocol: u8,
}

/******************************************************************************
 * API
 ******************************************************************************/

impl FiveTuple {
    pub fn new(
        src_ip: String,
        src_port: u16,
        dst_ip: String,
        dst_port: u16,
        protocol: u8,
    ) -> FiveTuple {
        FiveTuple {
            src_ip,
            src_port,
            dst_ip,
            dst_port,
            protocol,
        }
    }

    pub fn to_string(&self) -> String {
        let mut session_id = String::new();
        session_id.push_str(&self.src_ip.to_string());
        session_id.push_str(":");
        session_id.push_str(&self.src_port.to_string());
        session_id.push_str("-");
        session_id.push_str(&self.dst_ip.to_string());
        session_id.push_str(":");
        session_id.push_str(&self.dst_port.to_string());
        session_id.push_str("-");
        session_id.push_str(&self.protocol.to_string());
        session_id
    }
}

/******************************************************************************
 * TEST
 ******************************************************************************/

#[cfg(test)]
mod tests {
    use super::FiveTuple;

    #[test]
    fn test_five_tuple() {
        let five_tuple_4 = FiveTuple::new(
            "192.168.0.1".to_string(),
            2345,
            "192.168.0.2".to_string(),
            80,
            1,
        );
        let session_id_4 = five_tuple_4.to_string();
        assert_eq!(session_id_4, "192.168.0.1:2345-192.168.0.2:80-1");

        let five_tuple_6 = FiveTuple::new(
            "11:22:33:44:55:66:77:88".to_string(),
            2345,
            "88:99:aa:bb:cc:dd:ee:ff".to_string(),
            80,
            1,
        );
        let session_id_6 = five_tuple_6.to_string();
        assert_eq!(
            session_id_6,
            "11:22:33:44:55:66:77:88:2345-88:99:aa:bb:cc:dd:ee:ff:80-1"
        );
    }
}