167 lines
5.6 KiB
Rust
167 lines
5.6 KiB
Rust
use tun::Error;
|
|
use std::fmt;
|
|
|
|
// Here we will recieve bytes and try to get their destanation & apply Rules for them.
|
|
use crate::config::Config;
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
pub enum Protocol {
|
|
TCP,
|
|
UDP,
|
|
Unsupported(u8)
|
|
}
|
|
type SourceV4Ip = Ipv4;
|
|
type SourceV6Ip = Ipv6;
|
|
pub enum IpVersion {
|
|
V4,
|
|
V6
|
|
}
|
|
// type IpVersion = String;
|
|
type Ipv4 = [u8; 4];
|
|
type Ipv6 = [u8; 16];
|
|
type Port = u16;
|
|
#[derive(Debug, PartialEq)]
|
|
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,
|
|
dns: bool
|
|
},
|
|
// <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,
|
|
dns: bool
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for PacketInfo {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
if self.version() == "Ipv4" {
|
|
let src_ip = self.src_ipv4_ip();
|
|
let dst_ip = self.dst_ipv4_ip();
|
|
write!(f, "{} {}.{}.{}.{}:{} -> {}.{}.{}.{}:{} PROTO {} DNS? {}", self.version(), src_ip[0], src_ip[1], src_ip[2], src_ip[3], self.src_port, dst_ip[0], dst_ip[1], dst_ip[2], dst_ip[3], self.dst_port(), self.protocol(), self.dns())
|
|
}
|
|
// write!(f, "{} {}:{} -> {}:{} PROTO {} DNS? {}", self.version(), self.)
|
|
}
|
|
}
|
|
|
|
impl PacketInfo {
|
|
pub fn dns(&self) -> &bool {
|
|
match self {
|
|
PacketInfo::V4 { dns, ..} => dns,
|
|
PacketInfo::V6 { dns, ..} => dns,
|
|
}
|
|
}
|
|
pub fn dst_ipv4_ip(&self) -> &SourceV4Ip {
|
|
match self {
|
|
PacketInfo::V4 { dst_ip, .. } => dst_ip,
|
|
_ => &[0x0, 0x0, 0x0, 0x0].try_into().expect("this never should fail or even be called in the first place.")
|
|
}
|
|
}
|
|
pub fn src_ipv6_ip(&self) -> &SourceV6Ip {
|
|
match self {
|
|
PacketInfo::V6 { src_ip, .. } => src_ip,
|
|
_ => &[0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0].try_into().expect("this never should fail or even be called in the first place.")
|
|
}
|
|
}
|
|
pub fn src_ipv4_ip(&self) -> &SourceV4Ip {
|
|
match self {
|
|
PacketInfo::V4 { src_ip, .. } => src_ip,
|
|
_ => &[0x0, 0x0, 0x0, 0x0].try_into().expect("this never should fail or even be called in the first place.")
|
|
}
|
|
}
|
|
pub fn src_ipv6_ip(&self) -> &SourceV6Ip {
|
|
PacketInfo::V6.src_ip
|
|
}
|
|
pub fn version(&self) -> &IpVersion {
|
|
match self {
|
|
PacketInfo::V4 { .. }=> &IpVersion::V4,
|
|
PacketInfo::V6 { .. }=> &IpVersion::V6
|
|
}
|
|
}
|
|
pub fn protocol(&self) -> &Protocol {
|
|
match self {
|
|
PacketInfo::V4 { protocol, .. } => protocol,
|
|
PacketInfo::V6 { protocol, .. } => protocol,
|
|
}
|
|
}
|
|
}
|
|
|
|
// TODO: move these to some appropriate file for code readability.
|
|
type GenericError = Box<dyn std::error::Error + Send + Sync + 'static>;
|
|
type SniffedPacket = Result<PacketInfo, Box<dyn std::error::Error + Send + Sync + 'static>>;
|
|
type Packet = [u8];
|
|
|
|
pub fn sniff_raw_packets(packet: &Packet) -> SniffedPacket {
|
|
let ver = packet[0] >> 4;
|
|
match ver {
|
|
4 => {
|
|
// Internet Header Length (IHL).
|
|
let ihl = (packet[0] & 0x0F) as usize * 4;
|
|
let dst_port = Port::from_be_bytes([packet[ihl+2], packet[ihl+3]]);
|
|
let dns;
|
|
if dst_port == 53 { dns = true; } else { dns = false; };
|
|
// FIXME: hardcoded IPv4 port offset
|
|
let v4 = PacketInfo::V4{
|
|
src_ip: <Ipv4>::try_from(&packet[12..16])?,
|
|
src_port: Port::from_be_bytes([packet[ihl], packet[ihl+1]]),
|
|
dst_ip: <Ipv4>::try_from(&packet[16..20])?,
|
|
dst_port,
|
|
protocol: match packet[9] {
|
|
6 => Protocol::TCP,
|
|
17 => Protocol::UDP,
|
|
p => Protocol::Unsupported(p)
|
|
},
|
|
dns
|
|
};
|
|
if !matches!(v4.protocol(), Protocol::Unsupported(_)) {
|
|
println!("{v4:?}");
|
|
} else {
|
|
// TODO: make --debug option which will include this diagnostic, for general use this
|
|
// should be off
|
|
// println!("oppsie unsupported protocol: {:?}", v4.protocol());
|
|
}
|
|
Ok(v4)
|
|
},
|
|
6 => {
|
|
let dst_port = Port::from_be_bytes([packet[22], packet[23]]);
|
|
let dns;
|
|
if dst_port == 53 { dns = true; } else { dns = false; };
|
|
let v6 = PacketInfo::V6{
|
|
src_ip: <Ipv6>::try_from(&packet[8..24])?,
|
|
src_port: Port::from_be_bytes([packet[40], packet[41]]),
|
|
dst_ip: <Ipv6>::try_from(&packet[24..40])?,
|
|
dst_port,
|
|
protocol: match packet[6] {
|
|
6 => Protocol::TCP,
|
|
17 => Protocol::UDP,
|
|
p => Protocol::Unsupported(p)
|
|
},
|
|
dns
|
|
};
|
|
if !matches!(v6.protocol(), Protocol::Unsupported(_)) {
|
|
println!("{v6:?}");
|
|
} else {
|
|
// TODO: make --debug option which will include this diagnostic, for general use this
|
|
// should be off
|
|
// println!("oppsie unsupported protocol: {:?}", v6.protocol());
|
|
}
|
|
Ok(v6)
|
|
},
|
|
ver => {
|
|
Err(format!("unsuppiorted ver: {ver}").into())
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn apply_rules(config: Config, pinfo: PacketInfo) {
|
|
todo!()
|
|
}
|