1 use crate::flowgger::config::Config;
2
3 pub mod tcp_input;
4 #[cfg(feature = "coroutines")]
5 pub mod tcpco_input;
6
7 pub use super::Input;
8
9 const DEFAULT_FRAMING: &str = "line";
10 const DEFAULT_LISTEN: &str = "0.0.0.0:514";
11 #[cfg(feature = "coroutines")]
12 const DEFAULT_THREADS: usize = 1;
13 const DEFAULT_TIMEOUT: u64 = 3600;
14
15 #[derive(Clone)]
16 pub struct TcpConfig {
17 framing: String,
18 threads: usize,
19 }
20
21 #[cfg(feature = "coroutines")]
get_default_threads(config: &Config) -> usize22 fn get_default_threads(config: &Config) -> usize {
23 config
24 .lookup("input.tcp_threads")
25 .map_or(DEFAULT_THREADS, |x| {
26 x.as_integer()
27 .expect("input.tcp_threads must be an unsigned integer") as usize
28 })
29 }
30
31 #[cfg(not(feature = "coroutines"))]
get_default_threads(_config: &Config) -> usize32 fn get_default_threads(_config: &Config) -> usize {
33 1
34 }
35
config_parse(config: &Config) -> (TcpConfig, String, u64)36 pub fn config_parse(config: &Config) -> (TcpConfig, String, u64) {
37 let listen = config
38 .lookup("input.listen")
39 .map_or(DEFAULT_LISTEN, |x| {
40 x.as_str().expect("input.listen must be an ip:port string")
41 })
42 .to_owned();
43 let threads = get_default_threads(config);
44 let timeout = config.lookup("input.timeout").map_or(DEFAULT_TIMEOUT, |x| {
45 x.as_integer()
46 .expect("input.timeout must be an unsigned integer") as u64
47 });
48 let framing = if config.lookup("input.framed").map_or(false, |x| {
49 x.as_bool().expect("input.framed must be a boolean")
50 }) {
51 "syslen"
52 } else {
53 DEFAULT_FRAMING
54 };
55 let framing = config
56 .lookup("input.framing")
57 .map_or(framing, |x| {
58 x.as_str()
59 .expect(r#"input.framing must be a string set to "line", "nul" or "syslen""#)
60 })
61 .to_owned();
62 let tcp_config = TcpConfig { framing, threads };
63 (tcp_config, listen, timeout)
64 }
65