rtc_shared/
util.rs

1use crate::error::{Error, Result};
2use rand::{Rng, rng};
3use std::net::{SocketAddr, ToSocketAddrs};
4
5// match_range is a MatchFunc that accepts packets with the first byte in [lower..upper]
6fn 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
16/// MatchFuncs as described in RFC7983
17/// <https://tools.ietf.org/html/rfc7983>
18///              +----------------+
19///              |        [0..3] -+--> forward to STUN
20///              |                |
21///              |      [16..19] -+--> forward to ZRTP
22///              |                |
23///  packet -->  |      [20..63] -+--> forward to DTLS
24///              |                |
25///              |      [64..79] -+--> forward to TURN Channel
26///              |                |
27///              |    [128..191] -+--> forward to RTP/RTCP
28///              +----------------+
29/// match_dtls is a MatchFunc that accepts packets with the first byte in [20..63]
30/// as defied in RFC7983
31pub fn match_dtls(b: &[u8]) -> bool {
32    match_range(20, 63)(b)
33}
34
35// match_srtp_or_srtcp is a MatchFunc that accepts packets with the first byte in [128..191]
36// as defied in RFC7983
37pub fn match_srtp_or_srtcp(b: &[u8]) -> bool {
38    match_range(128, 191)(b)
39}
40
41pub fn is_rtcp(buf: &[u8]) -> bool {
42    // Not long enough to determine RTP/RTCP
43    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
51/// match_srtp is a MatchFunc that only matches SRTP and not SRTCP
52pub fn match_srtp(buf: &[u8]) -> bool {
53    match_srtp_or_srtcp(buf) && !is_rtcp(buf)
54}
55
56/// match_srtcp is a MatchFunc that only matches SRTCP and not SRTP
57pub fn match_srtcp(buf: &[u8]) -> bool {
58    match_srtp_or_srtcp(buf) && is_rtcp(buf)
59}
60
61/// lookup host to SocketAddr
62pub 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
78/// math_rand_alpha generates a mathematical random alphabet sequence of the requested length.
79pub fn math_rand_alpha(n: usize) -> String {
80    generate_crypto_random_string(n, RUNES_ALPHA)
81}
82
83/// math_rand_alpha generates a mathematical random alphabet and number sequence of the requested length.
84pub fn math_rand_alpha_number(n: usize) -> String {
85    generate_crypto_random_string(n, RUNES_ALPHA_NUMBER)
86}
87
88//TODO: generates a random string for cryptographic usage.
89pub 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}