1 /******************************************************************************/
2 /* pfixtools: a collection of postfix related tools */
4 /* ________________________________________________________________________ */
6 /* Redistribution and use in source and binary forms, with or without */
7 /* modification, are permitted provided that the following conditions */
10 /* 1. Redistributions of source code must retain the above copyright */
11 /* notice, this list of conditions and the following disclaimer. */
12 /* 2. Redistributions in binary form must reproduce the above copyright */
13 /* notice, this list of conditions and the following disclaimer in the */
14 /* documentation and/or other materials provided with the distribution. */
15 /* 3. The names of its contributors may not be used to endorse or promote */
16 /* products derived from this software without specific prior written */
19 /* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS */
20 /* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY */
23 /* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL */
24 /* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS */
25 /* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) */
26 /* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, */
27 /* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN */
28 /* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
29 /* POSSIBILITY OF SUCH DAMAGE. */
31 /* Copyright (c) 2006-2008 the Authors */
32 /* see AUTHORS and source files for details */
33 /******************************************************************************/
36 * Copyright © 2008 Florent Bruneau
43 static filter_runner_t runners[FTK_count];
44 static filter_constructor_t constructors[FTK_count];
45 static filter_destructor_t destructors[FTK_count];
46 static bool hooks[FTK_count][HTK_count];
47 static bool params[FTK_count][ATK_count];
49 static filter_context_constructor_t ctx_constructors[FTK_count];
50 static filter_context_destructor_t ctx_destructors[FTK_count];
51 static filter_async_handler_t async_handler = NULL;
53 static const filter_hook_t default_hook = {
55 .value = (char*)"DUNNO",
63 static const filter_hook_t async_hook = {
73 uint32_t filter_running = 0;
75 filter_type_t filter_register(const char *type, filter_constructor_t constructor,
76 filter_destructor_t destructor, filter_runner_t runner,
77 filter_context_constructor_t context_constructor,
78 filter_context_destructor_t context_destructor)
80 filter_token tok = filter_tokenize(type, m_strlen(type));
83 runners[tok] = runner;
84 constructors[tok] = constructor;
85 destructors[tok] = destructor;
87 ctx_constructors[tok] = context_constructor;
88 ctx_destructors[tok] = context_destructor;
92 filter_result_t filter_hook_register(filter_type_t filter,
95 filter_result_t tok = hook_tokenize(name, m_strlen(name));
99 hooks[filter][tok] = true;
103 filter_param_id_t filter_param_register(filter_type_t filter,
106 filter_param_id_t tok = param_tokenize(name, m_strlen(name));
107 CHECK_FILTER(filter);
110 params[filter][tok] = true;
114 void filter_async_handler_register(filter_async_handler_t handler)
116 async_handler = handler;
119 bool filter_build(filter_t *filter)
122 if (filter->type == FTK_UNKNOWN || filter->name == NULL) {
125 if (filter->hooks.len > 0) {
126 # define QSORT_TYPE filter_hook_t
127 # define QSORT_BASE filter->hooks.data
128 # define QSORT_NELT filter->hooks.len
129 # define QSORT_LT(a,b) a->type < b->type
132 filter_constructor_t constructor = constructors[filter->type];
134 ret = constructor(filter);
136 array_deep_wipe(filter->params, filter_params_wipe);
140 bool filter_update_references(filter_t *filter, A(filter_t) *filter_list)
142 foreach (filter_hook_t *hook, filter->hooks) {
143 if (!hook->postfix) {
144 hook->filter_id = filter_find_with_name(filter_list, hook->value);
145 if (hook->filter_id == -1) {
146 err("invalid filter name %s for hook %s",
147 hook->value, htokens[hook->type]);
150 p_delete(&hook->value);
156 static inline bool filter_check_loop(filter_t *filter, A(filter_t) *array, int level)
158 if (filter->last_seen == level) {
161 filter->last_seen = level;
162 foreach (filter_hook_t *hook, filter->hooks) {
166 if (hook->filter_id == level) {
169 if (!filter_check_loop(array_ptr(*array, hook->filter_id), array, level)) {
176 bool filter_check_safety(A(filter_t) *array)
178 foreach (filter_t *filter, *array) {
179 if (!filter_check_loop(filter, array, __Ai)) {
180 err("the filter tree contains a loop");
187 void filter_wipe(filter_t *filter)
189 filter_destructor_t destructor = destructors[filter->type];
193 array_deep_wipe(filter->hooks, filter_hook_wipe);
194 array_deep_wipe(filter->params, filter_params_wipe);
195 p_delete(&filter->name);
198 static inline const filter_hook_t *filter_hook_for_result(const filter_t *filter,
202 int end = filter->hooks.len;
204 if (res == HTK_ABORT) {
207 if (res == HTK_ASYNC) {
211 while (start < end) {
212 int mid = (start + end) / 2;
213 filter_hook_t *hook = array_ptr(filter->hooks, mid);
214 if (hook->type == res) {
215 debug("return hook of type %s, value %s",
216 htokens[hook->type], hook->value);
218 } else if (res < hook->type) {
224 warn("missing hook %s for filter %s", htokens[res], filter->name);
225 return &default_hook;
228 const filter_hook_t *filter_run(const filter_t *filter, const query_t *query,
229 filter_context_t *context)
231 debug("running filter %s (%s)", filter->name, ftokens[filter->type]);
233 filter_result_t res = runners[filter->type](filter, query, context);
235 if (res == HTK_ASYNC) {
236 context->current_filter = filter;
239 context->current_filter = NULL;
242 debug("filter run, result is %s", htokens[res]);
243 return filter_hook_for_result(filter, res);
246 bool filter_test(const filter_t *filter, const query_t *query,
247 filter_context_t *context, filter_result_t result)
249 return !!(runners[filter->type](filter, query, context) == result);
252 void filter_set_name(filter_t *filter, const char *name, int len)
254 filter->name = p_dupstr(name, len);
257 bool filter_set_type(filter_t *filter, const char *type, int len)
259 filter->type = filter_tokenize(type, len);
260 return filter->type != FTK_UNKNOWN;
263 bool filter_add_param(filter_t *filter, const char *name, int name_len,
264 const char *value, int value_len)
266 filter_param_t param;
267 param.type = param_tokenize(name, name_len);
268 if (param.type == ATK_UNKNOWN) {
269 err("unknown parameter %.*s", name_len, name);
272 if (!params[filter->type][param.type]) {
273 err("hook %s is not valid for filter %s",
274 atokens[param.type], ftokens[filter->type]);
277 param.value = p_dupstr(value, value_len);
278 param.value_len = value_len;
279 array_add(filter->params, param);
283 bool filter_add_hook(filter_t *filter, const char *name, int name_len,
284 const char *value, int value_len)
288 hook.type = hook_tokenize(name, name_len);
289 if (hook.type == HTK_UNKNOWN) {
290 err("unknown hook type %.*s", name_len, name);
293 if (!hooks[filter->type][hook.type] || hook.type == HTK_ABORT) {
294 err("hook %s not is valid for filter %s",
295 htokens[hook.type], ftokens[filter->type]);
300 /* Value format is (counter:id:incr)?(postfix:reply|filter_name)
303 if (strncmp(value, "counter:", 8) == 0) {
306 hook.counter = strtol(value, &end, 10);
307 if (end == value || *end != ':') {
308 err("hook %s, cannot read counter id", htokens[hook.type]);
310 } else if (hook.counter < 0 || hook.counter >= MAX_COUNTERS) {
311 err("hook %s, invalid counter id %d", htokens[hook.type], hook.counter);
315 hook.cost = strtol(value, &end, 10);
316 if (end == value || *end != ':') {
317 err("hook %s, cannot read counter increment", htokens[hook.type]);
319 } else if (hook.cost < 0) {
320 err("hook %s, invalid counter increment value %d", htokens[hook.type],
329 hook.postfix = (strncmp(value, "postfix:", 8) == 0);
330 if (hook.postfix && !query_format_check(value + 8)) {
331 err("invalid formatted text \"%s\"", value + 8);
334 hook.value = m_strdup(hook.postfix ? value + 8 : value);
335 array_add(filter->hooks, hook);
339 void filter_context_prepare(filter_context_t *context, void *qctx)
341 for (int i = 0 ; i < FTK_count ; ++i) {
342 if (ctx_constructors[i] != NULL) {
343 context->contexts[i] = ctx_constructors[i]();
346 context->current_filter = NULL;
347 context->data = qctx;
350 void filter_context_wipe(filter_context_t *context)
352 for (int i = 0 ; i < FTK_count ; ++i) {
353 if (ctx_destructors[i] != NULL) {
354 ctx_destructors[i](context->contexts[i]);
359 void filter_context_clean(filter_context_t *context)
361 p_clear(&context->counters, 1);
362 context->instance[0] = '\0';
365 void filter_post_async_result(filter_context_t *context, filter_result_t result)
367 const filter_t *filter = context->current_filter;
368 const filter_hook_t *hook = NULL;
370 if (result == HTK_ASYNC) {
374 hook = filter_hook_for_result(filter, result);
375 async_handler(context, hook);