diff options
| author | zedddie <rust@zedddie.rs> | 2026-03-17 23:15:47 +0100 |
|---|---|---|
| committer | tuturuu <zedddiezxc@gmail.com> | 2026-03-17 23:15:47 +0100 |
| commit | 1a5b7da6ae20cece911c05b1384a3373239772a3 (patch) | |
| tree | e1ca1c814bf4cb89ec6eb7b87800e44fba321bb7 | |
| parent | 50524cb5aee1f1f4464f60467122a44befc11203 (diff) | |
really broken WIP for now xD
| -rw-r--r-- | src/sniffing/headers.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/sniffing/headers.rs b/src/sniffing/headers.rs index 2472981..f5b3477 100644 --- a/src/sniffing/headers.rs +++ b/src/sniffing/headers.rs @@ -1,4 +1,5 @@ use tun::Error; +use std::fmt; // Here we will recieve bytes and try to get their destanation & apply Rules for them. use crate::config::Config; @@ -9,6 +10,13 @@ pub enum Protocol { UDP, Unsupported(u8) } +type SourceV4Ip = Ipv4; +type SourceV6Ip = Ipv6; +pub enum IpVersion { + V4, + V6 +} +// type IpVersion = String; type Ipv4 = [u8; 4]; type Ipv6 = [u8; 16]; type Port = u16; @@ -34,7 +42,51 @@ pub enum PacketInfo { } } +impl fmt::Display for PacketInfo { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.version() == "Ipv4" { + let src_ip = self.src_ipv4_ip(); + let dst_ip = self.dst_ipv4_ip(); + write!(f, "{} {}.{}.{}.{}:{} -> {}.{}.{}.{}:{} PROTO {} DNS? {}", self.version(), src_ip[0], src_ip[1], src_ip[2], src_ip[3], self.src_port, dst_ip[0], dst_ip[1], dst_ip[2], dst_ip[3], self.dst_port(), self.protocol(), self.dns()) + } + // write!(f, "{} {}:{} -> {}:{} PROTO {} DNS? {}", self.version(), self.) + } +} + impl PacketInfo { + pub fn dns(&self) -> &bool { + match self { + PacketInfo::V4 { dns, ..} => dns, + PacketInfo::V6 { dns, ..} => dns, + } + } + pub fn dst_ipv4_ip(&self) -> &SourceV4Ip { + match self { + PacketInfo::V4 { dst_ip, .. } => dst_ip, + _ => &[0x0, 0x0, 0x0, 0x0].try_into().expect("this never should fail or even be called in the first place.") + } + } + pub fn src_ipv6_ip(&self) -> &SourceV6Ip { + match self { + PacketInfo::V6 { src_ip, .. } => src_ip, + _ => &[0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0].try_into().expect("this never should fail or even be called in the first place.") + } + } + pub fn src_ipv4_ip(&self) -> &SourceV4Ip { + match self { + PacketInfo::V4 { src_ip, .. } => src_ip, + _ => &[0x0, 0x0, 0x0, 0x0].try_into().expect("this never should fail or even be called in the first place.") + } + } + pub fn src_ipv6_ip(&self) -> &SourceV6Ip { + PacketInfo::V6.src_ip + } + pub fn version(&self) -> &IpVersion { + match self { + PacketInfo::V4 { .. }=> &IpVersion::V4, + PacketInfo::V6 { .. }=> &IpVersion::V6 + } + } pub fn protocol(&self) -> &Protocol { match self { PacketInfo::V4 { protocol, .. } => protocol, |
