fn parse(data: &[u8]) -> Result<Ipv4PacketHeader> {
        let size = mem::size_of::<RawIpv4PacketHeader>();
        if data.len() < size {
            Err(PacketParseError::from("unable to parse IPv4 packet, not enough data"))
        } else {
            let ptr = data.as_ptr();
            let ptr = ptr as *const RawIpv4PacketHeader;
            let rh  = unsafe {
                &*ptr
            };

            let flags_foffset = u16::from_be(rh.flags_foffset);
            let ihl           = rh.vihl & 0x0f;
            let options_len   = ihl as usize - (size >> 2);
            let offset_1      = size as isize;

            if data.len() < (size + (options_len << 2)) {
                Err(PacketParseError::from("unable to parse IPv4 packet, not enough data"))
            } else {
                let options = unsafe {
                    utils::vec_from_raw_parts(
                        ptr.offset(offset_1) as *const u32,
                        options_len)
                };

                let res = Ipv4PacketHeader {
                    version:  rh.vihl >> 4,
                    dscp:     rh.dscp_ecn >> 2,
                    ecn:      rh.dscp_ecn & 0x03,
                    ident:    u16::from_be(rh.ident),
                    flags:    (flags_foffset >> 13) as u8,
                    foffset:  flags_foffset & 0x1fff,
                    ttl:      rh.ttl,
                    protocol: rh.protocol,
                    src:      raw::utils::slice_to_ipv4addr(&rh.src),
                    dst:      raw::utils::slice_to_ipv4addr(&rh.dst),
                    options:  options,
                    length:   u16::from_be(rh.length) as usize
                };

                Ok(res)
            }
        }
    }
/// Read header from given raw representation.
Read header from given raw representation.
Read header from given raw representation .
fn parse(data: &[u8]) -> Result<Ipv4PacketHeader> {
        let size = mem::size_of::<RawIpv4PacketHeader>();
        if data.len() < size {
            Err(PacketParseError::from("unable to parse IPv4 packet, not enough data"))
        } else {
            let ptr = data.as_ptr();
            let ptr = ptr as *const RawIpv4PacketHeader;
            let rh  = unsafe {
                &*ptr
            };
            let flags_foffset = u16::from_be(rh.flags_foffset);
            let ihl           = rh.vihl & 0x0f;
            let options_len   = ihl as usize - (size >> 2);
            let offset_1      = size as isize;
            if data.len() < (size + (options_len << 2)) {
                Err(PacketParseError::from("unable to parse IPv4 packet, not enough data"))
            } else {
                let options = unsafe {
                    utils::vec_from_raw_parts(
                        ptr.offset(offset_1) as *const u32,
                        options_len)
                };
                let res = Ipv4PacketHeader {
                    version:  rh.vihl >> 4,
                    dscp:     rh.dscp_ecn >> 2,
                    ecn:      rh.dscp_ecn & 0x03,
                    ident:    u16::from_be(rh.ident),
                    flags:    (flags_foffset >> 13) as u8,
                    foffset:  flags_foffset & 0x1fff,
                    ttl:      rh.ttl,
                    protocol: rh.protocol,
                    src:      raw::utils::slice_to_ipv4addr(&rh.src),
                    dst:      raw::utils::slice_to_ipv4addr(&rh.dst),
                    options:  options,
                    length:   u16::from_be(rh.length) as usize
                };
                Ok(res)
            }
        }
    }
fn parse ( data : & [ u8 ] ) -> Result < Ipv4PacketHeader > { let size = mem :: size_of :: < RawIpv4PacketHeader > ( ) ; if data . len ( ) < size { Err ( PacketParseError :: from ( " " ) ) } else { let ptr = data . as_ptr ( ) ; let ptr = ptr as * const RawIpv4PacketHeader ; let rh = unsafe { & * ptr } ; let flags_foffset = u16 :: from_be ( rh . flags_foffset ) ; let ihl = rh . vihl & 0x0f ; let options_len = ihl as usize - ( size >> 2 ) ; let offset_1 = size as isize ; if data . len ( ) < ( size + ( options_len << 2 ) ) { Err ( PacketParseError :: from ( " " ) ) } else { let options = unsafe { utils :: vec_from_raw_parts ( ptr . offset ( offset_1 ) as * const u32 , options_len ) } ; let res = Ipv4PacketHeader { version : rh . vihl >> 4 , dscp : rh . dscp_ecn >> 2 , ecn : rh . dscp_ecn & 0x03 , ident : u16 :: from_be ( rh . ident ) , flags : ( flags_foffset >> 13 ) as u8 , foffset : flags_foffset & 0x1fff , ttl : rh . ttl , protocol : rh . protocol , src : raw :: utils :: slice_to_ipv4addr ( & rh . src ) , dst : raw :: utils :: slice_to_ipv4addr ( & rh . dst ) , options : options , length : u16 :: from_be ( rh . length ) as usize } ; Ok ( res ) } } }
Read header from given raw representation.
Read header from given raw representation .
data
&[u8]
data
&[u8]