ipsuite 0.0.2

[WIP] Collection of Types for the Internet Protocol Suite
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
use core::{fmt, mem};

use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned, network_endian};

use super::{ChecksumWords, DataDebug};

#[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)]
#[repr(C, packed)]
pub struct Ipv4Pdu {
    pub fields: Ipv4HeaderFields,
    pub options_payload: [u8],
}

impl Ipv4Pdu {
    #[inline]
    pub fn from_bytes(buf: &[u8]) -> Result<&Self, Ipv4PduError> {
        Ipv4Pdu::ref_from_bytes(buf)
            .map_err(zerocopy::SizeError::from)
            .map_err(Into::into)
    }

    #[inline]
    pub fn from_bytes_mut(buf: &mut [u8]) -> Result<&mut Self, Ipv4PduError> {
        Ipv4Pdu::mut_from_bytes(buf)
            .map_err(zerocopy::SizeError::from)
            .map_err(Into::into)
    }

    #[inline]
    pub fn as_parts(&self) -> Result<(&Ipv4Header, &[u8]), Ipv4PduError> {
        let len = self.fields.header_length();
        let buf = self.as_bytes();
        if len < Ipv4HeaderFields::SIZE {
            return Err(Ipv4PduError::InvalidHeaderLength);
        }
        let (header, payload) = buf
            .split_at_checked(len)
            .ok_or(Ipv4PduError::InvalidHeaderLength)?;

        Ok((
            Ipv4Header::ref_from_bytes(header).map_err(zerocopy::SizeError::from)?,
            payload,
        ))
    }

    // XXX: split again?
    #[inline]
    pub fn as_mut_parts(
        &mut self,
        options: usize,
    ) -> Result<(&mut Ipv4Header, &mut [u8]), Ipv4PduError> {
        let buf = self.as_mut_bytes();
        let (header, payload) = buf
            .split_at_mut_checked(Ipv4HeaderFields::SIZE.saturating_add(options))
            .ok_or(Ipv4PduError::InvalidHeaderLength)?;
        Ok((
            Ipv4Header::mut_from_bytes(header).map_err(zerocopy::SizeError::from)?,
            payload,
        ))
    }
}

impl fmt::Debug for Ipv4Pdu {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Ipv4Pdu")
            .field("fields", &self.fields)
            // TODO: debug print options
            .field("options_payload", &DataDebug(&self.options_payload))
            .finish()
    }
}

#[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)]
#[repr(C, packed)]
pub struct Ipv4Header {
    pub fields: Ipv4HeaderFields,
    pub options: [u8],
}

impl Ipv4Header {
    #[inline]
    #[must_use]
    pub const fn length(&self) -> usize {
        mem::size_of_val(&self.fields).wrapping_add(self.options.len())
    }

    #[inline]
    pub fn update_checksum(&mut self) -> Result<(), Ipv4PduError> {
        self.as_mut_words()?.update_checksum(0);
        Ok(())
    }

    #[inline]
    pub fn verify_checksum(&self) -> Result<(), Ipv4PduError> {
        self.as_words()?
            .verify_checksum(0)
            .map_err(|()| Ipv4PduError::InvalidChecksum)
    }

    #[inline]
    pub fn pseudo_header(&self) -> Result<&Ipv4PseudoHeader, Ipv4PduError> {
        // TODO: half word at end
        Ipv4PseudoHeader::ref_from_bytes(self.as_bytes())
            .map_err(zerocopy::SizeError::from)
            .map_err(Into::into)
    }

    fn as_words(&self) -> Result<&Ipv4HeaderWords, Ipv4PduError> {
        // TODO: half word at end
        Ipv4HeaderWords::ref_from_bytes(self.as_bytes())
            .map_err(zerocopy::SizeError::from)
            .map_err(Into::into)
    }

    fn as_mut_words(&mut self) -> Result<&mut Ipv4HeaderWords, Ipv4PduError> {
        // TODO: half word at end
        Ipv4HeaderWords::mut_from_bytes(self.as_mut_bytes())
            .map_err(zerocopy::SizeError::from)
            .map_err(Into::into)
    }
}

impl fmt::Debug for Ipv4Header {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Ipv4Header")
            .field("fields", &self.fields)
            // TODO: debug print options
            .field("options", &format_args!("{:x?}", &self.options))
            .finish()
    }
}

#[derive(Copy, Clone, Debug, FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)]
#[repr(C, packed)]
pub struct Ipv4HeaderFields {
    pub version_ihl: u8,
    pub dscp_ecn: u8,
    pub total_length: network_endian::U16,
    pub identification: network_endian::U16,
    pub fragmentation: Fragmentation,
    pub ttl: u8,
    pub protocol: InetProtocol,
    pub checksum: network_endian::U16,
    pub saddr: Ipv4Address,
    pub daddr: Ipv4Address,
}

impl Default for Ipv4HeaderFields {
    #[inline]
    fn default() -> Self {
        Ipv4HeaderFields {
            version_ihl: 0x45,
            dscp_ecn: 0,
            total_length: 20.into(),
            identification: 0.into(),
            fragmentation: Fragmentation::default(),
            ttl: 255,
            protocol: InetProtocol::TCP,
            checksum: 0.into(),
            saddr: Ipv4Address::UNSPECIFIED,
            daddr: Ipv4Address::UNSPECIFIED,
        }
    }
}

impl fmt::Display for Ipv4HeaderFields {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "IPv4: len={} id={} frag={}/{} ttl={} protocol={} checksum={:04x} saddr={} daddr={}",
            self.total_length,
            self.identification,
            self.fragmentation.flags(),
            self.fragmentation.offset(),
            self.ttl,
            self.protocol,
            self.checksum,
            self.saddr,
            self.daddr,
        )
    }
}

impl Ipv4HeaderFields {
    pub const SIZE: usize = mem::size_of::<Self>();
    pub const WORDS: usize = Self::SIZE >> 1;

    #[inline]
    pub const fn set_version(&mut self, version: u8) {
        self.version_ihl = (self.version_ihl & 0b0000_1111) | ((version & 0b1111) << 4);
    }

    #[inline]
    pub const fn set_ihl(&mut self, ihl: u8) {
        self.version_ihl = (self.version_ihl & 0b1111_0000) | (ihl & 0b1111);
    }

    #[inline]
    #[must_use]
    pub const fn version(&self) -> u8 {
        (self.version_ihl >> 4) & 0b1111
    }

    #[inline]
    #[must_use]
    pub const fn ihl(&self) -> u8 {
        self.version_ihl & 0b1111
    }

    #[inline]
    #[must_use]
    #[allow(clippy::as_conversions, reason = "unsigned to usize")]
    pub const fn header_length(&self) -> usize {
        (self.ihl() as usize).wrapping_mul(4)
    }

    #[inline]
    #[must_use]
    #[allow(clippy::as_conversions, reason = "unsigned to usize")]
    pub const fn packet_length(&self) -> usize {
        self.total_length.get() as usize
    }
}

#[derive(
    Copy,
    Clone,
    Debug,
    FromBytes,
    IntoBytes,
    KnownLayout,
    Immutable,
    Unaligned,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
)]
#[repr(transparent)]
pub struct Ipv4Address(pub [u8; 4]);

impl Ipv4Address {
    pub const UNSPECIFIED: Self = Self([0, 0, 0, 0]);

    #[must_use]
    #[inline]
    pub const fn into_std(self) -> core::net::Ipv4Addr {
        let [a, b, c, d] = self.0;
        core::net::Ipv4Addr::new(a, b, c, d)
    }

    #[must_use]
    #[inline]
    pub const fn from_std(addr: core::net::Ipv4Addr) -> Self {
        Ipv4Address(addr.octets())
    }
}

impl From<core::net::Ipv4Addr> for Ipv4Address {
    #[inline]
    fn from(addr: core::net::Ipv4Addr) -> Self {
        Ipv4Address::from_std(addr)
    }
}

impl From<&core::net::Ipv4Addr> for Ipv4Address {
    #[inline]
    fn from(addr: &core::net::Ipv4Addr) -> Self {
        Ipv4Address::from_std(*addr)
    }
}

impl From<Ipv4Address> for core::net::Ipv4Addr {
    #[inline]
    fn from(addr: Ipv4Address) -> Self {
        addr.into_std()
    }
}

impl From<&Ipv4Address> for core::net::Ipv4Addr {
    #[inline]
    fn from(addr: &Ipv4Address) -> Self {
        addr.into_std()
    }
}

impl fmt::Display for Ipv4Address {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let [a, b, c, d] = self.0;
        write!(f, "{a}.{b}.{c}.{d}")
    }
}

#[derive(
    Copy,
    Clone,
    Debug,
    FromBytes,
    IntoBytes,
    KnownLayout,
    Immutable,
    Unaligned,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
)]
#[repr(transparent)]
pub struct InetProtocol(pub u8);

impl InetProtocol {
    pub const ICMP: Self = Self(1);
    pub const IGMP: Self = Self(2);
    pub const TCP: Self = Self(6);
    pub const UDP: Self = Self(17);
    pub const ENCAP: Self = Self(41);
    pub const OSPF: Self = Self(89);
    pub const SCTP: Self = Self(132);

    #[inline]
    #[must_use]
    pub const fn name(self) -> Option<&'static str> {
        Some(match self {
            Self::ICMP => "ICMP",
            Self::IGMP => "IGMP",
            Self::TCP => "TCP",
            Self::UDP => "UDP",
            Self::ENCAP => "ENCAP",
            Self::OSPF => "OSPF",
            Self::SCTP => "SCTP",
            _ => return None,
        })
    }
}

impl fmt::Display for InetProtocol {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(name) = self.name() {
            f.write_str(name)
        } else {
            write!(f, "{}", self.0)
        }
    }
}

#[derive(
    Copy,
    Clone,
    Debug,
    Default,
    FromBytes,
    IntoBytes,
    KnownLayout,
    Immutable,
    Unaligned,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    Hash,
)]
#[repr(transparent)]
pub struct Fragmentation(pub network_endian::U16);

impl Fragmentation {
    #[inline]
    #[must_use]
    pub const fn flags(self) -> u16 {
        self.0.get() >> 13
    }

    #[inline]
    #[must_use]
    pub const fn dont_fragment(self) -> bool {
        self.flags() & 0b010 != 0
    }

    #[inline]
    #[must_use]
    pub const fn more_fragments(self) -> bool {
        self.flags() & 0b001 != 0
    }

    #[inline]
    #[must_use]
    #[allow(clippy::as_conversions, reason = "unsigned to usize")]
    pub const fn offset(self) -> usize {
        (self.0.get() & 0b1_1111_1111_1111) as usize
    }
}

type Ipv4HeaderWords = ChecksumWords<{ Ipv4HeaderFields::WORDS }, 5>;

// TODO: native byteorder
#[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)]
#[repr(C, packed)]
pub struct Ipv4PseudoHeader {
    version_ihl_dscp_ecn: network_endian::U16,
    total_length: network_endian::U16,
    _identification: network_endian::U16,
    _fragmentation: Fragmentation,
    ttl_protocol: network_endian::U16,
    _checksum: network_endian::U16,
    saddr: [network_endian::U16; 2],
    daddr: [network_endian::U16; 2],
    _options: [u8],
}

impl Ipv4PseudoHeader {
    #[allow(clippy::as_conversions, reason = "u16 to u32")]
    #[inline]
    #[must_use]
    pub const fn checksum(&self) -> u32 {
        let mut cs = 0u32;
        cs = cs.wrapping_add(self.saddr[0].get() as u32);
        cs = cs.wrapping_add(self.saddr[1].get() as u32);
        cs = cs.wrapping_add(self.daddr[0].get() as u32);
        cs = cs.wrapping_add(self.daddr[1].get() as u32);
        cs = cs.wrapping_add(self.protocol() as u32);
        cs.wrapping_add(self.payload_length() as u32)
    }

    const fn protocol(&self) -> u16 {
        self.ttl_protocol.get() & 0xff
    }

    const fn header_length(&self) -> u16 {
        ((self.version_ihl_dscp_ecn.get() & 0x0F00) >> 8).wrapping_mul(4)
    }

    const fn total_length(&self) -> u16 {
        self.total_length.get()
    }

    const fn payload_length(&self) -> u16 {
        self.total_length().wrapping_sub(self.header_length())
    }
}

impl fmt::Debug for Ipv4PseudoHeader {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Ipv4PseudoHeader")
            .field("saddr", &self.saddr)
            .field("daddr", &self.daddr)
            .field("proto", &self.protocol())
            .field("len", &self.payload_length())
            .finish()
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Ipv4PduError {
    InvalidHeaderLength,
    InvalidChecksum,
    BufferTooShort,
}

// TODO: sealed trait for T
impl<T: ?Sized> From<zerocopy::SizeError<&mut [u8], T>> for Ipv4PduError {
    #[inline]
    fn from(_err: zerocopy::SizeError<&mut [u8], T>) -> Self {
        Ipv4PduError::BufferTooShort
    }
}

impl<T: ?Sized> From<zerocopy::SizeError<&[u8], T>> for Ipv4PduError {
    #[inline]
    fn from(_err: zerocopy::SizeError<&[u8], T>) -> Self {
        Ipv4PduError::BufferTooShort
    }
}