stun_rs/attributes/turn/
icmp.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use crate::attributes::{stunt_attribute, DecodeAttributeValue, EncodeAttributeValue};
use crate::common::check_buffer_boundaries;
use crate::context::{AttributeDecoderContext, AttributeEncoderContext};
use crate::error::{StunError, StunErrorType};
use crate::Decode;
use crate::Encode;
use bounded_integer::{BoundedU16, BoundedU8};
use std::convert::TryInto;

const ICMP: u16 = 0x8004;
const ICMP_SIZE: usize = 8;
const ICMP_ERROR_DATA_SIZE: usize = 4;

/// This type represents the value of the `ICMP` type.  Its interpretation
/// depends on whether the `ICMP` was received over IPv4 or IPv6.
/// Valid values are in the range from 0 to 127.
/// # Examples
/// ```rust
/// # use stun_rs::attributes::turn::IcmpType;
/// // Use maximum value
/// let icmp_type = IcmpType::new(127);
/// assert!(icmp_type.is_some());
///
/// // Use minimum value
/// let icmp_type = IcmpType::new(0);
/// assert!(icmp_type.is_some());
///
/// // Use out of range value
/// let icmp_type = IcmpType::new(128);
/// assert!(icmp_type.is_none());
///```
pub type IcmpType = BoundedU8<0, 127>;

/// This type represents the value of the `ICMP` code.  Its interpretation
/// depends on whether the `ICMP` was received over IPv4 or IPv6.
/// Valid values are in the range from 0 to 511.
/// # Examples
///```rust
/// # use stun_rs::attributes::turn::IcmpCode;
/// // Use maximum value
/// let icmp_code = IcmpCode::new(511);
/// assert!(icmp_code.is_some());
///
/// // Use minimum value
/// let icmp_code = IcmpCode::new(0);
/// assert!(icmp_code.is_some());
///
/// // Use out of range value
/// let icmp_code = IcmpCode::new(512);
/// assert!(icmp_code.is_none());
///```
pub type IcmpCode = BoundedU16<0, 511>;

/// This attribute is used by servers to signal the reason a `UDP` packet
/// was dropped.
/// # Examples
/// ```rust
/// # use stun_rs::attributes::turn::{Icmp, IcmpCode, IcmpType};
/// # use std::error::Error;
/// #
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let icmp_type = IcmpType::new(127).ok_or_else(|| "Invalid ICMP type")?;
/// let icmp_code = IcmpCode::new(511).ok_or_else(|| "Invalid ICMP code")?;
/// let attr = Icmp::new(icmp_type, icmp_code, [0x01, 0x02, 0x03, 0x04]);
///
/// assert_eq!(attr.icmp_type(), icmp_type);
/// assert_eq!(attr.icmp_code(), icmp_code);
/// assert_eq!(attr.error_data(), &[0x01, 0x02, 0x03, 0x04]);
/// #
/// # Ok(())
/// # }
///```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Icmp {
    icmp_type: IcmpType,
    icmp_code: IcmpCode,
    error_data: [u8; ICMP_ERROR_DATA_SIZE],
}

impl Icmp {
    /// Creates a new [`Icmp`] attribute
    pub fn new(
        icmp_type: IcmpType,
        icmp_code: IcmpCode,
        error_data: [u8; ICMP_ERROR_DATA_SIZE],
    ) -> Self {
        Self {
            icmp_type,
            icmp_code,
            error_data,
        }
    }

    /// Returns the `ICMP` type.
    pub fn icmp_type(&self) -> IcmpType {
        self.icmp_type
    }

    /// Returns the error data.
    pub fn icmp_code(&self) -> IcmpCode {
        self.icmp_code
    }

    /// Returns the error data. This field size is 4 bytes long.
    /// If the `ICMPv6` type is 2 ("Packet too big" message) or `ICMPv4`
    /// type is 3 (Destination Unreachable) and Code is 4 (fragmentation
    /// needed and `DF` set), the Error Data field will be set to the Maximum
    /// Transmission Unit of the next-hop link (Section 3.2 of
    /// [`RFC4443`](https://datatracker.ietf.org/doc/html/rfc4443#section-3.2)
    /// and Section 4 of [`RFC1191`](https://datatracker.ietf.org/doc/html/rfc1191#section-4)).
    /// For other `ICMPv6` types and `ICMPv4` types and codes, the Error Data field
    /// MUST be set to zero.
    pub fn error_data(&self) -> &[u8] {
        &self.error_data
    }
}

// Format of ICMP Attribute:
//  0                   1                   2                   3
//  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |  Reserved                     |  ICMP Type  |  ICMP Code      |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |                          Error Data                           |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

impl DecodeAttributeValue for Icmp {
    fn decode(ctx: AttributeDecoderContext) -> Result<(Self, usize), StunError> {
        let raw_value = ctx.raw_value();
        check_buffer_boundaries(raw_value, ICMP_SIZE)?;
        let (icmp, _) = u16::decode(&raw_value[2..=3])?;
        let icmp_type = IcmpType::new((icmp >> 9).try_into()?).ok_or_else(|| {
            StunError::new(
                StunErrorType::InvalidParam,
                format!("Decoded invalid ICMP type {}", icmp >> 9),
            )
        })?;
        let icmp_code = IcmpCode::new(0x01ff & icmp).ok_or_else(|| {
            StunError::new(
                StunErrorType::InvalidParam,
                format!("Decoded invalid ICMP code {}", 0x01ff & icmp),
            )
        })?;
        let mut error_data: [u8; ICMP_ERROR_DATA_SIZE] = [0x0; ICMP_ERROR_DATA_SIZE];
        error_data.copy_from_slice(&raw_value[4..ICMP_SIZE]);
        Ok((Icmp::new(icmp_type, icmp_code, error_data), ICMP_SIZE))
    }
}

impl EncodeAttributeValue for Icmp {
    fn encode(&self, mut ctx: AttributeEncoderContext) -> Result<usize, StunError> {
        let raw_value = ctx.raw_value_mut();
        check_buffer_boundaries(raw_value, ICMP_SIZE)?;
        raw_value[..=1].fill(0);
        let icmp_type: u16 = self.icmp_type.into();
        let icmp_code: u16 = self.icmp_code.into();
        let icmp: u16 = icmp_type << 9 | icmp_code;
        icmp.encode(&mut raw_value[2..=3])?;
        raw_value[4..ICMP_SIZE].copy_from_slice(&self.error_data);
        Ok(ICMP_SIZE)
    }
}

impl crate::attributes::AsVerifiable for Icmp {}

stunt_attribute!(Icmp, ICMP);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::StunErrorType;
    use crate::StunAttribute;

    #[test]
    fn decode_icmp_value() {
        let dummy_msg = [];
        let buffer: [u8; 0] = [];
        let ctx = AttributeDecoderContext::new(None, &dummy_msg, &buffer);
        let result = Icmp::decode(ctx);
        assert_eq!(
            result.expect_err("Error expected"),
            StunErrorType::SmallBuffer
        );

        let buffer = [0x00, 0x00, 0x03, 0x01, 0x01, 0x02, 0x03];
        let ctx = AttributeDecoderContext::new(None, &dummy_msg, &buffer);
        let result = Icmp::decode(ctx);
        assert_eq!(
            result.expect_err("Error expected"),
            StunErrorType::SmallBuffer
        );

        let buffer = [0x00, 0x00, 0x03, 0x01, 0x01, 0x02, 0x03, 0x04];
        let ctx = AttributeDecoderContext::new(None, &dummy_msg, &buffer);
        let (attr, size) = Icmp::decode(ctx).expect("Can not decode ICMP attribute");
        assert_eq!(size, ICMP_SIZE);
        assert_eq!(attr.icmp_type(), IcmpType::new(0x01u8).unwrap());
        assert_eq!(attr.icmp_code(), IcmpCode::new(0x101).unwrap());
        assert_eq!(attr.error_data(), &buffer[4..]);
    }

    #[test]
    fn encode_icmp_value() {
        let dummy_msg: [u8; 0] = [];
        let icmp_type = IcmpType::new(127).unwrap();
        let icmp_code = IcmpCode::new(511).unwrap();
        let error_data = [0x01, 0x02, 0x03, 0x04];
        let attr = Icmp::new(icmp_type, icmp_code, error_data);

        let mut buffer = [];
        let ctx = AttributeEncoderContext::new(None, &dummy_msg, &mut buffer);
        let result = attr.encode(ctx);
        assert_eq!(
            result.expect_err("Error expected"),
            StunErrorType::SmallBuffer
        );

        let mut buffer: [u8; 7] = [0xff; 7];
        let ctx = AttributeEncoderContext::new(None, &dummy_msg, &mut buffer);
        let result = attr.encode(ctx);
        assert_eq!(
            result.expect_err("Error expected"),
            StunErrorType::SmallBuffer
        );

        let mut buffer: [u8; 8] = [0xff; 8];
        let ctx = AttributeEncoderContext::new(None, &dummy_msg, &mut buffer);
        let result = attr.encode(ctx);
        assert_eq!(result, Ok(8));

        let expected_buffer = [0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03, 0x04];
        assert_eq!(&buffer[..], &expected_buffer[..]);
    }

    #[test]
    fn icmp_stunt_attribute() {
        let icmp_type = IcmpType::new(127).expect("Can not create ICMP type");
        let icmp_code = IcmpCode::new(511).expect("Can not create ICMP type");
        let icmp = Icmp::new(icmp_type, icmp_code, [0x01, 0x02, 0x03, 0x04]);

        let attr = StunAttribute::Icmp(icmp);
        assert!(attr.is_icmp());
        assert!(attr.as_icmp().is_ok());
        assert!(attr.as_unknown().is_err());

        assert!(!attr.attribute_type().is_comprehension_required());
        assert!(attr.attribute_type().is_comprehension_optional());

        let dbg_fmt = format!("{:?}", attr);
        assert_eq!("Icmp(Icmp { icmp_type: Bounded(127), icmp_code: Bounded(511), error_data: [1, 2, 3, 4] })", dbg_fmt);
    }
}