rpki 0.19.3

A library for validating and creating RPKI data.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
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
//! Raw protocol data.
//!
//! This module contains types that represent the protocol data units of
//! RTR in their wire representation. That is, these types can be given to
//! read and write operations as buffers.  See section 5 of RFC 6810 and
//! RFC 8210. Annoyingly, the format of the `EndOfData` PDU changes between
//! the two versions.

use std::{cmp, borrow, error, fmt, io, mem, ops, slice};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use bytes::Bytes;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use crate::resources::addr::{MaxLenPrefix, Prefix};
use crate::resources::asn::Asn;
use crate::rtr::Action;
use crate::util::base64;
use super::payload;
use super::state::{Serial, State};


//------------ Macro for Common Impls ----------------------------------------

macro_rules! common {
    ( $type:ident ) => {
        #[allow(dead_code)]
        impl $type {
            /// Writes a value to a writer.
            pub async fn write<A: AsyncWrite + Unpin>(
                &self,
                a: &mut A
            ) -> Result<(), io::Error> {
                a.write_all(self.as_ref()).await
            }
        }

        impl AsRef<[u8]> for $type {
            fn as_ref(&self) -> &[u8] {
                unsafe {
                    slice::from_raw_parts(
                        self as *const Self as *const u8,
                        mem::size_of::<Self>()
                    )
                }
            }
        }

        impl AsMut<[u8]> for $type {
            fn as_mut(&mut self) -> &mut [u8] {
                unsafe {
                    slice::from_raw_parts_mut(
                        self as *mut Self as *mut u8,
                        mem::size_of::<Self>()
                    )
                }
            }
        }
    }
}

macro_rules! concrete {
    ( $type:ident ) => {
        common!($type);

        #[allow(dead_code)]
        impl $type {
            /// Returns the value of the version field of the header.
            pub fn version(&self) -> u8 {
                self.header.version()
            }

            /// Returns the value of the session field of the header.
            ///
            /// Note that this field is used for other purposes in some PDU
            /// types.
            pub fn session(&self) -> u16 {
                self.header.session()
            }

            /// Returns the PDU size.
            ///
            /// The size is returned as a `u32` since that type is used in
            /// the header.
            pub fn size() -> u32 {
                mem::size_of::<Self>() as u32
            }

            /// Reads a value from a reader.
            ///
            /// If a value with a different PDU type is received, returns an
            /// error.
            pub async fn read<Sock: AsyncRead + Unpin>(
                sock: &mut Sock 
            ) -> Result<Self, io::Error> {
                let mut res = Self::default();
                sock.read_exact(res.header.as_mut()).await?;
                if res.header.pdu() != Self::PDU {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        concat!(
                            "PDU type mismatch when expecting ",
                            stringify!($type)
                        )
                    ))
                }
                if res.header.length() as usize != res.as_ref().len() {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        concat!(
                            "invalid length for ",
                            stringify!($type)
                        )
                    ))
                }
                sock.read_exact(&mut res.as_mut()[Header::LEN..]).await?;
                Ok(res)
            }

            /// Tries to read a value from a reader.
            ///
            /// If a different PDU type is received, returns the header as
            /// the error case of the ok case.
            pub async fn try_read<Sock: AsyncRead + Unpin>(
                sock: &mut Sock 
            ) -> Result<Result<Self, Header>, io::Error> {
                let mut res = Self::default();
                sock.read_exact(res.header.as_mut()).await?;
                if res.header.pdu() == Error::PDU {
                    // Since we should drop the session after an error, we
                    // can safely ignore all the rest of the error for now.
                    return Ok(Err(res.header))
                }
                if res.header.pdu() != Self::PDU {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        concat!(
                            "PDU type mismatch when expecting ",
                            stringify!($type)
                        )
                    ))
                }
                if res.header.length() as usize != res.as_ref().len() {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        concat!(
                            "invalid length for ",
                            stringify!($type)
                        )
                    ))
                }
                sock.read_exact(&mut res.as_mut()[Header::LEN..]).await?;
                Ok(Ok(res))
            }

            /// Reads only the payload part of a value from a reader.
            ///
            /// Assuming that the header was already read and is passed via
            /// `header`, the function reads the rest of the PUD from the
            /// reader and returns the complete value.
            pub async fn read_payload<Sock: AsyncRead + Unpin>(
                header: Header, sock: &mut Sock
            ) -> Result<Self, io::Error> {
                if header.length() as usize != mem::size_of::<Self>() {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        concat!(
                            "invalid length for ",
                            stringify!($type),
                            " PDU"
                        )
                    ))
                }
                let mut res = Self::default();
                sock.read_exact(&mut res.as_mut()[Header::LEN..]).await?;
                res.header = header;
                Ok(res)
            }
        }
    }
}


//------------ SerialNotify --------------------------------------------------

/// A serial notify informs a client that a cache has new data.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[repr(C, packed)]
#[allow(dead_code)]
pub struct SerialNotify {
    header: Header,
    serial: u32,
}

impl SerialNotify {
    /// The PDU type of a serial notify.
    pub const PDU: u8 = 0;

    /// Creates a new serial notify PDU.
    pub fn new(version: u8, state: State) -> Self {
        SerialNotify {
            header: Header::new(
                version, Self::PDU, state.session(), Self::size(),
            ),
            serial: state.serial().to_be(),
        }
    }
}

concrete!(SerialNotify);


//------------ SerialQuery ---------------------------------------------------

/// A serial query requests all updates since a router’s last update.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[repr(C, packed)]
#[allow(dead_code)]
pub struct SerialQuery {
    header: Header,
    payload: SerialQueryPayload,
}

impl SerialQuery {
    /// The payload type of a serial query.
    pub const PDU: u8 = 1;

    /// Creates a new serial query from the given state.
    pub fn new(version: u8, state: State) -> Self {
        SerialQuery {
            header: Header::new(
                version, Self::PDU, state.session(), Self::size()
            ),
            payload: SerialQueryPayload::new(state.serial()),
        }
    }
}

concrete!(SerialQuery);


//------------ SerialQueryPayload --------------------------------------------

/// The payload of a serial query.
///
/// This the serial query PDU without the header.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[repr(C, packed)]
pub struct SerialQueryPayload {
    serial: u32
}

impl SerialQueryPayload {
    /// Creates a new serial query payload from a serial number.
    pub fn new(serial: Serial) -> Self {
        SerialQueryPayload {
            serial: serial.to_be()
        }
    }

    /// Reads the serial query payload from a reader.
    pub async fn read<Sock: AsyncRead + Unpin>(
        sock: &mut Sock 
    ) -> Result<Self, io::Error> {
        let mut res = Self::default();
        sock.read_exact(res.as_mut()).await?;
        Ok(res)
    }

    /// Returns the router’s serial number announced in the serial query.
    pub fn serial(&self) -> Serial {
        Serial::from_be(self.serial)
    }
}

common!(SerialQueryPayload);


//------------ ResetQuery ----------------------------------------------------

/// A reset query requests the complete current set of data.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[repr(C, packed)]
pub struct ResetQuery {
    header: Header
}

impl ResetQuery {
    /// The payload type of a reset query.
    pub const PDU: u8 = 2;

    /// Creates a new reset query.
    pub fn new(version: u8) -> Self {
        ResetQuery {
            header: Header::new(version, 2, 0, 8)
        }
    }
}

concrete!(ResetQuery);


//------------ CacheResponse -------------------------------------------------

/// The cache response starts a sequence of payload PDUs with data.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[repr(C, packed)]
pub struct CacheResponse {
    header: Header
}

impl CacheResponse {
    /// The payload type of a cache response.
    pub const PDU: u8 = 3;

    /// Creates a new cache response for the given state.
    pub fn new(version: u8, state: State) -> Self {
        CacheResponse {
            header: Header::new(version, 3, state.session(), 8)
        }
    }
}

concrete!(CacheResponse);


//------------ Ipv4Prefix ----------------------------------------------------

/// An IPv4 prefix is the payload PDU for route origin authorisation in IPv4.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[repr(C, packed)]
#[allow(dead_code)]
pub struct Ipv4Prefix {
    header: Header,
    flags: u8,
    prefix_len: u8,
    max_len: u8,
    zero: u8,
    prefix: u32,
    asn: u32
}

impl Ipv4Prefix {
    /// The payload type of an IPv4 prefix.
    pub const PDU: u8 = 4;

    /// Creates a new IPv4 prefix from all the various fields.
    pub fn new(
        version: u8,
        flags: u8,
        prefix_len: u8,
        max_len: u8,
        prefix: Ipv4Addr,
        asn: Asn,
    ) -> Self {
        Ipv4Prefix {
            header: Header::new(version, Self::PDU, 0, 20),
            flags,
            prefix_len,
            max_len,
            zero: 0,
            prefix: u32::from(prefix).to_be(),
            asn: asn.into_u32().to_be()
        }
    }

    /// Returns the flags field of the prefix.
    ///
    /// The only flag currently used is the least significant but that is
    /// 1 for an announcement and 0 for a withdrawal.
    pub fn flags(&self) -> u8 {
        self.flags
    }

    /// Returns the prefix length.
    pub fn prefix_len(&self) -> u8 {
        self.prefix_len
    }

    /// Returns the max length.
    pub fn max_len(&self) -> u8 {
        self.max_len
    }

    /// Returns the prefix as IPv4 address.
    pub fn prefix(&self) -> Ipv4Addr {
        u32::from_be(self.prefix).into()
    }

    /// Returns the autonomous system number.
    pub fn asn(&self) -> Asn {
        u32::from_be(self.asn).into()
    }
}

concrete!(Ipv4Prefix);


//------------ Ipv6Prefix ----------------------------------------------------

/// An IPv6 prefix is the payload PDU for route origin authorisation in IPv6.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[repr(C, packed)]
#[allow(dead_code)]
pub struct Ipv6Prefix {
    header: Header,
    flags: u8,
    prefix_len: u8,
    max_len: u8,
    zero: u8,
    prefix: u128,
    asn: u32,
}

impl Ipv6Prefix {
    /// The payload type of an IPv6 prefix.
    pub const PDU: u8 = 6;

    /// Creates a new IPv6 prefix from all the various fields.
    pub fn new(
        version: u8,
        flags: u8,
        prefix_len: u8,
        max_len: u8,
        prefix: Ipv6Addr,
        asn: Asn,
    ) -> Self {
        Ipv6Prefix {
            header: Header::new(version, Self::PDU, 0, 32),
            flags,
            prefix_len,
            max_len,
            zero: 0,
            prefix: u128::from(prefix).to_be(),
            asn: asn.into_u32().to_be()
        }
    }

    /// Returns the flags field of the prefix.
    ///
    /// The only flag currently used is the least significant but that is
    /// 1 for an announcement and 0 for a withdrawal.
    pub fn flags(&self) -> u8 {
        self.flags
    }

    /// Returns the prefix length.
    pub fn prefix_len(&self) -> u8 {
        self.prefix_len
    }

    /// Returns the max length.
    pub fn max_len(&self) -> u8 {
        self.max_len
    }

    /// Returns the prefix as an IPv6 address.
    pub fn prefix(&self) -> Ipv6Addr {
        u128::from_be(self.prefix).into()
    }

    /// Returns the autonomous system number.
    pub fn asn(&self) -> Asn {
        u32::from_be(self.asn).into()
    }
}

concrete!(Ipv6Prefix);


//------------ RouterKey -----------------------------------------------------

/// A BGPsec router key.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct RouterKey {
    fixed: RouterKeyFixed,
    key_info: RouterKeyInfo,
}

#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[repr(C, packed)]
struct RouterKeyFixed {
    header: Header,
    key_identifier: [u8; 20],
    asn: u32,
}

impl RouterKey {
    /// The PDU type of a Router Key PDU.
    pub const PDU: u8 = 9;

    /// The maximum size of the subject public key info data.
    pub const fn max_key_info_size() -> usize {
        (u32::MAX as usize) - mem::size_of::<RouterKeyFixed>()
    }

    /// Creates a new router key PDU.
    ///
    /// # Panics
    ///
    /// This function panics if the length of the resulting PDU doesn’t fit
    /// in a `u32`.
    pub fn new(
        version: u8,
        flags: u8,
        key_identifier: [u8; 20],
        asn: Asn,
        key_info: RouterKeyInfo,
    ) -> Self {
        // We know it fits but let’s be sure.
        let len = u32::try_from(
            mem::size_of::<RouterKeyFixed>().checked_add(
                key_info.len()
            ).expect("RouterKey RTR PDU size overflow")
        ).expect("RouterKey RTR PDU size overflow");

        RouterKey {
            fixed: RouterKeyFixed {
                header: Header::new(
                    version, Self::PDU,
                    (flags as u16) << 8,
                    len
                ),
                key_identifier,
                asn: asn.into_u32().to_be(),
            },
            key_info
        }
    }

    /// Returns the value of the version field of the header.
    pub fn version(&self) -> u8 {
        self.fixed.header.version()
    }

    /// Returns the PDU size.
    ///
    /// The size is returned as a `u32` since that type is used in
    /// the header.
    pub fn size(&self) -> u32 {
        (
            mem::size_of::<RouterKeyFixed>() + self.key_info.len()
        ) as u32
    }

    /// Returns the flags field for the router key.
    ///
    /// The only flag currently used is the least significant bit that is
    /// 1 for an announcement and 0 for a withdrawal.
    pub fn flags(&self) -> u8 {
        // The two-byte Session field is reused for the Flags byte and one
        // reserved byte (of zeroes). As the value of the Session field is in
        // network byte order, we first convert it, then shift out the lower
        // byte.
        (u16::from_be(self.fixed.header.session) >> 8) as u8
    }

    /// Returns the subject key identifier.
    pub fn key_identifier(&self) -> [u8; 20] {
        self.fixed.key_identifier
    }

    /// Returns the ASN.
    pub fn asn(&self) -> Asn {
        u32::from_be(self.fixed.asn).into()
    }

    /// Returns a reference to the subject key info
    pub fn key_info(&self) -> &RouterKeyInfo {
        &self.key_info
    }

    /// Converts the PDU into the subject key info.
    pub fn into_key_info(self) -> RouterKeyInfo {
        self.key_info
    }

    /// Writes a value to a writer.
    pub async fn write<A: AsyncWrite + Unpin>(
        &self,
        a: &mut A
    ) -> Result<(), io::Error> {
        a.write_all(self.fixed.as_ref()).await?;
        a.write_all(self.key_info.as_ref()).await
    }

    /// Reads a value from a reader.
    ///
    /// If a value with a different PDU type is received, returns an
    /// error.
    pub async fn read<Sock: AsyncRead + Unpin>(
        sock: &mut Sock 
    ) -> Result<Self, io::Error> {
        let header = Header::read(sock).await?;
        if header.pdu() != Self::PDU {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "PDU type mismatch when expecting router key"
            ))
        }
        Self::read_payload(header, sock).await
    }

    /// Reads only the payload part of a value from a reader.
    pub async fn read_payload<Sock: AsyncRead + Unpin>(
        header: Header, sock: &mut Sock
    ) -> Result<Self, io::Error> {
        let info_len = match
            header.pdu_len()?.checked_sub(mem::size_of::<RouterKeyFixed>())
        {
            Some(len) => len,
            None => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "invalid length for router key"
                ))
            }
        };
        let mut fixed = RouterKeyFixed { header, .. Default::default() };
        sock.read_exact(&mut fixed.as_mut()[Header::LEN..]).await?;
        let key_info = RouterKeyInfo::read(sock, info_len).await?;
        Ok(RouterKey { fixed, key_info })
    }
}

impl AsRef<[u8]> for RouterKeyFixed {
    fn as_ref(&self) -> &[u8] {
        unsafe {
            slice::from_raw_parts(
                self as *const Self as *const u8,
                mem::size_of::<Self>()
            )
        }
    }
}

impl AsMut<[u8]> for RouterKeyFixed {
    fn as_mut(&mut self) -> &mut [u8] {
        unsafe {
            slice::from_raw_parts_mut(
                self as *mut Self as *mut u8,
                mem::size_of::<Self>()
            )
        }
    }
}


//------------ RouterKeyInfo -------------------------------------------------

/// The subject public key info data of a router key.
///
/// This is a simple newtype around a `Bytes` enforcing a size limit so that
/// a value can always be used in a router key PDU.
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct RouterKeyInfo(Bytes);

impl RouterKeyInfo {
    /// Creates a new value from some bytes.
    ///
    /// Returns an error if the bytes are too large to fit into a router key
    /// PDU.
    pub fn new(bytes: Bytes) -> Result<Self, KeyInfoError> {
        if bytes.len() > RouterKey::max_key_info_size() {
            Err(KeyInfoError)
        }
        else {
            Ok(RouterKeyInfo(bytes))
        }
    }

    /// Returns a reference to the octets of the value.
    pub fn as_slice(&self) -> &[u8] {
        self.0.as_ref()
    }

    /// Converts the value into the underlying bytes.
    pub fn into_bytes(self) -> Bytes {
        self.0
    }

    /// Reads a value of the given length from the buffer.
    async fn read<Sock: AsyncRead + Unpin>(
        sock: &mut Sock, len: usize,
    ) -> Result<Self, io::Error> {
        let mut key_info = vec![0u8; len];
        sock.read_exact(key_info.as_mut()).await?;
        Ok(RouterKeyInfo(key_info.into()))
    }
}


//--- TryFrom

impl TryFrom<Vec<u8>> for RouterKeyInfo {
    type Error = KeyInfoError;

    fn try_from(src: Vec<u8>) -> Result<Self, Self::Error> {
        Self::new(src.into())
    }
}

impl TryFrom<Bytes> for RouterKeyInfo {
    type Error = KeyInfoError;

    fn try_from(src: Bytes) -> Result<Self, Self::Error> {
        Self::new(src)
    }
}


//--- Deref, AsRef, and Borrow

impl ops::Deref for RouterKeyInfo {
    type Target = Bytes;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl AsRef<Bytes> for RouterKeyInfo {
    fn as_ref(&self) -> &Bytes {
        &self.0
    }
}

impl AsRef<[u8]> for RouterKeyInfo {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl borrow::Borrow<Bytes> for RouterKeyInfo {
    fn borrow(&self) -> &Bytes {
        self.as_ref()
    }
}

impl borrow::Borrow<[u8]> for RouterKeyInfo {
    fn borrow(&self) -> &[u8] {
        self.as_ref()
    }
}


//--- Display and Debug

impl fmt::Display for RouterKeyInfo{
    /// Formats the key info using the given formatter.
    ///
    /// The output format is identical to that used in local exception
    /// files defined by [RFC 8416], i.e., unpadded Base 64 using the URL-safe
    /// alphabet.
    ///
    /// [RFC 8416]: https://tools.ietf.org/html/rfc8416
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        base64::Slurm.display(self.0.as_ref()).fmt(f)
    }
}

impl fmt::Debug for RouterKeyInfo{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_tuple("RouterKeyInfo")
        .field(&format_args!("{self}"))
        .finish()
    }
}


//--- Arbitrary

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for RouterKeyInfo {
    fn arbitrary(
        u: &mut arbitrary::Unstructured<'a>
    ) -> arbitrary::Result<Self> {
        let size = usize::arbitrary(u)? % (RouterKey::max_key_info_size() + 1);
        Ok(Self(Bytes::copy_from_slice(u.bytes(size)?)))
    }
}


//------------ Aspa ----------------------------------------------------------

/// The PDU for ASPA.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Aspa {
    fixed: AspaFixed,
    providers: ProviderAsns,
}

#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[repr(C, packed)]
struct AspaFixed {
    header: Header,
    customer: u32,
}

impl Aspa {
    /// The PDU type of an ASPA PDU.
    pub const PDU: u8 = 11;

    /// Creates a new ASPA PDU.
    ///
    /// # Panics
    ///
    /// This function panics if the length of the resulting PDU doesn’t fit
    /// in a `u32`. Because `ProviderAsns` is now limited in size, this can’t
    /// happen.
    pub fn new(
        version: u8,
        flags: u8,
        customer: Asn,
        providers: ProviderAsns,
    ) -> Self {
        let len = u32::try_from(
            mem::size_of::<AspaFixed>().checked_add(
                providers.len()
            ).expect("ASPA RTR PDU size overflow")
        ).expect("ASPA RTR PDU size overflow");
        Aspa {
            fixed: AspaFixed {
                header: Header::new(
                    version, Self::PDU,
                    (flags as u16) << 8,
                    len
                ),
                customer: customer.into_u32().to_be(),
            },
            providers
        }
    }

    /// Returns the value of the version field of the header.
    pub fn version(&self) -> u8 {
        self.fixed.header.version()
    }

    /// Returns the PDU size.
    ///
    /// The size is returned as a `u32` since that type is used in
    /// the header.
    pub fn size(&self) -> u32 {
        u32::try_from(
            mem::size_of::<AspaFixed>() + self.providers.len()
        ).expect("long ASPA PDU")
    }

    /// Returns the flags field.
    ///
    /// The only flag currently used is the least significant bit that is
    /// 1 for an announcement and 0 for a withdrawal.
    pub fn flags(&self) -> u8 {
        // The two-byte Session field is reused for the Flags byte and one
        // reserved byte (of zeroes). As the value of the Session field is in
        // network byte order, we first convert it, then shift out the lower
        // byte.
        (u16::from_be(self.fixed.header.session) >> 8) as u8
    }

    /// Returns the customer ASN.
    pub fn customer(&self) -> Asn {
        u32::from_be(self.fixed.customer).into()
    }

    /// Returns a reference to the provider ASNs.
    pub fn providers(&self) -> &ProviderAsns {
        &self.providers
    }

    /// Converts the PDU into the provider ASNs.
    pub fn into_providers(self) -> ProviderAsns {
        self.providers
    }

    /// Writes a value to a writer.
    pub async fn write<A: AsyncWrite + Unpin>(
        &self,
        a: &mut A
    ) -> Result<(), io::Error> {
        a.write_all(self.fixed.as_ref()).await?;
        a.write_all(self.providers.as_ref()).await
    }

    /// Reads a value from a reader.
    ///
    /// If a value with a different PDU type is received, returns an
    /// error.
    pub async fn read<Sock: AsyncRead + Unpin>(
        sock: &mut Sock 
    ) -> Result<Self, io::Error> {
        let header = Header::read(sock).await?;
        if header.pdu() != Self::PDU {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "PDU type mismatch when expecting ASPA PDU"
            ))
        }
        Self::read_payload(header, sock).await
    }

    /// Reads only the payload part of a value from a reader.
    pub async fn read_payload<Sock: AsyncRead + Unpin>(
        header: Header, sock: &mut Sock
    ) -> Result<Self, io::Error> {
        let provider_len = match
            header.pdu_len()?.checked_sub(mem::size_of::<AspaFixed>())
        {
            Some(len) => {
                if len % 4 != 0 {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        "invalid length for ASPA PDU"
                    ))
                }
                len
            }
            None => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "invalid length for ASPA PDU"
                ))
            }
        };
        let mut fixed = AspaFixed { header, .. Default::default() };
        sock.read_exact(&mut fixed.as_mut()[Header::LEN..]).await?;
        let providers = ProviderAsns::read(sock, provider_len).await?;
        Ok(Aspa { fixed, providers })
    }
}

impl AsRef<[u8]> for AspaFixed {
    fn as_ref(&self) -> &[u8] {
        unsafe {
            slice::from_raw_parts(
                self as *const Self as *const u8,
                mem::size_of::<Self>()
            )
        }
    }
}

impl AsMut<[u8]> for AspaFixed {
    fn as_mut(&mut self) -> &mut [u8] {
        unsafe {
            slice::from_raw_parts_mut(
                self as *mut Self as *mut u8,
                mem::size_of::<Self>()
            )
        }
    }
}


//------------ ProviderAsns --------------------------------------------------

/// The provider ASNs of an ASPA PDU.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ProviderAsns(Bytes);

impl ProviderAsns {
    /// The maximum number of provider ASNs.
    pub const MAX_COUNT: usize = 16380;

    /// Returns an empty value.
    pub fn empty() -> Self {
        Self(Bytes::new())
    }

    /// Creates a new value from an iterator over ASNs.
    ///
    /// Returns an error if there are too many items in the iterator to fit
    /// into an RTR PDU.
    pub fn try_from_iter(
        iter: impl IntoIterator<Item = Asn>
    ) -> Result<Self, ProviderAsnsError> {
        let iter = iter.into_iter();
        let mut providers = Vec::with_capacity(iter.size_hint().0);
        iter.enumerate().try_for_each(|(idx, item)| {
            if idx >= Self::MAX_COUNT {
                return Err(ProviderAsnsError(()))
            }
            providers.extend_from_slice(&item.into_u32().to_be_bytes());
            Ok(())
        })?;
        Ok(ProviderAsns(providers.into()))
    }

    pub fn asn_count(&self) -> u16 {
        u16::try_from(
            self.0.len() / mem::size_of::<u32>()
        ).expect("ASPA RTR PDU size overflow")
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn iter(&self) -> impl Iterator<Item = Asn> + '_ {
        self.0.as_ref().chunks(mem::size_of::<u32>()).map(|chunk| {
            u32::from_be_bytes(
                TryFrom::try_from(chunk).expect("bad ASPA PDU size")
            ).into()
        })
    }

    /// Reads a value of the given length from the buffer.
    async fn read<Sock: AsyncRead + Unpin>(
        sock: &mut Sock, len: usize,
    ) -> Result<Self, io::Error> {
        let mut providers = vec![0u8; len];
        sock.read_exact(providers.as_mut()).await?;
        Ok(ProviderAsns(providers.into()))
    }
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for ProviderAsns {
    fn arbitrary(
        u: &mut arbitrary::Unstructured<'a>
    ) -> arbitrary::Result<Self> {
        let size = (
            usize::arbitrary(u)? % usize::from(u16::MAX)
        ) * mem::size_of::<Asn>();
        Ok(Self(Bytes::copy_from_slice(u.bytes(size)?)))
    }
}

impl AsRef<[u8]> for ProviderAsns {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}


//------------ Payload -------------------------------------------------------

/// All possible payload types.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum Payload {
    /// An IPv4 prefix.
    V4(Ipv4Prefix),

    /// An IPv6 prefix.
    V6(Ipv6Prefix),

    /// A router key.
    RouterKey(RouterKey),

    /// An ASPA unit.
    Aspa(Aspa),
}

impl Payload {
    /// Creates a payload PDU for the given payload.
    pub fn new(version: u8, flags: u8, payload: payload::PayloadRef) -> Self {
        match payload {
            payload::PayloadRef::Origin(origin) => {
                match origin.prefix.addr() {
                    IpAddr::V4(addr) => {
                        Payload::V4(Ipv4Prefix::new(
                            version,
                            flags,
                            origin.prefix.prefix_len(),
                            origin.prefix.resolved_max_len(),
                            addr,
                            origin.asn,
                        ))
                    }
                    IpAddr::V6(addr) => {
                        Payload::V6(Ipv6Prefix::new(
                            version,
                            flags,
                            origin.prefix.prefix_len(),
                            origin.prefix.resolved_max_len(),
                            addr,
                            origin.asn,
                        ))
                    }
                }
            }
            payload::PayloadRef::RouterKey(key) => {
                Payload::RouterKey(RouterKey::new(
                    version, flags,
                    key.key_identifier.into(),
                    key.asn,
                    key.key_info.clone()
                ))
            }
            payload::PayloadRef::Aspa(aspa) => {
                Payload::Aspa(Aspa::new(
                    version, flags, aspa.customer, aspa.providers.clone(),
                ))
            }
        }
    }

    /// Creates a payload PDU if it is supported in the given version.
    pub fn new_if_supported(
        version: u8, flags: u8, payload: payload::PayloadRef
    ) -> Option<Self> {
        let min_version = match payload {
            payload::PayloadRef::Origin(_) => 0,
            payload::PayloadRef::RouterKey(_) => 1,
            payload::PayloadRef::Aspa(_) => 2,
        };
        if min_version > version {
            None
        }
        else {
            Some(Self::new(version, flags, payload))
        }
    }

    /// Reads a payload PDU from a reader.
    ///
    /// The return type is a little convoluted, but hey. The method returns
    /// `Ok(Ok(Some(payload)))` if reading went well and there was a payload
    /// PDU that we support. It returns `Ok(Ok(None))`, if reading went well
    /// and there was a payload PDU but we don’t actually support it. If
    /// reading went well but we received an end-of-data PDU, it will be
    /// returned as `Ok(Err(eod))`. If reading fails or any other PDU is
    /// received, an error is returned.
    ///
    /// The reason we are just not returning unsupported payload types is that
    /// router keys and ASPA PDUs are variable length and we would need to
    /// allocate data. Which is a bit wasteful if we then just proceed to
    /// throw it away.
    pub async fn read<Sock: AsyncRead + Unpin>(
        sock: &mut Sock
    ) -> Result<Result<Option<Self>, EndOfData>, io::Error> {
        let header = Header::read(sock).await?;
        match header.pdu {
            Ipv4Prefix::PDU => {
                Ipv4Prefix::read_payload(header, sock).await.map(|res| {
                    Ok(Some(Payload::V4(res)))
                })
            }
            Ipv6Prefix::PDU => {
                Ipv6Prefix::read_payload(header, sock).await.map(|res| {
                    Ok(Some(Payload::V6(res)))
                })
            }
            RouterKey::PDU => {
                RouterKey::read_payload(header, sock).await.map(|res| {
                    Ok(Some(Payload::RouterKey(res)))
                })
            }
            Aspa::PDU => {
                Aspa::read_payload(header, sock).await.map(|res| {
                    Ok(Some(Payload::Aspa(res)))
                })
            }
            EndOfData::PDU => {
                EndOfData::read_payload(header, sock).await.map(Err)
            }
            _ => {
                Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "unexpected PDU in payload sequence"
                ))
            }
        }
    }

    /// Returns the RTR version of the payload PDU.
    pub fn version(&self) -> u8 {
        match *self {
            Payload::V4(ref data) => data.version(),
            Payload::V6(ref data) => data.version(),
            Payload::RouterKey(ref key) => key.version(),
            Payload::Aspa(ref aspa) => aspa.version(),
        }
    }

    /// Returns the flags of the payload PDU.
    pub fn flags(&self) -> u8 {
        match *self {
            Payload::V4(ref data) => data.flags(),
            Payload::V6(ref data) => data.flags(),
            Payload::RouterKey(ref key) => key.flags(),
            Payload::Aspa(ref aspa) => aspa.flags(),
        }
    }

    /// Writes the payload PDU to a writer.
    pub async fn write<A: AsyncWrite + Unpin>(
        &self,
        a: &mut A
    ) -> Result<(), io::Error> {
        match *self {
            Payload::V4(ref data) => data.write(a).await,
            Payload::V6(ref data) => data.write(a).await,
            Payload::RouterKey(ref data) => data.write(a).await,
            Payload::Aspa(ref aspa) => aspa.write(a).await,
        }
    }

    /// Converts the payload PDU into action and payload.
    ///
    /// Returns an error if the PDU isn’t acceptable for some reason.
    pub fn to_payload(
        &self
    ) -> Result<(payload::Action, payload::Payload), Error> {
        fn make_payload(
            action: &Action,
            payload: &Payload,
        ) -> Result<payload::Payload, &'static str> {
            match payload {
                Payload::V4(data) => {
                    Ok(payload::Payload::origin(
                        MaxLenPrefix::new(
                            Prefix::new_v4_relaxed(
                                data.prefix(), data.prefix_len()
                            )?,
                            Some(data.max_len())
                        )?,
                        data.asn(),
                    ))
                }
                Payload::V6(data) => {
                    Ok(payload::Payload::origin(
                        MaxLenPrefix::new(
                            Prefix::new_v6_relaxed(
                                data.prefix(), data.prefix_len()
                            )?,
                            Some(data.max_len())
                        )?,
                        data.asn(),
                    ))
                }
                Payload::RouterKey(key) => {
                    Ok(payload::Payload::router_key(
                        key.key_identifier().into(), key.asn(),
                        key.key_info().clone()
                    ))
                }
                Payload::Aspa(aspa) => {
                    Ok(match action {
                        Action::Withdraw => payload::Payload::aspa(
                            aspa.customer(), ProviderAsns::empty(),
                        ),
                        _ => payload::Payload::aspa(
                            aspa.customer(), aspa.providers().clone(),
                        ),
                    })
                }
            }
        }

        let action = payload::Action::from_flags(self.flags());
        Ok((
            action,
            make_payload(&action, self).map_err(|text| {
                Error::new(
                    self.version(), 0, self.as_partial_slice(), text.as_bytes()
                )
            })?
        ))
    }

    /// Returns an octets slice of as much of the PDU as possible.
    pub fn as_partial_slice(&self) -> &[u8] {
        match *self {
            Payload::V4(ref prefix) => prefix.as_ref(),
            Payload::V6(ref prefix) => prefix.as_ref(),
            Payload::RouterKey(ref key) => key.fixed.as_ref(),
            Payload::Aspa(ref aspa) => aspa.fixed.as_ref(),
        }
    }
}


//------------ EndOfData -----------------------------------------------------

/// End-of-data marks the end of sequence of payload PDUs.
///
/// This PDU differs between version 0 and 1 of RTR. Consequently, this
/// generic version is an enum that can be both, depending on the version
/// requested. For version 2, the PDU is the same as for version 1.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum EndOfData {
    V0(EndOfDataV0),
    V1(EndOfDataV1),
}

impl EndOfData {
    /// The PDU type of the end-of-data PDU.
    pub const PDU: u8 = 7;

    /// Creates a new end-of-data PDU from the data given.
    ///
    /// If version is 0, the `V0` variant is created and the three timer
    /// values are ignored. Otherwise, a `V1` variant is created with the
    /// given version.
    pub fn new(
        version: u8,
        state: State,
        timing: payload::Timing,
    ) -> Self {
        if version == 0 {
            EndOfData::V0(EndOfDataV0::new(state))
        }
        else {
            EndOfData::V1(EndOfDataV1::new(
                version, state, timing
            ))
        }
    }

    /// Reads the end-of-data payload from a reader.
    ///
    /// Which version of the end-of-data PDU is expected depends on the
    /// version field of the `header`. On success, the return value contains
    /// a full PDU, filling in missing data from `header`.
    pub async fn read_payload<Sock: AsyncRead + Unpin>(
        header: Header, sock: &mut Sock
    ) -> Result<Self, io::Error> {
        match header.version() {
            0 => {
                EndOfDataV0::read_payload(header, sock)
                    .await.map(EndOfData::V0)
            }
            1|2 => {
                EndOfDataV1::read_payload(header, sock)
                    .await.map(EndOfData::V1)
            }
            _ => {
                Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "invalid version in end of data PDU"
                ))
            }
        }
    }

    /// Returns the version field of the PDU.
    pub fn version(&self) -> u8 {
        match *self {
            EndOfData::V0(_) => 0,
            EndOfData::V1(v1_or_v2) => v1_or_v2.header.version()
        }
    }

    /// Returns the session ID.
    pub fn session(&self) -> u16 {
        match *self {
            EndOfData::V0(ref data) => data.session(),
            EndOfData::V1(ref data) => data.session(),
        }
    }

    /// Returns the serial number.
    pub fn serial(&self) -> Serial {
        match *self {
            EndOfData::V0(ref data) => data.serial(),
            EndOfData::V1(ref data) => data.serial(),
        }
    }

    /// Returns the state by combing session ID and serial number.
    pub fn state(&self) -> State {
        State::from_parts(self.session(), self.serial())
    }

    /// Returns the three timing values if they are available.
    ///
    /// The values are only available in the `V1` variant.
    pub fn timing(&self) -> Option<payload::Timing> {
        match *self {
            EndOfData::V0(_) => None,
            EndOfData::V1(ref data) => Some(data.timing()),
        }
    }

    /// Writes the PDU to a writer.
    pub async fn write<A: AsyncWrite + Unpin>(
        &self, a: &mut A
    ) -> Result<(), io::Error> {
        a.write_all(self.as_ref()).await
    }
}

impl AsRef<[u8]> for EndOfData {
    fn as_ref(&self) -> &[u8] {
        match *self {
            EndOfData::V0(ref inner) => inner.as_ref(),
            EndOfData::V1(ref inner) => inner.as_ref(),
        }
    }
}

impl AsMut<[u8]> for EndOfData {
    fn as_mut(&mut self) -> &mut [u8] {
        match *self {
            EndOfData::V0(ref mut inner) => inner.as_mut(),
            EndOfData::V1(ref mut inner) => inner.as_mut(),
        }
    }
}


//------------ EndOfDataV0 ---------------------------------------------------

/// End-of-data marks the end of sequence of payload PDUs.
///
/// This type is the version used in protocol version 0.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[repr(C, packed)]
pub struct EndOfDataV0 {
    header: Header,
    serial: u32
}

impl EndOfDataV0 {
    /// The PDU type of end-of-date.
    pub const PDU: u8 = 7;

    /// Creates a new end-of-data PDU from the given state.
    pub fn new(state: State) -> Self {
        EndOfDataV0 {
            header: Header::new(0, Self::PDU, state.session(), 12),
            serial: state.serial().to_be()
        }
    }

    /// Returns the serial number.
    pub fn serial(&self) -> Serial {
        Serial::from_be(self.serial)
    }
}

concrete!(EndOfDataV0);
    

//------------ EndOfDataV1 ---------------------------------------------------

/// End-of-data marks the end of sequence of payload PDUs.
///
/// This type is the version used beginning with protocol version 1.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[repr(C, packed)]
pub struct EndOfDataV1 {
    header: Header,
    serial: u32,
    refresh: u32,
    retry: u32,
    expire: u32,
}

impl EndOfDataV1 {
    /// The PDU type of end-of-data.
    pub const PDU: u8 = 7;

    /// Creates a new end-of-data PDU from state and timer values.
    pub fn new(
        version: u8,
        state: State,
        timing: payload::Timing,
    ) -> Self {
        EndOfDataV1 {
            header: Header::new(version, Self::PDU, state.session(), 24),
            serial: state.serial().to_be(),
            refresh: timing.refresh.to_be(),
            retry: timing.retry.to_be(),
            expire: timing.expire.to_be(),
        }
    }

    /// Returns the serial number.
    pub fn serial(&self) -> Serial {
        Serial::from_be(self.serial)
    }

    /// Returns the timing paramters.
    pub fn timing(&self) -> payload::Timing {
        payload::Timing {
            refresh: u32::from_be(self.refresh),
            retry: u32::from_be(self.retry),
            expire: u32::from_be(self.expire),
        }
    }
}

concrete!(EndOfDataV1);


//------------ CacheReset ----------------------------------------------------

/// Cache reset is a response to a serial query indicating unavailability.
///
/// If a cache doesn’t have information available that reaches back to the
/// serial number indicated in the serial query, it responds with a cache
/// reset.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[repr(C, packed)]
pub struct CacheReset {
    header: Header
}

impl CacheReset {
    /// The PDU type for a cache reset.
    pub const PDU: u8 = 8;

    /// Creates a cache reset.
    pub fn new(version: u8) -> Self {
        CacheReset {
            header: Header::new(version, Self::PDU, 0, 8)
        }
    }
}

concrete!(CacheReset);


//------------ Error ---------------------------------------------------------

/// An error report signals that something went wrong.
///
/// Error reports contain an error code and can contain both the erroneous
/// PDU and some diagnostic error text. Because of this, values of this type
/// are not fixed size byte arrays but rather are allocated according to the
/// contents of these two fields.
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct Error {
    octets: Vec<u8>,
}
/*
    /// The header of the error PDU.
    header: Header,

    /// The size of the embedded PDU in network byte order.
    pdu_len: u32,

    /// The embedded PDU.
    pdu: P,

    /// The size of the embedded reason text in network byte order.
    text_len: u32,

    /// The embedded text.
    text: T
*/

impl Error {
    /// The PDU type of an error PDU.
    pub const PDU: u8 = 10;

    /// Creates a new error PDU from components.
    pub fn new(
        version: u8,
        error_code: u16,
        pdu: impl AsRef<[u8]>,
        text: impl AsRef<[u8]>,
    ) -> Self {
        let pdu = pdu.as_ref();
        let text = text.as_ref();

        let size = 
            mem::size_of::<Header>()
            + 2 * mem::size_of::<u32>()
            + pdu.len() + text.len()
        ;
        let header = Header::new(
            version, 10, error_code, u32::try_from(size).unwrap()
        );

        let mut octets = Vec::with_capacity(size);
        octets.extend_from_slice(header.as_ref());
        octets.extend_from_slice(
            u32::try_from(pdu.len()).unwrap().to_be_bytes().as_ref()
        );
        octets.extend_from_slice(pdu);
        octets.extend_from_slice(
            u32::try_from(text.len()).unwrap().to_be_bytes().as_ref()
        );
        octets.extend_from_slice(text);

        Error { octets }
    }

    /// Skips over the payload of the error PDU.
    pub async fn skip_payload<Sock: AsyncRead + Unpin>(
        header: Header, sock: &mut Sock
    ) -> Result<(), io::Error> {
        let Some(mut remaining) = header.pdu_len()?.checked_sub(
            mem::size_of::<Header>()
        ) else {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "PDU size smaller than header size",
            ))
        };

        let mut buf = [0u8; 1024];
        while remaining > 0 {
            let read_len = cmp::min(remaining, mem::size_of_val(&buf));
            let read = sock.read(
                // Safety: We limited the length to the buffer size.
                unsafe { buf.get_unchecked_mut(..read_len) }
            ).await?;
            remaining -= read;
        }
        Ok(())
    }

    /// Writes the PUD to a writer.
    pub async fn write<A: AsyncWrite + Unpin>(
        &self, a: &mut A
    ) -> Result<(), io::Error> {
        a.write_all(self.as_ref()).await
    }
}


//--- AsRef and AsMut

impl AsRef<[u8]> for Error {
    fn as_ref(&self) -> &[u8] {
        self.octets.as_ref()
    }
}

impl AsMut<[u8]> for Error {
    fn as_mut(&mut self) -> &mut [u8] {
        self.octets.as_mut()
    }
}


//------------ ErrorCode -----------------------------------------------------

/// An error code.
///
/// This type wraps the raw error code that is part of the error PDU.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ErrorCode(pub u16);

impl ErrorCode {
    pub const CORRUPT_DATA: Self = Self(0);
    pub const INTERNAL_ERROR: Self = Self(1);
    pub const NO_DATA_AVAILABLE: Self = Self(2);
    pub const INVALID_REQUEST: Self = Self(3);
    pub const UNSUPPORTED_PROTOCOL_VERSION: Self = Self(4);
    pub const UNSUPPORTED_PDU_TYPE: Self = Self(5);
    pub const WITHDRAWAL_OF_UNKNOWN_RECORD: Self = Self(6);
    pub const DUPLICATE_ANNOUNCEMENT_RECEIVED: Self = Self(7);
    pub const UNEXPECTED_PROTOCOL_VERSION: Self = Self(8);
    pub const ASPA_PROVIDER_LIST_ERROR: Self = Self(9);
    pub const TRANSPORT_ERROR: Self = Self(10);
    pub const ORDERING_ERROR: Self = Self(11);
}

impl PartialEq<u16> for ErrorCode {
    fn eq(&self, other: &u16) -> bool {
        self.0 == *other
    }
}

impl PartialEq<ErrorCode> for u16 {
    fn eq(&self, other: &ErrorCode) -> bool {
        *self == other.0
    }
}


//------------ Header --------------------------------------------------------

/// The header portion of an RTR PDU.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[repr(C, packed)]
pub struct Header {
    /// The version of the PDU.
    version: u8,

    /// The PDU type.
    pdu: u8,

    /// The session ID for this RTR session.
    ///
    /// This field is re-used by some PDUs for other purposes.
    session: u16,

    /// The length of the PDU in network byte order.
    ///
    /// This is the size of the whole PDU including the header.
    length: u32,
}

impl Header {
    /// The size of the header.
    const LEN: usize = mem::size_of::<Self>();

    /// Creates a new header.
    pub fn new(version: u8, pdu: u8, session: u16, length: u32) -> Self {
        Header {
            version,
            pdu,
            session: session.to_be(),
            length: length.to_be(),
        }
    }

    /// Reads the header from a reader.
    pub async fn read<Sock: AsyncRead + Unpin>(
        sock: &mut Sock 
    ) -> Result<Self, io::Error> {
        let mut res = Self::default();
        sock.read_exact(res.as_mut()).await?;
        Ok(res)
    }

    /// Returns the version of this PDU.
    pub fn version(self) -> u8 {
        self.version
    }

    /// Returns the PDU type.
    pub fn pdu(self) -> u8 {
        self.pdu
    }

    /// Returns the session ID of this session.
    pub fn session(self) -> u16 {
        u16::from_be(self.session)
    }

    /// Returns the length of the PDU as a `u32`.
    ///
    /// This is the length of the full PDU including the header.
    pub fn length(self) -> u32 {
        u32::from_be(self.length)
    }

    /// Returns the length of the PDU as a `usize`.
    ///
    /// Since at least in theory `usize` may only be 16 bit long, the
    /// conversion can fail.
    ///
    /// This is the length of the full PDU including the header.
    pub fn pdu_len(self) -> Result<usize, io::Error> {
        usize::try_from(self.length()).map_err(|_| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                "PDU too large for this system to handle",
            )
        })
    }
}

common!(Header);


//============ ErrorTypes ====================================================

//------------ KeyInfoError --------------------------------------------------

/// The key info of a router key was too large.
#[derive(Clone, Copy, Debug)]
pub struct KeyInfoError;

impl fmt::Display for KeyInfoError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("router key size overflow")
    }
}

impl error::Error for KeyInfoError { }


//------------ ProviderAsnsError ---------------------------------------------

/// The provider ASNs of an ASPA unit were too large.
#[derive(Clone, Copy, Debug)]
pub struct ProviderAsnsError(());

impl fmt::Display for ProviderAsnsError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("provider ASNs size overflow")
    }
}

impl error::Error for ProviderAsnsError { }


//============ Tests =========================================================

#[cfg(all(test, feature = "tokio"))]
mod test {
    use super::*;
    use std::iter;

    const STATE: State = State::from_parts(0x1234, Serial(0xdead_beef));

    macro_rules! read_write {
        ( $ty:ident, $value:expr, $binary:expr $(,)? ) => {{
            let mut written = Vec::new();
            $value.write(&mut written).await.unwrap();
            assert_eq!(written, $binary);
            assert_eq!(
                $ty::read(&mut written.as_slice()).await.unwrap(),
                $value
            );
        }}
    }

    #[tokio::test]
    async fn read_write_serial_notify() {
        read_write!(
            SerialNotify,
            SerialNotify::new(1, STATE),
            [0x01, 0x00, 0x12, 0x34,   0, 0, 0, 12,   0xde, 0xad, 0xbe, 0xef]
        );
    }

    #[tokio::test]
    async fn read_write_serial_query() {
        read_write!(
            SerialQuery,
            SerialQuery::new(1, STATE),
            [0x01, 0x01, 0x12, 0x34,   0, 0, 0, 12,   0xde, 0xad, 0xbe, 0xef]
        );
    }

    #[tokio::test]
    async fn read_write_reset_query() {
        read_write!(
            ResetQuery,
            ResetQuery::new(1),
            [0x01, 0x02, 0, 0,   0, 0, 0, 8]
        );
    }

    #[tokio::test]
    async fn read_write_cache_response() {
        read_write!(
            CacheResponse,
            CacheResponse::new(1, STATE),
            [0x01, 0x03, 0x12, 0x34,   0, 0, 0, 8]
        );
    }

    #[tokio::test]
    async fn read_write_ipv4_prefix() {
        read_write!(
            Ipv4Prefix,
            Ipv4Prefix::new(
                1, 1, 23, 26, Ipv4Addr::new(192, 0, 2, 10),
                Asn::from_u32(0x1000f),
            ),
            [
                1, 4, 0, 0,      0, 0, 0, 20,  1, 23, 26, 0,
                192, 0, 2, 10,   0, 1, 0, 0x0f
            ],
        );
    }

    #[tokio::test]
    async fn read_write_ipv6_prefix() {
        read_write!(
            Ipv6Prefix,
            Ipv6Prefix::new(
                1, 1, 23, 26,
                Ipv6Addr::new(0x2001, 0xdb8, 0, 2, 10, 0, 0xdead, 0xbeef),
                Asn::from_u32(0x1000f),
            ),
            [
                1, 6, 0, 0,             0, 0, 0, 32,  1, 23, 26, 0,
                0x20, 0x01, 0xd, 0xb8,  0, 0, 0, 2,
                0, 10, 0, 0,            0xde, 0xad, 0xbe, 0xef,
                0, 1, 0, 0x0f
            ],
        );
    }

    #[tokio::test]
    async fn read_write_router_key() {
        read_write!(
            RouterKey,
            RouterKey::new(
                1, 1,
                [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],
                Asn::from_u32(0x1000f),
                RouterKeyInfo::new(Bytes::from_static(&[21,22,23,24])).unwrap()
            ),
            [
                1, 9, 1, 0,      0, 0, 0, 36,
                1, 2, 3, 4,      5, 6, 7, 8,
                9, 10, 11, 12,   13, 14, 15, 16,
                17, 18, 19, 20,  0, 1, 0, 15,
                21, 22, 23, 24,
            ]
        );
    }

    #[test]
    fn router_key_flags() {
        for flags in [0, 1, 128, 255] {
            let key = RouterKey::new(
                1, flags,
                [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],
                Asn::from_u32(0x1000f),
                RouterKeyInfo::new(Bytes::from_static(&[21,22,23,24])).unwrap()
            );
            assert_eq!(key.flags(), flags);
        }
    }

    #[tokio::test]
    async fn read_write_aspa() {
        read_write!(
            Aspa,
            Aspa::new(
                2, 1, Asn::from_u32(0x1000f),
                ProviderAsns::try_from_iter([
                    Asn::from_u32(0x1000d), Asn::from_u32(0x1000e)
                ]).unwrap(),
            ),
            [
                2, 11, 1, 0,    0, 0, 0, 20,
                0, 1, 0, 15,    0, 1, 0, 13,
                0, 1, 0, 14,
            ]
        );
    }

    #[test]
    fn aspa_flags() {
        for flags in [0, 1, 128, 255] {
            let aspa = Aspa::new(
                2, flags,
                Asn::from_u32(0x1000f),
                ProviderAsns::try_from_iter([
                    Asn::from_u32(0x1000d), Asn::from_u32(0x1000e)
                ]).unwrap(),
            );
            assert_eq!(aspa.flags(), flags);
        }
    }

    #[test]
    fn provider_count() {
        assert_eq!(
            ProviderAsns::try_from_iter(
                iter::repeat_n(Asn::from(0), ProviderAsns::MAX_COUNT - 1)
            ).unwrap().asn_count(),
            (ProviderAsns::MAX_COUNT - 1) as u16,
        );
        assert_eq!(
            ProviderAsns::try_from_iter(
                iter::repeat_n(Asn::from(0), ProviderAsns::MAX_COUNT)
            ).unwrap().asn_count(),
            ProviderAsns::MAX_COUNT as u16,
        );
        assert!(
            ProviderAsns::try_from_iter(
                iter::repeat_n(Asn::from(0), ProviderAsns::MAX_COUNT + 1)
            ).is_err()
        );
    }

    #[tokio::test]
    async fn read_write_end_of_data_v0() {
        read_write!(
            EndOfDataV0,
            EndOfDataV0::new(STATE),
            [0, 7, 0x12, 0x34,   0, 0, 0, 12,  0xde, 0xad, 0xbe, 0xef]
        );
    }

    #[tokio::test]
    async fn read_write_end_of_data_v1() {
        read_write!(
            EndOfDataV1,
            EndOfDataV1::new(1, STATE, Default::default()),
            [
                1, 7, 0x12, 0x34,         0, 0, 0, 24,
                0xde, 0xad, 0xbe, 0xef,   0x00, 0x00, 0x0e, 0x10,
                0x00, 0x00, 0x02, 0x58,   0x00, 0x00, 0x1c, 0x20,
            ]
        );
    }

    macro_rules! test_eod_pdu_version {
        ($version:expr, $raw:expr) => {
            if let Err(eod) = Payload::read(&mut $raw.as_slice()).await.unwrap() {
                assert_eq!($version, eod.version());
            } else {
                panic!("expected End of Data PDU");
            }
        }
    }

    #[tokio::test]
    async fn end_of_data_versions() {
        test_eod_pdu_version!(
            0,
            vec![0, 7, 0x12, 0x34, 0, 0, 0, 12,  0xde, 0xad, 0xbe, 0xef]
        );
        test_eod_pdu_version!(
            1,
            vec![
                0x01, 0x07, 0x8e, 0xef, 0x00, 0x00, 0x00, 0x18,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x8f,
                0x00, 0x00, 0x02, 0x58, 0x00, 0x00, 0x1c, 0x20
            ]
        );
        test_eod_pdu_version!(
            2,
            vec![
                0x02, 0x07, 0x8e, 0xef, 0x00, 0x00, 0x00, 0x18,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x8f,
                0x00, 0x00, 0x02, 0x58, 0x00, 0x00, 0x1c, 0x20
            ]
        );
    }

    #[tokio::test]
    async fn read_write_cache_reset() {
        read_write!(
            CacheReset,
            CacheReset::new(1),
            [1, 8, 0, 0,   0, 0, 0, 8]
        );
    }
}