use crate::error::{Error, Result};
use rand::{RngExt, rng};
use std::net::{SocketAddr, ToSocketAddrs};
fn match_range(lower: u8, upper: u8) -> impl Fn(&[u8]) -> bool {
move |buf: &[u8]| -> bool {
if buf.is_empty() {
return false;
}
let b = buf[0];
b >= lower && b <= upper
}
}
pub fn match_dtls(b: &[u8]) -> bool {
match_range(20, 63)(b)
}
pub fn match_srtp_or_srtcp(b: &[u8]) -> bool {
match_range(128, 191)(b)
}
pub fn is_rtcp(buf: &[u8]) -> bool {
if buf.len() < 4 {
return false;
}
let rtcp_packet_type = buf[1];
(192..=223).contains(&rtcp_packet_type)
}
pub fn match_srtp(buf: &[u8]) -> bool {
match_srtp_or_srtcp(buf) && !is_rtcp(buf)
}
pub fn match_srtcp(buf: &[u8]) -> bool {
match_srtp_or_srtcp(buf) && is_rtcp(buf)
}
pub fn lookup_host<T>(use_ipv4: bool, host: T) -> Result<SocketAddr>
where
T: ToSocketAddrs,
{
for remote_addr in host.to_socket_addrs()? {
if (use_ipv4 && remote_addr.is_ipv4()) || (!use_ipv4 && remote_addr.is_ipv6()) {
return Ok(remote_addr);
}
}
Err(Error::ErrAddressParseFailed)
}
const RUNES_ALPHA: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const RUNES_ALPHA_NUMBER: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
pub fn math_rand_alpha(n: usize) -> String {
generate_crypto_random_string(n, RUNES_ALPHA)
}
pub fn math_rand_alpha_number(n: usize) -> String {
generate_crypto_random_string(n, RUNES_ALPHA_NUMBER)
}
pub fn generate_crypto_random_string(n: usize, runes: &[u8]) -> String {
let mut rng = rng();
let rand_string: String = (0..n)
.map(|_| {
let idx = rng.random_range(0..runes.len());
runes[idx] as char
})
.collect();
rand_string
}
#[cfg(test)]
mod rtp_rtcp_demux_test {
use super::*;
fn packet(first: u8, second: u8) -> [u8; 12] {
let mut p = [0u8; 12];
p[0] = first;
p[1] = second;
p
}
#[test]
fn the_second_octet_192_to_223_is_rtcp_and_nothing_else_is() {
for second in 0..=u8::MAX {
let want = (192..=223).contains(&second);
assert_eq!(
want,
is_rtcp(&packet(0x80, second)),
"second octet {second}"
);
assert_eq!(
want,
match_srtcp(&packet(0x80, second)),
"second octet {second}"
);
assert_eq!(
!want,
match_srtp(&packet(0x80, second)),
"second octet {second}"
);
}
}
#[test]
fn payload_types_64_to_95_are_rtcp_only_with_the_marker_bit() {
for payload_type in 64..=95u8 {
assert!(
!is_rtcp(&packet(0x80, payload_type)),
"PT {payload_type}, M=0"
);
assert!(
is_rtcp(&packet(0x80, 0x80 | payload_type)),
"PT {payload_type}, M=1"
);
}
for payload_type in (0..=63u8).chain(96..=127) {
assert!(
!is_rtcp(&packet(0x80, payload_type)),
"PT {payload_type}, M=0"
);
assert!(
!is_rtcp(&packet(0x80, 0x80 | payload_type)),
"PT {payload_type}, M=1"
);
}
}
#[test]
fn every_rtcp_packet_type_in_use_is_rtcp() {
for (first, packet_type) in [
(0x80, 200), (0x81, 201), (0x81, 202), (0x81, 203), (0x80, 204), (0x81, 205), (0x8f, 205), (0x81, 206), (0xaf, 206), (0x80, 207), ] {
assert!(is_rtcp(&packet(first, packet_type)), "PT {packet_type}");
}
}
#[test]
fn only_a_first_octet_of_128_to_191_is_rtp_or_rtcp() {
for first in 0..=u8::MAX {
let rtp_or_rtcp = (128..=191).contains(&first);
assert_eq!(
rtp_or_rtcp,
match_srtcp(&packet(first, 200)),
"first octet {first}"
);
assert_eq!(
rtp_or_rtcp,
match_srtp(&packet(first, 96)),
"first octet {first}"
);
assert!(!match_srtp(&packet(first, 200)));
assert!(!match_srtcp(&packet(first, 96)));
}
}
#[test]
fn a_buffer_shorter_than_an_rtcp_header_is_not_rtcp() {
assert!(!is_rtcp(&[]));
assert!(!is_rtcp(&[0x80]));
assert!(!is_rtcp(&[0x80, 200]));
assert!(!is_rtcp(&[0x80, 200, 0]));
assert!(is_rtcp(&[0x80, 200, 0, 0]));
}
}