#[cfg(test)]
use Packet;
use pnet_macros_support::types::*;
#[derive(PartialEq)]
pub struct GrePacket<'p> {
packet: ::pnet_macros_support::packet::PacketData<'p>,
}
#[derive(PartialEq)]
pub struct MutableGrePacket<'p> {
packet: ::pnet_macros_support::packet::MutPacketData<'p>,
}
impl <'a> GrePacket<'a> {
#[inline]
pub fn new<'p>(packet: &'p [u8]) -> Option<GrePacket<'p>> {
if packet.len() >= GrePacket::minimum_packet_size() {
use ::pnet_macros_support::packet::PacketData;
Some(GrePacket{packet: PacketData::Borrowed(packet),})
} else { None }
}
pub fn owned(packet: Vec<u8>) -> Option<GrePacket<'static>> {
if packet.len() >= GrePacket::minimum_packet_size() {
use ::pnet_macros_support::packet::PacketData;
Some(GrePacket{packet: PacketData::Owned(packet),})
} else { None }
}
#[inline]
pub fn to_immutable<'p>(&'p self) -> GrePacket<'p> {
use ::pnet_macros_support::packet::PacketData;
GrePacket{packet: PacketData::Borrowed(self.packet.as_slice()),}
}
#[inline]
pub fn consume_to_immutable(self) -> GrePacket<'a> {
GrePacket{packet: self.packet.to_immutable(),}
}
#[inline]
pub const fn minimum_packet_size() -> usize { 4 }
#[inline]
pub fn packet_size(_packet: &Gre) -> usize {
4 + _packet.checksum.len() + _packet.offset.len() + _packet.key.len()
+ _packet.sequence.len() + _packet.routing.len() +
_packet.payload.len()
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_checksum_present(&self) -> u1 {
let _self = self;
let co = 0;
((_self.packet[co] as u1) & 128) >> 7
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_routing_present(&self) -> u1 {
let _self = self;
let co = 0;
((_self.packet[co] as u1) & 64) >> 6
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_key_present(&self) -> u1 {
let _self = self;
let co = 0;
((_self.packet[co] as u1) & 32) >> 5
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_sequence_present(&self) -> u1 {
let _self = self;
let co = 0;
((_self.packet[co] as u1) & 16) >> 4
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_strict_source_route(&self) -> u1 {
let _self = self;
let co = 0;
((_self.packet[co] as u1) & 8) >> 3
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_recursion_control(&self) -> u3 {
let _self = self;
let co = 0;
((_self.packet[co] as u3) & 7)
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_zero_flags(&self) -> u5 {
let _self = self;
let co = 1;
((_self.packet[co] as u5) & 248) >> 3
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_version(&self) -> u3 {
let _self = self;
let co = 1;
((_self.packet[co] as u3) & 7)
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_protocol_type(&self) -> u16be {
let _self = self;
let co = 2;
let b0 = ((_self.packet[co + 0] as u16be) << 8) as u16be;
let b1 = ((_self.packet[co + 1] as u16be)) as u16be;
b0 | b1
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_checksum_raw(&self) -> &[u8] {
use std::cmp::min;
let _self = self;
let current_offset = 4;
let end =
min(current_offset + gre_checksum_length(&_self.to_immutable()),
_self.packet.len());
&_self.packet[current_offset..end]
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_checksum(&self) -> Vec<U16BE> {
use pnet_macros_support::packet::FromPacket;
use std::cmp::min;
let _self = self;
let current_offset = 4;
let end =
min(current_offset + gre_checksum_length(&_self.to_immutable()),
_self.packet.len());
U16BEIterable{buf:
&_self.packet[current_offset..end],}.map(|packet|
packet.from_packet()).collect::<Vec<_>>()
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_checksum_iter(&self) -> U16BEIterable {
use std::cmp::min;
let _self = self;
let current_offset = 4;
let end =
min(current_offset + gre_checksum_length(&_self.to_immutable()),
_self.packet.len());
U16BEIterable{buf: &_self.packet[current_offset..end],}
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_offset_raw(&self) -> &[u8] {
use std::cmp::min;
let _self = self;
let current_offset = 4 + gre_checksum_length(&_self.to_immutable());
let end =
min(current_offset + gre_offset_length(&_self.to_immutable()),
_self.packet.len());
&_self.packet[current_offset..end]
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_offset(&self) -> Vec<U16BE> {
use pnet_macros_support::packet::FromPacket;
use std::cmp::min;
let _self = self;
let current_offset = 4 + gre_checksum_length(&_self.to_immutable());
let end =
min(current_offset + gre_offset_length(&_self.to_immutable()),
_self.packet.len());
U16BEIterable{buf:
&_self.packet[current_offset..end],}.map(|packet|
packet.from_packet()).collect::<Vec<_>>()
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_offset_iter(&self) -> U16BEIterable {
use std::cmp::min;
let _self = self;
let current_offset = 4 + gre_checksum_length(&_self.to_immutable());
let end =
min(current_offset + gre_offset_length(&_self.to_immutable()),
_self.packet.len());
U16BEIterable{buf: &_self.packet[current_offset..end],}
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_key_raw(&self) -> &[u8] {
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable());
let end =
min(current_offset + gre_key_length(&_self.to_immutable()),
_self.packet.len());
&_self.packet[current_offset..end]
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_key(&self) -> Vec<U32BE> {
use pnet_macros_support::packet::FromPacket;
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable());
let end =
min(current_offset + gre_key_length(&_self.to_immutable()),
_self.packet.len());
U32BEIterable{buf:
&_self.packet[current_offset..end],}.map(|packet|
packet.from_packet()).collect::<Vec<_>>()
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_key_iter(&self) -> U32BEIterable {
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable());
let end =
min(current_offset + gre_key_length(&_self.to_immutable()),
_self.packet.len());
U32BEIterable{buf: &_self.packet[current_offset..end],}
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_sequence_raw(&self) -> &[u8] {
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable());
let end =
min(current_offset + gre_sequence_length(&_self.to_immutable()),
_self.packet.len());
&_self.packet[current_offset..end]
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_sequence(&self) -> Vec<U32BE> {
use pnet_macros_support::packet::FromPacket;
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable());
let end =
min(current_offset + gre_sequence_length(&_self.to_immutable()),
_self.packet.len());
U32BEIterable{buf:
&_self.packet[current_offset..end],}.map(|packet|
packet.from_packet()).collect::<Vec<_>>()
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_sequence_iter(&self) -> U32BEIterable {
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable());
let end =
min(current_offset + gre_sequence_length(&_self.to_immutable()),
_self.packet.len());
U32BEIterable{buf: &_self.packet[current_offset..end],}
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_routing_raw(&self) -> &[u8] {
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable()) +
gre_sequence_length(&_self.to_immutable());
let end =
min(current_offset + gre_routing_length(&_self.to_immutable()),
_self.packet.len());
&_self.packet[current_offset..end]
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_routing(&self) -> Vec<u8> {
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable()) +
gre_sequence_length(&_self.to_immutable());
let pkt_len = self.packet.len();
let end =
min(current_offset + gre_routing_length(&_self.to_immutable()),
pkt_len);
let packet = &_self.packet[current_offset..end];
let mut vec: Vec<u8> = Vec::with_capacity(packet.len());
let mut co = 0;
for _ in 0..vec.capacity() {
vec.push({ (packet[co] as u8) });
co += 1;
}
vec
}
}
impl <'a> MutableGrePacket<'a> {
#[inline]
pub fn new<'p>(packet: &'p mut [u8]) -> Option<MutableGrePacket<'p>> {
if packet.len() >= MutableGrePacket::minimum_packet_size() {
use ::pnet_macros_support::packet::MutPacketData;
Some(MutableGrePacket{packet: MutPacketData::Borrowed(packet),})
} else { None }
}
pub fn owned(packet: Vec<u8>) -> Option<MutableGrePacket<'static>> {
if packet.len() >= MutableGrePacket::minimum_packet_size() {
use ::pnet_macros_support::packet::MutPacketData;
Some(MutableGrePacket{packet: MutPacketData::Owned(packet),})
} else { None }
}
#[inline]
pub fn to_immutable<'p>(&'p self) -> GrePacket<'p> {
use ::pnet_macros_support::packet::PacketData;
GrePacket{packet: PacketData::Borrowed(self.packet.as_slice()),}
}
#[inline]
pub fn consume_to_immutable(self) -> GrePacket<'a> {
GrePacket{packet: self.packet.to_immutable(),}
}
#[inline]
pub const fn minimum_packet_size() -> usize { 4 }
#[inline]
pub fn packet_size(_packet: &Gre) -> usize {
4 + _packet.checksum.len() + _packet.offset.len() + _packet.key.len()
+ _packet.sequence.len() + _packet.routing.len() +
_packet.payload.len()
}
#[inline]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn populate(&mut self, packet: &Gre) {
let _self = self;
_self.set_checksum_present(packet.checksum_present);
_self.set_routing_present(packet.routing_present);
_self.set_key_present(packet.key_present);
_self.set_sequence_present(packet.sequence_present);
_self.set_strict_source_route(packet.strict_source_route);
_self.set_recursion_control(packet.recursion_control);
_self.set_zero_flags(packet.zero_flags);
_self.set_version(packet.version);
_self.set_protocol_type(packet.protocol_type);
_self.set_checksum(&packet.checksum);
_self.set_offset(&packet.offset);
_self.set_key(&packet.key);
_self.set_sequence(&packet.sequence);
_self.set_routing(&packet.routing);
_self.set_payload(&packet.payload);
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_checksum_present(&self) -> u1 {
let _self = self;
let co = 0;
((_self.packet[co] as u1) & 128) >> 7
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_routing_present(&self) -> u1 {
let _self = self;
let co = 0;
((_self.packet[co] as u1) & 64) >> 6
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_key_present(&self) -> u1 {
let _self = self;
let co = 0;
((_self.packet[co] as u1) & 32) >> 5
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_sequence_present(&self) -> u1 {
let _self = self;
let co = 0;
((_self.packet[co] as u1) & 16) >> 4
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_strict_source_route(&self) -> u1 {
let _self = self;
let co = 0;
((_self.packet[co] as u1) & 8) >> 3
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_recursion_control(&self) -> u3 {
let _self = self;
let co = 0;
((_self.packet[co] as u3) & 7)
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_zero_flags(&self) -> u5 {
let _self = self;
let co = 1;
((_self.packet[co] as u5) & 248) >> 3
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_version(&self) -> u3 {
let _self = self;
let co = 1;
((_self.packet[co] as u3) & 7)
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_protocol_type(&self) -> u16be {
let _self = self;
let co = 2;
let b0 = ((_self.packet[co + 0] as u16be) << 8) as u16be;
let b1 = ((_self.packet[co + 1] as u16be)) as u16be;
b0 | b1
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_checksum_raw(&self) -> &[u8] {
use std::cmp::min;
let _self = self;
let current_offset = 4;
let end =
min(current_offset + gre_checksum_length(&_self.to_immutable()),
_self.packet.len());
&_self.packet[current_offset..end]
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_checksum(&self) -> Vec<U16BE> {
use pnet_macros_support::packet::FromPacket;
use std::cmp::min;
let _self = self;
let current_offset = 4;
let end =
min(current_offset + gre_checksum_length(&_self.to_immutable()),
_self.packet.len());
U16BEIterable{buf:
&_self.packet[current_offset..end],}.map(|packet|
packet.from_packet()).collect::<Vec<_>>()
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_checksum_iter(&self) -> U16BEIterable {
use std::cmp::min;
let _self = self;
let current_offset = 4;
let end =
min(current_offset + gre_checksum_length(&_self.to_immutable()),
_self.packet.len());
U16BEIterable{buf: &_self.packet[current_offset..end],}
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_offset_raw(&self) -> &[u8] {
use std::cmp::min;
let _self = self;
let current_offset = 4 + gre_checksum_length(&_self.to_immutable());
let end =
min(current_offset + gre_offset_length(&_self.to_immutable()),
_self.packet.len());
&_self.packet[current_offset..end]
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_offset(&self) -> Vec<U16BE> {
use pnet_macros_support::packet::FromPacket;
use std::cmp::min;
let _self = self;
let current_offset = 4 + gre_checksum_length(&_self.to_immutable());
let end =
min(current_offset + gre_offset_length(&_self.to_immutable()),
_self.packet.len());
U16BEIterable{buf:
&_self.packet[current_offset..end],}.map(|packet|
packet.from_packet()).collect::<Vec<_>>()
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_offset_iter(&self) -> U16BEIterable {
use std::cmp::min;
let _self = self;
let current_offset = 4 + gre_checksum_length(&_self.to_immutable());
let end =
min(current_offset + gre_offset_length(&_self.to_immutable()),
_self.packet.len());
U16BEIterable{buf: &_self.packet[current_offset..end],}
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_key_raw(&self) -> &[u8] {
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable());
let end =
min(current_offset + gre_key_length(&_self.to_immutable()),
_self.packet.len());
&_self.packet[current_offset..end]
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_key(&self) -> Vec<U32BE> {
use pnet_macros_support::packet::FromPacket;
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable());
let end =
min(current_offset + gre_key_length(&_self.to_immutable()),
_self.packet.len());
U32BEIterable{buf:
&_self.packet[current_offset..end],}.map(|packet|
packet.from_packet()).collect::<Vec<_>>()
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_key_iter(&self) -> U32BEIterable {
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable());
let end =
min(current_offset + gre_key_length(&_self.to_immutable()),
_self.packet.len());
U32BEIterable{buf: &_self.packet[current_offset..end],}
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_sequence_raw(&self) -> &[u8] {
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable());
let end =
min(current_offset + gre_sequence_length(&_self.to_immutable()),
_self.packet.len());
&_self.packet[current_offset..end]
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_sequence(&self) -> Vec<U32BE> {
use pnet_macros_support::packet::FromPacket;
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable());
let end =
min(current_offset + gre_sequence_length(&_self.to_immutable()),
_self.packet.len());
U32BEIterable{buf:
&_self.packet[current_offset..end],}.map(|packet|
packet.from_packet()).collect::<Vec<_>>()
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_sequence_iter(&self) -> U32BEIterable {
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable());
let end =
min(current_offset + gre_sequence_length(&_self.to_immutable()),
_self.packet.len());
U32BEIterable{buf: &_self.packet[current_offset..end],}
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_routing_raw(&self) -> &[u8] {
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable()) +
gre_sequence_length(&_self.to_immutable());
let end =
min(current_offset + gre_routing_length(&_self.to_immutable()),
_self.packet.len());
&_self.packet[current_offset..end]
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_routing(&self) -> Vec<u8> {
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable()) +
gre_sequence_length(&_self.to_immutable());
let pkt_len = self.packet.len();
let end =
min(current_offset + gre_routing_length(&_self.to_immutable()),
pkt_len);
let packet = &_self.packet[current_offset..end];
let mut vec: Vec<u8> = Vec::with_capacity(packet.len());
let mut co = 0;
for _ in 0..vec.capacity() {
vec.push({ (packet[co] as u8) });
co += 1;
}
vec
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_checksum_present(&mut self, val: u1) {
let _self = self;
let co = 0;
_self.packet[co + 0] =
((_self.packet[co + 0] & 127) | (((val & 1) << 7) as u8)) as u8;
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_routing_present(&mut self, val: u1) {
let _self = self;
let co = 0;
_self.packet[co + 0] =
((_self.packet[co + 0] & 191) | (((val & 1) << 6) as u8)) as u8;
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_key_present(&mut self, val: u1) {
let _self = self;
let co = 0;
_self.packet[co + 0] =
((_self.packet[co + 0] & 223) | (((val & 1) << 5) as u8)) as u8;
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_sequence_present(&mut self, val: u1) {
let _self = self;
let co = 0;
_self.packet[co + 0] =
((_self.packet[co + 0] & 239) | (((val & 1) << 4) as u8)) as u8;
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_strict_source_route(&mut self, val: u1) {
let _self = self;
let co = 0;
_self.packet[co + 0] =
((_self.packet[co + 0] & 247) | (((val & 1) << 3) as u8)) as u8;
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_recursion_control(&mut self, val: u3) {
let _self = self;
let co = 0;
_self.packet[co + 0] =
((_self.packet[co + 0] & 248) | (((val & 7)) as u8)) as u8;
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_zero_flags(&mut self, val: u5) {
let _self = self;
let co = 1;
_self.packet[co + 0] =
((_self.packet[co + 0] & 7) | (((val & 31) << 3) as u8)) as u8;
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_version(&mut self, val: u3) {
let _self = self;
let co = 1;
_self.packet[co + 0] =
((_self.packet[co + 0] & 248) | (((val & 7)) as u8)) as u8;
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_protocol_type(&mut self, val: u16be) {
let _self = self;
let co = 2;
_self.packet[co + 0] = ((val & 65280) >> 8) as u8;
_self.packet[co + 1] = (val) as u8;
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_checksum_raw_mut(&mut self) -> &mut [u8] {
use std::cmp::min;
let _self = self;
let current_offset = 4;
let end =
min(current_offset + gre_checksum_length(&_self.to_immutable()),
_self.packet.len());
&mut _self.packet[current_offset..end]
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_checksum(&mut self, vals: &[U16BE]) {
use pnet_macros_support::packet::PacketSize;
let _self = self;
let mut current_offset = 4;
let end = current_offset + gre_checksum_length(&_self.to_immutable());
for val in vals.into_iter() {
let mut packet =
MutableU16BEPacket::new(&mut _self.packet[current_offset..]).unwrap();
packet.populate(val);
current_offset += packet.packet_size();
assert!(current_offset <= end);
}
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_offset_raw_mut(&mut self) -> &mut [u8] {
use std::cmp::min;
let _self = self;
let current_offset = 4 + gre_checksum_length(&_self.to_immutable());
let end =
min(current_offset + gre_offset_length(&_self.to_immutable()),
_self.packet.len());
&mut _self.packet[current_offset..end]
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_offset(&mut self, vals: &[U16BE]) {
use pnet_macros_support::packet::PacketSize;
let _self = self;
let mut current_offset =
4 + gre_checksum_length(&_self.to_immutable());
let end = current_offset + gre_offset_length(&_self.to_immutable());
for val in vals.into_iter() {
let mut packet =
MutableU16BEPacket::new(&mut _self.packet[current_offset..]).unwrap();
packet.populate(val);
current_offset += packet.packet_size();
assert!(current_offset <= end);
}
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_key_raw_mut(&mut self) -> &mut [u8] {
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable());
let end =
min(current_offset + gre_key_length(&_self.to_immutable()),
_self.packet.len());
&mut _self.packet[current_offset..end]
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_key(&mut self, vals: &[U32BE]) {
use pnet_macros_support::packet::PacketSize;
let _self = self;
let mut current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable());
let end = current_offset + gre_key_length(&_self.to_immutable());
for val in vals.into_iter() {
let mut packet =
MutableU32BEPacket::new(&mut _self.packet[current_offset..]).unwrap();
packet.populate(val);
current_offset += packet.packet_size();
assert!(current_offset <= end);
}
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_sequence_raw_mut(&mut self) -> &mut [u8] {
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable());
let end =
min(current_offset + gre_sequence_length(&_self.to_immutable()),
_self.packet.len());
&mut _self.packet[current_offset..end]
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_sequence(&mut self, vals: &[U32BE]) {
use pnet_macros_support::packet::PacketSize;
let _self = self;
let mut current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable());
let end = current_offset + gre_sequence_length(&_self.to_immutable());
for val in vals.into_iter() {
let mut packet =
MutableU32BEPacket::new(&mut _self.packet[current_offset..]).unwrap();
packet.populate(val);
current_offset += packet.packet_size();
assert!(current_offset <= end);
}
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_routing_raw_mut(&mut self) -> &mut [u8] {
use std::cmp::min;
let _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable()) +
gre_sequence_length(&_self.to_immutable());
let end =
min(current_offset + gre_routing_length(&_self.to_immutable()),
_self.packet.len());
&mut _self.packet[current_offset..end]
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_routing(&mut self, vals: &[u8]) {
use std::ptr::copy_nonoverlapping;
let mut _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable()) +
gre_sequence_length(&_self.to_immutable());
let len = gre_routing_length(&_self.to_immutable());
assert!(vals . len ( ) <= len);
unsafe {
copy_nonoverlapping(vals[..].as_ptr(),
_self.packet[current_offset..].as_mut_ptr(),
vals.len())
}
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_payload(&mut self, vals: &[u8]) {
use std::ptr::copy_nonoverlapping;
let mut _self = self;
let current_offset =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable()) +
gre_sequence_length(&_self.to_immutable()) +
gre_routing_length(&_self.to_immutable());
unsafe {
copy_nonoverlapping(vals[..].as_ptr(),
_self.packet[current_offset..].as_mut_ptr(),
vals.len())
}
}
}
impl <'a> ::pnet_macros_support::packet::PacketSize for GrePacket<'a> {
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn packet_size(&self) -> usize {
let _self = self;
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable()) +
gre_sequence_length(&_self.to_immutable()) +
gre_routing_length(&_self.to_immutable())
}
}
impl <'a> ::pnet_macros_support::packet::PacketSize for MutableGrePacket<'a> {
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn packet_size(&self) -> usize {
let _self = self;
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable()) +
gre_sequence_length(&_self.to_immutable()) +
gre_routing_length(&_self.to_immutable())
}
}
impl <'a> ::pnet_macros_support::packet::MutablePacket for
MutableGrePacket<'a> {
#[inline]
fn packet_mut<'p>(&'p mut self) -> &'p mut [u8] { &mut self.packet[..] }
#[inline]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn payload_mut<'p>(&'p mut self) -> &'p mut [u8] {
let _self = self;
let start =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable()) +
gre_sequence_length(&_self.to_immutable()) +
gre_routing_length(&_self.to_immutable());
if _self.packet.len() <= start { return &mut []; }
&mut _self.packet[start..]
}
}
impl <'a> ::pnet_macros_support::packet::Packet for MutableGrePacket<'a> {
#[inline]
fn packet<'p>(&'p self) -> &'p [u8] { &self.packet[..] }
#[inline]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn payload<'p>(&'p self) -> &'p [u8] {
let _self = self;
let start =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable()) +
gre_sequence_length(&_self.to_immutable()) +
gre_routing_length(&_self.to_immutable());
if _self.packet.len() <= start { return &[]; }
&_self.packet[start..]
}
}
impl <'a> ::pnet_macros_support::packet::Packet for GrePacket<'a> {
#[inline]
fn packet<'p>(&'p self) -> &'p [u8] { &self.packet[..] }
#[inline]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn payload<'p>(&'p self) -> &'p [u8] {
let _self = self;
let start =
4 + gre_checksum_length(&_self.to_immutable()) +
gre_offset_length(&_self.to_immutable()) +
gre_key_length(&_self.to_immutable()) +
gre_sequence_length(&_self.to_immutable()) +
gre_routing_length(&_self.to_immutable());
if _self.packet.len() <= start { return &[]; }
&_self.packet[start..]
}
}
pub struct GreIterable<'a> {
buf: &'a [u8],
}
impl <'a> Iterator for GreIterable<'a> {
type
Item
=
GrePacket<'a>;
fn next(&mut self) -> Option<GrePacket<'a>> {
use pnet_macros_support::packet::PacketSize;
use std::cmp::min;
if self.buf.len() > 0 {
if let Some(ret) = GrePacket::new(self.buf) {
let start = min(ret.packet_size(), self.buf.len());
self.buf = &self.buf[start..];
return Some(ret);
}
}
None
}
fn size_hint(&self) -> (usize, Option<usize>) { (0, None) }
}
impl <'p> ::pnet_macros_support::packet::FromPacket for GrePacket<'p> {
type
T
=
Gre;
#[inline]
fn from_packet(&self) -> Gre {
use pnet_macros_support::packet::Packet;
let _self = self;
Gre{checksum_present: _self.get_checksum_present(),
routing_present: _self.get_routing_present(),
key_present: _self.get_key_present(),
sequence_present: _self.get_sequence_present(),
strict_source_route: _self.get_strict_source_route(),
recursion_control: _self.get_recursion_control(),
zero_flags: _self.get_zero_flags(),
version: _self.get_version(),
protocol_type: _self.get_protocol_type(),
checksum: _self.get_checksum(),
offset: _self.get_offset(),
key: _self.get_key(),
sequence: _self.get_sequence(),
routing: _self.get_routing(),
payload:
{
let payload = self.payload();
let mut vec = Vec::with_capacity(payload.len());
vec.extend_from_slice(payload);
vec
},}
}
}
impl <'p> ::pnet_macros_support::packet::FromPacket for MutableGrePacket<'p> {
type
T
=
Gre;
#[inline]
fn from_packet(&self) -> Gre {
use pnet_macros_support::packet::Packet;
let _self = self;
Gre{checksum_present: _self.get_checksum_present(),
routing_present: _self.get_routing_present(),
key_present: _self.get_key_present(),
sequence_present: _self.get_sequence_present(),
strict_source_route: _self.get_strict_source_route(),
recursion_control: _self.get_recursion_control(),
zero_flags: _self.get_zero_flags(),
version: _self.get_version(),
protocol_type: _self.get_protocol_type(),
checksum: _self.get_checksum(),
offset: _self.get_offset(),
key: _self.get_key(),
sequence: _self.get_sequence(),
routing: _self.get_routing(),
payload:
{
let payload = self.payload();
let mut vec = Vec::with_capacity(payload.len());
vec.extend_from_slice(payload);
vec
},}
}
}
impl <'p> ::std::fmt::Debug for GrePacket<'p> {
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
let _self = self;
write!(fmt ,
"GrePacket {{ checksum_present : {:?}, routing_present : {:?}, key_present : {:?}, sequence_present : {:?}, strict_source_route : {:?}, recursion_control : {:?}, zero_flags : {:?}, version : {:?}, protocol_type : {:?}, checksum : {:?}, offset : {:?}, key : {:?}, sequence : {:?}, routing : {:?}, }}"
, _self . get_checksum_present ( ) , _self .
get_routing_present ( ) , _self . get_key_present ( ) , _self
. get_sequence_present ( ) , _self . get_strict_source_route (
) , _self . get_recursion_control ( ) , _self .
get_zero_flags ( ) , _self . get_version ( ) , _self .
get_protocol_type ( ) , _self . get_checksum ( ) , _self .
get_offset ( ) , _self . get_key ( ) , _self . get_sequence (
) , _self . get_routing ( ))
}
}
impl <'p> ::std::fmt::Debug for MutableGrePacket<'p> {
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
let _self = self;
write!(fmt ,
"MutableGrePacket {{ checksum_present : {:?}, routing_present : {:?}, key_present : {:?}, sequence_present : {:?}, strict_source_route : {:?}, recursion_control : {:?}, zero_flags : {:?}, version : {:?}, protocol_type : {:?}, checksum : {:?}, offset : {:?}, key : {:?}, sequence : {:?}, routing : {:?}, }}"
, _self . get_checksum_present ( ) , _self .
get_routing_present ( ) , _self . get_key_present ( ) , _self
. get_sequence_present ( ) , _self . get_strict_source_route (
) , _self . get_recursion_control ( ) , _self .
get_zero_flags ( ) , _self . get_version ( ) , _self .
get_protocol_type ( ) , _self . get_checksum ( ) , _self .
get_offset ( ) , _self . get_key ( ) , _self . get_sequence (
) , _self . get_routing ( ))
}
}
#[derive(Clone, Debug)]
#[allow(unused_attributes)]
pub struct Gre {
pub checksum_present: u1,
pub routing_present: u1,
pub key_present: u1,
pub sequence_present: u1,
pub strict_source_route: u1,
pub recursion_control: u3,
pub zero_flags: u5,
pub version: u3,
pub protocol_type: u16be,
pub checksum: Vec<U16BE>,
pub offset: Vec<U16BE>,
pub key: Vec<U32BE>,
pub sequence: Vec<U32BE>,
pub routing: Vec<u8>,
pub payload: Vec<u8>,
}
fn gre_checksum_length(gre: &GrePacket) -> usize {
((gre.get_checksum_present() | gre.get_routing_present()) as usize) * 2
}
fn gre_offset_length(gre: &GrePacket) -> usize {
((gre.get_checksum_present() | gre.get_routing_present()) as usize) * 2
}
fn gre_key_length(gre: &GrePacket) -> usize {
(gre.get_key_present() as usize) * 4
}
fn gre_sequence_length(gre: &GrePacket) -> usize {
(gre.get_sequence_present() as usize) * 4
}
fn gre_routing_length(gre: &GrePacket) -> usize {
if 0 == gre.get_routing_present() {
0
} else { panic!("Source routed GRE packets not supported") }
}
#[derive(PartialEq)]
pub struct U16BEPacket<'p> {
packet: ::pnet_macros_support::packet::PacketData<'p>,
}
#[derive(PartialEq)]
pub struct MutableU16BEPacket<'p> {
packet: ::pnet_macros_support::packet::MutPacketData<'p>,
}
impl <'a> U16BEPacket<'a> {
#[inline]
pub fn new<'p>(packet: &'p [u8]) -> Option<U16BEPacket<'p>> {
if packet.len() >= U16BEPacket::minimum_packet_size() {
use ::pnet_macros_support::packet::PacketData;
Some(U16BEPacket{packet: PacketData::Borrowed(packet),})
} else { None }
}
pub fn owned(packet: Vec<u8>) -> Option<U16BEPacket<'static>> {
if packet.len() >= U16BEPacket::minimum_packet_size() {
use ::pnet_macros_support::packet::PacketData;
Some(U16BEPacket{packet: PacketData::Owned(packet),})
} else { None }
}
#[inline]
pub fn to_immutable<'p>(&'p self) -> U16BEPacket<'p> {
use ::pnet_macros_support::packet::PacketData;
U16BEPacket{packet: PacketData::Borrowed(self.packet.as_slice()),}
}
#[inline]
pub fn consume_to_immutable(self) -> U16BEPacket<'a> {
U16BEPacket{packet: self.packet.to_immutable(),}
}
#[inline]
pub const fn minimum_packet_size() -> usize { 2 }
#[inline]
pub fn packet_size(_packet: &U16BE) -> usize { 2 + _packet.unused.len() }
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_number(&self) -> u16be {
let _self = self;
let co = 0;
let b0 = ((_self.packet[co + 0] as u16be) << 8) as u16be;
let b1 = ((_self.packet[co + 1] as u16be)) as u16be;
b0 | b1
}
}
impl <'a> MutableU16BEPacket<'a> {
#[inline]
pub fn new<'p>(packet: &'p mut [u8]) -> Option<MutableU16BEPacket<'p>> {
if packet.len() >= MutableU16BEPacket::minimum_packet_size() {
use ::pnet_macros_support::packet::MutPacketData;
Some(MutableU16BEPacket{packet: MutPacketData::Borrowed(packet),})
} else { None }
}
pub fn owned(packet: Vec<u8>) -> Option<MutableU16BEPacket<'static>> {
if packet.len() >= MutableU16BEPacket::minimum_packet_size() {
use ::pnet_macros_support::packet::MutPacketData;
Some(MutableU16BEPacket{packet: MutPacketData::Owned(packet),})
} else { None }
}
#[inline]
pub fn to_immutable<'p>(&'p self) -> U16BEPacket<'p> {
use ::pnet_macros_support::packet::PacketData;
U16BEPacket{packet: PacketData::Borrowed(self.packet.as_slice()),}
}
#[inline]
pub fn consume_to_immutable(self) -> U16BEPacket<'a> {
U16BEPacket{packet: self.packet.to_immutable(),}
}
#[inline]
pub const fn minimum_packet_size() -> usize { 2 }
#[inline]
pub fn packet_size(_packet: &U16BE) -> usize { 2 + _packet.unused.len() }
#[inline]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn populate(&mut self, packet: &U16BE) {
let _self = self;
_self.set_number(packet.number);
_self.set_unused(&packet.unused);
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_number(&self) -> u16be {
let _self = self;
let co = 0;
let b0 = ((_self.packet[co + 0] as u16be) << 8) as u16be;
let b1 = ((_self.packet[co + 1] as u16be)) as u16be;
b0 | b1
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_number(&mut self, val: u16be) {
let _self = self;
let co = 0;
_self.packet[co + 0] = ((val & 65280) >> 8) as u8;
_self.packet[co + 1] = (val) as u8;
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_unused(&mut self, vals: &[u8]) {
use std::ptr::copy_nonoverlapping;
let mut _self = self;
let current_offset = 2;
let len = 0;
assert!(vals . len ( ) <= len);
unsafe {
copy_nonoverlapping(vals[..].as_ptr(),
_self.packet[current_offset..].as_mut_ptr(),
vals.len())
}
}
}
impl <'a> ::pnet_macros_support::packet::PacketSize for U16BEPacket<'a> {
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn packet_size(&self) -> usize { let _self = self; 2 + 0 }
}
impl <'a> ::pnet_macros_support::packet::PacketSize for MutableU16BEPacket<'a>
{
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn packet_size(&self) -> usize { let _self = self; 2 + 0 }
}
impl <'a> ::pnet_macros_support::packet::MutablePacket for
MutableU16BEPacket<'a> {
#[inline]
fn packet_mut<'p>(&'p mut self) -> &'p mut [u8] { &mut self.packet[..] }
#[inline]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn payload_mut<'p>(&'p mut self) -> &'p mut [u8] {
let _self = self;
let start = 2;
let end = ::std::cmp::min(2 + 0, _self.packet.len());
if _self.packet.len() <= start { return &mut []; }
&mut _self.packet[start..end]
}
}
impl <'a> ::pnet_macros_support::packet::Packet for MutableU16BEPacket<'a> {
#[inline]
fn packet<'p>(&'p self) -> &'p [u8] { &self.packet[..] }
#[inline]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn payload<'p>(&'p self) -> &'p [u8] {
let _self = self;
let start = 2;
let end = ::std::cmp::min(2 + 0, _self.packet.len());
if _self.packet.len() <= start { return &[]; }
&_self.packet[start..end]
}
}
impl <'a> ::pnet_macros_support::packet::Packet for U16BEPacket<'a> {
#[inline]
fn packet<'p>(&'p self) -> &'p [u8] { &self.packet[..] }
#[inline]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn payload<'p>(&'p self) -> &'p [u8] {
let _self = self;
let start = 2;
let end = ::std::cmp::min(2 + 0, _self.packet.len());
if _self.packet.len() <= start { return &[]; }
&_self.packet[start..end]
}
}
pub struct U16BEIterable<'a> {
buf: &'a [u8],
}
impl <'a> Iterator for U16BEIterable<'a> {
type
Item
=
U16BEPacket<'a>;
fn next(&mut self) -> Option<U16BEPacket<'a>> {
use pnet_macros_support::packet::PacketSize;
use std::cmp::min;
if self.buf.len() > 0 {
if let Some(ret) = U16BEPacket::new(self.buf) {
let start = min(ret.packet_size(), self.buf.len());
self.buf = &self.buf[start..];
return Some(ret);
}
}
None
}
fn size_hint(&self) -> (usize, Option<usize>) { (0, None) }
}
impl <'p> ::pnet_macros_support::packet::FromPacket for U16BEPacket<'p> {
type
T
=
U16BE;
#[inline]
fn from_packet(&self) -> U16BE {
use pnet_macros_support::packet::Packet;
let _self = self;
U16BE{number: _self.get_number(),
unused:
{
let payload = self.payload();
let mut vec = Vec::with_capacity(payload.len());
vec.extend_from_slice(payload);
vec
},}
}
}
impl <'p> ::pnet_macros_support::packet::FromPacket for MutableU16BEPacket<'p>
{
type
T
=
U16BE;
#[inline]
fn from_packet(&self) -> U16BE {
use pnet_macros_support::packet::Packet;
let _self = self;
U16BE{number: _self.get_number(),
unused:
{
let payload = self.payload();
let mut vec = Vec::with_capacity(payload.len());
vec.extend_from_slice(payload);
vec
},}
}
}
impl <'p> ::std::fmt::Debug for U16BEPacket<'p> {
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
let _self = self;
write!(fmt , "U16BEPacket {{ number : {:?}, }}" , _self . get_number
( ))
}
}
impl <'p> ::std::fmt::Debug for MutableU16BEPacket<'p> {
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
let _self = self;
write!(fmt , "MutableU16BEPacket {{ number : {:?}, }}" , _self .
get_number ( ))
}
}
#[derive(Clone, Debug)]
#[allow(unused_attributes)]
pub struct U16BE {
number: u16be,
unused: Vec<u8>,
}
#[derive(PartialEq)]
pub struct U32BEPacket<'p> {
packet: ::pnet_macros_support::packet::PacketData<'p>,
}
#[derive(PartialEq)]
pub struct MutableU32BEPacket<'p> {
packet: ::pnet_macros_support::packet::MutPacketData<'p>,
}
impl <'a> U32BEPacket<'a> {
#[inline]
pub fn new<'p>(packet: &'p [u8]) -> Option<U32BEPacket<'p>> {
if packet.len() >= U32BEPacket::minimum_packet_size() {
use ::pnet_macros_support::packet::PacketData;
Some(U32BEPacket{packet: PacketData::Borrowed(packet),})
} else { None }
}
pub fn owned(packet: Vec<u8>) -> Option<U32BEPacket<'static>> {
if packet.len() >= U32BEPacket::minimum_packet_size() {
use ::pnet_macros_support::packet::PacketData;
Some(U32BEPacket{packet: PacketData::Owned(packet),})
} else { None }
}
#[inline]
pub fn to_immutable<'p>(&'p self) -> U32BEPacket<'p> {
use ::pnet_macros_support::packet::PacketData;
U32BEPacket{packet: PacketData::Borrowed(self.packet.as_slice()),}
}
#[inline]
pub fn consume_to_immutable(self) -> U32BEPacket<'a> {
U32BEPacket{packet: self.packet.to_immutable(),}
}
#[inline]
pub const fn minimum_packet_size() -> usize { 4 }
#[inline]
pub fn packet_size(_packet: &U32BE) -> usize { 4 + _packet.unused.len() }
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_number(&self) -> u32be {
let _self = self;
let co = 0;
let b0 = ((_self.packet[co + 0] as u32be) << 24) as u32be;
let b1 = ((_self.packet[co + 1] as u32be) << 16) as u32be;
let b2 = ((_self.packet[co + 2] as u32be) << 8) as u32be;
let b3 = ((_self.packet[co + 3] as u32be)) as u32be;
b0 | b1 | b2 | b3
}
}
impl <'a> MutableU32BEPacket<'a> {
#[inline]
pub fn new<'p>(packet: &'p mut [u8]) -> Option<MutableU32BEPacket<'p>> {
if packet.len() >= MutableU32BEPacket::minimum_packet_size() {
use ::pnet_macros_support::packet::MutPacketData;
Some(MutableU32BEPacket{packet: MutPacketData::Borrowed(packet),})
} else { None }
}
pub fn owned(packet: Vec<u8>) -> Option<MutableU32BEPacket<'static>> {
if packet.len() >= MutableU32BEPacket::minimum_packet_size() {
use ::pnet_macros_support::packet::MutPacketData;
Some(MutableU32BEPacket{packet: MutPacketData::Owned(packet),})
} else { None }
}
#[inline]
pub fn to_immutable<'p>(&'p self) -> U32BEPacket<'p> {
use ::pnet_macros_support::packet::PacketData;
U32BEPacket{packet: PacketData::Borrowed(self.packet.as_slice()),}
}
#[inline]
pub fn consume_to_immutable(self) -> U32BEPacket<'a> {
U32BEPacket{packet: self.packet.to_immutable(),}
}
#[inline]
pub const fn minimum_packet_size() -> usize { 4 }
#[inline]
pub fn packet_size(_packet: &U32BE) -> usize { 4 + _packet.unused.len() }
#[inline]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn populate(&mut self, packet: &U32BE) {
let _self = self;
_self.set_number(packet.number);
_self.set_unused(&packet.unused);
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_number(&self) -> u32be {
let _self = self;
let co = 0;
let b0 = ((_self.packet[co + 0] as u32be) << 24) as u32be;
let b1 = ((_self.packet[co + 1] as u32be) << 16) as u32be;
let b2 = ((_self.packet[co + 2] as u32be) << 8) as u32be;
let b3 = ((_self.packet[co + 3] as u32be)) as u32be;
b0 | b1 | b2 | b3
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_number(&mut self, val: u32be) {
let _self = self;
let co = 0;
_self.packet[co + 0] = ((val & 4278190080) >> 24) as u8;
_self.packet[co + 1] = ((val & 16711680) >> 16) as u8;
_self.packet[co + 2] = ((val & 65280) >> 8) as u8;
_self.packet[co + 3] = (val) as u8;
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_unused(&mut self, vals: &[u8]) {
use std::ptr::copy_nonoverlapping;
let mut _self = self;
let current_offset = 4;
let len = 0;
assert!(vals . len ( ) <= len);
unsafe {
copy_nonoverlapping(vals[..].as_ptr(),
_self.packet[current_offset..].as_mut_ptr(),
vals.len())
}
}
}
impl <'a> ::pnet_macros_support::packet::PacketSize for U32BEPacket<'a> {
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn packet_size(&self) -> usize { let _self = self; 4 + 0 }
}
impl <'a> ::pnet_macros_support::packet::PacketSize for MutableU32BEPacket<'a>
{
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn packet_size(&self) -> usize { let _self = self; 4 + 0 }
}
impl <'a> ::pnet_macros_support::packet::MutablePacket for
MutableU32BEPacket<'a> {
#[inline]
fn packet_mut<'p>(&'p mut self) -> &'p mut [u8] { &mut self.packet[..] }
#[inline]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn payload_mut<'p>(&'p mut self) -> &'p mut [u8] {
let _self = self;
let start = 4;
let end = ::std::cmp::min(4 + 0, _self.packet.len());
if _self.packet.len() <= start { return &mut []; }
&mut _self.packet[start..end]
}
}
impl <'a> ::pnet_macros_support::packet::Packet for MutableU32BEPacket<'a> {
#[inline]
fn packet<'p>(&'p self) -> &'p [u8] { &self.packet[..] }
#[inline]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn payload<'p>(&'p self) -> &'p [u8] {
let _self = self;
let start = 4;
let end = ::std::cmp::min(4 + 0, _self.packet.len());
if _self.packet.len() <= start { return &[]; }
&_self.packet[start..end]
}
}
impl <'a> ::pnet_macros_support::packet::Packet for U32BEPacket<'a> {
#[inline]
fn packet<'p>(&'p self) -> &'p [u8] { &self.packet[..] }
#[inline]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn payload<'p>(&'p self) -> &'p [u8] {
let _self = self;
let start = 4;
let end = ::std::cmp::min(4 + 0, _self.packet.len());
if _self.packet.len() <= start { return &[]; }
&_self.packet[start..end]
}
}
pub struct U32BEIterable<'a> {
buf: &'a [u8],
}
impl <'a> Iterator for U32BEIterable<'a> {
type
Item
=
U32BEPacket<'a>;
fn next(&mut self) -> Option<U32BEPacket<'a>> {
use pnet_macros_support::packet::PacketSize;
use std::cmp::min;
if self.buf.len() > 0 {
if let Some(ret) = U32BEPacket::new(self.buf) {
let start = min(ret.packet_size(), self.buf.len());
self.buf = &self.buf[start..];
return Some(ret);
}
}
None
}
fn size_hint(&self) -> (usize, Option<usize>) { (0, None) }
}
impl <'p> ::pnet_macros_support::packet::FromPacket for U32BEPacket<'p> {
type
T
=
U32BE;
#[inline]
fn from_packet(&self) -> U32BE {
use pnet_macros_support::packet::Packet;
let _self = self;
U32BE{number: _self.get_number(),
unused:
{
let payload = self.payload();
let mut vec = Vec::with_capacity(payload.len());
vec.extend_from_slice(payload);
vec
},}
}
}
impl <'p> ::pnet_macros_support::packet::FromPacket for MutableU32BEPacket<'p>
{
type
T
=
U32BE;
#[inline]
fn from_packet(&self) -> U32BE {
use pnet_macros_support::packet::Packet;
let _self = self;
U32BE{number: _self.get_number(),
unused:
{
let payload = self.payload();
let mut vec = Vec::with_capacity(payload.len());
vec.extend_from_slice(payload);
vec
},}
}
}
impl <'p> ::std::fmt::Debug for U32BEPacket<'p> {
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
let _self = self;
write!(fmt , "U32BEPacket {{ number : {:?}, }}" , _self . get_number
( ))
}
}
impl <'p> ::std::fmt::Debug for MutableU32BEPacket<'p> {
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
let _self = self;
write!(fmt , "MutableU32BEPacket {{ number : {:?}, }}" , _self .
get_number ( ))
}
}
#[derive(Clone, Debug)]
#[allow(unused_attributes)]
pub struct U32BE {
number: u32be,
unused: Vec<u8>,
}
#[test]
fn gre_packet_test() {
let mut packet = [0u8; 4];
{
let mut gre_packet = MutableGrePacket::new(&mut packet[..]).unwrap();
gre_packet.set_protocol_type(2048);
assert_eq!(gre_packet . payload ( ) . len ( ) , 0);
}
let ref_packet = [0, 0, 8, 0];
assert_eq!(& ref_packet [ .. ] , & packet [ .. ]);
}
#[test]
fn gre_checksum_test() {
let mut packet = [0u8; 8];
{
let mut gre_packet = MutableGrePacket::new(&mut packet[..]).unwrap();
gre_packet.set_checksum_present(1);
assert_eq!(gre_packet . payload ( ) . len ( ) , 0);
assert_eq!(gre_packet . get_checksum ( ) . len ( ) , 1);
assert_eq!(gre_packet . get_offset ( ) . len ( ) , 1);
}
let ref_packet = [128, 0, 0, 0, 0, 0, 0, 0];
assert_eq!(& ref_packet [ .. ] , & packet [ .. ]);
}