diff options
| author | zedddie <rust@zedddie.rs> | 2026-03-23 19:11:40 +0100 |
|---|---|---|
| committer | tuturuu <zedddiezxc@gmail.com> | 2026-03-23 19:11:40 +0100 |
| commit | d0513bec6a51ee5d27cc90003945185438eca4a6 (patch) | |
| tree | d3796acd77d04dad0466333d12506709fb586fdc /src/sniffing/headers.rs | |
| parent | 1a5b7da6ae20cece911c05b1384a3373239772a3 (diff) | |
impl Display trait for PacketInfo
Diffstat (limited to 'src/sniffing/headers.rs')
| -rw-r--r-- | src/sniffing/headers.rs | 57 |
1 files changed, 38 insertions, 19 deletions
diff --git a/src/sniffing/headers.rs b/src/sniffing/headers.rs index f5b3477..4514e3b 100644 --- a/src/sniffing/headers.rs +++ b/src/sniffing/headers.rs @@ -12,11 +12,11 @@ pub enum Protocol { } type SourceV4Ip = Ipv4; type SourceV6Ip = Ipv6; +#[derive(PartialEq, Debug)] pub enum IpVersion { V4, V6 } -// type IpVersion = String; type Ipv4 = [u8; 4]; type Ipv6 = [u8; 16]; type Port = u16; @@ -44,12 +44,16 @@ 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()) + if self.version() == &IpVersion::V4 { + let src_ip = self.src_ipv4_ip().unwrap(); + let dst_ip = self.dst_ipv4_ip().unwrap(); + write!(f, "{:?} {}.{}.{}.{}:{} -> {}.{}.{}.{}:{} {:?} is 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()) + } else { + let src_ip = self.src_ipv6_ip().unwrap(); + let dst_ip = self.dst_ipv6_ip().unwrap(); + // y:y:y:y:y:y:y:y = 8 hexademical + write!(f, "{:?} {}:{}:{}:{}:{}:{}:{}:{} port:{} -> {}:{}:{}:{}:{}:{}:{}:{} port:{} {:?} is dns? {:?}", self.version(), src_ip[0], src_ip[1], src_ip[2], src_ip[3], src_ip[4], src_ip[5], src_ip[6], src_ip[7], self.src_port(), dst_ip[0], dst_ip[1], dst_ip[2], dst_ip[3], dst_ip[4], dst_ip[5], dst_ip[6], dst_ip[7], self.dst_port(), self.protocol(), self.dns()) } - // write!(f, "{} {}:{} -> {}:{} PROTO {} DNS? {}", self.version(), self.) } } @@ -60,26 +64,41 @@ impl PacketInfo { PacketInfo::V6 { dns, ..} => dns, } } - pub fn dst_ipv4_ip(&self) -> &SourceV4Ip { + pub fn src_ipv6_ip(&self) -> Option<&SourceV6Ip> { 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.") + PacketInfo::V6 { src_ip, .. } => Some(src_ip), + _ => None } } - pub fn src_ipv6_ip(&self) -> &SourceV6Ip { + pub fn dst_ipv6_ip(&self) -> Option<&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.") + PacketInfo::V6 { dst_ip, .. } => Some(dst_ip), + _ => None } } - pub fn src_ipv4_ip(&self) -> &SourceV4Ip { + pub fn src_ipv4_ip(&self) -> Option<&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.") + PacketInfo::V4 { src_ip, .. } => Some(src_ip), + _ => None, } } - pub fn src_ipv6_ip(&self) -> &SourceV6Ip { - PacketInfo::V6.src_ip + pub fn dst_ipv4_ip(&self) -> Option<&SourceV4Ip> { + match self { + PacketInfo::V4 { dst_ip, .. } => Some(dst_ip), + _ => None + } + } + pub fn src_port(&self) -> &Port { + match self { + PacketInfo::V4 { src_port, .. } => src_port, + PacketInfo::V6 { src_port, .. } => src_port + } + } + pub fn dst_port(&self) -> &Port { + match self { + PacketInfo::V4 { dst_port, .. } => dst_port, + PacketInfo::V6 { dst_port, .. } => dst_port + } } pub fn version(&self) -> &IpVersion { match self { @@ -123,7 +142,7 @@ pub fn sniff_raw_packets(packet: &Packet) -> SniffedPacket { dns }; if !matches!(v4.protocol(), Protocol::Unsupported(_)) { - println!("{v4:?}"); + println!("{v4}"); } else { // TODO: make --debug option which will include this diagnostic, for general use this // should be off @@ -148,7 +167,7 @@ pub fn sniff_raw_packets(packet: &Packet) -> SniffedPacket { dns }; if !matches!(v6.protocol(), Protocol::Unsupported(_)) { - println!("{v6:?}"); + println!("{v6}"); } else { // TODO: make --debug option which will include this diagnostic, for general use this // should be off |
