Skip to main content

rtc_srtp/
option.rs

1use shared::replay_detector::*;
2
3/// A factory for the [`ReplayDetector`] a context
4/// should use.
5///
6/// A factory rather than a value because each SSRC in a session needs its own detector
7/// state. Remote contexts default to a 64-packet sliding window; pass a different factory to
8/// widen it or to disable replay protection.
9pub type ContextOption = Box<dyn Fn() -> Box<dyn ReplayDetector> + Send + Sync>;
10
11pub(crate) const MAX_SEQUENCE_NUMBER: u16 = 65535;
12pub(crate) const MAX_SRTCP_INDEX: usize = 0x7FFFFFFF;
13
14/// srtp_replay_protection sets SRTP replay protection window size.
15pub fn srtp_replay_protection(window_size: usize) -> ContextOption {
16    Box::new(move || -> Box<dyn ReplayDetector> {
17        Box::new(WrappedSlidingWindowDetector::new(
18            window_size,
19            MAX_SEQUENCE_NUMBER as u64,
20        ))
21    })
22}
23
24/// Sets SRTCP replay protection window size.
25pub fn srtcp_replay_protection(window_size: usize) -> ContextOption {
26    Box::new(move || -> Box<dyn ReplayDetector> {
27        Box::new(WrappedSlidingWindowDetector::new(
28            window_size,
29            MAX_SRTCP_INDEX as u64,
30        ))
31    })
32}
33
34/// srtp_no_replay_protection disables SRTP replay protection.
35pub fn srtp_no_replay_protection() -> ContextOption {
36    Box::new(|| -> Box<dyn ReplayDetector> { Box::<NoOpReplayDetector>::default() })
37}
38
39/// srtcp_no_replay_protection disables SRTCP replay protection.
40pub fn srtcp_no_replay_protection() -> ContextOption {
41    Box::new(|| -> Box<dyn ReplayDetector> { Box::<NoOpReplayDetector>::default() })
42}