Skip to content

Commit b1e0323

Browse files
mikemiles-devmikemiles-dev
andauthored
fix: Lib Cleanup added ParsedNetflow enum. (#159)
Co-authored-by: mikemiles-dev <[email protected]>
1 parent 19ac42c commit b1e0323

File tree

6 files changed

+76
-63
lines changed

6 files changed

+76
-63
lines changed

RELEASES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# 0.6.4
22
* Removed uneeded DataNumber Parsing for Durations.
33
* Explicity rename methods DurationMicros and DurationNanos into DurationMicrosNTP and DurationNanosNTP.
4-
* Performance optimizations
4+
* Minor Performance optimizations
55

66
# 0.6.3
77
* Ipfix dateTimeNanoseconds and dateTimeMicroseconds use the NTP 64 bit time format #15

src/lib.rs

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -266,19 +266,15 @@ pub struct NetflowParser {
266266
}
267267

268268
#[derive(Debug, Clone)]
269-
pub struct ParsedNetflow {
270-
pub remaining: Vec<u8>,
271-
/// Parsed Netflow Packet
272-
pub result: NetflowPacket,
273-
}
274-
275-
impl ParsedNetflow {
276-
fn new(remaining: &[u8], result: NetflowPacket) -> Self {
277-
Self {
278-
remaining: remaining.to_vec(),
279-
result,
280-
}
281-
}
269+
pub enum ParsedNetflow {
270+
Success {
271+
packet: NetflowPacket,
272+
remaining: Vec<u8>,
273+
},
274+
Error {
275+
error: NetflowParseError,
276+
},
277+
UnallowedVersion,
282278
}
283279

284280
#[derive(Debug, Clone, Serialize)]
@@ -291,7 +287,6 @@ pub struct NetflowPacketError {
291287
pub enum NetflowParseError {
292288
Incomplete(String),
293289
Partial(PartialParse),
294-
UnallowedVersion(u16),
295290
UnknownVersion(Vec<u8>),
296291
}
297292

@@ -343,16 +338,19 @@ impl NetflowParser {
343338

344339
while !remaining.is_empty() {
345340
match self.parse_packet_by_version(&remaining) {
346-
Ok(parsed) => {
347-
packets.push(parsed.result);
348-
remaining = parsed.remaining;
341+
ParsedNetflow::Success {
342+
packet,
343+
remaining: new_remaining,
344+
} => {
345+
packets.push(packet);
346+
remaining = new_remaining;
349347
}
350-
Err(NetflowParseError::UnallowedVersion(_)) => {
348+
ParsedNetflow::UnallowedVersion => {
351349
break;
352350
}
353-
Err(e) => {
351+
ParsedNetflow::Error { error } => {
354352
packets.push(NetflowPacket::Error(NetflowPacketError {
355-
error: e,
353+
error,
356354
remaining: remaining.to_vec(),
357355
}));
358356
break;
@@ -376,27 +374,28 @@ impl NetflowParser {
376374
.collect()
377375
}
378376

379-
/// Checks the first u16 of the packet to determine the version. Parses the packet based on the version.
380-
/// If the version is unknown it returns an error. If the packet is incomplete it returns an error.
381-
/// If the packet is parsed successfully it returns the parsed Netflow packet and the remaining bytes.
382-
fn parse_packet_by_version(
383-
&mut self,
384-
packet: &[u8],
385-
) -> Result<ParsedNetflow, NetflowParseError> {
386-
let (packet, version) = GenericNetflowHeader::parse(packet)
387-
.map(|(remaining, header)| (remaining, header.version))
388-
.map_err(|e| NetflowParseError::Incomplete(e.to_string()))?;
377+
fn parse_packet_by_version(&mut self, packet: &[u8]) -> ParsedNetflow {
378+
let (packet, version) = match GenericNetflowHeader::parse(packet) {
379+
Ok((remaining, header)) => (remaining, header.version),
380+
Err(e) => {
381+
return ParsedNetflow::Error {
382+
error: NetflowParseError::Incomplete(e.to_string()),
383+
};
384+
}
385+
};
389386

390387
if !self.allowed_versions.contains(&version) {
391-
return Err(NetflowParseError::UnallowedVersion(version));
388+
return ParsedNetflow::UnallowedVersion;
392389
}
393390

394391
match version {
395392
5 => V5Parser::parse(packet),
396393
7 => V7Parser::parse(packet),
397394
9 => self.v9_parser.parse(packet),
398395
10 => self.ipfix_parser.parse(packet),
399-
_ => Err(NetflowParseError::UnknownVersion(packet.to_vec())),
396+
_ => ParsedNetflow::Error {
397+
error: NetflowParseError::UnknownVersion(packet.to_vec()),
398+
},
400399
}
401400
}
402401
}

src/static_versions/v5.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@ use std::net::Ipv4Addr;
1515
pub struct V5Parser;
1616

1717
impl V5Parser {
18-
pub fn parse(packet: &[u8]) -> Result<ParsedNetflow, NetflowParseError> {
19-
V5::parse(packet)
20-
.map(|(remaining, v5)| ParsedNetflow::new(remaining, NetflowPacket::V5(v5)))
21-
.map_err(|e| {
22-
NetflowParseError::Partial(PartialParse {
18+
pub fn parse(packet: &[u8]) -> ParsedNetflow {
19+
match V5::parse(packet) {
20+
Ok((remaining, v5)) => ParsedNetflow::Success {
21+
packet: NetflowPacket::V5(v5),
22+
remaining: remaining.to_vec(),
23+
},
24+
Err(e) => ParsedNetflow::Error {
25+
error: NetflowParseError::Partial(PartialParse {
2326
version: 5,
2427
error: e.to_string(),
2528
remaining: packet.to_vec(),
26-
})
27-
})
29+
}),
30+
},
31+
}
2832
}
2933
}
3034

src/static_versions/v7.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@ use std::net::Ipv4Addr;
1616
pub struct V7Parser;
1717

1818
impl V7Parser {
19-
pub fn parse(packet: &[u8]) -> Result<ParsedNetflow, NetflowParseError> {
20-
V7::parse(packet)
21-
.map(|(remaining, v7)| ParsedNetflow::new(remaining, NetflowPacket::V7(v7)))
22-
.map_err(|e| {
23-
NetflowParseError::Partial(PartialParse {
19+
pub fn parse(packet: &[u8]) -> ParsedNetflow {
20+
match V7::parse(packet) {
21+
Ok((remaining, v7)) => ParsedNetflow::Success {
22+
packet: NetflowPacket::V7(v7),
23+
remaining: remaining.to_vec(),
24+
},
25+
Err(e) => ParsedNetflow::Error {
26+
error: NetflowParseError::Partial(PartialParse {
2427
version: 7,
2528
error: e.to_string(),
2629
remaining: packet.to_vec(),
27-
})
28-
})
30+
}),
31+
},
32+
}
2933
}
3034
}
3135

src/variable_versions/ipfix.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,20 @@ pub struct IPFixParser {
4343
}
4444

4545
impl IPFixParser {
46-
pub fn parse(&mut self, packet: &[u8]) -> Result<ParsedNetflow, NetflowParseError> {
47-
IPFix::parse(packet, self)
48-
.map(|(remaining, ipfix)| {
49-
ParsedNetflow::new(remaining, NetflowPacket::IPFix(ipfix))
50-
})
51-
.map_err(|e| {
52-
NetflowParseError::Partial(PartialParse {
46+
pub fn parse(&mut self, packet: &[u8]) -> ParsedNetflow {
47+
match IPFix::parse(packet, self) {
48+
Ok((remaining, ipfix)) => ParsedNetflow::Success {
49+
packet: NetflowPacket::IPFix(ipfix),
50+
remaining: remaining.to_vec(),
51+
},
52+
Err(e) => ParsedNetflow::Error {
53+
error: NetflowParseError::Partial(PartialParse {
5354
version: 10,
5455
error: e.to_string(),
5556
remaining: packet.to_vec(),
56-
})
57-
})
57+
}),
58+
},
59+
}
5860
}
5961

6062
/// Add a template (Template, OptionsTemplate, V9Template, or V9OptionsTemplate) to the parser generically.

src/variable_versions/v9.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,20 @@ pub type V9FieldPair = (V9Field, FieldValue);
2727
pub type V9FlowRecord = Vec<V9FieldPair>;
2828

2929
impl V9Parser {
30-
pub fn parse(&mut self, packet: &[u8]) -> Result<ParsedNetflow, NetflowParseError> {
31-
V9::parse(packet, self)
32-
.map(|(remaining, v9)| ParsedNetflow::new(remaining, NetflowPacket::V9(v9)))
33-
.map_err(|e| {
34-
NetflowParseError::Partial(PartialParse {
30+
pub fn parse(&mut self, packet: &[u8]) -> ParsedNetflow {
31+
match V9::parse(packet, self) {
32+
Ok((remaining, v9)) => ParsedNetflow::Success {
33+
packet: NetflowPacket::V9(v9),
34+
remaining: remaining.to_vec(),
35+
},
36+
Err(e) => ParsedNetflow::Error {
37+
error: NetflowParseError::Partial(PartialParse {
3538
version: 9,
3639
error: e.to_string(),
3740
remaining: packet.to_vec(),
38-
})
39-
})
41+
}),
42+
},
43+
}
4044
}
4145
}
4246

0 commit comments

Comments
 (0)