From f74089a3651b8f26e7bda24afe9fd92794eea33c Mon Sep 17 00:00:00 2001 From: zedddie Date: Mon, 23 Mar 2026 20:58:56 +0100 Subject: fix ipv6 src&dst ip representation in PacketInfo --- src/sniffing/headers.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src/sniffing/headers.rs') diff --git a/src/sniffing/headers.rs b/src/sniffing/headers.rs index d49d402..40a311c 100644 --- a/src/sniffing/headers.rs +++ b/src/sniffing/headers.rs @@ -47,12 +47,12 @@ impl fmt::Display for PacketInfo { if self.version() == &IpVersion::V4 { let src_ip = self.src_ipv4_ip().unwrap(); let dst_ip = self.dst_ipv4_ip().unwrap(); - write!(f, "{:?} {}.{}.{}.{}:{} -> {}.{}.{}.{}:{} {:?} is 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, "{}.{}.{}.{}:{} -> {}.{}.{}.{}:{} {:?} is dns? {:?}", 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()) } else { let src_ip = self.src_ipv6_ip().unwrap(); let dst_ip = self.dst_ipv6_ip().unwrap(); - // y:y:y:y:y:y:y:y = 8 hexademical - write!(f, "{:?} {}:{}:{}:{}:{}:{}:{}:{} port:{} -> {}:{}:{}:{}:{}:{}:{}:{} port:{} {:?} is dns? {:?}", self.version(), src_ip[0], src_ip[1], src_ip[2], src_ip[3], src_ip[4], src_ip[5], src_ip[6], src_ip[7], self.src_port(), dst_ip[0], dst_ip[1], dst_ip[2], dst_ip[3], dst_ip[4], dst_ip[5], dst_ip[6], dst_ip[7], self.dst_port(), self.protocol(), self.dns()) + // y:y:y:y:y:y:y:y = 8 hexademical; y = segment, pair of 2 u8 big endian + write!(f, "{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x} port:{} -> {:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x} port:{} {:?} is dns? {:?}", src_ip[0], src_ip[1], src_ip[2], src_ip[3], src_ip[4], src_ip[5], src_ip[6], src_ip[7], self.src_port(), dst_ip[0], dst_ip[1], dst_ip[2], dst_ip[3], dst_ip[4], dst_ip[5], dst_ip[6], dst_ip[7], self.dst_port(), self.protocol(), self.dns()) } } } @@ -150,16 +150,17 @@ pub fn sniff_raw_packets(packet: &Packet) -> SniffedPacket { Ok(v4) }, 6 => { - // FIXME: fix ipv6 type representation to u16 paired u8 - let src_ip_bytes = &packet[8..24]; - let src_ip = src_ip_bytes.chunks(2).map(|b| u16::from_be_bytes(b[0], b[1])); + // y:y:y:y:y:y:y:y hexademical; y = segment, pair of 2 u8 in big endian + let src_ip = std::array::from_fn(|i| u16::from_be_bytes([packet[8 + i*2], packet[8 + i*2 + 1]])); + let dst_ip = std::array::from_fn(|i| u16::from_be_bytes([packet[24 + i*2], packet[24 + i*2 + 1]])); + let dst_port = Port::from_be_bytes([packet[42], packet[43]]); let dns; if dst_port == 53 { dns = true; } else { dns = false; }; let v6 = PacketInfo::V6{ src_ip, src_port: Port::from_be_bytes([packet[40], packet[41]]), - dst_ip: ::try_from(&packet[24..40])?, + dst_ip, dst_port, protocol: match packet[6] { 6 => Protocol::TCP, -- cgit v1.2.3