Coverage Report

Created: 2026-11-12 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata8/rust/src/direction.rs
Line
Count
Source
1
/* Copyright (C) 2017-2025 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17
18
pub const DIR_BOTH: u8 = 0b0000_1100;
19
const DIR_TOSERVER: u8 = 0b0000_0100;
20
const DIR_TOCLIENT: u8 = 0b0000_1000;
21
22
#[repr(C)]
23
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
24
pub enum Direction {
25
    ToServer = 0x04,
26
    ToClient = 0x08,
27
}
28
29
impl Direction {
30
    /// Return true if the direction is to server.
31
2.47M
    pub fn is_to_server(&self) -> bool {
32
2.47M
        matches!(self, Self::ToServer)
33
2.47M
    }
34
35
    /// Return true if the direction is to client.
36
2.47M
    pub fn is_to_client(&self) -> bool {
37
2.47M
        matches!(self, Self::ToClient)
38
2.47M
    }
39
40
110
    pub fn index(&self) -> usize {
41
110
        match self {
42
68
            Self::ToClient => 0,
43
42
            _ => 1,
44
        }
45
110
    }
46
}
47
48
impl Default for Direction {
49
12.9k
    fn default() -> Self {
50
12.9k
        Direction::ToServer
51
12.9k
    }
52
}
53
54
impl std::fmt::Display for Direction {
55
0
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56
0
        match self {
57
0
            Self::ToServer => write!(f, "toserver"),
58
0
            Self::ToClient => write!(f, "toclient"),
59
        }
60
0
    }
61
}
62
63
impl From<u8> for Direction {
64
57.5M
    fn from(d: u8) -> Self {
65
57.5M
        if d & (DIR_TOSERVER | DIR_TOCLIENT) == (DIR_TOSERVER | DIR_TOCLIENT) {
66
0
            debug_validate_fail!("Both directions are set");
67
0
            Direction::ToServer
68
57.5M
        } else if d & DIR_TOSERVER != 0 {
69
24.5M
            Direction::ToServer
70
32.9M
        } else if d & DIR_TOCLIENT != 0 {
71
32.9M
            Direction::ToClient
72
        } else {
73
0
            debug_validate_fail!("Unknown direction!!");
74
0
            Direction::ToServer
75
        }
76
57.5M
    }
77
}
78
79
impl From<Direction> for u8 {
80
285M
    fn from(d: Direction) -> u8 {
81
285M
        d as u8
82
285M
    }
83
}
84
85
#[cfg(test)]
86
mod test {
87
    use super::*;
88
89
    #[test]
90
    fn test_direction() {
91
        assert!(Direction::ToServer.is_to_server());
92
        assert!(!Direction::ToServer.is_to_client());
93
94
        assert!(Direction::ToClient.is_to_client());
95
        assert!(!Direction::ToClient.is_to_server());
96
    }
97
}