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.

§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 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);
Source

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.

Source

pub fn iter_packets_from_source<'a>( &'a mut self, source: K, data: &'a [u8], ) -> impl Iterator<Item = Result<NetflowPacket, NetflowError>> + 'a
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 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.

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_stats(&self) -> Vec<(&K, ParserCacheStats)>
where K: Clone,

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(&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.

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.

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> Freeze for RouterScopedParser<K>

§

impl<K> !RefUnwindSafe 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>
where K: Unpin,

§

impl<K> !UnwindSafe 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.