use flowscope::driver::{DriverBuilder, SlotHandle};
use flowscope::extract::{FiveTuple, FiveTupleKey};
use crate::protocol::{Dispatch, MessageProtocol, Protocol, ProtocolInitError};
impl MessageProtocol for Dns {}
#[derive(Debug, Clone, Copy)]
pub struct Dns;
impl Protocol for Dns {
type Message = flowscope::dns::DnsMessage;
const NAME: &'static str = flowscope::dns::PARSER_KIND_UDP;
fn dispatch() -> Dispatch {
Dispatch::Udp(vec![53])
}
fn register(
builder: &mut DriverBuilder<FiveTuple>,
) -> Result<SlotHandle<Self::Message, FiveTupleKey>, ProtocolInitError> {
let ports = match Self::dispatch() {
Dispatch::Udp(p) => p,
_ => unreachable!("Dns::dispatch is Dispatch::Udp by construction"),
};
Ok(builder.datagram_on_ports(flowscope::dns::DnsUdpParser::with_correlation(), ports))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dispatch_is_udp_53() {
match <Dns as Protocol>::dispatch() {
Dispatch::Udp(ports) => assert_eq!(ports, vec![53]),
other => panic!("expected Dispatch::Udp([53]), got {other:?}"),
}
}
#[test]
fn name_matches_flowscope_parser_kind() {
assert_eq!(<Dns as Protocol>::NAME, flowscope::dns::PARSER_KIND_UDP);
}
#[test]
fn register_returns_handle() {
let mut b = flowscope::driver::Driver::builder(FiveTuple::bidirectional());
let h = <Dns as Protocol>::register(&mut b);
assert!(h.is_ok());
}
}