singsing-rs 0.1.2

Blazing-fast IPv4/TCP port scanning library for Linux
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
#![doc = env!("CARGO_PKG_DESCRIPTION")]
#![doc = ""]
#![cfg_attr(doc, doc = include_str!("../README.md"))]
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/0xdea/singsing-rs/master/.img/logo_singsing.png"
)]
#![expect(
    clippy::pub_use,
    reason = "the crate's one `pub use` re-exports a foreign `ipnet` type that already appears \
              in our public API (`TargetsError`), the deliberate exception this lint warns \
              against as a module-layout anti-pattern; `use` items can't carry the attribute \
              themselves, so it's set here instead"
)]

#[cfg(not(target_os = "linux"))]
compile_error!("singsing-rs only supports Linux (see the Compatibility section in README.md)");

use std::any::Any;
use std::collections::{BTreeSet, HashMap, HashSet};
use std::error::Error;
use std::net::{IpAddr, Ipv4Addr};
use std::num::ParseIntError;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use std::{fs, io, thread};

use pnet::datalink;
use pnet::packet::ip::IpNextHeaderProtocols;
use pnet::packet::ipv4::{Ipv4Packet, MutableIpv4Packet, checksum};
use pnet::packet::tcp::{MutableTcpPacket, TcpFlags, TcpPacket, ipv4_checksum};
use pnet::packet::{MutablePacket as _, Packet as _};
use pnet::transport::{
    TransportChannelType, TransportReceiver, ipv4_packet_iter, transport_channel,
};

/// The packet length used for scanning.
const PACKET_LEN: usize = 40;
/// The receive buffer's length, reused for every packet read on the raw socket.
///
/// The socket is a `Layer3` raw socket, so the kernel delivers every matching IPv4/TCP packet on
/// the host to it, not just replies to this scan's own probes (unrelated packets are filtered out
/// in userspace by `classify_response`). The buffer must therefore be large enough for the
/// largest packet any such traffic could deliver, not just this scan's own `PACKET_LEN`-sized
/// probes and replies: a too-small buffer silently truncates an oversized read rather than
/// erroring. `1 MiB` comfortably exceeds the largest possible IPv4 packet (65,535 bytes).
const RECEIVE_BUFFER_LEN: usize = 1 << 20;

/// The maximum number of probes to send during a scan.
const MAX_PROBES: usize = 16_777_214;
/// The maximum time to listen for late replies after the final probe.
const MAX_TIMEOUT: Duration = Duration::from_hours(24);

/// One minute duration.
const ONE_MINUTE: Duration = Duration::from_mins(1);
/// Ten minute duration.
const TEN_MINUTES: Duration = Duration::from_mins(10);
/// Thirty minute duration.
const THIRTY_MINUTES: Duration = Duration::from_mins(30);
/// One hour duration.
const ONE_HOUR: Duration = Duration::from_hours(1);

/// A TCP port number.
pub type Port = u16;
/// A TCP sequence or acknowledgement number.
type SeqNum = u32;
/// Maps each target host/port pair to its expected TCP sequence number.
type ExpectedResponses = HashMap<(Ipv4Addr, Port), SeqNum>;

/// The error type returned by `scan`/`scan_with_callback`/`scan_with_callbacks`'s `on_result` and
/// `on_progress` callbacks.
///
/// Callbacks are caller-defined and can fail for reasons this crate can't enumerate in advance, so
/// their error is boxed rather than typed.
pub type CallbackError = Box<dyn Error + Send + Sync>;

/// Foreign types from `ipnet` that appear in this crate's public API (see [`TargetsError`]).
///
/// Re-exported so callers can name them without adding `ipnet` as a separate direct dependency,
/// and so that a semver-breaking `ipnet` upgrade shows up as a `singsing-rs` API change too.
pub use ipnet::{AddrParseError, Ipv4Net};

/// An error resolving a network interface's IPv4 address.
///
/// # Examples
///
/// ```
/// use singsing_rs::{InterfaceError, interface_ipv4};
///
/// let name = "singsing-rs-example-missing-interface";
/// match interface_ipv4(name) {
///     Err(InterfaceError::NotFound { name: n }) => assert_eq!(n, name),
///     other => panic!("unexpected result: {other:?}"),
/// }
/// ```
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum InterfaceError {
    /// No interface with the given name exists.
    #[error("network interface {name:?} does not exist")]
    NotFound {
        /// The requested interface name.
        name: String,
    },
    /// The interface exists but has no IPv4 address.
    #[error("network interface {name:?} has no IPv4 address")]
    NoIpv4 {
        /// The requested interface name.
        name: String,
    },
}

/// An error parsing scan targets.
///
/// # Examples
///
/// ```
/// use singsing_rs::{TargetsError, parse_targets};
///
/// assert!(matches!(
///     parse_targets("10.0.0.0/7"),
///     Err(TargetsError::TooLarge { .. })
/// ));
/// ```
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum TargetsError {
    /// The input contained `/` but was not a valid IPv4 network.
    #[error("invalid IPv4 network")]
    InvalidNetwork(#[source] AddrParseError),
    /// The input was not a valid IPv4 address.
    #[error("invalid IPv4 address")]
    InvalidAddress(#[source] AddrParseError),
    /// The network contains more usable addresses than the scan limit allows.
    #[error("{network} contains more than {max} usable addresses; split networks larger than a /8")]
    TooLarge {
        /// The oversized network.
        network: Ipv4Net,
        /// The maximum number of usable addresses.
        max: usize,
    },
}

/// An error parsing or reading scan ports.
///
/// # Examples
///
/// ```
/// use singsing_rs::{PortsError, parse_ports};
///
/// assert!(matches!(parse_ports("0"), Err(PortsError::PortZero)));
/// ```
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum PortsError {
    /// A comma-separated item was empty.
    #[error("empty port in {input:?}")]
    EmptyItem {
        /// The full port list that contained the empty item.
        input: String,
    },
    /// A range contained more than one `-`.
    #[error("invalid port range {item:?}")]
    InvalidRange {
        /// The malformed range item.
        item: String,
    },
    /// A range's start was greater than its end.
    #[error("reversed port range {item:?}")]
    ReversedRange {
        /// The reversed range item.
        item: String,
    },
    /// A port was not a valid `u16`.
    #[error("invalid TCP port {input:?}")]
    InvalidPort {
        /// The unparsable port text.
        input: String,
        /// The underlying integer parse error.
        #[source]
        source: ParseIntError,
    },
    /// Port zero was requested, which is not supported.
    #[error("TCP port zero is not supported")]
    PortZero,
    /// The services file could not be read.
    #[error("failed to read {}", path.display())]
    ServicesFileRead {
        /// The services file path.
        path: PathBuf,
        /// The underlying I/O error.
        #[source]
        source: io::Error,
    },
    /// The services file contained no TCP services.
    #[error("{} contains no TCP services", path.display())]
    NoTcpServices {
        /// The services file path.
        path: PathBuf,
    },
}

/// An error running a scan.
///
/// # Examples
///
/// ```
/// use singsing_rs::{ScanConfig, ScanError, scan};
/// use std::net::Ipv4Addr;
///
/// let config = ScanConfig::new(Vec::new(), vec![80], Ipv4Addr::LOCALHOST);
/// assert!(matches!(scan(&config), Err(ScanError::EmptyScan)));
/// ```
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ScanError {
    /// The scan had no targets or no ports.
    #[error("at least one target and one port are required")]
    EmptyScan,
    /// The configured bandwidth was zero.
    #[error("bandwidth must be greater than zero")]
    ZeroBandwidth,
    /// The configured bandwidth overflowed while converting to a packet rate.
    #[error("bandwidth is too large")]
    BandwidthOverflow,
    /// The configured late-reply timeout exceeds the maximum.
    #[error("timeout of {timeout:?} exceeds the maximum of {max:?}")]
    TimeoutTooLarge {
        /// The requested timeout.
        timeout: Duration,
        /// The maximum allowed timeout.
        max: Duration,
    },
    /// Multiplying the target and port counts overflowed `usize`.
    #[error("scan size overflow")]
    ScanSizeOverflow,
    /// The scan exceeds the maximum number of probes.
    #[error(
        "scan contains {probe_count} probes; maximum is {max} \
         (one port on a /8 or all 65,535 ports on a /24); split larger scans"
    )]
    TooManyProbes {
        /// The requested probe count.
        probe_count: usize,
        /// The maximum allowed probe count.
        max: usize,
    },
    /// `ScanConfig` contained a duplicate target/port pair.
    #[error("duplicate host/port pair {host}:{port}; ScanConfig targets and ports must be unique")]
    DuplicatePair {
        /// The duplicated target.
        host: Ipv4Addr,
        /// The duplicated port.
        port: Port,
    },
    /// Creating the raw transport socket failed.
    #[error("failed to create raw socket (run as root or grant CAP_NET_RAW)")]
    SocketCreation(#[source] io::Error),
    /// Receiving a raw packet failed.
    #[error("failed to receive raw packet")]
    Receive(#[source] io::Error),
    /// The packet receiver thread panicked.
    #[error("packet receiver thread panicked: {0}")]
    ReceiverPanicked(String),
    /// Transmission stopped after part of the scan was sent.
    #[error(transparent)]
    Incomplete(IncompleteScanError),
    /// The `on_result` callback returned an error.
    #[error("callback failed")]
    Callback(#[source] CallbackError),
}

/// An error that stopped probe transmission mid-scan.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum SendError {
    /// The fixed-size SYN packet buffer could not be parsed back into an IPv4 packet.
    #[error("failed to construct IPv4 packet")]
    PacketConstruction,
    /// Sending a probe failed.
    #[error("failed to send SYN to {host}:{port}")]
    Io {
        /// The probe's destination host.
        host: Ipv4Addr,
        /// The probe's destination port.
        port: Port,
        /// The underlying I/O error.
        #[source]
        source: io::Error,
    },
    /// The `on_progress` callback returned an error.
    #[error("callback failed")]
    Callback(#[source] CallbackError),
}

/// An error that stopped transmission after part of a scan was executed.
///
/// `IncompleteScanError` has no public constructor; callers only ever obtain one from
/// [`ScanError::Incomplete`], returned by [`scan`]/[`scan_with_callback`]/[`scan_with_callbacks`].
///
/// # Examples
///
/// ```no_run
/// use singsing_rs::{ScanConfig, ScanError, scan};
/// use std::net::Ipv4Addr;
///
/// let config = ScanConfig::new(vec![Ipv4Addr::LOCALHOST], vec![80], Ipv4Addr::LOCALHOST);
/// if let Err(ScanError::Incomplete(incomplete)) = scan(&config) {
///     eprintln!(
///         "sent {} of {} probes before stopping",
///         incomplete.probes_sent(),
///         incomplete.total_probes()
///     );
///     for result in incomplete.partial_results() {
///         println!("{result:?}");
///     }
/// }
/// ```
#[derive(Debug, thiserror::Error)]
#[error("scan stopped after sending {probes_sent} of {total_probes} probes")]
pub struct IncompleteScanError {
    /// The error that caused the incomplete scan.
    #[source]
    source: SendError,
    /// The results received from probes sent before transmission stopped.
    partial_results: Vec<ScanResult>,
    /// The number of probes successfully sent before the error.
    probes_sent: usize,
    /// The total number of probes requested by the scan.
    total_probes: usize,
}

impl IncompleteScanError {
    /// Returns results received from probes sent before transmission stopped.
    #[must_use]
    pub fn partial_results(&self) -> &[ScanResult] {
        &self.partial_results
    }

    /// Returns the number of probes successfully sent before the error.
    #[must_use]
    pub const fn probes_sent(&self) -> usize {
        self.probes_sent
    }

    /// Returns the total number of probes requested by the scan.
    #[must_use]
    pub const fn total_probes(&self) -> usize {
        self.total_probes
    }
}

/// Configuration for one SYN scan.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub struct ScanConfig {
    /// IPv4 addresses to scan.
    ///
    /// Addresses must be unique.
    pub targets: Vec<Ipv4Addr>,
    /// TCP ports to scan.
    ///
    /// Ports must be unique.
    pub ports: Vec<Port>,
    /// Source IPv4 address assigned to the selected interface.
    pub source: Ipv4Addr,
    /// Approximate maximum packet bandwidth in KiB/s.
    ///
    /// [`ScanConfig::new`] defaults this to 15 KiB/s, or approximately 384 probes per second
    /// with the scanner's 40-byte packet accounting.
    pub bandwidth_kib: u64,
    /// Time to listen for late replies after the final probe.
    ///
    /// Capped at 24 hours; [`ScanConfig::new`] defaults this to 30 seconds.
    pub timeout: Duration,
    /// Whether RST responses should be returned.
    pub show_closed: bool,
}

impl ScanConfig {
    /// Creates a configuration with 15 KiB/s bandwidth and a 30-second late-reply timeout.
    ///
    /// # Examples
    ///
    /// ```
    /// use singsing_rs::ScanConfig;
    /// use std::net::Ipv4Addr;
    ///
    /// let target: Ipv4Addr = "192.168.2.10".parse()?;
    /// let mut config = ScanConfig::new(vec![target], vec![22, 80, 443], Ipv4Addr::LOCALHOST);
    /// config.show_closed = true;
    ///
    /// assert_eq!(config.bandwidth_kib, 15);
    /// assert!(config.show_closed);
    /// # Ok::<(), std::net::AddrParseError>(())
    /// ```
    #[must_use]
    pub const fn new(targets: Vec<Ipv4Addr>, ports: Vec<Port>, source: Ipv4Addr) -> Self {
        Self {
            targets,
            ports,
            source,
            bandwidth_kib: 15,
            timeout: Duration::from_secs(30),
            show_closed: false,
        }
    }
}

/// Probe sending progress reported during a scan.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub struct ScanProgress {
    /// Number of probes sent so far.
    pub probes_sent: usize,
    /// Total number of probes in the scan.
    pub total_probes: usize,
    /// Time elapsed since sending began.
    pub elapsed: Duration,
}

impl ScanProgress {
    /// Creates a progress snapshot from the given probe counts and elapsed time.
    #[must_use]
    pub const fn new(probes_sent: usize, total_probes: usize, elapsed: Duration) -> Self {
        Self {
            probes_sent,
            total_probes,
            elapsed,
        }
    }

    /// Returns the integer completion percentage.
    #[must_use]
    pub const fn percent(self) -> usize {
        if self.total_probes == 0 {
            return 0;
        }
        self.probes_sent.saturating_mul(100) / self.total_probes
    }

    /// Estimates the time required to send the remaining probes.
    ///
    /// Returns `None` before the first probe is sent, since no rate can be estimated yet.
    #[must_use]
    pub fn estimated_remaining(self) -> Option<Duration> {
        let sent = u32::try_from(self.probes_sent).ok()?;
        let remaining = u32::try_from(self.total_probes.saturating_sub(self.probes_sent)).ok()?;

        if sent == 0 {
            return None;
        }
        self.elapsed.checked_mul(remaining)?.checked_div(sent)
    }
}

/// The state inferred from a TCP response.
///
/// # Examples
///
/// ```
/// use singsing_rs::{PortState, ScanResult};
/// use std::net::Ipv4Addr;
///
/// let result = ScanResult::new(Ipv4Addr::LOCALHOST, 443, PortState::Open);
/// match result.state {
///     PortState::Open => println!("{}:{} is open", result.host, result.port),
///     PortState::Closed => println!("{}:{} is closed", result.host, result.port),
///     _ => println!("{}:{} is some other state", result.host, result.port),
/// }
/// ```
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum PortState {
    /// A SYN/ACK was received.
    Open,
    /// A RST was received.
    Closed,
}

/// One response produced by a scan.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub struct ScanResult {
    /// The responding host.
    pub host: Ipv4Addr,
    /// The responding TCP port.
    pub port: Port,
    /// The inferred port state.
    pub state: PortState,
}

impl ScanResult {
    /// Creates a scan result for the given host, port, and inferred state.
    #[must_use]
    pub const fn new(host: Ipv4Addr, port: Port, state: PortState) -> Self {
        Self { host, port, state }
    }
}

/// Resolves the first IPv4 address assigned to a network interface.
///
/// # Errors
///
/// Returns an error if the interface does not exist or has no IPv4 address.
///
/// # Examples
///
/// ```
/// use singsing_rs::{InterfaceError, interface_ipv4};
/// use std::net::Ipv4Addr;
///
/// assert_eq!(interface_ipv4("lo")?, Ipv4Addr::LOCALHOST);
/// # Ok::<(), InterfaceError>(())
/// ```
pub fn interface_ipv4(name: &str) -> Result<Ipv4Addr, InterfaceError> {
    let interface = datalink::interfaces()
        .into_iter()
        .find(|interface| interface.name == name)
        .ok_or_else(|| InterfaceError::NotFound {
            name: name.to_owned(),
        })?;

    interface
        .ips
        .into_iter()
        .find_map(|network| match network.ip() {
            IpAddr::V4(address) => Some(address),
            IpAddr::V6(_) => None,
        })
        .ok_or_else(|| InterfaceError::NoIpv4 {
            name: name.to_owned(),
        })
}

/// Expands an IPv4 address or CIDR into scan targets.
///
/// Network and broadcast addresses are omitted for prefixes from `/0` through `/30`. Both
/// addresses of a `/31` are included, as is the single address of a `/32`, matching
/// [`Ipv4Net::hosts`].
///
/// # Errors
///
/// Returns an error for malformed IPv4/CIDR input or a network containing more usable addresses
/// than a `/8`.
///
/// # Examples
///
/// ```
/// use singsing_rs::parse_targets;
/// use std::net::Ipv4Addr;
///
/// assert_eq!(
///     parse_targets("192.168.2.0/30")?,
///     ["192.168.2.1".parse::<Ipv4Addr>()?, "192.168.2.2".parse()?]
/// );
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn parse_targets(input: &str) -> Result<Vec<Ipv4Addr>, TargetsError> {
    let network = if input.contains('/') {
        input.parse().map_err(TargetsError::InvalidNetwork)?
    } else {
        format!("{input}/32")
            .parse()
            .map_err(TargetsError::InvalidAddress)?
    };

    if usable_target_count(network).is_none_or(|count| count > MAX_PROBES) {
        return Err(TargetsError::TooLarge {
            network,
            max: MAX_PROBES,
        });
    }

    Ok(network.hosts().collect())
}

/// Parses comma-separated ports and inclusive ranges such as `21-23,80,443`.
///
/// Duplicate ports are removed and the result is returned in ascending order.
///
/// # Errors
///
/// Returns an error for empty items, reversed ranges, port zero, or values larger than 65535.
///
/// # Examples
///
/// ```
/// use singsing_rs::{PortsError, parse_ports};
///
/// assert_eq!(parse_ports("22,80,79-81")?, [22, 79, 80, 81]);
/// # Ok::<(), PortsError>(())
/// ```
pub fn parse_ports(input: &str) -> Result<Vec<Port>, PortsError> {
    let mut ports = BTreeSet::new();

    for item in input.split(',') {
        if item.is_empty() {
            return Err(PortsError::EmptyItem {
                input: input.to_owned(),
            });
        }

        let (start, end) = if let Some((start, end)) = item.split_once('-') {
            if end.contains('-') {
                return Err(PortsError::InvalidRange {
                    item: item.to_owned(),
                });
            }
            // Port range.
            (parse_port(start)?, parse_port(end)?)
        } else {
            // Single port.
            let port = parse_port(item)?;
            (port, port)
        };

        // After both halves are known to be valid ports, check for a reversed range.
        if start > end {
            return Err(PortsError::ReversedRange {
                item: item.to_owned(),
            });
        }

        // Insert the whole range at once.
        ports.extend(start..=end);
    }

    Ok(ports.into_iter().collect())
}

/// Reads TCP ports from a services file (normally `/etc/services`).
///
/// Duplicate ports are removed and the result is returned in ascending order.
///
/// # Errors
///
/// Returns an error when the file cannot be read or contains no TCP services.
///
/// # Examples
///
/// ```
/// use singsing_rs::ports_from_services;
/// use std::fs;
///
/// let path = std::env::temp_dir().join("singsing-rs-doctest-services");
/// fs::write(&path, "ssh 22/tcp\ndomain 53/udp\nhttp 80/tcp\n")?;
///
/// let ports = ports_from_services(&path)?;
/// fs::remove_file(&path)?;
///
/// assert_eq!(ports, [22, 80]);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn ports_from_services(path: impl AsRef<Path>) -> Result<Vec<Port>, PortsError> {
    let contents =
        fs::read_to_string(path.as_ref()).map_err(|source| PortsError::ServicesFileRead {
            path: path.as_ref().to_path_buf(),
            source,
        })?;
    let mut ports = BTreeSet::new();

    for line in contents.lines() {
        // Break lines into fields, skipping comments and empty lines.
        let mut fields = line
            .split('#')
            .next()
            .unwrap_or_default()
            .split_whitespace();

        // Skip service names and extract valid TCP ports to insert into the set.
        let _service = fields.next();
        if let Some(port_protocol) = fields.next()
            && let Some((port, "tcp")) = port_protocol.split_once('/')
            && let Ok(port) = parse_port(port)
        {
            ports.insert(port);
        }
    }

    if ports.is_empty() {
        return Err(PortsError::NoTcpServices {
            path: path.as_ref().to_path_buf(),
        });
    }

    Ok(ports.into_iter().collect())
}

/// Executes a Linux IPv4 SYN scan.
///
/// No reply means filtered or unreachable and therefore produces no result. Raw sockets require
/// root or `CAP_NET_RAW`.
///
/// # Errors
///
/// Returns an error for an empty or excessively large scan, invalid bandwidth, an excessive
/// timeout, duplicate targets or ports, raw socket permission failures, packet send failures,
/// or receiver failures. A transmission-phase failure is returned as [`IncompleteScanError`],
/// which retains results received for successfully sent probes.
///
/// # Examples
///
/// ```no_run
/// use singsing_rs::{ScanConfig, interface_ipv4, parse_targets, scan};
///
/// let source = interface_ipv4("eth0")?;
/// let targets = parse_targets("192.168.2.10")?;
/// let config = ScanConfig::new(targets, vec![22, 80, 443], source);
///
/// for result in scan(&config)? {
///     println!("{result:?}");
/// }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn scan(config: &ScanConfig) -> Result<Vec<ScanResult>, ScanError> {
    scan_with_callbacks(config, |_| Ok(()), |_| Ok(()))
}

/// Executes a SYN scan and calls `on_result` as each response arrives.
///
/// Results are still returned in sorted order after the scan. The callback is useful for
/// interactive clients that need immediate per-result feedback.
///
/// # Errors
///
/// Returns the same errors as [`scan`], along with errors returned by `on_result`.
///
/// # Examples
///
/// ```no_run
/// use singsing_rs::{ScanConfig, interface_ipv4, parse_targets, scan_with_callback};
///
/// let source = interface_ipv4("eth0")?;
/// let targets = parse_targets("192.168.2.10")?;
/// let config = ScanConfig::new(targets, vec![22, 80, 443], source);
///
/// scan_with_callback(&config, |result| {
///     println!("{result:?}");
///     Ok(())
/// })?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn scan_with_callback(
    config: &ScanConfig,
    on_result: impl FnMut(ScanResult) -> Result<(), CallbackError> + Send + 'static,
) -> Result<Vec<ScanResult>, ScanError> {
    scan_with_callbacks(config, on_result, |_| Ok(()))
}

/// Executes a SYN scan with callbacks for results and sending progress.
///
/// `on_result` runs as each response arrives. It needs to be `Send + 'static` because it gets
/// moved into the spawned receiver thread. While probes are being sent, `on_progress` runs every
/// minute for the first ten minutes, every ten minutes through the first hour, and every
/// thirty minutes thereafter.
///
/// # Errors
///
/// Returns the same errors as [`scan`], along with errors returned by either callback.
///
/// # Examples
///
/// ```no_run
/// use singsing_rs::{ScanConfig, interface_ipv4, parse_targets, scan_with_callbacks};
///
/// let source = interface_ipv4("eth0")?;
/// let targets = parse_targets("192.168.2.10")?;
/// let config = ScanConfig::new(targets, vec![22, 80, 443], source);
///
/// scan_with_callbacks(
///     &config,
///     |result| {
///         println!("{result:?}");
///         Ok(())
///     },
///     |progress| {
///         eprintln!("{}% complete", progress.percent());
///         Ok(())
///     },
/// )?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn scan_with_callbacks(
    config: &ScanConfig,
    mut on_result: impl FnMut(ScanResult) -> Result<(), CallbackError> + Send + 'static,
    mut on_progress: impl FnMut(ScanProgress) -> Result<(), CallbackError>,
) -> Result<Vec<ScanResult>, ScanError> {
    // Validate the scan configuration and build the expected responses table.
    let probe_count = validate_scan(config)?;
    let source_port = source_port();
    let nonce = nonce();
    let expected = Arc::new(expected_responses(config, nonce, probe_count)?);

    // Create the transport channel (`Layer3` raw socket).
    let protocol = TransportChannelType::Layer3(IpNextHeaderProtocols::Tcp);
    let (mut sender, mut receiver) =
        transport_channel(RECEIVE_BUFFER_LEN, protocol).map_err(ScanError::SocketCreation)?;

    // Spawn the receiver thread.
    let done = Arc::new(AtomicBool::new(false));
    let receiver_done = Arc::clone(&done);
    let receiver_expected = Arc::clone(&expected);
    let source = config.source;
    let timeout = config.timeout;
    let show_closed = config.show_closed;
    let receive_thread = thread::spawn(move || {
        let receive_config = ReceiveConfig {
            expected: &receiver_expected,
            source,
            source_port,
            show_closed,
            done: &receiver_done,
            timeout,
        };
        receive(&mut receiver, &receive_config, &mut on_result)
    });

    // Compute the send interval based on the requested bandwidth.
    //
    // Unlike `timeout`, `bandwidth_kib` has no upper sanity limit, only the overflow guard below
    // (~u64::MAX / 1024 KiB/s). An extreme but non-overflowing value drives `packets_per_second`
    // high enough that this division floors to zero, making `interval` `Duration::ZERO`; the send
    // loop then never sleeps, so the practical effect is unthrottled sending rather than a panic
    // or incorrect behavior, so no cap is needed.
    let bytes_per_second = config
        .bandwidth_kib
        .checked_mul(1024)
        .ok_or(ScanError::BandwidthOverflow)?;
    let packets_per_second = (bytes_per_second / 40).max(1);
    let interval = Duration::from_nanos(1_000_000_000_u64 / packets_per_second);

    // Send loop.
    //
    // Runs as an inline closure so a mid-loop error can be captured without immediately returning
    // from the outer function. This way, a send failure doesn't abort the receiver early, but just
    // gets folded into the final error once both sides are done, so partial results are not lost.
    let mut next_send = Instant::now();
    let started = next_send;
    let mut next_progress = ONE_MINUTE;
    let mut probes_sent = 0;
    let send_result = (|| -> Result<(), SendError> {
        #[expect(
            clippy::iter_over_hash_type,
            reason = "randomized `HashMap` iteration order is deliberate; see README's Transmission order section"
        )]
        for (&(host, port), &sequence) in expected.iter() {
            // Build one TCP SYN packet and send it.
            let packet = syn_packet(config.source, host, source_port, port, sequence);
            let ipv4_packet =
                MutableIpv4Packet::owned(packet).ok_or(SendError::PacketConstruction)?;
            sender
                .send_to(ipv4_packet, IpAddr::V4(host))
                .map_err(|io_error| SendError::Io {
                    host,
                    port,
                    source: io_error,
                })?;
            probes_sent += 1;

            // Throttle the send loop to the requested bandwidth.
            next_send += interval;
            if let Some(delay) = next_send.checked_duration_since(Instant::now()) {
                thread::sleep(delay);
            }

            // Track progress and invoke the callback if necessary.
            //
            // Unlike a failing `on_result` on the receive side, a failing `on_progress` here stops
            // the send loop like any other send-loop error, so it's preserved as `SendError::Callback`
            // inside `IncompleteScanError` (with partial results and counts), not discarded.
            let now = Instant::now();
            let elapsed = now.duration_since(started);
            if elapsed >= next_progress {
                on_progress(ScanProgress {
                    probes_sent,
                    total_probes: probe_count,
                    elapsed,
                })
                .map_err(SendError::Callback)?;
                next_progress = advance_progress_deadline(next_progress, elapsed);
            }
        }

        Ok(())
    })();

    // Whatever happens, unconditionally mark the send loop as done.
    //
    // Everything this thread wrote to memory before this store is guaranteed to be visible to the
    // receive thread that later does an `Acquire` load on `done`.
    done.store(true, Ordering::Release);

    // Join the receive thread and collect the results.
    //
    // The first `?` (via `map_err`) converts the panic payload to a `ScanError::ReceiverPanicked`.
    // The second `?` propagates any other error from the receive thread.
    let mut results = receive_thread.join().map_err(|payload| {
        ScanError::ReceiverPanicked(describe_panic_payload(&*payload).to_owned())
    })??;

    // Sort the results by host and port.
    results.sort_unstable_by_key(|result| (u32::from(result.host), result.port));

    // If the send loop failed mid-scan, return an `IncompleteScanError` with the collected results.
    if let Err(error) = send_result {
        return Err(ScanError::Incomplete(IncompleteScanError {
            source: error,
            partial_results: results,
            probes_sent,
            total_probes: probe_count,
        }));
    }

    Ok(results)
}

/// Returns the number of usable addresses in an IPv4 network.
///
/// Both addresses of a `/31` count as usable and a `/32` counts as one; otherwise the network
/// and broadcast addresses are excluded. Returns `None` on prefix-length arithmetic overflow.
fn usable_target_count(network: Ipv4Net) -> Option<usize> {
    let host_bits = 32_u32.checked_sub(u32::from(network.prefix_len()))?;

    match host_bits {
        0 => Some(1),
        1 => Some(2),
        bits => 1_usize.checked_shl(bits)?.checked_sub(2),
    }
}

/// Parses a single TCP port, rejecting port zero.
fn parse_port(input: &str) -> Result<Port, PortsError> {
    let port = input.parse().map_err(|source| PortsError::InvalidPort {
        input: input.to_owned(),
        source,
    })?;

    if port == 0 {
        return Err(PortsError::PortZero);
    }

    Ok(port)
}

/// Validates a scan configuration and returns its total probe count.
fn validate_scan(config: &ScanConfig) -> Result<usize, ScanError> {
    validate_probe_count(
        config.targets.len(),
        config.ports.len(),
        config.bandwidth_kib,
        config.timeout,
    )
}

/// Validates scan size and configuration limits, returning the total probe count.
///
/// Rejects an empty target or port list, zero bandwidth, a late-reply timeout above
/// [`MAX_TIMEOUT`], and a target * port product above [`MAX_PROBES`].
fn validate_probe_count(
    target_count: usize,
    port_count: usize,
    bandwidth_kib: u64,
    timeout: Duration,
) -> Result<usize, ScanError> {
    if target_count == 0 || port_count == 0 {
        return Err(ScanError::EmptyScan);
    }
    if bandwidth_kib == 0 {
        return Err(ScanError::ZeroBandwidth);
    }
    if timeout > MAX_TIMEOUT {
        return Err(ScanError::TimeoutTooLarge {
            timeout,
            max: MAX_TIMEOUT,
        });
    }

    let probe_count = target_count
        .checked_mul(port_count)
        .ok_or(ScanError::ScanSizeOverflow)?;
    if probe_count > MAX_PROBES {
        return Err(ScanError::TooManyProbes {
            probe_count,
            max: MAX_PROBES,
        });
    }

    Ok(probe_count)
}

/// Picks a random ephemeral TCP source port in the `49152..=65535` range, reused for every probe in the scan.
#[expect(
    clippy::as_conversions,
    reason = "`nonce() % 16384` is always in `0..16384`, so it always fits in a `u16`"
)]
fn source_port() -> Port {
    49152 + (nonce() % 16384) as u16
}

/// Returns a per-scan random nonce derived from the current sub-second time.
///
/// This nonce is not cryptographically robust, but it is sufficient for our purposes.
fn nonce() -> u32 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .subsec_nanos()
}

/// Builds the expected-response table mapping each target/port pair to its deterministic sequence
/// number.
///
/// Returns [`ScanError::DuplicatePair`] for a duplicate target/port pair.
fn expected_responses(
    config: &ScanConfig,
    nonce: u32,
    probe_count: usize,
) -> Result<ExpectedResponses, ScanError> {
    let mut expected = HashMap::with_capacity(probe_count);

    for &host in &config.targets {
        for &port in &config.ports {
            if expected
                .insert((host, port), sequence(host, port, nonce))
                .is_some()
            {
                return Err(ScanError::DuplicatePair { host, port });
            }
        }
    }
    Ok(expected)
}

/// Derives the deterministic expected TCP sequence number for a host/port pair, given the nonce.
///
/// This allows to correlate a reply with a probe, without the need to track live per-connection
/// state in memory. This classic stateless-SYN-scanning trick is robust against accidental
/// misclassification, but it does not provide any protection against a deliberately hostile
/// target trying to defeat correlation.
fn sequence(host: Ipv4Addr, port: Port, nonce: u32) -> SeqNum {
    u32::from(host)
        .rotate_left(13)
        .wrapping_add(u32::from(port).rotate_left(3))
        ^ nonce
}

/// Builds a raw 40-byte IPv4/TCP SYN packet for one probe.
fn syn_packet(
    source: Ipv4Addr,
    destination: Ipv4Addr,
    source_port: Port,
    destination_port: Port,
    sequence: SeqNum,
) -> Vec<u8> {
    let mut bytes = vec![0_u8; PACKET_LEN];

    #[expect(
        clippy::expect_used,
        reason = "`bytes` is exactly `PACKET_LEN`, sized to fit one IPv4 header and one TCP header, so packet construction cannot fail"
    )]
    let mut ipv4 = MutableIpv4Packet::new(&mut bytes).expect("fixed-size IPv4 packet");
    ipv4.set_version(4);
    ipv4.set_header_length(5);
    ipv4.set_total_length(40);
    #[expect(
        clippy::as_conversions,
        reason = "`sequence >> 16` keeps only the top 16 bits, so it always fits in a `u16`"
    )]
    ipv4.set_identification((sequence >> 16) as u16);
    ipv4.set_ttl(64);
    ipv4.set_next_level_protocol(IpNextHeaderProtocols::Tcp);
    ipv4.set_source(source);
    ipv4.set_destination(destination);

    #[expect(
        clippy::expect_used,
        reason = "`bytes` is exactly `PACKET_LEN`, sized to fit one IPv4 header and one TCP header, so packet construction cannot fail"
    )]
    let mut tcp = MutableTcpPacket::new(ipv4.payload_mut()).expect("fixed-size TCP packet");
    tcp.set_source(source_port);
    tcp.set_destination(destination_port);
    tcp.set_sequence(sequence);
    tcp.set_data_offset(5);
    tcp.set_flags(TcpFlags::SYN);
    tcp.set_window(64240);
    tcp.set_checksum(ipv4_checksum(&tcp.to_immutable(), &source, &destination));
    ipv4.set_checksum(checksum(&ipv4.to_immutable()));

    bytes
}

/// Advances a progress deadline past `elapsed`, skipping any missed intervals.
fn advance_progress_deadline(mut deadline: Duration, elapsed: Duration) -> Duration {
    while deadline <= elapsed {
        deadline = next_progress_deadline(deadline);
    }
    deadline
}

/// Returns the next progress deadline after `previous`.
///
/// Follows a growing schedule: every minute for the first ten minutes, every ten minutes through
/// the first hour, then every thirty minutes thereafter.
fn next_progress_deadline(previous: Duration) -> Duration {
    let interval = if previous < TEN_MINUTES {
        ONE_MINUTE
    } else if previous < ONE_HOUR {
        TEN_MINUTES
    } else {
        THIRTY_MINUTES
    };
    previous + interval
}

/// Configuration for receiving packets.
struct ReceiveConfig<'a> {
    /// Map of expected (source, port) pairs to sequence numbers.
    expected: &'a ExpectedResponses,
    /// Source IP address to filter packets by.
    source: Ipv4Addr,
    /// Source port to filter packets by.
    source_port: Port,
    /// Whether to show closed connections.
    show_closed: bool,
    /// Atomic flag indicating when to stop receiving.
    done: &'a AtomicBool,
    /// Timeout duration for receiving packets.
    timeout: Duration,
}

/// Reads and classifies raw packets until sending is done and the late-reply timeout elapses,
/// returning accepted results in arrival order.
fn receive(
    receiver: &mut TransportReceiver,
    config: &ReceiveConfig<'_>,
    on_result: &mut impl FnMut(ScanResult) -> Result<(), CallbackError>,
) -> Result<Vec<ScanResult>, ScanError> {
    let mut iterator = ipv4_packet_iter(receiver);
    let mut results = Vec::new();
    let mut seen = HashSet::new();
    let mut deadline = None;

    loop {
        // Check the done flag and set the deadline only once.
        if config.done.load(Ordering::Acquire) && deadline.is_none() {
            deadline = Some(Instant::now() + config.timeout);
        }
        // Break the loop if the deadline has passed.
        if deadline.is_some_and(|deadline| Instant::now() >= deadline) {
            break;
        }

        // Calculate the wait duration based on the deadline (capped at 100ms).
        let wait = deadline
            .and_then(|deadline| deadline.checked_duration_since(Instant::now()))
            .unwrap_or(Duration::from_millis(100))
            .min(Duration::from_millis(100));

        // Try to read a packet, blocking for at most `wait`.
        //
        // A genuine I/O error becomes a `ScanError::Receive` and ends the whole scan.
        // If no packet is received within `wait`, continue to the next iteration.
        let Some((ipv4, _)) = iterator
            .next_with_timeout(wait)
            .map_err(ScanError::Receive)?
        else {
            continue;
        };

        // If a packet is received, classify it and add it to the results.
        //
        // If the packet is not a valid response, it is ignored and the scan continues.
        let Some(result) = classify_response(
            &ipv4,
            config.expected,
            config.source,
            config.source_port,
            config.show_closed,
            &mut seen,
        ) else {
            continue;
        };

        // Call the user-defined callback with the accepted result and then add it to the results
        // in raw arrival order. Unlike a send-loop failure, a failing callback here currently
        // discards `results` entirely rather than preserving it via `IncompleteScanError`.
        on_result(result).map_err(ScanError::Callback)?;
        results.push(result);
    }

    Ok(results)
}

/// Correlates one received IPv4 packet against the expected-response table.
///
/// Returns `Some` only for a not-yet-seen reply whose destination address and port match the
/// scan's source, whose source host/port matches an actual probe, and whose acknowledgement
/// number matches the expected sequence.
fn classify_response(
    ipv4: &Ipv4Packet<'_>,
    expected: &ExpectedResponses,
    source: Ipv4Addr,
    source_port: Port,
    show_closed: bool,
    seen: &mut HashSet<(Ipv4Addr, Port)>,
) -> Option<ScanResult> {
    if ipv4.get_destination() != source {
        return None;
    }

    let tcp = TcpPacket::new(ipv4.payload())?;
    let key = (ipv4.get_source(), tcp.get_source());
    let (host, port) = key;
    let sequence = expected.get(&key)?;
    if tcp.get_destination() != source_port || tcp.get_acknowledgement() != sequence.wrapping_add(1)
    {
        return None;
    }

    let flags = tcp.get_flags();
    let state = if flags == TcpFlags::SYN | TcpFlags::ACK {
        PortState::Open
    } else if show_closed && (flags == TcpFlags::RST || flags == TcpFlags::RST | TcpFlags::ACK) {
        PortState::Closed
    } else {
        return None;
    };

    // Return `None` if the key is already seen, to avoid duplicate results.
    if !seen.insert(key) {
        return None;
    }

    Some(ScanResult { host, port, state })
}

/// Extracts a human-readable message from a thread panic payload.
///
/// Falls back to a generic message when the payload isn't the common `&str` or `String` shape
/// produced by `panic!`.
fn describe_panic_payload(payload: &(dyn Any + Send)) -> &str {
    payload
        .downcast_ref::<&str>()
        .copied()
        .or_else(|| payload.downcast_ref::<String>().map(String::as_str))
        .unwrap_or("unknown panic payload")
}

#[cfg(test)]
#[expect(clippy::panic_in_result_fn, reason = "panics are allowed in test code")]
#[expect(clippy::unwrap_used, reason = "tests can use `unwrap`")]
mod tests {
    use std::path::PathBuf;
    use std::sync::atomic::AtomicUsize;
    use std::{env, fs, io, process};

    use super::*;

    fn response_packet(
        remote: Ipv4Addr,
        local: Ipv4Addr,
        remote_port: Port,
        local_port: Port,
        acknowledgement: SeqNum,
        flags: u8,
    ) -> Vec<u8> {
        let mut bytes = vec![0_u8; PACKET_LEN];
        let mut ipv4 = MutableIpv4Packet::new(&mut bytes).unwrap();
        ipv4.set_version(4);
        ipv4.set_header_length(5);
        ipv4.set_total_length(40);
        ipv4.set_next_level_protocol(IpNextHeaderProtocols::Tcp);
        ipv4.set_source(remote);
        ipv4.set_destination(local);

        let mut tcp = MutableTcpPacket::new(ipv4.payload_mut()).unwrap();
        tcp.set_source(remote_port);
        tcp.set_destination(local_port);
        tcp.set_acknowledgement(acknowledgement);
        tcp.set_data_offset(5);
        tcp.set_flags(flags);
        bytes
    }

    fn classify_packet(
        bytes: &[u8],
        expected: &ExpectedResponses,
        source: Ipv4Addr,
        source_port: Port,
        show_closed: bool,
        seen: &mut HashSet<(Ipv4Addr, Port)>,
    ) -> Option<ScanResult> {
        let ipv4 = Ipv4Packet::new(bytes)?;
        classify_response(&ipv4, expected, source, source_port, show_closed, seen)
    }

    fn services_path() -> PathBuf {
        static NEXT_FILE: AtomicUsize = AtomicUsize::new(0);

        let number = NEXT_FILE.fetch_add(1, Ordering::Relaxed);
        env::temp_dir().join(format!("singsing-rs-services-{}-{number}", process::id()))
    }

    fn services_from(contents: &str) -> anyhow::Result<Vec<u16>> {
        let path = services_path();
        fs::write(&path, contents)?;
        let result = ports_from_services(&path).map_err(anyhow::Error::from);
        fs::remove_file(path)?;
        result
    }

    #[test]
    fn parses_ports_ranges_and_duplicates() {
        assert_eq!(parse_ports("22,80,79-81").unwrap(), [22, 79, 80, 81]);
    }

    #[test]
    fn rejects_invalid_ports() {
        assert!(matches!(parse_ports("0"), Err(PortsError::PortZero)));
        assert!(matches!(
            parse_ports("80-79"),
            Err(PortsError::ReversedRange { item }) if item == "80-79"
        ));
        assert!(matches!(
            parse_ports("65536"),
            Err(PortsError::InvalidPort { input, .. }) if input == "65536"
        ));
        assert!(matches!(
            parse_ports("22,"),
            Err(PortsError::EmptyItem { input }) if input == "22,"
        ));
        assert!(matches!(
            parse_ports("1-2-3"),
            Err(PortsError::InvalidRange { item }) if item == "1-2-3"
        ));
    }

    #[test]
    fn parses_host_and_network() {
        assert_eq!(
            parse_targets("192.168.2.9").unwrap(),
            ["192.168.2.9".parse::<Ipv4Addr>().unwrap()]
        );
        assert_eq!(
            parse_targets("192.168.2.0/30").unwrap(),
            [
                "192.168.2.1".parse::<Ipv4Addr>().unwrap(),
                "192.168.2.2".parse::<Ipv4Addr>().unwrap()
            ]
        );
        assert_eq!(
            parse_targets("192.168.2.0/31").unwrap(),
            [
                "192.168.2.0".parse::<Ipv4Addr>().unwrap(),
                "192.168.2.1".parse::<Ipv4Addr>().unwrap()
            ]
        );
        assert_eq!(
            parse_targets("192.168.2.7/32").unwrap(),
            ["192.168.2.7".parse::<Ipv4Addr>().unwrap()]
        );
    }

    #[test]
    fn normalizes_host_bits_and_rejects_invalid_targets() {
        assert_eq!(
            parse_targets("192.168.2.7/30").unwrap(),
            [
                "192.168.2.5".parse::<Ipv4Addr>().unwrap(),
                "192.168.2.6".parse::<Ipv4Addr>().unwrap()
            ]
        );
        assert!(matches!(
            parse_targets(""),
            Err(TargetsError::InvalidAddress(_))
        ));
        assert!(matches!(
            parse_targets("not-an-address"),
            Err(TargetsError::InvalidAddress(_))
        ));
        assert!(matches!(
            parse_targets("192.168.2.1/33"),
            Err(TargetsError::InvalidNetwork(_))
        ));
    }

    #[test]
    fn rejects_oversized_cidr_before_expansion() {
        let slash_8 = "10.0.0.0/8".parse::<Ipv4Net>().unwrap();
        let slash_31 = "192.168.2.0/31".parse::<Ipv4Net>().unwrap();
        let slash_32 = "192.168.2.1/32".parse::<Ipv4Net>().unwrap();

        assert_eq!(usable_target_count(slash_8), Some(MAX_PROBES));
        assert_eq!(usable_target_count(slash_31), Some(2));
        assert_eq!(usable_target_count(slash_32), Some(1));
        assert!(matches!(
            parse_targets("10.0.0.0/7"),
            Err(TargetsError::TooLarge { max, .. }) if max == MAX_PROBES
        ));
        assert!(matches!(
            parse_targets("0.0.0.0/0"),
            Err(TargetsError::TooLarge { max, .. }) if max == MAX_PROBES
        ));
    }

    #[test]
    fn resolves_loopback_interface_address() {
        assert_eq!(interface_ipv4("lo").unwrap(), Ipv4Addr::LOCALHOST);
    }

    #[test]
    fn rejects_unknown_interface() {
        let name = "singsing-rs-interface-does-not-exist";

        assert!(matches!(
            interface_ipv4(name),
            Err(InterfaceError::NotFound { name: n }) if n == name
        ));
    }

    #[test]
    #[expect(
        clippy::as_conversions,
        reason = "`sequence >> 16` keeps only the top 16 bits, so it always fits in a `u16`"
    )]
    fn builds_valid_syn_packet() {
        let source = "192.168.2.1".parse().unwrap();
        let destination = "172.16.100.2".parse().unwrap();
        let sequence = 0x1234_5678;
        let bytes = syn_packet(source, destination, 50000, 443, sequence);
        let ipv4 = Ipv4Packet::new(&bytes).unwrap();
        let tcp = TcpPacket::new(ipv4.payload()).unwrap();

        assert_eq!(bytes.len(), PACKET_LEN);
        assert_eq!(ipv4.get_version(), 4);
        assert_eq!(ipv4.get_header_length(), 5);
        assert_eq!(ipv4.get_total_length(), 40);
        assert_eq!(ipv4.get_identification(), (sequence >> 16) as u16);
        assert_eq!(ipv4.get_ttl(), 64);
        assert_eq!(ipv4.get_next_level_protocol(), IpNextHeaderProtocols::Tcp);
        assert_eq!(ipv4.get_source(), source);
        assert_eq!(ipv4.get_destination(), destination);
        let mut ip_for_checksum = MutableIpv4Packet::owned(bytes.clone()).unwrap();
        ip_for_checksum.set_checksum(0);
        assert_eq!(
            ipv4.get_checksum(),
            checksum(&ip_for_checksum.to_immutable())
        );
        let mut tcp_for_checksum = MutableTcpPacket::owned(tcp.packet().to_vec()).unwrap();
        tcp_for_checksum.set_checksum(0);
        assert_eq!(
            tcp.get_checksum(),
            ipv4_checksum(&tcp_for_checksum.to_immutable(), &source, &destination)
        );
        assert_eq!(tcp.packet().len(), 20);
        assert!(tcp.payload().is_empty());
        assert_eq!(tcp.get_source(), 50000);
        assert_eq!(tcp.get_destination(), 443);
        assert_eq!(tcp.get_sequence(), sequence);
        assert_eq!(tcp.get_acknowledgement(), 0);
        assert_eq!(tcp.get_data_offset(), 5);
        assert_eq!(tcp.get_flags(), TcpFlags::SYN);
        assert_eq!(tcp.get_window(), 64240);
        assert_eq!(tcp.get_urgent_ptr(), 0);
    }

    #[test]
    fn accepts_open_response_once() {
        let source = "192.168.2.1".parse().unwrap();
        let target = "172.16.100.2".parse().unwrap();
        let source_port = 50000;
        let target_port = 443;
        let sequence = 0x1234_5678_u32;
        let expected = HashMap::from([((target, target_port), sequence)]);
        let open = ScanResult {
            host: target,
            port: target_port,
            state: PortState::Open,
        };

        let valid_open = response_packet(
            target,
            source,
            target_port,
            source_port,
            sequence.wrapping_add(1),
            TcpFlags::SYN | TcpFlags::ACK,
        );
        let mut seen = HashSet::new();
        assert_eq!(
            classify_packet(
                &valid_open,
                &expected,
                source,
                source_port,
                false,
                &mut seen
            ),
            Some(open)
        );
        assert_eq!(
            classify_packet(
                &valid_open,
                &expected,
                source,
                source_port,
                false,
                &mut seen
            ),
            None
        );
    }

    #[test]
    fn rejects_uncorrelated_responses() {
        let source = "192.168.2.1".parse().unwrap();
        let target = "172.16.100.2".parse().unwrap();
        let other_target = "172.16.100.3".parse().unwrap();
        let source_port = 50000;
        let target_port = 443;
        let sequence = 0x1234_5678_u32;
        let expected = HashMap::from([((target, target_port), sequence)]);
        let invalid_packets = [
            response_packet(
                target,
                "192.168.2.2".parse().unwrap(),
                target_port,
                source_port,
                sequence.wrapping_add(1),
                TcpFlags::SYN | TcpFlags::ACK,
            ),
            response_packet(
                other_target,
                source,
                target_port,
                source_port,
                sequence.wrapping_add(1),
                TcpFlags::SYN | TcpFlags::ACK,
            ),
            response_packet(
                target,
                source,
                80,
                source_port,
                sequence.wrapping_add(1),
                TcpFlags::SYN | TcpFlags::ACK,
            ),
            response_packet(
                target,
                source,
                target_port,
                source_port + 1,
                sequence.wrapping_add(1),
                TcpFlags::SYN | TcpFlags::ACK,
            ),
            response_packet(
                target,
                source,
                target_port,
                source_port,
                sequence,
                TcpFlags::SYN | TcpFlags::ACK,
            ),
        ];
        for packet in invalid_packets {
            assert_eq!(
                classify_packet(
                    &packet,
                    &expected,
                    source,
                    source_port,
                    false,
                    &mut HashSet::new()
                ),
                None
            );
        }
    }

    #[test]
    fn reports_closed_responses_only_when_requested() {
        let source = "192.168.2.1".parse().unwrap();
        let target = "172.16.100.2".parse().unwrap();
        let source_port = 50000;
        let target_port = 443;
        let sequence = 0x1234_5678_u32;
        let expected = HashMap::from([((target, target_port), sequence)]);
        let closed_packet = response_packet(
            target,
            source,
            target_port,
            source_port,
            sequence.wrapping_add(1),
            TcpFlags::RST | TcpFlags::ACK,
        );
        let mut closed_seen = HashSet::new();
        assert_eq!(
            classify_packet(
                &closed_packet,
                &expected,
                source,
                source_port,
                false,
                &mut closed_seen
            ),
            None
        );
        assert_eq!(
            classify_packet(
                &closed_packet,
                &expected,
                source,
                source_port,
                true,
                &mut closed_seen
            ),
            Some(ScanResult {
                host: target,
                port: target_port,
                state: PortState::Closed,
            })
        );
    }

    #[test]
    fn ignores_truncated_and_unexpected_responses() {
        let source = "192.168.2.1".parse().unwrap();
        let target = "172.16.100.2".parse().unwrap();
        let source_port = 50000;
        let target_port = 443;
        let sequence = 0x1234_5678_u32;
        let expected = HashMap::from([((target, target_port), sequence)]);
        let mut truncated = vec![0_u8; 20];
        let mut ipv4 = MutableIpv4Packet::new(&mut truncated).unwrap();
        ipv4.set_version(4);
        ipv4.set_header_length(5);
        ipv4.set_total_length(20);
        ipv4.set_next_level_protocol(IpNextHeaderProtocols::Tcp);
        ipv4.set_source(target);
        ipv4.set_destination(source);

        let mut seen = HashSet::new();
        assert_eq!(
            classify_packet(&truncated, &expected, source, source_port, false, &mut seen),
            None
        );
        for flags in [TcpFlags::ACK, TcpFlags::SYN | TcpFlags::ACK | TcpFlags::RST] {
            let packet = response_packet(
                target,
                source,
                target_port,
                source_port,
                sequence.wrapping_add(1),
                flags,
            );
            assert_eq!(
                classify_packet(&packet, &expected, source, source_port, true, &mut seen),
                None
            );
        }

        let valid = response_packet(
            target,
            source,
            target_port,
            source_port,
            sequence.wrapping_add(1),
            TcpFlags::SYN | TcpFlags::ACK,
        );
        assert!(
            classify_packet(&valid, &expected, source, source_port, false, &mut seen).is_some()
        );
    }

    #[test]
    fn accepts_wrapped_acknowledgement_number() {
        let source = "192.168.2.1".parse().unwrap();
        let target = "172.16.100.2".parse().unwrap();
        let source_port = 50000;
        let target_port = 443;
        let expected = HashMap::from([((target, target_port), u32::MAX)]);
        let response = response_packet(
            target,
            source,
            target_port,
            source_port,
            0,
            TcpFlags::SYN | TcpFlags::ACK,
        );

        assert!(
            classify_packet(
                &response,
                &expected,
                source,
                source_port,
                false,
                &mut HashSet::new()
            )
            .is_some()
        );
    }

    #[test]
    fn validates_scan_limits_and_configuration() {
        let timeout = Duration::from_secs(30);

        assert_eq!(
            validate_probe_count(254, 65_535, 15, timeout).unwrap(),
            16_645_890
        );
        assert_eq!(
            validate_probe_count(256, 65_535, 15, timeout).unwrap(),
            16_776_960
        );
        assert_eq!(
            validate_probe_count(MAX_PROBES, 1, 15, timeout).unwrap(),
            MAX_PROBES
        );
        assert_eq!(validate_probe_count(1, 1, 15, MAX_TIMEOUT).unwrap(), 1);
        assert!(matches!(
            validate_probe_count(257, 65_535, 15, timeout),
            Err(ScanError::TooManyProbes { probe_count: 16_842_495, max }) if max == MAX_PROBES
        ));
        assert!(matches!(
            validate_probe_count(MAX_PROBES + 1, 1, 15, timeout),
            Err(ScanError::TooManyProbes { max, .. }) if max == MAX_PROBES
        ));
        assert!(matches!(
            validate_probe_count(usize::MAX, 2, 15, timeout),
            Err(ScanError::ScanSizeOverflow)
        ));
        assert!(matches!(
            validate_probe_count(0, 1, 15, timeout),
            Err(ScanError::EmptyScan)
        ));
        assert!(matches!(
            validate_probe_count(1, 0, 15, timeout),
            Err(ScanError::EmptyScan)
        ));
        assert!(matches!(
            validate_probe_count(1, 1, 0, timeout),
            Err(ScanError::ZeroBandwidth)
        ));
        assert!(matches!(
            validate_probe_count(1, 1, 15, MAX_TIMEOUT + Duration::from_secs(1)),
            Err(ScanError::TimeoutTooLarge { max, .. }) if max == MAX_TIMEOUT
        ));
    }

    #[test]
    fn timeout_too_large_reports_both_durations() {
        let timeout = MAX_TIMEOUT + Duration::from_secs(1);

        let error = validate_probe_count(1, 1, 15, timeout).unwrap_err();

        assert_eq!(
            error.to_string(),
            format!("timeout of {timeout:?} exceeds the maximum of {MAX_TIMEOUT:?}")
        );
    }

    #[test]
    fn rejects_duplicate_scan_config_entries() {
        let host = "192.168.2.1".parse().unwrap();
        let duplicate_targets = ScanConfig::new(vec![host, host], vec![443], host);
        let duplicate_ports = ScanConfig::new(vec![host], vec![443, 443], host);
        let unique = ScanConfig::new(vec![host], vec![80, 443], host);

        assert!(
            expected_responses(&duplicate_targets, 1, 2)
                .unwrap_err()
                .to_string()
                .contains("must be unique")
        );
        assert!(
            expected_responses(&duplicate_ports, 1, 2)
                .unwrap_err()
                .to_string()
                .contains("must be unique")
        );
        assert_eq!(expected_responses(&unique, 1, 2).unwrap().len(), 2);
    }

    #[test]
    fn parses_tcp_services_and_ignores_other_entries() -> anyhow::Result<()> {
        let ports = services_from(
            "\
# comment
ssh             22/tcp
domain          53/udp
http            80/tcp  www # inline comment
http-alt        80/tcp
malformed
invalid         nope/tcp
zero            0/tcp
",
        )?;

        assert_eq!(ports, [22, 80]);
        Ok(())
    }

    #[test]
    fn rejects_services_file_without_tcp_ports() {
        let path = services_path();
        fs::write(&path, "domain 53/udp\n# comment\nmalformed\n").unwrap();
        let error = ports_from_services(&path).unwrap_err();
        fs::remove_file(&path).unwrap();

        assert!(matches!(error, PortsError::NoTcpServices { path: p } if p == path));
    }

    #[test]
    fn reports_missing_services_file_path() {
        let path = services_path();
        let error = ports_from_services(&path).unwrap_err();

        assert!(matches!(
            &error,
            PortsError::ServicesFileRead { path: p, .. } if p == &path
        ));
        assert!(format!("{error:#}").contains(&path.display().to_string()));
    }

    #[test]
    fn incomplete_scan_error_preserves_context() {
        let host = "172.16.100.2".parse().unwrap();
        let partial_result = ScanResult {
            host,
            port: 443,
            state: PortState::Open,
        };
        let incomplete = IncompleteScanError {
            source: SendError::Io {
                host,
                port: 443,
                source: io::Error::other("send failed"),
            },
            partial_results: vec![partial_result],
            probes_sent: 7,
            total_probes: 10,
        };

        assert_eq!(incomplete.partial_results(), [partial_result]);
        assert_eq!(incomplete.probes_sent(), 7);
        assert_eq!(incomplete.total_probes(), 10);
        assert_eq!(
            incomplete.to_string(),
            "scan stopped after sending 7 of 10 probes"
        );
        assert_eq!(
            Error::source(&incomplete).unwrap().to_string(),
            "failed to send SYN to 172.16.100.2:443"
        );

        let error: anyhow::Error = incomplete.into();
        assert!(error.downcast_ref::<IncompleteScanError>().is_some());
        assert_eq!(
            format!("{error:#}"),
            "scan stopped after sending 7 of 10 probes: \
             failed to send SYN to 172.16.100.2:443: send failed"
        );
    }

    #[test]
    fn describes_str_panic_payload() {
        let payload: Box<dyn Any + Send> = Box::new("boom");
        assert_eq!(describe_panic_payload(&*payload), "boom");
    }

    #[test]
    fn describes_string_panic_payload() {
        let payload: Box<dyn Any + Send> = Box::new(String::from("boom"));
        assert_eq!(describe_panic_payload(&*payload), "boom");
    }

    #[test]
    fn describes_unrecognized_panic_payload() {
        let payload: Box<dyn Any + Send> = Box::new(42_i32);
        assert_eq!(describe_panic_payload(&*payload), "unknown panic payload");
    }

    #[test]
    fn estimates_scan_progress() {
        let progress = ScanProgress {
            probes_sent: 25,
            total_probes: 100,
            elapsed: Duration::from_secs(60),
        };

        assert_eq!(progress.percent(), 25);
        assert_eq!(
            progress.estimated_remaining(),
            Some(Duration::from_secs(180))
        );
    }

    #[test]
    fn handles_scan_progress_boundaries() {
        let no_probes = ScanProgress {
            probes_sent: 0,
            total_probes: 0,
            elapsed: Duration::from_secs(60),
        };
        let not_started = ScanProgress {
            total_probes: 100,
            ..no_probes
        };
        let complete = ScanProgress {
            probes_sent: 100,
            ..not_started
        };
        let over_complete = ScanProgress {
            probes_sent: 101,
            ..complete
        };

        assert_eq!(no_probes.percent(), 0);
        assert_eq!(no_probes.estimated_remaining(), None);
        assert_eq!(not_started.percent(), 0);
        assert_eq!(not_started.estimated_remaining(), None);
        assert_eq!(complete.percent(), 100);
        assert_eq!(complete.estimated_remaining(), Some(Duration::ZERO));
        assert_eq!(over_complete.estimated_remaining(), Some(Duration::ZERO));
    }

    #[test]
    fn probe_limit_accommodates_single_port_slash_8() {
        assert_eq!(MAX_PROBES, 16_777_214);
    }

    #[test]
    fn progress_schedule_uses_increasing_intervals() {
        assert_eq!(next_progress_deadline(Duration::from_mins(9)), TEN_MINUTES);
        assert_eq!(next_progress_deadline(TEN_MINUTES), Duration::from_mins(20));
        assert_eq!(next_progress_deadline(Duration::from_mins(50)), ONE_HOUR);
        assert_eq!(next_progress_deadline(ONE_HOUR), Duration::from_mins(90));
    }

    #[test]
    fn progress_schedule_skips_missed_deadlines() {
        assert_eq!(
            advance_progress_deadline(ONE_MINUTE, Duration::from_mins(35)),
            Duration::from_mins(40)
        );
    }
}