use flowscope::driver::{DriverBuilder, SlotHandle};
use flowscope::extract::{FiveTuple, FiveTupleKey};
use crate::protocol::{Dispatch, MessageProtocol, Protocol, ProtocolInitError};
#[derive(Debug, Clone, Copy)]
pub struct Stun;
impl MessageProtocol for Stun {}
impl Protocol for Stun {
type Message = flowscope::stun::StunMessage;
const NAME: &'static str = "stun";
fn dispatch() -> Dispatch {
Dispatch::Udp(vec![flowscope::stun::STUN_PORT])
}
fn register(
builder: &mut DriverBuilder<FiveTuple>,
) -> Result<SlotHandle<Self::Message, FiveTupleKey>, ProtocolInitError> {
let ports = match Self::dispatch() {
Dispatch::Udp(p) => p,
_ => unreachable!("Stun::dispatch is Dispatch::Udp by construction"),
};
Ok(builder.datagram_on_ports(flowscope::stun::StunParser::new(), ports))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dispatch_targets_udp_3478() {
match Stun::dispatch() {
Dispatch::Udp(ports) => assert_eq!(ports, vec![3478]),
other => panic!("expected Dispatch::Udp, got {other:?}"),
}
}
#[test]
fn name_matches_flowscope_kind() {
assert_eq!(Stun::NAME, flowscope::stun::PARSER_KIND);
}
}