Skip to main content

RouterScopedParser

Struct RouterScopedParser 

Source
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.

Uses an LRU cache internally for O(1) eviction of the least-recently-used source when at capacity, preventing DoS attacks via source address flooding.

§Type Parameter

  • K - The key type used to identify sources. Commonly SocketAddr for UDP sources, but can be any hashable type (e.g., String for named sources, u32 for 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>

Source

pub fn new() -> Self

Create a new scoped parser with default configuration.

Each new source will get a parser with default settings.

Source

pub fn try_with_builder( builder: NetflowParserBuilder, ) -> Result<Self, ConfigError>

Create a new scoped parser with a custom parser builder.

Validates the builder configuration eagerly. Returns an error if the builder configuration is invalid (e.g., zero cache size).

§Errors

Returns ConfigError if the builder configuration is invalid.

§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>::try_with_builder(builder)
    .expect("valid config");
Source

pub fn with_max_sources(self, max: usize) -> Result<Self, ConfigError>

Set the maximum number of sources to track.

When at capacity and a new source arrives, the least-recently-used source is automatically evicted to make room. Default: 10,000.

§Errors

Returns ConfigError::InvalidMaxSources if max is 0.

Source

pub fn with_max_sources_and_reporter<F>( self, max: usize, reporter: &mut F, ) -> Result<Self, ConfigError>

Set the maximum number of sources and report every source removed by reducing the capacity.

Reporting completes synchronously before this method returns. Reporter errors and panics are isolated and counted in source_removal_metrics.

§Errors

Returns ConfigError::InvalidMaxSources if max is 0.

Source

pub fn parse_from_source(&mut self, source: K, data: &[u8]) -> ParseResult
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. When at capacity, the least-recently-used source is automatically evicted by the LRU cache.

§Arguments
  • source - The source identifier (e.g., SocketAddr)
  • data - The raw NetFlow packet data
§Returns

A ParseResult containing successfully parsed packets and an optional error.

Source

pub fn parse_from_source_with_reporter<F>( &mut self, source: K, data: &[u8], reporter: &mut F, ) -> ParseResult

Parse data and synchronously report any source displaced by capacity pressure.

Source

pub fn iter_packets_from_source<'a>( &'a mut self, source: K, data: &'a [u8], ) -> Result<impl Iterator<Item = Result<NetflowPacket, NetflowError>> + 'a + use<'a, K>, NetflowError>
where K: Clone,

Parse NetFlow data from a source using the iterator API.

This is more efficient than parse_from_source when you don’t need all packets in a Vec.

§Arguments
  • source - The source identifier
  • data - The raw NetFlow packet data
§Returns

An iterator over parsed NetFlow packets.

Source

pub fn iter_packets_from_source_with_reporter<'a, F>( &'a mut self, source: K, data: &'a [u8], reporter: &mut F, ) -> Result<impl Iterator<Item = Result<NetflowPacket, NetflowError>> + 'a + use<'a, K, F>, NetflowError>

Iterate over packets and synchronously report any source displaced by capacity pressure before the iterator is returned.

Source

pub fn prune_idle_sources(&mut self, older_than: Duration) -> usize
where K: Clone,

Remove sources not seen within the given duration.

Returns the number of sources pruned.

Source

pub fn prune_idle_sources_with_reporter<F>( &mut self, older_than: Duration, reporter: &mut F, ) -> usize

Remove idle sources and synchronously report every removal.

Source

pub fn source_removal_metrics(&self) -> SourceRemovalMetrics

Return aggregate implicit source-removal metrics.

Source

pub fn get_source_info(&mut self, source: &K) -> Option<ParserCacheInfo>

Get statistics for a specific source’s template cache.

Returns None if the source hasn’t sent any packets yet.

Source

pub fn source_count(&self) -> usize

Get the number of registered sources.

Source

pub fn sources(&self) -> Vec<&K>

List all registered source identifiers.

Source

pub fn all_info(&self) -> Vec<(&K, ParserCacheInfo)>

Get statistics for all sources.

Source

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.

Source

pub fn clear_all_templates(&mut self)

Clear templates for all sources.

Source

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.

Source

pub fn get_parser(&mut 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. Note: This does not promote the entry in the LRU cache.

Source

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. Note: This does not promote the entry in the LRU cache.

Trait Implementations§

Source§

impl<K: Debug + Hash + Eq> Debug for RouterScopedParser<K>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: Hash + Eq> Default for RouterScopedParser<K>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<K> !RefUnwindSafe for RouterScopedParser<K>

§

impl<K> !UnwindSafe for RouterScopedParser<K>

§

impl<K> Freeze for RouterScopedParser<K>

§

impl<K> Send for RouterScopedParser<K>
where K: Send,

§

impl<K> Sync for RouterScopedParser<K>
where K: Sync,

§

impl<K> Unpin for RouterScopedParser<K>

§

impl<K> UnsafeUnpin for RouterScopedParser<K>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.