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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
use super::super::*;

use std::net::Ipv4Addr;
use std::fmt::{Debug, Formatter};

extern crate byteorder;
use self::byteorder::{ByteOrder, BigEndian, ReadBytesExt, WriteBytesExt};

///IPv4 header without options.
#[derive(Clone)]
pub struct Ipv4Header {
    pub differentiated_services_code_point: u8,
    pub explicit_congestion_notification: u8,
    ///Length of the payload of the ipv4 packet in bytes (does not contain the options).
    ///
    ///This field does not directly exist in an ipv4 header but instead is decoded from
    /// & encoded to the total_size field together with the options length (using the ihl).
    ///If the total_length field in a ipv4 header is smaller then 
    pub payload_len: u16,
    pub identification: u16,
    pub dont_fragment: bool,
    pub more_fragments: bool,
    pub fragments_offset: u16,
    pub time_to_live: u8,
    pub protocol: u8,
    pub header_checksum: u16,
    pub source: [u8;4],
    pub destination: [u8;4],
    ///Length of the options in the options_buffer in bytes.
    options_len: u8,
    options_buffer: [u8;40]
}

impl SerializedSize for Ipv4Header {
    ///Size of the header itself (without options) in bytes.
    const SERIALIZED_SIZE:usize = 20;
}

const IPV4_MAX_OPTIONS_LENGTH: usize = 10*4;

impl Ipv4Header {
    ///Constructs an Ipv4Header with standard values for non specified values.
    pub fn new(payload_len: u16, time_to_live: u8, protocol: IpTrafficClass, source: [u8;4], destination: [u8;4]) -> Ipv4Header {
        Ipv4Header {
            differentiated_services_code_point: 0,
            explicit_congestion_notification: 0,
            payload_len,
            identification: 0,
            dont_fragment: true,
            more_fragments: false,
            fragments_offset: 0,
            time_to_live,
            protocol: protocol as u8,
            header_checksum: 0,
            source,
            destination,
            options_len: 0,
            options_buffer: [0;40]
        }
    }

    ///Length of the header in 4 bytes (often also called IHL - Internet Header Lenght). 
    ///
    ///The minimum allowed length of a header is 5 (= 20 bytes) and the maximum length is 15 (= 60 bytes).
    pub fn ihl(&self) -> u8 {
        (self.options_len/4) + 5
    }

    ///Returns a slice to the options part of the header (empty if no options are present).
    pub fn options(&self) -> &[u8] {
        &self.options_buffer[..usize::from(self.options_len)]
    }

    ///Length of the header (includes options) in bytes.
    pub fn header_len(&self) -> usize {
        Ipv4Header::SERIALIZED_SIZE + usize::from(self.options_len)
    }

    ///Returns the total length of the header + payload in bytes.
    pub fn total_len(&self) -> u16 {
        self.payload_len + (Ipv4Header::SERIALIZED_SIZE as u16) + u16::from(self.options_len)
    }

    ///Sets the payload length if the value is not too big. Otherwise an error is returned.
    pub fn set_payload_len(&mut self, value: usize) -> Result<(), ValueError> {
        if usize::from(self.max_payload_len()) < value {
            use crate::ValueError::*;
            Err(Ipv4PayloadLengthTooLarge(value))
        } else {
            self.payload_len = value as u16;
            Ok(())
        }
    }

    ///Returns the maximum payload size based on the current options size.
    pub fn max_payload_len(&self) -> u16 {
        std::u16::MAX - u16::from(self.options_len) - (Ipv4Header::SERIALIZED_SIZE as u16)
    }

    ///Sets the options & header_length based on the provided length.
    ///The length of the given slice must be a multiple of 4 and maximum 40 bytes.
    ///If the length is not fullfilling these constraints, no data is set and
    ///an error is returned.
    pub fn set_options(&mut self, data: &[u8]) -> Result<(), ValueError> {
        use crate::ValueError::*;

        //check that the options length is within bounds
        if (IPV4_MAX_OPTIONS_LENGTH < data.len()) ||
           (0 != data.len() % 4)
        {
            Err(Ipv4OptionsLengthBad(data.len()))
        } else {
            //copy the data to the buffer
            self.options_buffer[..data.len()].copy_from_slice(data);

            //set the header length
            self.options_len = data.len() as u8;
            Ok(())
        }
    }

    ///Read an Ipv4Header from a slice and return the header & unused parts of the slice.
    pub fn read_from_slice(slice: &[u8]) -> Result<(Ipv4Header, &[u8]), ReadError> {
        let header = Ipv4HeaderSlice::from_slice(slice)?.to_header();
        let rest = &slice[header.header_len()..];
        Ok((
            header,
            rest
        ))
    }

    ///Reads an IPv4 header from the current position.
    pub fn read<T: io::Read + io::Seek + Sized>(reader: &mut T) -> Result<Ipv4Header, ReadError> {
        let value = reader.read_u8()?;
        let version = value >> 4;
        if 4 != version {
            return Err(ReadError::Ipv4UnexpectedVersion(version));
        }
        Ipv4Header::read_without_version(reader, value & 0xf)
    }

    ///Reads an IPv4 header assuming the version & ihl field have already been read.
    pub fn read_without_version<T: io::Read + io::Seek + Sized>(reader: &mut T, version_rest: u8) -> Result<Ipv4Header, ReadError> {
        let ihl = version_rest;
        if ihl < 5 {
            use crate::ReadError::*;
            return Err(Ipv4HeaderLengthBad(ihl));
        }
        let (dscp, ecn) = {
            let value = reader.read_u8()?;
            (value >> 2, value & 0x3)
        };
        let header_length = u16::from(ihl)*4;
        let total_length = reader.read_u16::<BigEndian>()?;
        if total_length < header_length {
            use crate::ReadError::*;
            return Err(Ipv4TotalLengthTooSmall(total_length));
        }
        let identification = reader.read_u16::<BigEndian>()?;
        let (dont_fragment, more_fragments, fragments_offset) = {
            let mut values: [u8; 2] = [0;2];
            reader.read_exact(&mut values)?;
            (0 != (values[0] & 0x40),
             0 != (values[0] & 0x20),
             {
                let buf = [values[0] & 0x1f, values[1]];
                let mut cursor = io::Cursor::new(&buf);
                cursor.read_u16::<BigEndian>()?
             })
        };
        Ok(Ipv4Header{
            differentiated_services_code_point: dscp,
            explicit_congestion_notification: ecn,
            payload_len: total_length - header_length,
            identification,
            dont_fragment,
            more_fragments,
            fragments_offset,
            time_to_live: reader.read_u8()?,
            protocol: reader.read_u8()?,
            header_checksum: reader.read_u16::<BigEndian>()?,
            source: {
                let mut values: [u8;4] = [0;4];
                reader.read_exact(&mut values)?;
                values
            },
            destination: {
                let mut values: [u8;4] = [0;4];
                reader.read_exact(&mut values)?;
                values
            },
            options_len: (ihl - 5)*4,
            options_buffer: {
                let mut values: [u8;40] = [0;40];
                
                let options_len = usize::from(ihl - 5)*4;
                if options_len > 0 {
                    reader.read_exact(&mut values[..options_len])?;
                }
                values
            },
        })
    }

    ///Checks if the values in this header are valid values for an ipv4 header.
    ///
    ///Specifically it will be checked, that:
    /// * payload_len + options_len is not too big to be encoded in the total_size header field
    /// * differentiated_services_code_point is not greater then 0x3f
    /// * explicit_congestion_notification is not greater then 0x3
    /// * fragments_offset is not greater then 0x1fff
    pub fn check_ranges(&self) -> Result<(), ValueError> {
        use crate::ErrorField::*;
        
        //check ranges
        max_check_u8(self.differentiated_services_code_point, 0x3f, Ipv4Dscp)?;
        max_check_u8(self.explicit_congestion_notification, 0x3, Ipv4Ecn)?;
        max_check_u16(self.fragments_offset, 0x1fff, Ipv4FragmentsOffset)?;
        max_check_u16(self.payload_len, self.max_payload_len(), Ipv4PayloadLength)?;

        Ok(())
    }

    ///Writes a given IPv4 header to the current position (this method automatically calculates the header length and checksum).
    pub fn write<T: io::Write + Sized>(&self, writer: &mut T) -> Result<(), WriteError> {
        //check ranges
        self.check_ranges()?;

        //write with recalculations
        self.write_ipv4_header_internal(writer, self.calc_header_checksum_unchecked())
    }

    ///Writes a given IPv4 header to the current position (this method just writes the specified checksum and does note compute it).
    pub fn write_raw<T: io::Write + Sized>(&self, writer: &mut T) -> Result<(), WriteError> {
        //check ranges
        self.check_ranges()?;

        //write
        self.write_ipv4_header_internal(writer, self.header_checksum)
    }

    ///Write the given header with the  checksum and header length specified in the seperate arguments
    fn write_ipv4_header_internal<T: io::Write>(&self, write: &mut T, header_checksum: u16) -> Result<(), WriteError> {
        //version & header_length
        write.write_u8((4 << 4) | self.ihl())?;

        //dscp & ecn        
        write.write_u8((self.differentiated_services_code_point << 2) | self.explicit_congestion_notification)?;

        //total length & id 
        write.write_u16::<BigEndian>(self.total_len())?;
        write.write_u16::<BigEndian>(self.identification)?;

        //flags & fragmentation offset
        {
            let mut buf: [u8;2] = [0;2];
            BigEndian::write_u16(&mut buf, self.fragments_offset);
            let flags = {
                let mut result = 0;
                if self.dont_fragment {
                    result |= 64;
                }
                if self.more_fragments {
                    result |= 32;
                }
                result
            };
            write.write_u8(
                flags |
                (buf[0] & 0x1f),
            )?;
            write.write_u8(
                buf[1]
            )?;
        }

        //rest
        write.write_u8(self.time_to_live)?;
        write.write_u8(self.protocol)?;
        write.write_u16::<BigEndian>(header_checksum)?;
        write.write_all(&self.source)?;
        write.write_all(&self.destination)?;

        //options
        write.write_all(&self.options())?;

        //done
        Ok(())
    }

    ///Calculate header checksum of the current ipv4 header.
    pub fn calc_header_checksum(&self) -> Result<u16, ValueError> {

        //check ranges
        self.check_ranges()?;

        //calculate the checksum
        Ok(self.calc_header_checksum_unchecked())
    }

    ///Calculate the header checksum under the assumtion that all value ranges in the header are correct
    fn calc_header_checksum_unchecked(&self) -> u16 {
        //version & header_length
        let mut sum: u32 = [
            BigEndian::read_u16(&[ (4 << 4) | self.ihl(),
                                (self.differentiated_services_code_point << 2) | self.explicit_congestion_notification ]),
            self.total_len(),
            self.identification,
            //flags & fragmentation offset
            {
                let mut buf: [u8;2] = [0;2];
                BigEndian::write_u16(&mut buf, self.fragments_offset);
                let flags = {
                    let mut result = 0;
                    if self.dont_fragment {
                        result |= 64;
                    }
                    if self.more_fragments {
                        result |= 32;
                    }
                    result
                };
                BigEndian::read_u16(&[flags | (buf[0] & 0x1f), buf[1]])
            },
            BigEndian::read_u16(&[self.time_to_live, self.protocol]),
            //skip checksum (for obvious reasons)
            BigEndian::read_u16(&self.source[0..2]),
            BigEndian::read_u16(&self.source[2..4]),
            BigEndian::read_u16(&self.destination[0..2]),
            BigEndian::read_u16(&self.destination[2..4])
        ].iter().map(|x| u32::from(*x)).sum();
        let options = self.options();
        for i in 0..(options.len()/2) {
            sum += u32::from( BigEndian::read_u16(&options[i*2..i*2 + 2]) );
        }

        let carry_add = (sum & 0xffff) + (sum >> 16);
        !( ((carry_add & 0xffff) + (carry_add >> 16)) as u16 )
    }
}

//NOTE: I would have prefered to NOT write my own Default, Debug & PartialEq implementation but there are no
//      default implementations availible for [u8;40] and the alternative of using [u32;10] would lead
//      to unsafe casting. Writing impl Debug for [u8;40] in a crate is also illegal as it could lead 
//      to an implementation collision between crates.
//      So the only option left to me was to write an implementation myself and deal with the added complexity
//      and potential added error source.

impl Default for Ipv4Header {
    fn default() -> Ipv4Header {
        Ipv4Header {
            differentiated_services_code_point: 0,
            explicit_congestion_notification: 0,
            payload_len: 0,
            identification: 0,
            dont_fragment: true,
            more_fragments: false,
            fragments_offset: 0,
            time_to_live: 0,
            protocol: 0,
            header_checksum: 0,
            source: [0;4],
            destination: [0;4],
            options_len: 0,
            options_buffer: [0;40]
        }
    }
}

impl Debug for Ipv4Header {
    fn fmt(&self, fotmatter: &mut Formatter) -> Result<(), std::fmt::Error> {
        write!(fotmatter, "Ipv4Header {{ ihl: {}, differentiated_services_code_point: {}, explicit_congestion_notification: {}, payload_len: {}, identification: {}, dont_fragment: {}, more_fragments: {}, fragments_offset: {}, time_to_live: {}, protocol: {}, header_checksum: {}, source: {:?}, destination: {:?}, options: {:?} }}", 
            self.ihl(),
            self.differentiated_services_code_point,
            self.explicit_congestion_notification,
            self.payload_len,
            self.identification,
            self.dont_fragment,
            self.more_fragments,
            self.fragments_offset,
            self.time_to_live,
            self.protocol,
            self.header_checksum,
            self.source,
            self.destination,
            self.options())
    }
}

impl std::cmp::PartialEq for Ipv4Header {
    fn eq(&self, other: &Ipv4Header) -> bool {
        self.differentiated_services_code_point == other.differentiated_services_code_point &&
        self.explicit_congestion_notification == other.explicit_congestion_notification &&
        self.payload_len == other.payload_len &&
        self.identification == other.identification &&
        self.dont_fragment == other.dont_fragment &&
        self.more_fragments == other.more_fragments &&
        self.fragments_offset == other.fragments_offset &&
        self.time_to_live == other.time_to_live &&
        self.protocol == other.protocol &&
        self.header_checksum == other.header_checksum &&
        self.source == other.source &&
        self.destination == other.destination &&
        self.options_len == other.options_len &&
        self.options() == other.options()
    }
}

impl std::cmp::Eq for Ipv4Header {}

///A slice containing an ipv4 header of a network package.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Ipv4HeaderSlice<'a> {
    slice: &'a [u8]
}

impl<'a> Ipv4HeaderSlice<'a> {

    ///Creates a slice containing an ipv4 header (including header options).
    pub fn from_slice(slice: &'a[u8]) -> Result<Ipv4HeaderSlice<'a>, ReadError> {

        //check length
        use crate::ReadError::*;
        if slice.len() < Ipv4Header::SERIALIZED_SIZE {
            return Err(UnexpectedEndOfSlice(Ipv4Header::SERIALIZED_SIZE));
        }

        //read version & ihl
        let (version, ihl) = {
            let value = slice[0];
            (value >> 4, value & 0xf)
        };

        //check version
        if 4 != version {
            return Err(Ipv4UnexpectedVersion(version));
        }

        //check that the ihl is correct
        if ihl < 5 {
            use crate::ReadError::*;
            return Err(Ipv4HeaderLengthBad(ihl));
        }

        //check that the slice contains enough data for the entire header + options
        let header_length = (usize::from(ihl))*4;
        if slice.len() < header_length {
            return Err(UnexpectedEndOfSlice(header_length));
        }

        //check the total_length can contain the header
        let total_length = BigEndian::read_u16(&slice[2..4]);
        if total_length < header_length as u16 {
            return Err(Ipv4TotalLengthTooSmall(total_length))
        }

        //all good
        Ok(Ipv4HeaderSlice {
            slice: &slice[..header_length]
        })
    }

    ///Returns the slice containing the ipv4 header
    #[inline]
    pub fn slice(&self) -> &'a [u8] {
        self.slice
    }

    ///Read the "version" field of the IPv4 header (should be 4).
    pub fn version(&self) -> u8 {
        self.slice[0] >> 4
    }

    ///Read the "ip header length" (length of the ipv4 header + options in multiples of 4 bytes).
    pub fn ihl(&self) -> u8 {
        self.slice[0] & 0xf
    }

    ///Read the "differentiated_services_code_point" from the slice.
    pub fn dcp(&self) -> u8 {
        self.slice[1] >> 2
    }

    ///Read the "explicit_congestion_notification" from the slice.
    pub fn ecn(&self) -> u8 {
        self.slice[1] & 0x3
    }

    ///Read the "total length" from the slice (total length of ip header + payload).
    pub fn total_len(&self) -> u16 {
        BigEndian::read_u16(&self.slice[2..4])
    }

    ///Determine the payload length based on the ihl & total_length field of the header.
    pub fn payload_len(&self) -> u16 {
        self.total_len() - u16::from(self.ihl())*4
    }

    ///Read the "identification" field from the slice.
    pub fn identification(&self) -> u16 {
        BigEndian::read_u16(&self.slice[4..6])
    }

    ///Read the "dont fragment" flag from the slice.
    pub fn dont_fragment(&self) -> bool {
        0 != (self.slice[6] & 0x40)
    }

    ///Read the "more fragments" flag from the slice.
    pub fn more_fragments(&self) -> bool {
        0 != (self.slice[6] & 0x20)
    }

    ///Read the "fragment_offset" field from the slice.
    pub fn fragments_offset(&self) -> u16 {
        let buf = [self.slice[6] & 0x1f, self.slice[7]];
        BigEndian::read_u16(&buf[..])
    }

    ///Read the "time_to_live" field from the slice.
    pub fn ttl(&self) -> u8 {
        self.slice[8]
    }

    ///Read the "protocol" field from the slice.
    pub fn protocol(&self) -> u8 {
        self.slice[9]
    }

    ///Read the "header checksum" field from the slice.
    pub fn header_checksum(&self) -> u16 {
        BigEndian::read_u16(&self.slice[10..12])
    }
    
    ///Returns a slice containing the ipv4 source address.
    pub fn source(&self) -> &'a [u8] {
        &self.slice[12..16]
    }

    ///Return the ipv4 source address as an std::net::Ipv4Addr
    pub fn source_addr(&self) -> Ipv4Addr {
        let mut result: [u8; 4] = Default::default();
        result.copy_from_slice(self.source());
        Ipv4Addr::from(result)
    }

    ///Returns a slice containing the ipv4 source address.
    pub fn destination(&self) -> &'a [u8] {
        &self.slice[16..20]
    }

    ///Return the ipv4 destination address as an std::net::Ipv4Addr
    pub fn destination_addr(&self) -> Ipv4Addr {
        let mut result: [u8; 4] = Default::default();
        result.copy_from_slice(self.destination());
        Ipv4Addr::from(result)
    }

    ///Returns a slice containing the ipv4 header options (empty when there are no options).
    pub fn options(&self) -> &'a [u8] {
        &self.slice[20..]
    }

    ///Decode all the fields and copy the results to a Ipv4Header struct
    pub fn to_header(&self) -> Ipv4Header {
        let options = self.options();
        Ipv4Header {
            differentiated_services_code_point: self.dcp(),
            explicit_congestion_notification: self.ecn(),
            payload_len: self.payload_len(),
            identification: self.identification(),
            dont_fragment: self.dont_fragment(),
            more_fragments: self.more_fragments(),
            fragments_offset: self.fragments_offset(),
            time_to_live: self.ttl(),
            protocol: self.protocol(),
            header_checksum: self.header_checksum(),
            source: {
                let mut result: [u8; 4] = Default::default();
                result.copy_from_slice(self.source());
                result
            },
            destination: {
                let mut result: [u8; 4] = Default::default();
                result.copy_from_slice(self.destination());
                result
            },
            options_len: options.len() as u8,
            options_buffer: {
                let mut result: [u8;40] = [0;40];
                result[..options.len()].copy_from_slice(options);
                result
            }
        }
    }
}