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
// 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
//! # The operating-system rule database
//!
//! The compiled artifact and the access over it.
//!
//! ## What it costs, measured
//!
//! Held flat and walked. That is a different shape from the service signatures
//! next door, and deliberately so: those carry thousands of regexes whose
//! *compilation* dominates everything, which is why they are compiled lazily,
//! cached, warmed in parallel, and narrowed by a port index and an Aho-Corasick
//! literal prefilter before any of it happens. A rule here compiles to nothing —
//! it is a handful of integer comparisons — so the same machinery would cost
//! more than it saves.
//!
//! Measured on this machine, one observation against a synthetic rule set:
//!
//! | rules | per host |
//! |---|---|
//! | 2 (what ships) | 0.8 µs |
//! | 1 000 | 33 µs |
//! | 10 000 | 278 µs |
//!
//! Linear, with a small constant. At the two rules that ship it is not
//! measurable against anything else a scan does; at ten thousand — which is what
//! translating a public corpus would bring — it is 18 seconds of CPU across a
//! `/16`, which is real but not disqualifying.
//!
//! Getting there took one fix worth naming, because it is the mistake this shape
//! invites. Rendering the option layout allocates, and doing it inside the
//! per-rule test cost **3.2 ms per host** at ten thousand rules — 210 seconds
//! across a `/16`, essentially all of it spent building the same short string
//! ten thousand times over. [`rules::matching`](super::rules) now works the
//! derived values out once for the whole set and orders the cheap integer
//! comparisons ahead of the string one, which is 11.5x at ten thousand rules and
//! 1.7x at two.
//!
//! ## Where the index goes, when it is needed
//!
//! [`RuleDb::matching`] is the seam, and nothing above it would change. The
//! natural narrowing is the direct analogue of the service side's port index: key
//! rules by reply kind and by an exactly-specified option layout, keep the rules
//! that state neither in a set that is always checked, and the walk goes from
//! every rule to a handful. It is not built, because two rules do not need it and
//! an index nobody can measure the benefit of is a guess.
//!
//! ## Where the artifact comes from
//!
//! Today it is the `bincode` blob `build.rs` compiles from
//! `assets/fingerprinting/os/`. [`RuleDb::global`] is the single place a
//! disk-loaded database will slot in later, which is what makes it possible to
//! use a corpus this crate can never ship: the largest and best operating-system
//! fingerprint database in existence is nmap's, and its licence is incompatible
//! with this one, so it can only ever be something a user brings and translates
//! on their own machine. The translator is the deliverable there; the corpus is
//! not.
use OnceLock;
use StackReply;
use rules;
use OsDefinition;
/// The rules compiled from `assets/fingerprinting/os/` by `build.rs`.
const EMBEDDED_RULES: & = include_bytes!;
static DB: = new;
/// Runtime view over the operating-system rules.