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
use super::*;
use rand::Rng;
use std::num::ParseIntError;
pub fn hexify(buf: &[u8]) -> String {
let mut hexstr = String::new();
for &b in buf {
hexstr.push_str(&format!("{:02x?}", b));
}
hexstr
}
pub fn unhexify(s: &str) -> Result<Vec<u8>, ParseIntError> {
(0..s.len())
.step_by(2)
.map(|i| u8::from_str_radix(&s[i..i + 2], 16))
.collect()
}
pub fn rnd_bundle(now: dtntime::CreationTimestamp) -> bundle::Bundle {
let mut rng = rand::thread_rng();
let dst_string = format!("node{}/inbox", rng.gen_range(1, 4));
let src_string = format!("node{}/inbox", rng.gen_range(1, 4));
let dst = eid::EndpointID::with_dtn(dst_string);
let src = eid::EndpointID::with_dtn(src_string);
let pblock = primary::PrimaryBlockBuilder::default()
.destination(dst)
.source(src.clone())
.report_to(src)
.creation_timestamp(now)
.lifetime(60 * 60 * 1_000_000)
.build()
.unwrap();
let mut b = bundle::BundleBuilder::default()
.primary(pblock)
.canonicals(vec![
canonical::new_payload_block(0, b"ABC".to_vec()),
canonical::new_bundle_age_block(1, 0, 0),
])
.build()
.unwrap();
b.set_crc(crc::CRC_16);
b.calculate_crc();
b
}