#[cfg(test)]
mod extended_report_test;
#[cfg(test)]
mod report_blocks_test;
pub mod dlrr;
pub mod ecn;
pub mod prt;
pub mod rle;
pub mod rrt;
pub mod ssr;
pub mod unknown;
pub mod vm;
pub use dlrr::{DLRRReport, DLRRReportBlock};
pub use ecn::{EcnSummaryReport, EcnSummaryReportBlock};
pub use prt::PacketReceiptTimesReportBlock;
pub use rle::{
Chunk, ChunkType, DuplicateRLEReportBlock, LossRLEReportBlock, RLEReportBlock,
reported_sequence_numbers,
};
pub use rrt::ReceiverReferenceTimeReportBlock;
pub use ssr::{StatisticsSummaryReportBlock, TTLorHopLimitType};
pub use unknown::UnknownReportBlock;
pub use vm::VoIPMetricsReportBlock;
use crate::Packet;
use crate::header::{HEADER_LENGTH, Header, PacketType, SSRC_LENGTH, read_packet};
use crate::util::get_padding_size;
use bytes::{Buf, BufMut, Bytes};
use shared::{
error::{Error, Result},
marshal::{Marshal, MarshalSize, Unmarshal},
};
use std::any::Any;
use std::fmt;
const XR_HEADER_LENGTH: usize = 4;
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum BlockType {
#[default]
Unknown,
LossRLE, DuplicateRLE, PacketReceiptTimes, ReceiverReferenceTime, DLRR, StatisticsSummary, VoIPMetrics, EcnSummary, Other(u8),
}
impl From<u8> for BlockType {
fn from(v: u8) -> Self {
match v {
0 => BlockType::Unknown,
1 => BlockType::LossRLE,
2 => BlockType::DuplicateRLE,
3 => BlockType::PacketReceiptTimes,
4 => BlockType::ReceiverReferenceTime,
5 => BlockType::DLRR,
6 => BlockType::StatisticsSummary,
7 => BlockType::VoIPMetrics,
13 => BlockType::EcnSummary,
other => BlockType::Other(other),
}
}
}
impl From<BlockType> for u8 {
fn from(t: BlockType) -> Self {
match t {
BlockType::Unknown => 0,
BlockType::LossRLE => 1,
BlockType::DuplicateRLE => 2,
BlockType::PacketReceiptTimes => 3,
BlockType::ReceiverReferenceTime => 4,
BlockType::DLRR => 5,
BlockType::StatisticsSummary => 6,
BlockType::VoIPMetrics => 7,
BlockType::EcnSummary => 13,
BlockType::Other(other) => other,
}
}
}
impl fmt::Display for BlockType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match *self {
BlockType::LossRLE => "LossRLEReportBlockType",
BlockType::DuplicateRLE => "DuplicateRLEReportBlockType",
BlockType::PacketReceiptTimes => "PacketReceiptTimesReportBlockType",
BlockType::ReceiverReferenceTime => "ReceiverReferenceTimeReportBlockType",
BlockType::DLRR => "DLRRReportBlockType",
BlockType::StatisticsSummary => "StatisticsSummaryReportBlockType",
BlockType::VoIPMetrics => "VoIPMetricsReportBlockType",
BlockType::EcnSummary => "EcnSummaryReportBlockType",
_ => "UnknownReportBlockType",
};
write!(f, "{s}")
}
}
pub type TypeSpecificField = u8;
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct XRHeader {
pub block_type: BlockType,
pub type_specific: TypeSpecificField,
pub block_length: u16,
}
impl MarshalSize for XRHeader {
fn marshal_size(&self) -> usize {
XR_HEADER_LENGTH
}
}
impl Marshal for XRHeader {
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
if buf.remaining_mut() < XR_HEADER_LENGTH {
return Err(Error::BufferTooShort);
}
buf.put_u8(self.block_type.into());
buf.put_u8(self.type_specific);
buf.put_u16(self.block_length);
Ok(XR_HEADER_LENGTH)
}
}
impl Unmarshal for XRHeader {
fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
where
Self: Sized,
B: Buf,
{
if raw_packet.remaining() < XR_HEADER_LENGTH {
return Err(Error::PacketTooShort);
}
let block_type: BlockType = raw_packet.get_u8().into();
let type_specific = raw_packet.get_u8();
let block_length = raw_packet.get_u16();
Ok(XRHeader {
block_type,
type_specific,
block_length,
})
}
}
#[derive(Debug, PartialEq, Default, Clone)]
pub struct ExtendedReport {
pub sender_ssrc: u32,
pub reports: Vec<Box<dyn Packet>>,
}
impl fmt::Display for ExtendedReport {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self:?}")
}
}
impl Packet for ExtendedReport {
fn header(&self) -> Header {
Header {
padding: false,
count: 0,
packet_type: PacketType::ExtendedReport,
length: ((self.marshal_size() / 4) - 1) as u16,
}
}
fn destination_ssrc(&self) -> Vec<u32> {
let mut ssrc = vec![];
for p in &self.reports {
ssrc.extend(p.destination_ssrc());
}
ssrc
}
fn raw_size(&self) -> usize {
let mut reps_length = 0;
for rep in &self.reports {
reps_length += rep.marshal_size();
}
HEADER_LENGTH + SSRC_LENGTH + reps_length
}
fn as_any(&self) -> &dyn Any {
self
}
fn equal(&self, other: &dyn Packet) -> bool {
other.as_any().downcast_ref::<ExtendedReport>() == Some(self)
}
fn cloned(&self) -> Box<dyn Packet> {
Box::new(self.clone())
}
}
impl MarshalSize for ExtendedReport {
fn marshal_size(&self) -> usize {
let l = self.raw_size();
l + get_padding_size(l)
}
}
impl Marshal for ExtendedReport {
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
if buf.remaining_mut() < self.marshal_size() {
return Err(Error::BufferTooShort);
}
let h = self.header();
let n = h.marshal_to(buf)?;
buf = &mut buf[n..];
buf.put_u32(self.sender_ssrc);
for report in &self.reports {
let n = report.marshal_to(buf)?;
buf = &mut buf[n..];
}
Ok(self.marshal_size())
}
}
impl ExtendedReport {
fn unmarshal_block(block_type: BlockType, block: Bytes) -> Result<Box<dyn Packet>> {
let raw_block = &mut block.clone();
let parsed: Result<Box<dyn Packet>> = match block_type {
BlockType::LossRLE => {
LossRLEReportBlock::unmarshal(raw_block).map(|b| Box::new(b) as Box<dyn Packet>)
}
BlockType::DuplicateRLE => DuplicateRLEReportBlock::unmarshal(raw_block)
.map(|b| Box::new(b) as Box<dyn Packet>),
BlockType::PacketReceiptTimes => PacketReceiptTimesReportBlock::unmarshal(raw_block)
.map(|b| Box::new(b) as Box<dyn Packet>),
BlockType::ReceiverReferenceTime => {
ReceiverReferenceTimeReportBlock::unmarshal(raw_block)
.map(|b| Box::new(b) as Box<dyn Packet>)
}
BlockType::DLRR => {
DLRRReportBlock::unmarshal(raw_block).map(|b| Box::new(b) as Box<dyn Packet>)
}
BlockType::StatisticsSummary => StatisticsSummaryReportBlock::unmarshal(raw_block)
.map(|b| Box::new(b) as Box<dyn Packet>),
BlockType::VoIPMetrics => {
VoIPMetricsReportBlock::unmarshal(raw_block).map(|b| Box::new(b) as Box<dyn Packet>)
}
BlockType::EcnSummary => {
EcnSummaryReportBlock::unmarshal(raw_block).map(|b| Box::new(b) as Box<dyn Packet>)
}
_ => Err(Error::WrongType),
};
match parsed {
Ok(report) => Ok(report),
Err(_) => Ok(Box::new(UnknownReportBlock::unmarshal(&mut block.clone())?)),
}
}
}
impl Unmarshal for ExtendedReport {
fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
where
Self: Sized,
B: Buf,
{
let (header, mut body) = read_packet(raw_packet)?;
if header.packet_type != PacketType::ExtendedReport {
return Err(Error::WrongType);
}
if body.len() < SSRC_LENGTH {
return Err(Error::PacketTooShort);
}
let sender_ssrc = body.get_u32();
let mut reports = vec![];
while body.has_remaining() {
if body.len() < XR_HEADER_LENGTH {
return Err(Error::PacketTooShort);
}
let block_length = u16::from_be_bytes([body[2], body[3]]) as usize * 4;
if body.len() < XR_HEADER_LENGTH + block_length {
return Err(Error::PacketTooShort);
}
let block_type: BlockType = body[0].into();
let block = body.split_to(XR_HEADER_LENGTH + block_length);
reports.push(Self::unmarshal_block(block_type, block)?);
}
Ok(ExtendedReport {
sender_ssrc,
reports,
})
}
}