summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzedddie <rust@zedddie.rs>2026-03-15 19:30:19 +0100
committertuturuu <zedddiezxc@gmail.com>2026-03-15 19:30:19 +0100
commitd48a7bebfb87e4aa27d191f0e6726c861a7fcea9 (patch)
treeac70a6089e651ceb703b19d645669e4227b3fb33
parent891a76eaf5ada282fd0568f856b63635d2a173e4 (diff)
somewhat working network header parser
-rw-r--r--src/main.rs5
-rw-r--r--src/sniffing/headers.rs50
-rw-r--r--src/startup.rs7
3 files changed, 44 insertions, 18 deletions
diff --git a/src/main.rs b/src/main.rs
index c2da16c..464146a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,6 +7,7 @@ use startup::init;
use std::io::Read;
-fn main() {
- init();
+fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>
+>{
+ init()
}
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<PacketInfo, Box<dyn std::error::Error + Send + Sync + 'static>> {
- 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())
diff --git a/src/startup.rs b/src/startup.rs
index 5caaa86..10a8441 100644
--- a/src/startup.rs
+++ b/src/startup.rs
@@ -1,7 +1,7 @@
// Here we iniitialize systems crucial for nsc
use std::io::Read;
use crate::sniffing::headers::sniff_raw_packets;
-
+use crate::sniffing::headers::Protocol;
pub fn init() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let mut config = tun::Configuration::default();
config
@@ -23,7 +23,10 @@ pub fn init() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
let amount = dev.read(&mut buf)?;
// dbg!(sniff_raw_packets(&buf[0..amount])?);
let govno = sniff_raw_packets(&buf[0..amount])?;
- println!("{govno:?}")
+ if !matches!(govno.protocol(), Protocol::Unsupported(_)) {
+ // println!("1")
+ // println!("{:?}", govno)
+ }
// dbg!("{:?}", &buf[0..amount]);
}
}