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
use super::*;
use nom::combinator::rest_len;
use tox_binary_io::*;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OnionResponse3 {
pub onion_return: OnionReturn,
pub payload: InnerOnionResponse
}
impl FromBytes for OnionResponse3 {
named!(from_bytes<OnionResponse3>, do_parse!(
verify!(rest_len, |len| *len <= ONION_MAX_PACKET_SIZE) >>
tag!(&[0x8c][..]) >>
onion_return: flat_map!(take!(ONION_RETURN_3_SIZE), OnionReturn::from_bytes) >>
payload: call!(InnerOnionResponse::from_bytes) >>
(OnionResponse3 { onion_return, payload })
));
}
impl ToBytes for OnionResponse3 {
fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
do_gen!(buf,
gen_be_u8!(0x8c) >>
gen_call!(|buf, onion_return| OnionReturn::to_bytes(onion_return, buf), &self.onion_return) >>
gen_call!(|buf, payload| InnerOnionResponse::to_bytes(payload, buf), &self.payload) >>
gen_len_limit(ONION_MAX_PACKET_SIZE)
)
}
}
#[cfg(test)]
mod tests {
use super::*;
const ONION_RETURN_3_PAYLOAD_SIZE: usize = ONION_RETURN_3_SIZE - secretbox::NONCEBYTES;
encode_decode_test!(
tox_crypto::crypto_init().unwrap(),
onion_response_3_encode_decode,
OnionResponse3 {
onion_return: OnionReturn {
nonce: secretbox::gen_nonce(),
payload: vec![42; ONION_RETURN_3_PAYLOAD_SIZE]
},
payload: InnerOnionResponse::OnionAnnounceResponse(OnionAnnounceResponse {
sendback_data: 12345,
nonce: gen_nonce(),
payload: vec![42; 123]
})
}
);
}