rustbgpd-wire 0.7.0

BGP message codec — encode/decode OPEN, KEEPALIVE, UPDATE, NOTIFICATION, ROUTE-REFRESH
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
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
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
use std::fmt;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use bytes::Bytes;

use crate::capability::{Afi, Safi};
use crate::constants::{as_path_segment, attr_flags, attr_type};
use crate::error::DecodeError;
use crate::nlri::{NlriEntry, Prefix};
use crate::notification::update_subcode;

/// Origin attribute values per RFC 4271 §5.1.1.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum Origin {
    /// Learned via IGP.
    Igp = 0,
    /// Learned via EGP.
    Egp = 1,
    /// Origin undetermined.
    Incomplete = 2,
}

impl Origin {
    /// Create from a raw byte value.
    #[must_use]
    pub fn from_u8(value: u8) -> Option<Self> {
        match value {
            0 => Some(Self::Igp),
            1 => Some(Self::Egp),
            2 => Some(Self::Incomplete),
            _ => None,
        }
    }
}

impl std::fmt::Display for Origin {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Igp => write!(f, "IGP"),
            Self::Egp => write!(f, "EGP"),
            Self::Incomplete => write!(f, "INCOMPLETE"),
        }
    }
}

/// `AS_PATH` segment types per RFC 4271 §4.3.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum AsPathSegment {
    /// `AS_SET` — unordered set of ASNs.
    AsSet(Vec<u32>),
    /// `AS_SEQUENCE` — ordered sequence of ASNs.
    AsSequence(Vec<u32>),
}

/// `AS_PATH` attribute.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AsPath {
    /// Ordered list of path segments.
    pub segments: Vec<AsPathSegment>,
}

impl AsPath {
    /// Count the total number of ASNs in the path for best-path comparison.
    /// `AS_SET` counts as 1 regardless of size (RFC 4271 §9.1.2.2).
    #[must_use]
    pub fn len(&self) -> usize {
        self.segments
            .iter()
            .map(|seg| match seg {
                AsPathSegment::AsSequence(asns) => asns.len(),
                AsPathSegment::AsSet(_) => 1,
            })
            .sum()
    }

    /// Returns `true` if the path has no segments.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.segments.is_empty()
    }

    /// Returns true if `asn` appears in any segment (`AS_SEQUENCE` or `AS_SET`).
    /// Used for loop detection per RFC 4271 §9.1.2.
    #[must_use]
    pub fn contains_asn(&self, asn: u32) -> bool {
        self.segments.iter().any(|seg| match seg {
            AsPathSegment::AsSequence(asns) | AsPathSegment::AsSet(asns) => asns.contains(&asn),
        })
    }

    /// Extract the origin ASN from the `AS_PATH`.
    ///
    /// The origin AS is the last ASN in the rightmost `AS_SEQUENCE` segment.
    /// Returns `None` if the path has no `AS_SEQUENCE` segments or all
    /// `AS_SEQUENCE` segments are empty.
    #[must_use]
    pub fn origin_asn(&self) -> Option<u32> {
        self.segments.iter().rev().find_map(|seg| match seg {
            AsPathSegment::AsSequence(asns) => asns.last().copied(),
            AsPathSegment::AsSet(_) => None,
        })
    }

    /// Returns `true` if every ASN in the path is a private ASN.
    ///
    /// Returns `false` for empty paths (no ASNs to check).
    #[must_use]
    pub fn all_private(&self) -> bool {
        let mut count = 0;
        for seg in &self.segments {
            match seg {
                AsPathSegment::AsSequence(asns) | AsPathSegment::AsSet(asns) => {
                    for asn in asns {
                        count += 1;
                        if !is_private_asn(*asn) {
                            return false;
                        }
                    }
                }
            }
        }
        count > 0
    }

    /// Convert to a string representation for regex matching.
    ///
    /// `AS_SEQUENCE` segments produce space-separated ASNs.
    /// `AS_SET` segments produce `{ASN1 ASN2}` (curly braces, space-separated).
    /// Multiple segments are space-separated.
    ///
    /// Examples: `"65001 65002"`, `"65001 {65003 65004}"`, `""` (empty path).
    #[must_use]
    pub fn to_aspath_string(&self) -> String {
        let mut parts = Vec::new();
        for seg in &self.segments {
            match seg {
                AsPathSegment::AsSequence(asns) => {
                    for asn in asns {
                        parts.push(asn.to_string());
                    }
                }
                AsPathSegment::AsSet(asns) => {
                    let inner: Vec<String> = asns.iter().map(ToString::to_string).collect();
                    parts.push(format!("{{{}}}", inner.join(" ")));
                }
            }
        }
        parts.join(" ")
    }
}

/// Returns `true` if the given ASN falls in a private-use range.
///
/// Private ranges (RFC 5398 + RFC 6996):
/// - 16-bit: 64512–65534
/// - 32-bit: 4200000000–4294967294
#[must_use]
pub fn is_private_asn(asn: u32) -> bool {
    (64512..=65534).contains(&asn) || (4_200_000_000..=4_294_967_294).contains(&asn)
}

/// RFC 4760 `MP_REACH_NLRI` attribute (type code 14).
///
/// Uses [`NlriEntry`] to carry Add-Path path IDs alongside each prefix.
/// For non-Add-Path peers, `path_id` is always 0.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MpReachNlri {
    /// Address family.
    pub afi: Afi,
    /// Sub-address family.
    pub safi: Safi,
    /// Next-hop address for the announced prefixes.
    ///
    /// For IPv6, this stores only the global address. When a 32-byte
    /// next-hop is received (global + link-local per RFC 4760 §3), the
    /// decoder extracts the first 16 bytes (global) and discards the
    /// link-local portion. `IpAddr` can only hold a single address, and
    /// link-local next-hops are not needed for routing decisions.
    ///
    /// RFC 8950 allows IPv4 unicast NLRI to use an IPv6 next hop in
    /// `MP_REACH_NLRI`, so this field may be IPv6 even when `afi == Ipv4`.
    ///
    /// For `FlowSpec` (SAFI 133), next-hop length is 0 and this field is
    /// unused (defaults to `0.0.0.0`).
    pub next_hop: IpAddr,
    /// Announced NLRI entries.
    pub announced: Vec<NlriEntry>,
    /// `FlowSpec` NLRI rules (RFC 8955). Populated only when `safi == FlowSpec`.
    pub flowspec_announced: Vec<crate::flowspec::FlowSpecRule>,
    /// EVPN NLRI routes (RFC 7432). Populated only when `safi == Evpn`.
    pub evpn_announced: Vec<crate::evpn::EvpnRoute>,
}

/// RFC 4760 `MP_UNREACH_NLRI` attribute (type 15).
///
/// Uses [`NlriEntry`] to carry Add-Path path IDs alongside each prefix.
/// For non-Add-Path peers, `path_id` is always 0.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MpUnreachNlri {
    /// Address family.
    pub afi: Afi,
    /// Sub-address family.
    pub safi: Safi,
    /// Withdrawn NLRI entries.
    pub withdrawn: Vec<NlriEntry>,
    /// `FlowSpec` NLRI rules withdrawn (RFC 8955). Populated only when `safi == FlowSpec`.
    pub flowspec_withdrawn: Vec<crate::flowspec::FlowSpecRule>,
    /// EVPN NLRI routes withdrawn (RFC 7432). Populated only when `safi == Evpn`.
    pub evpn_withdrawn: Vec<crate::evpn::EvpnRoute>,
}

/// RFC 4360 Extended Community — 8-byte value stored as `u64`.
///
/// Wire layout: type (1) + sub-type (1) + value (6).
/// Bit 6 of the type byte: 0 = transitive, 1 = non-transitive.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ExtendedCommunity(u64);

impl ExtendedCommunity {
    /// Create from a raw 8-byte value.
    #[must_use]
    pub fn new(raw: u64) -> Self {
        Self(raw)
    }

    /// Return the raw 8-byte value.
    #[must_use]
    pub fn as_u64(self) -> u64 {
        self.0
    }

    /// High byte — IANA-assigned type.
    #[must_use]
    pub fn type_byte(self) -> u8 {
        (self.0 >> 56) as u8
    }

    /// Second byte — sub-type within the type.
    #[must_use]
    pub fn subtype(self) -> u8 {
        self.0.to_be_bytes()[1]
    }

    /// Transitive if bit 6 of the type byte is 0.
    #[must_use]
    pub fn is_transitive(self) -> bool {
        self.type_byte() & 0x40 == 0
    }

    /// Bytes 2-7 of the community value.
    #[must_use]
    pub fn value_bytes(self) -> [u8; 6] {
        let b = self.0.to_be_bytes();
        [b[2], b[3], b[4], b[5], b[6], b[7]]
    }

    /// Decode as Route Target (sub-type 0x02).
    ///
    /// Returns `(global_admin, local_admin)` as raw u32 values. The
    /// interpretation of `global_admin` depends on the type byte:
    /// - Type 0x00 (2-octet AS specific): global = ASN (fits u16), local = u32
    /// - Type 0x01 (IPv4 address specific): global = IPv4 addr as u32, local = u16
    /// - Type 0x02 (4-octet AS specific): global = ASN (u32), local = u16
    ///
    /// Callers that need to distinguish these encodings (e.g. for display as
    /// `RT:192.0.2.1:100` vs `RT:65001:100`) must also check [`type_byte()`](Self::type_byte).
    #[must_use]
    pub fn route_target(self) -> Option<(u32, u32)> {
        if self.subtype() != 0x02 {
            return None;
        }
        self.decode_two_part()
    }

    /// Decode as Route Origin (sub-type 0x03).
    ///
    /// Same layout as [`route_target()`](Self::route_target) — returns raw
    /// `(global_admin, local_admin)` with the same type-byte-dependent
    /// interpretation. Check [`type_byte()`](Self::type_byte) to distinguish
    /// 2-octet AS, IPv4-address, and 4-octet AS encodings.
    #[must_use]
    pub fn route_origin(self) -> Option<(u32, u32)> {
        if self.subtype() != 0x03 {
            return None;
        }
        self.decode_two_part()
    }

    // -------------------------------------------------------------------
    // EVPN-specific typed accessors (RFC 7432 / RFC 8365 / RFC 9135)
    // -------------------------------------------------------------------

    /// Decode as BGP Encapsulation Extended Community (RFC 9012 §4.1, encoded
    /// per the widely-deployed RFC 5512 layout: 4-byte reserved + 2-byte
    /// Tunnel Type). Type 0x03, subtype 0x0C.
    ///
    /// Returns the Tunnel Type code. For VXLAN-EVPN (RFC 8365), the value is
    /// 8. Other common values: 7 = NVGRE, 11 = MPLS-over-GRE.
    ///
    /// The reserved bytes are intentionally not validated here: RFC 5512
    /// specifies MUST-zero on send, ignored on receive. FRR, `GoBGP`, Cisco,
    /// and Juniper all emit zeros in practice; rejecting non-zero reserves
    /// would break interop in the rare case an unknown implementation
    /// re-purposes those bytes. Consumers should treat the returned
    /// `tunnel_type` as the semantic signal.
    #[must_use]
    pub fn as_bgp_encapsulation(self) -> Option<u16> {
        if self.type_byte() & 0x3F != 0x03 || self.subtype() != 0x0C {
            return None;
        }
        let v = self.value_bytes();
        Some(u16::from_be_bytes([v[4], v[5]]))
    }

    /// Construct a BGP Encapsulation Extended Community (RFC 9012 §4.1).
    ///
    /// Writes 4 bytes of reserved zero followed by the 16-bit tunnel type.
    #[must_use]
    pub fn bgp_encapsulation(tunnel_type: u16) -> Self {
        let tt = tunnel_type.to_be_bytes();
        let raw = u64::from_be_bytes([0x03, 0x0C, 0, 0, 0, 0, tt[0], tt[1]]);
        Self(raw)
    }

    /// Decode as MAC Mobility Extended Community (RFC 7432 §7.7).
    /// Type 0x06, subtype 0x00.
    ///
    /// Returns `(sticky, sequence_number)`. The sticky bit (bit 0 of the
    /// flags byte) marks the MAC as non-movable; receivers must not displace
    /// a sticky MAC with a higher-sequence non-sticky advertisement.
    #[must_use]
    pub fn as_mac_mobility(self) -> Option<(bool, u32)> {
        if self.type_byte() & 0x3F != 0x06 || self.subtype() != 0x00 {
            return None;
        }
        let v = self.value_bytes();
        let sticky = (v[0] & 0x01) != 0;
        let seq = u32::from_be_bytes([v[2], v[3], v[4], v[5]]);
        Some((sticky, seq))
    }

    /// Construct a MAC Mobility Extended Community (RFC 7432 §7.7).
    #[must_use]
    pub fn mac_mobility(sticky: bool, sequence: u32) -> Self {
        let flags = u8::from(sticky);
        let s = sequence.to_be_bytes();
        let raw = u64::from_be_bytes([0x06, 0x00, flags, 0, s[0], s[1], s[2], s[3]]);
        Self(raw)
    }

    /// Decode as ESI Label Extended Community (RFC 7432 §7.5).
    /// Type 0x06, subtype 0x01.
    ///
    /// Returns `(single_active, label)`. The single-active flag (bit 0 of
    /// the flags byte) signals single-active multi-homing mode.
    #[must_use]
    pub fn as_esi_label(self) -> Option<(bool, u32)> {
        if self.type_byte() & 0x3F != 0x06 || self.subtype() != 0x01 {
            return None;
        }
        let v = self.value_bytes();
        let single_active = (v[0] & 0x01) != 0;
        let label = (u32::from(v[3]) << 16) | (u32::from(v[4]) << 8) | u32::from(v[5]);
        Some((single_active, label))
    }

    /// Construct an ESI Label Extended Community (RFC 7432 §7.5).
    ///
    /// `label` is a 24-bit MPLS label or VXLAN VNI; high 8 bits are masked.
    #[must_use]
    pub fn esi_label(single_active: bool, label: u32) -> Self {
        let flags = u8::from(single_active);
        let l = label & 0x00FF_FFFF;
        #[expect(clippy::cast_possible_truncation)]
        let raw = u64::from_be_bytes([
            0x06,
            0x01,
            flags,
            0,
            0,
            (l >> 16) as u8,
            (l >> 8) as u8,
            l as u8,
        ]);
        Self(raw)
    }

    /// Decode as ES-Import Route Target Extended Community (RFC 7432 §7.6).
    /// Type 0x06, subtype 0x02.
    ///
    /// Returns the 6-byte MAC address that serves as the import target for
    /// Type 4 ES routes.
    #[must_use]
    pub fn as_es_import_rt(self) -> Option<[u8; 6]> {
        if self.type_byte() & 0x3F != 0x06 || self.subtype() != 0x02 {
            return None;
        }
        Some(self.value_bytes())
    }

    /// Construct an ES-Import Route Target Extended Community.
    #[must_use]
    pub fn es_import_rt(mac: [u8; 6]) -> Self {
        let raw = u64::from_be_bytes([0x06, 0x02, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]]);
        Self(raw)
    }

    /// Decode as Router MAC Extended Community (RFC 9135 §4.1).
    /// Type 0x06, subtype 0x03.
    ///
    /// Returns the 6-byte router MAC used for symmetric IRB.
    #[must_use]
    pub fn as_router_mac(self) -> Option<[u8; 6]> {
        if self.type_byte() & 0x3F != 0x06 || self.subtype() != 0x03 {
            return None;
        }
        Some(self.value_bytes())
    }

    /// Construct a Router MAC Extended Community (RFC 9135 §4.1).
    #[must_use]
    pub fn router_mac(mac: [u8; 6]) -> Self {
        let raw = u64::from_be_bytes([0x06, 0x03, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]]);
        Self(raw)
    }

    /// Decode as Default Gateway Extended Community (RFC 4761 §3.2.5 /
    /// RFC 7432). Type 0x03, subtype 0x0D. This is a flag-only community:
    /// presence is the signal and the 6-byte value field must be all zeros.
    /// Malformed advertisements with non-zero value bytes are treated as
    /// non-matches rather than silently accepted — downstream policy and
    /// validation consumers treat this accessor as semantic truth.
    #[must_use]
    pub fn as_default_gateway(self) -> bool {
        self.type_byte() & 0x3F == 0x03 && self.subtype() == 0x0D && self.value_bytes() == [0u8; 6]
    }

    /// Construct a Default Gateway Extended Community.
    #[must_use]
    pub fn default_gateway() -> Self {
        let raw = u64::from_be_bytes([0x03, 0x0D, 0, 0, 0, 0, 0, 0]);
        Self(raw)
    }

    /// Decode the 6-byte value field as `(global_admin, local_admin)`.
    ///
    /// Handles all three RFC 4360 two-part layouts (2-octet AS, IPv4, 4-octet
    /// AS). Returns raw u32 values — the caller decides how to interpret
    /// `global_admin` (ASN vs IPv4 address) based on `type_byte()`.
    fn decode_two_part(self) -> Option<(u32, u32)> {
        let v = self.value_bytes();
        let t = self.type_byte() & 0x3F; // mask off high two bits
        match t {
            // 2-octet AS specific: AS(2) + value(4)
            0x00 => {
                let global = u32::from(u16::from_be_bytes([v[0], v[1]]));
                let local = u32::from_be_bytes([v[2], v[3], v[4], v[5]]);
                Some((global, local))
            }
            // IPv4 Address specific (0x01) or 4-octet AS specific (0x02): 4 + 2
            0x01 | 0x02 => {
                let global = u32::from_be_bytes([v[0], v[1], v[2], v[3]]);
                let local = u32::from(u16::from_be_bytes([v[4], v[5]]));
                Some((global, local))
            }
            _ => None,
        }
    }
}

impl fmt::Display for ExtendedCommunity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let is_ipv4 = self.type_byte() & 0x3F == 0x01;
        if let Some((g, l)) = self.route_target() {
            if is_ipv4 {
                write!(f, "RT:{}:{l}", Ipv4Addr::from(g))
            } else {
                write!(f, "RT:{g}:{l}")
            }
        } else if let Some((g, l)) = self.route_origin() {
            if is_ipv4 {
                write!(f, "RO:{}:{l}", Ipv4Addr::from(g))
            } else {
                write!(f, "RO:{g}:{l}")
            }
        } else {
            write!(f, "0x{:016x}", self.0)
        }
    }
}

/// RFC 8092 Large Community — 12-byte value: `(global_admin, local_data1, local_data2)`.
///
/// Each field is a 32-bit unsigned integer. Display format: `"65001:100:200"`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct LargeCommunity {
    /// Global administrator (typically ASN).
    pub global_admin: u32,
    /// First local data part.
    pub local_data1: u32,
    /// Second local data part.
    pub local_data2: u32,
}

impl LargeCommunity {
    /// Create a new large community value.
    #[must_use]
    pub fn new(global_admin: u32, local_data1: u32, local_data2: u32) -> Self {
        Self {
            global_admin,
            local_data1,
            local_data2,
        }
    }
}

impl fmt::Display for LargeCommunity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}:{}:{}",
            self.global_admin, self.local_data1, self.local_data2
        )
    }
}

/// A known path attribute or raw preserved bytes.
///
/// Known attributes are decoded into typed variants. Unknown attributes
/// are preserved as `RawAttribute` for pass-through with the Partial bit.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PathAttribute {
    /// `ORIGIN` attribute (type 1).
    Origin(Origin),
    /// `AS_PATH` attribute (type 2).
    AsPath(AsPath),
    /// `NEXT_HOP` attribute (type 3).
    NextHop(Ipv4Addr),
    /// `LOCAL_PREF` attribute (type 5).
    LocalPref(u32),
    /// `MULTI_EXIT_DISC` attribute (type 4).
    Med(u32),
    /// RFC 1997 COMMUNITIES — each u32 is high16=ASN, low16=value.
    Communities(Vec<u32>),
    /// RFC 4360 EXTENDED COMMUNITIES.
    ExtendedCommunities(Vec<ExtendedCommunity>),
    /// RFC 8092 LARGE COMMUNITIES.
    LargeCommunities(Vec<LargeCommunity>),
    /// RFC 4456 `ORIGINATOR_ID` — original router-id of the route.
    OriginatorId(Ipv4Addr),
    /// RFC 4456 `CLUSTER_LIST` — list of cluster-ids traversed.
    ClusterList(Vec<Ipv4Addr>),
    /// RFC 4760 `MP_REACH_NLRI`.
    MpReachNlri(MpReachNlri),
    /// RFC 4760 `MP_UNREACH_NLRI`.
    MpUnreachNlri(MpUnreachNlri),
    /// Unknown or unrecognized attribute, preserved for re-advertisement.
    Unknown(RawAttribute),
}

impl PathAttribute {
    /// Return the type code of this attribute.
    #[must_use]
    pub fn type_code(&self) -> u8 {
        match self {
            Self::Origin(_) => attr_type::ORIGIN,
            Self::AsPath(_) => attr_type::AS_PATH,
            Self::NextHop(_) => attr_type::NEXT_HOP,
            Self::LocalPref(_) => attr_type::LOCAL_PREF,
            Self::Med(_) => attr_type::MULTI_EXIT_DISC,
            Self::Communities(_) => attr_type::COMMUNITIES,
            Self::OriginatorId(_) => attr_type::ORIGINATOR_ID,
            Self::ClusterList(_) => attr_type::CLUSTER_LIST,
            Self::ExtendedCommunities(_) => attr_type::EXTENDED_COMMUNITIES,
            Self::LargeCommunities(_) => attr_type::LARGE_COMMUNITIES,
            Self::MpReachNlri(_) => attr_type::MP_REACH_NLRI,
            Self::MpUnreachNlri(_) => attr_type::MP_UNREACH_NLRI,
            Self::Unknown(raw) => raw.type_code,
        }
    }

    /// Return the wire flags for this attribute.
    #[must_use]
    pub fn flags(&self) -> u8 {
        match self {
            Self::Origin(_) | Self::AsPath(_) | Self::NextHop(_) | Self::LocalPref(_) => {
                attr_flags::TRANSITIVE
            }
            Self::Med(_)
            | Self::OriginatorId(_)
            | Self::ClusterList(_)
            | Self::MpReachNlri(_)
            | Self::MpUnreachNlri(_) => attr_flags::OPTIONAL,
            Self::Communities(_) | Self::ExtendedCommunities(_) | Self::LargeCommunities(_) => {
                attr_flags::OPTIONAL | attr_flags::TRANSITIVE
            }
            Self::Unknown(raw) => raw.flags,
        }
    }
}

/// Raw attribute preserved for pass-through (RFC 4271 §5).
///
/// On re-advertisement, the Partial bit (0x20) is OR'd into `flags`.
/// All other flags and bytes are preserved unchanged.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RawAttribute {
    /// Attribute flags byte (optional, transitive, partial, extended-length).
    pub flags: u8,
    /// Attribute type code.
    pub type_code: u8,
    /// Raw attribute value bytes.
    pub data: Bytes,
}

/// Decode path attributes from wire bytes (RFC 4271 §4.3).
///
/// Each attribute is: flags(1) + type(1) + length(1 or 2) + value.
/// The Extended Length flag determines 1-byte vs 2-byte length.
///
/// `four_octet_as` controls whether AS numbers in `AS_PATH` are 2 or 4 bytes.
///
/// # Errors
///
/// Returns `DecodeError` on truncated data or malformed attribute values.
pub fn decode_path_attributes(
    mut buf: &[u8],
    four_octet_as: bool,
    add_path_families: &[(Afi, Safi)],
) -> Result<Vec<PathAttribute>, DecodeError> {
    let mut attrs = Vec::new();

    while !buf.is_empty() {
        // Need at least flags(1) + type(1) = 2
        if buf.len() < 2 {
            return Err(DecodeError::MalformedField {
                message_type: "UPDATE",
                detail: "truncated attribute header".to_string(),
            });
        }

        let flags = buf[0];
        let type_code = buf[1];
        buf = &buf[2..];

        let extended = (flags & attr_flags::EXTENDED_LENGTH) != 0;
        let value_len = if extended {
            if buf.len() < 2 {
                return Err(DecodeError::MalformedField {
                    message_type: "UPDATE",
                    detail: "truncated extended-length attribute".to_string(),
                });
            }
            let len = u16::from_be_bytes([buf[0], buf[1]]) as usize;
            buf = &buf[2..];
            len
        } else {
            if buf.is_empty() {
                return Err(DecodeError::MalformedField {
                    message_type: "UPDATE",
                    detail: "truncated attribute length".to_string(),
                });
            }
            let len = buf[0] as usize;
            buf = &buf[1..];
            len
        };

        if buf.len() < value_len {
            return Err(DecodeError::MalformedField {
                message_type: "UPDATE",
                detail: format!(
                    "attribute type {type_code} value truncated: need {value_len}, have {}",
                    buf.len()
                ),
            });
        }

        let value = &buf[..value_len];
        buf = &buf[value_len..];

        let attr =
            decode_attribute_value(flags, type_code, value, four_octet_as, add_path_families)?;
        attrs.push(attr);
    }

    Ok(attrs)
}

/// Decode a single attribute value given its flags, type code, and raw bytes.
#[expect(clippy::too_many_lines)]
fn decode_attribute_value(
    flags: u8,
    type_code: u8,
    value: &[u8],
    four_octet_as: bool,
    add_path_families: &[(Afi, Safi)],
) -> Result<PathAttribute, DecodeError> {
    // Validate Optional + Transitive flags for known attribute types (RFC 4271 §6.3).
    let flags_mask = attr_flags::OPTIONAL | attr_flags::TRANSITIVE;
    if let Some(expected) = expected_flags(type_code)
        && (flags & flags_mask) != expected
    {
        return Err(DecodeError::UpdateAttributeError {
            subcode: update_subcode::ATTRIBUTE_FLAGS_ERROR,
            data: attr_error_data(flags, type_code, value),
            detail: format!(
                "type {} flags {:#04x} (expected {:#04x})",
                type_code,
                flags & flags_mask,
                expected
            ),
        });
    }

    match type_code {
        attr_type::ORIGIN => {
            if value.len() != 1 {
                return Err(DecodeError::UpdateAttributeError {
                    subcode: update_subcode::ATTRIBUTE_LENGTH_ERROR,
                    data: attr_error_data(flags, type_code, value),
                    detail: format!("ORIGIN length {} (expected 1)", value.len()),
                });
            }
            match Origin::from_u8(value[0]) {
                Some(origin) => Ok(PathAttribute::Origin(origin)),
                None => Err(DecodeError::UpdateAttributeError {
                    subcode: update_subcode::INVALID_ORIGIN,
                    data: attr_error_data(flags, type_code, value),
                    detail: format!("invalid ORIGIN value {}", value[0]),
                }),
            }
        }

        attr_type::AS_PATH => {
            let segments = decode_as_path(value, four_octet_as).map_err(|e| {
                DecodeError::UpdateAttributeError {
                    subcode: update_subcode::MALFORMED_AS_PATH,
                    data: attr_error_data(flags, type_code, value),
                    detail: e.to_string(),
                }
            })?;
            Ok(PathAttribute::AsPath(AsPath { segments }))
        }

        attr_type::NEXT_HOP => {
            if value.len() != 4 {
                return Err(DecodeError::UpdateAttributeError {
                    subcode: update_subcode::ATTRIBUTE_LENGTH_ERROR,
                    data: attr_error_data(flags, type_code, value),
                    detail: format!("NEXT_HOP length {} (expected 4)", value.len()),
                });
            }
            let addr = Ipv4Addr::new(value[0], value[1], value[2], value[3]);
            Ok(PathAttribute::NextHop(addr))
        }

        attr_type::MULTI_EXIT_DISC => {
            if value.len() != 4 {
                return Err(DecodeError::UpdateAttributeError {
                    subcode: update_subcode::ATTRIBUTE_LENGTH_ERROR,
                    data: attr_error_data(flags, type_code, value),
                    detail: format!("MED length {} (expected 4)", value.len()),
                });
            }
            let med = u32::from_be_bytes([value[0], value[1], value[2], value[3]]);
            Ok(PathAttribute::Med(med))
        }

        attr_type::LOCAL_PREF => {
            if value.len() != 4 {
                return Err(DecodeError::UpdateAttributeError {
                    subcode: update_subcode::ATTRIBUTE_LENGTH_ERROR,
                    data: attr_error_data(flags, type_code, value),
                    detail: format!("LOCAL_PREF length {} (expected 4)", value.len()),
                });
            }
            let lp = u32::from_be_bytes([value[0], value[1], value[2], value[3]]);
            Ok(PathAttribute::LocalPref(lp))
        }

        attr_type::COMMUNITIES => {
            if !value.len().is_multiple_of(4) {
                return Err(DecodeError::UpdateAttributeError {
                    subcode: update_subcode::ATTRIBUTE_LENGTH_ERROR,
                    data: attr_error_data(flags, type_code, value),
                    detail: format!("COMMUNITIES length {} not a multiple of 4", value.len()),
                });
            }
            let communities = value
                .chunks_exact(4)
                .map(|c| u32::from_be_bytes([c[0], c[1], c[2], c[3]]))
                .collect();
            Ok(PathAttribute::Communities(communities))
        }

        attr_type::EXTENDED_COMMUNITIES => {
            if !value.len().is_multiple_of(8) {
                return Err(DecodeError::UpdateAttributeError {
                    subcode: update_subcode::ATTRIBUTE_LENGTH_ERROR,
                    data: attr_error_data(flags, type_code, value),
                    detail: format!(
                        "EXTENDED_COMMUNITIES length {} not a multiple of 8",
                        value.len()
                    ),
                });
            }
            let communities = value
                .chunks_exact(8)
                .map(|c| {
                    ExtendedCommunity::new(u64::from_be_bytes([
                        c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7],
                    ]))
                })
                .collect();
            Ok(PathAttribute::ExtendedCommunities(communities))
        }

        attr_type::ORIGINATOR_ID => {
            if value.len() != 4 {
                return Err(DecodeError::UpdateAttributeError {
                    subcode: update_subcode::ATTRIBUTE_LENGTH_ERROR,
                    data: attr_error_data(flags, type_code, value),
                    detail: format!("ORIGINATOR_ID length {} (expected 4)", value.len()),
                });
            }
            let addr = Ipv4Addr::new(value[0], value[1], value[2], value[3]);
            Ok(PathAttribute::OriginatorId(addr))
        }

        attr_type::CLUSTER_LIST => {
            if !value.len().is_multiple_of(4) {
                return Err(DecodeError::UpdateAttributeError {
                    subcode: update_subcode::ATTRIBUTE_LENGTH_ERROR,
                    data: attr_error_data(flags, type_code, value),
                    detail: format!("CLUSTER_LIST length {} not a multiple of 4", value.len()),
                });
            }
            let ids = value
                .chunks_exact(4)
                .map(|c| Ipv4Addr::new(c[0], c[1], c[2], c[3]))
                .collect();
            Ok(PathAttribute::ClusterList(ids))
        }

        attr_type::LARGE_COMMUNITIES => {
            if value.is_empty() || !value.len().is_multiple_of(12) {
                return Err(DecodeError::UpdateAttributeError {
                    subcode: update_subcode::ATTRIBUTE_LENGTH_ERROR,
                    data: attr_error_data(flags, type_code, value),
                    detail: format!(
                        "LARGE_COMMUNITIES length {} invalid (must be non-zero multiple of 12)",
                        value.len()
                    ),
                });
            }
            let communities = value
                .chunks_exact(12)
                .map(|c| {
                    LargeCommunity::new(
                        u32::from_be_bytes([c[0], c[1], c[2], c[3]]),
                        u32::from_be_bytes([c[4], c[5], c[6], c[7]]),
                        u32::from_be_bytes([c[8], c[9], c[10], c[11]]),
                    )
                })
                .collect();
            Ok(PathAttribute::LargeCommunities(communities))
        }

        attr_type::MP_REACH_NLRI => decode_mp_reach_nlri(value, add_path_families),
        attr_type::MP_UNREACH_NLRI => decode_mp_unreach_nlri(value, add_path_families),

        // ATOMIC_AGGREGATE, AGGREGATOR, and any unknown type → RawAttribute
        _ => Ok(PathAttribute::Unknown(RawAttribute {
            flags,
            type_code,
            data: Bytes::copy_from_slice(value),
        })),
    }
}

/// Decode `MP_REACH_NLRI` (type 14) attribute value.
///
/// Wire layout (RFC 4760 §3):
///   AFI (2) | SAFI (1) | NH-Len (1) | Next Hop (variable) | Reserved (1) | NLRI (variable)
#[expect(clippy::too_many_lines)]
fn decode_mp_reach_nlri(
    value: &[u8],
    add_path_families: &[(Afi, Safi)],
) -> Result<PathAttribute, DecodeError> {
    if value.len() < 5 {
        return Err(DecodeError::MalformedField {
            message_type: "UPDATE",
            detail: format!("MP_REACH_NLRI too short: {} bytes", value.len()),
        });
    }

    let afi_raw = u16::from_be_bytes([value[0], value[1]]);
    let safi_raw = value[2];
    let nh_len = value[3] as usize;

    let afi = Afi::from_u16(afi_raw).ok_or_else(|| DecodeError::MalformedField {
        message_type: "UPDATE",
        detail: format!("MP_REACH_NLRI unsupported AFI {afi_raw}"),
    })?;
    let safi = Safi::from_u8(safi_raw).ok_or_else(|| DecodeError::MalformedField {
        message_type: "UPDATE",
        detail: format!("MP_REACH_NLRI unsupported SAFI {safi_raw}"),
    })?;

    // 4 bytes for AFI+SAFI+NH-Len, then nh_len bytes, then 1 reserved byte
    if value.len() < 4 + nh_len + 1 {
        return Err(DecodeError::MalformedField {
            message_type: "UPDATE",
            detail: format!(
                "MP_REACH_NLRI truncated: NH-Len={nh_len}, have {} bytes total",
                value.len()
            ),
        });
    }

    let nh_bytes = &value[4..4 + nh_len];
    // FlowSpec (SAFI 133): NH length is 0 — no next-hop for filter rules
    let next_hop = if safi == Safi::FlowSpec {
        if nh_len != 0 {
            return Err(DecodeError::MalformedField {
                message_type: "UPDATE",
                detail: format!("MP_REACH_NLRI FlowSpec next-hop length {nh_len} (expected 0)"),
            });
        }
        IpAddr::V4(Ipv4Addr::UNSPECIFIED)
    } else {
        match afi {
            Afi::Ipv4 => match nh_len {
                4 => IpAddr::V4(Ipv4Addr::new(
                    nh_bytes[0],
                    nh_bytes[1],
                    nh_bytes[2],
                    nh_bytes[3],
                )),
                16 | 32 => {
                    let mut octets = [0u8; 16];
                    octets.copy_from_slice(&nh_bytes[..16]);
                    IpAddr::V6(Ipv6Addr::from(octets))
                }
                _ => {
                    return Err(DecodeError::MalformedField {
                        message_type: "UPDATE",
                        detail: format!(
                            "MP_REACH_NLRI IPv4 next-hop length {nh_len} (expected 4, 16, or 32)"
                        ),
                    });
                }
            },
            Afi::Ipv6 => {
                if nh_len != 16 && nh_len != 32 {
                    return Err(DecodeError::MalformedField {
                        message_type: "UPDATE",
                        detail: format!(
                            "MP_REACH_NLRI IPv6 next-hop length {nh_len} (expected 16 or 32)"
                        ),
                    });
                }
                // Take first 16 bytes (global address); ignore link-local if 32
                let mut octets = [0u8; 16];
                octets.copy_from_slice(&nh_bytes[..16]);
                IpAddr::V6(Ipv6Addr::from(octets))
            }
            Afi::L2Vpn => match nh_len {
                4 => IpAddr::V4(Ipv4Addr::new(
                    nh_bytes[0],
                    nh_bytes[1],
                    nh_bytes[2],
                    nh_bytes[3],
                )),
                16 => {
                    let mut octets = [0u8; 16];
                    octets.copy_from_slice(&nh_bytes[..16]);
                    IpAddr::V6(Ipv6Addr::from(octets))
                }
                _ => {
                    return Err(DecodeError::MalformedField {
                        message_type: "UPDATE",
                        detail: format!(
                            "MP_REACH_NLRI L2VPN next-hop length {nh_len} (expected 4 or 16)"
                        ),
                    });
                }
            },
        }
    };

    // Skip reserved byte
    let nlri_start = 4 + nh_len + 1;
    let nlri_bytes = &value[nlri_start..];

    // FlowSpec (SAFI 133): NLRI is FlowSpec rules, not prefixes
    if safi == Safi::FlowSpec {
        let flowspec_rules = crate::flowspec::decode_flowspec_nlri(nlri_bytes, afi)?;
        return Ok(PathAttribute::MpReachNlri(MpReachNlri {
            afi,
            safi,
            next_hop,
            announced: vec![],
            flowspec_announced: flowspec_rules,
            evpn_announced: vec![],
        }));
    }

    // EVPN (AFI 25 / SAFI 70): NLRI is typed EVPN routes, not prefixes
    if afi == Afi::L2Vpn && safi == Safi::Evpn {
        let routes = crate::evpn::decode_evpn_nlri(nlri_bytes)?;
        return Ok(PathAttribute::MpReachNlri(MpReachNlri {
            afi,
            safi,
            next_hop,
            announced: vec![],
            flowspec_announced: vec![],
            evpn_announced: routes,
        }));
    }

    // SAFI 70 (EVPN) is only defined for AFI 25 (L2VPN). Reject any other
    // AFI explicitly so the unicast NLRI fallthrough below cannot
    // misinterpret the typed EVPN payload as a prefix list.
    if safi == Safi::Evpn {
        return Err(DecodeError::MalformedField {
            message_type: "UPDATE",
            detail: format!(
                "MP_REACH_NLRI SAFI EVPN with non-L2VPN AFI {} (only AFI L2VPN supported)",
                afi as u16
            ),
        });
    }

    let add_path = add_path_families.contains(&(afi, safi));
    let announced = match (afi, add_path) {
        (Afi::Ipv4, false) => crate::nlri::decode_nlri(nlri_bytes)?
            .into_iter()
            .map(|p| NlriEntry {
                path_id: 0,
                prefix: Prefix::V4(p),
            })
            .collect(),
        (Afi::Ipv4, true) => crate::nlri::decode_nlri_addpath(nlri_bytes)?
            .into_iter()
            .map(|e| NlriEntry {
                path_id: e.path_id,
                prefix: Prefix::V4(e.prefix),
            })
            .collect(),
        (Afi::Ipv6, false) => crate::nlri::decode_ipv6_nlri(nlri_bytes)?
            .into_iter()
            .map(|p| NlriEntry {
                path_id: 0,
                prefix: Prefix::V6(p),
            })
            .collect(),
        (Afi::Ipv6, true) => crate::nlri::decode_ipv6_nlri_addpath(nlri_bytes)?,
        (Afi::L2Vpn, _) => {
            return Err(DecodeError::MalformedField {
                message_type: "UPDATE",
                detail: format!(
                    "MP_REACH_NLRI L2VPN with unsupported SAFI {} (only EVPN supported)",
                    safi as u8
                ),
            });
        }
    };

    Ok(PathAttribute::MpReachNlri(MpReachNlri {
        afi,
        safi,
        next_hop,
        announced,
        flowspec_announced: vec![],
        evpn_announced: vec![],
    }))
}

/// Decode `MP_UNREACH_NLRI` (type 15) attribute value.
///
/// Wire layout (RFC 4760 §4):
///   AFI (2) | SAFI (1) | Withdrawn Routes (variable)
fn decode_mp_unreach_nlri(
    value: &[u8],
    add_path_families: &[(Afi, Safi)],
) -> Result<PathAttribute, DecodeError> {
    if value.len() < 3 {
        return Err(DecodeError::MalformedField {
            message_type: "UPDATE",
            detail: format!("MP_UNREACH_NLRI too short: {} bytes", value.len()),
        });
    }

    let afi_raw = u16::from_be_bytes([value[0], value[1]]);
    let safi_raw = value[2];

    let afi = Afi::from_u16(afi_raw).ok_or_else(|| DecodeError::MalformedField {
        message_type: "UPDATE",
        detail: format!("MP_UNREACH_NLRI unsupported AFI {afi_raw}"),
    })?;
    let safi = Safi::from_u8(safi_raw).ok_or_else(|| DecodeError::MalformedField {
        message_type: "UPDATE",
        detail: format!("MP_UNREACH_NLRI unsupported SAFI {safi_raw}"),
    })?;

    let withdrawn_bytes = &value[3..];

    // FlowSpec (SAFI 133): withdrawn is FlowSpec rules
    if safi == Safi::FlowSpec {
        let flowspec_rules = crate::flowspec::decode_flowspec_nlri(withdrawn_bytes, afi)?;
        return Ok(PathAttribute::MpUnreachNlri(MpUnreachNlri {
            afi,
            safi,
            withdrawn: vec![],
            flowspec_withdrawn: flowspec_rules,
            evpn_withdrawn: vec![],
        }));
    }

    // EVPN (AFI 25 / SAFI 70): withdrawn is typed EVPN routes, not prefixes
    if afi == Afi::L2Vpn && safi == Safi::Evpn {
        let routes = crate::evpn::decode_evpn_nlri(withdrawn_bytes)?;
        return Ok(PathAttribute::MpUnreachNlri(MpUnreachNlri {
            afi,
            safi,
            withdrawn: vec![],
            flowspec_withdrawn: vec![],
            evpn_withdrawn: routes,
        }));
    }

    // SAFI 70 (EVPN) is only defined for AFI 25 (L2VPN). Reject any other
    // AFI explicitly so the unicast NLRI fallthrough below cannot
    // misinterpret the typed EVPN payload as a prefix list.
    if safi == Safi::Evpn {
        return Err(DecodeError::MalformedField {
            message_type: "UPDATE",
            detail: format!(
                "MP_UNREACH_NLRI SAFI EVPN with non-L2VPN AFI {} (only AFI L2VPN supported)",
                afi as u16
            ),
        });
    }

    let add_path = add_path_families.contains(&(afi, safi));
    let withdrawn = match (afi, add_path) {
        (Afi::Ipv4, false) => crate::nlri::decode_nlri(withdrawn_bytes)?
            .into_iter()
            .map(|p| NlriEntry {
                path_id: 0,
                prefix: Prefix::V4(p),
            })
            .collect(),
        (Afi::Ipv4, true) => crate::nlri::decode_nlri_addpath(withdrawn_bytes)?
            .into_iter()
            .map(|e| NlriEntry {
                path_id: e.path_id,
                prefix: Prefix::V4(e.prefix),
            })
            .collect(),
        (Afi::Ipv6, false) => crate::nlri::decode_ipv6_nlri(withdrawn_bytes)?
            .into_iter()
            .map(|p| NlriEntry {
                path_id: 0,
                prefix: Prefix::V6(p),
            })
            .collect(),
        (Afi::Ipv6, true) => crate::nlri::decode_ipv6_nlri_addpath(withdrawn_bytes)?,
        (Afi::L2Vpn, _) => {
            return Err(DecodeError::MalformedField {
                message_type: "UPDATE",
                detail: format!(
                    "MP_UNREACH_NLRI L2VPN with unsupported SAFI {} (only EVPN supported)",
                    safi as u8
                ),
            });
        }
    };

    Ok(PathAttribute::MpUnreachNlri(MpUnreachNlri {
        afi,
        safi,
        withdrawn,
        flowspec_withdrawn: vec![],
        evpn_withdrawn: vec![],
    }))
}

/// Decode `AS_PATH` segments from the attribute value bytes.
fn decode_as_path(mut buf: &[u8], four_octet_as: bool) -> Result<Vec<AsPathSegment>, DecodeError> {
    let as_size: usize = if four_octet_as { 4 } else { 2 };
    let mut segments = Vec::new();

    while !buf.is_empty() {
        if buf.len() < 2 {
            return Err(DecodeError::MalformedField {
                message_type: "UPDATE",
                detail: "truncated AS_PATH segment header".to_string(),
            });
        }

        let seg_type = buf[0];
        let seg_count = buf[1] as usize;
        buf = &buf[2..];

        let needed = seg_count * as_size;
        if buf.len() < needed {
            return Err(DecodeError::MalformedField {
                message_type: "UPDATE",
                detail: format!(
                    "AS_PATH segment truncated: need {needed} bytes for {seg_count} ASNs, have {}",
                    buf.len()
                ),
            });
        }

        let mut asns = Vec::with_capacity(seg_count);
        for _ in 0..seg_count {
            let asn = if four_octet_as {
                let v = u32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]]);
                buf = &buf[4..];
                v
            } else {
                let v = u32::from(u16::from_be_bytes([buf[0], buf[1]]));
                buf = &buf[2..];
                v
            };
            asns.push(asn);
        }

        match seg_type {
            as_path_segment::AS_SET => segments.push(AsPathSegment::AsSet(asns)),
            as_path_segment::AS_SEQUENCE => segments.push(AsPathSegment::AsSequence(asns)),
            _ => {
                return Err(DecodeError::MalformedField {
                    message_type: "UPDATE",
                    detail: format!("unknown AS_PATH segment type {seg_type}"),
                });
            }
        }
    }

    Ok(segments)
}

/// Build the attribute-triplet (flags + type + length + value) used as
/// NOTIFICATION data in UPDATE error subcodes per RFC 4271 §6.3.
pub(crate) fn attr_error_data(flags: u8, type_code: u8, value: &[u8]) -> Vec<u8> {
    let mut buf = Vec::with_capacity(3 + value.len());
    if value.len() > 255 {
        buf.push(flags | attr_flags::EXTENDED_LENGTH);
        buf.push(type_code);
        #[expect(clippy::cast_possible_truncation)]
        let len = value.len() as u16;
        buf.extend_from_slice(&len.to_be_bytes());
    } else {
        buf.push(flags);
        buf.push(type_code);
        #[expect(clippy::cast_possible_truncation)]
        buf.push(value.len() as u8);
    }
    buf.extend_from_slice(value);
    buf
}

/// Return the expected Optional + Transitive flags for known attribute types.
/// Returns `None` for unrecognized types (no validation performed).
fn expected_flags(type_code: u8) -> Option<u8> {
    match type_code {
        // Well-known mandatory/discretionary: Optional=0, Transitive=1
        attr_type::ORIGIN
        | attr_type::AS_PATH
        | attr_type::NEXT_HOP
        | attr_type::LOCAL_PREF
        | attr_type::ATOMIC_AGGREGATE => Some(attr_flags::TRANSITIVE),
        // Optional non-transitive (RFC 4760 §3/§4: MP_REACH/UNREACH are non-transitive;
        // RFC 4456: ORIGINATOR_ID and CLUSTER_LIST are optional non-transitive)
        attr_type::MULTI_EXIT_DISC
        | attr_type::ORIGINATOR_ID
        | attr_type::CLUSTER_LIST
        | attr_type::MP_REACH_NLRI
        | attr_type::MP_UNREACH_NLRI => Some(attr_flags::OPTIONAL),
        // Optional transitive
        attr_type::AGGREGATOR
        | attr_type::COMMUNITIES
        | attr_type::EXTENDED_COMMUNITIES
        | attr_type::LARGE_COMMUNITIES => Some(attr_flags::OPTIONAL | attr_flags::TRANSITIVE),
        _ => None,
    }
}

/// Encode path attributes to wire bytes.
///
/// `four_octet_as` controls whether AS numbers in `AS_PATH` are 2 or 4 bytes.
/// Encode a list of path attributes into wire format.
///
/// When `add_path_mp` is true, `MP_REACH_NLRI` and `MP_UNREACH_NLRI` NLRI
/// entries include 4-byte path IDs per RFC 7911.
pub fn encode_path_attributes(
    attrs: &[PathAttribute],
    buf: &mut Vec<u8>,
    four_octet_as: bool,
    add_path_mp: bool,
) {
    for attr in attrs {
        let mut value = Vec::new();
        let flags;
        let type_code;

        match attr {
            PathAttribute::Origin(origin) => {
                flags = attr_flags::TRANSITIVE;
                type_code = attr_type::ORIGIN;
                value.push(*origin as u8);
            }
            PathAttribute::AsPath(as_path) => {
                flags = attr_flags::TRANSITIVE;
                type_code = attr_type::AS_PATH;
                encode_as_path(as_path, &mut value, four_octet_as);
            }
            PathAttribute::NextHop(addr) => {
                flags = attr_flags::TRANSITIVE;
                type_code = attr_type::NEXT_HOP;
                value.extend_from_slice(&addr.octets());
            }
            PathAttribute::Med(med) => {
                flags = attr_flags::OPTIONAL;
                type_code = attr_type::MULTI_EXIT_DISC;
                value.extend_from_slice(&med.to_be_bytes());
            }
            PathAttribute::LocalPref(lp) => {
                flags = attr_flags::TRANSITIVE;
                type_code = attr_type::LOCAL_PREF;
                value.extend_from_slice(&lp.to_be_bytes());
            }
            PathAttribute::Communities(communities) => {
                flags = attr_flags::OPTIONAL | attr_flags::TRANSITIVE;
                type_code = attr_type::COMMUNITIES;
                for &c in communities {
                    value.extend_from_slice(&c.to_be_bytes());
                }
            }
            PathAttribute::ExtendedCommunities(communities) => {
                flags = attr_flags::OPTIONAL | attr_flags::TRANSITIVE;
                type_code = attr_type::EXTENDED_COMMUNITIES;
                for &c in communities {
                    value.extend_from_slice(&c.as_u64().to_be_bytes());
                }
            }
            PathAttribute::LargeCommunities(communities) => {
                flags = attr_flags::OPTIONAL | attr_flags::TRANSITIVE;
                type_code = attr_type::LARGE_COMMUNITIES;
                for &c in communities {
                    value.extend_from_slice(&c.global_admin.to_be_bytes());
                    value.extend_from_slice(&c.local_data1.to_be_bytes());
                    value.extend_from_slice(&c.local_data2.to_be_bytes());
                }
            }
            PathAttribute::OriginatorId(addr) => {
                flags = attr_flags::OPTIONAL;
                type_code = attr_type::ORIGINATOR_ID;
                value.extend_from_slice(&addr.octets());
            }
            PathAttribute::ClusterList(ids) => {
                flags = attr_flags::OPTIONAL;
                type_code = attr_type::CLUSTER_LIST;
                for id in ids {
                    value.extend_from_slice(&id.octets());
                }
            }
            PathAttribute::MpReachNlri(mp) => {
                flags = attr_flags::OPTIONAL;
                type_code = attr_type::MP_REACH_NLRI;
                encode_mp_reach_nlri(mp, &mut value, add_path_mp);
            }
            PathAttribute::MpUnreachNlri(mp) => {
                flags = attr_flags::OPTIONAL;
                type_code = attr_type::MP_UNREACH_NLRI;
                encode_mp_unreach_nlri(mp, &mut value, add_path_mp);
            }
            PathAttribute::Unknown(raw) => {
                // RFC 4271 §5: unrecognized *optional* transitive attributes
                // must be propagated with the Partial bit set. Well-known
                // transitive attributes (OPTIONAL=0) must NOT get PARTIAL.
                let optional_transitive = attr_flags::OPTIONAL | attr_flags::TRANSITIVE;
                flags = if (raw.flags & optional_transitive) == optional_transitive {
                    raw.flags | attr_flags::PARTIAL
                } else {
                    raw.flags
                };
                type_code = raw.type_code;
                value.extend_from_slice(&raw.data);
            }
        }

        // Use extended length if value > 255 bytes
        if value.len() > 255 {
            buf.push(flags | attr_flags::EXTENDED_LENGTH);
            buf.push(type_code);
            #[expect(clippy::cast_possible_truncation)]
            let len = value.len() as u16;
            buf.extend_from_slice(&len.to_be_bytes());
        } else {
            buf.push(flags);
            buf.push(type_code);
            #[expect(clippy::cast_possible_truncation)]
            buf.push(value.len() as u8);
        }
        buf.extend_from_slice(&value);
    }
}

/// Encode `MP_REACH_NLRI` value bytes.
///
/// When `add_path` is true, each NLRI entry includes a 4-byte path ID
/// prefix per RFC 7911.
fn encode_mp_reach_nlri(mp: &MpReachNlri, buf: &mut Vec<u8>, add_path: bool) {
    buf.extend_from_slice(&(mp.afi as u16).to_be_bytes());
    buf.push(mp.safi as u8);

    // FlowSpec: NH length = 0, reserved = 0, then FlowSpec NLRI
    if mp.safi == Safi::FlowSpec {
        buf.push(0); // NH-Len = 0
        buf.push(0); // Reserved
        crate::flowspec::encode_flowspec_nlri(&mp.flowspec_announced, buf, mp.afi);
        return;
    }

    // EVPN: next-hop is the VTEP loopback IP (4 or 16 bytes), then EVPN NLRI
    if mp.afi == Afi::L2Vpn && mp.safi == Safi::Evpn {
        match mp.next_hop {
            IpAddr::V4(addr) => {
                buf.push(4);
                buf.extend_from_slice(&addr.octets());
            }
            IpAddr::V6(addr) => {
                buf.push(16);
                buf.extend_from_slice(&addr.octets());
            }
        }
        buf.push(0); // Reserved
        crate::evpn::encode_evpn_nlri(&mp.evpn_announced, buf);
        return;
    }

    match mp.next_hop {
        IpAddr::V4(addr) => {
            buf.push(4); // NH-Len
            buf.extend_from_slice(&addr.octets());
        }
        IpAddr::V6(addr) => {
            buf.push(16); // NH-Len
            buf.extend_from_slice(&addr.octets());
        }
    }

    buf.push(0); // Reserved

    if add_path {
        crate::nlri::encode_ipv6_nlri_addpath(&mp.announced, buf);
    } else {
        for entry in &mp.announced {
            match entry.prefix {
                Prefix::V4(p) => crate::nlri::encode_nlri(&[p], buf),
                Prefix::V6(p) => crate::nlri::encode_ipv6_nlri(&[p], buf),
            }
        }
    }
}

/// Encode `MP_UNREACH_NLRI` value bytes.
///
/// When `add_path` is true, each withdrawn entry includes a 4-byte path ID.
fn encode_mp_unreach_nlri(mp: &MpUnreachNlri, buf: &mut Vec<u8>, add_path: bool) {
    buf.extend_from_slice(&(mp.afi as u16).to_be_bytes());
    buf.push(mp.safi as u8);

    // FlowSpec: encode FlowSpec NLRI rules
    if mp.safi == Safi::FlowSpec {
        crate::flowspec::encode_flowspec_nlri(&mp.flowspec_withdrawn, buf, mp.afi);
        return;
    }

    // EVPN: encode EVPN NLRI routes
    if mp.afi == Afi::L2Vpn && mp.safi == Safi::Evpn {
        crate::evpn::encode_evpn_nlri(&mp.evpn_withdrawn, buf);
        return;
    }

    if add_path {
        crate::nlri::encode_ipv6_nlri_addpath(&mp.withdrawn, buf);
    } else {
        for entry in &mp.withdrawn {
            match entry.prefix {
                Prefix::V4(p) => crate::nlri::encode_nlri(&[p], buf),
                Prefix::V6(p) => crate::nlri::encode_ipv6_nlri(&[p], buf),
            }
        }
    }
}

/// Encode `AS_PATH` segments into value bytes.
fn encode_as_path(as_path: &AsPath, buf: &mut Vec<u8>, four_octet_as: bool) {
    for segment in &as_path.segments {
        let (seg_type, asns) = match segment {
            AsPathSegment::AsSet(asns) => (as_path_segment::AS_SET, asns),
            AsPathSegment::AsSequence(asns) => (as_path_segment::AS_SEQUENCE, asns),
        };
        for chunk in asns.chunks(u8::MAX as usize) {
            buf.push(seg_type);
            #[expect(clippy::cast_possible_truncation)]
            buf.push(chunk.len() as u8);
            for &asn in chunk {
                if four_octet_as {
                    buf.extend_from_slice(&asn.to_be_bytes());
                } else {
                    // RFC 6793: ASNs > 65535 are mapped to AS_TRANS (23456)
                    // in 2-octet AS_PATH encoding.
                    let as2 = u16::try_from(asn).unwrap_or(crate::constants::AS_TRANS);
                    buf.extend_from_slice(&as2.to_be_bytes());
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn mp_reach_evpn_attribute_roundtrip() {
        use crate::evpn::{EthernetTagId, EvpnImet, EvpnRoute, RouteDistinguisher};

        let mp = MpReachNlri {
            afi: Afi::L2Vpn,
            safi: Safi::Evpn,
            next_hop: IpAddr::V4(Ipv4Addr::new(10, 0, 0, 100)),
            announced: vec![],
            flowspec_announced: vec![],
            evpn_announced: vec![EvpnRoute::Imet(EvpnImet {
                rd: RouteDistinguisher([0, 0, 0xFD, 0xE8, 0, 0, 0, 0x64]),
                ethernet_tag: EthernetTagId(100),
                originator_ip: IpAddr::V4(Ipv4Addr::new(10, 0, 0, 100)),
            })],
        };
        let attr = PathAttribute::MpReachNlri(mp);

        let mut buf = Vec::new();
        encode_path_attributes(std::slice::from_ref(&attr), &mut buf, true, false);
        let decoded = decode_path_attributes(&buf, true, &[]).expect("decode");
        assert_eq!(decoded.len(), 1);
        assert_eq!(attr, decoded[0]);

        let PathAttribute::MpReachNlri(dec) = &decoded[0] else {
            panic!("not MP_REACH after decode");
        };
        assert_eq!(dec.afi, Afi::L2Vpn);
        assert_eq!(dec.safi, Safi::Evpn);
        assert_eq!(dec.evpn_announced.len(), 1);
        assert!(matches!(dec.evpn_announced[0], EvpnRoute::Imet(_)));
    }

    #[test]
    fn mp_unreach_evpn_attribute_roundtrip() {
        use crate::evpn::{EthernetSegmentIdentifier, EvpnEs, EvpnRoute, RouteDistinguisher};

        let mp = MpUnreachNlri {
            afi: Afi::L2Vpn,
            safi: Safi::Evpn,
            withdrawn: vec![],
            flowspec_withdrawn: vec![],
            evpn_withdrawn: vec![EvpnRoute::Es(EvpnEs {
                rd: RouteDistinguisher([0, 0, 0xFD, 0xE8, 0, 0, 0, 0x64]),
                esi: EthernetSegmentIdentifier([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
                originator_ip: IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)),
            })],
        };
        let attr = PathAttribute::MpUnreachNlri(mp);
        let mut buf = Vec::new();
        encode_path_attributes(std::slice::from_ref(&attr), &mut buf, true, false);
        let decoded = decode_path_attributes(&buf, true, &[]).expect("decode");
        assert_eq!(decoded.len(), 1);
        assert_eq!(attr, decoded[0]);
    }

    // ---- EVPN extended community typed accessors (RFC 7432 / 8365 / 9135) ---

    #[test]
    fn ext_comm_bgp_encapsulation_vxlan() {
        let c = ExtendedCommunity::bgp_encapsulation(8); // VXLAN
        assert_eq!(c.type_byte(), 0x03);
        assert_eq!(c.subtype(), 0x0C);
        assert_eq!(c.as_bgp_encapsulation(), Some(8));
        // Wire layout: 4 bytes reserved + 2-byte tunnel type
        let b = c.as_u64().to_be_bytes();
        assert_eq!(b[2..6], [0, 0, 0, 0]);
        assert_eq!(&b[6..8], &[0, 8]);
        // Negative: other subtypes return None
        assert_eq!(ExtendedCommunity::new(0).as_bgp_encapsulation(), None);
    }

    #[test]
    fn ext_comm_mac_mobility_sticky_and_sequence() {
        let m1 = ExtendedCommunity::mac_mobility(false, 42);
        assert_eq!(m1.as_mac_mobility(), Some((false, 42)));
        let m2 = ExtendedCommunity::mac_mobility(true, 12345);
        assert_eq!(m2.as_mac_mobility(), Some((true, 12345)));
        // Round-trip max sequence
        let m3 = ExtendedCommunity::mac_mobility(true, u32::MAX);
        assert_eq!(m3.as_mac_mobility(), Some((true, u32::MAX)));
        assert_eq!(ExtendedCommunity::new(0).as_mac_mobility(), None);
    }

    #[test]
    fn ext_comm_esi_label_flags_and_label() {
        let e1 = ExtendedCommunity::esi_label(false, 10_000);
        assert_eq!(e1.as_esi_label(), Some((false, 10_000)));
        let e2 = ExtendedCommunity::esi_label(true, 0x00FF_FFFF);
        assert_eq!(e2.as_esi_label(), Some((true, 0x00FF_FFFF)));
    }

    #[test]
    fn ext_comm_es_import_rt_mac() {
        let mac = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
        let e = ExtendedCommunity::es_import_rt(mac);
        assert_eq!(e.as_es_import_rt(), Some(mac));
        assert_eq!(e.type_byte(), 0x06);
        assert_eq!(e.subtype(), 0x02);
    }

    #[test]
    fn ext_comm_router_mac() {
        let mac = [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff];
        let e = ExtendedCommunity::router_mac(mac);
        assert_eq!(e.as_router_mac(), Some(mac));
    }

    #[test]
    fn ext_comm_default_gateway_flag_only() {
        let d = ExtendedCommunity::default_gateway();
        assert!(d.as_default_gateway());
        // Not a default gateway
        assert!(!ExtendedCommunity::bgp_encapsulation(8).as_default_gateway());
    }

    /// Regression: Default Gateway is a flag-only community (RFC 7432).
    /// Malformed advertisements that set non-zero bytes in the value
    /// field must NOT be treated as default-gateway matches.
    #[test]
    fn ext_comm_default_gateway_rejects_nonzero_value() {
        // Correct type/subtype (0x03/0x0D) but bogus value.
        let malformed =
            ExtendedCommunity::new(u64::from_be_bytes([0x03, 0x0D, 0, 0, 0, 0, 0, 0x01]));
        assert!(
            !malformed.as_default_gateway(),
            "default-gateway accessor must require all-zero value bytes"
        );
        // Sanity: the clean form still passes.
        assert!(ExtendedCommunity::default_gateway().as_default_gateway());
    }

    #[test]
    fn ext_comm_accessors_return_none_on_unrelated_communities() {
        let rt = ExtendedCommunity::new(u64::from_be_bytes([0x00, 0x02, 0xFD, 0xE8, 0, 0, 0, 100])); // RT:65000:100
        assert_eq!(rt.as_bgp_encapsulation(), None);
        assert_eq!(rt.as_mac_mobility(), None);
        assert_eq!(rt.as_esi_label(), None);
        assert_eq!(rt.as_es_import_rt(), None);
        assert_eq!(rt.as_router_mac(), None);
        assert!(!rt.as_default_gateway());
    }

    #[test]
    fn origin_from_u8_roundtrip() {
        assert_eq!(Origin::from_u8(0), Some(Origin::Igp));
        assert_eq!(Origin::from_u8(1), Some(Origin::Egp));
        assert_eq!(Origin::from_u8(2), Some(Origin::Incomplete));
        assert_eq!(Origin::from_u8(3), None);
    }

    #[test]
    fn origin_ordering() {
        assert!(Origin::Igp < Origin::Egp);
        assert!(Origin::Egp < Origin::Incomplete);
    }

    #[test]
    fn as_path_length_calculation() {
        let path = AsPath {
            segments: vec![
                AsPathSegment::AsSequence(vec![65001, 65002, 65003]),
                AsPathSegment::AsSet(vec![65004, 65005]),
            ],
        };
        // Sequence: 3 ASNs, Set: counts as 1 → total 4
        assert_eq!(path.len(), 4);
    }

    #[test]
    fn as_path_empty() {
        let path = AsPath { segments: vec![] };
        assert!(path.is_empty());
        assert_eq!(path.len(), 0);
    }

    #[test]
    fn contains_asn_in_sequence() {
        let path = AsPath {
            segments: vec![AsPathSegment::AsSequence(vec![65001, 65002, 65003])],
        };
        assert!(path.contains_asn(65002));
        assert!(!path.contains_asn(65004));
    }

    #[test]
    fn contains_asn_in_set() {
        let path = AsPath {
            segments: vec![AsPathSegment::AsSet(vec![65004, 65005])],
        };
        assert!(path.contains_asn(65005));
        assert!(!path.contains_asn(65001));
    }

    #[test]
    fn contains_asn_multiple_segments() {
        let path = AsPath {
            segments: vec![
                AsPathSegment::AsSequence(vec![65001, 65002]),
                AsPathSegment::AsSet(vec![65003]),
            ],
        };
        assert!(path.contains_asn(65001));
        assert!(path.contains_asn(65003));
        assert!(!path.contains_asn(65004));
    }

    #[test]
    fn contains_asn_empty_path() {
        let path = AsPath { segments: vec![] };
        assert!(!path.contains_asn(65001));
    }

    #[test]
    fn is_private_asn_boundaries() {
        // 16-bit private range boundaries
        assert!(!is_private_asn(64_511));
        assert!(is_private_asn(64_512));
        assert!(is_private_asn(65_534));
        assert!(!is_private_asn(65_535));

        // 32-bit private range boundaries
        assert!(!is_private_asn(4_199_999_999));
        assert!(is_private_asn(4_200_000_000));
        assert!(is_private_asn(4_294_967_294));
        assert!(!is_private_asn(4_294_967_295));
    }

    #[test]
    fn all_private_empty_path_is_false() {
        let path = AsPath { segments: vec![] };
        assert!(!path.all_private());
    }

    #[test]
    fn all_private_mixed_segments() {
        let path = AsPath {
            segments: vec![
                AsPathSegment::AsSet(vec![64_512, 65_000]),
                AsPathSegment::AsSequence(vec![4_200_000_000, 65_534]),
            ],
        };
        assert!(path.all_private());

        let non_private = AsPath {
            segments: vec![
                AsPathSegment::AsSet(vec![64_512, 65_000]),
                AsPathSegment::AsSequence(vec![65_535]),
            ],
        };
        assert!(!non_private.all_private());
    }

    #[test]
    fn decode_origin_igp() {
        // flags=0x40 (transitive), type=1, len=1, value=0 (IGP)
        let buf = [0x40, 0x01, 0x01, 0x00];
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(attrs.len(), 1);
        assert_eq!(attrs[0], PathAttribute::Origin(Origin::Igp));
    }

    #[test]
    fn decode_origin_egp() {
        let buf = [0x40, 0x01, 0x01, 0x01];
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(attrs[0], PathAttribute::Origin(Origin::Egp));
    }

    #[test]
    fn decode_origin_invalid_value() {
        // ORIGIN with value 5 — not a valid Origin (only 0-2 are defined)
        let buf = [0x40, 0x01, 0x01, 0x05];
        let err = decode_path_attributes(&buf, true, &[]).unwrap_err();
        match &err {
            DecodeError::UpdateAttributeError { subcode, .. } => {
                assert_eq!(*subcode, update_subcode::INVALID_ORIGIN);
            }
            other => panic!("expected UpdateAttributeError, got: {other:?}"),
        }
    }

    #[test]
    fn decode_next_hop() {
        // flags=0x40, type=3, len=4, value=10.0.0.1
        let buf = [0x40, 0x03, 0x04, 10, 0, 0, 1];
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(attrs[0], PathAttribute::NextHop(Ipv4Addr::new(10, 0, 0, 1)));
    }

    #[test]
    fn decode_med() {
        // flags=0x80 (optional), type=4, len=4, value=100
        let buf = [0x80, 0x04, 0x04, 0, 0, 0, 100];
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(attrs[0], PathAttribute::Med(100));
    }

    #[test]
    fn decode_local_pref() {
        // flags=0x40, type=5, len=4, value=200
        let buf = [0x40, 0x05, 0x04, 0, 0, 0, 200];
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(attrs[0], PathAttribute::LocalPref(200));
    }

    #[test]
    fn decode_as_path_4byte() {
        // flags=0x40, type=2, len=10
        // segment: type=2 (AS_SEQUENCE), count=2, ASNs: 65001, 65002 (4 bytes each)
        let buf = [
            0x40, 0x02, 0x0A, // header
            0x02, 0x02, // AS_SEQUENCE, 2 ASNs
            0x00, 0x00, 0xFD, 0xE9, // 65001
            0x00, 0x00, 0xFD, 0xEA, // 65002
        ];
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(
            attrs[0],
            PathAttribute::AsPath(AsPath {
                segments: vec![AsPathSegment::AsSequence(vec![65001, 65002])]
            })
        );
    }

    #[test]
    fn decode_as_path_2byte() {
        // flags=0x40, type=2, len=6
        // segment: type=2 (AS_SEQUENCE), count=2, ASNs: 100, 200 (2 bytes each)
        let buf = [
            0x40, 0x02, 0x06, // header
            0x02, 0x02, // AS_SEQUENCE, 2 ASNs
            0x00, 0x64, // 100
            0x00, 0xC8, // 200
        ];
        let attrs = decode_path_attributes(&buf, false, &[]).unwrap();
        assert_eq!(
            attrs[0],
            PathAttribute::AsPath(AsPath {
                segments: vec![AsPathSegment::AsSequence(vec![100, 200])]
            })
        );
    }

    #[test]
    fn decode_unknown_attribute_preserved() {
        // flags=0xC0 (optional+transitive), type=99, len=3, data=[1,2,3]
        let buf = [0xC0, 99, 0x03, 1, 2, 3];
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(
            attrs[0],
            PathAttribute::Unknown(RawAttribute {
                flags: 0xC0,
                type_code: 99,
                data: Bytes::from_static(&[1, 2, 3]),
            })
        );
    }

    #[test]
    fn decode_atomic_aggregate_as_unknown() {
        // ATOMIC_AGGREGATE: flags=0x40, type=6, len=0
        let buf = [0x40, 0x06, 0x00];
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert!(matches!(attrs[0], PathAttribute::Unknown(_)));
    }

    #[test]
    fn decode_extended_length() {
        // flags=0x50 (transitive+extended), type=2, len=0x000A (10)
        // Same AS_PATH as the 4-byte test
        let buf = [
            0x50, 0x02, 0x00, 0x0A, // header with extended length
            0x02, 0x02, // AS_SEQUENCE, 2 ASNs
            0x00, 0x00, 0xFD, 0xE9, // 65001
            0x00, 0x00, 0xFD, 0xEA, // 65002
        ];
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(
            attrs[0],
            PathAttribute::AsPath(AsPath {
                segments: vec![AsPathSegment::AsSequence(vec![65001, 65002])]
            })
        );
    }

    #[test]
    fn decode_multiple_attributes() {
        let mut buf = Vec::new();
        // ORIGIN IGP
        buf.extend_from_slice(&[0x40, 0x01, 0x01, 0x00]);
        // NEXT_HOP 10.0.0.1
        buf.extend_from_slice(&[0x40, 0x03, 0x04, 10, 0, 0, 1]);
        // AS_PATH empty
        buf.extend_from_slice(&[0x40, 0x02, 0x00]);

        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(attrs.len(), 3);
        assert_eq!(attrs[0], PathAttribute::Origin(Origin::Igp));
        assert_eq!(attrs[1], PathAttribute::NextHop(Ipv4Addr::new(10, 0, 0, 1)));
        assert_eq!(attrs[2], PathAttribute::AsPath(AsPath { segments: vec![] }));
    }

    #[test]
    fn roundtrip_attributes_4byte() {
        let attrs = vec![
            PathAttribute::Origin(Origin::Igp),
            PathAttribute::AsPath(AsPath {
                segments: vec![AsPathSegment::AsSequence(vec![65001, 65002])],
            }),
            PathAttribute::NextHop(Ipv4Addr::new(10, 0, 0, 1)),
            PathAttribute::Med(100),
            PathAttribute::LocalPref(200),
        ];

        let mut buf = Vec::new();
        encode_path_attributes(&attrs, &mut buf, true, false);
        let decoded = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(decoded, attrs);
    }

    #[test]
    fn roundtrip_attributes_2byte() {
        let attrs = vec![
            PathAttribute::Origin(Origin::Egp),
            PathAttribute::AsPath(AsPath {
                segments: vec![AsPathSegment::AsSequence(vec![100, 200])],
            }),
            PathAttribute::NextHop(Ipv4Addr::new(172, 16, 0, 1)),
        ];

        let mut buf = Vec::new();
        encode_path_attributes(&attrs, &mut buf, false, false);
        let decoded = decode_path_attributes(&buf, false, &[]).unwrap();
        assert_eq!(decoded, attrs);
    }

    #[test]
    fn reject_truncated_attribute_header() {
        let buf = [0x40]; // only 1 byte
        assert!(decode_path_attributes(&buf, true, &[]).is_err());
    }

    #[test]
    fn reject_truncated_attribute_value() {
        // ORIGIN claims 1 byte value but nothing follows
        let buf = [0x40, 0x01, 0x01];
        assert!(decode_path_attributes(&buf, true, &[]).is_err());
    }

    #[test]
    fn reject_bad_origin_length() {
        // ORIGIN with 2-byte value
        let buf = [0x40, 0x01, 0x02, 0x00, 0x00];
        assert!(decode_path_attributes(&buf, true, &[]).is_err());
    }

    #[test]
    fn as_path_with_set_and_sequence() {
        // AS_SEQUENCE [65001], AS_SET [65002, 65003]
        let attrs = vec![PathAttribute::AsPath(AsPath {
            segments: vec![
                AsPathSegment::AsSequence(vec![65001]),
                AsPathSegment::AsSet(vec![65002, 65003]),
            ],
        })];

        let mut buf = Vec::new();
        encode_path_attributes(&attrs, &mut buf, true, false);
        let decoded = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(decoded, attrs);
    }

    #[test]
    fn decode_communities_single() {
        // flags=0xC0 (optional+transitive), type=8, len=4, community=65001:100
        // 65001 = 0xFDE9, 100 = 0x0064 → u32 = 0xFDE90064
        let community: u32 = (65001 << 16) | 0x0064;
        let bytes = community.to_be_bytes();
        let buf = [0xC0, 0x08, 0x04, bytes[0], bytes[1], bytes[2], bytes[3]];
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(attrs.len(), 1);
        assert_eq!(attrs[0], PathAttribute::Communities(vec![community]));
    }

    #[test]
    fn decode_communities_multiple() {
        let c1: u32 = (65001 << 16) | 0x0064;
        let c2: u32 = (65002 << 16) | 0x00C8;
        let b1 = c1.to_be_bytes();
        let b2 = c2.to_be_bytes();
        let buf = [
            0xC0, 0x08, 0x08, b1[0], b1[1], b1[2], b1[3], b2[0], b2[1], b2[2], b2[3],
        ];
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(attrs[0], PathAttribute::Communities(vec![c1, c2]));
    }

    #[test]
    fn decode_communities_empty() {
        // flags=0xC0, type=8, len=0
        let buf = [0xC0, 0x08, 0x00];
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(attrs[0], PathAttribute::Communities(vec![]));
    }

    #[test]
    fn decode_communities_odd_length_rejected() {
        // flags=0xC0, type=8, len=3, only 3 bytes (not multiple of 4)
        let buf = [0xC0, 0x08, 0x03, 0x01, 0x02, 0x03];
        assert!(decode_path_attributes(&buf, true, &[]).is_err());
    }

    #[test]
    fn communities_roundtrip() {
        let c1: u32 = (65001 << 16) | 0x0064;
        let c2: u32 = (65002 << 16) | 0x00C8;
        let attrs = vec![PathAttribute::Communities(vec![c1, c2])];

        let mut buf = Vec::new();
        encode_path_attributes(&attrs, &mut buf, true, false);
        let decoded = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(decoded, attrs);
    }

    #[test]
    fn communities_type_code_and_flags() {
        let attr = PathAttribute::Communities(vec![]);
        assert_eq!(attr.type_code(), 8);
        assert_eq!(attr.flags(), attr_flags::OPTIONAL | attr_flags::TRANSITIVE);
    }

    // --- Extended Communities (RFC 4360) tests ---

    #[test]
    fn decode_extended_communities_single() {
        // Route Target 65001:100 — type 0x00, subtype 0x02, AS 65001 (2-octet), value 100
        let ec = ExtendedCommunity::new(0x0002_FDE9_0000_0064);
        let bytes = ec.as_u64().to_be_bytes();
        let buf = [
            0xC0, 0x10, 0x08, bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6],
            bytes[7],
        ];
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(attrs.len(), 1);
        assert_eq!(attrs[0], PathAttribute::ExtendedCommunities(vec![ec]));
    }

    #[test]
    fn decode_extended_communities_multiple() {
        let ec1 = ExtendedCommunity::new(0x0002_FDE9_0000_0064);
        let ec2 = ExtendedCommunity::new(0x0003_FDEA_0000_00C8);
        let b1 = ec1.as_u64().to_be_bytes();
        let b2 = ec2.as_u64().to_be_bytes();
        let mut buf = vec![0xC0, 0x10, 16]; // flags, type=16, len=16
        buf.extend_from_slice(&b1);
        buf.extend_from_slice(&b2);
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(attrs[0], PathAttribute::ExtendedCommunities(vec![ec1, ec2]));
    }

    #[test]
    fn decode_extended_communities_empty() {
        let buf = [0xC0, 0x10, 0x00];
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(attrs[0], PathAttribute::ExtendedCommunities(vec![]));
    }

    #[test]
    fn decode_extended_communities_bad_length() {
        // length 5 is not a multiple of 8
        let buf = [0xC0, 0x10, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05];
        assert!(decode_path_attributes(&buf, true, &[]).is_err());
    }

    #[test]
    fn extended_communities_roundtrip() {
        let ec1 = ExtendedCommunity::new(0x0002_FDE9_0000_0064);
        let ec2 = ExtendedCommunity::new(0x0003_FDEA_0000_00C8);
        let attrs = vec![PathAttribute::ExtendedCommunities(vec![ec1, ec2])];

        let mut buf = Vec::new();
        encode_path_attributes(&attrs, &mut buf, true, false);
        let decoded = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(decoded, attrs);
    }

    #[test]
    fn extended_communities_type_code_and_flags() {
        let attr = PathAttribute::ExtendedCommunities(vec![]);
        assert_eq!(attr.type_code(), 16);
        assert_eq!(attr.flags(), attr_flags::OPTIONAL | attr_flags::TRANSITIVE);
    }

    #[test]
    fn extended_community_type_subtype() {
        // Type 0x00, Sub-type 0x02 (Route Target, 2-octet AS)
        let ec = ExtendedCommunity::new(0x0002_FDE9_0000_0064);
        assert_eq!(ec.type_byte(), 0x00);
        assert_eq!(ec.subtype(), 0x02);
        assert!(ec.is_transitive());
    }

    #[test]
    fn extended_community_route_target() {
        // 2-octet AS RT: type=0x00, subtype=0x02, AS=65001, value=100
        let ec = ExtendedCommunity::new(0x0002_FDE9_0000_0064);
        assert_eq!(ec.route_target(), Some((65001, 100)));
        assert_eq!(ec.route_origin(), None);

        // 4-octet AS RT: type=0x02, subtype=0x02, AS=65537, value=200
        let ec4 = ExtendedCommunity::new(0x0202_0001_0001_00C8);
        assert_eq!(ec4.route_target(), Some((65537, 200)));

        // IPv4-specific RT: type=0x01, subtype=0x02, IP=192.0.2.1, value=100
        // 192.0.2.1 = 0xC0000201
        let ec_ipv4 = ExtendedCommunity::new(0x0102_C000_0201_0064);
        let (g, l) = ec_ipv4.route_target().unwrap();
        assert_eq!(g, 0xC000_0201); // 192.0.2.1 as u32
        assert_eq!(l, 100);
        // Callers distinguish via type_byte()
        assert_eq!(ec_ipv4.type_byte() & 0x3F, 0x01);
    }

    #[test]
    fn extended_community_is_transitive() {
        // Type 0x00 → transitive (bit 6 = 0)
        let t = ExtendedCommunity::new(0x0002_0000_0000_0000);
        assert!(t.is_transitive());

        // Type 0x40 → non-transitive (bit 6 = 1)
        let nt = ExtendedCommunity::new(0x4002_0000_0000_0000);
        assert!(!nt.is_transitive());
    }

    #[test]
    fn extended_community_display() {
        let rt = ExtendedCommunity::new(0x0002_FDE9_0000_0064);
        assert_eq!(rt.to_string(), "RT:65001:100");

        let ro = ExtendedCommunity::new(0x0003_FDE9_0000_0064);
        assert_eq!(ro.to_string(), "RO:65001:100");

        // IPv4-specific RT: type=0x01, subtype=0x02, IP=192.0.2.1, value=100
        let target_v4 = ExtendedCommunity::new(0x0102_C000_0201_0064);
        assert_eq!(target_v4.to_string(), "RT:192.0.2.1:100");

        // IPv4-specific RO
        let origin_v4 = ExtendedCommunity::new(0x0103_C000_0201_0064);
        assert_eq!(origin_v4.to_string(), "RO:192.0.2.1:100");

        // 4-octet AS RT
        let rt_as4 = ExtendedCommunity::new(0x0202_0001_0001_00C8);
        assert_eq!(rt_as4.to_string(), "RT:65537:200");

        // Non-transitive opaque → hex fallback
        let opaque = ExtendedCommunity::new(0x4300_1234_5678_9ABC);
        assert_eq!(opaque.to_string(), "0x4300123456789abc");
    }

    #[test]
    fn unknown_attribute_roundtrip() {
        // Input has flags 0xC0 (optional+transitive). After encoding, the
        // Partial bit is OR'd in for transitive unknowns → 0xE0.
        let attrs = vec![PathAttribute::Unknown(RawAttribute {
            flags: 0xC0,
            type_code: 99,
            data: Bytes::from_static(&[1, 2, 3, 4, 5]),
        })];

        let mut buf = Vec::new();
        encode_path_attributes(&attrs, &mut buf, true, false);
        let decoded = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(
            decoded,
            vec![PathAttribute::Unknown(RawAttribute {
                flags: 0xE0, // Partial bit set on re-advertisement
                type_code: 99,
                data: Bytes::from_static(&[1, 2, 3, 4, 5]),
            })]
        );
    }

    #[test]
    fn origin_with_optional_flag_rejected() {
        // ORIGIN with flags 0xC0 (Optional+Transitive) — should be 0x40 (Transitive only)
        let buf = [0xC0, 0x01, 0x01, 0x00];
        let err = decode_path_attributes(&buf, true, &[]).unwrap_err();
        match &err {
            DecodeError::UpdateAttributeError { subcode, .. } => {
                assert_eq!(*subcode, update_subcode::ATTRIBUTE_FLAGS_ERROR);
            }
            other => panic!("expected UpdateAttributeError, got: {other:?}"),
        }
    }

    #[test]
    fn med_with_transitive_flag_rejected() {
        // MED with flags 0xC0 (Optional+Transitive) — should be 0x80 (Optional only)
        let buf = [0xC0, 0x04, 0x04, 0, 0, 0, 100];
        let err = decode_path_attributes(&buf, true, &[]).unwrap_err();
        match &err {
            DecodeError::UpdateAttributeError { subcode, .. } => {
                assert_eq!(*subcode, update_subcode::ATTRIBUTE_FLAGS_ERROR);
            }
            other => panic!("expected UpdateAttributeError, got: {other:?}"),
        }
    }

    #[test]
    fn communities_without_optional_rejected() {
        // COMMUNITIES with flags 0x40 (Transitive only) — should be 0xC0 (Optional+Transitive)
        let buf = [0x40, 0x08, 0x04, 0, 0, 0, 100];
        let err = decode_path_attributes(&buf, true, &[]).unwrap_err();
        match &err {
            DecodeError::UpdateAttributeError { subcode, .. } => {
                assert_eq!(*subcode, update_subcode::ATTRIBUTE_FLAGS_ERROR);
            }
            other => panic!("expected UpdateAttributeError, got: {other:?}"),
        }
    }

    #[test]
    fn next_hop_length_error_subcode() {
        // NEXT_HOP with 3 bytes instead of 4
        let buf = [0x40, 0x03, 0x03, 10, 0, 0];
        let err = decode_path_attributes(&buf, true, &[]).unwrap_err();
        match &err {
            DecodeError::UpdateAttributeError { subcode, .. } => {
                assert_eq!(*subcode, update_subcode::ATTRIBUTE_LENGTH_ERROR);
            }
            other => panic!("expected UpdateAttributeError, got: {other:?}"),
        }
    }

    #[test]
    fn invalid_origin_value_subcode() {
        // ORIGIN with value 5 → subcode 6 (INVALID_ORIGIN)
        let buf = [0x40, 0x01, 0x01, 0x05];
        let err = decode_path_attributes(&buf, true, &[]).unwrap_err();
        match &err {
            DecodeError::UpdateAttributeError { subcode, .. } => {
                assert_eq!(*subcode, update_subcode::INVALID_ORIGIN);
            }
            other => panic!("expected UpdateAttributeError, got: {other:?}"),
        }
    }

    #[test]
    fn as_path_bad_segment_subcode() {
        // AS_PATH with unknown segment type 5
        let buf = [
            0x40, 0x02, 0x06, // AS_PATH header, length 6
            0x05, 0x01, // unknown segment type 5, count 1
            0x00, 0x00, 0xFD, 0xE9, // ASN 65001
        ];
        let err = decode_path_attributes(&buf, true, &[]).unwrap_err();
        match &err {
            DecodeError::UpdateAttributeError { subcode, .. } => {
                assert_eq!(*subcode, update_subcode::MALFORMED_AS_PATH);
            }
            other => panic!("expected UpdateAttributeError, got: {other:?}"),
        }
    }

    #[test]
    fn encode_unknown_transitive_sets_partial() {
        let attr = PathAttribute::Unknown(RawAttribute {
            flags: attr_flags::OPTIONAL | attr_flags::TRANSITIVE, // 0xC0
            type_code: 99,
            data: Bytes::from_static(&[1, 2]),
        });
        let mut buf = Vec::new();
        encode_path_attributes(&[attr], &mut buf, true, false);
        // First byte is flags — should have PARTIAL bit set
        assert_eq!(
            buf[0],
            attr_flags::OPTIONAL | attr_flags::TRANSITIVE | attr_flags::PARTIAL
        );
    }

    #[test]
    fn encode_unknown_wellknown_transitive_no_partial() {
        // Well-known transitive (OPTIONAL=0, TRANSITIVE=1) should NOT get PARTIAL
        let attr = PathAttribute::Unknown(RawAttribute {
            flags: attr_flags::TRANSITIVE, // 0x40, well-known transitive
            type_code: 99,
            data: Bytes::from_static(&[1, 2]),
        });
        let mut buf = Vec::new();
        encode_path_attributes(&[attr], &mut buf, true, false);
        assert_eq!(buf[0], attr_flags::TRANSITIVE);
    }

    #[test]
    fn encode_unknown_nontransitive_no_partial() {
        let attr = PathAttribute::Unknown(RawAttribute {
            flags: attr_flags::OPTIONAL, // 0x80, no Transitive
            type_code: 99,
            data: Bytes::from_static(&[1, 2]),
        });
        let mut buf = Vec::new();
        encode_path_attributes(&[attr], &mut buf, true, false);
        // First byte is flags — should NOT have PARTIAL bit
        assert_eq!(buf[0], attr_flags::OPTIONAL);
    }

    // --- MP_REACH_NLRI / MP_UNREACH_NLRI tests ---

    /// Helper to create a `NlriEntry` with `path_id=0`.
    fn nlri(prefix: Prefix) -> NlriEntry {
        NlriEntry { path_id: 0, prefix }
    }

    #[test]
    fn mp_reach_nlri_ipv6_roundtrip() {
        use crate::capability::{Afi, Safi};
        use crate::nlri::{Ipv6Prefix, Prefix};

        let mp = MpReachNlri {
            afi: Afi::Ipv6,
            safi: Safi::Unicast,
            next_hop: IpAddr::V6("2001:db8::1".parse().unwrap()),
            announced: vec![
                nlri(Prefix::V6(Ipv6Prefix::new(
                    "2001:db8:1::".parse().unwrap(),
                    48,
                ))),
                nlri(Prefix::V6(Ipv6Prefix::new(
                    "2001:db8:2::".parse().unwrap(),
                    48,
                ))),
            ],
            flowspec_announced: vec![],
            evpn_announced: vec![],
        };
        let attrs = vec![PathAttribute::MpReachNlri(mp.clone())];

        let mut buf = Vec::new();
        encode_path_attributes(&attrs, &mut buf, true, false);
        let decoded = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(decoded.len(), 1);
        assert_eq!(decoded[0], PathAttribute::MpReachNlri(mp));
    }

    #[test]
    fn mp_unreach_nlri_ipv6_roundtrip() {
        use crate::capability::{Afi, Safi};
        use crate::nlri::{Ipv6Prefix, Prefix};

        let mp = MpUnreachNlri {
            afi: Afi::Ipv6,
            safi: Safi::Unicast,
            withdrawn: vec![nlri(Prefix::V6(Ipv6Prefix::new(
                "2001:db8:1::".parse().unwrap(),
                48,
            )))],
            flowspec_withdrawn: vec![],
            evpn_withdrawn: vec![],
        };
        let attrs = vec![PathAttribute::MpUnreachNlri(mp.clone())];

        let mut buf = Vec::new();
        encode_path_attributes(&attrs, &mut buf, true, false);
        let decoded = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(decoded.len(), 1);
        assert_eq!(decoded[0], PathAttribute::MpUnreachNlri(mp));
    }

    #[test]
    fn mp_reach_nlri_ipv4_roundtrip() {
        use crate::capability::{Afi, Safi};
        use crate::nlri::Prefix;

        let mp = MpReachNlri {
            afi: Afi::Ipv4,
            safi: Safi::Unicast,
            next_hop: IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1)),
            announced: vec![nlri(Prefix::V4(crate::nlri::Ipv4Prefix::new(
                Ipv4Addr::new(10, 1, 0, 0),
                16,
            )))],
            flowspec_announced: vec![],
            evpn_announced: vec![],
        };
        let attrs = vec![PathAttribute::MpReachNlri(mp.clone())];

        let mut buf = Vec::new();
        encode_path_attributes(&attrs, &mut buf, true, false);
        let decoded = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(decoded[0], PathAttribute::MpReachNlri(mp));
    }

    #[test]
    fn mp_reach_nlri_ipv4_with_ipv6_nexthop_roundtrip() {
        use crate::capability::{Afi, Safi};
        use crate::nlri::Prefix;

        let mp = MpReachNlri {
            afi: Afi::Ipv4,
            safi: Safi::Unicast,
            next_hop: IpAddr::V6("2001:db8::1".parse().unwrap()),
            announced: vec![nlri(Prefix::V4(crate::nlri::Ipv4Prefix::new(
                Ipv4Addr::new(10, 1, 0, 0),
                16,
            )))],
            flowspec_announced: vec![],
            evpn_announced: vec![],
        };
        let attrs = vec![PathAttribute::MpReachNlri(mp.clone())];

        let mut buf = Vec::new();
        encode_path_attributes(&attrs, &mut buf, true, false);
        let decoded = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(decoded[0], PathAttribute::MpReachNlri(mp));
    }

    #[test]
    fn mp_reach_nlri_type_code_and_flags() {
        use crate::capability::{Afi, Safi};

        let attr = PathAttribute::MpReachNlri(MpReachNlri {
            afi: Afi::Ipv6,
            safi: Safi::Unicast,
            next_hop: IpAddr::V6(Ipv6Addr::UNSPECIFIED),
            announced: vec![],
            flowspec_announced: vec![],
            evpn_announced: vec![],
        });
        assert_eq!(attr.type_code(), 14);
        // RFC 4760 §3: MP_REACH_NLRI is optional non-transitive
        assert_eq!(attr.flags(), attr_flags::OPTIONAL);
    }

    #[test]
    fn mp_unreach_nlri_type_code_and_flags() {
        use crate::capability::{Afi, Safi};

        let attr = PathAttribute::MpUnreachNlri(MpUnreachNlri {
            afi: Afi::Ipv6,
            safi: Safi::Unicast,
            withdrawn: vec![],
            flowspec_withdrawn: vec![],
            evpn_withdrawn: vec![],
        });
        assert_eq!(attr.type_code(), 15);
        assert_eq!(attr.flags(), attr_flags::OPTIONAL);
    }

    #[test]
    fn mp_reach_nlri_empty_nlri() {
        use crate::capability::{Afi, Safi};

        let mp = MpReachNlri {
            afi: Afi::Ipv6,
            safi: Safi::Unicast,
            next_hop: IpAddr::V6("fe80::1".parse().unwrap()),
            announced: vec![],
            flowspec_announced: vec![],
            evpn_announced: vec![],
        };
        let attrs = vec![PathAttribute::MpReachNlri(mp.clone())];

        let mut buf = Vec::new();
        encode_path_attributes(&attrs, &mut buf, true, false);
        let decoded = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(decoded[0], PathAttribute::MpReachNlri(mp));
    }

    #[test]
    fn mp_reach_nlri_bad_flags_rejected() {
        // MP_REACH_NLRI (type 14) with flags 0x40 (Transitive only)
        // — should be 0xC0 (Optional+Transitive)
        // Build minimal valid value: AFI=2, SAFI=1, NH-Len=16, NH=::1, Reserved=0
        let mut value = Vec::new();
        value.extend_from_slice(&2u16.to_be_bytes()); // AFI IPv6
        value.push(1); // SAFI Unicast
        value.push(16); // NH-Len
        value.extend_from_slice(&"::1".parse::<Ipv6Addr>().unwrap().octets()); // NH
        value.push(0); // Reserved

        let mut buf = Vec::new();
        buf.push(0x40); // flags: Transitive only (wrong)
        buf.push(14); // type: MP_REACH_NLRI
        #[expect(clippy::cast_possible_truncation)]
        buf.push(value.len() as u8);
        buf.extend_from_slice(&value);

        let err = decode_path_attributes(&buf, true, &[]).unwrap_err();
        assert!(matches!(
            err,
            DecodeError::UpdateAttributeError {
                subcode: 4, // ATTRIBUTE_FLAGS_ERROR
                ..
            }
        ));
    }

    // --- MP Add-Path decode tests ---

    #[test]
    #[expect(clippy::cast_possible_truncation)]
    fn mp_reach_nlri_ipv4_addpath_decode() {
        use crate::capability::{Afi, Safi};
        use crate::nlri::Prefix;

        // Build MP_REACH_NLRI with Add-Path-encoded IPv4 NLRI:
        // path_id(4) + prefix_len(1) + prefix_bytes
        let mut value = Vec::new();
        value.extend_from_slice(&1u16.to_be_bytes()); // AFI IPv4
        value.push(1); // SAFI Unicast
        value.push(4); // NH-Len
        value.extend_from_slice(&[10, 0, 0, 1]); // Next Hop
        value.push(0); // Reserved
        // Add-Path NLRI: path_id=42, 10.1.0.0/16
        value.extend_from_slice(&42u32.to_be_bytes());
        value.push(16);
        value.extend_from_slice(&[10, 1]);

        let mut buf = Vec::new();
        buf.push(0x90); // flags: Optional + Extended Length
        buf.push(14); // type: MP_REACH_NLRI
        buf.extend_from_slice(&(value.len() as u16).to_be_bytes());
        buf.extend_from_slice(&value);

        // With Add-Path for IPv4 unicast → decode path_id
        let decoded = decode_path_attributes(&buf, true, &[(Afi::Ipv4, Safi::Unicast)]).unwrap();
        let PathAttribute::MpReachNlri(mp) = &decoded[0] else {
            panic!("expected MpReachNlri");
        };
        assert_eq!(mp.announced.len(), 1);
        assert_eq!(mp.announced[0].path_id, 42);
        assert!(matches!(mp.announced[0].prefix, Prefix::V4(p) if p.len == 16));

        // Without Add-Path → plain decoder misinterprets the path_id bytes
        // as prefix encoding and rejects the garbled data.
        assert!(decode_path_attributes(&buf, true, &[]).is_err());
    }

    #[test]
    #[expect(clippy::cast_possible_truncation)]
    fn mp_reach_nlri_ipv6_addpath_decode() {
        use crate::capability::{Afi, Safi};
        use crate::nlri::{Ipv6Prefix, Prefix};

        // Build MP_REACH_NLRI with Add-Path-encoded IPv6 NLRI
        let mut value = Vec::new();
        value.extend_from_slice(&2u16.to_be_bytes()); // AFI IPv6
        value.push(1); // SAFI Unicast
        value.push(16); // NH-Len
        value.extend_from_slice(&"2001:db8::1".parse::<Ipv6Addr>().unwrap().octets());
        value.push(0); // Reserved
        // Add-Path NLRI: path_id=99, 2001:db8:1::/48
        value.extend_from_slice(&99u32.to_be_bytes());
        value.push(48);
        value.extend_from_slice(&[0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01]);

        let mut buf = Vec::new();
        buf.push(0x90); // flags: Optional + Extended Length
        buf.push(14); // type: MP_REACH_NLRI
        buf.extend_from_slice(&(value.len() as u16).to_be_bytes());
        buf.extend_from_slice(&value);

        let decoded = decode_path_attributes(&buf, true, &[(Afi::Ipv6, Safi::Unicast)]).unwrap();
        let PathAttribute::MpReachNlri(mp) = &decoded[0] else {
            panic!("expected MpReachNlri");
        };
        assert_eq!(mp.announced.len(), 1);
        assert_eq!(mp.announced[0].path_id, 99);
        assert_eq!(
            mp.announced[0].prefix,
            Prefix::V6(Ipv6Prefix::new("2001:db8:1::".parse().unwrap(), 48))
        );
    }

    #[test]
    #[expect(clippy::cast_possible_truncation)]
    fn mp_unreach_nlri_ipv6_addpath_decode() {
        use crate::capability::{Afi, Safi};
        use crate::nlri::{Ipv6Prefix, Prefix};

        // Build MP_UNREACH_NLRI with Add-Path-encoded IPv6 NLRI
        let mut value = Vec::new();
        value.extend_from_slice(&2u16.to_be_bytes()); // AFI IPv6
        value.push(1); // SAFI Unicast
        // Add-Path NLRI: path_id=7, 2001:db8:2::/48
        value.extend_from_slice(&7u32.to_be_bytes());
        value.push(48);
        value.extend_from_slice(&[0x20, 0x01, 0x0d, 0xb8, 0x00, 0x02]);

        let mut buf = Vec::new();
        buf.push(0x90); // flags: Optional + Extended Length
        buf.push(15); // type: MP_UNREACH_NLRI
        buf.extend_from_slice(&(value.len() as u16).to_be_bytes());
        buf.extend_from_slice(&value);

        let decoded = decode_path_attributes(&buf, true, &[(Afi::Ipv6, Safi::Unicast)]).unwrap();
        let PathAttribute::MpUnreachNlri(mp) = &decoded[0] else {
            panic!("expected MpUnreachNlri");
        };
        assert_eq!(mp.withdrawn.len(), 1);
        assert_eq!(mp.withdrawn[0].path_id, 7);
        assert_eq!(
            mp.withdrawn[0].prefix,
            Prefix::V6(Ipv6Prefix::new("2001:db8:2::".parse().unwrap(), 48))
        );
    }

    #[test]
    fn mp_reach_addpath_only_applies_to_matching_family() {
        use crate::capability::{Afi, Safi};
        use crate::nlri::{Ipv6Prefix, Prefix};

        // Build plain (non-Add-Path) MP_REACH_NLRI for IPv6
        let mp = MpReachNlri {
            afi: Afi::Ipv6,
            safi: Safi::Unicast,
            next_hop: IpAddr::V6("2001:db8::1".parse().unwrap()),
            announced: vec![NlriEntry {
                path_id: 0,
                prefix: Prefix::V6(Ipv6Prefix::new("2001:db8:1::".parse().unwrap(), 48)),
            }],
            flowspec_announced: vec![],
            evpn_announced: vec![],
        };
        let attrs = vec![PathAttribute::MpReachNlri(mp.clone())];

        let mut buf = Vec::new();
        encode_path_attributes(&attrs, &mut buf, true, false);

        // Add-Path enabled for IPv4 only — IPv6 should still decode as plain
        let decoded = decode_path_attributes(&buf, true, &[(Afi::Ipv4, Safi::Unicast)]).unwrap();
        assert_eq!(decoded[0], PathAttribute::MpReachNlri(mp));
    }

    // --- ORIGINATOR_ID tests ---

    #[test]
    fn decode_originator_id() {
        // flags=0x80 (optional), type=9, len=4, value=1.2.3.4
        let buf = [0x80, 0x09, 0x04, 1, 2, 3, 4];
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(
            attrs[0],
            PathAttribute::OriginatorId(Ipv4Addr::new(1, 2, 3, 4))
        );
    }

    #[test]
    fn originator_id_roundtrip() {
        let attr = PathAttribute::OriginatorId(Ipv4Addr::new(10, 0, 0, 1));
        let mut buf = Vec::new();
        encode_path_attributes(std::slice::from_ref(&attr), &mut buf, true, false);
        let decoded = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(decoded, vec![attr]);
    }

    #[test]
    fn originator_id_wrong_length() {
        // 3 bytes instead of 4
        let buf = [0x80, 0x09, 0x03, 1, 2, 3];
        let err = decode_path_attributes(&buf, true, &[]).unwrap_err();
        assert!(matches!(
            err,
            DecodeError::UpdateAttributeError {
                subcode: 5, // ATTRIBUTE_LENGTH_ERROR
                ..
            }
        ));
    }

    #[test]
    fn originator_id_wrong_flags() {
        // flags=0x40 (transitive) — should be 0x80 (optional)
        let buf = [0x40, 0x09, 0x04, 1, 2, 3, 4];
        let err = decode_path_attributes(&buf, true, &[]).unwrap_err();
        assert!(matches!(
            err,
            DecodeError::UpdateAttributeError {
                subcode: 4, // ATTRIBUTE_FLAGS_ERROR
                ..
            }
        ));
    }

    // --- CLUSTER_LIST tests ---

    #[test]
    fn decode_cluster_list() {
        // flags=0x80 (optional), type=10, len=8, two cluster IDs
        let buf = [0x80, 0x0A, 0x08, 1, 2, 3, 4, 5, 6, 7, 8];
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(
            attrs[0],
            PathAttribute::ClusterList(vec![Ipv4Addr::new(1, 2, 3, 4), Ipv4Addr::new(5, 6, 7, 8),])
        );
    }

    #[test]
    fn cluster_list_roundtrip() {
        let attr = PathAttribute::ClusterList(vec![
            Ipv4Addr::new(10, 0, 0, 1),
            Ipv4Addr::new(10, 0, 0, 2),
        ]);
        let mut buf = Vec::new();
        encode_path_attributes(std::slice::from_ref(&attr), &mut buf, true, false);
        let decoded = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(decoded, vec![attr]);
    }

    #[test]
    fn cluster_list_wrong_length() {
        // 5 bytes — not a multiple of 4
        let buf = [0x80, 0x0A, 0x05, 1, 2, 3, 4, 5];
        let err = decode_path_attributes(&buf, true, &[]).unwrap_err();
        assert!(matches!(
            err,
            DecodeError::UpdateAttributeError {
                subcode: 5, // ATTRIBUTE_LENGTH_ERROR
                ..
            }
        ));
    }

    // -----------------------------------------------------------------------
    // Large Communities (RFC 8092)
    // -----------------------------------------------------------------------

    #[test]
    fn large_community_display() {
        let lc = LargeCommunity::new(65001, 100, 200);
        assert_eq!(lc.to_string(), "65001:100:200");
    }

    #[test]
    fn large_community_type_code_and_flags() {
        let attr = PathAttribute::LargeCommunities(vec![LargeCommunity::new(1, 2, 3)]);
        assert_eq!(attr.type_code(), attr_type::LARGE_COMMUNITIES);
        assert_eq!(attr.flags(), attr_flags::OPTIONAL | attr_flags::TRANSITIVE);
    }

    #[test]
    fn decode_large_community_single() {
        // flags=0xC0 (Optional|Transitive), type=32, length=12
        let mut buf = vec![0xC0, 32, 12];
        buf.extend_from_slice(&65001u32.to_be_bytes());
        buf.extend_from_slice(&100u32.to_be_bytes());
        buf.extend_from_slice(&200u32.to_be_bytes());
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(attrs.len(), 1);
        assert_eq!(
            attrs[0],
            PathAttribute::LargeCommunities(vec![LargeCommunity::new(65001, 100, 200)])
        );
    }

    #[test]
    fn decode_large_community_multiple() {
        // Two LCs: 24 bytes total
        let mut buf = vec![0xC0, 32, 24];
        for (g, l1, l2) in [(65001u32, 100u32, 200u32), (65002, 300, 400)] {
            buf.extend_from_slice(&g.to_be_bytes());
            buf.extend_from_slice(&l1.to_be_bytes());
            buf.extend_from_slice(&l2.to_be_bytes());
        }
        let attrs = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(
            attrs[0],
            PathAttribute::LargeCommunities(vec![
                LargeCommunity::new(65001, 100, 200),
                LargeCommunity::new(65002, 300, 400),
            ])
        );
    }

    #[test]
    fn decode_large_community_bad_length() {
        // 10 bytes — not a multiple of 12
        let buf = [0xC0, 32, 10, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0];
        let err = decode_path_attributes(&buf, true, &[]).unwrap_err();
        assert!(matches!(
            err,
            DecodeError::UpdateAttributeError {
                subcode: 5, // ATTRIBUTE_LENGTH_ERROR
                ..
            }
        ));
    }

    #[test]
    fn decode_large_community_empty_rejected() {
        // Zero-length LARGE_COMMUNITIES is rejected (must carry at least one community).
        let buf = [0xC0, 32, 0];
        let err = decode_path_attributes(&buf, true, &[]).unwrap_err();
        assert!(matches!(
            err,
            DecodeError::UpdateAttributeError {
                subcode: 5, // ATTRIBUTE_LENGTH_ERROR
                ..
            }
        ));
    }

    #[test]
    fn large_community_roundtrip() {
        let lcs = vec![
            LargeCommunity::new(65001, 100, 200),
            LargeCommunity::new(0, u32::MAX, 42),
        ];
        let attr = PathAttribute::LargeCommunities(lcs.clone());
        let mut buf = Vec::new();
        encode_path_attributes(&[attr], &mut buf, true, false);
        let decoded = decode_path_attributes(&buf, true, &[]).unwrap();
        assert_eq!(decoded.len(), 1);
        assert_eq!(decoded[0], PathAttribute::LargeCommunities(lcs));
    }

    #[test]
    fn large_community_expected_flags_validated() {
        // Wrong flags: TRANSITIVE only (0x40) instead of OPTIONAL|TRANSITIVE (0xC0)
        let mut buf = vec![0x40, 32, 12];
        buf.extend_from_slice(&1u32.to_be_bytes());
        buf.extend_from_slice(&2u32.to_be_bytes());
        buf.extend_from_slice(&3u32.to_be_bytes());
        let err = decode_path_attributes(&buf, true, &[]).unwrap_err();
        assert!(matches!(
            err,
            DecodeError::UpdateAttributeError {
                subcode: 4, // ATTRIBUTE_FLAGS_ERROR
                ..
            }
        ));
    }

    // -----------------------------------------------------------------------
    // AsPath::to_aspath_string()
    // -----------------------------------------------------------------------

    #[test]
    fn aspath_string_sequence() {
        let p = AsPath {
            segments: vec![AsPathSegment::AsSequence(vec![65001, 65002, 65003])],
        };
        assert_eq!(p.to_aspath_string(), "65001 65002 65003");
    }

    #[test]
    fn aspath_string_set() {
        let p = AsPath {
            segments: vec![AsPathSegment::AsSet(vec![65003, 65004])],
        };
        assert_eq!(p.to_aspath_string(), "{65003 65004}");
    }

    #[test]
    fn aspath_string_mixed() {
        let p = AsPath {
            segments: vec![
                AsPathSegment::AsSequence(vec![65001, 65002]),
                AsPathSegment::AsSet(vec![65003, 65004]),
            ],
        };
        assert_eq!(p.to_aspath_string(), "65001 65002 {65003 65004}");
    }

    #[test]
    fn aspath_string_empty() {
        let p = AsPath { segments: vec![] };
        assert_eq!(p.to_aspath_string(), "");
    }

    /// Regression: SAFI 70 (EVPN) is only valid under AFI 25 (L2VPN).
    /// Other AFIs with SAFI=Evpn must be rejected explicitly so the
    /// unicast NLRI fallthrough never tries to parse the typed EVPN
    /// payload as a prefix list.
    #[test]
    fn mp_reach_nlri_rejects_evpn_safi_with_non_l2vpn_afi() {
        // AFI=Ipv4 (1), SAFI=Evpn (70), NH-len=4, NH=192.0.2.1, reserved=0,
        // followed by an arbitrary EVPN-shaped byte (route type 3, len 0).
        let bytes = vec![
            0x00, 0x01, // AFI = Ipv4
            70,   // SAFI = Evpn
            4, 192, 0, 2, 1, // NH len + NH
            0, // reserved
            3, 0, // EVPN-style NLRI (route type 3, length 0)
        ];
        let err = decode_mp_reach_nlri(&bytes, &[]).unwrap_err();
        match err {
            DecodeError::MalformedField { detail, .. } => {
                assert!(detail.contains("SAFI EVPN"), "unexpected detail: {detail}");
            }
            other => panic!("expected MalformedField, got {other:?}"),
        }
    }

    #[test]
    fn mp_unreach_nlri_rejects_evpn_safi_with_non_l2vpn_afi() {
        let bytes = vec![
            0x00, 0x02, // AFI = Ipv6
            70,   // SAFI = Evpn
            3, 0, // EVPN-style withdrawal (route type 3, length 0)
        ];
        let err = decode_mp_unreach_nlri(&bytes, &[]).unwrap_err();
        match err {
            DecodeError::MalformedField { detail, .. } => {
                assert!(detail.contains("SAFI EVPN"), "unexpected detail: {detail}");
            }
            other => panic!("expected MalformedField, got {other:?}"),
        }
    }
}