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
use crate::config::plugin::Plugin;
use crate::Error;
use askama::Template;
use std::path::{Path, PathBuf};

#[derive(Clone)]
pub struct IpcPluginConfig {
    pub path: PathBuf,
    pub allocation_batch_size: usize,
    pub servers: usize,
    pub live: bool,
}

impl Default for IpcPluginConfig {
    fn default() -> Self {
        let p = if let Ok(p) = std::env::var("SURICATA_IPC_PLUGIN") {
            PathBuf::from(p)
        } else {
            PathBuf::from("/usr/lib/ipc-plugin.so")
        };
        Self::new(p)
    }
}

impl IpcPluginConfig {
    pub fn new(path: PathBuf) -> Self {
        Self {
            path: path,
            allocation_batch_size: 1_000,
            servers: 1,
            live: true,
        }
    }

    pub fn into_plugin<'a>(self) -> Result<(IpcPlugin, Vec<packet_ipc::Server<'a>>), Error> {
        let mut names = Vec::with_capacity(self.servers);
        let mut servers = Vec::with_capacity(self.servers);
        for _ in 0..self.servers {
            let server = packet_ipc::Server::new().map_err(Error::from)?;
            let server_name = server.name().clone();
            names.push(server_name);
            servers.push(server);
        }
        let names = names.join(",");
        let plugin = IpcPlugin {
            path: self.path,
            allocation_batch_size: self.allocation_batch_size,
            servers: names,
            live: self.live,
        };
        Ok((plugin, servers))
    }
}

#[derive(Template)]
#[template(path = "ipc-plugin.yaml.in", escape = "none")]
pub struct IpcPlugin {
    pub path: PathBuf,
    pub allocation_batch_size: usize,
    pub servers: String,
    pub live: bool,
}

impl Plugin for IpcPlugin {
    fn name(&self) -> &str {
        "ipc-plugin"
    }
    fn path(&self) -> &Path {
        self.path.as_path()
    }
    fn config(&self) -> Option<String> {
        Some(self.render().unwrap())
    }
}