diff --git a/oryx-common/src/ip.rs b/oryx-common/src/ip.rs index 2af062c..501668c 100644 --- a/oryx-common/src/ip.rs +++ b/oryx-common/src/ip.rs @@ -1,14 +1,43 @@ -use core::{fmt::Display, net::IpAddr}; +use core::fmt::Display; -use network_types::{icmp::IcmpHdr, tcp::TcpHdr, udp::UdpHdr}; +use core::net::{Ipv4Addr, Ipv6Addr}; + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub enum IpPacket { + V4(Ipv4Packet), + V6(Ipv6Packet), +} + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct Ipv4Packet { + pub src_ip: Ipv4Addr, + pub dst_ip: Ipv4Addr, + pub proto: IpProto, +} + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct Ipv6Packet { + pub src_ip: Ipv6Addr, + pub dst_ip: Ipv6Addr, + pub proto: IpProto, +} + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub enum IpProto { + Tcp(TcpPacket), + Udp(UdpPacket), + Icmp(IcmpPacket), +} #[repr(C)] #[derive(Debug, Copy, Clone)] pub struct TcpPacket { pub dst_port: u16, pub src_port: u16, - pub dst_ip: IpAddr, - pub src_ip: IpAddr, } #[repr(C)] @@ -16,16 +45,12 @@ pub struct TcpPacket { pub struct UdpPacket { pub dst_port: u16, pub src_port: u16, - pub dst_ip: IpAddr, - pub src_ip: IpAddr, } #[repr(C)] #[derive(Debug, Copy, Clone)] pub struct IcmpPacket { pub icmp_type: IcmpType, - pub dst_ip: IpAddr, - pub src_ip: IpAddr, } #[repr(C)] @@ -52,42 +77,59 @@ impl Display for IcmpType { } } -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub enum IpPacket { - Tcp(TcpPacket), - Udp(UdpPacket), - Icmp(IcmpPacket), -} - -#[repr(C)] -#[derive(Copy, Clone)] -pub enum ProtoHdr { - Tcp(TcpHdr), - Udp(UdpHdr), - Icmp(IcmpHdr), -} - impl Display for IpPacket { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self { - IpPacket::Tcp(p) => { - write!( - f, - "{} {} {} {} TCP", - p.src_ip, p.src_port, p.dst_ip, p.dst_port - ) - } - IpPacket::Udp(p) => { - write!( - f, - "{} {} {} {} UDP", - p.src_ip, p.src_port, p.dst_ip, p.dst_port - ) - } - IpPacket::Icmp(p) => { - write!(f, "{} {} ICMP", p.src_ip, p.dst_ip) - } + IpPacket::V4(ipv4_packet) => match ipv4_packet.proto { + IpProto::Tcp(tcp_packet) => { + write!( + f, + "{} {} {} {} TCP", + ipv4_packet.src_ip, + tcp_packet.src_port, + ipv4_packet.dst_ip, + tcp_packet.dst_port + ) + } + IpProto::Udp(udp_packet) => { + write!( + f, + "{} {} {} {} UDP", + ipv4_packet.src_ip, + udp_packet.src_port, + ipv4_packet.dst_ip, + udp_packet.dst_port + ) + } + IpProto::Icmp(_) => { + write!(f, "{} {} ICMP", ipv4_packet.src_ip, ipv4_packet.dst_ip) + } + }, + IpPacket::V6(ipv6_packet) => match ipv6_packet.proto { + IpProto::Tcp(tcp_packet) => { + write!( + f, + "{} {} {} {} TCP", + ipv6_packet.src_ip, + tcp_packet.src_port, + ipv6_packet.dst_ip, + tcp_packet.dst_port + ) + } + IpProto::Udp(udp_packet) => { + write!( + f, + "{} {} {} {} UDP", + ipv6_packet.src_ip, + udp_packet.src_port, + ipv6_packet.dst_ip, + udp_packet.dst_port + ) + } + IpProto::Icmp(_) => { + write!(f, "{} {} ICMP", ipv6_packet.src_ip, ipv6_packet.dst_ip) + } + }, } } } diff --git a/oryx-common/src/lib.rs b/oryx-common/src/lib.rs index fda3a56..1585fc4 100644 --- a/oryx-common/src/lib.rs +++ b/oryx-common/src/lib.rs @@ -1,14 +1,10 @@ #![no_std] -use core::{ - fmt::Display, - mem, - net::{IpAddr, Ipv4Addr}, -}; +use core::{fmt::Display, mem, net::Ipv4Addr}; use arp::{ArpPacket, ArpType, MacAddr}; -use ip::{IcmpPacket, IcmpType, IpPacket, ProtoHdr, TcpPacket, UdpPacket}; -use network_types::{arp::ArpHdr, ip::IpHdr}; +use ip::{IcmpPacket, IcmpType, IpPacket, IpProto, Ipv4Packet, Ipv6Packet, TcpPacket, UdpPacket}; +use network_types::{arp::ArpHdr, icmp::IcmpHdr, ip::IpHdr, tcp::TcpHdr, udp::UdpHdr}; pub mod arp; pub mod ip; @@ -16,10 +12,18 @@ pub mod protocols; #[repr(C)] pub enum RawPacket { - Ip(IpHdr, ip::ProtoHdr), + Ip(IpHdr, ProtoHdr), Arp(ArpHdr), } +#[repr(C)] +#[derive(Copy, Clone)] +pub enum ProtoHdr { + Tcp(TcpHdr), + Udp(UdpHdr), + Icmp(IcmpHdr), +} + impl RawPacket { pub const LEN: usize = mem::size_of::(); } @@ -49,74 +53,64 @@ impl From<[u8; RawPacket::LEN]> for AppPacket { let raw_packet = value.as_ptr() as *const RawPacket; match unsafe { &*raw_packet } { RawPacket::Ip(packet, proto) => match packet { - IpHdr::V4(ipv4_packet) => match proto { - ProtoHdr::Tcp(header) => { - let tcp_packet = TcpPacket { - src_ip: IpAddr::V4(Ipv4Addr::from(u32::from_be(ipv4_packet.src_addr))), - src_port: u16::from_be(header.source), - dst_ip: IpAddr::V4(Ipv4Addr::from(u32::from_be(ipv4_packet.dst_addr))), - dst_port: u16::from_be(header.dest), - }; - AppPacket::Ip(IpPacket::Tcp(tcp_packet)) - } - ProtoHdr::Udp(header) => { - let udp_packet = UdpPacket { - src_ip: IpAddr::V4(Ipv4Addr::from(u32::from_be(ipv4_packet.src_addr))), - src_port: u16::from_be(header.source), - dst_ip: IpAddr::V4(Ipv4Addr::from(u32::from_be(ipv4_packet.dst_addr))), - dst_port: u16::from_be(header.dest), - }; - Self::Ip(IpPacket::Udp(udp_packet)) - } - ProtoHdr::Icmp(header) => { - let icmp_type = match header.type_ { - 0 => IcmpType::EchoRequest, - 1 => IcmpType::EchoReply, - _ => IcmpType::DestinationUnreachable, - }; + IpHdr::V4(ipv4_packet) => { + let src_ip = Ipv4Addr::from(u32::from_be(ipv4_packet.src_addr)); + let dst_ip = Ipv4Addr::from(u32::from_be(ipv4_packet.dst_addr)); + + let proto = match proto { + ProtoHdr::Tcp(tcp_header) => IpProto::Tcp(TcpPacket { + src_port: u16::from_be(tcp_header.source), + dst_port: u16::from_be(tcp_header.dest), + }), + ProtoHdr::Udp(udp_header) => IpProto::Udp(UdpPacket { + src_port: u16::from_be(udp_header.source), + dst_port: u16::from_be(udp_header.dest), + }), + ProtoHdr::Icmp(header) => { + let icmp_type = match header.type_ { + 0 => IcmpType::EchoRequest, + 1 => IcmpType::EchoReply, + _ => IcmpType::DestinationUnreachable, + }; + IpProto::Icmp(IcmpPacket { icmp_type }) + } + }; + + AppPacket::Ip(IpPacket::V4(Ipv4Packet { + src_ip, + dst_ip, + proto, + })) + } + IpHdr::V6(ipv6_packet) => { + let src_ip = ipv6_packet.src_addr(); + let dst_ip = ipv6_packet.dst_addr(); - let icmp_packet = IcmpPacket { - src_ip: IpAddr::V4(Ipv4Addr::from(u32::from_be(ipv4_packet.src_addr))), - dst_ip: IpAddr::V4(Ipv4Addr::from(u32::from_be(ipv4_packet.dst_addr))), - icmp_type, - }; - Self::Ip(IpPacket::Icmp(icmp_packet)) - } - }, - IpHdr::V6(ipv6_packet) => match proto { - ProtoHdr::Tcp(header) => { - let tcp_packet = TcpPacket { - src_ip: IpAddr::V6(ipv6_packet.src_addr()), - src_port: u16::from_be(header.source), - dst_ip: IpAddr::V6(ipv6_packet.dst_addr()), - dst_port: u16::from_be(header.dest), - }; - Self::Ip(IpPacket::Tcp(tcp_packet)) - } - ProtoHdr::Udp(header) => { - let udp_packet = UdpPacket { - src_ip: IpAddr::V6(ipv6_packet.src_addr()), - src_port: u16::from_be(header.source), - dst_ip: IpAddr::V6(ipv6_packet.dst_addr()), - dst_port: u16::from_be(header.dest), - }; - Self::Ip(IpPacket::Udp(udp_packet)) - } - ProtoHdr::Icmp(header) => { - let icmp_type = match header.type_ { - 0 => IcmpType::EchoRequest, - 1 => IcmpType::EchoReply, - _ => IcmpType::DestinationUnreachable, - }; + let proto = match proto { + ProtoHdr::Tcp(tcp_header) => IpProto::Tcp(TcpPacket { + src_port: u16::from_be(tcp_header.source), + dst_port: u16::from_be(tcp_header.dest), + }), + ProtoHdr::Udp(udp_header) => IpProto::Udp(UdpPacket { + src_port: u16::from_be(udp_header.source), + dst_port: u16::from_be(udp_header.dest), + }), + ProtoHdr::Icmp(header) => { + let icmp_type = match header.type_ { + 0 => IcmpType::EchoRequest, + 1 => IcmpType::EchoReply, + _ => IcmpType::DestinationUnreachable, + }; + IpProto::Icmp(IcmpPacket { icmp_type }) + } + }; - let icmp_packet = IcmpPacket { - src_ip: IpAddr::V6(ipv6_packet.src_addr()), - dst_ip: IpAddr::V6(ipv6_packet.dst_addr()), - icmp_type, - }; - Self::Ip(IpPacket::Icmp(icmp_packet)) - } - }, + AppPacket::Ip(IpPacket::V6(Ipv6Packet { + src_ip, + dst_ip, + proto, + })) + } }, RawPacket::Arp(packet) => { let arp_type = match u16::from_be(packet.oper) { diff --git a/oryx-ebpf/src/main.rs b/oryx-ebpf/src/main.rs index 7cea341..f124f3d 100644 --- a/oryx-ebpf/src/main.rs +++ b/oryx-ebpf/src/main.rs @@ -17,9 +17,8 @@ use network_types::{ udp::UdpHdr, }; use oryx_common::{ - ip::ProtoHdr, protocols::{LinkProtocol, NetworkProtocol, Protocol, TransportProtocol}, - RawPacket, + ProtoHdr, RawPacket, }; #[map] diff --git a/oryx-tui/src/app.rs b/oryx-tui/src/app.rs index d4a28a8..43b0959 100644 --- a/oryx-tui/src/app.rs +++ b/oryx-tui/src/app.rs @@ -1,4 +1,4 @@ -use oryx_common::ip::IpPacket; +use oryx_common::ip::{IpPacket, IpProto}; use oryx_common::protocols::{ Protocol, NB_LINK_PROTOCOL, NB_NETWORK_PROTOCOL, NB_TRANSPORT_PROTOCOL, }; @@ -629,27 +629,52 @@ impl App { fuzzy::highlight(pattern, "ARP".to_string()).cyan(), ]), AppPacket::Ip(packet) => match packet { - IpPacket::Tcp(p) => Row::new(vec![ - fuzzy::highlight(pattern, p.src_ip.to_string()).blue(), - fuzzy::highlight(pattern, p.src_port.to_string()).yellow(), - fuzzy::highlight(pattern, p.dst_ip.to_string()).blue(), - fuzzy::highlight(pattern, p.dst_port.to_string()).yellow(), - fuzzy::highlight(pattern, "TCP".to_string()).cyan(), - ]), - IpPacket::Udp(p) => Row::new(vec![ - fuzzy::highlight(pattern, p.src_ip.to_string()).blue(), - fuzzy::highlight(pattern, p.src_port.to_string()).yellow(), - fuzzy::highlight(pattern, p.dst_ip.to_string()).blue(), - fuzzy::highlight(pattern, p.dst_port.to_string()).yellow(), - fuzzy::highlight(pattern, "UDP".to_string()).cyan(), - ]), - IpPacket::Icmp(p) => Row::new(vec![ - fuzzy::highlight(pattern, p.src_ip.to_string()).blue(), - Cell::from(Line::from("-").centered()).yellow(), - fuzzy::highlight(pattern, p.dst_ip.to_string()).blue(), - Cell::from(Line::from("-").centered()).yellow(), - fuzzy::highlight(pattern, "ICMP".to_string()).cyan(), - ]), + IpPacket::V4(ipv4_packet) => match ipv4_packet.proto { + IpProto::Tcp(p) => Row::new(vec![ + fuzzy::highlight(pattern, ipv4_packet.src_ip.to_string()).blue(), + fuzzy::highlight(pattern, p.src_port.to_string()).yellow(), + fuzzy::highlight(pattern, ipv4_packet.dst_ip.to_string()).blue(), + fuzzy::highlight(pattern, p.dst_port.to_string()).yellow(), + fuzzy::highlight(pattern, "TCP".to_string()).cyan(), + ]), + IpProto::Udp(p) => Row::new(vec![ + fuzzy::highlight(pattern, ipv4_packet.src_ip.to_string()).blue(), + fuzzy::highlight(pattern, p.src_port.to_string()).yellow(), + fuzzy::highlight(pattern, ipv4_packet.dst_ip.to_string()).blue(), + fuzzy::highlight(pattern, p.dst_port.to_string()).yellow(), + fuzzy::highlight(pattern, "UDP".to_string()).cyan(), + ]), + IpProto::Icmp(_) => Row::new(vec![ + fuzzy::highlight(pattern, ipv4_packet.src_ip.to_string()).blue(), + Cell::from(Line::from("-").centered()).yellow(), + fuzzy::highlight(pattern, ipv4_packet.dst_ip.to_string()).blue(), + Cell::from(Line::from("-").centered()).yellow(), + fuzzy::highlight(pattern, "ICMP".to_string()).cyan(), + ]), + }, + IpPacket::V6(ipv6_packet) => match ipv6_packet.proto { + IpProto::Tcp(p) => Row::new(vec![ + fuzzy::highlight(pattern, ipv6_packet.src_ip.to_string()).blue(), + fuzzy::highlight(pattern, p.src_port.to_string()).yellow(), + fuzzy::highlight(pattern, ipv6_packet.dst_ip.to_string()).blue(), + fuzzy::highlight(pattern, p.dst_port.to_string()).yellow(), + fuzzy::highlight(pattern, "TCP".to_string()).cyan(), + ]), + IpProto::Udp(p) => Row::new(vec![ + fuzzy::highlight(pattern, ipv6_packet.src_ip.to_string()).blue(), + fuzzy::highlight(pattern, p.src_port.to_string()).yellow(), + fuzzy::highlight(pattern, ipv6_packet.dst_ip.to_string()).blue(), + fuzzy::highlight(pattern, p.dst_port.to_string()).yellow(), + fuzzy::highlight(pattern, "UDP".to_string()).cyan(), + ]), + IpProto::Icmp(_) => Row::new(vec![ + fuzzy::highlight(pattern, ipv6_packet.src_ip.to_string()).blue(), + Cell::from(Line::from("-").centered()).yellow(), + fuzzy::highlight(pattern, ipv6_packet.dst_ip.to_string()).blue(), + Cell::from(Line::from("-").centered()).yellow(), + fuzzy::highlight(pattern, "ICMP".to_string()).cyan(), + ]), + }, }, }) .collect() @@ -669,35 +694,92 @@ impl App { Span::from("ARP".to_string()).into_centered_line().cyan(), ]), AppPacket::Ip(packet) => match packet { - IpPacket::Tcp(p) => Row::new(vec![ - Span::from(p.src_ip.to_string()).into_centered_line().blue(), - Span::from(p.src_port.to_string()) - .into_centered_line() - .yellow(), - Span::from(p.dst_ip.to_string()).into_centered_line().blue(), - Span::from(p.dst_port.to_string()) - .into_centered_line() - .yellow(), - Span::from("TCP".to_string()).into_centered_line().cyan(), - ]), - IpPacket::Udp(p) => Row::new(vec![ - Span::from(p.src_ip.to_string()).into_centered_line().blue(), - Span::from(p.src_port.to_string()) - .into_centered_line() - .yellow(), - Span::from(p.dst_ip.to_string()).into_centered_line().blue(), - Span::from(p.dst_port.to_string()) - .into_centered_line() - .yellow(), - Span::from("UDP".to_string()).into_centered_line().cyan(), - ]), - IpPacket::Icmp(p) => Row::new(vec![ - Span::from(p.src_ip.to_string()).into_centered_line().blue(), - Span::from("-").into_centered_line().yellow(), - Span::from(p.dst_ip.to_string()).into_centered_line().blue(), - Span::from("-").into_centered_line().yellow(), - Span::from("ICMP".to_string()).into_centered_line().cyan(), - ]), + IpPacket::V4(ipv4_packet) => match ipv4_packet.proto { + IpProto::Tcp(p) => Row::new(vec![ + Span::from(ipv4_packet.src_ip.to_string()) + .into_centered_line() + .blue(), + Span::from(p.src_port.to_string()) + .into_centered_line() + .yellow(), + Span::from(ipv4_packet.dst_ip.to_string()) + .into_centered_line() + .blue(), + Span::from(p.dst_port.to_string()) + .into_centered_line() + .yellow(), + Span::from("TCP".to_string()).into_centered_line().cyan(), + ]), + IpProto::Udp(p) => Row::new(vec![ + Span::from(ipv4_packet.src_ip.to_string()) + .into_centered_line() + .blue(), + Span::from(p.src_port.to_string()) + .into_centered_line() + .yellow(), + Span::from(ipv4_packet.dst_ip.to_string()) + .into_centered_line() + .blue(), + Span::from(p.dst_port.to_string()) + .into_centered_line() + .yellow(), + Span::from("UDP".to_string()).into_centered_line().cyan(), + ]), + IpProto::Icmp(_) => Row::new(vec![ + Span::from(ipv4_packet.src_ip.to_string()) + .into_centered_line() + .blue(), + Span::from("-").into_centered_line().yellow(), + Span::from(ipv4_packet.dst_ip.to_string()) + .into_centered_line() + .blue(), + Span::from("-").into_centered_line().yellow(), + Span::from("ICMP".to_string()).into_centered_line().cyan(), + ]), + }, + IpPacket::V6(ipv6_packet) => match ipv6_packet.proto { + IpProto::Tcp(p) => Row::new(vec![ + Span::from(ipv6_packet.src_ip.to_string()) + .into_centered_line() + .blue(), + Span::from(p.src_port.to_string()) + .into_centered_line() + .yellow(), + Span::from(ipv6_packet.dst_ip.to_string()) + .into_centered_line() + .blue(), + Span::from(p.dst_port.to_string()) + .into_centered_line() + .yellow(), + Span::from("TCP".to_string()).into_centered_line().cyan(), + ]), + IpProto::Udp(p) => Row::new(vec![ + Span::from(ipv6_packet.src_ip.to_string()) + .into_centered_line() + .blue(), + Span::from(p.src_port.to_string()) + .into_centered_line() + .yellow(), + Span::from(ipv6_packet.dst_ip.to_string()) + .into_centered_line() + .blue(), + Span::from(p.dst_port.to_string()) + .into_centered_line() + .yellow(), + Span::from("UDP".to_string()).into_centered_line().cyan(), + ]), + IpProto::Icmp(_) => Row::new(vec![ + Span::from(ipv6_packet.src_ip.to_string()) + .into_centered_line() + .blue(), + Span::from("-").into_centered_line().yellow(), + Span::from(ipv6_packet.dst_ip.to_string()) + .into_centered_line() + .blue(), + Span::from("-").into_centered_line().yellow(), + Span::from("ICMP".to_string()).into_centered_line().cyan(), + ]), + }, }, }) .collect() diff --git a/oryx-tui/src/export.rs b/oryx-tui/src/export.rs index 5cc6c85..4cff86e 100644 --- a/oryx-tui/src/export.rs +++ b/oryx-tui/src/export.rs @@ -2,7 +2,7 @@ use std::fs::{create_dir, OpenOptions}; use std::io::prelude::*; use std::os::unix::fs::chown; -use oryx_common::ip::IpPacket; +use oryx_common::ip::{IpPacket, IpProto}; use oryx_common::AppPacket; use crate::app::AppResult; @@ -46,27 +46,52 @@ pub fn export(packets: &[AppPacket]) -> AppResult<()> { )?; } AppPacket::Ip(packet) => match packet { - IpPacket::Tcp(p) => { - writeln!( - file, - "{:39} {:<11} {:39} {:<11} TCP", - p.src_ip, p.src_port, p.dst_ip, p.dst_port - )?; - } - IpPacket::Udp(p) => { - writeln!( - file, - "{:39} {:<11} {:39} {:<11} UDP", - p.src_ip, p.src_port, p.dst_ip, p.dst_port - )?; - } - IpPacket::Icmp(p) => { - writeln!( - file, - "{:39} {:^11} {:39} {:^11} ICMP", - p.src_ip, "-", p.dst_ip, "-" - )?; - } + IpPacket::V4(ipv4_packet) => match ipv4_packet.proto { + IpProto::Tcp(p) => { + writeln!( + file, + "{:39} {:<11} {:39} {:<11} TCP", + ipv4_packet.src_ip, p.src_port, ipv4_packet.dst_ip, p.dst_port + )?; + } + IpProto::Udp(p) => { + writeln!( + file, + "{:39} {:<11} {:39} {:<11} UDP", + ipv4_packet.src_ip, p.src_port, ipv4_packet.dst_ip, p.dst_port + )?; + } + IpProto::Icmp(_) => { + writeln!( + file, + "{:39} {:^11} {:39} {:^11} ICMP", + ipv4_packet.src_ip, "-", ipv4_packet.dst_ip, "-" + )?; + } + }, + IpPacket::V6(ipv6_packet) => match ipv6_packet.proto { + IpProto::Tcp(p) => { + writeln!( + file, + "{:39} {:<11} {:39} {:<11} TCP", + ipv6_packet.src_ip, p.src_port, ipv6_packet.dst_ip, p.dst_port + )?; + } + IpProto::Udp(p) => { + writeln!( + file, + "{:39} {:<11} {:39} {:<11} UDP", + ipv6_packet.src_ip, p.src_port, ipv6_packet.dst_ip, p.dst_port + )?; + } + IpProto::Icmp(_) => { + writeln!( + file, + "{:39} {:^11} {:39} {:^11} ICMP", + ipv6_packet.src_ip, "-", ipv6_packet.dst_ip, "-" + )?; + } + }, }, } } diff --git a/oryx-tui/src/stats.rs b/oryx-tui/src/stats.rs index 3764ecc..d3da021 100644 --- a/oryx-tui/src/stats.rs +++ b/oryx-tui/src/stats.rs @@ -3,7 +3,7 @@ use oryx_common::AppPacket; use std::collections::HashMap; use std::net::{IpAddr, Ipv4Addr}; -use oryx_common::ip::IpPacket; +use oryx_common::ip::{IpPacket, IpProto}; use ratatui::layout::{Alignment, Constraint, Direction, Flex, Layout, Rect}; use ratatui::style::{Color, Style}; @@ -52,115 +52,48 @@ impl Stats { self.link.arp += 1; } AppPacket::Ip(packet) => match packet { - IpPacket::Tcp(p) => { - match p.src_ip { - IpAddr::V4(ip) => { - self.network.ipv4 += 1; - self.transport.tcp += 1; + IpPacket::V4(ipv4_packet) => { + self.network.ipv4 += 1; - if !ip.is_private() && !ip.is_loopback() { - if let Some((_, counts)) = self.addresses.get_mut(&ip) { - *counts += 1; - } else if let Ok(host) = lookup_addr(&IpAddr::V4(ip)) { - self.addresses.insert(ip, (Some(host), 1)); - } else { - self.addresses.insert(ip, (None, 1)); - } - } + if !ipv4_packet.dst_ip.is_private() && !ipv4_packet.dst_ip.is_loopback() { + if let Some((_, counts)) = self.addresses.get_mut(&ipv4_packet.dst_ip) { + *counts += 1; + } else if let Ok(host) = lookup_addr(&IpAddr::V4(ipv4_packet.dst_ip)) { + self.addresses.insert(ipv4_packet.dst_ip, (Some(host), 1)); + } else { + self.addresses.insert(ipv4_packet.dst_ip, (None, 1)); } + } - IpAddr::V6(_) => { - self.network.ipv6 += 1; + match ipv4_packet.proto { + IpProto::Tcp(_) => { self.transport.tcp += 1; } - }; - - if let IpAddr::V4(ip) = p.dst_ip { - if !ip.is_private() && !ip.is_loopback() { - if let Some((_, counts)) = self.addresses.get_mut(&ip) { - *counts += 1; - } else if let Ok(host) = lookup_addr(&IpAddr::V4(ip)) { - self.addresses.insert(ip, (Some(host), 1)); - } else { - self.addresses.insert(ip, (None, 1)); - } - } - }; - } - IpPacket::Udp(p) => { - match p.src_ip { - IpAddr::V4(ip) => { - self.network.ipv4 += 1; - self.transport.udp += 1; - - if !ip.is_private() && !ip.is_loopback() { - if let Some((_, counts)) = self.addresses.get_mut(&ip) { - *counts += 1; - } else if let Ok(host) = lookup_addr(&IpAddr::V4(ip)) { - self.addresses.insert(ip, (Some(host), 1)); - } else { - self.addresses.insert(ip, (None, 1)); - } - } - } - - IpAddr::V6(_) => { - self.network.ipv6 += 1; + IpProto::Udp(_) => { self.transport.udp += 1; } - }; - - if let IpAddr::V4(ip) = p.dst_ip { - if !ip.is_private() && !ip.is_loopback() { - if let Some((_, counts)) = self.addresses.get_mut(&ip) { - *counts += 1; - } else if let Ok(host) = lookup_addr(&IpAddr::V4(ip)) { - self.addresses.insert(ip, (Some(host), 1)); - } else { - self.addresses.insert(ip, (None, 1)); - } + IpProto::Icmp(_) => { + self.network.icmp += 1; } - }; + } } - IpPacket::Icmp(p) => { - self.network.icmp += 1; - - match p.src_ip { - IpAddr::V4(_) => { - self.network.ipv4 += 1; - } - - IpAddr::V6(_) => { - self.network.ipv6 += 1; + IpPacket::V6(ipv6_packet) => { + self.network.ipv6 += 1; + match ipv6_packet.proto { + IpProto::Tcp(_) => { + self.transport.tcp += 1; } - }; - - if let IpAddr::V4(ip) = p.src_ip { - if !ip.is_private() && !ip.is_loopback() { - if let Some((_, counts)) = self.addresses.get_mut(&ip) { - *counts += 1; - } else if let Ok(host) = lookup_addr(&IpAddr::V4(ip)) { - self.addresses.insert(ip, (Some(host), 1)); - } else { - self.addresses.insert(ip, (None, 1)); - } + IpProto::Udp(_) => { + self.transport.udp += 1; } - }; - - if let IpAddr::V4(ip) = p.dst_ip { - if !ip.is_private() && !ip.is_loopback() { - if let Some((_, counts)) = self.addresses.get_mut(&ip) { - *counts += 1; - } else if let Ok(host) = lookup_addr(&IpAddr::V4(ip)) { - self.addresses.insert(ip, (Some(host), 1)); - } else { - self.addresses.insert(ip, (None, 1)); - } + IpProto::Icmp(_) => { + self.network.icmp += 1; } - }; + } } }, } + self.total += 1; }