internet 0.0.3

Network library for rust
Documentation
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
//! Encoding for IPv4 options following [RFC 791].
//!
//! [IETF RFC 791]: https://datatracker.ietf.org/doc/html/rfc791

use crate::{Buf, BufError, BufMut, BufResult, Codec, Cursor};

/// IPv4 options.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Ipv4Option<'a> {
    /// End of Option List (Type 0).
    EndOfOptionList(EndOfOptionList),
    /// No-Operation (Type 1).
    NoOperation(NoOperation),
    /// Security (Type 130).
    Security(Security<'a>),
    /// Loose Source Routing (Type 131).
    LooseSourceRoute(LooseSourceRoute<'a>),
    /// Strict Source Routing (Type 137).
    StrictSourceRoute(StrictSourceRoute<'a>),
    /// Record Route (Type 7).
    RecordRoute(RecordRoute<'a>),
    /// Stream ID (Type 136).
    StreamId(StreamId),
    /// Internet Timestamp (Type 68).
    InternetTimestamp(InternetTimestamp<'a>),
    /// Router Alert (Type 148).
    RouterAlert(RouterAlert),
}

impl<'a> Codec for Ipv4Option<'a> {
    fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
        match self {
            Ipv4Option::EndOfOptionList(opt) => opt.encode(writer, ()),
            Ipv4Option::NoOperation(opt) => opt.encode(writer, ()),
            Ipv4Option::Security(opt) => opt.encode(writer, ()),
            Ipv4Option::LooseSourceRoute(opt) => opt.encode(writer, ()),
            Ipv4Option::StrictSourceRoute(opt) => opt.encode(writer, ()),
            Ipv4Option::RecordRoute(opt) => opt.encode(writer, ()),
            Ipv4Option::StreamId(opt) => opt.encode(writer, ()),
            Ipv4Option::InternetTimestamp(opt) => opt.encode(writer, ()),
            Ipv4Option::RouterAlert(opt) => opt.encode(writer, ()),
        }
    }

    fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
        let typ_peek = reader.peek_u8()?;

        match typ_peek {
            0 => Ok(Self::EndOfOptionList(EndOfOptionList::decode(reader, ())?)),
            1 => Ok(Self::NoOperation(NoOperation::decode(reader, ())?)),
            7 => Ok(Self::RecordRoute(RecordRoute::decode(reader, ())?)),
            68 => Ok(Self::InternetTimestamp(InternetTimestamp::decode(
                reader,
                (),
            )?)),
            130 => Ok(Self::Security(Security::decode(reader, ())?)),
            131 => Ok(Self::LooseSourceRoute(LooseSourceRoute::decode(
                reader,
                (),
            )?)),
            136 => Ok(Self::StreamId(StreamId::decode(reader, ())?)),
            137 => Ok(Self::StrictSourceRoute(StrictSourceRoute::decode(
                reader,
                (),
            )?)),
            148 => Ok(Self::RouterAlert(RouterAlert::decode(reader, ())?)),
            _ => Err(BufError::UnexpectedValue),
        }
    }
}

///
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum Type {
    /// End of Option List (EOOL).
    EndOfOptionList = 0,
    /// No-Operation (NOP).
    NoOperation = 1,
    /// Record Route (RR).
    RecordRoute = 7,
    /// Internet Timestamp (TS).
    InternetTimestamp = 68,
    /// Security (SEC).
    Security = 130,
    /// Loose Source Routing (LSRR).
    LooseSourceRoute = 131,
    /// Stream ID (SID).
    StreamId = 136,
    /// Strict Source Routing (SSRR).
    StrictSourceRoute = 137,
    /// Router Alert (RA) [RFC 2113].
    RouterAlert = 148,
}

impl Codec for Type {
    fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
        (*self as u8).encode(writer, ())
    }

    fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
        match u8::decode(reader, ())? {
            x if x == Self::EndOfOptionList as u8 => Ok(Self::EndOfOptionList),
            x if x == Self::NoOperation as u8 => Ok(Self::NoOperation),
            x if x == Self::RecordRoute as u8 => Ok(Self::RecordRoute),
            x if x == Self::InternetTimestamp as u8 => Ok(Self::InternetTimestamp),
            x if x == Self::Security as u8 => Ok(Self::Security),
            x if x == Self::LooseSourceRoute as u8 => Ok(Self::LooseSourceRoute),
            x if x == Self::StreamId as u8 => Ok(Self::StreamId),
            x if x == Self::StrictSourceRoute as u8 => Ok(Self::StrictSourceRoute),
            x if x == Self::RouterAlert as u8 => Ok(Self::RouterAlert),
            _ => Err(BufError::UnexpectedValue),
        }
    }
}

/// End of Option List (EOOL)
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EndOfOptionList;

impl EndOfOptionList {
    /// The Type
    pub const TYPE: Type = Type::EndOfOptionList;
}

impl Codec for EndOfOptionList {
    fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
        Self::TYPE.encode(writer, ())
    }

    fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
        if Type::decode(reader, ())? != Self::TYPE {
            return Err(BufError::UnexpectedValue);
        }
        Ok(Self)
    }
}

/// No-Operation (NOP)
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NoOperation;

impl NoOperation {
    ///
    pub const TYPE: Type = Type::NoOperation;
}

impl Codec for NoOperation {
    fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
        Self::TYPE.encode(writer, ())
    }

    fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
        if Type::decode(reader, ())? != Self::TYPE {
            return Err(BufError::UnexpectedValue);
        }
        Ok(Self)
    }
}

/// Router Alert
///
/// ...
///
/// [RFC 2113]: https://datatracker.ietf.org/doc/html/rfc2113
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RouterAlert {
    /// .
    pub value: u16,
}

impl RouterAlert {
    /// The Type.
    pub const TYPE: Type = Type::RouterAlert;
}

impl Codec for RouterAlert {
    fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
        Self::TYPE.encode(writer, ())?;
        4u8.encode(writer, ())?; // length
        self.value.encode(writer, ())
    }

    fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
        if Type::decode(reader, ())? != Self::TYPE {
            return Err(BufError::UnexpectedValue);
        }
        let _length = u8::decode(reader, ())?;
        Ok(Self {
            value: u16::decode(reader, ())?,
        })
    }
}

/// Record Route (RR)
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RecordRoute<'a> {
    ///
    pub pointer: u8,
    ///
    pub route_data: &'a [u8],
}

impl<'a> RecordRoute<'a> {
    ///
    pub const TYPE: Type = Type::RecordRoute;

    fn encoded_len(&self) -> usize {
        3 + self.route_data.len() // 1 (kind) + 1 (length) + 1 (pointer) + data
    }
}

impl<'a> Codec for RecordRoute<'a> {
    fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
        Self::TYPE.encode(writer, ())?;
        (self.encoded_len() as u8).encode(writer, ())?;
        self.pointer.encode(writer, ())?;

        todo!("Encode variable length slice: self.route_data");
    }

    fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
        if Type::decode(reader, ())? != Self::TYPE {
            return Err(BufError::UnexpectedValue);
        }
        let length = u8::decode(reader, ())?;
        let _pointer = u8::decode(reader, ())?;
        let _data_len = length.saturating_sub(3);

        todo!("Decode variable length slice of length: _data_len into route_data");
    }
}

/// Loose Source Routing (LSRR)
///
/// ...
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LooseSourceRoute<'a> {
    ///
    pub pointer: u8,
    ///
    pub route_data: &'a [u8],
}

impl<'a> LooseSourceRoute<'a> {
    ///
    pub const TYPE: Type = Type::LooseSourceRoute;

    fn encoded_len(&self) -> usize {
        3 + self.route_data.len()
    }
}

impl<'a> Codec for LooseSourceRoute<'a> {
    fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
        Self::TYPE.encode(writer, ())?;
        (self.encoded_len() as u8).encode(writer, ())?;
        self.pointer.encode(writer, ())?;

        todo!("Encode variable length slice: self.route_data");
    }

    fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
        if Type::decode(reader, ())? != Self::TYPE {
            return Err(BufError::UnexpectedValue);
        }
        let length = u8::decode(reader, ())?;
        let _pointer = u8::decode(reader, ())?;
        let _data_len = length.saturating_sub(3);

        todo!("Decode variable length slice of length: _data_len into route_data");
    }
}

/// Strict Source Routing (SSRR)
///
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct StrictSourceRoute<'a> {
    ///
    pub pointer: u8,
    ///
    pub route_data: &'a [u8],
}

impl<'a> StrictSourceRoute<'a> {
    ///
    pub const TYPE: Type = Type::StrictSourceRoute;

    fn encoded_len(&self) -> usize {
        3 + self.route_data.len()
    }
}

impl<'a> Codec for StrictSourceRoute<'a> {
    fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
        Self::TYPE.encode(writer, ())?;
        (self.encoded_len() as u8).encode(writer, ())?;
        self.pointer.encode(writer, ())?;

        todo!("Encode variable length slice: self.route_data");
    }

    fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
        if Type::decode(reader, ())? != Self::TYPE {
            return Err(BufError::UnexpectedValue);
        }
        let length = u8::decode(reader, ())?;
        let _pointer = u8::decode(reader, ())?;
        let _data_len = length.saturating_sub(3);

        todo!("Decode variable length slice of length: _data_len into route_data");
    }
}

/// Internet Timestamp (TS)
///
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct InternetTimestamp<'a> {
    ///
    pub pointer: u8,
    ///
    pub overflow_and_flags: u8,
    ///
    pub data: &'a [u8],
}

impl<'a> InternetTimestamp<'a> {
    ///
    pub const TYPE: Type = Type::InternetTimestamp;

    fn encoded_len(&self) -> usize {
        4 + self.data.len() // 1 (kind) + 1 (length) + 1 (pointer) + 1 (oflw+flg) + data
    }
}

impl<'a> Codec for InternetTimestamp<'a> {
    fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
        Self::TYPE.encode(writer, ())?;
        (self.encoded_len() as u8).encode(writer, ())?;
        self.pointer.encode(writer, ())?;
        self.overflow_and_flags.encode(writer, ())?;

        todo!("Encode variable length slice: self.data");
    }

    fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
        if Type::decode(reader, ())? != Self::TYPE {
            return Err(BufError::UnexpectedValue);
        }
        let length = u8::decode(reader, ())?;
        let _pointer = u8::decode(reader, ())?;
        let _overflow_and_flags = u8::decode(reader, ())?;
        let _data_len = length.saturating_sub(4);

        todo!("Decode variable length slice of length: _data_len into data");
    }
}

/// Stream ID (SID)
///
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct StreamId {
    ///
    pub id: u16,
}

impl StreamId {
    ///
    pub const TYPE: Type = Type::StreamId;
}

impl Codec for StreamId {
    fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
        Self::TYPE.encode(writer, ())?;
        4u8.encode(writer, ())?; // length (must be 4)
        self.id.encode(writer, ())
    }

    fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
        if Type::decode(reader, ())? != Self::TYPE {
            return Err(BufError::UnexpectedValue);
        }
        let _length = u8::decode(reader, ())?;
        Ok(Self {
            id: u16::decode(reader, ())?,
        })
    }
}

/// Security (SEC)
///
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Security<'a> {
    ///
    pub data: &'a [u8],
}

impl<'a> Security<'a> {
    ///
    pub const TYPE: Type = Type::Security;

    fn encoded_len(&self) -> usize {
        2 + self.data.len()
    }
}

impl<'a> Codec for Security<'a> {
    fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
        Self::TYPE.encode(writer, ())?;
        (self.encoded_len() as u8).encode(writer, ())?;

        todo!("Encode variable length slice: self.data");
    }

    fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
        if Type::decode(reader, ())? != Self::TYPE {
            return Err(BufError::UnexpectedValue);
        }
        let length = u8::decode(reader, ())?;
        let _data_len = length.saturating_sub(2);

        todo!("Decode variable length slice of length: _data_len into data");
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test() {}
}