session only.Expand description
Pluggable L7 message parsers.
Two trait families:
-
SessionParser— for stream-based protocols (HTTP/1, TLS, DNS-over-TCP). One parser per session; receives bytes viafeed_initiator/feed_responder; returns aVecof typed messages every call. Pair withnetring::SessionStreamto get an async stream of L7 events. -
DatagramParser— for packet-based protocols (DNS-over-UDP, syslog, NTP, SNMP). Receives one L4 payload at a time. Pair withnetring::DatagramStream.
Both trait shapes return owned Vec<Message> rather than borrowed
iterators or SmallVec to keep the public API stable across
versions of smallvec etc. The per-call allocation is amortized
across many bytes worth of work.
§SessionParser vs Reassembler
crate::Reassembler is the lower-level hook: one instance per
(flow, side), receives raw TCP segments, callback-driven via
a user-supplied handler. SessionParser is the higher-level
abstraction: one instance per flow, two feed_* methods,
returns typed messages directly. Pick whichever fits your
integration:
| Concern | Reassembler | SessionParser |
|---|---|---|
| Granularity | per (flow, side) | per flow |
| Output | callback (Handler) | iterator/Stream of messages |
| Cross-direction state | painful | natural |
| UDP support | no | use DatagramParser |
§Example
use flowscope::{FlowSide, SessionParser};
#[derive(Default, Clone)]
struct LineParser {
init_buf: Vec<u8>,
resp_buf: Vec<u8>,
}
impl SessionParser for LineParser {
type Message = (FlowSide, String);
fn feed_initiator(&mut self, bytes: &[u8]) -> Vec<Self::Message> {
feed(&mut self.init_buf, bytes, FlowSide::Initiator)
}
fn feed_responder(&mut self, bytes: &[u8]) -> Vec<Self::Message> {
feed(&mut self.resp_buf, bytes, FlowSide::Responder)
}
}
fn feed(buf: &mut Vec<u8>, bytes: &[u8], side: FlowSide) -> Vec<(FlowSide, String)> {
buf.extend_from_slice(bytes);
let mut out = Vec::new();
while let Some(nl) = buf.iter().position(|&b| b == b'\n') {
let line = String::from_utf8_lossy(&buf[..nl]).into_owned();
out.push((side, line));
buf.drain(..=nl);
}
out
}Enums§
- Session
Event - Output of a
SessionParserorDatagramParser-backed stream.
Traits§
- Datagram
Parser - Parses a packet-oriented L7 protocol. One instance per flow; receives one L4 payload at a time along with which side sent it.
- Datagram
Parser Factory - Builds a fresh
DatagramParserper session. - Session
Parser - Parses a stream-oriented L7 protocol session. One instance per flow; both directions feed through the same parser, allowing state to interleave.
- Session
Parser Factory - Builds a fresh
SessionParserper session. Modeled oncrate::ReassemblerFactory.