Move some code.
[apps/pfixtools.git] / postlicyd / filter.c
1 /******************************************************************************/
2 /*          pfixtools: a collection of postfix related tools                  */
3 /*          ~~~~~~~~~                                                         */
4 /*  ________________________________________________________________________  */
5 /*                                                                            */
6 /*  Redistribution and use in source and binary forms, with or without        */
7 /*  modification, are permitted provided that the following conditions        */
8 /*  are met:                                                                  */
9 /*                                                                            */
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     */
17 /*     permission.                                                            */
18 /*                                                                            */
19 /*  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND   */
20 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE     */
21 /*  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR        */
22 /*  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS    */
23 /*  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR    */
24 /*  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF      */
25 /*  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  */
26 /*  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN   */
27 /*  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)   */
28 /*  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF    */
29 /*  THE POSSIBILITY OF SUCH DAMAGE.                                           */
30 /******************************************************************************/
31
32 /*
33  * Copyright © 2008 Florent Bruneau
34  */
35
36 #include "str.h"
37 #include "buffer.h"
38 #include "filter.h"
39
40 static filter_runner_t      runners[FTK_count];
41 static filter_constructor_t constructors[FTK_count];
42 static filter_destructor_t  destructors[FTK_count];
43 static bool                 hooks[FTK_count][HTK_count];
44 static bool                 params[FTK_count][ATK_count];
45
46 static filter_context_constructor_t ctx_constructors[FTK_count];
47 static filter_context_destructor_t  ctx_destructors[FTK_count];
48 static filter_async_handler_t       async_handler = NULL;
49
50 static const filter_hook_t default_hook = {
51     .type      = 0,
52     .value     = (char*)"DUNNO",
53     .postfix   = true,
54     .async     = false,
55     .filter_id = 0
56 };
57
58 static const filter_hook_t async_hook = {
59     .type      = 0,
60     .value     = NULL,
61     .postfix   = false,
62     .async     = true,
63     .filter_id = 0
64 };
65
66 uint32_t filter_running = 0;
67
68 filter_type_t filter_register(const char *type, filter_constructor_t constructor,
69                               filter_destructor_t destructor, filter_runner_t runner,
70                               filter_context_constructor_t context_constructor,
71                               filter_context_destructor_t context_destructor)
72 {
73     filter_token tok = filter_tokenize(type, m_strlen(type));
74     CHECK_FILTER(tok);
75
76     runners[tok] = runner;
77     constructors[tok] = constructor;
78     destructors[tok] = destructor;
79
80     ctx_constructors[tok] = context_constructor;
81     ctx_destructors[tok]  = context_destructor;
82     return tok;
83 }
84
85 filter_result_t filter_hook_register(filter_type_t filter,
86                                      const char *name)
87 {
88     filter_result_t tok = hook_tokenize(name, m_strlen(name));
89     CHECK_FILTER(filter);
90     CHECK_HOOK(tok);
91
92     hooks[filter][tok] = true;
93     return tok;
94 }
95
96 filter_param_id_t filter_param_register(filter_type_t filter,
97                                         const char *name)
98 {
99     filter_param_id_t tok = param_tokenize(name, m_strlen(name));
100     CHECK_FILTER(filter);
101     CHECK_PARAM(tok);
102
103     params[filter][tok] = true;
104     return tok;
105 }
106
107 void filter_async_handler_register(filter_async_handler_t handler)
108 {
109     async_handler = handler;
110 }
111
112 bool filter_build(filter_t *filter)
113 {
114     bool ret = true;
115     if (filter->type == FTK_UNKNOWN || filter->name == NULL) {
116         return false;
117     }
118     if (filter->hooks.len > 0) {
119 #       define QSORT_TYPE filter_hook_t
120 #       define QSORT_BASE filter->hooks.data
121 #       define QSORT_NELT filter->hooks.len
122 #       define QSORT_LT(a,b) a->type < b->type
123 #       include "qsort.c"
124     }
125     filter_constructor_t constructor = constructors[filter->type];
126     if (constructor) {
127         ret = constructor(filter);
128     }
129     array_deep_wipe(filter->params, filter_params_wipe);
130     return ret;
131 }
132
133 bool filter_update_references(filter_t *filter, A(filter_t) *filter_list)
134 {
135     foreach (filter_hook_t *hook, filter->hooks) {
136         if (!hook->postfix) {
137             hook->filter_id = filter_find_with_name(filter_list, hook->value);
138             if (hook->filter_id == -1) {
139                 err("invalid filter name %s for hook %s",
140                     hook->value, htokens[hook->type]);
141                 return false;
142             }
143             p_delete(&hook->value);
144         }
145     }}
146     return true;
147 }
148
149 static inline bool filter_check_loop(filter_t *filter, A(filter_t) *array, int level)
150 {
151     if (filter->last_seen == level) {
152         return true;
153     }
154     filter->last_seen = level;
155     foreach (filter_hook_t *hook, filter->hooks) {
156         if (hook->postfix) {
157             continue;
158         }
159         if (hook->filter_id == level) {
160             return false;
161         }
162         if (!filter_check_loop(array_ptr(*array, hook->filter_id), array, level)) {
163             return false;
164         }
165     }}
166     return true;
167 }
168
169 bool filter_check_safety(A(filter_t) *array)
170 {
171     foreach (filter_t *filter, *array) {
172         if (!filter_check_loop(filter, array, __Ai)) {
173             err("the filter tree contains a loop");
174             return false;
175         }
176     }}
177     return true;
178 }
179
180 void filter_wipe(filter_t *filter)
181 {
182     filter_destructor_t destructor = destructors[filter->type];
183     if (destructor) {
184         destructor(filter);
185     }
186     array_deep_wipe(filter->hooks, filter_hook_wipe);
187     array_deep_wipe(filter->params, filter_params_wipe);
188     p_delete(&filter->name);
189 }
190
191 static inline const filter_hook_t *filter_hook_for_result(const filter_t *filter,
192                                                           filter_result_t res)
193 {
194     int start = 0;
195     int end   = filter->hooks.len;
196
197     if (res == HTK_ABORT) {
198         return NULL;
199     }
200     if (res == HTK_ASYNC) {
201         return &async_hook;
202     }
203
204     while (start < end) {
205         int mid = (start + end) / 2;
206         filter_hook_t *hook = array_ptr(filter->hooks, mid);
207         if (hook->type == res) {
208             debug("return hook of type %s, value %s",
209                   htokens[hook->type], hook->value);
210             return hook;
211         } else if (res < hook->type) {
212             end = mid;
213         } else {
214             start = mid + 1;
215         }
216     }
217     warn("missing hook %s for filter %s", htokens[res], filter->name);
218     return &default_hook;
219 }
220
221 const filter_hook_t *filter_run(const filter_t *filter, const query_t *query,
222                                 filter_context_t *context)
223 {
224     debug("running filter %s (%s)", filter->name, ftokens[filter->type]);
225     ++filter_running;
226     filter_result_t res = runners[filter->type](filter, query, context);
227
228     if (res == HTK_ASYNC) {
229         context->current_filter = filter;
230     } else {
231         --filter_running;
232         context->current_filter = NULL;
233     }
234
235     debug("filter run, result is %s", htokens[res]);
236     return filter_hook_for_result(filter, res);
237 }
238
239 bool filter_test(const filter_t *filter, const query_t *query,
240                  filter_context_t *context, filter_result_t result)
241 {
242     return !!(runners[filter->type](filter, query, context) == result);
243 }
244
245 void filter_set_name(filter_t *filter, const char *name, int len)
246 {
247     filter->name = p_dupstr(name, len);
248 }
249
250 bool filter_set_type(filter_t *filter, const char *type, int len)
251 {
252     filter->type = filter_tokenize(type, len);
253     return filter->type != FTK_UNKNOWN;
254 }
255
256 bool filter_add_param(filter_t *filter, const char *name, int name_len,
257                       const char *value, int value_len)
258 {
259     filter_param_t param;
260     param.type = param_tokenize(name, name_len);
261     if (param.type == ATK_UNKNOWN) {
262         err("unknown parameter %.*s", name_len, name);
263         return false;
264     }
265     if (!params[filter->type][param.type]) {
266         err("hook %s is not valid for filter %s",
267             atokens[param.type], ftokens[filter->type]);
268         return false;
269     }
270     param.value     = p_dupstr(value, value_len);
271     param.value_len = value_len;
272     array_add(filter->params, param);
273     return true;
274 }
275
276 bool filter_add_hook(filter_t *filter, const char *name, int name_len,
277                      const char *value, int value_len)
278 {
279     filter_hook_t hook;
280     hook.type  = hook_tokenize(name, name_len);
281     if (hook.type == HTK_UNKNOWN) {
282         err("unknown hook type %.*s", name_len, name);
283         return false;
284     }
285     if (!hooks[filter->type][hook.type] || hook.type == HTK_ABORT) {
286         err("hook %s not is valid for filter %s",
287             htokens[hook.type], ftokens[filter->type]);
288         return false;
289     }
290     hook.async   = false;
291     hook.postfix = (strncmp(value, "postfix:", 8) == 0);
292     hook.value = m_strdup(hook.postfix ? value + 8 : value);
293     hook.filter_id = -1;
294     array_add(filter->hooks, hook);
295     return true;
296 }
297
298 void filter_context_prepare(filter_context_t *context, void *qctx)
299 {
300     for (int i = 0 ; i < FTK_count ; ++i) {
301         if (ctx_constructors[i] != NULL) {
302             context->contexts[i] = ctx_constructors[i]();
303         }
304     }
305     context->current_filter = NULL;
306     context->data = qctx;
307 }
308
309 void filter_context_wipe(filter_context_t *context)
310 {
311     for (int i = 0 ; i < FTK_count ; ++i) {
312         if (ctx_destructors[i] != NULL) {
313             ctx_destructors[i](context->contexts[i]);
314         }
315     }
316 }
317
318 void filter_post_async_result(filter_context_t *context, filter_result_t result)
319 {
320     const filter_t *filter = context->current_filter;
321     const filter_hook_t *hook = NULL;
322
323     if (result == HTK_ASYNC) {
324         return;
325     }
326     --filter_running;
327     hook = filter_hook_for_result(filter, result);
328     async_handler(context, hook);
329 }