|
50 | 50 | //!
|
51 | 51 | //! ## Netflow Common
|
52 | 52 | //!
|
53 |
| -//! For convenience we have included a `NetflowCommon` structure. This will allow you to use common |
54 |
| -//! Netflow fields without unpacking specific versions (fields like `src_port`, `dst_port`, etc.). If the |
55 |
| -//! packet flow does not have the matching field it will simply be left as `None`. |
56 |
| -//! |
57 |
| -//! ### Netflow Common fields: |
58 |
| -//! ```ignore |
59 |
| -//! src_addr: Option<IpAddr>, |
60 |
| -//! dst_addr: Option<IpAddr>, |
61 |
| -//! src_port: Option<u16>, |
62 |
| -//! dst_port: Option<u16>, |
63 |
| -//! protocol_number: Option<u8>, |
64 |
| -//! protocol_type: Option<ProtocolTypes>, |
65 |
| -//! first_seen: Option<u32>, |
66 |
| -//! last_seen: Option<u32>, |
| 53 | +//! For convenience we have included a `NetflowCommon` and `NetflowCommonFlowSet` structure. |
| 54 | +//! This will allow you to use common fields without unpacking values from specific versions. |
| 55 | +//! If the packet flow does not have the matching field it will simply be left as `None`. |
| 56 | +//! |
| 57 | +//! ### NetflowCommon and NetflowCommonFlowSet Struct: |
| 58 | +//! ```rust |
| 59 | +//! use std::net::IpAddr; |
| 60 | +//! use netflow_parser::protocol::ProtocolTypes; |
| 61 | +//! |
| 62 | +//! #[derive(Debug, Default)] |
| 63 | +//! pub struct NetflowCommon { |
| 64 | +//! pub version: u16, |
| 65 | +//! pub timestamp: u32, |
| 66 | +//! pub flowsets: Vec<NetflowCommonFlowSet>, |
| 67 | +//! } |
| 68 | +//! |
| 69 | +//! #[derive(Debug, Default)] |
| 70 | +//! struct NetflowCommonFlowSet { |
| 71 | +//! src_addr: Option<IpAddr>, |
| 72 | +//! dst_addr: Option<IpAddr>, |
| 73 | +//! src_port: Option<u16>, |
| 74 | +//! dst_port: Option<u16>, |
| 75 | +//! protocol_number: Option<u8>, |
| 76 | +//! protocol_type: Option<ProtocolTypes>, |
| 77 | +//! first_seen: Option<u32>, |
| 78 | +//! last_seen: Option<u32>, |
| 79 | +//! } |
67 | 80 | //! ```
|
68 | 81 | //!
|
| 82 | +//! ### Converting NetflowPacket to NetflowCommon |
| 83 | +//! |
69 | 84 | //! ```rust
|
70 | 85 | //! use netflow_parser::{NetflowParser, NetflowPacket};
|
71 | 86 | //!
|
|
84 | 99 | //! }
|
85 | 100 | //! ```
|
86 | 101 | //!
|
| 102 | +//! ### Alternative if you just want to gather all flowsets from all packets into a flattened vector of NetflowCommonFlowSet: |
| 103 | +//! |
| 104 | +//! ```rust |
| 105 | +//! use netflow_parser::{NetflowParser, NetflowPacket}; |
| 106 | +//! |
| 107 | +//! 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, |
| 108 | +//! 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, |
| 109 | +//! 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7]; |
| 110 | +//! let netflow_common_flowsets = NetflowParser::default() |
| 111 | +//! .parse_bytes_as_netflow_common_flowsets(&v5_packet); |
| 112 | +//! |
| 113 | +//! println!("Flowsets: {:?}", netflow_common_flowsets); |
| 114 | +//! ``` |
| 115 | +//! |
87 | 116 | //! ## Re-Exporting flows
|
88 | 117 | //! Netflow Parser now supports parsed V5, V7, V9, IPFix can be re-exported back into bytes.
|
89 | 118 | //! ```rust
|
@@ -139,7 +168,7 @@ pub mod static_versions;
|
139 | 168 | mod tests;
|
140 | 169 | pub mod variable_versions;
|
141 | 170 |
|
142 |
| -use crate::netflow_common::{NetflowCommon, NetflowCommonError}; |
| 171 | +use crate::netflow_common::{NetflowCommon, NetflowCommonError, NetflowCommonFlowSet}; |
143 | 172 |
|
144 | 173 | use static_versions::{v5::V5, v7::V7};
|
145 | 174 | use variable_versions::ipfix::{IPFix, IPFixParser};
|
@@ -184,7 +213,6 @@ impl NetflowPacket {
|
184 | 213 | pub fn is_error(&self) -> bool {
|
185 | 214 | matches!(self, Self::Error(_v))
|
186 | 215 | }
|
187 |
| - |
188 | 216 | pub fn as_netflow_common(&self) -> Result<NetflowCommon, NetflowCommonError> {
|
189 | 217 | self.try_into()
|
190 | 218 | }
|
@@ -277,6 +305,19 @@ impl NetflowParser {
|
277 | 305 | })
|
278 | 306 | }
|
279 | 307 |
|
| 308 | + /// Takes a Netflow packet slice and returns a vector of Parsed NetflowCommonFlowSet |
| 309 | + #[inline] |
| 310 | + pub fn parse_bytes_as_netflow_common_flowsets( |
| 311 | + &mut self, |
| 312 | + packet: &[u8], |
| 313 | + ) -> Vec<NetflowCommonFlowSet> { |
| 314 | + let netflow_packets = self.parse_bytes(packet); |
| 315 | + netflow_packets |
| 316 | + .iter() |
| 317 | + .flat_map(|n| n.as_netflow_common().unwrap_or_default().flowsets) |
| 318 | + .collect() |
| 319 | + } |
| 320 | + |
280 | 321 | /// Checks the first u16 of the packet to determine the version. Parses the packet based on the version.
|
281 | 322 | /// If the version is unknown it returns an error. If the packet is incomplete it returns an error.
|
282 | 323 | /// If the packet is parsed successfully it returns the parsed Netflow packet and the remaining bytes.
|
|
0 commit comments