X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=postlicyd%2Ffilter.h;h=39c311966b4e5e74803bb343a255b3e904250c8e;hb=f93216b477be3efccfb4a5c0db3a4ee1730a17b2;hp=f532d1a97f7c20d9cf5c31ce5a096bf8175a3f6d;hpb=9b993811fe1fd208afd954e86c0bfc81a78af04f;p=apps%2Fpfixtools.git diff --git a/postlicyd/filter.h b/postlicyd/filter.h index f532d1a..39c3119 100644 --- a/postlicyd/filter.h +++ b/postlicyd/filter.h @@ -38,61 +38,262 @@ #include "common.h" #include "filter_tokens.h" +#include "hook_tokens.h" +#include "param_tokens.h" #include "query.h" +#include "array.h" + typedef filter_token filter_type_t; +typedef hook_token filter_result_t; +typedef param_token filter_param_id_t; typedef struct filter_hook_t { - const char *name; - const char *value; + filter_result_t type; + char *value; + + int counter; + int cost; + + unsigned postfix:1; + unsigned async:1; + int filter_id; + } filter_hook_t; +ARRAY(filter_hook_t) -typedef struct filter_params_t { - const char *name; - const char *value; -} filter_params_t; +typedef struct filter_param_t { + filter_param_id_t type; + char *value; + int value_len; +} filter_param_t; +ARRAY(filter_param_t) +/** Description of a filter. + */ typedef struct filter_t { char *name; filter_type_t type; - filter_hook_t *hooks; - filter_params_t *params; + A(filter_hook_t) hooks; void *data; + + A(filter_param_t) params; + + /* Loop checking flags. + */ + int last_seen; } filter_t; +ARRAY(filter_t) + +#define MAX_COUNTERS (64) + +/** Context of the query. To be filled with data to use when + * performing asynchronous filtering. + */ +typedef struct filter_context_t { + /* filter context + */ + const filter_t *current_filter; + void *contexts[FTK_count]; + + /* message context + */ + char instance[64]; + uint32_t counters[MAX_COUNTERS]; + + /* connection context + */ + void *data; +} filter_context_t; + + +#define FILTER_INIT { NULL, FTK_UNKNOWN, ARRAY_INIT, NULL, ARRAY_INIT, -1 } +#define CHECK_FILTER(Filter) \ + assert(Filter != FTK_UNKNOWN && Filter != FTK_count \ + && "Unknown filter type") +#define CHECK_HOOK(Hook) \ + assert(Hook != HTK_UNKNOWN && Hook != HTK_count \ + && "Unknown hook") +#define CHECK_PARAM(Param) \ + assert(Param != ATK_UNKNOWN && Param != ATK_count \ + && "Unknown param") + + +/* Callback to be implemented by a filter. + */ -typedef const char *filter_result_t; typedef filter_result_t (*filter_runner_t)(const filter_t *filter, - const query_t *query); + const query_t *query, + filter_context_t *context); typedef bool (*filter_constructor_t)(filter_t *filter); typedef void (*filter_destructor_t)(filter_t *filter); +typedef void *(*filter_context_constructor_t)(void); +typedef void (*filter_context_destructor_t)(void*); + +typedef void (*filter_async_handler_t)(filter_context_t *context, + const filter_hook_t *result); + +/** Number of filter currently running. + */ +extern uint32_t filter_running; + +/* Registration. + */ + __attribute__((nonnull(1,4))) -void filter_register(const char *type, filter_constructor_t constructor, - filter_destructor_t destructor, filter_runner_t runner); +filter_type_t filter_register(const char *type, filter_constructor_t constructor, + filter_destructor_t destructor, filter_runner_t runner, + filter_context_constructor_t context_constructor, + filter_context_destructor_t context_destructor); + +__attribute__((nonnull(2))) +filter_result_t filter_hook_register(filter_type_t filter, const char *name); + +__attribute__((nonnull(2))) +filter_param_id_t filter_param_register(filter_type_t filter, const char *name); + +__attribute__((nonnull)) +void filter_async_handler_register(filter_async_handler_t handler); + +/* Filter builder. + */ + +__attribute__((nonnull(1))) +static inline void filter_init(filter_t *filter) +{ + const filter_t f = FILTER_INIT; + *filter = f; +} __attribute__((nonnull(1,2))) -bool filter_set_name(filter_t *filter, const char *name, ssize_t len); +void filter_set_name(filter_t *filter, const char *name, int len); __attribute__((nonnull(1,2))) -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); __attribute__((nonnull(1,2,4))) -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); __attribute__((nonnull(1,2,4))) -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); __attribute__((nonnull(1))) bool filter_build(filter_t *filter); +__attribute__((nonnull(1,2))) +static inline int filter_find_with_name(const A(filter_t) *array, const char *name) +{ + int start = 0; + int end = array->len; + + while (start < end) { + int mid = (start + end) / 2; + int cmp = strcmp(name, array_elt(*array, mid).name); + + if (cmp == 0) { + return mid; + } else if (cmp < 0) { + end = mid; + } else { + start = mid + 1; + } + } + return -1; +} + +__attribute__((nonnull(1,2))) +bool filter_update_references(filter_t *filter, A(filter_t) *array); + +__attribute__((nonnull(1))) +bool filter_check_safety(A(filter_t) *array); + +__attribute__((nonnull(1))) +static inline void filter_hook_wipe(filter_hook_t *hook) +{ + p_delete(&hook->value); +} + +__attribute__((nonnull(1))) +static inline void filter_params_wipe(filter_param_t *param) +{ + p_delete(¶m->value); +} + __attribute__((nonnull(1))) void filter_wipe(filter_t *filter); + +/* Runner. + */ + __attribute__((nonnull(1,2))) -filter_result_t filter_run(const filter_t *filter, const query_t *query); +const filter_hook_t *filter_run(const filter_t *filter, const query_t *query, + filter_context_t *context); + +__attribute__((nonnull(1,2))) +bool filter_test(const filter_t *filter, const query_t *query, + filter_context_t *context, filter_result_t expt); + + +/* Parsing Helpers + */ + +#define FILTER_PARAM_PARSE_STRING(Param, Dest) \ + case ATK_ ## Param: { \ + (Dest) = param->value; \ + } break + +#define FILTER_PARAM_PARSE_INT(Param, Dest) \ + case ATK_ ## Param: { \ + char *next; \ + (Dest) = strtol(param->value, &next, 10); \ + PARSE_CHECK(!*next, "invalid %s value %.*s", atokens[ATK_ ## Param], \ + param->value_len, param->value); \ + } break + +#define FILTER_PARAM_PARSE_BOOLEAN(Param, Dest) \ + case ATK_ ## Param: { \ + if (param->value_len == 1 && param->value[0] == '1') { \ + (Dest) = true; \ + } else if (param->value_len == 1 && param->value[0] == '0') { \ + (Dest) = false; \ + } else if (param->value_len == 4 \ + && ascii_tolower(param->value[0]) == 't' \ + && ascii_tolower(param->value[1]) == 'r' \ + && ascii_tolower(param->value[2]) == 'u' \ + && ascii_tolower(param->value[3]) == 'e') { \ + (Dest) = true; \ + } else if (param->value_len == 5 \ + && ascii_tolower(param->value[0]) == 'f' \ + && ascii_tolower(param->value[1]) == 'a' \ + && ascii_tolower(param->value[2]) == 'l' \ + && ascii_tolower(param->value[3]) == 's' \ + && ascii_tolower(param->value[4]) == 'e') { \ + (Dest) = false; \ + } else { \ + PARSE_CHECK(false, "invalid %s value %.*s", atokens[ATK_ ## Param],\ + param->value_len, param->value); \ + } \ + } break + + +/* Filter context + */ + +__attribute__((nonnull(1))) +void filter_context_prepare(filter_context_t *context, void* qctx); + +__attribute__((nonnull)) +void filter_context_wipe(filter_context_t *context); + +__attribute__((nonnull)) +void filter_context_clean(filter_context_t *context); +__attribute__((nonnull)) +void filter_post_async_result(filter_context_t *context, filter_result_t result); #endif