flowscope 0.26.0

Passive flow & session tracking for packet capture (runtime-free, cross-platform)
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
//! Per-parser dispatch shared by [`super::SessionDriver`],
//! [`super::DatagramDriver`] and every slot of the typed
//! [`crate::driver::Driver`].
//!
//! A *core* owns one parser type's per-flow state and turns
//! reassembled stream chunks (or datagrams) into messages and parser
//! closes. It owns no flow table: the engine
//! ([`super::engine::Engine`]) feeds it from one shared
//! [`crate::FlowDriver`], which is what keeps every consumer's notion
//! of "a flow" identical.
//!
//! Per-flow parser state is `Probing → Active → Closed`. `Closed` is
//! a tombstone kept until the flow's transport end, so a poisoned /
//! done / gap-stopped parser is never re-created mid-flow.

use std::collections::HashMap;
use std::hash::Hash;

use ahash::RandomState;
use smallvec::SmallVec;

use crate::Timestamp;
use crate::detect::signatures::{SignatureFn, SignatureMatch};
use crate::event::{AnomalyKind, EndReason, FlowSide, FlowStats};
use crate::extractor::Orientation;
use crate::parser_kind::ParserKind;
use crate::reassembler::{Chunk, Chunks, ReassemblyStop, StreamChunks};
use crate::session::{
    DatagramParser, DatagramParserFactory, GapResponse, SessionParser, SessionParserFactory,
    Transports,
};

/// Cap on the size of `poison_reason()` strings carried through
/// [`AnomalyKind::SessionParseError`] and parser-close details.
pub(crate) const REASON_MAX_BYTES: usize = 256;

/// Default probing budget (data-bearing packets) for signature slots.
pub const DEFAULT_PROBE_PACKETS: u8 = 4;

/// Bytes per side a signature is evaluated on. Every shipped
/// signature decides within this prefix.
pub const PROBE_BUFFER_CAP: usize = 64;

/// Cap on the stream bytes held for replay while a flow is probed.
/// Past it, a side's further bytes are not kept; if the signature
/// then matches, the parser gets them as a leading gap (it starts
/// mid-stream on that side, and is told so).
pub(crate) const PROBE_REPLAY_BYTE_CAP: usize = 64 * 1024;

pub(crate) fn truncate_reason(s: &str) -> String {
    let mut owned = String::from(s);
    if owned.len() > REASON_MAX_BYTES {
        let cap = (0..=REASON_MAX_BYTES)
            .rev()
            .find(|i| owned.is_char_boundary(*i))
            .unwrap_or(0);
        owned.truncate(cap);
    }
    owned
}

/// Source and destination port of a packet, when it has them.
pub(crate) type Ports = Option<(u16, u16)>;

/// Reassembled output handed to a core: chunks drained from a
/// reassembler, or in-order bytes let through straight from the
/// packet (no copy).
#[derive(Clone, Copy)]
pub(crate) enum Stream<'a> {
    Chunks(&'a StreamChunks),
    Bytes(&'a [u8]),
}

impl<'a> Stream<'a> {
    pub(crate) fn is_empty(&self) -> bool {
        match self {
            Stream::Chunks(c) => c.is_empty(),
            Stream::Bytes(b) => b.is_empty(),
        }
    }

    /// Data bytes (gaps excluded).
    pub(crate) fn len(&self) -> usize {
        match self {
            Stream::Chunks(c) => c.len(),
            Stream::Bytes(b) => b.len(),
        }
    }

    pub(crate) fn stop(&self) -> Option<ReassemblyStop> {
        match self {
            Stream::Chunks(c) => c.stop(),
            Stream::Bytes(_) => None,
        }
    }

    pub(crate) fn iter(&self) -> StreamIter<'a> {
        match *self {
            Stream::Chunks(c) => StreamIter::Chunks(c.iter()),
            Stream::Bytes(b) => StreamIter::Bytes((!b.is_empty()).then_some(b)),
        }
    }

    /// Owned copy (for probing replay).
    pub(crate) fn to_chunks(self) -> StreamChunks {
        match self {
            Stream::Chunks(c) => c.clone(),
            Stream::Bytes(b) => {
                let mut c = StreamChunks::new();
                c.push_data(b);
                c
            }
        }
    }
}

pub(crate) enum StreamIter<'a> {
    Chunks(Chunks<'a>),
    Bytes(Option<&'a [u8]>),
}

impl<'a> Iterator for StreamIter<'a> {
    type Item = Chunk<'a>;
    fn next(&mut self) -> Option<Chunk<'a>> {
        match self {
            StreamIter::Chunks(c) => c.next(),
            StreamIter::Bytes(b) => b.take().map(Chunk::Data),
        }
    }
}

/// Which flows a core wants.
pub(crate) enum Selector {
    /// Every flow of the right transport.
    All,
    /// Flows with either port in the set.
    Ports(SmallVec<[u16; 4]>),
    /// Flows whose first bytes match a signature.
    Signature {
        signature: SignatureFn,
        max_probe_packets: u8,
    },
}

impl Selector {
    pub(crate) fn needs_ports(&self) -> bool {
        matches!(self, Selector::Ports(_))
    }

    /// Whether a packet with these ports can belong to a wanted flow.
    pub(crate) fn admits(&self, ports: Ports) -> bool {
        match self {
            Selector::Ports(set) => {
                ports.is_some_and(|(s, d)| set.contains(&s) || set.contains(&d))
            }
            Selector::All | Selector::Signature { .. } => true,
        }
    }
}

/// Where a core's output goes. The session drivers put everything in
/// one ordered buffer; typed-driver slots send messages to a drain
/// handle and closes / anomalies to the lifecycle buffer.
pub(crate) trait Output<K, M> {
    fn message(
        &mut self,
        key: &K,
        side: FlowSide,
        orientation: Orientation,
        message: M,
        ts: Timestamp,
        parser_kind: ParserKind,
    );

    /// A parser was closed. `at_flow_end` is `true` when the close is
    /// just the flow ending (the lifecycle already says so), `false`
    /// when the parser stopped early and the flow goes on.
    fn parser_closed(
        &mut self,
        key: &K,
        parser_kind: ParserKind,
        reason: EndReason,
        detail: Option<String>,
        ts: Timestamp,
        at_flow_end: bool,
    );

    /// A parser stopped reading one side; the other goes on.
    fn parser_side_stopped(
        &mut self,
        key: &K,
        parser_kind: ParserKind,
        side: FlowSide,
        reason: EndReason,
        detail: Option<String>,
        ts: Timestamp,
    );

    /// Called only when the anomaly is shown (anomalies on, not
    /// masked, not paused); the caller has already counted and traced
    /// it.
    fn anomaly(&mut self, key: &K, kind: AnomalyKind, ts: Timestamp);
}

/// Which parser-lifecycle output reaches the consumer (#204): the
/// engine's event gates — `emit_anomalies`, the
/// [`crate::EventMask`] and pause — applied to what the cores emit.
/// Gating only filters output: a parser is closed, its state released
/// and its close counted (`flowscope_parser_closed_total`) whatever
/// these say.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Emit {
    /// Anomalies are enabled: a `SessionParseError` is counted
    /// (`flowscope_anomalies_total`) and traced, like the reassembly
    /// anomalies, whether or not the event is shown.
    pub count_anomalies: bool,
    /// `SessionParseError` events: anomalies enabled,
    /// [`crate::EventMask::FLOW_ANOMALY`] not masked, not paused.
    pub anomalies: bool,
    /// Early `ParserClosed` (the flow goes on) and
    /// `ParserSideStopped`: not paused. No mask bit of their own.
    pub parser_events: bool,
    /// A flow-end `ParserClosed` rides with its flow's `Ended`:
    /// emitted only when that `Ended` is
    /// ([`crate::EventMask::ENDED`] not masked, not paused).
    pub flow_end_closes: bool,
}

/// Per-call context from the engine.
pub(crate) struct Ctx<'a, K> {
    pub key: &'a K,
    pub l4: Option<crate::L4Proto>,
    pub side: FlowSide,
    pub orientation: Orientation,
    pub ts: Timestamp,
    pub emit: Emit,
}

/// Why a parser closed early, before the flow's end.
enum Close {
    Poison(Option<String>),
    Done,
    Gap(FlowSide, u64),
    /// Both sides stopped (the last stop's reason).
    BothSides(EndReason),
}

impl Close {
    fn reason(&self) -> EndReason {
        match self {
            Close::Poison(_) => EndReason::ParseError,
            Close::Done => EndReason::ParserDone,
            Close::Gap(..) => EndReason::StreamGap,
            Close::BothSides(reason) => *reason,
        }
    }

    fn detail(self) -> Option<String> {
        match self {
            Close::Poison(r) => r,
            Close::Done => None,
            Close::Gap(side, n) => Some(format!("{n} bytes missing on the {side} side")),
            Close::BothSides(_) => Some("both sides stopped".to_owned()),
        }
    }
}

/// What ended a [`feed`] early.
enum FeedEnd {
    /// Close the whole parser.
    Close(Close),
    /// Stop reading this side.
    StopSide(EndReason, String),
}

/// A parser with its per-side stop flags.
struct Active<P> {
    parser: P,
    stopped: [bool; 2],
}

fn side_idx(side: FlowSide) -> usize {
    match side {
        FlowSide::Initiator => 0,
        FlowSide::Responder => 1,
    }
}

impl<P: SessionParser> Active<P> {
    fn new(parser: P) -> Self {
        Self {
            parser,
            stopped: [false, false],
        }
    }

    /// Feed one side. Returns `true` when the parser is closed.
    fn feed<K, O>(
        &mut self,
        cx: &Ctx<'_, K>,
        stream: &Stream<'_>,
        scratch: &mut Vec<P::Message>,
        out: &mut O,
    ) -> bool
    where
        O: Output<K, P::Message>,
    {
        let si = side_idx(cx.side);
        if self.stopped[si] {
            return false;
        }
        let kind = self.parser.parser_kind();
        match feed(&mut self.parser, cx, stream, scratch, out) {
            None => false,
            Some(FeedEnd::Close(close)) => {
                emit_close(cx, kind, close, out);
                true
            }
            Some(FeedEnd::StopSide(reason, detail)) => {
                self.stopped[si] = true;
                crate::obs::record_parser_side_stopped(kind, cx.side, reason);
                if cx.emit.parser_events {
                    out.parser_side_stopped(cx.key, kind, cx.side, reason, Some(detail), cx.ts);
                }
                if self.stopped == [true, true] {
                    emit_close(cx, kind, Close::BothSides(reason), out);
                    return true;
                }
                false
            }
        }
    }
}

/// Probing state of one flow (boxed: most flows never probe, and it
/// is much larger than a parser handle).
#[derive(Default)]
struct Probe {
    packets: u8,
    /// Contiguous prefix of each side the signature reads.
    prefix: [SmallVec<[u8; PROBE_BUFFER_CAP]>; 2],
    /// A gap (or stop) ended that side's prefix: bytes after it are
    /// not contiguous with it.
    sealed: [bool; 2],
    /// The stream seen so far, in arrival order, for the parser once
    /// it pins.
    replay: Vec<(FlowSide, Orientation, Timestamp, StreamChunks)>,
    replay_bytes: usize,
    /// Whether each side has a chunk in `replay` (the first one is
    /// always kept).
    kept: [bool; 2],
    /// Bytes (data and gaps) of each side not kept in `replay` past
    /// the cap.
    lost: [u64; 2],
}

/// A signature verdict.
enum Verdict {
    Match,
    Reject,
    Undecided,
}

impl Probe {
    /// Record one side's output and evaluate the signature.
    fn step(
        &mut self,
        cx: &Ctx<'_, impl Sized>,
        stream: &Stream<'_>,
        signature: SignatureFn,
        max_probe_packets: u8,
    ) -> Verdict {
        let si = side_idx(cx.side);
        if !self.sealed[si] {
            let prefix = &mut self.prefix[si];
            for chunk in stream.iter() {
                match chunk {
                    Chunk::Data(d) => {
                        let room = PROBE_BUFFER_CAP.saturating_sub(prefix.len());
                        prefix.extend_from_slice(&d[..room.min(d.len())]);
                    }
                    Chunk::Gap(_) => {
                        self.sealed[si] = true;
                        break;
                    }
                }
            }
            if stream.stop().is_some() {
                self.sealed[si] = true;
            }
        }
        self.packets = self.packets.saturating_add(1);
        let len = stream.len();
        if !self.kept[si]
            || (self.lost[si] == 0 && self.replay_bytes + len <= PROBE_REPLAY_BYTE_CAP)
        {
            self.kept[si] = true;
            self.replay_bytes += len;
            self.replay
                .push((cx.side, cx.orientation, cx.ts, stream.to_chunks()));
        } else {
            let gaps: u64 = stream
                .iter()
                .map(|c| match c {
                    Chunk::Gap(n) => n,
                    Chunk::Data(_) => 0,
                })
                .sum();
            self.lost[si] += len as u64 + gaps;
        }
        let verdicts = (signature(&self.prefix[0]), signature(&self.prefix[1]));
        if matches!(verdicts.0, SignatureMatch::Match)
            || matches!(verdicts.1, SignatureMatch::Match)
        {
            Verdict::Match
        } else if (matches!(verdicts.0, SignatureMatch::NoMatch)
            && matches!(verdicts.1, SignatureMatch::NoMatch))
            || self.packets >= max_probe_packets
        {
            Verdict::Reject
        } else {
            Verdict::Undecided
        }
    }
}

enum SessionFlow<P> {
    /// Signature not decided yet. `replay` keeps the stream seen so
    /// far, in arrival order, for the parser once it pins.
    Probing(Box<Probe>),
    Active(Active<P>),
    /// Closed early or rejected by the signature; ignored until the
    /// flow ends.
    Closed,
}

/// A stream-parser core: one [`SessionParserFactory`], per-flow
/// parser state.
pub(crate) struct SessionCore<K, F>
where
    F: SessionParserFactory<K>,
{
    factory: F,
    selector: Selector,
    flows: HashMap<K, SessionFlow<F::Parser>, RandomState>,
    scratch: Vec<<F::Parser as SessionParser>::Message>,
}

impl<K, F> SessionCore<K, F>
where
    K: Hash + Eq + Clone,
    F: SessionParserFactory<K>,
{
    pub(crate) fn new(factory: F, selector: Selector) -> Self {
        Self {
            factory,
            selector,
            flows: HashMap::with_hasher(RandomState::new()),
            scratch: Vec::new(),
        }
    }

    pub(crate) fn selector(&self) -> &Selector {
        &self.selector
    }

    /// Does this core want the byte stream of a flow whose packet
    /// has these ports? Consistent across a flow's packets.
    pub(crate) fn wants(&self, ports: Ports) -> bool {
        self.selector.admits(ports)
    }

    /// New reassembled output of one side of a flow. A flow this
    /// core has no state for yet is picked up only if `ports` pass
    /// the selector.
    pub(crate) fn on_stream<O>(
        &mut self,
        cx: &Ctx<'_, K>,
        ports: Ports,
        chunks: &Stream<'_>,
        out: &mut O,
    ) where
        O: Output<K, <F::Parser as SessionParser>::Message>,
    {
        if chunks.is_empty() {
            return;
        }
        if !self.flows.contains_key(cx.key) {
            if !self.selector.admits(ports) {
                return;
            }
            let state = match self.selector {
                Selector::Signature { .. } => SessionFlow::Probing(Box::default()),
                Selector::All | Selector::Ports(_) => {
                    SessionFlow::Active(Active::new(self.factory.new_parser(cx.key)))
                }
            };
            self.flows.insert(cx.key.clone(), state);
        }
        let state = self.flows.get_mut(cx.key).expect("just inserted");
        match state {
            SessionFlow::Closed => {}
            SessionFlow::Active(active) => {
                if active.feed(cx, chunks, &mut self.scratch, out) {
                    *state = SessionFlow::Closed;
                }
            }
            SessionFlow::Probing(probe) => {
                let Selector::Signature {
                    signature,
                    max_probe_packets,
                } = self.selector
                else {
                    unreachable!("only signature cores probe")
                };
                match probe.step(cx, chunks, signature, max_probe_packets) {
                    Verdict::Undecided => {}
                    Verdict::Reject => *state = SessionFlow::Closed,
                    Verdict::Match => {
                        let probe = std::mem::take(&mut **probe);
                        let parser = self.factory.new_parser(cx.key);
                        *state = match pin(parser, probe, cx, &mut self.scratch, out) {
                            Some(active) => SessionFlow::Active(active),
                            None => SessionFlow::Closed,
                        };
                    }
                }
            }
        }
    }

    /// Per side (initiator, responder): `true` when this core will
    /// never use that side's stream again — its parser closed or
    /// stopped the side, the signature rejected the flow, or the flow
    /// was never admitted.
    pub(crate) fn streams_done(&self, key: &K, ports: Ports) -> [bool; 2] {
        match self.flows.get(key) {
            Some(SessionFlow::Closed) => [true, true],
            Some(SessionFlow::Active(a)) => a.stopped,
            Some(SessionFlow::Probing(_)) => [false, false],
            None => {
                let done = !self.selector.admits(ports);
                [done, done]
            }
        }
    }

    /// The flow ended. `finals` are the last drained chunks of each
    /// side (initiator, responder) — usually empty, or out-of-order
    /// data released by the end-of-flow flush. They go through the
    /// same path as live data: a flow still probing is probed on
    /// them, and a flow first seen in them is picked up (`ports` are
    /// the flow's, for port selectors).
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn on_flow_end<O>(
        &mut self,
        key: &K,
        reason: EndReason,
        stats: &FlowStats,
        finals: [&StreamChunks; 2],
        ports: Ports,
        emit: Emit,
        out: &mut O,
    ) where
        O: Output<K, <F::Parser as SessionParser>::Message>,
    {
        let ts = stats.last_seen;
        for (side, chunks) in [FlowSide::Initiator, FlowSide::Responder]
            .into_iter()
            .zip(finals)
        {
            if chunks.is_empty() {
                continue;
            }
            let cx = Ctx {
                key,
                l4: Some(crate::L4Proto::Tcp),
                side,
                orientation: stats.orientation_for(side),
                ts,
                emit,
            };
            self.on_stream(&cx, ports, &Stream::Chunks(chunks), out);
        }
        let Some(SessionFlow::Active(mut active)) = self.flows.remove(key) else {
            return;
        };
        let kind = active.parser.parser_kind();
        // Each side still read ends on its own terms: `fin_*` when the
        // flow ended gracefully or that side sent a FIN, `rst_*`
        // otherwise. A stopped side gets neither.
        for side in [FlowSide::Initiator, FlowSide::Responder] {
            if active.stopped[side_idx(side)] {
                continue;
            }
            let fin_seen = match side {
                FlowSide::Initiator => stats.fin_initiator,
                FlowSide::Responder => stats.fin_responder,
            };
            if reason.is_graceful() || fin_seen {
                self.scratch.clear();
                match side {
                    FlowSide::Initiator => active.parser.fin_initiator(&mut self.scratch),
                    FlowSide::Responder => active.parser.fin_responder(&mut self.scratch),
                }
                let orientation = stats.orientation_for(side);
                for m in self.scratch.drain(..) {
                    out.message(key, side, orientation, m, ts, kind);
                }
            } else {
                match side {
                    FlowSide::Initiator => active.parser.rst_initiator(),
                    FlowSide::Responder => active.parser.rst_responder(),
                }
            }
        }
        crate::obs::record_parser_closed(kind, reason);
        if emit.flow_end_closes {
            out.parser_closed(key, kind, reason, None, ts, true);
        }
    }

    /// Periodic hook: `on_tick` on every live parser, closing those
    /// that poison or finish during it.
    pub(crate) fn on_tick<O>(
        &mut self,
        now: Timestamp,
        stamp: Timestamp,
        orientation_of: &dyn Fn(&K) -> Orientation,
        emit: Emit,
        out: &mut O,
    ) where
        O: Output<K, <F::Parser as SessionParser>::Message>,
    {
        for (key, state) in self.flows.iter_mut() {
            let SessionFlow::Active(Active { parser, .. }) = state else {
                continue;
            };
            let kind = parser.parser_kind();
            let orientation = orientation_of(key);
            self.scratch.clear();
            parser.on_tick(now, &mut self.scratch);
            for m in self.scratch.drain(..) {
                out.message(key, FlowSide::Initiator, orientation, m, stamp, kind);
            }
            if let Some(close) = check_close(parser) {
                *state = SessionFlow::Closed;
                let cx = Ctx {
                    key,
                    l4: None,
                    side: FlowSide::Initiator,
                    orientation,
                    ts: stamp,
                    emit,
                };
                emit_close(&cx, kind, close, out);
            }
        }
    }

    /// Drop state of flows the tracker no longer holds (their `Ended`
    /// was shed by an event mask, issue #185).
    pub(crate) fn retain(&mut self, alive: &dyn Fn(&K) -> bool) {
        self.flows.retain(|k, _| alive(k));
    }
}

/// A probed flow matched: feed the parser what was seen so far —
/// the replay log in arrival order, then, for a side whose bytes did
/// not all fit, a gap for the missing part. `None` when the parser
/// closed during the replay.
fn pin<K, P, O>(
    parser: P,
    probe: Probe,
    cx: &Ctx<'_, K>,
    scratch: &mut Vec<P::Message>,
    out: &mut O,
) -> Option<Active<P>>
where
    P: SessionParser,
    O: Output<K, P::Message>,
{
    let mut active = Active::new(parser);
    for (side, orientation, ts, chunks) in &probe.replay {
        let rcx = Ctx {
            key: cx.key,
            l4: cx.l4,
            side: *side,
            orientation: *orientation,
            ts: *ts,
            emit: cx.emit,
        };
        if active.feed(&rcx, &Stream::Chunks(chunks), scratch, out) {
            return None;
        }
    }
    for (i, side) in [FlowSide::Initiator, FlowSide::Responder]
        .into_iter()
        .enumerate()
    {
        if probe.lost[i] == 0 {
            continue;
        }
        let mut gap = StreamChunks::new();
        gap.push_gap(probe.lost[i]);
        let rcx = Ctx {
            key: cx.key,
            l4: cx.l4,
            side,
            orientation: match side == cx.side {
                true => cx.orientation,
                false => cx.orientation.flipped(),
            },
            ts: cx.ts,
            emit: cx.emit,
        };
        if active.feed(&rcx, &Stream::Chunks(&gap), scratch, out) {
            return None;
        }
    }
    Some(active)
}

/// Feed one side's chunks into `parser`. Returns what ended the feed
/// early, if anything: the parser must close, or stop this side.
fn feed<K, P, O>(
    parser: &mut P,
    cx: &Ctx<'_, K>,
    chunks: &Stream<'_>,
    scratch: &mut Vec<P::Message>,
    out: &mut O,
) -> Option<FeedEnd>
where
    P: SessionParser,
    O: Output<K, P::Message>,
{
    let kind = parser.parser_kind();
    for chunk in chunks.iter() {
        scratch.clear();
        let gap = match chunk {
            Chunk::Data(bytes) => {
                match cx.side {
                    FlowSide::Initiator => parser.feed_initiator(bytes, cx.ts, scratch),
                    FlowSide::Responder => parser.feed_responder(bytes, cx.ts, scratch),
                }
                None
            }
            Chunk::Gap(missing) => Some((missing, parser.on_gap(cx.side, missing, cx.ts, scratch))),
        };
        for m in scratch.drain(..) {
            out.message(cx.key, cx.side, cx.orientation, m, cx.ts, kind);
        }
        if let Some(close) = check_close(parser) {
            return Some(FeedEnd::Close(close));
        }
        match gap {
            Some((missing, GapResponse::Stop)) => {
                return Some(FeedEnd::Close(Close::Gap(cx.side, missing)));
            }
            Some((missing, GapResponse::StopSide)) => {
                return Some(FeedEnd::StopSide(
                    EndReason::StreamGap,
                    format!("{missing} bytes missing"),
                ));
            }
            _ => {}
        }
    }
    chunks.stop().map(|stop| {
        FeedEnd::StopSide(
            EndReason::BufferOverflow,
            format!("reassembly stopped: {stop}"),
        )
    })
}

fn check_close<P: SessionParser>(parser: &P) -> Option<Close> {
    if parser.is_poisoned() {
        Some(Close::Poison(parser.poison_reason().map(truncate_reason)))
    } else if parser.is_done() {
        Some(Close::Done)
    } else {
        None
    }
}

fn emit_close<K, M, O: Output<K, M>>(cx: &Ctx<'_, K>, kind: ParserKind, close: Close, out: &mut O) {
    let reason = close.reason();
    crate::obs::record_parser_closed(kind, reason);
    if let Close::Poison(detail) = &close
        && cx.emit.count_anomalies
    {
        let anomaly = AnomalyKind::SessionParseError {
            side: cx.side,
            reason: detail.clone(),
            parser_kind: kind,
            slot: None,
        };
        // Counted whenever anomalies are on; the mask and a pause only
        // decide whether the event is shown.
        crate::obs::record_anomaly(&anomaly);
        crate::obs::trace_anomaly(&anomaly);
        if cx.emit.anomalies {
            out.anomaly(cx.key, anomaly, cx.ts);
        }
    }
    if cx.emit.parser_events {
        out.parser_closed(cx.key, kind, reason, close.detail(), cx.ts, false);
    }
}

enum DatagramFlow<P> {
    Probing { packets: u8 },
    Active(P),
    Closed,
}

/// A datagram-parser core: one [`DatagramParserFactory`], per-flow
/// parser state.
pub(crate) struct DatagramCore<K, F>
where
    F: DatagramParserFactory<K>,
{
    factory: F,
    selector: Selector,
    transports: Transports,
    flows: HashMap<K, DatagramFlow<F::Parser>, RandomState>,
    scratch: Vec<<F::Parser as DatagramParser>::Message>,
}

impl<K, F> DatagramCore<K, F>
where
    K: Hash + Eq + Clone,
    F: DatagramParserFactory<K>,
{
    pub(crate) fn new(factory: F, selector: Selector) -> Self {
        let transports = factory.transports();
        Self {
            factory,
            selector,
            transports,
            flows: HashMap::with_hasher(RandomState::new()),
            scratch: Vec::new(),
        }
    }

    pub(crate) fn selector(&self) -> &Selector {
        &self.selector
    }

    /// Does this core want datagrams of this transport / ports?
    pub(crate) fn wants(&self, ports: Ports, l4: Option<crate::L4Proto>) -> bool {
        self.transports.admits(l4) && self.selector.admits(ports)
    }

    /// One datagram payload.
    pub(crate) fn on_datagram<O>(
        &mut self,
        cx: &Ctx<'_, K>,
        ports: Ports,
        payload: &[u8],
        out: &mut O,
    ) where
        O: Output<K, <F::Parser as DatagramParser>::Message>,
    {
        if !self.transports.admits(cx.l4) {
            return;
        }
        if !self.flows.contains_key(cx.key) {
            if !self.selector.admits(ports) {
                return;
            }
            let state = match self.selector {
                Selector::Signature { .. } => DatagramFlow::Probing { packets: 0 },
                Selector::All | Selector::Ports(_) => {
                    DatagramFlow::Active(self.factory.new_parser(cx.key))
                }
            };
            self.flows.insert(cx.key.clone(), state);
        }
        let state = self.flows.get_mut(cx.key).expect("just inserted");
        if let DatagramFlow::Probing { packets } = state {
            let Selector::Signature {
                signature,
                max_probe_packets,
            } = self.selector
            else {
                unreachable!("only signature cores probe")
            };
            match signature(payload) {
                SignatureMatch::Match => {
                    *state = DatagramFlow::Active(self.factory.new_parser(cx.key))
                }
                verdict => {
                    *packets = packets.saturating_add(1);
                    if matches!(verdict, SignatureMatch::NoMatch) || *packets >= max_probe_packets {
                        *state = DatagramFlow::Closed;
                    }
                    return;
                }
            }
        }
        let DatagramFlow::Active(parser) = state else {
            return;
        };
        let kind = parser.parser_kind();
        self.scratch.clear();
        parser.parse(payload, cx.side, cx.ts, &mut self.scratch);
        for m in self.scratch.drain(..) {
            out.message(cx.key, cx.side, cx.orientation, m, cx.ts, kind);
        }
        if let Some(close) = check_datagram_close(parser) {
            *state = DatagramFlow::Closed;
            emit_close(cx, kind, close, out);
        }
    }

    pub(crate) fn on_flow_end<O>(
        &mut self,
        key: &K,
        reason: EndReason,
        stats: &FlowStats,
        emit: Emit,
        out: &mut O,
    ) where
        O: Output<K, <F::Parser as DatagramParser>::Message>,
    {
        if let Some(DatagramFlow::Active(parser)) = self.flows.remove(key) {
            crate::obs::record_parser_closed(parser.parser_kind(), reason);
            if emit.flow_end_closes {
                out.parser_closed(
                    key,
                    parser.parser_kind(),
                    reason,
                    None,
                    stats.last_seen,
                    true,
                );
            }
        }
    }

    pub(crate) fn on_tick<O>(
        &mut self,
        now: Timestamp,
        stamp: Timestamp,
        orientation_of: &dyn Fn(&K) -> Orientation,
        emit: Emit,
        out: &mut O,
    ) where
        O: Output<K, <F::Parser as DatagramParser>::Message>,
    {
        for (key, state) in self.flows.iter_mut() {
            let DatagramFlow::Active(parser) = state else {
                continue;
            };
            let kind = parser.parser_kind();
            let orientation = orientation_of(key);
            self.scratch.clear();
            parser.on_tick(now, &mut self.scratch);
            for m in self.scratch.drain(..) {
                out.message(key, FlowSide::Initiator, orientation, m, stamp, kind);
            }
            if let Some(close) = check_datagram_close(parser) {
                *state = DatagramFlow::Closed;
                let cx = Ctx {
                    key,
                    l4: None,
                    side: FlowSide::Initiator,
                    orientation,
                    ts: stamp,
                    emit,
                };
                emit_close(&cx, kind, close, out);
            }
        }
    }

    pub(crate) fn retain(&mut self, alive: &dyn Fn(&K) -> bool) {
        self.flows.retain(|k, _| alive(k));
    }
}

fn check_datagram_close<P: DatagramParser>(parser: &P) -> Option<Close> {
    if parser.is_poisoned() {
        Some(Close::Poison(parser.poison_reason().map(truncate_reason)))
    } else if parser.is_done() {
        Some(Close::Done)
    } else {
        None
    }
}