syslog_rs/
syslog_provider.rs

1
2
3
4
5pub trait SyslogDestination: std::fmt::Debug + Send + 'static
6{
7    fn init(self) -> SyRes<TapTypeData>;
8}
9
10#[derive(Debug)]
11pub struct SyslogLocal
12{
13    /// A path to custom `UnixDatagram` socket.
14    custom_path: Option<PathBuf>,
15
16    /// usefull only if `custom_path` is [Some]
17    use_alt_path: bool,
18}
19
20impl SyslogLocal
21{
22    /// Creates a default connection to local syslog server. The correct path will be picked
23    /// automatically.
24    pub 
25    fn new() -> Self
26    {
27        return
28            Self
29            {
30                custom_path: None,
31                use_alt_path: false,
32            };
33    }
34
35    /// Creates a special connection to specific local socket. If `use_alt_path` is 
36    /// set to true and `custom_path` is not available, other pre-programmed paths
37    /// will be probed and selected the first available.
38    pub 
39    fn new_custom_path<P: Into<PathBuf>>(custom_path: P, use_alt_path: bool) -> Self
40    {
41        return
42            Self
43            {
44                custom_path: Some(custom_path.into()),
45                use_alt_path: use_alt_path,
46            };
47    }
48}
49
50impl SyslogDestination for SyslogLocal
51{
52    fn init(self) -> SyRes<TapTypeData> 
53    {
54        return 
55            TapTypeData
56                ::new_unix(
57                    self.custom_path.as_ref().map(|v| v.as_path()), 
58                    self.use_alt_path
59                );
60    }
61}
62
63#[cfg(feature = "build_with_net")]
64pub mod imp_syslog_net
65{
66    use crate::{error::SyRes, socket::{TapTypeData, TapTypeDataRemProto}};
67
68    use super::SyslogDestination;
69
70    #[derive(Debug)]
71    pub struct SyslogNet
72    {
73        proto: TapTypeDataRemProto,
74        dest_addr: String,
75        bind_addr: Option<String>,
76    }
77
78    impl SyslogNet
79    {
80        /// Creates a new TCP connection to syslog server.
81        /// 
82        /// # Arguments
83        /// 
84        /// `dest_addr` - a server's address in form "ip:port"
85        pub 
86        fn new_tcp<D: AsRef<str>>(dest_addr: impl Into<String>) -> Self
87        {
88            return
89                Self
90                {
91                    proto: TapTypeDataRemProto::Tcp,
92                    dest_addr: dest_addr.into(),
93                    bind_addr: None,
94                };
95        }
96
97        /// Creates a new UDP connection to syslog server.
98        /// 
99        /// # Arguments
100        /// 
101        /// `dest_addr` - a server's address in form "ip:port"
102        pub 
103        fn new_udp(dest_addr: impl Into<String>) -> Self
104        {
105            return
106                Self
107                {
108                    proto: TapTypeDataRemProto::Udp,
109                    dest_addr: dest_addr.into(),
110                    bind_addr: None
111                };
112        }
113
114        /// Creates a new UDP connection to syslog server.
115        /// 
116        /// # Arguments
117        /// 
118        /// `dest_addr` - a server's address ("ip:port") i.e "127.0.0.1:514"
119        /// 
120        /// `bins_addr` - allows to bind a specific address ("ip:port") i.e "127.0.0.1:30000"
121        pub 
122        fn new_udp_bind(dest_addr: impl Into<String>, bind_addr: impl Into<String>) -> Self
123        {
124            return
125                Self
126                {
127                    proto: TapTypeDataRemProto::Udp,
128                    dest_addr: dest_addr.into(),
129                    bind_addr: Some(bind_addr.into()),
130                };
131        }
132    }
133
134    impl SyslogDestination for SyslogNet
135    {
136        fn init(self) -> SyRes<TapTypeData> 
137        {
138            return TapTypeData::new_net(self.proto, self.dest_addr, self.bind_addr);
139        }
140    }
141}
142
143use std::path::PathBuf;
144
145use crate::{error::SyRes, socket::TapTypeData};
146
147#[cfg(feature = "build_with_net")]
148pub use self::imp_syslog_net::SyslogNet;
149
150#[cfg(feature = "build_with_file")]
151pub mod imp_syslog_file
152{
153    use std::path::PathBuf;
154
155    use crate::{error::SyRes, socket::TapTypeData};
156
157    use super::SyslogDestination;
158
159    #[derive(Debug)]
160    pub struct SyslogFile
161    {
162        /// A path to text file on the file system.
163        file_path: PathBuf
164    }
165
166    impl SyslogFile
167    {
168        /// Sets where to create or open file and write there.
169        pub 
170        fn new<P: Into<PathBuf>>(dest_path: P) -> Self
171        {
172            return
173                Self
174                {
175                    file_path: dest_path.into() 
176                };
177        }
178    }
179
180    impl SyslogDestination for SyslogFile
181    {
182        fn init(self) -> SyRes<TapTypeData> 
183        {
184            return TapTypeData::new_file(self.file_path);
185        }
186    }
187}
188
189#[cfg(feature = "build_with_file")]
190pub use self::imp_syslog_file::SyslogFile;
191
192#[cfg(feature = "build_with_tls")]
193pub mod imp_syslog_tls
194{
195    use crate::{error::SyRes, socket::TapTypeData};
196
197    use super::SyslogDestination;
198
199    #[derive(Debug)]
200    pub struct SyslogTls
201    {
202        dest_addr: String,
203        serv_name: String,
204        root_cert: Vec<u8>,
205    }
206
207    impl SyslogTls
208    {
209        /// Creates a new TLS over TCP connection to syslog server. 
210        /// 
211        /// # Arguments
212        /// 
213        /// * `dest_addr` - an (ip:port) to specific server.
214        /// 
215        /// * `serv_name` - a FQDN of server (should match with `root_cert`)
216        /// 
217        /// * `root_cert` - a root certificate of the server.
218        pub 
219        fn new(dest_addr: impl Into<String>, serv_name: impl Into<String>, root_cert: Vec<u8>) -> Self
220        {
221            return
222                Self
223                {
224                    dest_addr: dest_addr.into(),
225                    serv_name: serv_name.into(),
226                    root_cert: root_cert
227                };
228        }
229    }
230
231    impl SyslogDestination for SyslogTls
232    {
233        fn init(self) -> SyRes<TapTypeData> 
234        {
235            return TapTypeData::new_tls(self.dest_addr, &self.serv_name, &self.root_cert);
236        }
237    }
238}
239
240#[cfg(feature = "build_with_tls")]
241pub use self::imp_syslog_tls::SyslogTls;