webrtc_srtp/
error.rs

1use std::io;
2
3use thiserror::Error;
4use tokio::sync::mpsc::error::SendError as MpscSendError;
5
6pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Error, Debug, PartialEq)]
9#[non_exhaustive]
10pub enum Error {
11    #[error("duplicated packet")]
12    ErrDuplicated,
13    #[error("SRTP master key is not long enough")]
14    ErrShortSrtpMasterKey,
15    #[error("SRTP master salt is not long enough")]
16    ErrShortSrtpMasterSalt,
17    #[error("no such SRTP Profile")]
18    ErrNoSuchSrtpProfile,
19    #[error("indexOverKdr > 0 is not supported yet")]
20    ErrNonZeroKdrNotSupported,
21    #[error("exporter called with wrong label")]
22    ErrExporterWrongLabel,
23    #[error("no config provided")]
24    ErrNoConfig,
25    #[error("no conn provided")]
26    ErrNoConn,
27    #[error("failed to verify auth tag")]
28    ErrFailedToVerifyAuthTag,
29    #[error("packet is too short to be RTP packet")]
30    ErrTooShortRtp,
31    #[error("packet is too short to be RTCP packet")]
32    ErrTooShortRtcp,
33    #[error("payload differs")]
34    ErrPayloadDiffers,
35    #[error("started channel used incorrectly, should only be closed")]
36    ErrStartedChannelUsedIncorrectly,
37    #[error("exceeded the maximum number of packets")]
38    ErrExceededMaxPackets,
39
40    #[error("stream has not been inited, unable to close")]
41    ErrStreamNotInited,
42    #[error("stream is already closed")]
43    ErrStreamAlreadyClosed,
44    #[error("stream is already inited")]
45    ErrStreamAlreadyInited,
46    #[error("failed to cast child")]
47    ErrFailedTypeAssertion,
48
49    #[error("index_over_kdr > 0 is not supported yet")]
50    UnsupportedIndexOverKdr,
51    #[error("SRTP Master Key must be len {0}, got {1}")]
52    SrtpMasterKeyLength(usize, usize),
53    #[error("SRTP Salt must be len {0}, got {1}")]
54    SrtpSaltLength(usize, usize),
55    #[error("SyntaxError: {0}")]
56    ExtMapParse(String),
57    #[error("srtp ssrc={0} index={1}: duplicated")]
58    SrtpSsrcDuplicated(u32, u16),
59    #[error("srtcp ssrc={0} index={1}: duplicated")]
60    SrtcpSsrcDuplicated(u32, usize),
61    #[error("ssrc {0} not exist in srtcp_ssrc_state")]
62    SsrcMissingFromSrtcp(u32),
63    #[error("Stream with ssrc {0} exists")]
64    StreamWithSsrcExists(u32),
65    #[error("Session RTP/RTCP type must be same as input buffer")]
66    SessionRtpRtcpTypeMismatch,
67    #[error("Session EOF")]
68    SessionEof,
69    #[error("too short SRTP packet: only {0} bytes, expected > {1} bytes")]
70    SrtpTooSmall(usize, usize),
71    #[error("too short SRTCP packet: only {0} bytes, expected > {1} bytes")]
72    SrtcpTooSmall(usize, usize),
73    #[error("failed to verify rtp auth tag")]
74    RtpFailedToVerifyAuthTag,
75    #[error("too short auth tag: only {0} bytes, expected > {1} bytes")]
76    RtcpInvalidLengthAuthTag(usize, usize),
77    #[error("failed to verify rtcp auth tag")]
78    RtcpFailedToVerifyAuthTag,
79    #[error("SessionSRTP has been closed")]
80    SessionSrtpAlreadyClosed,
81    #[error("this stream is not a RTPStream")]
82    InvalidRtpStream,
83    #[error("this stream is not a RTCPStream")]
84    InvalidRtcpStream,
85
86    #[error("{0}")]
87    Io(#[source] IoError),
88    #[error("{0}")]
89    KeyingMaterial(#[from] util::KeyingMaterialExporterError),
90    #[error("mpsc send: {0}")]
91    MpscSend(String),
92    #[error("{0}")]
93    Util(#[from] util::Error),
94    #[error("{0}")]
95    Rtcp(#[from] rtcp::Error),
96    #[error("aes gcm: {0}")]
97    AesGcm(#[from] aes_gcm::Error),
98
99    #[error("{0}")]
100    Other(String),
101}
102
103#[derive(Debug, Error)]
104#[error("io error: {0}")]
105pub struct IoError(#[from] pub io::Error);
106
107// Workaround for wanting PartialEq for io::Error.
108impl PartialEq for IoError {
109    fn eq(&self, other: &Self) -> bool {
110        self.0.kind() == other.0.kind()
111    }
112}
113
114impl From<io::Error> for Error {
115    fn from(e: io::Error) -> Self {
116        Error::Io(IoError(e))
117    }
118}
119
120// Because Tokio SendError is parameterized, we sadly lose the backtrace.
121impl<T> From<MpscSendError<T>> for Error {
122    fn from(e: MpscSendError<T>) -> Self {
123        Error::MpscSend(e.to_string())
124    }
125}