1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use processor::{OutputProcessor, ConfigurableFilter};
use std::collections::HashMap;
use std::sync::mpsc::Receiver;
use std::thread::JoinHandle;
use std::net::UdpSocket;
pub struct Network {
name: String
}
impl Network {
pub fn new(name: String) -> Network {
Network{ name: name }
}
}
impl ConfigurableFilter for Network {
fn human_name(&self) -> &str {
self.name.as_ref()
}
fn mandatory_fields(&self) -> Vec<&str> {
vec!["destination", "port"]
}
}
impl OutputProcessor for Network {
fn start(&self, rx:Receiver<String>, config: &Option<HashMap<String,String>>) -> Result<JoinHandle<()>, String> {
self.requires_fields(config, self.mandatory_fields());
self.invoke(rx, config, Network::handle_func)
}
fn handle_func(rx: Receiver<String>, oconfig: Option<HashMap<String,String>>) {
let config = oconfig.expect("Need a configuration");
let destination_ip = config.get("destination").expect("Need a destination IP").clone();
let destination_port = config.get("port").expect("Need a destination port").parse::<u32>().unwrap();
let udp = UdpSocket::bind("0.0.0.0:0").unwrap();
let dest = format!("{}:{}", destination_ip, destination_port);
loop {
match rx.recv() {
Ok(l) => {
udp.send_to(l.as_bytes(), &*dest).unwrap();
},
Err(e) => { panic!(e) }
}
}
}
}