From e41159c954364247e9ab84379584d1255e0e5edf Mon Sep 17 00:00:00 2001 From: mikemiles-dev Date: Mon, 17 Jun 2024 00:23:33 -0500 Subject: [PATCH] Fixed exporting 3 byte DataNumbers --- Cargo.toml | 3 ++- RELEASES.md | 3 +++ SECURITY.md | 1 + src/variable_versions/common.rs | 33 +++++++++++++++++++++++++++++---- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index aaff480..f6aa4c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "netflow_parser" description = "Parser for Netflow Cisco V5, V7, V9, IPFIX" -version = "0.3.4" +version = "0.3.5" edition = "2021" author = "michael.mileusnich@gmail.com" license = "MIT OR Apache-2.0" @@ -10,6 +10,7 @@ readme = "README.md" repository = "https://github.com/mikemiles-dev/netflow_parser/" [dependencies] +byteorder = "1.5.0" nom = "7.1.3" nom-derive = "0.10.1" mac_address = "1.1.5" diff --git a/RELEASES.md b/RELEASES.md index 51c569a..fe6b3e4 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,3 +1,6 @@ +# 0.3.5 + * 3 Byte Data Numbers now correctly converts back to be_bytes. + # 0.3.4 * Added 3 byte DataNumber support. diff --git a/SECURITY.md b/SECURITY.md index 8c006b9..c81289d 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -4,6 +4,7 @@ | Version | Supported | | ------- | ------------------ | +| 0.3.5 | :white_check_mark: | | 0.3.4 | :white_check_mark: | | 0.3.3 | :white_check_mark: | | 0.3.2 | :white_check_mark: | diff --git a/src/variable_versions/common.rs b/src/variable_versions/common.rs index 3590f22..5838dd1 100644 --- a/src/variable_versions/common.rs +++ b/src/variable_versions/common.rs @@ -1,9 +1,9 @@ use crate::protocol::ProtocolTypes; +use byteorder::{BigEndian, WriteBytesExt}; use nom::bytes::complete::take; use nom::error::{Error as NomError, ErrorKind}; -use nom::number::complete::{be_i24, be_u24}; -use nom::number::complete::{be_u128, be_u32}; +use nom::number::complete::{be_i24, be_u128, be_u24, be_u32}; use nom::Err as NomErr; use nom::IResult; use nom_derive::*; @@ -19,6 +19,8 @@ use std::time::Duration; pub enum DataNumber { U8(u8), U16(u16), + U24(u32), + I24(i32), U32(u32), U64(u64), U128(u128), @@ -47,8 +49,8 @@ impl DataNumber { match field_length { 1 if !signed => Ok(u8::parse(i)?).map(|(i, j)| (i, Self::U8(j))), 2 if !signed => Ok(u16::parse(i)?).map(|(i, j)| (i, Self::U16(j))), - 3 if !signed => Ok(be_u24(i).map(|(i, j)| (i, Self::U32(j)))?), - 3 if signed => Ok(be_i24(i).map(|(i, j)| (i, Self::I32(j)))?), + 3 if !signed => Ok(be_u24(i).map(|(i, j)| (i, Self::U24(j)))?), + 3 if signed => Ok(be_i24(i).map(|(i, j)| (i, Self::I24(j)))?), 4 if signed => Ok(i32::parse(i)?).map(|(i, j)| (i, Self::I32(j))), 4 if !signed => Ok(u32::parse(i)?).map(|(i, j)| (i, Self::U32(j))), 8 if !signed => Ok(u64::parse(i)?).map(|(i, j)| (i, Self::U64(j))), @@ -61,6 +63,16 @@ impl DataNumber { match self { DataNumber::U8(n) => n.to_be_bytes().to_vec(), DataNumber::U16(n) => n.to_be_bytes().to_vec(), + DataNumber::U24(n) => { + let mut wtr = Vec::new(); + wtr.write_u24::(*n).unwrap(); + wtr + } + DataNumber::I24(n) => { + let mut wtr = Vec::new(); + wtr.write_i24::(*n).unwrap(); + wtr + } DataNumber::U32(n) => n.to_be_bytes().to_vec(), DataNumber::U64(n) => n.to_be_bytes().to_vec(), DataNumber::U128(n) => n.to_be_bytes().to_vec(), @@ -167,6 +179,8 @@ impl From for usize { fn from(val: DataNumber) -> Self { match val { DataNumber::U8(i) => i as usize, + DataNumber::I24(i) => i as usize, + DataNumber::U24(i) => i as usize, DataNumber::U32(i) => i as usize, DataNumber::I32(i) => i as usize, DataNumber::U16(i) => i as usize, @@ -222,3 +236,14 @@ pub enum FieldDataType { ProtocolType, Unknown, } + +#[cfg(test)] +mod common_tests { + #[test] + fn it_tests_3_byte_data_number_exports() { + use super::DataNumber; + let data = DataNumber::parse(&[1, 246, 118], 3, false).unwrap().1; + println!("{:?}", data); + assert_eq!(data.to_be_bytes(), vec![1, 246, 118]); + } +}