reworked ipv6, field still faulty
This commit is contained in:
parent
86b9051277
commit
cfa405b5c2
|
@ -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<T: AsRef<[u8]> + AsMut<[u8]>> BitfieldIpV6Header<T> {
|
||||
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<T: AsRef<[u8]> + AsMut<[u8]>> BitfieldIpV6Header<T> {
|
|||
}
|
||||
|
||||
|
||||
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)]
|
||||
|
|
Loading…
Reference in New Issue