pf_rs/
common.rs

1use nix::libc;
2
3/*
4#[macro_export]
5macro_rules! cfor {
6    ($init: stmt; $cond: expr; $step: expr; $body: block) => {
7        {
8            let mut first = true;
9            $init;
10            while {
11                if first {
12                    first = false
13                } else {
14                    $step
15                }
16
17                $cond
18            } $body
19        }
20    }
21}
22*/
23
24pub const PF_TABLE_NAME_SIZE: usize = 32;
25pub const PF_RULE_LABEL_SIZE: usize = 64;
26pub const MAXPATHLEN: usize = 1024;
27pub const IFNAMSIZ: usize = 16;
28pub const IF_NAMESIZE: usize = 16;
29
30//libc is missing some constants for the FreeBSD
31pub const AI_NUMERICHOST: libc::c_int = 0x00000004;
32
33pub(crate)
34fn sanitize_str(st: &str) -> String
35{
36    let mut out = String::with_capacity(st.len());
37
38    for c in st.chars()
39    {
40        if c.is_ascii_alphanumeric() == true ||
41            c.is_ascii_punctuation() == true ||
42            c == ' '
43        {
44            out.push(c);
45        }
46        else
47        {
48            let mut buf = [0_u8; 4];
49            c.encode_utf8(&mut buf);
50
51            let formatted: String = 
52                buf[0..c.len_utf8()].into_iter()
53                    .map(|c| format!("\\x{:02x}", c))
54                    .collect();
55
56            out.push_str(&formatted);
57        }
58    }
59
60    return out;
61}
62