rsiprtp 0.4.1

Modular SIP/RTP communications stack for Rust with Sans-IO state machines
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
//! ICE agent implementation (RFC 8445).
//!
//! Handles candidate gathering and connectivity checks.

use crate::ice::candidate::{calculate_pair_priority, Candidate, CandidateType};
use crate::ice::stun::StunServer;
use std::collections::HashMap;
use std::net::{IpAddr, SocketAddr};
use std::sync::Arc;
use thiserror::Error;
use tokio::net::UdpSocket;
use tokio::sync::{oneshot, Mutex, RwLock};
use tokio::task::JoinHandle;
use tracing::{debug, info, warn};

/// Wire constants shared by the request/response code paths.
const MAGIC_COOKIE: u32 = 0x2112A442;
const BINDING_REQUEST: u16 = 0x0001;
const BINDING_RESPONSE: u16 = 0x0101;
const ATTR_USERNAME: u16 = 0x0006;
const ATTR_MESSAGE_INTEGRITY: u16 = 0x0008;
const ATTR_XOR_MAPPED_ADDRESS: u16 = 0x0020;

#[cfg(test)]
use std::sync::atomic::{AtomicU64, Ordering};

/// ICE agent errors.
#[derive(Error, Debug)]
pub enum IceError {
    /// Underlying network I/O error.
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    /// STUN protocol error during candidate gathering or connectivity checks.
    #[error("STUN error: {0}")]
    Stun(#[from] crate::ice::stun::StunError),

    /// No candidates were gathered or supplied — cannot perform ICE.
    #[error("No candidates available")]
    NoCandidates,

    /// ICE processing failed (no valid pair found, all checks failed, etc.).
    #[error("ICE failed: {0}")]
    Failed(String),
}

/// ICE agent role.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IceRole {
    /// Controlling agent (makes nomination decisions).
    Controlling,
    /// Controlled agent (follows controlling agent's decisions).
    Controlled,
}

/// ICE agent state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IceState {
    /// Not started.
    New,
    /// Gathering candidates.
    Gathering,
    /// Checking connectivity.
    Checking,
    /// At least one valid pair found.
    Connected,
    /// All checks complete, selected pair nominated.
    Completed,
    /// ICE failed.
    Failed,
    /// ICE closed.
    Closed,
}

/// ICE candidate pair.
#[derive(Debug, Clone)]
pub struct CandidatePair {
    /// Local candidate.
    pub local: Candidate,
    /// Remote candidate.
    pub remote: Candidate,
    /// Pair priority.
    pub priority: u64,
    /// Pair state.
    pub state: PairState,
    /// Whether this pair is nominated.
    pub nominated: bool,
}

/// Candidate pair state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PairState {
    /// Waiting to be checked.
    Waiting,
    /// Check in progress.
    InProgress,
    /// Check succeeded.
    Succeeded,
    /// Check failed.
    Failed,
    /// Pair frozen (waiting for other checks).
    Frozen,
}

/// ICE agent configuration.
#[derive(Debug, Clone)]
pub struct IceConfig {
    /// STUN servers to use for gathering.
    pub stun_servers: Vec<StunServer>,
    /// ICE credentials (ufrag:pwd) — local username fragment.
    pub local_ufrag: String,
    /// Local ICE password used for STUN message integrity.
    pub local_pwd: String,
    /// Wall-clock cap on `gather_candidates`. On timeout, whatever
    /// candidates have already been gathered are returned (partial results
    /// are useful — the caller can still negotiate). Host gathering happens
    /// first, so a timeout will typically still leave at least the host
    /// candidates intact.
    pub gather_timeout_ms: u64,
    /// Check timeout.
    pub check_timeout_ms: u64,
}

impl Default for IceConfig {
    fn default() -> Self {
        Self {
            stun_servers: vec![StunServer::GOOGLE],
            local_ufrag: generate_ice_ufrag(),
            local_pwd: generate_ice_pwd(),
            // 1500ms: STUN RTT well under 200ms (HLD); leaves headroom
            // for several retransmits before the deferred-answer path
            // forces send.
            gather_timeout_ms: 1500,
            check_timeout_ms: 3000,
        }
    }
}

/// Generate random ICE ufrag (4-256 chars).
fn generate_ice_ufrag() -> String {
    use rand::Rng;
    let mut rng = rand::thread_rng();
    let chars: String = (0..8)
        .map(|_| {
            let idx = rng.gen_range(0..36);
            if idx < 10 {
                (b'0' + idx) as char
            } else {
                (b'a' + idx - 10) as char
            }
        })
        .collect();
    chars
}

/// Generate random ICE password (22-256 chars).
fn generate_ice_pwd() -> String {
    use rand::Rng;
    let mut rng = rand::thread_rng();
    let chars: String = (0..24)
        .map(|_| {
            let idx = rng.gen_range(0..36);
            if idx < 10 {
                (b'0' + idx) as char
            } else {
                (b'a' + idx - 10) as char
            }
        })
        .collect();
    chars
}

/// Per-socket dispatcher state: the dispatcher drains the bound socket,
/// answers inbound STUN binding requests directly, and forwards inbound
/// STUN binding responses to whatever check-loop registered the matching
/// transaction ID. Every access is a write (insert / remove / clear),
/// so a `Mutex` is the right primitive here — `RwLock` would only add
/// overhead.
type PendingResponses = Arc<Mutex<HashMap<[u8; 12], oneshot::Sender<Vec<u8>>>>>;

/// ICE agent.
pub struct IceAgent {
    config: IceConfig,
    role: IceRole,
    state: Arc<RwLock<IceState>>,
    local_candidates: Arc<RwLock<Vec<Candidate>>>,
    remote_candidates: Arc<RwLock<Vec<Candidate>>>,
    candidate_pairs: Arc<RwLock<Vec<CandidatePair>>>,
    selected_pair: Arc<RwLock<Option<CandidatePair>>>,
    sockets: Arc<RwLock<HashMap<SocketAddr, Arc<UdpSocket>>>>,
    /// One dispatcher task per bound host socket. Tracked so `close`
    /// can abort them.
    responder_tasks: Arc<RwLock<HashMap<SocketAddr, JoinHandle<()>>>>,
    /// In-flight outgoing STUN transactions, keyed by transaction ID.
    /// The dispatcher hands the response bytes to the matching oneshot.
    pending_responses: PendingResponses,
    /// Remote ICE credentials.
    remote_ufrag: Arc<RwLock<Option<String>>>,
    remote_pwd: Arc<RwLock<Option<String>>>,
}

impl IceAgent {
    /// Create a new ICE agent.
    pub fn new(config: IceConfig, role: IceRole) -> Self {
        Self {
            config,
            role,
            state: Arc::new(RwLock::new(IceState::New)),
            local_candidates: Arc::new(RwLock::new(Vec::new())),
            remote_candidates: Arc::new(RwLock::new(Vec::new())),
            candidate_pairs: Arc::new(RwLock::new(Vec::new())),
            selected_pair: Arc::new(RwLock::new(None)),
            sockets: Arc::new(RwLock::new(HashMap::new())),
            responder_tasks: Arc::new(RwLock::new(HashMap::new())),
            pending_responses: Arc::new(Mutex::new(HashMap::new())),
            remote_ufrag: Arc::new(RwLock::new(None)),
            remote_pwd: Arc::new(RwLock::new(None)),
        }
    }

    /// Set remote ICE credentials.
    pub async fn set_remote_credentials(&self, ufrag: &str, pwd: &str) {
        *self.remote_ufrag.write().await = Some(ufrag.to_string());
        *self.remote_pwd.write().await = Some(pwd.to_string());
        debug!("Set remote credentials: ufrag={}", ufrag);
    }

    /// Get remote ICE credentials.
    pub async fn remote_credentials(&self) -> Option<(String, String)> {
        let ufrag = self.remote_ufrag.read().await.clone()?;
        let pwd = self.remote_pwd.read().await.clone()?;
        Some((ufrag, pwd))
    }

    /// Get the ICE credentials.
    pub fn local_credentials(&self) -> (&str, &str) {
        (&self.config.local_ufrag, &self.config.local_pwd)
    }

    /// Get the current state.
    pub async fn state(&self) -> IceState {
        *self.state.read().await
    }

    /// Get local candidates.
    pub async fn local_candidates(&self) -> Vec<Candidate> {
        self.local_candidates.read().await.clone()
    }

    /// Get the selected candidate pair.
    pub async fn selected_pair(&self) -> Option<CandidatePair> {
        self.selected_pair.read().await.clone()
    }

    /// Get the host-bound socket for an address that appeared as a host
    /// candidate's address (i.e. anything in `local_candidates()` with
    /// `CandidateType::Host`).
    ///
    /// After `start_checks` succeeds, the caller may take ownership of the
    /// socket for RTP; the agent will not race on it provided no further
    /// ICE operations (gather, restart) are invoked.
    pub async fn socket_for(&self, addr: SocketAddr) -> Option<Arc<UdpSocket>> {
        self.sockets.read().await.get(&addr).cloned()
    }

    /// Gather local candidates.
    ///
    /// Discovers host candidates from local interfaces and
    /// server-reflexive candidates from STUN servers.
    ///
    /// The whole call is bounded by `IceConfig::gather_timeout_ms`. If the
    /// budget elapses, gathering stops and whatever candidates have already
    /// been gathered are returned — partial results are useful (host
    /// candidates alone are enough to keep negotiating).
    pub async fn gather_candidates(&self) -> Result<Vec<Candidate>, IceError> {
        *self.state.write().await = IceState::Gathering;
        info!("Starting ICE candidate gathering");

        let budget = std::time::Duration::from_millis(self.config.gather_timeout_ms);
        match tokio::time::timeout(budget, self.gather_candidates_inner()).await {
            Ok(result) => result,
            Err(_) => {
                // Timeout: return whatever was stored so far.
                let partial = self.local_candidates.read().await.clone();
                warn!(
                    "ICE candidate gathering timed out after {}ms; returning {} partial candidates",
                    self.config.gather_timeout_ms,
                    partial.len()
                );
                Ok(partial)
            }
        }
    }

    /// Inner gathering body — host then srflx, each batch stored in
    /// `local_candidates` so the outer timeout wrapper can return partial
    /// results.
    async fn gather_candidates_inner(&self) -> Result<Vec<Candidate>, IceError> {
        let mut candidates = Vec::new();

        // Gather host candidates from local interfaces
        let host_candidates = self.gather_host_candidates().await?;
        candidates.extend(host_candidates);
        // Stash hosts before we start STUN — if srflx gathering times out
        // mid-flight, the timeout branch can still return the hosts.
        *self.local_candidates.write().await = candidates.clone();

        // Gather server-reflexive candidates from STUN
        let srflx_candidates = self.gather_srflx_candidates().await;
        candidates.extend(srflx_candidates);

        // Store final candidate set
        *self.local_candidates.write().await = candidates.clone();

        info!("Gathered {} candidates", candidates.len());
        Ok(candidates)
    }

    /// Gather host candidates from local interfaces.
    async fn gather_host_candidates(&self) -> Result<Vec<Candidate>, IceError> {
        let addrs = get_local_addresses();
        self.gather_host_candidates_with(addrs).await
    }

    async fn gather_host_candidates_with(
        &self,
        addrs: Vec<IpAddr>,
    ) -> Result<Vec<Candidate>, IceError> {
        let mut candidates = Vec::new();

        for addr in addrs {
            // Bind a socket for this address
            let bind_addr = SocketAddr::new(addr, 0);
            match UdpSocket::bind(bind_addr).await {
                Ok(socket) => {
                    let local_addr = socket_local_addr(&socket)?;
                    debug!("Bound socket to {}", local_addr);

                    // Create host candidate
                    let candidate = Candidate::host(local_addr, 1);
                    candidates.push(candidate);

                    // Store socket and start a dispatcher task for it.
                    let socket = Arc::new(socket);
                    self.sockets
                        .write()
                        .await
                        .insert(local_addr, socket.clone());
                    self.ensure_dispatcher(local_addr, socket).await;
                }
                Err(e) => {
                    warn!("Failed to bind to {}: {}", bind_addr, e);
                }
            }
        }

        Ok(candidates)
    }

    /// Start (or noop) the per-socket dispatcher loop that answers
    /// inbound STUN binding requests and routes inbound binding
    /// responses to the matching outgoing-check oneshot. Idempotent
    /// per `local_addr`.
    ///
    /// Note on the bind-then-spawn window: between `UdpSocket::bind`
    /// returning and the spawned task issuing its first `recv_from`,
    /// the kernel buffers any inbound datagrams in the per-socket
    /// queue. Nothing is lost as long as the queue isn't overrun;
    /// a STUN check arriving "early" simply waits a few microseconds
    /// for the loop to drain it.
    async fn ensure_dispatcher(&self, local_addr: SocketAddr, socket: Arc<UdpSocket>) {
        let mut tasks = self.responder_tasks.write().await;
        if tasks.contains_key(&local_addr) {
            return;
        }

        let pending = self.pending_responses.clone();
        let local_ufrag = self.config.local_ufrag.clone();
        let local_pwd = self.config.local_pwd.clone();
        let handle = tokio::spawn(async move {
            stun_dispatcher_loop(socket, pending, local_ufrag, local_pwd).await;
        });
        tasks.insert(local_addr, handle);
    }

    /// Send an outgoing STUN message and await the response (matched
    /// by transaction ID via the dispatcher) with a timeout. The
    /// dispatcher must already be running for `socket`'s local address
    /// — callers ensure this via `ensure_dispatcher` or by binding
    /// through `gather_host_candidates_with`.
    async fn send_and_await_response(
        &self,
        socket: &UdpSocket,
        target: SocketAddr,
        msg: &[u8],
        txn_id: [u8; 12],
        timeout_dur: std::time::Duration,
    ) -> Result<Vec<u8>, std::io::Error> {
        let (tx, rx) = oneshot::channel();
        self.pending_responses.lock().await.insert(txn_id, tx);

        let send_result = socket.send_to(msg, target).await;
        if let Err(e) = send_result {
            self.pending_responses.lock().await.remove(&txn_id);
            return Err(e);
        }

        let result = tokio::time::timeout(timeout_dur, rx).await;
        // Whatever happened, drop the pending entry.
        self.pending_responses.lock().await.remove(&txn_id);
        match result {
            Ok(Ok(bytes)) => Ok(bytes),
            Ok(Err(_)) => Err(std::io::Error::other("dispatcher dropped")),
            Err(_) => Err(std::io::Error::new(
                std::io::ErrorKind::TimedOut,
                "STUN response timeout",
            )),
        }
    }

    /// Gather server-reflexive candidates from STUN servers.
    async fn gather_srflx_candidates(&self) -> Vec<Candidate> {
        let mut candidates = Vec::new();

        // Snapshot sockets to avoid holding the lock across awaits.
        let sockets: Vec<(SocketAddr, Arc<UdpSocket>)> = self
            .sockets
            .read()
            .await
            .iter()
            .map(|(a, s)| (*a, s.clone()))
            .collect();

        for (base_addr, socket) in sockets {
            for server in &self.config.stun_servers {
                match self.stun_binding_request(&socket, server).await {
                    Ok(mapped_addr) => {
                        if mapped_addr != base_addr {
                            let candidate = Candidate::server_reflexive(mapped_addr, base_addr, 1);
                            debug!(
                                "Discovered srflx candidate: {} (base: {})",
                                mapped_addr, base_addr
                            );
                            candidates.push(candidate);
                        }
                    }
                    Err(e) => {
                        warn!("STUN request to {} failed: {}", server.name, e);
                    }
                }
            }
        }

        candidates
    }

    /// Send a STUN binding request using an existing socket.
    async fn stun_binding_request(
        &self,
        socket: &Arc<UdpSocket>,
        server: &StunServer,
    ) -> Result<SocketAddr, crate::ice::stun::StunError> {
        use bytes::{Buf, BufMut, BytesMut};
        use rand::RngCore;
        use std::time::Duration;

        // Generate transaction ID
        let mut txn_id = [0u8; 12];
        rand::thread_rng().fill_bytes(&mut txn_id);

        // Build request
        let mut request = BytesMut::with_capacity(20);
        request.put_u16(BINDING_REQUEST);
        request.put_u16(0);
        request.put_u32(MAGIC_COOKIE);
        request.put_slice(&txn_id);

        // Make sure the dispatcher is running for this socket's local
        // address; otherwise the response will never reach us.
        let local_addr = match socket.local_addr() {
            Ok(a) => a,
            Err(e) => return Err(crate::ice::stun::StunError::Io(e)),
        };
        self.ensure_dispatcher(local_addr, socket.clone()).await;

        // Send + await via the dispatcher.
        let timeout_dur = Duration::from_millis(self.config.check_timeout_ms);
        let response = match self
            .send_and_await_response(socket, server.addr, &request, txn_id, timeout_dur)
            .await
        {
            Ok(bytes) => bytes,
            Err(e) if e.kind() == std::io::ErrorKind::TimedOut => {
                return Err(crate::ice::stun::StunError::Timeout);
            }
            Err(e) => return Err(crate::ice::stun::StunError::Io(e)),
        };

        // Parse response.
        let data = response.as_slice();
        if data.len() < 20 {
            return Err(crate::ice::stun::StunError::InvalidResponse(
                "Too short".into(),
            ));
        }

        let mut buf = data;
        let msg_type = buf.get_u16();
        if msg_type != BINDING_RESPONSE {
            return Err(crate::ice::stun::StunError::InvalidResponse(
                "Not a response".into(),
            ));
        }

        let msg_len = buf.get_u16() as usize;
        let cookie = buf.get_u32();
        if cookie != MAGIC_COOKIE {
            return Err(crate::ice::stun::StunError::InvalidResponse(
                "Bad cookie".into(),
            ));
        }

        // Skip transaction ID — already matched by the dispatcher.
        buf.advance(12);

        // Parse attributes to find XOR-MAPPED-ADDRESS
        let mut attrs = &data[20..20 + msg_len.min(data.len() - 20)];
        while attrs.len() >= 4 {
            let attr_type = attrs.get_u16();
            let attr_len = attrs.get_u16() as usize;

            if attr_type == ATTR_XOR_MAPPED_ADDRESS && attr_len >= 8 {
                // XOR-MAPPED-ADDRESS
                let _reserved = attrs.get_u8();
                let family = attrs.get_u8();
                let port = attrs.get_u16() ^ (MAGIC_COOKIE >> 16) as u16;

                if family == 0x01 {
                    let ip_bytes = [
                        attrs.get_u8() ^ 0x21,
                        attrs.get_u8() ^ 0x12,
                        attrs.get_u8() ^ 0xA4,
                        attrs.get_u8() ^ 0x42,
                    ];
                    return Ok(SocketAddr::new(
                        IpAddr::V4(std::net::Ipv4Addr::from(ip_bytes)),
                        port,
                    ));
                }
            }

            let padded = (attr_len + 3) & !3;
            if attrs.len() >= padded {
                attrs.advance(padded);
            } else {
                break;
            }
        }

        Err(crate::ice::stun::StunError::NoMappedAddress)
    }

    /// Add remote candidates.
    pub async fn add_remote_candidates(&self, candidates: Vec<Candidate>) {
        {
            let mut remote = self.remote_candidates.write().await;
            remote.extend(candidates);
        } // Drop write lock before calling form_candidate_pairs

        // Form new candidate pairs
        self.form_candidate_pairs().await;
    }

    /// Form candidate pairs from local and remote candidates.
    async fn form_candidate_pairs(&self) {
        let local = self.local_candidates.read().await;
        let remote = self.remote_candidates.read().await;
        let mut pairs = self.candidate_pairs.write().await;

        for l in local.iter() {
            for r in remote.iter() {
                // Only pair candidates with same component
                if l.component != r.component {
                    continue;
                }

                // Only pair compatible transports
                if l.transport != r.transport {
                    continue;
                }

                // Check if pair already exists
                let exists = pairs
                    .iter()
                    .any(|p| p.local.address == l.address && p.remote.address == r.address);

                if !exists {
                    let is_controlling = self.role == IceRole::Controlling;
                    let priority = calculate_pair_priority(is_controlling, l.priority, r.priority);

                    pairs.push(CandidatePair {
                        local: l.clone(),
                        remote: r.clone(),
                        priority,
                        state: PairState::Frozen,
                        nominated: false,
                    });
                }
            }
        }

        // Sort by priority (highest first)
        pairs.sort_by(|a, b| b.priority.cmp(&a.priority));

        // Unfreeze first pair of each foundation
        let mut seen_foundations = std::collections::HashSet::new();
        for pair in pairs.iter_mut() {
            let key = (&pair.local.foundation, &pair.remote.foundation);
            if !seen_foundations.contains(&key) {
                pair.state = PairState::Waiting;
                seen_foundations.insert(key);
            }
        }

        debug!("Formed {} candidate pairs", pairs.len());
    }

    /// Start connectivity checks.
    ///
    /// Performs STUN connectivity checks on candidate pairs according to RFC 8445.
    /// Each check sends a STUN Binding Request to the remote candidate and waits
    /// for a response to validate the path.
    ///
    /// Known limitation: peer-reflexive (prflx) candidate discovery for
    /// symmetric-NAT peers is not implemented (out of scope per the ICE
    /// HLD — symmetric NAT requires a TURN relay, which this thin ICE
    /// path doesn't wire in). The caller should plan to fall back to a
    /// relay if both peers sit behind symmetric NATs.
    pub async fn start_checks(&self) -> Result<(), IceError> {
        *self.state.write().await = IceState::Checking;
        info!("Starting ICE connectivity checks");

        // Get credentials for STUN requests
        let remote_creds = self.remote_credentials().await;
        let (remote_ufrag, remote_pwd) = match remote_creds {
            Some((u, p)) => (u, p),
            None => {
                warn!("Remote credentials not set, falling back to simple selection");
                return self.fallback_selection().await;
            }
        };

        // Get pairs to check (copy to avoid holding lock during async operations)
        let pairs_to_check: Vec<(usize, CandidatePair)> = {
            let pairs = self.candidate_pairs.read().await;
            pairs
                .iter()
                .enumerate()
                .filter(|(_, p)| p.state == PairState::Waiting)
                .map(|(i, p)| (i, p.clone()))
                .collect()
        };

        // Perform connectivity checks
        for (idx, pair) in pairs_to_check {
            // Mark as in-progress
            {
                let mut pairs = self.candidate_pairs.write().await;
                let pair_state = pairs.get_mut(idx).expect("candidate pair missing");
                pair_state.state = PairState::InProgress;
            }

            debug!(
                "Checking pair {} <-> {}",
                pair.local.address, pair.remote.address
            );

            // Find the socket for this local candidate
            let socket = {
                let sockets = self.sockets.read().await;
                // For host candidates, use the candidate's address
                // For srflx/relay, use the related address (base)
                let socket_addr = pair.local.related_address.unwrap_or(pair.local.address);
                sockets.get(&socket_addr).cloned()
            };

            let check_result = match socket {
                Some(sock) => {
                    self.perform_connectivity_check(&sock, &pair, &remote_ufrag, &remote_pwd)
                        .await
                }
                None => {
                    warn!("No socket for local candidate {}", pair.local.address);
                    false
                }
            };
            // `sock` is an `Arc<UdpSocket>`, so the dispatcher's clone
            // remains live regardless of this scope.

            // Update pair state
            let mut succeeded = false;
            {
                let mut pairs = self.candidate_pairs.write().await;
                let pair_state = pairs.get_mut(idx).expect("candidate pair missing");
                if check_result {
                    pair_state.state = PairState::Succeeded;
                    info!(
                        "Connectivity check succeeded: {} <-> {}",
                        pair.local.address, pair.remote.address
                    );

                    // If controlling, nominate this pair
                    if self.role == IceRole::Controlling {
                        pair_state.nominated = true;
                    }

                    // Select this pair
                    let mut selected = self.selected_pair.write().await;
                    *selected = Some(pair_state.clone());
                    *self.state.write().await = IceState::Connected;
                    succeeded = true;
                } else {
                    pair_state.state = PairState::Failed;
                    debug!(
                        "Connectivity check failed: {} <-> {}",
                        pair.local.address, pair.remote.address
                    );
                }
            }

            if succeeded {
                // Unfreeze other pairs with same foundation (for triggered checks)
                self.unfreeze_related_pairs(idx, &pair).await;
                return Ok(());
            }
        }

        // No successful checks, try to find any valid pair
        self.fallback_selection().await
    }

    /// Perform a single connectivity check on a candidate pair.
    ///
    /// Sends a STUN Binding Request with ICE credentials (USERNAME, MESSAGE-INTEGRITY)
    /// and validates the response.
    async fn perform_connectivity_check(
        &self,
        socket: &Arc<UdpSocket>,
        pair: &CandidatePair,
        remote_ufrag: &str,
        remote_pwd: &str,
    ) -> bool {
        use bytes::{BufMut, BytesMut};
        use hmac::{Hmac, Mac};
        use rand::RngCore;
        use sha1::Sha1;
        use std::time::Duration;

        const ATTR_PRIORITY: u16 = 0x0024;
        const ATTR_ICE_CONTROLLING: u16 = 0x802A;
        const ATTR_ICE_CONTROLLED: u16 = 0x8029;

        type HmacSha1 = Hmac<Sha1>;

        // Make sure the dispatcher is running for this socket so we
        // can receive responses (and answer the peer's checks too).
        let local_addr = match socket.local_addr() {
            Ok(a) => a,
            Err(e) => {
                warn!("Cannot resolve local addr for connectivity check: {}", e);
                return false;
            }
        };
        self.ensure_dispatcher(local_addr, socket.clone()).await;

        // Generate transaction ID
        let mut txn_id = [0u8; 12];
        rand::thread_rng().fill_bytes(&mut txn_id);

        // Build ICE username: remote_ufrag:local_ufrag
        let username = format!("{}:{}", remote_ufrag, self.config.local_ufrag);

        // Build attributes
        let mut attrs = BytesMut::new();

        // USERNAME attribute
        let username_bytes = username.as_bytes();
        attrs.put_u16(ATTR_USERNAME);
        attrs.put_u16(username_bytes.len() as u16);
        attrs.put_slice(username_bytes);
        // Pad to 4-byte boundary
        let padding = (4 - (username_bytes.len() % 4)) % 4;
        for _ in 0..padding {
            attrs.put_u8(0);
        }

        // PRIORITY attribute (our priority for this candidate)
        attrs.put_u16(ATTR_PRIORITY);
        attrs.put_u16(4);
        attrs.put_u32(pair.local.priority);

        // ICE-CONTROLLING or ICE-CONTROLLED with tie-breaker
        let mut tie_breaker = [0u8; 8];
        rand::thread_rng().fill_bytes(&mut tie_breaker);

        match self.role {
            IceRole::Controlling => {
                attrs.put_u16(ATTR_ICE_CONTROLLING);
                attrs.put_u16(8);
                attrs.put_slice(&tie_breaker);
            }
            IceRole::Controlled => {
                attrs.put_u16(ATTR_ICE_CONTROLLED);
                attrs.put_u16(8);
                attrs.put_slice(&tie_breaker);
            }
        }

        // Build message header (length will be updated for MESSAGE-INTEGRITY)
        let mut msg = BytesMut::with_capacity(20 + attrs.len() + 24);
        msg.put_u16(BINDING_REQUEST);
        msg.put_u16(attrs.len() as u16);
        msg.put_u32(MAGIC_COOKIE);
        msg.put_slice(&txn_id);
        msg.put_slice(&attrs);

        // Add MESSAGE-INTEGRITY using remote password as key (short-term credential)
        // For ICE, the key is the remote password directly (not MD5 hashed)
        {
            let current_len = msg.len();
            let new_len = (current_len - 20 + 24) as u16;
            msg[2] = (new_len >> 8) as u8;
            msg[3] = (new_len & 0xFF) as u8;

            let mut mac = HmacSha1::new_from_slice(remote_pwd.as_bytes())
                .expect("HMAC can take any key size");
            mac.update(&msg);
            let integrity = mac.finalize().into_bytes();

            msg.put_u16(ATTR_MESSAGE_INTEGRITY);
            msg.put_u16(20);
            msg.put_slice(&integrity);
        }

        // Send + await response via the dispatcher.
        let target_addr = pair.remote.address;
        let check_timeout = Duration::from_millis(self.config.check_timeout_ms);

        match self
            .send_and_await_response(socket, target_addr, &msg, txn_id, check_timeout)
            .await
        {
            Ok(response_bytes) => {
                // Dispatcher matches by transaction ID and forwards
                // any well-formed-length packet — re-validate the
                // STUN-level fields here so a malformed reply doesn't
                // count as a successful check.
                if response_bytes.len() < 20 {
                    debug!(
                        "Connectivity check response from {} too short ({} bytes)",
                        target_addr,
                        response_bytes.len()
                    );
                    return false;
                }
                let resp_type = u16::from_be_bytes([response_bytes[0], response_bytes[1]]);
                let resp_cookie = u32::from_be_bytes([
                    response_bytes[4],
                    response_bytes[5],
                    response_bytes[6],
                    response_bytes[7],
                ]);
                if resp_type != BINDING_RESPONSE || resp_cookie != MAGIC_COOKIE {
                    debug!(
                        "Connectivity check response from {} malformed (type=0x{:04x} cookie=0x{:08x})",
                        target_addr, resp_type, resp_cookie
                    );
                    return false;
                }
                debug!(
                    "Received valid STUN response for connectivity check to {}",
                    target_addr
                );
                true
            }
            Err(e) => {
                debug!(
                    "Connectivity check to {} did not complete: {}",
                    target_addr, e
                );
                false
            }
        }
    }

    /// Unfreeze candidate pairs with the same foundation after a successful check.
    async fn unfreeze_related_pairs(&self, succeeded_idx: usize, succeeded: &CandidatePair) {
        let mut pairs = self.candidate_pairs.write().await;
        for (i, pair) in pairs.iter_mut().enumerate() {
            if i != succeeded_idx
                && pair.state == PairState::Frozen
                && pair.local.foundation == succeeded.local.foundation
            {
                pair.state = PairState::Waiting;
            }
        }
    }

    /// Fallback selection when connectivity checks aren't possible.
    async fn fallback_selection(&self) -> Result<(), IceError> {
        let pairs = self.candidate_pairs.read().await;

        // Try host-to-host first
        for pair in pairs.iter() {
            if pair.local.candidate_type == CandidateType::Host
                && pair.remote.candidate_type == CandidateType::Host
            {
                let mut selected = self.selected_pair.write().await;
                *selected = Some(pair.clone());
                *self.state.write().await = IceState::Connected;
                info!(
                    "Fallback: Selected host-to-host pair: {} <-> {}",
                    pair.local.address, pair.remote.address
                );
                return Ok(());
            }
        }

        // Try any pair
        if let Some(pair) = pairs.first() {
            let mut selected = self.selected_pair.write().await;
            *selected = Some(pair.clone());
            *self.state.write().await = IceState::Connected;
            info!(
                "Fallback: Selected first available pair: {} <-> {}",
                pair.local.address, pair.remote.address
            );
            return Ok(());
        }

        *self.state.write().await = IceState::Failed;
        Err(IceError::NoCandidates)
    }

    /// Stop the per-socket STUN responder dispatchers without
    /// otherwise tearing down the agent.
    ///
    /// Sockets stay bound and remain in the agent's `sockets` map, so
    /// `socket_for(...)` keeps returning the same `Arc<UdpSocket>` and
    /// any clones the caller already obtained continue to receive
    /// traffic. Cloned `rtp_socket()` handles held by callers (e.g.
    /// the RTP loop) are unaffected by this call beyond the
    /// dispatcher releasing its own clone.
    ///
    /// Use this once a validated pair has been selected: the caller
    /// is taking the socket for RTP, and we no longer want the
    /// dispatcher consuming inbound packets out from under them.
    /// `close()` remains the full-shutdown path.
    pub async fn stop_responders(&self) {
        let mut tasks = self.responder_tasks.write().await;
        for (_addr, handle) in tasks.drain() {
            handle.abort();
        }
    }

    /// Close the ICE agent.
    pub async fn close(&self) {
        *self.state.write().await = IceState::Closed;
        self.sockets.write().await.clear();
        // Abort all dispatcher tasks. Cloned `Arc<UdpSocket>` handles
        // held by callers (e.g. RTP) remain alive — only our reference
        // is dropped.
        let mut tasks = self.responder_tasks.write().await;
        for (_addr, handle) in tasks.drain() {
            handle.abort();
        }
        self.pending_responses.lock().await.clear();
    }
}

/// Get local IP addresses suitable for ICE.
fn get_local_addresses() -> Vec<IpAddr> {
    get_local_addresses_with(bind_default, connect_default, local_addr_default)
}

#[cfg(test)]
static FORCE_HOST_LOCAL_ADDR_ERROR: AtomicU64 = AtomicU64::new(0);

#[cfg(test)]
fn force_host_local_addr_error_once() {
    FORCE_HOST_LOCAL_ADDR_ERROR.store(current_thread_id(), Ordering::SeqCst);
}

#[cfg(test)]
fn take_forced_host_local_addr_error() -> Option<std::io::Error> {
    let current = current_thread_id();
    if FORCE_HOST_LOCAL_ADDR_ERROR.load(Ordering::SeqCst) == current {
        let _ = FORCE_HOST_LOCAL_ADDR_ERROR.compare_exchange(
            current,
            0,
            Ordering::SeqCst,
            Ordering::SeqCst,
        );
        Some(std::io::Error::other("forced local_addr error"))
    } else {
        None
    }
}

#[cfg(test)]
fn current_thread_id() -> u64 {
    use std::hash::{Hash, Hasher};

    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    std::thread::current().id().hash(&mut hasher);
    let id = hasher.finish();
    normalize_thread_id(id)
}

#[cfg(test)]
fn normalize_thread_id(id: u64) -> u64 {
    if id == 0 {
        1
    } else {
        id
    }
}

/// Per-socket STUN dispatcher loop. Reads inbound packets and either
/// answers binding requests (with `MESSAGE-INTEGRITY` validated against
/// `local_pwd`) or forwards binding responses to whichever outgoing
/// transaction is awaiting them.
async fn stun_dispatcher_loop(
    socket: Arc<UdpSocket>,
    pending: PendingResponses,
    local_ufrag: String,
    local_pwd: String,
) {
    // 1500 covers a typical Ethernet MTU. Real STUN messages are far
    // smaller (a few hundred bytes), and `recv_from` truncates rather
    // than failing on oversize, so we log and drop oversized packets.
    let mut buf = vec![0u8; 1500];
    loop {
        let (len, from) = match socket.recv_from(&mut buf).await {
            Ok(t) => t,
            Err(e) => {
                debug!("STUN dispatcher recv error, exiting: {}", e);
                return;
            }
        };
        if len == buf.len() {
            // Datagram filled the buffer exactly; UDP has no fragment
            // signal here. Anything legitimate from a STUN peer fits
            // well below 1500 bytes — log and drop rather than forward
            // a possibly-truncated message.
            debug!(
                "STUN dispatcher dropped possibly-truncated {}-byte datagram from {}",
                len, from
            );
            continue;
        }
        if len < 20 {
            continue;
        }
        let data = &buf[..len];
        let msg_type = u16::from_be_bytes([data[0], data[1]]);
        let cookie = u32::from_be_bytes([data[4], data[5], data[6], data[7]]);
        let mut txn_id = [0u8; 12];
        txn_id.copy_from_slice(&data[8..20]);

        // Forward to a pending outgoing transaction first if the txn
        // ID matches, regardless of msg_type/cookie. This lets the
        // caller (`stun_binding_request`, `perform_connectivity_check`)
        // re-validate and surface `StunError::InvalidResponse` for
        // malformed replies — staying permissive here keeps the
        // public API contract intact. Only if no transaction is
        // waiting for this ID do we treat the packet as a peer
        // binding request.
        let pending_match = pending.lock().await.remove(&txn_id);
        if let Some(tx) = pending_match {
            let _ = tx.send(data.to_vec());
            continue;
        }

        if cookie == MAGIC_COOKIE && msg_type == BINDING_REQUEST {
            let owned: Vec<u8> = data.to_vec();
            if let Some(resp) =
                build_binding_response(&owned, txn_id, from, &local_ufrag, &local_pwd)
            {
                if let Err(e) = socket.send_to(&resp, from).await {
                    debug!("Failed to send STUN binding response to {}: {}", from, e);
                }
            }
        } else {
            // Unmatched non-request (stray response, indication, or
            // bad-cookie noise): drop silently.
            debug!(
                "STUN dispatcher dropped unmatched packet from {} (type=0x{:04x})",
                from, msg_type
            );
        }
    }
}

/// Validate an inbound STUN binding request and build the success
/// response bytes. Returns `None` if validation fails (the dispatcher
/// then drops the packet silently — RFC 5389 permits a STUN error
/// response, but staying silent is simpler and safer).
fn build_binding_response(
    request: &[u8],
    txn_id: [u8; 12],
    from: SocketAddr,
    local_ufrag: &str,
    local_pwd: &str,
) -> Option<Vec<u8>> {
    use bytes::{BufMut, BytesMut};
    use hmac::{Hmac, Mac};
    use sha1::Sha1;

    type HmacSha1 = Hmac<Sha1>;

    if request.len() < 20 {
        return None;
    }
    let total_attrs_len = u16::from_be_bytes([request[2], request[3]]) as usize;
    if request.len() < 20 + total_attrs_len {
        return None;
    }

    // Walk attributes, locate USERNAME and MESSAGE-INTEGRITY.
    let mut username: Option<&[u8]> = None;
    let mut mi_offset: Option<usize> = None;
    let mut mi_value: Option<&[u8]> = None;

    let mut off = 20;
    let attrs_end = 20 + total_attrs_len;
    while off + 4 <= attrs_end {
        let attr_type = u16::from_be_bytes([request[off], request[off + 1]]);
        let attr_len = u16::from_be_bytes([request[off + 2], request[off + 3]]) as usize;
        let val_start = off + 4;
        let val_end = val_start + attr_len;
        if val_end > attrs_end {
            return None;
        }
        match attr_type {
            ATTR_USERNAME => {
                username = Some(&request[val_start..val_end]);
            }
            ATTR_MESSAGE_INTEGRITY => {
                if attr_len != 20 {
                    return None;
                }
                mi_offset = Some(off);
                mi_value = Some(&request[val_start..val_end]);
                // RFC 5389 §15.4: attributes after MESSAGE-INTEGRITY
                // are not covered by the HMAC, so trusting them would
                // let an attacker append arbitrary unauthenticated
                // attributes. Stop here.
                break;
            }
            _ => {}
        }
        // Advance past padded attribute.
        let padded = (attr_len + 3) & !3;
        off = val_start + padded;
    }

    let username = username?;
    let mi_offset = mi_offset?;
    let mi_value = mi_value?;

    // RFC 5389 §10.1.2: verify MESSAGE-INTEGRITY *before* USERNAME.
    // An attacker who can guess our local ufrag must not learn whether
    // their MI guess was right; we reject all unauthenticated traffic
    // identically (silent drop). Validating MI first also avoids
    // doing a string comparison on attacker-controlled input before
    // the cryptographic check.
    let mut signed = request[..mi_offset].to_vec();
    let new_len = (mi_offset - 20 + 24) as u16;
    signed[2] = (new_len >> 8) as u8;
    signed[3] = (new_len & 0xff) as u8;

    let mut mac =
        HmacSha1::new_from_slice(local_pwd.as_bytes()).expect("HMAC accepts any key length");
    mac.update(&signed);
    if mac.verify_slice(mi_value).is_err() {
        debug!("STUN request rejected: MESSAGE-INTEGRITY mismatch");
        return None;
    }

    // USERNAME must be of the form `local_ufrag:peer_ufrag`. We
    // verify only the first component matches our local ufrag —
    // peers send `<their_remote_ufrag>:<their_local_ufrag>` and our
    // remote_ufrag (from peer's POV) is our local_ufrag.
    let username_str = std::str::from_utf8(username).ok()?;
    let mut split = username_str.splitn(2, ':');
    let first = split.next()?;
    if first != local_ufrag {
        debug!(
            "STUN request rejected: USERNAME prefix `{}` != local ufrag",
            first
        );
        return None;
    }

    // Build success response: BINDING_RESPONSE + magic + txn_id +
    // XOR-MAPPED-ADDRESS + MESSAGE-INTEGRITY (signed with local_pwd).
    let mut attrs = BytesMut::new();
    match from {
        SocketAddr::V4(addr) => {
            attrs.put_u16(ATTR_XOR_MAPPED_ADDRESS);
            attrs.put_u16(8);
            attrs.put_u8(0);
            attrs.put_u8(0x01); // IPv4
            let xor_port = addr.port() ^ (MAGIC_COOKIE >> 16) as u16;
            attrs.put_u16(xor_port);
            let ip = addr.ip().octets();
            let cookie = MAGIC_COOKIE.to_be_bytes();
            for i in 0..4 {
                attrs.put_u8(ip[i] ^ cookie[i]);
            }
        }
        SocketAddr::V6(addr) => {
            // RFC 5389 §15.2: IPv6 XOR-MAPPED-ADDRESS is 24 bytes —
            // family 0x02, port XORed with the high 16 bits of the
            // magic cookie, address XORed with the magic cookie
            // followed by the 12-byte transaction ID.
            attrs.put_u16(ATTR_XOR_MAPPED_ADDRESS);
            attrs.put_u16(20);
            attrs.put_u8(0);
            attrs.put_u8(0x02); // IPv6
            let xor_port = addr.port() ^ (MAGIC_COOKIE >> 16) as u16;
            attrs.put_u16(xor_port);
            let ip = addr.ip().octets();
            let cookie = MAGIC_COOKIE.to_be_bytes();
            for i in 0..4 {
                attrs.put_u8(ip[i] ^ cookie[i]);
            }
            for i in 0..12 {
                attrs.put_u8(ip[4 + i] ^ txn_id[i]);
            }
        }
    }

    let mut msg = BytesMut::with_capacity(20 + attrs.len() + 24);
    msg.put_u16(BINDING_RESPONSE);
    // Will be patched before HMAC.
    msg.put_u16(attrs.len() as u16);
    msg.put_u32(MAGIC_COOKIE);
    msg.put_slice(&txn_id);
    msg.put_slice(&attrs);

    {
        let current_len = msg.len();
        let new_len = (current_len - 20 + 24) as u16;
        msg[2] = (new_len >> 8) as u8;
        msg[3] = (new_len & 0xff) as u8;

        let mut mac =
            HmacSha1::new_from_slice(local_pwd.as_bytes()).expect("HMAC accepts any key length");
        mac.update(&msg);
        let integrity = mac.finalize().into_bytes();

        msg.put_u16(ATTR_MESSAGE_INTEGRITY);
        msg.put_u16(20);
        msg.put_slice(&integrity);
    }

    Some(msg.to_vec())
}

fn socket_local_addr(socket: &UdpSocket) -> Result<SocketAddr, IceError> {
    #[cfg(test)]
    if let Some(err) = take_forced_host_local_addr_error() {
        return Err(IceError::Io(err));
    }
    socket.local_addr().map_err(IceError::Io)
}

fn bind_default() -> std::io::Result<std::net::UdpSocket> {
    std::net::UdpSocket::bind("0.0.0.0:0")
}

fn connect_default(socket: &std::net::UdpSocket) -> std::io::Result<()> {
    socket.connect("8.8.8.8:80")
}

fn local_addr_default(socket: &std::net::UdpSocket) -> std::io::Result<std::net::SocketAddr> {
    socket.local_addr()
}

fn get_local_addresses_with(
    bind: fn() -> std::io::Result<std::net::UdpSocket>,
    connect: fn(&std::net::UdpSocket) -> std::io::Result<()>,
    local_addr: fn(&std::net::UdpSocket) -> std::io::Result<std::net::SocketAddr>,
) -> Vec<IpAddr> {
    let mut addrs = Vec::new();

    // Try to get addresses by connecting to a public address
    // This finds the default route interface
    if let Ok(socket) = bind() {
        if connect(&socket).is_ok() {
            if let Ok(local) = local_addr(&socket) {
                addrs.push(local.ip());
            }
        }
    }

    // Also include localhost for testing
    addrs.push(IpAddr::V4(std::net::Ipv4Addr::LOCALHOST));

    addrs.dedup();
    addrs
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ice::Transport;
    use std::sync::Once;

    fn init_tracing() {
        static INIT: Once = Once::new();
        INIT.call_once(|| {
            let _ = tracing_subscriber::fmt()
                .with_max_level(tracing::Level::TRACE)
                .with_test_writer()
                .try_init();
        });
    }

    #[test]
    fn test_normalize_thread_id_branches() {
        assert_eq!(normalize_thread_id(0), 1);
        assert_eq!(normalize_thread_id(42), 42);
    }

    // IceError tests
    #[test]
    fn test_ice_error_io() {
        let io_err = std::io::Error::other("test");
        let err: IceError = io_err.into();
        assert!(err.to_string().contains("IO error"));
    }

    #[test]
    fn test_ice_error_no_candidates() {
        let err = IceError::NoCandidates;
        assert!(err.to_string().contains("No candidates"));
    }

    #[test]
    fn test_ice_error_failed() {
        let err = IceError::Failed("test reason".to_string());
        assert!(err.to_string().contains("test reason"));
    }

    #[test]
    fn test_ice_error_debug() {
        let err = IceError::NoCandidates;
        let debug = format!("{:?}", err);
        assert!(debug.contains("NoCandidates"));
    }

    // IceRole tests
    #[test]
    fn test_ice_role_debug() {
        assert!(format!("{:?}", IceRole::Controlling).contains("Controlling"));
        assert!(format!("{:?}", IceRole::Controlled).contains("Controlled"));
    }

    #[test]
    fn test_ice_role_clone() {
        let role = IceRole::Controlling;
        let cloned = role;
        assert_eq!(role, cloned);
    }

    #[test]
    fn test_ice_role_eq() {
        assert_eq!(IceRole::Controlling, IceRole::Controlling);
        assert_ne!(IceRole::Controlling, IceRole::Controlled);
    }

    // IceState tests
    #[test]
    fn test_ice_state_debug() {
        assert!(format!("{:?}", IceState::New).contains("New"));
        assert!(format!("{:?}", IceState::Gathering).contains("Gathering"));
        assert!(format!("{:?}", IceState::Checking).contains("Checking"));
        assert!(format!("{:?}", IceState::Connected).contains("Connected"));
        assert!(format!("{:?}", IceState::Completed).contains("Completed"));
        assert!(format!("{:?}", IceState::Failed).contains("Failed"));
        assert!(format!("{:?}", IceState::Closed).contains("Closed"));
    }

    #[test]
    fn test_ice_state_clone() {
        let state = IceState::Connected;
        let cloned = state;
        assert_eq!(state, cloned);
    }

    #[test]
    fn test_ice_state_eq() {
        assert_eq!(IceState::New, IceState::New);
        assert_ne!(IceState::New, IceState::Closed);
    }

    // PairState tests
    #[test]
    fn test_pair_state_debug() {
        assert!(format!("{:?}", PairState::Waiting).contains("Waiting"));
        assert!(format!("{:?}", PairState::InProgress).contains("InProgress"));
        assert!(format!("{:?}", PairState::Succeeded).contains("Succeeded"));
        assert!(format!("{:?}", PairState::Failed).contains("Failed"));
        assert!(format!("{:?}", PairState::Frozen).contains("Frozen"));
    }

    #[test]
    fn test_pair_state_clone() {
        let state = PairState::Succeeded;
        let cloned = state;
        assert_eq!(state, cloned);
    }

    #[test]
    fn test_pair_state_eq() {
        assert_eq!(PairState::Waiting, PairState::Waiting);
        assert_ne!(PairState::Waiting, PairState::Failed);
    }

    // CandidatePair tests
    #[test]
    fn test_candidate_pair_debug() {
        let local = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::LOCALHOST), 5000),
            1,
        );
        let remote = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );
        let pair = CandidatePair {
            local,
            remote,
            priority: 1000,
            state: PairState::Waiting,
            nominated: false,
        };
        let debug = format!("{:?}", pair);
        assert!(debug.contains("CandidatePair"));
    }

    #[test]
    fn test_candidate_pair_clone() {
        let local = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::LOCALHOST), 5000),
            1,
        );
        let remote = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );
        let pair = CandidatePair {
            local,
            remote,
            priority: 1000,
            state: PairState::Waiting,
            nominated: false,
        };
        let cloned = pair.clone();
        assert_eq!(cloned.priority, pair.priority);
        assert_eq!(cloned.state, pair.state);
    }

    // IceConfig tests
    #[test]
    fn test_ice_config_default() {
        let config = IceConfig::default();
        assert!(!config.stun_servers.is_empty());
        assert_eq!(config.local_ufrag.len(), 8);
        assert_eq!(config.local_pwd.len(), 24);
        assert_eq!(config.gather_timeout_ms, 1500);
        assert_eq!(config.check_timeout_ms, 3000);
    }

    #[test]
    fn test_ice_config_custom() {
        let config = IceConfig {
            stun_servers: vec![],
            local_ufrag: "myufrag1".to_string(),
            local_pwd: "mypasswordisverysecret1!".to_string(),
            gather_timeout_ms: 10000,
            check_timeout_ms: 5000,
        };
        assert!(config.stun_servers.is_empty());
        assert_eq!(config.local_ufrag, "myufrag1");
        assert_eq!(config.gather_timeout_ms, 10000);
    }

    #[test]
    fn test_ice_config_debug() {
        let config = IceConfig::default();
        let debug = format!("{:?}", config);
        assert!(debug.contains("IceConfig"));
    }

    #[test]
    fn test_ice_config_clone() {
        let config = IceConfig::default();
        let cloned = config.clone();
        assert_eq!(cloned.gather_timeout_ms, config.gather_timeout_ms);
    }

    // Credential generation tests
    #[test]
    fn test_generate_ufrag() {
        let ufrag = generate_ice_ufrag();
        assert_eq!(ufrag.len(), 8);
        assert!(ufrag.chars().all(|c| c.is_ascii_alphanumeric()));
    }

    #[test]
    fn test_generate_pwd() {
        let pwd = generate_ice_pwd();
        assert_eq!(pwd.len(), 24);
        assert!(pwd.chars().all(|c| c.is_ascii_alphanumeric()));
    }

    #[test]
    fn test_generate_ufrag_uniqueness() {
        let ufrag1 = generate_ice_ufrag();
        let ufrag2 = generate_ice_ufrag();
        assert_ne!(ufrag1, ufrag2);
    }

    #[test]
    fn test_generate_pwd_uniqueness() {
        let pwd1 = generate_ice_pwd();
        let pwd2 = generate_ice_pwd();
        assert_ne!(pwd1, pwd2);
    }

    // get_local_addresses tests
    fn bind_ok() -> std::io::Result<std::net::UdpSocket> {
        std::net::UdpSocket::bind("127.0.0.1:0")
    }

    fn bind_err() -> std::io::Result<std::net::UdpSocket> {
        Err(std::io::Error::other("bind failed"))
    }

    fn connect_ok(socket: &std::net::UdpSocket) -> std::io::Result<()> {
        socket.connect("127.0.0.1:9")
    }

    fn connect_err(_socket: &std::net::UdpSocket) -> std::io::Result<()> {
        Err(std::io::Error::other("connect failed"))
    }

    fn local_addr_ok(socket: &std::net::UdpSocket) -> std::io::Result<std::net::SocketAddr> {
        socket.local_addr()
    }

    fn local_addr_err(_socket: &std::net::UdpSocket) -> std::io::Result<std::net::SocketAddr> {
        Err(std::io::Error::other("addr failed"))
    }

    #[test]
    fn test_get_local_addresses() {
        let addrs = get_local_addresses();
        // Should at least contain localhost
        assert!(!addrs.is_empty());
        assert!(addrs.contains(&IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)));
    }

    #[test]
    fn test_get_local_addresses_with_bind_error() {
        let addrs = get_local_addresses_with(bind_err, connect_ok, local_addr_ok);
        assert_eq!(addrs, vec![IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)]);
    }

    #[test]
    fn test_get_local_addresses_with_connect_error() {
        let addrs = get_local_addresses_with(bind_ok, connect_err, local_addr_ok);
        assert_eq!(addrs, vec![IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)]);
    }

    #[test]
    fn test_get_local_addresses_with_local_addr_error() {
        let addrs = get_local_addresses_with(bind_ok, connect_ok, local_addr_err);
        assert_eq!(addrs, vec![IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)]);
    }

    #[test]
    fn test_get_local_addresses_with_success() {
        let addrs = get_local_addresses_with(bind_ok, connect_ok, local_addr_ok);
        assert_eq!(addrs, vec![IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)]);
    }

    // IceAgent tests
    #[tokio::test]
    async fn test_ice_agent_creation() {
        let config = IceConfig::default();
        let agent = IceAgent::new(config, IceRole::Controlling);

        assert_eq!(agent.state().await, IceState::New);
    }

    #[tokio::test]
    async fn test_ice_agent_controlled_role() {
        let config = IceConfig::default();
        let agent = IceAgent::new(config, IceRole::Controlled);

        assert_eq!(agent.role, IceRole::Controlled);
        assert_eq!(agent.state().await, IceState::New);
    }

    #[tokio::test]
    async fn test_local_credentials() {
        let config = IceConfig {
            stun_servers: vec![],
            local_ufrag: "testufrag".to_string(),
            local_pwd: "testpasswordverysecure".to_string(),
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let (ufrag, pwd) = agent.local_credentials();
        assert_eq!(ufrag, "testufrag");
        assert_eq!(pwd, "testpasswordverysecure");
    }

    #[tokio::test]
    async fn test_gather_host_candidates() {
        // Use empty STUN servers to avoid network calls
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let candidates = agent.gather_candidates().await.unwrap();

        // Should have at least one host candidate
        assert!(!candidates.is_empty());
        assert!(candidates
            .iter()
            .any(|c| c.candidate_type == CandidateType::Host));
    }

    #[tokio::test]
    async fn test_gather_host_candidates_with_bind_error() {
        init_tracing();
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let addrs = vec![
            IpAddr::V4(std::net::Ipv4Addr::new(203, 0, 113, 1)),
            IpAddr::V4(std::net::Ipv4Addr::LOCALHOST),
        ];
        let candidates = agent.gather_host_candidates_with(addrs).await.unwrap();
        assert!(candidates
            .iter()
            .any(|c| c.address.ip() == IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)));
    }

    #[tokio::test]
    async fn test_local_candidates_accessor() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Initially empty
        assert!(agent.local_candidates().await.is_empty());

        // After gathering
        let _ = agent.gather_candidates().await;
        assert!(!agent.local_candidates().await.is_empty());
    }

    #[tokio::test]
    async fn test_form_candidate_pairs() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Directly add local and remote candidates without network
        let local = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );
        agent.local_candidates.write().await.push(local);

        let remote = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 100)), 5000),
            1,
        );
        agent.add_remote_candidates(vec![remote]).await;

        // Check that pairs were formed
        let pairs = agent.candidate_pairs.read().await;
        assert_eq!(pairs.len(), 1);
        assert_eq!(pairs[0].state, PairState::Waiting);
    }

    #[tokio::test]
    async fn test_form_candidate_pairs_different_components() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Add local candidate for component 1
        let local = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );
        agent.local_candidates.write().await.push(local);

        // Add remote candidate for component 2 (shouldn't pair)
        let remote = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 100)), 5000),
            2, // Different component
        );
        agent.add_remote_candidates(vec![remote]).await;

        // No pairs should be formed due to component mismatch
        let pairs = agent.candidate_pairs.read().await;
        assert_eq!(pairs.len(), 0);
    }

    #[tokio::test]
    async fn test_form_candidate_pairs_skips_mismatched_transport() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let mut local = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );
        local.transport = Transport::Tcp;
        agent.local_candidates.write().await.push(local);

        let remote = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 100)), 5000),
            1,
        );
        agent.remote_candidates.write().await.push(remote);

        agent.form_candidate_pairs().await;

        let pairs = agent.candidate_pairs.read().await;
        assert!(pairs.is_empty());
    }

    #[tokio::test]
    async fn test_set_remote_credentials() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Initially no remote credentials
        assert!(agent.remote_credentials().await.is_none());

        // Set credentials
        agent.set_remote_credentials("abc123", "password456").await;

        // Verify they're set
        let creds = agent.remote_credentials().await;
        assert!(creds.is_some());
        let (ufrag, pwd) = creds.unwrap();
        assert_eq!(ufrag, "abc123");
        assert_eq!(pwd, "password456");
    }

    #[tokio::test]
    async fn test_selected_pair_initially_none() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        assert!(agent.selected_pair().await.is_none());
    }

    #[tokio::test]
    async fn test_start_checks_missing_socket_falls_back() {
        init_tracing();
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );
        agent.local_candidates.write().await.push(local);

        let remote = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 100)), 5000),
            1,
        );
        agent.add_remote_candidates(vec![remote]).await;
        agent.set_remote_credentials("remufrag", "rempwd").await;

        let result = agent.start_checks().await;
        assert!(result.is_ok());
        assert_eq!(agent.state().await, IceState::Connected);
    }

    #[tokio::test]
    async fn test_start_checks_success() {
        init_tracing();
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let local_addr = local_socket.local_addr().unwrap();
        let local = Candidate::host(local_addr, 1);
        agent.local_candidates.write().await.push(local.clone());
        agent
            .sockets
            .write()
            .await
            .insert(local.address, local_socket.clone());

        let mock = MockStunServer::new().await;
        let remote = Candidate::host(mock.addr, 1);
        agent.add_remote_candidates(vec![remote]).await;
        agent
            .set_remote_credentials("remoteufrag", "remotepwd")
            .await;

        let mock_socket = mock.socket.clone();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            let mut txn_id = [0u8; 12];
            txn_id.copy_from_slice(&buf[8..20]);
            let response = MockStunServer::build_connectivity_check_response(&txn_id);
            let _ = mock_socket.send_to(&response, peer).await;
        });

        let result = agent.start_checks().await;
        assert!(result.is_ok());
        let selected = agent.selected_pair().await.unwrap();
        assert_eq!(selected.state, PairState::Succeeded);
        assert!(selected.nominated);
    }

    #[tokio::test]
    async fn test_perform_connectivity_check_send_error() {
        init_tracing();
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let local_addr = local_socket.local_addr().unwrap();
        let local_cand = Candidate::host(local_addr, 1);
        let remote_cand = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::LOCALHOST), 0),
            1,
        );
        let pair = CandidatePair {
            local: local_cand,
            remote: remote_cand,
            priority: 100,
            state: PairState::Waiting,
            nominated: false,
        };

        let result = agent
            .perform_connectivity_check(&local_socket, &pair, "remoteufrag", "remotepwd")
            .await;
        assert!(!result);
    }

    #[tokio::test]
    async fn test_fallback_selection() {
        init_tracing();
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Add local candidate
        let local = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );
        agent.local_candidates.write().await.push(local.clone());

        // Create socket for the local candidate
        let socket = UdpSocket::bind("127.0.0.1:0").await.unwrap();
        agent
            .sockets
            .write()
            .await
            .insert(local.address, Arc::new(socket));

        // Add remote candidate
        let remote = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 100)), 5000),
            1,
        );
        agent.add_remote_candidates(vec![remote]).await;

        // Without remote credentials, should use fallback
        let result = agent.start_checks().await;
        assert!(result.is_ok());

        // Should have selected the host-to-host pair
        let selected = agent.selected_pair().await;
        assert!(selected.is_some());
        let pair = selected.unwrap();
        assert_eq!(pair.local.candidate_type, CandidateType::Host);
        assert_eq!(pair.remote.candidate_type, CandidateType::Host);
    }

    #[tokio::test]
    async fn test_fallback_selection_non_host_pair() {
        init_tracing();
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let base = SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001);
        let mapped = SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 2)), 5002);
        let remote_mapped =
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 3)), 5003);

        let local_host = Candidate::host(base, 1);
        let remote_srflx = Candidate::server_reflexive(remote_mapped, base, 1);
        let local_srflx = Candidate::server_reflexive(mapped, base, 1);
        let remote_host = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 100)), 5000),
            1,
        );

        let pair1 = CandidatePair {
            local: local_host.clone(),
            remote: remote_srflx,
            priority: 1000,
            state: PairState::Waiting,
            nominated: false,
        };
        let pair2 = CandidatePair {
            local: local_srflx,
            remote: remote_host,
            priority: 900,
            state: PairState::Waiting,
            nominated: false,
        };
        {
            let mut pairs = agent.candidate_pairs.write().await;
            pairs.push(pair1);
            pairs.push(pair2);
        }

        let result = agent.fallback_selection().await;
        assert!(result.is_ok());

        let selected = agent.selected_pair().await;
        assert!(selected.is_some());
        let selected = selected.unwrap();
        assert_eq!(selected.local.candidate_type, CandidateType::Host);
        assert_eq!(
            selected.remote.candidate_type,
            CandidateType::ServerReflexive
        );
    }

    #[tokio::test]
    async fn test_fallback_no_candidates() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // No candidates added, should fail
        let result = agent.start_checks().await;
        assert!(result.is_err());
        assert!(agent.state().await == IceState::Failed);
    }

    #[tokio::test]
    async fn test_ice_state_transitions() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Initial state
        assert_eq!(agent.state().await, IceState::New);

        // After gathering
        let _ = agent.gather_candidates().await;
        assert_eq!(agent.state().await, IceState::Gathering);

        // Close
        agent.close().await;
        assert_eq!(agent.state().await, IceState::Closed);
    }

    #[tokio::test]
    async fn test_close_clears_sockets() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Gather to create sockets
        let _ = agent.gather_candidates().await;
        assert!(!agent.sockets.read().await.is_empty());

        // Close should clear sockets
        agent.close().await;
        assert!(agent.sockets.read().await.is_empty());
    }

    #[tokio::test]
    async fn test_socket_for_returns_bound_socket() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let candidates = agent.gather_candidates().await.expect("gather");
        // Pick any host candidate; its address must be a key in the
        // sockets map.
        let host = candidates
            .iter()
            .find(|c| c.candidate_type == CandidateType::Host)
            .expect("host candidate");
        let socket = agent.socket_for(host.address).await.expect("socket");
        assert_eq!(socket.local_addr().unwrap(), host.address);
    }

    #[tokio::test]
    async fn test_socket_for_unknown_address() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);
        let unknown = SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(203, 0, 113, 99)), 65000);
        assert!(agent.socket_for(unknown).await.is_none());
    }

    #[tokio::test]
    async fn test_multiple_remote_candidates() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Add local candidate
        let local = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );
        agent.local_candidates.write().await.push(local);

        // Add multiple remote candidates
        let remote1 = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 100)), 5000),
            1,
        );
        let remote2 = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 101)), 5000),
            1,
        );
        agent.add_remote_candidates(vec![remote1, remote2]).await;

        // Should have 2 pairs
        let pairs = agent.candidate_pairs.read().await;
        assert_eq!(pairs.len(), 2);
    }

    // ========== New Comprehensive Tests for Coverage ==========

    // Mock UDP server for testing STUN interactions
    struct MockStunServer {
        socket: Arc<UdpSocket>,
        addr: SocketAddr,
    }

    impl MockStunServer {
        async fn new() -> Self {
            let socket = UdpSocket::bind("127.0.0.1:0").await.unwrap();
            let addr = socket.local_addr().unwrap();
            Self {
                socket: Arc::new(socket),
                addr,
            }
        }

        // Build a STUN Binding Success Response with XOR-MAPPED-ADDRESS
        fn build_binding_response(txn_id: &[u8; 12], mapped_addr: SocketAddr) -> Vec<u8> {
            use bytes::{BufMut, BytesMut};

            const MAGIC_COOKIE: u32 = 0x2112A442;
            const BINDING_RESPONSE: u16 = 0x0101;
            const ATTR_XOR_MAPPED_ADDRESS: u16 = 0x0020;

            let mut attrs = BytesMut::new();

            // XOR-MAPPED-ADDRESS attribute
            match mapped_addr {
                SocketAddr::V4(addr) => {
                    attrs.put_u16(ATTR_XOR_MAPPED_ADDRESS);
                    attrs.put_u16(8); // Length
                    attrs.put_u8(0); // Reserved
                    attrs.put_u8(0x01); // Family: IPv4

                    // XOR port with magic cookie high 16 bits
                    let xor_port = addr.port() ^ (MAGIC_COOKIE >> 16) as u16;
                    attrs.put_u16(xor_port);

                    // XOR IP with magic cookie
                    let ip_bytes = addr.ip().octets();
                    let cookie_bytes = MAGIC_COOKIE.to_be_bytes();
                    for i in 0..4 {
                        attrs.put_u8(ip_bytes[i] ^ cookie_bytes[i]);
                    }
                }
                SocketAddr::V6(_) => {
                    // For simplicity, not implementing IPv6
                    panic!("IPv6 not implemented in mock");
                }
            }

            // Build message
            let mut msg = BytesMut::new();
            msg.put_u16(BINDING_RESPONSE);
            msg.put_u16(attrs.len() as u16);
            msg.put_u32(MAGIC_COOKIE);
            msg.put_slice(txn_id);
            msg.put_slice(&attrs);

            msg.to_vec()
        }

        // Build a STUN Binding Success Response for connectivity check (with MESSAGE-INTEGRITY validation)
        fn build_connectivity_check_response(txn_id: &[u8; 12]) -> Vec<u8> {
            use bytes::{BufMut, BytesMut};

            const MAGIC_COOKIE: u32 = 0x2112A442;
            const BINDING_RESPONSE: u16 = 0x0101;

            // Simple response with just header (minimal valid response)
            let mut msg = BytesMut::new();
            msg.put_u16(BINDING_RESPONSE);
            msg.put_u16(0); // No attributes
            msg.put_u32(MAGIC_COOKIE);
            msg.put_slice(txn_id);

            msg.to_vec()
        }
    }

    #[test]
    #[should_panic(expected = "IPv6 not implemented in mock")]
    fn test_mock_stun_server_ipv6_not_implemented() {
        let txn_id = [0u8; 12];
        let addr = SocketAddr::new(IpAddr::V6(std::net::Ipv6Addr::LOCALHOST), 9999);
        let _ = MockStunServer::build_binding_response(&txn_id, addr);
    }

    // Test: perform_connectivity_check with successful response
    #[tokio::test]
    async fn test_perform_connectivity_check_success() {
        init_tracing();
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Create local socket
        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let local_addr = local_socket.local_addr().unwrap();

        // Create mock remote server
        let mock = MockStunServer::new().await;
        let mock_addr = mock.addr;

        // Create candidate pair
        let local_cand = Candidate::host(local_addr, 1);
        let remote_cand = Candidate::host(mock_addr, 1);
        let pair = CandidatePair {
            local: local_cand,
            remote: remote_cand,
            priority: 1000,
            state: PairState::Waiting,
            nominated: false,
        };

        // Spawn mock server to respond
        let mock_socket = mock.socket.clone();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            let mut txn_id = [0u8; 12];
            txn_id.copy_from_slice(&buf[8..20]);

            // Send response
            let response = MockStunServer::build_connectivity_check_response(&txn_id);
            let _ = mock_socket.send_to(&response, peer).await;
        });

        // Perform connectivity check
        let result = agent
            .perform_connectivity_check(&local_socket, &pair, "remoteufrag", "remotepwd")
            .await;

        assert!(result);
    }

    // Test: perform_connectivity_check with timeout
    #[tokio::test]
    async fn test_perform_connectivity_check_timeout() {
        init_tracing();
        let config = IceConfig {
            stun_servers: vec![],
            check_timeout_ms: 100, // Very short timeout
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Create local socket
        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let local_addr = local_socket.local_addr().unwrap();

        // Use unreachable address for timeout
        let unreachable_addr =
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 0, 2, 1)), 9999);

        let local_cand = Candidate::host(local_addr, 1);
        let remote_cand = Candidate::host(unreachable_addr, 1);
        let pair = CandidatePair {
            local: local_cand,
            remote: remote_cand,
            priority: 1000,
            state: PairState::Waiting,
            nominated: false,
        };

        // Perform connectivity check - should timeout
        let result = agent
            .perform_connectivity_check(&local_socket, &pair, "remoteufrag", "remotepwd")
            .await;

        assert!(!result);
    }

    // Test: perform_connectivity_check with invalid response
    #[tokio::test]
    async fn test_perform_connectivity_check_invalid_response() {
        let config = IceConfig {
            stun_servers: vec![],
            check_timeout_ms: 500,
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let local_addr = local_socket.local_addr().unwrap();

        let mock = MockStunServer::new().await;
        let mock_addr = mock.addr;

        let local_cand = Candidate::host(local_addr, 1);
        let remote_cand = Candidate::host(mock_addr, 1);
        let pair = CandidatePair {
            local: local_cand,
            remote: remote_cand,
            priority: 1000,
            state: PairState::Waiting,
            nominated: false,
        };

        // Spawn mock server to send invalid response
        let mock_socket = mock.socket.clone();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            // Send invalid response (too short)
            let _ = mock_socket.send_to(&[0, 1, 2, 3], peer).await;
        });

        let result = agent
            .perform_connectivity_check(&local_socket, &pair, "remoteufrag", "remotepwd")
            .await;

        assert!(!result);
    }

    // Test: perform_connectivity_check with wrong message type
    #[tokio::test]
    async fn test_perform_connectivity_check_bad_message_type() {
        use bytes::{BufMut, BytesMut};

        let config = IceConfig {
            stun_servers: vec![],
            check_timeout_ms: 100,
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let local_addr = local_socket.local_addr().unwrap();

        let mock = MockStunServer::new().await;
        let mock_addr = mock.addr;

        let local_cand = Candidate::host(local_addr, 1);
        let remote_cand = Candidate::host(mock_addr, 1);
        let pair = CandidatePair {
            local: local_cand,
            remote: remote_cand,
            priority: 1000,
            state: PairState::Waiting,
            nominated: false,
        };

        let mock_socket = mock.socket.clone();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            let mut txn_id = [0u8; 12];
            txn_id.copy_from_slice(&buf[8..20]);

            let mut msg = BytesMut::new();
            msg.put_u16(0x0001); // Not a response
            msg.put_u16(0);
            msg.put_u32(0x2112A442);
            msg.put_slice(&txn_id);
            let _ = mock_socket.send_to(&msg, peer).await;
        });

        let result = agent
            .perform_connectivity_check(&local_socket, &pair, "remoteufrag", "remotepwd")
            .await;

        assert!(!result);
    }

    // Test: perform_connectivity_check with bad magic cookie
    #[tokio::test]
    async fn test_perform_connectivity_check_bad_cookie() {
        use bytes::{BufMut, BytesMut};

        let config = IceConfig {
            stun_servers: vec![],
            check_timeout_ms: 100,
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let local_addr = local_socket.local_addr().unwrap();

        let mock = MockStunServer::new().await;
        let mock_addr = mock.addr;

        let local_cand = Candidate::host(local_addr, 1);
        let remote_cand = Candidate::host(mock_addr, 1);
        let pair = CandidatePair {
            local: local_cand,
            remote: remote_cand,
            priority: 1000,
            state: PairState::Waiting,
            nominated: false,
        };

        let mock_socket = mock.socket.clone();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            let mut txn_id = [0u8; 12];
            txn_id.copy_from_slice(&buf[8..20]);

            let mut msg = BytesMut::new();
            msg.put_u16(0x0101);
            msg.put_u16(0);
            msg.put_u32(0xDEADBEEF);
            msg.put_slice(&txn_id);
            let _ = mock_socket.send_to(&msg, peer).await;
        });

        let result = agent
            .perform_connectivity_check(&local_socket, &pair, "remoteufrag", "remotepwd")
            .await;

        assert!(!result);
    }

    // Test: perform_connectivity_check with wrong transaction ID
    #[tokio::test]
    async fn test_perform_connectivity_check_wrong_transaction_id() {
        let config = IceConfig {
            stun_servers: vec![],
            check_timeout_ms: 500,
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let local_addr = local_socket.local_addr().unwrap();

        let mock = MockStunServer::new().await;
        let mock_addr = mock.addr;

        let local_cand = Candidate::host(local_addr, 1);
        let remote_cand = Candidate::host(mock_addr, 1);
        let pair = CandidatePair {
            local: local_cand,
            remote: remote_cand,
            priority: 1000,
            state: PairState::Waiting,
            nominated: false,
        };

        // Spawn mock server to send response with wrong transaction ID
        let mock_socket = mock.socket.clone();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            // Send response with different transaction ID
            let wrong_txn_id = [9u8; 12];
            let response = MockStunServer::build_connectivity_check_response(&wrong_txn_id);
            let _ = mock_socket.send_to(&response, peer).await;
        });

        let result = agent
            .perform_connectivity_check(&local_socket, &pair, "remoteufrag", "remotepwd")
            .await;

        assert!(!result);
    }

    // Test: Controlling agent nominates successful pair
    #[tokio::test]
    async fn test_controlling_agent_nominates_pair() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Create a pair and simulate successful check
        let local = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );
        let remote = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 100)), 5000),
            1,
        );

        agent.local_candidates.write().await.push(local.clone());
        agent.add_remote_candidates(vec![remote.clone()]).await;

        // Manually mark pair as succeeded (simulating successful check)
        {
            let mut pairs = agent.candidate_pairs.write().await;
            let pair = pairs.get_mut(0).unwrap();
            pair.state = PairState::Succeeded;
            pair.nominated = true;

            // Select the pair
            *agent.selected_pair.write().await = Some(pair.clone());
            *agent.state.write().await = IceState::Connected;
        }

        // Verify controlling agent nominated the pair
        let selected = agent.selected_pair().await.unwrap();
        assert!(selected.nominated);
        assert_eq!(selected.state, PairState::Succeeded);
        assert_eq!(agent.state().await, IceState::Connected);
    }

    // Test: Controlled agent does not nominate
    #[tokio::test]
    async fn test_controlled_agent_does_not_nominate() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlled);

        let local = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );
        let remote = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 100)), 5000),
            1,
        );

        agent.local_candidates.write().await.push(local);
        agent.add_remote_candidates(vec![remote]).await;

        // Manually mark pair as succeeded
        {
            let mut pairs = agent.candidate_pairs.write().await;
            let pair = pairs.get_mut(0).unwrap();
            pair.state = PairState::Succeeded;
            pair.nominated = false;

            *agent.selected_pair.write().await = Some(pair.clone());
            *agent.state.write().await = IceState::Connected;
        }

        // Verify controlled agent did not nominate
        let selected = agent.selected_pair().await.unwrap();
        assert!(!selected.nominated);
    }

    // Test: pair prioritization logic
    #[tokio::test]
    async fn test_pair_prioritization() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Add multiple local candidates with different priorities
        let local1 = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );
        let local2 = Candidate::server_reflexive(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(1, 2, 3, 4)), 5002),
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );

        agent.local_candidates.write().await.push(local1);
        agent.local_candidates.write().await.push(local2);

        // Add remote candidate
        let remote = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 100)), 5000),
            1,
        );
        agent.add_remote_candidates(vec![remote]).await;

        // Check pairs are sorted by priority (highest first)
        let pairs = agent.candidate_pairs.read().await;
        assert_eq!(pairs.len(), 2);

        // Host candidates have higher priority than srflx
        // So first pair should have host local candidate
        assert!(pairs[0].priority >= pairs[1].priority);
    }

    // Test: unfreeze_related_pairs functionality
    #[tokio::test]
    async fn test_unfreeze_related_pairs() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Create candidates with same foundation
        let local1 = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );
        let local2 = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5002),
            1,
        );

        let remote1 = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 100)), 5000),
            1,
        );
        let remote2 = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 101)), 5001),
            1,
        );

        agent.local_candidates.write().await.push(local1.clone());
        agent.local_candidates.write().await.push(local2);
        agent
            .add_remote_candidates(vec![remote1.clone(), remote2])
            .await;

        // Set all pairs to Frozen except first
        {
            let mut pairs = agent.candidate_pairs.write().await;
            for (i, pair) in pairs.iter_mut().enumerate() {
                if i == 0 {
                    pair.state = PairState::Succeeded;
                } else {
                    pair.state = PairState::Frozen;
                }
            }
        }

        // Create a succeeded pair for unfreezing
        let succeeded_pair = CandidatePair {
            local: local1,
            remote: remote1,
            priority: 1000,
            state: PairState::Succeeded,
            nominated: false,
        };

        // Unfreeze related pairs
        agent.unfreeze_related_pairs(0, &succeeded_pair).await;

        // Check that pairs with same foundation are now Waiting
        let pairs = agent.candidate_pairs.read().await;
        let unfrozen_count = pairs
            .iter()
            .filter(|p| p.state == PairState::Waiting)
            .count();
        assert!(unfrozen_count > 0);
    }

    #[tokio::test]
    async fn test_unfreeze_related_pairs_skips_mismatched() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_base = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(10, 0, 0, 1)), 5001),
            1,
        );
        let local_other = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(10, 0, 0, 2)), 5002),
            1,
        );
        let remote = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(10, 0, 0, 100)), 5000),
            1,
        );

        let succeeded = CandidatePair {
            local: local_base.clone(),
            remote: remote.clone(),
            priority: 1000,
            state: PairState::Succeeded,
            nominated: false,
        };

        let frozen_match = CandidatePair {
            local: local_base.clone(),
            remote: remote.clone(),
            priority: 900,
            state: PairState::Frozen,
            nominated: false,
        };

        let waiting_match = CandidatePair {
            local: local_base.clone(),
            remote: remote.clone(),
            priority: 800,
            state: PairState::Waiting,
            nominated: false,
        };

        let frozen_other = CandidatePair {
            local: local_other,
            remote,
            priority: 700,
            state: PairState::Frozen,
            nominated: false,
        };

        {
            let mut pairs = agent.candidate_pairs.write().await;
            pairs.clear();
            pairs.push(succeeded.clone());
            pairs.push(frozen_match);
            pairs.push(waiting_match);
            pairs.push(frozen_other);
        }

        agent.unfreeze_related_pairs(0, &succeeded).await;

        let pairs = agent.candidate_pairs.read().await;
        assert_eq!(pairs[1].state, PairState::Waiting);
        assert_eq!(pairs[2].state, PairState::Waiting);
        assert_eq!(pairs[3].state, PairState::Frozen);
    }

    // Test: stun_binding_request with successful response
    #[tokio::test]
    async fn test_stun_binding_request_success() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Create local socket
        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());

        // Create mock STUN server
        let mock = MockStunServer::new().await;
        let server = StunServer {
            name: "mock",
            addr: mock.addr,
        };

        // Expected mapped address
        let mapped_addr = SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(1, 2, 3, 4)), 5678);

        // Spawn mock server to respond
        let mock_socket = mock.socket.clone();
        let expected_mapped = mapped_addr;
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            let mut txn_id = [0u8; 12];
            txn_id.copy_from_slice(&buf[8..20]);
            let response = MockStunServer::build_binding_response(&txn_id, expected_mapped);
            let _ = mock_socket.send_to(&response, peer).await;
        });

        // Perform STUN binding request
        let result = agent.stun_binding_request(&local_socket, &server).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), mapped_addr);
    }

    // Test: stun_binding_request with too-short response
    #[tokio::test]
    async fn test_stun_binding_request_too_short() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let mock = MockStunServer::new().await;
        let server = StunServer {
            name: "mock",
            addr: mock.addr,
        };

        let mock_socket = mock.socket.clone();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            let _ = mock_socket.send_to(&[0u8; 8], peer).await;
        });

        let result = agent.stun_binding_request(&local_socket, &server).await;
        assert!(result.is_err());
    }

    // Test: stun_binding_request with non-response message type
    #[tokio::test]
    async fn test_stun_binding_request_not_response() {
        use bytes::{BufMut, BytesMut};

        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let mock = MockStunServer::new().await;
        let server = StunServer {
            name: "mock",
            addr: mock.addr,
        };

        let mock_socket = mock.socket.clone();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            let mut txn_id = [0u8; 12];
            txn_id.copy_from_slice(&buf[8..20]);

            let mut msg = BytesMut::new();
            msg.put_u16(0x0001); // Not a response
            msg.put_u16(0);
            msg.put_u32(0x2112A442);
            msg.put_slice(&txn_id);
            let _ = mock_socket.send_to(&msg, peer).await;
        });

        let result = agent.stun_binding_request(&local_socket, &server).await;
        let err = result.expect_err("expected InvalidResponse");
        assert!(
            matches!(err, crate::ice::stun::StunError::InvalidResponse(_)),
            "expected InvalidResponse, got {:?}",
            err
        );
    }

    // Test: stun_binding_request with non-XOR attribute payload
    #[tokio::test]
    async fn test_stun_binding_request_non_xor_attribute() {
        use bytes::{BufMut, BytesMut};

        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let mock = MockStunServer::new().await;
        let server = StunServer {
            name: "mock",
            addr: mock.addr,
        };

        let mock_socket = mock.socket.clone();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            let mut txn_id = [0u8; 12];
            txn_id.copy_from_slice(&buf[8..20]);

            let mut attrs = BytesMut::new();
            attrs.put_u16(0x0001); // Dummy attribute
            attrs.put_u16(4);
            attrs.put_u32(0x01020304);

            let mut msg = BytesMut::new();
            msg.put_u16(0x0101);
            msg.put_u16(attrs.len() as u16);
            msg.put_u32(0x2112A442);
            msg.put_slice(&txn_id);
            msg.put_slice(&attrs);
            let _ = mock_socket.send_to(&msg, peer).await;
        });

        let result = agent.stun_binding_request(&local_socket, &server).await;
        assert!(result.is_err());
    }

    // Test: stun_binding_request with non-IPv4 XOR-MAPPED-ADDRESS family
    #[tokio::test]
    async fn test_stun_binding_request_non_ipv4_family() {
        use bytes::{BufMut, BytesMut};

        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let mock = MockStunServer::new().await;
        let server = StunServer {
            name: "mock",
            addr: mock.addr,
        };

        let mock_socket = mock.socket.clone();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            let mut txn_id = [0u8; 12];
            txn_id.copy_from_slice(&buf[8..20]);

            let mut attrs = BytesMut::new();
            attrs.put_u16(0x0020); // XOR-MAPPED-ADDRESS
            attrs.put_u16(8);
            attrs.put_u8(0);
            attrs.put_u8(0x02); // IPv6 family
            attrs.put_u16(0x1234);
            attrs.put_u32(0x01020304);

            let mut msg = BytesMut::new();
            msg.put_u16(0x0101);
            msg.put_u16(attrs.len() as u16);
            msg.put_u32(0x2112A442);
            msg.put_slice(&txn_id);
            msg.put_slice(&attrs);
            let _ = mock_socket.send_to(&msg, peer).await;
        });

        let result = agent.stun_binding_request(&local_socket, &server).await;
        assert!(result.is_err());
    }

    // Test: stun_binding_request with short XOR-MAPPED-ADDRESS attribute
    #[tokio::test]
    async fn test_stun_binding_request_short_xor_attribute() {
        use bytes::{BufMut, BytesMut};

        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let mock = MockStunServer::new().await;
        let server = StunServer {
            name: "mock",
            addr: mock.addr,
        };

        let mock_socket = mock.socket.clone();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            let mut txn_id = [0u8; 12];
            txn_id.copy_from_slice(&buf[8..20]);

            let mut attrs = BytesMut::new();
            attrs.put_u16(0x0020); // XOR-MAPPED-ADDRESS
            attrs.put_u16(4); // Too short
            attrs.put_u32(0x01020304);

            let mut msg = BytesMut::new();
            msg.put_u16(0x0101);
            msg.put_u16(attrs.len() as u16);
            msg.put_u32(0x2112A442);
            msg.put_slice(&txn_id);
            msg.put_slice(&attrs);
            let _ = mock_socket.send_to(&msg, peer).await;
        });

        let result = agent.stun_binding_request(&local_socket, &server).await;
        assert!(result.is_err());
    }

    // Test: stun_binding_request with timeout
    #[tokio::test]
    async fn test_stun_binding_request_timeout() {
        let config = IceConfig {
            stun_servers: vec![],
            check_timeout_ms: 10,
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let server_socket = UdpSocket::bind("127.0.0.1:0").await.unwrap();
        let server_addr = server_socket.local_addr().unwrap();

        let server = StunServer {
            name: "unreachable",
            addr: server_addr,
        };

        tokio::spawn(async move {
            let mut buf = [0u8; 1024];
            let _ = server_socket.recv_from(&mut buf).await;
        });

        let result = agent.stun_binding_request(&local_socket, &server).await;
        let err = result.unwrap_err();
        assert_eq!(err.to_string(), "Request timeout");
    }

    // Test: stun_binding_request with invalid response (bad magic cookie)
    #[tokio::test]
    async fn test_stun_binding_request_bad_magic_cookie() {
        use bytes::{BufMut, BytesMut};

        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let mock = MockStunServer::new().await;
        let server = StunServer {
            name: "mock",
            addr: mock.addr,
        };

        let mock_socket = mock.socket.clone();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            // Build response with bad magic cookie
            let mut msg = BytesMut::new();
            msg.put_u16(0x0101); // BINDING_RESPONSE
            msg.put_u16(0); // Length
            msg.put_u32(0xDEADBEEF); // Wrong magic cookie
            msg.put_slice(&buf[8..20]); // Transaction ID
            let _ = mock_socket.send_to(&msg, peer).await;
        });

        // R4: dispatcher forwards by txn-id, then `stun_binding_request`
        // re-validates the cookie and surfaces `InvalidResponse`.
        let result = agent.stun_binding_request(&local_socket, &server).await;
        let err = result.expect_err("expected InvalidResponse");
        assert!(
            matches!(err, crate::ice::stun::StunError::InvalidResponse(_)),
            "expected InvalidResponse, got {:?}",
            err
        );
    }

    // Test: stun_binding_request with missing XOR-MAPPED-ADDRESS
    #[tokio::test]
    async fn test_stun_binding_request_no_mapped_address() {
        use bytes::{BufMut, BytesMut};

        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let mock = MockStunServer::new().await;
        let server = StunServer {
            name: "mock",
            addr: mock.addr,
        };

        let mock_socket = mock.socket.clone();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            let txn_id: [u8; 12] = buf[8..20].try_into().expect("short STUN request");

            // Build response without XOR-MAPPED-ADDRESS
            let mut msg = BytesMut::new();
            msg.put_u16(0x0101); // BINDING_RESPONSE
            msg.put_u16(0); // No attributes
            msg.put_u32(0x2112A442); // Magic cookie
            msg.put_slice(&txn_id);
            let _ = mock_socket.send_to(&msg, peer).await;
        });

        let result = agent.stun_binding_request(&local_socket, &server).await;
        assert!(result.is_err());
    }

    // Test: gather_srflx_candidates with STUN server
    #[tokio::test]
    async fn test_gather_srflx_candidates() {
        init_tracing();
        // Create mock STUN server
        let mock = MockStunServer::new().await;

        let config = IceConfig {
            stun_servers: vec![StunServer {
                name: "mock",
                addr: mock.addr,
            }],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // First gather host candidates
        let _ = agent.gather_host_candidates().await;

        // Spawn mock server to respond (respond once per request)
        let mock_socket = mock.socket.clone();
        let mapped_addr = SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(1, 2, 3, 4)), 9999);
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            // Respond to first request only
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            let txn_id: [u8; 12] = buf[8..20].try_into().expect("short STUN request");
            let response = MockStunServer::build_binding_response(&txn_id, mapped_addr);
            let _ = mock_socket.send_to(&response, peer).await;
        });

        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

        // Gather srflx candidates
        let srflx_candidates = agent.gather_srflx_candidates().await;

        // Should have at least one srflx candidate
        assert!(!srflx_candidates.is_empty());
        assert!(srflx_candidates
            .iter()
            .any(|c| c.candidate_type == CandidateType::ServerReflexive));
    }

    // Test: gather_srflx_candidates ignores mapped address equal to base
    #[tokio::test]
    async fn test_gather_srflx_candidates_ignores_base_address() {
        let mock = MockStunServer::new().await;

        let config = IceConfig {
            stun_servers: vec![StunServer {
                name: "mock",
                addr: mock.addr,
            }],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let local_addr = local_socket.local_addr().unwrap();
        agent
            .sockets
            .write()
            .await
            .insert(local_addr, local_socket.clone());

        let mock_socket = mock.socket.clone();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            let mut txn_id = [0u8; 12];
            txn_id.copy_from_slice(&buf[8..20]);
            let response = MockStunServer::build_binding_response(&txn_id, local_addr);
            let _ = mock_socket.send_to(&response, peer).await;
        });

        let srflx_candidates = agent.gather_srflx_candidates().await;
        assert!(srflx_candidates.is_empty());
    }

    // Test: gather_srflx_candidates handles STUN error
    #[tokio::test]
    async fn test_gather_srflx_candidates_stun_error() {
        let mock = MockStunServer::new().await;

        let config = IceConfig {
            stun_servers: vec![StunServer {
                name: "mock",
                addr: mock.addr,
            }],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let local_addr = local_socket.local_addr().unwrap();
        agent
            .sockets
            .write()
            .await
            .insert(local_addr, local_socket.clone());

        let mock_socket = mock.socket.clone();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            let _ = mock_socket.send_to(&[0u8; 8], peer).await;
        });

        let srflx_candidates = agent.gather_srflx_candidates().await;
        assert!(srflx_candidates.is_empty());
    }

    // Test: fallback_selection prefers host-to-host pairs
    #[tokio::test]
    async fn test_fallback_prefers_host_to_host() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Add srflx local candidate
        let local_srflx = Candidate::server_reflexive(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(1, 2, 3, 4)), 5001),
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );

        // Add host local candidate
        let local_host = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 2)), 5002),
            1,
        );

        agent.local_candidates.write().await.push(local_srflx);
        agent.local_candidates.write().await.push(local_host);

        // Add host remote candidate
        let remote_host = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 100)), 5000),
            1,
        );
        agent.add_remote_candidates(vec![remote_host]).await;

        // Fallback selection
        let result = agent.fallback_selection().await;
        assert!(result.is_ok());

        // Should prefer host-to-host
        let selected = agent.selected_pair().await.unwrap();
        assert_eq!(selected.local.candidate_type, CandidateType::Host);
        assert_eq!(selected.remote.candidate_type, CandidateType::Host);
    }

    // Test: form_candidate_pairs prevents duplicates
    #[tokio::test]
    async fn test_form_candidate_pairs_no_duplicates() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );
        agent.local_candidates.write().await.push(local);

        let remote = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 100)), 5000),
            1,
        );

        // Add same remote candidate twice
        agent.add_remote_candidates(vec![remote.clone()]).await;
        agent.add_remote_candidates(vec![remote]).await;

        // Should only have 1 pair (no duplicates)
        let pairs = agent.candidate_pairs.read().await;
        assert_eq!(pairs.len(), 1);
    }

    // Test: form_candidate_pairs initializes with Frozen state
    #[tokio::test]
    async fn test_form_candidate_pairs_frozen_state() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Add multiple local candidates with different foundations
        let local1 = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );
        let local2 = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 2)), 5002),
            1,
        );

        agent.local_candidates.write().await.push(local1);
        agent.local_candidates.write().await.push(local2);

        let remote = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 100)), 5000),
            1,
        );
        agent.add_remote_candidates(vec![remote]).await;

        let pairs = agent.candidate_pairs.read().await;

        // First pair of each foundation should be Waiting, others Frozen
        let waiting_count = pairs
            .iter()
            .filter(|p| p.state == PairState::Waiting)
            .count();
        assert!(waiting_count > 0);
    }

    // Test: start_checks updates pair states correctly
    #[tokio::test]
    async fn test_start_checks_pair_state_progression() {
        let config = IceConfig {
            stun_servers: vec![],
            check_timeout_ms: 500,
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        agent.set_remote_credentials("remoteusr", "remotepwd").await;

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let local_addr = local_socket.local_addr().unwrap();

        // Use unreachable address so check fails
        let unreachable_addr =
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 0, 2, 1)), 9999);

        let local_cand = Candidate::host(local_addr, 1);
        agent.local_candidates.write().await.push(local_cand);
        agent
            .sockets
            .write()
            .await
            .insert(local_addr, local_socket.clone());

        let remote_cand = Candidate::host(unreachable_addr, 1);
        agent.add_remote_candidates(vec![remote_cand]).await;

        {
            let mut pairs = agent.candidate_pairs.write().await;
            let pair = pairs.get_mut(0).expect("missing candidate pair");
            pair.state = PairState::Waiting;
        }

        // Start checks - should fail and go to fallback
        let _ = agent.start_checks().await;

        // Check pair state was updated to Failed
        let pairs = agent.candidate_pairs.read().await;
        assert!(pairs.iter().any(|p| p.state == PairState::Failed));
    }

    // Test: start_checks succeeds and nominates for controlling role
    #[tokio::test]
    async fn test_start_checks_success_controlling() {
        let config = IceConfig {
            stun_servers: vec![],
            check_timeout_ms: 500,
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        agent.set_remote_credentials("remoteusr", "remotepwd").await;

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let local_addr = local_socket.local_addr().unwrap();
        let local_cand = Candidate::host(local_addr, 1);
        agent.local_candidates.write().await.push(local_cand);
        agent
            .sockets
            .write()
            .await
            .insert(local_addr, local_socket.clone());

        let mock = MockStunServer::new().await;
        let remote_cand = Candidate::host(mock.addr, 1);
        agent.add_remote_candidates(vec![remote_cand]).await;

        let mock_socket = mock.socket.clone();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            let mut txn_id = [0u8; 12];
            txn_id.copy_from_slice(&buf[8..20]);
            let response = MockStunServer::build_connectivity_check_response(&txn_id);
            let _ = mock_socket.send_to(&response, peer).await;
        });

        let result = agent.start_checks().await;
        assert!(result.is_ok());

        let selected = agent.selected_pair().await;
        assert!(selected.is_some());
        assert!(selected.unwrap().nominated);
    }

    // Test: start_checks succeeds without nomination when controlled
    #[tokio::test]
    async fn test_start_checks_success_controlled() {
        let config = IceConfig {
            stun_servers: vec![],
            check_timeout_ms: 500,
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlled);

        agent.set_remote_credentials("remoteusr", "remotepwd").await;

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let local_addr = local_socket.local_addr().unwrap();
        let local_cand = Candidate::host(local_addr, 1);
        agent.local_candidates.write().await.push(local_cand);
        agent
            .sockets
            .write()
            .await
            .insert(local_addr, local_socket.clone());

        let mock = MockStunServer::new().await;
        let remote_cand = Candidate::host(mock.addr, 1);
        agent.add_remote_candidates(vec![remote_cand]).await;

        let mock_socket = mock.socket.clone();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (_len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            let mut txn_id = [0u8; 12];
            txn_id.copy_from_slice(&buf[8..20]);
            let response = MockStunServer::build_connectivity_check_response(&txn_id);
            let _ = mock_socket.send_to(&response, peer).await;
        });

        let result = agent.start_checks().await;
        assert!(result.is_ok());

        let selected = agent.selected_pair().await;
        assert!(selected.is_some());
        assert!(!selected.unwrap().nominated);
    }

    // Test: Controlled agent uses correct ICE-CONTROLLED attribute
    #[tokio::test]
    async fn test_controlled_agent_ice_attribute() {
        let config = IceConfig {
            stun_servers: vec![],
            check_timeout_ms: 1000,
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlled);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let local_addr = local_socket.local_addr().unwrap();

        let mock = MockStunServer::new().await;
        let mock_addr = mock.addr;

        let local_cand = Candidate::host(local_addr, 1);
        let remote_cand = Candidate::host(mock_addr, 1);
        let pair = CandidatePair {
            local: local_cand,
            remote: remote_cand,
            priority: 1000,
            state: PairState::Waiting,
            nominated: false,
        };

        // Spawn server to check for ICE-CONTROLLED attribute
        let mock_socket = mock.socket.clone();
        let (controlled_tx, controlled_rx) = tokio::sync::oneshot::channel();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            // Check for ICE-CONTROLLED attribute (0x8029)
            let has_controlled = buf[..len].windows(2).any(|w| w[0] == 0x80 && w[1] == 0x29);
            let _ = controlled_tx.send(has_controlled);

            let txn_id: [u8; 12] = buf[8..20].try_into().expect("short STUN request");
            let response = MockStunServer::build_connectivity_check_response(&txn_id);
            let _ = mock_socket.send_to(&response, peer).await;
        });

        let result = agent
            .perform_connectivity_check(&local_socket, &pair, "remoteufrag", "remotepwd")
            .await;

        assert!(result);
        assert!(controlled_rx.await.unwrap_or(false));
    }

    // Test: remote credentials update
    #[tokio::test]
    async fn test_update_remote_credentials() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Set initial credentials
        agent.set_remote_credentials("user1", "pass1").await;
        let (ufrag, pwd) = agent.remote_credentials().await.unwrap();
        assert_eq!(ufrag, "user1");
        assert_eq!(pwd, "pass1");

        // Update credentials
        agent.set_remote_credentials("user2", "pass2").await;
        let (ufrag, pwd) = agent.remote_credentials().await.unwrap();
        assert_eq!(ufrag, "user2");
        assert_eq!(pwd, "pass2");
    }

    #[tokio::test]
    async fn test_remote_credentials_missing_password() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        *agent.remote_ufrag.write().await = Some("user".to_string());
        let creds = agent.remote_credentials().await;
        assert!(creds.is_none());
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_gather_candidates_forced_local_addr_error() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        force_host_local_addr_error_once();
        let result = agent.gather_candidates().await;
        assert!(result.is_err());
    }

    // Test: pair priority calculation differs between controlling and controlled
    #[tokio::test]
    async fn test_pair_priority_role_difference() {
        let config_controlling = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent_controlling = IceAgent::new(config_controlling, IceRole::Controlling);

        let config_controlled = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent_controlled = IceAgent::new(config_controlled, IceRole::Controlled);

        // Add same candidates to both agents
        let local = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );
        let remote = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 100)), 5000),
            1,
        );

        agent_controlling
            .local_candidates
            .write()
            .await
            .push(local.clone());
        agent_controlling
            .add_remote_candidates(vec![remote.clone()])
            .await;

        agent_controlled.local_candidates.write().await.push(local);
        agent_controlled.add_remote_candidates(vec![remote]).await;

        // Both should compute same priority for the pair
        let pairs_controlling = agent_controlling.candidate_pairs.read().await;
        let pairs_controlled = agent_controlled.candidate_pairs.read().await;

        assert_eq!(pairs_controlling[0].priority, pairs_controlled[0].priority);
    }

    // Test: gather_candidates sets state to Gathering
    #[tokio::test]
    async fn test_gather_candidates_state() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        assert_eq!(agent.state().await, IceState::New);

        let _ = agent.gather_candidates().await;

        // State should be Gathering (remains after gathering completes)
        assert_eq!(agent.state().await, IceState::Gathering);
    }

    // Test: gather_candidates respects gather_timeout_ms as a wall-clock cap.
    // With a 1ms budget, the call must return quickly even though host
    // gathering would normally take longer. Partial results (possibly empty
    // or just hosts) are acceptable; what matters is the wall-clock bound.
    #[tokio::test]
    async fn test_gather_candidates_respects_gather_timeout() {
        // Use a STUN server pointed at an unrouteable address so srflx
        // gathering would otherwise stall up to check_timeout_ms.
        let bogus_stun = StunServer {
            name: "bogus",
            addr: SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(203, 0, 113, 1)), 3478),
        };
        let config = IceConfig {
            stun_servers: vec![bogus_stun],
            check_timeout_ms: 5_000,
            gather_timeout_ms: 1,
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let start = tokio::time::Instant::now();
        // Must not error — partial results are returned on timeout.
        let _ = agent
            .gather_candidates()
            .await
            .expect("gather (partial ok)");
        let elapsed = start.elapsed();

        // Generous bound: the cap is 1ms, but host binding + scheduler
        // overhead can run a few hundred ms on busy CI. 2 seconds is well
        // below check_timeout_ms (5s), proving the cap kicked in.
        assert!(
            elapsed < std::time::Duration::from_secs(2),
            "gather_candidates took {:?} — gather_timeout_ms not enforced",
            elapsed
        );
    }

    // Test: start_checks sets state to Checking
    #[tokio::test]
    async fn test_start_checks_state() {
        let config = IceConfig {
            stun_servers: vec![],
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        // Add minimal setup for checks
        let local = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 1)), 5001),
            1,
        );
        agent.local_candidates.write().await.push(local);

        let remote = Candidate::host(
            SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 100)), 5000),
            1,
        );
        agent.add_remote_candidates(vec![remote]).await;

        // Without remote credentials, will use fallback
        let _ = agent.start_checks().await;

        // Should end up in Connected state (via fallback)
        assert_eq!(agent.state().await, IceState::Connected);
    }

    // Test: STUN request includes correct USERNAME format
    #[tokio::test]
    async fn test_connectivity_check_username_format() {
        let config = IceConfig {
            stun_servers: vec![],
            local_ufrag: "localusr".to_string(),
            local_pwd: "localpwd".to_string(),
            check_timeout_ms: 1000,
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlling);

        let local_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await.unwrap());
        let local_addr = local_socket.local_addr().unwrap();

        let mock = MockStunServer::new().await;
        let mock_addr = mock.addr;

        let local_cand = Candidate::host(local_addr, 1);
        let remote_cand = Candidate::host(mock_addr, 1);
        let pair = CandidatePair {
            local: local_cand,
            remote: remote_cand,
            priority: 1000,
            state: PairState::Waiting,
            nominated: false,
        };

        // Spawn server to check USERNAME attribute format
        let mock_socket = mock.socket.clone();
        let (username_tx, username_rx) = tokio::sync::oneshot::channel();
        tokio::spawn(async move {
            let mut buf = vec![0u8; 1024];
            let (len, peer) = mock_socket.recv_from(&mut buf).await.unwrap();
            let payload = String::from_utf8_lossy(&buf[..len]);
            let found_username = payload.contains("remoteusr") & payload.contains("localusr");

            let _ = username_tx.send(found_username);
            let txn_id: [u8; 12] = buf[8..20].try_into().expect("short STUN request");
            let response = MockStunServer::build_connectivity_check_response(&txn_id);
            let _ = mock_socket.send_to(&response, peer).await;
        });

        let result = agent
            .perform_connectivity_check(&local_socket, &pair, "remoteusr", "remotepwd")
            .await;

        assert!(result);
        assert!(username_rx.await.unwrap_or(false));
    }

    // ========== Phase 2.5: STUN responder tests ==========

    /// Build a STUN binding request mirroring `perform_connectivity_check`'s
    /// wire layout, with USERNAME (`<receiver_ufrag>:<sender_ufrag>`) and
    /// MESSAGE-INTEGRITY keyed on `mi_key`. Returns `(bytes, txn_id)`.
    fn build_signed_binding_request(
        receiver_ufrag: &str,
        sender_ufrag: &str,
        mi_key: &[u8],
    ) -> (Vec<u8>, [u8; 12]) {
        use bytes::{BufMut, BytesMut};
        use hmac::{Hmac, Mac};
        use rand::RngCore;
        use sha1::Sha1;
        type HmacSha1 = Hmac<Sha1>;

        let mut txn_id = [0u8; 12];
        rand::thread_rng().fill_bytes(&mut txn_id);

        let username = format!("{}:{}", receiver_ufrag, sender_ufrag);
        let username_bytes = username.as_bytes();

        let mut attrs = BytesMut::new();
        attrs.put_u16(ATTR_USERNAME);
        attrs.put_u16(username_bytes.len() as u16);
        attrs.put_slice(username_bytes);
        let pad = (4 - (username_bytes.len() % 4)) % 4;
        for _ in 0..pad {
            attrs.put_u8(0);
        }

        let mut msg = BytesMut::with_capacity(20 + attrs.len() + 24);
        msg.put_u16(BINDING_REQUEST);
        msg.put_u16(attrs.len() as u16);
        msg.put_u32(MAGIC_COOKIE);
        msg.put_slice(&txn_id);
        msg.put_slice(&attrs);

        // MESSAGE-INTEGRITY using `mi_key`.
        let current_len = msg.len();
        let new_len = (current_len - 20 + 24) as u16;
        msg[2] = (new_len >> 8) as u8;
        msg[3] = (new_len & 0xff) as u8;

        let mut mac = HmacSha1::new_from_slice(mi_key).expect("hmac key");
        mac.update(&msg);
        let integrity = mac.finalize().into_bytes();
        msg.put_u16(ATTR_MESSAGE_INTEGRITY);
        msg.put_u16(20);
        msg.put_slice(&integrity);

        (msg.to_vec(), txn_id)
    }

    /// Parse a STUN binding response and extract the XOR-MAPPED-ADDRESS,
    /// returning `(xor_addr, txn_id)`.
    fn parse_binding_response_xor(bytes: &[u8]) -> Option<(SocketAddr, [u8; 12])> {
        if bytes.len() < 20 {
            return None;
        }
        if u16::from_be_bytes([bytes[0], bytes[1]]) != BINDING_RESPONSE {
            return None;
        }
        if u32::from_be_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]) != MAGIC_COOKIE {
            return None;
        }
        let mut txn_id = [0u8; 12];
        txn_id.copy_from_slice(&bytes[8..20]);
        let total_attrs_len = u16::from_be_bytes([bytes[2], bytes[3]]) as usize;
        let end = 20 + total_attrs_len.min(bytes.len() - 20);

        let mut off = 20;
        while off + 4 <= end {
            let attr_type = u16::from_be_bytes([bytes[off], bytes[off + 1]]);
            let attr_len = u16::from_be_bytes([bytes[off + 2], bytes[off + 3]]) as usize;
            let val_start = off + 4;
            let val_end = val_start + attr_len;
            if val_end > end {
                return None;
            }
            if attr_type == ATTR_XOR_MAPPED_ADDRESS && attr_len >= 8 {
                let family = bytes[val_start + 1];
                let port = u16::from_be_bytes([bytes[val_start + 2], bytes[val_start + 3]])
                    ^ (MAGIC_COOKIE >> 16) as u16;
                if family == 0x01 {
                    let cookie = MAGIC_COOKIE.to_be_bytes();
                    let ip_bytes = [
                        bytes[val_start + 4] ^ cookie[0],
                        bytes[val_start + 5] ^ cookie[1],
                        bytes[val_start + 6] ^ cookie[2],
                        bytes[val_start + 7] ^ cookie[3],
                    ];
                    return Some((
                        SocketAddr::new(IpAddr::V4(std::net::Ipv4Addr::from(ip_bytes)), port),
                        txn_id,
                    ));
                }
            }
            let padded = (attr_len + 3) & !3;
            off = val_start + padded;
        }
        None
    }

    /// Pick the loopback host candidate from a freshly-gathered agent.
    async fn pick_loopback_host(agent: &IceAgent) -> Candidate {
        let cands = agent.local_candidates().await;
        cands
            .into_iter()
            .find(|c| c.candidate_type == CandidateType::Host && c.address.ip().is_loopback())
            .expect("loopback host candidate")
    }

    #[tokio::test]
    async fn responder_answers_valid_binding_request() {
        init_tracing();
        let config = IceConfig {
            stun_servers: vec![],
            local_ufrag: "loctufrag".to_string(),
            local_pwd: "locpwd1234567890123456".to_string(),
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlled);
        let _ = agent.gather_candidates().await.expect("gather");

        let host = pick_loopback_host(&agent).await;

        // Side socket pretending to be a peer.
        let peer = UdpSocket::bind("127.0.0.1:0").await.unwrap();
        let peer_addr = peer.local_addr().unwrap();

        let (req, txn_id) =
            build_signed_binding_request("loctufrag", "peerufrag", b"locpwd1234567890123456");
        peer.send_to(&req, host.address).await.unwrap();

        let mut buf = vec![0u8; 1500];
        let (len, from) = tokio::time::timeout(
            std::time::Duration::from_millis(500),
            peer.recv_from(&mut buf),
        )
        .await
        .expect("response did not time out")
        .expect("recv ok");
        assert_eq!(from, host.address);

        let (xor_addr, resp_txn) =
            parse_binding_response_xor(&buf[..len]).expect("XOR-MAPPED-ADDRESS present");
        assert_eq!(resp_txn, txn_id, "txn id round-trips");
        assert_eq!(xor_addr, peer_addr, "XOR-MAPPED-ADDRESS reflects peer");
    }

    #[tokio::test]
    async fn responder_drops_invalid_username() {
        init_tracing();
        let config = IceConfig {
            stun_servers: vec![],
            local_ufrag: "loctufrag".to_string(),
            local_pwd: "locpwd1234567890123456".to_string(),
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlled);
        let _ = agent.gather_candidates().await.expect("gather");

        let host = pick_loopback_host(&agent).await;

        let peer = UdpSocket::bind("127.0.0.1:0").await.unwrap();

        // USERNAME prefix doesn't match local ufrag → dispatcher drops.
        let (req, _txn_id) =
            build_signed_binding_request("wrongone", "peerufrag", b"locpwd1234567890123456");
        peer.send_to(&req, host.address).await.unwrap();

        let mut buf = vec![0u8; 1500];
        let result = tokio::time::timeout(
            std::time::Duration::from_millis(150),
            peer.recv_from(&mut buf),
        )
        .await;
        assert!(result.is_err(), "expected no response, got {:?}", result);
    }

    #[tokio::test]
    async fn responder_drops_invalid_message_integrity() {
        init_tracing();
        let config = IceConfig {
            stun_servers: vec![],
            local_ufrag: "loctufrag".to_string(),
            local_pwd: "locpwd1234567890123456".to_string(),
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlled);
        let _ = agent.gather_candidates().await.expect("gather");

        let host = pick_loopback_host(&agent).await;

        let peer = UdpSocket::bind("127.0.0.1:0").await.unwrap();

        // USERNAME ok, MI signed with the wrong key → dispatcher drops.
        let (req, _txn_id) =
            build_signed_binding_request("loctufrag", "peerufrag", b"WRONGKEY999999999999");
        peer.send_to(&req, host.address).await.unwrap();

        let mut buf = vec![0u8; 1500];
        let result = tokio::time::timeout(
            std::time::Duration::from_millis(150),
            peer.recv_from(&mut buf),
        )
        .await;
        assert!(result.is_err(), "expected no response, got {:?}", result);
    }

    #[tokio::test]
    async fn responder_tasks_aborted_on_close() {
        init_tracing();
        let config = IceConfig {
            stun_servers: vec![],
            local_ufrag: "loctufrag".to_string(),
            local_pwd: "locpwd1234567890123456".to_string(),
            ..Default::default()
        };
        let agent = IceAgent::new(config, IceRole::Controlled);
        let _ = agent.gather_candidates().await.expect("gather");
        let host = pick_loopback_host(&agent).await;
        // Keep the cloned Arc alive past close so the OS socket is not
        // freed; we want to prove the dispatcher task no longer reads.
        let _kept = agent.socket_for(host.address).await.expect("socket");

        agent.close().await;

        // Send a fresh binding request — no responder running, so no
        // reply should come back.
        let peer = UdpSocket::bind("127.0.0.1:0").await.unwrap();
        let (req, _txn_id) =
            build_signed_binding_request("loctufrag", "peerufrag", b"locpwd1234567890123456");
        peer.send_to(&req, host.address).await.unwrap();

        let mut buf = vec![0u8; 1500];
        let result = tokio::time::timeout(
            std::time::Duration::from_millis(150),
            peer.recv_from(&mut buf),
        )
        .await;
        assert!(
            result.is_err(),
            "expected silence after close, got {:?}",
            result
        );
    }

    /// Decode an IPv6 XOR-MAPPED-ADDRESS attribute the way RFC 5389
    /// §15.2 specifies: family must be 0x02, port XORed with the high
    /// 16 bits of the magic cookie, and the address XORed with the
    /// magic cookie followed by the transaction ID.
    fn parse_binding_response_xor_v6(bytes: &[u8]) -> Option<(std::net::Ipv6Addr, u16, [u8; 12])> {
        if bytes.len() < 20 {
            return None;
        }
        if u16::from_be_bytes([bytes[0], bytes[1]]) != BINDING_RESPONSE {
            return None;
        }
        if u32::from_be_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]) != MAGIC_COOKIE {
            return None;
        }
        let mut txn_id = [0u8; 12];
        txn_id.copy_from_slice(&bytes[8..20]);
        let total_attrs_len = u16::from_be_bytes([bytes[2], bytes[3]]) as usize;
        let end = 20 + total_attrs_len.min(bytes.len() - 20);

        let mut off = 20;
        while off + 4 <= end {
            let attr_type = u16::from_be_bytes([bytes[off], bytes[off + 1]]);
            let attr_len = u16::from_be_bytes([bytes[off + 2], bytes[off + 3]]) as usize;
            let val_start = off + 4;
            let val_end = val_start + attr_len;
            if val_end > end {
                return None;
            }
            if attr_type == ATTR_XOR_MAPPED_ADDRESS && attr_len == 20 {
                let family = bytes[val_start + 1];
                if family != 0x02 {
                    return None;
                }
                let port = u16::from_be_bytes([bytes[val_start + 2], bytes[val_start + 3]])
                    ^ (MAGIC_COOKIE >> 16) as u16;
                let cookie = MAGIC_COOKIE.to_be_bytes();
                let mut ip = [0u8; 16];
                for i in 0..4 {
                    ip[i] = bytes[val_start + 4 + i] ^ cookie[i];
                }
                for i in 0..12 {
                    ip[4 + i] = bytes[val_start + 8 + i] ^ txn_id[i];
                }
                return Some((std::net::Ipv6Addr::from(ip), port, txn_id));
            }
            let padded = (attr_len + 3) & !3;
            off = val_start + padded;
        }
        None
    }

    #[test]
    fn build_binding_response_emits_ipv6_xor_mapped_address() {
        // Drive `build_binding_response` directly with an IPv6 source
        // address and decode the XOR-MAPPED-ADDRESS to prove the
        // RFC 5389 §15.2 layout round-trips.
        let local_ufrag = "loctufrag";
        let local_pwd = b"locpwd1234567890123456";
        let (req, txn_id) = build_signed_binding_request(local_ufrag, "peerufrag", local_pwd);

        let from = SocketAddr::new(
            IpAddr::V6(std::net::Ipv6Addr::new(
                0x2001, 0xdb8, 0, 0, 0, 0, 0, 0xabcd,
            )),
            54321,
        );
        let resp = build_binding_response(
            &req,
            txn_id,
            from,
            local_ufrag,
            std::str::from_utf8(local_pwd).unwrap(),
        )
        .expect("v6 response built");

        let (ip, port, resp_txn) =
            parse_binding_response_xor_v6(&resp).expect("v6 XOR-MAPPED-ADDRESS round-trips");
        assert_eq!(resp_txn, txn_id);
        assert_eq!(port, 54321);
        assert_eq!(
            ip,
            std::net::Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0xabcd)
        );
    }
}