@@ -75,9 +75,10 @@ pub mod protocol;
75
75
pub mod static_versions;
76
76
pub mod variable_versions;
77
77
78
+ use nom_derive:: Parse ;
78
79
use serde:: Serialize ;
79
80
80
- use netflow_header:: { NetflowHeader , NetflowVersion } ;
81
+ use netflow_header:: { NetflowHeader , NetflowHeaderResult } ;
81
82
use static_versions:: { v5:: V5 , v7:: V7 } ;
82
83
use variable_versions:: ipfix:: { IPFix , IPFixParser } ;
83
84
use variable_versions:: v9:: { V9Parser , V9 } ;
@@ -121,24 +122,44 @@ impl NetflowPacketResult {
121
122
}
122
123
}
123
124
125
+ impl From < V5 > for NetflowPacketResult {
126
+ fn from ( v5 : V5 ) -> Self {
127
+ Self :: V5 ( v5)
128
+ }
129
+ }
130
+
131
+ impl From < V7 > for NetflowPacketResult {
132
+ fn from ( v7 : V7 ) -> Self {
133
+ Self :: V7 ( v7)
134
+ }
135
+ }
136
+
137
+ impl From < V9 > for NetflowPacketResult {
138
+ fn from ( v9 : V9 ) -> Self {
139
+ Self :: V9 ( v9)
140
+ }
141
+ }
142
+
143
+ impl From < IPFix > for NetflowPacketResult {
144
+ fn from ( ipfix : IPFix ) -> Self {
145
+ Self :: IPFix ( ipfix)
146
+ }
147
+ }
148
+
124
149
#[ derive( Debug , Clone ) ]
125
150
struct ParsedNetflow {
126
151
remaining : Vec < u8 > ,
127
152
/// Parsed Netflow Packet
128
153
netflow_packet : NetflowPacketResult ,
129
154
}
130
155
131
- /// Trait provided for all static parser versions
132
- trait NetflowByteParserStatic {
133
- fn parse_bytes ( packet : & [ u8 ] ) -> Result < ParsedNetflow , Box < dyn std:: error:: Error > > ;
134
- }
135
-
136
- /// Trait provided for all variable parser versions. We need a mutable self reference to store things like tempalates.
137
- trait NetflowByteParserVariable {
138
- fn parse_bytes (
139
- & mut self ,
140
- packet : & [ u8 ] ,
141
- ) -> Result < ParsedNetflow , Box < dyn std:: error:: Error > > ;
156
+ impl ParsedNetflow {
157
+ fn new ( remaining : & [ u8 ] , netflow_packet : NetflowPacketResult ) -> Self {
158
+ Self {
159
+ remaining : remaining. to_vec ( ) ,
160
+ netflow_packet,
161
+ }
162
+ }
142
163
}
143
164
144
165
#[ derive( Default , Debug ) ]
@@ -148,28 +169,50 @@ pub struct NetflowParser {
148
169
}
149
170
150
171
impl NetflowParser {
151
- /// We match versions to parsers .
172
+ /// Parses a Netflow by version packet and returns a Parsed Netflow .
152
173
fn parse_by_version < ' a > (
153
174
& ' a mut self ,
154
175
packet : & ' a [ u8 ] ,
155
176
) -> Result < ParsedNetflow , Box < dyn std:: error:: Error > > {
156
177
match NetflowHeader :: parse_header ( packet) {
157
- Ok ( ( i, netflow_header) ) if netflow_header. version == NetflowVersion :: V5 => {
158
- V5 :: parse_bytes ( i)
159
- }
160
- Ok ( ( i, netflow_header) ) if netflow_header. version == NetflowVersion :: V7 => {
161
- V7 :: parse_bytes ( i)
162
- }
163
- Ok ( ( i, netflow_header) ) if netflow_header. version == NetflowVersion :: V9 => {
164
- self . v9_parser . parse_bytes ( i)
165
- }
166
- Ok ( ( i, netflow_header) ) if netflow_header. version == NetflowVersion :: IPFix => {
167
- self . ipfix_parser . parse_bytes ( i)
168
- }
178
+ Ok ( NetflowHeaderResult :: V5 ( v5_packet) ) => Self :: parse_v5 ( v5_packet) ,
179
+ Ok ( NetflowHeaderResult :: V7 ( v7_packet) ) => Self :: parse_v7 ( v7_packet) ,
180
+ Ok ( NetflowHeaderResult :: V9 ( v9_packet) ) => self . parse_v9 ( v9_packet) ,
181
+ Ok ( NetflowHeaderResult :: IPFix ( ipfix_packet) ) => self . parse_ipfix ( ipfix_packet) ,
169
182
_ => Err ( "Not Supported" . to_string ( ) . into ( ) ) ,
170
183
}
171
184
}
172
185
186
+ fn parse_v5 ( v5_packet : & [ u8 ] ) -> Result < ParsedNetflow , Box < dyn std:: error:: Error > > {
187
+ V5 :: parse ( v5_packet)
188
+ . map_err ( |e| format ! ( "Could not parse V5 packet: {e}" ) . into ( ) )
189
+ . map ( |( remaining, v5_parsed) | ParsedNetflow :: new ( remaining, v5_parsed. into ( ) ) )
190
+ }
191
+
192
+ fn parse_v7 ( v7_packet : & [ u8 ] ) -> Result < ParsedNetflow , Box < dyn std:: error:: Error > > {
193
+ V7 :: parse ( v7_packet)
194
+ . map_err ( |e| format ! ( "Could not parse V7 packet: {e}" ) . into ( ) )
195
+ . map ( |( remaining, v5_parsed) | ParsedNetflow :: new ( remaining, v5_parsed. into ( ) ) )
196
+ }
197
+
198
+ fn parse_v9 (
199
+ & mut self ,
200
+ v9_packet : & [ u8 ] ,
201
+ ) -> Result < ParsedNetflow , Box < dyn std:: error:: Error > > {
202
+ V9 :: parse ( v9_packet, & mut self . v9_parser )
203
+ . map_err ( |e| format ! ( "Could not parse V9 packet: {e}" ) . into ( ) )
204
+ . map ( |( remaining, v9_parsed) | ParsedNetflow :: new ( remaining, v9_parsed. into ( ) ) )
205
+ }
206
+
207
+ fn parse_ipfix (
208
+ & mut self ,
209
+ ipfix_packet : & [ u8 ] ,
210
+ ) -> Result < ParsedNetflow , Box < dyn std:: error:: Error > > {
211
+ IPFix :: parse ( ipfix_packet, & mut self . ipfix_parser )
212
+ . map_err ( |e| format ! ( "Could not parse v10_packet: {e}" ) . into ( ) )
213
+ . map ( |( remaining, ipfix_parsed) | ParsedNetflow :: new ( remaining, ipfix_parsed. into ( ) ) )
214
+ }
215
+
173
216
/// Takes a Netflow packet slice and returns a vector of Parsed Netflows.
174
217
/// If we reach some parse error we return what items be have.
175
218
///
0 commit comments