|
1 | 1 | use crate::protocol::ProtocolTypes; |
2 | | - |
3 | 2 | use byteorder::{BigEndian, WriteBytesExt}; |
4 | | -use nom::Err as NomErr; |
5 | | -use nom::IResult; |
6 | | -use nom::bytes::complete::take; |
7 | | -use nom::error::{Error as NomError, ErrorKind}; |
8 | | -use nom::number::complete::{be_i24, be_u24, be_u32, be_u128}; |
9 | | -use nom_derive::*; |
| 3 | +use nom::{ |
| 4 | + Err as NomErr, IResult, |
| 5 | + bytes::complete::take, |
| 6 | + error::{Error as NomError, ErrorKind}, |
| 7 | + number::complete::{be_i24, be_u24, be_u32, be_u128}, |
| 8 | +}; |
| 9 | +use nom_derive::Parse; |
10 | 10 | use serde::Serialize; |
11 | | - |
12 | | -use std::convert::Into; |
13 | | -use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; |
14 | | -use std::time::Duration; |
| 11 | +use std::{ |
| 12 | + net::{IpAddr, Ipv4Addr, Ipv6Addr}, |
| 13 | + time::Duration, |
| 14 | +}; |
15 | 15 |
|
16 | 16 | macro_rules! impl_try_from { |
17 | 17 | ($($t:ty => $v:ident),*; $($s:ty => $sv:ident),*) => { |
@@ -173,26 +173,6 @@ impl DataNumber { |
173 | 173 | } |
174 | 174 | } |
175 | 175 |
|
176 | | -/// Convert into usize, mainly for serialization purposes |
177 | | -impl From<DataNumber> for usize { |
178 | | - fn from(val: DataNumber) -> Self { |
179 | | - match val { |
180 | | - DataNumber::U8(i) => usize::from(i), |
181 | | - DataNumber::I8(i) => i as usize, |
182 | | - DataNumber::U16(i) => i as usize, |
183 | | - DataNumber::I16(i) => i as usize, |
184 | | - DataNumber::I24(i) => i as usize, |
185 | | - DataNumber::U24(i) => i as usize, |
186 | | - DataNumber::U32(i) => i as usize, |
187 | | - DataNumber::I32(i) => i as usize, |
188 | | - DataNumber::U64(i) => i as usize, |
189 | | - DataNumber::I64(i) => i as usize, |
190 | | - DataNumber::U128(i) => i as usize, |
191 | | - DataNumber::I128(i) => i as usize, |
192 | | - } |
193 | | - } |
194 | | -} |
195 | | - |
196 | 176 | #[derive(Debug, PartialEq, PartialOrd, Clone, Serialize)] |
197 | 177 | pub struct ApplicationId { |
198 | 178 | pub classification_engine_id: u8, |
@@ -240,6 +220,12 @@ impl FieldValue { |
240 | 220 | } |
241 | 221 | } |
242 | 222 |
|
| 223 | + fn make_ntp_time_with_unit(seconds: u32, fraction: u32, unit: u64) -> Duration { |
| 224 | + Duration::from_secs(u64::from(seconds)).saturating_add(Duration::from_micros( |
| 225 | + ((u64::from(fraction)).saturating_mul(unit)) >> 32, |
| 226 | + )) |
| 227 | + } |
| 228 | + |
243 | 229 | pub fn from_field_type( |
244 | 230 | remaining: &[u8], |
245 | 231 | field_type: FieldDataType, |
@@ -297,40 +283,51 @@ impl FieldValue { |
297 | 283 | (i, FieldValue::MacAddr(mac_addr)) |
298 | 284 | } |
299 | 285 | FieldDataType::DurationSeconds => { |
300 | | - let (i, data_number) = DataNumber::parse(remaining, field_length, false)?; |
301 | | - ( |
302 | | - i, |
303 | | - FieldValue::Duration(Duration::from_secs( |
304 | | - <DataNumber as Into<usize>>::into(data_number) as u64, |
305 | | - )), |
306 | | - ) |
| 286 | + let (i, value) = match field_length { |
| 287 | + 4 => { |
| 288 | + let (i, seconds) = u32::parse_be(remaining)?; |
| 289 | + let dur = Duration::from_secs(seconds.into()); |
| 290 | + (i, FieldValue::Duration(dur)) |
| 291 | + } |
| 292 | + 8 => { |
| 293 | + let (i, seconds) = u64::parse_be(remaining)?; |
| 294 | + let dur = Duration::from_secs(seconds); |
| 295 | + (i, FieldValue::Duration(dur)) |
| 296 | + } |
| 297 | + _ => { |
| 298 | + return Err(NomErr::Error(NomError::new(remaining, ErrorKind::Fail))); |
| 299 | + } |
| 300 | + }; |
| 301 | + (i, value) |
307 | 302 | } |
308 | 303 | FieldDataType::DurationMillis => { |
309 | | - let (i, data_number) = DataNumber::parse(remaining, field_length, false)?; |
310 | | - ( |
311 | | - i, |
312 | | - FieldValue::Duration(Duration::from_millis( |
313 | | - <DataNumber as Into<usize>>::into(data_number) as u64, |
314 | | - )), |
315 | | - ) |
| 304 | + let (i, value) = match field_length { |
| 305 | + 4 => { |
| 306 | + let (i, seconds) = u32::parse_be(remaining)?; |
| 307 | + let dur = Duration::from_millis(u64::from(seconds)); |
| 308 | + (i, FieldValue::Duration(dur)) |
| 309 | + } |
| 310 | + 8 => { |
| 311 | + let (i, seconds) = u64::parse_be(remaining)?; |
| 312 | + let dur = Duration::from_millis(seconds); |
| 313 | + (i, FieldValue::Duration(dur)) |
| 314 | + } |
| 315 | + _ => { |
| 316 | + return Err(NomErr::Error(NomError::new(remaining, ErrorKind::Fail))); |
| 317 | + } |
| 318 | + }; |
| 319 | + (i, value) |
316 | 320 | } |
317 | | - FieldDataType::DurationMicros => { |
| 321 | + FieldDataType::DurationMicrosNTP => { |
318 | 322 | let (i, seconds) = u32::parse_be(remaining)?; |
319 | 323 | let (i, fraction) = u32::parse_be(i)?; |
320 | | - let dur = |
321 | | - Duration::from_secs(seconds as u64).saturating_add(Duration::from_micros( |
322 | | - ((u64::from(fraction)).saturating_mul(1_000_000)) >> 32, |
323 | | - )); |
| 324 | + let dur = Self::make_ntp_time_with_unit(seconds, fraction, 1_000_000); |
324 | 325 | (i, FieldValue::Duration(dur)) |
325 | 326 | } |
326 | | - FieldDataType::DurationNanos => { |
| 327 | + FieldDataType::DurationNanosNTP => { |
327 | 328 | let (i, seconds) = u32::parse_be(remaining)?; |
328 | 329 | let (i, fraction) = u32::parse_be(i)?; |
329 | | - let dur = Duration::from_secs(u64::from(seconds)).saturating_add( |
330 | | - Duration::from_nanos( |
331 | | - (u64::from(fraction).saturating_mul(1_000_000_000)) >> 32, |
332 | | - ), |
333 | | - ); |
| 330 | + let dur = Self::make_ntp_time_with_unit(seconds, fraction, 1_000_000_000); |
334 | 331 | (i, FieldValue::Duration(dur)) |
335 | 332 | } |
336 | 333 | FieldDataType::ProtocolType => { |
@@ -361,8 +358,8 @@ pub enum FieldDataType { |
361 | 358 | Float64, |
362 | 359 | DurationSeconds, |
363 | 360 | DurationMillis, |
364 | | - DurationMicros, |
365 | | - DurationNanos, |
| 361 | + DurationMicrosNTP, |
| 362 | + DurationNanosNTP, |
366 | 363 | Ip4Addr, |
367 | 364 | Ip6Addr, |
368 | 365 | MacAddr, |
|
0 commit comments