impl of ipv6

This commit is contained in:
Stefan Friese 2020-05-15 12:49:10 +02:00
parent fa59be0b1c
commit dd49870da4
1 changed files with 58 additions and 1 deletions

View File

@ -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<T: AsRef<[u8]> + AsMut<[u8]>> BitfieldIpV6Header<T> {
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 {