1use crate::error::{Error, Result};
2use rand::{Rng, rng};
3use std::net::{SocketAddr, ToSocketAddrs};
4
5fn match_range(lower: u8, upper: u8) -> impl Fn(&[u8]) -> bool {
7 move |buf: &[u8]| -> bool {
8 if buf.is_empty() {
9 return false;
10 }
11 let b = buf[0];
12 b >= lower && b <= upper
13 }
14}
15
16pub fn match_dtls(b: &[u8]) -> bool {
32 match_range(20, 63)(b)
33}
34
35pub fn match_srtp_or_srtcp(b: &[u8]) -> bool {
38 match_range(128, 191)(b)
39}
40
41pub fn is_rtcp(buf: &[u8]) -> bool {
42 if buf.len() < 4 {
44 return false;
45 }
46
47 let rtcp_packet_type = buf[1];
48 (192..=223).contains(&rtcp_packet_type)
49}
50
51pub fn match_srtp(buf: &[u8]) -> bool {
53 match_srtp_or_srtcp(buf) && !is_rtcp(buf)
54}
55
56pub fn match_srtcp(buf: &[u8]) -> bool {
58 match_srtp_or_srtcp(buf) && is_rtcp(buf)
59}
60
61pub fn lookup_host<T>(use_ipv4: bool, host: T) -> Result<SocketAddr>
63where
64 T: ToSocketAddrs,
65{
66 for remote_addr in host.to_socket_addrs()? {
67 if (use_ipv4 && remote_addr.is_ipv4()) || (!use_ipv4 && remote_addr.is_ipv6()) {
68 return Ok(remote_addr);
69 }
70 }
71
72 Err(Error::ErrAddressParseFailed)
73}
74
75const RUNES_ALPHA: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
76const RUNES_ALPHA_NUMBER: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
77
78pub fn math_rand_alpha(n: usize) -> String {
80 generate_crypto_random_string(n, RUNES_ALPHA)
81}
82
83pub fn math_rand_alpha_number(n: usize) -> String {
85 generate_crypto_random_string(n, RUNES_ALPHA_NUMBER)
86}
87
88pub fn generate_crypto_random_string(n: usize, runes: &[u8]) -> String {
90 let mut rng = rng();
91
92 let rand_string: String = (0..n)
93 .map(|_| {
94 let idx = rng.random_range(0..runes.len());
95 runes[idx] as char
96 })
97 .collect();
98
99 rand_string
100}