networking abstractions & early parsing logic
This commit is contained in:
parent
e8b2411864
commit
59a17c5475
1 changed files with 60 additions and 2 deletions
|
|
@ -1,9 +1,67 @@
|
||||||
|
use tun::Error;
|
||||||
|
|
||||||
// Here we will recieve bytes and try to get their destanation & apply Rules for them.
|
// Here we will recieve bytes and try to get their destanation & apply Rules for them.
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
|
||||||
struct PacketInfo;
|
enum Protocol {
|
||||||
|
TCP,
|
||||||
|
UDP
|
||||||
|
}
|
||||||
|
type Ipv4 = [u8; 4];
|
||||||
|
type Ipv6 = [u8; 16];
|
||||||
|
type Port = u16;
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
pub fn sniff_raw_packets(packet: &[u8]) -> Result<PacketInfo, Box<dyn std::error::Error + Send + Sync + 'static>> {
|
pub fn sniff_raw_packets(packet: &[u8]) -> Result<PacketInfo, Box<dyn std::error::Error + Send + Sync + 'static>> {
|
||||||
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) {
|
pub fn apply_rules(config: Config, pinfo: PacketInfo) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue