Skip to content

Commit

Permalink
Added NetflowCommon struct and doc. (#78)
Browse files Browse the repository at this point in the history
* Added NetflowCommon.

---------

Co-authored-by: mikemiles-dev <[email protected]>
  • Loading branch information
mikemiles-dev and mikemiles-dev authored Sep 16, 2024
1 parent cead909 commit 045cfdd
Show file tree
Hide file tree
Showing 28 changed files with 770 additions and 146 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "netflow_parser"
description = "Parser for Netflow Cisco V5, V7, V9, IPFIX"
version = "0.4.0"
version = "0.4.1"
edition = "2021"
author = "[email protected]"
license = "MIT OR Apache-2.0"
Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,42 @@ 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>,
```

```rust
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.
Expand Down
5 changes: 5 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.4.1
* Added NetflowCommon structure. This acts as a helper for common Netflow Fields (like src_ip, src_port, etc).
* V5, V7 SysUpTime, First, Last times now u32 from Duration.
* IPFix export time u32 from Duration.

# 0.4.0
* NetflowPacketResult now simply NetflowPacket.
* General parser cleanup and removal of uneeded code.
Expand Down
43 changes: 43 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,42 @@
//! 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:
//! ```ignore
//! 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>,
//! ```
//!
//! ```rust
//! 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.
//! ```rust
Expand Down Expand Up @@ -97,11 +133,14 @@
//!
//! ```cargo run --example netflow_udp_listener_tokio```
pub mod netflow_common;
pub mod protocol;
pub mod static_versions;
mod tests;
pub mod variable_versions;

use crate::netflow_common::{NetflowCommon, NetflowCommonError};

use static_versions::{v5::V5, v7::V7};
use variable_versions::ipfix::{IPFix, IPFixParser};
use variable_versions::v9::{V9Parser, V9};
Expand Down Expand Up @@ -145,6 +184,10 @@ impl NetflowPacket {
pub fn is_error(&self) -> bool {
matches!(self, Self::Error(_v))
}

pub fn as_netflow_common(&self) -> Result<NetflowCommon, NetflowCommonError> {
self.try_into()
}
}

#[derive(Nom)]
Expand Down
Loading

0 comments on commit 045cfdd

Please sign in to comment.