X-Git-Url: http://git.madism.org/?p=apps%2Fpfixtools.git;a=blobdiff_plain;f=postlicyd%2Ffilter.c;h=7aba638679e61d9b6bc766992290e15c786ffec3;hp=a067ef3b564699888186f64e93b605295e49c06d;hb=59b8220d0227fe68537a563b3e5fa2e63e26bc0d;hpb=4e8709f2fc6338d2774c24c9c353deca1990f600 diff --git a/postlicyd/filter.c b/postlicyd/filter.c index a067ef3..7aba638 100644 --- a/postlicyd/filter.c +++ b/postlicyd/filter.c @@ -56,8 +56,6 @@ filter_type_t filter_register(const char *type, filter_constructor_t constructor filter_token tok = filter_tokenize(type, m_strlen(type)); CHECK_FILTER(tok); - syslog(LOG_DEBUG, "filter type %s registered", type); - runners[tok] = runner; constructors[tok] = constructor; destructors[tok] = destructor; @@ -71,9 +69,6 @@ filter_result_t filter_hook_register(filter_type_t filter, CHECK_FILTER(filter); CHECK_HOOK(tok); - syslog(LOG_DEBUG, "hook %s registered for filter type %s", name, - ftokens[filter]); - hooks[filter][tok] = true; return tok; } @@ -85,9 +80,6 @@ filter_param_id_t filter_param_register(filter_type_t filter, CHECK_FILTER(filter); CHECK_PARAM(tok); - syslog(LOG_INFO, "param %s registered for filter type %s", name, - ftokens[filter]); - params[filter][tok] = true; return tok; } @@ -119,8 +111,8 @@ bool filter_update_references(filter_t *filter, A(filter_t) *filter_list) if (!hook->postfix) { hook->filter_id = filter_find_with_name(filter_list, hook->value); if (hook->filter_id == -1) { - syslog(LOG_ERR, "invalid filter name %s for hook %s", - hook->value, htokens[hook->type]); + err("invalid filter name %s for hook %s", + hook->value, htokens[hook->type]); return false; } p_delete(&hook->value); @@ -129,36 +121,31 @@ bool filter_update_references(filter_t *filter, A(filter_t) *filter_list) return true; } -static inline bool filter_check_loop(filter_t *filter, A(filter_t) *array, bool cleanup) +static inline bool filter_check_loop(filter_t *filter, A(filter_t) *array, int level) { - if (cleanup) { - foreach (filter_t *filter2, *array) { - filter2->seen = false; - }} - } else if (filter->seen) { - return false; - } - if (filter->safe) { + if (filter->last_seen == level) { return true; } - filter->seen = true; + filter->last_seen = level; foreach (filter_hook_t *hook, filter->hooks) { if (hook->postfix) { continue; } - if (!filter_check_loop(array_ptr(*array, hook->filter_id), array, false)) { + if (hook->filter_id == level) { + return false; + } + if (!filter_check_loop(array_ptr(*array, hook->filter_id), array, level)) { return false; } }} - filter->safe = true; return true; } bool filter_check_safety(A(filter_t) *array) { foreach (filter_t *filter, *array) { - if (!filter_check_loop(filter, array, true)) { - syslog(LOG_ERR, "the filter tree contains a loop"); + if (!filter_check_loop(filter, array, __Ai)) { + err("the filter tree contains a loop"); return false; } }} @@ -180,21 +167,20 @@ const filter_hook_t *filter_run(const filter_t *filter, const query_t *query) { int start = 0; int end = filter->hooks.len; - //syslog(LOG_DEBUG, "running filter %s (%s)", - // filter->name, ftokens[filter->type]); + debug("running filter %s (%s)", filter->name, ftokens[filter->type]); filter_result_t res = runners[filter->type](filter, query); if (res == HTK_ABORT) { return NULL; } - //syslog(LOG_DEBUG, "filter run, result is %s", htokens[res]); + debug("filter run, result is %s", htokens[res]); while (start < end) { int mid = (start + end) / 2; filter_hook_t *hook = array_ptr(filter->hooks, mid); if (hook->type == res) { - //syslog(LOG_DEBUG, "return hook of type %s, value %s", - // htokens[hook->type], hook->value); + debug("return hook of type %s, value %s", + htokens[hook->type], hook->value); return hook; } else if (res < hook->type) { end = mid; @@ -202,55 +188,58 @@ const filter_hook_t *filter_run(const filter_t *filter, const query_t *query) start = mid + 1; } } - syslog(LOG_WARNING, "missing hook %s for filter %s", - htokens[res], filter->name); + warn("missing hook %s for filter %s", htokens[res], filter->name); return &default_hook; } -void filter_set_name(filter_t *filter, const char *name, ssize_t len) +bool filter_test(const filter_t *filter, const query_t *query, filter_result_t result) +{ + return !!(runners[filter->type](filter, query) == result); +} + +void filter_set_name(filter_t *filter, const char *name, int len) { - filter->name = p_new(char, len + 1); - memcpy(filter->name, name, len); - filter->name[len] = '\0'; + filter->name = p_dupstr(name, len); } -bool filter_set_type(filter_t *filter, const char *type, ssize_t len) +bool filter_set_type(filter_t *filter, const char *type, int len) { filter->type = filter_tokenize(type, len); return filter->type != FTK_UNKNOWN; } -bool filter_add_param(filter_t *filter, const char *name, ssize_t name_len, - const char *value, ssize_t value_len) +bool filter_add_param(filter_t *filter, const char *name, int name_len, + const char *value, int value_len) { filter_param_t param; param.type = param_tokenize(name, name_len); if (param.type == ATK_UNKNOWN) { - syslog(LOG_ERR, "unknown parameter %.*s", name_len, name); + err("unknown parameter %.*s", name_len, name); return false; } if (!params[filter->type][param.type]) { - syslog(LOG_ERR, "hook %s is not valid for filter %s", - atokens[param.type], ftokens[filter->type]); + err("hook %s is not valid for filter %s", + atokens[param.type], ftokens[filter->type]); return false; } - param.value = m_strdup(value); + param.value = p_dupstr(value, value_len); + param.value_len = value_len; array_add(filter->params, param); return true; } -bool filter_add_hook(filter_t *filter, const char *name, ssize_t name_len, - const char *value, ssize_t value_len) +bool filter_add_hook(filter_t *filter, const char *name, int name_len, + const char *value, int value_len) { filter_hook_t hook; hook.type = hook_tokenize(name, name_len); if (hook.type == HTK_UNKNOWN) { - syslog(LOG_ERR, "unknown hook type %.*s", name_len, name); + err("unknown hook type %.*s", name_len, name); return false; } if (!hooks[filter->type][hook.type] || hook.type == HTK_ABORT) { - syslog(LOG_ERR, "hook %s not is valid for filter %s", - htokens[hook.type], ftokens[filter->type]); + err("hook %s not is valid for filter %s", + htokens[hook.type], ftokens[filter->type]); return false; } hook.postfix = (strncmp(value, "postfix:", 8) == 0);