Browse Source

* refactor filter's stats() library call

master
Alex 'AdUser' Z 3 years ago
parent
commit
a77d43679e
  1. 15
      src/filter.c
  2. 2
      src/filter.h
  3. 21
      src/filters/filter.c
  4. 12
      src/filters/filter.h
  5. 37
      src/filters/pcre.c
  6. 38
      src/filters/preg.c
  7. 2
      src/source.h

15
src/filter.c

@ -179,23 +179,10 @@ f2b_filter_match(f2b_filter_t *filter, const char *line, char *buf, size_t buf_s
void void
f2b_filter_cmd_stats(char *buf, size_t bufsize, f2b_filter_t *filter) { f2b_filter_cmd_stats(char *buf, size_t bufsize, f2b_filter_t *filter) {
bool reset = true;
char *pattern;
char tmp[256];
const char *fmt =
"- pattern: %s\n"
" matches: %d\n";
int matches;
assert(filter != NULL); assert(filter != NULL);
assert(buf != NULL); assert(buf != NULL);
buf[0] = '\0'; filter->stats(filter->cfg, buf, bufsize);
while (filter->stats(filter->cfg, &matches, &pattern, reset)) {
snprintf(tmp, sizeof(tmp), fmt, pattern, matches);
strlcat(buf, tmp, bufsize);
reset = false;
}
} }
void void

2
src/filter.h

@ -34,7 +34,7 @@ typedef struct f2b_filter_t {
/** dlsym pointer to handler of @a flush command */ /** dlsym pointer to handler of @a flush command */
bool (*flush) (void *cfg); bool (*flush) (void *cfg);
/** dlsym pointer to handler of @a stats command */ /** dlsym pointer to handler of @a stats command */
bool (*stats) (void *cfg, int *matches, char **pattern, bool reset); bool (*stats) (void *cfg, char *buf, size_t bufsize);
/** dlsym pointer to handler of @a match command */ /** dlsym pointer to handler of @a match command */
bool (*match) (void *cfg, const char *line, char *buf, size_t buf_size); bool (*match) (void *cfg, const char *line, char *buf, size_t buf_size);
/** dlsym pointer to handler of @a destroy command */ /** dlsym pointer to handler of @a destroy command */

21
src/filters/filter.c

@ -34,3 +34,24 @@ logcb(cfg_t *cfg, void (*cb)(enum loglevel lvl, const char *msg)) {
cfg->logcb = cb; cfg->logcb = cb;
} }
bool
stats(cfg_t *cfg, char *buf, size_t bufsize) {
char tmp[PATTERN_MAX + 64];
const char *fmt =
"- pattern: %s\n"
" matches: %d\n";
assert(cfg != NULL);
if (buf == NULL || bufsize == 0)
return false;
for (rx_t *rx = cfg->regexps; rx != NULL; rx = rx->next) {
snprintf(tmp, sizeof(tmp), fmt, rx->pattern, rx->matches);
strlcat(buf, tmp, bufsize);
}
return true;
}

12
src/filters/filter.h

@ -93,6 +93,7 @@ enum loglevel {
* Opaque module handler, contains module internal structs * Opaque module handler, contains module internal structs
*/ */
typedef struct _config cfg_t; typedef struct _config cfg_t;
typedef struct _regexp rx_t;
/** /**
* @brief Create instance of module * @brief Create instance of module
@ -129,14 +130,13 @@ extern bool ready(cfg_t *cfg);
*/ */
extern void logcb(cfg_t *cfg, void (*cb)(enum loglevel l, const char *msg)); extern void logcb(cfg_t *cfg, void (*cb)(enum loglevel l, const char *msg));
/** /**
* @brief Fetch match stats by-pattern * @brief Fetch filter match stats
* @param cfg Module handler * @param cfg Module handler
* @param matches Pointer to storage for matches count * @param buf Pointer to storage for stats report
* @param pattern Associated pattern * @param bufsize Available size for report on @a buf pointer
* @param reset Reset to start of statistics (for later calls) * @returns true on success, false on error
* @returns false on no more stats or true otherwise with filling @a matches and @a pattern
*/ */
extern bool stats(cfg_t *cfg, int *matches, char **pattern, bool reset); extern bool stats(cfg_t *cfg, char *buf, size_t bufsize);
/** /**
* @brief Match given line against configured regexps * @brief Match given line against configured regexps
* @param cfg Module handler * @param cfg Module handler

37
src/filters/pcre.c

@ -10,13 +10,13 @@
#define MODNAME "pcre" #define MODNAME "pcre"
#define HOST_REGEX "(?<host>[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})" #define HOST_REGEX "(?<host>[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})"
typedef struct f2b_regex_t { struct _regexp {
struct f2b_regex_t *next; rx_t *next;
char pattern[PATTERN_MAX]; char pattern[PATTERN_MAX];
int matches; int matches;
pcre *regex; pcre *regex;
pcre_extra *data; pcre_extra *data;
} f2b_regex_t; };
struct _config { struct _config {
char id[ID_MAX]; char id[ID_MAX];
@ -24,8 +24,7 @@ struct _config {
bool icase; bool icase;
bool study; bool study;
bool usejit; bool usejit;
f2b_regex_t *regexps; rx_t *regexps;
f2b_regex_t *statp;
}; };
#include "filter.c" #include "filter.c"
@ -73,7 +72,7 @@ config(cfg_t *cfg, const char *key, const char *value) {
bool bool
append(cfg_t *cfg, const char *pattern) { append(cfg_t *cfg, const char *pattern) {
f2b_regex_t *regex = NULL; rx_t *regex = NULL;
int flags = 0; int flags = 0;
size_t bufsize; size_t bufsize;
char *buf = NULL; char *buf = NULL;
@ -98,7 +97,7 @@ append(cfg_t *cfg, const char *pattern) {
strlcat(buf, HOST_REGEX, bufsize); strlcat(buf, HOST_REGEX, bufsize);
strlcat(buf, token + strlen(HOST_TOKEN), bufsize); strlcat(buf, token + strlen(HOST_TOKEN), bufsize);
if ((regex = calloc(1, sizeof(f2b_regex_t))) == NULL) if ((regex = calloc(1, sizeof(rx_t))) == NULL)
return false; return false;
if ((regex->regex = pcre_compile(buf, flags, &errptr, &erroffset, NULL)) == NULL) { if ((regex->regex = pcre_compile(buf, flags, &errptr, &erroffset, NULL)) == NULL) {
@ -135,26 +134,8 @@ ready(cfg_t *cfg) {
return false; return false;
} }
bool
stats(cfg_t *cfg, int *matches, char **pattern, bool reset) {
assert(cfg != NULL);
if (reset)
cfg->statp = cfg->regexps;
if (cfg->statp) {
*matches = cfg->statp->matches;
*pattern = cfg->statp->pattern;
cfg->statp = cfg->statp->next;
return true;
}
return false;
}
bool bool
match(cfg_t *cfg, const char *line, char *buf, size_t buf_size) { match(cfg_t *cfg, const char *line, char *buf, size_t buf_size) {
f2b_regex_t *r = NULL;
enum { OVECSIZE = 30 }; enum { OVECSIZE = 30 };
int ovector[OVECSIZE]; int ovector[OVECSIZE];
int flags = 0; int flags = 0;
@ -164,7 +145,7 @@ match(cfg_t *cfg, const char *line, char *buf, size_t buf_size) {
assert(line != NULL); assert(line != NULL);
assert(buf != NULL); assert(buf != NULL);
for (r = cfg->regexps; r != NULL; r = r->next) { for (rx_t *r = cfg->regexps; r != NULL; r = r->next) {
rc = pcre_exec(r->regex, r->data, line, strlen(line), 0, flags, ovector, OVECSIZE); rc = pcre_exec(r->regex, r->data, line, strlen(line), 0, flags, ovector, OVECSIZE);
if (rc < 0 && rc == PCRE_ERROR_NOMATCH) if (rc < 0 && rc == PCRE_ERROR_NOMATCH)
continue; continue;
@ -188,11 +169,11 @@ match(cfg_t *cfg, const char *line, char *buf, size_t buf_size) {
void void
flush(cfg_t *cfg) { flush(cfg_t *cfg) {
f2b_regex_t *next = NULL, *r = NULL; rx_t *next = NULL;
assert(cfg != NULL); assert(cfg != NULL);
for (r = cfg->regexps; r != NULL; r = next) { for (rx_t *r = cfg->regexps; r != NULL; r = next) {
next = r->next; next = r->next;
#ifdef PCRE_CONFIG_JIT #ifdef PCRE_CONFIG_JIT
if (cfg->study) if (cfg->study)

38
src/filters/preg.c

@ -6,25 +6,25 @@
*/ */
#include <regex.h> #include <regex.h>
#include "../strlcpy.h"
#include "filter.h" #include "filter.h"
#define MODNAME "preg" #define MODNAME "preg"
/* draft */ /* draft */
#define HOST_REGEX "([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})" #define HOST_REGEX "([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})"
typedef struct f2b_regex_t { struct _regexp {
struct f2b_regex_t *next; rx_t *next;
char pattern[PATTERN_MAX]; char pattern[PATTERN_MAX];
int matches; int matches;
regex_t regex; regex_t regex;
} f2b_regex_t; };
struct _config { struct _config {
char id[ID_MAX]; char id[ID_MAX];
bool icase; bool icase;
void (*logcb)(enum loglevel lvl, const char *msg); void (*logcb)(enum loglevel lvl, const char *msg);
f2b_regex_t *regexps; rx_t *regexps;
f2b_regex_t *statp;
}; };
#include "filter.c" #include "filter.c"
@ -57,7 +57,7 @@ config(cfg_t *cfg, const char *key, const char *value) {
bool bool
append(cfg_t *cfg, const char *pattern) { append(cfg_t *cfg, const char *pattern) {
f2b_regex_t *regex = NULL; rx_t *regex = NULL;
int flags = REG_EXTENDED; int flags = REG_EXTENDED;
size_t bufsize; size_t bufsize;
char *buf = NULL; char *buf = NULL;
@ -81,7 +81,7 @@ append(cfg_t *cfg, const char *pattern) {
strlcat(buf, HOST_REGEX, bufsize); strlcat(buf, HOST_REGEX, bufsize);
strlcat(buf, token + strlen(HOST_TOKEN), bufsize); strlcat(buf, token + strlen(HOST_TOKEN), bufsize);
if ((regex = calloc(1, sizeof(f2b_regex_t))) == NULL) if ((regex = calloc(1, sizeof(rx_t))) == NULL)
return false; return false;
if ((ret = regcomp(&regex->regex, buf, flags)) == 0) { if ((ret = regcomp(&regex->regex, buf, flags)) == 0) {
@ -107,26 +107,8 @@ ready(cfg_t *cfg) {
return false; return false;
} }
bool
stats(cfg_t *cfg, int *matches, char **pattern, bool reset) {
assert(cfg != NULL);
if (reset)
cfg->statp = cfg->regexps;
if (cfg->statp) {
*matches = cfg->statp->matches;
*pattern = cfg->statp->pattern;
cfg->statp = cfg->statp->next;
return true;
}
return false;
}
bool bool
match(cfg_t *cfg, const char *line, char *buf, size_t buf_size) { match(cfg_t *cfg, const char *line, char *buf, size_t buf_size) {
f2b_regex_t *r = NULL;
size_t match_len = 0; size_t match_len = 0;
regmatch_t match[2]; regmatch_t match[2];
@ -134,7 +116,7 @@ match(cfg_t *cfg, const char *line, char *buf, size_t buf_size) {
assert(line != NULL); assert(line != NULL);
assert(buf != NULL); assert(buf != NULL);
for (r = cfg->regexps; r != NULL; r = r->next) { for (rx_t *r = cfg->regexps; r != NULL; r = r->next) {
if (regexec(&r->regex, line, 2, &match[0], 0) != 0) if (regexec(&r->regex, line, 2, &match[0], 0) != 0)
continue; continue;
/* matched */ /* matched */
@ -152,11 +134,11 @@ match(cfg_t *cfg, const char *line, char *buf, size_t buf_size) {
void void
flush(cfg_t *cfg) { flush(cfg_t *cfg) {
f2b_regex_t *next = NULL, *r = NULL; rx_t *next = NULL;
assert(cfg != NULL); assert(cfg != NULL);
for (r = cfg->regexps; r != NULL; r = next) { for (rx_t *r = cfg->regexps; r != NULL; r = next) {
next = r->next; next = r->next;
regfree(&r->regex); regfree(&r->regex);
free(r); free(r);

2
src/source.h

@ -32,6 +32,8 @@ typedef struct f2b_source_t {
bool (*start) (void *cfg); bool (*start) (void *cfg);
/** dlsym pointer to handler of @a next command */ /** dlsym pointer to handler of @a next command */
bool (*next) (void *cfg, char *buf, size_t bufsize, bool reset); bool (*next) (void *cfg, char *buf, size_t bufsize, bool reset);
/** dlsym pointer to handler of @a stats command */
bool (*stats) (void *cfg, char *buf, size_t bufsize);
/** dlsym pointer to handler of @a stop command */ /** dlsym pointer to handler of @a stop command */
bool (*stop) (void *cfg); bool (*stop) (void *cfg);
/** dlsym pointer to handler of @a destroy command */ /** dlsym pointer to handler of @a destroy command */

Loading…
Cancel
Save