summaryrefslogtreecommitdiff
path: root/src/sniffing/headers.rs
blob: 0a1a7424a7100faf945c86152311a42c395fef83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
}
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
    }
}
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()?,
                src_port: u16::from_be_bytes([packet[20], packet[21]]),
                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,
                    p => return Err(format!("unsuppiorted protocol: {p}").into())
                }
            })
        },
        6 => {
            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].try_into()?,
                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())
                }
            })
        },
        ver => {
            Err(format!("unsuppiorted ver: {ver}").into())
        }
    }
}

pub fn apply_rules(config: Config, pinfo: PacketInfo) {
    todo!()
}