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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
/* Copyright (C) 2017 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
//! Module for retrieving configuration details.
use std::os::raw::c_char;
use std::os::raw::c_void;
use std::os::raw::c_int;
use std::ffi::{CString, CStr};
use std::ptr;
use std::str;
use nom7::{
character::complete::{multispace0, not_line_ending},
sequence::{preceded, tuple},
number::complete::double,
combinator::verify,
IResult,
};
extern {
fn ConfGet(key: *const c_char, res: *mut *const c_char) -> i8;
fn ConfGetChildValue(conf: *const c_void, key: *const c_char,
vptr: *mut *const c_char) -> i8;
fn ConfGetChildValueBool(conf: *const c_void, key: *const c_char,
vptr: *mut c_int) -> i8;
fn ConfGetNode(key: *const c_char) -> *const c_void;
}
pub fn conf_get_node(key: &str) -> Option<ConfNode> {
let key = if let Ok(key) = CString::new(key) {
key
} else {
return None;
};
let node = unsafe { ConfGetNode(key.as_ptr()) };
if node.is_null() {
None
} else {
Some(ConfNode::wrap(node))
}
}
// Return the string value of a configuration value.
pub fn conf_get(key: &str) -> Option<&str> {
let mut vptr: *const c_char = ptr::null_mut();
unsafe {
let s = CString::new(key).unwrap();
if ConfGet(s.as_ptr(), &mut vptr) != 1 {
SCLogDebug!("Failed to find value for key {}", key);
return None;
}
}
if vptr.is_null() {
return None;
}
let value = str::from_utf8(unsafe{
CStr::from_ptr(vptr).to_bytes()
}).unwrap();
return Some(value);
}
// Return the value of key as a boolean. A value that is not set is
// the same as having it set to false.
pub fn conf_get_bool(key: &str) -> bool {
if let Some(val) = conf_get(key) {
match val {
"1" | "yes" | "true" | "on" => {
return true;
},
_ => {},
}
}
return false;
}
/// Wrap a Suricata ConfNode and expose some of its methods with a
/// Rust friendly interface.
pub struct ConfNode {
pub conf: *const c_void,
}
impl ConfNode {
pub fn wrap(conf: *const c_void) -> Self {
return Self { conf }
}
pub fn get_child_value(&self, key: &str) -> Option<&str> {
let mut vptr: *const c_char = ptr::null_mut();
unsafe {
let s = CString::new(key).unwrap();
if ConfGetChildValue(self.conf,
s.as_ptr(),
&mut vptr) != 1 {
return None;
}
}
if vptr.is_null() {
return None;
}
let value = str::from_utf8(unsafe{
CStr::from_ptr(vptr).to_bytes()
}).unwrap();
return Some(value);
}
pub fn get_child_bool(&self, key: &str) -> bool {
let mut vptr: c_int = 0;
unsafe {
let s = CString::new(key).unwrap();
if ConfGetChildValueBool(self.conf,
s.as_ptr(),
&mut vptr) != 1 {
return false;
}
}
if vptr == 1 {
return true;
}
return false;
}
}
const BYTE: u64 = 1;
const KILOBYTE: u64 = 1024;
const MEGABYTE: u64 = 1_048_576;
const GIGABYTE: u64 = 1_073_741_824;
/// Helper function to retrieve memory unit from a string slice
///
/// Return value: u64
///
/// # Arguments
///
/// * `unit` - A string slice possibly containing memory unit
fn get_memunit(unit: &str) -> u64 {
let unit = &unit.to_lowercase()[..];
match unit {
"b" => { BYTE }
"kb" => { KILOBYTE }
"mb" => { MEGABYTE }
"gb" => { GIGABYTE }
_ => { 0 }
}
}
/// Parses memory units from human readable form to machine readable
///
/// Return value:
/// Result => Ok(u64)
/// => Err(error string)
///
/// # Arguments
///
/// * `arg` - A string slice that holds the value parsed from the config
pub fn get_memval(arg: &str) -> Result<u64, &'static str> {
let arg = arg.trim();
let val: f64;
let mut unit: &str;
let mut parser = tuple((preceded(multispace0, double),
preceded(multispace0, verify(not_line_ending, |c: &str| c.len() < 3))));
let r: IResult<&str, (f64, &str)> = parser(arg);
if let Ok(r) = r {
val = (r.1).0;
unit = (r.1).1;
} else {
return Err("Error parsing the memory value");
}
if unit.is_empty() {
unit = "B";
}
let unit = get_memunit(unit);
if unit == 0 {
return Err("Invalid memory unit");
}
let res = val * unit as f64;
Ok(res as u64)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_memval_nospace() {
let s = "10";
let res = 10 ;
assert_eq!(Ok(10), get_memval(s));
let s = "10kb";
assert_eq!(Ok(res * KILOBYTE), get_memval(s));
let s = "10Kb";
assert_eq!(Ok(res * KILOBYTE), get_memval(s));
let s = "10KB";
assert_eq!(Ok(res * KILOBYTE), get_memval(s));
let s = "10mb";
assert_eq!(Ok(res * MEGABYTE), get_memval(s));
let s = "10gb";
assert_eq!(Ok(res * GIGABYTE), get_memval(s));
}
#[test]
fn test_memval_space_start() {
let s = " 10";
let res = 10 ;
assert_eq!(Ok(res), get_memval(s));
let s = " 10Kb";
assert_eq!(Ok(res * KILOBYTE), get_memval(s));
let s = " 10mb";
assert_eq!(Ok(res * MEGABYTE), get_memval(s));
let s = " 10Gb";
assert_eq!(Ok(res * GIGABYTE), get_memval(s));
let s = " 30b";
assert_eq!(Ok(30), get_memval(s));
}
#[test]
fn test_memval_space_end() {
let s = " 10 ";
let res = 10 ;
assert_eq!(Ok(res), get_memval(s));
let s = "10Kb ";
assert_eq!(Ok(res * KILOBYTE), get_memval(s));
let s = "10mb ";
assert_eq!(Ok(res * MEGABYTE), get_memval(s));
let s = " 10Gb ";
assert_eq!(Ok(res * GIGABYTE), get_memval(s));
let s = " 30b ";
assert_eq!(Ok(30), get_memval(s));
}
#[test]
fn test_memval_space_in_bw() {
let s = " 10 ";
let res = 10 ;
assert_eq!(Ok(res), get_memval(s));
let s = "10 Kb ";
assert_eq!(Ok(res * KILOBYTE), get_memval(s));
let s = "10 mb";
assert_eq!(Ok(res * MEGABYTE), get_memval(s));
let s = " 10 Gb ";
assert_eq!(Ok(res * GIGABYTE), get_memval(s));
let s = "30 b";
assert_eq!(Ok(30), get_memval(s));
}
#[test]
fn test_memval_float_val() {
let s = " 10.5 ";
assert_eq!(Ok(10), get_memval(s));
let s = "10.8Kb ";
assert_eq!(Ok((10.8 * KILOBYTE as f64) as u64), get_memval(s));
let s = "10.4 mb ";
assert_eq!(Ok((10.4 * MEGABYTE as f64) as u64), get_memval(s));
let s = " 10.5Gb ";
assert_eq!(Ok((10.5 * GIGABYTE as f64) as u64), get_memval(s));
let s = " 30.0 b ";
assert_eq!(Ok(30), get_memval(s));
}
#[test]
fn test_memval_erroneous_val() {
let s = "5eb";
assert!(get_memval(s).is_err());
let s = "5 1kb";
assert!(get_memval(s).is_err());
let s = "61k b";
assert!(get_memval(s).is_err());
let s = "8 8 k b";
assert!(get_memval(s).is_err());
}
}
|