From dd49870da426bfe1317a5a108eb52047ea69dad6 Mon Sep 17 00:00:00 2001 From: Stefan Friese Date: Fri, 15 May 2020 12:49:10 +0200 Subject: [PATCH] impl of ipv6 --- src/parser/packet_handler.rs | 59 +++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src/parser/packet_handler.rs b/src/parser/packet_handler.rs index 6613f44..a115da1 100644 --- a/src/parser/packet_handler.rs +++ b/src/parser/packet_handler.rs @@ -4,7 +4,7 @@ extern crate bitfield; use byteorder::{ByteOrder, LittleEndian}; use eui48::{MacAddress, Eui48}; -use std::net::{IpAddr, Ipv4Addr}; +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use bitfield::bitfield; @@ -117,6 +117,63 @@ pub fn ip_handler ( packet_data: &[u8] ) -> IpV4Header { } } +/* ipv6 */ +pub struct IpV6Header { + pub version: u32, + pub traffic_class: u32, + pub flow_label: u32, + pub payload_length: u32, + pub next_header: u32, + pub hop_limit: u32, + pub source_address: IpAddr, + pub destination_address: IpAddr, +} + +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; +} + +impl + AsMut<[u8]>> BitfieldIpV6Header { + fn get_source_as_ip_addr(&self) -> Ipv6Addr { + let mut src = [0; 8]; + for (i, src) in src.iter_mut().enumerate() { + *src = self.get_source_address(i); + } + src.into() + } +} + + +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 v6_header = BitfieldIpV6Header(_tail); + + IpV6Header { + version: v6_header.get_version(), + traffic_class: v6_header.get_traffic_class(), + flow_label: v6_header.get_flow_label(), + payload_length: v6_header.get_payload_length(), + next_header: v6_header.get_next_header(), + hop_limit: v6_header.get_hop_limit(), + source_address: IpAddr::V6( v6_header.get_source_as_ip_addr() ), + destination_address: IpAddr::V6( v6_header.get_destination_address() ), + } +} + /* tcp */ #[derive(Debug,Copy,Clone)] pub struct TcpHeader {