Skip to main content

o_sfu_rfc/
dtls.rs

1//! Legacy DTLS record framing from [RFC 6347 section 4.1](https://www.rfc-editor.org/rfc/rfc6347.html#section-4.1).
2
3/// Record header containing type, version, epoch, sequence number and length.
4const RECORD_HEADER_BYTES: usize = 13;
5/// Version follows the one-byte content type.
6const VERSION_OFFSET: usize = 1;
7/// Length follows type, version, epoch and the 48-bit sequence number.
8const LENGTH_OFFSET: usize = 11;
9/// DTLS 1.2 wire version, distinct from TLS 1.2's version bytes.
10const VERSION_1_2: [u8; 2] = [0xfe, 0xfd];
11
12/// DTLS 1.0 record-layer wire version.
13/// Reference: <https://www.rfc-editor.org/rfc/rfc4347.html#section-4.1>
14const VERSION_1_0: [u8; 2] = [0xfe, 0xff];
15
16/// Checks the first record after the WebRTC multiplexer has selected DTLS.
17/// Later records, record contents and cryptographic validity remain unchecked.
18pub(crate) fn has_datagram_shape(packet: &[u8]) -> bool {
19    let Some(header) = packet.first_chunk::<RECORD_HEADER_BYTES>() else {
20        return false;
21    };
22    let version = header.get(VERSION_OFFSET..VERSION_OFFSET + VERSION_1_0.len());
23    let length = u16::from_be_bytes([header[LENGTH_OFFSET], header[LENGTH_OFFSET + 1]]);
24    (version == Some(VERSION_1_0.as_slice()) || version == Some(VERSION_1_2.as_slice()))
25        && usize::from(length) + RECORD_HEADER_BYTES <= packet.len()
26}