stun_codec/rfc5389/
errors.rs

1//! Error codes that are defined in [RFC 5389 -- 15.6 ERROR-CODE].
2//!
3//! [RFC 5389 -- 15.6 ERROR-CODE]: https://tools.ietf.org/html/rfc5389#section-15.6
4use crate::rfc5389::attributes::ErrorCode;
5
6/// `300`: "Try Alternate".
7///
8/// > The client should contact an alternate server for
9/// > this request.  This error response MUST only be sent if the
10/// > request included a USERNAME attribute and a valid MESSAGE-
11/// > INTEGRITY attribute; otherwise, it MUST NOT be sent and error
12/// > code 400 (Bad Request) is suggested.  This error response MUST
13/// > be protected with the MESSAGE-INTEGRITY attribute, and receivers
14/// > MUST validate the MESSAGE-INTEGRITY of this response before
15/// > redirecting themselves to an alternate server.
16/// >
17/// > > Note: Failure to generate and validate message integrity
18/// > > for a 300 response allows an on-path attacker to falsify a
19/// > > 300 response thus causing subsequent STUN messages to be
20/// > > sent to a victim.
21/// >
22/// > [RFC 5389 -- 15.6 ERROR-CODE]
23///
24/// [RFC 5389 -- 15.6 ERROR-CODE]: https://tools.ietf.org/html/rfc5389#section-15.6
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
26pub struct TryAlternate;
27impl TryAlternate {
28    /// The codepoint of the error.
29    pub const CODEPOINT: u16 = 300;
30}
31impl From<TryAlternate> for ErrorCode {
32    fn from(_: TryAlternate) -> Self {
33        ErrorCode::new(TryAlternate::CODEPOINT, "Try Alternate".to_owned()).expect("never fails")
34    }
35}
36
37/// `400`: "Bad Request".
38///
39/// > The request was malformed.  The client SHOULD NOT
40/// > retry the request without modification from the previous
41/// > attempt.  The server may not be able to generate a valid
42/// > MESSAGE-INTEGRITY for this error, so the client MUST NOT expect
43/// > a valid MESSAGE-INTEGRITY attribute on this response.
44/// >
45/// > [RFC 5389 -- 15.6 ERROR-CODE]
46///
47/// [RFC 5389 -- 15.6 ERROR-CODE]: https://tools.ietf.org/html/rfc5389#section-15.6
48#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
49pub struct BadRequest;
50impl BadRequest {
51    /// The codepoint of the error.
52    pub const CODEPOINT: u16 = 400;
53}
54impl From<BadRequest> for ErrorCode {
55    fn from(_: BadRequest) -> Self {
56        ErrorCode::new(BadRequest::CODEPOINT, "Bad Request".to_owned()).expect("never fails")
57    }
58}
59
60/// `401`: "Unauthorized".
61///
62/// > The request did not contain the correct
63/// > credentials to proceed.  The client should retry the request
64/// > with proper credentials.
65/// >
66/// > [RFC 5389 -- 15.6 ERROR-CODE]
67///
68/// [RFC 5389 -- 15.6 ERROR-CODE]: https://tools.ietf.org/html/rfc5389#section-15.6
69#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
70pub struct Unauthorized;
71impl Unauthorized {
72    /// The codepoint of the error.
73    pub const CODEPOINT: u16 = 401;
74}
75impl From<Unauthorized> for ErrorCode {
76    fn from(_: Unauthorized) -> Self {
77        ErrorCode::new(Unauthorized::CODEPOINT, "Unauthorized".to_owned()).expect("never fails")
78    }
79}
80
81/// `420`: "Unknown Attribute".
82///
83/// > The server received a STUN packet containing
84/// > a comprehension-required attribute that it did not understand.
85/// > The server MUST put this unknown attribute in the UNKNOWN-
86/// > ATTRIBUTE attribute of its error response.
87/// >
88/// > [RFC 5389 -- 15.6 ERROR-CODE]
89///
90/// [RFC 5389 -- 15.6 ERROR-CODE]: https://tools.ietf.org/html/rfc5389#section-15.6
91#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
92pub struct UnknownAttribute;
93impl UnknownAttribute {
94    /// The codepoint of the error.
95    pub const CODEPOINT: u16 = 420;
96}
97impl From<UnknownAttribute> for ErrorCode {
98    fn from(_: UnknownAttribute) -> Self {
99        ErrorCode::new(UnknownAttribute::CODEPOINT, "Unknown Attribute".to_owned())
100            .expect("never fails")
101    }
102}
103
104/// `438`: "Stale Nonce".
105///
106/// > The NONCE used by the client was no longer valid.
107/// > The client should retry, using the NONCE provided in the
108/// > response.
109/// >
110/// > [RFC 5389 -- 15.6 ERROR-CODE]
111///
112/// [RFC 5389 -- 15.6 ERROR-CODE]: https://tools.ietf.org/html/rfc5389#section-15.6
113#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
114pub struct StaleNonce;
115impl StaleNonce {
116    /// The codepoint of the error.
117    pub const CODEPOINT: u16 = 438;
118}
119impl From<StaleNonce> for ErrorCode {
120    fn from(_: StaleNonce) -> Self {
121        ErrorCode::new(StaleNonce::CODEPOINT, "Stale Nonce".to_owned()).expect("never fails")
122    }
123}
124
125/// `500`: "Server Error".
126///
127/// > The server has suffered a temporary error.  The
128/// > client should try again.
129/// >
130/// > [RFC 5389 -- 15.6 ERROR-CODE]
131///
132/// [RFC 5389 -- 15.6 ERROR-CODE]: https://tools.ietf.org/html/rfc5389#section-15.6
133#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
134pub struct ServerError;
135impl ServerError {
136    /// The codepoint of the error.
137    pub const CODEPOINT: u16 = 500;
138}
139impl From<ServerError> for ErrorCode {
140    fn from(_: ServerError) -> Self {
141        ErrorCode::new(ServerError::CODEPOINT, "Server Error".to_owned()).expect("never fails")
142    }
143}