webrtc_sctp/
error.rs

1use std::io;
2
3use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, Error>;
6
7#[derive(Debug, Error, PartialEq, Eq, Clone)]
8#[non_exhaustive]
9pub enum Error {
10    #[error("raw is too small for a SCTP chunk")]
11    ErrChunkHeaderTooSmall,
12    #[error("not enough data left in SCTP packet to satisfy requested length")]
13    ErrChunkHeaderNotEnoughSpace,
14    #[error("chunk PADDING is non-zero at offset")]
15    ErrChunkHeaderPaddingNonZero,
16    #[error("chunk has invalid length")]
17    ErrChunkHeaderInvalidLength,
18
19    #[error("ChunkType is not of type ABORT")]
20    ErrChunkTypeNotAbort,
21    #[error("failed build Abort Chunk")]
22    ErrBuildAbortChunkFailed,
23    #[error("ChunkType is not of type COOKIEACK")]
24    ErrChunkTypeNotCookieAck,
25    #[error("ChunkType is not of type COOKIEECHO")]
26    ErrChunkTypeNotCookieEcho,
27    #[error("ChunkType is not of type ctError")]
28    ErrChunkTypeNotCtError,
29    #[error("failed build Error Chunk")]
30    ErrBuildErrorChunkFailed,
31    #[error("failed to marshal stream")]
32    ErrMarshalStreamFailed,
33    #[error("chunk too short")]
34    ErrChunkTooShort,
35    #[error("ChunkType is not of type ForwardTsn")]
36    ErrChunkTypeNotForwardTsn,
37    #[error("ChunkType is not of type HEARTBEAT")]
38    ErrChunkTypeNotHeartbeat,
39    #[error("ChunkType is not of type HEARTBEATACK")]
40    ErrChunkTypeNotHeartbeatAck,
41    #[error("heartbeat is not long enough to contain Heartbeat Info")]
42    ErrHeartbeatNotLongEnoughInfo,
43    #[error("failed to parse param type")]
44    ErrParseParamTypeFailed,
45    #[error("heartbeat should only have HEARTBEAT param")]
46    ErrHeartbeatParam,
47    #[error("failed unmarshalling param in Heartbeat Chunk")]
48    ErrHeartbeatChunkUnmarshal,
49    #[error("unimplemented")]
50    ErrUnimplemented,
51    #[error("heartbeat Ack must have one param")]
52    ErrHeartbeatAckParams,
53    #[error("heartbeat Ack must have one param, and it should be a HeartbeatInfo")]
54    ErrHeartbeatAckNotHeartbeatInfo,
55    #[error("unable to marshal parameter for Heartbeat Ack")]
56    ErrHeartbeatAckMarshalParam,
57
58    #[error("raw is too small for error cause")]
59    ErrErrorCauseTooSmall,
60
61    #[error("unhandled ParamType `{typ}`")]
62    ErrParamTypeUnhandled { typ: u16 },
63
64    #[error("unexpected ParamType")]
65    ErrParamTypeUnexpected,
66
67    #[error("param header too short")]
68    ErrParamHeaderTooShort,
69    #[error("param self reported length is shorter than header length")]
70    ErrParamHeaderSelfReportedLengthShorter,
71    #[error("param self reported length is longer than header length")]
72    ErrParamHeaderSelfReportedLengthLonger,
73    #[error("failed to parse param type")]
74    ErrParamHeaderParseFailed,
75
76    #[error("packet to short")]
77    ErrParamPacketTooShort,
78    #[error("outgoing SSN reset request parameter too short")]
79    ErrSsnResetRequestParamTooShort,
80    #[error("reconfig response parameter too short")]
81    ErrReconfigRespParamTooShort,
82    #[error("invalid algorithm type")]
83    ErrInvalidAlgorithmType,
84
85    #[error("failed to parse param type")]
86    ErrInitChunkParseParamTypeFailed,
87    #[error("failed unmarshalling param in Init Chunk")]
88    ErrInitChunkUnmarshalParam,
89    #[error("unable to marshal parameter for INIT/INITACK")]
90    ErrInitAckMarshalParam,
91
92    #[error("ChunkType is not of type INIT")]
93    ErrChunkTypeNotTypeInit,
94    #[error("chunk Value isn't long enough for mandatory parameters exp")]
95    ErrChunkValueNotLongEnough,
96    #[error("ChunkType of type INIT flags must be all 0")]
97    ErrChunkTypeInitFlagZero,
98    #[error("failed to unmarshal INIT body")]
99    ErrChunkTypeInitUnmarshalFailed,
100    #[error("failed marshaling INIT common data")]
101    ErrChunkTypeInitMarshalFailed,
102    #[error("ChunkType of type INIT ACK InitiateTag must not be 0")]
103    ErrChunkTypeInitInitiateTagZero,
104    #[error("INIT ACK inbound stream request must be > 0")]
105    ErrInitInboundStreamRequestZero,
106    #[error("INIT ACK outbound stream request must be > 0")]
107    ErrInitOutboundStreamRequestZero,
108    #[error("INIT ACK Advertised Receiver Window Credit (a_rwnd) must be >= 1500")]
109    ErrInitAdvertisedReceiver1500,
110
111    #[error("packet is smaller than the header size")]
112    ErrChunkPayloadSmall,
113    #[error("ChunkType is not of type PayloadData")]
114    ErrChunkTypeNotPayloadData,
115    #[error("ChunkType is not of type Reconfig")]
116    ErrChunkTypeNotReconfig,
117    #[error("ChunkReconfig has invalid ParamA")]
118    ErrChunkReconfigInvalidParamA,
119
120    #[error("failed to parse param type")]
121    ErrChunkParseParamTypeFailed,
122    #[error("unable to marshal parameter A for reconfig")]
123    ErrChunkMarshalParamAReconfigFailed,
124    #[error("unable to marshal parameter B for reconfig")]
125    ErrChunkMarshalParamBReconfigFailed,
126
127    #[error("ChunkType is not of type SACK")]
128    ErrChunkTypeNotSack,
129    #[error("SACK Chunk size is not large enough to contain header")]
130    ErrSackSizeNotLargeEnoughInfo,
131
132    #[error("invalid chunk size")]
133    ErrInvalidChunkSize,
134    #[error("ChunkType is not of type SHUTDOWN")]
135    ErrChunkTypeNotShutdown,
136
137    #[error("ChunkType is not of type SHUTDOWN-ACK")]
138    ErrChunkTypeNotShutdownAck,
139    #[error("ChunkType is not of type SHUTDOWN-COMPLETE")]
140    ErrChunkTypeNotShutdownComplete,
141
142    #[error("raw is smaller than the minimum length for a SCTP packet")]
143    ErrPacketRawTooSmall,
144    #[error("unable to parse SCTP chunk, not enough data for complete header")]
145    ErrParseSctpChunkNotEnoughData,
146    #[error("failed to unmarshal, contains unknown chunk type")]
147    ErrUnmarshalUnknownChunkType,
148    #[error("checksum mismatch theirs")]
149    ErrChecksumMismatch,
150
151    #[error("unexpected chunk popped (unordered)")]
152    ErrUnexpectedChuckPoppedUnordered,
153    #[error("unexpected chunk popped (ordered)")]
154    ErrUnexpectedChuckPoppedOrdered,
155    #[error("unexpected q state (should've been selected)")]
156    ErrUnexpectedQState,
157    #[error("try again")]
158    ErrTryAgain,
159
160    #[error("abort chunk, with following errors")]
161    ErrChunk,
162    #[error("shutdown called in non-Established state")]
163    ErrShutdownNonEstablished,
164    #[error("association closed before connecting")]
165    ErrAssociationClosedBeforeConn,
166    #[error("association init failed")]
167    ErrAssociationInitFailed,
168    #[error("association handshake closed")]
169    ErrAssociationHandshakeClosed,
170    #[error("silently discard")]
171    ErrSilentlyDiscard,
172    #[error("the init not stored to send")]
173    ErrInitNotStoredToSend,
174    #[error("cookieEcho not stored to send")]
175    ErrCookieEchoNotStoredToSend,
176    #[error("sctp packet must not have a source port of 0")]
177    ErrSctpPacketSourcePortZero,
178    #[error("sctp packet must not have a destination port of 0")]
179    ErrSctpPacketDestinationPortZero,
180    #[error("init chunk must not be bundled with any other chunk")]
181    ErrInitChunkBundled,
182    #[error("init chunk expects a verification tag of 0 on the packet when out-of-the-blue")]
183    ErrInitChunkVerifyTagNotZero,
184    #[error("todo: handle Init when in state")]
185    ErrHandleInitState,
186    #[error("no cookie in InitAck")]
187    ErrInitAckNoCookie,
188    #[error("there already exists a stream with identifier")]
189    ErrStreamAlreadyExist,
190    #[error("Failed to create a stream with identifier")]
191    ErrStreamCreateFailed,
192    #[error("unable to be popped from inflight queue TSN")]
193    ErrInflightQueueTsnPop,
194    #[error("requested non-existent TSN")]
195    ErrTsnRequestNotExist,
196    #[error("sending reset packet in non-Established state")]
197    ErrResetPacketInStateNotExist,
198    #[error("unexpected parameter type")]
199    ErrParameterType,
200    #[error("sending payload data in non-Established state")]
201    ErrPayloadDataStateNotExist,
202    #[error("unhandled chunk type")]
203    ErrChunkTypeUnhandled,
204    #[error("handshake failed (INIT ACK)")]
205    ErrHandshakeInitAck,
206    #[error("handshake failed (COOKIE ECHO)")]
207    ErrHandshakeCookieEcho,
208
209    #[error("outbound packet larger than maximum message size")]
210    ErrOutboundPacketTooLarge,
211    #[error("Stream closed")]
212    ErrStreamClosed,
213    #[error("Short buffer (size: {size:?}) to be filled")]
214    ErrShortBuffer { size: usize },
215    #[error("Io EOF")]
216    ErrEof,
217    #[error("Invalid SystemTime")]
218    ErrInvalidSystemTime,
219    #[error("Net Conn read error")]
220    ErrNetConnReadError,
221    #[error("Max Data Channel ID")]
222    ErrMaxDataChannelID,
223
224    #[error("{0}")]
225    Other(String),
226}
227
228impl From<Error> for io::Error {
229    fn from(error: Error) -> Self {
230        match error {
231            e @ Error::ErrEof => io::Error::new(io::ErrorKind::UnexpectedEof, e.to_string()),
232            e @ Error::ErrStreamClosed => {
233                io::Error::new(io::ErrorKind::ConnectionAborted, e.to_string())
234            }
235            e => io::Error::new(io::ErrorKind::Other, e.to_string()),
236        }
237    }
238}