use crate::Timestamp;
use crate::event::{AnomalyKind, EndReason, FlowSide, FlowStats};
use crate::extractor::{L4Proto, TcpInfo};
use crate::history::HistoryString;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum Event<K, M> {
FlowStarted {
key: K,
ts: Timestamp,
l4: Option<L4Proto>,
},
FlowEstablished {
key: K,
ts: Timestamp,
l4: Option<L4Proto>,
},
FlowPacket {
key: K,
side: FlowSide,
len: usize,
ts: Timestamp,
tcp: Option<TcpInfo>,
frame: Option<Vec<u8>>,
},
FlowEnded {
key: K,
reason: EndReason,
stats: FlowStats,
history: HistoryString,
l4: Option<L4Proto>,
ts: Timestamp,
},
FlowTick {
key: K,
stats: FlowStats,
ts: Timestamp,
},
Message {
key: K,
side: FlowSide,
message: M,
ts: Timestamp,
parser_kind: &'static str,
},
ParserClosed {
key: K,
parser_kind: &'static str,
reason: EndReason,
ts: Timestamp,
},
FlowAnomaly {
key: K,
kind: AnomalyKind,
ts: Timestamp,
},
TrackerAnomaly { kind: AnomalyKind, ts: Timestamp },
}
impl<K, M> Event<K, M> {
pub fn key(&self) -> Option<&K> {
match self {
Event::FlowStarted { key, .. }
| Event::FlowEstablished { key, .. }
| Event::FlowPacket { key, .. }
| Event::FlowEnded { key, .. }
| Event::FlowTick { key, .. }
| Event::Message { key, .. }
| Event::ParserClosed { key, .. }
| Event::FlowAnomaly { key, .. } => Some(key),
Event::TrackerAnomaly { .. } => None,
}
}
pub fn timestamp(&self) -> Timestamp {
match self {
Event::FlowStarted { ts, .. }
| Event::FlowEstablished { ts, .. }
| Event::FlowPacket { ts, .. }
| Event::FlowEnded { ts, .. }
| Event::FlowTick { ts, .. }
| Event::Message { ts, .. }
| Event::ParserClosed { ts, .. }
| Event::FlowAnomaly { ts, .. }
| Event::TrackerAnomaly { ts, .. } => *ts,
}
}
pub fn parser_kind(&self) -> Option<&'static str> {
match self {
Event::Message { parser_kind, .. } | Event::ParserClosed { parser_kind, .. } => {
Some(*parser_kind)
}
_ => None,
}
}
pub fn anomaly_kind(&self) -> Option<&AnomalyKind> {
match self {
Event::FlowAnomaly { kind, .. } | Event::TrackerAnomaly { kind, .. } => Some(kind),
_ => None,
}
}
pub fn is_flow_event(&self) -> bool {
matches!(
self,
Event::FlowStarted { .. }
| Event::FlowEstablished { .. }
| Event::FlowPacket { .. }
| Event::FlowEnded { .. }
| Event::FlowTick { .. }
)
}
pub fn is_parser_event(&self) -> bool {
matches!(self, Event::Message { .. } | Event::ParserClosed { .. })
}
}