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
use crate::attributes::{AsVerifiable, EncodeAttributeValue, Verifiable};
use crate::context::AttributeEncoderContext;
use crate::error::{StunError, StunErrorType};
use crate::{AttributeType, StunAttribute};
#[derive(Debug, PartialEq, Eq)]
pub struct Unknown {
attr_type: AttributeType,
attr_data: Option<Vec<u8>>,
}
impl Unknown {
pub(crate) fn new<'a, T>(attr_type: AttributeType, data: T) -> Self
where
T: Into<Option<&'a [u8]>>,
{
Self {
attr_type,
attr_data: data.into().map(Vec::from),
}
}
pub fn attribute_type(&self) -> AttributeType {
self.attr_type
}
pub fn attribute_data(&self) -> Option<&[u8]> {
self.attr_data.as_ref().map(|v| v.as_ref())
}
}
impl AsVerifiable for Unknown {
fn as_verifiable_ref(&self) -> Option<&dyn Verifiable> {
None
}
}
impl EncodeAttributeValue for Unknown {
fn encode(&self, _ctx: AttributeEncoderContext) -> Result<usize, StunError> {
Err(StunError::new(
StunErrorType::InvalidParam,
format!(
"Unknown attribute [{:#02x}] can not be encoded",
self.attr_type.as_u16()
),
))
}
fn post_encode(&self, mut _ctx: AttributeEncoderContext) -> Result<(), StunError> {
Err(StunError::new(
StunErrorType::InvalidParam,
format!(
"Unknown attribute [{:#02x}] can not be decoded",
self.attr_type.as_u16()
),
))
}
}
impl From<Unknown> for StunAttribute {
fn from(value: Unknown) -> Self {
crate::attributes::StunAttribute::Unknown(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{DecoderContextBuilder, MessageDecoderBuilder};
#[test]
fn decode_unknown_attribute() {
let test_buffer = [
0x01, 0x01, 0x00, 0x08, 0x21, 0x12, 0xa4, 0x42, 0xb7, 0xe7, 0xa7, 0x01, 0xbc, 0x34, 0xd6, 0x86, 0xfa, 0x87, 0xdf, 0xae, 0xff, 0xff, 0x00, 0x02, 0x01, 0x02, 0xff, 0xff,
];
let ctx = DecoderContextBuilder::default().build();
let decoder = MessageDecoderBuilder::default().with_context(ctx).build();
let (msg, size) = decoder
.decode(&test_buffer)
.expect("Can not decode STUN message");
assert_eq!(size, 28);
let attributes = msg.attributes();
assert_eq!(attributes.len(), 1);
let attr = attributes
.get(0)
.expect("Can not get first attribute")
.as_unknown()
.expect("Not unknown attribute");
assert_eq!(attr.attribute_type(), AttributeType::from(0xffff));
assert_eq!(attr.attribute_data(), None);
let ctx = DecoderContextBuilder::default().with_unknown_data().build();
let decoder = MessageDecoderBuilder::default().with_context(ctx).build();
let (msg, size) = decoder
.decode(&test_buffer)
.expect("Can not decode STUN message");
assert_eq!(size, 28);
let attributes = msg.attributes();
assert_eq!(attributes.len(), 1);
let attr = attributes
.get(0)
.expect("Can not get first attribute")
.as_unknown()
.expect("Not unknown attribute");
assert_eq!(attr.attribute_type(), AttributeType::from(0xffff));
assert_eq!(attr.attribute_data(), Some([0x01, 0x02].as_ref()));
assert!(attr.as_verifiable_ref().is_none());
}
#[test]
fn encode_unknown_attribute() {
let dummy_msg: [u8; 0] = [];
let mut buffer: [u8; 6] = [0x00; 6];
let attr = Unknown::new(
AttributeType::from(0xffff),
Some([0x01, 0x02, 0x03, 0x04].as_ref()),
);
let ctx = AttributeEncoderContext::new(None, &dummy_msg, &mut buffer);
let result = attr.encode(ctx);
assert_eq!(
result.expect_err("Error expected"),
StunErrorType::InvalidParam
);
let ctx = AttributeEncoderContext::new(None, &dummy_msg, &mut buffer);
let result = attr.post_encode(ctx);
assert_eq!(
result.expect_err("Error expected"),
StunErrorType::InvalidParam
);
}
#[test]
fn unknown_stunt_attribute() {
let attr = StunAttribute::Unknown(Unknown::new(AttributeType::from(0x1234), None));
assert!(attr.is_unknown());
assert!(attr.as_unknown().is_ok());
assert!(attr.as_error_code().is_err());
let dbg_fmt = format!("{:?}", attr);
assert_eq!(
"Unknown(Unknown { attr_type: AttributeType (0x1234), attr_data: None })",
dbg_fmt
);
}
}