use alloc::vec::Vec;
use pnet_macros::packet;
use pnet_macros_support::{
packet::PrimitiveValues,
types::{u16be, u32be},
};
#[packet]
#[derive(Eq, PartialEq)]
pub struct IpDiscovery {
#[construct_with(u16be)]
pub pkt_type: IpDiscoveryType,
pub length: u16be,
pub ssrc: u32be,
#[length = "length - FIXED_SIZE_COMPONENT"]
pub address: Vec<u8>,
pub port: u16be,
#[payload]
#[length = "0"]
pub payload: Vec<u8>,
}
#[packet]
#[derive(Eq, PartialEq)]
pub struct Keepalive {
pub ssrc: u32be,
#[payload]
#[length = "0"]
pub payload: Vec<u8>,
}
const FIXED_SIZE_COMPONENT: usize = core::mem::size_of::<u16>() + core::mem::size_of::<u32>();
const IP_DISCOVERY_LEN: usize = IpDiscoveryPacket::minimum_packet_size() + 64;
impl IpDiscoveryPacket<'_> {
#[must_use]
pub const fn const_packet_size() -> usize {
IP_DISCOVERY_LEN
}
}
impl MutableIpDiscoveryPacket<'_> {
#[must_use]
pub const fn const_packet_size() -> usize {
IP_DISCOVERY_LEN
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum IpDiscoveryType {
Request,
Response,
Other(u16),
}
impl IpDiscoveryType {
fn new(val: u16) -> Self {
match val {
1 => Self::Request,
2 => Self::Response,
_ => Self::Other(val),
}
}
}
impl PrimitiveValues for IpDiscoveryType {
type T = (u16,);
fn to_primitive_values(&self) -> Self::T {
match self {
Self::Request => (1,),
Self::Response => (2,),
Self::Other(n) => (*n,),
}
}
}