From cfa405b5c2e2f85cd2b330e1f73c23b0f1ef13da Mon Sep 17 00:00:00 2001 From: gurkenhabicht Date: Mon, 18 May 2020 15:21:09 +0200 Subject: [PATCH] reworked ipv6, field still faulty --- src/parser/packet_handler.rs | 72 +++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 18 deletions(-) diff --git a/src/parser/packet_handler.rs b/src/parser/packet_handler.rs index 4b0e72d..38729c5 100644 --- a/src/parser/packet_handler.rs +++ b/src/parser/packet_handler.rs @@ -2,11 +2,11 @@ extern crate eui48; extern crate byteorder; extern crate bitfield; -use byteorder::{ByteOrder, LittleEndian}; +use byteorder::{ByteOrder, LittleEndian, ReadBytesExt}; use eui48::{MacAddress, Eui48}; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use bitfield::{bitfield, BitRange}; - +use std::io::Cursor; /* ethernet */ const ETH_ALEN: usize = 6; @@ -118,6 +118,41 @@ pub fn ip_handler ( packet_data: &[u8] ) -> IpV4Header { } /* ipv6 */ +#[derive(Debug, Copy, Clone)] +pub struct IpV6Header { + pub version: u8, + pub traffic_class: u8, + pub flow_label: u32, + pub payload_length: u16, + pub next_header: u8, + pub hop_limit: u8, + pub source_address: IpAddr, + pub destination_address: IpAddr, +} + +pub fn ipv6_handler ( packet_data: &[u8] ) -> IpV6Header { + let (_head, tail) = packet_data.split_at(ETHER_HDRLEN); + let (raw_hdr, _) = tail.split_at(40); + let mut _tail: [u8; 40] = [0; 40]; + _tail.copy_from_slice(raw_hdr); + //let mut rdr = Cursor::new(_tail); + + + + IpV6Header { + version: &raw_hdr[0] & 0xf0 >> 4, + traffic_class: (&raw_hdr[0] & 0x0f >> 4)| (&raw_hdr[1] & 0xf0 <<4) , + flow_label: LittleEndian::read_u32( &[0x00 ,(&_tail[1] &0x0f) , _tail[2] , _tail[3]]), + payload_length: LittleEndian::read_u16(&[_tail[4], _tail[5]]), + next_header: _tail[6], + hop_limit: _tail[7], + source_address: IpAddr::V6(Ipv6Addr::from(LittleEndian::read_u128(&_tail[8..24]))), + destination_address: IpAddr::V6(Ipv6Addr::from(LittleEndian::read_u128(&_tail[24..40]))), + } +} + +/* +#[derive(Debug,Copy, Clone)] pub struct IpV6Header { pub version: u32, pub traffic_class: u32, @@ -127,25 +162,25 @@ pub struct IpV6Header { pub hop_limit: u32, pub source_address: IpAddr, pub destination_address: IpAddr, -} - -bitfield!{ +}*/ +/* I ve got no glue how this bitfield syntax works with impl and bitranges involved, if you do plz tell me */ +/*bitfield!{ struct BitfieldIpV6Header (MSB0 [u8]); impl Debug; u32; - get_version, _: 0, 3; - get_traffic_class, _: 4, 11; - get_flow_label, _: 12, 31; - get_payload_length, _: 32, 47; - get_next_header, _: 48, 55; - get_hop_limit, _: 56, 63; - u16,get_source_address, _: 191, 64, 16; - u128,into Ipv6Addr,get_destination_address, _:319, 192; + get_version, _: 3, 0; + get_traffic_class, _: 11, 4; + get_flow_label, _: 31, 12; + get_payload_length, _: 47, 32; + get_next_header, _: 55, 48; + get_hop_limit, _: 63, 56; + u8,get_source_address, _: 191, 64, 16; + u32,into Ipv6Addr, get_destination_address, _:319, 192; } impl + AsMut<[u8]>> BitfieldIpV6Header { fn get_source_as_ip_addr(&self) -> Ipv6Addr { - let mut src = [0; 8]; + let mut src = [0; 16]; for (i, src) in src.iter_mut().enumerate() { *src = self.get_source_address(i); } @@ -154,11 +189,11 @@ impl + AsMut<[u8]>> BitfieldIpV6Header { } -fn ipv6_handler( packet_data: &[u8] ) -> IpV6Header { +pub fn ipv6_handler( packet_data: &[u8] ) -> IpV6Header { let (_head, tail) = packet_data.split_at(ETHER_HDRLEN); - let (raw_hdr, _) = tail.split_at(24); - let mut _tail: [u8; 24] = [0; 24]; - _tail.copy_from_slice(raw_hdr); + let (raw_hdr, _) = tail.split_at(40); + let mut _tail: [u8; 40] = [0; 40]; + _tail.copy_from_slice(&raw_hdr); let v6_header = BitfieldIpV6Header(_tail); @@ -173,6 +208,7 @@ fn ipv6_handler( packet_data: &[u8] ) -> IpV6Header { destination_address: IpAddr::V6( v6_header.get_destination_address() ), } } +*/ /* tcp */ #[derive(Debug,Copy,Clone)]