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
/// VSA trait represents the general vendor-specific struct related methods.
pub trait VSA {
/// len returns the length of sub-attribute of vendor-specific.
///
/// Ref: RFC 4679 - https://datatracker.ietf.org/doc/html/rfc4679
/// > Vendor-Length
/// >
/// > The Vendor-Length field is one octet and indicates the length of
/// > the entire sub-attribute, including the Vendor-Type,
/// > Vendor-Length, and Value fields.
fn len(&self) -> usize;
/// is_empty returns whether the VSA is empty or not.
fn is_empty(&self) -> bool;
/// message returns the serialized vendor-specific message for AVP.
fn message(&self) -> Vec<u8>;
}
/// StringVSA represents the VSA according to the RFC 2865.
#[derive(Debug, Clone, PartialEq)]
pub struct StringVSA {
vendor_id: Vec<u8>,
vendor_type: u8,
length: u8,
value: Vec<u8>,
}
impl StringVSA {
const BYTE_SIZE_OFFSET: usize = 2;
pub fn new(vendor_id: i32, vendor_type: u8, value: &str) -> StringVSA {
StringVSA {
vendor_id: vendor_id.to_be_bytes().to_vec(),
vendor_type,
/*
* Ref: RFC 4679 - https://datatracker.ietf.org/doc/html/rfc4679
* > Vendor-Length
* >
* > The Vendor-Length field is one octet and indicates the length of
* > the entire sub-attribute, including the Vendor-Type,
* > Vendor-Length, and Value fields.
*/
length: (Self::BYTE_SIZE_OFFSET + value.len()) as u8,
value: value.as_bytes().to_vec(),
}
}
}
impl VSA for StringVSA {
fn len(&self) -> usize {
self.length as usize
}
fn is_empty(&self) -> bool {
self.length == 0
}
/// message returns the serialized vendor-specific message for AVP.
///
/// Format:
/// 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
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// | Type | Length | Vendor-Id
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// Vendor-Id (cont) | Vendor type | Vendor length |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// | Attribute-Specific...
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
///
/// See also: RFC 2865 - https://datatracker.ietf.org/doc/html/rfc2865
fn message(&self) -> Vec<u8> {
let total_length: usize = Self::BYTE_SIZE_OFFSET + self.vendor_id.len() + self.value.len();
let mut result = Vec::with_capacity(total_length);
result.extend(&self.vendor_id);
result.extend(vec![self.vendor_type, self.length]);
result.extend(&self.value);
result
}
}
/// TaggedStringVSA represents the VSA which has a tag value.
#[derive(Debug, Clone, PartialEq)]
pub struct TaggedStringVSA {
vendor_id: Vec<u8>,
vendor_type: u8,
length: u8,
tag: u8,
value: Vec<u8>,
}
impl TaggedStringVSA {
const BYTE_SIZE_OFFSET: usize = 3;
pub fn new(vendor_id: i32, vendor_type: u8, tag: u8, value: &str) -> TaggedStringVSA {
TaggedStringVSA {
vendor_id: vendor_id.to_be_bytes().to_vec(),
vendor_type,
/*
* Ref: RFC 4679 - https://datatracker.ietf.org/doc/html/rfc4679
* > Vendor-Length
* >
* > The Vendor-Length field is one octet and indicates the length of
* > the entire sub-attribute, including the Vendor-Type,
* > Vendor-Length, and Value fields.
*/
length: (Self::BYTE_SIZE_OFFSET + value.len()) as u8,
tag,
value: value.as_bytes().to_vec(),
}
}
}
impl VSA for TaggedStringVSA {
fn len(&self) -> usize {
self.length as usize
}
fn is_empty(&self) -> bool {
self.length == 0
}
/// message returns the serialized vendor-specific message for AVP.
///
/// Format:
/// 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
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// | Type | Length | Vendor-Id
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// Vendor-Id (cont) | Vendor type | Vendor length |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// | Tag | Attribute-Specific...
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
///
/// See also: CISCO RADIUS Attributes Configuration Guide - https://www.cisco.com/c/en/us/td/docs/ios-xml/ios/sec_usr_radatt/configuration/xe-16/sec-usr-radatt-xe-16-book.pdf
fn message(&self) -> Vec<u8> {
let total_length: usize = Self::BYTE_SIZE_OFFSET + self.vendor_id.len() + self.value.len();
let mut result = Vec::with_capacity(total_length);
result.extend(&self.vendor_id);
result.extend(vec![self.vendor_type, self.length, self.tag]);
result.extend(&self.value);
result
}
}
#[cfg(test)]
mod string_vsa_tests {
use crate::core::vsa::{StringVSA, VSA};
#[test]
fn it_should_get_len_successfully() {
let vendor_id = 4874;
let vsa_type = 65;
let value = "bar(1000,5441)";
let vsa = StringVSA::new(vendor_id, vsa_type, value);
assert_eq!(vsa.len(), 16);
}
#[test]
fn it_should_get_message_successfully() {
let vendor_id = 4874;
let vsa_type = 65;
let value = "bar(1000,5441)";
let vsa = StringVSA::new(vendor_id, vsa_type, value);
assert_eq!(
vsa.message(),
[0, 0, 19, 10, 65, 16, 98, 97, 114, 40, 49, 48, 48, 48, 44, 53, 52, 52, 49, 41]
)
}
}
#[cfg(test)]
mod tagged_string_vsa_tests {
use crate::core::vsa::{TaggedStringVSA, VSA};
#[test]
fn it_should_get_len_successfully() {
let vendor_id = 4874;
let vsa_type = 65;
let tag = 5;
let value = "bar(1000,5441)";
let vsa = TaggedStringVSA::new(vendor_id, vsa_type, tag, value);
assert_eq!(vsa.len(), 17);
}
#[test]
fn it_should_get_message_successfully() {
let vendor_id = 4874;
let vsa_type = 65;
let tag = 5;
let value = "bar(1000,5441)";
let vsa = TaggedStringVSA::new(vendor_id, vsa_type, tag, value);
assert_eq!(
vsa.message(),
[0, 0, 19, 10, 65, 17, 5, 98, 97, 114, 40, 49, 48, 48, 48, 44, 53, 52, 52, 49, 41]
)
}
}