/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 | 18.9M | pub fn is_to_server(&self) -> bool { |
32 | 18.9M | matches!(self, Self::ToServer) |
33 | 18.9M | } |
34 | | |
35 | | /// Return true if the direction is to client. |
36 | 18.9M | pub fn is_to_client(&self) -> bool { |
37 | 18.9M | matches!(self, Self::ToClient) |
38 | 18.9M | } |
39 | | |
40 | 0 | pub fn index(&self) -> usize { |
41 | 0 | match self { |
42 | 0 | Self::ToClient => 0, |
43 | 0 | _ => 1, |
44 | | } |
45 | 0 | } |
46 | | } |
47 | | |
48 | | impl Default for Direction { |
49 | 313k | fn default() -> Self { |
50 | 313k | Direction::ToServer |
51 | 313k | } |
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 | 138M | fn from(d: u8) -> Self { |
65 | 138M | if d & (DIR_TOSERVER | DIR_TOCLIENT) == (DIR_TOSERVER | DIR_TOCLIENT) { |
66 | 0 | debug_validate_fail!("Both directions are set"); |
67 | 0 | Direction::ToServer |
68 | 138M | } else if d & DIR_TOSERVER != 0 { |
69 | 65.4M | Direction::ToServer |
70 | 72.8M | } else if d & DIR_TOCLIENT != 0 { |
71 | 72.8M | Direction::ToClient |
72 | | } else { |
73 | 0 | debug_validate_fail!("Unknown direction!!"); |
74 | 0 | Direction::ToServer |
75 | | } |
76 | 138M | } |
77 | | } |
78 | | |
79 | | impl From<Direction> for u8 { |
80 | 1.56G | fn from(d: Direction) -> u8 { |
81 | 1.56G | d as u8 |
82 | 1.56G | } |
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 | | } |