etherparse 0.20.1

A library for parsing & writing a bunch of packet based protocols (EthernetII, IPv4, IPv6, UDP, TCP ...).
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
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
use crate::*;
use proptest::prelude::*;
use proptest::*;

pub fn vlan_ethertype_any() -> impl Strategy<Value = EtherType> {
    prop_oneof![
        Just(ether_type::VLAN_TAGGED_FRAME),
        Just(ether_type::PROVIDER_BRIDGING),
        Just(ether_type::VLAN_DOUBLE_TAGGED_FRAME),
    ]
}

prop_compose! {
    pub fn ether_type_any()
        (value in any::<u16>())
        -> EtherType
    {
        EtherType(value)
    }
}

prop_compose! {
    pub fn ether_type_unknown()
        (ether_type in ether_type_any().prop_filter("ether_type must be unknown",
        |v| !ETHERNET_KNOWN_ETHER_TYPES.iter().any(|&x| v == &x)))
        -> EtherType
    {
        ether_type
    }
}

prop_compose! {
    pub fn vlan_id_any()
        (value in 0..=0b0000_1111_1111_1111u16)
        -> VlanId
    {
        VlanId::try_new(value).unwrap()
    }
}

prop_compose! {
    pub fn vlan_pcp_any()
        (value in 0..=0b0000_0111u8)
        -> VlanPcp
    {
        VlanPcp::try_new(value).unwrap()
    }
}

prop_compose! {
    pub fn vlan_single_unknown()(
        pcp in vlan_pcp_any(),
        drop_eligible_indicator in any::<bool>(),
        vlan_id in vlan_id_any(),
        ether_type in ether_type_any().prop_filter("ether_type must be unknown",
            |v| !ETHERNET_KNOWN_ETHER_TYPES.iter().any(|&x| v == &x)))
        -> SingleVlanHeader
    {
        SingleVlanHeader {
            pcp,
            drop_eligible_indicator,
            vlan_id,
            ether_type,
        }
    }
}

prop_compose! {
    pub fn ip_ecn_any()
        (value in 0u8..=0b0000_0011)
        -> IpEcn
    {
        IpEcn::try_new(value).unwrap()
    }
}

prop_compose! {
    pub fn ip_dscp_any()
        (value in 0u8..=0b0011_1111)
        -> IpDscp
    {
        IpDscp::try_new(value).unwrap()
    }
}

prop_compose! {
    pub fn ipv6_flow_label_any()
        (value in 0u32..=0b1111_11111111_11111111u32)
        -> Ipv6FlowLabel
    {
        Ipv6FlowLabel::try_new(value).unwrap()
    }
}

prop_compose! {
    pub fn ip_number_any()
        (value in any::<u8>())
        -> IpNumber
    {
        IpNumber(value)
    }
}

prop_compose! {
    pub fn ethernet_2_with(ether_type: EtherType)(
        source in prop::array::uniform6(any::<u8>()),
        dest in prop::array::uniform6(any::<u8>()),
        ether_type in proptest::strategy::Just(ether_type))
        -> Ethernet2Header
    {
        Ethernet2Header {
            source: source,
            destination: dest,
            ether_type: ether_type
        }
    }
}

prop_compose! {
    pub fn ethernet_2_any()
        (ether_type in ether_type_any())
        (result in ethernet_2_with(ether_type))
        -> Ethernet2Header
    {
        result
    }
}

prop_compose! {
    pub fn linux_sll_packet_type_any()
        (value in 0..=LinuxSllPacketType::MAX_VAL)
        -> LinuxSllPacketType
    {
        LinuxSllPacketType::try_from(value).unwrap()
    }
}

prop_compose! {
    pub fn linux_sll_arp_hrd_type_any()
        (index in 0..=(LinuxSllProtocolType::SUPPORTED_ARPHWD.len()-1))
        -> ArpHardwareId
    {
        LinuxSllProtocolType::SUPPORTED_ARPHWD[index]
    }
}

prop_compose! {
    pub fn linux_sll_sender_address_any()
        (mut sender_address in prop::collection::vec(any::<u8>(), 0..8))
        -> (u16, [u8; 8])
    {
        let size = sender_address.len().try_into().unwrap();
        sender_address.resize(8, 0);
        let mut v: [u8; 8] = [0u8;8];
        v.copy_from_slice(&sender_address);

        (size, v)
    }
}

prop_compose! {
    pub fn linux_sll_any()
        (packet_type in linux_sll_packet_type_any(),
        arp_hrd_type in linux_sll_arp_hrd_type_any(),
        (sender_address_valid_length, sender_address) in linux_sll_sender_address_any(),
        protocol_num in any::<u16>()
    )
        -> LinuxSllHeader
    {
        LinuxSllHeader {
            packet_type,
            arp_hrd_type,
            sender_address_valid_length,
            sender_address,
            protocol_type: LinuxSllProtocolType::try_from((arp_hrd_type, protocol_num)).unwrap()
        }
    }
}

pub static ETHERNET_KNOWN_ETHER_TYPES: &'static [EtherType] = &[
    ether_type::ARP,
    ether_type::IPV4,
    ether_type::IPV6,
    ether_type::VLAN_TAGGED_FRAME,
    ether_type::PROVIDER_BRIDGING,
    ether_type::VLAN_DOUBLE_TAGGED_FRAME,
    ether_type::MACSEC,
];

prop_compose! {
    pub fn ethernet_2_unknown()(
        source in prop::array::uniform6(any::<u8>()),
        dest in prop::array::uniform6(any::<u8>()),
        ether_type in ether_type_any().prop_filter("ether_type must be unknown",
            |v| !ETHERNET_KNOWN_ETHER_TYPES.iter().any(|&x| v == &x)))
        -> Ethernet2Header
    {
        Ethernet2Header {
            source: source,
            destination: dest,
            ether_type: ether_type
        }
    }
}

prop_compose! {
    pub fn vlan_single_with(ether_type: EtherType)(
        pcp in vlan_pcp_any(),
        drop_eligible_indicator in any::<bool>(),
        vlan_id in vlan_id_any(),
        ether_type in proptest::strategy::Just(ether_type))
        -> SingleVlanHeader
    {
        SingleVlanHeader {
            pcp,
            drop_eligible_indicator,
            vlan_id,
            ether_type,
        }
    }
}

prop_compose! {
    pub fn vlan_single_any()
        (ether_type in ether_type_any())
        (result in vlan_single_with(ether_type))
        -> SingleVlanHeader
    {
        result
    }
}

prop_compose! {
    pub fn vlan_double_any()
        (ether_type in ether_type_any())
        (result in vlan_double_with(ether_type))
        -> DoubleVlanHeader
    {
        result
    }
}

prop_compose! {
    pub fn vlan_double_with(ether_type: EtherType)(
        outer_ethertype in vlan_ethertype_any(),
        inner_ethertype in proptest::strategy::Just(ether_type)
    )(
        outer in vlan_single_with(outer_ethertype),
        inner in vlan_single_with(inner_ethertype)
    ) -> DoubleVlanHeader {
        DoubleVlanHeader {
            outer,
            inner
        }
    }
}

prop_compose! {
    pub fn macsec_an_any()(
        an in 0u8..=0b11,
    ) -> MacsecAn
    {
        MacsecAn::try_from(an).unwrap()
    }
}

prop_compose! {
    pub fn macsec_short_len_any()(
        sl in 0u8..=0b0011_1111,
    ) -> MacsecShortLen
    {
        MacsecShortLen::try_from(sl).unwrap()
    }
}

prop_compose! {
    pub fn macsec_any()(
        endstation_id in any::<bool>(),
        scb in any::<bool>(),
        encrypted in any::<bool>(),
        userdata_changed in any::<bool>(),
        an in macsec_an_any(),
        short_len in macsec_short_len_any(),
        packet_nr in any::<u32>(),
        sci_present in any::<bool>(),
        sci in any::<u64>(),
        next_ether_type in ether_type_any()
    ) -> MacsecHeader
    {
        MacsecHeader {
            ptype: if encrypted {
                if userdata_changed {
                    MacsecPType::Encrypted
                } else {
                    MacsecPType::EncryptedUnmodified
                }
            } else {
                if userdata_changed {
                    MacsecPType::Modified
                } else {
                    MacsecPType::Unmodified(next_ether_type)
                }
            },
            endstation_id,
            scb,
            an,
            short_len,
            packet_nr,
            sci: if sci_present {
                Some(sci)
            } else {
                None
            },
        }
    }
}

prop_compose! {
    pub fn macsec_unknown()(
        endstation_id in any::<bool>(),
        scb in any::<bool>(),
        encrypted in any::<bool>(),
        userdata_changed in any::<bool>(),
        an in macsec_an_any(),
        short_len in macsec_short_len_any(),
        packet_nr in any::<u32>(),
        sci_present in any::<bool>(),
        sci in any::<u64>(),
        next_ether_type in ether_type_any().prop_filter("ether_type must be unknown",
            |v| !ETHERNET_KNOWN_ETHER_TYPES.iter().any(|&x| v == &x)))
        -> MacsecHeader
    {
        MacsecHeader {
            ptype: if encrypted {
                if userdata_changed {
                    MacsecPType::Encrypted
                } else {
                    MacsecPType::EncryptedUnmodified
                }
            } else {
                if userdata_changed {
                    MacsecPType::Modified
                } else {
                    MacsecPType::Unmodified(next_ether_type)
                }
            },
            endstation_id,
            scb,
            an,
            short_len,
            packet_nr,
            sci: if sci_present {
                Some(sci)
            } else {
                None
            },
        }
    }
}

prop_compose! {
    pub fn arp_packet_any()
    (
        hw_addr_size in any::<u8>(),
        proto_addr_size in any::<u8>()
    )
    (
        hw_addr_type in any::<u16>(),
        proto_addr_type in any::<u16>(),
        operation in any::<u16>(),
        sender_hw_addr in prop::collection::vec(any::<u8>(), hw_addr_size as usize),
        sender_protocol_addr in prop::collection::vec(any::<u8>(), proto_addr_size as usize),
        target_hw_addr in prop::collection::vec(any::<u8>(), hw_addr_size as usize),
        target_protocol_addr in prop::collection::vec(any::<u8>(), proto_addr_size as usize)
    ) -> ArpPacket
    {
        ArpPacket::new(
            ArpHardwareId(hw_addr_type),
            EtherType(proto_addr_type),
            ArpOperation(operation),
            &sender_hw_addr[..],
            &sender_protocol_addr[..],
            &target_hw_addr[..],
            &target_protocol_addr[..]
        ).unwrap()
    }
}

prop_compose! {
    pub fn arp_eth_ipv4_packet_any()
    (
        operation in any::<u16>(),
        sender_mac in prop::array::uniform6(any::<u8>()),
        sender_ipv4 in prop::array::uniform4(any::<u8>()),
        target_mac in prop::array::uniform6(any::<u8>()),
        target_ipv4 in prop::array::uniform4(any::<u8>())
    ) -> ArpEthIpv4Packet
    {
        ArpEthIpv4Packet {
            operation: ArpOperation(operation),
            sender_mac,
            sender_ipv4,
            target_mac,
            target_ipv4,
        }
    }
}

prop_compose! {
    pub fn ipv4_options_any()
    (
        len_div_4 in 0u8..10,
        options_part0 in prop::array::uniform32(any::<u8>()),
        options_part1 in prop::array::uniform8(any::<u8>())
    ) -> Ipv4Options
    {
        let mut options: [u8;40] = [0;40];
        //copy together 40 bytes of random data (the limit for static arrays in proptest 32,
        //so a 32 & 8 byte array get combined here)
        let len = usize::from(len_div_4)*4;
        if len > 0 {
            let sub_len = std::cmp::min(len,32);
            options[..sub_len].copy_from_slice(&options_part0[..sub_len]);
        }
        if len > 32 {
            let sub_len = len - 32;
            options[32..len].copy_from_slice(&options_part1[..sub_len]);
        }

        //set the options
        (&options[..len]).try_into().unwrap()
    }
}

prop_compose! {
    pub fn ipv4_with(protocol: IpNumber)
    (
        protocol in proptest::strategy::Just(protocol),
        options in ipv4_options_any()
    )(
        source in prop::array::uniform4(any::<u8>()),
        destination in prop::array::uniform4(any::<u8>()),
        dscp in 0u8..=0b0011_1111,
        ecn in 0u8..=0b0000_0011,
        identification in any::<u16>(),
        time_to_live in any::<u8>(),
        dont_fragment in any::<bool>(),
        more_fragments in any::<bool>(),
        fragment_offset in prop::bits::u16::between(0, 13),
        header_checksum in any::<u16>(),
        total_len in ((Ipv4Header::MIN_LEN + usize::from(options.len())) as u16)..core::u16::MAX,
        protocol in proptest::strategy::Just(protocol),
        options in proptest::strategy::Just(options)
    ) -> Ipv4Header
    {
        Ipv4Header{
            dscp: dscp.try_into().unwrap(),
            ecn: ecn.try_into().unwrap(),
            total_len,
            identification,
            dont_fragment,
            more_fragments,
            fragment_offset: fragment_offset.try_into().unwrap(),
            time_to_live,
            protocol,
            header_checksum,
            source,
            destination,
            options
        }
    }
}
prop_compose! {
    pub fn ipv4_any()
               (protocol in ip_number_any())
               (result in ipv4_with(protocol))
               -> Ipv4Header
    {
        result
    }
}

static IPV4_KNOWN_PROTOCOLS: &[IpNumber] = &[
    ip_number::ICMP,
    ip_number::UDP,
    ip_number::TCP,
    ip_number::AUTH,
    ip_number::IPV6_ICMP,
];

prop_compose! {
    pub fn ipv4_unknown()
        (protocol in ip_number_any().prop_filter("protocol must be unknown",
            |v| !IPV4_KNOWN_PROTOCOLS.iter().any(|&x| v == &x))
        )
        (header in ipv4_with(protocol)
    ) -> Ipv4Header
    {
        header
    }
}

prop_compose! {
    pub fn ipv4_extensions_with(next_header: IpNumber)
    (
        has_auth in any::<bool>(),
        auth in ip_auth_with(next_header)
    ) -> Ipv4Extensions
    {
        if has_auth {
            Ipv4Extensions{
                auth: Some(auth),
            }
        } else {
            Ipv4Extensions{
                auth: None,
            }
        }
    }
}

prop_compose! {
    pub fn ipv4_extensions_any()
               (protocol in ip_number_any())
               (result in ipv4_extensions_with(protocol))
               -> Ipv4Extensions
    {
        result
    }
}

prop_compose! {
    pub fn ipv4_extensions_unknown()
        (
            next_header in ip_number_any().prop_filter(
                "next_header must be unknown",
                |v| !IPV4_KNOWN_PROTOCOLS.iter().any(|&x| v == &x)
            )
        ) (
            result in ipv4_extensions_with(next_header)
        ) -> Ipv4Extensions
    {
        result
    }
}

prop_compose! {
    pub fn ipv6_with(next_header: IpNumber)
    (
        source in prop::array::uniform16(any::<u8>()),
        dest in prop::array::uniform16(any::<u8>()),
        traffic_class in any::<u8>(),
        flow_label in ipv6_flow_label_any(),
        payload_length in any::<u16>(),
        hop_limit in any::<u8>(),
        next_header in proptest::strategy::Just(next_header)
    ) -> Ipv6Header
    {
        Ipv6Header {
            traffic_class: traffic_class,
            flow_label: flow_label,
            payload_length: payload_length,
            next_header: next_header,
            hop_limit: hop_limit,
            source: source,
            destination: dest
        }
    }
}

prop_compose! {
    pub fn ipv6_any()
        (next_header in ip_number_any())
        (result in ipv6_with(next_header)
    ) -> Ipv6Header
    {
        result
    }
}

static IPV6_KNOWN_NEXT_HEADERS: &[IpNumber] = &[
    ip_number::ICMP,
    ip_number::UDP,
    ip_number::TCP,
    ip_number::IPV6_HOP_BY_HOP,
    ip_number::IPV6_ICMP,
    ip_number::IPV6_ROUTE,
    ip_number::IPV6_FRAG,
    ip_number::AUTH,
    ip_number::IPV6_DEST_OPTIONS,
    ip_number::MOBILITY,
    ip_number::HIP,
    ip_number::SHIM6,
    // currently not supported:
    // - EncapsulatingSecurityPayload
    // - ExperimentalAndTesting0
    // - ExperimentalAndTesting1
];

prop_compose! {
    pub fn ipv6_unknown()(
        source in prop::array::uniform16(any::<u8>()),
        destination in prop::array::uniform16(any::<u8>()),
        traffic_class in any::<u8>(),
        flow_label in ipv6_flow_label_any(),
        payload_length in any::<u16>(),
        hop_limit in any::<u8>(),
        next_header in ip_number_any().prop_filter("next_header must be unknown",
            |v| !IPV6_KNOWN_NEXT_HEADERS.iter().any(|&x| v == &x))
    ) -> Ipv6Header
    {
        Ipv6Header {
            traffic_class,
            flow_label,
            payload_length,
            next_header,
            hop_limit,
            source,
            destination,
        }
    }
}

prop_compose! {
    pub fn ipv6_raw_ext_with(
        next_header: IpNumber,
        len: u8
    ) (
        next_header in proptest::strategy::Just(next_header),
        payload in proptest::collection::vec(any::<u8>(), (len as usize)*8 + 6)
    ) -> Ipv6RawExtHeader
    {
        Ipv6RawExtHeader::new_raw(
            next_header,
            &payload[..]
        ).unwrap()
    }
}

prop_compose! {
    pub fn ipv6_raw_ext_any()
        (
            next_header in ip_number_any(),
            len in any::<u8>()
        ) (
            result in ipv6_raw_ext_with(next_header, len)
    ) -> Ipv6RawExtHeader
    {
        result
    }
}

prop_compose! {
    pub fn ipv6_extensions_with(next_header: IpNumber)
    (
        has_hop_by_hop_options in any::<bool>(),
        hop_by_hop_options in ipv6_raw_ext_any(),
        has_destination_options in any::<bool>(),
        destination_options in ipv6_raw_ext_any(),
        has_routing in any::<bool>(),
        routing in ipv6_raw_ext_any(),
        has_fragment in any::<bool>(),
        fragment in ipv6_fragment_any(),
        has_auth in any::<bool>(),
        auth in ip_auth_with(next_header),
        has_final_destination_options in any::<bool>(),
        final_destination_options in ipv6_raw_ext_any()
    ) -> Ipv6Extensions
    {
        let mut result = Ipv6Extensions {
            hop_by_hop_options: if has_hop_by_hop_options {
                Some(hop_by_hop_options)
            } else {
                None
            },
            destination_options: if has_destination_options {
                Some(destination_options)
            } else {
                None
            },
            routing: if has_routing {
                Some(
                    Ipv6RoutingExtensions{
                        routing,
                        final_destination_options: if has_final_destination_options {
                            Some(final_destination_options)
                        } else {
                            None
                        }
                    }
                )
            } else {
                None
            },
            fragment: if has_fragment {
                Some(fragment)
            } else {
                None
            },
            auth: if has_auth {
                Some(auth)
            } else {
                None
            },
        };
        result.set_next_headers(next_header);
        result
    }
}

prop_compose! {
    pub fn ipv6_extensions_any()
        (
            next_header in ip_number_any()
        ) (
            result in ipv6_extensions_with(next_header)
    ) -> Ipv6Extensions
    {
        result
    }
}

prop_compose! {
    pub fn ipv6_extensions_unknown()
        (
            next_header in ip_number_any().prop_filter(
                "next_header must be unknown",
                |v| !IPV6_KNOWN_NEXT_HEADERS.iter().any(|&x| v == &x)
            )
        ) (
            result in ipv6_extensions_with(next_header)
        ) -> Ipv6Extensions
    {
        result
    }
}

prop_compose! {
    pub fn ipv6_fragment_with(
        next_header: IpNumber
    ) (
        next_header in proptest::strategy::Just(next_header),
        fragment_offset in 0u16..=0b0001_1111_1111_1111u16,
        more_fragments in any::<bool>(),
        identification in any::<u32>(),
    ) -> Ipv6FragmentHeader
    {
        Ipv6FragmentHeader::new(
            next_header,
            fragment_offset.try_into().unwrap(),
            more_fragments,
            identification
        )
    }
}

prop_compose! {
    pub fn ipv6_fragment_any()
        (next_header in ip_number_any())
        (result in ipv6_fragment_with(next_header)
    ) -> Ipv6FragmentHeader
    {
        result
    }
}

prop_compose! {
    pub fn ip_auth_with(
        next_header: IpNumber
    ) (
        next_header in proptest::strategy::Just(next_header),
        len in 1..0xffu8
    ) (
        next_header in proptest::strategy::Just(next_header),
        spi in any::<u32>(),
        sequence_number in any::<u32>(),
        icv in proptest::collection::vec(any::<u8>(), (len as usize)*4)
    ) -> IpAuthHeader {
        IpAuthHeader::new(
            next_header,
            spi,
            sequence_number,
            &icv
        ).unwrap()
    }
}

prop_compose! {
    pub fn ip_auth_any() (
        next_header in ip_number_any()
    ) (
        header in ip_auth_with(next_header)
    ) -> IpAuthHeader {
        header
    }
}

prop_compose! {
    pub fn udp_any()(
            source_port in any::<u16>(),
            destination_port in any::<u16>(),
            length in any::<u16>(),
            checksum in any::<u16>())
        -> UdpHeader
    {
        UdpHeader {
            source_port: source_port,
            destination_port: destination_port,
            length: length,
            checksum: checksum
        }
    }
}

prop_compose! {
    pub fn tcp_any()
        (data_offset in TcpHeader::MIN_DATA_OFFSET..(TcpHeader::MAX_DATA_OFFSET + 1))
        (
            source_port in any::<u16>(),
            destination_port in any::<u16>(),
            sequence_number in any::<u32>(),
            acknowledgment_number in any::<u32>(),
            ns in any::<bool>(),
            fin in any::<bool>(),
            syn in any::<bool>(),
            rst in any::<bool>(),
            psh in any::<bool>(),
            ack in any::<bool>(),
            ece in any::<bool>(),
            urg in any::<bool>(),
            cwr  in any::<bool>(),
            window_size in any::<u16>(),
            checksum in any::<u16>(),
            urgent_pointer in any::<u16>(),
            options in proptest::collection::vec(any::<u8>(), ((data_offset - 5) as usize)*4))
        -> TcpHeader
    {
        let mut result = TcpHeader::new(source_port, destination_port, sequence_number, window_size);
        result.acknowledgment_number = acknowledgment_number;
        result.ns = ns;
        result.fin = fin;
        result.syn = syn;
        result.rst = rst;
        result.psh = psh;
        result.ack = ack;
        result.ece = ece;
        result.urg = urg;
        result.cwr = cwr;
        result.checksum = checksum;
        result.urgent_pointer = urgent_pointer;
        result.set_options_raw(&options[..]).unwrap();
        result
    }
}

prop_compose! {
    pub fn tcp_options_any()
    (data_offset in TcpHeader::MIN_DATA_OFFSET..(TcpHeader::MAX_DATA_OFFSET + 1))
    (
        options in proptest::collection::vec(any::<u8>(), ((data_offset - 5) as usize)*4)
    ) -> TcpOptions
    {
        TcpOptions::try_from_slice(&options).unwrap()
    }
}

prop_compose! {
    pub fn icmpv4_type_any()
        (
            bytes in any::<[u8;20]>(),
        ) -> Icmpv4Type
    {
        Icmpv4Header::from_slice(&bytes).unwrap().0.icmp_type
    }
}

prop_compose! {
    pub fn icmpv4_header_any()
        (
            bytes in any::<[u8;20]>(),
        ) -> Icmpv4Header
    {
        Icmpv4Header::from_slice(&bytes).unwrap().0
    }
}

prop_compose! {
    pub fn icmpv6_type_any()
        (
            bytes in any::<[u8;8]>(),
        ) -> Icmpv6Type
    {
        Icmpv6Header::from_slice(&bytes).unwrap().0.icmp_type
    }
}

prop_compose! {
    pub fn icmpv6_header_any()
        (
            bytes in any::<[u8;8]>(),
        ) -> Icmpv6Header
    {
        Icmpv6Header::from_slice(&bytes).unwrap().0
    }
}