zond-engine 0.14.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
// 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

//! # Turning matched rules into an answer
//!
//! [`classify`] takes an observation, asks the rule database about it, and
//! returns what can honestly be said, which is sometimes nothing.
//!
//! ## One packet is one piece of evidence, not nine
//!
//! The hop counter, the window, the option layout and the window scale of a
//! single reply are not independent. They are consequences of one stack build,
//! chosen together by one set of authors, and a host that has one of them
//! usually has all of them. Scoring each as a separate vote triple-counts a
//! single observation and manufactures confidence out of nothing.
//!
//! So a whole observation is matched jointly against a rule and yields **one**
//! result. Independence is claimed only *between* genuinely different sources,
//! a stack, a service banner, a hardware address, a DHCP option, where it is
//! close enough to true to build on. That is phase 5's work and this module is
//! shaped to receive it: [`OsVerdict`] carries a source and a confidence rather
//! than a bare name.
//!
//! ## Why the ceiling is where it is
//!
//! [`MAX_STACK_ACCURACY`] caps what this evidence alone may claim, and it sits
//! below the 85 that
//! [`OsFingerprint::is_highly_confident`](crate::model::host::OsFingerprint::is_highly_confident)
//! reads. That threshold is what a caller uses to decide whether to stop and
//! accept an answer or send something more intrusive, so a number reachable
//! from one correlated packet would make it meaningless, every host with an
//! open port would look settled and no further probe would ever be justified.
//!
//! Reaching high confidence has to take corroboration from a second, genuinely
//! independent source. Today there is none, so nothing this module produces is
//! highly confident, and that is the honest state of the evidence rather than a
//! placeholder.
//!
//! ## Nothing is a valid answer
//!
//! Below [`MIN_REPORTABLE_ACCURACY`] this reports no operating system at all. A
//! wrong confident answer costs more than a missing one: a scan that says
//! nothing invites a second look, and one that says "Linux" about a Windows host
//! is believed. nmap's aggressive-guess mode is a wart, not a feature to copy.

use crate::model::host::OsFingerprint;
use crate::model::host::{OsEvidence, OsSource};

use super::db::RuleDb;
use super::observation::{StackObservation, StackReply};
use super::series::SeriesClasses;
use super::signature::{OsDefinition, Provenance};

/// The most a single reply's stack shape may claim on its own.
///
/// Below the 85 that marks high confidence, and for the reason given in the
/// module documentation: one packet's fields are one observation however many
/// of them agree.
pub const MAX_STACK_ACCURACY: u8 = 70;

/// The least a finding may score and still be reported.
///
/// Under this, [`classify`] yields nothing rather than the least bad guess.
pub const MIN_REPORTABLE_ACCURACY: u8 = 40;

/// What a rule measured by this engine is worth before its own weight applies.
///
/// A rule that matched said everything it tests is true of this host. What it
/// does *not* establish is that no other rule would have said the same, which
/// is why several matches do not raise the score; see [`classify`].
const MEASURED_ACCURACY: f32 = 65.0;

/// What a rule taken from published stack characteristics is worth.
///
/// Lower, and not because published defaults are unreliable, an initial hop
/// counter and an option order are ordinary engineering facts. Lower because
/// the rule has not been seen *through this engine's own probe on a real
/// network*, which is the gap that has already caught this project out once:
/// option negotiation is reciprocal, so a documented layout is documented
/// against some probe, and against a different one it is wrong while looking
/// right.
///
/// Above [`MIN_REPORTABLE_ACCURACY`], so such a rule reports rather than hides,
/// a plausible answer that says how sure it is beats no answer. Confirming one
/// on real hardware is what promotes it.
const PUBLISHED_ACCURACY: f32 = 50.0;

/// What the rules concluded about one observation.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct OsVerdict {
    /// The broad family, where anything could name one.
    ///
    /// `None` where every source abstained, a host identified down to its make
    /// and model by an agent that never said what it runs. See
    /// [`OsEvidence::family`](crate::model::host::OsEvidence::family).
    pub family: Option<String>,
    /// What kind of box this is, where a source said: `Printer`, `Switch`,
    /// `Router`. Orthogonal to the family, never a substitute for it.
    pub device: Option<String>,
    /// The vendor, where a rule named one.
    pub vendor: Option<String>,
    /// The product, where a rule named one.
    pub product: Option<String>,
    /// The version, where a rule named one.
    pub version: Option<String>,
    /// A Common Platform Enumeration identifier, where a rule named one.
    pub cpe: Option<String>,
    /// The kernel release, where a source read one.
    ///
    /// Beside [`version`](Self::version) rather than instead of it: a
    /// distribution release and the kernel it ships are two facts about one
    /// machine, and a source that knows one may know nothing of the other.
    pub kernel: Option<String>,
    /// The instruction set, where a source read one.
    ///
    /// A third axis beside what the machine runs and what it is. Nothing in a
    /// handshake carries it; it arrives from text, and an SNMP `sysDescr` on a
    /// Unix host is `uname -a`, which ends with the machine type.
    pub arch: Option<String>,
    /// How sure this is, on the `0..=100` scale
    /// [`OsFingerprint`] uses. Bounded by [`MAX_STACK_ACCURACY`].
    ///
    /// About the family, which is the part every source can speak to.
    pub accuracy: u8,
    /// How sure the parts *past* the family are, where this names any.
    ///
    /// Separate because they are usually attested differently: a stack reading
    /// and a banner may agree that a host is Linux while only the banner can say
    /// which release, and one figure for both would report the weaker claim at
    /// the stronger claim's strength. `None` where nothing finer than a family
    /// was named.
    pub detail_accuracy: Option<u8>,
    /// What produced it.
    pub source: OsSource,
    /// One line describing the observation this was read off, for a report to
    /// carry beside the conclusion. See
    /// [`StackObservation::summary`](super::StackObservation::summary).
    pub evidence: String,
}

impl OsVerdict {
    /// Presents this verdict as one item for [`resolve`](super::resolve) to fold
    /// against other sources.
    ///
    /// The accuracy becomes a probability, because that is the scale the
    /// combining arithmetic works on, a percentage summed against another
    /// percentage means nothing, where two probabilities for one hypothesis
    /// combine exactly.
    ///
    /// A whole reply is one item, never one per field. Its hop counter, window
    /// and option layout are consequences of one stack build and agree by
    /// construction; scoring them apart would count a single observation several
    /// times over and manufacture confidence out of nothing.
    pub fn as_evidence(&self) -> OsEvidence {
        OsEvidence {
            source: self.source,
            family: self.family.clone(),
            device: self.device.clone(),
            vendor: self.vendor.clone(),
            product: self.product.clone(),
            version: self.version.clone(),
            kernel: self.kernel.clone(),
            arch: self.arch.clone(),
            cpe: self.cpe.clone(),
            confidence: f32::from(self.accuracy) / 100.0,
            evidence: self.evidence.clone(),
        }
    }

    /// The label a reader sees: the most specific thing the rules supported.
    ///
    /// Infallible, because [`resolve`](super::resolve) declines rather than
    /// return a verdict that names nothing.
    pub fn label(&self) -> String {
        // A corpus that sets `product` to the family name is *declining* to name
        // a product, and for a Linux distribution it puts the distribution in
        // `vendor`. 993 shipped rules are written that way, which is why a host
        // running Debian 12 was reported as `Linux 12.0`: a version number no
        // Linux has ever had, attached to the wrong noun.
        //
        // A rule with **no** product at all is a different case and must not be
        // read the same way. There, `vendor` is often the maker of a device
        // rather than the publisher of an operating system, Ubiquiti, AXIS,
        // Crestron, and seventeen rules pair `Microsoft` with `Windows`, which
        // this would otherwise render as `Microsoft 10`. Those keep the family.
        match (&self.product, &self.vendor) {
            (Some(product), Some(vendor)) if Some(product.as_str()) == self.family.as_deref() => {
                vendor.clone()
            }
            // A device class means the product is a model number, and a model
            // number without its maker names nothing a reader can look up:
            // `NC-8700w` is a string, `Brother NC-8700w` is a printer.
            (Some(product), Some(vendor))
                if self.device.is_some() && !product.starts_with(vendor.as_str()) =>
            {
                format!("{vendor} {product}")
            }
            (Some(product), _) => product.clone(),
            (None, vendor) => self
                .family
                .clone()
                .or_else(|| vendor.clone())
                .unwrap_or_else(|| self.device.clone().unwrap_or_default()),
        }
    }

    /// Projects onto the model's [`OsFingerprint`].
    ///
    /// `OsFingerprint` ranks findings from different techniques by accuracy and
    /// fills gaps on a tie, so what is handed over here needs to be honest about
    /// how much it knows rather than as specific as possible.
    pub fn to_fingerprint(&self) -> OsFingerprint {
        let mut fingerprint =
            OsFingerprint::new(self.label(), self.accuracy).with_evidence(&*self.evidence);

        if let Some(family) = &self.family {
            fingerprint = fingerprint.with_family(&**family);
        }
        if let Some(device) = &self.device {
            fingerprint = fingerprint.with_device(&**device);
        }

        if let Some(vendor) = &self.vendor {
            fingerprint = fingerprint.with_vendor(&**vendor);
        }
        if let Some(version) = &self.version {
            fingerprint = fingerprint.with_generation(&**version);
        }
        if let Some(cpe) = &self.cpe {
            fingerprint.add_cpe(&**cpe);
        }
        if let Some(kernel) = &self.kernel {
            fingerprint = fingerprint.with_kernel(&**kernel);
        }
        if let Some(arch) = &self.arch {
            fingerprint = fingerprint.with_arch(&**arch);
        }
        if let Some(accuracy) = self.detail_accuracy {
            fingerprint = fingerprint.with_detail_accuracy(accuracy);
        }
        fingerprint
    }
}

/// Names the operating system behind `reply`, or nothing.
///
/// # How several matches are scored
///
/// Rules are not alternatives to be ranked; they are claims that overlap. Three
/// outcomes, and the middle one is the one worth explaining:
///
/// - **One rule matched.** It is the only thing that describes this host, and it
///   scores its base worth times its own weight.
/// - **Several matched and agree.** They corroborate on whichever of the two
///   identifying axes they both speak to, and disagree, or say nothing, below
///   it. The verdict keeps only the parts *all* of them agree on, which is
///   usually few. The score does not rise for the agreement: two rules written
///   from the same measurements are not two pieces of evidence.
/// - **Several matched and contradict each other.** Two rules naming different
///   families, or different device classes, cannot both be right and nothing
///   here can say which is. Reported as nothing: a tie broken by
///   weight would be reporting an authoring decision as a measurement.
///
/// A rule that names a family and one that names only a device class are not in
/// the third case. They answer different questions about one host and the
/// verdict carries both, which is what keeps a rule reading a hop counter from
/// arguing with a banner about what the box runs.
///
/// Returns `None` when no rule matched, when the matches contradict each other,
/// or when the result scores below [`MIN_REPORTABLE_ACCURACY`].
pub fn classify(db: &RuleDb, reply: &StackReply) -> Option<OsVerdict> {
    let matched: Vec<&OsDefinition> = db.matching(reply).collect();
    score(matched, reply.summary())
}

/// Names the operating system behind a host that was asked more than once.
///
/// The active counterpart of [`classify`]. Each reading is one reply paired
/// with what the series it belongs to turned out to be, and a host contributes
/// as many readings as it gave distinct kinds of answer, a SYN+ACK from an open
/// port, a reset from a closed one. Rules are gathered across all of them and
/// scored together.
///
/// # One host is one piece of evidence, however many packets it took
///
/// Every reading here came from one stack, so they are not independent and the
/// result is a single [`OsVerdict`] rather than one per reply. Returning several
/// would put them through [`resolve`](super::resolve)'s noisy-OR as though a
/// machine agreeing with itself were two sources agreeing with each other, which
/// is the same double-count [`classify`] avoids within a single reply.
///
/// # Why the readings are kept apart rather than pooled
///
/// A stack's resets and its handshake answers come from different code paths
/// that disagree about the same fields, measured, on one host: identifier zero
/// on the SYN+ACK path and a global counter on the reset path. So each reply
/// carries the series read from replies of *its own kind*, and a rule sees the
/// classes belonging to the segment it declares. Pooling them would compare a
/// host against itself under two policies at once.
///
/// # Every reading is recorded, including the ones nothing matched
///
/// The evidence line carries what each series turned out to be whether or not a
/// rule read it. That is deliberate and it is most of this function's value
/// today: the corpus holds no rule written for a reset, so a host's reset
/// series currently names nothing, and it is precisely the measurement somebody
/// needs in front of them to write the first one. A reading dropped for
/// matching nothing is a reading nobody can author from.
pub fn classify_series(db: &RuleDb, readings: &[(StackReply, SeriesClasses)]) -> Option<OsVerdict> {
    let matched: Vec<&OsDefinition> = readings
        .iter()
        .flat_map(|(reply, series)| db.matching_with_series(reply, series))
        .collect();

    let mut lines: Vec<String> = readings
        .iter()
        .map(|(reply, series)| format!("{} {}", reply.summary(), series.summary()))
        .collect();
    lines.sort_unstable();
    lines.dedup();

    score(matched, lines.join(" | "))
}

/// Two matched rules named one part differently.
///
/// Only ever produced for the two axes that *identify* a host, where dissent has
/// to stop the verdict. Below them a contradiction and an absence come to the
/// same thing, so [`consensus`]'s error is discarded there.
struct Contradiction;

/// The one value every rule that states `part` gives, or `None` where none
/// states it.
///
/// Silence abstains and dissent is fatal, and keeping the two apart is what
/// this exists for. A rule that leaves a part empty is not disagreeing: a
/// family-level rule and a version-level one that both matched are a refinement,
/// and reading the broader one's silence as dissent would erase every version a
/// rule can name. Two rules naming different values is the other case entirely.
fn consensus<'a>(
    matched: &[&'a OsDefinition],
    part: impl Fn(&'a OsDefinition) -> Option<&'a str>,
) -> Result<Option<String>, Contradiction> {
    let mut stated = matched.iter().copied().filter_map(part);
    let Some(candidate) = stated.next() else {
        return Ok(None);
    };
    match stated.all(|other| other == candidate) {
        true => Ok(Some(candidate.to_owned())),
        false => Err(Contradiction),
    }
}

/// Scores the rules that matched, whatever gathered them, into one verdict.
///
/// The half [`classify`] and [`classify_series`] share: they differ in which
/// rules they ask about and what the resulting line says, and agree on
/// everything after. `evidence` is that line, already rendered, because only the
/// caller knows how many replies went into it.
fn score(matched: Vec<&OsDefinition>, evidence: String) -> Option<OsVerdict> {
    if matched.is_empty() {
        return None;
    }

    // The two axes that identify the host. Dissent on either is fatal: the rules
    // cannot both be right and nothing here can say which is, and a tie broken by
    // weight would report an authoring decision as a measurement.
    let family = consensus(&matched, |rule| rule.os.family.as_deref()).ok()?;
    let device = consensus(&matched, |rule| rule.os.device.as_deref()).ok()?;
    if family.is_none() && device.is_none() {
        return None;
    }

    // Keep the finer parts of the path, where the rules that spoke to them
    // agree. Silence abstains here too, and two rules naming *different* values
    // yield nothing rather than nothing at all: below the identifying axes a
    // contradiction and an absence mean the same thing, which is that the part
    // goes unreported.
    let agreed = |part: fn(&OsDefinition) -> Option<&str>| -> Option<String> {
        consensus(&matched, part).unwrap_or_default()
    };

    // The weight of the least confident match, not the most: a set of rules is
    // only as good as its weakest member when they are all claiming the same
    // thing.
    let weight = matched
        .iter()
        .map(|rule| rule.weight)
        .fold(f32::INFINITY, f32::min);

    // The least confident provenance among the matches, for the same reason as
    // the weight: a set of rules claiming one thing is only as good as its
    // weakest member.
    let base = if matched
        .iter()
        .all(|rule| rule.provenance == Provenance::Measured)
    {
        MEASURED_ACCURACY
    } else {
        PUBLISHED_ACCURACY
    };

    let accuracy = (base * weight).clamp(0.0, f32::from(MAX_STACK_ACCURACY)) as u8;
    if accuracy < MIN_REPORTABLE_ACCURACY {
        return None;
    }

    let (vendor, product, version, cpe) = (
        agreed(|rule| rule.os.vendor.as_deref()),
        agreed(|rule| rule.os.product.as_deref()),
        agreed(|rule| rule.os.version.as_deref()),
        agreed(|rule| rule.os.cpe.as_deref()),
    );

    Some(OsVerdict {
        family,
        device,
        // A rule is one source, and it asserts its whole identity at once: the
        // release it names is worth exactly what the rule is worth. The two
        // figures only come apart once *several* sources are folded together,
        // which is `resolve`'s job rather than this one's.
        detail_accuracy: (vendor.is_some()
            || product.is_some()
            || version.is_some()
            || cpe.is_some())
        .then_some(accuracy),
        vendor,
        product,
        version,
        // A stack rule reads a reply's shape, which carries neither a kernel
        // release nor an instruction set: one kernel build emits the same
        // handshake on every architecture it targets. Only a service that states
        // one can supply either.
        kernel: None,
        arch: None,
        cpe,
        accuracy,
        source: OsSource::TcpStack,
        evidence,
    })
}

/// Names the operating system behind a TCP reply, from bytes.
///
/// The whole path in one call, for a caller who has a TCP segment and what its
/// IP header said: build the observation, ask the shipped rules, return what can
/// be said. Nothing here opens a socket or touches the scanner.
pub fn classify_reply(
    ip: crate::model::capture::IpObservation,
    segment: &[u8],
) -> Option<OsVerdict> {
    let observed = StackObservation::from_tcp(ip, segment)?;
    classify(RuleDb::global(), &observed.into())
}

/// Names the operating system behind an echo reply, from bytes.
///
/// The counterpart of [`classify_reply`] for the reply a host with no open and
/// no closed port can still give. `sent_payload` is what the request carried,
/// which is the only way to know whether what came back is what went out.
pub fn classify_echo_reply(
    ip: crate::model::capture::IpObservation,
    message: &[u8],
    sent_payload: &[u8],
) -> Option<OsVerdict> {
    let observed = super::observation::EchoObservation::from_echo_reply(ip, message, sent_payload)?;
    classify(RuleDb::global(), &observed.into())
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::capture::{IpObservation, Ipv4Observation};
    use crate::protocols::tcp::flags;

    /// The IP header a Linux host on this segment answered with.
    fn ip() -> IpObservation {
        IpObservation::V4(Ipv4Observation {
            ttl: 64,
            identification: 0,
            dont_fragment: true,
            more_fragments: false,
            dscp: 0,
            ecn: 0,
        })
    }

    /// A TCP segment carrying `flags`, `window` and `options`, assembled from
    /// RFC 793 offsets rather than through this crate's own builder.
    fn segment(flag_byte: u8, window: u16, options: &[u8]) -> Vec<u8> {
        let mut bytes = vec![0u8; 20 + options.len()];
        bytes[0..2].copy_from_slice(&80u16.to_be_bytes());
        bytes[2..4].copy_from_slice(&50_000u16.to_be_bytes());
        bytes[4..8].copy_from_slice(&1u32.to_be_bytes());
        bytes[12] = (((20 + options.len()) / 4) as u8) << 4;
        bytes[13] = flag_byte;
        bytes[14..16].copy_from_slice(&window.to_be_bytes());
        bytes[20..].copy_from_slice(options);
        bytes
    }

    /// The option bytes a single-board computer running Debian bookworm on
    /// kernel 6.12.47 answered a negotiating SYN with, recorded off the wire on
    /// 2026-08-18.
    const DEBIAN_BOOKWORM: [u8; 20] = [
        0x02, 0x04, 0x05, 0xb4, 0x04, 0x02, 0x08, 0x0a, 0xad, 0x58, 0xa5, 0xa7, 0x64, 0x48, 0x96,
        0x12, 0x01, 0x03, 0x03, 0x07,
    ];

    /// The whole path, from the bytes a real labelled host sent to a name. This
    /// is the claim phase 4 makes, and the machine it is made about is one whose
    /// operating system is known independently of anything the engine says.
    #[test]
    fn a_recorded_linux_reply_is_named_linux() {
        let verdict = classify_reply(
            ip(),
            &segment(flags::SYN | flags::ACK, 65_160, &DEBIAN_BOOKWORM),
        )
        .expect("a labelled Linux host is identified");

        assert_eq!(verdict.family.as_deref(), Some("Linux"));
        assert_eq!(verdict.source, OsSource::TcpStack);

        let fingerprint = verdict.to_fingerprint();
        assert_eq!(fingerprint.name(), "Linux");
        assert_eq!(fingerprint.family(), Some("Linux"));
    }

    /// The weight bound is where the knob stops turning.
    ///
    /// A verdict's accuracy is a base worth times the weight, clamped at
    /// [`MAX_STACK_ACCURACY`]. Past the point where that clamp bites, every
    /// weight produces the same answer: the bound was ten and measured, 1.08
    /// through 10 were indistinguishable, so nine tenths of the range an author
    /// could reach for did nothing and the build validated against the end of it
    /// where nothing happens.
    ///
    /// This fails if the bound rises back above where the arithmetic saturates.
    #[test]
    fn the_weight_bound_leaves_no_dead_range() {
        use super::super::MAX_RULE_WEIGHT;

        let saturates_at = f32::from(MAX_STACK_ACCURACY) / MEASURED_ACCURACY;
        assert!(
            saturates_at < MAX_RULE_WEIGHT,
            "a measured rule saturates at {saturates_at}, which is at or above the \
             bound of {MAX_RULE_WEIGHT}, so no weight reaches the ceiling"
        );

        // And not so far above it that most of the range is inert.
        assert!(
            MAX_RULE_WEIGHT < saturates_at * 2.0,
            "weights from {saturates_at} to {MAX_RULE_WEIGHT} all produce the same \
             answer, so most of the range an author can reach for does nothing"
        );
    }

    /// The ceiling exists so that one correlated packet cannot satisfy the
    /// threshold a caller uses to stop probing. If stack evidence alone could
    /// reach high confidence, every host with an open port would look settled and
    /// no further probe would ever be justified.
    #[test]
    fn stack_evidence_alone_never_reaches_high_confidence() {
        let verdict = classify_reply(
            ip(),
            &segment(flags::SYN | flags::ACK, 65_160, &DEBIAN_BOOKWORM),
        )
        .expect("a labelled Linux host is identified");

        assert!(verdict.accuracy <= MAX_STACK_ACCURACY);
        assert!(
            !verdict.to_fingerprint().is_highly_confident(),
            "one reply's shape is one observation, however many of its fields agree"
        );
    }

    /// Silence about an unknown host beats a confident wrong answer: a scan that
    /// reports nothing invites a second look, and one that reports "Linux" about
    /// a Windows machine is believed.
    #[test]
    fn a_shape_no_rule_describes_is_reported_as_nothing() {
        // An option layout nothing emits: a maximum segment size, an option kind
        // no stack writes, and padding. Well-formed, and belonging to no family
        // in the corpus.
        //
        // The layout rather than the window; see the test
        // below. A window nothing has been measured at is an ordinary Linux
        // host whose owner tuned it, and naming that one nothing was the defect
        // this pair now guards from both sides.
        let nothing_emits = [2, 4, 0x05, 0xb4, 99, 2, 1, 1];
        let verdict = classify_reply(
            ip(),
            &segment(flags::SYN | flags::ACK, 65_160, &nothing_emits),
        );
        assert!(verdict.is_none());
    }

    /// And the other side of it: a host is not disqualified for having been
    /// tuned.
    ///
    /// Measured 2026-08-21: one `sysctl -w net.ipv4.tcp_rmem=...` on an
    /// untouched kernel moved a Debian guest's window and window scale together,
    /// and the rule that pinned them stopped matching. A machine went from
    /// `Linux [65%]` to no operating system at all because somebody had raised
    /// their receive buffers, which is a rule describing a configuration while
    /// claiming to describe a system.
    ///
    /// The hop counter, the option layout and the two capabilities the peer
    /// named are what survive tuning, and they are what the rule now tests.
    #[test]
    fn a_linux_host_with_tuned_receive_buffers_is_still_linux() {
        for window in [12_345u16, 29_200, 64_240, 65_160] {
            let verdict = classify_reply(
                ip(),
                &segment(flags::SYN | flags::ACK, window, &DEBIAN_BOOKWORM),
            )
            .unwrap_or_else(|| panic!("a Linux handshake advertising {window} names nothing"));
            assert_eq!(verdict.family.as_deref(), Some("Linux"));
        }
    }

    /// A distribution is named by its distribution, not by its kernel.
    ///
    /// The imported corpus writes a Linux distro as `vendor = "Debian"`,
    /// `product = "Linux"`, `family = "Linux"`, 993 rules set `product` to the
    /// family name like that, and reading `product` as the label reported a
    /// real Debian 12 host as `Linux 12.0`, a version number no Linux has ever
    /// carried.
    #[test]
    fn a_distribution_is_named_by_its_distribution() {
        let verdict = OsVerdict {
            family: Some("Linux".to_owned()),
            device: None,
            vendor: Some("Debian".to_owned()),
            product: Some("Linux".to_owned()),
            version: Some("12".to_owned()),
            kernel: None,
            arch: None,
            cpe: None,
            accuracy: 84,
            detail_accuracy: Some(55),
            source: OsSource::ServiceBanner,
            evidence: "service banner names Linux".to_owned(),
        };

        let fingerprint = verdict.to_fingerprint();
        assert_eq!(fingerprint.name(), "Debian");
        assert_eq!(fingerprint.family(), Some("Linux"));
        assert_eq!(
            fingerprint.to_string(),
            "Linux [84%] · Debian 12 [55%]",
            "the family carries what several sources agreed; the release carries \
             what the one source that named it was worth"
        );
    }

    /// A device class means the product is a model number, and a model number
    /// without its maker names nothing anybody can look up. `NC-8700w` is a
    /// string; `Brother NC-8700w` is a printer.
    #[test]
    fn a_model_number_is_labelled_with_the_maker_that_built_it() {
        let verdict = OsVerdict {
            family: Some("Network device".to_owned()),
            device: Some("Printer".to_owned()),
            vendor: Some("Brother".to_owned()),
            product: Some("NC-8700w".to_owned()),
            version: Some("ZL".to_owned()),
            kernel: None,
            arch: None,
            cpe: None,
            accuracy: 40,
            detail_accuracy: Some(56),
            source: OsSource::SnmpAgent,
            evidence: "snmp agent names NC-8700w".to_owned(),
        };

        let fingerprint = verdict.to_fingerprint();
        assert_eq!(fingerprint.name(), "Brother NC-8700w");
        assert_eq!(fingerprint.device(), Some("Printer"));
        assert_eq!(fingerprint.family(), Some("Network device"));

        // And not twice, where the corpus already wrote the maker into the model.
        let spelled_out = OsVerdict {
            product: Some("Brother HL-1660e".to_owned()),
            ..verdict
        };
        assert_eq!(spelled_out.label(), "Brother HL-1660e");
    }

    /// A verdict with no family at all, an agent that named a box outright and
    /// never said what it runs, still labels itself off what it did establish.
    #[test]
    fn a_verdict_without_a_family_is_still_named() {
        let verdict = OsVerdict {
            family: None,
            device: Some("Printer".to_owned()),
            vendor: Some("Brother".to_owned()),
            product: Some("NC-8700w".to_owned()),
            version: Some("ZL".to_owned()),
            kernel: None,
            arch: None,
            cpe: None,
            accuracy: 56,
            detail_accuracy: Some(56),
            source: OsSource::SnmpAgent,
            evidence: "snmp agent names NC-8700w".to_owned(),
        };

        let fingerprint = verdict.to_fingerprint();
        assert_eq!(fingerprint.name(), "Brother NC-8700w");
        assert_eq!(fingerprint.family(), None);
    }

    /// And the case that rule must not break. A rule naming no product leaves
    /// `vendor` meaning whoever made the *machine* as often as whoever
    /// published the system, so `Microsoft` + `Windows`, of which the corpus
    /// holds seventeen, has to stay `Windows`.
    #[test]
    fn a_vendor_without_a_product_does_not_replace_the_family() {
        let verdict = OsVerdict {
            family: Some("Windows".to_owned()),
            device: None,
            vendor: Some("Microsoft".to_owned()),
            product: None,
            version: Some("10".to_owned()),
            kernel: None,
            arch: None,
            cpe: None,
            accuracy: 60,
            detail_accuracy: Some(60),
            source: OsSource::ServiceBanner,
            evidence: "service banner names Windows".to_owned(),
        };

        assert_eq!(verdict.to_fingerprint().name(), "Windows");
    }

    /// A reset carries no options at all, and the corpus holds no rule written
    /// for one. Naming a host from a reset would mean matching a rule against a
    /// segment it was never written for.
    #[test]
    fn a_reset_names_nothing() {
        assert!(classify_reply(ip(), &segment(flags::RST | flags::ACK, 0, &[])).is_none());
    }

    /// Two rules that disagree about the family cannot both be right, and nothing
    /// in one reply can say which is. Breaking the tie by weight would report an
    /// authoring decision as a measurement.
    #[test]
    fn rules_that_contradict_each_other_name_nothing() {
        use super::super::signature::{
            MatchRule, OsDefinition, OsIdentity, Predicate, Provenance, ReplyKind,
        };

        let rule = |family: &str| OsDefinition {
            os: OsIdentity {
                family: Some(family.to_string()),
                device: None,
                vendor: None,
                product: None,
                version: None,
                cpe: None,
            },
            provenance: Provenance::Measured,
            notes: None,
            weight: 1.0,
            r#match: MatchRule {
                reply: ReplyKind::SynAck,
                initial_hops: Some(Predicate {
                    equals: Some(64),
                    ..Default::default()
                }),
                ..Default::default()
            },
            example: Vec::new(),
        };

        let db = RuleDb::try_from_rules(vec![rule("Linux"), rule("Windows")])
            .expect("the rules are well formed");
        let observed = StackObservation::from_tcp(
            ip(),
            &segment(flags::SYN | flags::ACK, 65_160, &DEBIAN_BOOKWORM),
        )
        .unwrap()
        .into();

        assert_eq!(db.matching(&observed).count(), 2, "both rules match");
        assert!(
            classify(&db, &observed).is_none(),
            "and the contradiction is reported as no answer, not as the heavier rule"
        );
    }
}

#[cfg(test)]
mod second_family {
    use super::*;
    use crate::model::capture::{IpObservation, Ipv4Observation};
    use crate::protocols::tcp::flags;

    /// The options a Mac answered with, rebuilt from the values measured off it:
    /// maximum segment size 1460, no-op, window scale 6, two no-ops, a
    /// timestamp, SACK-permitted, and the trailing end-of-list that no other
    /// family in the corpus writes.
    fn darwin_options() -> Vec<u8> {
        let mut options = vec![2, 4, 0x05, 0xb4]; // MSS 1460
        options.push(1); // NOP
        options.extend_from_slice(&[3, 3, 6]); // window scale 6
        options.push(1); // NOP
        options.push(1); // NOP
        options.extend_from_slice(&[8, 10]); // timestamp
        options.extend_from_slice(&0x1122_3344u32.to_be_bytes());
        options.extend_from_slice(&0x5566_7788u32.to_be_bytes());
        options.extend_from_slice(&[4, 2]); // SACK permitted
        options.push(0); // end of list
        options.push(0); // padding to a four-byte boundary
        options
    }

    fn segment(flag_byte: u8, window: u16, options: &[u8]) -> Vec<u8> {
        let mut bytes = vec![0u8; 20 + options.len()];
        bytes[0..2].copy_from_slice(&22u16.to_be_bytes());
        bytes[2..4].copy_from_slice(&50_000u16.to_be_bytes());
        bytes[4..8].copy_from_slice(&1u32.to_be_bytes());
        bytes[12] = (((20 + options.len()) / 4) as u8) << 4;
        bytes[13] = flag_byte;
        bytes[14..16].copy_from_slice(&window.to_be_bytes());
        bytes[20..].copy_from_slice(options);
        bytes
    }

    fn ip() -> IpObservation {
        IpObservation::V4(Ipv4Observation {
            ttl: 64,
            identification: 0,
            dont_fragment: true,
            more_fragments: false,
            dscp: 0,
            ecn: 0,
        })
    }

    /// The corpus now holds a second family confirmed on hardware, which is what
    /// makes the Linux rules falsifiable: until there was something else for a
    /// reply to be named, "everything is Linux" and "the rules work" produced the
    /// same output.
    ///
    /// Darwin shares its hop counter with Linux, so nothing in the IP header
    /// separates them. The option order does, in every position.
    #[tokio::test(flavor = "current_thread")]
    async fn a_measured_darwin_reply_is_named_macos_and_not_linux() {
        let observed = classify_reply(
            ip(),
            &segment(flags::SYN | flags::ACK, 65_535, &darwin_options()),
        )
        .expect("a labelled Mac is identified");

        assert_eq!(observed.family.as_deref(), Some("macOS"));
        assert_eq!(observed.vendor.as_deref(), Some("Apple"));

        // Confirmed against hardware, so it scores as a measured rule rather
        // than a published one.
        assert_eq!(observed.accuracy, MEASURED_ACCURACY as u8);
    }

    /// The window is where the two families differ in kind rather than in value.
    /// Linux counts its window in segments; Darwin announces the largest number
    /// the field holds, whatever the path. A rule that read the second as though
    /// it were the first would match one network and not the next.
    #[test]
    fn a_flat_window_is_not_read_as_a_multiple() {
        let observed = StackObservation::from_tcp(
            ip(),
            &segment(flags::SYN | flags::ACK, 65_535, &darwin_options()),
        )
        .expect("the reply parses");

        assert_eq!(observed.window, 65_535);
        assert_eq!(observed.effective_mss(), Some(1448));
        assert_eq!(
            observed.window_in_units(),
            Some((45, 375)),
            "the derived figures exist, and describe the path rather than the sender"
        );
    }
}

#[cfg(test)]
mod series_backed {
    use super::*;
    use crate::fingerprint::os::series::{ClockClass, IdClass, IsnClass};
    use crate::model::capture::{IpObservation, Ipv4Observation};
    use crate::protocols::tcp::flags;

    use super::super::signature::{MatchRule, OsIdentity, Predicate, ReplyKind};

    fn ip() -> IpObservation {
        IpObservation::V4(Ipv4Observation {
            ttl: 64,
            identification: 0,
            dont_fragment: true,
            more_fragments: false,
            dscp: 0,
            ecn: 0,
        })
    }

    /// A handshake answer, assembled from RFC 793's offsets.
    fn syn_ack() -> StackReply {
        let mut bytes = vec![0u8; 20];
        bytes[0..2].copy_from_slice(&22u16.to_be_bytes());
        bytes[2..4].copy_from_slice(&50_000u16.to_be_bytes());
        bytes[12] = 5 << 4;
        bytes[13] = flags::SYN | flags::ACK;
        bytes[14..16].copy_from_slice(&64_240u16.to_be_bytes());
        StackObservation::from_tcp(ip(), &bytes)
            .expect("a handshake answer")
            .into()
    }

    /// What several replies from a stack with a hashed generator look like once
    /// classified.
    fn hashed() -> SeriesClasses {
        SeriesClasses {
            identifiers: IdClass::Zero,
            sequences: IsnClass::Hashed,
            clock: ClockClass::Hertz(1000),
        }
    }

    /// An identity naming a family, and a release where the rule reaches one.
    ///
    /// The product travels with the version because a version needs something to
    /// be a version *of*: `OsDefinition::validate` refuses the pair without it,
    /// and it caught these fixtures the day the checked constructor arrived. The
    /// corpus writes a distribution the same way.
    fn named(family: &str, version: Option<&str>) -> OsIdentity {
        OsIdentity {
            family: Some(family.to_owned()),
            device: None,
            vendor: None,
            product: version.map(|_| "Debian".to_owned()),
            version: version.map(str::to_owned),
            cpe: None,
        }
    }

    fn rule(os: OsIdentity, r#match: MatchRule) -> OsDefinition {
        OsDefinition {
            os,
            provenance: Provenance::Measured,
            notes: None,
            weight: 1.0,
            r#match,
            example: Vec::new(),
        }
    }

    /// A predicate over the hop counter, which every rule here shares so that
    /// the series predicate is the only thing separating them.
    fn at_64() -> Option<Predicate<u8>> {
        Some(Predicate {
            equals: Some(64),
            ..Default::default()
        })
    }

    fn is(name: &str) -> Option<Predicate<String>> {
        Some(Predicate {
            equals: Some(name.to_owned()),
            ..Default::default()
        })
    }

    /// The guarantee that makes a series predicate safe to author: it cannot be
    /// satisfied by a single reply, because a single reply has no series to
    /// satisfy it with. A rule that says "this generator hashes" must never be
    /// matched by one sequence number, whatever that number is.
    #[test]
    fn a_series_rule_cannot_be_satisfied_by_one_reply() {
        let db = RuleDb::try_from_rules(vec![rule(
            named("Linux", Some("6.x")),
            MatchRule {
                reply: ReplyKind::SynAck,
                initial_hops: at_64(),
                sequence_class: is("hashed"),
                ..Default::default()
            },
        )])
        .expect("the rules are well formed");
        let reply = syn_ack();

        assert!(
            classify(&db, &reply).is_none(),
            "the passive path has no series, so a series rule must fail against it"
        );

        let verdict = classify_series(&db, &[(reply, hashed())])
            .expect("the same rule matches once the series is known");
        assert_eq!(verdict.family.as_deref(), Some("Linux"));
        assert_eq!(verdict.version.as_deref(), Some("6.x"));
    }

    /// The reason a version-level rule can exist at all.
    ///
    /// A rule naming a release is necessarily *narrower* than the family rule
    /// that describes the same stack, so both match, every time. Treating the
    /// family rule's silence about the version as disagreement would erase the
    /// version on the hosts the finer rule was written for, which is more
    /// evidence yielding a less specific answer.
    #[test]
    fn a_broader_rule_matching_beside_a_finer_one_does_not_erase_the_version() {
        let db = RuleDb::try_from_rules(vec![
            rule(
                named("Linux", None),
                MatchRule {
                    reply: ReplyKind::SynAck,
                    initial_hops: at_64(),
                    ..Default::default()
                },
            ),
            rule(
                named("Linux", Some("6.x")),
                MatchRule {
                    reply: ReplyKind::SynAck,
                    initial_hops: at_64(),
                    sequence_class: is("hashed"),
                    ..Default::default()
                },
            ),
        ])
        .expect("the rules are well formed");

        let reply = syn_ack();
        assert_eq!(
            db.matching_with_series(&reply, &hashed()).count(),
            2,
            "both rules describe this host"
        );

        let verdict =
            classify_series(&db, &[(reply, hashed())]).expect("a host both rules describe");
        assert_eq!(verdict.family.as_deref(), Some("Linux"));
        assert_eq!(
            verdict.version.as_deref(),
            Some("6.x"),
            "the finer rule states a version and the broader one abstains"
        );
    }

    /// Abstention is not agreement with anything: two rules naming *different*
    /// releases still cannot both be right, and nothing here can say which is.
    #[test]
    fn two_rules_naming_different_versions_keep_neither() {
        let with_version = |version: &str, class: &str| {
            rule(
                named("Linux", Some(version)),
                MatchRule {
                    reply: ReplyKind::SynAck,
                    initial_hops: at_64(),
                    sequence_class: is(class),
                    ..Default::default()
                },
            )
        };
        let db = RuleDb::try_from_rules(vec![
            with_version("6.x", "hashed"),
            with_version("7.x", "hashed"),
        ])
        .expect("the rules are well formed");

        let verdict = classify_series(&db, &[(syn_ack(), hashed())]).expect("the family is agreed");
        assert_eq!(verdict.family.as_deref(), Some("Linux"));
        assert!(
            verdict.version.is_none(),
            "a contradiction about the release is not a release"
        );
    }

    /// Several replies from one host are one piece of evidence about one stack.
    /// The scanner that collects them hands them over together for exactly this
    /// reason: separately they would pass through the resolver's noisy-OR as
    /// though a machine agreeing with itself were two sources agreeing with each
    /// other.
    #[test]
    fn a_host_read_several_ways_still_yields_one_verdict() {
        let db = RuleDb::try_from_rules(vec![rule(
            named("Linux", None),
            MatchRule {
                reply: ReplyKind::SynAck,
                initial_hops: at_64(),
                ..Default::default()
            },
        )])
        .expect("the rules are well formed");

        let one = classify_series(&db, &[(syn_ack(), hashed())]).expect("one reading names it");
        let twice = classify_series(&db, &[(syn_ack(), hashed()), (syn_ack(), hashed())])
            .expect("two readings still name it");

        assert_eq!(one.family, twice.family);
        assert_eq!(
            one.accuracy, twice.accuracy,
            "reading one stack twice is not two pieces of evidence"
        );
    }
}