Skip to main content

emissary_core/
protocol.rs

1// Permission is hereby granted, free of charge, to any person obtaining a
2// copy of this software and associated documentation files (the "Software"),
3// to deal in the Software without restriction, including without limitation
4// the rights to use, copy, modify, merge, publish, distribute, sublicense,
5// and/or sell copies of the Software, and to permit persons to whom the
6// Software is furnished to do so, subject to the following conditions:
7//
8// The above copyright notice and this permission notice shall be included in
9// all copies or substantial portions of the Software.
10//
11// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
16// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19/// Protocol type.
20#[derive(Debug, Copy, Clone, PartialEq, Eq)]
21pub enum Protocol {
22    /// Streaming protocol.
23    Streaming,
24
25    /// Repliable datagrams.
26    Datagram,
27
28    /// Repliable datagrams with replay prevention,
29    /// offline signature support and flags extensibility.
30    Datagram2,
31
32    /// Raw datagrams.
33    Anonymous,
34}
35
36impl Protocol {
37    /// Attempt to convert `protocol` into [`Protocol`].
38    pub fn from_u8(protocol: u8) -> Option<Self> {
39        match protocol {
40            6u8 => Some(Self::Streaming),
41            17u8 => Some(Self::Datagram),
42            19u8 => Some(Self::Datagram2),
43            18u8 => Some(Self::Anonymous),
44            _ => {
45                tracing::warn!(?protocol, "unknown i2cp protocol");
46                None
47            }
48        }
49    }
50
51    /// Serialize [`Protocol`].
52    pub fn as_u8(self) -> u8 {
53        match self {
54            Self::Streaming => 6u8,
55            Self::Datagram => 17u8,
56            Self::Datagram2 => 19u8,
57            Self::Anonymous => 18u8,
58        }
59    }
60}