Skip to main content

rtci_rtp/header/
validity.rs

1//! The RTP header validity checks of [RFC 3550] Appendix A.1.
2//!
3//! `check_rtp_validity` applies them to the bytes of a datagram before anything is parsed or
4//! allocated, so a receiver can tell an RTP packet from garbage (the appendix's "weak validity
5//! checks"). The checks that need more than one packet (a new source's probation, sequence
6//! number jumps) are [`SourceSequence`](crate::sequence::SourceSequence).
7//!
8//! [RFC 3550]: https://datatracker.ietf.org/doc/html/rfc3550#appendix-A.1
9
10use shared::error::{Error, Result};
11
12use super::{
13    CC_MASK, CSRC_LENGTH, CSRC_OFFSET, EXTENSION_MASK, EXTENSION_SHIFT, HEADER_LENGTH,
14    PADDING_MASK, PADDING_SHIFT, PT_MASK, RTP_VERSION, VERSION_MASK, VERSION_SHIFT,
15};
16
17/// The second octet of an RTCP sender report: an RTP packet with M=1 and PT 72.
18const RTCP_SR: u8 = 200;
19/// The second octet of an RTCP receiver report: an RTP packet with M=1 and PT 73.
20const RTCP_RR: u8 = 201;
21
22/// Where the parts of a valid RTP packet lie.
23#[derive(Debug, Copy, Clone, PartialEq, Eq)]
24pub struct RtpLayout {
25    /// The payload type.
26    pub payload_type: u8,
27    /// Bytes of header: the fixed header, the CSRC list and the extension block.
28    pub header_len: usize,
29    /// Bytes of payload, padding excluded.
30    pub payload_len: usize,
31    /// Bytes of padding after the payload, the count octet included; 0 without the P bit.
32    pub padding_len: usize,
33}
34
35/// Applies the header validity checks of RFC 3550 Appendix A.1 to a datagram.
36///
37/// * the version is 2;
38/// * the payload type is known (`payload_type_known`), and the second octet is not that of an
39///   RTCP SR or RR;
40/// * the packet is long enough for its CSRC count;
41/// * with the X bit, the extension block fits the packet;
42/// * with the P bit, the last octet is a padding count that is not zero and not larger than
43///   what follows the header.
44///
45/// `payload_type_known` is the profile's part: the appendix has a receiver that does not know
46/// a payload type ignore the packet (RFC 3550 section 5.1). Pass `|_| true` to check the
47/// structure alone.
48///
49/// # Errors
50///
51/// [`Error::ErrHeaderSizeInsufficient`], [`Error::BadVersion`], [`Error::WrongPayloadType`],
52/// [`Error::ErrHeaderSizeInsufficientForExtension`] or [`Error::ErrShortPacket`] (padding),
53/// in the order of the checks above.
54pub fn check_rtp_validity(
55    packet: &[u8],
56    payload_type_known: impl FnOnce(u8) -> bool,
57) -> Result<RtpLayout> {
58    if packet.len() < CSRC_OFFSET {
59        return Err(Error::ErrHeaderSizeInsufficient);
60    }
61    let b0 = packet[0];
62    if b0 >> VERSION_SHIFT & VERSION_MASK != RTP_VERSION {
63        return Err(Error::BadVersion);
64    }
65    let b1 = packet[1];
66    let payload_type = b1 & PT_MASK;
67    if b1 == RTCP_SR || b1 == RTCP_RR || !payload_type_known(payload_type) {
68        return Err(Error::WrongPayloadType);
69    }
70
71    let mut header_len = CSRC_OFFSET + usize::from(b0 & CC_MASK) * CSRC_LENGTH;
72    if packet.len() < header_len {
73        return Err(Error::ErrHeaderSizeInsufficient);
74    }
75    if b0 >> EXTENSION_SHIFT & EXTENSION_MASK != 0 {
76        if packet.len() < header_len + HEADER_LENGTH {
77            return Err(Error::ErrHeaderSizeInsufficientForExtension);
78        }
79        let words = u16::from_be_bytes([packet[header_len + 2], packet[header_len + 3]]);
80        header_len += HEADER_LENGTH + usize::from(words) * 4;
81        if packet.len() < header_len {
82            return Err(Error::ErrHeaderSizeInsufficientForExtension);
83        }
84    }
85
86    let after_header = packet.len() - header_len;
87    let padding_len = if b0 >> PADDING_SHIFT & PADDING_MASK != 0 {
88        let count = packet.last().map_or(0, |&count| usize::from(count));
89        if count == 0 || count > after_header {
90            return Err(Error::ErrShortPacket);
91        }
92        count
93    } else {
94        0
95    };
96
97    Ok(RtpLayout {
98        payload_type,
99        header_len,
100        payload_len: after_header - padding_len,
101        padding_len,
102    })
103}
104
105#[cfg(test)]
106#[path = "validity_test.rs"]
107mod validity_test;