1 use thiserror::Error; 2 3 use std::io; 4 use std::net; 5 use std::num::ParseIntError; 6 use std::time::SystemTimeError; 7 8 pub type Result<T> = std::result::Result<T, Error>; 9 10 #[derive(Debug, Error, PartialEq)] 11 #[non_exhaustive] 12 pub enum Error { 13 #[error("turn: RelayAddress must be valid IP to use RelayAddressGeneratorStatic")] 14 ErrRelayAddressInvalid, 15 #[error("turn: PacketConnConfigs and ConnConfigs are empty, unable to proceed")] 16 ErrNoAvailableConns, 17 #[error("turn: PacketConnConfig must have a non-nil Conn")] 18 ErrConnUnset, 19 #[error("turn: ListenerConfig must have a non-nil Listener")] 20 ErrListenerUnset, 21 #[error("turn: RelayAddressGenerator has invalid ListeningAddress")] 22 ErrListeningAddressInvalid, 23 #[error("turn: RelayAddressGenerator in RelayConfig is unset")] 24 ErrRelayAddressGeneratorUnset, 25 #[error("turn: max retries exceeded")] 26 ErrMaxRetriesExceeded, 27 #[error("turn: MaxPort must be not 0")] 28 ErrMaxPortNotZero, 29 #[error("turn: MaxPort must be not 0")] 30 ErrMinPortNotZero, 31 #[error("turn: MaxPort less than MinPort")] 32 ErrMaxPortLessThanMinPort, 33 #[error("turn: relay_conn cannot not be nil")] 34 ErrNilConn, 35 #[error("turn: TODO")] 36 ErrTodo, 37 #[error("turn: already listening")] 38 ErrAlreadyListening, 39 #[error("turn: Server failed to close")] 40 ErrFailedToClose, 41 #[error("turn: failed to retransmit transaction")] 42 ErrFailedToRetransmitTransaction, 43 #[error("all retransmissions failed")] 44 ErrAllRetransmissionsFailed, 45 #[error("no binding found for channel")] 46 ErrChannelBindNotFound, 47 #[error("STUN server address is not set for the client")] 48 ErrStunserverAddressNotSet, 49 #[error("only one Allocate() caller is allowed")] 50 ErrOneAllocateOnly, 51 #[error("already allocated")] 52 ErrAlreadyAllocated, 53 #[error("non-STUN message from STUN server")] 54 ErrNonStunmessage, 55 #[error("failed to decode STUN message")] 56 ErrFailedToDecodeStun, 57 #[error("unexpected STUN request message")] 58 ErrUnexpectedStunrequestMessage, 59 #[error("channel number not in [0x4000, 0x7FFF]")] 60 ErrInvalidChannelNumber, 61 #[error("channelData length != len(Data)")] 62 ErrBadChannelDataLength, 63 #[error("unexpected EOF")] 64 ErrUnexpectedEof, 65 #[error("invalid value for requested family attribute")] 66 ErrInvalidRequestedFamilyValue, 67 #[error("fake error")] 68 ErrFakeErr, 69 #[error("try again")] 70 ErrTryAgain, 71 #[error("use of closed network connection")] 72 ErrClosed, 73 #[error("addr is not a net.UDPAddr")] 74 ErrUdpaddrCast, 75 #[error("already closed")] 76 ErrAlreadyClosed, 77 #[error("try-lock is already locked")] 78 ErrDoubleLock, 79 #[error("transaction closed")] 80 ErrTransactionClosed, 81 #[error("wait_for_result called on non-result transaction")] 82 ErrWaitForResultOnNonResultTransaction, 83 #[error("failed to build refresh request")] 84 ErrFailedToBuildRefreshRequest, 85 #[error("failed to refresh allocation")] 86 ErrFailedToRefreshAllocation, 87 #[error("failed to get lifetime from refresh response")] 88 ErrFailedToGetLifetime, 89 #[error("too short buffer")] 90 ErrShortBuffer, 91 #[error("unexpected response type")] 92 ErrUnexpectedResponse, 93 #[error("AllocatePacketConn must be set")] 94 ErrAllocatePacketConnMustBeSet, 95 #[error("AllocateConn must be set")] 96 ErrAllocateConnMustBeSet, 97 #[error("LeveledLogger must be set")] 98 ErrLeveledLoggerMustBeSet, 99 #[error("you cannot use the same channel number with different peer")] 100 ErrSameChannelDifferentPeer, 101 #[error("allocations must not be created with nil FivTuple")] 102 ErrNilFiveTuple, 103 #[error("allocations must not be created with nil FiveTuple.src_addr")] 104 ErrNilFiveTupleSrcAddr, 105 #[error("allocations must not be created with nil FiveTuple.dst_addr")] 106 ErrNilFiveTupleDstAddr, 107 #[error("allocations must not be created with nil turnSocket")] 108 ErrNilTurnSocket, 109 #[error("allocations must not be created with a lifetime of 0")] 110 ErrLifetimeZero, 111 #[error("allocation attempt created with duplicate FiveTuple")] 112 ErrDupeFiveTuple, 113 #[error("failed to cast net.Addr to *net.UDPAddr")] 114 ErrFailedToCastUdpaddr, 115 #[error("failed to generate nonce")] 116 ErrFailedToGenerateNonce, 117 #[error("failed to send error message")] 118 ErrFailedToSendError, 119 #[error("duplicated Nonce generated, discarding request")] 120 ErrDuplicatedNonce, 121 #[error("no such user exists")] 122 ErrNoSuchUser, 123 #[error("unexpected class")] 124 ErrUnexpectedClass, 125 #[error("unexpected method")] 126 ErrUnexpectedMethod, 127 #[error("failed to handle")] 128 ErrFailedToHandle, 129 #[error("unhandled STUN packet")] 130 ErrUnhandledStunpacket, 131 #[error("unable to handle ChannelData")] 132 ErrUnableToHandleChannelData, 133 #[error("failed to create stun message from packet")] 134 ErrFailedToCreateStunpacket, 135 #[error("failed to create channel data from packet")] 136 ErrFailedToCreateChannelData, 137 #[error("relay already allocated for 5-TUPLE")] 138 ErrRelayAlreadyAllocatedForFiveTuple, 139 #[error("RequestedTransport must be UDP")] 140 ErrRequestedTransportMustBeUdp, 141 #[error("no support for DONT-FRAGMENT")] 142 ErrNoDontFragmentSupport, 143 #[error("Request must not contain RESERVATION-TOKEN and EVEN-PORT")] 144 ErrRequestWithReservationTokenAndEvenPort, 145 #[error("no allocation found")] 146 ErrNoAllocationFound, 147 #[error("unable to handle send-indication, no permission added")] 148 ErrNoPermission, 149 #[error("packet write smaller than packet")] 150 ErrShortWrite, 151 #[error("no such channel bind")] 152 ErrNoSuchChannelBind, 153 #[error("failed writing to socket")] 154 ErrFailedWriteSocket, 155 #[error("parse int: {0}")] 156 ParseInt(#[from] ParseIntError), 157 #[error("parse addr: {0}")] 158 ParseIp(#[from] net::AddrParseError), 159 #[error("{0}")] 160 Io(#[source] IoError), 161 #[error("{0}")] 162 Util(#[from] util::Error), 163 #[error("{0}")] 164 Stun(#[from] stun::Error), 165 #[error("{0}")] 166 Other(String), 167 } 168 169 #[derive(Debug, Error)] 170 #[error("io error: {0}")] 171 pub struct IoError(#[from] pub io::Error); 172 173 // Workaround for wanting PartialEq for io::Error. 174 impl PartialEq for IoError { eq(&self, other: &Self) -> bool175 fn eq(&self, other: &Self) -> bool { 176 self.0.kind() == other.0.kind() 177 } 178 } 179 180 impl From<io::Error> for Error { from(e: io::Error) -> Self181 fn from(e: io::Error) -> Self { 182 Error::Io(IoError(e)) 183 } 184 } 185 186 impl From<SystemTimeError> for Error { from(e: SystemTimeError) -> Self187 fn from(e: SystemTimeError) -> Self { 188 Error::Other(e.to_string()) 189 } 190 } 191