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
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
use super::super::*;

use std::net::{Ipv4Addr, Ipv6Addr};

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

///Internet protocol headers version 4 & 6
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum IpHeader {
    Version4(Ipv4Header),
    Version6(Ipv6Header)
}

impl IpHeader {
    ///Reads an IP (v4 or v6) header from the current position.
    pub fn read<T: io::Read + io::Seek + Sized>(reader: &mut T) -> Result<IpHeader, ReadError> {
        let value = reader .read_u8()?;
        match value >> 4 {
            4 => Ok(IpHeader::Version4(Ipv4Header::read_without_version(reader, value & 0xf)?)),
            6 => Ok(IpHeader::Version6(Ipv6Header::read_without_version(reader, value & 0xf)?)),
            version => Err(ReadError::IpUnsupportedVersion(version))
        }
    }
    ///Writes an IP (v4 or v6) header to the current position
    pub fn write<T: io::Write + Sized>(&self, writer: &mut T) -> Result<(), WriteError> {
        use IpHeader::*;
        match *self {
            Version4(ref value) => value.write(writer, &[]),
            Version6(ref value) => value.write(writer)
        }
    }
}

///IPv4 header without options.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Ipv4Header {
    pub header_length: u8,
    pub differentiated_services_code_point: u8,
    pub explicit_congestion_notification: u8,
    pub total_length: 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]
}


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

impl Ipv4Header {
    ///Constructs an Ipv4Header with standard values for non specified values.
    ///Note: This header calculates the checksum assuming that there are no ipv4 options. In case there are calculate the checksum using the "calc_header_checksum" method.
    pub fn new(payload_and_options_length: usize, time_to_live: u8, protocol: IpTrafficClass, source: [u8;4], destination: [u8;4]) -> Result<Ipv4Header, ValueError> {
        
        //check that the total length fits into the field
        const MAX_PAYLOAD_AND_OPTIONS_LENGTH: usize = (std::u16::MAX as usize) - Ipv4Header::SERIALIZED_SIZE;
        if MAX_PAYLOAD_AND_OPTIONS_LENGTH < payload_and_options_length {
            return Err(ValueError::Ipv4PayloadAndOptionsLengthTooLarge(payload_and_options_length));
        }

        Ok(Ipv4Header {
            header_length: 0,
            differentiated_services_code_point: 0,
            explicit_congestion_notification: 0,
            total_length: (payload_and_options_length + 20) as u16,
            identification: 0,
            dont_fragment: true,
            more_fragments: false,
            fragments_offset: 0,
            time_to_live: time_to_live,
            protocol: protocol as u8,
            header_checksum: 0,
            source: source,
            destination: destination
        })
    }

    ///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));
        }
        match Ipv4Header::read_without_version(reader, value & 0xf) {
            Ok(value) => Ok(value),
            Err(err) => Err(ReadError::IoError(err))
        }
    }

    ///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, io::Error> {
        let ihl = version_rest;
        let (dscp, ecn) = {
            let value = reader.read_u8()?;
            (value >> 2, value & 0x3)
        };
        let total_length = reader.read_u16::<BigEndian>()?;
        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,
            total_length: total_length,
            identification: identification,
            dont_fragment: dont_fragment,
            more_fragments: more_fragments,
            fragments_offset: 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
            },
            header_length: ihl
        })
    }

    ///Skips the ipv4 header options based on the header length.
    pub fn skip_options<T: io::Read + io::Seek + Sized>(&self, reader: &mut T) -> Result<(), ReadError> {
        //return an error if the provided header length is too small (smaller then the header itself)
        if self.header_length < 5 {
            use ReadError::*;
            return Err(Ipv4HeaderLengthBad(self.header_length));
        }

        let skip = ((self.header_length - 5) as i64)*4;
        if skip > 0 {
            //seek does not return an error, when the end is reached
            //to ensure this still happens an read_exact is added at the end
            //that throws an error
            if skip > 4 {
                use std::io::SeekFrom;
                reader.seek(SeekFrom::Current(skip - 4))?;
            }
            let mut buffer: [u8;4] = [0;4];
            reader.read_exact(&mut buffer)?;
        }
        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, options: &[u8]) -> Result<(), WriteError> {
        use 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)?;
        if options.len() > 10*4 || options.len() % 4 != 0 {
            return Err(
                WriteError::ValueError(
                    ValueError::Ipv4OptionsLengthBad(
                        options.len())));
        }

        //write with recalculations
        let header_legnth = 5 + (options.len()/4) as u8;
        self.write_ipv4_header_internal(writer, options, header_legnth, self.calc_header_checksum_unchecked(header_legnth, options))
    }

    ///Writes a given IPv4 header to the current position (this method just writes the specified checksum and header_length and does note compute it).
    pub fn write_raw<T: io::Write + Sized>(&self, writer: &mut T, options: &[u8]) -> Result<(), WriteError> {
        use ErrorField::*;
        
        //check ranges
        max_check_u8(self.header_length, 0xf, Ipv4HeaderLength)?;
        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)?;
        if options.len() > 10*4 || options.len() % 4 != 0 {
            return Err(
                WriteError::ValueError(
                    ValueError::Ipv4OptionsLengthBad(
                        options.len())));
        }

        //write
        self.write_ipv4_header_internal(writer, options, self.header_length, 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, options: &[u8], header_length: u8, header_checksum: u16) -> Result<(), WriteError> {
        //version & header_length
        write.write_u8((4 << 4) | header_length)?;

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

        //total length & id 
        write.write_u16::<BigEndian>(self.total_length)?;
        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 = result | 64;
                }
                if self.more_fragments {
                    result = 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(&options)?;
        Ok(())
    }

    ///Calculate header checksum of the current ipv4 header.
    pub fn calc_header_checksum(&self, options: &[u8]) -> Result<u16, ValueError> {
        use ErrorField::*;
        use ValueError::Ipv4OptionsLengthBad;

        //check ranges
        max_check_u8(self.header_length, 0xf, Ipv4HeaderLength)?;
        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)?;
        if options.len() > 10*4 || options.len() % 4 != 0 {
            return Err(Ipv4OptionsLengthBad(options.len()));
        }

        //calculate the checksum
        Ok(self.calc_header_checksum_unchecked(self.header_length, options))
    }

    ///Calculate the header checksum under the assumtion that all value ranges in the header are correct
    fn calc_header_checksum_unchecked(&self, header_length: u8, options: &[u8]) -> u16 {
        //version & header_length
        let mut sum = [
            BigEndian::read_u16(&[ (4 << 4) | header_length,
                                (self.differentiated_services_code_point << 2) | self.explicit_congestion_notification ]),
            self.total_length,
            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 = result | 64;
                    }
                    if self.more_fragments {
                        result = 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().fold(0, |a: u32, x: &u16| a + (*x as u32));
        for i in 0..(options.len()/2) {
            sum += BigEndian::read_u16(&options[i*2..i*2 + 2]) as u32;
        }

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

    ///Sets the field total_length based on the size of the payload and the options. Returns an error if the payload is too big to fit.
    pub fn set_payload_and_options_length(&mut self, size: usize) -> Result<(), ValueError> {
        //check that the total length fits into the field
        const MAX_PAYLOAD_AND_OPTIONS_LENGTH: usize = (std::u16::MAX as usize) - Ipv4Header::SERIALIZED_SIZE;
        if MAX_PAYLOAD_AND_OPTIONS_LENGTH < size {
            return Err(ValueError::Ipv4PayloadAndOptionsLengthTooLarge(size));
        }

        self.total_length = (size + Ipv4Header::SERIALIZED_SIZE) as u16;
        Ok(())
    }
}

///IPv6 header according to rfc8200.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Ipv6Header {
    pub traffic_class: u8,
    ///If non 0 serves as a hint to router and switches with multiple outbound paths that these packets should stay on the same path, so that they will not be reordered.
    pub flow_label: u32,
    ///The length of the payload and extension headers in 
    pub payload_length: u16,
    ///Specifies what the next header or transport layer protocol is (see IpTrafficClass for a definitions of ids).
    pub next_header: u8,
    ///The number of hops the packet can take before it is discarded.
    pub hop_limit: u8,
    ///IPv6 source address
    pub source: [u8;16],
    ///IPv6 destination address
    pub destination: [u8;16]
}

impl SerializedSize for Ipv6Header {
    ///Size of the header itself in bytes.
    const SERIALIZED_SIZE:usize = 40;
}

impl Ipv6Header {

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

    ///Reads an IPv6 header assuming the version & flow_label field have already been read.
    pub fn read_without_version<T: io::Read + io::Seek + Sized>(reader: &mut T, version_rest: u8) -> Result<Ipv6Header, io::Error> {
        let (traffic_class, flow_label) = {
            //read 4 bytes
            let mut buffer: [u8; 4] = [0;4];
            reader.read_exact(&mut buffer[1..])?;

            //extract class
            let traffic_class = (version_rest << 4) | (buffer[1] >> 4);

            //remove traffic class from buffer & read flow_label
            buffer[1] = buffer[1] & 0xf;
            (traffic_class, byteorder::BigEndian::read_u32(&buffer))
        };
        
        Ok(Ipv6Header{
            traffic_class: traffic_class,
            flow_label: flow_label,
            payload_length: reader.read_u16::<BigEndian>()?,
            next_header: reader.read_u8()?,
            hop_limit: reader.read_u8()?,
            source: {
                let mut buffer: [u8; 16] = [0;16];
                reader.read_exact(&mut buffer)?;
                buffer
            },
            destination: {
                let mut buffer: [u8; 16] = [0;16];
                reader.read_exact(&mut buffer)?;
                buffer
            }
        })
    }

    ///Skips the ipv6 header extension and returns the traffic_class
    pub fn skip_header_extension<T: io::Read + io::Seek + Sized>(reader: &mut T, traffic_class: u8) -> Result<u8, io::Error> {
        let next_header = reader.read_u8()?;
        //determine the length (fragmentation header has a fixed length & the rest a length field)
        const FRAG: u8 = IpTrafficClass::IPv6FragmentationHeader as u8;
        let rest_length = if traffic_class == FRAG {
            //fragmentation header has the fixed length of 64bits (one already read)
            7
        } else {
            //Length of the Hop-by-Hop Options header in 8-octet units, not including the first 8 octets.
            (((reader.read_u8()? as i64) + 1)*8) - 2
        };
        //Sadly seek does not return an error if the seek could not be fullfilled.
        //Some implementations do not even truncate the returned position to the
        //last valid one. std::io::Cursor for example just moves the position
        //over the border of the given slice (e.g. returns position 15 even when
        //the given slice contains only 1 element).
        //The only option, to detect that we are in an invalid state, is to move the
        //seek offset to one byte before the end and then execute a normal read to
        //trigger an error.
        reader.seek(io::SeekFrom::Current(rest_length - 1))?;
        reader.read_u8()?;
        Ok(next_header)
    }

    ///Skips all ipv6 header extensions and returns the last traffic_class
    pub fn skip_all_header_extensions<T: io::Read + io::Seek + Sized>(reader: &mut T, traffic_class: u8) -> Result<u8, ReadError> {
        use IpTrafficClass::*;
        const HOP_BY_HOP: u8 = IPv6HeaderHopByHop as u8;
        const ROUTE: u8 = IPv6RouteHeader as u8;
        const FRAG: u8 = IPv6FragmentationHeader as u8;
        const OPTIONS: u8 = IPv6DestinationOptions as u8;
        const AUTH: u8 = IPv6AuthenticationHeader as u8;
        const ENCAP_SEC: u8 = IPv6EncapSecurityPayload as u8;

        let mut next_traffic_class = traffic_class;
        for _i in 0..IPV6_MAX_NUM_HEADER_EXTENSIONS {
            match next_traffic_class {
                HOP_BY_HOP | ROUTE | FRAG | OPTIONS | AUTH | ENCAP_SEC => {
                    next_traffic_class = Ipv6Header::skip_header_extension(reader, next_traffic_class)?;
                },
                _ => return Ok(next_traffic_class)
            }
        }
        match next_traffic_class {
            HOP_BY_HOP | ROUTE | FRAG | OPTIONS | AUTH | ENCAP_SEC => Err(ReadError::Ipv6TooManyHeaderExtensions),
            value => Ok(value)
        }
    }

    ///Writes a given IPv6 header to the current position.
    pub fn write<T: io::Write + Sized>(&self, writer: &mut T) -> Result<(), WriteError> {
        use ErrorField::*;
        fn max_check_u32(value: u32, max: u32, field: ErrorField) -> Result<(), WriteError> {
            if value <= max {
                Ok(())
            } else {
                Err(
                    WriteError::ValueError(
                        ValueError::U32TooLarge{
                            value: value, 
                            max: max, 
                            field: field }))
            }
        };

        //version & traffic class p0
        writer.write_u8((6 << 4) | (self.traffic_class >> 4))?;

        //flow label
        max_check_u32(self.flow_label, 0xfffff, Ipv6FlowLabel)?;
        {
            //write as a u32 to a buffer and write only the "lower bytes"
            let mut buffer: [u8; 4] = [0;4];
            byteorder::BigEndian::write_u32(&mut buffer, self.flow_label);
            //add the traffic_class
            buffer[1] = buffer[1] | (self.traffic_class << 4);
            //skip "highest" byte of big endian
            writer.write_all(&buffer[1..])?;
        }

        //rest
        writer.write_u16::<BigEndian>(self.payload_length)?;
        writer.write_u8(self.next_header)?;
        writer.write_u8(self.hop_limit)?;
        writer.write_all(&self.source)?;
        writer.write_all(&self.destination)?;

        Ok(())
    }

    ///Sets the field total_length based on the size of the payload and the options. Returns an error if the payload is too big to fit.
    pub fn set_payload_length(&mut self, size: usize) -> Result<(), ValueError> {
        //check that the total length fits into the field
        const MAX_PAYLOAD_LENGTH: usize = std::u16::MAX as usize;
        if MAX_PAYLOAD_LENGTH < size {
            return Err(ValueError::Ipv6PayloadLengthTooLarge(size));
        }

        self.payload_length = size as u16;
        Ok(())
    }
}

impl<'a> PacketSlice<'a, Ipv4Header> {

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

        //check length
        use std::io::ErrorKind::UnexpectedEof;
        use std::io::Error;
        use ReadError::*;
        if slice.len() < Ipv4Header::SERIALIZED_SIZE {
            return Err(IoError(Error::from(UnexpectedEof)));
        }

        //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 ReadError::*;
            return Err(Ipv4HeaderLengthBad(ihl));
        }

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

        //all good
        Ok(PacketSlice {
            slice: &slice[..total_length],
            phantom: std::marker::PhantomData{}
        })
    }

    ///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_length(&self) -> u16 {
        BigEndian::read_u16(&self.slice[2..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 {
        Ipv4Header {
            header_length: self.ihl(),
            differentiated_services_code_point: self.dcp(),
            explicit_congestion_notification: self.ecn(),
            total_length: self.total_length(),
            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
            }
        }
    }
}

impl<'a> PacketSlice<'a, Ipv6Header> {

    ///Creates a slice containing an ipv6 header (without header extensions).
    pub fn from_slice(slice: &'a[u8]) -> Result<PacketSlice<'a, Ipv6Header>, ReadError> {

        //check length
        use std::io::ErrorKind::UnexpectedEof;
        use std::io::Error;
        use ReadError::*;
        if slice.len() < Ipv6Header::SERIALIZED_SIZE {
            return Err(IoError(Error::from(UnexpectedEof)));
        }

        //read version & ihl
        let version = slice[0] >> 4;

        //check version
        if 6 != version {
            return Err(Ipv6UnexpectedVersion(version));
        }

        //all good
        Ok(PacketSlice {
            slice: &slice[..Ipv6Header::SERIALIZED_SIZE],
            phantom: std::marker::PhantomData{}
        })
    }

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

    ///Read the "traffic class" field from the slice.
    pub fn traffic_class(&self) -> u8 {
        (self.slice[0] << 4) | (self.slice[1] >> 4)
    }

    ///Read the "flow label" field from the slice.
    pub fn flow_label(&self) -> u32 {
        byteorder::BigEndian::read_u32(&[0, self.slice[1] & 0xf, self.slice[2], self.slice[3]])
    }

    ///Read the "payload length" field from  the slice. The length should contain the length of all extension headers and payload.
    pub fn payload_length(&self) -> u16 {
        byteorder::BigEndian::read_u16(&self.slice[4..6])
    }

    ///Read the "next header" field from the slice. The next header value specifies what the next header or transport layer protocol is (see IpTrafficClass for a definitions of ids).
    pub fn next_header(&self) -> u8 {
        self.slice[6]
    }

    ///Read the "hop limit" field from the slice. The hop limit specifies the number of hops the packet can take before it is discarded.
    pub fn hop_limit(&self) -> u8 {
        self.slice[7]
    }

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

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

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

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

    ///Decode all the fields and copy the results to a Ipv6Header struct
    pub fn to_header(&self) -> Ipv6Header {
        Ipv6Header {
            traffic_class: self.traffic_class(),
            flow_label: self.flow_label(),
            payload_length: self.payload_length(),
            next_header: self.next_header(),
            hop_limit: self.hop_limit(),
            source: {
                let mut result: [u8; 16] = Default::default();
                result.copy_from_slice(self.source());
                result
            },
            destination: {
                let mut result: [u8; 16] = Default::default();
                result.copy_from_slice(self.destination());
                result
            }
        }
    }
}

///Maximum number of header extensions allowed (according to the ipv6 rfc8200).
pub const IPV6_MAX_NUM_HEADER_EXTENSIONS: usize = 7;

///Dummy struct for ipv6 header extensions.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Ipv6ExtensionHeader {
    next_header: u8,
    length: u8
}

impl<'a> PacketSlice<'a, Ipv6ExtensionHeader> {
    ///Creates a slice containing an ipv6 header extension.
    pub fn from_slice(header_type: u8, slice: &'a[u8]) -> Result<PacketSlice<'a, Ipv6ExtensionHeader>, ReadError> {

        //check length
        use std::io::ErrorKind::UnexpectedEof;
        use std::io::Error;
        use ReadError::*;
        if slice.len() < 8 {
            return Err(IoError(Error::from(UnexpectedEof)));
        }

        //check length
        const FRAG: u8 = IpTrafficClass::IPv6FragmentationHeader as u8;
        let len = if FRAG == header_type {
            8
        } else {
            ((slice[1] as usize) + 1)*8
        };

        //check the length again now that the expected length is known
        if slice.len() < len {
            return Err(IoError(Error::from(UnexpectedEof)));
        }

        //all good
        Ok(PacketSlice {
            slice: &slice[..len],
            phantom: std::marker::PhantomData{}
        })
    }

    ///Returns the id of the next header (see IpTrafficClass for a definition of all ids).
    pub fn next_header(&self) -> u8 {
        self.slice[0]
    }
}

///Identifiers for the traffic_class field in ipv6 headers and protocol field in ipv4 headers.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum IpTrafficClass {
    ///IPv6 Hop-by-Hop Option [RFC8200]
    IPv6HeaderHopByHop = 0,
    ///Internet Control Message [RFC792]
    Icmp = 1,
    ///Internet Group Management [RFC1112]
    Igmp = 2,
    ///Gateway-to-Gateway [RFC823]
    Ggp = 3,
    ///IPv4 encapsulation [RFC2003]
    IPv4 = 4,
    ///Stream [RFC1190][RFC1819]
    Stream = 5,
    ///Transmission Control [RFC793]
    Tcp = 6,
    ///CBT [Tony_Ballardie]
    Cbt = 7,
    ///Exterior Gateway Protocol [RFC888][David_Mills]
    Egp = 8,
    ///any private interior gateway (used by Cisco for their IGRP) [Internet_Assigned_Numbers_Authority]
    Igp = 9,
    ///BBN RCC Monitoring [Steve_Chipman]
    BbnRccMon = 10,
    ///Network Voice Protocol [RFC741][Steve_Casner]
    NvpII = 11,
    ///PUP
    Pup = 12,
    ///ARGUS (deprecated) [Robert_W_Scheifler]
    Argus = 13,
    ///EMCON [<mystery contact>]
    Emcon = 14,
    ///Cross Net Debugger [Haverty, J., "XNET Formats for Internet Protocol Version 4", IEN 158, October 1980.][Jack_Haverty]
    Xnet = 15,
    ///Chaos [J_Noel_Chiappa]
    Chaos = 16,
    ///User Datagram [RFC768][Jon_Postel]
    Udp = 17,
    ///Multiplexing [Cohen, D. and J. Postel, "Multiplexing Protocol", IEN 90, USC/Information Sciences Institute, May 1979.][Jon_Postel]
    Mux = 18,
    ///DCN Measurement Subsystems [David_Mills]
    DcnMeas = 19,
    ///Host Monitoring [RFC869][Bob_Hinden]
    Hmp = 20,
    ///Packet Radio Measurement [Zaw_Sing_Su]
    Prm = 21,
    ///XEROX NS IDP
    XnsIdp = 22,
    ///Trunk-1 [Barry_Boehm]
    Trunk1 = 23,
    ///Trunk-2 [Barry_Boehm]
    Trunk2 = 24,
    ///Leaf-1 [Barry_Boehm]
    Leaf1 = 25,
    ///Leaf-2 [Barry_Boehm]
    Leaf2 = 26,
    ///Reliable Data Protocol [RFC908][Bob_Hinden]
    Rdp = 27,
    ///Internet Reliable Transaction [RFC938][Trudy_Miller]
    Irtp = 28,
    ///ISO Transport Protocol Class 4 [RFC905][<mystery contact>]
    IsoTp4 = 29,
    ///Bulk Data Transfer Protocol [RFC969][David_Clark]
    NetBlt = 30,
    ///MFE Network Services Protocol [Shuttleworth, B., "A Documentary of MFENet, a National Computer Network", UCRL-52317, Lawrence Livermore Labs, Livermore, California, June 1977.][Barry_Howard]
    MfeNsp = 31,
    ///MERIT Internodal Protocol [Hans_Werner_Braun]
    MeritInp = 32,
    ///Datagram Congestion Control Protocol [RFC4340]
    Dccp = 33,
    ///Third Party Connect Protocol [Stuart_A_Friedberg]
    ThirdPartyConnectProtocol = 34,
    ///Inter-Domain Policy Routing Protocol [Martha_Steenstrup]
    Idpr = 35,
    ///XTP [Greg_Chesson]
    Xtp = 36,
    ///Datagram Delivery Protocol [Wesley_Craig]
    Ddp = 37,
    ///IDPR Control Message Transport Proto [Martha_Steenstrup]
    IdprCmtp = 38,
    ///TP++ Transport Protocol [Dirk_Fromhein]
    TpPlusPlus = 39,
    ///IL Transport Protocol [Dave_Presotto]
    Il = 40,
    ///IPv6 encapsulation [RFC2473]
    Ipv6 = 41,
    ///Source Demand Routing Protocol [Deborah_Estrin]
    Sdrp = 42,
    ///Routing Header for IPv6 [Steve_Deering]
    IPv6RouteHeader = 43,
    ///Fragment Header for IPv6 [Steve_Deering]
    IPv6FragmentationHeader = 44,
    ///Inter-Domain Routing Protocol [Sue_Hares]
    Idrp = 45,
    ///Reservation Protocol [RFC2205][RFC3209][Bob_Braden]
    Rsvp = 46,
    ///Generic Routing Encapsulation [RFC2784][Tony_Li]
    Gre = 47,
    ///Dynamic Source Routing Protocol [RFC4728]
    Dsr = 48,
    ///BNA [Gary Salamon]
    Bna = 49,
    ///Encap Security Payload [RFC4303]
    IPv6EncapSecurityPayload = 50,
    ///Authentication Header [RFC4302]
    IPv6AuthenticationHeader = 51,
    ///Integrated Net Layer Security  TUBA [K_Robert_Glenn]
    Inlsp = 52,
    ///IP with Encryption (deprecated) [John_Ioannidis]
    Swipe = 53,
    ///NBMA Address Resolution Protocol [RFC1735]
    Narp = 54,
    ///IP Mobility [Charlie_Perkins]
    Mobile = 55,
    ///Transport Layer Security Protocol using Kryptonet key management [Christer_Oberg]
    Tlsp = 56,
    ///SKIP [Tom_Markson]
    Skip = 57,
    ///ICMP for IPv6 [RFC8200]
    IPv6Icmp = 58,
    ///No Next Header for IPv6 [RFC8200]
    IPv6NoNextHeader = 59,
    ///Destination Options for IPv6 [RFC8200]
    IPv6DestinationOptions = 60,
    ///any host internal protocol [Internet_Assigned_Numbers_Authority]
    AnyHostInternalProtocol = 61,
    ///CFTP [Forsdick, H., "CFTP", Network Message, Bolt Beranek and Newman, January 1982.][Harry_Forsdick]
    Cftp = 62,
    ///any local network [Internet_Assigned_Numbers_Authority]
    AnyLocalNetwork = 63,
    ///SATNET and Backroom EXPAK [Steven_Blumenthal]
    SatExpak = 64,
    ///Kryptolan [Paul Liu]
    Krytolan = 65,
    ///MIT Remote Virtual Disk Protocol [Michael_Greenwald]
    Rvd = 66,
    ///Internet Pluribus Packet Core [Steven_Blumenthal]
    Ippc = 67,
    ///any distributed file system [Internet_Assigned_Numbers_Authority]
    AnyDistributedFileSystem = 68,
    ///SATNET Monitoring [Steven_Blumenthal]
    SatMon = 69,
    ///VISA Protocol [Gene_Tsudik]
    Visa = 70,
    ///Internet Packet Core Utility [Steven_Blumenthal]
    Ipcv = 71,
    ///Computer Protocol Network Executive [David Mittnacht]
    Cpnx = 72,
    ///Computer Protocol Heart Beat [David Mittnacht]
    Cphb = 73,
    ///Wang Span Network [Victor Dafoulas]
    Wsn = 74,
    ///Packet Video Protocol [Steve_Casner]
    Pvp = 75,
    ///Backroom SATNET Monitoring [Steven_Blumenthal]
    BrSatMon = 76,
    ///SUN ND PROTOCOL-Temporary [William_Melohn]
    SunNd = 77,
    ///WIDEBAND Monitoring [Steven_Blumenthal]
    WbMon = 78,
    ///WIDEBAND EXPAK [Steven_Blumenthal]
    WbExpak = 79,
    ///ISO Internet Protocol [Marshall_T_Rose]
    IsoIp = 80,
    ///VMTP [Dave_Cheriton]
    Vmtp = 81,
    ///SECURE-VMTP [Dave_Cheriton]
    SecureVmtp = 82,
    ///VINES [Brian Horn]
    Vines = 83,
    ///Transaction Transport Protocol or Internet Protocol Traffic Manager [Jim_Stevens]
    TtpOrIptm = 84,
    ///NSFNET-IGP [Hans_Werner_Braun]
    NsfnetIgp = 85,
    ///Dissimilar Gateway Protocol [M/A-COM Government Systems, "Dissimilar Gateway Protocol Specification, Draft Version", Contract no. CS901145, November 16, 1987.][Mike_Little]
    Dgp = 86,
    ///TCF [Guillermo_A_Loyola]
    Tcf = 87,
    ///EIGRP [RFC7868]
    Eigrp = 88,
    ///OSPFIGP [RFC1583][RFC2328][RFC5340][John_Moy]
    Ospfigp = 89,
    ///Sprite RPC Protocol [Welch, B., "The Sprite Remote Procedure Call System", Technical Report, UCB/Computer Science Dept., 86/302, University of California at Berkeley, June 1986.][Bruce Willins]
    SpriteRpc = 90,
    ///Locus Address Resolution Protocol [Brian Horn]
    Larp = 91,
    ///Multicast Transport Protocol [Susie_Armstrong]
    Mtp = 92,
    ///AX.25 Frames [Brian_Kantor]
    Ax25 = 93,
    ///IP-within-IP Encapsulation Protocol [John_Ioannidis]
    Ipip = 94,
    ///Mobile Internetworking Control Pro. (deprecated) [John_Ioannidis]
    Micp = 95,
    ///Semaphore Communications Sec. Pro. [Howard_Hart]
    SccSp = 96,
    ///Ethernet-within-IP Encapsulation [RFC3378]
    EtherIp = 97,
    ///Encapsulation Header [RFC1241][Robert_Woodburn]
    Encap = 98,
    ///GMTP [[RXB5]]
    Gmtp = 100,
    ///Ipsilon Flow Management Protocol [Bob_Hinden][November 1995, 1997.]
    Ifmp = 101,
    ///PNNI over IP [Ross_Callon]
    Pnni = 102,
    ///Protocol Independent Multicast [RFC7761][Dino_Farinacci]
    Pim = 103,
    ///ARIS [Nancy_Feldman]
    Aris = 104,
    ///SCPS [Robert_Durst]
    Scps = 105,
    ///QNX [Michael_Hunter]
    Qnx = 106,
    ///Active Networks [Bob_Braden]
    ActiveNetworks = 107,
    ///IP Payload Compression Protocol [RFC2393]
    IpComp = 108,
    ///Sitara Networks Protocol [Manickam_R_Sridhar]
    SitraNetworksProtocol = 109,
    ///Compaq Peer Protocol [Victor_Volpe]
    CompaqPeer = 110,
    ///IPX in IP [CJ_Lee]
    IpxInIp = 111,
    ///Virtual Router Redundancy Protocol [RFC5798]
    Vrrp = 112,
    ///PGM Reliable Transport Protocol [Tony_Speakman]
    Pgm = 113,
    ///any 0-hop protocol [Internet_Assigned_Numbers_Authority]
    AnyZeroHopProtocol = 114,
    ///Layer Two Tunneling Protocol [RFC3931][Bernard_Aboba]
    Layer2TunnelingProtocol = 115,
    ///D-II Data Exchange (DDX) [John_Worley]
    Ddx = 116,
    ///Interactive Agent Transfer Protocol [John_Murphy]
    Iatp = 117,
    ///Schedule Transfer Protocol [Jean_Michel_Pittet]
    Stp = 118,
    ///SpectraLink Radio Protocol [Mark_Hamilton]
    Srp = 119,
    ///UTI [Peter_Lothberg]
    Uti = 120,
    ///Simple Message Protocol [Leif_Ekblad]
    SimpleMessageProtocol = 121,
    ///Simple Multicast Protocol (deprecated) [Jon_Crowcroft][draft-perlman-simple-multicast]
    Sm = 122,
    ///Performance Transparency Protocol [Michael_Welzl]
    Ptp = 123,
    ///ISIS over IPv4 [Tony_Przygienda]
    IsisOverIpv4 = 124,
    ///FIRE [Criag_Partridge]
    Fire = 125,
    ///Combat Radio Transport Protocol [Robert_Sautter]
    Crtp = 126,
    ///Combat Radio User Datagram [Robert_Sautter]
    Crudp = 127,
    ///SSCOPMCE [Kurt_Waber]
    Sscopmce = 128,
    ///IPLT [[Hollbach]]
    Iplt = 129,
    ///Secure Packet Shield [Bill_McIntosh]
    Sps = 130,
    ///Private IP Encapsulation within IP [Bernhard_Petri]
    Pipe = 131,
    ///Stream Control Transmission Protocol [Randall_R_Stewart]
    Sctp = 132,
    ///Fibre Channel [Murali_Rajagopal][RFC6172]
    Fc = 133,
    ///RSVP-E2E-IGNORE [RFC3175]
    RsvpE2eIgnore = 134,
    ///MobilityHeader [RFC6275]
    MobilityHeader = 135,
    ///UDPLite [RFC3828]
    UdpLite = 136,
    /// [RFC4023]
    MplsInIp = 137,
    ///MANET Protocols [RFC5498]
    Manet = 138,
    ///Host Identity Protocol [RFC7401]
    Hip = 139,
    ///Shim6 Protocol [RFC5533]
    Shim6 = 140,
    ///Wrapped Encapsulating Security Payload [RFC5840]
    Wesp = 141,
    ///Robust Header Compression [RFC5858]
    Rohc = 142
}