use ihl to derive port bytes

This commit is contained in:
zedddie 2026-03-17 22:27:24 +01:00 committed by tuturuu
parent da8e70f2e3
commit 50524cb5ae
No known key found for this signature in database
GPG key ID: B352C3C2894405A7
2 changed files with 14 additions and 12 deletions

View file

@ -52,13 +52,15 @@ pub fn sniff_raw_packets(packet: &Packet) -> SniffedPacket {
let ver = packet[0] >> 4;
match ver {
4 => {
let dst_port = Port::from_be_bytes([packet[22], packet[23]]);
// 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[20], packet[21]]),
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] {