Docs.rs
  • netflow_parser-0.4.3
    • netflow_parser 0.4.3
    • Docs.rs crate page
    • MIT OR Apache-2.0
    • Links
    • Repository
    • crates.io
    • Source
    • Owners
    • mikemiles-dev
    • Dependencies
      • byteorder ^1.5.0 normal
      • mac_address ^1.1.5 normal
      • nom ^7.1.3 normal
      • nom-derive ^0.10.1 normal
      • serde ^1.0.166 normal
      • hex ^0.4.3 dev
      • insta ^1.30.0 dev
      • serde_json ^1.0.100 dev
      • tokio ^1.38.0 dev
      • tokio-macros ^0.2.0-alpha.6 dev
    • Versions
    • 12.46% of the crate is documented
  • Go to latest version
  • Platform
    • i686-pc-windows-msvc
    • i686-unknown-linux-gnu
    • x86_64-apple-darwin
    • x86_64-pc-windows-msvc
    • x86_64-unknown-linux-gnu
  • Feature flags
  • docs.rs
    • About docs.rs
    • Badges
    • Builds
    • Metadata
    • Shorthand URLs
    • Download
    • Rustdoc JSON
    • Build queue
    • Privacy policy
  • Rust
    • Rust website
    • The Book
    • Standard Library API Reference
    • Rust by Example
    • The Cargo Guide
    • Clippy Documentation

Crate netflow_parser

netflow_parser0.4.3

  • All Items

Sections

  • netflow_parser
    • Description
    • References
    • Example
    • Want Serialization such as JSON?
    • Filtering for a specific version
    • Netflow Common
    • Re-Exporting flows
    • V9/IPFix notes:
    • Features
    • Included Examples

Crate Items

  • Modules
  • Structs
  • Enums

Crates

  • netflow_parser
?
Settings

Crate netflow_parser

source ·
Expand description

§netflow_parser

§Description

A Netflow Parser library for Cisco V5, V7, V9, IPFIX written in Rust. Supports chaining of multple versions in the same stream. ({v5 packet}, {v7packet}, {v5packet}, {v9packet}, etc.)

§References

See: https://en.wikipedia.org/wiki/NetFlow

§Example

§V5

use netflow_parser::{NetflowParser, NetflowPacket};

let v5_packet = [0, 5, 2, 0, 3, 0, 4, 0, 5, 0, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7,];
match NetflowParser::default().parse_bytes(&v5_packet).first() {
    Some(NetflowPacket::V5(v5)) => assert_eq!(v5.header.version, 5),
    Some(NetflowPacket::Error(e)) => println!("{:?}", e),
    _ => (),
}

§Want Serialization such as JSON?

Structures fully support serialization. Below is an example using the serde_json macro:

use serde_json::json;
use netflow_parser::NetflowParser;

let v5_packet = [0, 5, 2, 0, 3, 0, 4, 0, 5, 0, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7,];
println!("{}", json!(NetflowParser::default().parse_bytes(&v5_packet)).to_string());
[{"V5":{"body":{"d_octets":66051,"d_pkts":101124105,"dst_addr":"4.5.6.7","dst_as":515,"dst_mask":5,"dst_port":1029,"first":67438087,"input":515,"last":134807553,"next_hop":"8.9.0.1","output":1029,"pad1":6,"pad2":1543,"protocol":"EGP","src_addr":"0.1.2.3","src_as":1,"src_mask":4,"src_port":515,"tcp_flags":7,"tos":9},"header":{"count":512,"engine_id":7,"engine_type":6,"flow_sequence":33752069,"sampling_interval":2057,"sys_up_time":50332672,"unix_nsecs":134807553,"unix_secs":83887623,"unix_time":{"nanos_since_epoch":134807553,"secs_since_epoch":83887623},"version":5}}}]

§Filtering for a specific version

use netflow_parser::{NetflowParser, NetflowPacket};

let v5_packet = [0, 5, 2, 0, 3, 0, 4, 0, 5, 0, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7,];
let parsed = NetflowParser::default().parse_bytes(&v5_packet);

let v5_parsed: Vec<NetflowPacket> = parsed.into_iter().filter(|p| p.is_v5()).collect();

§Netflow Common

For convenience we have included a NetflowCommon structure. This will allow you to use common Netflow fields without unpacking specific versions (fields like src_port, dst_port, etc.). If the packet flow does not have the matching field it will simply be left as None.

§Netflow Common fields:

ⓘ
src_addr: Option<IpAddr>,
dst_addr: Option<IpAddr>,
src_port: Option<u16>,
dst_port: Option<u16>,
protocol_number: Option<u8>,
protocol_type: Option<ProtocolTypes>,
first_seen: Option<u32>,
last_seen: Option<u32>,
use netflow_parser::{NetflowParser, NetflowPacket};

let v5_packet = [0, 5, 0, 1, 3, 0, 4, 0, 5, 0, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3,
    4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,
    2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7];
let netflow_common = NetflowParser::default()
                     .parse_bytes(&v5_packet)
                     .first()
                     .unwrap()
                     .as_netflow_common()
                     .unwrap();

for common_flow in netflow_common.flowsets.iter() {
    println!("Src Addr: {} Dst Addr: {}", common_flow.src_addr.unwrap(), common_flow.dst_addr.unwrap());
}

§Re-Exporting flows

Netflow Parser now supports parsed V5, V7, V9, IPFix can be re-exported back into bytes.

use netflow_parser::{NetflowParser, NetflowPacket};

let packet = [
    0, 5, 0, 1, 3, 0, 4, 0, 5, 0, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3,
    4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,
    2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7,
];
if let NetflowPacket::V5(v5) = NetflowParser::default()
    .parse_bytes(&packet)
    .first()
    .unwrap()
{
    assert_eq!(v5.to_be_bytes(), packet);
}

§V9/IPFix notes:

Parse the data (&[u8] as any other versions. The parser (NetflowParser) holds onto already parsed templates, so you can just send a header/data flowset combo and it will use the cached templates.) To see cached templates simply use the parser for the correct version (v9_parser for v9, ipfix_parser for IPFix.)

use netflow_parser::NetflowParser;
let parser = NetflowParser::default();
dbg!(parser.v9_parser.templates);
dbg!(parser.v9_parser.options_templates);

To access templates flowset of a processed V9/IPFix flowset you can find the flowsets attribute on the Parsed Record. In there you can find Templates, Option Templates, and Data Flowsets.

§Features

  • parse_unknown_fields - When enabled fields not listed in this library will attempt to be parsed as a Vec of bytes and the field_number listed. When disabled an error is thrown when attempting to parse those fields. Enabled by default.

§Included Examples

Some examples has been included mainly for those who want to use this parser to read from a Socket and parse netflow. In those cases with V9/IPFix it is best to create a new parser for each router. There are both single threaded and multi-threaded examples in the examples directory.

To run:

cargo run --example netflow_udp_listener_multi_threaded

or

cargo run --example netflow_udp_listener_single_threaded

or

cargo run --example netflow_udp_listener_tokio

Modules§

  • netflow_common
  • protocol
  • static_versions
  • variable_versions

Structs§

  • NetflowPacketError
  • NetflowParser

Enums§

  • NetflowPacket
    Enum of supported Netflow Versions
  • NetflowParseError

Results

enum
netflow_parser::NetflowPacket
Enum of supported Netflow Versions
struct
netflow_parser::NetflowPacketError
enum variant
netflow_parser::variable_versions::v9_lookup::ScopeFieldType::NetflowCache
struct
netflow_parser::NetflowParser
struct field
netflow_parser::variable_versions::v9::ScopeDataField::net_flow_cache
NetFlowCache
extern crate
netflow_parser
netflow_parser
method
netflow_parser::NetflowPacket::clone
method
netflow_parser::NetflowPacket::is_v5
method
netflow_parser::NetflowPacket::is_v7
method
netflow_parser::NetflowPacket::is_v9
method
netflow_parser::NetflowPacket::is_error
method
netflow_parser::NetflowPacket::is_ipfix
method
netflow_parser::NetflowPacket::fmt
method
netflow_parser::netflow_common::NetflowCommon::try_from
method
netflow_parser::NetflowPacket::serialize
method
netflow_parser::NetflowPacket::as_netflow_common
method
netflow_parser::NetflowPacket::clone
method
netflow_parser::NetflowParser::parse_bytes
Takes a Netflow packet slice and returns a vector of …