From: Florent Bruneau Date: Mon, 15 Sep 2008 14:46:29 +0000 (+0200) Subject: Implements filter runner. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=86bf8f7a016cfd8475ffc363a4cac0a29a4aacfd;p=apps%2Fpfixtools.git Implements filter runner. Signed-off-by: Florent Bruneau --- diff --git a/postlicyd/filter.c b/postlicyd/filter.c index f00a89a..aadc58c 100644 --- a/postlicyd/filter.c +++ b/postlicyd/filter.c @@ -133,9 +133,26 @@ void filter_wipe(filter_t *filter) p_delete(&filter->name); } -filter_result_t filter_run(const filter_t *filter, const query_t *query) +filter_hook_t *filter_run(const filter_t *filter, const query_t *query) { - return runners[filter->type](filter, query); + int start = 0; + int end = filter->hooks.len; + filter_result_t res = runners[filter->type](filter, query); + + while (start < end) { + int mid = (start + end) / 2; + filter_hook_t *hook = array_ptr(filter->hooks, mid); + if (hook->type == res) { + return hook; + } else if (res < hook->type) { + end = mid; + } else { + start = mid + 1; + } + } + syslog(LOG_WARNING, "missing hook %s for filter %s", + htokens[res], filter->name); + return NULL; } void filter_set_name(filter_t *filter, const char *name, ssize_t len) diff --git a/postlicyd/filter.h b/postlicyd/filter.h index 12bde4b..8a1aacb 100644 --- a/postlicyd/filter.h +++ b/postlicyd/filter.h @@ -163,7 +163,7 @@ __attribute__((nonnull(1))) void filter_wipe(filter_t *filter); __attribute__((nonnull(1,2))) -filter_result_t filter_run(const filter_t *filter, const query_t *query); +filter_hook_t *filter_run(const filter_t *filter, const query_t *query); #endif diff --git a/postlicyd/main-postlicyd.c b/postlicyd/main-postlicyd.c index 3cce400..ad61029 100644 --- a/postlicyd/main-postlicyd.c +++ b/postlicyd/main-postlicyd.c @@ -166,25 +166,31 @@ static void policy_answer(server_t *pcy, const char *fmt, ...) epoll_modify(pcy->fd, EPOLLIN | EPOLLOUT, pcy); } -static bool policy_run_filter(const query_t* query, void* filter, void* conf) -{ - return false; -} - -static void policy_process(server_t *pcy) +static void policy_process(server_t *pcy, config_t *config) { const query_t* query = pcy->data; - if (!policy_run_filter(query, NULL, NULL)) { - policy_answer(pcy, "DUNNO"); + filter_t *filter = array_ptr(config->filters, config->entry_point); + while (true) { + filter_hook_t *hook = filter_run(filter, query); + if (hook == NULL) { + policy_answer(pcy, "DUNNO"); + return; + } else if (hook->postfix) { + policy_answer(pcy, "%s", hook->value); + return; + } else { + filter = array_ptr(config->filters, hook->filter_id); + } } } -static int policy_run(server_t *pcy, void* config) +static int policy_run(server_t *pcy, void* vconfig) { ssize_t search_offs = MAX(0, pcy->ibuf.len - 1); int nb = buffer_read(&pcy->ibuf, pcy->fd, -1); const char *eoq; - query_t* query = pcy->data; + query_t *query = pcy->data; + config_t *config = vconfig; if (nb < 0) { if (errno == EAGAIN || errno == EINTR) @@ -205,7 +211,7 @@ static int policy_run(server_t *pcy, void* config) return -1; query->eoq = eoq + strlen("\n\n"); epoll_modify(pcy->fd, 0, pcy); - policy_process(pcy); + policy_process(pcy, config); return 0; }