Browse Source

* refactor filter modules library interface

master
Alex 'AdUser' Z 4 years ago
parent
commit
31c201a44b
  1. 7
      src/filter-test.c
  2. 16
      src/filter.c
  3. 14
      src/filter.h
  4. 1
      src/log.h

7
src/filter-test.c

@ -23,7 +23,6 @@ int main(int argc, char *argv[]) {
char line[LOGLINE_MAX] = "";
char stats[4096];
size_t read = 0, matched = 0;
const char *error;
FILE *file = NULL;
if (argc < 3)
@ -62,12 +61,8 @@ int main(int argc, char *argv[]) {
matched++;
fprintf(stdout, "+ %s\n", match);
continue;
}
error = f2b_filter_error(filter);
if (*error == '\0') {
fprintf(stdout, "- (no-match): %s", line);
} else {
fprintf(stdout, "! (error) : %s\n", error);
fprintf(stdout, "- (no-match): %s", line);
}
}
fclose(file);

16
src/filter.c

@ -97,7 +97,7 @@ f2b_filter_create(f2b_config_section_t *config, const char *file) {
goto cleanup;
if ((*(void **) (&filter->append) = dlsym(filter->h, "append")) == NULL)
goto cleanup;
if ((*(void **) (&filter->error) = dlsym(filter->h, "error")) == NULL)
if ((*(void **) (&filter->logcb) = dlsym(filter->h, "logcb")) == NULL)
goto cleanup;
if ((*(void **) (&filter->ready) = dlsym(filter->h, "ready")) == NULL)
goto cleanup;
@ -116,6 +116,8 @@ f2b_filter_create(f2b_config_section_t *config, const char *file) {
goto cleanup;
}
filter->logcb(filter->cfg, f2b_log_mod_cb);
/* try init */
for (param = config->param; param != NULL; param = param->next) {
if (strcmp(param->name, FILTER_LIBRARY_PARAM) == 0)
@ -175,12 +177,6 @@ f2b_filter_match(f2b_filter_t *filter, const char *line, char *buf, size_t buf_s
return filter->match(filter->cfg, line, buf, buf_size);
}
const char *
f2b_filter_error(f2b_filter_t *filter) {
assert(filter != NULL);
return filter->error(filter->cfg);
}
void
f2b_filter_cmd_stats(char *buf, size_t bufsize, f2b_filter_t *filter) {
bool reset = true;
@ -208,7 +204,7 @@ f2b_filter_cmd_reload(char *buf, size_t bufsize, f2b_filter_t *filter) {
assert(filter != NULL);
filter->flush(filter->cfg);
if (f2b_filter_load_file(filter, filter->file))
return;
strlcpy(buf, f2b_filter_error(filter), bufsize);
if (f2b_filter_load_file(filter, filter->file)) {
snprintf(buf, bufsize, "can't reload filter");
}
}

14
src/filter.h

@ -27,8 +27,8 @@ typedef struct f2b_filter_t {
bool (*config) (void *cfg, const char *key, const char *value);
/** dlsym pointer to handler of @a append command */
bool (*append) (void *cfg, const char *pattern);
/** dlsym pointer to handler of @a error command */
char *(*error) (void *cfg);
/** dlsym pointer to handler of @a logcb command */
void (*logcb) (void *cfg, void (*cb)(log_msgtype_t lvl, const char *msg));
/** dlsym pointer to handler of @a ready command */
bool (*ready) (void *cfg);
/** dlsym pointer to handler of @a flush command */
@ -54,18 +54,13 @@ f2b_filter_t * f2b_filter_create (f2b_config_section_t *config, const char *id)
*/
void f2b_filter_destroy (f2b_filter_t *f);
/**
* @brief Get last filter error
* @param f Pointer to filter struct
* @returns Pointer to string with description of last error
*/
const char * f2b_filter_error (f2b_filter_t *f);
/**
* @brief Append pattern to filter
* @param f Pointer to filter struct
* @param pattern Match pattern
* @returns true on success, false on error with setting last error
* @returns true on success, false on error
*/
bool f2b_filter_append(f2b_filter_t *f, const char *pattern);
/**
* @brief Match a line against given filter
@ -80,6 +75,7 @@ bool f2b_filter_match (f2b_filter_t *f, const char *line, char *buf, size_t bufs
/* handlers for csocket commands processing */
/** handler of 'jail $JAIL filter reload' cmd */
void f2b_filter_cmd_reload(char *buf, size_t bufsize, f2b_filter_t *f);
/** handler of 'jail $JAIL filter stats' cmd */
void f2b_filter_cmd_stats (char *buf, size_t bufsize, f2b_filter_t *f);

1
src/log.h

@ -41,6 +41,7 @@ void f2b_log_msg(log_msgtype_t l, const char *fmt, ...)
* @param msg Log message string
*/
void f2b_log_mod_cb(log_msgtype_t l, const char *msg);
/**
* @brief Limit logging messages by importance
* @param level Min level of messages for logging

Loading…
Cancel
Save