diff options
| author | luwenpeng <[email protected]> | 2023-08-18 12:18:18 +0800 |
|---|---|---|
| committer | luwenpeng <[email protected]> | 2023-08-18 14:04:25 +0800 |
| commit | d1b6c9b332b8ce6ab3e48bbb02797801c39b5f1d (patch) | |
| tree | 5bbd48a1f2d0fc461b3e7bd5dd92de7519aff2f5 | |
| parent | b4d0d78a8d9e95363552a1d032f8e9eb564862c1 (diff) | |
[feature] Add five tuple
| -rw-r--r-- | src/lib.rs | 3 | ||||
| -rw-r--r-- | src/session/mod.rs | 1 | ||||
| -rw-r--r-- | src/session/tuple.rs | 123 |
3 files changed, 126 insertions, 1 deletions
@@ -1,3 +1,4 @@ pub mod packet; pub mod protocol; -pub mod event;
\ No newline at end of file +pub mod event; +pub mod session;
\ No newline at end of file diff --git a/src/session/mod.rs b/src/session/mod.rs new file mode 100644 index 0000000..682d981 --- /dev/null +++ b/src/session/mod.rs @@ -0,0 +1 @@ +pub mod tuple; diff --git a/src/session/tuple.rs b/src/session/tuple.rs new file mode 100644 index 0000000..08a4fe1 --- /dev/null +++ b/src/session/tuple.rs @@ -0,0 +1,123 @@ +use std::net::Ipv4Addr; +use std::net::Ipv6Addr; + +/****************************************************************************** + * Struct + ******************************************************************************/ + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Address { + V4(Ipv4Addr), + V6(Ipv6Addr), +} + +impl ToString for Address { + fn to_string(&self) -> String { + match self { + Address::V4(ip) => ip.to_string(), + Address::V6(ip) => ip.to_string(), + } + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct FiveTuple { + src_ip: Address, + dst_ip: Address, + src_port: u16, + dst_port: u16, + protocol: u8, +} + +/****************************************************************************** + * API + ******************************************************************************/ + +impl FiveTuple { + pub fn from_v4( + src_ip: Ipv4Addr, + dst_ip: Ipv4Addr, + src_port: u16, + dst_port: u16, + protocol: u8, + ) -> FiveTuple { + FiveTuple { + src_ip: Address::V4(src_ip), + dst_ip: Address::V4(dst_ip), + src_port: src_port, + dst_port: dst_port, + protocol: protocol, + } + } + + pub fn from_v6( + src_ip: Ipv6Addr, + dst_ip: Ipv6Addr, + src_port: u16, + dst_port: u16, + protocol: u8, + ) -> FiveTuple { + FiveTuple { + src_ip: Address::V6(src_ip), + dst_ip: Address::V6(dst_ip), + src_port: src_port, + dst_port: dst_port, + protocol: protocol, + } + } + + pub fn to_string(&self) -> String { + let mut session_id = String::new(); + match self.src_ip { + Address::V4(_) => session_id.push_str("4:"), + Address::V6(_) => session_id.push_str("6:"), + } + 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; + use std::net::Ipv4Addr; + use std::net::Ipv6Addr; + + #[test] + fn test_five_tuple() { + let five_tuple_4 = FiveTuple::from_v4( + Ipv4Addr::new(192, 168, 0, 1), + Ipv4Addr::new(192, 168, 0, 2), + 2345, + 80, + 1, + ); + let session_id_4 = five_tuple_4.to_string(); + assert_eq!(session_id_4, "4:192.168.0.1:2345-192.168.0.2:80:1"); + + let five_tuple_6 = FiveTuple::from_v6( + Ipv6Addr::new(0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88), + Ipv6Addr::new(0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff), + 2345, + 80, + 1, + ); + let session_id_6 = five_tuple_6.to_string(); + assert_eq!( + session_id_6, + "6:11:22:33:44:55:66:77:88:2345-88:99:aa:bb:cc:dd:ee:ff:80:1" + ); + } +} |
