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
//! [`KeyFields`] + [`AnomalyFields`] — structured field access
//! for emit writers (EVE, NDJSON, CSV, Zeek, custom).
//!
//! Lets writers pull typed accessors (`IpAddr`, `u16`,
//! `&'static str`) off a flow key + an [`crate::AnomalyKind`]
//! without going through `Debug` formatting.
//!
//! Plan 126 introduced these as one trait; plan 130 (0.12.0)
//! split them along their natural cleavage so the type system
//! reflects what's a flow-key property vs an anomaly property:
//!
//! - [`KeyFields`] — 5-tuple / protocol accessors. Default
//! impls on [`crate::extract::FiveTupleKey`] and
//! [`crate::L4Proto`]. Implement for custom keys to make them
//! work with every [`crate::emit`] writer.
//! - [`AnomalyFields`] — anomaly-classification accessors.
//! Implemented on [`crate::AnomalyKind`].
use std::net::IpAddr;
/// Structured access to flow-key fields for emit writers.
///
/// All methods default to `None` so implementors override only
/// the fields they actually carry. Emit writers MUST tolerate
/// `None` returns — they correspond to "field not applicable
/// for this key type" (e.g. `src_port()` on an IP-only key).
///
/// # Implementing for custom keys
///
/// Custom [`crate::FlowExtractor::Key`] types should implement
/// this trait if they want to flow through CSV / NDJSON / Zeek /
/// EVE without fallback `Debug` formatting:
///
/// ```
/// use std::net::IpAddr;
/// use flowscope::KeyFields;
///
/// struct MyKey { src: IpAddr, dst: IpAddr }
///
/// impl KeyFields for MyKey {
/// fn src_ip(&self) -> Option<IpAddr> { Some(self.src) }
/// fn dest_ip(&self) -> Option<IpAddr> { Some(self.dst) }
/// }
/// ```
pub trait KeyFields {
/// Source IP for the flow.
fn src_ip(&self) -> Option<IpAddr> {
None
}
/// Source port (TCP/UDP).
fn src_port(&self) -> Option<u16> {
None
}
/// Destination IP for the flow.
fn dest_ip(&self) -> Option<IpAddr> {
None
}
/// Destination port (TCP/UDP).
fn dest_port(&self) -> Option<u16> {
None
}
/// L4 protocol as a static EVE-compatible label
/// (`"TCP"` / `"UDP"` / `"ICMP"` / `"ICMPv6"` / `"SCTP"`).
fn proto_str(&self) -> Option<&'static str> {
None
}
/// L4 protocol number per IANA assigned numbers
/// (TCP=6, UDP=17, ICMP=1, ICMPv6=58, SCTP=132). Used by
/// IPFIX-IE-keyed exporters that need the numeric ID
/// alongside the [`Self::proto_str`] label. Default `None`
/// — override on keys that carry an L4 protocol.
///
/// Issue #16 — needed by the
/// [`FlowRecord::from_key_fields`](crate::FlowRecord::from_key_fields)
/// generic constructor so emit writers can unify the
/// `write_event(FlowEnded)` → `write_flow_record` code path.
fn protocol_identifier(&self) -> Option<u8> {
None
}
/// Application-layer protocol label, e.g. `"http"` /
/// `"dns"` / `"tls"`. Default `None` — emit writers
/// typically thread the `parser_kind` from
/// [`crate::driver::SlotMessage`] instead. Override only on
/// custom keys that carry app-layer hints natively.
fn app_proto_str(&self) -> Option<&'static str> {
None
}
}
/// Anomaly classification accessor — implemented on
/// [`crate::AnomalyKind`] in this crate. Consumers writing
/// alert-shaped emit writers (EVE, future Suricata-shaped
/// outputs) constrain on this trait separately from
/// [`KeyFields`].
pub trait AnomalyFields {
/// EVE `anomaly.type` classification.
///
/// Suricata schema:
/// - `"stream"` — transport-layer state / reassembly anomalies
/// - `"decode"` — frame-integrity anomalies
/// - `"applayer"` — parser-driven application-layer anomalies
fn anomaly_type(&self) -> Option<&'static str> {
None
}
/// EVE `anomaly.event` — the stable slug, e.g.
/// `"ooo_segment"` or `"buffer_overflow"`.
/// [`crate::AnomalyKind`] implements via `short_kind()`.
fn anomaly_event(&self) -> Option<&'static str> {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
struct CustomKey {
src: IpAddr,
dst: IpAddr,
}
impl KeyFields for CustomKey {
fn src_ip(&self) -> Option<IpAddr> {
Some(self.src)
}
fn dest_ip(&self) -> Option<IpAddr> {
Some(self.dst)
}
}
#[test]
fn key_fields_default_impls_return_none() {
struct Empty;
impl KeyFields for Empty {}
let e = Empty;
assert!(e.src_ip().is_none());
assert!(e.src_port().is_none());
assert!(e.dest_ip().is_none());
assert!(e.dest_port().is_none());
assert!(e.proto_str().is_none());
assert!(e.app_proto_str().is_none());
}
#[test]
fn anomaly_fields_default_impls_return_none() {
struct Empty;
impl AnomalyFields for Empty {}
let e = Empty;
assert!(e.anomaly_type().is_none());
assert!(e.anomaly_event().is_none());
}
#[test]
fn custom_key_overrides_only_chosen_fields() {
let k = CustomKey {
src: "10.0.0.1".parse().unwrap(),
dst: "10.0.0.2".parse().unwrap(),
};
assert_eq!(k.src_ip(), Some("10.0.0.1".parse().unwrap()));
assert_eq!(k.dest_ip(), Some("10.0.0.2".parse().unwrap()));
// Not overridden — defaults to None.
assert!(k.src_port().is_none());
assert!(k.proto_str().is_none());
}
}