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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use std::net::Ipv4Addr;
use super::command_number::CommandNumber;
use crate::timestamp::timestamp::TimeStamp;
use deku::prelude::*;
#[derive(Clone, Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(endian = "big")]
pub struct RequestTwSession {
/// Command number field. The value of Request-TW-Session is `5`.
#[deku(assert_eq = "CommandNumber::RequestTwSession")]
command_number: CommandNumber,
/// Must be zero.
#[deku(bits = "4", assert_eq = "0u8")]
mbz_first: u8,
/// IP version numbers for sender and receiver. Meaningful values are `4` and `6`.
#[deku(bits = "4")]
ipvn: u8,
/// Used to inform Server to act as sender in TWAMP-Test.
///
/// In TWAMP, it is always set to 0.
conf_sender: u8,
/// Used to inform Server to act as receiver in TWAMP-Test.
///
/// In TWAMP, it is always set to 0.
conf_receiver: u8,
/// Used by Control-Client to determine when to send test packets.
///
/// Must be zero in TWAMP as Session-Reflector does not process incoming packets and only
/// reflects, and does not require this info.
number_of_schedule_slots: u32,
/// Number of active measurement packets to be sent during TWAMP-Session.
///
/// Must be zero as Session-Reflector does not process incoming packets, therefore does not
/// need to know the number of packets.
number_of_packets: u32,
/// UDP port on which Session-Sender will send from and receive TWAMP-Test packets.
pub sender_port: u16,
/// UDP port on which Session-Sender will send TWAMP-Test to and from where Session-Reflector
/// will reflect packets.
pub receiver_port: u16,
/// IP address of sender. Can be set to 0 in which case the IP of Control-Client will be used.
pub sender_address: Ipv4Addr,
/// Utilised if [IPVN](Self::ipvn) is `6` otherwise is MBZ (Must Be Zero).
sender_address_cont: [u8; 12],
/// IP address of receiver. Can be set to 0 in which case the IP of Server will be used.
pub receiver_address: Ipv4Addr,
/// Utilised if [IPVN](Self::ipvn) is `6` otherwise is MBZ (Must Be Zero).
receiver_address_cont: [u8; 12],
/// Session Identifier. Must be 0 since it's generated on receiving side.
sid: u128,
/// Number of bytes to append to normal TWAMP-Test packet.
pub padding_length: u32,
/// Time when the session should be started. Cannot be before the time Start-Sessions is
/// issued.
pub start_time: TimeStamp,
/// From [RFC 5357](https://datatracker.ietf.org/doc/html/rfc5357/#section-3.5):
///
/// Timeout is the interval that the Session-Reflector MUST wait after receiving a
/// Stop-Sessions message. In case there are test packets still in transit, the
/// Session-Reflector MUST reflect them if they arrive within the Timeout interval following
/// the reception of the Stop-Sessions message. The Session-Reflector MUST NOT reflect packets
/// that are received beyond the timeout.
pub timeout: u64,
/// Set [DSCP](https://datatracker.ietf.org/doc/html/rfc2474).
///
/// If present, the same value **must** be used in TWAMP-Test packets.
type_p_descriptor: u32,
octets_to_be_reflected: u16,
length_of_padding_to_reflect: u16,
/// MBZ (Must Be Zero).
#[deku(assert_eq = "0u32")]
mbz_last: u32,
hmac: [u8; 16],
}
impl RequestTwSession {
pub fn new(
sender_address: Ipv4Addr,
sender_port: u16,
receiver_address: Ipv4Addr,
receiver_port: u16,
start_time: Option<TimeStamp>,
timeout: u64,
) -> Self {
RequestTwSession {
command_number: CommandNumber::RequestTwSession,
mbz_first: 0, // Must be zero.
ipvn: 4,
conf_sender: 0, // Must be zero.
conf_receiver: 0, // Must be zero.
number_of_schedule_slots: 0, // Must be zero.
number_of_packets: 0, // Must be zero.
sender_port,
receiver_port,
sender_address,
sender_address_cont: [0; 12],
receiver_address,
receiver_address_cont: [0; 12],
sid: 0, // Must be zero.
padding_length: 0,
start_time: start_time.unwrap_or_default(),
timeout,
type_p_descriptor: 0,
octets_to_be_reflected: 0,
length_of_padding_to_reflect: 0,
mbz_last: 0, // Must be zero.
hmac: [0; 16],
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const REQUEST_TW_SESSION_LENGTH_IN_BYTES: usize = 112;
#[test]
fn command_number_is_correct() {
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
0,
Ipv4Addr::new(127, 0, 0, 1),
0,
None,
900,
);
assert_eq!(
request_tw_session.command_number,
CommandNumber::RequestTwSession
);
}
#[test]
fn first_mbz_are_zeros() {
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
0,
Ipv4Addr::new(127, 0, 0, 1),
0,
None,
900,
);
assert_eq!(request_tw_session.mbz_first, 0u8);
}
/// Only support IPv4 for now.
#[test]
fn ipvn_is_correct() {
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
0,
Ipv4Addr::new(127, 0, 0, 1),
0,
None,
900,
);
assert_eq!(request_tw_session.ipvn, 4u8);
}
#[test]
fn conf_sender_is_zero() {
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
0,
Ipv4Addr::new(127, 0, 0, 1),
0,
None,
900,
);
assert_eq!(request_tw_session.conf_sender, 0u8);
}
#[test]
fn conf_receiver_is_zero() {
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
0,
Ipv4Addr::new(127, 0, 0, 1),
0,
None,
900,
);
assert_eq!(request_tw_session.conf_receiver, 0u8);
}
#[test]
fn number_of_schedule_slots_is_zero() {
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
0,
Ipv4Addr::new(127, 0, 0, 1),
0,
None,
900,
);
assert_eq!(request_tw_session.number_of_schedule_slots, 0u32);
}
#[test]
fn number_of_packets_is_zero() {
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
0,
Ipv4Addr::new(127, 0, 0, 1),
0,
None,
900,
);
assert_eq!(request_tw_session.number_of_packets, 0u32);
}
#[test]
fn sender_port_is_assigned() {
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
12345,
Ipv4Addr::new(127, 0, 0, 1),
0,
None,
900,
);
assert_eq!(request_tw_session.sender_port, 12345);
}
#[test]
fn receiver_port_is_assigned() {
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
0,
Ipv4Addr::new(127, 0, 0, 1),
12345,
None,
900,
);
assert_eq!(request_tw_session.receiver_port, 12345);
}
#[test]
fn sender_address_is_assigned() {
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
0,
Ipv4Addr::new(127, 0, 0, 1),
0,
None,
900,
);
assert_eq!(
request_tw_session.sender_address,
Ipv4Addr::new(127, 0, 0, 1)
);
}
/// Supporting only Ipv4 so this should be MBZ.
#[test]
fn sender_address_cont_is_mbz() {
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
0,
Ipv4Addr::new(127, 0, 0, 1),
0,
None,
900,
);
assert_eq!(request_tw_session.sender_address_cont, [0; 12]);
}
#[test]
fn receiver_address_is_assigned() {
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
0,
Ipv4Addr::new(127, 0, 0, 1),
0,
None,
900,
);
assert_eq!(
request_tw_session.receiver_address,
Ipv4Addr::new(127, 0, 0, 1)
);
}
/// Supporting only Ipv4 so this should be MBZ.
#[test]
fn receiver_address_cont_is_mbz() {
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
0,
Ipv4Addr::new(127, 0, 0, 1),
0,
None,
900,
);
assert_eq!(request_tw_session.receiver_address_cont, [0; 12]);
}
#[test]
fn sid_is_zero() {
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
0,
Ipv4Addr::new(127, 0, 0, 1),
0,
None,
900,
);
assert_eq!(request_tw_session.sid, 0);
}
#[test]
#[ignore]
fn padding_length_is_assigned() {
todo!();
}
#[test]
fn start_time_is_assigned() {
let timestamp = TimeStamp::default();
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
0,
Ipv4Addr::new(127, 0, 0, 1),
0,
Some(timestamp),
900,
);
assert_eq!(request_tw_session.start_time, timestamp);
}
#[test]
#[ignore]
fn timeout_is_assigned() {
todo!();
}
#[test]
#[ignore]
fn type_p_descriptor_is_assigned() {
todo!();
}
#[test]
#[ignore]
fn octets_to_be_reflected_is_assigned() {
todo!();
}
#[test]
#[ignore]
fn length_of_padding_to_reflect_is_assigned() {
todo!();
}
#[test]
fn last_mbz_are_zeros() {
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
0,
Ipv4Addr::new(127, 0, 0, 1),
0,
None,
900,
);
assert_eq!(request_tw_session.mbz_last, 0);
}
#[test]
#[ignore]
fn hmac_is_assigned() {
todo!();
}
#[test]
fn struct_serialized_has_size_acc_to_rfc() {
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
0,
Ipv4Addr::new(127, 0, 0, 1),
0,
None,
900,
);
let encoded = request_tw_session.to_bytes().unwrap();
assert_eq!(encoded.len(), REQUEST_TW_SESSION_LENGTH_IN_BYTES)
}
#[test]
fn deserialize_to_struct() {
let request_tw_session = RequestTwSession::new(
Ipv4Addr::new(127, 0, 0, 1),
0,
Ipv4Addr::new(127, 0, 0, 1),
0,
None,
900,
);
let encoded = request_tw_session.to_bytes().unwrap();
let (_rest, val) = RequestTwSession::from_bytes((&encoded, 0)).unwrap();
assert_eq!(val, request_tw_session)
}
}