File tree Expand file tree Collapse file tree 7 files changed +47
-4
lines changed Expand file tree Collapse file tree 7 files changed +47
-4
lines changed Original file line number Diff line number Diff line change @@ -22,5 +22,7 @@ parse_unknown_fields = []
22
22
23
23
[dev-dependencies ]
24
24
insta = { version = " 1.30.0" , features = [" yaml" ] }
25
+ tokio = { version = " 1.38.0" , features = [" full" ] }
26
+ tokio-macros = { version = " 0.2.0-alpha.6" }
25
27
hex = " 0.4.3"
26
28
serde_json = " 1.0.100"
Original file line number Diff line number Diff line change @@ -94,3 +94,7 @@ To run:
94
94
or
95
95
96
96
``` cargo run --example netflow_udp_listener_single_threaded ```
97
+
98
+ or
99
+
100
+ ``` cargo run --example netflow_udp_listener_tokio ```
Original file line number Diff line number Diff line change 1
1
# 0.3.6
2
2
* Added V9 Post NAT fields 225-228.
3
+ * Added Tokio Async Example
3
4
4
5
# 0.3.5
5
6
* 3 Byte Data Numbers now correctly converts back to be_bytes.
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashMap ;
2
+ use std:: io;
3
+ use tokio:: net:: UdpSocket ;
4
+
5
+ use netflow_parser:: NetflowParser ;
6
+
7
+ #[ tokio:: main]
8
+ async fn main ( ) -> io:: Result < ( ) > {
9
+ let mut parsers: HashMap < String , NetflowParser > = HashMap :: new ( ) ;
10
+
11
+ let sock = UdpSocket :: bind ( "0.0.0.0:9995" ) . await ?;
12
+
13
+ let mut buf = [ 0 ; 65535 ] ;
14
+
15
+ loop {
16
+ let ( len, addr) = sock. recv_from ( & mut buf) . await ?;
17
+
18
+ let data = buf[ ..len] . to_vec ( ) ;
19
+ let data = data. as_slice ( ) ;
20
+
21
+ let result = match parsers. get_mut ( & addr. to_string ( ) ) {
22
+ Some ( parser) => parser. parse_bytes ( data) ,
23
+ None => {
24
+ let mut new_parser = NetflowParser :: default ( ) ;
25
+ let result = new_parser. parse_bytes ( data) ;
26
+ parsers. insert ( addr. to_string ( ) , new_parser) ;
27
+ result
28
+ }
29
+ } ;
30
+ println ! ( "{:?}" , result) ;
31
+ }
32
+ }
Original file line number Diff line number Diff line change 92
92
//! or
93
93
//!
94
94
//! ```cargo run --example netflow_udp_listener_single_threaded```
95
+ //!
96
+ //! or
97
+ //!
98
+ //! ```cargo run --example netflow_udp_listener_tokio```
95
99
96
100
mod parser;
97
101
pub mod protocol;
Original file line number Diff line number Diff line change @@ -121,14 +121,14 @@ pub struct FlowSetBody {
121
121
pub options_template : Option < OptionsTemplate > ,
122
122
// Data
123
123
#[ nom(
124
- Cond = "id > SET_MIN_RANGE && parser.templates.get (&id).is_some( )" ,
124
+ Cond = "id > SET_MIN_RANGE && parser.templates.contains_key (&id)" ,
125
125
Parse = "{ |i| Data::parse(i, parser, id) }"
126
126
) ]
127
127
#[ serde( skip_serializing_if = "Option::is_none" ) ]
128
128
pub data : Option < Data > ,
129
129
// OptionsData
130
130
#[ nom(
131
- Cond = "id > SET_MIN_RANGE && parser.options_templates.get (&id).is_some( )" ,
131
+ Cond = "id > SET_MIN_RANGE && parser.options_templates.contains_key (&id)" ,
132
132
Parse = "{ |i| OptionsData::parse(i, parser, id) }"
133
133
) ]
134
134
#[ serde( skip_serializing_if = "Option::is_none" ) ]
Original file line number Diff line number Diff line change @@ -121,14 +121,14 @@ pub struct FlowSetBody {
121
121
pub options_templates : Option < Vec < OptionsTemplate > > ,
122
122
// Options Data
123
123
#[ nom(
124
- Cond = "flow_set_id > FLOW_SET_MIN_RANGE && parser.options_templates.get (&flow_set_id).is_some( )" ,
124
+ Cond = "flow_set_id > FLOW_SET_MIN_RANGE && parser.options_templates.contains_key (&flow_set_id)" ,
125
125
Parse = "{ |i| OptionsData::parse(i, parser, flow_set_id) }"
126
126
) ]
127
127
#[ serde( skip_serializing_if = "Option::is_none" ) ]
128
128
pub options_data : Option < OptionsData > ,
129
129
// Data
130
130
#[ nom(
131
- Cond = "flow_set_id > FLOW_SET_MIN_RANGE && parser.templates.get (&flow_set_id).is_some( )" ,
131
+ Cond = "flow_set_id > FLOW_SET_MIN_RANGE && parser.templates.contains_key (&flow_set_id)" ,
132
132
Parse = "{ |i| Data::parse(i, parser, flow_set_id) }"
133
133
) ]
134
134
#[ serde( skip_serializing_if = "Option::is_none" ) ]
You can’t perform that action at this time.
0 commit comments