From 59a17c547563dab8a70830185ba763d8343ac109 Mon Sep 17 00:00:00 2001 From: zedddie Date: Sun, 15 Mar 2026 16:57:56 +0100 Subject: networking abstractions & early parsing logic --- src/sniffing/headers.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) (limited to 'src/sniffing/headers.rs') diff --git a/src/sniffing/headers.rs b/src/sniffing/headers.rs index 7ed1a88..d1d0a84 100644 --- a/src/sniffing/headers.rs +++ b/src/sniffing/headers.rs @@ -1,9 +1,67 @@ +use tun::Error; + // Here we will recieve bytes and try to get their destanation & apply Rules for them. use crate::config::Config; -struct PacketInfo; +enum Protocol { + TCP, + UDP +} +type Ipv4 = [u8; 4]; +type Ipv6 = [u8; 16]; +type Port = u16; +enum PacketInfo { + // + V4 { + src_ip: Ipv4, + src_port: Port, + dst_ip: Ipv4, + dst_port: Port, + protocol: Protocol + }, + // + V6 { + src_ip: Ipv6, + src_port: Port, + dst_ip: Ipv6, + dst_port: Port, + protocol: Protocol + } +} pub fn sniff_raw_packets(packet: &[u8]) -> Result> { - todo!() + let ver = packet[0] >> 4; + match ver { + 4 => { + PacketInfo::V4{ + src_ip: packet[12..16], + src_port: u16::from_be_bytes([packet[20], packet[21]]), + dst_ip: packet[16..20], + dst_port: u16::from_be_bytes([packet[22], packet[23]]), + protocol: match packet[9] { + 6 => Protocol::TCP, + 4 => Protocol::UDP, + _ => return Err(format!("unsuppiorted protocol: {p}").into()) + } + } + }, + 6 => { + PacketInfo::V6{ + src_ip: packet[8..24], + src_port: u16::from_be_bytes([packet[40], packet[41]]), + dst_ip: packet[24..40], + dst_port: u16::from_be_bytes([packet[42], packet[43]]), + protocol: match packet[6] { + 6 => Protocol::TCP, + 4 => Protocol::UDP, + _ => return Err(format!("unsuppiorted protocol: {p}").into()) + } + } + }, + ver => { + panic!("unexpected packet ver: {ver}"); + Error + } + } } pub fn apply_rules(config: Config, pinfo: PacketInfo) { -- cgit v1.2.3 From 891a76eaf5ada282fd0568f856b63635d2a173e4 Mon Sep 17 00:00:00 2001 From: zedddie Date: Sun, 15 Mar 2026 17:17:24 +0100 Subject: somewhat wip sniffing --- src/sniffing/headers.rs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'src/sniffing/headers.rs') diff --git a/src/sniffing/headers.rs b/src/sniffing/headers.rs index d1d0a84..0a1a742 100644 --- a/src/sniffing/headers.rs +++ b/src/sniffing/headers.rs @@ -3,6 +3,7 @@ use tun::Error; // Here we will recieve bytes and try to get their destanation & apply Rules for them. use crate::config::Config; +#[derive(Debug)] enum Protocol { TCP, UDP @@ -10,7 +11,8 @@ enum Protocol { type Ipv4 = [u8; 4]; type Ipv6 = [u8; 16]; type Port = u16; -enum PacketInfo { +#[derive(Debug)] +pub enum PacketInfo { // V4 { src_ip: Ipv4, @@ -29,37 +31,38 @@ enum PacketInfo { } } pub fn sniff_raw_packets(packet: &[u8]) -> Result> { + println!("something"); let ver = packet[0] >> 4; + dbg!(ver); match ver { 4 => { - PacketInfo::V4{ - src_ip: packet[12..16], + Ok(PacketInfo::V4{ + src_ip: packet[12..16].try_into()?, src_port: u16::from_be_bytes([packet[20], packet[21]]), - dst_ip: packet[16..20], + dst_ip: packet[16..20].try_into()?, dst_port: u16::from_be_bytes([packet[22], packet[23]]), protocol: match packet[9] { 6 => Protocol::TCP, 4 => Protocol::UDP, - _ => return Err(format!("unsuppiorted protocol: {p}").into()) + p => return Err(format!("unsuppiorted protocol: {p}").into()) } - } + }) }, 6 => { - PacketInfo::V6{ - src_ip: packet[8..24], + Ok(PacketInfo::V6{ + src_ip: packet[8..24].try_into()?, src_port: u16::from_be_bytes([packet[40], packet[41]]), - dst_ip: packet[24..40], + dst_ip: packet[24..40].try_into()?, dst_port: u16::from_be_bytes([packet[42], packet[43]]), protocol: match packet[6] { 6 => Protocol::TCP, 4 => Protocol::UDP, - _ => return Err(format!("unsuppiorted protocol: {p}").into()) + p => return Err(format!("unsuppiorted protocol: {p}").into()) } - } + }) }, ver => { - panic!("unexpected packet ver: {ver}"); - Error + Err(format!("unsuppiorted ver: {ver}").into()) } } } -- cgit v1.2.3 From d48a7bebfb87e4aa27d191f0e6726c861a7fcea9 Mon Sep 17 00:00:00 2001 From: zedddie Date: Sun, 15 Mar 2026 19:30:19 +0100 Subject: somewhat working network header parser --- src/sniffing/headers.rs | 50 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 14 deletions(-) (limited to 'src/sniffing/headers.rs') diff --git a/src/sniffing/headers.rs b/src/sniffing/headers.rs index 0a1a742..c90a849 100644 --- a/src/sniffing/headers.rs +++ b/src/sniffing/headers.rs @@ -4,9 +4,10 @@ use tun::Error; use crate::config::Config; #[derive(Debug)] -enum Protocol { +pub enum Protocol { TCP, - UDP + UDP, + Unsupported(u8) } type Ipv4 = [u8; 4]; type Ipv6 = [u8; 16]; @@ -30,36 +31,57 @@ pub enum PacketInfo { protocol: Protocol } } + +impl PacketInfo { + pub fn protocol(&self) -> &Protocol { + match self { + PacketInfo::V4 { protocol, .. } => protocol, + PacketInfo::V6 { protocol, .. } => protocol, + } + } +} pub fn sniff_raw_packets(packet: &[u8]) -> Result> { - println!("something"); let ver = packet[0] >> 4; dbg!(ver); match ver { 4 => { - Ok(PacketInfo::V4{ - src_ip: packet[12..16].try_into()?, + let v4 = PacketInfo::V4{ + src_ip: <[u8; 4]>::try_from(&packet[12..16])?, src_port: u16::from_be_bytes([packet[20], packet[21]]), - dst_ip: packet[16..20].try_into()?, + dst_ip: <[u8; 4]>::try_from(&packet[16..20])?, dst_port: u16::from_be_bytes([packet[22], packet[23]]), protocol: match packet[9] { 6 => Protocol::TCP, - 4 => Protocol::UDP, - p => return Err(format!("unsuppiorted protocol: {p}").into()) + 17 => Protocol::UDP, + p => Protocol::Unsupported(p) } - }) + }; + if !matches!(v4.protocol(), Protocol::Unsupported(_)) { + println!("{v4:?}"); + } else { + println!("oppsie unsupported"); + } + Ok(v4) }, 6 => { - Ok(PacketInfo::V6{ - src_ip: packet[8..24].try_into()?, + println!("im in 6!"); + let v6 = PacketInfo::V6{ + src_ip: <[u8; 16]>::try_from(&packet[8..24])?, src_port: u16::from_be_bytes([packet[40], packet[41]]), - dst_ip: packet[24..40].try_into()?, + dst_ip: <[u8; 16]>::try_from(&packet[24..40])?, dst_port: u16::from_be_bytes([packet[42], packet[43]]), protocol: match packet[6] { 6 => Protocol::TCP, 4 => Protocol::UDP, - p => return Err(format!("unsuppiorted protocol: {p}").into()) + p => Protocol::Unsupported(p) } - }) + }; + if !matches!(v6.protocol(), Protocol::Unsupported(_)) { + println!("{v6:?}"); + } else { + println!("oppsie unsupported"); + } + Ok(v6) }, ver => { Err(format!("unsuppiorted ver: {ver}").into()) -- cgit v1.2.3