use pnet_macros_support::types::*;
use std::mem;
const FIXED_SIZE_COMPONENT: usize =
std::mem::size_of::<u16>() + std::mem::size_of::<u32>();
#[derive(PartialEq)]
pub struct IpDiscoveryPacket<'p> {
packet: ::pnet_macros_support::packet::PacketData<'p>,
}
#[derive(PartialEq)]
pub struct MutableIpDiscoveryPacket<'p> {
packet: ::pnet_macros_support::packet::MutPacketData<'p>,
}
impl <'a> IpDiscoveryPacket<'a> {
#[inline]
pub fn new<'p>(packet: &'p [u8]) -> Option<IpDiscoveryPacket<'p>> {
if packet.len() >= IpDiscoveryPacket::minimum_packet_size() {
use ::pnet_macros_support::packet::PacketData;
Some(IpDiscoveryPacket{packet: PacketData::Borrowed(packet),})
} else { None }
}
pub fn owned(packet: Vec<u8>) -> Option<IpDiscoveryPacket<'static>> {
if packet.len() >= IpDiscoveryPacket::minimum_packet_size() {
use ::pnet_macros_support::packet::PacketData;
Some(IpDiscoveryPacket{packet: PacketData::Owned(packet),})
} else { None }
}
#[inline]
pub fn to_immutable<'p>(&'p self) -> IpDiscoveryPacket<'p> {
use ::pnet_macros_support::packet::PacketData;
IpDiscoveryPacket{packet:
PacketData::Borrowed(self.packet.as_slice()),}
}
#[inline]
pub fn consume_to_immutable(self) -> IpDiscoveryPacket<'a> {
IpDiscoveryPacket{packet: self.packet.to_immutable(),}
}
#[inline]
pub const fn minimum_packet_size() -> usize { 10 }
#[inline]
pub fn packet_size(_packet: &IpDiscovery) -> usize {
10 + _packet.address.len() + _packet.payload.len()
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_pkt_type(&self) -> IpDiscoveryType {
#[inline(always)]
#[allow(trivial_numeric_casts, unused_parens)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn get_arg0(_self: &IpDiscoveryPacket) -> u16be {
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
}
IpDiscoveryType::new(get_arg0(&self))
}
#[inline]
#[allow(trivial_numeric_casts, unused_parens)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_length(&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, unused_parens)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_ssrc(&self) -> u32be {
let _self = self;
let co = 4;
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 get_address_raw(&self) -> &[u8] {
use std::cmp::min;
let _self = self;
let current_offset = 8;
let end =
min(current_offset + (_self.get_length() as usize) -
(FIXED_SIZE_COMPONENT as usize), _self.packet.len());
&_self.packet[current_offset..end]
}
#[inline]
#[allow(trivial_numeric_casts, unused_parens)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_address(&self) -> Vec<u8> {
use std::cmp::min;
let _self = self;
let current_offset = 8;
let pkt_len = self.packet.len();
let end =
min(current_offset + (_self.get_length() as usize) -
(FIXED_SIZE_COMPONENT as usize), 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, unused_parens)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_port(&self) -> u16be {
let _self = self;
let co =
8 + (_self.get_length() as usize) -
(FIXED_SIZE_COMPONENT as usize);
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> MutableIpDiscoveryPacket<'a> {
#[inline]
pub fn new<'p>(packet: &'p mut [u8])
-> Option<MutableIpDiscoveryPacket<'p>> {
if packet.len() >= MutableIpDiscoveryPacket::minimum_packet_size() {
use ::pnet_macros_support::packet::MutPacketData;
Some(MutableIpDiscoveryPacket{packet:
MutPacketData::Borrowed(packet),})
} else { None }
}
pub fn owned(packet: Vec<u8>)
-> Option<MutableIpDiscoveryPacket<'static>> {
if packet.len() >= MutableIpDiscoveryPacket::minimum_packet_size() {
use ::pnet_macros_support::packet::MutPacketData;
Some(MutableIpDiscoveryPacket{packet:
MutPacketData::Owned(packet),})
} else { None }
}
#[inline]
pub fn to_immutable<'p>(&'p self) -> IpDiscoveryPacket<'p> {
use ::pnet_macros_support::packet::PacketData;
IpDiscoveryPacket{packet:
PacketData::Borrowed(self.packet.as_slice()),}
}
#[inline]
pub fn consume_to_immutable(self) -> IpDiscoveryPacket<'a> {
IpDiscoveryPacket{packet: self.packet.to_immutable(),}
}
#[inline]
pub const fn minimum_packet_size() -> usize { 10 }
#[inline]
pub fn packet_size(_packet: &IpDiscovery) -> usize {
10 + _packet.address.len() + _packet.payload.len()
}
#[inline]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn populate(&mut self, packet: &IpDiscovery) {
let _self = self;
_self.set_pkt_type(packet.pkt_type);
_self.set_length(packet.length);
_self.set_ssrc(packet.ssrc);
_self.set_address(&packet.address);
_self.set_port(packet.port);
_self.set_payload(&packet.payload);
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_pkt_type(&self) -> IpDiscoveryType {
#[inline(always)]
#[allow(trivial_numeric_casts, unused_parens)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn get_arg0(_self: &MutableIpDiscoveryPacket) -> u16be {
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
}
IpDiscoveryType::new(get_arg0(&self))
}
#[inline]
#[allow(trivial_numeric_casts, unused_parens)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_length(&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, unused_parens)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_ssrc(&self) -> u32be {
let _self = self;
let co = 4;
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 get_address_raw(&self) -> &[u8] {
use std::cmp::min;
let _self = self;
let current_offset = 8;
let end =
min(current_offset + (_self.get_length() as usize) -
(FIXED_SIZE_COMPONENT as usize), _self.packet.len());
&_self.packet[current_offset..end]
}
#[inline]
#[allow(trivial_numeric_casts, unused_parens)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_address(&self) -> Vec<u8> {
use std::cmp::min;
let _self = self;
let current_offset = 8;
let pkt_len = self.packet.len();
let end =
min(current_offset + (_self.get_length() as usize) -
(FIXED_SIZE_COMPONENT as usize), 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, unused_parens)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn get_port(&self) -> u16be {
let _self = self;
let co =
8 + (_self.get_length() as usize) -
(FIXED_SIZE_COMPONENT as usize);
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_pkt_type(&mut self, val: IpDiscoveryType) {
use pnet_macros_support::packet::PrimitiveValues;
let _self = self;
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn set_arg0(_self: &mut MutableIpDiscoveryPacket, val: u16be) {
let co = 0;
_self.packet[co + 0] = ((val & 65280) >> 8) as u8;
_self.packet[co + 1] = (val) as u8;
}
let vals = val.to_primitive_values();
set_arg0(_self, vals.0);
}
#[inline]
#[allow(trivial_numeric_casts)]
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
pub fn set_length(&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 set_ssrc(&mut self, val: u32be) {
let _self = self;
let co = 4;
_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 get_address_raw_mut(&mut self) -> &mut [u8] {
use std::cmp::min;
let _self = self;
let current_offset = 8;
let end =
min(current_offset + (_self.get_length() as usize) -
(FIXED_SIZE_COMPONENT as usize), _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_address(&mut self, vals: &[u8]) {
use std::ptr::copy_nonoverlapping;
let mut _self = self;
let current_offset = 8;
let len =
(_self.get_length() as usize) - (FIXED_SIZE_COMPONENT as usize);
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_port(&mut self, val: u16be) {
let _self = self;
let co =
8 + (_self.get_length() as usize) -
(FIXED_SIZE_COMPONENT as usize);
_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_payload(&mut self, vals: &[u8]) {
use std::ptr::copy_nonoverlapping;
let mut _self = self;
let current_offset =
10 + (_self.get_length() as usize) -
(FIXED_SIZE_COMPONENT as usize);
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 IpDiscoveryPacket<'a>
{
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn packet_size(&self) -> usize {
let _self = self;
10 + (_self.get_length() as usize) - (FIXED_SIZE_COMPONENT as usize) +
0
}
}
impl <'a> ::pnet_macros_support::packet::PacketSize for
MutableIpDiscoveryPacket<'a> {
#[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
fn packet_size(&self) -> usize {
let _self = self;
10 + (_self.get_length() as usize) - (FIXED_SIZE_COMPONENT as usize) +
0
}
}
impl <'a> ::pnet_macros_support::packet::MutablePacket for
MutableIpDiscoveryPacket<'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 =
10 + (_self.get_length() as usize) -
(FIXED_SIZE_COMPONENT as usize);
let end =
::std::cmp::min(10 + (_self.get_length() as usize) -
(FIXED_SIZE_COMPONENT as usize) + 0,
_self.packet.len());
if _self.packet.len() <= start { return &mut []; }
&mut _self.packet[start..end]
}
}
impl <'a> ::pnet_macros_support::packet::Packet for
MutableIpDiscoveryPacket<'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 =
10 + (_self.get_length() as usize) -
(FIXED_SIZE_COMPONENT as usize);
let end =
::std::cmp::min(10 + (_self.get_length() as usize) -
(FIXED_SIZE_COMPONENT as usize) + 0,
_self.packet.len());
if _self.packet.len() <= start { return &[]; }
&_self.packet[start..end]
}
}
impl <'a> ::pnet_macros_support::packet::Packet for IpDiscoveryPacket<'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 =
10 + (_self.get_length() as usize) -
(FIXED_SIZE_COMPONENT as usize);
let end =
::std::cmp::min(10 + (_self.get_length() as usize) -
(FIXED_SIZE_COMPONENT as usize) + 0,
_self.packet.len());
if _self.packet.len() <= start { return &[]; }
&_self.packet[start..end]
}
}
pub struct IpDiscoveryIterable<'a> {
buf: &'a [u8],
}
impl <'a> Iterator for IpDiscoveryIterable<'a> {
type
Item
=
IpDiscoveryPacket<'a>;
fn next(&mut self) -> Option<IpDiscoveryPacket<'a>> {
use pnet_macros_support::packet::PacketSize;
use std::cmp::min;
if self.buf.len() > 0 {
if let Some(ret) = IpDiscoveryPacket::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 IpDiscoveryPacket<'p>
{
type
T
=
IpDiscovery;
#[inline]
fn from_packet(&self) -> IpDiscovery {
use pnet_macros_support::packet::Packet;
let _self = self;
IpDiscovery{pkt_type: _self.get_pkt_type(),
length: _self.get_length(),
ssrc: _self.get_ssrc(),
address: _self.get_address(),
port: _self.get_port(),
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
MutableIpDiscoveryPacket<'p> {
type
T
=
IpDiscovery;
#[inline]
fn from_packet(&self) -> IpDiscovery {
use pnet_macros_support::packet::Packet;
let _self = self;
IpDiscovery{pkt_type: _self.get_pkt_type(),
length: _self.get_length(),
ssrc: _self.get_ssrc(),
address: _self.get_address(),
port: _self.get_port(),
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 IpDiscoveryPacket<'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 ,
"IpDiscoveryPacket {{ pkt_type : {:?}, length : {:?}, ssrc : {:?}, address : {:?}, port : {:?}, }}"
, _self . get_pkt_type ( ) , _self . get_length ( ) , _self .
get_ssrc ( ) , _self . get_address ( ) , _self . get_port (
))
}
}
impl <'p> ::std::fmt::Debug for MutableIpDiscoveryPacket<'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 ,
"MutableIpDiscoveryPacket {{ pkt_type : {:?}, length : {:?}, ssrc : {:?}, address : {:?}, port : {:?}, }}"
, _self . get_pkt_type ( ) , _self . get_length ( ) , _self .
get_ssrc ( ) , _self . get_address ( ) , _self . get_port (
))
}
}
#[derive(Clone, Debug)]
#[allow(unused_attributes)]
pub struct IpDiscovery {
pub pkt_type: IpDiscoveryType,
pub length: u16be,
pub ssrc: u32be,
pub address: Vec<u8>,
pub port: u16be,
pub payload: Vec<u8>,
}