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
39 #ifndef PFIXTOOLS_FILTER_H
40 #define PFIXTOOLS_FILTER_H
43 #include "filter_tokens.h"
44 #include "hook_tokens.h"
45 #include "param_tokens.h"
50 typedef filter_token filter_type_t;
51 typedef hook_token filter_result_t;
52 typedef param_token filter_param_id_t;
54 typedef struct filter_hook_t {
68 typedef struct filter_param_t {
69 filter_param_id_t type;
75 /** Description of a filter.
77 typedef struct filter_t {
81 A(filter_hook_t) hooks;
84 A(filter_param_t) params;
86 /* Loop checking flags.
92 #define MAX_COUNTERS (64)
94 /** Context of the query. To be filled with data to use when
95 * performing asynchronous filtering.
97 typedef struct filter_context_t {
100 const filter_t *current_filter;
101 void *contexts[FTK_count];
106 uint32_t counters[MAX_COUNTERS];
108 /* connection context
114 #define FILTER_INIT { NULL, FTK_UNKNOWN, ARRAY_INIT, NULL, ARRAY_INIT, -1 }
115 #define CHECK_FILTER(Filter) \
116 assert(Filter != FTK_UNKNOWN && Filter != FTK_count \
117 && "Unknown filter type")
118 #define CHECK_HOOK(Hook) \
119 assert(Hook != HTK_UNKNOWN && Hook != HTK_count \
121 #define CHECK_PARAM(Param) \
122 assert(Param != ATK_UNKNOWN && Param != ATK_count \
126 /* Callback to be implemented by a filter.
129 typedef filter_result_t (*filter_runner_t)(const filter_t *filter,
130 const query_t *query,
131 filter_context_t *context);
132 typedef bool (*filter_constructor_t)(filter_t *filter);
133 typedef void (*filter_destructor_t)(filter_t *filter);
135 typedef void *(*filter_context_constructor_t)(void);
136 typedef void (*filter_context_destructor_t)(void*);
138 typedef void (*filter_async_handler_t)(filter_context_t *context,
139 const filter_hook_t *result);
141 /** Number of filter currently running.
143 extern uint32_t filter_running;
148 __attribute__((nonnull(1,4)))
149 filter_type_t filter_register(const char *type, filter_constructor_t constructor,
150 filter_destructor_t destructor, filter_runner_t runner,
151 filter_context_constructor_t context_constructor,
152 filter_context_destructor_t context_destructor);
154 __attribute__((nonnull(2)))
155 filter_result_t filter_hook_register(filter_type_t filter, const char *name);
157 __attribute__((nonnull(2)))
158 filter_param_id_t filter_param_register(filter_type_t filter, const char *name);
160 __attribute__((nonnull))
161 void filter_async_handler_register(filter_async_handler_t handler);
166 __attribute__((nonnull(1)))
167 static inline void filter_init(filter_t *filter)
169 const filter_t f = FILTER_INIT;
173 __attribute__((nonnull(1,2)))
174 void filter_set_name(filter_t *filter, const char *name, int len);
176 __attribute__((nonnull(1,2)))
177 bool filter_set_type(filter_t *filter, const char *type, int len);
179 __attribute__((nonnull(1,2,4)))
180 bool filter_add_param(filter_t *filter, const char *name, int name_len,
181 const char *value, int value_len);
183 __attribute__((nonnull(1,2,4)))
184 bool filter_add_hook(filter_t *filter, const char *name, int name_len,
185 const char *value, int value_len);
187 __attribute__((nonnull(1)))
188 bool filter_build(filter_t *filter);
190 __attribute__((nonnull(1,2)))
191 static inline int filter_find_with_name(const A(filter_t) *array, const char *name)
194 int end = array->len;
196 while (start < end) {
197 int mid = (start + end) / 2;
198 int cmp = strcmp(name, array_elt(*array, mid).name);
202 } else if (cmp < 0) {
211 __attribute__((nonnull(1,2)))
212 bool filter_update_references(filter_t *filter, A(filter_t) *array);
214 __attribute__((nonnull(1)))
215 bool filter_check_safety(A(filter_t) *array);
217 __attribute__((nonnull(1)))
218 static inline void filter_hook_wipe(filter_hook_t *hook)
220 p_delete(&hook->value);
223 __attribute__((nonnull(1)))
224 static inline void filter_params_wipe(filter_param_t *param)
226 p_delete(¶m->value);
229 __attribute__((nonnull(1)))
230 void filter_wipe(filter_t *filter);
236 __attribute__((nonnull(1,2)))
237 const filter_hook_t *filter_run(const filter_t *filter, const query_t *query,
238 filter_context_t *context);
240 __attribute__((nonnull(1,2)))
241 bool filter_test(const filter_t *filter, const query_t *query,
242 filter_context_t *context, filter_result_t expt);
248 #define FILTER_PARAM_PARSE_STRING(Param, Dest, Copy) \
249 case ATK_ ## Param: { \
250 (Dest) = (Copy) ? m_strdup(param->value) : param->value; \
253 #define FILTER_PARAM_PARSE_INT(Param, Dest) \
254 case ATK_ ## Param: { \
256 (Dest) = strtol(param->value, &next, 10); \
257 PARSE_CHECK(!*next, "invalid %s value %.*s", atokens[ATK_ ## Param], \
258 param->value_len, param->value); \
261 #define FILTER_PARAM_PARSE_BOOLEAN(Param, Dest) \
262 case ATK_ ## Param: { \
263 if (param->value_len == 1 && param->value[0] == '1') { \
265 } else if (param->value_len == 1 && param->value[0] == '0') { \
267 } else if (param->value_len == 4 \
268 && ascii_tolower(param->value[0]) == 't' \
269 && ascii_tolower(param->value[1]) == 'r' \
270 && ascii_tolower(param->value[2]) == 'u' \
271 && ascii_tolower(param->value[3]) == 'e') { \
273 } else if (param->value_len == 5 \
274 && ascii_tolower(param->value[0]) == 'f' \
275 && ascii_tolower(param->value[1]) == 'a' \
276 && ascii_tolower(param->value[2]) == 'l' \
277 && ascii_tolower(param->value[3]) == 's' \
278 && ascii_tolower(param->value[4]) == 'e') { \
281 PARSE_CHECK(false, "invalid %s value %.*s", atokens[ATK_ ## Param],\
282 param->value_len, param->value); \
290 __attribute__((nonnull(1)))
291 void filter_context_prepare(filter_context_t *context, void* qctx);
293 __attribute__((nonnull))
294 void filter_context_wipe(filter_context_t *context);
296 __attribute__((nonnull))
297 void filter_context_clean(filter_context_t *context);
299 __attribute__((nonnull))
300 void filter_post_async_result(filter_context_t *context, filter_result_t result);