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
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
use crate::{
    err::{ipv6_exts::*, Layer},
    *,
};

/// IPv6 extension headers present after the ip header.
///
/// Currently supported:
///
/// * Authentication Header
/// * Hop by Hop Options Header
/// * Destination Options Header (before and after routing headers)
/// * Routing Header
/// * Fragment
/// * Authentication Header
///
/// Currently not supported:
///
/// * Encapsulating Security Payload Header (ESP)
/// * Host Identity Protocol (HIP)
/// * IP Mobility
/// * Site Multihoming by IPv6 Intermediation (SHIM6)
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct Ipv6Extensions {
    pub hop_by_hop_options: Option<Ipv6RawExtHeader>,
    pub destination_options: Option<Ipv6RawExtHeader>,
    pub routing: Option<Ipv6RoutingExtensions>,
    pub fragment: Option<Ipv6FragmentHeader>,
    pub auth: Option<IpAuthHeader>,
}

impl Ipv6Extensions {
    /// Minimum length required for extension header in bytes/octets.
    /// Which is zero as no extension headers are required.
    pub const MIN_LEN: usize = 0;

    /// Maximum summed up length of all extension headers in bytes/octets.
    pub const MAX_LEN: usize = Ipv6RawExtHeader::MAX_LEN * 2
        + Ipv6RoutingExtensions::MAX_LEN
        + Ipv6FragmentHeader::LEN
        + IpAuthHeader::MAX_LEN;

    /// Reads as many extension headers as possible from the slice.
    ///
    /// Returns the found ipv6 extension headers, the next header ip number after the read
    /// headers and a slice containing the rest of the packet after the read headers.
    ///
    /// Note that this function can only handle ipv6 extensions if each extension header does
    /// occur at most once, except for destination options headers which are allowed to
    /// exist once in front of a routing header and once after a routing header.
    ///
    /// In case that more extension headers then can fit into a `Ipv6Extensions` struct are
    /// encountered, the parsing is stoped at the point where the data would no longer fit into
    /// the struct. In such a scenario a struct with the data that could be parsed is returned
    /// together with the next header ip number and slice containing the unparsed data.
    ///
    /// It is in the responsibility of the caller to handle a scenario like this.
    ///
    /// The reason that no error is generated, is that even though according to RFC 8200 packets
    /// "should" not contain more then one occurence of an extension header the RFC also specifies
    /// that "IPv6 nodes must accept and attempt to process extension headers in any order and
    /// occurring any number of times in the same packet". So packets with multiple headers "should"
    /// not exist, but are still valid IPv6 packets. As such this function does not generate a
    /// parsing error, as it is not an invalid packet, but if packets like these are encountered
    /// the user of this function has to themself decide how to handle packets like these.
    ///
    /// The only exception is if an hop by hop header is located somewhere else then directly at
    /// the start. In this case an `ReadError::Ipv6HopByHopHeaderNotAtStart` error is generated as
    /// the hop by hop header is required to be located directly after the IPv6 header according
    /// to RFC 8200.
    pub fn from_slice(
        start_ip_number: IpNumber,
        slice: &[u8],
    ) -> Result<(Ipv6Extensions, IpNumber, &[u8]), err::ipv6_exts::HeaderSliceError> {
        let mut result: Ipv6Extensions = Default::default();
        let mut rest = slice;
        let mut next_header = start_ip_number;

        use err::ipv6_exts::{HeaderError::*, HeaderSliceError::*};
        use ip_number::*;

        // the hop by hop header is required to occur directly after the ipv6 header
        if IPV6_HOP_BY_HOP == next_header {
            let slice = Ipv6RawExtHeaderSlice::from_slice(rest).map_err(Len)?;
            rest = &rest[slice.slice().len()..];
            next_header = slice.next_header();
            result.hop_by_hop_options = Some(slice.to_header());
        }

        loop {
            match next_header {
                IPV6_HOP_BY_HOP => {
                    return Err(Content(HopByHopNotAtStart));
                }
                IPV6_DEST_OPTIONS => {
                    if let Some(ref mut routing) = result.routing {
                        // if the routing header is already present
                        // this this a "final destination options" header
                        if routing.final_destination_options.is_some() {
                            // more then one header of this type found -> abort parsing
                            return Ok((result, next_header, rest));
                        } else {
                            let slice = Ipv6RawExtHeaderSlice::from_slice(rest)
                                .map_err(|err| Len(err.add_offset(slice.len() - rest.len())))?;
                            rest = &rest[slice.slice().len()..];
                            next_header = slice.next_header();
                            routing.final_destination_options = Some(slice.to_header());
                        }
                    } else if result.destination_options.is_some() {
                        // more then one header of this type found -> abort parsing
                        return Ok((result, next_header, rest));
                    } else {
                        let slice = Ipv6RawExtHeaderSlice::from_slice(rest)
                            .map_err(|err| Len(err.add_offset(slice.len() - rest.len())))?;
                        rest = &rest[slice.slice().len()..];
                        next_header = slice.next_header();
                        result.destination_options = Some(slice.to_header());
                    }
                }
                IPV6_ROUTE => {
                    if result.routing.is_some() {
                        // more then one header of this type found -> abort parsing
                        return Ok((result, next_header, rest));
                    } else {
                        let slice = Ipv6RawExtHeaderSlice::from_slice(rest)
                            .map_err(|err| Len(err.add_offset(slice.len() - rest.len())))?;
                        rest = &rest[slice.slice().len()..];
                        next_header = slice.next_header();
                        result.routing = Some(Ipv6RoutingExtensions {
                            routing: slice.to_header(),
                            final_destination_options: None,
                        });
                    }
                }
                IPV6_FRAG => {
                    if result.fragment.is_some() {
                        // more then one header of this type found -> abort parsing
                        return Ok((result, next_header, rest));
                    } else {
                        let slice = Ipv6FragmentHeaderSlice::from_slice(rest)
                            .map_err(|err| Len(err.add_offset(slice.len() - rest.len())))?;
                        rest = &rest[slice.slice().len()..];
                        next_header = slice.next_header();
                        result.fragment = Some(slice.to_header());
                    }
                }
                AUTH => {
                    if result.auth.is_some() {
                        // more then one header of this type found -> abort parsing
                        return Ok((result, next_header, rest));
                    } else {
                        let slice = IpAuthHeaderSlice::from_slice(rest).map_err(|err| {
                            use err::ip_auth::HeaderSliceError as I;
                            use err::ipv6_exts::HeaderError as O;
                            match err {
                                I::Len(err) => Len(err.add_offset(slice.len() - rest.len())),
                                I::Content(err) => Content(O::IpAuth(err)),
                            }
                        })?;
                        rest = &rest[slice.slice().len()..];
                        next_header = slice.next_header();
                        result.auth = Some(slice.to_header());
                    }
                }
                _ => {
                    // done parsing, the next header is not a known header extension
                    return Ok((result, next_header, rest));
                }
            }
        }
        //should not be hit
    }

    /// Reads as many extension headers as possible from the slice until a non IPv6 extension
    /// header or an error gets encountered.
    ///
    /// This function differs from [`Ipv6Extensions::from_slice`] in that it returns the successfully
    /// parsed parts together with the error. While [`Ipv6Extensions::from_slice`] only returns an
    /// error.
    ///
    /// Note that this function (same as [`Ipv6Extensions::from_slice`]) will stop parsing as soon
    /// as more headers then can be stored in [`Ipv6Extensions`] are encountered. E.g. if there is
    /// more then one "auth" header the function returns as soon as the second "auth" header is
    /// encountered.
    pub fn from_slice_lax(
        start_ip_number: IpNumber,
        slice: &[u8],
    ) -> (
        Ipv6Extensions,
        IpNumber,
        &[u8],
        Option<(err::ipv6_exts::HeaderSliceError, err::Layer)>,
    ) {
        let mut result: Ipv6Extensions = Default::default();
        let mut rest = slice;
        let mut next_header = start_ip_number;

        use err::ipv6_exts::{HeaderError::*, HeaderSliceError::*};
        use ip_number::*;

        // the hop by hop header is required to occur directly after the ipv6 header
        if IPV6_HOP_BY_HOP == next_header {
            match Ipv6RawExtHeaderSlice::from_slice(rest) {
                Ok(slice) => {
                    rest = &rest[slice.slice().len()..];
                    next_header = slice.next_header();
                    result.hop_by_hop_options = Some(slice.to_header());
                }
                Err(error) => {
                    return (
                        result,
                        next_header,
                        rest,
                        Some((Len(error), Layer::Ipv6HopByHopHeader)),
                    );
                }
            }
        }

        loop {
            match next_header {
                IPV6_HOP_BY_HOP => {
                    return (
                        result,
                        next_header,
                        rest,
                        Some((Content(HopByHopNotAtStart), Layer::Ipv6HopByHopHeader)),
                    );
                }
                IPV6_DEST_OPTIONS => {
                    if let Some(ref mut routing) = result.routing {
                        // if the routing header is already present
                        // this this a "final destination options" header
                        if routing.final_destination_options.is_some() {
                            // more then one header of this type found -> abort parsing
                            return (result, next_header, rest, None);
                        } else {
                            match Ipv6RawExtHeaderSlice::from_slice(rest) {
                                Ok(slice) => {
                                    rest = &rest[slice.slice().len()..];
                                    next_header = slice.next_header();
                                    routing.final_destination_options = Some(slice.to_header());
                                }
                                Err(err) => {
                                    return (
                                        result,
                                        next_header,
                                        rest,
                                        Some((
                                            Len(err.add_offset(slice.len() - rest.len())),
                                            Layer::Ipv6DestOptionsHeader,
                                        )),
                                    );
                                }
                            }
                        }
                    } else if result.destination_options.is_some() {
                        // more then one header of this type found -> abort parsing
                        return (result, next_header, rest, None);
                    } else {
                        match Ipv6RawExtHeaderSlice::from_slice(rest) {
                            Ok(slice) => {
                                rest = &rest[slice.slice().len()..];
                                next_header = slice.next_header();
                                result.destination_options = Some(slice.to_header());
                            }
                            Err(err) => {
                                return (
                                    result,
                                    next_header,
                                    rest,
                                    Some((
                                        Len(err.add_offset(slice.len() - rest.len())),
                                        Layer::Ipv6DestOptionsHeader,
                                    )),
                                );
                            }
                        }
                    }
                }
                IPV6_ROUTE => {
                    if result.routing.is_some() {
                        // more then one header of this type found -> abort parsing
                        return (result, next_header, rest, None);
                    } else {
                        match Ipv6RawExtHeaderSlice::from_slice(rest) {
                            Ok(slice) => {
                                rest = &rest[slice.slice().len()..];
                                next_header = slice.next_header();
                                result.routing = Some(Ipv6RoutingExtensions {
                                    routing: slice.to_header(),
                                    final_destination_options: None,
                                });
                            }
                            Err(err) => {
                                return (
                                    result,
                                    next_header,
                                    rest,
                                    Some((
                                        Len(err.add_offset(slice.len() - rest.len())),
                                        Layer::Ipv6RouteHeader,
                                    )),
                                );
                            }
                        }
                    }
                }
                IPV6_FRAG => {
                    if result.fragment.is_some() {
                        // more then one header of this type found -> abort parsing
                        return (result, next_header, rest, None);
                    } else {
                        match Ipv6FragmentHeaderSlice::from_slice(rest) {
                            Ok(slice) => {
                                rest = &rest[slice.slice().len()..];
                                next_header = slice.next_header();
                                result.fragment = Some(slice.to_header());
                            }
                            Err(err) => {
                                return (
                                    result,
                                    next_header,
                                    rest,
                                    Some((
                                        Len(err.add_offset(slice.len() - rest.len())),
                                        Layer::Ipv6FragHeader,
                                    )),
                                );
                            }
                        }
                    }
                }
                AUTH => {
                    if result.auth.is_some() {
                        // more then one header of this type found -> abort parsing
                        return (result, next_header, rest, None);
                    } else {
                        match IpAuthHeaderSlice::from_slice(rest) {
                            Ok(slice) => {
                                rest = &rest[slice.slice().len()..];
                                next_header = slice.next_header();
                                result.auth = Some(slice.to_header());
                            }
                            Err(err) => {
                                use err::ip_auth::HeaderSliceError as I;
                                use err::ipv6_exts::HeaderError as O;
                                return (
                                    result,
                                    next_header,
                                    rest,
                                    Some((
                                        match err {
                                            I::Len(err) => {
                                                Len(err.add_offset(slice.len() - rest.len()))
                                            }
                                            I::Content(err) => Content(O::IpAuth(err)),
                                        },
                                        Layer::IpAuthHeader,
                                    )),
                                );
                            }
                        }
                    }
                }
                _ => {
                    // done parsing, the next header is not a known header extension
                    return (result, next_header, rest, None);
                }
            }
        }
        //should not be hit
    }

    /// Reads as many extension headers as possible from the reader and returns the found ipv6
    /// extension headers and the next header ip number.
    ///
    /// If no extension headers are present an unfilled struct and the original `first_header`
    /// ip number is returned.
    ///
    /// Note that this function can only handle ipv6 extensions if each extension header does
    /// occur at most once, except for destination options headers which are allowed to
    /// exist once in front of a routing header and once after a routing header.
    ///
    /// In case that more extension headers then can fit into a `Ipv6Extensions` struct are
    /// encountered, the parsing is stoped at the point where the data would no longer fit into
    /// the struct. In such a scenario a struct with the data that could be parsed is returned
    /// together with the next header ip number that identfies which header could be read next.
    ///
    /// It is in the responsibility of the caller to handle a scenario like this.
    ///
    /// The reason that no error is generated, is that even though according to RFC 8200, packets
    /// "should" not contain more then one occurence of an extension header, the RFC also specifies
    /// that "IPv6 nodes must accept and attempt to process extension headers in any order and
    /// occurring any number of times in the same packet". So packets with multiple headers "should"
    /// not exist, but are still valid IPv6 packets. As such this function does not generate a
    /// parsing error, as it is not an invalid packet, but if packets like these are encountered
    /// the user of this function has to themself decide how to handle packets like these.
    ///
    /// The only exception is if an hop by hop header is located somewhere else then directly at
    /// the start. In this case an `ReadError::Ipv6HopByHopHeaderNotAtStart` error is generated as
    /// the hop by hop header is required to be located directly after the IPv6 header according
    /// to RFC 8200.
    #[cfg(feature = "std")]
    #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
    pub fn read<T: std::io::Read + std::io::Seek + Sized>(
        reader: &mut T,
        start_ip_number: IpNumber,
    ) -> Result<(Ipv6Extensions, IpNumber), err::ipv6_exts::HeaderReadError> {
        let mut result: Ipv6Extensions = Default::default();
        let mut next_protocol = start_ip_number;

        use err::ipv6_exts::{HeaderError::*, HeaderReadError::*};
        use ip_number::*;

        // the hop by hop header is required to occur directly after the ipv6 header
        if IPV6_HOP_BY_HOP == next_protocol {
            let header = Ipv6RawExtHeader::read(reader).map_err(Io)?;
            next_protocol = header.next_header;
            result.hop_by_hop_options = Some(header);
        }

        loop {
            match next_protocol {
                IPV6_HOP_BY_HOP => {
                    return Err(Content(HopByHopNotAtStart));
                }
                IPV6_DEST_OPTIONS => {
                    if let Some(ref mut routing) = result.routing {
                        // if the routing header is already present
                        // asume this is a "final destination options" header
                        if routing.final_destination_options.is_some() {
                            // more then one header of this type found -> abort parsing
                            return Ok((result, next_protocol));
                        } else {
                            let header = Ipv6RawExtHeader::read(reader).map_err(Io)?;
                            next_protocol = header.next_header;
                            routing.final_destination_options = Some(header);
                        }
                    } else if result.destination_options.is_some() {
                        // more then one header of this type found -> abort parsing
                        return Ok((result, next_protocol));
                    } else {
                        let header = Ipv6RawExtHeader::read(reader).map_err(Io)?;
                        next_protocol = header.next_header;
                        result.destination_options = Some(header);
                    }
                }
                IPV6_ROUTE => {
                    if result.routing.is_some() {
                        // more then one header of this type found -> abort parsing
                        return Ok((result, next_protocol));
                    } else {
                        let header = Ipv6RawExtHeader::read(reader).map_err(Io)?;
                        next_protocol = header.next_header;
                        result.routing = Some(Ipv6RoutingExtensions {
                            routing: header,
                            final_destination_options: None,
                        });
                    }
                }
                IPV6_FRAG => {
                    if result.fragment.is_some() {
                        // more then one header of this type found -> abort parsing
                        return Ok((result, next_protocol));
                    } else {
                        let header = Ipv6FragmentHeader::read(reader).map_err(Io)?;
                        next_protocol = header.next_header;
                        result.fragment = Some(header);
                    }
                }
                AUTH => {
                    if result.auth.is_some() {
                        // more then one header of this type found -> abort parsing
                        return Ok((result, next_protocol));
                    } else {
                        let header = IpAuthHeader::read(reader).map_err(|err| {
                            use err::ip_auth::HeaderReadError as I;
                            match err {
                                I::Io(err) => Io(err),
                                I::Content(err) => Content(IpAuth(err)),
                            }
                        })?;
                        next_protocol = header.next_header;
                        result.auth = Some(header);
                    }
                }
                _ => {
                    // done parsing, the next header is not a known header extension
                    return Ok((result, next_protocol));
                }
            }
        }

        //should not be hit
    }

    /// Reads as many extension headers as possible from the limited reader and returns the found ipv6
    /// extension headers and the next header ip number.
    ///
    /// If no extension headers are present an unfilled struct and the original `first_header`
    /// ip number is returned.
    ///
    /// Note that this function can only handle ipv6 extensions if each extension header does
    /// occur at most once, except for destination options headers which are allowed to
    /// exist once in front of a routing header and once after a routing header.
    ///
    /// In case that more extension headers then can fit into a `Ipv6Extensions` struct are
    /// encountered, the parsing is stoped at the point where the data would no longer fit into
    /// the struct. In such a scenario a struct with the data that could be parsed is returned
    /// together with the next header ip number that identfies which header could be read next.
    ///
    /// It is in the responsibility of the caller to handle a scenario like this.
    ///
    /// The reason that no error is generated, is that even though according to RFC 8200, packets
    /// "should" not contain more then one occurence of an extension header, the RFC also specifies
    /// that "IPv6 nodes must accept and attempt to process extension headers in any order and
    /// occurring any number of times in the same packet". So packets with multiple headers "should"
    /// not exist, but are still valid IPv6 packets. As such this function does not generate a
    /// parsing error, as it is not an invalid packet, but if packets like these are encountered
    /// the user of this function has to themself decide how to handle packets like these.
    ///
    /// The only exception is if an hop by hop header is located somewhere else then directly at
    /// the start. In this case an `ReadError::Ipv6HopByHopHeaderNotAtStart` error is generated as
    /// the hop by hop header is required to be located directly after the IPv6 header according
    /// to RFC 8200.
    #[cfg(feature = "std")]
    #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
    pub fn read_limited<T: std::io::Read + std::io::Seek + Sized>(
        reader: &mut crate::io::LimitedReader<T>,
        start_ip_number: IpNumber,
    ) -> Result<(Ipv6Extensions, IpNumber), HeaderLimitedReadError> {
        use ip_number::*;
        use HeaderError::*;
        use HeaderLimitedReadError::*;

        fn map_limited_err(err: err::io::LimitedReadError) -> HeaderLimitedReadError {
            use crate::err::io::LimitedReadError as I;
            match err {
                I::Io(err) => Io(err),
                I::Len(err) => Len(err),
            }
        }

        // start decoding
        let mut result: Ipv6Extensions = Default::default();
        let mut next_protocol = start_ip_number;

        // the hop by hop header is required to occur directly after the ipv6 header
        if IPV6_HOP_BY_HOP == next_protocol {
            let header = Ipv6RawExtHeader::read_limited(reader).map_err(map_limited_err)?;
            next_protocol = header.next_header;
            result.hop_by_hop_options = Some(header);
        }

        loop {
            match next_protocol {
                IPV6_HOP_BY_HOP => {
                    return Err(Content(HopByHopNotAtStart));
                }
                IPV6_DEST_OPTIONS => {
                    if let Some(ref mut routing) = result.routing {
                        // if the routing header is already present
                        // asume this is a "final destination options" header
                        if routing.final_destination_options.is_some() {
                            // more then one header of this type found -> abort parsing
                            return Ok((result, next_protocol));
                        } else {
                            let header =
                                Ipv6RawExtHeader::read_limited(reader).map_err(map_limited_err)?;
                            next_protocol = header.next_header;
                            routing.final_destination_options = Some(header);
                        }
                    } else if result.destination_options.is_some() {
                        // more then one header of this type found -> abort parsing
                        return Ok((result, next_protocol));
                    } else {
                        let header =
                            Ipv6RawExtHeader::read_limited(reader).map_err(map_limited_err)?;
                        next_protocol = header.next_header;
                        result.destination_options = Some(header);
                    }
                }
                IPV6_ROUTE => {
                    if result.routing.is_some() {
                        // more then one header of this type found -> abort parsing
                        return Ok((result, next_protocol));
                    } else {
                        let header =
                            Ipv6RawExtHeader::read_limited(reader).map_err(map_limited_err)?;
                        next_protocol = header.next_header;
                        result.routing = Some(Ipv6RoutingExtensions {
                            routing: header,
                            final_destination_options: None,
                        });
                    }
                }
                IPV6_FRAG => {
                    if result.fragment.is_some() {
                        // more then one header of this type found -> abort parsing
                        return Ok((result, next_protocol));
                    } else {
                        let header =
                            Ipv6FragmentHeader::read_limited(reader).map_err(map_limited_err)?;
                        next_protocol = header.next_header;
                        result.fragment = Some(header);
                    }
                }
                AUTH => {
                    if result.auth.is_some() {
                        // more then one header of this type found -> abort parsing
                        return Ok((result, next_protocol));
                    } else {
                        let header = IpAuthHeader::read_limited(reader).map_err(|err| {
                            use err::ip_auth::HeaderLimitedReadError as I;
                            match err {
                                I::Io(err) => Io(err),
                                I::Len(err) => Len(err),
                                I::Content(err) => Content(IpAuth(err)),
                            }
                        })?;
                        next_protocol = header.next_header;
                        result.auth = Some(header);
                    }
                }
                _ => {
                    // done parsing, the next header is not a known header extension
                    return Ok((result, next_protocol));
                }
            }
        }

        //should not be hit
    }

    /// Writes the given headers to a writer based on the order defined in
    /// the next_header fields of the headers and the first header_id
    /// passed to this function.
    ///
    /// It is required that all next header are correctly set in the headers
    /// and no other ipv6 header extensions follow this header. If this is not
    /// the case an [`err::ipv6_exts::HeaderWriteError::Content`] error is
    /// returned.
    #[cfg(feature = "std")]
    #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
    pub fn write<T: std::io::Write + Sized>(
        &self,
        writer: &mut T,
        first_header: IpNumber,
    ) -> Result<(), err::ipv6_exts::HeaderWriteError> {
        use err::ipv6_exts::ExtsWalkError::*;
        use err::ipv6_exts::HeaderWriteError::*;
        use ip_number::*;

        /// Struct flagging if a header needs to be written.
        struct NeedsWrite {
            pub hop_by_hop_options: bool,
            pub destination_options: bool,
            pub routing: bool,
            pub fragment: bool,
            pub auth: bool,
            pub final_destination_options: bool,
        }

        let mut needs_write = NeedsWrite {
            hop_by_hop_options: self.hop_by_hop_options.is_some(),
            destination_options: self.destination_options.is_some(),
            routing: self.routing.is_some(),
            fragment: self.fragment.is_some(),
            auth: self.auth.is_some(),
            final_destination_options: if let Some(ref routing) = self.routing {
                routing.final_destination_options.is_some()
            } else {
                false
            },
        };

        let mut next_header = first_header;
        let mut route_written = false;

        // check if hop by hop header should be written first
        if IPV6_HOP_BY_HOP == next_header {
            let header = &self.hop_by_hop_options.as_ref().unwrap();
            header.write(writer).map_err(Io)?;
            next_header = header.next_header;
            needs_write.hop_by_hop_options = false;
        }

        loop {
            match next_header {
                IPV6_HOP_BY_HOP => {
                    // Only trigger a "hop by hop not at start" error
                    // if we actually still have to write a hop by hop header.
                    //
                    // The ip number for hop by hop is 0, which could be used
                    // as a placeholder by user and later replaced. So let's
                    // not be overzealous and allow a next header with hop
                    // by hop if it is not part of this extensions struct.
                    if needs_write.hop_by_hop_options {
                        // the hop by hop header is only allowed at the start
                        return Err(Content(HopByHopNotAtStart));
                    } else {
                        break;
                    }
                }
                IPV6_DEST_OPTIONS => {
                    // the destination options are allowed to be written twice
                    // once before a routing header and once after.
                    if route_written {
                        if needs_write.final_destination_options {
                            let header = &self
                                .routing
                                .as_ref()
                                .unwrap()
                                .final_destination_options
                                .as_ref()
                                .unwrap();
                            header.write(writer).map_err(Io)?;
                            next_header = header.next_header;
                            needs_write.final_destination_options = false;
                        } else {
                            break;
                        }
                    } else if needs_write.destination_options {
                        let header = &self.destination_options.as_ref().unwrap();
                        header.write(writer).map_err(Io)?;
                        next_header = header.next_header;
                        needs_write.destination_options = false;
                    } else {
                        break;
                    }
                }
                IPV6_ROUTE => {
                    if needs_write.routing {
                        let header = &self.routing.as_ref().unwrap().routing;
                        header.write(writer).map_err(Io)?;
                        next_header = header.next_header;
                        needs_write.routing = false;
                        // for destination options
                        route_written = true;
                    } else {
                        break;
                    }
                }
                IPV6_FRAG => {
                    if needs_write.fragment {
                        let header = &self.fragment.as_ref().unwrap();
                        header.write(writer).map_err(Io)?;
                        next_header = header.next_header;
                        needs_write.fragment = false;
                    } else {
                        break;
                    }
                }
                AUTH => {
                    if needs_write.auth {
                        let header = &self.auth.as_ref().unwrap();
                        header.write(writer).map_err(Io)?;
                        next_header = header.next_header;
                        needs_write.auth = false;
                    } else {
                        break;
                    }
                }
                _ => {
                    // reached an unknown next_header id, proceed to check if everything was written
                    break;
                }
            }
        }

        // check that all header have been written
        if needs_write.hop_by_hop_options {
            Err(Content(ExtNotReferenced {
                missing_ext: IpNumber::IPV6_HEADER_HOP_BY_HOP,
            }))
        } else if needs_write.destination_options {
            Err(Content(ExtNotReferenced {
                missing_ext: IpNumber::IPV6_DESTINATION_OPTIONS,
            }))
        } else if needs_write.routing {
            Err(Content(ExtNotReferenced {
                missing_ext: IpNumber::IPV6_ROUTE_HEADER,
            }))
        } else if needs_write.fragment {
            Err(Content(ExtNotReferenced {
                missing_ext: IpNumber::IPV6_FRAGMENTATION_HEADER,
            }))
        } else if needs_write.auth {
            Err(Content(ExtNotReferenced {
                missing_ext: IpNumber::AUTHENTICATION_HEADER,
            }))
        } else if needs_write.final_destination_options {
            Err(Content(ExtNotReferenced {
                missing_ext: IpNumber::IPV6_DESTINATION_OPTIONS,
            }))
        } else {
            Ok(())
        }
    }

    /// Length of the all present headers in bytes.
    pub fn header_len(&self) -> usize {
        let mut result = 0;

        if let Some(ref header) = self.hop_by_hop_options {
            result += header.header_len();
        }
        if let Some(ref header) = self.destination_options {
            result += header.header_len();
        }
        if let Some(ref header) = self.routing {
            result += header.routing.header_len();
            if let Some(ref header) = header.final_destination_options {
                result += header.header_len();
            }
        }
        if let Some(ref header) = self.fragment {
            result += header.header_len();
        }
        if let Some(ref header) = self.auth {
            result += header.header_len();
        }

        result
    }

    /// Sets all the next_header fields of the headers based on the adviced default order
    /// with the given protocol number as last "next header" value. The return value is the protocol
    /// number of the first existing extension header that should be entered in the ipv6 header as
    /// next_header.
    ///
    /// If no extension headers are present the value of the argument is returned.
    pub fn set_next_headers(&mut self, last_protocol_number: IpNumber) -> IpNumber {
        use ip_number::*;

        let mut next = last_protocol_number;

        // go through the proposed order of extension headers from
        // RFC 8200 backwards. The header order defined in RFC8200 is:
        //
        // * IPv6 header
        // * Hop-by-Hop Options header
        // * Destination Options header
        // * Routing header
        // * Fragment header
        // * Authentication header
        // * Encapsulating Security Payload header
        // * Destination Options header
        // * Upper-Layer header
        //
        if let Some(ref mut routing) = self.routing {
            if let Some(ref mut header) = routing.final_destination_options {
                header.next_header = next;
                next = IPV6_DEST_OPTIONS;
            }
        }
        if let Some(ref mut header) = self.auth {
            header.next_header = next;
            next = AUTH;
        }
        if let Some(ref mut header) = self.fragment {
            header.next_header = next;
            next = IPV6_FRAG;
        }
        if let Some(ref mut routing) = self.routing {
            routing.routing.next_header = next;
            next = IPV6_ROUTE;
        }
        if let Some(ref mut header) = self.destination_options {
            header.next_header = next;
            next = IPV6_DEST_OPTIONS;
        }
        if let Some(ref mut header) = self.hop_by_hop_options {
            header.next_header = next;
            next = IPV6_HOP_BY_HOP;
        }

        next
    }

    /// Return next header based on the extension headers and
    /// the first ip protocol number.
    pub fn next_header(&self, first_next_header: IpNumber) -> Result<IpNumber, ExtsWalkError> {
        use ip_number::*;
        use ExtsWalkError::*;

        /// Struct flagging if a header needs to be referenced.
        struct OutstandingRef {
            pub hop_by_hop_options: bool,
            pub destination_options: bool,
            pub routing: bool,
            pub fragment: bool,
            pub auth: bool,
            pub final_destination_options: bool,
        }

        let mut outstanding_refs = OutstandingRef {
            hop_by_hop_options: self.hop_by_hop_options.is_some(),
            destination_options: self.destination_options.is_some(),
            routing: self.routing.is_some(),
            fragment: self.fragment.is_some(),
            auth: self.auth.is_some(),
            final_destination_options: if let Some(ref routing) = self.routing {
                routing.final_destination_options.is_some()
            } else {
                false
            },
        };

        let mut next = first_next_header;
        let mut route_refed = false;

        // check if hop by hop header should be written first
        if IPV6_HOP_BY_HOP == next {
            if let Some(ref header) = self.hop_by_hop_options {
                next = header.next_header;
                outstanding_refs.hop_by_hop_options = false;
            }
        }

        loop {
            match next {
                IPV6_HOP_BY_HOP => {
                    // Only trigger a "hop by hop not at start" error
                    // if we actually still have to write a hop by hop header.
                    //
                    // The ip number for hop by hop is 0, which could be used
                    // as a placeholder by user and later replaced. So let's
                    // not be overzealous and allow a next header with hop
                    // by hop if it is not part of this extensions struct.
                    if outstanding_refs.hop_by_hop_options {
                        // the hop by hop header is only allowed at the start
                        return Err(HopByHopNotAtStart);
                    } else {
                        break;
                    }
                }
                IPV6_DEST_OPTIONS => {
                    // the destination options are allowed to be written twice
                    // once before a routing header and once after.
                    if route_refed {
                        if outstanding_refs.final_destination_options {
                            let header = &self
                                .routing
                                .as_ref()
                                .unwrap()
                                .final_destination_options
                                .as_ref()
                                .unwrap();
                            next = header.next_header;
                            outstanding_refs.final_destination_options = false;
                        } else {
                            break;
                        }
                    } else if outstanding_refs.destination_options {
                        let header = &self.destination_options.as_ref().unwrap();
                        next = header.next_header;
                        outstanding_refs.destination_options = false;
                    } else {
                        break;
                    }
                }
                IPV6_ROUTE => {
                    if outstanding_refs.routing {
                        let header = &self.routing.as_ref().unwrap().routing;
                        next = header.next_header;
                        outstanding_refs.routing = false;
                        // for destination options
                        route_refed = true;
                    } else {
                        break;
                    }
                }
                IPV6_FRAG => {
                    if outstanding_refs.fragment {
                        let header = &self.fragment.as_ref().unwrap();
                        next = header.next_header;
                        outstanding_refs.fragment = false;
                    } else {
                        break;
                    }
                }
                AUTH => {
                    if outstanding_refs.auth {
                        let header = &self.auth.as_ref().unwrap();
                        next = header.next_header;
                        outstanding_refs.auth = false;
                    } else {
                        break;
                    }
                }
                _ => break,
            }
        }

        // assume all done
        if outstanding_refs.hop_by_hop_options {
            return Err(ExtNotReferenced {
                missing_ext: IpNumber::IPV6_HEADER_HOP_BY_HOP,
            });
        }
        if outstanding_refs.destination_options {
            return Err(ExtNotReferenced {
                missing_ext: IpNumber::IPV6_DESTINATION_OPTIONS,
            });
        }
        if outstanding_refs.routing {
            return Err(ExtNotReferenced {
                missing_ext: IpNumber::IPV6_ROUTE_HEADER,
            });
        }
        if outstanding_refs.fragment {
            return Err(ExtNotReferenced {
                missing_ext: IpNumber::IPV6_FRAGMENTATION_HEADER,
            });
        }
        if outstanding_refs.auth {
            return Err(ExtNotReferenced {
                missing_ext: IpNumber::AUTHENTICATION_HEADER,
            });
        }
        if outstanding_refs.final_destination_options {
            return Err(ExtNotReferenced {
                missing_ext: IpNumber::IPV6_DESTINATION_OPTIONS,
            });
        }

        Ok(next)
    }

    /// Returns true if a fragmentation header is present in
    /// the extensions that fragments the payload.
    ///
    /// Note: A fragmentation header can still be present
    /// even if the return value is false in case the fragmentation
    /// headers don't fragment the payload. This is the case if
    /// the offset of all fragmentation header is 0 and the
    /// more fragment bit is not set.
    #[inline]
    pub fn is_fragmenting_payload(&self) -> bool {
        if let Some(frag) = self.fragment.as_ref() {
            frag.is_fragmenting_payload()
        } else {
            false
        }
    }

    /// Returns true if no IPv6 extension header is present (all fields `None`).
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.hop_by_hop_options.is_none()
            && self.destination_options.is_none()
            && self.routing.is_none()
            && self.fragment.is_none()
            && self.auth.is_none()
    }
}

#[cfg(test)]
pub mod ipv6_exts_test_helpers {
    use super::*;
    use crate::ip_number::*;
    use alloc::vec::Vec;

    /// IP numbers that are assigned ipv6 header extensions.
    pub const EXTENSION_KNOWN_IP_NUMBERS: [IpNumber; 5] = [
        AUTH,
        IPV6_DEST_OPTIONS,
        IPV6_HOP_BY_HOP,
        IPV6_FRAG,
        IPV6_ROUTE,
    ];

    /// Helper struct that generates test data with dummy
    /// extension header data.
    pub struct ExtensionTestPayload {
        pub ip_numbers: Vec<IpNumber>,
        pub lengths: Vec<usize>,
        pub data: Vec<u8>,
    }

    impl ExtensionTestPayload {
        pub fn new(ip_numbers: &[IpNumber], header_sizes: &[u8]) -> ExtensionTestPayload {
            assert!(ip_numbers.len() > 1);
            assert!(header_sizes.len() > 0);

            let mut result = ExtensionTestPayload {
                ip_numbers: ip_numbers.to_vec(),
                lengths: Vec::with_capacity(ip_numbers.len() - 1),
                data: Vec::with_capacity((ip_numbers.len() - 1) * (0xff * 8 + 8)),
            };
            for i in 0..ip_numbers.len() - 1 {
                result.add_payload(
                    ip_numbers[i],
                    ip_numbers[i + 1],
                    header_sizes[i % header_sizes.len()],
                )
            }
            result
        }

        pub fn slice(&self) -> &[u8] {
            &self.data
        }

        fn add_payload(&mut self, ip_number: IpNumber, next_header: IpNumber, header_ext_len: u8) {
            match ip_number {
                IPV6_HOP_BY_HOP | IPV6_ROUTE | IPV6_DEST_OPTIONS => {
                    // insert next header & size
                    let mut raw: [u8; 0xff * 8 + 8] = [0; 0xff * 8 + 8];
                    raw[0] = next_header.0;
                    raw[1] = header_ext_len;

                    // insert payload
                    self.data
                        .extend_from_slice(&raw[..8 + usize::from(header_ext_len) * 8]);
                    self.lengths.push(8 + usize::from(header_ext_len) * 8);
                }
                IPV6_FRAG => {
                    // generate payload
                    let mut raw: [u8; 8] = [0; 8];
                    raw[0] = next_header.0;
                    raw[1] = 0;

                    // insert payload
                    self.data.extend_from_slice(&raw[..8]);
                    self.lengths.push(8);
                }
                AUTH => {
                    let mut raw: [u8; 0xff * 4 + 8] = [0; 0xff * 4 + 8];
                    raw[0] = next_header.0;
                    // authentfication header len is defined as
                    // '32-bit words (4-byteunits), minus "2"'
                    let len = if header_ext_len > 0 {
                        raw[1] = header_ext_len;
                        usize::from(header_ext_len) * 4
                    } else {
                        // auth has a minimum size of 1
                        raw[1] = 1;
                        4
                    } + 8;
                    self.data.extend_from_slice(&raw[..len]);
                    self.lengths.push(len);
                }
                _ => unreachable!(),
            }
        }

        /// Returns true of the payload will trigger a "hop by hop not
        /// at start" error which is not ignored because of an early
        /// parsing abort.
        pub fn exts_hop_by_hop_error(&self) -> bool {
            struct ReadState {
                dest_opt: bool,
                routing: bool,
                final_dest_opt: bool,
                frag: bool,
                auth: bool,
            }

            // state if a header type has already been read
            let mut read = ReadState {
                dest_opt: false,
                routing: false,
                final_dest_opt: false,
                frag: false,
                auth: false,
            };

            for i in 0..self.ip_numbers.len() {
                match self.ip_numbers[i] {
                    IPV6_HOP_BY_HOP => {
                        if i != 0 {
                            return true;
                        }
                    }
                    IPV6_ROUTE => {
                        if read.routing {
                            return false;
                        } else {
                            read.routing = true;
                        }
                    }
                    IPV6_DEST_OPTIONS => {
                        // check the kind of destination options (aka is it before or after the routing header)
                        if read.routing {
                            // final dest opt
                            if read.final_dest_opt {
                                return false;
                            } else {
                                read.final_dest_opt = true;
                            }
                        } else {
                            // dst opt
                            if read.dest_opt {
                                return false;
                            } else {
                                read.dest_opt = true;
                            }
                        }
                    }
                    IPV6_FRAG => {
                        if read.frag {
                            return false;
                        } else {
                            read.frag = true;
                        }
                    }
                    AUTH => {
                        if read.auth {
                            return false;
                        } else {
                            read.auth = true;
                        }
                    }
                    _ => return false,
                }
            }
            return false;
        }

        /// Checks the if the extensions match the expected values based
        /// on this test payload.
        pub fn assert_extensions(
            &self,
            exts: &Ipv6Extensions,
        ) -> (usize, Option<IpNumber>, IpNumber) {
            struct ReadState {
                hop_by_hop: bool,
                dest_opt: bool,
                routing: bool,
                final_dest_opt: bool,
                frag: bool,
                auth: bool,
            }

            // state if a header type has already been read
            let mut read = ReadState {
                hop_by_hop: false,
                dest_opt: false,
                routing: false,
                final_dest_opt: false,
                frag: false,
                auth: false,
            };

            let mut slice = &self.data[..];
            let mut last_decoded = None;
            let mut post_header = self.ip_numbers[0];

            for i in 0..self.ip_numbers.len() - 1 {
                let mut stop = false;
                match self.ip_numbers[i] {
                    IPV6_HOP_BY_HOP => {
                        assert!(false == read.hop_by_hop);
                        let (header, rest) = Ipv6RawExtHeader::from_slice(slice).unwrap();
                        assert_eq!(&header, exts.hop_by_hop_options.as_ref().unwrap());
                        slice = rest;
                        read.hop_by_hop = true;
                        last_decoded = Some(IPV6_HOP_BY_HOP);
                    }
                    IPV6_ROUTE => {
                        if read.routing {
                            stop = true;
                        } else {
                            let (header, rest) = Ipv6RawExtHeader::from_slice(slice).unwrap();
                            assert_eq!(&header, &exts.routing.as_ref().unwrap().routing);
                            slice = rest;
                            read.routing = true;
                            last_decoded = Some(IPV6_ROUTE);
                        }
                    }
                    IPV6_DEST_OPTIONS => {
                        // check the kind of destination options (aka is it before or after the routing header)
                        if read.routing {
                            // final dest opt
                            if read.final_dest_opt {
                                stop = true;
                            } else {
                                let (header, rest) = Ipv6RawExtHeader::from_slice(slice).unwrap();
                                assert_eq!(
                                    &header,
                                    exts.routing
                                        .as_ref()
                                        .unwrap()
                                        .final_destination_options
                                        .as_ref()
                                        .unwrap()
                                );
                                slice = rest;
                                read.final_dest_opt = true;
                                last_decoded = Some(IPV6_DEST_OPTIONS);
                            }
                        } else {
                            // dst opt
                            if read.dest_opt {
                                stop = true;
                            } else {
                                let (header, rest) = Ipv6RawExtHeader::from_slice(slice).unwrap();
                                assert_eq!(&header, exts.destination_options.as_ref().unwrap());
                                slice = rest;
                                read.dest_opt = true;
                                last_decoded = Some(IPV6_DEST_OPTIONS);
                            }
                        }
                    }
                    IPV6_FRAG => {
                        if read.frag {
                            // duplicate header -> stop
                            stop = true;
                        } else {
                            let (header, rest) = Ipv6FragmentHeader::from_slice(slice).unwrap();
                            assert_eq!(&header, exts.fragment.as_ref().unwrap());
                            slice = rest;
                            read.frag = true;
                            last_decoded = Some(IPV6_FRAG);
                        }
                    }
                    AUTH => {
                        if read.auth {
                            // duplicate header -> stop
                            stop = true;
                        } else {
                            let (header, rest) = IpAuthHeader::from_slice(slice).unwrap();
                            assert_eq!(&header, exts.auth.as_ref().unwrap());
                            slice = rest;
                            read.auth = true;
                            last_decoded = Some(AUTH);
                        }
                    }
                    _ => {
                        // non extension header -> stop
                        stop = true;
                    }
                }
                if stop {
                    post_header = self.ip_numbers[i];
                    break;
                } else {
                    post_header = self.ip_numbers[i + 1];
                }
            }

            // check the non parsed headers are not present
            if false == read.hop_by_hop {
                assert!(exts.hop_by_hop_options.is_none());
            }
            if false == read.dest_opt {
                assert!(exts.destination_options.is_none());
            }
            if false == read.routing {
                assert!(exts.routing.is_none());
            } else {
                if false == read.final_dest_opt {
                    assert!(exts
                        .routing
                        .as_ref()
                        .unwrap()
                        .final_destination_options
                        .is_none());
                }
            }
            if false == read.frag {
                assert!(exts.fragment.is_none());
            }
            if false == read.auth {
                assert!(exts.auth.is_none());
            }

            (self.data.len() - slice.len(), last_decoded, post_header)
        }

        /// Return the expected lax from slice result and ipnumber which caused
        /// an error.
        pub fn lax_extensions_for_len(
            &self,
            limiting_len: usize,
        ) -> (Ipv6Extensions, IpNumber, &[u8], Option<IpNumber>) {
            // state if a header type has already been read
            let mut exts: Ipv6Extensions = Default::default();
            let mut post_header = *self.ip_numbers.first().unwrap();
            let mut slice = &self.data[..];

            for i in 0..self.ip_numbers.len() - 1 {
                // check if the limiting size gets hit
                if self.slice().len() - slice.len() + self.lengths[i] > limiting_len {
                    return (
                        exts,
                        self.ip_numbers[i],
                        &self.slice()[self.slice().len() - slice.len()..limiting_len],
                        Some(self.ip_numbers[i]),
                    );
                }

                let mut stop = false;
                match self.ip_numbers[i] {
                    IPV6_HOP_BY_HOP => {
                        assert!(exts.hop_by_hop_options.is_none());
                        let (header, rest) = Ipv6RawExtHeader::from_slice(slice).unwrap();
                        exts.hop_by_hop_options = Some(header);
                        slice = rest;
                    }
                    IPV6_ROUTE => {
                        if exts.routing.is_some() {
                            stop = true;
                        } else {
                            let (header, rest) = Ipv6RawExtHeader::from_slice(slice).unwrap();
                            exts.routing = Some(Ipv6RoutingExtensions {
                                routing: header,
                                final_destination_options: None,
                            });
                            slice = rest;
                        }
                    }
                    IPV6_DEST_OPTIONS => {
                        // check the kind of destination options (aka is it before or after the routing header)
                        if let Some(routing) = exts.routing.as_mut() {
                            // final dest opt
                            if routing.final_destination_options.is_some() {
                                stop = true;
                            } else {
                                let (header, rest) = Ipv6RawExtHeader::from_slice(slice).unwrap();
                                routing.final_destination_options = Some(header);
                                slice = rest;
                            }
                        } else {
                            // dst opt
                            if exts.destination_options.is_some() {
                                stop = true;
                            } else {
                                let (header, rest) = Ipv6RawExtHeader::from_slice(slice).unwrap();
                                exts.destination_options = Some(header);
                                slice = rest;
                            }
                        }
                    }
                    IPV6_FRAG => {
                        if exts.fragment.is_some() {
                            // duplicate header -> stop
                            stop = true;
                        } else {
                            let (header, rest) = Ipv6FragmentHeader::from_slice(slice).unwrap();
                            exts.fragment = Some(header);
                            slice = rest;
                        }
                    }
                    AUTH => {
                        if exts.auth.is_some() {
                            // duplicate header -> stop
                            stop = true;
                        } else {
                            let (header, rest) = IpAuthHeader::from_slice(slice).unwrap();
                            exts.auth = Some(header);
                            slice = rest;
                        }
                    }
                    _ => {
                        // non extension header -> stop
                        stop = true;
                    }
                }
                if stop {
                    post_header = self.ip_numbers[i];
                    break;
                } else {
                    post_header = self.ip_numbers[i + 1];
                }
            }

            (
                exts,
                post_header,
                &self.slice()[self.slice().len() - slice.len()..limiting_len],
                None,
            )
        }
    }

    /// extension header data.
    #[derive(Clone)]
    pub struct ExtensionTestHeaders {
        pub ip_numbers: Vec<IpNumber>,
        pub data: Ipv6Extensions,
    }

    impl ExtensionTestHeaders {
        pub fn new(ip_numbers: &[IpNumber], header_sizes: &[u8]) -> ExtensionTestHeaders {
            assert!(ip_numbers.len() > 1);
            assert!(header_sizes.len() > 0);

            let mut result = ExtensionTestHeaders {
                ip_numbers: ip_numbers.to_vec(),
                data: Default::default(),
            };
            for i in 0..ip_numbers.len() - 1 {
                let succ = result.add_payload(
                    ip_numbers[i],
                    ip_numbers[i + 1],
                    header_sizes[i % header_sizes.len()],
                );
                if false == succ {
                    // write was not possible (duplicate)
                    // reduce the list so the current ip number
                    // is the final one
                    result.ip_numbers.truncate(i + 1);
                    break;
                }
            }
            result
        }

        pub fn introduce_missing_ref(&mut self, new_header: IpNumber) -> IpNumber {
            assert!(self.ip_numbers.len() >= 2);

            // set the next_header of the last extension header and return the id
            if self.ip_numbers.len() >= 3 {
                match self.ip_numbers[self.ip_numbers.len() - 3] {
                    IPV6_HOP_BY_HOP => {
                        self.data.hop_by_hop_options.as_mut().unwrap().next_header = new_header;
                    }
                    IPV6_DEST_OPTIONS => {
                        if self.ip_numbers[..self.ip_numbers.len() - 3]
                            .iter()
                            .any(|&x| x == IPV6_ROUTE)
                        {
                            self.data
                                .routing
                                .as_mut()
                                .unwrap()
                                .final_destination_options
                                .as_mut()
                                .unwrap()
                                .next_header = new_header;
                        } else {
                            self.data.destination_options.as_mut().unwrap().next_header =
                                new_header;
                        }
                    }
                    IPV6_ROUTE => {
                        self.data.routing.as_mut().unwrap().routing.next_header = new_header;
                    }
                    IPV6_FRAG => {
                        self.data.fragment.as_mut().unwrap().next_header = new_header;
                    }
                    AUTH => {
                        self.data.auth.as_mut().unwrap().next_header = new_header;
                    }
                    _ => unreachable!(),
                }
                match self.ip_numbers[self.ip_numbers.len() - 2] {
                    IPV6_HOP_BY_HOP => IpNumber::IPV6_HEADER_HOP_BY_HOP,
                    IPV6_DEST_OPTIONS => IpNumber::IPV6_DESTINATION_OPTIONS,
                    IPV6_ROUTE => IpNumber::IPV6_ROUTE_HEADER,
                    IPV6_FRAG => IpNumber::IPV6_FRAGMENTATION_HEADER,
                    AUTH => IpNumber::AUTHENTICATION_HEADER,
                    _ => unreachable!(),
                }
            } else {
                // rewrite start number in case it is just one extension header
                let missing = self.ip_numbers[0];
                self.ip_numbers[0] = new_header;
                match missing {
                    IPV6_HOP_BY_HOP => IpNumber::IPV6_HEADER_HOP_BY_HOP,
                    IPV6_DEST_OPTIONS => IpNumber::IPV6_DESTINATION_OPTIONS,
                    IPV6_ROUTE => IpNumber::IPV6_ROUTE_HEADER,
                    IPV6_FRAG => IpNumber::IPV6_FRAGMENTATION_HEADER,
                    AUTH => IpNumber::AUTHENTICATION_HEADER,
                    _ => unreachable!(),
                }
            }
        }

        fn add_payload(
            &mut self,
            ip_number: IpNumber,
            next_header: IpNumber,
            header_ext_len: u8,
        ) -> bool {
            match ip_number {
                IPV6_HOP_BY_HOP | IPV6_ROUTE | IPV6_DEST_OPTIONS => {
                    use Ipv6RawExtHeader as R;
                    let payload: [u8; R::MAX_PAYLOAD_LEN] = [0; R::MAX_PAYLOAD_LEN];
                    let len = usize::from(header_ext_len) * 8 + 6;

                    let raw = Ipv6RawExtHeader::new_raw(next_header, &payload[..len]).unwrap();
                    match ip_number {
                        IPV6_HOP_BY_HOP => {
                            if self.data.hop_by_hop_options.is_none() {
                                self.data.hop_by_hop_options = Some(raw);
                                true
                            } else {
                                false
                            }
                        }
                        IPV6_ROUTE => {
                            if self.data.routing.is_none() {
                                self.data.routing = Some(Ipv6RoutingExtensions {
                                    routing: raw,
                                    final_destination_options: None,
                                });
                                true
                            } else {
                                false
                            }
                        }
                        IPV6_DEST_OPTIONS => {
                            if let Some(ref mut route) = self.data.routing {
                                if route.final_destination_options.is_none() {
                                    route.final_destination_options = Some(raw);
                                    true
                                } else {
                                    false
                                }
                            } else {
                                // dest option
                                if self.data.destination_options.is_none() {
                                    self.data.destination_options = Some(raw);
                                    true
                                } else {
                                    false
                                }
                            }
                        }
                        _ => unreachable!(),
                    }
                }
                IPV6_FRAG => {
                    if self.data.fragment.is_none() {
                        self.data.fragment = Some(Ipv6FragmentHeader::new(
                            next_header,
                            IpFragOffset::ZERO,
                            true,
                            123,
                        ));
                        true
                    } else {
                        false
                    }
                }
                AUTH => {
                    if self.data.auth.is_none() {
                        use IpAuthHeader as A;

                        let mut len = usize::from(header_ext_len) * 4;
                        if len > A::MAX_ICV_LEN {
                            len = A::MAX_ICV_LEN;
                        }
                        let raw_icv: [u8; A::MAX_ICV_LEN] = [0; A::MAX_ICV_LEN];
                        self.data.auth = Some(
                            IpAuthHeader::new(next_header, 123, 234, &raw_icv[..len]).unwrap(),
                        );
                        true
                    } else {
                        false
                    }
                }
                _ => unreachable!(),
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::ipv6_exts_test_helpers::*;
    use super::*;
    use crate::ip_number::*;
    use crate::test_gens::*;
    use alloc::{borrow::ToOwned, vec::Vec};
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn from_slice(
            header_size in any::<u8>(),
            post_header in ip_number_any()
                .prop_filter("Must be a non ipv6 header relevant ip number".to_owned(),
                    |v| !EXTENSION_KNOWN_IP_NUMBERS.iter().any(|&x| v == &x)
                )
        ) {
            use err::ipv6_exts::{HeaderError::*, HeaderSliceError::*};

            // no extension headers filled
            {
                let some_data = [1,2,3,4];
                let actual = Ipv6Extensions::from_slice(post_header, &some_data).unwrap();
                assert_eq!(actual.0, Default::default());
                assert_eq!(actual.1, post_header);
                assert_eq!(actual.2, &some_data);
            }

            /// Run a test with the given ip numbers
            fn run_test(ip_numbers: &[IpNumber], header_sizes: &[u8]) {
                // setup test payload
                let e = ExtensionTestPayload::new(
                    ip_numbers,
                    header_sizes
                );

                if e.exts_hop_by_hop_error() {
                    // a hop by hop header that is not at the start triggers an error
                    assert_eq!(
                        Ipv6Extensions::from_slice(ip_numbers[0], e.slice()).unwrap_err(),
                        Content(HopByHopNotAtStart)
                    );
                } else {
                    // normal read
                    let (header, next, rest) = Ipv6Extensions::from_slice(ip_numbers[0], e.slice()).unwrap();
                    let (read_len, last_header, expected_post_header) = e.assert_extensions(&header);
                    assert_eq!(next, expected_post_header);
                    assert_eq!(rest, &e.slice()[read_len..]);

                    // unexpected end of slice
                    {
                        let mut offset: usize = 0;
                        for l in &e.lengths {
                            if offset + l >= read_len {
                                break;
                            }
                            offset += l;
                        }

                        assert_eq!(
                            Ipv6Extensions::from_slice(ip_numbers[0], &e.slice()[..read_len - 1]).unwrap_err(),
                            Len(err::LenError {
                                required_len: read_len - offset,
                                len: read_len - offset - 1,
                                len_source: LenSource::Slice,
                                layer: match last_header.unwrap() {
                                    AUTH => err::Layer::IpAuthHeader,
                                    IPV6_FRAG => err::Layer::Ipv6FragHeader,
                                    _ => err::Layer::Ipv6ExtHeader
                                },
                                layer_start_offset: offset,
                            })
                        );
                    }
                }
            }

            // test the parsing of different extension header combinations
            for first_header in &EXTENSION_KNOWN_IP_NUMBERS {

                // single header parsing
                run_test(
                    &[*first_header, post_header],
                    &[header_size],
                );

                for second_header in &EXTENSION_KNOWN_IP_NUMBERS {

                    // double header parsing
                    run_test(
                        &[*first_header, *second_header, post_header],
                        &[header_size],
                    );

                    for third_header in &EXTENSION_KNOWN_IP_NUMBERS {
                        // tripple header parsing
                        run_test(
                            &[*first_header, *second_header, *third_header, post_header],
                            &[header_size],
                        );
                    }
                }
            }

            // test that the auth content error gets forwarded
            {
                let auth = IpAuthHeader::new(post_header, 0, 0, &[]).unwrap();
                let mut bytes = auth.to_bytes();
                // inject an invalid len value
                bytes[1] = 0;
                let actual = Ipv6Extensions::from_slice(AUTH, &bytes).unwrap_err();

                use err::ipv6_exts::HeaderError::IpAuth;
                use err::ip_auth::HeaderError::ZeroPayloadLen;
                assert_eq!(actual, Content(IpAuth(ZeroPayloadLen)));
            }
        }
    }

    proptest! {
        #[test]
        fn from_slice_lax(
            header_size in any::<u8>(),
            post_header in ip_number_any()
                .prop_filter("Must be a non ipv6 header relevant ip number".to_owned(),
                    |v| !EXTENSION_KNOWN_IP_NUMBERS.iter().any(|&x| v == &x)
                )
        ) {
            use err::ipv6_exts::{HeaderError::*, HeaderSliceError::*};

            // no extension headers filled
            {
                let some_data = [1,2,3,4];
                let actual = Ipv6Extensions::from_slice_lax(post_header, &some_data);
                assert_eq!(actual.0, Default::default());
                assert_eq!(actual.1, post_header);
                assert_eq!(actual.2, &some_data);
                assert!(actual.3.is_none());
            }

            /// Run a test with the given ip numbers
            fn run_test(ip_numbers: &[IpNumber], header_sizes: &[u8]) {
                // setup test payload
                let e = ExtensionTestPayload::new(
                    ip_numbers,
                    header_sizes
                );

                if e.exts_hop_by_hop_error() {
                    // a hop by hop header that is not at the start triggers an error
                    let actual = Ipv6Extensions::from_slice_lax(ip_numbers[0], e.slice());
                    assert_eq!(actual.3.unwrap(), (Content(HopByHopNotAtStart), Layer::Ipv6HopByHopHeader));
                } else {
                    // normal read
                    let norm_actual = Ipv6Extensions::from_slice_lax(ip_numbers[0], e.slice());
                    let norm_expected = e.lax_extensions_for_len(e.slice().len());
                    assert_eq!(norm_actual.0, norm_expected.0);
                    assert_eq!(norm_actual.1, norm_expected.1);
                    assert_eq!(norm_actual.2, norm_expected.2);
                    assert!(norm_actual.3.is_none());

                    // unexpected end of slice
                    if norm_actual.0.header_len() > 0 {

                        let norm_len = norm_actual.0.header_len();
                        let actual = Ipv6Extensions::from_slice_lax(ip_numbers[0], &e.slice()[..norm_len - 1]);

                        let expected = e.lax_extensions_for_len(norm_len - 1);
                        assert_eq!(actual.0, expected.0);
                        assert_eq!(actual.1, expected.1);
                        assert_eq!(actual.2, expected.2);
                        let len_err = actual.3.unwrap().0.len_error().unwrap().clone();
                        assert_eq!(len_err.len, norm_len - 1 - expected.0.header_len());
                        assert_eq!(len_err.len_source, LenSource::Slice);
                        assert_eq!(
                            len_err.layer,
                            match expected.3.unwrap() {
                                AUTH => err::Layer::IpAuthHeader,
                                IPV6_FRAG => err::Layer::Ipv6FragHeader,
                                _ => err::Layer::Ipv6ExtHeader
                            }
                        );
                        assert_eq!(len_err.layer_start_offset, expected.0.header_len());
                    }
                }
            }

            // test the parsing of different extension header combinations
            for first_header in &EXTENSION_KNOWN_IP_NUMBERS {

                // single header parsing
                run_test(
                    &[*first_header, post_header],
                    &[header_size],
                );

                for second_header in &EXTENSION_KNOWN_IP_NUMBERS {

                    // double header parsing
                    run_test(
                        &[*first_header, *second_header, post_header],
                        &[header_size],
                    );

                    for third_header in &EXTENSION_KNOWN_IP_NUMBERS {
                        // tripple header parsing
                        run_test(
                            &[*first_header, *second_header, *third_header, post_header],
                            &[header_size],
                        );
                    }
                }
            }

            // test that the auth content error gets forwarded
            {
                let auth = IpAuthHeader::new(post_header, 0, 0, &[]).unwrap();
                let mut bytes = auth.to_bytes();
                // inject an invalid len value
                bytes[1] = 0;
                let actual = Ipv6Extensions::from_slice_lax(AUTH, &bytes);
                assert_eq!(0, actual.0.header_len());
                assert_eq!(AUTH, actual.1);
                assert_eq!(&bytes[..], actual.2);

                use err::ipv6_exts::HeaderError::IpAuth;
                use err::ip_auth::HeaderError::ZeroPayloadLen;
                assert_eq!(actual.3.unwrap(), (Content(IpAuth(ZeroPayloadLen)), Layer::IpAuthHeader));
            }
        }
    }

    proptest! {
        #[test]
        fn read(
            header_size in any::<u8>(),
            post_header in ip_number_any()
                .prop_filter("Must be a non ipv6 header relevant ip number".to_owned(),
                    |v| !EXTENSION_KNOWN_IP_NUMBERS.iter().any(|&x| v == &x)
                )
        ) {
            use err::ipv6_exts::HeaderError::*;
            use std::io::Cursor;

            // no extension headers filled
            {
                let mut cursor = Cursor::new(&[]);
                let actual = Ipv6Extensions::read(&mut cursor, post_header).unwrap();
                assert_eq!(actual.0, Default::default());
                assert_eq!(actual.1, post_header);
                assert_eq!(0, cursor.position());
            }

            /// Run a test with the given ip numbers
            fn run_test(ip_numbers: &[IpNumber], header_sizes: &[u8]) {
                // setup test payload
                let e = ExtensionTestPayload::new(
                    ip_numbers,
                    header_sizes
                );
                let mut cursor = Cursor::new(e.slice());

                if e.exts_hop_by_hop_error() {
                    // a hop by hop header that is not at the start triggers an error
                    assert_eq!(
                        Ipv6Extensions::read(&mut cursor, ip_numbers[0]).unwrap_err().content_error().unwrap(),
                        HopByHopNotAtStart
                    );
                } else {
                    // normal read
                    let (header, next) = Ipv6Extensions::read(&mut cursor, ip_numbers[0]).unwrap();
                    let (read_len, _, expected_post_header) = e.assert_extensions(&header);
                    assert_eq!(next, expected_post_header);
                    assert_eq!(cursor.position() as usize, read_len);

                    // unexpected end of slice
                    {
                        let mut short_cursor = Cursor::new(&e.slice()[..read_len - 1]);
                        assert!(
                            Ipv6Extensions::read(&mut short_cursor, ip_numbers[0])
                            .unwrap_err()
                            .io_error()
                            .is_some()
                        );
                    }
                }
            }

            // test the parsing of different extension header combinations
            for first_header in &EXTENSION_KNOWN_IP_NUMBERS {

                // single header parsing
                run_test(
                    &[*first_header, post_header],
                    &[header_size],
                );

                for second_header in &EXTENSION_KNOWN_IP_NUMBERS {

                    // double header parsing
                    run_test(
                        &[*first_header, *second_header, post_header],
                        &[header_size],
                    );

                    for third_header in &EXTENSION_KNOWN_IP_NUMBERS {
                        // tripple header parsing
                        run_test(
                            &[*first_header, *second_header, *third_header, post_header],
                            &[header_size],
                        );
                    }
                }
            }

            // test that the auth content error gets forwarded
            {
                let auth = IpAuthHeader::new(post_header, 0, 0, &[]).unwrap();
                let mut bytes = auth.to_bytes();
                // inject an invalid len value
                bytes[1] = 0;
                let mut cursor = Cursor::new(&bytes[..]);
                let actual = Ipv6Extensions::read(&mut cursor, AUTH).unwrap_err();

                use err::ipv6_exts::HeaderError::IpAuth;
                use err::ip_auth::HeaderError::ZeroPayloadLen;
                assert_eq!(actual.content_error().unwrap(), IpAuth(ZeroPayloadLen));
            }
        }
    }

    proptest! {
        #[test]
        fn read_limited(
            header_size in any::<u8>(),
            post_header in ip_number_any()
                .prop_filter("Must be a non ipv6 header relevant ip number".to_owned(),
                    |v| !EXTENSION_KNOWN_IP_NUMBERS.iter().any(|&x| v == &x)
                )
        ) {
            use err::ipv6_exts::HeaderError::*;
            use err::Layer;
            use std::io::Cursor;
            use crate::io::LimitedReader;

            // no extension headers filled
            {
                let mut reader = LimitedReader::new(
                    Cursor::new(&[]),
                    0,
                    LenSource::Slice,
                    0,
                    Layer::Ipv6Header
                );
                let actual = Ipv6Extensions::read_limited(&mut reader, post_header).unwrap();
                assert_eq!(actual.0, Default::default());
                assert_eq!(actual.1, post_header);
                assert_eq!(0, reader.read_len());
            }

            /// Run a test with the given ip numbers
            fn run_test(ip_numbers: &[IpNumber], header_sizes: &[u8]) {
                // setup test payload
                let e = ExtensionTestPayload::new(
                    ip_numbers,
                    header_sizes
                );
                let mut reader = LimitedReader::new(
                    Cursor::new(e.slice()),
                    e.slice().len(),
                    LenSource::Slice,
                    0,
                    Layer::Ipv6Header
                );

                if e.exts_hop_by_hop_error() {
                    // a hop by hop header that is not at the start triggers an error
                    assert_eq!(
                        Ipv6Extensions::read_limited(&mut reader, ip_numbers[0]).unwrap_err().content().unwrap(),
                        HopByHopNotAtStart
                    );
                } else {
                    // normal read
                    let (header, next) = Ipv6Extensions::read_limited(&mut reader, ip_numbers[0]).unwrap();
                    let (read_len, _, expected_post_header) = e.assert_extensions(&header);
                    assert_eq!(next, expected_post_header);
                    assert_eq!(reader.read_len() + reader.layer_offset(), read_len);

                    // io error unexpected end
                    {
                        let mut short_reader = LimitedReader::new(
                            Cursor::new(&e.slice()[..read_len - 1]),
                            read_len,
                            LenSource::Slice,
                            0,
                            Layer::Ipv6Header
                        );

                        assert!(
                            Ipv6Extensions::read_limited(&mut short_reader, ip_numbers[0])
                            .unwrap_err()
                            .io()
                            .is_some()
                        );
                    }

                    // len error
                    {
                        let mut short_reader = LimitedReader::new(
                            Cursor::new(e.slice()),
                            read_len - 1,
                            LenSource::Slice,
                            0,
                            Layer::Ipv6Header
                        );

                        assert!(
                            Ipv6Extensions::read_limited(&mut short_reader, ip_numbers[0])
                            .unwrap_err()
                            .len()
                            .is_some()
                        );
                    }
                }
            }

            // test the parsing of different extension header combinations
            for first_header in &EXTENSION_KNOWN_IP_NUMBERS {

                // single header parsing
                run_test(
                    &[*first_header, post_header],
                    &[header_size],
                );

                for second_header in &EXTENSION_KNOWN_IP_NUMBERS {

                    // double header parsing
                    run_test(
                        &[*first_header, *second_header, post_header],
                        &[header_size],
                    );

                    for third_header in &EXTENSION_KNOWN_IP_NUMBERS {
                        // tripple header parsing
                        run_test(
                            &[*first_header, *second_header, *third_header, post_header],
                            &[header_size],
                        );
                    }
                }
            }

            // test that the auth content error gets forwarded
            {
                let auth = IpAuthHeader::new(post_header, 0, 0, &[]).unwrap();
                let mut bytes = auth.to_bytes();
                // inject an invalid len value
                bytes[1] = 0;
                let mut reader = LimitedReader::new(
                    Cursor::new(&bytes[..]),
                    bytes.len(),
                    LenSource::Slice,
                    0,
                    Layer::Ipv6Header
                );
                let actual = Ipv6Extensions::read_limited(&mut reader, AUTH).unwrap_err();

                use err::ipv6_exts::HeaderError::IpAuth;
                use err::ip_auth::HeaderError::ZeroPayloadLen;
                assert_eq!(actual.content().unwrap(), IpAuth(ZeroPayloadLen));
            }
        }
    }

    proptest! {
        #[test]
        fn write(
            header_size in any::<u8>(),
            post_header in ip_number_any()
                .prop_filter("Must be a non ipv6 header relevant ip number".to_owned(),
                    |v| !EXTENSION_KNOWN_IP_NUMBERS.iter().any(|&x| v == &x)
                )
        ) {
            // no extension headers filled
            {
                let exts : Ipv6Extensions = Default::default();
                let mut buffer = Vec::new();
                exts.write(&mut buffer, post_header).unwrap();
                assert_eq!(0, buffer.len());
            }

            /// Run a test with the given ip numbers
            fn run_test(ip_numbers: &[IpNumber], header_sizes: &[u8], post_header: IpNumber) {
                use std::io::Cursor;
                use crate::err::ipv6_exts::ExtsWalkError::*;

                // setup test header
                let e = ExtensionTestHeaders::new(
                    ip_numbers,
                    header_sizes
                );

                if e.ip_numbers[1..e.ip_numbers.len()-1].iter().any(|&x| x == IPV6_HOP_BY_HOP) {
                    // a hop by hop header that is not at the start triggers an error
                    let mut writer = Vec::with_capacity(e.data.header_len());
                    assert_eq!(
                        e.data.write(&mut writer, e.ip_numbers[0]).unwrap_err().content().unwrap(),
                        &HopByHopNotAtStart
                    );
                } else {
                    // normal write
                    {
                        let mut writer = Vec::with_capacity(e.data.header_len());
                        e.data.write(&mut writer, e.ip_numbers[0]).unwrap();

                        if *e.ip_numbers.last().unwrap() != IPV6_HOP_BY_HOP {
                            // decoding if there will be no duplicate hop by hop error
                            // will be triggered
                            let (read, read_next, _) = Ipv6Extensions::from_slice(
                                e.ip_numbers[0],
                                &writer
                            ).unwrap();
                            assert_eq!(e.data, read);
                            assert_eq!(*e.ip_numbers.last().unwrap(), read_next);
                        }
                    }

                    // write error
                    {
                        let mut buffer = Vec::with_capacity(e.data.header_len() - 1);
                        buffer.resize(e.data.header_len() - 1, 0);
                        let mut cursor = Cursor::new(&mut buffer[..]);

                        let err = e.data.write(
                            &mut cursor,
                            e.ip_numbers[0]
                        ).unwrap_err();

                        assert!(err.io().is_some());
                    }

                    // missing reference (skip the last header)
                    {
                        use crate::err::ipv6_exts::ExtsWalkError::ExtNotReferenced;

                        let mut missing_ref = e.clone();
                        let missing_ext = missing_ref.introduce_missing_ref(post_header);

                        let mut writer = Vec::with_capacity(e.data.header_len());
                        let err = missing_ref.data.write(
                            &mut writer,
                            missing_ref.ip_numbers[0]
                        ).unwrap_err();

                        assert_eq!(
                            err.content().unwrap(),
                            &ExtNotReferenced{ missing_ext }
                        );
                    }
                }
            }

            // test the parsing of different extension header combinations
            for first_header in &EXTENSION_KNOWN_IP_NUMBERS {

                // single header parsing
                run_test(
                    &[*first_header, post_header],
                    &[header_size],
                    post_header,
                );

                for second_header in &EXTENSION_KNOWN_IP_NUMBERS {

                    // double header parsing
                    run_test(
                        &[*first_header, *second_header, post_header],
                        &[header_size],
                        post_header,
                    );

                    for third_header in &EXTENSION_KNOWN_IP_NUMBERS {
                        // tripple header parsing
                        run_test(
                            &[*first_header, *second_header, *third_header, post_header],
                            &[header_size],
                            post_header,
                        );
                    }
                }
            }
        }
    }

    proptest! {
        #[test]
        fn header_len(
            hop_by_hop_options in ipv6_raw_ext_any(),
            destination_options in ipv6_raw_ext_any(),
            routing in ipv6_raw_ext_any(),
            fragment in ipv6_fragment_any(),
            auth in ip_auth_any(),
            final_destination_options in ipv6_raw_ext_any(),
        ) {
            // None
            {
                let exts : Ipv6Extensions = Default::default();
                assert_eq!(0, exts.header_len());
            }

            // All filled
            {
                let exts = Ipv6Extensions{
                    hop_by_hop_options: Some(hop_by_hop_options.clone()),
                    destination_options: Some(destination_options.clone()),
                    routing: Some(
                        Ipv6RoutingExtensions{
                            routing: routing.clone(),
                            final_destination_options: Some(final_destination_options.clone()),
                        }
                    ),
                    fragment: Some(fragment.clone()),
                    auth: Some(auth.clone()),
                };
                assert_eq!(
                    exts.header_len(),
                    (
                        hop_by_hop_options.header_len() +
                        destination_options.header_len() +
                        routing.header_len() +
                        final_destination_options.header_len() +
                        fragment.header_len() +
                        auth.header_len()
                    )
                );
            }

            // Routing without final destination options
            {
                let exts = Ipv6Extensions{
                    hop_by_hop_options: Some(hop_by_hop_options.clone()),
                    destination_options: Some(destination_options.clone()),
                    routing: Some(
                        Ipv6RoutingExtensions{
                            routing: routing.clone(),
                            final_destination_options: None,
                        }
                    ),
                    fragment: Some(fragment.clone()),
                    auth: Some(auth.clone()),
                };
                assert_eq!(
                    exts.header_len(),
                    (
                        hop_by_hop_options.header_len() +
                        destination_options.header_len() +
                        routing.header_len() +
                        fragment.header_len() +
                        auth.header_len()
                    )
                );
            }
        }
    }

    proptest! {
        #[test]
        fn set_next_headers(
            hop_by_hop_options in ipv6_raw_ext_any(),
            destination_options in ipv6_raw_ext_any(),
            routing in ipv6_raw_ext_any(),
            fragment in ipv6_fragment_any(),
            auth in ip_auth_any(),
            final_destination_options in ipv6_raw_ext_any(),
            post_header in ip_number_any()
                .prop_filter("Must be a non ipv6 header relevant ip number".to_owned(),
                    |v| !EXTENSION_KNOWN_IP_NUMBERS.iter().any(|&x| v == &x)
                ),
        ) {
            // none filled
            {
                let mut exts : Ipv6Extensions = Default::default();
                assert_eq!(post_header, exts.set_next_headers(post_header));
                assert!(exts.hop_by_hop_options.is_none());
                assert!(exts.destination_options.is_none());
                assert!(exts.routing.is_none());
                assert!(exts.fragment.is_none());
                assert!(exts.auth.is_none());
            }

            // all filled
            {
                let mut exts = Ipv6Extensions{
                    hop_by_hop_options: Some(hop_by_hop_options.clone()),
                    destination_options: Some(destination_options.clone()),
                    routing: Some(
                        Ipv6RoutingExtensions{
                            routing: routing.clone(),
                            final_destination_options: Some(final_destination_options.clone()),
                        }
                    ),
                    fragment: Some(fragment.clone()),
                    auth: Some(auth.clone()),
                };
                assert_eq!(IPV6_HOP_BY_HOP, exts.set_next_headers(post_header));

                assert_eq!(IPV6_DEST_OPTIONS, exts.hop_by_hop_options.as_ref().unwrap().next_header);
                assert_eq!(IPV6_ROUTE, exts.destination_options.as_ref().unwrap().next_header);
                assert_eq!(IPV6_FRAG, exts.routing.as_ref().unwrap().routing.next_header);
                assert_eq!(AUTH, exts.fragment.as_ref().unwrap().next_header);
                assert_eq!(IPV6_DEST_OPTIONS, exts.auth.as_ref().unwrap().next_header);
                assert_eq!(post_header, exts.routing.as_ref().unwrap().final_destination_options.as_ref().unwrap().next_header);
            }
        }
    }

    proptest! {
        #[test]
        fn next_header(
            header_size in any::<u8>(),
            post_header in ip_number_any()
                .prop_filter("Must be a non ipv6 header relevant ip number".to_owned(),
                    |v| !EXTENSION_KNOWN_IP_NUMBERS.iter().any(|&x| v == &x)
                ),)
        {
            // test empty
            {
                let exts : Ipv6Extensions = Default::default();
                assert_eq!(post_header, exts.next_header(post_header).unwrap());
            }

            /// Run a test with the given ip numbers
            fn run_test(ip_numbers: &[IpNumber], header_sizes: &[u8], post_header: IpNumber) {
                // setup test header
                let e = ExtensionTestHeaders::new(
                    ip_numbers,
                    header_sizes
                );

                if e.ip_numbers[1..e.ip_numbers.len()-1].iter().any(|&x| x == IPV6_HOP_BY_HOP) {
                    // a hop by hop header that is not at the start triggers an error
                    use crate::err::ipv6_exts::ExtsWalkError::HopByHopNotAtStart;
                    assert_eq!(
                        e.data.next_header(e.ip_numbers[0]).unwrap_err(),
                        HopByHopNotAtStart
                    );
                } else {
                    // normal header
                    assert_eq!(
                        *e.ip_numbers.last().unwrap(),
                        e.data.next_header(e.ip_numbers[0]).unwrap()
                    );

                    // missing reference (skip the last header)
                    {
                        use crate::err::ipv6_exts::ExtsWalkError::ExtNotReferenced;

                        let mut missing_ref = e.clone();
                        let missing_ext = missing_ref.introduce_missing_ref(post_header);
                        assert_eq!(
                            missing_ref.data.next_header(missing_ref.ip_numbers[0]).unwrap_err(),
                            ExtNotReferenced{ missing_ext }
                        );
                    }
                }
            }

            // test the parsing of different extension header combinations
            for first_header in &EXTENSION_KNOWN_IP_NUMBERS {

                // single header parsing
                run_test(
                    &[*first_header, post_header],
                    &[header_size],
                    post_header,
                );

                for second_header in &EXTENSION_KNOWN_IP_NUMBERS {

                    // double header parsing
                    run_test(
                        &[*first_header, *second_header, post_header],
                        &[header_size],
                        post_header,
                    );

                    for third_header in &EXTENSION_KNOWN_IP_NUMBERS {
                        // tripple header parsing
                        run_test(
                            &[*first_header, *second_header, *third_header, post_header],
                            &[header_size],
                            post_header,
                        );
                    }
                }
            }
        }
    }

    #[test]
    fn is_fragmenting_payload() {
        // empty
        assert_eq!(
            false,
            Ipv6Extensions {
                hop_by_hop_options: None,
                destination_options: None,
                routing: None,
                fragment: None,
                auth: None,
            }
            .is_fragmenting_payload()
        );

        // non fragmenting frag header
        assert_eq!(
            false,
            Ipv6Extensions {
                hop_by_hop_options: None,
                destination_options: None,
                routing: None,
                fragment: Some(Ipv6FragmentHeader::new(
                    ip_number::UDP,
                    IpFragOffset::ZERO,
                    false,
                    0
                )),
                auth: None,
            }
            .is_fragmenting_payload()
        );

        // fragmenting frag header
        assert!(Ipv6Extensions {
            hop_by_hop_options: None,
            destination_options: None,
            routing: None,
            fragment: Some(Ipv6FragmentHeader::new(
                ip_number::UDP,
                IpFragOffset::ZERO,
                true,
                0
            )),
            auth: None,
        }
        .is_fragmenting_payload());
    }

    #[test]
    fn is_empty() {
        // empty
        assert!(Ipv6Extensions {
            hop_by_hop_options: None,
            destination_options: None,
            routing: None,
            fragment: None,
            auth: None,
        }
        .is_empty());

        // hop_by_hop_options
        assert_eq!(
            false,
            Ipv6Extensions {
                hop_by_hop_options: Some(
                    Ipv6RawExtHeader::new_raw(ip_number::UDP, &[1, 2, 3, 4, 5, 6]).unwrap()
                ),
                destination_options: None,
                routing: None,
                fragment: None,
                auth: None,
            }
            .is_empty()
        );

        // destination_options
        assert_eq!(
            false,
            Ipv6Extensions {
                hop_by_hop_options: None,
                destination_options: Some(
                    Ipv6RawExtHeader::new_raw(ip_number::UDP, &[1, 2, 3, 4, 5, 6]).unwrap()
                ),
                routing: None,
                fragment: None,
                auth: None,
            }
            .is_empty()
        );

        // routing
        assert_eq!(
            false,
            Ipv6Extensions {
                hop_by_hop_options: None,
                destination_options: None,
                routing: Some(Ipv6RoutingExtensions {
                    routing: Ipv6RawExtHeader::new_raw(ip_number::UDP, &[1, 2, 3, 4, 5, 6])
                        .unwrap(),
                    final_destination_options: None,
                }),
                fragment: None,
                auth: None,
            }
            .is_empty()
        );

        // fragment
        assert_eq!(
            false,
            Ipv6Extensions {
                hop_by_hop_options: None,
                destination_options: None,
                routing: None,
                fragment: Some(Ipv6FragmentHeader::new(
                    ip_number::UDP,
                    IpFragOffset::ZERO,
                    true,
                    0
                )),
                auth: None,
            }
            .is_empty()
        );

        // auth
        assert_eq!(
            false,
            Ipv6Extensions {
                hop_by_hop_options: None,
                destination_options: None,
                routing: None,
                fragment: None,
                auth: Some(IpAuthHeader::new(ip_number::UDP, 0, 0, &[]).unwrap()),
            }
            .is_empty()
        );
    }

    #[test]
    fn debug() {
        use alloc::format;

        let a: Ipv6Extensions = Default::default();
        assert_eq!(
            &format!(
                "Ipv6Extensions {{ hop_by_hop_options: {:?}, destination_options: {:?}, routing: {:?}, fragment: {:?}, auth: {:?} }}",
                a.hop_by_hop_options,
                a.destination_options,
                a.routing,
                a.fragment,
                a.auth,
            ),
            &format!("{:?}", a)
        );
    }

    #[test]
    fn clone_eq() {
        let a: Ipv6Extensions = Default::default();
        assert_eq!(a, a.clone());
    }

    #[test]
    fn default() {
        let a: Ipv6Extensions = Default::default();
        assert_eq!(a.hop_by_hop_options, None);
        assert_eq!(a.destination_options, None);
        assert_eq!(a.routing, None);
        assert_eq!(a.fragment, None);
        assert_eq!(a.auth, None);
    }
}