tox_packet/dht/
mod.rs

1/*! Top-level DHT udp packets according
2    to [Tox spec](https://zetok.github.io/tox-spec/#packet-kind)
3*/
4
5#[cfg(test)]
6#[macro_use]
7mod macros;
8pub mod packed_node;
9mod ping_request;
10mod ping_response;
11mod nodes_request;
12mod nodes_response;
13mod dht_request;
14mod cookie_request;
15mod cookie_response;
16mod bootstrap_info;
17mod lan_discovery;
18mod crypto_handshake;
19mod crypto_data;
20mod cookie;
21mod errors;
22
23pub use self::ping_request::*;
24pub use self::ping_response::*;
25pub use self::nodes_request::*;
26pub use self::nodes_response::*;
27pub use self::dht_request::*;
28pub use self::cookie_request::*;
29pub use self::cookie_response::*;
30pub use self::bootstrap_info::*;
31pub use self::lan_discovery::*;
32pub use self::crypto_handshake::*;
33pub use self::crypto_data::*;
34pub use self::cookie::*;
35pub use self::errors::*;
36
37use nom::{alt, map};
38
39use nom::{
40    named, do_parse, tag, call, take, verify,  eof,
41};
42
43use cookie_factory::{
44    do_gen,
45    gen_slice,
46    gen_call,
47    gen_cond,
48    gen_be_u8,
49    gen_be_u16,
50    gen_be_u32,
51    gen_be_u64,
52    gen_many_ref
53};
54
55use tox_binary_io::*;
56use crate::onion::*;
57
58pub fn unix_time(time: std::time::SystemTime) -> u64 {
59    let since_the_epoch = time.duration_since(std::time::UNIX_EPOCH)
60        .expect("Current time is earlier than Unix epoch");
61
62    since_the_epoch.as_secs()
63}
64
65/// A serialized `Packet` should be not longer than 2048 bytes.
66pub const MAX_DHT_PACKET_SIZE: usize = 2048;
67
68/** DHT packet enum that encapsulates all types of DHT packets.
69*/
70#[derive(Clone, Debug, Eq, PartialEq)]
71pub enum Packet {
72    /// [`PingRequest`](./struct.PingRequest.html) structure.
73    PingRequest(PingRequest),
74    /// [`PingResponse`](./struct.PingResponse.html) structure.
75    PingResponse(PingResponse),
76    /// [`NodesRequest`](./struct.NodesRequest.html) structure.
77    NodesRequest(NodesRequest),
78    /// [`NodesResponse`](./struct.NodesResponse.html) structure.
79    NodesResponse(NodesResponse),
80    /// [`CookieRequest`](./struct.CookieRequest.html) structure.
81    CookieRequest(CookieRequest),
82    /// [`CookieRequest`](./struct.CookieRequest.html) structure.
83    CookieResponse(CookieResponse),
84    /// [`CryptoHandshake`](./struct.CryptoHandshake.html) structure.
85    CryptoHandshake(CryptoHandshake),
86    /// [`CryptoData`](./struct.CryptoData.html) structure.
87    CryptoData(CryptoData),
88    /// [`DhtRequest`](./struct.DhtRequest.html) structure.
89    DhtRequest(DhtRequest),
90    /// [`LanDiscovery`](./struct.LanDiscovery.html) structure.
91    LanDiscovery(LanDiscovery),
92    /// [`OnionRequest0`](../onion/struct.OnionRequest0.html) structure.
93    OnionRequest0(OnionRequest0),
94    /// [`OnionRequest1`](../onion/struct.OnionRequest1.html) structure.
95    OnionRequest1(OnionRequest1),
96    /// [`OnionRequest2`](../onion/struct.OnionRequest2.html) structure.
97    OnionRequest2(OnionRequest2),
98    /// [`OnionAnnounceRequest`](../onion/struct.OnionAnnounceRequest.html) structure.
99    OnionAnnounceRequest(OnionAnnounceRequest),
100    /// [`OnionAnnounceResponse`](../onion/struct.OnionAnnounceResponse.html) structure.
101    OnionAnnounceResponse(OnionAnnounceResponse),
102    /// [`OnionDataRequest`](../onion/struct.OnionDataRequest.html) structure.
103    OnionDataRequest(OnionDataRequest),
104    /// [`OnionDataResponse`](../onion/struct.OnionDataResponse.html) structure.
105    OnionDataResponse(OnionDataResponse),
106    /// [`OnionResponse3`](../onion/struct.OnionResponse3.html) structure.
107    OnionResponse3(OnionResponse3),
108    /// [`OnionResponse2`](../onion/struct.OnionResponse2.html) structure.
109    OnionResponse2(OnionResponse2),
110    /// [`OnionResponse1`](../onion/struct.OnionResponse1.html) structure.
111    OnionResponse1(OnionResponse1),
112    /// [`BootstrapInfo`](./struct.BootstrapInfo.html) structure.
113    BootstrapInfo(BootstrapInfo)
114}
115
116impl ToBytes for Packet {
117    fn to_bytes<'a>(&self, buf: (&'a mut [u8], usize)) -> Result<(&'a mut [u8], usize), GenError> {
118        match *self {
119            Packet::PingRequest(ref p) => p.to_bytes(buf),
120            Packet::PingResponse(ref p) => p.to_bytes(buf),
121            Packet::NodesRequest(ref p) => p.to_bytes(buf),
122            Packet::NodesResponse(ref p) => p.to_bytes(buf),
123            Packet::CookieRequest(ref p) => p.to_bytes(buf),
124            Packet::CookieResponse(ref p) => p.to_bytes(buf),
125            Packet::CryptoHandshake(ref p) => p.to_bytes(buf),
126            Packet::CryptoData(ref p) => p.to_bytes(buf),
127            Packet::DhtRequest(ref p) => p.to_bytes(buf),
128            Packet::LanDiscovery(ref p) => p.to_bytes(buf),
129            Packet::OnionRequest0(ref p) => p.to_bytes(buf),
130            Packet::OnionRequest1(ref p) => p.to_bytes(buf),
131            Packet::OnionRequest2(ref p) => p.to_bytes(buf),
132            Packet::OnionAnnounceRequest(ref p) => p.to_bytes(buf),
133            Packet::OnionAnnounceResponse(ref p) => p.to_bytes(buf),
134            Packet::OnionDataRequest(ref p) => p.to_bytes(buf),
135            Packet::OnionDataResponse(ref p) => p.to_bytes(buf),
136            Packet::OnionResponse3(ref p) => p.to_bytes(buf),
137            Packet::OnionResponse2(ref p) => p.to_bytes(buf),
138            Packet::OnionResponse1(ref p) => p.to_bytes(buf),
139            Packet::BootstrapInfo(ref p) => p.to_bytes(buf)
140        }
141    }
142}
143
144impl FromBytes for Packet {
145    named!(from_bytes<Packet>, alt!(
146        map!(PingRequest::from_bytes, Packet::PingRequest) |
147        map!(PingResponse::from_bytes, Packet::PingResponse) |
148        map!(NodesRequest::from_bytes, Packet::NodesRequest) |
149        map!(NodesResponse::from_bytes, Packet::NodesResponse) |
150        map!(CookieRequest::from_bytes, Packet::CookieRequest) |
151        map!(CookieResponse::from_bytes, Packet::CookieResponse) |
152        map!(CryptoHandshake::from_bytes, Packet::CryptoHandshake) |
153        map!(CryptoData::from_bytes, Packet::CryptoData) |
154        map!(DhtRequest::from_bytes, Packet::DhtRequest) |
155        map!(LanDiscovery::from_bytes, Packet::LanDiscovery) |
156        map!(OnionRequest0::from_bytes, Packet::OnionRequest0) |
157        map!(OnionRequest1::from_bytes, Packet::OnionRequest1) |
158        map!(OnionRequest2::from_bytes, Packet::OnionRequest2) |
159        map!(OnionAnnounceRequest::from_bytes, Packet::OnionAnnounceRequest) |
160        map!(OnionAnnounceResponse::from_bytes, Packet::OnionAnnounceResponse) |
161        map!(OnionDataRequest::from_bytes, Packet::OnionDataRequest) |
162        map!(OnionDataResponse::from_bytes, Packet::OnionDataResponse) |
163        map!(OnionResponse3::from_bytes, Packet::OnionResponse3) |
164        map!(OnionResponse2::from_bytes, Packet::OnionResponse2) |
165        map!(OnionResponse1::from_bytes, Packet::OnionResponse1) |
166        map!(BootstrapInfo::from_bytes, Packet::BootstrapInfo)
167    ));
168}