Merge pull request 'basic headers-parsing logic' (#1) from headers-parsing into main

Reviewed-on: https://codeberg.org/NamelessTeam/nsc/pulls/1
This commit is contained in:
Namilsk 2026-03-15 19:41:15 +01:00
commit d4f6b7d788
3 changed files with 98 additions and 10 deletions

View file

@ -1,12 +1,13 @@
mod config;
mod geoparsers;
mod sniffing;
pub mod sniffing;
mod startup;
use startup::init;
use std::io::Read;
fn main() {
init();
fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>
>{
init()
}

View file

@ -1,9 +1,92 @@
use tun::Error;
// Here we will recieve bytes and try to get their destanation & apply Rules for them.
use crate::config::Config;
struct PacketInfo;
#[derive(Debug)]
pub enum Protocol {
TCP,
UDP,
Unsupported(u8)
}
type Ipv4 = [u8; 4];
type Ipv6 = [u8; 16];
type Port = u16;
#[derive(Debug)]
pub enum PacketInfo {
// <https://www.geeksforgeeks.org/computer-networks/what-is-ipv4/>
V4 {
src_ip: Ipv4,
src_port: Port,
dst_ip: Ipv4,
dst_port: Port,
protocol: Protocol
},
// <https://www.geeksforgeeks.org/computer-networks/internet-protocol-version-6-ipv6-header/>
V6 {
src_ip: Ipv6,
src_port: Port,
dst_ip: Ipv6,
dst_port: Port,
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>> {
todo!()
let ver = packet[0] >> 4;
dbg!(ver);
match ver {
4 => {
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: <[u8; 4]>::try_from(&packet[16..20])?,
dst_port: u16::from_be_bytes([packet[22], packet[23]]),
protocol: match packet[9] {
6 => Protocol::TCP,
17 => Protocol::UDP,
p => Protocol::Unsupported(p)
}
};
if !matches!(v4.protocol(), Protocol::Unsupported(_)) {
println!("{v4:?}");
} else {
println!("oppsie unsupported");
}
Ok(v4)
},
6 => {
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: <[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 => Protocol::Unsupported(p)
}
};
if !matches!(v6.protocol(), Protocol::Unsupported(_)) {
println!("{v6:?}");
} else {
println!("oppsie unsupported");
}
Ok(v6)
},
ver => {
Err(format!("unsuppiorted ver: {ver}").into())
}
}
}
pub fn apply_rules(config: Config, pinfo: PacketInfo) {

View file

@ -1,7 +1,7 @@
// Here we iniitialize systems crucial for nsc
use std::io::Read;
use sniffing::headers::sniff_raw_packets;
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
@ -21,8 +21,12 @@ pub fn init() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
loop {
let amount = dev.read(&mut buf)?;
sniff_raw_packets(&buf[0..amount]);
dbg!("{:?}", &buf[0..amount]);
// dbg!(sniff_raw_packets(&buf[0..amount])?);
let govno = sniff_raw_packets(&buf[0..amount])?;
if !matches!(govno.protocol(), Protocol::Unsupported(_)) {
// println!("1")
// println!("{:?}", govno)
}
// dbg!("{:?}", &buf[0..amount]);
}
}