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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
#[cfg(feature = "std")]
use crate::std_identity::Keystore;
#[cfg(not(feature = "std"))]
use crate::no_std_identity::Keystore;
use crate::{
anon_req::ClearAnonRequest, packet_content::PacketContent, request::ClearRequest,
response::ClearResponse, text::ClearText,
};
use bytes::{Buf, Bytes};
use tinyvec::ArrayVec;
#[derive(PartialEq, Clone, core::fmt::Debug)]
pub struct Packet {
pub route_type: RouteType,
pub version: PayloadVersion,
pub path: ArrayVec<[u16; 64]>,
pub transport: [u16; 2],
pub raw_content: Bytes,
pub content: PacketContent,
pub incomplete: bool,
}
impl core::str::FromStr for Packet {
type Err = hex::FromHexError;
fn from_str(hex_str: &str) -> Result<Self, Self::Err> {
let hex = hex::decode(hex_str)?;
Ok(Packet::from(Bytes::copy_from_slice(&hex)))
}
}
impl From<Bytes> for Packet {
fn from(bytes_in: Bytes) -> Self {
let mut bytes = bytes_in;
// This is the packet we'll build as we begin to parse
let mut packet = Packet::default();
if bytes.len() < 2 {
return packet;
}
let header = bytes.get_u8();
// Parse the header byte
packet.route_type = RouteType::from(header);
packet.version = PayloadVersion::from(header);
// Get the transport if it's provided
packet.transport = match packet.route_type {
RouteType::TransportFlood | RouteType::TransportDirect => {
// The packet isn't long enough to contain the transport
if bytes.len() < 4 {
return packet;
}
let t1 = bytes.get_u16_le();
let t2 = bytes.get_u16_le();
[t1, t2]
}
_ => [0, 0],
};
// Get the route
if bytes.is_empty() {
return packet;
}
let path_length = bytes.get_u8() as usize;
packet.path = match packet.version {
PayloadVersion::VersionOne => {
// The packet isn't long enough for the indicated route
if bytes.len() < path_length {
return packet;
}
let mut route = ArrayVec::new();
for _ in 0..path_length {
let path_element = bytes.get_u8() as u16;
route.push(path_element);
}
route
}
PayloadVersion::VersionTwo => {
// The packet isn't long enough for the indicated route
if bytes.len() < path_length * 2 {
return packet;
}
let mut route = ArrayVec::new();
for i in 0..path_length {
let path_element = bytes.get_u16();
route[i] = path_element;
}
route
}
_ => {
return packet;
}
};
// Get the rest of the payload and pass the parsing to the other structs
packet.raw_content = bytes.clone();
packet.content = PacketContent::new(header, bytes);
// Unfortunately, the trace packet is a bit weird.
// So, if this is a trace packet, move things back into the right place.
if let PacketContent::Trace(trace) = packet.content {
let mut trace = trace;
let path_snr = packet.path;
for (index, element) in trace.temp_path.iter().enumerate() {
packet.path[index] = *element as u16;
}
for u in path_snr.iter() {
let i = *u as i8;
let f = i as f32;
trace.path_snr.push(f / 4.0);
}
// DON'T delete the temp path, so Trace has access to the path
// when it prints the SNR results in its Display function.
packet.content = PacketContent::Trace(trace);
}
// Mark the packet as complete and valid
packet.incomplete = false;
packet
}
}
impl Default for Packet {
fn default() -> Self {
Packet {
route_type: RouteType::Invalid,
version: PayloadVersion::Invalid,
path: ArrayVec::new(),
transport: [0, 0],
raw_content: Bytes::new(),
content: PacketContent::Invalid,
incomplete: true,
}
}
}
impl core::fmt::Display for Packet {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Display::fmt(&self.route_type, f)?;
f.write_str(" | ")?;
core::fmt::Display::fmt(&self.version, f)?;
if self.route_type == RouteType::TransportDirect
|| self.route_type == RouteType::TransportFlood
{
f.write_fmt(format_args!(
" | {:4x?}, {:4x?} | ",
self.transport[0], self.transport[1]
))?
} else {
f.write_fmt(format_args!(" | |"))?
}
let len = self.path.len();
match self.path.len() {
0 => f.write_fmt(format_args!(" [] | ")),
1 => f.write_fmt(format_args!(" [{:02x?}] | ", self.path[0])),
2 => f.write_fmt(format_args!(
" [{:02x?}, {:02x?}] | ",
self.path[0], self.path[1]
)),
3 => f.write_fmt(format_args!(
" [{:02x?}, {:02x?}, {:02x?}] | ",
self.path[0], self.path[1], self.path[2]
)),
_ => f.write_fmt(format_args!(
" [{:02x?}, {:02x?}, ... {:02x?}, {:02x?}] | ",
self.path[0],
self.path[1],
self.path[len - 2],
self.path[len - 1]
)),
}?;
if self.incomplete {
f.write_str("x | ")
} else {
f.write_str(" | ")
}?;
core::fmt::Display::fmt(&self.content, f)
}
}
impl Packet {
#[cfg(feature = "std")]
pub fn try_decrypt(&mut self, keystore: &Keystore) -> bool {
match self.content {
// Encrypted packet types
PacketContent::Path(ref mut path) => path.cipher.try_decrypt(keystore),
PacketContent::Request(ref mut request) => {
let result = request.cipher.try_decrypt(keystore);
if let Some(cleartext) = &request.cipher.cleartext {
let cleartext = ClearRequest::from(cleartext.clone());
request.cleartext = Some(cleartext);
result
} else {
false
}
}
PacketContent::Response(ref mut response) => {
response.cipher.try_decrypt(keystore);
if let Some(cleartext) = &response.cipher.cleartext {
response.cleartext = Some(ClearResponse::from(cleartext.to_owned()));
true
} else {
false
}
}
PacketContent::Text(ref mut text) => {
text.cipher.try_decrypt(keystore);
if let Some(cleartext) = &text.cipher.cleartext {
text.cleartext = Some(ClearText::from(cleartext.to_owned()));
true
} else {
false
}
}
PacketContent::AnonReq(ref mut anon_req) => {
let decrypt_result = keystore.decrypt_anon(
anon_req.dest,
&anon_req.public_key,
anon_req.mac,
&anon_req.ciphertext,
);
if let Some(cleartext) = decrypt_result {
anon_req.request = Some(ClearAnonRequest::from(cleartext.0));
true
} else {
false
}
}
PacketContent::GroupText(ref mut group_text) => group_text.try_decrypt(keystore),
// None of the other packets implement any encryption
_ => false,
}
}
}
#[derive(PartialEq, Debug, Clone)]
pub enum RouteType {
TransportFlood,
Flood,
Direct,
TransportDirect,
Invalid,
}
impl core::fmt::Display for RouteType {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
RouteType::TransportFlood => f.write_str("T-Flood "),
RouteType::Flood => f.write_str(" Flood "),
RouteType::Direct => f.write_str(" Direct"),
RouteType::TransportDirect => f.write_str("T-Direct"),
RouteType::Invalid => f.write_str("INVALID "),
}
}
}
impl From<u8> for RouteType {
fn from(value: u8) -> Self {
match value & 0x03 {
0x00 => RouteType::TransportFlood,
0x01 => RouteType::Flood,
0x02 => RouteType::Direct,
0x03 => RouteType::TransportDirect,
_ => RouteType::Invalid,
}
}
}
#[derive(PartialEq, Debug, Clone)]
pub enum PayloadVersion {
VersionOne,
VersionTwo,
VersionThree,
VersionFour,
Invalid,
}
impl From<u8> for PayloadVersion {
fn from(value: u8) -> Self {
let value = (value & 0xC0) >> 6;
assert!(value < 4, "Programming error in masking and bit-shifting");
match value {
0x00 => PayloadVersion::VersionOne,
0x01 => PayloadVersion::VersionTwo,
0x02 => PayloadVersion::VersionThree,
0x03 => PayloadVersion::VersionFour,
_ => PayloadVersion::Invalid,
}
}
}
impl core::fmt::Display for PayloadVersion {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
PayloadVersion::VersionOne => f.write_str("v1"),
PayloadVersion::VersionTwo => f.write_str("v2"),
PayloadVersion::VersionThree => f.write_str("v3"),
PayloadVersion::VersionFour => f.write_str("v4"),
PayloadVersion::Invalid => f.write_str("xx"),
}
}
}
#[cfg(test)]
mod tests {
use core::str::FromStr;
use tinyvec::array_vec;
use super::*;
use crate::packet_content::{PacketContent, Raw};
#[test]
fn header_route_type() {
// The route type is the lowest-order two bits
assert_eq!(RouteType::TransportFlood, RouteType::from(0x00));
assert_eq!(RouteType::TransportFlood, RouteType::from(0xFC));
assert_eq!(RouteType::Flood, RouteType::from(0x01));
assert_eq!(RouteType::Flood, RouteType::from(0xFD));
assert_eq!(RouteType::Direct, RouteType::from(0x02));
assert_eq!(RouteType::Direct, RouteType::from(0xFE));
assert_eq!(RouteType::TransportDirect, RouteType::from(0x03));
assert_eq!(RouteType::TransportDirect, RouteType::from(0xFF));
assert_eq!(format!("{}", RouteType::TransportFlood), "T-Flood ");
assert_eq!(format!("{}", RouteType::Flood), " Flood ");
assert_eq!(format!("{}", RouteType::Direct), " Direct");
assert_eq!(format!("{}", RouteType::TransportDirect), "T-Direct");
assert_eq!(format!("{}", RouteType::Invalid), "INVALID ");
}
#[test]
fn header_version() {
assert_eq!(PayloadVersion::VersionOne, PayloadVersion::from(0x00));
assert_eq!(PayloadVersion::VersionOne, PayloadVersion::from(0x3F));
assert_eq!(PayloadVersion::VersionTwo, PayloadVersion::from(0x40));
assert_eq!(PayloadVersion::VersionTwo, PayloadVersion::from(0x7F));
assert_eq!(PayloadVersion::VersionThree, PayloadVersion::from(0x80));
assert_eq!(PayloadVersion::VersionThree, PayloadVersion::from(0xBF));
assert_eq!(PayloadVersion::VersionFour, PayloadVersion::from(0xC0));
assert_eq!(PayloadVersion::VersionFour, PayloadVersion::from(0xFF));
assert_eq!(format!("{}", PayloadVersion::VersionOne), "v1");
assert_eq!(format!("{}", PayloadVersion::VersionTwo), "v2");
assert_eq!(format!("{}", PayloadVersion::VersionThree), "v3");
assert_eq!(format!("{}", PayloadVersion::VersionFour), "v4");
assert_eq!(format!("{}", PayloadVersion::Invalid), "xx");
}
#[test]
fn packet() {
// Check the hex decode errors
assert_eq!(
Err(hex::FromHexError::InvalidHexCharacter { c: 's', index: 0 }),
Packet::from_str("s0")
);
assert_eq!(Err(hex::FromHexError::OddLength), Packet::from_str("0"));
// Check errors related to packet length issues
assert_eq!(Packet::default(), Packet::from_str("").unwrap());
let rhs_packet = Packet::from_str("01").unwrap();
assert_eq!(Packet::default(), rhs_packet);
// Packet not long enough to contain the provided path
let mut lhs_packet = Packet::default();
lhs_packet.route_type = RouteType::Flood;
lhs_packet.version = PayloadVersion::VersionOne;
let rhs_packet = Packet::from_str("0101").unwrap();
assert_eq!(lhs_packet, rhs_packet);
// Packet not long enough for transport
let mut lhs_packet = Packet::default();
lhs_packet.route_type = RouteType::TransportDirect;
lhs_packet.version = PayloadVersion::VersionOne;
let rhs_packet = Packet::from_str("0301").unwrap();
assert_eq!(lhs_packet, rhs_packet);
// Packet not long enough for version 2 path
let mut lhs_packet = Packet::default();
lhs_packet.route_type = RouteType::Direct;
lhs_packet.version = PayloadVersion::VersionTwo;
let rhs_packet = Packet::from_str("420102").unwrap();
assert_eq!(lhs_packet, rhs_packet);
// Ensure packet remainder is captured. This
// will test version 1 plus transport
let lhs_packet = Packet {
route_type: RouteType::TransportDirect,
version: PayloadVersion::VersionOne,
path: array_vec!([u16; 64] => 0x06, 0x07, 0x08, 0x09, 0x0A),
transport: [0x0102, 0x0304],
raw_content: Bytes::copy_from_slice(&[
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
]),
content: PacketContent::Raw(Raw {
bytes: Bytes::copy_from_slice(&[
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
]),
}),
incomplete: false,
};
let rhs_packet: Packet =
Packet::from_str("3F0201040305060708090A10111213141516171819").unwrap();
let compare_string = "T-Direct | v1 | 102, 304 | [06, 07, ... 09, 0a] | | RAW | Raw { bytes: b\"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\" }";
assert_eq!(format!("{}", rhs_packet), compare_string);
assert_eq!(lhs_packet, rhs_packet);
}
#[test]
fn packet_display() {
let mut packet = Packet {
route_type: RouteType::Direct,
version: PayloadVersion::VersionOne,
path: ArrayVec::new(),
transport: [0, 1],
raw_content: Bytes::new(),
content: PacketContent::Invalid,
incomplete: false,
};
assert_eq!(
format!("{}", packet),
" Direct | v1 | | [] | | INVALID | INVALID"
);
packet.path.push(0x01);
assert_eq!(
format!("{}", packet),
" Direct | v1 | | [01] | | INVALID | INVALID"
);
packet.path.push(0x02);
assert_eq!(
format!("{}", packet),
" Direct | v1 | | [01, 02] | | INVALID | INVALID"
);
packet.path.push(0xff);
assert_eq!(
format!("{}", packet),
" Direct | v1 | | [01, 02, ff] | | INVALID | INVALID"
);
packet.incomplete = true;
assert_eq!(
format!("{}", packet),
" Direct | v1 | | [01, 02, ff] | x | INVALID | INVALID"
);
}
}
|