zond-engine 0.16.0

The core networking library powering Zond. Raw packet injection, protocol parsing, service and OS fingerprinting, and asynchronous network scanning.
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
// Copyright (c) 2026 Erik Lening (hollowpointer) and Contributors
//
// This file is part of Zond Engine, licensed under the GNU Affero General
// Public License, version 3 or later. See the LICENSE file for details, or
// <https://www.gnu.org/licenses/agpl-3.0.html>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! # UDP Port Probing
//!
//! Implements the privileged UDP half of [`crate::scanner::scan`]. It probes
//! specific `(address, port)` pairs with raw UDP packets and classifies each
//! one by whether and how it responds.
//!
//! UDP scanning is harder than the SYN scan next door because UDP carries no
//! handshake to correlate against. A closed port answers with an ICMP Port
//! Unreachable, an open one answers with a UDP datagram *if* it understands
//! what was sent, and a filtered one says nothing at all - which is also what
//! an open port that ignored the probe does. So:
//!
//! - a direct UDP reply is [`PortState::Open`],
//! - an ICMP Port Unreachable is [`PortState::Closed`],
//! - silence until the deadline is [`PortState::OpenFiltered`], because it
//!   genuinely cannot distinguish the two.
//!
//! ## Tying a reply to its probe
//!
//! Every probe in a scan leaves from one fixed source port, chosen when the
//! scanner is built. That single port is what makes both answers correlatable:
//!
//! - A **direct reply** is addressed back to it, so the kernel's BPF filter
//!   admits this scan's replies and drops the rest of the host's UDP traffic
//!   ([`ProbeKind::UdpProbe`]).
//! - An **ICMP error** carries no ports of its own, but RFC 792 requires it to
//!   quote the datagram that caused it - IP header plus the first eight bytes,
//!   which is exactly a whole UDP header. That quotation names the probe: its
//!   source port proves the datagram was ours, and its destination address and
//!   port say *which* probe, so one error retires exactly one probe.
//!
//! Reading the quoted packet rather than the error's own source address also
//! keeps a router's error attributable: the ICMP comes from the router, but the
//! probe it refers to was aimed at the host behind it.

use std::net::IpAddr;
use std::time::{Duration, Instant};

use async_trait::async_trait;
use pnet_packet::ip::IpNextHeaderProtocols;
use pnet_packet::udp::UdpPacket;
use tokio::sync::mpsc;

use crate::config::{ProbeTuning, ServiceDetection};
use crate::journal::settle::Outcome;
use crate::logging::error;
use crate::model::capture::IpObservation;
use crate::model::host::{HostStatus, StatusProtocol, StatusReason};
use crate::model::port::discovery::{Discovery as PortDiscovery, ScanResponse};
use crate::model::port::{PortState, Protocol};
use crate::model::target::PlannedTarget;
use crate::protocols::sizes::UDP_HDR_LEN;
use crate::report::ScannerKind;
use crate::scanner::pacing::congestion::WindowLimits;
use crate::scanner::pacing::deadline::AdaptiveDeadlineConfig;
use crate::scanner::pacing::retry::{RetryPolicy, SilentHostPolicy};
use crate::scanner::pacing::timer::ScanBudget;
use crate::scanner::payload;
use crate::scanner::session::ScanContext;
use crate::scanner::strategy::{PortScanner, StrategyError};
use crate::system::interface::SourceResolver;
use crate::transport::capture::CapturedSegment;
use crate::transport::probe::{ProbeKind, ProbeTransport};

use super::super::raw::{EvasionParts, send_udp};
use super::{AuditLabels, CoreParts, ProbeTarget, RawPortScan, RawProbeScan};
use crate::scanner::strategy::icmp_error::{self, Unreachable};

/// How long this scan runs and how it adapts.
///
/// Not the profile the SYN scanners share
/// ([`DEADLINE_CONFIG`](super::super::raw::DEADLINE_CONFIG)), because the thing being
/// waited for is different in kind. A SYN probe is answered by the target's
/// TCP stack as fast as the link allows. A UDP probe's most informative answer
/// is an ICMP error, and hosts **rate-limit** those: Linux emits roughly one
/// destination-unreachable per second by default (`net.ipv4.icmp_ratelimit`),
/// and BSD does the same. Answers to a multi-port scan therefore arrive spread
/// over seconds no matter how fast the network is.
///
/// That reshapes every number here, but `silence_floor` most of all. The SYN
/// profile gives up after 150 ms of quiet, which is generous for a stack that
/// answers immediately and *meaningless* against a host allowed to speak once
/// per second: the scan would stop while its answers were still queued and
/// legally on their way, then report the ports it never heard about as
/// filtered. A floor above the rate-limit interval is what makes silence
/// evidence of anything at all.
const DEADLINE_CONFIG: AdaptiveDeadlineConfig = AdaptiveDeadlineConfig::new(
    // Hard ceiling: a UDP scan is inherently slow, but it still has to finish.
    ScanBudget::new(
        Duration::from_millis(2_000),
        Duration::from_millis(200),
        Duration::from_secs(45),
    ),
    // Minimum runtime, so a scan cannot conclude before the first rate-limited
    // answers have had time to arrive.
    ScanBudget::new(
        Duration::from_millis(500),
        Duration::from_millis(50),
        Duration::from_secs(10),
    ),
    // Silence floor: longer than one rate-limit interval, so quiet means
    // "nothing is coming" rather than "the host is not allowed to answer yet".
    Duration::from_millis(1_200),
    Duration::from_secs(5),
    4.0,
    20,
);

/// How a UDP probe is retransmitted.
///
/// Every number here is set against a rate limit rather than against a round
/// trip, which is what makes this profile different in kind from the SYN one
/// ([`RETRY_POLICY`](super::super::raw::RETRY_POLICY)). A closed UDP port answers with an
/// ICMP error, and hosts emit those at roughly one per second; a retry sooner
/// than that interval is guaranteed to be wasted, because the answer it is
/// chasing was never allowed to be sent.
///
/// Two attempts rather than three, and a gentler backoff than the SYN profile
/// uses, for the same reason. Backing off aggressively is how a scanner relieves
/// congestion it is causing, but a UDP scan is not waiting on congestion, it is
/// waiting on permission - so doubling buys little and costs a great deal of
/// tail latency on a scan that is already the slowest thing the engine does. A
/// probe therefore lives about 3.75 s against an unmeasured host and almost
/// exactly 3 s against a measured one, in exchange for every port getting a
/// second chance rather than a single one.
const RETRY_POLICY: RetryPolicy = RetryPolicy::new(
    2,
    Duration::from_millis(1_500),
    Duration::from_millis(1_200),
    Duration::from_secs(5),
    1.5,
    0.2,
    Some(SilentHostPolicy::new(32, 1)),
);

/// The most probes left outstanding at once, and equally the window this scan
/// is paced by, which for UDP is a window that does not move.
///
/// Two jobs: it bounds the memory a scan of a large address space can occupy,
/// and it keeps the send loop from emptying the dispatcher into the network as
/// fast as the socket accepts writes - a burst that outruns any rate-limited
/// host's ability to answer manufactures open-filtered verdicts.
///
/// Fixed, where the TCP scanner's equivalent adapts. A congestion window
/// needs evidence, and a UDP scan is given none: silence is its ordinary
/// outcome rather than a signal, and its replies carry nothing naming the
/// attempt they answer, so neither the growth nor the reduction side of the
/// controller has anything to read. See
/// [`congestion`](crate::scanner::pacing::congestion) for the argument, and
/// `UDP_PORT_RATE_PER_SEC` in the parent module for what paces this scan
/// instead.
///
/// The ceiling is global rather than per host because
/// [`Dispatcher`](crate::scanner::dispatcher::Dispatcher) already hands out
/// shuffled targets, so consecutive probes in a multi-host scan naturally land
/// on different hosts. A per-host cap on top of that would constrain something
/// the target stream has already spread out.
const MAX_IN_FLIGHT: u32 = 512;

/// Probes specific `(address, port)` pairs with raw UDP packets.
pub struct UdpPortScanner {
    /// Everything a raw port scan carries and does regardless of protocol: the
    /// transport, the ledger, the deadline, the pacing and the stop conditions.
    /// What stays in this file is what a *UDP* probe is and what its answers
    /// prove - which for UDP is mostly what an ICMP error proves, since an open
    /// port is under no obligation to say anything at all.
    core: RawProbeScan<()>,
    /// How far the second pass may go to name what answered. Held here because
    /// the raw scan that classifies a port opens no conversation with it; see
    /// [`detect_services`](PortScanner::detect_services).
    service_detection: ServiceDetection,
}

impl UdpPortScanner {
    /// Builds a scanner that selects each probe's source via `resolver`, sized
    /// for a scan covering `target_count` `(address, port)` pairs.
    ///
    /// The scan's fixed source port is drawn from the high ephemeral range,
    /// where it is unlikely to collide with a listening service on this host,
    /// and the transport's capture filter is built around it.
    pub fn new(
        resolver: SourceResolver,
        ctx: ScanContext,
        target_count: usize,
        tuning: ProbeTuning,
    ) -> Result<Self, StrategyError> {
        let src_port: u16 = tuning
            .evasion
            .source_port_or(rand::random_range(50_000..u16::MAX));
        let transport = ProbeTransport::open_with(
            ProbeKind::UdpProbe {
                reply_port: src_port,
            },
            tuning.evasion.effective_send_mode(tuning.send_mode),
        )?;

        Ok(Self {
            service_detection: tuning.service_detection,
            core: Self::core(resolver, ctx, transport, &tuning, src_port, target_count),
        })
    }

    /// Builds a scanner around an already-opened transport, so the caller
    /// decides how probes reach the wire and where replies come from.
    ///
    /// `src_port` must be the port the transport's capture filter was built
    /// around, since it is what both halves use to recognize this scan's own
    /// traffic - see the module documentation. Paired with a synthetic
    /// transport (`ProbeTransport::from_parts`, behind the `test-support`
    /// feature) this is the seam that lets classification be driven against a
    /// simulated network rather than a real one.
    pub fn with_transport(
        resolver: SourceResolver,
        ctx: ScanContext,
        transport: ProbeTransport,
        target_count: usize,
        src_port: u16,
    ) -> Self {
        let tuning = ProbeTuning::default();
        Self {
            service_detection: tuning.service_detection,
            core: Self::core(resolver, ctx, transport, &tuning, src_port, target_count),
        }
    }

    /// The core a UDP port scan runs on.
    ///
    /// The pace it must outlive is the send rate itself: a UDP scan has no
    /// evidence to run a congestion window on, so the rate is the pacing rather
    /// than a backstop and is the slowest thing about the scan.
    fn core(
        resolver: SourceResolver,
        ctx: ScanContext,
        transport: ProbeTransport,
        tuning: &ProbeTuning,
        src_port: u16,
        target_count: usize,
    ) -> RawProbeScan<()> {
        let rate = super::super::raw::rate_within(
            tuning.max_probe_rate,
            tuning.min_probe_rate,
            super::UDP_PORT_RATE_PER_SEC,
        );

        RawProbeScan::new(CoreParts {
            resolver,
            ctx,
            transport,
            tuning,
            src_port,
            target_count,
            retry: RETRY_POLICY.configured(tuning.retry),
            rate,
            deadline: DEADLINE_CONFIG,
            pace: Duration::from_secs(1) / rate.get(),
            window: WindowLimits::fixed(MAX_IN_FLIGHT),
            max_unresolved: MAX_IN_FLIGHT as usize,
        })
    }

    /// A reply that matches no outstanding probe is dropped: it is a duplicate
    /// of one already resolved, an answer to a probe already written off, or a
    /// packet that reached us despite not answering anything this scan sent.
    ///
    /// The round trip is whatever the ledger is willing to vouch for. A probe
    /// that was sent once is unambiguous; one that was retried is not, since
    /// the two datagrams are identical on the wire, and no sample is taken.
    fn resolve_probe(
        &mut self,
        target: ProbeTarget,
        state: PortState,
        sender: IpAddr,
        ttl: Option<u8>,
        now: Instant,
    ) {
        let Some(resolution) = self.core.ledger.resolve(&target, None, now) else {
            // A duplicate of one already resolved, an answer to a probe already
            // written off, or a packet that reached us despite answering nothing
            // this scan sent.
            self.core.audit.record_reply_without_rtt();
            return;
        };

        let rtt = resolution.rtt;
        self.core.record_answer(&resolution);
        self.record_port_answered_by(target.0, target.1, state, Some(sender), ttl, rtt);
        // The target spoke: the only outcome that settles positively.
        self.settle(Outcome::Answered {
            position: resolution.payload,
        });
    }
}

/// The port a direct UDP reply answers for, if the datagram is addressed to
/// this scan's source port.
///
/// ## The protocols this cannot reach
///
/// The port credited is the port the reply came *from*, which is what lets one
/// socket probe many ports on a host and still say which of them answered.
/// TFTP breaks that assumption by design: RFC 1350 has the server allocate a
/// fresh transfer identifier on receiving a request and send every packet after
/// that, the error included, from the new port. Port 69 only ever *receives*.
///
/// So a TFTP error is credited to a transient port nobody asked about, and 69
/// is reported `open|filtered` on a host that answered. Measured against
/// `tftpd-hpa`, which replied from 54154, 43519 and 34965 on three consecutive
/// probes; no request form draws a reply from 69 at all.
///
/// Identifying it wants a second way to correlate, not another signature: a
/// corpus rule for TFTP could never fire, because the reply it reads cannot
/// reach the matcher.
///
/// The capture filter already narrows the UDP half to `src_port`, but that is a
/// performance boundary rather than a guarantee: a transport can be built with
/// no filter at all (`ProbeTransport::from_parts`), and a filter that silently
/// stopped matching would otherwise turn into false `Open`s. The check is cheap
/// and it is the only thing making the reply *ours*.
fn answering_probe(bytes: &[u8], src_port: u16) -> Option<(u16, &[u8])> {
    let udp = UdpPacket::new(bytes)?;
    if udp.get_destination() != src_port {
        return None;
    }
    // The datagram's own payload, which is where the answer to "what is this
    // host" lives when the port's protocol can say. Sliced from `bytes` at the
    // fixed header length rather than taken from the parsed packet, whose
    // borrow ends with it, and rather than derived from the length field,
    // which a padded frame makes shorter than what was actually captured.
    Some((udp.get_source(), &bytes[UDP_HDR_LEN..]))
}

/// What a reply is a statement about: the port that was probed, or the address
/// as a whole.
///
/// The distinction is not cosmetic. A reply reporting on the port resolves the
/// probe that provoked it; one reporting on the host says nothing about any
/// particular port, and the probe is left outstanding to time out on its own
/// rather than being given a verdict its evidence does not support.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Verdict {
    /// The probed port is in this state. Covers both a direct UDP reply and
    /// every ICMP code that reports on a port rather than on the path.
    Port(PortState),
    /// The address itself could not be reached. The only evidence in the engine
    /// that produces [`HostStatus::Down`].
    Host,
}

/// What an ICMP error means for the UDP port it was drawn by.
///
/// A port unreachable is the one unambiguous "closed" a UDP scan ever gets:
/// the datagram reached a stack, which looked for a listener and found none.
/// That reading is specific to UDP - the identical message answering a TCP
/// probe means something else entirely, which is why this mapping lives beside
/// the scanner it belongs to rather than in [`icmp_error`].
fn verdict_of(reason: Unreachable) -> Verdict {
    match reason {
        Unreachable::Port => Verdict::Port(PortState::Closed),
        // A prohibition is the path refusing delivery. A protocol unreachable is
        // the host saying it has no UDP stack, which is a stranger thing and
        // still not a closed port: no listener was ever looked for. Both leave
        // the port unprobed in effect, which is what filtered says.
        Unreachable::Prohibited | Unreachable::Protocol => Verdict::Port(PortState::Filtered),
        Unreachable::Host => Verdict::Host,
    }
}

/// The probe an ICMP error is about and what the error says about it.
///
/// `None` unless the quoted datagram is a UDP probe this scan sent, which its
/// source port is what proves. Its destination address and port name the probe
/// to retire, and both come from the quotation rather than from the error's own
/// header, so an error relayed by a router still points at the host the probe
/// was aimed at.
fn quoted_probe(error: &icmp_error::IcmpError<'_>, src_port: u16) -> Option<ProbeTarget> {
    if error.quoted.protocol != IpNextHeaderProtocols::Udp {
        return None;
    }

    let udp = UdpPacket::new(error.quoted.payload)?;
    if udp.get_source() != src_port {
        return None;
    }

    Some((error.quoted.destination, udp.get_destination()))
}

impl RawPortScan for UdpPortScanner {
    type Token = ();

    fn core(&self) -> &RawProbeScan<()> {
        &self.core
    }

    fn core_mut(&mut self) -> &mut RawProbeScan<()> {
        &mut self.core
    }

    fn protocol(&self) -> Protocol {
        Protocol::Udp
    }

    /// Always open-filtered. UDP carries no handshake, so an open port that did
    /// not recognise the payload says exactly as little as a firewall does, and
    /// no amount of waiting separates the two.
    fn silence_means(&self) -> PortState {
        PortState::OpenFiltered
    }

    fn audit_labels(&self) -> AuditLabels {
        AuditLabels {
            tag: "udp-port",
            silence: "open-filtered",
        }
    }

    /// Sends one datagram at `(ip, port)` and records the attempt.
    ///
    /// Used for the first attempt and every retry alike, and the retry is
    /// byte-for-byte the probe that preceded it: the payload is what makes an
    /// open port answer at all, and the source port is the scan's identity on
    /// the wire, so neither may vary between attempts.
    /// Classifies one captured reply and, if it answers an outstanding probe,
    /// resolves that probe.
    fn handle_reply(&mut self, reply: &CapturedSegment, now: Instant) {
        let classified = match reply.protocol {
            IpNextHeaderProtocols::Udp => {
                answering_probe(&reply.bytes, self.core.src_port).map(|(port, datagram)| {
                    // What the reply proves about the *host*, which is a
                    // separate claim from the port verdict below and survives
                    // the probe being resolved twice: a duplicate answer is
                    // still a name server answering.
                    if let Some(role) = payload::declared_role(port, datagram) {
                        self.core.ctx.update_host(reply.source, |host| {
                            host.add_network_role(role);
                        });
                    }
                    ((reply.source, port), Verdict::Port(PortState::Open))
                })
            }
            _ => icmp_error::parse(reply).and_then(|error| {
                let target = quoted_probe(&error, self.core.src_port)?;
                Some((target, verdict_of(error.reason)))
            }),
        };

        match classified {
            Some((target, Verdict::Port(state))) => {
                self.resolve_probe(
                    target,
                    state,
                    reply.source,
                    // The header this reply arrived under, read here because
                    // there is no second chance to read it: whether the datagram
                    // came from the target or from something refusing on its
                    // behalf, the hop counter is the cheapest evidence of which.
                    reply.observation.map(IpObservation::remaining_hops),
                    now,
                )
            }
            // Named a host but resolved no probe. Counted as seen rather than
            // off-target: it came from an address this scan asked about.
            // The probe is left outstanding. This message reports
            // that the address could not be reached at all, so it carries no
            // verdict on the port it happened to quote, and the probe should
            // retire on its own schedule like any other unanswered one.
            Some((target, Verdict::Host)) => {
                // No token: a UDP quotation carries the eight bytes RFC 792
                // guarantees and a UDP header has no nonce field in them, so the
                // probe's identity is the key, and it has to name a live one.
                self.core.record_host_down(&target, None, reply.source);
            }
            None => {}
        }
    }

    /// Retires one outstanding probe with the state its reply established,
    /// crediting the round trip to the deadline.
    ///
    /// Files a port verdict and whatever the reply that produced it proves about
    /// the host.
    ///
    /// `sender` is the address the reply actually came from, or `None` when the
    /// verdict came from a spent attempt budget rather than from a packet.
    /// Everything here turns on comparing it against `ip`, because an ICMP error
    /// names two addresses - the hop that generated it, and the destination of
    /// the datagram it quotes - and they are different claims:
    ///
    /// - **The target answered.** Any reply the host sent proves it is up, and
    ///   that includes ones negative about the port: a port unreachable is
    ///   emitted by the host's own IP stack, and an administrative rejection
    ///   from the host itself is a host that exists and is policing its traffic.
    /// - **A middlebox rejected the probe by policy.** Something is enforcing a
    ///   perimeter around this address, which is [`HostStatus::Filtered`] - the
    ///   variant's documented meaning, and materially different from an address
    ///   nothing answers for.
    /// - **A middlebox reported the port closed.** The port verdict stands,
    ///   since the message reports on the port, but no host status is recorded:
    ///   the address that answered is not the address that was asked, and a NAT
    ///   answering on another host's behalf must not be read as that host being
    ///   up.
    /// - **Nothing answered.** `OpenFiltered` from exhaustion records nothing.
    ///   Silence is not evidence about a host.
    fn record_port(&mut self, ip: IpAddr, port_num: u16, state: PortState, sender: Option<IpAddr>) {
        // Nothing answered, so there is no header to read and no round trip to
        // credit. The fuller form below is for the paths that had a reply.
        self.record_port_answered_by(ip, port_num, state, sender, None, None);
    }

    /// One send, first attempt or retry. `position` is `Some` only for a probe
    /// that has never gone out, since the ledger keeps it thereafter.
    fn send(&mut self, ip: IpAddr, port: u16, position: Option<u64>, now: Instant) {
        let Some(src_addr) = self.core.resolver.resolve(ip) else {
            error!(
                verbosity = 2,
                "no route to {ip}; skipping UDP probe to {ip}:{port}"
            );
            return;
        };

        // Whether this send takes a slot in the congestion window. A retry does
        // not: the slot went back when the question it repeats ran out of
        // round-trip budget. The ledger is what knows, since it is what holds
        // the probe between attempts.
        let first_attempt = !self.core.ledger.contains(&(ip, port));

        let sent = send_udp(
            self.core.transport.tx.as_ref(),
            self.core.src_port,
            src_addr,
            ip,
            self.core.resolver.zone_of(ip),
            port,
            EvasionParts {
                emission: self.core.emission,
                shaping: self.core.shaping,
                decoys: &self.core.decoys,
            },
            &mut self.core.send_failure,
        );
        self.core.record_send(ip, sent.is_some(), first_attempt);

        if sent.is_some() {
            match position {
                Some(position) => self.core.ledger.arm(ip, (ip, port), (), position, now),
                None => self.core.ledger.rearm(ip, (ip, port), (), now),
            }
        }
    }
}

impl UdpPortScanner {
    /// [`record_port`](RawPortScan::record_port), also carrying what the reply
    /// that produced the verdict was measured to be.
    ///
    /// Kept off the shared trait for the reason the TCP scanner keeps its own:
    /// the trait is the machinery both protocols share, and what a reply carried
    /// is read from a header only one of them was holding.
    fn record_port_answered_by(
        &mut self,
        ip: IpAddr,
        port_num: u16,
        state: PortState,
        sender: Option<IpAddr>,
        ttl: Option<u8>,
        rtt: Option<Duration>,
    ) {
        let port = crate::fingerprint::baseline_port(port_num, Protocol::Udp, state);

        // The packet that settled it, written down beside the host evidence
        // drawn from the same two facts. Without it a UDP port carried a verdict
        // and no account of it, which for this protocol is the worst case of
        // all: almost every silence here is `open|filtered`, and a reader has no
        // way to tell a refusal that arrived from one that never came.
        let port = match port_evidence(state, sender, ip) {
            Some(reason) => {
                let mut discovery = PortDiscovery::new(reason);
                if let Some(rtt) = rtt {
                    discovery = discovery.with_rtt(rtt);
                }
                if let Some(ttl) = ttl {
                    discovery = discovery.with_ttl(ttl);
                }
                port.with_discovery(discovery)
            }
            None => port,
        };
        let evidence = match (state, sender) {
            (PortState::Open, _) => Some((
                HostStatus::Up,
                StatusReason::new(StatusProtocol::Udp, "udp reply from a probed port"),
            )),
            (PortState::Closed, Some(sender)) if sender == ip => Some((
                HostStatus::Up,
                StatusReason::new(
                    StatusProtocol::IcmpUnreachable,
                    "port unreachable from the host",
                ),
            )),
            (PortState::Filtered, Some(sender)) if sender == ip => Some((
                HostStatus::Up,
                StatusReason::new(
                    StatusProtocol::IcmpUnreachable,
                    "administratively prohibited by the host",
                ),
            )),
            (PortState::Filtered, Some(sender)) => Some((
                HostStatus::Filtered,
                StatusReason::new(
                    StatusProtocol::IcmpUnreachable,
                    "administratively prohibited in path",
                )
                .from_source(sender),
            )),
            _ => None,
        };

        self.core.ctx.update_host(ip, |host| {
            host.add_port(port);
            if let Some((status, reason)) = evidence {
                host.record_evidence(status, reason);
            }
        });
    }
}

/// Which packet settled a UDP port, in the vocabulary a report records.
///
/// The port-level mirror of the host evidence recorded beside it, drawn from the
/// same two facts. A closed UDP port is only ever known by the unreachable that
/// says so, nothing else refuses a datagram, so the two verdicts a reply can
/// produce here are both ICMP, and which one turns on who sent it.
///
/// `None` where nothing arrived. `OpenFiltered` from exhaustion is the ordinary
/// outcome of a UDP scan and has no packet to name: recording `no reply` for it
/// would dress the protocol's normal silence as a finding.
fn port_evidence(state: PortState, sender: Option<IpAddr>, target: IpAddr) -> Option<ScanResponse> {
    match (state, sender) {
        (PortState::Open, _) => Some(ScanResponse::UdpResponse),
        // A port unreachable is the refusal that means nothing is listening.
        (PortState::Closed, _) => Some(ScanResponse::IcmpUnreachable),
        // A prohibition from the host is its own policy; from anywhere else it
        // is somebody in the path refusing on its behalf.
        (PortState::Filtered, Some(from)) => Some(match from == target {
            true => ScanResponse::IcmpProhibited,
            false => ScanResponse::IcmpUnreachable,
        }),
        _ => None,
    }
}

#[async_trait]
impl PortScanner for UdpPortScanner {
    fn kind(&self) -> ScannerKind {
        ScannerKind::UdpPort
    }

    fn supported_protocols(&self) -> Vec<Protocol> {
        vec![Protocol::Udp]
    }

    /// Consumes `targets`, sending a UDP probe for each UDP target and classifying
    /// every reply (or ICMP error), until each probe has been resolved or the
    /// scan's deadline expires. Anything still outstanding when the loop ends is
    /// reported as OpenFiltered.
    ///
    /// New targets are admitted only while fewer than `MAX_IN_FLIGHT` probes
    /// are outstanding, and released no faster than
    /// `UDP_PORT_RATE_PER_SEC`. Both are fixed,
    /// unlike the TCP scanner's window, because a UDP scan is given no evidence
    /// it could adapt on: silence is its ordinary outcome and its replies name
    /// no attempt.
    async fn scan(&mut self, targets: mpsc::Receiver<PlannedTarget>) -> Result<(), StrategyError> {
        super::drive(self, targets).await;
        Ok(())
    }

    /// Identifies the UDP services this scanner found open.
    ///
    /// The second pass asks each port the question the corpus registers for it
    /// and reads the answer through
    /// [`from_datagram`](crate::fingerprint::reads_replies), which is a
    /// different exchange from the TCP one beside it: a datagram out and a
    /// datagram back, with no connection between them.
    ///
    /// Scoped to [`Protocol::Udp`] so the SYN scanner sharing a composite with
    /// this one keeps the TCP half. Both members run the phase over what each
    /// discovered, which is what stops an open TCP port being fingerprinted once
    /// per member.
    ///
    /// Not the trait's no-op: identifying a UDP service needs a UDP
    /// conversation, and the engine has one.
    async fn detect_services(&mut self, ctx: &ScanContext) {
        crate::scanner::service::detect(ctx, self.service_detection, Protocol::Udp).await;
    }
}

// ╔════════════════════════════════════════════╗
// ║ ████████╗███████╗███████╗████████╗███████╗ ║
// ║ ╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝██╔════╝ ║
// ║    ██║   █████╗  ███████╗   ██║   ███████╗ ║
// ║    ██║   ██╔══╝  ╚════██║   ██║   ╚════██║ ║
// ║    ██║   ███████╗███████║   ██║   ███████║ ║
// ║    ╚═╝   ╚══════╝╚══════╝   ╚═╝   ╚══════╝ ║
// ╚════════════════════════════════════════════╝

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::host::NetworkRole;
    use crate::model::target::Target;
    use std::net::{Ipv4Addr, Ipv6Addr};

    use pnet_packet::icmp::destination_unreachable::{
        DestinationUnreachablePacket, IcmpCodes, MutableDestinationUnreachablePacket,
    };
    use pnet_packet::icmp::{IcmpCode, IcmpTypes};
    use pnet_packet::icmpv6::{Icmpv6Code, Icmpv6Packet, Icmpv6Types, MutableIcmpv6Packet};

    use crate::scanner::strategy::icmp_error::{
        ICMPV6_ADMIN_PROHIBITED, ICMPV6_INGRESS_EGRESS_POLICY, ICMPV6_NO_ROUTE,
        ICMPV6_PORT_UNREACHABLE, ICMPV6_REJECT_ROUTE, ICMPV6_UNUSED_LEN,
    };

    use crate::protocols::{ip, udp};
    use crate::scanner::session::ScanSession;
    use crate::transport::probe::{MockSender, ProbeTransport};

    const TARGET: IpAddr = IpAddr::V4(Ipv4Addr::new(192, 0, 2, 200));
    const TARGET_V6: IpAddr = IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 200));
    /// A router between us and the target, which reports errors under its own
    /// address rather than the target's.
    const ROUTER: IpAddr = IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1));
    /// This host's addresses, as the scanner's source resolver reports them.
    const LOCAL_V4: IpAddr = IpAddr::V4(Ipv4Addr::new(192, 0, 2, 50));
    const LOCAL_V6: IpAddr = IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 50));
    /// The fixed source port the scanner under test probes from.
    const SCAN_SRC_PORT: u16 = 54_321;

    fn on_link_interface() -> crate::system::interface::Link {
        use crate::system::interface::{Link, LinkAddress};
        Link::new("test0", 0).with_addresses(vec![
            LinkAddress::new(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 50)), 24),
            LinkAddress::new(
                IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 50)),
                64,
            ),
        ])
    }

    /// [`scanner_with_mock`] plus the probe log, for the tests that assert on
    /// what actually reached the wire rather than only on what was recorded.
    fn scanner_with_recorder() -> (UdpPortScanner, ScanSession, SentProbes) {
        let (session, ctx) = ScanSession::new();
        let (_reply_tx, reply_rx) = tokio::sync::mpsc::channel(1024);
        let sender = MockSender::default();
        let sent = sender.sent.clone();
        let transport = ProbeTransport::from_parts(Box::new(sender), reply_rx);
        let resolver = SourceResolver::from_links(&[on_link_interface()]);

        let scanner = UdpPortScanner::with_transport(resolver, ctx, transport, 8, SCAN_SRC_PORT);
        (scanner, session, sent)
    }

    /// The probes a [`MockSender`] recorded, shared with the scanner under test.
    type SentProbes = std::sync::Arc<std::sync::Mutex<Vec<crate::transport::probe::SentProbe>>>;

    fn scanner_with_mock() -> (UdpPortScanner, ScanSession) {
        let (session, ctx) = ScanSession::new();
        let (_reply_tx, reply_rx) = tokio::sync::mpsc::channel(1024);
        let transport = ProbeTransport::from_parts(Box::new(MockSender::default()), reply_rx);
        let resolver = SourceResolver::from_links(&[on_link_interface()]);

        let scanner = UdpPortScanner::with_transport(resolver, ctx, transport, 8, SCAN_SRC_PORT);
        (scanner, session)
    }

    fn probe(scanner: &mut UdpPortScanner, ip: IpAddr, port: u16) {
        scanner.send_probe(PlannedTarget::new(
            u64::from(port),
            Target {
                ip,
                port,
                protocol: Protocol::Udp,
            },
        ));
    }

    fn host_status(session: &ScanSession, ip: IpAddr) -> Option<HostStatus> {
        session.hosts().get(ip).map(|host| host.status())
    }

    fn port_state(session: &ScanSession, ip: IpAddr, port: u16) -> Option<PortState> {
        session
            .hosts()
            .get(ip)
            .and_then(|h| h.ports().find(|p| p.number() == port).map(|p| p.state()))
    }

    /// A UDP port that answered records what answered it, and the hop counter
    /// the reply arrived under.
    ///
    /// This protocol needs the account more than TCP does: almost every silence
    /// here is `open|filtered`, so a reader with only the verdict cannot tell a
    /// refusal that arrived from one that never came.
    #[test]
    fn an_answered_udp_port_records_the_datagram_that_settled_it() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);

        let mut reply = udp_reply(53, SCAN_SRC_PORT);
        reply.observation = Some(IpObservation::V4(crate::model::capture::Ipv4Observation {
            ttl: 58,
            identification: 0,
            dont_fragment: true,
            more_fragments: false,
            dscp: 0,
            ecn: 0,
        }));
        scanner.handle_reply(&reply, Instant::now());

        let discovery = session
            .hosts()
            .get(TARGET)
            .and_then(|host| {
                host.ports()
                    .find(|port| port.number() == 53)
                    .and_then(|port| port.discovery().cloned())
            })
            .expect("the port carries its evidence");

        assert_eq!(discovery.reason(), &ScanResponse::UdpResponse);
        assert_eq!(discovery.ttl(), Some(58));
    }

    /// The protocol's ordinary outcome is silence, and silence gets no packet.
    ///
    /// `OpenFiltered` from exhaustion is what most of a UDP scan comes back as.
    /// Recording `no reply` against every one of them would dress the normal
    /// case as a finding.
    #[test]
    fn an_unanswered_udp_port_records_no_evidence() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);

        scanner.record_port(TARGET, 53, PortState::OpenFiltered, None);

        assert_eq!(
            port_state(&session, TARGET, 53),
            Some(PortState::OpenFiltered)
        );
        assert!(
            session
                .hosts()
                .get(TARGET)
                .and_then(|host| host
                    .ports()
                    .find(|port| port.number() == 53)
                    .and_then(|port| port.discovery().cloned()))
                .is_none(),
            "a silence was dressed up as a packet"
        );
    }

    /// A reply as the capture layer would deliver it: bytes plus the protocol
    /// the IP header said they are.
    fn captured(
        source: IpAddr,
        protocol: pnet_packet::ip::IpNextHeaderProtocol,
        bytes: Vec<u8>,
    ) -> CapturedSegment {
        CapturedSegment::synthetic(source, protocol, bytes)
    }

    /// A direct UDP reply from `src_port`, addressed back to `dst_port`.
    fn udp_reply(src_port: u16, dst_port: u16) -> CapturedSegment {
        udp_reply_saying(src_port, dst_port, vec![])
    }

    /// A reply carrying something, for the ports whose answers say more than
    /// "somebody is listening".
    fn udp_reply_saying(src_port: u16, dst_port: u16, said: Vec<u8>) -> CapturedSegment {
        captured(
            TARGET,
            IpNextHeaderProtocols::Udp,
            udp::build_packet(TARGET, LOCAL_V4, src_port, dst_port, said).unwrap(),
        )
    }

    /// The quoted datagram an ICMP error carries: the IP header of the probe
    /// plus its UDP header. Built with the very functions that build a real
    /// probe, so the test agrees with the wire by construction rather than by
    /// a hand-written byte array.
    fn quoted_probe_packet(from: IpAddr, to: IpAddr, src_port: u16, dst_port: u16) -> Vec<u8> {
        let datagram = udp::build_packet(from, to, src_port, dst_port, vec![]).unwrap();
        let len = datagram.len() as u16;
        let header = match (from, to) {
            (IpAddr::V4(s), IpAddr::V4(d)) => {
                ip::build_ipv4_header(s, d, len, IpNextHeaderProtocols::Udp, ip::HOP_LIMIT_ROUTED)
                    .unwrap()
            }
            (IpAddr::V6(s), IpAddr::V6(d)) => {
                ip::build_ipv6_header(s, d, len, IpNextHeaderProtocols::Udp, ip::HOP_LIMIT_ROUTED)
            }
            _ => panic!("IP version mismatch in test fixture"),
        };
        header.into_iter().chain(datagram).collect()
    }

    /// An ICMPv4 error of `code` from `from`, quoting a probe sent to
    /// `to:dst_port` from `src_port`.
    fn icmpv4_error(
        from: IpAddr,
        code: IcmpCode,
        to: IpAddr,
        src_port: u16,
        dst_port: u16,
    ) -> CapturedSegment {
        let quoted = quoted_probe_packet(LOCAL_V4, to, src_port, dst_port);
        let mut buf = vec![0u8; DestinationUnreachablePacket::minimum_packet_size() + quoted.len()];
        let mut packet = MutableDestinationUnreachablePacket::new(&mut buf).unwrap();
        packet.set_icmp_type(IcmpTypes::DestinationUnreachable);
        packet.set_icmp_code(code);
        packet.set_payload(&quoted);
        captured(from, IpNextHeaderProtocols::Icmp, buf)
    }

    /// An ICMPv6 error of `code`, quoting a probe sent to `to:dst_port`.
    fn icmpv6_error(code: Icmpv6Code, to: IpAddr, src_port: u16, dst_port: u16) -> CapturedSegment {
        let quoted = quoted_probe_packet(LOCAL_V6, to, src_port, dst_port);
        let mut buf =
            vec![0u8; Icmpv6Packet::minimum_packet_size() + ICMPV6_UNUSED_LEN + quoted.len()];
        let mut packet = MutableIcmpv6Packet::new(&mut buf).unwrap();
        packet.set_icmpv6_type(Icmpv6Types::DestinationUnreachable);
        packet.set_icmpv6_code(code);
        // Four unused bytes precede the quotation (RFC 4443 §3.1).
        let mut payload = vec![0u8; ICMPV6_UNUSED_LEN];
        payload.extend_from_slice(&quoted);
        packet.set_payload(&payload);
        captured(to, IpNextHeaderProtocols::Icmpv6, buf)
    }

    /// A port verdict and a claim about the host are two findings, and the
    /// second is read out of the datagram the first was inferred from. The
    /// payload has to be sliced off at exactly the UDP header: one byte either
    /// way and the message no longer parses, which reads as an ordinary open
    /// port and silently loses the role on every name server a scan finds.
    #[test]
    fn a_dns_answer_names_the_host_a_name_server() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);

        // The engine's own question with the QR bit set, which is what a name
        // server sends back.
        let mut answer = crate::scanner::payload::for_port(53).to_vec();
        answer[2] |= 0b1000_0000;

        scanner.handle_reply(&udp_reply_saying(53, SCAN_SRC_PORT, answer), Instant::now());

        assert_eq!(port_state(&session, TARGET, 53), Some(PortState::Open));
        let host = session.hosts().get(TARGET).expect("the host answered");
        assert!(
            host.network_roles().contains(&NetworkRole::DnsServer),
            "the reply parsed as DNS, which a bound socket cannot fake"
        );
    }

    /// The same port answering with something that is not DNS is an open port
    /// and nothing more: a socket bound to 53 is not a name server.
    #[test]
    fn an_open_port_53_that_does_not_speak_dns_is_only_an_open_port() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);

        scanner.handle_reply(
            &udp_reply_saying(53, SCAN_SRC_PORT, b"hello".to_vec()),
            Instant::now(),
        );

        assert_eq!(port_state(&session, TARGET, 53), Some(PortState::Open));
        let host = session.hosts().get(TARGET).expect("the host answered");
        assert!(host.network_roles().is_empty());
    }

    #[test]
    fn direct_udp_reply_is_open() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);

        scanner.handle_reply(&udp_reply(53, SCAN_SRC_PORT), Instant::now());

        assert_eq!(port_state(&session, TARGET, 53), Some(PortState::Open));
        assert!(scanner.core.ledger.is_empty());
    }

    /// A datagram from a pending port that is *not* addressed to this scan's
    /// source port answers some other conversation on the host, not our probe.
    #[test]
    fn udp_traffic_not_addressed_to_the_scan_is_ignored() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);

        scanner.handle_reply(
            &udp_reply(53, SCAN_SRC_PORT.wrapping_add(1)),
            Instant::now(),
        );

        assert_eq!(port_state(&session, TARGET, 53), None);
        assert_eq!(scanner.core.ledger.len(), 1);
    }

    #[test]
    fn icmp_port_unreachable_is_closed() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);

        scanner.handle_reply(
            &icmpv4_error(
                TARGET,
                IcmpCodes::DestinationPortUnreachable,
                TARGET,
                SCAN_SRC_PORT,
                53,
            ),
            Instant::now(),
        );

        assert_eq!(port_state(&session, TARGET, 53), Some(PortState::Closed));
        assert!(scanner.core.ledger.is_empty());
    }

    /// An unreachable message names one port in its quoted datagram, and must
    /// retire that probe alone. Every other probe to the same host is
    /// still outstanding and must stay that way.
    #[test]
    fn icmp_unreachable_closes_only_the_port_it_quotes() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);
        probe(&mut scanner, TARGET, 161);
        probe(&mut scanner, TARGET, 123);

        scanner.handle_reply(
            &icmpv4_error(
                TARGET,
                IcmpCodes::DestinationPortUnreachable,
                TARGET,
                SCAN_SRC_PORT,
                161,
            ),
            Instant::now(),
        );

        assert_eq!(port_state(&session, TARGET, 161), Some(PortState::Closed));
        assert_eq!(port_state(&session, TARGET, 53), None);
        assert_eq!(port_state(&session, TARGET, 123), None);
        assert_eq!(scanner.core.ledger.len(), 2);
    }

    /// An error relayed by a router carries the router's address, but quotes a
    /// probe aimed at the host behind it. The quoted destination is what
    /// identifies the probe.
    #[test]
    fn unreachable_from_a_router_resolves_the_quoted_target() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);

        scanner.handle_reply(
            &icmpv4_error(
                ROUTER,
                IcmpCodes::DestinationPortUnreachable,
                TARGET,
                SCAN_SRC_PORT,
                53,
            ),
            Instant::now(),
        );

        assert_eq!(port_state(&session, TARGET, 53), Some(PortState::Closed));
        assert_eq!(port_state(&session, ROUTER, 53), None);
    }

    /// Host unreachable reports on the address, not on the port that happened to
    /// be quoted. Recording it as a port verdict would invent a fact about a
    /// port nothing ever answered for.
    #[test]
    fn host_unreachable_is_a_host_verdict_and_not_a_port_one() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);

        scanner.handle_reply(
            &icmpv4_error(
                ROUTER,
                IcmpCodes::DestinationHostUnreachable,
                TARGET,
                SCAN_SRC_PORT,
                53,
            ),
            Instant::now(),
        );

        assert_eq!(host_status(&session, TARGET), Some(HostStatus::Down));
        assert_eq!(
            port_state(&session, TARGET, 53),
            None,
            "the port has no verdict yet and the probe must be left to retire on its own"
        );
        assert_eq!(
            scanner.core.ledger.len(),
            1,
            "an unreachable address says nothing about the probe's fate"
        );
    }

    /// The distinction the whole host-status design turns on: an ICMP error
    /// names the hop that sent it as well as the address it is about, and only
    /// the first says whether the target is alive.
    #[test]
    fn a_port_unreachable_proves_the_host_only_when_the_host_sent_it() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);
        scanner.handle_reply(
            &icmpv4_error(
                TARGET,
                IcmpCodes::DestinationPortUnreachable,
                TARGET,
                SCAN_SRC_PORT,
                53,
            ),
            Instant::now(),
        );
        assert_eq!(host_status(&session, TARGET), Some(HostStatus::Up));

        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);
        scanner.handle_reply(
            &icmpv4_error(
                ROUTER,
                IcmpCodes::DestinationPortUnreachable,
                TARGET,
                SCAN_SRC_PORT,
                53,
            ),
            Instant::now(),
        );
        assert_eq!(
            host_status(&session, TARGET),
            Some(HostStatus::Unknown),
            "a middlebox answering for an address does not make that address alive"
        );
        assert_eq!(
            port_state(&session, TARGET, 53),
            Some(PortState::Closed),
            "the port verdict still stands: the message does report on the port"
        );
    }

    /// A policy rejection from a middlebox proves a perimeter, not a host - but
    /// a perimeter is still more than nothing, which is what separates
    /// `Filtered` from `Unknown`.
    #[test]
    fn an_in_path_policy_rejection_is_filtered_rather_than_up() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);

        scanner.handle_reply(
            &icmpv4_error(
                ROUTER,
                IcmpCodes::CommunicationAdministrativelyProhibited,
                TARGET,
                SCAN_SRC_PORT,
                53,
            ),
            Instant::now(),
        );

        assert_eq!(host_status(&session, TARGET), Some(HostStatus::Filtered));
    }

    /// Silence is the one thing that must never move a host's status, however
    /// many probes it swallows. `OpenFiltered` is a port verdict reached by
    /// exhaustion, and a host that has sent nothing has proved nothing.
    #[test]
    fn exhausting_every_attempt_leaves_the_host_unknown() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);

        let mut now = Instant::now();
        for _ in 0..8 {
            now += Duration::from_secs(10);
            scanner.service_retries(now);
        }

        assert_eq!(
            port_state(&session, TARGET, 53),
            Some(PortState::OpenFiltered)
        );
        assert_eq!(host_status(&session, TARGET), Some(HostStatus::Unknown));
        assert!(
            session
                .hosts()
                .get(TARGET)
                .expect("the port verdict created the host")
                .reasons()
                .is_empty(),
            "silence is not evidence and must leave no audit trail"
        );
    }

    /// An unreachable quoting a datagram this scan never sent - a different
    /// source port - belongs to someone else's traffic.
    #[test]
    fn unreachable_quoting_a_foreign_probe_is_ignored() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);

        scanner.handle_reply(
            &icmpv4_error(
                TARGET,
                IcmpCodes::DestinationPortUnreachable,
                TARGET,
                SCAN_SRC_PORT.wrapping_add(1),
                53,
            ),
            Instant::now(),
        );

        assert_eq!(port_state(&session, TARGET, 53), None);
        assert_eq!(scanner.core.ledger.len(), 1);
    }

    /// Only code 3 says a port answered. The codes that describe a blocked
    /// path prove the probe did not arrive, which is `Filtered` - a strictly
    /// better answer than letting the probe time out into `OpenFiltered`.
    #[test]
    fn administratively_prohibited_icmp_is_filtered() {
        for code in [
            IcmpCodes::DestinationProtocolUnreachable,
            IcmpCodes::NetworkAdministrativelyProhibited,
            IcmpCodes::HostAdministrativelyProhibited,
            IcmpCodes::CommunicationAdministrativelyProhibited,
        ] {
            let (mut scanner, session) = scanner_with_mock();
            probe(&mut scanner, TARGET, 53);

            scanner.handle_reply(
                &icmpv4_error(TARGET, code, TARGET, SCAN_SRC_PORT, 53),
                Instant::now(),
            );

            assert_eq!(
                port_state(&session, TARGET, 53),
                Some(PortState::Filtered),
                "ICMP code {code:?} should read as filtered"
            );
        }
    }

    /// A code that reports on neither the port nor the path leaves the probe
    /// outstanding, to time out into `OpenFiltered` like any other silence.
    #[test]
    fn uninformative_icmp_codes_leave_the_probe_outstanding() {
        for code in [
            IcmpCodes::DestinationNetworkUnknown,
            IcmpCodes::FragmentationRequiredAndDFFlagSet,
            IcmpCodes::SourceRouteFailed,
        ] {
            let (mut scanner, session) = scanner_with_mock();
            probe(&mut scanner, TARGET, 53);

            scanner.handle_reply(
                &icmpv4_error(TARGET, code, TARGET, SCAN_SRC_PORT, 53),
                Instant::now(),
            );

            assert_eq!(port_state(&session, TARGET, 53), None, "code {code:?}");
            assert_eq!(scanner.core.ledger.len(), 1, "code {code:?}");
        }
    }

    #[test]
    fn icmpv6_policy_refusals_are_filtered() {
        for code in [
            ICMPV6_ADMIN_PROHIBITED,
            ICMPV6_INGRESS_EGRESS_POLICY,
            ICMPV6_REJECT_ROUTE,
        ] {
            let (mut scanner, session) = scanner_with_mock();
            probe(&mut scanner, TARGET_V6, 53);

            scanner.handle_reply(
                &icmpv6_error(code, TARGET_V6, SCAN_SRC_PORT, 53),
                Instant::now(),
            );

            assert_eq!(
                port_state(&session, TARGET_V6, 53),
                Some(PortState::Filtered),
                "ICMPv6 code {code:?} should read as filtered"
            );
        }
    }

    /// An ICMP error's first two bytes (type 3, code 3) read as the source port
    /// 771 if the segment is parsed as UDP. Carrying the protocol from the IP
    /// header is what stops that from becoming a false `Open`.
    #[test]
    fn icmp_error_is_never_read_as_a_udp_reply() {
        let (mut scanner, session) = scanner_with_mock();
        // 0x0303: what an ICMPv4 unreachable's type/code look like as a port.
        probe(&mut scanner, TARGET, 771);
        probe(&mut scanner, TARGET, 53);

        scanner.handle_reply(
            &icmpv4_error(
                TARGET,
                IcmpCodes::DestinationPortUnreachable,
                TARGET,
                SCAN_SRC_PORT,
                53,
            ),
            Instant::now(),
        );

        assert_eq!(port_state(&session, TARGET, 771), None);
        assert_eq!(port_state(&session, TARGET, 53), Some(PortState::Closed));
    }

    #[test]
    fn icmpv6_port_unreachable_is_closed() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET_V6, 53);

        scanner.handle_reply(
            &icmpv6_error(ICMPV6_PORT_UNREACHABLE, TARGET_V6, SCAN_SRC_PORT, 53),
            Instant::now(),
        );

        assert_eq!(port_state(&session, TARGET_V6, 53), Some(PortState::Closed));
        assert!(scanner.core.ledger.is_empty());
    }

    /// ICMPv6 code 0 is "no route to destination" - a statement about the path.
    #[test]
    fn icmpv6_no_route_is_ignored() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET_V6, 53);

        scanner.handle_reply(
            &icmpv6_error(ICMPV6_NO_ROUTE, TARGET_V6, SCAN_SRC_PORT, 53),
            Instant::now(),
        );

        assert_eq!(port_state(&session, TARGET_V6, 53), None);
        assert_eq!(scanner.core.ledger.len(), 1);
    }

    /// A truncated or malformed reply must be dropped, never panic: every byte
    /// of it was chosen by a remote host.
    #[test]
    fn malformed_replies_are_dropped() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);

        for len in 0..48usize {
            let bytes = vec![0xFFu8; len];
            for protocol in [
                IpNextHeaderProtocols::Udp,
                IpNextHeaderProtocols::Icmp,
                IpNextHeaderProtocols::Icmpv6,
            ] {
                scanner.handle_reply(&captured(TARGET, protocol, bytes.clone()), Instant::now());
            }
        }

        assert_eq!(port_state(&session, TARGET, 53), None);
        assert_eq!(scanner.core.ledger.len(), 1);
    }

    /// An unanswered probe is sent again rather than written off, since silence
    /// on a first UDP probe is the least informative signal in the protocol.
    #[test]
    fn an_unanswered_probe_is_sent_again() {
        let (mut scanner, session, sent) = scanner_with_recorder();
        probe(&mut scanner, TARGET, 53);

        scanner.service_retries(Instant::now() + Duration::from_secs(2));

        assert_eq!(sent.lock().unwrap().len(), 2, "the probe was not retried");
        assert_eq!(port_state(&session, TARGET, 53), None, "no verdict yet");
    }

    /// A probe that has spent its budget is written off while the scan is still
    /// running, so results reach the caller as they are decided rather than all
    /// at once at the end.
    #[test]
    fn a_probe_that_spends_its_budget_is_written_off_during_the_scan() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);

        // Each retry reschedules from the moment it is sent, so the schedule
        // has to be walked rather than jumped over.
        let mut now = Instant::now();
        for _ in 0..RETRY_POLICY.max_attempts + 1 {
            now += RETRY_POLICY.worst_case_probe_lifetime();
            scanner.service_retries(now);
        }

        assert_eq!(
            port_state(&session, TARGET, 53),
            Some(PortState::OpenFiltered)
        );
        assert!(scanner.core.ledger.is_empty());
    }

    /// Running out of attempts is not activity: nothing answered, so the
    /// adaptive deadline must not be told the scan is making progress.
    #[test]
    fn running_out_of_attempts_does_not_extend_the_deadline() {
        let (mut scanner, _session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);

        // A deadline whose silence clock has been reset reports a full tick;
        // capture the value before and after to see whether it moved.
        let before = scanner.core.deadline.time_until_next_tick();
        let mut now = Instant::now();
        for _ in 0..RETRY_POLICY.max_attempts + 1 {
            now += RETRY_POLICY.worst_case_probe_lifetime();
            scanner.service_retries(now);
        }
        let after = scanner.core.deadline.time_until_next_tick();

        assert!(
            after <= before,
            "silence clock was reset by an expiry ({before:?} -> {after:?})"
        );
    }

    /// While probes are outstanding the loop must not sleep past the point
    /// where the next one falls due - nothing else will wake it, because
    /// silence is exactly the case being timed.
    #[test]
    fn pending_probes_shorten_the_sleep() {
        let (mut scanner, _session) = scanner_with_mock();
        let now = Instant::now();
        assert!(scanner.core.ledger.is_empty());
        let idle = scanner.core.tick_delay(now);

        probe(&mut scanner, TARGET, 53);
        let busy = scanner.core.tick_delay(now);

        assert!(busy < idle, "sleep not shortened while probes are out");
        assert!(
            busy <= RETRY_POLICY.worst_case_probe_lifetime(),
            "the loop would sleep past the probe's whole schedule"
        );
    }

    /// The UDP profile has to tolerate silence for longer than a host's ICMP
    /// rate-limit interval (~1/sec), or a scan concludes while its answers are
    /// still queued. This is the property that makes it a separate profile from
    /// the SYN one, so it is asserted rather than left to a comment.
    #[test]
    fn silence_floor_outlasts_the_icmp_rate_limit() {
        const ICMP_RATE_LIMIT_INTERVAL: Duration = Duration::from_secs(1);

        assert!(
            DEADLINE_CONFIG.silence_floor > ICMP_RATE_LIMIT_INTERVAL,
            "silence floor {:?} is shorter than one rate-limited answer",
            DEADLINE_CONFIG.silence_floor
        );
        assert!(
            super::super::super::raw::DEADLINE_CONFIG.silence_floor < ICMP_RATE_LIMIT_INTERVAL,
            "the SYN profile was expected to be the tighter one"
        );
    }

    #[test]
    fn unanswered_probes_resolve_as_filtered() {
        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 161);

        scanner.resolve_remaining();

        assert_eq!(
            port_state(&session, TARGET, 161),
            Some(PortState::OpenFiltered)
        );
        assert!(scanner.core.ledger.is_empty());
    }

    #[test]
    fn non_udp_targets_are_not_probed() {
        let (mut scanner, _session) = scanner_with_mock();
        scanner.send_probe(PlannedTarget::new(
            0,
            Target {
                ip: TARGET,
                port: 80,
                protocol: Protocol::Tcp,
            },
        ));
        assert!(scanner.core.ledger.is_empty());
    }

    /// Every probe in a scan must leave from the one port the capture filter
    /// and the quoted-datagram check are built around.
    #[test]
    fn every_probe_is_sent_from_the_scan_source_port() {
        let (_session, ctx) = ScanSession::new();
        let (_reply_tx, reply_rx) = tokio::sync::mpsc::channel(1024);
        let sender = MockSender::default();
        let sent = sender.sent.clone();
        let transport = ProbeTransport::from_parts(Box::new(sender), reply_rx);
        let mut scanner = UdpPortScanner::with_transport(
            SourceResolver::from_links(&[on_link_interface()]),
            ctx,
            transport,
            8,
            SCAN_SRC_PORT,
        );

        for port in [53, 161, 123] {
            probe(&mut scanner, TARGET, port);
        }

        let sent = sent.lock().unwrap();
        assert_eq!(sent.len(), 3);
        for (segment, _src, _dst) in sent.iter() {
            let udp = UdpPacket::new(segment).expect("probe is a UDP datagram");
            assert_eq!(udp.get_source(), SCAN_SRC_PORT);
        }
    }

    /// A UDP quotation carries no nonce — the eight bytes RFC 792 guarantees are
    /// the UDP header, which has no field for one — so the probe's identity is
    /// the whole of the evidence and it has to name a probe that exists.
    ///
    /// Two addresses the sender chose freely: one never probed, one probed on a
    /// different port. Neither is a host this scan may file down.
    #[test]
    fn a_host_unreachable_about_no_live_probe_records_nothing() {
        let never = IpAddr::V4(Ipv4Addr::new(203, 0, 113, 77));

        let (mut scanner, session) = scanner_with_mock();
        probe(&mut scanner, TARGET, 53);

        // An address this scan never addressed at all.
        scanner.handle_reply(
            &icmpv4_error(
                ROUTER,
                IcmpCodes::DestinationHostUnreachable,
                never,
                SCAN_SRC_PORT,
                53,
            ),
            Instant::now(),
        );
        assert_eq!(
            host_status(&session, never),
            None,
            "an address nothing was sent to is not a host this scan found"
        );

        // The right address, a port nobody asked about.
        scanner.handle_reply(
            &icmpv4_error(
                ROUTER,
                IcmpCodes::DestinationHostUnreachable,
                TARGET,
                SCAN_SRC_PORT,
                9999,
            ),
            Instant::now(),
        );
        assert_ne!(
            host_status(&session, TARGET),
            Some(HostStatus::Down),
            "port 9999 was never probed, so this quotes nothing this scan sent"
        );
    }
}