Skip to content

Commit 378dc0f

Browse files
mikemiles-devmikemiles-dev
and
mikemiles-dev
authored
Tokio example (#74)
Co-authored-by: mikemiles-dev <[email protected]>
1 parent 17a3371 commit 378dc0f

File tree

7 files changed

+47
-4
lines changed

7 files changed

+47
-4
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ parse_unknown_fields = []
2222

2323
[dev-dependencies]
2424
insta = { version = "1.30.0", features = ["yaml"] }
25+
tokio = { version = "1.38.0", features = ["full"] }
26+
tokio-macros = { version = "0.2.0-alpha.6" }
2527
hex = "0.4.3"
2628
serde_json = "1.0.100"

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,7 @@ To run:
9494
or
9595

9696
```cargo run --example netflow_udp_listener_single_threaded```
97+
98+
or
99+
100+
```cargo run --example netflow_udp_listener_tokio```

RELEASES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# 0.3.6
22
* Added V9 Post NAT fields 225-228.
3+
* Added Tokio Async Example
34

45
# 0.3.5
56
* 3 Byte Data Numbers now correctly converts back to be_bytes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@
9292
//! or
9393
//!
9494
//! ```cargo run --example netflow_udp_listener_single_threaded```
95+
//!
96+
//! or
97+
//!
98+
//! ```cargo run --example netflow_udp_listener_tokio```
9599
96100
mod parser;
97101
pub mod protocol;

src/variable_versions/ipfix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ pub struct FlowSetBody {
121121
pub options_template: Option<OptionsTemplate>,
122122
// Data
123123
#[nom(
124-
Cond = "id > SET_MIN_RANGE && parser.templates.get(&id).is_some()",
124+
Cond = "id > SET_MIN_RANGE && parser.templates.contains_key(&id)",
125125
Parse = "{ |i| Data::parse(i, parser, id) }"
126126
)]
127127
#[serde(skip_serializing_if = "Option::is_none")]
128128
pub data: Option<Data>,
129129
// OptionsData
130130
#[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)",
132132
Parse = "{ |i| OptionsData::parse(i, parser, id) }"
133133
)]
134134
#[serde(skip_serializing_if = "Option::is_none")]

src/variable_versions/v9.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ pub struct FlowSetBody {
121121
pub options_templates: Option<Vec<OptionsTemplate>>,
122122
// Options Data
123123
#[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)",
125125
Parse = "{ |i| OptionsData::parse(i, parser, flow_set_id) }"
126126
)]
127127
#[serde(skip_serializing_if = "Option::is_none")]
128128
pub options_data: Option<OptionsData>,
129129
// Data
130130
#[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)",
132132
Parse = "{ |i| Data::parse(i, parser, flow_set_id) }"
133133
)]
134134
#[serde(skip_serializing_if = "Option::is_none")]

0 commit comments

Comments
 (0)