use crate::{active_children, list_values, named_children, split_keyword, statements, unquote};
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct MatchCondition {
pub name: String,
pub values: Vec<String>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum FilterAction {
Accept,
Discard,
Reject,
NextTerm,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct FilterActions {
pub action: Option<FilterAction>,
pub count: Option<String>,
pub policer: Option<String>,
pub forwarding_class: Option<String>,
pub loss_priority: Option<String>,
pub log: bool,
pub syslog: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct FilterTerm {
pub name: String,
pub from: Vec<MatchCondition>,
pub then: FilterActions,
}
impl FilterTerm {
pub fn condition(&self, name: &str) -> Option<&MatchCondition> {
self.from.iter().find(|c| c.name == name)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct FirewallFilter {
pub family: String,
pub name: String,
pub terms: Vec<FilterTerm>,
}
pub(super) fn parse_filter(family: &str, name: &str, line: &ConfigLine) -> FirewallFilter {
FirewallFilter {
family: family.to_string(),
name: name.to_string(),
terms: named_children(line, "term")
.map(|(term_name, term)| FilterTerm {
name: term_name.to_string(),
from: parse_conditions(term),
then: parse_actions(term),
})
.collect(),
}
}
fn parse_conditions(term: &ConfigLine) -> Vec<MatchCondition> {
let mut found = Vec::new();
for child in active_children(term) {
if child.text == "from" {
found.extend(active_children(child).map(parse_condition));
} else if let Some(statement) = child.arg("from") {
found.push(condition_from(statement, &[]));
}
}
found
}
fn parse_condition(line: &ConfigLine) -> MatchCondition {
condition_from(&line.text, &line.children)
}
fn condition_from(statement: &str, children: &[ConfigLine]) -> MatchCondition {
let (name, arg) = split_keyword(statement);
let mut values = if arg.is_empty() {
Vec::new()
} else if arg.starts_with('[') {
list_values(arg)
} else {
vec![unquote(arg).to_string()]
};
values.extend(
children
.iter()
.filter(|c| !c.text.starts_with("inactive:"))
.map(|c| c.text.clone()),
);
MatchCondition {
name: name.to_string(),
values,
}
}
fn parse_actions(term: &ConfigLine) -> FilterActions {
let mut actions = FilterActions::default();
for statement in statements(term, "then") {
let (action, arg) = split_keyword(statement);
let value = || Some(unquote(arg).to_string());
match (action, arg) {
("accept", _) => actions.action = Some(FilterAction::Accept),
("discard", _) => actions.action = Some(FilterAction::Discard),
("reject", _) => actions.action = Some(FilterAction::Reject),
("next", "term") => actions.action = Some(FilterAction::NextTerm),
("count", _) => actions.count = value(),
("policer", _) => actions.policer = value(),
("forwarding-class", _) => actions.forwarding_class = value(),
("loss-priority", _) => actions.loss_priority = value(),
("log", "") => actions.log = true,
("syslog", "") => actions.syslog = true,
_ => {}
}
}
actions
}