freenet 0.2.43

Freenet core software
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
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap};
use std::net::{IpAddr, SocketAddr};
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::AtomicU32;
use std::time::Duration;

use crate::config::{GlobalExecutor, GlobalRng, PCK_VERSION};
use crate::transport::crypto::TransportSecretKey;
use crate::transport::packet_data::UnknownEncryption;
use crate::transport::symmetric_message::OutboundConnection;
use crate::util::backoff::ExponentialBackoff;
use aes_gcm::{Aes128Gcm, KeyInit};
use dashmap::DashSet;
use futures::{
    Future, FutureExt, TryFutureExt,
    future::BoxFuture,
    stream::{FuturesUnordered, StreamExt},
};
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use tokio::{
    net::UdpSocket,
    sync::{mpsc, oneshot},
    task_local,
};
use tracing::{Instrument, span};
use version_cmp::PROTOC_VERSION;

use super::{
    Socket, TransportError,
    congestion_control::{CongestionControl, CongestionControlConfig},
    crypto::{TransportKeypair, TransportPublicKey},
    fast_channel::{self, FastSender},
    global_bandwidth::GlobalBandwidthManager,
    packet_data::{
        MAX_PACKET_SIZE, MAX_RECV_PACKET_SIZE as RECV_BUF_SIZE, PacketData, SymmetricAES,
    },
    peer_connection::{PeerConnection, RemoteConnection},
    sent_packet_tracker::SentPacketTracker,
    symmetric_message::{SymmetricMessage, SymmetricMessagePayload},
    token_bucket::TokenBucket,
};
use crate::simulation::{RealTime, TimeSource};

// Constants for interval increase
const INITIAL_INTERVAL: Duration = Duration::from_millis(50);

/// Capacity of the inbound packet channel from connection_handler to PeerConnection.
///
/// At FixedRate 10 Mbps (~892 packets/sec), 2048 packets provides ~2.3 seconds
/// of buffer headroom, up from ~1.1 seconds at capacity 1000.
const INBOUND_CHANNEL_CAPACITY: usize = 2048;

/// Minimum interval between asymmetric decryption attempts for the same address.
/// Prevents CPU exhaustion from attackers sending intro-sized packets.
/// Note: X25519 decryption is ~100x faster than RSA-2048, so this is very conservative.
const ASYM_DECRYPTION_RATE_LIMIT: Duration = Duration::from_secs(1);

/// Interval for cleaning up expired rate limit entries.
/// This prevents unbounded growth of the last_asym_attempt HashMap.
const RATE_LIMIT_CLEANUP_INTERVAL: Duration = Duration::from_secs(60);

/// Duration to keep connections in RecentlyClosed state.
/// Allows in-flight packets to drain without triggering asymmetric decryption.
const RECENTLY_CLOSED_DURATION: Duration = Duration::from_secs(2);

/// Gateway connection acceptance rate limiting after restart.
///
/// When a gateway restarts, all connected peers detect their connections are dead
/// and attempt to reconnect simultaneously ("thundering herd"). This overwhelms
/// the fast_channel buffers for inter-gateway links, triggering packet drops
/// that can cascade into a non-recovering overflow loop.
///
/// The rate limiter gradually increases the connection acceptance rate:
/// - Phase 1 (0-30s):   max 5 new connections/second
/// - Phase 2 (30s-2m):  max 20 new connections/second
/// - Phase 3 (>2m):     unlimited
const GW_RAMP_PHASE1_DURATION: Duration = Duration::from_secs(30);
const GW_RAMP_PHASE2_DURATION: Duration = Duration::from_secs(120);
const GW_RAMP_PHASE1_RATE: u64 = 5;
const GW_RAMP_PHASE2_RATE: u64 = 20;

/// Duration to keep outdated peer entries before cleanup.
/// Peers with incompatible protocol versions are ignored for this duration.
const OUTDATED_PEER_EXPIRY: Duration = Duration::from_secs(600);

/// Window used to sample the congestion controller's current rate when
/// initializing a token bucket. Must stay in sync with `MIN_BUCKET_CAPACITY_BYTES`:
/// the bucket holds exactly one window's worth of tokens at the initial rate
/// (`initial_rate / 10` == `initial_rate * TOKEN_BUCKET_WINDOW`).
const TOKEN_BUCKET_WINDOW: Duration = Duration::from_millis(100);

/// Minimum token bucket capacity in bytes (8 KiB floor).
///
/// Prevents the bucket from becoming so small that a single max-size packet
/// (~1200 bytes) cannot be admitted, stalling connections at very low rates.
/// 8 KiB (~6–7 max-size packets) provides enough headroom for burst handling
/// without allowing large uncontrolled bursts.
const MIN_BUCKET_CAPACITY_BYTES: usize = 8192;

pub type SerializedMessage = Vec<u8>;

type GatewayConnectionFuture<S, T> = BoxFuture<
    'static,
    Result<
        (
            RemoteConnection<S, T>,
            InboundRemoteConnection,
            PacketData<SymmetricAES>,
        ),
        TransportError,
    >,
>;

type TraverseNatFuture<S, T> =
    BoxFuture<'static, Result<(RemoteConnection<S, T>, InboundRemoteConnection), TransportError>>;

/// Maximum retries for socket binding in case of transient port conflicts.
/// This handles race conditions in test environments where ports are released
/// and rebound quickly.
const SOCKET_BIND_MAX_RETRIES: usize = 5;

/// Backoff configuration for socket bind retries.
/// Base delay of 50ms, max delay of 1s (exponential backoff: 50ms, 100ms, 200ms, 400ms, 800ms).
const SOCKET_BIND_BACKOFF: ExponentialBackoff =
    ExponentialBackoff::new(Duration::from_millis(50), Duration::from_secs(1));

/// Create a pair of connection handlers for managing transport connections.
///
/// This is the main entry point for establishing UDP connections with NAT traversal.
/// Returns an outbound handler (for initiating connections) and inbound handler
/// (for receiving connection notifications).
///
/// # Arguments
/// * `keypair` - X25519 keypair for this peer
/// * `listen_host` - IP address to bind to (default `::` for dual-stack on all interfaces)
/// * `listen_port` - UDP port to bind to
/// * `is_gateway` - Whether this peer acts as a gateway
/// * `bandwidth_limit` - Optional per-connection bandwidth limit in bytes/sec
/// * `global_bandwidth` - Optional shared bandwidth manager
/// * `ledbat_min_ssthresh` - Optional minimum slow-start threshold for LEDBAT
/// * `congestion_config` - Optional congestion control config (defaults to FixedRate)
#[allow(clippy::too_many_arguments)]
pub async fn create_connection_handler<S: Socket>(
    keypair: TransportKeypair,
    listen_host: IpAddr,
    listen_port: u16,
    is_gateway: bool,
    bandwidth_limit: Option<usize>,
    global_bandwidth: Option<Arc<GlobalBandwidthManager>>,
    ledbat_min_ssthresh: Option<usize>,
    congestion_config: Option<CongestionControlConfig>,
) -> Result<
    (
        OutboundConnectionHandler<S>,
        InboundConnectionHandler<S>,
        tokio::task::JoinHandle<TransportError>,
    ),
    TransportError,
> {
    // Bind the UDP socket to the specified port with retry for transient failures
    let bind_addr: SocketAddr = (listen_host, listen_port).into();
    tracing::debug!(
        target: "freenet_core::transport::send_debug",
        bind_addr = %bind_addr,
        is_gateway,
        "Binding UDP socket"
    );

    let socket = bind_socket_with_retry::<S>(bind_addr, SOCKET_BIND_MAX_RETRIES).await?;

    tracing::info!(
        target: "freenet_core::transport::send_debug",
        bind_addr = %bind_addr,
        is_gateway,
        "UDP socket bound successfully"
    );
    let (och, new_connection_notifier, listen_handle) = OutboundConnectionHandler::config_listener(
        Arc::new(socket),
        keypair,
        is_gateway,
        (listen_host, listen_port).into(),
        bandwidth_limit,
        global_bandwidth,
        ledbat_min_ssthresh,
        congestion_config,
    )?;
    Ok((
        och,
        InboundConnectionHandler {
            new_connection_notifier,
        },
        listen_handle,
    ))
}

/// Bind a socket with retry logic for transient "Address in use" errors.
///
/// In test environments with parallel tests, there can be a brief window between
/// releasing a reserved port and rebinding where another process grabs it.
/// This retry logic handles such transient conflicts gracefully.
async fn bind_socket_with_retry<S: Socket>(
    addr: SocketAddr,
    max_retries: usize,
) -> Result<S, TransportError> {
    let mut last_err = None;

    for attempt in 0..max_retries {
        match S::bind(addr).await {
            Ok(socket) => return Ok(socket),
            Err(e) if e.kind() == std::io::ErrorKind::AddrInUse && attempt + 1 < max_retries => {
                let delay = SOCKET_BIND_BACKOFF.delay(attempt as u32);
                tracing::debug!(
                    bind_addr = %addr,
                    attempt = attempt + 1,
                    max_retries,
                    delay_ms = delay.as_millis(),
                    "Socket bind failed with AddrInUse, retrying after delay"
                );
                last_err = Some(e);
                tokio::time::sleep(delay).await;
            }
            Err(e) => return Err(e.into()),
        }
    }

    Err(last_err
        .map(TransportError::from)
        .unwrap_or_else(|| TransportError::IO(std::io::ErrorKind::AddrInUse.into())))
}

/// Receives  new inbound connections from the network.
pub struct InboundConnectionHandler<S = UdpSocket, TS: TimeSource = RealTime> {
    new_connection_notifier: mpsc::Receiver<PeerConnection<S, TS>>,
}

impl<S, TS: TimeSource> InboundConnectionHandler<S, TS> {
    pub async fn next_connection(&mut self) -> Option<PeerConnection<S, TS>> {
        self.new_connection_notifier.recv().await
    }
}

/// Requests a new outbound connection to a remote peer.
pub struct OutboundConnectionHandler<S = UdpSocket, TS: TimeSource = RealTime> {
    send_queue: mpsc::Sender<(SocketAddr, ConnectionEvent<S, TS>)>,
    expected_non_gateway: Arc<DashSet<IpAddr>>,
}

impl<S, TS: TimeSource> Clone for OutboundConnectionHandler<S, TS> {
    fn clone(&self) -> Self {
        Self {
            send_queue: self.send_queue.clone(),
            expected_non_gateway: self.expected_non_gateway.clone(),
        }
    }
}

/// Return type for `config_listener`: the outbound handler, inbound connection receiver,
/// and the JoinHandle for the UDP listen task (must be monitored for unexpected exits).
type ListenerSetup<S> = (
    OutboundConnectionHandler<S>,
    mpsc::Receiver<PeerConnection<S>>,
    tokio::task::JoinHandle<TransportError>,
);

#[allow(private_bounds)]
impl<S: Socket> OutboundConnectionHandler<S> {
    #[allow(clippy::too_many_arguments)]
    fn config_listener(
        socket: Arc<S>,
        keypair: TransportKeypair,
        is_gateway: bool,
        socket_addr: SocketAddr,
        bandwidth_limit: Option<usize>,
        global_bandwidth: Option<Arc<GlobalBandwidthManager>>,
        ledbat_min_ssthresh: Option<usize>,
        congestion_config: Option<CongestionControlConfig>,
    ) -> Result<ListenerSetup<S>, TransportError> {
        let (conn_handler_sender, conn_handler_receiver) = mpsc::channel(100);
        let (new_connection_sender, new_connection_notifier) = mpsc::channel(10);
        let expected_non_gateway = Arc::new(DashSet::new());

        let time_source = RealTime::new();
        let transport = UdpPacketsListener {
            is_gateway,
            socket_listener: socket,
            this_peer_keypair: keypair,
            connections: ConnectionStateManager::new(time_source.clone()),
            connection_handler: conn_handler_receiver,
            new_connection_notifier: new_connection_sender,
            this_addr: socket_addr,
            dropped_packets: HashMap::new(),
            last_drop_warning_nanos: time_source.now_nanos(),
            bandwidth_limit,
            global_bandwidth,
            ledbat_min_ssthresh,
            expected_non_gateway: expected_non_gateway.clone(),
            gw_rate_limiter: GatewayConnectionRateLimiter::new(time_source.clone()),
            time_source,
            congestion_config: Some(congestion_config.unwrap_or_default()),
        };
        let connection_handler = OutboundConnectionHandler {
            send_queue: conn_handler_sender,
            expected_non_gateway,
        };

        // Packets are now sent directly to socket from each connection,
        // bypassing the centralized rate limiter that was causing serialization bottlenecks.
        // Per-connection rate limiting is handled by TokenBucket and LEDBAT in RemoteConnection.
        let listen_handle = GlobalExecutor::spawn(RANDOM_U64.scope(
            {
                // Use GlobalRng for deterministic simulation
                let seed = GlobalRng::random_u64();
                let mut rng = StdRng::seed_from_u64(seed);
                rng.random()
            },
            transport.listen(),
        ));

        Ok((connection_handler, new_connection_notifier, listen_handle))
    }

    #[cfg(any(test, feature = "bench"))]
    pub(crate) fn new_test(
        socket_addr: SocketAddr,
        socket: Arc<S>,
        keypair: TransportKeypair,
        is_gateway: bool,
    ) -> Result<(Self, mpsc::Receiver<PeerConnection<S>>), TransportError> {
        let (handler, receiver, _listen_handle) = Self::config_listener(
            socket,
            keypair,
            is_gateway,
            socket_addr,
            None,
            None,
            None,
            None,
        )?;
        Ok((handler, receiver))
    }

    #[cfg(any(test, feature = "bench"))]
    pub(crate) fn new_test_with_bandwidth(
        socket_addr: SocketAddr,
        socket: Arc<S>,
        keypair: TransportKeypair,
        is_gateway: bool,
        bandwidth_limit: Option<usize>,
    ) -> Result<(Self, mpsc::Receiver<PeerConnection<S>>), TransportError> {
        let (handler, receiver, _listen_handle) = Self::config_listener(
            socket,
            keypair,
            is_gateway,
            socket_addr,
            bandwidth_limit,
            None,
            None,
            None,
        )?;
        Ok((handler, receiver))
    }

    pub async fn connect(
        &mut self,
        remote_public_key: TransportPublicKey,
        remote_addr: SocketAddr,
    ) -> Pin<Box<dyn Future<Output = Result<PeerConnection<S>, TransportError>> + Send>> {
        if self.expected_non_gateway.insert(remote_addr.ip()) {
            tracing::debug!(
                peer_addr = %remote_addr,
                direction = "outbound",
                "Awaiting outbound handshake response from remote IP"
            );
        }
        let (open_connection, recv_connection) = oneshot::channel();
        if self
            .send_queue
            .send((
                remote_addr,
                ConnectionEvent::ConnectionStart {
                    remote_public_key,
                    open_connection,
                },
            ))
            .await
            .is_err()
        {
            return async { Err(TransportError::ChannelClosed) }.boxed();
        }
        recv_connection
            .map(|res| match res {
                Ok(Ok(remote_conn)) => Ok(PeerConnection::new(remote_conn)),
                Ok(Err(e)) => Err(e),
                Err(_) => Err(TransportError::ConnectionEstablishmentFailure {
                    cause: "Failed to establish connection".into(),
                }),
            })
            .boxed()
    }

    pub fn expect_incoming(&self, remote_addr: SocketAddr) {
        if self.expected_non_gateway.insert(remote_addr.ip()) {
            tracing::debug!(
                peer_addr = %remote_addr,
                direction = "inbound",
                "Registered expected inbound handshake from remote IP"
            );
        }
    }

    /// Returns a clone of the expected inbound tracker.
    ///
    /// This is used by `EventListenerState` to register expected inbound connections
    /// without holding a reference to the entire `OutboundConnectionHandler`.
    pub fn expected_inbound_tracker(&self) -> ExpectedInboundTracker {
        ExpectedInboundTracker(self.expected_non_gateway.clone())
    }
}

/// A handle for registering expected inbound connections.
///
/// This is a type-erased wrapper around the expected inbound IP set, allowing
/// `EventListenerState` to register expected connections without being generic
/// over the socket type.
#[derive(Clone)]
pub struct ExpectedInboundTracker(Arc<DashSet<IpAddr>>);

impl ExpectedInboundTracker {
    /// Register an expected inbound connection from the given address.
    pub fn expect_incoming(&self, remote_addr: SocketAddr) {
        if self.0.insert(remote_addr.ip()) {
            tracing::debug!(
                peer_addr = %remote_addr,
                direction = "inbound",
                "Registered expected inbound handshake from remote IP"
            );
        }
    }
}

// VirtualTime-specific impl block for instant benchmark simulation (test/bench only)
//
// This impl block provides methods for creating and using OutboundConnectionHandler with VirtualTime.
// By being specific to VirtualTime (not generic over TimeSource), it avoids conflicting with
// the OutboundConnectionHandler<S> impl which uses RealTime by default.
#[cfg(any(test, feature = "bench"))]
#[allow(private_bounds)]
impl<S: Socket> OutboundConnectionHandler<S, crate::simulation::VirtualTime> {
    /// Configure listener with VirtualTime for instant benchmark execution.
    ///
    /// The time_source is passed through to UdpPacketsListener, RemoteConnection,
    /// and PeerConnection so all timing operations use the same source.
    #[allow(clippy::too_many_arguments)]
    fn config_listener_with_virtual_time(
        socket: Arc<S>,
        keypair: TransportKeypair,
        is_gateway: bool,
        socket_addr: SocketAddr,
        bandwidth_limit: Option<usize>,
        global_bandwidth: Option<Arc<GlobalBandwidthManager>>,
        ledbat_min_ssthresh: Option<usize>,
        time_source: crate::simulation::VirtualTime,
        congestion_config: Option<CongestionControlConfig>,
    ) -> Result<
        (
            Self,
            mpsc::Receiver<PeerConnection<S, crate::simulation::VirtualTime>>,
        ),
        TransportError,
    > {
        let (conn_handler_sender, conn_handler_receiver) = mpsc::channel(100);
        let (new_connection_sender, new_connection_notifier) = mpsc::channel(10);
        let expected_non_gateway = Arc::new(DashSet::new());

        let transport = UdpPacketsListener {
            is_gateway,
            socket_listener: socket,
            this_peer_keypair: keypair,
            connections: ConnectionStateManager::new(time_source.clone()),
            connection_handler: conn_handler_receiver,
            new_connection_notifier: new_connection_sender,
            this_addr: socket_addr,
            dropped_packets: HashMap::new(),
            last_drop_warning_nanos: time_source.now_nanos(),
            bandwidth_limit,
            global_bandwidth,
            ledbat_min_ssthresh,
            expected_non_gateway: expected_non_gateway.clone(),
            gw_rate_limiter: GatewayConnectionRateLimiter::new(time_source.clone()),
            time_source,
            congestion_config,
        };
        let connection_handler = OutboundConnectionHandler {
            send_queue: conn_handler_sender,
            expected_non_gateway,
        };

        // Packets are now sent directly to socket from each connection,
        // bypassing the centralized rate limiter that was causing serialization bottlenecks.
        // Per-connection rate limiting is handled by TokenBucket and LEDBAT in RemoteConnection.
        // Note: In test/bench code, the listen handle is intentionally dropped by callers
        // since mock sockets manage their own lifetimes.
        let _listen_handle = GlobalExecutor::spawn(RANDOM_U64.scope(
            {
                let seed = GlobalRng::random_u64();
                let mut rng = StdRng::seed_from_u64(seed);
                rng.random()
            },
            transport.listen(),
        ));

        Ok((connection_handler, new_connection_notifier))
    }

    /// Create a test connection handler with VirtualTime and custom congestion control.
    ///
    /// This allows benchmarks to test with different congestion control algorithms.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn new_test_with_congestion_config(
        socket_addr: SocketAddr,
        socket: Arc<S>,
        keypair: TransportKeypair,
        is_gateway: bool,
        bandwidth_limit: Option<usize>,
        time_source: crate::simulation::VirtualTime,
        congestion_config: Option<CongestionControlConfig>,
    ) -> Result<
        (
            Self,
            mpsc::Receiver<PeerConnection<S, crate::simulation::VirtualTime>>,
        ),
        TransportError,
    > {
        Self::config_listener_with_virtual_time(
            socket,
            keypair,
            is_gateway,
            socket_addr,
            bandwidth_limit,
            None,
            None,
            time_source,
            congestion_config,
        )
    }

    /// Connect to a remote peer using VirtualTime.
    pub async fn connect(
        &mut self,
        remote_public_key: TransportPublicKey,
        remote_addr: SocketAddr,
    ) -> Pin<
        Box<
            dyn Future<
                    Output = Result<
                        PeerConnection<S, crate::simulation::VirtualTime>,
                        TransportError,
                    >,
                > + Send,
        >,
    > {
        if self.expected_non_gateway.insert(remote_addr.ip()) {
            tracing::debug!(
                peer_addr = %remote_addr,
                direction = "outbound",
                "Awaiting outbound handshake response from remote IP"
            );
        }
        let (open_connection, recv_connection) = oneshot::channel();
        if self
            .send_queue
            .send((
                remote_addr,
                ConnectionEvent::ConnectionStart {
                    remote_public_key,
                    open_connection,
                },
            ))
            .await
            .is_err()
        {
            return async { Err(TransportError::ChannelClosed) }.boxed();
        }
        recv_connection
            .map(|res| match res {
                Ok(Ok(remote_conn)) => Ok(PeerConnection::new(remote_conn)),
                Ok(Err(e)) => Err(e),
                Err(_) => Err(TransportError::ConnectionEstablishmentFailure {
                    cause: "Failed to establish connection".into(),
                }),
            })
            .boxed()
    }

    pub fn expect_incoming(&self, remote_addr: SocketAddr) {
        if self.expected_non_gateway.insert(remote_addr.ip()) {
            tracing::debug!(
                peer_addr = %remote_addr,
                direction = "inbound",
                "Registered expected inbound handshake from remote IP"
            );
        }
    }

    /// Returns a clone of the expected inbound tracker.
    pub fn expected_inbound_tracker(&self) -> ExpectedInboundTracker {
        ExpectedInboundTracker(self.expected_non_gateway.clone())
    }
}

/// Handles UDP transport internally.
struct UdpPacketsListener<S = UdpSocket, T: TimeSource = RealTime> {
    socket_listener: Arc<S>,
    /// Unified state manager for all remote connections.
    /// Ensures atomic state transitions during connection lifecycle
    /// (handshake → established → closed) to prevent race conditions.
    connections: ConnectionStateManager<S, T>,
    connection_handler: mpsc::Receiver<(SocketAddr, ConnectionEvent<S, T>)>,
    this_peer_keypair: TransportKeypair,
    is_gateway: bool,
    new_connection_notifier: mpsc::Sender<PeerConnection<S, T>>,
    this_addr: SocketAddr,
    /// Tracks dropped packet counts per remote address for congestion diagnostics.
    /// Used with `last_drop_warning_nanos` to rate-limit warning logs.
    dropped_packets: HashMap<SocketAddr, u64>,
    /// Timestamp (nanos) of last drop warning to prevent log spam.
    last_drop_warning_nanos: u64,
    /// Per-connection bandwidth limit in bytes/sec.
    /// For global fair-share limiting, use `global_bandwidth` instead.
    bandwidth_limit: Option<usize>,
    /// Global bandwidth manager for fair sharing across all connections.
    /// When set, per-connection rates are derived from total_limit / active_connections.
    global_bandwidth: Option<Arc<GlobalBandwidthManager>>,
    /// Minimum ssthresh floor for LEDBAT timeout recovery.
    /// Prevents ssthresh death spiral on high-latency paths.
    ledbat_min_ssthresh: Option<usize>,
    /// IPs expected to connect as regular peers (not gateways).
    /// Used to validate connection attempts and prevent gateway impersonation.
    expected_non_gateway: Arc<DashSet<IpAddr>>,
    time_source: T,
    /// Congestion control configuration for new connections.
    /// When None, uses the default configuration (BBR).
    congestion_config: Option<CongestionControlConfig>,
    /// Rate limiter for gateway connection acceptance after restart.
    /// Ramps up connection acceptance rate to prevent thundering herd.
    /// Only active when `is_gateway` is true.
    gw_rate_limiter: GatewayConnectionRateLimiter<T>,
}

type OngoingConnectionResult<S, T> = Option<
    Result<
        Result<(RemoteConnection<S, T>, InboundRemoteConnection), (TransportError, SocketAddr)>,
        tokio::task::JoinError,
    >,
>;

type GwOngoingConnectionResult<S, T> = Option<
    Result<
        Result<
            (
                RemoteConnection<S, T>,
                InboundRemoteConnection,
                PacketData<SymmetricAES>,
            ),
            (TransportError, SocketAddr),
        >,
        tokio::task::JoinError,
    >,
>;

/// Unified connection state for a remote peer.
enum ConnectionState<S, T: TimeSource> {
    /// Gateway is performing asymmetric handshake with a connecting peer.
    GatewayHandshake {
        packet_sender: FastSender<PacketData<UnknownEncryption>>,
    },
    /// Outbound NAT traversal handshake in progress.
    NatTraversal {
        packet_sender: FastSender<PacketData<UnknownEncryption>>,
        result_sender: oneshot::Sender<Result<RemoteConnection<S, T>, TransportError>>,
    },
    /// Connection fully established with symmetric encryption.
    Established {
        inbound_packet_sender: FastSender<PacketData<UnknownEncryption>>,
    },
    /// Connection recently closed - drains in-flight packets.
    /// Allows new intro packets for reconnection.
    RecentlyClosed { closed_at_nanos: u64 },
}

/// Manages connection state with atomic transitions.
/// Single source of truth for all connection states per address.
struct ConnectionStateManager<S, T: TimeSource> {
    states: BTreeMap<SocketAddr, ConnectionState<S, T>>,
    last_asym_attempt: HashMap<SocketAddr, u64>,
    time_source: T,
}

impl<S, T: TimeSource> ConnectionStateManager<S, T> {
    fn new(time_source: T) -> Self {
        Self {
            states: BTreeMap::new(),
            last_asym_attempt: HashMap::new(),
            time_source,
        }
    }

    /// Get immutable reference to current state for routing decisions.
    fn get_state(&self, addr: &SocketAddr) -> Option<&ConnectionState<S, T>> {
        self.states.get(addr)
    }

    /// Check if connection is established for the given address.
    fn is_established(&self, addr: &SocketAddr) -> bool {
        matches!(
            self.get_state(addr),
            Some(ConnectionState::Established { .. })
        )
    }

    /// Check if a gateway handshake is in progress for the given address.
    fn has_gateway_handshake(&self, addr: &SocketAddr) -> bool {
        matches!(
            self.get_state(addr),
            Some(ConnectionState::GatewayHandshake { .. })
        )
    }

    /// Check if a NAT traversal handshake is in progress for the given address.
    fn has_nat_traversal(&self, addr: &SocketAddr) -> bool {
        matches!(
            self.get_state(addr),
            Some(ConnectionState::NatTraversal { .. })
        )
    }

    /// Try to send a packet to an established connection.
    /// Returns Ok(true) if sent, Ok(false) if channel full, Err if disconnected.
    fn try_send_established(
        &mut self,
        addr: &SocketAddr,
        packet: PacketData<UnknownEncryption>,
    ) -> Result<bool, ()> {
        if let Some(ConnectionState::Established {
            inbound_packet_sender,
        }) = self.states.get(addr)
        {
            match inbound_packet_sender.try_send(packet) {
                Ok(()) => Ok(true),
                Err(fast_channel::TrySendError::Full(_)) => Ok(false),
                Err(fast_channel::TrySendError::Disconnected(_)) => {
                    self.mark_closed(addr);
                    Err(())
                }
            }
        } else {
            Err(())
        }
    }

    /// Try to send a packet to an ongoing gateway handshake.
    /// Returns Ok(true) if sent, Ok(false) if channel full/should drop.
    fn try_send_gateway_handshake(
        &self,
        addr: &SocketAddr,
        packet: PacketData<UnknownEncryption>,
    ) -> Result<bool, ()> {
        if let Some(ConnectionState::GatewayHandshake { packet_sender, .. }) = self.states.get(addr)
        {
            match packet_sender.try_send(packet) {
                Ok(()) => Ok(true),
                Err(fast_channel::TrySendError::Full(_)) => Ok(false),
                Err(fast_channel::TrySendError::Disconnected(_)) => Ok(false), // Handshake completing
            }
        } else {
            Err(())
        }
    }

    /// Try to send a packet to an ongoing NAT traversal.
    /// Returns Ok(true) if sent, Ok(false) if channel closed.
    async fn send_nat_traversal(
        &self,
        addr: &SocketAddr,
        packet: PacketData<UnknownEncryption>,
    ) -> Result<bool, ()> {
        if let Some(ConnectionState::NatTraversal { packet_sender, .. }) = self.states.get(addr) {
            match packet_sender.send_async(packet).await {
                Ok(()) => Ok(true),
                Err(_) => Ok(false),
            }
        } else {
            Err(())
        }
    }

    /// Start a gateway handshake. Only succeeds if no active state exists.
    fn start_gateway_handshake(
        &mut self,
        addr: SocketAddr,
        packet_sender: FastSender<PacketData<UnknownEncryption>>,
    ) -> bool {
        use std::collections::btree_map::Entry;
        let now = self.time_source.now_nanos();

        match self.states.entry(addr) {
            Entry::Vacant(entry) => {
                entry.insert(ConnectionState::GatewayHandshake { packet_sender });
                true
            }
            Entry::Occupied(mut entry) => {
                if let ConnectionState::RecentlyClosed { closed_at_nanos } = entry.get() {
                    if now.saturating_sub(*closed_at_nanos)
                        >= RECENTLY_CLOSED_DURATION.as_nanos() as u64
                    {
                        entry.insert(ConnectionState::GatewayHandshake { packet_sender });
                        return true;
                    } else {
                        tracing::debug!(
                            peer_addr = %addr,
                            remaining_ms = (RECENTLY_CLOSED_DURATION.as_nanos() as u64 - now.saturating_sub(*closed_at_nanos)) / 1_000_000,
                            "Rejecting gateway handshake - connection recently closed"
                        );
                    }
                }
                false
            }
        }
    }

    /// Complete gateway handshake atomically. Returns true if state was GatewayHandshake.
    fn complete_gateway_handshake(
        &mut self,
        addr: SocketAddr,
        inbound_packet_sender: FastSender<PacketData<UnknownEncryption>>,
    ) -> bool {
        use std::collections::btree_map::Entry;

        match self.states.entry(addr) {
            Entry::Occupied(mut entry) => {
                if matches!(entry.get(), ConnectionState::GatewayHandshake { .. }) {
                    entry.insert(ConnectionState::Established {
                        inbound_packet_sender,
                    });
                    true
                } else {
                    false
                }
            }
            Entry::Vacant(_) => false,
        }
    }

    /// Fail a gateway handshake - remove state.
    fn fail_gateway_handshake(&mut self, addr: &SocketAddr) {
        if matches!(
            self.states.get(addr),
            Some(ConnectionState::GatewayHandshake { .. })
        ) {
            self.states.remove(addr);
        }
    }

    /// Start NAT traversal.
    fn start_nat_traversal(
        &mut self,
        addr: SocketAddr,
        packet_sender: FastSender<PacketData<UnknownEncryption>>,
        result_sender: oneshot::Sender<Result<RemoteConnection<S, T>, TransportError>>,
    ) -> bool {
        use std::collections::btree_map::Entry;
        let now = self.time_source.now_nanos();

        match self.states.entry(addr) {
            Entry::Vacant(entry) => {
                entry.insert(ConnectionState::NatTraversal {
                    packet_sender,
                    result_sender,
                });
                true
            }
            Entry::Occupied(mut entry) => {
                match entry.get() {
                    ConnectionState::Established { .. } => {
                        // Higher layer requested new connection, clear existing one
                        self.last_asym_attempt.remove(&addr);
                        tracing::info!(
                            peer_addr = %addr,
                            "Clearing existing connection to allow fresh NAT traversal"
                        );
                        entry.insert(ConnectionState::NatTraversal {
                            packet_sender,
                            result_sender,
                        });
                        true
                    }
                    ConnectionState::NatTraversal { .. } => {
                        // Already in progress, reject duplicate
                        false
                    }
                    ConnectionState::GatewayHandshake { .. } => {
                        // Don't overwrite ongoing gateway handshake to avoid orphaning its task
                        tracing::debug!(
                            peer_addr = %addr,
                            "Rejecting NAT traversal - gateway handshake already in progress"
                        );
                        false
                    }
                    ConnectionState::RecentlyClosed { closed_at_nanos } => {
                        // Allow if grace period expired
                        if now.saturating_sub(*closed_at_nanos)
                            >= RECENTLY_CLOSED_DURATION.as_nanos() as u64
                        {
                            entry.insert(ConnectionState::NatTraversal {
                                packet_sender,
                                result_sender,
                            });
                            true
                        } else {
                            tracing::debug!(
                                peer_addr = %addr,
                                remaining_ms = (RECENTLY_CLOSED_DURATION.as_nanos() as u64 - now.saturating_sub(*closed_at_nanos)) / 1_000_000,
                                "Rejecting NAT traversal - connection recently closed"
                            );
                            false
                        }
                    }
                }
            }
        }
    }

    /// Complete NAT traversal atomically. Returns result_sender if state was NatTraversal.
    fn complete_nat_traversal(
        &mut self,
        addr: SocketAddr,
        inbound_packet_sender: FastSender<PacketData<UnknownEncryption>>,
    ) -> Option<oneshot::Sender<Result<RemoteConnection<S, T>, TransportError>>> {
        use std::collections::btree_map::Entry;

        match self.states.entry(addr) {
            Entry::Occupied(mut entry) => {
                if matches!(entry.get(), ConnectionState::NatTraversal { .. }) {
                    let old = entry.insert(ConnectionState::Established {
                        inbound_packet_sender,
                    });
                    if let ConnectionState::NatTraversal { result_sender, .. } = old {
                        return Some(result_sender);
                    }
                }
                None
            }
            Entry::Vacant(_) => None,
        }
    }

    /// Fail NAT traversal - remove state and return result_sender for error reporting.
    fn fail_nat_traversal(
        &mut self,
        addr: &SocketAddr,
    ) -> Option<oneshot::Sender<Result<RemoteConnection<S, T>, TransportError>>> {
        if matches!(
            self.states.get(addr),
            Some(ConnectionState::NatTraversal { .. })
        ) {
            if let Some(ConnectionState::NatTraversal { result_sender, .. }) =
                self.states.remove(addr)
            {
                return Some(result_sender);
            }
        }
        None
    }

    /// Mark connection as closed. Only established connections enter the grace period.
    fn mark_closed(&mut self, addr: &SocketAddr) {
        match self.states.remove(addr) {
            Some(ConnectionState::Established { .. }) => {
                self.states.insert(
                    *addr,
                    ConnectionState::RecentlyClosed {
                        closed_at_nanos: self.time_source.now_nanos(),
                    },
                );
            }
            Some(ConnectionState::NatTraversal { result_sender, .. }) => {
                // Receiver may have timed out; log but don't fail
                if result_sender
                    .send(Err(TransportError::ConnectionClosed(*addr)))
                    .is_err()
                {
                    tracing::debug!(peer_addr = %addr, "NAT traversal result receiver already dropped");
                }
            }
            _ => {}
        }
    }

    /// Remove established connection and return the packet sender (for detection purposes).
    fn remove_established(
        &mut self,
        addr: &SocketAddr,
    ) -> Option<FastSender<PacketData<UnknownEncryption>>> {
        if matches!(
            self.states.get(addr),
            Some(ConnectionState::Established { .. })
        ) {
            if let Some(ConnectionState::Established {
                inbound_packet_sender,
            }) = self.states.remove(addr)
            {
                return Some(inbound_packet_sender);
            }
        }
        None
    }

    /// Check if address is rate limited for asymmetric decryption.
    fn is_rate_limited(&self, addr: &SocketAddr) -> bool {
        let now = self.time_source.now_nanos();
        self.last_asym_attempt.get(addr).is_some_and(|last_nanos| {
            now.saturating_sub(*last_nanos) < ASYM_DECRYPTION_RATE_LIMIT.as_nanos() as u64
        })
    }

    /// Record an asymmetric decryption attempt for rate limiting.
    fn record_asym_attempt(&mut self, addr: SocketAddr) {
        let now = self.time_source.now_nanos();
        self.last_asym_attempt.insert(addr, now);
    }

    /// Clear rate limit tracking for an address.
    fn clear_rate_limit(&mut self, addr: &SocketAddr) {
        self.last_asym_attempt.remove(addr);
    }

    /// Remove stale closed connections from the same IP.
    fn remove_stale_from_ip(&mut self, new_addr: SocketAddr) {
        let remote_ip = new_addr.ip();
        let stale_addrs: Vec<_> = self
            .states
            .iter()
            .filter_map(|(addr, state)| {
                if addr.ip() == remote_ip && *addr != new_addr {
                    if let ConnectionState::Established {
                        inbound_packet_sender,
                    } = state
                    {
                        if inbound_packet_sender.is_closed() {
                            return Some(*addr);
                        }
                    }
                }
                None
            })
            .collect();

        for stale_addr in stale_addrs {
            self.states.remove(&stale_addr);
            self.last_asym_attempt.remove(&stale_addr);
            tracing::debug!(
                stale_peer_addr = %stale_addr,
                new_peer_addr = %new_addr,
                "Removed stale closed connection from same IP"
            );
        }
    }

    /// Clean up expired RecentlyClosed states and rate limit entries.
    fn cleanup_expired(&mut self) {
        let now = self.time_source.now_nanos();

        self.states.retain(|_, state| {
            if let ConnectionState::RecentlyClosed { closed_at_nanos } = state {
                now.saturating_sub(*closed_at_nanos) < RECENTLY_CLOSED_DURATION.as_nanos() as u64
            } else {
                true
            }
        });

        self.last_asym_attempt.retain(|_, last_attempt_nanos| {
            now.saturating_sub(*last_attempt_nanos) < ASYM_DECRYPTION_RATE_LIMIT.as_nanos() as u64
        });
    }
}

/// Rate limiter for gateway connection acceptance after restart.
///
/// Prevents thundering herd when a gateway restarts and all peers reconnect
/// simultaneously. Uses a 1-second tumbling window to count accepted connections
/// and compares against a rate that ramps up over time since gateway start.
struct GatewayConnectionRateLimiter<T: TimeSource> {
    /// Nanos timestamp when the gateway started listening.
    start_nanos: u64,
    /// Nanos timestamp of the current 1-second window start.
    window_start_nanos: u64,
    /// Number of connections accepted in the current window.
    window_count: u64,
    time_source: T,
}

impl<T: TimeSource> GatewayConnectionRateLimiter<T> {
    fn new(time_source: T) -> Self {
        let now = time_source.now_nanos();
        Self {
            start_nanos: now,
            window_start_nanos: now,
            window_count: 0,
            time_source,
        }
    }

    /// Returns the maximum connections/second allowed based on time since gateway start.
    fn current_rate_limit(&self) -> Option<u64> {
        let elapsed = self
            .time_source
            .now_nanos()
            .saturating_sub(self.start_nanos);
        if elapsed < GW_RAMP_PHASE1_DURATION.as_nanos() as u64 {
            Some(GW_RAMP_PHASE1_RATE)
        } else if elapsed < GW_RAMP_PHASE2_DURATION.as_nanos() as u64 {
            Some(GW_RAMP_PHASE2_RATE)
        } else {
            None // Unlimited
        }
    }

    /// Check if a new connection should be accepted. Returns true if allowed.
    fn try_accept(&mut self) -> bool {
        let Some(max_rate) = self.current_rate_limit() else {
            return true; // Unlimited phase
        };

        let now = self.time_source.now_nanos();
        let window_elapsed = now.saturating_sub(self.window_start_nanos);

        // Reset window every second
        if window_elapsed >= Duration::from_secs(1).as_nanos() as u64 {
            self.window_start_nanos = now;
            self.window_count = 0;
        }

        if self.window_count < max_rate {
            self.window_count += 1;
            true
        } else {
            false
        }
    }
}

#[cfg(any(test, feature = "bench"))]
impl<S, T: TimeSource> Drop for UdpPacketsListener<S, T> {
    fn drop(&mut self) {
        tracing::info!(%self.this_addr, "Dropping UdpPacketsListener");
    }
}
task_local! {
    static RANDOM_U64: [u8; 8];
}

/// The amount of times to retry NAT traversal before giving up. It should be sufficient
/// so we give peers enough time for the other party to start the connection on its end.
#[cfg(not(any(test, feature = "bench")))]
pub(super) const NAT_TRAVERSAL_MAX_ATTEMPTS: usize = 40;
#[cfg(any(test, feature = "bench"))]
pub(super) const NAT_TRAVERSAL_MAX_ATTEMPTS: usize = 10;

impl<S: Socket, T: TimeSource> UdpPacketsListener<S, T> {
    #[tracing::instrument(level = "debug", name = "transport_listener", fields(peer = %self.this_peer_keypair.public), skip_all)]
    async fn listen(mut self) -> TransportError {
        tracing::debug!(
            bind_addr = %self.this_addr,
            "Listening for packets"
        );
        let mut buf = [0u8; RECV_BUF_SIZE];
        let mut connection_tasks = FuturesUnordered::new();
        let mut gw_connection_tasks = FuturesUnordered::new();
        let mut outdated_peer: HashMap<SocketAddr, u64> = HashMap::new();
        let mut last_cleanup_nanos = self.time_source.now_nanos();

        'outer: loop {
            // DST: Use deterministic_select! for fair but deterministic branch ordering
            crate::deterministic_select! {
                recv_result = self.socket_listener.recv_from(&mut buf) => {
                    match recv_result {
                        Ok((size, remote_addr)) => {
                            if let Some(time_nanos) = outdated_peer.get(&remote_addr) {
                                let now_nanos = self.time_source.now_nanos();
                                if now_nanos.saturating_sub(*time_nanos) < OUTDATED_PEER_EXPIRY.as_nanos() as u64 {
                                    let packet_data = PacketData::<_, RECV_BUF_SIZE>::from_buf(&buf[..size]);
                                    if packet_data.is_intro_packet() {
                                        if self.connections.is_rate_limited(&remote_addr) {
                                            continue;
                                        }
                                        self.connections.record_asym_attempt(remote_addr);
                                        if let Ok(decrypted) = packet_data.try_decrypt_asym(&self.this_peer_keypair.secret) {
                                            let decrypted_data = decrypted.data();
                                            let proto_len = PROTOC_VERSION.len();
                                            if decrypted_data.len() >= proto_len + 16 {
                                                let protoc_bytes: Result<[u8; 8], _> =
                                                    decrypted_data[..proto_len].try_into();
                                                if let Ok(ref remote_ver) = protoc_bytes {
                                                    if version_cmp::is_compatible(&PROTOC_VERSION, remote_ver).is_ok() {
                                                        tracing::info!(
                                                            peer_addr = %remote_addr,
                                                            "Outdated peer upgraded. Allowing reconnection."
                                                        );
                                                        outdated_peer.remove(&remote_addr);
                                                        self.connections.clear_rate_limit(&remote_addr);
                                                    } else {
                                                        continue;
                                                    }
                                                } else {
                                                    continue;
                                                }
                                            } else {
                                                continue;
                                            }
                                        } else {
                                            continue;
                                        }
                                    } else {
                                        continue;
                                    }
                                } else {
                                    outdated_peer.remove(&remote_addr);
                                }
                            }
                            let packet_data = PacketData::from_buf(&buf[..size]);

                            tracing::trace!(
                                peer_addr = %remote_addr,
                                packet_size = size,
                                is_established = self.connections.is_established(&remote_addr),
                                has_gw_handshake = self.connections.has_gateway_handshake(&remote_addr),
                                has_nat_traversal = self.connections.has_nat_traversal(&remote_addr),
                                "Received packet from remote"
                            );

                            // Route packet based on connection state
                            if let Some(state) = self.connections.get_state(&remote_addr) {
                                match state {
                                    ConnectionState::Established { .. } => {
                                        // Check for new identity (peer restart).
                                        // Both gateways and regular peers detect restart: a valid
                                        // intro packet on an established connection means the remote
                                        // restarted and is attempting to reconnect (possibly reusing
                                        // an existing NAT hole). Tearing down the stale session
                                        // lets the new handshake proceed. Rate-limited to 1/sec per
                                        // IP to prevent asymmetric decryption DoS.
                                        let is_new_identity = if packet_data.is_intro_packet() {
                                            if self.connections.is_rate_limited(&remote_addr) {
                                                false
                                            } else {
                                                self.connections.record_asym_attempt(remote_addr);
                                                match packet_data.try_decrypt_asym(&self.this_peer_keypair.secret) {
                                                    Ok(decrypted) => {
                                                        let decrypted_data = decrypted.data();
                                                        let proto_len = PROTOC_VERSION.len();
                                                        if decrypted_data.len() >= proto_len + 16 {
                                                            decrypted_data[..proto_len].try_into().ok()
                                                                .map(|remote_ver: [u8; 8]| {
                                                                    version_cmp::is_compatible(&PROTOC_VERSION, &remote_ver).is_ok()
                                                                })
                                                                .unwrap_or(false)
                                                        } else {
                                                            false
                                                        }
                                                    }
                                                    Err(_) => false,
                                                }
                                            }
                                        } else {
                                            false
                                        };

                                        if is_new_identity {
                                            tracing::info!(
                                                peer_addr = %remote_addr,
                                                is_gateway = self.is_gateway,
                                                "Detected peer restart (new intro on established connection). Resetting session."
                                            );
                                            self.connections.clear_rate_limit(&remote_addr);
                                            self.connections.remove_established(&remote_addr);

                                            if !self.is_gateway {
                                                // Non-gateway: accept the restarted peer's intro
                                                // using a server-side handshake (same as gateways).
                                                // This lets the restarted peer reconnect directly
                                                // via an existing NAT hole without full gateway
                                                // re-bootstrap.
                                                let inbound_key_bytes = key_from_addr(&remote_addr);
                                                let (gw_ongoing_connection, packets_sender) =
                                                    self.gateway_connection(packet_data, remote_addr, inbound_key_bytes);
                                                if self.connections.start_gateway_handshake(remote_addr, packets_sender) {
                                                    let task = GlobalExecutor::spawn(gw_ongoing_connection
                                                        .instrument(tracing::span!(tracing::Level::DEBUG, "reconnect_accept"))
                                                        .map_err(move |error| {
                                                            tracing::debug!(peer_addr = %remote_addr, error = %error, "Reconnect accept error");
                                                            (error, remote_addr)
                                                        }));
                                                    gw_connection_tasks.push(task);
                                                }
                                                continue;
                                            }
                                            // Gateway: fall through to gateway handler for fresh session
                                        } else {
                                            match self.connections.try_send_established(&remote_addr, packet_data) {
                                                Ok(true) => continue,
                                                Ok(false) => {
                                                    // Channel full - track dropped packets
                                                    let dropped_count = self.dropped_packets.entry(remote_addr).or_insert(0);
                                                    *dropped_count += 1;
                                                    let now_nanos = self.time_source.now_nanos();
                                                    if now_nanos.saturating_sub(self.last_drop_warning_nanos) > Duration::from_secs(10).as_nanos() as u64 {
                                                        let total_dropped: u64 = self.dropped_packets.values().sum();
                                                        tracing::warn!(
                                                            total_dropped,
                                                            elapsed_secs = 10,
                                                            "Channel overflow: dropped packets (bandwidth limit may be too high or receiver too slow)"
                                                        );
                                                        for (addr, count) in &self.dropped_packets {
                                                            if *count > 100 {
                                                                tracing::warn!(
                                                                    peer_addr = %addr,
                                                                    dropped_count = count,
                                                                    "High packet drop rate for peer"
                                                                );
                                                            }
                                                        }
                                                        self.dropped_packets.clear();
                                                        self.last_drop_warning_nanos = now_nanos;
                                                    }
                                                    continue;
                                                }
                                                Err(()) => {
                                                    tracing::debug!(peer_addr = %remote_addr, "Connection closed");
                                                    continue;
                                                }
                                            }
                                        }
                                    }

                                    ConnectionState::GatewayHandshake { .. } => {
                                        if packet_data.is_intro_packet() {
                                            tracing::debug!(peer_addr = %remote_addr, "Ignoring duplicate intro during gateway handshake");
                                            continue;
                                        }
                                        match self.connections.try_send_gateway_handshake(&remote_addr, packet_data) {
                                            Ok(true) => continue,
                                            Ok(false) | Err(()) => {
                                                tracing::debug!(peer_addr = %remote_addr, "Gateway handshake channel busy/closed, dropping packet");
                                                continue;
                                            }
                                        }
                                    }

                                    ConnectionState::NatTraversal { .. } => {
                                        // Rate limit intro packets to prevent decryption storms
                                        // during NAT traversal handshake
                                        if packet_data.is_intro_packet() {
                                            if self.connections.is_rate_limited(&remote_addr) {
                                                tracing::trace!(peer_addr = %remote_addr, "Rate limiting NAT traversal intro packet");
                                                continue;
                                            }
                                            self.connections.record_asym_attempt(remote_addr);
                                        }
                                        if let Err(()) = self.connections.send_nat_traversal(&remote_addr, packet_data).await {
                                            tracing::debug!(peer_addr = %remote_addr, "failed to send NAT traversal packet");
                                        }
                                        continue;
                                    }

                                    ConnectionState::RecentlyClosed { .. } => {
                                        if !packet_data.is_intro_packet() || !self.is_gateway {
                                            tracing::trace!(peer_addr = %remote_addr, "Dropping packet for recently closed connection");
                                            continue;
                                        }
                                        // Intro packet - fall through to gateway handler for reconnection
                                    }
                                }
                            }

                            // Gateway intro packet handling
                            if self.is_gateway {
                                if self.connections.has_gateway_handshake(&remote_addr) {
                                    tracing::debug!(peer_addr = %remote_addr, "Gateway connection already in progress");
                                    continue;
                                }

                                // Clean up stale closed connections from same IP
                                self.connections.remove_stale_from_ip(remote_addr);

                                // Only process intro packets — check before rate limiters
                                // to avoid wasting rate limit budget on non-intro packets.
                                if !packet_data.is_intro_packet() {
                                    tracing::trace!(peer_addr = %remote_addr, "Dropping non-intro packet from unknown address");
                                    continue;
                                }

                                // Rate limit intro attempts (per-IP)
                                if self.connections.is_rate_limited(&remote_addr) {
                                    tracing::trace!(peer_addr = %remote_addr, "Rate limiting gateway intro attempt");
                                    continue;
                                }

                                // Global rate limit: prevent thundering herd after gateway restart.
                                // Gradually ramps up connection acceptance rate over 2 minutes.
                                // Safe to drop: peers send multiple intro packets per attempt
                                // and retry via initial_join_procedure's loop with jitter.
                                if !self.gw_rate_limiter.try_accept() {
                                    tracing::trace!(
                                        peer_addr = %remote_addr,
                                        max_rate = ?self.gw_rate_limiter.current_rate_limit(),
                                        "Deferring connection: gateway ramp-up rate limit"
                                    );
                                    continue;
                                }

                                self.connections.record_asym_attempt(remote_addr);

                                let inbound_key_bytes = key_from_addr(&remote_addr);
                                let (gw_ongoing_connection, packets_sender) = self.gateway_connection(packet_data, remote_addr, inbound_key_bytes);
                                if !self.connections.start_gateway_handshake(remote_addr, packets_sender) {
                                    continue; // State transition failed (e.g., RecentlyClosed still draining)
                                }
                                let task = GlobalExecutor::spawn(gw_ongoing_connection
                                    .instrument(tracing::span!(tracing::Level::DEBUG, "gateway_connection"))
                                    .map_err(move |error| {
                                        tracing::debug!(peer_addr = %remote_addr, error = %error, "Gateway connection error");
                                        (error, remote_addr)
                                    }));
                                gw_connection_tasks.push(task);
                                continue;
                            } else {
                                // Non-gateway peers: mark as expected and wait for the normal peer handshake flow.
                                self.expected_non_gateway.insert(remote_addr.ip());
                                tracing::trace!(
                                    peer_addr = %remote_addr,
                                    "Ignoring unknown packet (not an intro, no active connection)"
                                );
                                continue;
                            }
                        }
                        Err(e) => {
                            // Transient errors (e.g., ECONNREFUSED on Linux for ICMP port unreachable)
                            // should not kill the listener. Only fatal errors should exit.
                            let kind = e.kind();
                            if matches!(kind, std::io::ErrorKind::ConnectionReset
                                | std::io::ErrorKind::ConnectionRefused
                                | std::io::ErrorKind::ConnectionAborted
                                | std::io::ErrorKind::Interrupted
                                | std::io::ErrorKind::WouldBlock
                                | std::io::ErrorKind::TimedOut)
                            {
                                tracing::warn!(
                                    error = ?e,
                                    error_kind = ?kind,
                                    "Transient UDP recv_from error, continuing"
                                );
                                continue;
                            }
                            tracing::error!(
                                error = ?e,
                                error_kind = ?kind,
                                bind_addr = %self.this_addr,
                                "Fatal UDP recv_from error, listen task exiting"
                            );
                            return e.into();
                        }
                    }
                },
                // Handle completed gateway connection handshakes
                gw_connection_handshake = gw_connection_tasks.next(), if !gw_connection_tasks.is_empty() => {
                    let Some(res): GwOngoingConnectionResult<S, T> = gw_connection_handshake else {
                        unreachable!("gw_connection_tasks.next() should only return None if empty, which is guarded");
                    };
                    // Issue #2725: Handle JoinError properly - tasks can be cancelled during
                    // shutdown or cleanup, not just panic. Cancellation is expected behavior
                    // and should not crash the handler.
                    let task_result = match res {
                        Ok(inner) => inner,
                        Err(join_error) => {
                            if join_error.is_cancelled() {
                                tracing::debug!(
                                    direction = "inbound",
                                    "Gateway connection task was cancelled"
                                );
                            } else {
                                // Task panicked - log but don't propagate to avoid crashing handler
                                tracing::error!(
                                    direction = "inbound",
                                    "Gateway connection task panicked: {join_error}"
                                );
                            }
                            continue;
                        }
                    };
                    match task_result {
                        // Successful gateway connection
                        Ok((outbound_remote_conn, inbound_remote_connection, outbound_ack_packet)) => {
                            let remote_addr = outbound_remote_conn.remote_addr;
                            let sent_tracker = outbound_remote_conn.sent_tracker.clone();

                            if self.connections.complete_gateway_handshake(remote_addr, inbound_remote_connection.inbound_packet_sender) {
                                if self.new_connection_notifier
                                    .send(PeerConnection::new(outbound_remote_conn))
                                    .await
                                    .is_err() {
                                    tracing::error!(peer_addr = %remote_addr, "Failed to notify new gateway connection");
                                    break 'outer TransportError::ConnectionClosed(self.this_addr);
                                }
                                sent_tracker.lock().report_sent_packet(
                                    SymmetricMessage::FIRST_PACKET_ID,
                                    outbound_ack_packet.prepared_send(),
                                );
                            } else {
                                tracing::debug!(peer_addr = %remote_addr, "Connection state changed during handshake");
                            }
                        }
                        // Failed gateway connection
                        Err((error, remote_addr)) => {
                            tracing::info!(error = %error, peer_addr = %remote_addr, "Failed to establish gateway connection");
                            if matches!(error, TransportError::ProtocolVersionMismatch { .. }) {
                                outdated_peer.insert(remote_addr, self.time_source.now_nanos());
                            }
                            self.connections.fail_gateway_handshake(&remote_addr);
                        }
                    }
                },
                // Handle completed NAT traversal handshakes
                connection_handshake = connection_tasks.next(), if !connection_tasks.is_empty() => {
                    let Some(res): OngoingConnectionResult<S, T> = connection_handshake else {
                        unreachable!("connection_tasks.next() should only return None if empty, which is guarded");
                    };
                    // Issue #2725: Handle JoinError properly - tasks can be cancelled during
                    // shutdown or cleanup, not just panic. Cancellation is expected behavior
                    // and should not crash the handler.
                    let task_result = match res {
                        Ok(inner) => inner,
                        Err(join_error) => {
                            if join_error.is_cancelled() {
                                tracing::debug!(
                                    direction = "outbound",
                                    "Connection task was cancelled"
                                );
                            } else {
                                // Task panicked - log but don't propagate to avoid crashing handler
                                tracing::error!(
                                    direction = "outbound",
                                    "Connection task panicked: {join_error}"
                                );
                            }
                            continue;
                        }
                    };
                    match task_result {
                        // Successful NAT traversal
                        Ok((outbound_remote_conn, inbound_remote_connection)) => {
                            let remote_addr = outbound_remote_conn.remote_addr;
                            self.expected_non_gateway.remove(&remote_addr.ip());

                            // Atomic transition: NatTraversal -> Established
                            if let Some(result_sender) = self.connections.complete_nat_traversal(
                                remote_addr,
                                inbound_remote_connection.inbound_packet_sender,
                            ) {
                                tracing::info!(peer_addr = %remote_addr, "NAT traversal connection established");
                                if result_sender.send(Ok(outbound_remote_conn)).is_err() {
                                    tracing::debug!(peer_addr = %remote_addr, "NAT traversal result receiver already dropped");
                                }
                            } else {
                                tracing::debug!(peer_addr = %remote_addr, "Connection state changed during NAT traversal");
                            }
                        }
                        // Failed NAT traversal
                        Err((error, remote_addr)) => {
                            match &error {
                                TransportError::ProtocolVersionMismatch { .. } => {
                                    tracing::warn!(%error);
                                }
                                TransportError::ChannelClosed | TransportError::ConnectionClosed(_) | TransportError::ConnectionEstablishmentFailure { .. } | TransportError::SendFailed(..) | TransportError::IO(_) | TransportError::Other(_) | TransportError::PubKeyDecryptionError(_) | TransportError::Serialization(_) => {
                                    tracing::error!(error = %error, peer_addr = %remote_addr, "Failed NAT traversal");
                                }
                            }
                            self.expected_non_gateway.remove(&remote_addr.ip());
                            if let Some(result_sender) = self.connections.fail_nat_traversal(&remote_addr) {
                                if result_sender.send(Err(error)).is_err() {
                                    tracing::debug!(peer_addr = %remote_addr, "NAT traversal failure result receiver already dropped");
                                }
                            }
                        }
                    }
                },
                // Handle new connection requests
                connection_event = self.connection_handler.recv() => {
                    let Some((remote_addr, event)) = connection_event else {
                        // This fires both during graceful shutdown (event loop exits,
                        // drops senders) and unexpected sender drops. Use warn! since
                        // graceful shutdown is the common case.
                        tracing::warn!(
                            bind_addr = %self.this_addr,
                            "Connection handler channel closed — listen task exiting. \
                             This is expected during graceful shutdown."
                        );
                        return TransportError::ConnectionClosed(self.this_addr);
                    };
                    tracing::debug!(peer_addr = %remote_addr, "Received connection event");
                    let ConnectionEvent::ConnectionStart { remote_public_key, open_connection } = event;

                    // Check for existing NAT traversal in progress
                    if self.connections.has_nat_traversal(&remote_addr) {
                        tracing::debug!(peer_addr = %remote_addr, "NAT traversal already in progress");
                        if open_connection.send(Err(TransportError::ConnectionEstablishmentFailure {
                            cause: "connection attempt already in progress".into(),
                        })).is_err() {
                            tracing::debug!(peer_addr = %remote_addr, "duplicate NAT traversal notification receiver already dropped");
                        }
                        continue;
                    }

                    tracing::info!(peer_addr = %remote_addr, "Starting NAT traversal");
                    let (ongoing_connection, packets_sender) = self.traverse_nat(remote_addr, remote_public_key.clone());

                    if !self.connections.start_nat_traversal(remote_addr, packets_sender, open_connection) {
                        continue;
                    }

                    self.expected_non_gateway.insert(remote_addr.ip());
                    let task = GlobalExecutor::spawn(ongoing_connection
                        .map_err(move |err| (err, remote_addr))
                        .instrument(span!(tracing::Level::DEBUG, "traverse_nat"))
                    );
                    connection_tasks.push(task);
                },
                _ = async {}, if {
                    let now_nanos = self.time_source.now_nanos();
                    now_nanos.saturating_sub(last_cleanup_nanos) >= RATE_LIMIT_CLEANUP_INTERVAL.as_nanos() as u64
                } => {
                    self.connections.cleanup_expired();
                    let now_nanos = self.time_source.now_nanos();
                    last_cleanup_nanos = now_nanos;

                    let outdated_cutoff = now_nanos.saturating_sub(OUTDATED_PEER_EXPIRY.as_nanos() as u64);
                    outdated_peer.retain(|_, timestamp| *timestamp > outdated_cutoff);
                }
            }
        }
    }

    #[allow(clippy::type_complexity)]
    fn gateway_connection(
        &mut self,
        remote_intro_packet: PacketData<UnknownEncryption>,
        remote_addr: SocketAddr,
        inbound_key_bytes: [u8; 16],
    ) -> (
        GatewayConnectionFuture<S, T>,
        FastSender<PacketData<UnknownEncryption>>,
    ) {
        let secret = self.this_peer_keypair.secret.clone();
        let bandwidth_limit = self.bandwidth_limit;
        let global_bandwidth = self.global_bandwidth.clone();
        let _ledbat_min_ssthresh = self.ledbat_min_ssthresh;
        let socket = self.socket_listener.clone();
        let time_source = self.time_source.clone();
        let congestion_config = self.congestion_config.clone();

        let (inbound_from_remote, next_inbound) =
            fast_channel::bounded::<PacketData<UnknownEncryption>>(100);
        let f = async move {
            let decrypted_intro_packet =
                secret
                    .decrypt(remote_intro_packet.data())
                    .inspect_err(|err| {
                        tracing::debug!(
                            peer_addr = %remote_addr,
                            error = %err,
                            direction = "inbound",
                            "Failed to decrypt intro packet"
                        );
                    })?;

            let protoc = decrypted_intro_packet
                .get(..PROTOC_VERSION.len())
                .ok_or_else(|| TransportError::ConnectionEstablishmentFailure {
                    cause: "Packet too small to contain protocol version".into(),
                })?;

            let outbound_key_bytes = decrypted_intro_packet
                .get(PROTOC_VERSION.len()..PROTOC_VERSION.len() + 16)
                .ok_or_else(|| TransportError::ConnectionEstablishmentFailure {
                    cause: "Packet too small to contain outbound key bytes".into(),
                })?;

            let outbound_key = Aes128Gcm::new_from_slice(outbound_key_bytes).map_err(|_| {
                TransportError::ConnectionEstablishmentFailure {
                    cause: "invalid symmetric key".into(),
                }
            })?;
            let protoc_bytes: [u8; 8] =
                protoc
                    .try_into()
                    .map_err(|_| TransportError::ConnectionEstablishmentFailure {
                        cause: "Protocol version field wrong size".into(),
                    })?;
            match version_cmp::is_compatible(&PROTOC_VERSION, &protoc_bytes) {
                Ok(remote_info) => {
                    crate::transport::report_peer_version(remote_info.version);
                }
                Err(reason) => {
                    // Track version even on incompatible peers — aids version discovery.
                    let remote_info = version_cmp::parse_version_bytes(&protoc_bytes);
                    crate::transport::report_peer_version(remote_info.version);
                    if version_cmp::remote_requires_newer_than_us(&PROTOC_VERSION, &protoc_bytes) {
                        crate::transport::signal_urgent_update();
                    }
                    let packet = SymmetricMessage::ack_error(&outbound_key)?;
                    socket
                        .send_to(&packet.prepared_send(), remote_addr)
                        .await
                        .map_err(|_| TransportError::ChannelClosed)?;
                    return Err(TransportError::ProtocolVersionMismatch {
                        expected: PCK_VERSION.to_string(),
                        actual: reason.to_string(),
                    });
                }
            }

            let inbound_key = Aes128Gcm::new(&inbound_key_bytes.into());
            let outbound_ack_packet =
                SymmetricMessage::ack_ok(&outbound_key, inbound_key_bytes, remote_addr)?;

            tracing::trace!(
                peer_addr = %remote_addr,
                direction = "inbound",
                packet_len = outbound_ack_packet.data().len(),
                "Sending outbound ack packet"
            );

            socket
                .send_to(&outbound_ack_packet.clone().prepared_send(), remote_addr)
                .await
                .map_err(|_| TransportError::ChannelClosed)?;

            // wait until the remote sends the ack packet
            let timeout_result = crate::deterministic_select! {
                result = next_inbound.recv_async() => Some(result),
                _ = time_source.sleep(Duration::from_secs(5)) => None,
            };
            match timeout_result {
                Some(Ok(packet)) => {
                    let _ = packet.try_decrypt_sym(&inbound_key).map_err(|_| {
                        tracing::debug!(
                            peer_addr = %remote_addr,
                            direction = "inbound",
                            packet_len = packet.data().len(),
                            "Failed to decrypt packet with inbound key"
                        );
                        TransportError::ConnectionEstablishmentFailure {
                            cause: "invalid symmetric key".into(),
                        }
                    })?;
                }
                Some(Err(_)) => {
                    return Err(TransportError::ConnectionEstablishmentFailure {
                        cause: "connection closed".into(),
                    });
                }
                None => {
                    return Err(TransportError::ConnectionEstablishmentFailure {
                        cause: "connection timed out waiting for response".into(),
                    });
                }
            }

            let sent_tracker = Arc::new(parking_lot::Mutex::new(
                SentPacketTracker::new_with_time_source(time_source.clone()),
            ));

            // Initialize congestion controller (BBR by default, configurable for benchmarks)
            let congestion_controller = congestion_config
                .clone()
                .unwrap_or_default()
                .build_arc_with_time_source(time_source.clone());

            // Initialize token bucket for smooth packet pacing.
            // Use global bandwidth manager if configured, otherwise use the
            // congestion controller's configured rate. Previously defaulted to
            // 10 MB/s which is 8x higher than FixedRate's 1.25 MB/s — the
            // initial burst triggered packet loss and immediate loss_pause,
            // collapsing throughput. See #3702.
            let initial_rate = if let Some(ref global) = global_bandwidth {
                global.register_connection()
            } else if let Some(limit) = bandwidth_limit {
                limit
            } else {
                congestion_controller.current_rate(TOKEN_BUCKET_WINDOW)
            };
            // Capacity = one TOKEN_BUCKET_WINDOW worth of tokens at the configured rate.
            // Previously 1MB, which allowed an immediate burst 800x larger
            // than FixedRate's per-100ms budget, triggering loss. See #3702.
            let bucket_capacity = (initial_rate / 10).max(MIN_BUCKET_CAPACITY_BYTES);
            let token_bucket = Arc::new(TokenBucket::new_with_time_source(
                bucket_capacity,
                initial_rate,
                time_source.clone(),
            ));

            let (inbound_packet_tx, inbound_packet_rx) =
                fast_channel::bounded(INBOUND_CHANNEL_CAPACITY);

            // Issue #2395: Drain any packets that arrived during the handshake.
            // The peer may send application-level messages (like ConnectRequest) immediately
            // after its side of the handshake completes, but before our handshake finishes.
            // These packets are buffered in `next_inbound` but only the ACK packet is read above.
            // Forward any remaining packets to the new connection channel so they're not lost.
            let mut forwarded_count = 0;
            while let Ok(packet) = next_inbound.try_recv() {
                if inbound_packet_tx.try_send(packet).is_err() {
                    tracing::warn!(
                        peer_addr = %remote_addr,
                        "Failed to forward handshake-buffered packet to connection channel (channel full)"
                    );
                    break;
                }
                forwarded_count += 1;
            }
            if forwarded_count > 0 {
                tracing::debug!(
                    peer_addr = %remote_addr,
                    forwarded_count,
                    "Forwarded packets buffered during handshake to connection channel"
                );
            }

            let remote_conn = RemoteConnection {
                outbound_symmetric_key: outbound_key,
                remote_addr,
                sent_tracker: sent_tracker.clone(),
                last_packet_id: Arc::new(AtomicU32::new(0)),
                inbound_packet_recv: inbound_packet_rx,
                inbound_symmetric_key: inbound_key,
                inbound_symmetric_key_bytes: inbound_key_bytes,
                global_bandwidth: global_bandwidth.clone(),
                my_address: None,
                transport_secret_key: secret,
                congestion_controller,
                token_bucket,
                socket,
                time_source,
            };

            let inbound_conn = InboundRemoteConnection {
                inbound_packet_sender: inbound_packet_tx,
            };

            tracing::debug!(
                peer_addr = %remote_addr,
                direction = "inbound",
                "Returning connection at gateway"
            );
            Ok((remote_conn, inbound_conn, outbound_ack_packet))
        };
        (f.boxed(), inbound_from_remote)
    }

    #[allow(clippy::type_complexity)]
    fn traverse_nat(
        &mut self,
        remote_addr: SocketAddr,
        remote_public_key: TransportPublicKey,
    ) -> (
        TraverseNatFuture<S, T>,
        FastSender<PacketData<UnknownEncryption>>,
    ) {
        tracing::debug!(
            peer_addr = %remote_addr,
            direction = "outbound",
            "Starting NAT traversal"
        );

        #[allow(clippy::large_enum_variant)]
        enum HandshakePhase {
            /// Initiated the outbound handshake by sending our intro packet
            /// Waiting for remote's intro packet
            StartOutbound,
            /// Received remote's intro, now sending symmetric ACK with our key
            RemoteInbound,
        }

        fn decrypt_asym(
            remote_addr: SocketAddr,
            packet: &PacketData<UnknownEncryption>,
            transport_secret_key: &TransportSecretKey,
            outbound_sym_key: &mut Option<Aes128Gcm>,
            state: &mut HandshakePhase,
        ) -> Result<(), ()> {
            if let Ok(decrypted_intro_packet) = packet.try_decrypt_asym(transport_secret_key) {
                tracing::debug!(
                    peer_addr = %remote_addr,
                    direction = "outbound",
                    "Received intro packet"
                );
                let protoc = &decrypted_intro_packet.data()[..PROTOC_VERSION.len()];
                if let Ok(protoc_bytes) = <[u8; 8]>::try_from(protoc) {
                    match version_cmp::is_compatible(&PROTOC_VERSION, &protoc_bytes) {
                        Ok(remote_info) => {
                            crate::transport::report_peer_version(remote_info.version);
                        }
                        Err(reason) => {
                            // Track version even on incompatible peers — aids version discovery.
                            let remote_info = version_cmp::parse_version_bytes(&protoc_bytes);
                            crate::transport::report_peer_version(remote_info.version);
                            if version_cmp::remote_requires_newer_than_us(
                                &PROTOC_VERSION,
                                &protoc_bytes,
                            ) {
                                crate::transport::signal_urgent_update();
                            }
                            tracing::debug!(
                                peer_addr = %remote_addr,
                                %reason,
                                direction = "outbound",
                                "Remote is using an incompatible protocol version"
                            );
                            return Err(());
                        }
                    }
                } else {
                    tracing::debug!(
                        peer_addr = %remote_addr,
                        direction = "outbound",
                        "Protocol version field wrong size"
                    );
                    return Err(());
                }
                let outbound_key_bytes =
                    &decrypted_intro_packet.data()[PROTOC_VERSION.len()..PROTOC_VERSION.len() + 16];
                let outbound_key =
                    Aes128Gcm::new_from_slice(outbound_key_bytes).expect("correct length");
                *outbound_sym_key = Some(outbound_key.clone());
                // Got remote's key, now we can send ACK with our key
                *state = HandshakePhase::RemoteInbound;
                return Ok(());
            }
            tracing::trace!(
                peer_addr = %remote_addr,
                direction = "outbound",
                "Failed to decrypt packet"
            );
            Err(())
        }

        let transport_secret_key = self.this_peer_keypair.secret.clone();
        let bandwidth_limit = self.bandwidth_limit;
        let global_bandwidth = self.global_bandwidth.clone();
        let _ledbat_min_ssthresh = self.ledbat_min_ssthresh;
        let socket = self.socket_listener.clone();
        let time_source = self.time_source.clone();
        let congestion_config = self.congestion_config.clone();
        let (inbound_from_remote, next_inbound) =
            fast_channel::bounded::<PacketData<UnknownEncryption>>(100);
        let this_addr = self.this_addr;
        let f = async move {
            tracing::info!(
                peer_addr = %remote_addr,
                direction = "outbound",
                "Starting outbound handshake (NAT traversal)"
            );
            let mut state = HandshakePhase::StartOutbound;
            let mut attempts = 0usize;
            let start_time_nanos = time_source.now_nanos();
            let overall_deadline = Duration::from_secs(3);

            // Generate symmetric key using GlobalRng for deterministic simulation
            let mut inbound_sym_key_bytes = [0u8; 16];
            GlobalRng::fill_bytes(&mut inbound_sym_key_bytes);
            let inbound_sym_key = Aes128Gcm::new(&inbound_sym_key_bytes.into());

            let mut outbound_sym_key: Option<Aes128Gcm> = None;
            let outbound_intro_packet = {
                let mut data = [0u8; { 16 + PROTOC_VERSION.len() }];
                data[..PROTOC_VERSION.len()].copy_from_slice(&PROTOC_VERSION);
                data[PROTOC_VERSION.len()..].copy_from_slice(&inbound_sym_key_bytes);
                PacketData::<_, MAX_PACKET_SIZE>::encrypt_with_pubkey(&data, &remote_public_key)
            };

            let mut sent_tracker = SentPacketTracker::new_with_time_source(time_source.clone());

            // NAT traversal: keep sending packets throughout the deadline to maintain NAT bindings.
            // The acceptor may start hole-punching well before the joiner, so we must keep
            // sending until the deadline expires, not just until we've sent MAX_ATTEMPTS packets.
            while time_source.now_nanos().saturating_sub(start_time_nanos)
                < overall_deadline.as_nanos() as u64
            {
                // Send a packet if we haven't exceeded the max attempts
                if attempts < NAT_TRAVERSAL_MAX_ATTEMPTS {
                    match state {
                        HandshakePhase::StartOutbound => {
                            tracing::trace!(
                                peer_addr = %remote_addr,
                                direction = "outbound",
                                "Sending protocol version and inbound key"
                            );
                            socket
                                .send_to(outbound_intro_packet.data(), remote_addr)
                                .await
                                .map_err(|_| TransportError::ChannelClosed)?;
                            attempts += 1;
                        }
                        HandshakePhase::RemoteInbound => {
                            tracing::trace!(
                                peer_addr = %remote_addr,
                                direction = "outbound",
                                "Sending back protocol version and inbound key to remote"
                            );
                            let our_inbound = SymmetricMessage::ack_ok(
                                outbound_sym_key.as_ref().expect("should be set"),
                                inbound_sym_key_bytes,
                                remote_addr,
                            )?;
                            socket
                                .send_to(our_inbound.data(), remote_addr)
                                .await
                                .map_err(|_| TransportError::ChannelClosed)?;
                            sent_tracker.report_sent_packet(
                                SymmetricMessage::FIRST_PACKET_ID,
                                our_inbound.data().into(),
                            );
                            attempts += 1;
                        }
                    }
                }
                let next_inbound_result = crate::deterministic_select! {
                    result = next_inbound.recv_async() => Some(result),
                    _ = time_source.sleep(Duration::from_millis(200)) => None,
                };
                match next_inbound_result {
                    Some(Ok(packet)) => {
                        tracing::trace!(
                            peer_addr = %remote_addr,
                            direction = "outbound",
                            "Received packet after sending it"
                        );
                        match state {
                            HandshakePhase::StartOutbound => {
                                tracing::trace!(
                                    peer_addr = %remote_addr,
                                    direction = "outbound",
                                    packet_len = packet.data().len(),
                                    "Received packet from remote"
                                );
                                if let Ok(decrypted_packet) =
                                    packet.try_decrypt_sym(&inbound_sym_key)
                                {
                                    // Remote decrypted our intro and is sending ACK with their key
                                    let symmetric_message =
                                        SymmetricMessage::deser(decrypted_packet.data())?;

                                    #[cfg(test)]
                                    {
                                        tracing::trace!(
                                            peer_addr = %remote_addr,
                                            direction = "outbound",
                                            payload = ?symmetric_message.payload,
                                            "Received symmetric packet"
                                        );
                                    }
                                    #[cfg(not(test))]
                                    {
                                        tracing::trace!(
                                            peer_addr = %remote_addr,
                                            direction = "outbound",
                                            "Received symmetric packet"
                                        );
                                    }

                                    match symmetric_message.payload {
                                        SymmetricMessagePayload::AckConnection {
                                            result:
                                                Ok(OutboundConnection {
                                                    key,
                                                    remote_addr: my_address,
                                                }),
                                        } => {
                                            let outbound_sym_key = Aes128Gcm::new_from_slice(&key)
                                                .map_err(|_| {
                                                    TransportError::ConnectionEstablishmentFailure {
                                                        cause: "invalid symmetric key".into(),
                                                    }
                                                })?;
                                            tracing::trace!(
                                                peer_addr = %remote_addr,
                                                direction = "outbound",
                                                "Sending back ack connection"
                                            );
                                            let ack_packet = SymmetricMessage::ack_ok(
                                                &outbound_sym_key,
                                                inbound_sym_key_bytes,
                                                remote_addr,
                                            )?;
                                            socket
                                                .send_to(ack_packet.data(), remote_addr)
                                                .await
                                                .map_err(|_| TransportError::ChannelClosed)?;
                                            let (inbound_sender, inbound_recv) =
                                                fast_channel::bounded(INBOUND_CHANNEL_CAPACITY);

                                            // Initialize congestion controller (BBR by default, configurable for benchmarks)
                                            let congestion_controller = congestion_config
                                                .clone()
                                                .unwrap_or_default()
                                                .build_arc_with_time_source(time_source.clone());

                                            // Initialize token bucket at congestion controller's
                                            // rate to avoid initial burst. See #3702.
                                            let initial_rate =
                                                if let Some(ref global) = global_bandwidth {
                                                    global.register_connection()
                                                } else if let Some(limit) = bandwidth_limit {
                                                    limit
                                                } else {
                                                    congestion_controller
                                                        .current_rate(TOKEN_BUCKET_WINDOW)
                                                };
                                            let bucket_capacity =
                                                (initial_rate / 10).max(MIN_BUCKET_CAPACITY_BYTES);
                                            let token_bucket =
                                                Arc::new(TokenBucket::new_with_time_source(
                                                    bucket_capacity,
                                                    initial_rate,
                                                    time_source.clone(),
                                                ));

                                            tracing::info!(
                                                peer_addr = %remote_addr,
                                                attempts,
                                                direction = "outbound",
                                                "Outbound handshake completed (ack path)"
                                            );
                                            return Ok((
                                                RemoteConnection {
                                                    outbound_symmetric_key: outbound_sym_key,
                                                    remote_addr,
                                                    sent_tracker: Arc::new(
                                                        parking_lot::Mutex::new(sent_tracker),
                                                    ),
                                                    last_packet_id: Arc::new(AtomicU32::new(0)),
                                                    inbound_packet_recv: inbound_recv,
                                                    inbound_symmetric_key: inbound_sym_key,
                                                    inbound_symmetric_key_bytes:
                                                        inbound_sym_key_bytes,
                                                    my_address: Some(my_address),
                                                    transport_secret_key: transport_secret_key
                                                        .clone(),
                                                    congestion_controller,
                                                    token_bucket,
                                                    socket: socket.clone(),
                                                    global_bandwidth: global_bandwidth.clone(),
                                                    time_source: time_source.clone(),
                                                },
                                                InboundRemoteConnection {
                                                    inbound_packet_sender: inbound_sender,
                                                },
                                            ));
                                        }
                                        SymmetricMessagePayload::AckConnection {
                                            result: Err(err),
                                        } => {
                                            return Err(handle_ack_connection_error(err));
                                        }
                                        SymmetricMessagePayload::ShortMessage { .. }
                                        | SymmetricMessagePayload::StreamFragment { .. }
                                        | SymmetricMessagePayload::NoOp
                                        | SymmetricMessagePayload::Ping { .. }
                                        | SymmetricMessagePayload::Pong { .. } => {
                                            tracing::trace!(
                                                peer_addr = %remote_addr,
                                                direction = "outbound",
                                                "Unexpected packet from remote"
                                            );
                                            continue;
                                        }
                                    }
                                }

                                // probably the first packet to punch through the NAT
                                if decrypt_asym(
                                    remote_addr,
                                    &packet,
                                    &transport_secret_key,
                                    &mut outbound_sym_key,
                                    &mut state,
                                )
                                .is_ok()
                                {
                                    continue;
                                }

                                tracing::trace!(
                                    peer_addr = %remote_addr,
                                    direction = "outbound",
                                    "Failed to decrypt packet"
                                );
                                continue;
                            }
                            HandshakePhase::RemoteInbound => {
                                // Handle repeated intro packet during NAT traversal
                                // or proceed if symmetric ack received
                                if packet.is_intro_packet() {
                                    tracing::trace!(
                                        peer_addr = %remote_addr,
                                        direction = "outbound",
                                        "Received repeated intro packet"
                                    );
                                    continue;
                                }
                                // if is not an intro packet, the connection is successful and we can proceed
                                let (inbound_sender, inbound_recv) =
                                    fast_channel::bounded(INBOUND_CHANNEL_CAPACITY);

                                // Initialize congestion controller (BBR by default, configurable for benchmarks)
                                let congestion_controller = congestion_config
                                    .clone()
                                    .unwrap_or_default()
                                    .build_arc_with_time_source(time_source.clone());

                                // Initialize token bucket at congestion controller's
                                // rate to avoid initial burst. See #3702.
                                let initial_rate = if let Some(ref global) = global_bandwidth {
                                    global.register_connection()
                                } else if let Some(limit) = bandwidth_limit {
                                    limit
                                } else {
                                    congestion_controller.current_rate(TOKEN_BUCKET_WINDOW)
                                };
                                let bucket_capacity =
                                    (initial_rate / 10).max(MIN_BUCKET_CAPACITY_BYTES);
                                let token_bucket = Arc::new(TokenBucket::new_with_time_source(
                                    bucket_capacity,
                                    initial_rate,
                                    time_source.clone(),
                                ));

                                tracing::info!(
                                    peer_addr = %remote_addr,
                                    attempts,
                                    direction = "outbound",
                                    "Outbound handshake completed (inbound ack path)"
                                );
                                return Ok((
                                    RemoteConnection {
                                        outbound_symmetric_key: outbound_sym_key
                                            .expect("should be set at this stage"),
                                        remote_addr,
                                        sent_tracker: Arc::new(parking_lot::Mutex::new(
                                            SentPacketTracker::new_with_time_source(
                                                time_source.clone(),
                                            ),
                                        )),
                                        last_packet_id: Arc::new(AtomicU32::new(0)),
                                        inbound_packet_recv: inbound_recv,
                                        inbound_symmetric_key: inbound_sym_key,
                                        inbound_symmetric_key_bytes: inbound_sym_key_bytes,
                                        my_address: None,
                                        transport_secret_key: transport_secret_key.clone(),
                                        congestion_controller,
                                        token_bucket,
                                        socket: socket.clone(),
                                        global_bandwidth: global_bandwidth.clone(),
                                        time_source: time_source.clone(),
                                    },
                                    InboundRemoteConnection {
                                        inbound_packet_sender: inbound_sender,
                                    },
                                ));
                            }
                        }
                    }
                    Some(Err(_)) => {
                        tracing::debug!(
                            bind_addr = %this_addr,
                            peer_addr = %remote_addr,
                            direction = "outbound",
                            "Connection closed"
                        );
                        return Err(TransportError::ConnectionClosed(remote_addr));
                    }
                    None => {
                        tracing::trace!(
                            bind_addr = %this_addr,
                            peer_addr = %remote_addr,
                            direction = "outbound",
                            "Failed to receive UDP response in time, retrying"
                        );
                    }
                }

                time_source.sleep(INITIAL_INTERVAL).await;
            }

            let elapsed_nanos = time_source.now_nanos().saturating_sub(start_time_nanos);
            let elapsed_ms = elapsed_nanos / 1_000_000;
            tracing::warn!(
                peer_addr = %remote_addr,
                attempts,
                elapsed_ms,
                direction = "outbound",
                "Outbound handshake failed: max connection attempts reached"
            );
            Err(TransportError::ConnectionEstablishmentFailure {
                cause: "max connection attempts reached".into(),
            })
        };
        (f.boxed(), inbound_from_remote)
    }
}

fn handle_ack_connection_error(err: Cow<'static, str>) -> TransportError {
    if let Some(expected) = err.split("expected version").nth(1) {
        let remote_version_str = expected.trim();
        // Check if this old peer's rejection actually means we need to update.
        // Old peers do strict exact-match. If they're compatible with our range
        // but just don't support range-checking, we should NOT self-update.
        if !version_cmp::old_peer_rejection_means_we_must_update(
            remote_version_str,
            &PROTOC_VERSION,
        ) {
            tracing::info!(
                remote_version = remote_version_str,
                "Old peer rejected us (strict match) but is within our compatible range — not triggering self-update"
            );
            return TransportError::ProtocolVersionMismatch {
                expected: remote_version_str.to_string(),
                actual: PCK_VERSION.to_string(),
            };
        }
        // Remote is genuinely newer — signal version mismatch
        crate::transport::signal_version_mismatch();
        TransportError::ProtocolVersionMismatch {
            expected: remote_version_str.to_string(),
            actual: PCK_VERSION.to_string(),
        }
    } else {
        TransportError::ConnectionEstablishmentFailure { cause: err }
    }
}

fn key_from_addr(addr: &SocketAddr) -> [u8; 16] {
    let current_time = chrono::Utc::now();
    let mut hasher = blake3::Hasher::new();
    match addr {
        SocketAddr::V4(v4) => {
            hasher.update(&v4.ip().octets());
            hasher.update(&v4.port().to_le_bytes());
        }
        SocketAddr::V6(v6) => {
            hasher.update(&v6.ip().octets());
            hasher.update(&v6.port().to_le_bytes());
        }
    }
    hasher.update(
        current_time
            .date_naive()
            .and_hms_opt(0, 0, 0)
            .unwrap()
            .and_utc()
            .timestamp()
            .to_le_bytes()
            .as_ref(),
    );
    RANDOM_U64.with(|&random_value| {
        hasher.update(random_value.as_ref());
    });
    hasher.finalize().as_bytes()[..16].try_into().unwrap()
}

pub(crate) enum ConnectionEvent<S = UdpSocket, TS: TimeSource = RealTime> {
    ConnectionStart {
        remote_public_key: TransportPublicKey,
        open_connection: oneshot::Sender<Result<RemoteConnection<S, TS>, TransportError>>,
    },
}

struct InboundRemoteConnection {
    inbound_packet_sender: FastSender<PacketData<UnknownEncryption>>,
}

mod version_cmp {
    use crate::config::{MIN_COMPATIBLE_VERSION, PCK_VERSION};

    /// New-format marker byte. Old peers see major=0xFF (!=their major) → clean reject.
    const NEW_FORMAT_MARKER: u8 = 0xFF;
    /// Format version byte in position 7. Old peers see reserved!=0 → clean reject.
    const FORMAT_VERSION: u8 = 1;

    /// Our version in the new 8-byte wire format.
    ///
    /// Layout: `[0xFF, major, minor, patch_hi, patch_lo, min_patch_hi, min_patch_lo, format=1]`
    ///
    /// Old peers (0.1.152 and earlier) used: `[major, minor, patch_u8, flags(4), reserved=0]`
    /// They see byte[0]=0xFF as major mismatch → clean ack_error. No crypto confusion.
    pub(super) const PROTOC_VERSION: [u8; 8] =
        encode_new_format(PCK_VERSION, MIN_COMPATIBLE_VERSION);

    /// Parsed version info from an 8-byte wire version field.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub(super) struct VersionInfo {
        pub version: (u8, u8, u16),
        /// None for old-format peers (they don't advertise min_compatible).
        pub min_compatible: Option<(u8, u8, u16)>,
    }

    impl std::fmt::Display for VersionInfo {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            let (maj, min, pat) = self.version;
            write!(f, "{maj}.{min}.{pat}")
        }
    }

    /// Encode version + min_compatible into the new 8-byte format.
    const fn encode_new_format(version: &str, min_compatible: &str) -> [u8; 8] {
        let (major, minor, patch) = parse_semver(version);
        let (_min_maj, _min_min, min_patch) = parse_semver(min_compatible);
        [
            NEW_FORMAT_MARKER,
            major,
            minor,
            (patch >> 8) as u8,
            patch as u8,
            (min_patch >> 8) as u8,
            min_patch as u8,
            FORMAT_VERSION,
        ]
    }

    /// Parse "X.Y.Z" or "X.Y.Z-prerelease" into (major, minor, patch as u16).
    const fn parse_semver(version: &str) -> (u8, u8, u16) {
        let mut major = 0u8;
        let mut minor = 0u8;
        let mut patch = 0u16;
        let mut state = 0u8; // 0: major, 1: minor, 2: patch

        let bytes = version.as_bytes();
        let mut i = 0;
        while i < bytes.len() {
            let c = bytes[i];
            if c == b'.' {
                state += 1;
            } else if c >= b'0' && c <= b'9' {
                let digit = (c - b'0') as u16;
                match state {
                    0 => major = (major as u16 * 10 + digit) as u8,
                    1 => minor = (minor as u16 * 10 + digit) as u8,
                    2 => patch = patch * 10 + digit,
                    _ => {}
                }
            } else {
                break; // Pre-release suffix — ignored for wire format.
            }
            i += 1;
        }
        (major, minor, patch)
    }

    /// Parse 8 version bytes. Detects old (byte[7]==0) vs new (byte[0]==0xFF, byte[7]!=0).
    pub(super) fn parse_version_bytes(bytes: &[u8; 8]) -> VersionInfo {
        if bytes[0] == NEW_FORMAT_MARKER && bytes[7] != 0 {
            // New format
            let major = bytes[1];
            let minor = bytes[2];
            let patch = u16::from_be_bytes([bytes[3], bytes[4]]);
            let min_patch = u16::from_be_bytes([bytes[5], bytes[6]]);
            VersionInfo {
                version: (major, minor, patch),
                min_compatible: Some((major, minor, min_patch)),
            }
        } else {
            // Old format: [major, minor, patch_u8, flags(4), reserved=0]
            let major = bytes[0];
            let minor = bytes[1];
            let patch = bytes[2] as u16;
            VersionInfo {
                version: (major, minor, patch),
                min_compatible: None,
            }
        }
    }

    /// Version tuple comparison: returns true if a >= b.
    /// Uses lexicographic tuple ordering, which matches semver precedence.
    pub(super) fn ver_ge(a: (u8, u8, u16), b: (u8, u8, u16)) -> bool {
        a >= b
    }

    /// Returns true if the remote's min_compatible exceeds our local version,
    /// meaning we MUST update to connect to this peer.
    pub(super) fn remote_requires_newer_than_us(local: &[u8; 8], remote_bytes: &[u8; 8]) -> bool {
        let remote_info = parse_version_bytes(remote_bytes);
        let local_info = parse_version_bytes(local);
        remote_info
            .min_compatible
            .is_some_and(|min| !ver_ge(local_info.version, min))
    }

    /// Bidirectional compatibility check.
    ///
    /// - Old remote (no min_compatible): check `remote_ver >= local_min_compatible`
    /// - New remote: check both directions
    pub(super) fn is_compatible(local: &[u8; 8], remote: &[u8; 8]) -> Result<VersionInfo, String> {
        let local_info = parse_version_bytes(local);
        let remote_info = parse_version_bytes(remote);

        // Check: remote version >= our min_compatible
        if let Some(local_min) = local_info.min_compatible {
            if !ver_ge(remote_info.version, local_min) {
                return Err(format!(
                    "remote {} too old for our min_compatible {}.{}.{}",
                    remote_info, local_min.0, local_min.1, local_min.2,
                ));
            }
        }

        // Check: our version >= remote's min_compatible (if remote is new-format)
        if let Some(remote_min) = remote_info.min_compatible {
            if !ver_ge(local_info.version, remote_min) {
                return Err(format!(
                    "our {} too old for remote's min_compatible {}.{}.{}",
                    local_info, remote_min.0, remote_min.1, remote_min.2,
                ));
            }
        }

        Ok(remote_info)
    }

    /// Check if a version mismatch from an old peer means WE need to update.
    ///
    /// When an old peer rejects us (they do strict exact-match), parse their
    /// version from the error. If their version >= our min_compatible, they're
    /// compatible with us — they just don't know it. We don't need to update.
    pub(super) fn old_peer_rejection_means_we_must_update(
        remote_version_str: &str,
        local: &[u8; 8],
    ) -> bool {
        let local_info = parse_version_bytes(local);
        let (rmaj, rmin, rpatch) = parse_semver(remote_version_str);

        if let Some(local_min) = local_info.min_compatible {
            // If remote < our min, we don't care about their rejection
            // (they're too old for us anyway)
            if !ver_ge((rmaj, rmin, rpatch), local_min) {
                return false;
            }
        }

        // Remote is compatible with us. Check if WE are compatible with them.
        // Old peers have strict matching, so their "min_compatible" is effectively
        // their exact version. If our version < their version, we may need to update.
        // But actually, an old peer rejects anyone != their exact version.
        // Since we're new-format and they're old-format, they'll always reject us.
        // The question is: should we update? Only if they're NEWER than us.
        !ver_ge(local_info.version, (rmaj, rmin, rpatch))
    }

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

        #[test]
        fn test_parse_semver() {
            assert_eq!(parse_semver("0.1.152"), (0, 1, 152));
            assert_eq!(parse_semver("0.1.256"), (0, 1, 256));
            assert_eq!(parse_semver("0.1.65535"), (0, 1, 65535));
            assert_eq!(parse_semver("1.2.3-alpha"), (1, 2, 3));
            assert_eq!(parse_semver("10.20.300"), (10, 20, 300));
        }

        #[test]
        fn test_new_format_roundtrip() {
            let encoded = encode_new_format("0.1.153", "0.1.152");
            let info = parse_version_bytes(&encoded);
            assert_eq!(info.version, (0, 1, 153));
            assert_eq!(info.min_compatible, Some((0, 1, 152)));
        }

        #[test]
        fn test_old_format_parsing() {
            // Old format: [major=0, minor=1, patch=152, flags(4), reserved=0]
            let old = [0u8, 1, 152, 0, 0, 0, 0, 0];
            let info = parse_version_bytes(&old);
            assert_eq!(info.version, (0, 1, 152));
            assert_eq!(info.min_compatible, None);
        }

        #[test]
        fn test_old_format_with_flags() {
            // Old format with pre-release flags: still old format because byte[7]=0
            let old = [0u8, 1, 152, 0x12, 0x34, 0x56, 0x78, 0];
            let info = parse_version_bytes(&old);
            assert_eq!(info.version, (0, 1, 152));
            assert_eq!(info.min_compatible, None);
        }

        #[test]
        fn test_new_accepts_old_compatible() {
            // New peer (0.1.153, min=0.1.152) accepts old peer (0.1.152)
            let new = encode_new_format("0.1.153", "0.1.152");
            let old = [0u8, 1, 152, 0, 0, 0, 0, 0];
            let result = is_compatible(&new, &old);
            assert!(result.is_ok(), "new should accept compatible old peer");
            assert_eq!(result.unwrap().version, (0, 1, 152));
        }

        #[test]
        fn test_new_rejects_old_incompatible() {
            // New peer (0.1.155, min=0.1.153) rejects old peer (0.1.152)
            let new = encode_new_format("0.1.155", "0.1.153");
            let old = [0u8, 1, 152, 0, 0, 0, 0, 0];
            let result = is_compatible(&new, &old);
            assert!(result.is_err(), "new should reject incompatible old peer");
        }

        #[test]
        fn test_new_to_new_bidirectional() {
            // Both new format, compatible
            let a = encode_new_format("0.1.155", "0.1.153");
            let b = encode_new_format("0.1.153", "0.1.152");
            assert!(is_compatible(&a, &b).is_ok());
            assert!(is_compatible(&b, &a).is_ok());
        }

        #[test]
        fn test_new_to_new_asymmetric_reject() {
            // A requires min 0.1.154, B is 0.1.153 → bidirectional check fails both ways
            // because B (0.1.153) < A's min (0.1.154)
            let a = encode_new_format("0.1.155", "0.1.154");
            let b = encode_new_format("0.1.153", "0.1.150");
            assert!(
                is_compatible(&a, &b).is_err(),
                "A should reject B (too old)"
            );
            assert!(
                is_compatible(&b, &a).is_err(),
                "B should also reject A (B knows it's too old for A's min)"
            );

            // One-way compatible: A requires min 0.1.152, B is 0.1.153 → both accept
            // B requires min 0.1.150, A is 0.1.155 → both accept
            let a2 = encode_new_format("0.1.155", "0.1.152");
            let b2 = encode_new_format("0.1.153", "0.1.150");
            assert!(is_compatible(&a2, &b2).is_ok(), "A2 should accept B2");
            assert!(is_compatible(&b2, &a2).is_ok(), "B2 should accept A2");
        }

        #[test]
        fn test_patch_256_and_above() {
            let v = encode_new_format("0.1.256", "0.1.200");
            let info = parse_version_bytes(&v);
            assert_eq!(info.version, (0, 1, 256));
            assert_eq!(info.min_compatible, Some((0, 1, 200)));

            let v2 = encode_new_format("0.1.65535", "0.1.1000");
            let info2 = parse_version_bytes(&v2);
            assert_eq!(info2.version, (0, 1, 65535));
            assert_eq!(info2.min_compatible, Some((0, 1, 1000)));
        }

        #[test]
        fn test_old_peer_rejection_analysis() {
            let local = encode_new_format("0.1.153", "0.1.152");

            // Old peer 0.1.152 rejects us — but 0.1.152 >= our min 0.1.152,
            // and 0.1.152 < 0.1.153 (our version), so we are newer. No update needed.
            assert!(
                !old_peer_rejection_means_we_must_update("0.1.152", &local),
                "should not trigger update when we are newer"
            );

            // Old peer 0.1.154 rejects us — they're newer. We should update.
            assert!(
                old_peer_rejection_means_we_must_update("0.1.154", &local),
                "should trigger update when remote is newer"
            );

            // Old peer 0.1.153 rejects us — same version. No update needed.
            assert!(
                !old_peer_rejection_means_we_must_update("0.1.153", &local),
                "should not trigger update for same version"
            );
        }

        #[test]
        fn test_protoc_version_is_new_format() {
            // Verify our compiled PROTOC_VERSION uses the new format
            assert_eq!(
                PROTOC_VERSION[0], NEW_FORMAT_MARKER,
                "byte 0 must be 0xFF marker"
            );
            assert_eq!(
                PROTOC_VERSION[7], FORMAT_VERSION,
                "byte 7 must be format version"
            );
        }

        #[test]
        fn test_remote_requires_newer_than_us() {
            // Remote requires min 0.1.154, we are 0.1.153 → we are too old
            let local = encode_new_format("0.1.153", "0.1.152");
            let remote = encode_new_format("0.1.155", "0.1.154");
            assert!(remote_requires_newer_than_us(&local, &remote));

            // Remote requires min 0.1.152, we are 0.1.153 → we are fine
            let remote2 = encode_new_format("0.1.155", "0.1.152");
            assert!(!remote_requires_newer_than_us(&local, &remote2));

            // Old-format remote (no min_compatible) → never triggers urgent
            let old_remote = [0u8, 1, 155, 0, 0, 0, 0, 0];
            assert!(!remote_requires_newer_than_us(&local, &old_remote));
        }

        #[test]
        fn test_ver_ge() {
            assert!(ver_ge((0, 1, 153), (0, 1, 152)));
            assert!(ver_ge((0, 1, 152), (0, 1, 152)));
            assert!(!ver_ge((0, 1, 151), (0, 1, 152)));
            assert!(ver_ge((0, 2, 0), (0, 1, 999)));
            assert!(ver_ge((1, 0, 0), (0, 255, 65535)));
        }

        /// Wire format only encodes min_patch, inheriting major.minor from the version.
        /// This is a deliberate constraint: during the 0.x era, all releases share
        /// major=0, minor=1. When major/minor changes, bump the format byte.
        #[test]
        fn test_min_compatible_shares_major_minor_with_version() {
            // min_compatible always gets the same major.minor as the version
            let v = encode_new_format("0.1.200", "0.1.150");
            let info = parse_version_bytes(&v);
            assert_eq!(info.version, (0, 1, 200));
            assert_eq!(info.min_compatible, Some((0, 1, 150)));

            // Even if min_compatible had a different major.minor in the source string,
            // only the patch is preserved (major.minor inherited from version).
            let v2 = encode_new_format("1.0.5", "0.1.3");
            let info2 = parse_version_bytes(&v2);
            assert_eq!(info2.version, (1, 0, 5));
            // min_compatible inherits (1, 0, _) from version, patch=3 from min_compat
            assert_eq!(info2.min_compatible, Some((1, 0, 3)));
        }

        /// Old peer well below our min_compatible should not trigger our self-update.
        #[test]
        fn test_old_peer_rejection_below_min_compatible() {
            let local = encode_new_format("0.1.153", "0.1.152");

            // Old peer 0.1.100 is WAY below our min_compatible (0.1.152).
            // Their rejection doesn't matter — they're too old for us anyway.
            assert!(
                !old_peer_rejection_means_we_must_update("0.1.100", &local),
                "should not trigger update for peer below our min_compatible"
            );
        }

        /// parse_semver with adversarial/malformed input.
        #[test]
        fn test_parse_semver_malformed() {
            // Empty string
            assert_eq!(parse_semver(""), (0, 0, 0));
            // Missing patch
            assert_eq!(parse_semver("0.1"), (0, 1, 0));
            // Non-numeric
            assert_eq!(parse_semver("abc"), (0, 0, 0));
            // Extra dots
            assert_eq!(parse_semver("0.1.2.3"), (0, 1, 2));
            // Leading zeros (parsed as decimal digits)
            assert_eq!(parse_semver("0.1.007"), (0, 1, 7));
        }

        /// Old-format always has byte[7]==0. New format always has byte[7]!=0.
        #[test]
        fn test_format_detection_byte7() {
            // Old format: byte[7] = 0
            let old = [0u8, 1, 152, 0, 0, 0, 0, 0];
            assert!(parse_version_bytes(&old).min_compatible.is_none());

            // New format: byte[7] = FORMAT_VERSION (1)
            let new = encode_new_format("0.1.153", "0.1.152");
            assert_eq!(new[7], FORMAT_VERSION);
            assert!(parse_version_bytes(&new).min_compatible.is_some());

            // Hypothetical old peer with major=255: byte[0]=0xFF, byte[7]=0
            // Should be parsed as OLD format (byte[7]==0)
            let old_255 = [0xFF, 1, 152, 0, 0, 0, 0, 0];
            let info = parse_version_bytes(&old_255);
            assert!(
                info.min_compatible.is_none(),
                "byte[0]=0xFF but byte[7]=0 → old format"
            );
            assert_eq!(info.version, (0xFF, 1, 152));
        }

        /// Both old-format peers: is_compatible always succeeds (no min_compatible check).
        #[test]
        fn test_both_old_format_always_compatible() {
            let a = [0u8, 1, 152, 0, 0, 0, 0, 0];
            let b = [0u8, 1, 151, 0, 0, 0, 0, 0];
            // Both old format — no range checking possible, both accept
            assert!(is_compatible(&a, &b).is_ok());
            assert!(is_compatible(&b, &a).is_ok());
        }

        /// Cross-major version peers are always incompatible because min_compatible
        /// inherits major.minor from the version (wire format only encodes min_patch).
        /// A 1.0.x peer's min_compatible is always (1, 0, _), which no 0.x peer satisfies.
        #[test]
        fn test_cross_major_version_always_incompatible() {
            let v1 = encode_new_format("1.0.5", "1.0.3");
            let v0 = encode_new_format("0.1.200", "0.1.150");
            // v0 (0.1.200) < v1's min (1.0.3) → v1 rejects v0
            assert!(
                is_compatible(&v1, &v0).is_err(),
                "v1 should reject v0 (cross-major incompatible)"
            );
            // v1 (1.0.5) >= v0's min (0.1.150) → v0 accepts v1, but v1 (1.0.5) satisfies
            // v0's min anyway. However, the bidirectional check means v0 also checks
            // v1's min: v0 (0.1.200) < (1.0.3) → reject.
            assert!(
                is_compatible(&v0, &v1).is_err(),
                "v0 should also reject v1 (v0's version < v1's min)"
            );
        }
    }

    /// Verifies that handshake completion is atomic - state transitions directly
    /// from GatewayHandshake to Established with no intermediate None state.
    #[test]
    fn test_atomic_handshake_completion_no_packet_loss() {
        use super::{ConnectionStateManager, fast_channel};
        use crate::simulation::VirtualTime;
        use crate::transport::packet_data::{PacketData, UnknownEncryption};
        use std::net::SocketAddr;
        use tokio::net::UdpSocket;

        let time = VirtualTime::new();
        let mut manager: ConnectionStateManager<UdpSocket, VirtualTime> =
            ConnectionStateManager::new(time);
        let addr: SocketAddr = "127.0.0.1:8000".parse().unwrap();

        // Create a channel for the handshake
        let (tx, _rx) = fast_channel::bounded::<PacketData<UnknownEncryption>>(16);

        // Start gateway handshake
        let started = manager.start_gateway_handshake(addr, tx);
        assert!(started, "Should start gateway handshake");
        assert!(
            manager.has_gateway_handshake(&addr),
            "Should be in GatewayHandshake state"
        );

        // Create channel for established connection
        let (established_tx, _established_rx) =
            fast_channel::bounded::<PacketData<UnknownEncryption>>(16);

        // Complete the handshake atomically
        let completed = manager.complete_gateway_handshake(addr, established_tx);
        assert!(completed, "Should complete gateway handshake");

        // Verify state is Established (not None) - proves no gap
        assert!(
            manager.is_established(&addr),
            "State must be Established immediately after completion - proves no race gap"
        );
        assert!(
            manager.get_state(&addr).is_some(),
            "get_state() must return Some immediately after transition"
        );
    }

    /// Proves RecentlyClosed state prevents misrouting packets to asymmetric decryption.
    ///
    /// Without RecentlyClosed, packets arriving after connection close would fall through
    /// to the asymmetric decryption handler (expensive and wrong).
    #[test]
    fn test_recently_closed_prevents_asymmetric_decryption() {
        use super::{
            ConnectionState, ConnectionStateManager, RECENTLY_CLOSED_DURATION, fast_channel,
        };
        use crate::simulation::VirtualTime;
        use crate::transport::packet_data::{PacketData, UnknownEncryption};
        use std::net::SocketAddr;
        use std::time::Duration;
        use tokio::net::UdpSocket;

        let time = VirtualTime::new();
        let mut manager: ConnectionStateManager<UdpSocket, VirtualTime> =
            ConnectionStateManager::new(time.clone());
        let addr: SocketAddr = "127.0.0.1:8001".parse().unwrap();

        // Create an established connection
        let (tx, _rx) = fast_channel::bounded::<PacketData<UnknownEncryption>>(16);
        manager.start_gateway_handshake(addr, tx);
        let (established_tx, _established_rx) =
            fast_channel::bounded::<PacketData<UnknownEncryption>>(16);
        manager.complete_gateway_handshake(addr, established_tx);
        assert!(manager.is_established(&addr));

        // Close the connection
        manager.mark_closed(&addr);

        // Verify state is RecentlyClosed (not removed)
        let state = manager.get_state(&addr);
        assert!(
            state.is_some(),
            "State should exist as RecentlyClosed, not be removed"
        );
        assert!(
            matches!(state, Some(ConnectionState::RecentlyClosed { .. })),
            "State must be RecentlyClosed - prevents packets from falling through"
        );

        // Verify it's not treated as unknown (which would trigger asymmetric decryption)
        assert!(!manager.is_established(&addr), "Should not be Established");
        assert!(
            manager.get_state(&addr).is_some(),
            "Should NOT return None - that would trigger asymmetric handler"
        );

        // Advance time past expiration and cleanup
        time.advance(RECENTLY_CLOSED_DURATION + Duration::from_millis(1));
        manager.cleanup_expired();

        // Now the state should be gone
        assert!(
            manager.get_state(&addr).is_none(),
            "State should be cleaned up after expiration"
        );
    }

    #[test]
    fn test_gateway_connection_rate_limiter() {
        use super::{
            GW_RAMP_PHASE1_DURATION, GW_RAMP_PHASE1_RATE, GW_RAMP_PHASE2_DURATION,
            GW_RAMP_PHASE2_RATE, GatewayConnectionRateLimiter,
        };
        use crate::simulation::VirtualTime;
        use std::time::Duration;

        let time = VirtualTime::new();
        let mut limiter = GatewayConnectionRateLimiter::new(time.clone());

        // Phase 1: should allow GW_RAMP_PHASE1_RATE connections per second
        for _ in 0..GW_RAMP_PHASE1_RATE {
            assert!(limiter.try_accept(), "Should accept within phase 1 rate");
        }
        // Next one should be rejected
        assert!(
            !limiter.try_accept(),
            "Should reject when phase 1 rate exceeded"
        );

        // Advance past 1 second — window should reset inside try_accept
        time.advance(Duration::from_millis(1001));
        assert!(
            limiter.try_accept(),
            "Should accept after window reset in phase 1"
        );

        // Advance into phase 2 (past phase 1 boundary + past current window).
        // Use the SAME limiter to verify try_accept handles long time gaps
        // and correctly resets its sliding window across phase transitions.
        time.advance(GW_RAMP_PHASE1_DURATION);
        assert_eq!(
            limiter.current_rate_limit(),
            Some(GW_RAMP_PHASE2_RATE),
            "Should be in phase 2 after advancing past phase 1 duration"
        );

        for _ in 0..GW_RAMP_PHASE2_RATE {
            assert!(limiter.try_accept(), "Should accept within phase 2 rate");
        }
        assert!(
            !limiter.try_accept(),
            "Should reject when phase 2 rate exceeded"
        );

        // Advance into unlimited phase (past phase 2 boundary).
        // Again, same limiter instance — validates window reset over long gaps.
        time.advance(GW_RAMP_PHASE2_DURATION);
        assert_eq!(
            limiter.current_rate_limit(),
            None,
            "Should be unlimited after phase 2 duration"
        );

        // Should accept unlimited connections
        for _ in 0..1000 {
            assert!(limiter.try_accept(), "Should accept unlimited in phase 3");
        }
    }
}

#[cfg(any(test, feature = "bench"))]
pub mod mock_transport {
    #![allow(clippy::single_range_in_vec_init)]

    use std::{
        fmt::Debug,
        net::Ipv4Addr,
        ops::Range,
        sync::atomic::{AtomicU16, AtomicU64, AtomicUsize, Ordering},
    };

    use dashmap::DashMap;
    use futures::{TryStreamExt, stream::FuturesOrdered};
    use rand::{Rng, SeedableRng};
    use serde::{Serialize, de::DeserializeOwned};
    use tokio::sync::Mutex;
    use tracing::info;

    use super::*;
    #[allow(unused_imports)] // Used in some test configurations
    use crate::transport::packet_data::MAX_DATA_SIZE;

    #[test]
    fn test_handle_ack_connection_error() {
        let err_msg =
            "remote is using a different protocol version, expected version 1.2.3".to_string();
        match handle_ack_connection_error(err_msg.into()) {
            TransportError::ProtocolVersionMismatch { expected, actual } => {
                assert_eq!(expected, "1.2.3");
                assert_eq!(actual, PCK_VERSION);
            }
            TransportError::ChannelClosed
            | TransportError::ConnectionClosed(_)
            | TransportError::ConnectionEstablishmentFailure { .. }
            | TransportError::SendFailed(..)
            | TransportError::IO(_)
            | TransportError::Other(_)
            | TransportError::PubKeyDecryptionError(_)
            | TransportError::Serialization(_) => panic!("Expected ProtocolVersionMismatch error"),
        }

        let err_msg_no_version = "remote is using a different protocol version".to_string();
        match handle_ack_connection_error(err_msg_no_version.clone().into()) {
            TransportError::ConnectionEstablishmentFailure { cause } => {
                assert_eq!(cause, err_msg_no_version);
            }
            TransportError::ChannelClosed
            | TransportError::ConnectionClosed(_)
            | TransportError::ProtocolVersionMismatch { .. }
            | TransportError::SendFailed(..)
            | TransportError::IO(_)
            | TransportError::Other(_)
            | TransportError::PubKeyDecryptionError(_)
            | TransportError::Serialization(_) => {
                panic!("Expected ConnectionEstablishmentFailure error")
            }
        }
    }

    /// Channel mapping for mock socket communication between peers.
    /// Packet format: (source_addr, data, available_at_nanos)
    /// available_at_nanos = VirtualTime when packet becomes available (0 = immediate)
    pub type Channels = Arc<DashMap<SocketAddr, mpsc::UnboundedSender<(SocketAddr, Vec<u8>, u64)>>>;

    /// Policy for simulating packet loss in mock transport.
    #[derive(Default, Clone)]
    pub enum PacketDropPolicy {
        /// Receive all packets without dropping
        #[default]
        ReceiveAll,
        /// Drop the packets randomly based on the factor (0.0 to 1.0)
        Factor(f64),
        /// Drop packets with the given packet index ranges
        Ranges(Vec<Range<usize>>),
    }

    /// Policy for simulating network delay in mock transport.
    #[derive(Clone, Default)]
    pub enum PacketDelayPolicy {
        /// No artificial delay (instant delivery)
        #[default]
        NoDelay,
        /// Fixed delay for all packets
        Fixed(Duration),
        /// Uniform random delay between min and max
        Uniform { min: Duration, max: Duration },
    }

    /// Mock socket implementation for testing transport without real network I/O.
    pub struct MockSocket {
        /// Inbound packets: (source_addr, data, available_at_nanos)
        inbound: Mutex<mpsc::UnboundedReceiver<(SocketAddr, Vec<u8>, u64)>>,
        this: SocketAddr,
        packet_drop_policy: PacketDropPolicy,
        packet_delay_policy: PacketDelayPolicy,
        num_packets_sent: AtomicUsize,
        rng: std::sync::Mutex<rand::rngs::SmallRng>,
        channels: Channels,
        /// Optional VirtualTime for instant delay simulation in benchmarks.
        /// When set, packet delivery waits until VirtualTime reaches available_at_nanos.
        virtual_time: Option<crate::simulation::VirtualTime>,
    }

    impl MockSocket {
        /// Create a new MockSocket with the given configuration.
        pub async fn new(
            packet_drop_policy: PacketDropPolicy,
            packet_delay_policy: PacketDelayPolicy,
            addr: SocketAddr,
            channels: Channels,
        ) -> Self {
            let (outbound, inbound) = mpsc::unbounded_channel();
            channels.insert(addr, outbound);
            static SEED: AtomicU64 = AtomicU64::new(0xfeedbeef);
            MockSocket {
                inbound: Mutex::new(inbound),
                this: addr,
                packet_drop_policy,
                packet_delay_policy,
                num_packets_sent: AtomicUsize::new(0),
                rng: std::sync::Mutex::new(rand::rngs::SmallRng::seed_from_u64(
                    SEED.fetch_add(1, std::sync::atomic::Ordering::SeqCst),
                )),
                channels,
                virtual_time: None,
            }
        }

        /// Create a new MockSocket with VirtualTime support for instant delay simulation.
        ///
        /// When `time_source` is provided, packet delays advance virtual time instead of
        /// waiting wall-clock time. This enables instant simulation of high-RTT scenarios.
        pub async fn with_time_source(
            packet_drop_policy: PacketDropPolicy,
            packet_delay_policy: PacketDelayPolicy,
            addr: SocketAddr,
            channels: Channels,
            time_source: Option<crate::simulation::VirtualTime>,
        ) -> Self {
            let (outbound, inbound) = mpsc::unbounded_channel();
            channels.insert(addr, outbound);
            static SEED: AtomicU64 = AtomicU64::new(0xfeedbeef);
            MockSocket {
                inbound: Mutex::new(inbound),
                this: addr,
                packet_drop_policy,
                packet_delay_policy,
                num_packets_sent: AtomicUsize::new(0),
                rng: std::sync::Mutex::new(rand::rngs::SmallRng::seed_from_u64(
                    SEED.fetch_add(1, std::sync::atomic::Ordering::SeqCst),
                )),
                channels,
                virtual_time: time_source,
            }
        }
    }

    impl Socket for MockSocket {
        async fn bind(_addr: SocketAddr) -> Result<Self, std::io::Error> {
            unimplemented!()
        }

        async fn recv_from(&self, buf: &mut [u8]) -> std::io::Result<(usize, SocketAddr)> {
            // Wait for a packet from the channel
            let Some((remote, packet, available_at_nanos)) =
                self.inbound.try_lock().unwrap().recv().await
            else {
                tracing::error!(this = %self.this, "connection closed");
                return Err(std::io::ErrorKind::ConnectionAborted.into());
            };

            // Wait until the packet is available (simulates transit delay)
            // This registers a VirtualTime wakeup that auto-advance will process
            if let Some(ref vt) = self.virtual_time {
                if available_at_nanos > 0 {
                    vt.sleep_until(available_at_nanos).await;
                }
            }

            // Deliver the packet
            buf[..packet.len()].copy_from_slice(&packet[..]);
            Ok((packet.len(), remote))
        }

        async fn send_to(&self, buf: &[u8], target: SocketAddr) -> std::io::Result<usize> {
            let (delay, available_at_nanos) = self.compute_delay_and_timestamp();

            // Send packet with delivery timestamp (don't advance VirtualTime here)
            let result = self.send_packet_internal(buf, target, available_at_nanos);

            // For non-VirtualTime with delay, use wall-clock sleep
            if self.virtual_time.is_none() && !delay.is_zero() {
                tokio::time::sleep(delay).await;
            } else {
                // Just yield to let receiver process
                tokio::task::yield_now().await;
            }

            result
        }

        fn send_to_blocking(&self, buf: &[u8], target: SocketAddr) -> std::io::Result<usize> {
            let (_delay, available_at_nanos) = self.compute_delay_and_timestamp();
            self.send_packet_internal(buf, target, available_at_nanos)
        }
    }

    impl MockSocket {
        /// Compute delay and delivery timestamp based on packet delay policy.
        fn compute_delay_and_timestamp(&self) -> (Duration, u64) {
            let delay = match &self.packet_delay_policy {
                PacketDelayPolicy::NoDelay => Duration::ZERO,
                PacketDelayPolicy::Fixed(delay) => *delay,
                PacketDelayPolicy::Uniform { min, max } => {
                    let range = max.as_nanos().saturating_sub(min.as_nanos());
                    if range == 0 {
                        *min
                    } else {
                        let random_nanos = {
                            let mut rng = self.rng.try_lock().unwrap();
                            rng.random::<u128>() % range
                        };
                        *min + Duration::from_nanos(random_nanos as u64)
                    }
                }
            };

            // Calculate delivery timestamp based on current VirtualTime + delay
            let available_at_nanos = if let Some(ref vt) = self.virtual_time {
                vt.now_nanos() + delay.as_nanos() as u64
            } else {
                0 // No VirtualTime, deliver immediately
            };

            (delay, available_at_nanos)
        }

        /// Internal packet send with explicit delivery timestamp.
        fn send_packet_internal(
            &self,
            buf: &[u8],
            target: SocketAddr,
            available_at_nanos: u64,
        ) -> std::io::Result<usize> {
            let packet_idx = self.num_packets_sent.fetch_add(1, Ordering::Release);
            match &self.packet_drop_policy {
                PacketDropPolicy::ReceiveAll => {}
                PacketDropPolicy::Factor(factor) => {
                    if *factor > self.rng.try_lock().unwrap().random::<f64>() {
                        tracing::trace!(id=%packet_idx, %self.this, "drop packet");
                        return Ok(buf.len());
                    }
                }
                PacketDropPolicy::Ranges(ranges) => {
                    if ranges.iter().any(|r| r.contains(&packet_idx)) {
                        tracing::trace!(id=%packet_idx, %self.this, "drop packet");
                        return Ok(buf.len());
                    }
                }
            }

            assert!(self.this != target, "cannot send to self");
            let Some(sender) = self.channels.get(&target).map(|v| v.value().clone()) else {
                return Ok(0);
            };
            sender
                .send((self.this, buf.to_vec(), available_at_nanos))
                .map_err(|_| std::io::ErrorKind::ConnectionAborted)?;
            Ok(buf.len())
        }
    }

    impl Drop for MockSocket {
        fn drop(&mut self) {
            self.channels.remove(&self.this);
        }
    }

    /// Create a mock peer connection for testing/benchmarking.
    ///
    /// Returns the peer's public key, outbound connection handler, and socket address.
    pub async fn create_mock_peer(
        packet_drop_policy: PacketDropPolicy,
        channels: Channels,
    ) -> anyhow::Result<(
        TransportPublicKey,
        OutboundConnectionHandler<MockSocket>,
        SocketAddr,
    )> {
        create_mock_peer_internal(
            packet_drop_policy,
            PacketDelayPolicy::NoDelay,
            false,
            channels,
            None,
        )
        .await
        .map(|(pk, (o, _), s)| (pk, o, s))
    }

    /// Create a mock peer connection with custom delay policy for testing/benchmarking.
    ///
    /// Returns the peer's public key, outbound connection handler, and socket address.
    pub async fn create_mock_peer_with_delay(
        packet_drop_policy: PacketDropPolicy,
        packet_delay_policy: PacketDelayPolicy,
        channels: Channels,
    ) -> anyhow::Result<(
        TransportPublicKey,
        OutboundConnectionHandler<MockSocket>,
        SocketAddr,
    )> {
        create_mock_peer_internal(
            packet_drop_policy,
            packet_delay_policy,
            false,
            channels,
            None,
        )
        .await
        .map(|(pk, (o, _), s)| (pk, o, s))
    }

    /// Create a mock peer connection with custom delay and bandwidth limit for testing/benchmarking.
    ///
    /// Returns the peer's public key, outbound connection handler, and socket address.
    pub async fn create_mock_peer_with_bandwidth(
        packet_drop_policy: PacketDropPolicy,
        packet_delay_policy: PacketDelayPolicy,
        channels: Channels,
        bandwidth_limit: Option<usize>,
    ) -> anyhow::Result<(
        TransportPublicKey,
        OutboundConnectionHandler<MockSocket>,
        SocketAddr,
    )> {
        create_mock_peer_internal(
            packet_drop_policy,
            packet_delay_policy,
            false,
            channels,
            bandwidth_limit,
        )
        .await
        .map(|(pk, (o, _), s)| (pk, o, s))
    }

    /// Create a mock peer connection with virtual time support for benchmarking.
    ///
    /// When `time_source` is provided, the entire transport stack uses VirtualTime
    /// for all timing operations. Delays advance virtual time instead of waiting
    /// real time. This enables instant simulation of high-RTT scenarios
    /// (e.g., 300ms RTT completes in ~1ms wall time).
    ///
    /// Returns the peer's public key, outbound connection handler, and socket address.
    pub async fn create_mock_peer_with_virtual_time(
        packet_drop_policy: PacketDropPolicy,
        packet_delay_policy: PacketDelayPolicy,
        channels: Channels,
        time_source: crate::simulation::VirtualTime,
    ) -> anyhow::Result<(
        TransportPublicKey,
        OutboundConnectionHandler<MockSocket, crate::simulation::VirtualTime>,
        SocketAddr,
    )> {
        create_mock_peer_internal_vt(
            packet_drop_policy,
            packet_delay_policy,
            false,
            channels,
            None,
            Some(time_source),
        )
        .await
        .map(|(pk, (o, _), s)| (pk, o, s))
    }

    /// Create a mock gateway connection for testing/benchmarking.
    ///
    /// Returns the gateway's public key, outbound handler, inbound connection receiver, and socket address.
    pub async fn create_mock_gateway(
        packet_drop_policy: PacketDropPolicy,
        channels: Channels,
    ) -> Result<
        (
            TransportPublicKey,
            (
                OutboundConnectionHandler<MockSocket>,
                mpsc::Receiver<PeerConnection<MockSocket>>,
            ),
            SocketAddr,
        ),
        anyhow::Error,
    > {
        create_mock_peer_internal(
            packet_drop_policy,
            PacketDelayPolicy::NoDelay,
            true,
            channels,
            None,
        )
        .await
    }

    async fn create_mock_peer_internal(
        packet_drop_policy: PacketDropPolicy,
        packet_delay_policy: PacketDelayPolicy,
        gateway: bool,
        channels: Channels,
        bandwidth_limit: Option<usize>,
    ) -> Result<
        (
            TransportPublicKey,
            (
                OutboundConnectionHandler<MockSocket>,
                mpsc::Receiver<PeerConnection<MockSocket>>,
            ),
            SocketAddr,
        ),
        anyhow::Error,
    > {
        static PORT: AtomicU16 = AtomicU16::new(25000);

        let peer_keypair = TransportKeypair::new();
        let peer_pub = peer_keypair.public.clone();
        let port = PORT.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        let socket = Arc::new(
            MockSocket::new(
                packet_drop_policy,
                packet_delay_policy,
                (Ipv4Addr::LOCALHOST, port).into(),
                channels,
            )
            .await,
        );
        let (peer_conn, inbound_conn) = OutboundConnectionHandler::new_test_with_bandwidth(
            (Ipv4Addr::LOCALHOST, port).into(),
            socket,
            peer_keypair,
            gateway,
            bandwidth_limit,
        )
        .expect("failed to create peer");
        Ok((
            peer_pub,
            (peer_conn, inbound_conn),
            (Ipv4Addr::LOCALHOST, port).into(),
        ))
    }

    /// Internal function with VirtualTime support for instant delay simulation.
    ///
    /// When time_source is provided, the entire transport stack uses VirtualTime
    /// for all timing operations (MockSocket delays, protocol timers, retransmit checks).
    async fn create_mock_peer_internal_vt(
        packet_drop_policy: PacketDropPolicy,
        packet_delay_policy: PacketDelayPolicy,
        gateway: bool,
        channels: Channels,
        bandwidth_limit: Option<usize>,
        time_source: Option<crate::simulation::VirtualTime>,
    ) -> Result<
        (
            TransportPublicKey,
            (
                OutboundConnectionHandler<MockSocket, crate::simulation::VirtualTime>,
                mpsc::Receiver<PeerConnection<MockSocket, crate::simulation::VirtualTime>>,
            ),
            SocketAddr,
        ),
        anyhow::Error,
    > {
        create_mock_peer_internal_vt_with_congestion(
            packet_drop_policy,
            packet_delay_policy,
            gateway,
            channels,
            bandwidth_limit,
            time_source,
            None,
        )
        .await
    }

    /// Internal function with VirtualTime and congestion control support.
    ///
    /// This is the most flexible internal peer creation function, supporting:
    /// - VirtualTime for instant delay simulation
    /// - Custom congestion control algorithm selection (BBR or LEDBAT)
    #[allow(clippy::too_many_arguments)]
    async fn create_mock_peer_internal_vt_with_congestion(
        packet_drop_policy: PacketDropPolicy,
        packet_delay_policy: PacketDelayPolicy,
        gateway: bool,
        channels: Channels,
        bandwidth_limit: Option<usize>,
        time_source: Option<crate::simulation::VirtualTime>,
        congestion_config: Option<CongestionControlConfig>,
    ) -> Result<
        (
            TransportPublicKey,
            (
                OutboundConnectionHandler<MockSocket, crate::simulation::VirtualTime>,
                mpsc::Receiver<PeerConnection<MockSocket, crate::simulation::VirtualTime>>,
            ),
            SocketAddr,
        ),
        anyhow::Error,
    > {
        static PORT: AtomicU16 = AtomicU16::new(25000);

        let peer_keypair = TransportKeypair::new();
        let peer_pub = peer_keypair.public.clone();
        let port = PORT.fetch_add(1, std::sync::atomic::Ordering::SeqCst);

        // Use provided time source or create a new one
        let vt = time_source.unwrap_or_default();

        let socket = Arc::new(
            MockSocket::with_time_source(
                packet_drop_policy,
                packet_delay_policy,
                (Ipv4Addr::LOCALHOST, port).into(),
                channels,
                Some(vt.clone()),
            )
            .await,
        );

        // Use the same VirtualTime for both MockSocket and protocol layer
        let (peer_conn, inbound_conn) = OutboundConnectionHandler::new_test_with_congestion_config(
            (Ipv4Addr::LOCALHOST, port).into(),
            socket,
            peer_keypair,
            gateway,
            bandwidth_limit,
            vt,
            congestion_config,
        )
        .expect("failed to create peer");
        Ok((
            peer_pub,
            (peer_conn, inbound_conn),
            (Ipv4Addr::LOCALHOST, port).into(),
        ))
    }

    /// Create a mock peer connection with VirtualTime and custom congestion control.
    ///
    /// This function supports selecting the congestion control algorithm (BBR or LEDBAT)
    /// for benchmarking purposes. Pass `None` for `congestion_config` to use the default (BBR).
    ///
    /// Returns the peer's public key, outbound connection handler, and socket address.
    pub async fn create_mock_peer_with_congestion_config(
        packet_drop_policy: PacketDropPolicy,
        packet_delay_policy: PacketDelayPolicy,
        channels: Channels,
        time_source: crate::simulation::VirtualTime,
        congestion_config: Option<CongestionControlConfig>,
    ) -> anyhow::Result<(
        TransportPublicKey,
        OutboundConnectionHandler<MockSocket, crate::simulation::VirtualTime>,
        SocketAddr,
    )> {
        create_mock_peer_internal_vt_with_congestion(
            packet_drop_policy,
            packet_delay_policy,
            false,
            channels,
            None,
            Some(time_source),
            congestion_config,
        )
        .await
        .map(|(pk, (o, _), s)| (pk, o, s))
    }

    // Test infrastructure for multi-peer communication tests (reserved for future use)
    #[allow(dead_code)]
    trait TestFixture: Clone + Send + Sync + 'static {
        type Message: DeserializeOwned + Serialize + Send + Debug + 'static;
        fn expected_iterations(&self) -> usize;
        fn gen_msg(&mut self) -> Self::Message;
        fn assert_message_ok(&self, peer_idx: usize, msg: Self::Message) -> bool;
    }

    #[allow(dead_code)]
    struct TestConfig {
        packet_drop_policy: PacketDropPolicy,
        peers: usize,
        wait_time: Duration,
    }

    impl Default for TestConfig {
        fn default() -> Self {
            Self {
                packet_drop_policy: PacketDropPolicy::ReceiveAll,
                peers: 2,
                wait_time: Duration::from_secs(10), // Increased for CI reliability
            }
        }
    }

    #[allow(dead_code)]
    async fn run_test<G: TestFixture>(
        config: TestConfig,
        generators: Vec<G>,
    ) -> anyhow::Result<()> {
        assert_eq!(generators.len(), config.peers);
        let mut peer_keys_and_addr = vec![];
        let mut peer_conns = vec![];
        let channels = Arc::new(DashMap::new());
        for _ in 0..config.peers {
            let (peer_pub, peer, peer_addr) =
                create_mock_peer(config.packet_drop_policy.clone(), channels.clone()).await?;
            peer_keys_and_addr.push((peer_pub, peer_addr));
            peer_conns.push(peer);
        }

        let mut tasks = vec![];
        let barrier = Arc::new(tokio::sync::Barrier::new(config.peers));
        for (i, (mut peer, test_generator)) in peer_conns.into_iter().zip(generators).enumerate() {
            let mut peer_keys_and_addr = peer_keys_and_addr.clone();
            peer_keys_and_addr.remove(i);
            let barrier_cp = barrier.clone();
            let peer = GlobalExecutor::spawn(async move {
                let mut conns = FuturesOrdered::new();
                let mut establish_conns = Vec::new();
                barrier_cp.wait().await;
                for (peer_pub, peer_addr) in &peer_keys_and_addr {
                    let peer_conn = tokio::time::timeout(
                        Duration::from_secs(30), // Increased connection timeout for CI reliability
                        peer.connect(peer_pub.clone(), *peer_addr).await,
                    );
                    establish_conns.push(peer_conn);
                }
                let connections = futures::future::try_join_all(establish_conns)
                    .await?
                    .into_iter()
                    .collect::<Result<Vec<_>, _>>()?;
                // additional wait time so we can clear up any additional messages that may need to be sent
                let extra_wait = if config.wait_time.as_secs() > 10 {
                    Duration::from_secs(3)
                } else {
                    Duration::from_millis(200)
                };
                for ((_, peer_addr), mut peer_conn) in
                    peer_keys_and_addr.into_iter().zip(connections)
                {
                    let mut test_gen_cp = test_generator.clone();
                    conns.push_back(async move {
                        let mut messages = vec![];
                        let mut to = tokio::time::interval(config.wait_time);
                        to.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
                        to.tick().await;
                        let start = tokio::time::Instant::now();
                        let iters = test_gen_cp.expected_iterations();
                        while messages.len() < iters {
                            peer_conn.send(test_gen_cp.gen_msg()).await?;
                            let msg = tokio::select! {
                                _ = to.tick() => {
                                    return Err::<_, anyhow::Error>(
                                        anyhow::anyhow!(
                                            "timeout waiting for messages, total time: {time:.2}; done iters {iter}",
                                            time = start.elapsed().as_secs_f64(),
                                            iter = messages.len()
                                        )
                                    );
                                }
                                msg = peer_conn.recv() => {
                                    msg
                                }
                            };
                            match msg {
                                Ok(msg) => {
                                    let output_as_str: G::Message = bincode::deserialize(&msg)?;
                                    messages.push(output_as_str);
                                    info!("{peer_addr:?} received {} messages", messages.len());
                                }
                                Err(error) => {
                                    tracing::error!(%error, "error receiving message");
                                    return Err(error.into());
                                }
                            }
                        }
                        let _sent = peer_conn.send(test_gen_cp.gen_msg()).await;

                        tracing::info!(%peer_addr, "finished");
                        let _recv = tokio::time::timeout(extra_wait, peer_conn.recv()).await;
                        Ok(messages)
                    });
                }
                let results = conns.try_collect::<Vec<_>>().await?;
                Ok::<_, anyhow::Error>((results, test_generator))
            });
            tasks.push(peer);
        }

        let all_results = futures::future::try_join_all(tasks)
            .await?
            .into_iter()
            .collect::<Result<Vec<_>, _>>()?;
        for (peer_results, test_gen) in all_results {
            for (idx, result) in peer_results.into_iter().enumerate() {
                assert_eq!(result.len(), test_gen.expected_iterations());
                for msg in result {
                    assert!(test_gen.assert_message_ok(idx, msg));
                }
            }
        }

        Ok(())
    }

    #[tokio::test]
    async fn simulate_nat_traversal() -> anyhow::Result<()> {
        let channels = Arc::new(DashMap::new());
        let (peer_a_pub, mut peer_a, peer_a_addr) =
            create_mock_peer(Default::default(), channels.clone()).await?;
        let (peer_b_pub, mut peer_b, peer_b_addr) =
            create_mock_peer(Default::default(), channels).await?;

        let peer_b = GlobalExecutor::spawn(async move {
            let peer_a_conn = peer_b.connect(peer_a_pub, peer_a_addr).await;
            let _conn = tokio::time::timeout(Duration::from_secs(500), peer_a_conn).await??;
            Ok::<_, anyhow::Error>(())
        });

        let peer_a = GlobalExecutor::spawn(async move {
            let peer_b_conn = peer_a.connect(peer_b_pub, peer_b_addr).await;
            let _conn = tokio::time::timeout(Duration::from_secs(500), peer_b_conn).await??;
            Ok::<_, anyhow::Error>(())
        });

        let (a, b) = tokio::try_join!(peer_a, peer_b)?;
        a?;
        b?;
        Ok(())
    }

    #[ignore = "should be fixed"]
    #[tokio::test]
    async fn simulate_nat_traversal_drop_first_packets_for_all() -> anyhow::Result<()> {
        let channels = Arc::new(DashMap::new());
        let (peer_a_pub, mut peer_a, peer_a_addr) =
            create_mock_peer(PacketDropPolicy::Ranges(vec![0..1]), channels.clone()).await?;
        let (peer_b_pub, mut peer_b, peer_b_addr) =
            create_mock_peer(PacketDropPolicy::Ranges(vec![0..1]), channels).await?;

        let peer_b = GlobalExecutor::spawn(async move {
            let peer_a_conn = peer_b.connect(peer_a_pub, peer_a_addr).await;
            let _conn = tokio::time::timeout(Duration::from_secs(500), peer_a_conn).await??;
            Ok::<_, anyhow::Error>(())
        });

        let peer_a = GlobalExecutor::spawn(async move {
            let peer_b_conn = peer_a.connect(peer_b_pub, peer_b_addr).await;
            let _conn = tokio::time::timeout(Duration::from_secs(500), peer_b_conn).await??;
            Ok::<_, anyhow::Error>(())
        });

        let (a, b) = tokio::try_join!(peer_a, peer_b)?;
        a?;
        b?;
        Ok(())
    }

    #[tokio::test]
    async fn simulate_nat_traversal_drop_first_packets_of_peerb() -> anyhow::Result<()> {
        let channels = Arc::new(DashMap::new());
        let (peer_a_pub, mut peer_a, peer_a_addr) =
            create_mock_peer(Default::default(), channels.clone()).await?;
        let (peer_b_pub, mut peer_b, peer_b_addr) =
            create_mock_peer(PacketDropPolicy::Ranges(vec![0..1]), channels).await?;

        let peer_b = GlobalExecutor::spawn(async move {
            let peer_a_conn = peer_b.connect(peer_a_pub, peer_a_addr).await;
            let mut conn = tokio::time::timeout(Duration::from_secs(500), peer_a_conn).await??;
            let _recv = tokio::time::timeout(Duration::from_secs(3), conn.recv()).await;
            Ok::<_, anyhow::Error>(())
        });

        let peer_a = GlobalExecutor::spawn(async move {
            let peer_b_conn = peer_a.connect(peer_b_pub, peer_b_addr).await;
            let mut conn = tokio::time::timeout(Duration::from_secs(500), peer_b_conn).await??;
            let _recv = tokio::time::timeout(Duration::from_secs(3), conn.recv()).await;
            Ok::<_, anyhow::Error>(())
        });

        let (a, b) = tokio::try_join!(peer_a, peer_b)?;
        a?;
        b?;
        Ok(())
    }

    #[tokio::test]
    async fn simulate_nat_traversal_drop_packet_ranges_of_peerb_killed() -> anyhow::Result<()> {
        let channels = Arc::new(DashMap::new());
        let (peer_a_pub, mut peer_a, peer_a_addr) =
            create_mock_peer(Default::default(), channels.clone()).await?;
        let (peer_b_pub, mut peer_b, peer_b_addr) = create_mock_peer(
            PacketDropPolicy::Ranges(vec![0..1, 3..usize::MAX]),
            channels,
        )
        .await?;

        let peer_b = GlobalExecutor::spawn(async move {
            let peer_a_conn = peer_b.connect(peer_a_pub, peer_a_addr).await;
            let mut conn = tokio::time::timeout(Duration::from_secs(2), peer_a_conn).await??;
            conn.send("some data").await.inspect_err(|error| {
                tracing::error!(%error, "error while sending message to peer a");
            })?;
            tracing::info!("Dropping peer b");
            Ok::<_, anyhow::Error>(())
        });

        let peer_a = GlobalExecutor::spawn(async move {
            let peer_b_conn = peer_a.connect(peer_b_pub, peer_b_addr).await;
            let mut conn = tokio::time::timeout(Duration::from_secs(2), peer_b_conn).await??;
            let b = tokio::time::timeout(Duration::from_secs(2), conn.recv()).await??;
            // we should receive the message
            assert_eq!(&b[8..], b"some data");
            tracing::info!("Peer a received package from peer b");
            tokio::time::sleep(Duration::from_secs(3)).await;
            // conn should be broken as the remote peer cannot receive message and ping
            // Use timeout to avoid hanging if connection detection is slow
            let res = tokio::time::timeout(Duration::from_secs(10), conn.recv()).await;
            if res.is_err() {
                tracing::error!(
                    "Test timeout: connection detection took longer than 10s - investigate if this causes test failures"
                );
            }
            assert!(res.is_err() || res.unwrap().is_err());
            Ok::<_, anyhow::Error>(())
        });

        let (a, b) = tokio::try_join!(peer_a, peer_b)?;
        a?;
        b?;

        Ok(())
    }

    #[tokio::test]
    async fn simulate_nat_traversal_drop_packet_ranges_of_peerb() -> anyhow::Result<()> {
        let channels = Arc::new(DashMap::new());
        let (peer_a_pub, mut peer_a, peer_a_addr) =
            create_mock_peer(Default::default(), channels.clone()).await?;
        // Drop pattern: packet 0 dropped (retry succeeds at count 1), packets 1-2 succeed
        // (backoff resets), packets 3-4 dropped (retry succeeds at count 5).
        // With RFC 6298 exponential backoff: 1s + 2s = 3s for the 3..5 range.
        // Original 3..9 (6 consecutive drops) would take 1+2+4+8+16+32=63s, exceeding
        // reasonable connection timeout - that scenario should fail, not recover.
        let (peer_b_pub, mut peer_b, peer_b_addr) =
            create_mock_peer(PacketDropPolicy::Ranges(vec![0..1, 3..5]), channels).await?;

        let peer_b = GlobalExecutor::spawn(async move {
            let peer_a_conn = peer_b.connect(peer_a_pub, peer_a_addr).await;
            let mut conn = tokio::time::timeout(Duration::from_secs(15), peer_a_conn)
                .await
                .inspect_err(|_| tracing::error!("peer a timed out"))?
                .inspect_err(|error| tracing::error!(%error, "error while connecting to peer a"))?;
            tracing::info!("Connected peer b to peer a");
            conn.send("some foo").await.inspect_err(|error| {
                tracing::error!(%error, "error while sending 1st message");
            })?;
            tokio::time::sleep(Duration::from_secs(2)).await;
            // although we drop some packets, we still alive
            conn.send("some data").await.inspect_err(|error| {
                tracing::error!(%error, "error while sending 2nd message");
            })?;
            let _recv = tokio::time::timeout(Duration::from_secs(10), conn.recv()).await;
            Ok::<_, anyhow::Error>(conn)
        });

        let peer_a = GlobalExecutor::spawn(async move {
            let peer_b_conn = peer_a.connect(peer_b_pub, peer_b_addr).await;
            let mut conn = tokio::time::timeout(Duration::from_secs(15), peer_b_conn)
                .await
                .inspect_err(|_| tracing::error!("peer b timed out"))?
                .inspect_err(|error| tracing::error!(%error, "error while connecting to peer b"))?;
            tracing::info!("Connected peer a to peer b");

            // we should receive the message
            let b = conn.recv().await.inspect_err(|error| {
                tracing::error!(%error, "error while receiving 1st message");
            })?;
            assert_eq!(&b[8..], b"some foo");
            // conn should not be broken
            let b = conn.recv().await.inspect_err(|error| {
                tracing::error!(%error, "error while receiving 2nd message");
            })?;
            assert_eq!(&b[8..], b"some data");
            let _sent = conn.send("complete").await.inspect_err(|error| {
                tracing::error!(%error, "error while sending 3rd message");
            });
            Ok::<_, anyhow::Error>(conn)
        });

        let (a, b) = tokio::try_join!(peer_a, peer_b)?;
        let _a = a?;
        let _b = b?;

        Ok(())
    }

    #[tokio::test]
    async fn simulate_gateway_connection() -> anyhow::Result<()> {
        let channels = Arc::new(DashMap::new());
        let (_peer_a_pub, mut peer_a, _peer_a_addr) =
            create_mock_peer(Default::default(), channels.clone()).await?;
        let (gw_pub, (_oc, mut gw_conn), gw_addr) =
            create_mock_gateway(Default::default(), channels).await?;

        let gw = GlobalExecutor::spawn(async move {
            let gw_conn = gw_conn.recv();
            let _gw = tokio::time::timeout(Duration::from_secs(10), gw_conn)
                .await?
                .ok_or(anyhow::anyhow!("no connection"))?;
            Ok::<_, anyhow::Error>(())
        });

        let peer_a = GlobalExecutor::spawn(async move {
            let peer_b_conn = peer_a.connect(gw_pub, gw_addr).await;
            let _conn = tokio::time::timeout(Duration::from_secs(60), peer_b_conn).await??;
            Ok::<_, anyhow::Error>(())
        });

        let (a, b) = tokio::try_join!(peer_a, gw)?;
        a?;
        b?;
        Ok(())
    }

    #[ignore = "should be fixed"]
    #[tokio::test]
    async fn simulate_gateway_connection_drop_first_packets_of_gateway() -> anyhow::Result<()> {
        let channels = Arc::new(DashMap::new());
        let (_peer_a_pub, mut peer_a, _peer_a_addr) =
            create_mock_peer(Default::default(), channels.clone()).await?;
        let (gw_pub, (_oc, mut gw_conn), gw_addr) =
            create_mock_gateway(PacketDropPolicy::Ranges(vec![0..1]), channels.clone()).await?;

        let gw = GlobalExecutor::spawn(async move {
            let gw_conn = gw_conn.recv();
            let _gw = tokio::time::timeout(Duration::from_secs(10), gw_conn)
                .await?
                .ok_or(anyhow::anyhow!("no connection"))?;
            Ok::<_, anyhow::Error>(())
        });

        let peer_a = GlobalExecutor::spawn(async move {
            let peer_b_conn = peer_a.connect(gw_pub, gw_addr).await;
            let _conn = tokio::time::timeout(Duration::from_secs(500), peer_b_conn).await??;
            Ok::<_, anyhow::Error>(())
        });

        let (a, b) = tokio::try_join!(peer_a, gw)?;
        a?;
        b?;
        Ok(())
    }

    #[ignore = "should be fixed"]
    #[tokio::test]
    async fn simulate_gateway_connection_drop_first_packets_for_all() -> anyhow::Result<()> {
        let channels = Arc::new(DashMap::new());
        let (_peer_a_pub, mut peer_a, _peer_a_addr) =
            create_mock_peer(PacketDropPolicy::Ranges(vec![0..1]), channels.clone()).await?;
        let (gw_pub, (_oc, mut gw_conn), gw_addr) =
            create_mock_gateway(PacketDropPolicy::Ranges(vec![0..1]), channels).await?;

        let gw = GlobalExecutor::spawn(async move {
            let gw_conn = gw_conn.recv();
            let _gw = tokio::time::timeout(Duration::from_secs(10), gw_conn)
                .await?
                .ok_or(anyhow::anyhow!("no connection"))?;
            Ok::<_, anyhow::Error>(())
        });

        let peer_a = GlobalExecutor::spawn(async move {
            let peer_b_conn = peer_a.connect(gw_pub, gw_addr).await;
            let _conn = tokio::time::timeout(Duration::from_secs(10), peer_b_conn).await??;
            Ok::<_, anyhow::Error>(())
        });

        let (a, b) = tokio::try_join!(peer_a, gw)?;
        a?;
        b?;
        Ok(())
    }

    #[tokio::test]
    async fn simulate_gateway_connection_drop_first_packets_of_peer() -> anyhow::Result<()> {
        let channels = Arc::new(DashMap::new());
        let (_peer_a_pub, mut peer_a, _peer_a_addr) =
            create_mock_peer(PacketDropPolicy::Ranges(vec![0..1]), channels.clone()).await?;
        let (gw_pub, (_oc, mut gw_conn), gw_addr) =
            create_mock_gateway(Default::default(), channels).await?;

        let gw = GlobalExecutor::spawn(async move {
            let gw_conn = gw_conn.recv();
            let _gw = tokio::time::timeout(Duration::from_secs(10), gw_conn)
                .await?
                .ok_or(anyhow::anyhow!("no connection"))?;
            Ok::<_, anyhow::Error>(())
        });

        let peer_a = GlobalExecutor::spawn(async move {
            let peer_b_conn = peer_a.connect(gw_pub, gw_addr).await;
            let _conn = tokio::time::timeout(Duration::from_secs(10), peer_b_conn).await??;
            Ok::<_, anyhow::Error>(())
        });

        let (a, b) = tokio::try_join!(peer_a, gw)?;
        a?;
        b?;
        Ok(())
    }

    #[tokio::test]
    #[ignore = "Flaky in CI - connection to remote closed errors"]
    async fn simulate_send_short_message() -> anyhow::Result<()> {
        #[derive(Clone, Copy)]
        struct TestData(&'static str);

        impl TestFixture for TestData {
            type Message = String;
            fn expected_iterations(&self) -> usize {
                10
            }

            fn gen_msg(&mut self) -> Self::Message {
                self.0.to_string()
            }

            fn assert_message_ok(&self, _peer_idx: usize, msg: Self::Message) -> bool {
                msg == "foo"
            }
        }

        run_test(
            TestConfig {
                peers: 10,
                wait_time: Duration::from_secs(60), // Increased timeout for CI reliability
                ..Default::default()
            },
            Vec::from_iter((0..10).map(|_| TestData("foo"))),
        )
        .await
    }

    /// Tests sending a large short message using the high-level send() API.
    /// Uses a simple connection pattern for reliability.
    ///
    /// The max short message size is MAX_DATA_SIZE - SymmetricMessage::short_message_overhead().
    /// We test with a payload just under MAX_DATA_SIZE to verify short message handling
    /// near the boundary. The payload must fit in MAX_DATA_SIZE minus serialization and
    /// SymmetricMessage overhead.
    #[tokio::test]
    async fn simulate_send_max_short_message() -> anyhow::Result<()> {
        let channels = Arc::new(DashMap::new());
        let (peer_a_pub, mut peer_a, peer_a_addr) =
            create_mock_peer(Default::default(), channels.clone()).await?;
        let (peer_b_pub, mut peer_b, peer_b_addr) =
            create_mock_peer(Default::default(), channels).await?;

        // Use a large payload that will be sent as a short message.
        // Must be small enough that bincode serialization + SymmetricMessage overhead
        // fits within MAX_DATA_SIZE (currently 1171 bytes).
        let test_data: Vec<u8> = (0..1000).map(|i| (i % 256) as u8).collect();
        let expected_len = test_data.len();

        let peer_a = GlobalExecutor::spawn(async move {
            let peer_b_conn = peer_a.connect(peer_b_pub, peer_b_addr).await;
            let mut conn = tokio::time::timeout(Duration::from_secs(5), peer_b_conn).await??;
            let msg = tokio::time::timeout(Duration::from_secs(5), conn.recv()).await??;
            let deserialized: Vec<u8> = bincode::deserialize(&msg)?;
            assert_eq!(deserialized.len(), expected_len);
            Ok::<_, anyhow::Error>(())
        });

        let peer_b = GlobalExecutor::spawn(async move {
            let peer_a_conn = peer_b.connect(peer_a_pub, peer_a_addr).await;
            let mut conn = tokio::time::timeout(Duration::from_secs(5), peer_a_conn).await??;
            // Small delay to ensure peer_a's recv() is ready before we send.
            // Without this, the send can complete before peer_a starts receiving,
            // causing the message to be lost in the mock transport.
            tokio::time::sleep(Duration::from_millis(100)).await;
            conn.send(test_data).await?;
            Ok::<_, anyhow::Error>(())
        });

        let (a, b) = tokio::join!(peer_a, peer_b);
        a??;
        b??;
        Ok(())
    }

    /// Tests that sending a message larger than MAX_DATA_SIZE fails as expected.
    #[tokio::test]
    async fn simulate_send_max_short_message_plus_1() -> anyhow::Result<()> {
        let channels = Arc::new(DashMap::new());
        let (peer_a_pub, mut peer_a, peer_a_addr) =
            create_mock_peer(Default::default(), channels.clone()).await?;
        let (peer_b_pub, mut peer_b, peer_b_addr) =
            create_mock_peer(Default::default(), channels).await?;

        let peer_a = GlobalExecutor::spawn(async move {
            let peer_b_conn = peer_a.connect(peer_b_pub, peer_b_addr).await;
            let mut conn = tokio::time::timeout(Duration::from_secs(1), peer_b_conn).await??;
            let _recv = tokio::time::timeout(Duration::from_secs(1), conn.recv()).await??;
            Ok::<_, anyhow::Error>(())
        });

        let peer_b = GlobalExecutor::spawn(async move {
            let peer_a_conn = peer_b.connect(peer_a_pub, peer_a_addr).await;
            let mut conn = tokio::time::timeout(Duration::from_secs(1), peer_a_conn).await??;
            let data = vec![0u8; MAX_DATA_SIZE + 1];
            let data =
                tokio::task::spawn_blocking(move || bincode::serialize(&data).unwrap()).await?;
            // This should panic or return an error since data is too large
            conn.outbound_short_message(data).await?;
            Ok::<_, anyhow::Error>(())
        });

        let (a, b) = tokio::join!(peer_a, peer_b);
        // peer_a should get an error (timeout waiting for message)
        assert!(a?.is_err());
        // peer_b task should error when trying to send oversized message
        let b_result = b?;
        assert!(b_result.is_err(), "Expected error from peer_b, got Ok");
        Ok(())
    }

    #[tokio::test]
    async fn simulate_send_streamed_message() -> anyhow::Result<()> {
        #[derive(Clone, Copy)]
        struct TestData(&'static str);

        impl TestFixture for TestData {
            type Message = String;
            fn expected_iterations(&self) -> usize {
                10
            }

            fn gen_msg(&mut self) -> Self::Message {
                self.0.repeat(3000)
            }

            fn assert_message_ok(&self, _: usize, msg: Self::Message) -> bool {
                if self.0 == "foo" {
                    msg.contains("bar") && msg.len() == "bar".len() * 3000
                } else {
                    msg.contains("foo") && msg.len() == "foo".len() * 3000
                }
            }
        }

        run_test(
            TestConfig::default(),
            vec![TestData("foo"), TestData("bar")],
        )
        .await
    }

    #[ignore = "should be fixed"]
    #[tokio::test(flavor = "multi_thread", worker_threads = 4)]
    async fn simulate_packet_dropping() -> anyhow::Result<()> {
        #[derive(Clone, Copy)]
        struct TestData(&'static str);

        impl TestFixture for TestData {
            type Message = String;
            fn expected_iterations(&self) -> usize {
                10
            }

            fn gen_msg(&mut self) -> Self::Message {
                self.0.repeat(1000)
            }

            fn assert_message_ok(&self, _: usize, msg: Self::Message) -> bool {
                if self.0 == "foo" {
                    msg.contains("bar") && msg.len() == "bar".len() * 1000
                } else {
                    msg.contains("foo") && msg.len() == "foo".len() * 1000
                }
            }
        }

        let mut tests = FuturesOrdered::new();
        let mut rng = rand::rngs::StdRng::seed_from_u64(3);
        let mut test_no = 0;
        for _ in 0..2 {
            for factor in std::iter::repeat(())
                .map(|_| rng.random::<f64>())
                .filter(|x| *x > 0.05 && *x < 0.25)
                .take(3)
            {
                let wait_time = Duration::from_secs(((factor * 5.0 * 15.0) + 15.0) as u64);
                tracing::info!(
                    "test #{test_no}: packet loss factor: {factor} (wait time: {wait_time})",
                    wait_time = wait_time.as_secs()
                );

                let now = tokio::time::Instant::now();
                tests.push_back(GlobalExecutor::spawn(
                    run_test(
                        TestConfig {
                            packet_drop_policy: PacketDropPolicy::Factor(factor),
                            wait_time,
                            ..Default::default()
                        },
                        vec![TestData("foo"), TestData("bar")],
                    )
                    .inspect(move |r| {
                        let msg = if r.is_ok() {
                            format!(
                                "successfully, total time: {}s (t/o: {}s, factor: {factor:.3})",
                                now.elapsed().as_secs(),
                                wait_time.as_secs()
                            )
                        } else {
                            format!(
                                "with error, total time: {}s (t/o: {}s, factor: {factor:.3})",
                                now.elapsed().as_secs(),
                                wait_time.as_secs()
                            )
                        };
                        if r.is_err() {
                            tracing::error!("test #{test_no} finished {}", msg);
                        } else {
                            tracing::info!("test #{test_no} finished {}", msg);
                        }
                    }),
                ));
                test_no += 1;
            }

            while let Some(result) = tests.next().await {
                result??;
            }
        }

        Ok(())
    }

    /// Test that a peer reconnecting from a different port is handled correctly.
    ///
    /// This simulates the scenario from issue #2235 where:
    /// 1. Peer connects to gateway from port A - succeeds
    /// 2. Peer disconnects (connection dropped)
    /// 3. Peer reconnects from port B (new ephemeral port)
    /// 4. Gateway should recognize this as a new session and accept immediately
    ///
    /// The bug was that the gateway retained stale crypto state, causing AEAD
    /// decryption failures on the new connection.
    #[tokio::test]
    async fn gateway_handles_peer_reconnection_from_different_port() -> anyhow::Result<()> {
        let channels = Arc::new(DashMap::new());

        // Create the gateway
        let (gw_pub, (gw_outbound, mut gw_conn), gw_addr) =
            create_mock_gateway(Default::default(), channels.clone()).await?;

        // Create peer A at port X
        let (_peer_a_pub, mut peer_a, peer_a_addr) =
            create_mock_peer(Default::default(), channels.clone()).await?;

        tracing::info!(
            "Step 1: Peer A at {} connecting to gateway at {}",
            peer_a_addr,
            gw_addr
        );

        // Peer A connects to gateway
        let gw_task = GlobalExecutor::spawn(async move {
            let conn = tokio::time::timeout(Duration::from_secs(10), gw_conn.recv())
                .await?
                .ok_or(anyhow::anyhow!("gateway: no inbound connection"))?;
            tracing::info!("Gateway received connection from {}", conn.remote_addr());
            Ok::<_, anyhow::Error>((gw_conn, conn))
        });

        let peer_a_conn = tokio::time::timeout(
            Duration::from_secs(10),
            peer_a.connect(gw_pub.clone(), gw_addr).await,
        )
        .await??;
        tracing::info!("Peer A connected successfully");

        let (mut gw_conn, _gw_peer_a_conn) = gw_task.await??;

        // Step 2: Simulate peer disconnect by dropping the connection
        // The gateway still has state for the old connection in remote_connections
        tracing::info!("Step 2: Dropping peer A connection");
        drop(peer_a_conn);
        drop(peer_a);

        // Give a moment for cleanup (in reality this could be much longer)
        tokio::time::sleep(Duration::from_millis(100)).await;

        // Step 3: Create a new peer at a DIFFERENT port (simulating restart with new ephemeral port)
        // This peer uses the same shared channels so packets can still route
        tracing::info!("Step 3: Creating peer B at different port");
        let (_peer_b_pub, mut peer_b, peer_b_addr) =
            create_mock_peer(Default::default(), channels.clone()).await?;

        // Verify this is testing the same-IP-different-port scenario
        assert_eq!(
            peer_a_addr.ip(),
            peer_b_addr.ip(),
            "Test requires both peers to have same IP (simulating reconnection)"
        );
        assert_ne!(
            peer_a_addr.port(),
            peer_b_addr.port(),
            "Peer B should have different port than peer A"
        );
        tracing::info!(
            "Peer B created at {} (different from peer A at {})",
            peer_b_addr,
            peer_a_addr
        );

        // Step 4: Peer B connects to gateway - this should work on first attempt
        tracing::info!("Step 4: Peer B connecting to gateway");

        let gw_task = GlobalExecutor::spawn(async move {
            let conn = tokio::time::timeout(Duration::from_secs(5), gw_conn.recv())
                .await?
                .ok_or(anyhow::anyhow!("gateway: no inbound connection for peer B"))?;
            tracing::info!(
                "Gateway received connection from {} (peer B)",
                conn.remote_addr()
            );
            Ok::<_, anyhow::Error>(conn)
        });

        let start = tokio::time::Instant::now();
        let peer_b_conn = tokio::time::timeout(
            Duration::from_secs(5),
            peer_b.connect(gw_pub.clone(), gw_addr).await,
        )
        .await??;
        let elapsed = start.elapsed();

        tracing::info!("Peer B connected in {:?}", elapsed);

        let _gw_peer_b_conn = gw_task.await??;

        // Verify the connection works by checking addresses match
        assert_eq!(peer_b_conn.remote_addr(), gw_addr);

        // The connection should have completed quickly (within the first attempt window)
        // If the bug exists, this would take multiple retries (16+ attempts at 200ms each = 3+ seconds)
        assert!(
            elapsed < Duration::from_secs(2),
            "Connection took {:?}, which suggests multiple retry attempts were needed. \
             This indicates stale crypto state issue from #2235.",
            elapsed
        );

        // Cleanup
        drop(peer_b_conn);
        drop(peer_b);
        drop(gw_outbound);

        Ok(())
    }

    /// Test that multiple peers behind the same NAT (same IP, different ports) can
    /// connect simultaneously without interfering with each other.
    ///
    /// This verifies that the stale connection cleanup (issue #2235) doesn't break
    /// legitimate multi-peer scenarios where multiple devices share the same public IP.
    #[tokio::test]
    async fn multiple_peers_behind_same_nat() -> anyhow::Result<()> {
        let channels = Arc::new(DashMap::new());

        // Create the gateway
        let (gw_pub, (_gw_outbound, mut gw_conn), gw_addr) =
            create_mock_gateway(Default::default(), channels.clone()).await?;

        // Create peer A (first device behind NAT)
        let (_peer_a_pub, mut peer_a, peer_a_addr) =
            create_mock_peer(Default::default(), channels.clone()).await?;

        // Create peer B (second device behind NAT, same IP different port)
        let (_peer_b_pub, mut peer_b, peer_b_addr) =
            create_mock_peer(Default::default(), channels.clone()).await?;

        // Verify both peers have same IP but different ports (simulating NAT)
        assert_eq!(
            peer_a_addr.ip(),
            peer_b_addr.ip(),
            "Both peers should have same IP (behind same NAT)"
        );
        assert_ne!(
            peer_a_addr.port(),
            peer_b_addr.port(),
            "Peers should have different ports"
        );

        tracing::info!(
            "Testing multiple peers behind same NAT: Peer A at {}, Peer B at {}",
            peer_a_addr,
            peer_b_addr
        );

        // Step 1: Peer A connects to gateway
        let gw_task_a = GlobalExecutor::spawn(async move {
            let conn = tokio::time::timeout(Duration::from_secs(10), gw_conn.recv())
                .await?
                .ok_or(anyhow::anyhow!("gateway: no connection from peer A"))?;
            tracing::info!(
                "Gateway received connection from peer A: {}",
                conn.remote_addr()
            );
            Ok::<_, anyhow::Error>((gw_conn, conn))
        });

        let peer_a_conn = tokio::time::timeout(
            Duration::from_secs(10),
            peer_a.connect(gw_pub.clone(), gw_addr).await,
        )
        .await??;
        tracing::info!("Peer A connected successfully");

        let (mut gw_conn, gw_peer_a_conn) = gw_task_a.await??;

        // Step 2: Peer B connects to gateway (while peer A is still connected)
        let gw_task_b = GlobalExecutor::spawn(async move {
            let conn = tokio::time::timeout(Duration::from_secs(10), gw_conn.recv())
                .await?
                .ok_or(anyhow::anyhow!("gateway: no connection from peer B"))?;
            tracing::info!(
                "Gateway received connection from peer B: {}",
                conn.remote_addr()
            );
            Ok::<_, anyhow::Error>((gw_conn, conn))
        });

        let peer_b_conn = tokio::time::timeout(
            Duration::from_secs(10),
            peer_b.connect(gw_pub.clone(), gw_addr).await,
        )
        .await??;
        tracing::info!("Peer B connected successfully");

        let (_gw_conn, gw_peer_b_conn) = gw_task_b.await??;

        // Step 3: Verify BOTH connections are still valid
        // The critical check: peer A's connection should NOT have been disrupted
        // when peer B connected from the same IP
        assert_eq!(
            peer_a_conn.remote_addr(),
            gw_addr,
            "Peer A connection should still be valid"
        );
        assert_eq!(
            peer_b_conn.remote_addr(),
            gw_addr,
            "Peer B connection should be valid"
        );
        assert_eq!(
            gw_peer_a_conn.remote_addr(),
            peer_a_addr,
            "Gateway should still have peer A's connection"
        );
        assert_eq!(
            gw_peer_b_conn.remote_addr(),
            peer_b_addr,
            "Gateway should have peer B's connection"
        );

        tracing::info!("Both peers successfully connected and remain active");

        Ok(())
    }

    /// Create a mock peer at a specific socket address for testing.
    ///
    /// This allows testing scenarios where a peer reconnects from the same IP:port.
    #[allow(dead_code)]
    async fn create_mock_peer_at_addr(
        packet_drop_policy: PacketDropPolicy,
        addr: SocketAddr,
        channels: Channels,
    ) -> anyhow::Result<(
        TransportPublicKey,
        OutboundConnectionHandler<MockSocket>,
        SocketAddr,
    )> {
        let peer_keypair = TransportKeypair::new();
        let peer_pub = peer_keypair.public.clone();
        let socket = Arc::new(
            MockSocket::new(
                packet_drop_policy,
                PacketDelayPolicy::NoDelay,
                addr,
                channels,
            )
            .await,
        );
        let (peer_conn, _inbound_conn) =
            OutboundConnectionHandler::new_test(addr, socket, peer_keypair, false)
                .expect("failed to create peer");
        Ok((peer_pub, peer_conn, addr))
    }

    /// Create a mock gateway at a specific socket address for testing.
    ///
    /// This allows testing scenarios where a gateway restarts at the same IP:port with new keys.
    #[allow(dead_code)]
    async fn create_mock_gateway_at_addr(
        packet_drop_policy: PacketDropPolicy,
        addr: SocketAddr,
        channels: Channels,
    ) -> anyhow::Result<(
        TransportPublicKey,
        (
            OutboundConnectionHandler<MockSocket>,
            mpsc::Receiver<PeerConnection<MockSocket>>,
        ),
        SocketAddr,
    )> {
        let gw_keypair = TransportKeypair::new();
        let gw_pub = gw_keypair.public.clone();
        let socket = Arc::new(
            MockSocket::new(
                packet_drop_policy,
                PacketDelayPolicy::NoDelay,
                addr,
                channels,
            )
            .await,
        );
        let (gw_conn, inbound_conn) =
            OutboundConnectionHandler::new_test(addr, socket, gw_keypair, true)
                .expect("failed to create gateway");
        Ok((gw_pub, (gw_conn, inbound_conn), addr))
    }

    /// Test that a peer reconnecting to a gateway after gateway restart is handled correctly.
    ///
    /// This simulates the scenario from issue #2470 where:
    /// 1. Peer connects to Gateway A (identity X) at gw_addr
    /// 2. Gateway A restarts (simulated by dropping and recreating)
    /// 3. Gateway B (identity Y) starts at the SAME gw_addr
    /// 4. Peer tries to reconnect to gw_addr with Gateway B's public key
    ///
    /// The bug was that the peer retained the old session with Gateway A's encryption keys.
    /// When the peer called connect() again, the old code would return the stale connection
    /// instead of establishing a fresh handshake with Gateway B's new keys.
    ///
    /// The fix clears any existing remote_connections entry when connect() is called,
    /// ensuring a fresh handshake occurs.
    #[tokio::test]
    async fn peer_handles_gateway_restart_same_addr_new_identity() -> anyhow::Result<()> {
        let channels = Arc::new(DashMap::new());

        // Use a fixed port for the gateway to simulate restart at same address
        let gw_addr: SocketAddr = (Ipv4Addr::LOCALHOST, 55555).into();

        // Step 1: Create Gateway A with identity X
        tracing::info!("Step 1: Creating Gateway A (identity X) at {}", gw_addr);
        let (gw_a_pub, (gw_a_outbound, mut gw_a_conn), _) =
            create_mock_gateway_at_addr(Default::default(), gw_addr, channels.clone()).await?;
        tracing::info!("Gateway A public key: {:?}", gw_a_pub);

        // Create the peer at a regular address
        let (_peer_pub, mut peer, _peer_addr) =
            create_mock_peer(Default::default(), channels.clone()).await?;

        // Peer connects to Gateway A
        let gw_task = GlobalExecutor::spawn(async move {
            let conn = tokio::time::timeout(Duration::from_secs(10), gw_a_conn.recv())
                .await?
                .ok_or(anyhow::anyhow!(
                    "Gateway A: no inbound connection from peer"
                ))?;
            tracing::info!(
                "Gateway A received connection from peer at {}",
                conn.remote_addr()
            );
            Ok::<_, anyhow::Error>(conn)
        });

        let peer_conn_a = tokio::time::timeout(
            Duration::from_secs(10),
            peer.connect(gw_a_pub.clone(), gw_addr).await,
        )
        .await??;
        tracing::info!("Peer connected to Gateway A successfully");

        let _gw_a_peer_conn = gw_task.await??;

        // Verify the connection works
        assert_eq!(peer_conn_a.remote_addr(), gw_addr);

        // Step 2: Simulate gateway restart by dropping Gateway A
        tracing::info!("Step 2: Simulating Gateway A restart (dropping connection and handler)");
        drop(peer_conn_a);
        drop(gw_a_outbound);

        // Give time for cleanup
        tokio::time::sleep(Duration::from_millis(100)).await;

        // Step 3: Create Gateway B with NEW identity (Y) at the SAME address
        tracing::info!(
            "Step 3: Creating Gateway B (NEW identity Y) at SAME address {}",
            gw_addr
        );
        let (gw_b_pub, (_gw_b_outbound, mut gw_b_conn), _) =
            create_mock_gateway_at_addr(Default::default(), gw_addr, channels.clone()).await?;
        tracing::info!("Gateway B public key: {:?}", gw_b_pub);

        // Verify this test is actually testing new identity
        assert_ne!(
            gw_a_pub, gw_b_pub,
            "Gateway B must have different identity than Gateway A"
        );

        // Step 4: Peer reconnects to Gateway B (new identity) at same address
        // This is where the bug manifests:
        // - Peer still has remote_connections entry for gw_addr with Gateway A's session
        // - Without the fix: connect() returns stale connection with wrong keys
        // - With the fix: connect() clears stale entry and does fresh handshake
        tracing::info!("Step 4: Peer reconnecting to Gateway B (new identity) at same address");

        let gw_task = GlobalExecutor::spawn(async move {
            let conn = tokio::time::timeout(Duration::from_secs(5), gw_b_conn.recv())
                .await?
                .ok_or(anyhow::anyhow!(
                    "Gateway B: no inbound connection from peer (bug #2470 - peer didn't clear stale session)"
                ))?;
            tracing::info!(
                "Gateway B received connection from peer at {}",
                conn.remote_addr()
            );
            Ok::<_, anyhow::Error>(conn)
        });

        let start = tokio::time::Instant::now();
        let peer_conn_b = tokio::time::timeout(
            Duration::from_secs(5),
            peer.connect(gw_b_pub.clone(), gw_addr).await,
        )
        .await??;
        let elapsed = start.elapsed();

        tracing::info!("Peer reconnected to Gateway B in {:?}", elapsed);

        let _gw_b_peer_conn = gw_task.await??;

        // Verify the connection works
        assert_eq!(peer_conn_b.remote_addr(), gw_addr);

        // The connection should complete quickly
        assert!(
            elapsed < Duration::from_secs(2),
            "Connection took {:?}, which suggests the peer failed to clear stale session. \
             This is bug #2470: peer doesn't reset session when gateway restarts with new identity.",
            elapsed
        );

        tracing::info!("Test passed: Peer successfully reconnected to restarted gateway");

        Ok(())
    }

    /// Test that a peer reconnecting from the SAME IP:port with a NEW identity is handled correctly.
    ///
    /// This simulates the scenario from issue #2277 where:
    /// 1. Peer A (identity X) connects to gateway from IP:port
    /// 2. Peer A disconnects (peer restarts, clears state, gets new identity)
    /// 3. Peer B (identity Y) tries to connect from SAME IP:port (NAT assigns same mapping)
    /// 4. Gateway should recognize this as a new peer and establish a fresh session
    ///
    /// The bug was that the gateway retained the old session with peer A's encryption keys,
    /// causing peer B's handshake packets to fail decryption and be silently dropped.
    #[tokio::test]
    async fn gateway_handles_peer_reconnection_same_addr_new_identity() -> anyhow::Result<()> {
        let channels = Arc::new(DashMap::new());

        // Create the gateway
        let (gw_pub, (gw_outbound, mut gw_conn), gw_addr) =
            create_mock_gateway(Default::default(), channels.clone()).await?;

        // Use a fixed port for testing the same-address scenario
        let peer_addr: SocketAddr = (Ipv4Addr::LOCALHOST, 44444).into();

        // Step 1: Create peer A with identity X at the fixed address
        tracing::info!(
            "Step 1: Creating peer A (identity X) at {} connecting to gateway at {}",
            peer_addr,
            gw_addr
        );

        let (peer_a_pub, mut peer_a, _) =
            create_mock_peer_at_addr(Default::default(), peer_addr, channels.clone()).await?;

        tracing::info!("Peer A public key: {:?}", peer_a_pub);

        // Peer A connects to gateway
        let gw_task = GlobalExecutor::spawn(async move {
            let conn = tokio::time::timeout(Duration::from_secs(10), gw_conn.recv())
                .await?
                .ok_or(anyhow::anyhow!(
                    "gateway: no inbound connection from peer A"
                ))?;
            tracing::info!(
                "Gateway received connection from peer A at {}",
                conn.remote_addr()
            );
            Ok::<_, anyhow::Error>((gw_conn, conn))
        });

        let peer_a_conn = tokio::time::timeout(
            Duration::from_secs(10),
            peer_a.connect(gw_pub.clone(), gw_addr).await,
        )
        .await??;
        tracing::info!("Peer A connected successfully");

        let (mut gw_conn, _gw_peer_a_conn) = gw_task.await??;

        // Step 2: Simulate peer restart by dropping everything related to peer A
        // This simulates: peer process killed, state cleared, restarted with new identity
        tracing::info!("Step 2: Simulating peer A restart (dropping connection and handler)");
        drop(peer_a_conn);
        drop(peer_a);

        // Give time for cleanup (in reality this could be much longer)
        tokio::time::sleep(Duration::from_millis(100)).await;

        // Step 3: Create peer B with NEW identity (Y) at the SAME address
        // This simulates the NAT assigning the same external IP:port to the restarted peer
        tracing::info!(
            "Step 3: Creating peer B (NEW identity Y) at SAME address {}",
            peer_addr
        );

        let (peer_b_pub, mut peer_b, _) =
            create_mock_peer_at_addr(Default::default(), peer_addr, channels.clone()).await?;

        tracing::info!("Peer B public key: {:?}", peer_b_pub);

        // Verify this test is actually testing new identity
        assert_ne!(
            peer_a_pub, peer_b_pub,
            "Peer B must have different identity than peer A"
        );

        // Step 4: Peer B (new identity) connects to gateway from same IP:port
        // This is where the bug manifests:
        // - Gateway still has remote_connections entry for peer_addr with peer A's session
        // - Peer B's handshake packets arrive, get sent to peer A's session handler
        // - Decryption fails because peer B is using different keys
        // - Handshake silently fails
        tracing::info!("Step 4: Peer B (new identity) connecting to gateway from same address");

        let gw_task = GlobalExecutor::spawn(async move {
            let conn = tokio::time::timeout(Duration::from_secs(5), gw_conn.recv())
                .await?
                .ok_or(anyhow::anyhow!(
                    "gateway: no inbound connection from peer B (bug #2277)"
                ))?;
            tracing::info!(
                "Gateway received connection from peer B at {}",
                conn.remote_addr()
            );
            Ok::<_, anyhow::Error>(conn)
        });

        let start = tokio::time::Instant::now();
        let peer_b_conn = tokio::time::timeout(
            Duration::from_secs(5),
            peer_b.connect(gw_pub.clone(), gw_addr).await,
        )
        .await??;
        let elapsed = start.elapsed();

        tracing::info!("Peer B connected in {:?}", elapsed);

        let _gw_peer_b_conn = gw_task.await??;

        // Verify the connection works
        assert_eq!(peer_b_conn.remote_addr(), gw_addr);

        // The connection should complete quickly
        assert!(
            elapsed < Duration::from_secs(2),
            "Connection took {:?}, which suggests the gateway failed to recognize the new identity. \
             This is bug #2277: gateway doesn't reset session when peer restarts with new identity.",
            elapsed
        );

        // Cleanup
        drop(peer_b_conn);
        drop(peer_b);
        drop(gw_outbound);

        Ok(())
    }
}