summaryrefslogtreecommitdiff
path: root/src/sniffing
diff options
context:
space:
mode:
authorzedddie <zedddie@noreply.codeberg.org>2026-03-17 00:04:20 +0100
committerzedddie <zedddie@noreply.codeberg.org>2026-03-17 00:04:20 +0100
commit0f333db702f96379fe75c7003eb065cdf29d5112 (patch)
tree894a077fe1c6b36282ae6b5b3055f9b6783f6a57 /src/sniffing
parent7b1bfbbed9f6abda4fcf692ccedd47b139c77f40 (diff)
parent2eb03ac7688387e15d3b210f1946896380d38080 (diff)
Merge pull request 'small-fixes & improvements' (#3) from small-fixes into main
Reviewed-on: https://codeberg.org/NamelessTeam/nsc/pulls/3
Diffstat (limited to 'src/sniffing')
-rw-r--r--src/sniffing/headers.rs55
1 files changed, 37 insertions, 18 deletions
diff --git a/src/sniffing/headers.rs b/src/sniffing/headers.rs
index 9b605b9..a5b0480 100644
--- a/src/sniffing/headers.rs
+++ b/src/sniffing/headers.rs
@@ -20,7 +20,8 @@ pub enum PacketInfo {
src_port: Port,
dst_ip: Ipv4,
dst_port: Port,
- protocol: Protocol
+ protocol: Protocol,
+ dns: bool
},
// <https://www.geeksforgeeks.org/computer-networks/internet-protocol-version-6-ipv6-header/>
V6 {
@@ -28,7 +29,8 @@ pub enum PacketInfo {
src_port: Port,
dst_ip: Ipv6,
dst_port: Port,
- protocol: Protocol
+ protocol: Protocol,
+ dns: bool
}
}
@@ -40,46 +42,63 @@ impl PacketInfo {
}
}
}
-pub fn sniff_raw_packets(packet: &[u8]) -> Result<PacketInfo, Box<dyn std::error::Error + Send + Sync + 'static>> {
+
+// 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;
- dbg!(ver);
match ver {
4 => {
+ let dst_port = Port::from_be_bytes([packet[22], packet[23]]);
+ let dns;
+ if dst_port == 53 { dns = true; } else { dns = false; };
+ // FIXME: hardcoded IPv4 port offset
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]]),
+ src_ip: <Ipv4>::try_from(&packet[12..16])?,
+ src_port: Port::from_be_bytes([packet[20], packet[21]]),
+ 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 {
- println!("oppsie unsupported");
+ // 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 => {
- println!("im in 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: <[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]]),
+ 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,
- 4 => Protocol::UDP,
+ 17 => Protocol::UDP,
p => Protocol::Unsupported(p)
- }
+ },
+ dns
};
if !matches!(v6.protocol(), Protocol::Unsupported(_)) {
println!("{v6:?}");
} else {
- println!("oppsie unsupported");
+ // TODO: make --debug option which will include this diagnostic, for general use this
+ // should be off
+ // println!("oppsie unsupported protocol: {:?}", v6.protocol());
}
Ok(v6)
},