pub struct RouterScopedParser<K: Hash + Eq> { /* private fields */ }Expand description
A parser that maintains separate template caches for each NetFlow source.
This is the recommended pattern for multi-source deployments where different routers may use the same template IDs for different field definitions. By keeping separate parser instances per source, templates are properly isolated.
§Type Parameter
K- The key type used to identify sources. CommonlySocketAddrfor UDP sources, but can be any hashable type (e.g.,Stringfor named sources,u32for observation domain IDs, etc.)
§Examples
§Basic usage with SocketAddr
use netflow_parser::RouterScopedParser;
use std::net::SocketAddr;
let mut scoped_parser = RouterScopedParser::<SocketAddr>::new();
// Parse packet from source 1
let source1 = "192.168.1.1:2055".parse().unwrap();
let data1 = vec![/* netflow data */];
let packets = scoped_parser.parse_from_source(source1, &data1);
// Parse packet from source 2 (separate template cache)
let source2 = "192.168.1.2:2055".parse().unwrap();
let data2 = vec![/* netflow data */];
let packets = scoped_parser.parse_from_source(source2, &data2);§Custom source keys
use netflow_parser::RouterScopedParser;
// Use string identifiers for sources
let mut scoped_parser = RouterScopedParser::<String>::new();
let packets = scoped_parser.parse_from_source(
"router-nyc-01".to_string(),
&data
);Implementations§
Source§impl<K: Hash + Eq> RouterScopedParser<K>
impl<K: Hash + Eq> RouterScopedParser<K>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new scoped parser with default configuration.
Each new source will get a parser with default settings.
Sourcepub fn with_builder(builder: NetflowParserBuilder) -> Self
pub fn with_builder(builder: NetflowParserBuilder) -> Self
Create a new scoped parser with a custom parser builder.
The builder will be used to create new parser instances for each source.
§Examples
use netflow_parser::{RouterScopedParser, NetflowParser};
use netflow_parser::variable_versions::ttl::TtlConfig;
use std::time::Duration;
use std::net::SocketAddr;
let builder = NetflowParser::builder()
.with_cache_size(5000)
.with_ttl(TtlConfig::new(Duration::from_secs(3600)));
let scoped_parser = RouterScopedParser::<SocketAddr>::with_builder(builder);Sourcepub fn parse_from_source(
&mut self,
source: K,
data: &[u8],
) -> Result<Vec<NetflowPacket>, NetflowError>where
K: Clone,
pub fn parse_from_source(
&mut self,
source: K,
data: &[u8],
) -> Result<Vec<NetflowPacket>, NetflowError>where
K: Clone,
Parse NetFlow data from a specific source.
This will automatically create a new parser instance for new sources, or reuse the existing parser for known sources.
§Arguments
source- The source identifier (e.g., SocketAddr)data- The raw NetFlow packet data
§Returns
A vector of parsed NetFlow packets from the given data.
Sourcepub fn iter_packets_from_source<'a>(
&'a mut self,
source: K,
data: &'a [u8],
) -> impl Iterator<Item = Result<NetflowPacket, NetflowError>> + 'awhere
K: Clone,
pub fn iter_packets_from_source<'a>(
&'a mut self,
source: K,
data: &'a [u8],
) -> impl Iterator<Item = Result<NetflowPacket, NetflowError>> + 'awhere
K: Clone,
Sourcepub fn get_source_stats(&self, source: &K) -> Option<ParserCacheStats>
pub fn get_source_stats(&self, source: &K) -> Option<ParserCacheStats>
Get statistics for a specific source’s template cache.
Returns None if the source hasn’t sent any packets yet.
Sourcepub fn source_count(&self) -> usize
pub fn source_count(&self) -> usize
Get the number of registered sources.
Sourcepub fn all_stats(&self) -> Vec<(&K, ParserCacheStats)>where
K: Clone,
pub fn all_stats(&self) -> Vec<(&K, ParserCacheStats)>where
K: Clone,
Get statistics for all sources.
Sourcepub fn clear_source_templates(&mut self, source: &K)
pub fn clear_source_templates(&mut self, source: &K)
Clear templates for a specific source.
This is useful for testing or when you need to force template re-learning for a specific source.
Sourcepub fn clear_all_templates(&mut self)
pub fn clear_all_templates(&mut self)
Clear templates for all sources.
Sourcepub fn remove_source(&mut self, source: &K) -> Option<NetflowParser>
pub fn remove_source(&mut self, source: &K) -> Option<NetflowParser>
Remove a source and its parser.
This is useful for cleaning up parsers for sources that are no longer active.
Sourcepub fn get_parser(&self, source: &K) -> Option<&NetflowParser>
pub fn get_parser(&self, source: &K) -> Option<&NetflowParser>
Get a reference to a specific source’s parser.
Returns None if the source hasn’t sent any packets yet.
Sourcepub fn get_parser_mut(&mut self, source: &K) -> Option<&mut NetflowParser>
pub fn get_parser_mut(&mut self, source: &K) -> Option<&mut NetflowParser>
Get a mutable reference to a specific source’s parser.
Returns None if the source hasn’t sent any packets yet.