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